1 | package CFClient::UI; |
1 | package CFClient::UI; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | |
4 | |
5 | use Scalar::Util; |
5 | use Scalar::Util (); |
|
|
6 | use List::Util (); |
6 | |
7 | |
7 | use CFClient; |
8 | use CFClient; |
8 | |
9 | |
9 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | |
11 | |
11 | our $TOPLEVEL; |
12 | our $ROOT; |
12 | our $BUTTON_STATE; |
13 | our $BUTTON_STATE; |
13 | |
14 | |
14 | # class methods for events |
15 | # class methods for events |
15 | sub feed_sdl_key_down_event { |
16 | sub feed_sdl_key_down_event { |
16 | $FOCUS->key_down ($_[0]) if $FOCUS; |
17 | $FOCUS->key_down ($_[0]) if $FOCUS; |
… | |
… | |
23 | sub feed_sdl_button_down_event { |
24 | sub feed_sdl_button_down_event { |
24 | my ($ev) = @_; |
25 | my ($ev) = @_; |
25 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
26 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
26 | |
27 | |
27 | if (!$BUTTON_STATE) { |
28 | if (!$BUTTON_STATE) { |
28 | my $widget = $TOPLEVEL->find_widget ($x, $y); |
29 | my $widget = $ROOT->find_widget ($x, $y); |
29 | |
30 | |
30 | $GRAB = $widget; |
31 | $GRAB = $widget; |
31 | $GRAB->update if $GRAB; |
32 | $GRAB->update if $GRAB; |
32 | } |
33 | } |
33 | |
34 | |
34 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
35 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
35 | |
36 | |
36 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
37 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
37 | } |
38 | } |
38 | |
39 | |
39 | sub feed_sdl_button_up_event { |
40 | sub feed_sdl_button_up_event { |
40 | my ($ev) = @_; |
41 | my ($ev) = @_; |
41 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
42 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
42 | |
43 | |
43 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
44 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
44 | |
45 | |
45 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
46 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
46 | |
47 | |
47 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
48 | $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
48 | |
49 | |
49 | if (!$BUTTON_STATE) { |
50 | if (!$BUTTON_STATE) { |
50 | my $grab = $GRAB; undef $GRAB; |
51 | my $grab = $GRAB; undef $GRAB; |
51 | $grab->update if $grab; |
52 | $grab->update if $grab; |
52 | $GRAB->update if $GRAB; |
53 | $GRAB->update if $GRAB; |
… | |
… | |
55 | |
56 | |
56 | sub feed_sdl_motion_event { |
57 | sub feed_sdl_motion_event { |
57 | my ($ev) = @_; |
58 | my ($ev) = @_; |
58 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
59 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
59 | |
60 | |
60 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
61 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
61 | |
62 | |
62 | if ($widget != $HOVER) { |
63 | if ($widget != $HOVER) { |
63 | my $hover = $HOVER; $HOVER = $widget; |
64 | my $hover = $HOVER; $HOVER = $widget; |
64 | |
65 | |
65 | $hover->update if $hover; |
66 | $hover->update if $hover && $hover->{can_hover}; |
66 | $HOVER->update if $HOVER; |
67 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
67 | } |
68 | } |
68 | |
69 | |
69 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
70 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
|
|
71 | } |
|
|
72 | |
|
|
73 | # convert position array to integers |
|
|
74 | sub harmonize { |
|
|
75 | my ($vals) = @_; |
|
|
76 | |
|
|
77 | my $rem = 0; |
|
|
78 | |
|
|
79 | for (@$vals) { |
|
|
80 | my $i = int $_ + $rem; |
|
|
81 | $rem += $_ - $i; |
|
|
82 | $_ = $i; |
|
|
83 | } |
70 | } |
84 | } |
71 | |
85 | |
72 | ############################################################################# |
86 | ############################################################################# |
73 | |
87 | |
74 | package CFClient::UI::Base; |
88 | package CFClient::UI::Base; |
… | |
… | |
78 | use SDL::OpenGL; |
92 | use SDL::OpenGL; |
79 | |
93 | |
80 | sub new { |
94 | sub new { |
81 | my $class = shift; |
95 | my $class = shift; |
82 | |
96 | |
83 | bless { |
97 | my $self = bless { |
84 | x => 0, |
98 | x => 0, |
85 | y => 0, |
99 | y => 0, |
86 | z => 0, |
100 | z => 0, |
|
|
101 | w => -1, |
|
|
102 | h => -1, |
87 | @_ |
103 | @_ |
88 | }, $class |
104 | }, $class; |
|
|
105 | |
|
|
106 | for (keys %$self) { |
|
|
107 | if (/^connect_(.*)$/) { |
|
|
108 | $self->connect ($1 => delete $self->{$_}); |
|
|
109 | } |
|
|
110 | } |
|
|
111 | |
|
|
112 | $self |
89 | } |
113 | } |
90 | |
114 | |
91 | sub move { |
115 | sub move { |
92 | my ($self, $x, $y, $z) = @_; |
116 | my ($self, $x, $y, $z) = @_; |
93 | $self->{x} = $x; |
117 | $self->{x} = $x; |
… | |
… | |
102 | sub size_request { |
126 | sub size_request { |
103 | require Carp; |
127 | require Carp; |
104 | Carp::confess "size_request is abtract"; |
128 | Carp::confess "size_request is abtract"; |
105 | } |
129 | } |
106 | |
130 | |
107 | sub size_allocate { |
131 | sub _size_allocate { |
108 | my ($self, $w, $h) = @_; |
132 | my ($self, $x, $y, $w, $h) = @_; |
|
|
133 | |
|
|
134 | $self->{x} = $x; |
|
|
135 | $self->{y} = $y; |
|
|
136 | |
|
|
137 | return unless $self->{w} != $w || $self->{h} != $h; |
109 | |
138 | |
110 | $self->{w} = $w; |
139 | $self->{w} = $w; |
111 | $self->{h} = $h; |
140 | $self->{h} = $h; |
112 | } |
|
|
113 | |
141 | |
114 | # translate global koordinates to local coordinate system |
142 | 1 |
115 | sub translate { |
143 | } |
|
|
144 | |
|
|
145 | sub size_allocate { |
|
|
146 | my ($self, $x, $y, $w, $h) = @_; |
|
|
147 | |
|
|
148 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
149 | } |
|
|
150 | |
|
|
151 | # return top left coordinates |
|
|
152 | sub _topleft { |
116 | my ($self, $x, $y) = @_; |
153 | my ($self, $x, $y) = @_; |
117 | |
154 | |
118 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
155 | $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); |
|
|
156 | } |
|
|
157 | |
|
|
158 | # translate global coordinates to local coordinate system |
|
|
159 | sub coord2local { |
|
|
160 | my ($self, $x, $y) = @_; |
|
|
161 | |
|
|
162 | my ($X, $Y) = $self->_topleft; |
|
|
163 | ($x - $X, $y - $Y) |
|
|
164 | } |
|
|
165 | |
|
|
166 | # translate local coordinates to global coordinate system |
|
|
167 | sub coord2global { |
|
|
168 | my ($self, $x, $y) = @_; |
|
|
169 | |
|
|
170 | my ($X, $Y) = $self->_topleft; |
|
|
171 | ($x + $X, $y + $Y) |
119 | } |
172 | } |
120 | |
173 | |
121 | sub focus_in { |
174 | sub focus_in { |
122 | my ($self) = @_; |
175 | my ($self) = @_; |
123 | |
176 | |
124 | return if $FOCUS == $self; |
177 | return if $FOCUS == $self; |
|
|
178 | return unless $self->{can_focus}; |
125 | |
179 | |
126 | my $focus = $FOCUS; $FOCUS = $self; |
180 | my $focus = $FOCUS; $FOCUS = $self; |
127 | $focus->update if $focus; |
181 | $focus->update if $focus; |
128 | $FOCUS->update; |
182 | $FOCUS->update; |
129 | } |
183 | } |
… | |
… | |
162 | glPushMatrix; |
216 | glPushMatrix; |
163 | glTranslate $self->{x}, $self->{y}, 0; |
217 | glTranslate $self->{x}, $self->{y}, 0; |
164 | $self->_draw; |
218 | $self->_draw; |
165 | glPopMatrix; |
219 | glPopMatrix; |
166 | |
220 | |
167 | if ($self == $HOVER) { |
221 | if ($self == $HOVER && $self->{can_hover}) { |
168 | my ($x, $y) = @$self{qw(x y)}; |
222 | my ($x, $y) = @$self{qw(x y)}; |
169 | |
223 | |
170 | glColor 1, 1, 1, 0.1; |
224 | glColor 0, 0, 1, 0.2; |
171 | glEnable GL_BLEND; |
225 | glEnable GL_BLEND; |
|
|
226 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
172 | glBegin GL_QUADS; |
227 | glBegin GL_QUADS; |
173 | glVertex $x , $y; |
228 | glVertex $x , $y; |
174 | glVertex $x + $self->{w}, $y; |
229 | glVertex $x + $self->{w}, $y; |
175 | glVertex $x + $self->{w}, $y + $self->{h}; |
230 | glVertex $x + $self->{w}, $y + $self->{h}; |
176 | glVertex $x , $y + $self->{h}; |
231 | glVertex $x , $y + $self->{h}; |
… | |
… | |
183 | my ($self) = @_; |
238 | my ($self) = @_; |
184 | |
239 | |
185 | warn "no draw defined for $self\n"; |
240 | warn "no draw defined for $self\n"; |
186 | } |
241 | } |
187 | |
242 | |
188 | sub bbox { |
|
|
189 | my ($self) = @_; |
|
|
190 | my ($w, $h) = $self->size_request; |
|
|
191 | ( |
|
|
192 | $self->{x}, |
|
|
193 | $self->{y}, |
|
|
194 | $self->{x} = $w, |
|
|
195 | $self->{y} = $h |
|
|
196 | ) |
|
|
197 | } |
|
|
198 | |
|
|
199 | sub find_widget { |
243 | sub find_widget { |
200 | my ($self, $x, $y) = @_; |
244 | my ($self, $x, $y) = @_; |
201 | |
245 | |
202 | return $self |
246 | return $self |
203 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
247 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
204 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
248 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
205 | |
249 | |
206 | () |
250 | () |
207 | } |
251 | } |
208 | |
252 | |
209 | sub del_parent { $_[0]->{parent} = undef } |
|
|
210 | |
|
|
211 | sub set_parent { |
253 | sub set_parent { |
212 | my ($self, $par) = @_; |
254 | my ($self, $parent) = @_; |
213 | |
255 | |
214 | $self->{parent} = $par; |
|
|
215 | Scalar::Util::weaken $self->{parent}; |
256 | Scalar::Util::weaken ($self->{parent} = $parent); |
216 | } |
|
|
217 | |
|
|
218 | sub get_parent { |
|
|
219 | $_[0]->{parent} |
|
|
220 | } |
257 | } |
221 | |
258 | |
222 | sub update { |
259 | sub update { |
223 | my ($self) = @_; |
260 | my ($self) = @_; |
224 | |
261 | |
… | |
… | |
258 | my $class = shift; |
295 | my $class = shift; |
259 | |
296 | |
260 | # range [value, low, high, page] |
297 | # range [value, low, high, page] |
261 | |
298 | |
262 | $class->SUPER::new ( |
299 | $class->SUPER::new ( |
263 | bg => [0, 0, 0, 0.4], |
300 | bg => [0, 0, 0, 0.2], |
264 | active_bg => [1, 1, 1], |
301 | active_bg => [1, 1, 1, 0.5], |
265 | @_ |
302 | @_ |
266 | ) |
303 | ) |
267 | } |
304 | } |
268 | |
305 | |
269 | sub _draw { |
306 | sub _draw { |
270 | my ($self) = @_; |
307 | my ($self) = @_; |
271 | |
308 | |
272 | my ($w, $h) = @$self{qw(w h)}; |
309 | my ($w, $h) = @$self{qw(w h)}; |
273 | |
310 | |
|
|
311 | glEnable GL_BLEND; |
|
|
312 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
274 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
313 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
|
|
314 | |
275 | glBegin GL_QUADS; |
315 | glBegin GL_QUADS; |
276 | glVertex 0 , 0; |
316 | glVertex 0 , 0; |
277 | glVertex 0 , $h; |
317 | glVertex 0 , $h; |
278 | glVertex $w, $h; |
318 | glVertex $w, $h; |
279 | glVertex $w, 0; |
319 | glVertex $w, 0; |
280 | glEnd; |
320 | glEnd; |
|
|
321 | |
|
|
322 | glDisable GL_BLEND; |
281 | } |
323 | } |
282 | |
324 | |
283 | ############################################################################# |
325 | ############################################################################# |
284 | |
326 | |
285 | package CFClient::UI::Empty; |
327 | package CFClient::UI::Empty; |
… | |
… | |
308 | |
350 | |
309 | $self |
351 | $self |
310 | } |
352 | } |
311 | |
353 | |
312 | sub add { |
354 | sub add { |
313 | my ($self, $chld, $expand) = @_; |
355 | my ($self, $child) = @_; |
314 | |
356 | |
315 | $chld->{expand} = $expand; |
|
|
316 | $chld->set_parent ($self); |
357 | $child->set_parent ($self); |
|
|
358 | |
|
|
359 | use sort 'stable'; |
317 | |
360 | |
318 | $self->{children} = [ |
361 | $self->{children} = [ |
319 | sort { $a->{z} <=> $b->{z} } |
362 | sort { $a->{z} <=> $b->{z} } |
320 | @{$self->{children}}, $chld |
363 | @{$self->{children}}, $child |
321 | ]; |
364 | ]; |
322 | |
365 | |
323 | $self->size_allocate ($self->{w}, $self->{h}) |
366 | $self->{w} = $self->{h} = -1; |
324 | if $self->{w}; #TODO: check for "realised state" |
367 | $self->update; |
325 | } |
368 | } |
326 | |
369 | |
327 | sub remove { |
370 | sub remove { |
328 | my ($self, $widget) = @_; |
371 | my ($self, $widget) = @_; |
329 | |
372 | |
330 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
373 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
331 | |
374 | |
332 | $self->size_allocate ($self->{w}, $self->{h}); |
375 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
333 | } |
376 | } |
334 | |
377 | |
335 | sub find_widget { |
378 | sub find_widget { |
336 | my ($self, $x, $y) = @_; |
379 | my ($self, $x, $y) = @_; |
337 | |
380 | |
… | |
… | |
390 | sub size_request { |
433 | sub size_request { |
391 | $_[0]{children}[0]->size_request |
434 | $_[0]{children}[0]->size_request |
392 | } |
435 | } |
393 | |
436 | |
394 | sub size_allocate { |
437 | sub size_allocate { |
395 | my ($self, $w, $h) = @_; |
438 | my ($self, $x, $y, $w, $h) = @_; |
396 | |
439 | |
397 | return unless $self->{w} != $w || $self->{h} != $h; |
440 | $self->_size_allocate ($x, $y, $w, $h) or return; |
398 | |
441 | |
399 | $self->SUPER::size_allocate ($w, $h); |
|
|
400 | $self->{children}[0]->size_allocate ($w, $h); |
442 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
401 | } |
443 | } |
402 | |
444 | |
403 | ############################################################################# |
445 | ############################################################################# |
404 | |
446 | |
405 | package CFClient::UI::Window; |
447 | package CFClient::UI::Window; |
… | |
… | |
423 | } |
465 | } |
424 | |
466 | |
425 | sub render_chld { |
467 | sub render_chld { |
426 | my ($self) = @_; |
468 | my ($self) = @_; |
427 | |
469 | |
428 | $self->{texture} = |
470 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
429 | CFClient::Texture->new_from_opengl ( |
471 | glClearColor 0, 0, 0, 1; |
430 | $self->{w}, $self->{h}, sub { $self->child->draw } |
472 | glClear GL_COLOR_BUFFER_BIT; |
431 | ); |
473 | $self->child->draw; |
|
|
474 | }; |
432 | } |
475 | } |
433 | |
476 | |
434 | sub size_allocate { |
477 | sub size_allocate { |
435 | my ($self, $w, $h) = @_; |
478 | my ($self, $x, $y, $w, $h) = @_; |
436 | |
479 | |
437 | return unless $self->{w} != $w || $self->{h} != $h; |
480 | $self->_size_allocate ($x, $y, $w, $h) or return; |
438 | |
481 | |
439 | $self->{w} = $w; |
|
|
440 | $self->{h} = $h; |
|
|
441 | |
|
|
442 | $self->child->size_allocate ($w, $h); |
482 | $self->child->size_allocate (0, 0, $w, $h); |
443 | |
483 | |
444 | $self->render_chld; |
484 | $self->render_chld; |
445 | } |
485 | } |
446 | |
486 | |
447 | sub _draw { |
487 | sub _draw { |
… | |
… | |
451 | |
491 | |
452 | my $tex = $self->{texture} |
492 | my $tex = $self->{texture} |
453 | or return; |
493 | or return; |
454 | |
494 | |
455 | glEnable GL_BLEND; |
495 | glEnable GL_BLEND; |
|
|
496 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
456 | glEnable GL_TEXTURE_2D; |
497 | glEnable GL_TEXTURE_2D; |
457 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
498 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
458 | |
499 | |
459 | $tex->draw_quad (0, 0, $w, $h); |
500 | $tex->draw_quad (0, 0, $w, $h); |
460 | |
501 | |
… | |
… | |
479 | |
520 | |
480 | map { $_ + 4 } $chld->size_request; |
521 | map { $_ + 4 } $chld->size_request; |
481 | } |
522 | } |
482 | |
523 | |
483 | sub size_allocate { |
524 | sub size_allocate { |
484 | my ($self, $w, $h) = @_; |
525 | my ($self, $x, $y, $w, $h) = @_; |
485 | |
526 | |
486 | return unless $self->{w} != $w || $self->{h} != $h; |
527 | $self->_size_allocate ($x, $y, $w, $h) or return; |
487 | |
528 | |
488 | $self->{w} = $w; |
|
|
489 | $self->{h} = $h; |
|
|
490 | |
|
|
491 | $self->child->size_allocate ($w - 4, $h - 4); |
529 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
492 | $self->child->move (2, 2); |
|
|
493 | } |
530 | } |
494 | |
531 | |
495 | sub _draw { |
532 | sub _draw { |
496 | my ($self) = @_; |
533 | my ($self) = @_; |
497 | |
534 | |
… | |
… | |
520 | |
557 | |
521 | my @tex = |
558 | my @tex = |
522 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
559 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
523 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
560 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
524 | |
561 | |
|
|
562 | sub new { |
|
|
563 | my $class = shift; |
|
|
564 | |
|
|
565 | # TODO: user_x, user_y, overwrite moveto? |
|
|
566 | |
|
|
567 | $class->SUPER::new ( |
|
|
568 | bg => [1, 1, 1, 1], |
|
|
569 | border_bg => [1, 1, 1, 1], |
|
|
570 | border => $::FONTSIZE * 0.8, |
|
|
571 | @_ |
|
|
572 | ) |
|
|
573 | } |
|
|
574 | |
525 | sub size_request { |
575 | sub size_request { |
526 | my ($self) = @_; |
576 | my ($self) = @_; |
527 | |
577 | |
|
|
578 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
|
|
579 | |
528 | my ($w, $h) = $self->SUPER::size_request; |
580 | my ($w, $h) = $self->SUPER::size_request; |
529 | |
581 | |
530 | $h += $tex[1]->{h}; |
582 | ( |
531 | $h += $tex[4]->{h}; |
583 | $w + $self->{border} * 2, |
532 | $w += $tex[2]->{w}; |
584 | $h + $self->{border} * 2, |
533 | $w += $tex[3]->{w}; |
585 | ) |
534 | |
|
|
535 | ($w, $h) |
|
|
536 | } |
586 | } |
537 | |
587 | |
538 | sub size_allocate { |
588 | sub size_allocate { |
539 | my ($self, $w, $h) = @_; |
589 | my ($self, $x, $y, $w, $h) = @_; |
540 | |
590 | |
541 | return unless $self->{w} != $w || $self->{h} != $h; |
591 | $self->_size_allocate ($x, $y, $w, $h) or return; |
542 | |
592 | |
543 | $self->SUPER::size_allocate ($w, $h); |
593 | $h -= List::Util::max 0, $self->{border} * 2; |
|
|
594 | $w -= List::Util::max 0, $self->{border} * 2; |
544 | |
595 | |
545 | $h -= $tex[1]->{h}; |
596 | $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); |
546 | $h -= $tex[4]->{h}; |
597 | } |
547 | $w -= $tex[2]->{w}; |
|
|
548 | $w -= $tex[3]->{w}; |
|
|
549 | |
598 | |
550 | $h = $h < 0 ? 0 : $h; |
599 | sub button_down { |
551 | $w = $w < 0 ? 0 : $w; |
600 | my ($self, $ev, $x, $y) = @_; |
552 | |
601 | |
553 | my $child = $self->child; |
602 | if ($x < $self->{w} && $x >= $self->{w} - $self->{border} |
|
|
603 | && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { |
554 | |
604 | |
555 | $child->size_allocate ($w, $h); |
605 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
556 | $child->move ($tex[3]->{w}, $tex[1]->{h}); |
606 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
|
|
607 | |
|
|
608 | $self->{motion} = sub { |
|
|
609 | my ($ev, $x, $y) = @_; |
|
|
610 | |
|
|
611 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
612 | |
|
|
613 | $self->{user_w} = $bw + $x - $ox; |
|
|
614 | $self->{user_h} = $bh + $y - $oy; |
|
|
615 | $self->update; |
|
|
616 | }; |
|
|
617 | |
|
|
618 | } elsif ($x >= 0 && $x < $self->{w} |
|
|
619 | && $y >= 0 && $y < $self->{border}) { |
|
|
620 | |
|
|
621 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
622 | my ($bx, $by) = ($self->{x}, $self->{y}); |
|
|
623 | |
|
|
624 | $self->{motion} = sub { |
|
|
625 | my ($ev, $x, $y) = @_; |
|
|
626 | |
|
|
627 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
628 | |
|
|
629 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
|
|
630 | $self->update; |
|
|
631 | }; |
|
|
632 | } |
|
|
633 | } |
|
|
634 | |
|
|
635 | sub button_up { |
|
|
636 | my ($self, $ev, $x, $y) = @_; |
|
|
637 | |
|
|
638 | delete $self->{motion}; |
|
|
639 | } |
|
|
640 | |
|
|
641 | sub mouse_motion { |
|
|
642 | my ($self, $ev, $x, $y) = @_; |
|
|
643 | |
|
|
644 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
557 | } |
645 | } |
558 | |
646 | |
559 | sub _draw { |
647 | sub _draw { |
560 | my ($self) = @_; |
648 | my ($self) = @_; |
561 | |
649 | |
562 | my ($w, $h) = ($self->{w}, $self->{h}); |
650 | my ($w, $h ) = ($self->{w}, $self->{h}); |
563 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
651 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
564 | |
652 | |
565 | glEnable GL_BLEND; |
653 | glEnable GL_BLEND; |
|
|
654 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
566 | glEnable GL_TEXTURE_2D; |
655 | glEnable GL_TEXTURE_2D; |
567 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
568 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
656 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
569 | |
657 | |
570 | my $top = $tex[1]; |
658 | glColor @{ $self->{border_bg} }; |
571 | $top->draw_quad (0, 0, $w, $top->{h}); |
659 | $tex[1]->draw_quad (0, 0, $w, $self->{border}); |
572 | |
660 | $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); |
573 | my $left = $tex[3]; |
661 | $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); |
574 | $left->draw_quad (0, $top->{h}, $left->{w}, $ch); |
662 | $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); |
575 | |
|
|
576 | my $right = $tex[2]; |
|
|
577 | $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch); |
|
|
578 | |
|
|
579 | my $bottom = $tex[4]; |
|
|
580 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
|
|
581 | |
663 | |
582 | my $bg = $tex[0]; |
664 | my $bg = $tex[0]; |
583 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
|
|
584 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
585 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
|
|
586 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
|
|
587 | |
665 | |
|
|
666 | # TODO: repeat texture not scale |
588 | my $rep_x = $cw / $bg->{w}; |
667 | my $rep_x = $cw / $bg->{w}; |
589 | my $rep_y = $ch / $bg->{h}; |
668 | my $rep_y = $ch / $bg->{h}; |
590 | |
669 | |
|
|
670 | glColor @{ $self->{bg} }; |
|
|
671 | |
|
|
672 | $bg->{s} = $rep_x; |
|
|
673 | $bg->{t} = $rep_y; |
|
|
674 | $bg->{wrap_mode} = 1; |
591 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
675 | $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); |
592 | |
676 | |
|
|
677 | glDisable GL_TEXTURE_2D; |
593 | glDisable GL_BLEND; |
678 | glDisable GL_BLEND; |
594 | glDisable GL_TEXTURE_2D; |
|
|
595 | |
679 | |
596 | $self->child->draw; |
680 | $self->child->draw; |
597 | |
|
|
598 | } |
681 | } |
599 | |
682 | |
600 | ############################################################################# |
683 | ############################################################################# |
601 | |
684 | |
602 | package CFClient::UI::Table; |
685 | package CFClient::UI::Table; |
603 | |
686 | |
604 | our @ISA = CFClient::UI::Base::; |
687 | our @ISA = CFClient::UI::Base::; |
605 | |
688 | |
|
|
689 | use List::Util qw(max sum); |
|
|
690 | |
606 | use SDL::OpenGL; |
691 | use SDL::OpenGL; |
607 | |
692 | |
|
|
693 | sub new { |
|
|
694 | my $class = shift; |
|
|
695 | |
|
|
696 | $class->SUPER::new ( |
|
|
697 | col_expand => [], |
|
|
698 | @_ |
|
|
699 | ) |
|
|
700 | } |
|
|
701 | |
608 | sub add { |
702 | sub add { |
609 | my ($self, $x, $y, $chld) = @_; |
703 | my ($self, $x, $y, $child) = @_; |
610 | my $old_chld = $self->{children}[$y][$x]; |
|
|
611 | |
704 | |
|
|
705 | $child->set_parent ($self); |
612 | $self->{children}[$y][$x] = $chld; |
706 | $self->{children}[$y][$x] = $child; |
613 | $chld->set_parent ($self); |
707 | |
|
|
708 | $self->{w} = $self->{h} = -1; |
614 | $self->update; |
709 | $self->update; |
615 | } |
710 | } |
616 | |
711 | |
617 | sub max_row_height { |
712 | # TODO: move to container class maybe? send childs a signal on removal? |
|
|
713 | sub clear { |
618 | my ($self, $row) = @_; |
714 | my ($self) = @_; |
619 | |
715 | |
620 | my $hs = 0; |
716 | delete $self->{children}; |
621 | for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { |
717 | $self->update; |
|
|
718 | } |
|
|
719 | |
|
|
720 | sub get_wh { |
|
|
721 | my ($self) = @_; |
|
|
722 | |
|
|
723 | my (@w, @h); |
|
|
724 | |
|
|
725 | for my $y (0 .. $#{$self->{children}}) { |
622 | my $c = $self->{children}->[$row]->[$xi]; |
726 | my $row = $self->{children}[$y] |
623 | if ($c) { |
727 | or next; |
|
|
728 | |
|
|
729 | for my $x (0 .. $#$row) { |
|
|
730 | my $widget = $row->[$x] |
|
|
731 | or next; |
624 | my ($w, $h) = $c->size_request; |
732 | my ($w, $h) = $widget->size_request; |
625 | if ($hs < $h) { $hs = $h } |
733 | |
|
|
734 | $w[$x] = max $w[$x], $w; |
|
|
735 | $h[$y] = max $h[$y], $h; |
626 | } |
736 | } |
627 | } |
737 | } |
628 | return $hs; |
|
|
629 | } |
|
|
630 | |
738 | |
631 | sub max_col_width { |
739 | (\@w, \@h) |
|
|
740 | } |
|
|
741 | |
|
|
742 | sub size_request { |
632 | my ($self, $col) = @_; |
743 | my ($self) = @_; |
633 | |
744 | |
|
|
745 | my ($ws, $hs) = $self->get_wh; |
|
|
746 | |
|
|
747 | ( |
|
|
748 | (sum @$ws), |
|
|
749 | (sum @$hs), |
|
|
750 | ) |
|
|
751 | } |
|
|
752 | |
|
|
753 | sub size_allocate { |
|
|
754 | my ($self, $x, $y, $w, $h) = @_; |
|
|
755 | |
|
|
756 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
757 | |
|
|
758 | my ($ws, $hs) = $self->get_wh; |
|
|
759 | |
|
|
760 | my $req_w = sum @$ws; |
|
|
761 | my $req_h = sum @$hs; |
|
|
762 | |
|
|
763 | # TODO: nicer code && do row_expand |
|
|
764 | my @col_expand = @{$self->{col_expand}}; |
|
|
765 | @col_expand = (1) x @$ws unless @col_expand; |
|
|
766 | my $col_expand = (sum @col_expand) || 1; |
|
|
767 | |
|
|
768 | # linearly scale sizes |
|
|
769 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
|
|
770 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
|
|
771 | |
|
|
772 | CFClient::UI::harmonize $ws; |
|
|
773 | CFClient::UI::harmonize $hs; |
|
|
774 | |
|
|
775 | my $y; |
|
|
776 | |
|
|
777 | for my $r (0 .. $#{$self->{children}}) { |
|
|
778 | my $row = $self->{children}[$r] |
|
|
779 | or next; |
|
|
780 | |
634 | my $ws = 0; |
781 | my $x = 0; |
635 | for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { |
782 | my $row_h = $hs->[$r]; |
636 | my $c = ($self->{children}->[$yi] || [])->[$col]; |
783 | |
637 | if ($c) { |
784 | for my $c (0 .. $#$row) { |
638 | my ($w, $h) = $c->size_request; |
785 | my $col_w = $ws->[$c]; |
639 | if ($ws < $w) { $ws = $w } |
786 | |
|
|
787 | if (my $widget = $row->[$c]) { |
|
|
788 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
|
|
789 | } |
|
|
790 | |
|
|
791 | $x += $col_w; |
640 | } |
792 | } |
|
|
793 | |
|
|
794 | $y += $row_h; |
641 | } |
795 | } |
642 | return $ws; |
796 | |
643 | } |
797 | } |
|
|
798 | |
|
|
799 | sub find_widget { |
|
|
800 | my ($self, $x, $y) = @_; |
|
|
801 | |
|
|
802 | $x -= $self->{x}; |
|
|
803 | $y -= $self->{y}; |
|
|
804 | |
|
|
805 | my $res; |
|
|
806 | |
|
|
807 | for (grep $_, map @$_, grep $_, @{ $self->{children} }) { |
|
|
808 | $res = $_->find_widget ($x, $y) |
|
|
809 | and return $res; |
|
|
810 | } |
|
|
811 | |
|
|
812 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
|
|
813 | } |
|
|
814 | |
|
|
815 | sub _draw { |
|
|
816 | my ($self) = @_; |
|
|
817 | |
|
|
818 | for (grep $_, @{$self->{children}}) { |
|
|
819 | $_->draw for grep $_, @$_; |
|
|
820 | } |
|
|
821 | } |
|
|
822 | |
|
|
823 | ############################################################################# |
|
|
824 | |
|
|
825 | package CFClient::UI::HBox; |
|
|
826 | |
|
|
827 | # TODO: wrap into common Box base class |
|
|
828 | |
|
|
829 | our @ISA = CFClient::UI::Container::; |
644 | |
830 | |
645 | sub size_request { |
831 | sub size_request { |
646 | my ($self) = @_; |
832 | my ($self) = @_; |
647 | |
833 | |
|
|
834 | my @alloc = map [$_->size_request], @{$self->{children}}; |
|
|
835 | |
|
|
836 | ( |
|
|
837 | (List::Util::sum map $_->[0], @alloc), |
|
|
838 | (List::Util::max map $_->[1], @alloc), |
|
|
839 | ) |
|
|
840 | } |
|
|
841 | |
|
|
842 | sub size_allocate { |
|
|
843 | my ($self, $x, $y, $w, $h) = @_; |
|
|
844 | |
|
|
845 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
846 | |
648 | my ($hs, $ws) = (0, 0); |
847 | ($h, $w) = ($w, $h); |
649 | |
848 | |
650 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
849 | my $children = $self->{children}; |
651 | $hs += $self->max_row_height ($yi); |
|
|
652 | } |
|
|
653 | |
850 | |
654 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
851 | my @h = map +($_->size_request)[0], @$children; |
655 | my $wm = 0; |
852 | |
656 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
853 | my $req_h = List::Util::sum @h; |
657 | $wm += $self->max_col_width ($xi) |
854 | |
|
|
855 | if ($req_h > $h) { |
|
|
856 | # ah well, not enough space |
|
|
857 | $_ *= $h / $req_h for @h; |
|
|
858 | } else { |
|
|
859 | my $exp = List::Util::sum map $_->{expand}, @$children; |
|
|
860 | $exp ||= 1; |
|
|
861 | |
|
|
862 | for (0 .. $#$children) { |
|
|
863 | my $child = $children->[$_]; |
|
|
864 | |
|
|
865 | my $alloc_h = $h[$_]; |
|
|
866 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
|
|
867 | $h[$_] = $alloc_h; |
658 | } |
868 | } |
659 | if ($ws < $wm) { $ws = $wm } |
|
|
660 | } |
869 | } |
661 | |
870 | |
662 | return ($ws, $hs); |
871 | CFClient::UI::harmonize \@h; |
663 | } |
|
|
664 | |
|
|
665 | sub _draw { |
|
|
666 | my ($self) = @_; |
|
|
667 | |
872 | |
668 | my $y = 0; |
873 | my $y = 0; |
669 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
874 | for (0 .. $#$children) { |
|
|
875 | my $child = $children->[$_]; |
670 | my $x = 0; |
876 | my $h = $h[$_]; |
|
|
877 | $child->size_allocate ($y, 0, $h, $w); |
671 | |
878 | |
672 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
879 | $y += $h; |
673 | |
|
|
674 | my $c = $self->{children}->[$yi]->[$xi]; |
|
|
675 | if ($c) { |
|
|
676 | $c->move ($x, $y, 0); #TODO: Move to size_request |
|
|
677 | $c->draw if $c; |
|
|
678 | } |
|
|
679 | |
|
|
680 | $x += $self->max_col_width ($xi); |
|
|
681 | } |
|
|
682 | |
|
|
683 | $y += $self->max_row_height ($yi); |
|
|
684 | } |
880 | } |
685 | } |
881 | } |
686 | |
882 | |
687 | ############################################################################# |
883 | ############################################################################# |
688 | |
884 | |
689 | package CFClient::UI::VBox; |
885 | package CFClient::UI::VBox; |
690 | |
886 | |
|
|
887 | # TODO: wrap into common Box base class |
|
|
888 | |
691 | our @ISA = CFClient::UI::Container::; |
889 | our @ISA = CFClient::UI::Container::; |
692 | |
|
|
693 | use SDL::OpenGL; |
|
|
694 | |
890 | |
695 | sub size_request { |
891 | sub size_request { |
696 | my ($self) = @_; |
892 | my ($self) = @_; |
697 | |
893 | |
698 | my @alloc = map [$_->size_request], @{$self->{children}}; |
894 | my @alloc = map [$_->size_request], @{$self->{children}}; |
… | |
… | |
702 | (List::Util::sum map $_->[1], @alloc), |
898 | (List::Util::sum map $_->[1], @alloc), |
703 | ) |
899 | ) |
704 | } |
900 | } |
705 | |
901 | |
706 | sub size_allocate { |
902 | sub size_allocate { |
707 | my ($self, $w, $h) = @_; |
903 | my ($self, $x, $y, $w, $h) = @_; |
708 | |
904 | |
709 | return unless $self->{w} != $w || $self->{h} != $h; |
905 | $self->_size_allocate ($x, $y, $w, $h); |
710 | |
|
|
711 | $self->{w} = $w; |
|
|
712 | $self->{h} = $h; |
|
|
713 | |
|
|
714 | return unless $self->{h}; |
|
|
715 | |
906 | |
716 | my $children = $self->{children}; |
907 | my $children = $self->{children}; |
717 | |
908 | |
718 | my @h = map +($_->size_request)[1], @$children; |
909 | my @h = map +($_->size_request)[1], @$children; |
719 | |
910 | |
720 | my $req_h = List::Util::sum @h; |
911 | my $req_h = List::Util::sum @h; |
721 | |
912 | |
722 | if ($req_h > $h) { |
913 | if ($req_h > $h) { |
723 | # ah well, not enough space |
914 | # ah well, not enough space |
724 | $_ = $h[$_] * $h / $req_h for @h; |
915 | $_ *= $h / $req_h for @h; |
725 | } else { |
916 | } else { |
726 | my @exp = grep $_->{expand}, @$children; |
917 | my $exp = List::Util::sum map $_->{expand}, @$children; |
727 | @exp = @$children unless @exp; |
918 | $exp ||= 1; |
728 | |
919 | |
729 | my %exp = map +($_ => 1), @exp; |
|
|
730 | |
|
|
731 | for (0 .. $#$children) { |
920 | for (0 .. $#$children) { |
732 | my $child = $children->[$_]; |
921 | my $child = $children->[$_]; |
733 | |
922 | |
734 | my $alloc_h = $h[$_]; |
923 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
735 | $alloc_h += ($h - $req_h) / @exp if $exp{$child}; |
|
|
736 | $h[$_] = $alloc_h; |
|
|
737 | } |
924 | } |
738 | } |
925 | } |
|
|
926 | |
|
|
927 | CFClient::UI::harmonize \@h; |
739 | |
928 | |
740 | my $y = 0; |
929 | my $y = 0; |
741 | for (0 .. $#$children) { |
930 | for (0 .. $#$children) { |
742 | my $child = $children->[$_]; |
931 | my $child = $children->[$_]; |
743 | my $h = $h[$_]; |
932 | my $h = $h[$_]; |
744 | $child->move (0, $y); |
|
|
745 | $child->size_allocate ($w, $h); |
933 | $child->size_allocate (0, $y, $w, $h); |
746 | |
934 | |
747 | $y += $h; |
935 | $y += $h; |
748 | } |
936 | } |
749 | } |
937 | } |
750 | |
938 | |
… | |
… | |
758 | |
946 | |
759 | sub new { |
947 | sub new { |
760 | my ($class, %arg) = @_; |
948 | my ($class, %arg) = @_; |
761 | |
949 | |
762 | my $self = $class->SUPER::new ( |
950 | my $self = $class->SUPER::new ( |
763 | fg => [1, 1, 1], |
951 | fg => [1, 1, 1], |
764 | height => $::FONTSIZE, |
952 | fontsize => $::FONTSIZE, |
765 | text => "", |
953 | text => "", |
766 | align => -1, |
954 | align => -1, |
|
|
955 | valign => -1, |
|
|
956 | padding => 2, |
767 | layout => new CFClient::Layout, |
957 | layout => new CFClient::Layout, |
768 | %arg |
958 | %arg |
769 | ); |
959 | ); |
770 | |
960 | |
771 | $self->set_text ($self->{text}); |
961 | $self->set_text ($self->{text}); |
772 | |
962 | |
… | |
… | |
788 | |
978 | |
789 | $self->{text} = $text; |
979 | $self->{text} = $text; |
790 | $self->{layout}->set_markup ($text); |
980 | $self->{layout}->set_markup ($text); |
791 | |
981 | |
792 | delete $self->{texture}; |
982 | delete $self->{texture}; |
|
|
983 | # $self->{w} = $self->{h} = -1; |
793 | $self->update; |
984 | $self->update; |
794 | } |
985 | } |
795 | |
986 | |
796 | sub get_text { |
987 | sub get_text { |
797 | my ($self, $text) = @_; |
988 | my ($self, $text) = @_; |
… | |
… | |
801 | |
992 | |
802 | sub size_request { |
993 | sub size_request { |
803 | my ($self) = @_; |
994 | my ($self) = @_; |
804 | |
995 | |
805 | $self->{layout}->set_width; |
996 | $self->{layout}->set_width; |
806 | $self->{layout}->set_height ($self->{height}); |
997 | $self->{layout}->set_height ($self->{fontsize}); |
807 | $self->{layout}->size |
998 | my ($w, $h) = $self->{layout}->size; |
808 | # if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
999 | |
809 | # ( |
1000 | ( |
810 | # $self->{texture}{w}, |
1001 | $w + $self->{padding} * 2, |
811 | # $self->{texture}{h}, |
1002 | $h + $self->{padding} * 2, |
812 | # ) |
1003 | ) |
813 | # } else { |
|
|
814 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h}; |
|
|
815 | # |
|
|
816 | # ($w, $h) |
|
|
817 | # } |
|
|
818 | } |
1004 | } |
819 | |
1005 | |
820 | sub size_allocate { |
1006 | sub size_allocate { |
821 | my ($self, $w, $h) = @_; |
1007 | my ($self, $x, $y, $w, $h) = @_; |
822 | |
1008 | |
823 | return unless $self->{w} != $w || $self->{h} != $h; |
1009 | $self->_size_allocate ($x, $y, $w, $h) or return; |
824 | |
1010 | |
825 | $self->SUPER::size_allocate ($w, $h); |
|
|
826 | delete $self->{texture}; |
1011 | delete $self->{texture}; |
827 | } |
1012 | } |
828 | |
1013 | |
829 | sub update { |
1014 | sub update { |
830 | my ($self) = @_; |
1015 | my ($self) = @_; |
… | |
… | |
836 | sub _draw { |
1021 | sub _draw { |
837 | my ($self) = @_; |
1022 | my ($self) = @_; |
838 | |
1023 | |
839 | my $tex = $self->{texture} ||= do { |
1024 | my $tex = $self->{texture} ||= do { |
840 | $self->{layout}->set_width ($self->{w}); |
1025 | $self->{layout}->set_width ($self->{w}); |
|
|
1026 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
841 | new_from_layout CFClient::Texture $self->{layout}; |
1027 | new_from_layout CFClient::Texture $self->{layout} |
842 | }; |
1028 | }; |
843 | |
1029 | |
844 | glEnable GL_BLEND; |
1030 | glEnable GL_BLEND; |
|
|
1031 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
845 | glEnable GL_TEXTURE_2D; |
1032 | glEnable GL_TEXTURE_2D; |
846 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
847 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1033 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
848 | |
1034 | |
849 | glColor @{$self->{fg}}; |
1035 | glColor @{$self->{fg}}; |
850 | |
1036 | |
851 | my $x = |
1037 | my $x = |
852 | $self->{align} < 0 ? 0 |
1038 | $self->{align} < 0 ? $self->{padding} |
853 | : $self->{align} > 0 ? $self->{w} - $self->{texture}{w} |
1039 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
854 | : ($self->{w} + $self->{texture}{w}) * 0.5; |
1040 | : ($self->{w} - $tex->{w}) * 0.5; |
855 | |
1041 | |
|
|
1042 | my $y = |
|
|
1043 | $self->{valign} < 0 ? $self->{padding} |
|
|
1044 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
|
|
1045 | : ($self->{h} - $tex->{h}) * 0.5; |
|
|
1046 | |
856 | $tex->draw_quad ($x, 0); |
1047 | $tex->draw_quad (int $x, int $y); |
857 | |
1048 | |
|
|
1049 | glDisable GL_TEXTURE_2D; |
858 | glDisable GL_BLEND; |
1050 | glDisable GL_BLEND; |
859 | glDisable GL_TEXTURE_2D; |
|
|
860 | } |
1051 | } |
861 | |
1052 | |
862 | ############################################################################# |
1053 | ############################################################################# |
863 | |
1054 | |
864 | package CFClient::UI::Entry; |
1055 | package CFClient::UI::Entry; |
… | |
… | |
871 | sub new { |
1062 | sub new { |
872 | my $class = shift; |
1063 | my $class = shift; |
873 | |
1064 | |
874 | $class->SUPER::new ( |
1065 | $class->SUPER::new ( |
875 | fg => [1, 1, 1], |
1066 | fg => [1, 1, 1], |
876 | bg => [0, 0, 0, 0.4], |
1067 | bg => [0, 0, 0, 0.2], |
877 | active_bg => [1, 1, 1], |
1068 | active_bg => [1, 1, 1, 0.5], |
878 | active_fg => [0, 0, 0], |
1069 | active_fg => [0, 0, 0], |
|
|
1070 | can_hover => 1, |
|
|
1071 | can_focus => 1, |
|
|
1072 | valign => 0, |
879 | @_ |
1073 | @_ |
880 | ) |
1074 | ) |
881 | } |
1075 | } |
882 | |
1076 | |
883 | sub _set_text { |
1077 | sub _set_text { |
884 | my ($self, $text) = @_; |
1078 | my ($self, $text) = @_; |
885 | |
1079 | |
|
|
1080 | my $old_text = $self->{text}; |
|
|
1081 | |
886 | $self->{last_activity} = $::NOW; |
1082 | $self->{last_activity} = $::NOW; |
887 | |
1083 | |
888 | $self->{text} = $text; |
1084 | $self->{text} = $text; |
889 | $self->{layout}->set_width ($self->{w}); |
1085 | $self->{layout}->set_width ($self->{w}); |
|
|
1086 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
890 | |
1087 | |
891 | $text =~ s/./*/g if $self->{hidden}; |
1088 | $text =~ s/./*/g if $self->{hidden}; |
892 | |
1089 | |
893 | |
|
|
894 | $self->{layout}->set_markup ($self->escape_text ($text)); |
1090 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
895 | |
1091 | |
896 | $text = substr $text, 0, $self->{cursor}; |
1092 | $text = substr $text, 0, $self->{cursor}; |
897 | utf8::encode $text; |
1093 | utf8::encode $text; |
898 | |
1094 | |
899 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
1095 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
1096 | |
|
|
1097 | $self->emit (changed => $self->{text}) # XXX: is this the right place to do this? |
|
|
1098 | if $old_text ne $self->{text}; |
900 | } |
1099 | } |
901 | |
1100 | |
902 | sub size_request { |
1101 | sub size_request { |
903 | my ($self) = @_; |
1102 | my ($self) = @_; |
904 | |
1103 | |
… | |
… | |
906 | |
1105 | |
907 | ($w + 1, $h) # add 1 for cursor |
1106 | ($w + 1, $h) # add 1 for cursor |
908 | } |
1107 | } |
909 | |
1108 | |
910 | sub size_allocate { |
1109 | sub size_allocate { |
911 | my ($self, $w, $h) = @_; |
1110 | my ($self, $x, $y, $w, $h) = @_; |
912 | |
1111 | |
913 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
914 | |
|
|
915 | $self->SUPER::size_allocate ($w, $h); |
1112 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
916 | |
1113 | |
917 | $self->_set_text ($self->{text}); |
1114 | $self->_set_text ($self->{text}); |
918 | } |
1115 | } |
919 | |
1116 | |
920 | sub set_text { |
1117 | sub set_text { |
… | |
… | |
941 | substr $text, $self->{cursor}, 1, ""; |
1138 | substr $text, $self->{cursor}, 1, ""; |
942 | } elsif ($sym == SDLK_LEFT) { |
1139 | } elsif ($sym == SDLK_LEFT) { |
943 | --$self->{cursor} if $self->{cursor}; |
1140 | --$self->{cursor} if $self->{cursor}; |
944 | } elsif ($sym == SDLK_RIGHT) { |
1141 | } elsif ($sym == SDLK_RIGHT) { |
945 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1142 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
|
|
1143 | } elsif ($sym == SDLK_HOME) { |
|
|
1144 | $self->{cursor} = 0; |
|
|
1145 | } elsif ($sym == SDLK_END) { |
|
|
1146 | $self->{cursor} = length $text; |
|
|
1147 | } elsif ($sym == SDLK_ESCAPE) { |
|
|
1148 | $self->emit ('escape'); |
946 | } elsif ($uni) { |
1149 | } elsif ($uni) { |
947 | substr $text, $self->{cursor}++, 0, chr $uni; |
1150 | substr $text, $self->{cursor}++, 0, chr $uni; |
948 | } |
1151 | } |
949 | |
1152 | |
950 | $self->_set_text ($text); |
1153 | $self->_set_text ($text); |
… | |
… | |
965 | $self->SUPER::button_down ($ev, $x, $y); |
1168 | $self->SUPER::button_down ($ev, $x, $y); |
966 | |
1169 | |
967 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
1170 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
968 | |
1171 | |
969 | # byte-index to char-index |
1172 | # byte-index to char-index |
970 | my $text = $self->{layout}; |
1173 | my $text = $self->{text}; |
971 | utf8::encode $text; |
1174 | utf8::encode $text; |
972 | $self->{cursor} = length substr $text, 0, $idx; |
1175 | $self->{cursor} = length substr $text, 0, $idx; |
973 | |
1176 | |
974 | $self->_set_text ($self->{text}); |
1177 | $self->_set_text ($self->{text}); |
975 | $self->update; |
1178 | $self->update; |
… | |
… | |
990 | $self->{fg} = $self->{active_fg}; |
1193 | $self->{fg} = $self->{active_fg}; |
991 | } else { |
1194 | } else { |
992 | glColor @{$self->{bg}}; |
1195 | glColor @{$self->{bg}}; |
993 | } |
1196 | } |
994 | |
1197 | |
|
|
1198 | glEnable GL_BLEND; |
|
|
1199 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
995 | glBegin GL_QUADS; |
1200 | glBegin GL_QUADS; |
996 | glVertex 0 , 0; |
1201 | glVertex 0 , 0; |
997 | glVertex 0 , $self->{h}; |
1202 | glVertex 0 , $self->{h}; |
998 | glVertex $self->{w}, $self->{h}; |
1203 | glVertex $self->{w}, $self->{h}; |
999 | glVertex $self->{w}, 0; |
1204 | glVertex $self->{w}, 0; |
1000 | glEnd; |
1205 | glEnd; |
|
|
1206 | glDisable GL_BLEND; |
1001 | |
1207 | |
1002 | $self->SUPER::_draw; |
1208 | $self->SUPER::_draw; |
1003 | |
1209 | |
1004 | #TODO: force update every cursor change :( |
1210 | #TODO: force update every cursor change :( |
1005 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
1211 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
… | |
… | |
1009 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
1215 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
1010 | glEnd; |
1216 | glEnd; |
1011 | } |
1217 | } |
1012 | } |
1218 | } |
1013 | |
1219 | |
1014 | ############################################################################# |
|
|
1015 | |
|
|
1016 | package CFClient::UI::Slider; |
1220 | package CFClient::UI::LineEntry; |
1017 | |
1221 | |
1018 | use strict; |
1222 | our @ISA = CFClient::UI::Entry::; |
1019 | |
1223 | |
|
|
1224 | use SDL; |
1020 | use SDL::OpenGL; |
1225 | use SDL::OpenGL; |
1021 | use SDL::OpenGL::Constants; |
|
|
1022 | |
1226 | |
1023 | our @ISA = CFClient::UI::DrawBG::; |
1227 | sub key_down { |
1024 | |
|
|
1025 | sub size_request { |
|
|
1026 | my ($self) = @_; |
1228 | my ($self, $ev) = @_; |
1027 | |
1229 | |
1028 | my $w = 50; |
1230 | my $sym = $ev->key_sym; |
1029 | my $h = 10; |
|
|
1030 | |
1231 | |
1031 | $self->{vertical} ? ($h, $w) : ($w, $h) |
1232 | if ($sym == SDLK_RETURN) { |
|
|
1233 | $self->emit (activate => $self->get_text); |
|
|
1234 | $self->update; |
|
|
1235 | |
|
|
1236 | } else { |
|
|
1237 | $self->SUPER::key_down ($ev); |
|
|
1238 | } |
|
|
1239 | |
1032 | } |
1240 | } |
|
|
1241 | |
|
|
1242 | ############################################################################# |
|
|
1243 | |
|
|
1244 | package CFClient::UI::Button; |
|
|
1245 | |
|
|
1246 | our @ISA = CFClient::UI::Label::; |
|
|
1247 | |
|
|
1248 | use SDL; |
|
|
1249 | use SDL::OpenGL; |
|
|
1250 | |
|
|
1251 | my @tex = |
|
|
1252 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1253 | qw(b1_button_active.png); |
1033 | |
1254 | |
1034 | sub new { |
1255 | sub new { |
1035 | my $class = shift; |
1256 | my $class = shift; |
1036 | |
1257 | |
|
|
1258 | $class->SUPER::new ( |
|
|
1259 | padding => 4, |
|
|
1260 | fg => [1, 1, 1], |
|
|
1261 | bg => [1, 1, 1, 0.2], |
|
|
1262 | active_fg => [1, 1, 0], |
|
|
1263 | can_hover => 1, |
|
|
1264 | @_ |
|
|
1265 | ) |
|
|
1266 | } |
|
|
1267 | |
|
|
1268 | sub button_up { |
|
|
1269 | my ($self, $ev, $x, $y) = @_; |
|
|
1270 | |
|
|
1271 | if ($x >= 0 && $x < $self->{w} |
|
|
1272 | && $y >= 0 && $y < $self->{h}) { |
|
|
1273 | $self->emit ("activate"); |
|
|
1274 | } |
|
|
1275 | } |
|
|
1276 | |
|
|
1277 | sub _draw { |
|
|
1278 | my ($self) = @_; |
|
|
1279 | |
|
|
1280 | local $self->{fg} = $self->{fg}; |
|
|
1281 | my $tex = $tex[0]; |
|
|
1282 | |
|
|
1283 | glEnable GL_BLEND; |
|
|
1284 | glEnable GL_TEXTURE_2D; |
|
|
1285 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1286 | |
|
|
1287 | if ($GRAB == $self) { |
|
|
1288 | $self->{fg} = $self->{active_fg}; |
|
|
1289 | } |
|
|
1290 | |
|
|
1291 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
1292 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1293 | |
|
|
1294 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
|
|
1295 | |
|
|
1296 | glDisable GL_TEXTURE_2D; |
|
|
1297 | glDisable GL_BLEND; |
|
|
1298 | |
|
|
1299 | $self->SUPER::_draw; |
|
|
1300 | } |
|
|
1301 | |
|
|
1302 | ############################################################################# |
|
|
1303 | |
|
|
1304 | package CFClient::UI::CheckBox; |
|
|
1305 | |
|
|
1306 | our @ISA = CFClient::UI::DrawBG::; |
|
|
1307 | |
|
|
1308 | my @tex = |
|
|
1309 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1310 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
|
|
1311 | |
|
|
1312 | use SDL; |
|
|
1313 | use SDL::OpenGL; |
|
|
1314 | |
|
|
1315 | sub new { |
|
|
1316 | my $class = shift; |
|
|
1317 | |
|
|
1318 | $class->SUPER::new ( |
|
|
1319 | padding => 2, |
|
|
1320 | fg => [1, 1, 1], |
|
|
1321 | active_fg => [1, 1, 0], |
|
|
1322 | state => 0, |
|
|
1323 | can_hover => 1, |
|
|
1324 | @_ |
|
|
1325 | ) |
|
|
1326 | } |
|
|
1327 | |
|
|
1328 | sub size_request { |
|
|
1329 | my ($self) = @_; |
|
|
1330 | |
|
|
1331 | ($self->{padding} * 2 + 6) x 2 |
|
|
1332 | } |
|
|
1333 | |
|
|
1334 | sub size_allocate { |
|
|
1335 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1336 | |
|
|
1337 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
1338 | } |
|
|
1339 | |
|
|
1340 | sub button_down { |
|
|
1341 | my ($self, $ev, $x, $y) = @_; |
|
|
1342 | |
|
|
1343 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
|
|
1344 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
|
|
1345 | $self->{state} = !$self->{state}; |
|
|
1346 | $self->emit (changed => $self->{state}); |
|
|
1347 | } |
|
|
1348 | } |
|
|
1349 | |
|
|
1350 | sub _draw { |
|
|
1351 | my ($self) = @_; |
|
|
1352 | |
|
|
1353 | $self->SUPER::_draw; |
|
|
1354 | |
|
|
1355 | glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; |
|
|
1356 | |
|
|
1357 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
|
|
1358 | |
|
|
1359 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
|
|
1360 | |
|
|
1361 | glEnable GL_BLEND; |
|
|
1362 | glEnable GL_TEXTURE_2D; |
|
|
1363 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1364 | |
|
|
1365 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
1366 | |
|
|
1367 | $tex->draw_quad (0, 0, $s, $s); |
|
|
1368 | |
|
|
1369 | glDisable GL_TEXTURE_2D; |
|
|
1370 | glDisable GL_BLEND; |
|
|
1371 | } |
|
|
1372 | |
|
|
1373 | ############################################################################# |
|
|
1374 | |
|
|
1375 | package CFClient::UI::Slider; |
|
|
1376 | |
|
|
1377 | use strict; |
|
|
1378 | |
|
|
1379 | use SDL::OpenGL; |
|
|
1380 | |
|
|
1381 | our @ISA = CFClient::UI::DrawBG::; |
|
|
1382 | |
|
|
1383 | my @tex = |
|
|
1384 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1385 | qw(s1_slider.png s1_slider_bg.png); |
|
|
1386 | |
|
|
1387 | sub new { |
|
|
1388 | my $class = shift; |
|
|
1389 | |
1037 | # range [value, low, high, page] |
1390 | # range [value, low, high, page] |
1038 | |
1391 | |
|
|
1392 | # TODO: 0-width page |
|
|
1393 | # TODO: req_w/h are wrong with vertical |
|
|
1394 | # TODO: calculations are off |
1039 | $class->SUPER::new ( |
1395 | my $self = $class->SUPER::new ( |
1040 | fg => [1, 1, 1], |
1396 | fg => [1, 1, 1], |
1041 | active_fg => [0, 0, 0], |
1397 | active_fg => [0, 0, 0], |
1042 | range => [0, 0, 100, 10], |
1398 | range => [0, 0, 100, 10], |
|
|
1399 | req_w => 40, |
|
|
1400 | req_h => 13, |
1043 | vertical => 1, |
1401 | vertical => 0, |
|
|
1402 | can_hover => 1, |
|
|
1403 | inner_pad => 5, |
1044 | @_ |
1404 | @_ |
1045 | ) |
1405 | ); |
|
|
1406 | |
|
|
1407 | $self |
|
|
1408 | } |
|
|
1409 | |
|
|
1410 | sub size_request { |
|
|
1411 | my ($self) = @_; |
|
|
1412 | |
|
|
1413 | my $w = $self->{req_w}; |
|
|
1414 | my $h = $self->{req_h}; |
|
|
1415 | |
|
|
1416 | $self->{vertical} ? ($h, $w) : ($w, $h) |
1046 | } |
1417 | } |
1047 | |
1418 | |
1048 | sub button_down { |
1419 | sub button_down { |
1049 | my ($self, $ev, $x, $y) = @_; |
1420 | my ($self, $ev, $x, $y) = @_; |
1050 | |
1421 | |
… | |
… | |
1058 | if ($GRAB == $self) { |
1429 | if ($GRAB == $self) { |
1059 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1430 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1060 | |
1431 | |
1061 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
1432 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
1062 | |
1433 | |
|
|
1434 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
|
|
1435 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
1436 | |
|
|
1437 | $x -= $inner_pad_px; # substract the padding |
1063 | $x = $x * ($hi - $lo) / $w + $lo; |
1438 | $x = $x * ($hi - $lo) / $inner_w + $lo; |
1064 | $x = $lo if $x < $lo; |
1439 | $x = $lo if $x < $lo; |
1065 | $x = $hi - $page if $x > $hi - $page; |
1440 | $x = $hi - $page if $x > $hi - $page; |
1066 | $self->{range}[0] = $x; |
1441 | $self->{range}[0] = $x; |
1067 | |
1442 | |
1068 | $self->emit (changed => $x); |
1443 | $self->emit (changed => $x); |
1069 | $self->update; |
1444 | $self->update; |
1070 | } |
1445 | } |
|
|
1446 | } |
|
|
1447 | |
|
|
1448 | # the inner_* stuff is for generating a padding for the slider handle, |
|
|
1449 | # so that the handle doesn't leave the texture. This calculation isn't 100% |
|
|
1450 | # correct propably, but it does the job for now |
|
|
1451 | sub _calc_inner_pad_px { |
|
|
1452 | my ($self, $w) = @_; |
|
|
1453 | ($w / 100) * $self->{inner_pad} # % to pixels |
1071 | } |
1454 | } |
1072 | |
1455 | |
1073 | sub _draw { |
1456 | sub _draw { |
1074 | my ($self) = @_; |
1457 | my ($self) = @_; |
1075 | |
1458 | |
… | |
… | |
1089 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1472 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1090 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1473 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1091 | |
1474 | |
1092 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1475 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1093 | |
1476 | |
|
|
1477 | $hi = $value + 1 if $lo == $hi; |
|
|
1478 | |
|
|
1479 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
|
|
1480 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
1481 | |
1094 | $page = int $page * $w / ($hi - $lo); |
1482 | $page = int $page * $inner_w / ($hi - $lo); |
1095 | $value = int +($value - $lo) * $w / ($hi - $lo); |
1483 | $value = int +($value - $lo) * $inner_w / ($hi - $lo); |
1096 | |
1484 | |
1097 | $w -= $page; |
1485 | $w -= $page; |
1098 | $page &= ~1; |
1486 | $page &= ~1; |
1099 | glTranslate $page * 0.5, 0, 0; |
1487 | glTranslate $page * 0.5, 0, 0; |
|
|
1488 | $page ||= 2; |
1100 | |
1489 | |
1101 | glColor @$fg; |
|
|
1102 | glBegin GL_LINES; |
|
|
1103 | glVertex 0, 0; glVertex 0, $h; |
|
|
1104 | glVertex $w - 1, 0; glVertex $w - 1, $h; |
|
|
1105 | glVertex 0, $h * 0.5; glVertex $w, $h * 0.5; |
|
|
1106 | glEnd; |
|
|
1107 | |
|
|
1108 | my $knob_a = $value - $page * 0.5; |
1490 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
1109 | my $knob_b = $value + $page * 0.5; |
1491 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
1110 | |
1492 | |
1111 | glBegin GL_QUADS; |
1493 | glEnable GL_BLEND; |
1112 | glColor @$fg; |
1494 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1113 | glVertex $knob_a, 0; |
1495 | glEnable GL_TEXTURE_2D; |
1114 | glVertex $knob_a, $h; |
1496 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1115 | glVertex $knob_b, $h; |
|
|
1116 | glVertex $knob_b, 0; |
|
|
1117 | |
1497 | |
1118 | if ($knob_a < $knob_b - 2) { |
1498 | # draw background |
1119 | glColor @$bg; |
1499 | $tex[1]->draw_quad (0, 0, $w, $h); |
1120 | glVertex $knob_a + 1, 1; |
1500 | |
1121 | glVertex $knob_a + 1, $h - 1; |
1501 | # draw handle |
1122 | glVertex $knob_b - 1, $h - 1; |
1502 | $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); |
1123 | glVertex $knob_b - 1, 1; |
1503 | |
|
|
1504 | glDisable GL_BLEND; |
|
|
1505 | glDisable GL_TEXTURE_2D; |
|
|
1506 | } |
|
|
1507 | |
|
|
1508 | ############################################################################# |
|
|
1509 | |
|
|
1510 | package CFClient::UI::TextView; |
|
|
1511 | |
|
|
1512 | our @ISA = CFClient::UI::HBox::; |
|
|
1513 | |
|
|
1514 | use SDL::OpenGL; |
|
|
1515 | |
|
|
1516 | sub new { |
|
|
1517 | my $class = shift; |
|
|
1518 | |
|
|
1519 | my $self = $class->SUPER::new ( |
|
|
1520 | req_w => $::WIDTH / 6, |
|
|
1521 | req_h => $::HEIGHT / 6, |
|
|
1522 | fontsize => $::FONTSIZE, |
|
|
1523 | @_, |
|
|
1524 | |
|
|
1525 | layout => (new CFClient::Layout), |
|
|
1526 | par => [], |
|
|
1527 | height => 0, |
|
|
1528 | children => [ |
|
|
1529 | (new CFClient::UI::Empty expand => 1), |
|
|
1530 | (new CFClient::UI::Slider vertical => 1), |
|
|
1531 | ], |
|
|
1532 | ); |
|
|
1533 | |
|
|
1534 | $self->{children}[1]->connect (changed => sub { |
|
|
1535 | $self->update; |
|
|
1536 | }); |
|
|
1537 | |
|
|
1538 | $self |
|
|
1539 | } |
|
|
1540 | |
|
|
1541 | sub set_fontsize { |
|
|
1542 | my ($self, $fontsize) = @_; |
|
|
1543 | |
|
|
1544 | $self->{fontsize} = $fontsize; |
|
|
1545 | $self->reflow; |
|
|
1546 | } |
|
|
1547 | |
|
|
1548 | sub text_height { |
|
|
1549 | my ($self, $text) = @_; |
|
|
1550 | |
|
|
1551 | my $layout = $self->{layout}; |
|
|
1552 | |
|
|
1553 | $layout->set_height ($self->{fontsize}); |
|
|
1554 | $layout->set_width ($self->{w}); |
|
|
1555 | $layout->set_text ($text); |
1124 | } |
1556 | |
1125 | glEnd; |
1557 | ($layout->size)[1] |
|
|
1558 | } |
|
|
1559 | |
|
|
1560 | sub reflow { |
|
|
1561 | my ($self) = @_; |
|
|
1562 | |
|
|
1563 | $self->{need_reflow}++; |
|
|
1564 | $self->update; |
|
|
1565 | } |
|
|
1566 | |
|
|
1567 | sub size_request { |
|
|
1568 | my ($self) = @_; |
|
|
1569 | |
|
|
1570 | ($self->{req_w}, $self->{req_h}) |
|
|
1571 | } |
|
|
1572 | |
|
|
1573 | sub size_allocate { |
|
|
1574 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1575 | |
|
|
1576 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
|
|
1577 | |
|
|
1578 | $self->{layout}->set_height ($self->{fontsize}); |
|
|
1579 | $self->{layout}->set_width ($self->{w}); |
|
|
1580 | |
|
|
1581 | $self->reflow; |
|
|
1582 | } |
|
|
1583 | |
|
|
1584 | sub add_paragraph { |
|
|
1585 | my ($self, $color, $text) = @_; |
|
|
1586 | |
|
|
1587 | #TODO: intelligently "reformat" paragraph |
|
|
1588 | |
|
|
1589 | my $height = $self->text_height ($text); |
|
|
1590 | |
|
|
1591 | $self->{height} += $height; |
|
|
1592 | |
|
|
1593 | push @{$self->{par}}, [$height, $color, $text]; |
|
|
1594 | |
|
|
1595 | $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; |
|
|
1596 | $self->{children}[1]->update; |
|
|
1597 | } |
|
|
1598 | |
|
|
1599 | sub update { |
|
|
1600 | my ($self) = @_; |
|
|
1601 | |
|
|
1602 | $self->SUPER::update; |
|
|
1603 | |
|
|
1604 | return unless $self->{h} > 0; |
|
|
1605 | |
|
|
1606 | delete $self->{texture}; |
|
|
1607 | |
|
|
1608 | $ROOT->on_refresh ($self, sub { |
|
|
1609 | if (delete $self->{need_reflow}) { |
|
|
1610 | my $height = 0; |
|
|
1611 | |
|
|
1612 | $height += $_->[0] = $self->text_height ($_->[2]) |
|
|
1613 | for @{$self->{par}}; |
|
|
1614 | |
|
|
1615 | $self->{height} = $height; |
|
|
1616 | |
|
|
1617 | $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
|
|
1618 | |
|
|
1619 | delete $self->{texture}; |
|
|
1620 | } |
|
|
1621 | |
|
|
1622 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
|
|
1623 | glClearColor 0, 0, 0, 1; |
|
|
1624 | glClear GL_COLOR_BUFFER_BIT; |
|
|
1625 | |
|
|
1626 | glEnable GL_BLEND; |
|
|
1627 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1628 | glEnable GL_TEXTURE_2D; |
|
|
1629 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1630 | |
|
|
1631 | my $top = int $self->{children}[1]{range}[0]; |
|
|
1632 | |
|
|
1633 | my $y0 = $top; |
|
|
1634 | my $y1 = $top + $self->{h}; |
|
|
1635 | |
|
|
1636 | my $y = 0; |
|
|
1637 | |
|
|
1638 | my $layout = $self->{layout}; |
|
|
1639 | |
|
|
1640 | for my $par (@{$self->{par}}) { |
|
|
1641 | my $h = $par->[0]; |
|
|
1642 | |
|
|
1643 | if ($y0 < $y + $h && $y < $y1) { |
|
|
1644 | $layout->set_text ($par->[2]); |
|
|
1645 | |
|
|
1646 | glColor @{ $par->[1] }; |
|
|
1647 | my ($W, $H) = $layout->size; |
|
|
1648 | CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); |
|
|
1649 | } |
|
|
1650 | |
|
|
1651 | $y += $h; |
|
|
1652 | } |
|
|
1653 | |
|
|
1654 | glDisable GL_TEXTURE_2D; |
|
|
1655 | glDisable GL_BLEND; |
|
|
1656 | }; |
|
|
1657 | }); |
|
|
1658 | } |
|
|
1659 | |
|
|
1660 | sub _draw { |
|
|
1661 | my ($self) = @_; |
|
|
1662 | |
|
|
1663 | if ($self->{texture}) { |
|
|
1664 | glEnable GL_TEXTURE_2D; |
|
|
1665 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1666 | $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); |
|
|
1667 | glDisable GL_TEXTURE_2D; |
|
|
1668 | } |
|
|
1669 | |
|
|
1670 | $self->{children}[1]->draw; |
|
|
1671 | |
1126 | } |
1672 | } |
1127 | |
1673 | |
1128 | ############################################################################# |
1674 | ############################################################################# |
1129 | |
1675 | |
1130 | package CFClient::UI::MapWidget; |
1676 | package CFClient::UI::MapWidget; |
… | |
… | |
1133 | |
1679 | |
1134 | use List::Util qw(min max); |
1680 | use List::Util qw(min max); |
1135 | |
1681 | |
1136 | use SDL; |
1682 | use SDL; |
1137 | use SDL::OpenGL; |
1683 | use SDL::OpenGL; |
1138 | use SDL::OpenGL::Constants; |
|
|
1139 | |
1684 | |
1140 | our @ISA = CFClient::UI::Base::; |
1685 | our @ISA = CFClient::UI::Base::; |
1141 | |
1686 | |
1142 | sub new { |
1687 | sub new { |
1143 | my $class = shift; |
1688 | my $class = shift; |
1144 | |
1689 | |
1145 | $class->SUPER::new ( |
1690 | $class->SUPER::new ( |
1146 | z => -1, |
1691 | z => -1, |
|
|
1692 | can_focus => 1, |
1147 | list => (glGenLists 1), |
1693 | list => (glGenLists 1), |
1148 | @_ |
1694 | @_ |
1149 | ) |
1695 | ) |
1150 | } |
1696 | } |
1151 | |
1697 | |
1152 | sub key_down { |
1698 | sub key_down { |
… | |
… | |
1154 | } |
1700 | } |
1155 | |
1701 | |
1156 | sub key_up { |
1702 | sub key_up { |
1157 | } |
1703 | } |
1158 | |
1704 | |
|
|
1705 | sub button_down { |
|
|
1706 | my ($self, $ev, $x, $y) = @_; |
|
|
1707 | |
|
|
1708 | $self->focus_in; |
|
|
1709 | |
|
|
1710 | if ($ev->button == 2) { |
|
|
1711 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
1712 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
|
|
1713 | |
|
|
1714 | $self->{motion} = sub { |
|
|
1715 | my ($ev, $x, $y) = @_; |
|
|
1716 | |
|
|
1717 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
1718 | |
|
|
1719 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
|
|
1720 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
|
|
1721 | |
|
|
1722 | $self->update; |
|
|
1723 | }; |
|
|
1724 | } |
|
|
1725 | } |
|
|
1726 | |
|
|
1727 | sub button_up { |
|
|
1728 | my ($self, $ev, $x, $y) = @_; |
|
|
1729 | |
|
|
1730 | delete $self->{motion}; |
|
|
1731 | } |
|
|
1732 | |
|
|
1733 | sub mouse_motion { |
|
|
1734 | my ($self, $ev, $x, $y) = @_; |
|
|
1735 | |
|
|
1736 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
|
|
1737 | } |
|
|
1738 | |
1159 | sub size_request { |
1739 | sub size_request { |
1160 | ( |
1740 | ( |
1161 | 1 + int $::WIDTH / 32, |
1741 | 1 + 32 * int $::WIDTH / 32, |
1162 | 1 + int $::HEIGHT / 32, |
1742 | 1 + 32 * int $::HEIGHT / 32, |
1163 | ) |
1743 | ) |
1164 | } |
1744 | } |
1165 | |
1745 | |
1166 | sub update { |
1746 | sub update { |
1167 | my ($self) = @_; |
1747 | my ($self) = @_; |
1168 | |
1748 | |
1169 | $self->{need_update} = 1; |
1749 | $self->{need_update} = 1; |
|
|
1750 | $self->SUPER::update; |
1170 | } |
1751 | } |
1171 | |
1752 | |
1172 | sub _draw { |
1753 | sub draw { |
1173 | my ($self) = @_; |
1754 | my ($self) = @_; |
1174 | |
1755 | |
1175 | if (delete $self->{need_update}) { |
1756 | if (delete $self->{need_update}) { |
1176 | glNewList $self->{list}, GL_COMPILE; |
1757 | glNewList $self->{list}, GL_COMPILE; |
1177 | |
1758 | |
1178 | my $mx = $::CONN->{mapx}; |
1759 | if ($::MAP) { |
1179 | my $my = $::CONN->{mapy}; |
|
|
1180 | |
|
|
1181 | my $map = $::CONN->{map}; |
|
|
1182 | |
|
|
1183 | my ($xofs, $yofs); |
|
|
1184 | |
|
|
1185 | my $sw = 1 + int $::WIDTH / 32; |
1760 | my $sw = int $::WIDTH / 32; |
1186 | my $sh = 1 + int $::HEIGHT / 32; |
1761 | my $sh = int $::HEIGHT / 32; |
1187 | |
1762 | |
1188 | if ($::CONN->{mapw} > $sw) { |
1763 | my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
1189 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
1764 | my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
1190 | } else { |
1765 | |
1191 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
1766 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
|
|
1767 | |
|
|
1768 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
|
|
1769 | |
|
|
1770 | if ($::CFG->{fow_enable}) { |
|
|
1771 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
|
|
1772 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
|
|
1773 | glConvolutionFilter2D |
|
|
1774 | GL_CONVOLUTION_2D, |
|
|
1775 | GL_ALPHA, |
|
|
1776 | 3, 3, |
|
|
1777 | GL_ALPHA, GL_FLOAT, |
|
|
1778 | pack "f*", |
|
|
1779 | 0.1, 0.1, 0.1, |
|
|
1780 | 0.1, 0.2, 0.1, |
|
|
1781 | 0.1, 0.1, 0.1, |
|
|
1782 | ; |
|
|
1783 | glEnable GL_CONVOLUTION_2D; |
|
|
1784 | } |
|
|
1785 | |
|
|
1786 | my $tex = new CFClient::Texture |
|
|
1787 | w => $w, |
|
|
1788 | h => $h, |
|
|
1789 | data => $data, |
|
|
1790 | internalformat => GL_ALPHA, |
|
|
1791 | format => GL_ALPHA; |
|
|
1792 | |
|
|
1793 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
|
|
1794 | |
|
|
1795 | glEnable GL_BLEND; |
|
|
1796 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1797 | glEnable GL_TEXTURE_2D; |
|
|
1798 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1799 | |
|
|
1800 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1801 | $tex->draw_quad (0, 0, $w * 32, $h * 32); |
|
|
1802 | |
|
|
1803 | glDisable GL_TEXTURE_2D; |
|
|
1804 | glDisable GL_BLEND; |
|
|
1805 | } |
|
|
1806 | |
|
|
1807 | # HACK BEGIN |
|
|
1808 | { |
|
|
1809 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
1810 | my ($w, $h) = (250, 250); |
|
|
1811 | |
|
|
1812 | glEnable GL_BLEND; |
|
|
1813 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1814 | glEnable GL_TEXTURE_2D; |
|
|
1815 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1816 | |
|
|
1817 | CFClient::Texture->new ( |
|
|
1818 | w => $w, |
|
|
1819 | h => $h, |
|
|
1820 | data => $::MAP->mapmap ($w, $h), |
|
|
1821 | type => GL_UNSIGNED_INT_8_8_8_8_REV |
|
|
1822 | )->draw_quad (100, 100); |
|
|
1823 | |
|
|
1824 | glDisable GL_TEXTURE_2D; |
|
|
1825 | glDisable GL_BLEND; |
|
|
1826 | } |
|
|
1827 | # HACK END |
1192 | } |
1828 | } |
1193 | |
1829 | |
1194 | if ($::CONN->{maph} > $sh) { |
|
|
1195 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
|
|
1196 | } else { |
|
|
1197 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
|
|
1198 | } |
|
|
1199 | |
|
|
1200 | glEnable GL_TEXTURE_2D; |
|
|
1201 | glEnable GL_BLEND; |
|
|
1202 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1203 | |
|
|
1204 | my $sw4 = ($sw + 3) & ~3; |
|
|
1205 | my $darkness = "\x00" x ($sw4 * $sh); |
|
|
1206 | |
|
|
1207 | for my $x (0 .. $sw - 1) { |
|
|
1208 | my $row = $map->[$x + $xofs]; |
|
|
1209 | for my $y (0 .. $sh - 1) { |
|
|
1210 | |
|
|
1211 | my $cell = $row->[$y + $yofs] |
|
|
1212 | or next; |
|
|
1213 | |
|
|
1214 | my $dark = $cell->[0]; |
|
|
1215 | if ($dark < 0) { |
|
|
1216 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
|
|
1217 | } else { |
|
|
1218 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
|
|
1219 | } |
|
|
1220 | |
|
|
1221 | for my $num (grep $_, @$cell[1,2,3]) { |
|
|
1222 | my $tex = $::CONN->{face}[$num]{texture} || next; |
|
|
1223 | |
|
|
1224 | my ($w, $h) = @$tex{qw(w h)}; |
|
|
1225 | |
|
|
1226 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
|
|
1227 | } |
|
|
1228 | } |
|
|
1229 | } |
|
|
1230 | |
|
|
1231 | # if (1) { # higher quality darkness |
|
|
1232 | # $lighting =~ s/(.)/$1$1$1/gs; |
|
|
1233 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
|
|
1234 | # |
|
|
1235 | # $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear"); |
|
|
1236 | # |
|
|
1237 | # $lighting = $pb->get_pixels; |
|
|
1238 | # $lighting =~ s/(.)../$1/gs; |
|
|
1239 | # } |
|
|
1240 | |
|
|
1241 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1242 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1243 | |
|
|
1244 | $darkness = new CFClient::Texture |
|
|
1245 | width => $sw4, |
|
|
1246 | height => $sh, |
|
|
1247 | data => $darkness, |
|
|
1248 | internalformat => GL_ALPHA, |
|
|
1249 | format => GL_ALPHA; |
|
|
1250 | |
|
|
1251 | glColor 0.45, 0.45, 0.45, 1; |
|
|
1252 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
|
|
1253 | |
|
|
1254 | glDisable GL_TEXTURE_2D; |
|
|
1255 | glDisable GL_BLEND; |
|
|
1256 | |
|
|
1257 | glEndList; |
1830 | glEndList; |
1258 | } |
1831 | } |
1259 | |
1832 | |
|
|
1833 | glPushMatrix; |
1260 | glCallList $self->{list}; |
1834 | glCallList $self->{list}; |
|
|
1835 | glPopMatrix; |
|
|
1836 | |
|
|
1837 | if ($FOCUS != $self) { |
|
|
1838 | glColor 64/255, 64/255, 64/255; |
|
|
1839 | glLogicOp GL_AND; |
|
|
1840 | glEnable GL_COLOR_LOGIC_OP; |
|
|
1841 | glBegin GL_QUADS; |
|
|
1842 | glVertex 0, 0; |
|
|
1843 | glVertex 0, $::HEIGHT; |
|
|
1844 | glVertex $::WIDTH, $::HEIGHT; |
|
|
1845 | glVertex $::WIDTH, 0; |
|
|
1846 | glEnd; |
|
|
1847 | glDisable GL_COLOR_LOGIC_OP; |
|
|
1848 | } |
1261 | } |
1849 | } |
1262 | |
1850 | |
1263 | my %DIR = ( |
1851 | my %DIR = ( |
1264 | SDLK_KP8, [1, "north"], |
1852 | SDLK_KP8, [1, "north"], |
1265 | SDLK_KP9, [2, "northeast"], |
1853 | SDLK_KP9, [2, "northeast"], |
… | |
… | |
1281 | |
1869 | |
1282 | my $mod = $ev->key_mod; |
1870 | my $mod = $ev->key_mod; |
1283 | my $sym = $ev->key_sym; |
1871 | my $sym = $ev->key_sym; |
1284 | |
1872 | |
1285 | if ($sym == SDLK_KP5) { |
1873 | if ($sym == SDLK_KP5) { |
1286 | $::CONN->send ("command stay fire"); |
1874 | $::CONN->user_send ("command stay fire"); |
|
|
1875 | } elsif ($sym == SDLK_a) { |
|
|
1876 | $::CONN->user_send ("command apply"); |
|
|
1877 | } elsif ($sym == SDLK_QUOTE) { |
|
|
1878 | $self->emit ('activate_console'); |
|
|
1879 | } elsif ($sym == SDLK_SLASH) { |
|
|
1880 | $self->emit ('activate_console' => '/'); |
1287 | } elsif (exists $DIR{$sym}) { |
1881 | } elsif (exists $DIR{$sym}) { |
1288 | if ($mod & KMOD_SHIFT) { |
1882 | if ($mod & KMOD_SHIFT) { |
1289 | $self->{shft}++; |
1883 | $self->{shft}++; |
1290 | $::CONN->send ("command fire $DIR{$sym}[0]"); |
1884 | $::CONN->user_send ("command fire $DIR{$sym}[0]"); |
1291 | } elsif ($mod & KMOD_CTRL) { |
1885 | } elsif ($mod & KMOD_CTRL) { |
1292 | $self->{ctrl}++; |
1886 | $self->{ctrl}++; |
1293 | $::CONN->send ("command run $DIR{$sym}[0]"); |
1887 | $::CONN->user_send ("command run $DIR{$sym}[0]"); |
1294 | } else { |
1888 | } else { |
1295 | $::CONN->send ("command $DIR{$sym}[1]"); |
1889 | $::CONN->user_send ("command $DIR{$sym}[1]"); |
1296 | } |
1890 | } |
1297 | } |
1891 | } |
1298 | } |
1892 | } |
1299 | |
1893 | |
1300 | sub key_up { |
1894 | sub key_up { |
… | |
… | |
1302 | |
1896 | |
1303 | my $mod = $ev->key_mod; |
1897 | my $mod = $ev->key_mod; |
1304 | my $sym = $ev->key_sym; |
1898 | my $sym = $ev->key_sym; |
1305 | |
1899 | |
1306 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
1900 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
1307 | $::CONN->send ("command fire_stop"); |
1901 | $::CONN->user_send ("command fire_stop"); |
1308 | } |
1902 | } |
1309 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
1903 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
1310 | $::CONN->send ("command run_stop"); |
1904 | $::CONN->user_send ("command run_stop"); |
1311 | } |
1905 | } |
1312 | } |
1906 | } |
1313 | |
1907 | |
1314 | ############################################################################# |
1908 | ############################################################################# |
1315 | |
1909 | |
… | |
… | |
1353 | glPopMatrix; |
1947 | glPopMatrix; |
1354 | } |
1948 | } |
1355 | |
1949 | |
1356 | ############################################################################# |
1950 | ############################################################################# |
1357 | |
1951 | |
1358 | package CFClient::UI::Toplevel; |
1952 | package CFClient::UI::Flopper; |
|
|
1953 | |
|
|
1954 | our @ISA = CFClient::UI::Button::; |
|
|
1955 | |
|
|
1956 | sub new { |
|
|
1957 | my $class = shift; |
|
|
1958 | |
|
|
1959 | my $self = $class->SUPER::new ( |
|
|
1960 | state => 0, |
|
|
1961 | connect_activate => \&toggle_flopper, |
|
|
1962 | @_ |
|
|
1963 | ); |
|
|
1964 | |
|
|
1965 | if ($self->{state}) { |
|
|
1966 | $self->{state} = 0; |
|
|
1967 | $self->toggle_flopper; |
|
|
1968 | } |
|
|
1969 | |
|
|
1970 | $self |
|
|
1971 | } |
|
|
1972 | |
|
|
1973 | sub toggle_flopper { |
|
|
1974 | my ($self) = @_; |
|
|
1975 | |
|
|
1976 | # TODO: use animation |
|
|
1977 | if ($self->{state} = !$self->{state}) { |
|
|
1978 | $CFClient::UI::ROOT->add ($self->{other}); |
|
|
1979 | $self->{other}->move ($self->coord2global (0, $self->{h})); |
|
|
1980 | $self->emit ("open"); |
|
|
1981 | } else { |
|
|
1982 | $CFClient::UI::ROOT->remove ($self->{other}); |
|
|
1983 | $self->emit ("close"); |
|
|
1984 | } |
|
|
1985 | |
|
|
1986 | $self->emit (changed => $self->{state}); |
|
|
1987 | } |
|
|
1988 | |
|
|
1989 | ############################################################################# |
|
|
1990 | |
|
|
1991 | package CFClient::UI::Root; |
1359 | |
1992 | |
1360 | our @ISA = CFClient::UI::Container::; |
1993 | our @ISA = CFClient::UI::Container::; |
|
|
1994 | |
|
|
1995 | use SDL::OpenGL; |
1361 | |
1996 | |
1362 | sub size_request { |
1997 | sub size_request { |
1363 | ($::WIDTH, $::HEIGHT) |
1998 | ($::WIDTH, $::HEIGHT) |
1364 | } |
1999 | } |
1365 | |
2000 | |
1366 | sub size_allocate { |
2001 | sub size_allocate { |
1367 | my ($self, $w, $h) = @_; |
2002 | my ($self, $x, $y, $w, $h) = @_; |
1368 | |
2003 | |
1369 | $self->SUPER::size_allocate ($w, $h); |
2004 | $self->_size_allocate ($x, $y, $w, $h); |
1370 | |
2005 | |
1371 | $_->size_allocate ($_->size_request) |
2006 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
1372 | for @{$self->{children}}; |
2007 | for @{$self->{children}}; |
1373 | } |
2008 | } |
1374 | |
2009 | |
1375 | sub translate { |
2010 | sub _topleft { |
1376 | my ($self, $x, $y) = @_; |
2011 | my ($self, $x, $y) = @_; |
1377 | |
2012 | |
1378 | ($x, $y) |
2013 | ($x, $y) |
1379 | } |
2014 | } |
1380 | |
2015 | |
1381 | sub update { |
2016 | sub update { |
1382 | my ($self) = @_; |
2017 | my ($self) = @_; |
1383 | |
2018 | |
1384 | $self->size_allocate ($self->size_request); |
2019 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
1385 | ::refresh (); |
2020 | ::refresh (); |
1386 | } |
2021 | } |
1387 | |
2022 | |
1388 | sub add { |
2023 | sub add { |
1389 | my ($self, $widget) = @_; |
2024 | my ($self, $widget) = @_; |
1390 | |
2025 | |
1391 | $self->SUPER::add ($widget); |
2026 | $self->SUPER::add ($widget); |
1392 | |
2027 | |
1393 | $widget->size_allocate ($widget->size_request); |
2028 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
|
|
2029 | } |
|
|
2030 | |
|
|
2031 | sub on_refresh { |
|
|
2032 | my ($self, $id, $cb) = @_; |
|
|
2033 | |
|
|
2034 | $self->{refresh_hook}{$id} = $cb; |
1394 | } |
2035 | } |
1395 | |
2036 | |
1396 | sub draw { |
2037 | sub draw { |
1397 | my ($self) = @_; |
2038 | my ($self) = @_; |
1398 | |
2039 | |
|
|
2040 | while (my $rcb = delete $self->{refresh_hook}) { |
|
|
2041 | $_->() for values %$rcb; |
|
|
2042 | } |
|
|
2043 | |
|
|
2044 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
|
|
2045 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
2046 | glClear GL_COLOR_BUFFER_BIT; |
|
|
2047 | |
|
|
2048 | glMatrixMode GL_PROJECTION; |
|
|
2049 | glLoadIdentity; |
|
|
2050 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; |
|
|
2051 | glMatrixMode GL_MODELVIEW; |
|
|
2052 | glLoadIdentity; |
|
|
2053 | |
1399 | $self->_draw; |
2054 | $self->_draw; |
1400 | } |
2055 | } |
1401 | |
2056 | |
1402 | ############################################################################# |
2057 | ############################################################################# |
1403 | |
2058 | |
1404 | package CFClient::UI; |
2059 | package CFClient::UI; |
1405 | |
2060 | |
1406 | $TOPLEVEL = new CFClient::UI::Toplevel; |
2061 | $ROOT = new CFClient::UI::Root; |
1407 | |
2062 | |
1408 | 1 |
2063 | 1 |
1409 | |
2064 | |