… | |
… | |
8 | use CFClient; |
8 | use CFClient; |
9 | |
9 | |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
11 | |
11 | |
12 | our $ROOT; |
12 | our $ROOT; |
|
|
13 | our $TOOLTIP; |
13 | our $BUTTON_STATE; |
14 | our $BUTTON_STATE; |
|
|
15 | |
|
|
16 | sub check_tooltip { |
|
|
17 | if (!$GRAB) { |
|
|
18 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
|
|
19 | if (length $widget->{tooltip}) { |
|
|
20 | |
|
|
21 | if ($TOOLTIP->{owner} != $widget) { |
|
|
22 | $TOOLTIP->{owner} = $widget; |
|
|
23 | |
|
|
24 | my $tip = $widget->{tooltip}; |
|
|
25 | |
|
|
26 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
27 | |
|
|
28 | $TOOLTIP->set_markup ($widget->{tooltip}); |
|
|
29 | $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0)); |
|
|
30 | $TOOLTIP->show; |
|
|
31 | } |
|
|
32 | |
|
|
33 | return; |
|
|
34 | } |
|
|
35 | } |
|
|
36 | } |
|
|
37 | |
|
|
38 | $TOOLTIP->hide; |
|
|
39 | delete $TOOLTIP->{owner}; |
|
|
40 | } |
14 | |
41 | |
15 | # class methods for events |
42 | # class methods for events |
16 | sub feed_sdl_key_down_event { |
43 | sub feed_sdl_key_down_event { |
17 | $FOCUS->key_down ($_[0]) if $FOCUS; |
44 | $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0]) |
|
|
45 | if $FOCUS; |
18 | } |
46 | } |
19 | |
47 | |
20 | sub feed_sdl_key_up_event { |
48 | sub feed_sdl_key_up_event { |
21 | $FOCUS->key_up ($_[0]) if $FOCUS; |
49 | $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0]) |
|
|
50 | if $FOCUS; |
22 | } |
51 | } |
23 | |
52 | |
24 | sub feed_sdl_button_down_event { |
53 | sub feed_sdl_button_down_event { |
25 | my ($ev) = @_; |
54 | my ($ev) = @_; |
26 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
55 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
27 | |
56 | |
28 | if (!$BUTTON_STATE) { |
57 | if (!$BUTTON_STATE) { |
29 | my $widget = $ROOT->find_widget ($x, $y); |
58 | my $widget = $ROOT->find_widget ($x, $y); |
30 | |
59 | |
31 | $GRAB = $widget; |
60 | $GRAB = $widget; |
32 | $GRAB->update if $GRAB; |
61 | $GRAB->update if $GRAB; |
33 | } |
|
|
34 | |
62 | |
|
|
63 | check_tooltip; |
|
|
64 | } |
|
|
65 | |
35 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
66 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
36 | |
67 | |
37 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
68 | if ($GRAB) { |
|
|
69 | ($x, $y) = $GRAB->coord2local ($x, $y); |
|
|
70 | $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y); |
|
|
71 | } |
38 | } |
72 | } |
39 | |
73 | |
40 | sub feed_sdl_button_up_event { |
74 | sub feed_sdl_button_up_event { |
41 | my ($ev) = @_; |
75 | my ($ev) = @_; |
42 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
76 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
43 | |
77 | |
44 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
78 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
45 | |
79 | |
46 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
80 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
47 | |
81 | |
48 | $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
82 | if ($GRAB) { |
|
|
83 | ($x, $y) = $GRAB->coord2local ($x, $y); |
|
|
84 | $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y); |
|
|
85 | } |
49 | |
86 | |
50 | if (!$BUTTON_STATE) { |
87 | if (!$BUTTON_STATE) { |
51 | my $grab = $GRAB; undef $GRAB; |
88 | my $grab = $GRAB; undef $GRAB; |
52 | $grab->update if $grab; |
89 | $grab->update if $grab; |
53 | $GRAB->update if $GRAB; |
90 | $GRAB->update if $GRAB; |
|
|
91 | |
|
|
92 | check_tooltip; |
54 | } |
93 | } |
55 | } |
94 | } |
56 | |
95 | |
57 | sub feed_sdl_motion_event { |
96 | sub feed_sdl_motion_event { |
58 | my ($ev) = @_; |
97 | my ($ev) = @_; |
59 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
98 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
60 | |
99 | |
61 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
100 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
62 | |
101 | |
63 | if ($widget != $HOVER) { |
102 | if ($widget != $HOVER) { |
64 | my $hover = $HOVER; $HOVER = $widget; |
103 | my $hover = $HOVER; $HOVER = $widget; |
65 | |
104 | |
66 | $hover->update if $hover && $hover->{can_hover}; |
105 | $hover->update if $hover && $hover->{can_hover}; |
67 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
106 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
68 | } |
|
|
69 | |
107 | |
70 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
108 | check_tooltip; |
|
|
109 | } |
|
|
110 | |
|
|
111 | if ($HOVER) { |
|
|
112 | ($x, $y) = $HOVER->coord2local ($x, $y); |
|
|
113 | $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y); |
|
|
114 | } |
71 | } |
115 | } |
72 | |
116 | |
73 | # convert position array to integers |
117 | # convert position array to integers |
74 | sub harmonize { |
118 | sub harmonize { |
75 | my ($vals) = @_; |
119 | my ($vals) = @_; |
… | |
… | |
87 | |
131 | |
88 | package CFClient::UI::Base; |
132 | package CFClient::UI::Base; |
89 | |
133 | |
90 | use strict; |
134 | use strict; |
91 | |
135 | |
92 | use SDL::OpenGL; |
136 | use CFClient::OpenGL; |
93 | |
137 | |
94 | sub new { |
138 | sub new { |
95 | my $class = shift; |
139 | my $class = shift; |
96 | |
140 | |
97 | my $self = bless { |
141 | my $self = bless { |
98 | x => 0, |
142 | x => 0, |
99 | y => 0, |
143 | y => 0, |
100 | z => 0, |
144 | z => 0, |
|
|
145 | can_events => 1, |
101 | @_ |
146 | @_ |
102 | }, $class; |
147 | }, $class; |
103 | |
148 | |
104 | for (keys %$self) { |
149 | for (keys %$self) { |
105 | if (/^connect_(.*)$/) { |
150 | if (/^connect_(.*)$/) { |
… | |
… | |
108 | } |
153 | } |
109 | |
154 | |
110 | $self |
155 | $self |
111 | } |
156 | } |
112 | |
157 | |
|
|
158 | sub destroy { |
|
|
159 | my ($self) = @_; |
|
|
160 | |
|
|
161 | $self->hide; |
|
|
162 | %$self = (); |
|
|
163 | } |
|
|
164 | |
113 | sub show { |
165 | sub show { |
114 | my ($self) = @_; |
166 | my ($self) = @_; |
115 | |
167 | |
116 | return if $self->{parent}; |
168 | return if $self->{parent}; |
117 | |
169 | |
… | |
… | |
119 | } |
171 | } |
120 | |
172 | |
121 | sub hide { |
173 | sub hide { |
122 | my ($self) = @_; |
174 | my ($self) = @_; |
123 | |
175 | |
124 | return unless $self->{parent}; |
176 | undef $GRAB if $GRAB == $self; |
|
|
177 | undef $HOVER if $HOVER == $self; |
125 | |
178 | |
126 | $self->{parent}->remove ($self); |
179 | $self->{parent}->remove ($self) |
|
|
180 | if $self->{parent}; |
127 | } |
181 | } |
128 | |
182 | |
129 | sub move { |
183 | sub move { |
130 | my ($self, $x, $y, $z) = @_; |
184 | my ($self, $x, $y, $z) = @_; |
131 | |
185 | |
… | |
… | |
134 | $self->{z} = $z if defined $z; |
188 | $self->{z} = $z if defined $z; |
135 | |
189 | |
136 | $self->update; |
190 | $self->update; |
137 | } |
191 | } |
138 | |
192 | |
139 | sub needs_redraw { |
193 | sub set_size { |
140 | 0 |
194 | my ($self, $w, $h) = @_; |
|
|
195 | |
|
|
196 | $self->{user_w} = $w; |
|
|
197 | $self->{user_h} = $h; |
|
|
198 | |
|
|
199 | $self->check_size; |
141 | } |
200 | } |
142 | |
201 | |
143 | sub size_request { |
202 | sub size_request { |
144 | require Carp; |
203 | require Carp; |
145 | Carp::confess "size_request is abtract"; |
204 | Carp::confess "size_request is abstract"; |
146 | } |
205 | } |
147 | |
206 | |
148 | sub configure { |
207 | sub configure { |
149 | my ($self, $x, $y, $w, $h) = @_; |
208 | my ($self, $x, $y, $w, $h) = @_; |
150 | |
209 | |
|
|
210 | if ($self->{aspect}) { |
|
|
211 | my $w2 = List::Util::min $w, int $h * $self->{aspect}; |
|
|
212 | my $h2 = List::Util::min $h, int $w / $self->{aspect}; |
|
|
213 | |
|
|
214 | # use alignment to adjust x, y |
|
|
215 | |
|
|
216 | $x += int +($w - $w2) * 0.5; |
|
|
217 | $y += int +($h - $h2) * 0.5; |
|
|
218 | |
|
|
219 | ($w, $h) = ($w2, $h2); |
|
|
220 | } |
|
|
221 | |
|
|
222 | if ($self->{x} != $x || $self->{y} != $y) { |
151 | $self->{x} = $x; |
223 | $self->{x} = $x; |
152 | $self->{y} = $y; |
224 | $self->{y} = $y; |
|
|
225 | $self->update; |
|
|
226 | } |
153 | |
227 | |
154 | return unless $self->{w} != $w || $self->{h} != $h; |
228 | if ($self->{w} != $w || $self->{h} != $h) { |
155 | |
|
|
156 | $self->{w} = $w; |
229 | $self->{w} = $w; |
157 | $self->{h} = $h; |
230 | $self->{h} = $h; |
158 | |
231 | |
159 | $self->size_allocate ($w, $h); |
232 | $self->size_allocate ($w, $h); |
160 | $self->update; |
233 | $self->update; |
|
|
234 | } |
161 | } |
235 | } |
162 | |
236 | |
163 | sub size_allocate { |
237 | sub size_allocate { |
164 | # nothing to be done |
238 | # nothing to be done |
|
|
239 | } |
|
|
240 | |
|
|
241 | sub set_max_size { |
|
|
242 | my ($self, $w, $h) = @_; |
|
|
243 | |
|
|
244 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
|
|
245 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
165 | } |
246 | } |
166 | |
247 | |
167 | # return top left coordinates |
248 | # return top left coordinates |
168 | sub _topleft { |
249 | sub _topleft { |
169 | my ($self, $x, $y) = @_; |
250 | my ($self, $x, $y) = @_; |
… | |
… | |
252 | glVertex $x + $self->{w}, $y + $self->{h}; |
333 | glVertex $x + $self->{w}, $y + $self->{h}; |
253 | glVertex $x , $y + $self->{h}; |
334 | glVertex $x , $y + $self->{h}; |
254 | glEnd; |
335 | glEnd; |
255 | glDisable GL_BLEND; |
336 | glDisable GL_BLEND; |
256 | } |
337 | } |
|
|
338 | |
|
|
339 | if ($ENV{PCLIENT_DEBUG}) { |
|
|
340 | glPushMatrix; |
|
|
341 | glColor 1, 1, 0, 1; |
|
|
342 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
|
|
343 | glBegin GL_LINE_LOOP; |
|
|
344 | glVertex 0 , 0; |
|
|
345 | glVertex $self->{w}, 0; |
|
|
346 | glVertex $self->{w}, $self->{h}; |
|
|
347 | glVertex 0 , $self->{h}; |
|
|
348 | glEnd; |
|
|
349 | glPopMatrix; |
|
|
350 | CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
|
|
351 | } |
257 | } |
352 | } |
258 | |
353 | |
259 | sub _draw { |
354 | sub _draw { |
260 | my ($self) = @_; |
355 | my ($self) = @_; |
261 | |
356 | |
262 | warn "no draw defined for $self\n"; |
357 | warn "no draw defined for $self\n"; |
263 | } |
358 | } |
264 | |
359 | |
265 | sub find_widget { |
360 | sub find_widget { |
266 | my ($self, $x, $y) = @_; |
361 | my ($self, $x, $y) = @_; |
|
|
362 | |
|
|
363 | return () unless $self->{can_events}; |
267 | |
364 | |
268 | return $self |
365 | return $self |
269 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
366 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
270 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
367 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
271 | |
368 | |
… | |
… | |
279 | } |
376 | } |
280 | |
377 | |
281 | sub check_size { |
378 | sub check_size { |
282 | my ($self) = @_; |
379 | my ($self) = @_; |
283 | |
380 | |
284 | my ($w, $h) = $self->size_request; |
381 | $self->{parent} |
|
|
382 | or return 1; |
285 | |
383 | |
|
|
384 | my ($w, $h) = $self->{user_w} && $self->{user_h} |
|
|
385 | ? @$self{qw(user_w user_h)} |
|
|
386 | : $self->size_request; |
|
|
387 | |
286 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
388 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
287 | $self->{req_w} = $w; |
389 | $self->{req_w} = $w; |
288 | $self->{req_h} = $h; |
390 | $self->{req_h} = $h; |
289 | |
391 | |
290 | $self->{parent}->check_size |
392 | $self->{parent}->check_size |
291 | if $self->{parent}; |
393 | or $self->size_allocate ( |
|
|
394 | (List::Util::max $self->{w}, $w), |
|
|
395 | (List::Util::max $self->{h}, $h), |
|
|
396 | ); |
|
|
397 | |
|
|
398 | 1 |
|
|
399 | } else { |
|
|
400 | 0 |
292 | } |
401 | } |
293 | } |
402 | } |
294 | |
403 | |
295 | sub update { |
404 | sub update { |
296 | my ($self) = @_; |
405 | my ($self) = @_; |
… | |
… | |
306 | } |
415 | } |
307 | |
416 | |
308 | sub emit { |
417 | sub emit { |
309 | my ($self, $signal, @args) = @_; |
418 | my ($self, $signal, @args) = @_; |
310 | |
419 | |
311 | for my $cb (@{$self->{signal_cb}{$signal} || []}) { |
420 | List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []} |
312 | $cb->($self, @args); |
|
|
313 | } |
|
|
314 | } |
421 | } |
315 | |
422 | |
316 | sub DESTROY { |
423 | sub DESTROY { |
317 | my ($self) = @_; |
424 | my ($self) = @_; |
318 | |
425 | |
… | |
… | |
324 | package CFClient::UI::DrawBG; |
431 | package CFClient::UI::DrawBG; |
325 | |
432 | |
326 | our @ISA = CFClient::UI::Base::; |
433 | our @ISA = CFClient::UI::Base::; |
327 | |
434 | |
328 | use strict; |
435 | use strict; |
329 | use SDL::OpenGL; |
436 | use CFClient::OpenGL; |
330 | |
437 | |
331 | sub new { |
438 | sub new { |
332 | my $class = shift; |
439 | my $class = shift; |
333 | |
440 | |
334 | # range [value, low, high, page] |
441 | # range [value, low, high, page] |
… | |
… | |
363 | |
470 | |
364 | package CFClient::UI::Empty; |
471 | package CFClient::UI::Empty; |
365 | |
472 | |
366 | our @ISA = CFClient::UI::Base::; |
473 | our @ISA = CFClient::UI::Base::; |
367 | |
474 | |
|
|
475 | sub new { |
|
|
476 | my ($class, %arg) = @_; |
|
|
477 | $class->SUPER::new (can_events => 0, %arg); |
|
|
478 | } |
|
|
479 | |
368 | sub size_request { |
480 | sub size_request { |
369 | (0, 0) |
481 | (0, 0) |
370 | } |
482 | } |
371 | |
483 | |
372 | sub draw { } |
484 | sub draw { } |
… | |
… | |
380 | sub new { |
492 | sub new { |
381 | my ($class, %arg) = @_; |
493 | my ($class, %arg) = @_; |
382 | |
494 | |
383 | my $children = delete $arg{children} || []; |
495 | my $children = delete $arg{children} || []; |
384 | |
496 | |
385 | my $self = $class->SUPER::new (children => [], %arg); |
497 | my $self = $class->SUPER::new (children => [], can_events => 0, %arg); |
386 | $self->add ($_) for @$children; |
498 | $self->add ($_) for @$children; |
387 | |
499 | |
388 | $self |
500 | $self |
389 | } |
501 | } |
390 | |
502 | |
… | |
… | |
405 | |
517 | |
406 | sub remove { |
518 | sub remove { |
407 | my ($self, $child) = @_; |
519 | my ($self, $child) = @_; |
408 | |
520 | |
409 | delete $child->{parent}; |
521 | delete $child->{parent}; |
|
|
522 | $child->hide; |
410 | |
523 | |
411 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
524 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
412 | |
525 | |
413 | $self->check_size; |
526 | $self->check_size; |
|
|
527 | $self->update; |
|
|
528 | } |
|
|
529 | |
|
|
530 | sub clear { |
|
|
531 | my ($self) = @_; |
|
|
532 | |
|
|
533 | my $children = delete $self->{children}; |
|
|
534 | $self->{children} = []; |
|
|
535 | |
|
|
536 | for (@$children) { |
|
|
537 | delete $_->{parent}; |
|
|
538 | $_->hide; |
|
|
539 | } |
414 | } |
540 | } |
415 | |
541 | |
416 | sub find_widget { |
542 | sub find_widget { |
417 | my ($self, $x, $y) = @_; |
543 | my ($self, $x, $y) = @_; |
418 | |
544 | |
… | |
… | |
482 | |
608 | |
483 | package CFClient::UI::Window; |
609 | package CFClient::UI::Window; |
484 | |
610 | |
485 | our @ISA = CFClient::UI::Bin::; |
611 | our @ISA = CFClient::UI::Bin::; |
486 | |
612 | |
487 | use SDL::OpenGL; |
613 | use CFClient::OpenGL; |
488 | |
614 | |
489 | sub new { |
615 | sub new { |
490 | my ($class, %arg) = @_; |
616 | my ($class, %arg) = @_; |
491 | |
617 | |
492 | my $self = $class->SUPER::new (%arg); |
618 | my $self = $class->SUPER::new (%arg); |
… | |
… | |
546 | sub new { die } |
672 | sub new { die } |
547 | |
673 | |
548 | sub size_request { |
674 | sub size_request { |
549 | my ($self) = @_; |
675 | my ($self) = @_; |
550 | |
676 | |
551 | @$self{qw(child_w child_h)} = $self->child->size_request; |
677 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
552 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
678 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
553 | |
679 | |
554 | @$self{qw(child_w child_h)} |
680 | @$self{qw(child_w child_h)} |
555 | } |
681 | } |
556 | |
682 | |
… | |
… | |
565 | |
691 | |
566 | package CFClient::UI::Frame; |
692 | package CFClient::UI::Frame; |
567 | |
693 | |
568 | our @ISA = CFClient::UI::Bin::; |
694 | our @ISA = CFClient::UI::Bin::; |
569 | |
695 | |
570 | use SDL::OpenGL; |
696 | use CFClient::OpenGL; |
571 | |
|
|
572 | sub size_request { |
|
|
573 | my ($self) = @_; |
|
|
574 | my $chld = $self->child |
|
|
575 | or return (0, 0); |
|
|
576 | |
|
|
577 | $chld->move (2, 2); |
|
|
578 | |
|
|
579 | map { $_ + 4 } $chld->size_request; |
|
|
580 | } |
|
|
581 | |
|
|
582 | sub size_allocate { |
|
|
583 | my ($self, $x, $y, $w, $h) = @_; |
|
|
584 | |
|
|
585 | $self->child->configure (2, 2, $w - 4, $h - 4); |
|
|
586 | } |
|
|
587 | |
|
|
588 | sub _draw { |
|
|
589 | my ($self) = @_; |
|
|
590 | |
|
|
591 | my $chld = $self->child; |
|
|
592 | |
|
|
593 | my ($w, $h) = $chld->size_request; |
|
|
594 | |
|
|
595 | glBegin GL_QUADS; |
|
|
596 | glColor 0, 0, 0; |
|
|
597 | glVertex 0 , 0; |
|
|
598 | glVertex 0 , $h + 4; |
|
|
599 | glVertex $w + 4 , $h + 4; |
|
|
600 | glVertex $w + 4 , 0; |
|
|
601 | glEnd; |
|
|
602 | |
|
|
603 | $chld->draw; |
|
|
604 | } |
|
|
605 | |
|
|
606 | ############################################################################# |
|
|
607 | |
|
|
608 | package CFClient::UI::FancyFrame; |
|
|
609 | |
|
|
610 | our @ISA = CFClient::UI::Bin::; |
|
|
611 | |
|
|
612 | use SDL::OpenGL; |
|
|
613 | |
|
|
614 | my @tex = |
|
|
615 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
616 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
617 | |
697 | |
618 | sub new { |
698 | sub new { |
619 | my $class = shift; |
699 | my $class = shift; |
620 | |
700 | |
621 | # TODO: user_x, user_y, overwrite moveto? |
|
|
622 | |
|
|
623 | $class->SUPER::new ( |
701 | my $self = $class->SUPER::new ( |
624 | bg => [1, 1, 1, 1], |
702 | bg => [1, 1, 1, 1], |
625 | border_bg => [1, 1, 1, 1], |
703 | border_bg => [1, 1, 1, 1], |
626 | border => 0.8, |
704 | border => 0.8, |
627 | @_ |
705 | @_ |
628 | ) |
706 | ); |
|
|
707 | |
|
|
708 | $self |
|
|
709 | } |
|
|
710 | |
|
|
711 | sub _draw { |
|
|
712 | my ($self) = @_; |
|
|
713 | |
|
|
714 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
715 | |
|
|
716 | glEnable GL_BLEND; |
|
|
717 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
718 | glEnable GL_TEXTURE_2D; |
|
|
719 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
720 | |
|
|
721 | # glBegin GL_QUADS; |
|
|
722 | # glColor 0, 0, 0, 0; |
|
|
723 | # glVertex 0 , 0; |
|
|
724 | # glVertex 0 , $h; |
|
|
725 | # glVertex $w, $h; |
|
|
726 | # glVertex $w, 0; |
|
|
727 | # glEnd; |
|
|
728 | |
|
|
729 | |
|
|
730 | $self->child->draw; |
|
|
731 | glDisable GL_BLEND; |
|
|
732 | glDisable GL_TEXTURE_2D; |
|
|
733 | } |
|
|
734 | |
|
|
735 | ############################################################################# |
|
|
736 | |
|
|
737 | package CFClient::UI::FancyFrame; |
|
|
738 | |
|
|
739 | our @ISA = CFClient::UI::Bin::; |
|
|
740 | |
|
|
741 | use CFClient::OpenGL; |
|
|
742 | |
|
|
743 | my @tex = |
|
|
744 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
745 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
746 | |
|
|
747 | sub new { |
|
|
748 | my $class = shift; |
|
|
749 | |
|
|
750 | # TODO: user_x, user_y, overwrite moveto? |
|
|
751 | |
|
|
752 | my $self = $class->SUPER::new ( |
|
|
753 | bg => [1, 1, 1, 1], |
|
|
754 | border_bg => [1, 1, 1, 1], |
|
|
755 | border => 0.8, |
|
|
756 | can_events => 0, |
|
|
757 | @_ |
|
|
758 | ); |
|
|
759 | |
|
|
760 | $self->{title} &&= new CFClient::UI::Label |
|
|
761 | align => 0, |
|
|
762 | valign => 1, |
|
|
763 | text => $self->{title}, |
|
|
764 | fontsize => 1; |
|
|
765 | |
|
|
766 | $self |
629 | } |
767 | } |
630 | |
768 | |
631 | sub border { |
769 | sub border { |
632 | int $_[0]{border} * $::FONTSIZE |
770 | int $_[0]{border} * $::FONTSIZE |
633 | } |
771 | } |
634 | |
772 | |
635 | sub size_request { |
773 | sub size_request { |
636 | my ($self) = @_; |
774 | my ($self) = @_; |
637 | |
|
|
638 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
|
|
639 | |
775 | |
640 | my ($w, $h) = $self->SUPER::size_request; |
776 | my ($w, $h) = $self->SUPER::size_request; |
641 | |
777 | |
642 | ( |
778 | ( |
643 | $w + $self->border * 2, |
779 | $w + $self->border * 2, |
… | |
… | |
649 | my ($self, $w, $h) = @_; |
785 | my ($self, $w, $h) = @_; |
650 | |
786 | |
651 | $h -= List::Util::max 0, $self->border * 2; |
787 | $h -= List::Util::max 0, $self->border * 2; |
652 | $w -= List::Util::max 0, $self->border * 2; |
788 | $w -= List::Util::max 0, $self->border * 2; |
653 | |
789 | |
|
|
790 | $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) |
|
|
791 | if $self->{title}; |
|
|
792 | |
654 | $self->child->configure ($self->border, $self->border, $w, $h); |
793 | $self->child->configure ($self->border, $self->border, $w, $h); |
655 | } |
794 | } |
656 | |
795 | |
657 | sub button_down { |
796 | sub button_down { |
658 | my ($self, $ev, $x, $y) = @_; |
797 | my ($self, $ev, $x, $y) = @_; |
… | |
… | |
660 | my $border = $self->border; |
799 | my $border = $self->border; |
661 | |
800 | |
662 | if ($x < $self->{w} && $x >= $self->{w} - $border |
801 | if ($x < $self->{w} && $x >= $self->{w} - $border |
663 | && $y < $self->{h} && $y >= $self->{h} - $border) { |
802 | && $y < $self->{h} && $y >= $self->{h} - $border) { |
664 | |
803 | |
665 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
804 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
666 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
805 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
667 | |
806 | |
668 | $self->{motion} = sub { |
807 | $self->{motion} = sub { |
669 | my ($ev, $x, $y) = @_; |
808 | my ($ev, $x, $y) = @_; |
670 | |
809 | |
671 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
810 | ($x, $y) = ($ev->{x}, $ev->{y}); |
672 | |
811 | |
673 | $self->{user_w} = $bw + $x - $ox; |
812 | $self->{user_w} = $bw + $x - $ox; |
674 | $self->{user_h} = $bh + $y - $oy; |
813 | $self->{user_h} = $bh + $y - $oy; |
675 | $self->check_size; |
814 | $self->check_size; |
676 | }; |
815 | }; |
677 | |
816 | |
678 | } elsif ($x >= 0 && $x < $self->{w} |
817 | } elsif ($x >= 0 && $x < $self->{w} |
679 | && $y >= 0 && $y < $border) { |
818 | && $y >= 0 && $y < $border) { |
680 | |
819 | |
681 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
820 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
682 | my ($bx, $by) = ($self->{x}, $self->{y}); |
821 | my ($bx, $by) = ($self->{x}, $self->{y}); |
683 | |
822 | |
684 | $self->{motion} = sub { |
823 | $self->{motion} = sub { |
685 | my ($ev, $x, $y) = @_; |
824 | my ($ev, $x, $y) = @_; |
686 | |
825 | |
687 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
826 | ($x, $y) = ($ev->{x}, $ev->{y}); |
688 | |
827 | |
689 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
828 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
690 | $self->update; |
829 | $self->update; |
691 | }; |
830 | }; |
692 | } |
831 | } |
… | |
… | |
737 | $bg->draw_quad ($border, $border, $cw, $ch); |
876 | $bg->draw_quad ($border, $border, $cw, $ch); |
738 | |
877 | |
739 | glDisable GL_TEXTURE_2D; |
878 | glDisable GL_TEXTURE_2D; |
740 | glDisable GL_BLEND; |
879 | glDisable GL_BLEND; |
741 | |
880 | |
|
|
881 | $self->{title}->draw if $self->{title}; |
742 | $self->child->draw; |
882 | $self->child->draw; |
743 | } |
883 | } |
744 | |
884 | |
745 | ############################################################################# |
885 | ############################################################################# |
746 | |
886 | |
… | |
… | |
748 | |
888 | |
749 | our @ISA = CFClient::UI::Base::; |
889 | our @ISA = CFClient::UI::Base::; |
750 | |
890 | |
751 | use List::Util qw(max sum); |
891 | use List::Util qw(max sum); |
752 | |
892 | |
753 | use SDL::OpenGL; |
893 | use CFClient::OpenGL; |
754 | |
894 | |
755 | sub new { |
895 | sub new { |
756 | my $class = shift; |
896 | my $class = shift; |
757 | |
897 | |
758 | $class->SUPER::new ( |
898 | $class->SUPER::new ( |
… | |
… | |
772 | |
912 | |
773 | # TODO: move to container class maybe? send childs a signal on removal? |
913 | # TODO: move to container class maybe? send childs a signal on removal? |
774 | sub clear { |
914 | sub clear { |
775 | my ($self) = @_; |
915 | my ($self) = @_; |
776 | |
916 | |
777 | delete $self->{children}; |
917 | my $children = delete $self->{children}; |
|
|
918 | |
|
|
919 | for (grep $_, map @$_, grep $_, @$children) { |
|
|
920 | delete $_->{parent}; |
|
|
921 | $_->hide; |
|
|
922 | } |
|
|
923 | |
778 | $self->update; |
924 | $self->update; |
779 | } |
925 | } |
780 | |
926 | |
781 | sub get_wh { |
927 | sub get_wh { |
782 | my ($self) = @_; |
928 | my ($self) = @_; |
… | |
… | |
788 | or next; |
934 | or next; |
789 | |
935 | |
790 | for my $x (0 .. $#$row) { |
936 | for my $x (0 .. $#$row) { |
791 | my $widget = $row->[$x] |
937 | my $widget = $row->[$x] |
792 | or next; |
938 | or next; |
793 | my ($w, $h) = $widget->size_request; |
939 | my ($w, $h) = @$widget{qw(req_w req_h)}; |
794 | |
940 | |
795 | $w[$x] = max $w[$x], $w; |
941 | $w[$x] = max $w[$x], $w; |
796 | $h[$y] = max $h[$y], $h; |
942 | $h[$y] = max $h[$y], $h; |
797 | } |
943 | } |
798 | } |
944 | } |
… | |
… | |
903 | |
1049 | |
904 | ($h, $w) = ($w, $h); |
1050 | ($h, $w) = ($w, $h); |
905 | |
1051 | |
906 | my $children = $self->{children}; |
1052 | my $children = $self->{children}; |
907 | |
1053 | |
908 | my @h = map +($_->size_request)[0], @$children; |
1054 | my @h = map $_->{req_w}, @$children; |
909 | |
1055 | |
910 | my $req_h = List::Util::sum @h; |
1056 | my $req_h = List::Util::sum @h; |
911 | |
1057 | |
912 | if ($req_h > $h) { |
1058 | if ($req_h > $h) { |
913 | # ah well, not enough space |
1059 | # ah well, not enough space |
… | |
… | |
961 | sub size_allocate { |
1107 | sub size_allocate { |
962 | my ($self, $w, $h) = @_; |
1108 | my ($self, $w, $h) = @_; |
963 | |
1109 | |
964 | my $children = $self->{children}; |
1110 | my $children = $self->{children}; |
965 | |
1111 | |
966 | my @h = map +($_->size_request)[1], @$children; |
1112 | my @h = map $_->{req_h}, @$children; |
967 | |
1113 | |
968 | my $req_h = List::Util::sum @h; |
1114 | my $req_h = List::Util::sum @h; |
969 | |
1115 | |
970 | if ($req_h > $h) { |
1116 | if ($req_h > $h) { |
971 | # ah well, not enough space |
1117 | # ah well, not enough space |
… | |
… | |
999 | |
1145 | |
1000 | package CFClient::UI::Label; |
1146 | package CFClient::UI::Label; |
1001 | |
1147 | |
1002 | our @ISA = CFClient::UI::Base::; |
1148 | our @ISA = CFClient::UI::Base::; |
1003 | |
1149 | |
1004 | use SDL::OpenGL; |
1150 | use CFClient::OpenGL; |
1005 | |
1151 | |
1006 | sub new { |
1152 | sub new { |
1007 | my ($class, %arg) = @_; |
1153 | my ($class, %arg) = @_; |
1008 | |
1154 | |
1009 | my $self = $class->SUPER::new ( |
1155 | my $self = $class->SUPER::new ( |
1010 | fg => [1, 1, 1], |
1156 | fg => [1, 1, 1], |
|
|
1157 | #font => default_font |
1011 | fontsize => 1, |
1158 | fontsize => 1, |
1012 | text => "", |
1159 | text => "", |
1013 | align => -1, |
1160 | align => -1, |
1014 | valign => -1, |
1161 | valign => -1, |
1015 | padding => 2, |
1162 | padding => 2, |
1016 | layout => new CFClient::Layout, |
1163 | layout => new CFClient::Layout, |
|
|
1164 | can_events => 0, |
1017 | %arg |
1165 | %arg |
1018 | ); |
1166 | ); |
1019 | |
1167 | |
1020 | $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# |
1168 | if (exists $self->{template}) { |
|
|
1169 | my $layout = new CFClient::Layout; |
|
|
1170 | $layout->set_text (delete $self->{template}); |
|
|
1171 | $self->{template} = $layout; |
|
|
1172 | } |
1021 | |
1173 | |
1022 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
1174 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
1023 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
1175 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
1024 | |
1176 | |
1025 | $self |
1177 | $self |
1026 | } |
1178 | } |
1027 | |
1179 | |
1028 | sub escape_text { |
1180 | sub escape { |
1029 | local $_ = $_[1]; |
1181 | local $_ = $_[1]; |
1030 | |
1182 | |
1031 | s/&/&/g; |
1183 | s/&/&/g; |
1032 | s/>/>/g; |
1184 | s/>/>/g; |
1033 | s/</</g; |
1185 | s/</</g; |
… | |
… | |
1039 | my ($self, $text) = @_; |
1191 | my ($self, $text) = @_; |
1040 | |
1192 | |
1041 | $self->{layout}->set_text ($text); |
1193 | $self->{layout}->set_text ($text); |
1042 | |
1194 | |
1043 | delete $self->{texture}; |
1195 | delete $self->{texture}; |
|
|
1196 | $self->check_size; |
1044 | $self->update; |
1197 | $self->update; |
1045 | } |
1198 | } |
1046 | |
1199 | |
1047 | sub set_markup { |
1200 | sub set_markup { |
1048 | my ($self, $markup) = @_; |
1201 | my ($self, $markup) = @_; |
1049 | |
1202 | |
1050 | $self->{layout}->set_markup ($markup); |
1203 | $self->{layout}->set_markup ($markup); |
1051 | |
1204 | |
1052 | delete $self->{texture}; |
1205 | delete $self->{texture}; |
|
|
1206 | $self->check_size; |
1053 | $self->update; |
1207 | $self->update; |
1054 | } |
1208 | } |
1055 | |
1209 | |
1056 | sub size_request { |
1210 | sub size_request { |
1057 | my ($self) = @_; |
1211 | my ($self) = @_; |
1058 | |
1212 | |
1059 | $self->{layout}->set_width; |
1213 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
|
|
1214 | $self->{layout}->set_width ($self->{max_w} || -1); |
1060 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1215 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1061 | |
1216 | |
1062 | my ($w, $h) = $self->{layout}->size; |
1217 | my ($w, $h) = $self->{layout}->size; |
|
|
1218 | |
|
|
1219 | if (exists $self->{template}) { |
|
|
1220 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
|
|
1221 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1222 | |
|
|
1223 | my ($w2, $h2) = $self->{template}->size; |
|
|
1224 | |
|
|
1225 | $w = List::Util::max $w, $w2; |
|
|
1226 | $h = List::Util::max $h, $h2; |
|
|
1227 | } |
1063 | |
1228 | |
1064 | ( |
1229 | ( |
1065 | $w + $self->{padding} * 2, |
1230 | $w + $self->{padding} * 2, |
1066 | $h + $self->{padding} * 2, |
1231 | $h + $self->{padding} * 2, |
1067 | ) |
1232 | ) |
… | |
… | |
1071 | my ($self, $w, $h) = @_; |
1236 | my ($self, $w, $h) = @_; |
1072 | |
1237 | |
1073 | delete $self->{texture}; |
1238 | delete $self->{texture}; |
1074 | } |
1239 | } |
1075 | |
1240 | |
|
|
1241 | sub set_fontsize { |
|
|
1242 | my ($self, $fontsize) = @_; |
|
|
1243 | |
|
|
1244 | $self->{fontsize} = $fontsize; |
|
|
1245 | delete $self->{texture}; |
|
|
1246 | $self->check_size; |
|
|
1247 | $self->update; |
|
|
1248 | } |
|
|
1249 | |
1076 | sub _draw { |
1250 | sub _draw { |
1077 | my ($self) = @_; |
1251 | my ($self) = @_; |
1078 | |
1252 | |
1079 | my $tex = $self->{texture} ||= do { |
1253 | my $tex = $self->{texture} ||= do { |
|
|
1254 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1080 | $self->{layout}->set_width ($self->{w}); |
1255 | $self->{layout}->set_width ($self->{w}); |
1081 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1256 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1082 | new_from_layout CFClient::Texture $self->{layout} |
1257 | new_from_layout CFClient::Texture $self->{layout} |
1083 | }; |
1258 | }; |
1084 | |
1259 | |
… | |
… | |
1111 | |
1286 | |
1112 | package CFClient::UI::EntryBase; |
1287 | package CFClient::UI::EntryBase; |
1113 | |
1288 | |
1114 | our @ISA = CFClient::UI::Label::; |
1289 | our @ISA = CFClient::UI::Label::; |
1115 | |
1290 | |
1116 | use SDL; |
1291 | use CFClient::OpenGL; |
1117 | use SDL::OpenGL; |
|
|
1118 | |
1292 | |
1119 | sub new { |
1293 | sub new { |
1120 | my $class = shift; |
1294 | my $class = shift; |
1121 | |
1295 | |
1122 | $class->SUPER::new ( |
1296 | $class->SUPER::new ( |
1123 | fg => [1, 1, 1], |
1297 | fg => [1, 1, 1], |
1124 | bg => [0, 0, 0, 0.2], |
1298 | bg => [0, 0, 0, 0.2], |
1125 | active_bg => [1, 1, 1, 0.5], |
1299 | active_bg => [1, 1, 1, 0.5], |
1126 | active_fg => [0, 0, 0], |
1300 | active_fg => [0, 0, 0], |
1127 | can_hover => 1, |
1301 | can_hover => 1, |
1128 | can_focus => 1, |
1302 | can_focus => 1, |
1129 | valign => 0, |
1303 | valign => 0, |
|
|
1304 | can_events => 1, |
1130 | @_ |
1305 | @_ |
1131 | ) |
1306 | ) |
1132 | } |
1307 | } |
1133 | |
1308 | |
1134 | sub _set_text { |
1309 | sub _set_text { |
… | |
… | |
1162 | } |
1337 | } |
1163 | |
1338 | |
1164 | sub size_allocate { |
1339 | sub size_allocate { |
1165 | my ($self, $w, $h) = @_; |
1340 | my ($self, $w, $h) = @_; |
1166 | |
1341 | |
1167 | $self->_set_text ($self->{text}); |
1342 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
1168 | } |
1343 | } |
1169 | |
1344 | |
1170 | sub set_text { |
1345 | sub set_text { |
1171 | my ($self, $text) = @_; |
1346 | my ($self, $text) = @_; |
1172 | |
1347 | |
… | |
… | |
1176 | } |
1351 | } |
1177 | |
1352 | |
1178 | sub key_down { |
1353 | sub key_down { |
1179 | my ($self, $ev) = @_; |
1354 | my ($self, $ev) = @_; |
1180 | |
1355 | |
1181 | my $mod = $ev->key_mod; |
1356 | my $mod = $ev->{mod}; |
1182 | my $sym = $ev->key_sym; |
1357 | my $sym = $ev->{sym}; |
1183 | |
|
|
1184 | my $uni = $ev->key_unicode; |
1358 | my $uni = $ev->{unicode}; |
1185 | |
1359 | |
1186 | my $text = $self->get_text; |
1360 | my $text = $self->get_text; |
1187 | |
1361 | |
1188 | if ($sym == SDLK_BACKSPACE) { |
1362 | if ($sym == 8) { |
1189 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1363 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1190 | } elsif ($sym == SDLK_DELETE) { |
1364 | } elsif ($sym == 127) { |
1191 | substr $text, $self->{cursor}, 1, ""; |
1365 | substr $text, $self->{cursor}, 1, ""; |
1192 | } elsif ($sym == SDLK_LEFT) { |
1366 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1193 | --$self->{cursor} if $self->{cursor}; |
1367 | --$self->{cursor} if $self->{cursor}; |
1194 | } elsif ($sym == SDLK_RIGHT) { |
1368 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1195 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1369 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1196 | } elsif ($sym == SDLK_HOME) { |
1370 | } elsif ($sym == CFClient::SDLK_HOME) { |
1197 | $self->{cursor} = 0; |
1371 | $self->{cursor} = 0; |
1198 | } elsif ($sym == SDLK_END) { |
1372 | } elsif ($sym == CFClient::SDLK_END) { |
1199 | $self->{cursor} = length $text; |
1373 | $self->{cursor} = length $text; |
1200 | } elsif ($sym == SDLK_ESCAPE) { |
1374 | } elsif ($sym == 27) { |
1201 | $self->emit ('escape'); |
1375 | $self->emit ('escape'); |
1202 | } elsif ($uni) { |
1376 | } elsif ($uni) { |
1203 | substr $text, $self->{cursor}++, 0, chr $uni; |
1377 | substr $text, $self->{cursor}++, 0, chr $uni; |
1204 | } |
1378 | } |
1205 | |
1379 | |
… | |
… | |
1280 | |
1454 | |
1281 | package CFClient::UI::Entry; |
1455 | package CFClient::UI::Entry; |
1282 | |
1456 | |
1283 | our @ISA = CFClient::UI::EntryBase::; |
1457 | our @ISA = CFClient::UI::EntryBase::; |
1284 | |
1458 | |
1285 | use SDL; |
1459 | use CFClient::OpenGL; |
1286 | use SDL::OpenGL; |
|
|
1287 | |
1460 | |
1288 | sub key_down { |
1461 | sub key_down { |
1289 | my ($self, $ev) = @_; |
1462 | my ($self, $ev) = @_; |
1290 | |
1463 | |
1291 | my $sym = $ev->key_sym; |
1464 | my $sym = $ev->{sym}; |
1292 | |
1465 | |
1293 | if ($sym == SDLK_RETURN) { |
1466 | if ($sym == 13) { |
1294 | $self->emit (activate => $self->get_text); |
1467 | $self->emit (activate => $self->get_text); |
1295 | $self->update; |
1468 | $self->update; |
1296 | |
1469 | |
1297 | } else { |
1470 | } else { |
1298 | $self->SUPER::key_down ($ev); |
1471 | $self->SUPER::key_down ($ev); |
… | |
… | |
1304 | |
1477 | |
1305 | package CFClient::UI::Button; |
1478 | package CFClient::UI::Button; |
1306 | |
1479 | |
1307 | our @ISA = CFClient::UI::Label::; |
1480 | our @ISA = CFClient::UI::Label::; |
1308 | |
1481 | |
1309 | use SDL; |
1482 | use CFClient::OpenGL; |
1310 | use SDL::OpenGL; |
|
|
1311 | |
1483 | |
1312 | my @tex = |
1484 | my @tex = |
1313 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1485 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1314 | qw(b1_button_active.png); |
1486 | qw(b1_button_active.png); |
1315 | |
1487 | |
1316 | sub new { |
1488 | sub new { |
1317 | my $class = shift; |
1489 | my $class = shift; |
1318 | |
1490 | |
1319 | $class->SUPER::new ( |
1491 | $class->SUPER::new ( |
1320 | padding => 4, |
1492 | padding => 4, |
1321 | fg => [1, 1, 1], |
1493 | fg => [1, 1, 1], |
1322 | bg => [1, 1, 1, 0.2], |
1494 | bg => [1, 1, 1, 0.2], |
1323 | active_fg => [0, 0, 1], |
1495 | active_fg => [0, 0, 1], |
1324 | can_hover => 1, |
1496 | can_hover => 1, |
1325 | align => 0, |
1497 | align => 0, |
1326 | valign => 0, |
1498 | valign => 0, |
|
|
1499 | can_events => 1, |
1327 | @_ |
1500 | @_ |
1328 | ) |
1501 | ) |
1329 | } |
1502 | } |
1330 | |
1503 | |
1331 | sub button_up { |
1504 | sub button_up { |
… | |
… | |
1365 | package CFClient::UI::CheckBox; |
1538 | package CFClient::UI::CheckBox; |
1366 | |
1539 | |
1367 | our @ISA = CFClient::UI::DrawBG::; |
1540 | our @ISA = CFClient::UI::DrawBG::; |
1368 | |
1541 | |
1369 | my @tex = |
1542 | my @tex = |
1370 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1543 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1371 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1544 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1372 | |
1545 | |
1373 | use SDL; |
1546 | use CFClient::OpenGL; |
1374 | use SDL::OpenGL; |
|
|
1375 | |
1547 | |
1376 | sub new { |
1548 | sub new { |
1377 | my $class = shift; |
1549 | my $class = shift; |
1378 | |
1550 | |
1379 | $class->SUPER::new ( |
1551 | $class->SUPER::new ( |
… | |
… | |
1425 | glDisable GL_BLEND; |
1597 | glDisable GL_BLEND; |
1426 | } |
1598 | } |
1427 | |
1599 | |
1428 | ############################################################################# |
1600 | ############################################################################# |
1429 | |
1601 | |
1430 | package CFClient::UI::VGauge; |
1602 | package CFClient::UI::Image; |
1431 | |
1603 | |
1432 | our @ISA = CFClient::UI::Base::; |
1604 | our @ISA = CFClient::UI::Base::; |
1433 | |
1605 | |
1434 | use SDL::OpenGL; |
1606 | use CFClient::OpenGL; |
|
|
1607 | use Carp qw/confess/; |
1435 | |
1608 | |
1436 | my %tex = ( |
1609 | our %loaded_images; |
1437 | food => [ |
|
|
1438 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1439 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
|
|
1440 | ], |
|
|
1441 | grace => [ |
|
|
1442 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1443 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ |
|
|
1444 | ], |
|
|
1445 | hp => [ |
|
|
1446 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1447 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
|
|
1448 | ], |
|
|
1449 | mana => [ |
|
|
1450 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1451 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ |
|
|
1452 | ], |
|
|
1453 | ); |
|
|
1454 | |
1610 | |
1455 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
|
|
1456 | sub new { |
1611 | sub new { |
1457 | my $class = shift; |
1612 | my $class = shift; |
1458 | |
1613 | |
1459 | my $self = $class->SUPER::new (gauge => 'food', @_); |
1614 | my $self = $class->SUPER::new (can_events => 0, @_); |
|
|
1615 | |
|
|
1616 | $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; |
|
|
1617 | |
|
|
1618 | $loaded_images{$self->{image}} ||= |
|
|
1619 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; |
|
|
1620 | |
|
|
1621 | my $tex = $self->{tex} = $loaded_images{$self->{image}}; |
|
|
1622 | |
|
|
1623 | Scalar::Util::weaken $loaded_images{$self->{image}}; |
|
|
1624 | |
|
|
1625 | $self->{aspect} = $tex->{w} / $tex->{h}; |
1460 | |
1626 | |
1461 | $self |
1627 | $self |
1462 | } |
1628 | } |
1463 | |
1629 | |
1464 | sub size_request { |
1630 | sub size_request { |
1465 | my ($self) = @_; |
1631 | my ($self) = @_; |
1466 | |
1632 | |
1467 | my $tex = $tex{$self->{gauge}}[0]; |
1633 | ($self->{tex}->{w}, $self->{tex}->{h}) |
1468 | |
|
|
1469 | @$tex{qw(w h)} |
|
|
1470 | } |
|
|
1471 | |
|
|
1472 | sub set_max { |
|
|
1473 | my ($self, $max) = @_; |
|
|
1474 | |
|
|
1475 | $self->{max_val} = $max; |
|
|
1476 | } |
|
|
1477 | |
|
|
1478 | sub set_value { |
|
|
1479 | my ($self, $val, $max) = @_; |
|
|
1480 | |
|
|
1481 | $self->set_max ($max) |
|
|
1482 | if defined $max; |
|
|
1483 | |
|
|
1484 | $max = $self->{max_val}; |
|
|
1485 | $self->{val} = $val; |
|
|
1486 | |
|
|
1487 | $self->update; |
|
|
1488 | } |
1634 | } |
1489 | |
1635 | |
1490 | sub _draw { |
1636 | sub _draw { |
1491 | my ($self) = @_; |
1637 | my ($self) = @_; |
1492 | |
1638 | |
1493 | my $tex = $tex{$self->{gauge}}; |
1639 | my $tex = $self->{tex}; |
1494 | |
1640 | |
1495 | my ($w, $h) = ($self->{w}, $self->{h}); |
1641 | my ($w, $h) = ($self->{w}, $self->{h}); |
1496 | |
1642 | |
1497 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
1643 | if ($self->{rot90}) { |
1498 | $ycut = 1 if $self->{val} > $self->{max_val}; |
1644 | glRotate 90, 0, 0, 1; |
|
|
1645 | glTranslate 0, -$self->{w}, 0; |
1499 | |
1646 | |
1500 | my $t1 = $tex->[0]; |
1647 | ($w, $h) = ($h, $w); |
1501 | my $t2 = $tex->[1]; |
1648 | } |
1502 | |
1649 | |
1503 | glEnable GL_BLEND; |
1650 | glEnable GL_BLEND; |
1504 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1651 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1505 | glEnable GL_TEXTURE_2D; |
1652 | glEnable GL_TEXTURE_2D; |
1506 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1653 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1507 | |
1654 | |
|
|
1655 | $tex->draw_quad (0, 0, $w, $h); |
|
|
1656 | |
|
|
1657 | glDisable GL_BLEND; |
|
|
1658 | glDisable GL_TEXTURE_2D; |
|
|
1659 | } |
|
|
1660 | |
|
|
1661 | ############################################################################# |
|
|
1662 | |
|
|
1663 | package CFClient::UI::VGauge; |
|
|
1664 | |
|
|
1665 | our @ISA = CFClient::UI::Base::; |
|
|
1666 | |
|
|
1667 | use List::Util qw(min max); |
|
|
1668 | |
|
|
1669 | use CFClient::OpenGL; |
|
|
1670 | |
|
|
1671 | my %tex = ( |
|
|
1672 | food => [ |
|
|
1673 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1674 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
|
|
1675 | ], |
|
|
1676 | grace => [ |
|
|
1677 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1678 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
|
|
1679 | ], |
|
|
1680 | hp => [ |
|
|
1681 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1682 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
|
|
1683 | ], |
|
|
1684 | mana => [ |
|
|
1685 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1686 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
|
|
1687 | ], |
|
|
1688 | ); |
|
|
1689 | |
|
|
1690 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
|
|
1691 | sub new { |
|
|
1692 | my $class = shift; |
|
|
1693 | |
|
|
1694 | my $self = $class->SUPER::new ( |
|
|
1695 | type => 'food', |
|
|
1696 | @_ |
|
|
1697 | ); |
|
|
1698 | |
|
|
1699 | $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h}; |
|
|
1700 | |
|
|
1701 | $self |
|
|
1702 | } |
|
|
1703 | |
|
|
1704 | sub size_request { |
|
|
1705 | my ($self) = @_; |
|
|
1706 | |
|
|
1707 | #my $tex = $tex{$self->{type}}[0]; |
|
|
1708 | #@$tex{qw(w h)} |
|
|
1709 | (0, 0) |
|
|
1710 | } |
|
|
1711 | |
|
|
1712 | sub set_max { |
|
|
1713 | my ($self, $max) = @_; |
|
|
1714 | |
|
|
1715 | $self->{max_val} = $max; |
|
|
1716 | } |
|
|
1717 | |
|
|
1718 | sub set_value { |
|
|
1719 | my ($self, $val, $max) = @_; |
|
|
1720 | |
|
|
1721 | $self->set_max ($max) |
|
|
1722 | if defined $max; |
|
|
1723 | |
|
|
1724 | $max = $self->{max_val}; |
|
|
1725 | $self->{val} = $val; |
|
|
1726 | |
|
|
1727 | $self->update; |
|
|
1728 | } |
|
|
1729 | |
|
|
1730 | sub _draw { |
|
|
1731 | my ($self) = @_; |
|
|
1732 | |
|
|
1733 | my $tex = $tex{$self->{type}}; |
|
|
1734 | my ($t1, $t2, $t3) = @$tex; |
|
|
1735 | |
|
|
1736 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1737 | |
|
|
1738 | if ($self->{vertical}) { |
|
|
1739 | glRotate 90, 0, 0, 1; |
|
|
1740 | glTranslate 0, -$self->{w}, 0; |
|
|
1741 | |
|
|
1742 | ($w, $h) = ($h, $w); |
|
|
1743 | } |
|
|
1744 | |
|
|
1745 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
|
|
1746 | |
|
|
1747 | my $ycut1 = max 0, min 1, $ycut; |
|
|
1748 | my $ycut2 = max 0, min 1, $ycut - 1; |
|
|
1749 | |
1508 | my $h1 = $self->{h} - $ycut * $self->{h}; |
1750 | my $h1 = $self->{h} * (1 - $ycut1); |
1509 | my $h2 = $ycut * $self->{h}; |
1751 | my $h2 = $self->{h} * (1 - $ycut2); |
1510 | |
1752 | |
1511 | my $yp = 0; |
1753 | glEnable GL_BLEND; |
|
|
1754 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1755 | glEnable GL_TEXTURE_2D; |
|
|
1756 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1512 | |
1757 | |
1513 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
1758 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
1514 | glBegin GL_QUADS; |
1759 | glBegin GL_QUADS; |
1515 | glTexCoord 0 , 0; glVertex 0 , $yp; |
1760 | glTexCoord 0 , 0; glVertex 0 , 0; |
1516 | glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; |
1761 | glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1; |
1517 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; |
1762 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1; |
1518 | glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; |
1763 | glTexCoord $t1->{s}, 0; glVertex $w, 0; |
1519 | glEnd; |
1764 | glEnd; |
1520 | |
1765 | |
1521 | $yp += $h1; |
1766 | my $ycut1 = List::Util::min 1, $ycut; |
1522 | |
|
|
1523 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
1767 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
1524 | glBegin GL_QUADS; |
1768 | glBegin GL_QUADS; |
1525 | glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; |
1769 | glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1; |
1526 | glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; |
1770 | glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2; |
1527 | glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2; |
|
|
1528 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; |
1771 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2; |
|
|
1772 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1; |
1529 | glEnd; |
1773 | glEnd; |
|
|
1774 | |
|
|
1775 | if ($t3) { |
|
|
1776 | glBindTexture GL_TEXTURE_2D, $t3->{name}; |
|
|
1777 | glBegin GL_QUADS; |
|
|
1778 | glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2; |
|
|
1779 | glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h}; |
|
|
1780 | glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h}; |
|
|
1781 | glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2; |
|
|
1782 | glEnd; |
|
|
1783 | } |
1530 | |
1784 | |
1531 | glDisable GL_BLEND; |
1785 | glDisable GL_BLEND; |
1532 | glDisable GL_TEXTURE_2D; |
1786 | glDisable GL_TEXTURE_2D; |
1533 | } |
1787 | } |
1534 | |
1788 | |
1535 | ############################################################################# |
1789 | ############################################################################# |
1536 | |
1790 | |
|
|
1791 | package CFClient::UI::Gauge; |
|
|
1792 | |
|
|
1793 | our @ISA = CFClient::UI::VBox::; |
|
|
1794 | |
|
|
1795 | sub new { |
|
|
1796 | my ($class, %arg) = @_; |
|
|
1797 | |
|
|
1798 | my $self = $class->SUPER::new ( |
|
|
1799 | tooltip => $arg{type}, |
|
|
1800 | can_hover => 1, |
|
|
1801 | %arg, |
|
|
1802 | ); |
|
|
1803 | |
|
|
1804 | $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); |
|
|
1805 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
|
|
1806 | $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999"); |
|
|
1807 | |
|
|
1808 | $self |
|
|
1809 | } |
|
|
1810 | |
|
|
1811 | sub set_fontsize { |
|
|
1812 | my ($self, $fsize) = @_; |
|
|
1813 | |
|
|
1814 | $self->{value}->set_fontsize ($fsize); |
|
|
1815 | $self->{max} ->set_fontsize ($fsize); |
|
|
1816 | } |
|
|
1817 | |
|
|
1818 | sub set_value { |
|
|
1819 | my ($self, $val, $max) = @_; |
|
|
1820 | |
|
|
1821 | $self->set_max ($max) |
|
|
1822 | if defined $max; |
|
|
1823 | |
|
|
1824 | $self->{gauge}->set_value ($val, $max); |
|
|
1825 | $self->{value}->set_text ($val); |
|
|
1826 | } |
|
|
1827 | |
|
|
1828 | sub set_max { |
|
|
1829 | my ($self, $max) = @_; |
|
|
1830 | |
|
|
1831 | $self->{gauge}->set_max ($max); |
|
|
1832 | $self->{max}->set_text ($max); |
|
|
1833 | } |
|
|
1834 | |
|
|
1835 | ############################################################################# |
|
|
1836 | |
1537 | package CFClient::UI::Slider; |
1837 | package CFClient::UI::Slider; |
1538 | |
1838 | |
1539 | use strict; |
1839 | use strict; |
1540 | |
1840 | |
1541 | use SDL::OpenGL; |
1841 | use CFClient::OpenGL; |
1542 | |
1842 | |
1543 | our @ISA = CFClient::UI::DrawBG::; |
1843 | our @ISA = CFClient::UI::DrawBG::; |
1544 | |
1844 | |
1545 | my @tex = |
1845 | my @tex = |
1546 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1846 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
… | |
… | |
1671 | |
1971 | |
1672 | package CFClient::UI::TextView; |
1972 | package CFClient::UI::TextView; |
1673 | |
1973 | |
1674 | our @ISA = CFClient::UI::HBox::; |
1974 | our @ISA = CFClient::UI::HBox::; |
1675 | |
1975 | |
1676 | use SDL::OpenGL; |
1976 | use CFClient::OpenGL; |
1677 | |
1977 | |
1678 | sub new { |
1978 | sub new { |
1679 | my $class = shift; |
1979 | my $class = shift; |
1680 | |
1980 | |
1681 | my $self = $class->SUPER::new ( |
1981 | my $self = $class->SUPER::new ( |
1682 | fontsize => 1, |
1982 | fontsize => 1, |
|
|
1983 | can_events => 0, |
|
|
1984 | #font => default_font |
1683 | @_, |
1985 | @_, |
1684 | |
1986 | |
1685 | layout => (new CFClient::Layout), |
1987 | layout => (new CFClient::Layout), |
1686 | par => [], |
1988 | par => [], |
1687 | height => 0, |
1989 | height => 0, |
1688 | children => [ |
1990 | children => [ |
1689 | (new CFClient::UI::Empty expand => 1), |
1991 | (new CFClient::UI::Empty expand => 1), |
1690 | (new CFClient::UI::Slider vertical => 1), |
1992 | (new CFClient::UI::Slider vertical => 1), |
1691 | ], |
1993 | ], |
1692 | ); |
1994 | ); |
1693 | |
1995 | |
… | |
… | |
1727 | sub size_allocate { |
2029 | sub size_allocate { |
1728 | my ($self, $w, $h) = @_; |
2030 | my ($self, $w, $h) = @_; |
1729 | |
2031 | |
1730 | $self->SUPER::size_allocate ($w, $h); |
2032 | $self->SUPER::size_allocate ($w, $h); |
1731 | |
2033 | |
|
|
2034 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1732 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
2035 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1733 | $self->{layout}->set_width ($self->{children}[0]{w}); |
2036 | $self->{layout}->set_width ($self->{children}[0]{w}); |
1734 | |
2037 | |
1735 | $self->reflow; |
2038 | $self->reflow; |
1736 | } |
2039 | } |
… | |
… | |
1788 | my $y1 = $top + $self->{h}; |
2091 | my $y1 = $top + $self->{h}; |
1789 | |
2092 | |
1790 | my $y = 0; |
2093 | my $y = 0; |
1791 | |
2094 | |
1792 | my $layout = $self->{layout}; |
2095 | my $layout = $self->{layout}; |
|
|
2096 | |
|
|
2097 | $layout->set_font ($self->{font}) if $self->{font}; |
1793 | |
2098 | |
1794 | for my $par (@{$self->{par}}) { |
2099 | for my $par (@{$self->{par}}) { |
1795 | my $h = $par->[0]; |
2100 | my $h = $par->[0]; |
1796 | |
2101 | |
1797 | if ($y0 < $y + $h && $y < $y1) { |
2102 | if ($y0 < $y + $h && $y < $y1) { |
… | |
… | |
1825 | |
2130 | |
1826 | } |
2131 | } |
1827 | |
2132 | |
1828 | ############################################################################# |
2133 | ############################################################################# |
1829 | |
2134 | |
1830 | package CFClient::UI::MapWidget; |
|
|
1831 | |
|
|
1832 | use strict; |
|
|
1833 | |
|
|
1834 | use List::Util qw(min max); |
|
|
1835 | |
|
|
1836 | use SDL; |
|
|
1837 | use SDL::OpenGL; |
|
|
1838 | |
|
|
1839 | our @ISA = CFClient::UI::Base::; |
|
|
1840 | |
|
|
1841 | sub new { |
|
|
1842 | my $class = shift; |
|
|
1843 | |
|
|
1844 | $class->SUPER::new ( |
|
|
1845 | z => -1, |
|
|
1846 | can_focus => 1, |
|
|
1847 | list => (glGenLists 1), |
|
|
1848 | @_ |
|
|
1849 | ) |
|
|
1850 | } |
|
|
1851 | |
|
|
1852 | sub key_down { |
|
|
1853 | print "MAPKEYDOWN\n"; |
|
|
1854 | } |
|
|
1855 | |
|
|
1856 | sub key_up { |
|
|
1857 | } |
|
|
1858 | |
|
|
1859 | sub button_down { |
|
|
1860 | my ($self, $ev, $x, $y) = @_; |
|
|
1861 | |
|
|
1862 | $self->focus_in; |
|
|
1863 | |
|
|
1864 | if ($ev->button == 2) { |
|
|
1865 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
1866 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
|
|
1867 | |
|
|
1868 | $self->{motion} = sub { |
|
|
1869 | my ($ev, $x, $y) = @_; |
|
|
1870 | |
|
|
1871 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
1872 | |
|
|
1873 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
|
|
1874 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
|
|
1875 | |
|
|
1876 | $self->update; |
|
|
1877 | }; |
|
|
1878 | } |
|
|
1879 | } |
|
|
1880 | |
|
|
1881 | sub button_up { |
|
|
1882 | my ($self, $ev, $x, $y) = @_; |
|
|
1883 | |
|
|
1884 | delete $self->{motion}; |
|
|
1885 | } |
|
|
1886 | |
|
|
1887 | sub mouse_motion { |
|
|
1888 | my ($self, $ev, $x, $y) = @_; |
|
|
1889 | |
|
|
1890 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
|
|
1891 | } |
|
|
1892 | |
|
|
1893 | sub size_request { |
|
|
1894 | ( |
|
|
1895 | 1 + 32 * int $::WIDTH / 32, |
|
|
1896 | 1 + 32 * int $::HEIGHT / 32, |
|
|
1897 | ) |
|
|
1898 | } |
|
|
1899 | |
|
|
1900 | sub update { |
|
|
1901 | my ($self) = @_; |
|
|
1902 | |
|
|
1903 | $self->{need_update} = 1; |
|
|
1904 | $self->SUPER::update; |
|
|
1905 | } |
|
|
1906 | |
|
|
1907 | sub draw { |
|
|
1908 | my ($self) = @_; |
|
|
1909 | |
|
|
1910 | if (delete $self->{need_update}) { |
|
|
1911 | glNewList $self->{list}, GL_COMPILE; |
|
|
1912 | |
|
|
1913 | if ($::MAP) { |
|
|
1914 | my $sw = int $::WIDTH / 32; |
|
|
1915 | my $sh = int $::HEIGHT / 32; |
|
|
1916 | |
|
|
1917 | my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
|
|
1918 | my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
|
|
1919 | |
|
|
1920 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
|
|
1921 | |
|
|
1922 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
|
|
1923 | |
|
|
1924 | if ($::CFG->{fow_enable}) { |
|
|
1925 | if ($::CFG->{fow_smooth} && $CFClient::GL_VERSION >= 1.2) { # smooth fog of war |
|
|
1926 | glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER); |
|
|
1927 | glConvolutionFilter2D ( |
|
|
1928 | GL_CONVOLUTION_2D, |
|
|
1929 | GL_ALPHA, |
|
|
1930 | 3, 3, |
|
|
1931 | GL_ALPHA, GL_FLOAT, |
|
|
1932 | pack "f*", |
|
|
1933 | 0.1, 0.1, 0.1, |
|
|
1934 | 0.1, 0.2, 0.1, |
|
|
1935 | 0.1, 0.1, 0.1, |
|
|
1936 | ); |
|
|
1937 | glEnable GL_CONVOLUTION_2D; |
|
|
1938 | } |
|
|
1939 | |
|
|
1940 | $self->{fow_texture} = new CFClient::Texture |
|
|
1941 | w => $w, |
|
|
1942 | h => $h, |
|
|
1943 | data => $data, |
|
|
1944 | internalformat => GL_ALPHA, |
|
|
1945 | format => GL_ALPHA; |
|
|
1946 | |
|
|
1947 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
|
|
1948 | |
|
|
1949 | glEnable GL_BLEND; |
|
|
1950 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1951 | glEnable GL_TEXTURE_2D; |
|
|
1952 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1953 | |
|
|
1954 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1955 | $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); |
|
|
1956 | |
|
|
1957 | glDisable GL_TEXTURE_2D; |
|
|
1958 | glDisable GL_BLEND; |
|
|
1959 | } |
|
|
1960 | |
|
|
1961 | # HACK BEGIN |
|
|
1962 | { |
|
|
1963 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
1964 | my ($w, $h) = (250, 250); |
|
|
1965 | |
|
|
1966 | glEnable GL_BLEND; |
|
|
1967 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1968 | glEnable GL_TEXTURE_2D; |
|
|
1969 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1970 | |
|
|
1971 | $self->{mapmap_texture} = |
|
|
1972 | new CFClient::Texture |
|
|
1973 | w => $w, |
|
|
1974 | h => $h, |
|
|
1975 | data => $::MAP->mapmap ($w, $h), |
|
|
1976 | type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
|
|
1977 | |
|
|
1978 | $self->{mapmap_texture}->draw_quad (100, 100); |
|
|
1979 | |
|
|
1980 | glDisable GL_TEXTURE_2D; |
|
|
1981 | glDisable GL_BLEND; |
|
|
1982 | } |
|
|
1983 | # HACK END |
|
|
1984 | } |
|
|
1985 | |
|
|
1986 | glEndList; |
|
|
1987 | } |
|
|
1988 | |
|
|
1989 | glPushMatrix; |
|
|
1990 | glCallList $self->{list}; |
|
|
1991 | glPopMatrix; |
|
|
1992 | |
|
|
1993 | if ($FOCUS != $self) { |
|
|
1994 | glColor 64/255, 64/255, 64/255; |
|
|
1995 | glLogicOp GL_AND; |
|
|
1996 | glEnable GL_COLOR_LOGIC_OP; |
|
|
1997 | glBegin GL_QUADS; |
|
|
1998 | glVertex 0, 0; |
|
|
1999 | glVertex 0, $::HEIGHT; |
|
|
2000 | glVertex $::WIDTH, $::HEIGHT; |
|
|
2001 | glVertex $::WIDTH, 0; |
|
|
2002 | glEnd; |
|
|
2003 | glDisable GL_COLOR_LOGIC_OP; |
|
|
2004 | } |
|
|
2005 | } |
|
|
2006 | |
|
|
2007 | my %DIR = ( |
|
|
2008 | SDLK_KP8, [1, "north"], |
|
|
2009 | SDLK_KP9, [2, "northeast"], |
|
|
2010 | SDLK_KP6, [3, "east"], |
|
|
2011 | SDLK_KP3, [4, "southeast"], |
|
|
2012 | SDLK_KP2, [5, "south"], |
|
|
2013 | SDLK_KP1, [6, "southwest"], |
|
|
2014 | SDLK_KP4, [7, "west"], |
|
|
2015 | SDLK_KP7, [8, "northwest"], |
|
|
2016 | |
|
|
2017 | SDLK_UP, [1, "north"], |
|
|
2018 | SDLK_RIGHT, [3, "east"], |
|
|
2019 | SDLK_DOWN, [5, "south"], |
|
|
2020 | SDLK_LEFT, [7, "west"], |
|
|
2021 | ); |
|
|
2022 | |
|
|
2023 | sub key_down { |
|
|
2024 | my ($self, $ev) = @_; |
|
|
2025 | |
|
|
2026 | my $mod = $ev->key_mod; |
|
|
2027 | my $sym = $ev->key_sym; |
|
|
2028 | |
|
|
2029 | if ($sym == SDLK_KP5) { |
|
|
2030 | $::CONN->user_send ("stay fire"); |
|
|
2031 | } elsif ($sym == SDLK_a) { |
|
|
2032 | $::CONN->user_send ("apply"); |
|
|
2033 | } elsif ($sym == SDLK_QUOTE) { |
|
|
2034 | $self->emit ('activate_console'); |
|
|
2035 | } elsif ($sym == SDLK_SLASH) { |
|
|
2036 | $self->emit ('activate_console' => '/'); |
|
|
2037 | } elsif (exists $DIR{$sym}) { |
|
|
2038 | if ($mod & KMOD_SHIFT) { |
|
|
2039 | $self->{shft}++; |
|
|
2040 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
|
|
2041 | } elsif ($mod & KMOD_CTRL) { |
|
|
2042 | $self->{ctrl}++; |
|
|
2043 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
|
|
2044 | } else { |
|
|
2045 | $::CONN->user_send ("$DIR{$sym}[1]"); |
|
|
2046 | } |
|
|
2047 | } |
|
|
2048 | } |
|
|
2049 | |
|
|
2050 | sub key_up { |
|
|
2051 | my ($self, $ev) = @_; |
|
|
2052 | |
|
|
2053 | my $mod = $ev->key_mod; |
|
|
2054 | my $sym = $ev->key_sym; |
|
|
2055 | |
|
|
2056 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
|
|
2057 | $::CONN->user_send ("fire_stop"); |
|
|
2058 | } |
|
|
2059 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
|
|
2060 | $::CONN->user_send ("run_stop"); |
|
|
2061 | } |
|
|
2062 | } |
|
|
2063 | |
|
|
2064 | ############################################################################# |
|
|
2065 | |
|
|
2066 | package CFClient::UI::Animator; |
2135 | package CFClient::UI::Animator; |
2067 | |
2136 | |
2068 | use SDL::OpenGL; |
2137 | use CFClient::OpenGL; |
2069 | |
2138 | |
2070 | our @ISA = CFClient::UI::Bin::; |
2139 | our @ISA = CFClient::UI::Bin::; |
2071 | |
2140 | |
2072 | sub moveto { |
2141 | sub moveto { |
2073 | my ($self, $x, $y) = @_; |
2142 | my ($self, $x, $y) = @_; |
… | |
… | |
2111 | |
2180 | |
2112 | sub new { |
2181 | sub new { |
2113 | my $class = shift; |
2182 | my $class = shift; |
2114 | |
2183 | |
2115 | my $self = $class->SUPER::new ( |
2184 | my $self = $class->SUPER::new ( |
2116 | state => 0, |
2185 | state => 0, |
2117 | connect_activate => \&toggle_flopper, |
2186 | connect_activate => \&toggle_flopper, |
2118 | @_ |
2187 | @_ |
2119 | ); |
2188 | ); |
2120 | |
2189 | |
2121 | if ($self->{state}) { |
2190 | if ($self->{state}) { |
… | |
… | |
2142 | $self->emit (changed => $self->{state}); |
2211 | $self->emit (changed => $self->{state}); |
2143 | } |
2212 | } |
2144 | |
2213 | |
2145 | ############################################################################# |
2214 | ############################################################################# |
2146 | |
2215 | |
|
|
2216 | package CFClient::UI::Tooltip; |
|
|
2217 | |
|
|
2218 | our @ISA = CFClient::UI::Bin::; |
|
|
2219 | |
|
|
2220 | use CFClient::OpenGL; |
|
|
2221 | |
|
|
2222 | sub new { |
|
|
2223 | my $class = shift; |
|
|
2224 | |
|
|
2225 | $class->SUPER::new ( |
|
|
2226 | @_, |
|
|
2227 | can_events => 0, |
|
|
2228 | ) |
|
|
2229 | } |
|
|
2230 | |
|
|
2231 | sub set_markup { |
|
|
2232 | my ($self, $text) = @_; |
|
|
2233 | |
|
|
2234 | $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; |
|
|
2235 | $self->{label}->set_markup ($text); |
|
|
2236 | $self->add ($self->{label}); |
|
|
2237 | } |
|
|
2238 | |
|
|
2239 | sub size_request { |
|
|
2240 | my ($self) = @_; |
|
|
2241 | |
|
|
2242 | $self->child->set_max_size ($::WIDTH * 0.3); |
|
|
2243 | |
|
|
2244 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
|
|
2245 | |
|
|
2246 | ($w + 4, $h + 4) |
|
|
2247 | } |
|
|
2248 | |
|
|
2249 | sub size_allocate { |
|
|
2250 | my ($self, $w, $h) = @_; |
|
|
2251 | |
|
|
2252 | $self->SUPER::size_allocate ($w - 4, $h - 4); |
|
|
2253 | } |
|
|
2254 | |
|
|
2255 | sub _draw { |
|
|
2256 | my ($self) = @_; |
|
|
2257 | |
|
|
2258 | glPushMatrix; |
|
|
2259 | glTranslate 0.375, 0.375; |
|
|
2260 | |
|
|
2261 | my ($w, $h) = @$self{qw(w h)}; |
|
|
2262 | |
|
|
2263 | glColor 1, 0.8, 0.4; |
|
|
2264 | glBegin GL_QUADS; |
|
|
2265 | glVertex 0 , 0; |
|
|
2266 | glVertex 0 , $h; |
|
|
2267 | glVertex $w, $h; |
|
|
2268 | glVertex $w, 0; |
|
|
2269 | glEnd; |
|
|
2270 | |
|
|
2271 | glColor 0, 0, 0; |
|
|
2272 | glBegin GL_LINE_LOOP; |
|
|
2273 | glVertex 0 , 0; |
|
|
2274 | glVertex 0 , $h; |
|
|
2275 | glVertex $w, $h; |
|
|
2276 | glVertex $w, 0; |
|
|
2277 | glEnd; |
|
|
2278 | |
|
|
2279 | glPopMatrix; |
|
|
2280 | |
|
|
2281 | glTranslate 2, 2; |
|
|
2282 | $self->SUPER::_draw; |
|
|
2283 | } |
|
|
2284 | |
|
|
2285 | ############################################################################# |
|
|
2286 | |
|
|
2287 | package CFClient::UI::Face; |
|
|
2288 | |
|
|
2289 | our @ISA = CFClient::UI::Base::; |
|
|
2290 | |
|
|
2291 | use CFClient::OpenGL; |
|
|
2292 | |
|
|
2293 | sub new { |
|
|
2294 | my $class = shift; |
|
|
2295 | |
|
|
2296 | $class->SUPER::new ( |
|
|
2297 | aspect => 1, |
|
|
2298 | @_, |
|
|
2299 | ) |
|
|
2300 | } |
|
|
2301 | |
|
|
2302 | sub size_request { |
|
|
2303 | (32, 8) |
|
|
2304 | } |
|
|
2305 | |
|
|
2306 | sub draw { |
|
|
2307 | my ($self) = @_; |
|
|
2308 | |
|
|
2309 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
|
|
2310 | |
|
|
2311 | if ($tex) { |
|
|
2312 | glEnable GL_BLEND; |
|
|
2313 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2314 | glEnable GL_TEXTURE_2D; |
|
|
2315 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
2316 | glColor 1, 1, 1, 1; |
|
|
2317 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
|
|
2318 | glDisable GL_TEXTURE_2D; |
|
|
2319 | glDisable GL_BLEND; |
|
|
2320 | } |
|
|
2321 | } |
|
|
2322 | |
|
|
2323 | ############################################################################# |
|
|
2324 | |
2147 | package CFClient::UI::Root; |
2325 | package CFClient::UI::Root; |
2148 | |
2326 | |
2149 | our @ISA = CFClient::UI::Container::; |
2327 | our @ISA = CFClient::UI::Container::; |
2150 | |
2328 | |
2151 | use SDL::OpenGL; |
2329 | use CFClient::OpenGL; |
2152 | |
2330 | |
2153 | sub check_size { |
2331 | sub check_size { |
2154 | my ($self) = @_; |
2332 | my ($self) = @_; |
2155 | |
2333 | |
2156 | $self->configure (0, 0, $::WITH, $::HEIGHT); |
2334 | $self->configure (0, 0, $::WIDTH, $::HEIGHT); |
2157 | } |
2335 | } |
2158 | |
2336 | |
2159 | sub size_request { |
2337 | sub size_request { |
2160 | ($::WIDTH, $::HEIGHT) |
2338 | ($::WIDTH, $::HEIGHT) |
2161 | } |
2339 | } |
… | |
… | |
2163 | sub configure { |
2341 | sub configure { |
2164 | my ($self, $x, $y, $w, $h) = @_; |
2342 | my ($self, $x, $y, $w, $h) = @_; |
2165 | |
2343 | |
2166 | $self->SUPER::configure ($x, $y, $w, $h); |
2344 | $self->SUPER::configure ($x, $y, $w, $h); |
2167 | |
2345 | |
2168 | $_->configure ($_->{x}, $_->{y}, $_->size_request) |
|
|
2169 | for @{$self->{children}}; |
2346 | for my $child (@{$self->{children}}) { |
|
|
2347 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
|
|
2348 | |
|
|
2349 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
|
|
2350 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
|
|
2351 | $child->configure ($X, $Y, $W,$H); |
|
|
2352 | } |
2170 | } |
2353 | } |
2171 | |
2354 | |
2172 | sub _topleft { |
2355 | sub _topleft { |
2173 | my ($self, $x, $y) = @_; |
2356 | my ($self, $x, $y) = @_; |
2174 | |
2357 | |
… | |
… | |
2182 | ::refresh (); |
2365 | ::refresh (); |
2183 | } |
2366 | } |
2184 | |
2367 | |
2185 | sub add { |
2368 | sub add { |
2186 | my ($self, $child) = @_; |
2369 | my ($self, $child) = @_; |
|
|
2370 | |
|
|
2371 | # integerize window positions |
|
|
2372 | $child->{x} = int $child->{x}; |
|
|
2373 | $child->{y} = int $child->{y}; |
2187 | |
2374 | |
2188 | $self->SUPER::add ($child); |
2375 | $self->SUPER::add ($child); |
2189 | } |
2376 | } |
2190 | |
2377 | |
2191 | sub on_refresh { |
2378 | sub on_refresh { |
… | |
… | |
2217 | ############################################################################# |
2404 | ############################################################################# |
2218 | |
2405 | |
2219 | package CFClient::UI; |
2406 | package CFClient::UI; |
2220 | |
2407 | |
2221 | $ROOT = new CFClient::UI::Root; |
2408 | $ROOT = new CFClient::UI::Root; |
|
|
2409 | $TOOLTIP = new CFClient::UI::Tooltip; |
2222 | |
2410 | |
2223 | 1 |
2411 | 1 |
2224 | |
2412 | |