… | |
… | |
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 (exists $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_text ($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->key_down ($_[0]) if $FOCUS; |
18 | } |
45 | } |
… | |
… | |
21 | $FOCUS->key_up ($_[0]) if $FOCUS; |
48 | $FOCUS->key_up ($_[0]) if $FOCUS; |
22 | } |
49 | } |
23 | |
50 | |
24 | sub feed_sdl_button_down_event { |
51 | sub feed_sdl_button_down_event { |
25 | my ($ev) = @_; |
52 | my ($ev) = @_; |
26 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
53 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
27 | |
54 | |
28 | if (!$BUTTON_STATE) { |
55 | if (!$BUTTON_STATE) { |
29 | my $widget = $ROOT->find_widget ($x, $y); |
56 | my $widget = $ROOT->find_widget ($x, $y); |
30 | |
57 | |
31 | $GRAB = $widget; |
58 | $GRAB = $widget; |
32 | $GRAB->update if $GRAB; |
59 | $GRAB->update if $GRAB; |
33 | } |
|
|
34 | |
60 | |
|
|
61 | check_tooltip; |
|
|
62 | } |
|
|
63 | |
35 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
64 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
36 | |
65 | |
37 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
66 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
38 | } |
67 | } |
39 | |
68 | |
40 | sub feed_sdl_button_up_event { |
69 | sub feed_sdl_button_up_event { |
41 | my ($ev) = @_; |
70 | my ($ev) = @_; |
42 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
71 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
43 | |
72 | |
44 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
73 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
45 | |
74 | |
46 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
75 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
47 | |
76 | |
48 | $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
77 | $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
49 | |
78 | |
50 | if (!$BUTTON_STATE) { |
79 | if (!$BUTTON_STATE) { |
51 | my $grab = $GRAB; undef $GRAB; |
80 | my $grab = $GRAB; undef $GRAB; |
52 | $grab->update if $grab; |
81 | $grab->update if $grab; |
53 | $GRAB->update if $GRAB; |
82 | $GRAB->update if $GRAB; |
|
|
83 | |
|
|
84 | check_tooltip; |
54 | } |
85 | } |
55 | } |
86 | } |
56 | |
87 | |
57 | sub feed_sdl_motion_event { |
88 | sub feed_sdl_motion_event { |
58 | my ($ev) = @_; |
89 | my ($ev) = @_; |
59 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
90 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
60 | |
91 | |
61 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
92 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
62 | |
93 | |
63 | if ($widget != $HOVER) { |
94 | if ($widget != $HOVER) { |
64 | my $hover = $HOVER; $HOVER = $widget; |
95 | my $hover = $HOVER; $HOVER = $widget; |
65 | |
96 | |
66 | $hover->update if $hover && $hover->{can_hover}; |
97 | $hover->update if $hover && $hover->{can_hover}; |
67 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
98 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
|
|
99 | |
|
|
100 | check_tooltip; |
68 | } |
101 | } |
69 | |
102 | |
70 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
103 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
71 | } |
104 | } |
72 | |
105 | |
… | |
… | |
87 | |
120 | |
88 | package CFClient::UI::Base; |
121 | package CFClient::UI::Base; |
89 | |
122 | |
90 | use strict; |
123 | use strict; |
91 | |
124 | |
92 | use SDL::OpenGL; |
125 | use CFClient::OpenGL; |
93 | |
126 | |
94 | sub new { |
127 | sub new { |
95 | my $class = shift; |
128 | my $class = shift; |
96 | |
129 | |
97 | my $self = bless { |
130 | my $self = bless { |
98 | x => 0, |
131 | x => 0, |
99 | y => 0, |
132 | y => 0, |
100 | z => 0, |
133 | z => 0, |
101 | w => -1, |
134 | can_events => 1, |
102 | h => -1, |
|
|
103 | @_ |
135 | @_ |
104 | }, $class; |
136 | }, $class; |
105 | |
137 | |
106 | for (keys %$self) { |
138 | for (keys %$self) { |
107 | if (/^connect_(.*)$/) { |
139 | if (/^connect_(.*)$/) { |
… | |
… | |
110 | } |
142 | } |
111 | |
143 | |
112 | $self |
144 | $self |
113 | } |
145 | } |
114 | |
146 | |
|
|
147 | sub show { |
|
|
148 | my ($self) = @_; |
|
|
149 | |
|
|
150 | return if $self->{parent}; |
|
|
151 | |
|
|
152 | $CFClient::UI::ROOT->add ($self); |
|
|
153 | } |
|
|
154 | |
|
|
155 | sub hide { |
|
|
156 | my ($self) = @_; |
|
|
157 | |
|
|
158 | return unless $self->{parent}; |
|
|
159 | |
|
|
160 | $self->{parent}->remove ($self); |
|
|
161 | } |
|
|
162 | |
115 | sub move { |
163 | sub move { |
116 | my ($self, $x, $y, $z) = @_; |
164 | my ($self, $x, $y, $z) = @_; |
|
|
165 | |
117 | $self->{x} = int $x; |
166 | $self->{x} = int $x; |
118 | $self->{y} = int $y; |
167 | $self->{y} = int $y; |
119 | $self->{z} = $z if defined $z; |
168 | $self->{z} = $z if defined $z; |
|
|
169 | |
|
|
170 | $self->update; |
120 | } |
171 | } |
121 | |
172 | |
122 | sub needs_redraw { |
173 | sub needs_redraw { |
123 | 0 |
174 | 0 |
124 | } |
175 | } |
125 | |
176 | |
126 | sub size_request { |
177 | sub size_request { |
127 | require Carp; |
178 | require Carp; |
128 | Carp::confess "size_request is abtract"; |
179 | Carp::confess "size_request is abstract"; |
129 | } |
180 | } |
130 | |
181 | |
131 | sub _size_allocate { |
182 | sub configure { |
132 | my ($self, $x, $y, $w, $h) = @_; |
183 | my ($self, $x, $y, $w, $h) = @_; |
133 | |
184 | |
|
|
185 | if ($self->{aspect}) { |
|
|
186 | my $w2 = List::Util::min $w, int $h * $self->{aspect}; |
|
|
187 | my $h2 = List::Util::min $h, int $w / $self->{aspect}; |
|
|
188 | |
|
|
189 | # use alignment to adjust x, y |
|
|
190 | |
|
|
191 | $x += int +($w - $w2) * 0.5; |
|
|
192 | $y += int +($h - $h2) * 0.5; |
|
|
193 | |
|
|
194 | ($w, $h) = ($w2, $h2); |
|
|
195 | } |
|
|
196 | |
|
|
197 | if ($self->{x} != $x || $self->{y} != $y) { |
134 | $self->{x} = $x; |
198 | $self->{x} = $x; |
135 | $self->{y} = $y; |
199 | $self->{y} = $y; |
|
|
200 | $self->update; |
|
|
201 | } |
136 | |
202 | |
137 | return unless $self->{w} != $w || $self->{h} != $h; |
203 | if ($self->{w} != $w || $self->{h} != $h) { |
138 | |
|
|
139 | $self->{w} = $w; |
204 | $self->{w} = $w; |
140 | $self->{h} = $h; |
205 | $self->{h} = $h; |
141 | |
206 | |
|
|
207 | $self->size_allocate ($w, $h); |
|
|
208 | $self->update; |
142 | 1 |
209 | } |
143 | } |
210 | } |
144 | |
211 | |
145 | sub size_allocate { |
212 | sub size_allocate { |
146 | my ($self, $x, $y, $w, $h) = @_; |
213 | # nothing to be done |
147 | |
|
|
148 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
149 | } |
214 | } |
150 | |
215 | |
151 | # return top left coordinates |
216 | # return top left coordinates |
152 | sub _topleft { |
217 | sub _topleft { |
153 | my ($self, $x, $y) = @_; |
218 | my ($self, $x, $y) = @_; |
… | |
… | |
247 | } |
312 | } |
248 | |
313 | |
249 | sub find_widget { |
314 | sub find_widget { |
250 | my ($self, $x, $y) = @_; |
315 | my ($self, $x, $y) = @_; |
251 | |
316 | |
|
|
317 | return () unless $self->{can_events}; |
|
|
318 | |
252 | return $self |
319 | return $self |
253 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
320 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
254 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
321 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
255 | |
322 | |
256 | () |
323 | () |
… | |
… | |
260 | my ($self, $parent) = @_; |
327 | my ($self, $parent) = @_; |
261 | |
328 | |
262 | Scalar::Util::weaken ($self->{parent} = $parent); |
329 | Scalar::Util::weaken ($self->{parent} = $parent); |
263 | } |
330 | } |
264 | |
331 | |
|
|
332 | sub check_size { |
|
|
333 | my ($self) = @_; |
|
|
334 | |
|
|
335 | return unless $self->{parent}; |
|
|
336 | |
|
|
337 | my ($w, $h) = $self->size_request; |
|
|
338 | |
|
|
339 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
|
|
340 | $self->{req_w} = $w; |
|
|
341 | $self->{req_h} = $h; |
|
|
342 | |
|
|
343 | $self->{parent}->check_size; |
|
|
344 | } |
|
|
345 | } |
|
|
346 | |
265 | sub update { |
347 | sub update { |
266 | my ($self) = @_; |
348 | my ($self) = @_; |
267 | |
349 | |
268 | $self->{parent}->update |
350 | $self->{parent}->update |
269 | if $self->{parent}; |
351 | if $self->{parent}; |
270 | } |
352 | } |
271 | |
353 | |
272 | sub connect { |
354 | sub connect { |
273 | my ($self, $signal, $cb) = @_; |
355 | my ($self, $signal, $cb) = @_; |
274 | |
356 | |
275 | push @{ $self->{cb}{$signal} }, $cb; |
357 | push @{ $self->{signal_cb}{$signal} }, $cb; |
276 | } |
358 | } |
277 | |
359 | |
278 | sub emit { |
360 | sub emit { |
279 | my ($self, $signal, @args) = @_; |
361 | my ($self, $signal, @args) = @_; |
280 | |
362 | |
|
|
363 | for my $cb (@{$self->{signal_cb}{$signal} || []}) { |
281 | $_->($self, @args) |
364 | $cb->($self, @args); |
282 | for @{$self->{cb}{$signal} || []}; |
365 | } |
283 | } |
366 | } |
284 | |
367 | |
285 | sub DESTROY { |
368 | sub DESTROY { |
286 | my ($self) = @_; |
369 | my ($self) = @_; |
287 | |
370 | |
… | |
… | |
293 | package CFClient::UI::DrawBG; |
376 | package CFClient::UI::DrawBG; |
294 | |
377 | |
295 | our @ISA = CFClient::UI::Base::; |
378 | our @ISA = CFClient::UI::Base::; |
296 | |
379 | |
297 | use strict; |
380 | use strict; |
298 | use SDL::OpenGL; |
381 | use CFClient::OpenGL; |
299 | |
382 | |
300 | sub new { |
383 | sub new { |
301 | my $class = shift; |
384 | my $class = shift; |
302 | |
385 | |
303 | # range [value, low, high, page] |
386 | # range [value, low, high, page] |
… | |
… | |
332 | |
415 | |
333 | package CFClient::UI::Empty; |
416 | package CFClient::UI::Empty; |
334 | |
417 | |
335 | our @ISA = CFClient::UI::Base::; |
418 | our @ISA = CFClient::UI::Base::; |
336 | |
419 | |
|
|
420 | sub new { |
|
|
421 | my ($class, %arg) = @_; |
|
|
422 | $class->SUPER::new (can_events => 0, %arg); |
|
|
423 | } |
|
|
424 | |
337 | sub size_request { |
425 | sub size_request { |
338 | (0, 0) |
426 | (0, 0) |
339 | } |
427 | } |
340 | |
428 | |
341 | sub draw { } |
429 | sub draw { } |
… | |
… | |
349 | sub new { |
437 | sub new { |
350 | my ($class, %arg) = @_; |
438 | my ($class, %arg) = @_; |
351 | |
439 | |
352 | my $children = delete $arg{children} || []; |
440 | my $children = delete $arg{children} || []; |
353 | |
441 | |
354 | my $self = $class->SUPER::new (children => [], %arg); |
442 | my $self = $class->SUPER::new (children => [], can_events => 0, %arg); |
355 | $self->add ($_) for @$children; |
443 | $self->add ($_) for @$children; |
356 | |
444 | |
357 | $self |
445 | $self |
358 | } |
446 | } |
359 | |
447 | |
… | |
… | |
367 | $self->{children} = [ |
455 | $self->{children} = [ |
368 | sort { $a->{z} <=> $b->{z} } |
456 | sort { $a->{z} <=> $b->{z} } |
369 | @{$self->{children}}, $child |
457 | @{$self->{children}}, $child |
370 | ]; |
458 | ]; |
371 | |
459 | |
372 | $self->{w} = $self->{h} = -1; |
460 | $child->check_size; |
|
|
461 | } |
|
|
462 | |
|
|
463 | sub remove { |
|
|
464 | my ($self, $child) = @_; |
|
|
465 | |
|
|
466 | delete $child->{parent}; |
|
|
467 | |
|
|
468 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
|
|
469 | |
|
|
470 | $self->check_size; |
373 | $self->update; |
471 | $self->update; |
374 | } |
|
|
375 | |
|
|
376 | sub remove { |
|
|
377 | my ($self, $widget) = @_; |
|
|
378 | |
|
|
379 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
|
|
380 | |
|
|
381 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
|
|
382 | } |
472 | } |
383 | |
473 | |
384 | sub find_widget { |
474 | sub find_widget { |
385 | my ($self, $x, $y) = @_; |
475 | my ($self, $x, $y) = @_; |
386 | |
476 | |
… | |
… | |
416 | |
506 | |
417 | $class->SUPER::new (children => [$child], %arg) |
507 | $class->SUPER::new (children => [$child], %arg) |
418 | } |
508 | } |
419 | |
509 | |
420 | sub add { |
510 | sub add { |
421 | my ($self, $widget) = @_; |
511 | my ($self, $child) = @_; |
422 | |
512 | |
423 | $self->{children} = []; |
513 | $self->{children} = []; |
424 | |
514 | |
425 | $self->SUPER::add ($widget); |
515 | $self->SUPER::add ($child); |
426 | } |
516 | } |
427 | |
517 | |
428 | sub remove { |
518 | sub remove { |
429 | my ($self, $widget) = @_; |
519 | my ($self, $widget) = @_; |
430 | |
520 | |
… | |
… | |
439 | sub size_request { |
529 | sub size_request { |
440 | $_[0]{children}[0]->size_request |
530 | $_[0]{children}[0]->size_request |
441 | } |
531 | } |
442 | |
532 | |
443 | sub size_allocate { |
533 | sub size_allocate { |
444 | my ($self, $x, $y, $w, $h) = @_; |
534 | my ($self, $w, $h) = @_; |
445 | |
535 | |
446 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
447 | |
|
|
448 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
536 | $self->{children}[0]->configure (0, 0, $w, $h); |
449 | } |
537 | } |
450 | |
538 | |
451 | ############################################################################# |
539 | ############################################################################# |
452 | |
540 | |
453 | package CFClient::UI::Window; |
541 | package CFClient::UI::Window; |
454 | |
542 | |
455 | our @ISA = CFClient::UI::Bin::; |
543 | our @ISA = CFClient::UI::Bin::; |
456 | |
544 | |
457 | use SDL::OpenGL; |
545 | use CFClient::OpenGL; |
458 | |
546 | |
459 | sub new { |
547 | sub new { |
460 | my ($class, %arg) = @_; |
548 | my ($class, %arg) = @_; |
461 | |
549 | |
462 | my $self = $class->SUPER::new (%arg); |
550 | my $self = $class->SUPER::new (%arg); |
… | |
… | |
479 | $self->child->draw; |
567 | $self->child->draw; |
480 | }; |
568 | }; |
481 | } |
569 | } |
482 | |
570 | |
483 | sub size_allocate { |
571 | sub size_allocate { |
484 | my ($self, $x, $y, $w, $h) = @_; |
572 | my ($self, $w, $h) = @_; |
485 | |
573 | |
486 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
487 | |
|
|
488 | $self->child->size_allocate (0, 0, $w, $h); |
574 | $self->child->configure (0, 0, $w, $h); |
489 | |
575 | |
490 | $self->render_chld; |
576 | $self->render_chld; |
491 | } |
577 | } |
492 | |
578 | |
493 | sub _draw { |
579 | sub _draw { |
… | |
… | |
509 | glDisable GL_TEXTURE_2D; |
595 | glDisable GL_TEXTURE_2D; |
510 | } |
596 | } |
511 | |
597 | |
512 | ############################################################################# |
598 | ############################################################################# |
513 | |
599 | |
|
|
600 | package CFClient::UI::ViewPort; |
|
|
601 | |
|
|
602 | our @ISA = CFClient::UI::Window::; |
|
|
603 | |
|
|
604 | sub new { die } |
|
|
605 | |
|
|
606 | sub size_request { |
|
|
607 | my ($self) = @_; |
|
|
608 | |
|
|
609 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
|
|
610 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
|
|
611 | |
|
|
612 | @$self{qw(child_w child_h)} |
|
|
613 | } |
|
|
614 | |
|
|
615 | sub _draw { |
|
|
616 | my ($self) = @_; |
|
|
617 | |
|
|
618 | $self->{children}[1]->draw; |
|
|
619 | } |
|
|
620 | |
|
|
621 | |
|
|
622 | ############################################################################# |
|
|
623 | |
514 | package CFClient::UI::Frame; |
624 | package CFClient::UI::Frame; |
515 | |
625 | |
516 | our @ISA = CFClient::UI::Bin::; |
626 | our @ISA = CFClient::UI::Bin::; |
517 | |
627 | |
518 | use SDL::OpenGL; |
628 | use CFClient::OpenGL; |
519 | |
|
|
520 | sub size_request { |
|
|
521 | my ($self) = @_; |
|
|
522 | my $chld = $self->child |
|
|
523 | or return (0, 0); |
|
|
524 | |
|
|
525 | $chld->move (2, 2); |
|
|
526 | |
|
|
527 | map { $_ + 4 } $chld->size_request; |
|
|
528 | } |
|
|
529 | |
|
|
530 | sub size_allocate { |
|
|
531 | my ($self, $x, $y, $w, $h) = @_; |
|
|
532 | |
|
|
533 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
534 | |
|
|
535 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
|
|
536 | } |
|
|
537 | |
|
|
538 | sub _draw { |
|
|
539 | my ($self) = @_; |
|
|
540 | |
|
|
541 | my $chld = $self->child; |
|
|
542 | |
|
|
543 | my ($w, $h) = $chld->size_request; |
|
|
544 | |
|
|
545 | glBegin GL_QUADS; |
|
|
546 | glColor 0, 0, 0; |
|
|
547 | glVertex 0 , 0; |
|
|
548 | glVertex 0 , $h + 4; |
|
|
549 | glVertex $w + 4 , $h + 4; |
|
|
550 | glVertex $w + 4 , 0; |
|
|
551 | glEnd; |
|
|
552 | |
|
|
553 | $chld->draw; |
|
|
554 | } |
|
|
555 | |
|
|
556 | ############################################################################# |
|
|
557 | |
|
|
558 | package CFClient::UI::FancyFrame; |
|
|
559 | |
|
|
560 | our @ISA = CFClient::UI::Bin::; |
|
|
561 | |
|
|
562 | use SDL::OpenGL; |
|
|
563 | |
|
|
564 | my @tex = |
|
|
565 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
566 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
567 | |
629 | |
568 | sub new { |
630 | sub new { |
569 | my $class = shift; |
631 | my $class = shift; |
570 | |
632 | |
571 | # TODO: user_x, user_y, overwrite moveto? |
|
|
572 | |
|
|
573 | $class->SUPER::new ( |
633 | my $self = $class->SUPER::new ( |
574 | bg => [1, 1, 1, 1], |
634 | bg => [1, 1, 1, 1], |
575 | border_bg => [1, 1, 1, 1], |
635 | border_bg => [1, 1, 1, 1], |
576 | border => int $::FONTSIZE * 0.8, |
636 | border => 0.8, |
577 | @_ |
637 | @_ |
|
|
638 | ); |
|
|
639 | |
|
|
640 | $self |
|
|
641 | } |
|
|
642 | |
|
|
643 | sub set_size { |
|
|
644 | my ($self, $w, $h) = @_; |
|
|
645 | $self->{req_w} = $w; |
|
|
646 | $self->{req_h} = $h; |
|
|
647 | $self->check_size; |
|
|
648 | } |
|
|
649 | |
|
|
650 | sub size_request { |
|
|
651 | my ($self) = @_; |
|
|
652 | ($self->{req_w}, $self->{req_h}) |
|
|
653 | } |
|
|
654 | |
|
|
655 | sub size_allocate { |
|
|
656 | my ($self, $w, $h) = @_; |
|
|
657 | $self->{w} = $w; |
|
|
658 | $self->{h} = $h; |
|
|
659 | $self->child->configure (0, 0, $w, $h); |
|
|
660 | } |
|
|
661 | |
|
|
662 | sub _draw { |
|
|
663 | my ($self) = @_; |
|
|
664 | |
|
|
665 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
666 | |
|
|
667 | glEnable GL_BLEND; |
|
|
668 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
669 | glEnable GL_TEXTURE_2D; |
|
|
670 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
671 | |
|
|
672 | # glBegin GL_QUADS; |
|
|
673 | # glColor 0, 0, 0, 0; |
|
|
674 | # glVertex 0 , 0; |
|
|
675 | # glVertex 0 , $h; |
|
|
676 | # glVertex $w, $h; |
|
|
677 | # glVertex $w, 0; |
|
|
678 | # glEnd; |
|
|
679 | |
|
|
680 | |
|
|
681 | $self->child->draw; |
|
|
682 | glDisable GL_BLEND; |
|
|
683 | glDisable GL_TEXTURE_2D; |
|
|
684 | } |
|
|
685 | |
|
|
686 | ############################################################################# |
|
|
687 | |
|
|
688 | package CFClient::UI::FancyFrame; |
|
|
689 | |
|
|
690 | our @ISA = CFClient::UI::Bin::; |
|
|
691 | |
|
|
692 | use CFClient::OpenGL; |
|
|
693 | |
|
|
694 | my @tex = |
|
|
695 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
696 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
697 | |
|
|
698 | sub new { |
|
|
699 | my $class = shift; |
|
|
700 | |
|
|
701 | # TODO: user_x, user_y, overwrite moveto? |
|
|
702 | |
|
|
703 | my $self = $class->SUPER::new ( |
|
|
704 | bg => [1, 1, 1, 1], |
|
|
705 | border_bg => [1, 1, 1, 1], |
|
|
706 | border => 0.8, |
|
|
707 | can_events => 1, |
|
|
708 | @_ |
|
|
709 | ); |
|
|
710 | |
|
|
711 | $self->{title} &&= new CFClient::UI::Label |
|
|
712 | align => 0, |
|
|
713 | valign => 1, |
|
|
714 | text => $self->{title}, |
|
|
715 | fontsize => 1; |
|
|
716 | |
|
|
717 | $self |
|
|
718 | } |
|
|
719 | |
|
|
720 | sub border { |
|
|
721 | int $_[0]{border} * $::FONTSIZE |
|
|
722 | } |
|
|
723 | |
|
|
724 | sub size_request { |
|
|
725 | my ($self) = @_; |
|
|
726 | |
|
|
727 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
|
|
728 | |
|
|
729 | my ($w, $h) = $self->SUPER::size_request; |
|
|
730 | |
|
|
731 | ( |
|
|
732 | $w + $self->border * 2, |
|
|
733 | $h + $self->border * 2, |
578 | ) |
734 | ) |
579 | } |
735 | } |
580 | |
736 | |
581 | sub size_request { |
|
|
582 | my ($self) = @_; |
|
|
583 | |
|
|
584 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
|
|
585 | |
|
|
586 | my ($w, $h) = $self->SUPER::size_request; |
|
|
587 | |
|
|
588 | ( |
|
|
589 | $w + $self->{border} * 2, |
|
|
590 | $h + $self->{border} * 2, |
|
|
591 | ) |
|
|
592 | } |
|
|
593 | |
|
|
594 | sub size_allocate { |
737 | sub size_allocate { |
595 | my ($self, $x, $y, $w, $h) = @_; |
738 | my ($self, $w, $h) = @_; |
596 | |
739 | |
597 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
598 | |
|
|
599 | $h -= List::Util::max 0, $self->{border} * 2; |
740 | $h -= List::Util::max 0, $self->border * 2; |
600 | $w -= List::Util::max 0, $self->{border} * 2; |
741 | $w -= List::Util::max 0, $self->border * 2; |
601 | |
742 | |
|
|
743 | $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) |
|
|
744 | if $self->{title}; |
|
|
745 | |
602 | $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); |
746 | $self->child->configure ($self->border, $self->border, $w, $h); |
603 | } |
747 | } |
604 | |
748 | |
605 | sub button_down { |
749 | sub button_down { |
606 | my ($self, $ev, $x, $y) = @_; |
750 | my ($self, $ev, $x, $y) = @_; |
607 | |
751 | |
|
|
752 | my $border = $self->border; |
|
|
753 | |
608 | if ($x < $self->{w} && $x >= $self->{w} - $self->{border} |
754 | if ($x < $self->{w} && $x >= $self->{w} - $border |
609 | && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { |
755 | && $y < $self->{h} && $y >= $self->{h} - $border) { |
610 | |
756 | |
611 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
757 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
612 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
758 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
613 | |
759 | |
614 | $self->{motion} = sub { |
760 | $self->{motion} = sub { |
615 | my ($ev, $x, $y) = @_; |
761 | my ($ev, $x, $y) = @_; |
616 | |
762 | |
617 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
763 | ($x, $y) = ($ev->{x}, $ev->{y}); |
618 | |
764 | |
619 | $self->{user_w} = $bw + $x - $ox; |
765 | $self->{user_w} = $bw + $x - $ox; |
620 | $self->{user_h} = $bh + $y - $oy; |
766 | $self->{user_h} = $bh + $y - $oy; |
621 | $self->update; |
767 | $self->check_size; |
622 | }; |
768 | }; |
623 | |
769 | |
624 | } elsif ($x >= 0 && $x < $self->{w} |
770 | } elsif ($x >= 0 && $x < $self->{w} |
625 | && $y >= 0 && $y < $self->{border}) { |
771 | && $y >= 0 && $y < $border) { |
626 | |
772 | |
627 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
773 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
628 | my ($bx, $by) = ($self->{x}, $self->{y}); |
774 | my ($bx, $by) = ($self->{x}, $self->{y}); |
629 | |
775 | |
630 | $self->{motion} = sub { |
776 | $self->{motion} = sub { |
631 | my ($ev, $x, $y) = @_; |
777 | my ($ev, $x, $y) = @_; |
632 | |
778 | |
633 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
779 | ($x, $y) = ($ev->{x}, $ev->{y}); |
634 | |
780 | |
635 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
781 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
636 | $self->update; |
782 | $self->update; |
637 | }; |
783 | }; |
638 | } |
784 | } |
… | |
… | |
659 | glEnable GL_BLEND; |
805 | glEnable GL_BLEND; |
660 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
806 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
661 | glEnable GL_TEXTURE_2D; |
807 | glEnable GL_TEXTURE_2D; |
662 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
808 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
663 | |
809 | |
|
|
810 | my $border = $self->border; |
|
|
811 | |
664 | glColor @{ $self->{border_bg} }; |
812 | glColor @{ $self->{border_bg} }; |
665 | $tex[1]->draw_quad (0, 0, $w, $self->{border}); |
813 | $tex[1]->draw_quad (0, 0, $w, $border); |
666 | $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); |
814 | $tex[3]->draw_quad (0, $border, $border, $ch); |
667 | $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); |
815 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
668 | $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); |
816 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
669 | |
817 | |
670 | my $bg = $tex[0]; |
818 | my $bg = $tex[0]; |
671 | |
819 | |
672 | # TODO: repeat texture not scale |
820 | # TODO: repeat texture not scale |
673 | my $rep_x = $cw / $bg->{w}; |
821 | my $rep_x = $cw / $bg->{w}; |
… | |
… | |
676 | glColor @{ $self->{bg} }; |
824 | glColor @{ $self->{bg} }; |
677 | |
825 | |
678 | $bg->{s} = $rep_x; |
826 | $bg->{s} = $rep_x; |
679 | $bg->{t} = $rep_y; |
827 | $bg->{t} = $rep_y; |
680 | $bg->{wrap_mode} = 1; |
828 | $bg->{wrap_mode} = 1; |
681 | $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); |
829 | $bg->draw_quad ($border, $border, $cw, $ch); |
682 | |
830 | |
683 | glDisable GL_TEXTURE_2D; |
831 | glDisable GL_TEXTURE_2D; |
684 | glDisable GL_BLEND; |
832 | glDisable GL_BLEND; |
685 | |
833 | |
|
|
834 | $self->{title}->draw if $self->{title}; |
686 | $self->child->draw; |
835 | $self->child->draw; |
687 | } |
836 | } |
688 | |
837 | |
689 | ############################################################################# |
838 | ############################################################################# |
690 | |
839 | |
… | |
… | |
692 | |
841 | |
693 | our @ISA = CFClient::UI::Base::; |
842 | our @ISA = CFClient::UI::Base::; |
694 | |
843 | |
695 | use List::Util qw(max sum); |
844 | use List::Util qw(max sum); |
696 | |
845 | |
697 | use SDL::OpenGL; |
846 | use CFClient::OpenGL; |
698 | |
847 | |
699 | sub new { |
848 | sub new { |
700 | my $class = shift; |
849 | my $class = shift; |
701 | |
850 | |
702 | $class->SUPER::new ( |
851 | $class->SUPER::new ( |
… | |
… | |
709 | my ($self, $x, $y, $child) = @_; |
858 | my ($self, $x, $y, $child) = @_; |
710 | |
859 | |
711 | $child->set_parent ($self); |
860 | $child->set_parent ($self); |
712 | $self->{children}[$y][$x] = $child; |
861 | $self->{children}[$y][$x] = $child; |
713 | |
862 | |
714 | $self->{w} = $self->{h} = -1; |
863 | $child->check_size; |
715 | $self->update; |
|
|
716 | } |
864 | } |
717 | |
865 | |
718 | # TODO: move to container class maybe? send childs a signal on removal? |
866 | # TODO: move to container class maybe? send childs a signal on removal? |
719 | sub clear { |
867 | sub clear { |
720 | my ($self) = @_; |
868 | my ($self) = @_; |
… | |
… | |
733 | or next; |
881 | or next; |
734 | |
882 | |
735 | for my $x (0 .. $#$row) { |
883 | for my $x (0 .. $#$row) { |
736 | my $widget = $row->[$x] |
884 | my $widget = $row->[$x] |
737 | or next; |
885 | or next; |
738 | my ($w, $h) = $widget->size_request; |
886 | my ($w, $h) = @$widget{qw(req_w req_h)}; |
739 | |
887 | |
740 | $w[$x] = max $w[$x], $w; |
888 | $w[$x] = max $w[$x], $w; |
741 | $h[$y] = max $h[$y], $h; |
889 | $h[$y] = max $h[$y], $h; |
742 | } |
890 | } |
743 | } |
891 | } |
… | |
… | |
755 | (sum @$hs), |
903 | (sum @$hs), |
756 | ) |
904 | ) |
757 | } |
905 | } |
758 | |
906 | |
759 | sub size_allocate { |
907 | sub size_allocate { |
760 | my ($self, $x, $y, $w, $h) = @_; |
908 | my ($self, $w, $h) = @_; |
761 | |
|
|
762 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
763 | |
909 | |
764 | my ($ws, $hs) = $self->get_wh; |
910 | my ($ws, $hs) = $self->get_wh; |
765 | |
911 | |
766 | my $req_w = sum @$ws; |
912 | my $req_w = sum @$ws; |
767 | my $req_h = sum @$hs; |
913 | my $req_h = sum @$hs; |
… | |
… | |
789 | |
935 | |
790 | for my $c (0 .. $#$row) { |
936 | for my $c (0 .. $#$row) { |
791 | my $col_w = $ws->[$c]; |
937 | my $col_w = $ws->[$c]; |
792 | |
938 | |
793 | if (my $widget = $row->[$c]) { |
939 | if (my $widget = $row->[$c]) { |
794 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
940 | $widget->configure ($x, $y, $col_w, $row_h); |
795 | } |
941 | } |
796 | |
942 | |
797 | $x += $col_w; |
943 | $x += $col_w; |
798 | } |
944 | } |
799 | |
945 | |
… | |
… | |
844 | (List::Util::max map $_->[1], @alloc), |
990 | (List::Util::max map $_->[1], @alloc), |
845 | ) |
991 | ) |
846 | } |
992 | } |
847 | |
993 | |
848 | sub size_allocate { |
994 | sub size_allocate { |
849 | my ($self, $x, $y, $w, $h) = @_; |
995 | my ($self, $w, $h) = @_; |
850 | |
|
|
851 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
852 | |
996 | |
853 | ($h, $w) = ($w, $h); |
997 | ($h, $w) = ($w, $h); |
854 | |
998 | |
855 | my $children = $self->{children}; |
999 | my $children = $self->{children}; |
856 | |
1000 | |
857 | my @h = map +($_->size_request)[0], @$children; |
1001 | my @h = map $_->{req_w}, @$children; |
858 | |
1002 | |
859 | my $req_h = List::Util::sum @h; |
1003 | my $req_h = List::Util::sum @h; |
860 | |
1004 | |
861 | if ($req_h > $h) { |
1005 | if ($req_h > $h) { |
862 | # ah well, not enough space |
1006 | # ah well, not enough space |
… | |
… | |
878 | |
1022 | |
879 | my $y = 0; |
1023 | my $y = 0; |
880 | for (0 .. $#$children) { |
1024 | for (0 .. $#$children) { |
881 | my $child = $children->[$_]; |
1025 | my $child = $children->[$_]; |
882 | my $h = $h[$_]; |
1026 | my $h = $h[$_]; |
883 | $child->size_allocate ($y, 0, $h, $w); |
1027 | $child->configure ($y, 0, $h, $w); |
884 | |
1028 | |
885 | $y += $h; |
1029 | $y += $h; |
886 | } |
1030 | } |
|
|
1031 | |
|
|
1032 | 1 |
887 | } |
1033 | } |
888 | |
1034 | |
889 | ############################################################################# |
1035 | ############################################################################# |
890 | |
1036 | |
891 | package CFClient::UI::VBox; |
1037 | package CFClient::UI::VBox; |
… | |
… | |
904 | (List::Util::sum map $_->[1], @alloc), |
1050 | (List::Util::sum map $_->[1], @alloc), |
905 | ) |
1051 | ) |
906 | } |
1052 | } |
907 | |
1053 | |
908 | sub size_allocate { |
1054 | sub size_allocate { |
909 | my ($self, $x, $y, $w, $h) = @_; |
1055 | my ($self, $w, $h) = @_; |
910 | |
|
|
911 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
912 | |
1056 | |
913 | my $children = $self->{children}; |
1057 | my $children = $self->{children}; |
914 | |
1058 | |
915 | my @h = map +($_->size_request)[1], @$children; |
1059 | my @h = map $_->{req_h}, @$children; |
916 | |
1060 | |
917 | my $req_h = List::Util::sum @h; |
1061 | my $req_h = List::Util::sum @h; |
918 | |
1062 | |
919 | if ($req_h > $h) { |
1063 | if ($req_h > $h) { |
920 | # ah well, not enough space |
1064 | # ah well, not enough space |
… | |
… | |
934 | |
1078 | |
935 | my $y = 0; |
1079 | my $y = 0; |
936 | for (0 .. $#$children) { |
1080 | for (0 .. $#$children) { |
937 | my $child = $children->[$_]; |
1081 | my $child = $children->[$_]; |
938 | my $h = $h[$_]; |
1082 | my $h = $h[$_]; |
939 | $child->size_allocate (0, $y, $w, $h); |
1083 | $child->configure (0, $y, $w, $h); |
940 | |
1084 | |
941 | $y += $h; |
1085 | $y += $h; |
942 | } |
1086 | } |
|
|
1087 | |
|
|
1088 | 1 |
943 | } |
1089 | } |
944 | |
1090 | |
945 | ############################################################################# |
1091 | ############################################################################# |
946 | |
1092 | |
947 | package CFClient::UI::Label; |
1093 | package CFClient::UI::Label; |
948 | |
1094 | |
949 | our @ISA = CFClient::UI::Base::; |
1095 | our @ISA = CFClient::UI::Base::; |
950 | |
1096 | |
951 | use SDL::OpenGL; |
1097 | use CFClient::OpenGL; |
952 | |
1098 | |
953 | sub new { |
1099 | sub new { |
954 | my ($class, %arg) = @_; |
1100 | my ($class, %arg) = @_; |
955 | |
1101 | |
956 | my $self = $class->SUPER::new ( |
1102 | my $self = $class->SUPER::new ( |
957 | fg => [1, 1, 1], |
1103 | fg => [1, 1, 1], |
958 | fontsize => $::FONTSIZE, |
1104 | fontsize => 1, |
959 | text => "", |
1105 | text => "", |
960 | align => -1, |
1106 | align => -1, |
961 | valign => -1, |
1107 | valign => -1, |
962 | padding => 2, |
1108 | padding => 2, |
963 | layout => new CFClient::Layout, |
1109 | layout => new CFClient::Layout, |
|
|
1110 | can_events => 0, |
964 | %arg |
1111 | %arg |
965 | ); |
1112 | ); |
966 | |
1113 | |
967 | $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# |
1114 | if (exists $self->{template}) { |
|
|
1115 | my $layout = new CFClient::Layout; |
|
|
1116 | $layout->set_text (delete $self->{template}); |
|
|
1117 | $self->{template} = $layout; |
|
|
1118 | } |
968 | |
1119 | |
969 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
1120 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
970 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
1121 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
971 | |
1122 | |
972 | $self |
1123 | $self |
… | |
… | |
986 | my ($self, $text) = @_; |
1137 | my ($self, $text) = @_; |
987 | |
1138 | |
988 | $self->{layout}->set_text ($text); |
1139 | $self->{layout}->set_text ($text); |
989 | |
1140 | |
990 | delete $self->{texture}; |
1141 | delete $self->{texture}; |
|
|
1142 | $self->check_size; |
991 | $self->update; |
1143 | $self->update; |
992 | } |
1144 | } |
993 | |
1145 | |
994 | sub set_markup { |
1146 | sub set_markup { |
995 | my ($self, $markup) = @_; |
1147 | my ($self, $markup) = @_; |
996 | |
1148 | |
997 | $self->{layout}->set_markup ($markup); |
1149 | $self->{layout}->set_markup ($markup); |
998 | |
1150 | |
999 | delete $self->{texture}; |
1151 | delete $self->{texture}; |
|
|
1152 | $self->check_size; |
1000 | $self->update; |
1153 | $self->update; |
1001 | } |
1154 | } |
1002 | |
1155 | |
1003 | sub size_request { |
1156 | sub size_request { |
1004 | my ($self) = @_; |
1157 | my ($self) = @_; |
1005 | |
1158 | |
1006 | $self->{layout}->set_width; |
1159 | $self->{layout}->set_width; |
1007 | $self->{layout}->set_height ($self->{fontsize}); |
1160 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1008 | |
1161 | |
1009 | my ($w, $h) = $self->{layout}->size; |
1162 | my ($w, $h) = $self->{layout}->size; |
|
|
1163 | |
|
|
1164 | if (exists $self->{template}) { |
|
|
1165 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1166 | |
|
|
1167 | my ($w2, $h2) = $self->{template}->size; |
|
|
1168 | |
|
|
1169 | $w = List::Util::max $w, $w2; |
|
|
1170 | $h = List::Util::max $h, $h2; |
|
|
1171 | } |
1010 | |
1172 | |
1011 | ( |
1173 | ( |
1012 | $w + $self->{padding} * 2, |
1174 | $w + $self->{padding} * 2, |
1013 | $h + $self->{padding} * 2, |
1175 | $h + $self->{padding} * 2, |
1014 | ) |
1176 | ) |
1015 | } |
1177 | } |
1016 | |
1178 | |
1017 | sub size_allocate { |
1179 | sub size_allocate { |
1018 | my ($self, $x, $y, $w, $h) = @_; |
1180 | my ($self, $w, $h) = @_; |
1019 | |
|
|
1020 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
1021 | |
1181 | |
1022 | delete $self->{texture}; |
1182 | delete $self->{texture}; |
1023 | } |
1183 | } |
1024 | |
1184 | |
1025 | sub update { |
1185 | sub set_fontsize { |
1026 | my ($self) = @_; |
1186 | my ($self, $fontsize) = @_; |
1027 | |
1187 | |
|
|
1188 | $self->{fontsize} = $fontsize; |
1028 | delete $self->{texture}; |
1189 | delete $self->{texture}; |
|
|
1190 | $self->check_size; |
1029 | $self->SUPER::update; |
1191 | $self->update; |
1030 | } |
1192 | } |
1031 | |
1193 | |
1032 | sub _draw { |
1194 | sub _draw { |
1033 | my ($self) = @_; |
1195 | my ($self) = @_; |
1034 | |
1196 | |
1035 | my $tex = $self->{texture} ||= do { |
1197 | my $tex = $self->{texture} ||= do { |
1036 | $self->{layout}->set_width ($self->{w}); |
1198 | $self->{layout}->set_width ($self->{w}); |
1037 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); |
1199 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1038 | new_from_layout CFClient::Texture $self->{layout} |
1200 | new_from_layout CFClient::Texture $self->{layout} |
1039 | }; |
1201 | }; |
1040 | |
1202 | |
1041 | glEnable GL_BLEND; |
1203 | glEnable GL_BLEND; |
1042 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1204 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
… | |
… | |
1067 | |
1229 | |
1068 | package CFClient::UI::EntryBase; |
1230 | package CFClient::UI::EntryBase; |
1069 | |
1231 | |
1070 | our @ISA = CFClient::UI::Label::; |
1232 | our @ISA = CFClient::UI::Label::; |
1071 | |
1233 | |
1072 | use SDL; |
1234 | use CFClient::OpenGL; |
1073 | use SDL::OpenGL; |
|
|
1074 | |
1235 | |
1075 | sub new { |
1236 | sub new { |
1076 | my $class = shift; |
1237 | my $class = shift; |
1077 | |
1238 | |
1078 | $class->SUPER::new ( |
1239 | $class->SUPER::new ( |
… | |
… | |
1081 | active_bg => [1, 1, 1, 0.5], |
1242 | active_bg => [1, 1, 1, 0.5], |
1082 | active_fg => [0, 0, 0], |
1243 | active_fg => [0, 0, 0], |
1083 | can_hover => 1, |
1244 | can_hover => 1, |
1084 | can_focus => 1, |
1245 | can_focus => 1, |
1085 | valign => 0, |
1246 | valign => 0, |
|
|
1247 | can_events => 1, |
1086 | @_ |
1248 | @_ |
1087 | ) |
1249 | ) |
1088 | } |
1250 | } |
1089 | |
1251 | |
1090 | sub _set_text { |
1252 | sub _set_text { |
… | |
… | |
1092 | |
1254 | |
1093 | delete $self->{cur_h}; |
1255 | delete $self->{cur_h}; |
1094 | |
1256 | |
1095 | return if $self->{text} eq $text; |
1257 | return if $self->{text} eq $text; |
1096 | |
1258 | |
|
|
1259 | delete $self->{texture}; |
|
|
1260 | |
1097 | $self->{last_activity} = $::NOW; |
1261 | $self->{last_activity} = $::NOW; |
1098 | $self->{text} = $text; |
1262 | $self->{text} = $text; |
1099 | |
1263 | |
1100 | $text =~ s/./*/g if $self->{hidden}; |
1264 | $text =~ s/./*/g if $self->{hidden}; |
1101 | $self->{layout}->set_text ("$text "); |
1265 | $self->{layout}->set_text ("$text "); |
… | |
… | |
1114 | |
1278 | |
1115 | ($w + 1, $h) # add 1 for cursor |
1279 | ($w + 1, $h) # add 1 for cursor |
1116 | } |
1280 | } |
1117 | |
1281 | |
1118 | sub size_allocate { |
1282 | sub size_allocate { |
1119 | my ($self, $x, $y, $w, $h) = @_; |
1283 | my ($self, $w, $h) = @_; |
1120 | |
1284 | |
1121 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
1285 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
1122 | |
|
|
1123 | $self->_set_text ($self->{text}); |
|
|
1124 | } |
1286 | } |
1125 | |
1287 | |
1126 | sub set_text { |
1288 | sub set_text { |
1127 | my ($self, $text) = @_; |
1289 | my ($self, $text) = @_; |
1128 | |
1290 | |
… | |
… | |
1132 | } |
1294 | } |
1133 | |
1295 | |
1134 | sub key_down { |
1296 | sub key_down { |
1135 | my ($self, $ev) = @_; |
1297 | my ($self, $ev) = @_; |
1136 | |
1298 | |
1137 | my $mod = $ev->key_mod; |
1299 | my $mod = $ev->{mod}; |
1138 | my $sym = $ev->key_sym; |
1300 | my $sym = $ev->{sym}; |
1139 | |
|
|
1140 | my $uni = $ev->key_unicode; |
1301 | my $uni = $ev->{unicode}; |
1141 | |
1302 | |
1142 | my $text = $self->get_text; |
1303 | my $text = $self->get_text; |
1143 | |
1304 | |
1144 | if ($sym == SDLK_BACKSPACE) { |
1305 | if ($sym == 8) { |
1145 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1306 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1146 | } elsif ($sym == SDLK_DELETE) { |
1307 | } elsif ($sym == 127) { |
1147 | substr $text, $self->{cursor}, 1, ""; |
1308 | substr $text, $self->{cursor}, 1, ""; |
1148 | } elsif ($sym == SDLK_LEFT) { |
1309 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1149 | --$self->{cursor} if $self->{cursor}; |
1310 | --$self->{cursor} if $self->{cursor}; |
1150 | } elsif ($sym == SDLK_RIGHT) { |
1311 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1151 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1312 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1152 | } elsif ($sym == SDLK_HOME) { |
1313 | } elsif ($sym == CFClient::SDLK_HOME) { |
1153 | $self->{cursor} = 0; |
1314 | $self->{cursor} = 0; |
1154 | } elsif ($sym == SDLK_END) { |
1315 | } elsif ($sym == CFClient::SDLK_END) { |
1155 | $self->{cursor} = length $text; |
1316 | $self->{cursor} = length $text; |
1156 | } elsif ($sym == SDLK_ESCAPE) { |
1317 | } elsif ($sym == 27) { |
1157 | $self->emit ('escape'); |
1318 | $self->emit ('escape'); |
1158 | } elsif ($uni) { |
1319 | } elsif ($uni) { |
1159 | substr $text, $self->{cursor}++, 0, chr $uni; |
1320 | substr $text, $self->{cursor}++, 0, chr $uni; |
1160 | } |
1321 | } |
1161 | |
1322 | |
… | |
… | |
1236 | |
1397 | |
1237 | package CFClient::UI::Entry; |
1398 | package CFClient::UI::Entry; |
1238 | |
1399 | |
1239 | our @ISA = CFClient::UI::EntryBase::; |
1400 | our @ISA = CFClient::UI::EntryBase::; |
1240 | |
1401 | |
1241 | use SDL; |
1402 | use CFClient::OpenGL; |
1242 | use SDL::OpenGL; |
|
|
1243 | |
1403 | |
1244 | sub key_down { |
1404 | sub key_down { |
1245 | my ($self, $ev) = @_; |
1405 | my ($self, $ev) = @_; |
1246 | |
1406 | |
1247 | my $sym = $ev->key_sym; |
1407 | my $sym = $ev->{sym}; |
1248 | |
1408 | |
1249 | if ($sym == SDLK_RETURN) { |
1409 | if ($sym == 13) { |
1250 | $self->emit (activate => $self->get_text); |
1410 | $self->emit (activate => $self->get_text); |
1251 | $self->update; |
1411 | $self->update; |
1252 | |
1412 | |
1253 | } else { |
1413 | } else { |
1254 | $self->SUPER::key_down ($ev); |
1414 | $self->SUPER::key_down ($ev); |
… | |
… | |
1260 | |
1420 | |
1261 | package CFClient::UI::Button; |
1421 | package CFClient::UI::Button; |
1262 | |
1422 | |
1263 | our @ISA = CFClient::UI::Label::; |
1423 | our @ISA = CFClient::UI::Label::; |
1264 | |
1424 | |
1265 | use SDL; |
1425 | use CFClient::OpenGL; |
1266 | use SDL::OpenGL; |
|
|
1267 | |
1426 | |
1268 | my @tex = |
1427 | my @tex = |
1269 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1428 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1270 | qw(b1_button_active.png); |
1429 | qw(b1_button_active.png); |
1271 | |
1430 | |
1272 | sub new { |
1431 | sub new { |
1273 | my $class = shift; |
1432 | my $class = shift; |
1274 | |
1433 | |
… | |
… | |
1276 | padding => 4, |
1435 | padding => 4, |
1277 | fg => [1, 1, 1], |
1436 | fg => [1, 1, 1], |
1278 | bg => [1, 1, 1, 0.2], |
1437 | bg => [1, 1, 1, 0.2], |
1279 | active_fg => [0, 0, 1], |
1438 | active_fg => [0, 0, 1], |
1280 | can_hover => 1, |
1439 | can_hover => 1, |
|
|
1440 | align => 0, |
|
|
1441 | valign => 0, |
|
|
1442 | can_events => 1, |
1281 | @_ |
1443 | @_ |
1282 | ) |
1444 | ) |
1283 | } |
1445 | } |
1284 | |
1446 | |
1285 | sub button_up { |
1447 | sub button_up { |
… | |
… | |
1319 | package CFClient::UI::CheckBox; |
1481 | package CFClient::UI::CheckBox; |
1320 | |
1482 | |
1321 | our @ISA = CFClient::UI::DrawBG::; |
1483 | our @ISA = CFClient::UI::DrawBG::; |
1322 | |
1484 | |
1323 | my @tex = |
1485 | my @tex = |
1324 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1486 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1325 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1487 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1326 | |
1488 | |
1327 | use SDL; |
1489 | use CFClient::OpenGL; |
1328 | use SDL::OpenGL; |
|
|
1329 | |
1490 | |
1330 | sub new { |
1491 | sub new { |
1331 | my $class = shift; |
1492 | my $class = shift; |
1332 | |
1493 | |
1333 | $class->SUPER::new ( |
1494 | $class->SUPER::new ( |
… | |
… | |
1344 | my ($self) = @_; |
1505 | my ($self) = @_; |
1345 | |
1506 | |
1346 | ($self->{padding} * 2 + 6) x 2 |
1507 | ($self->{padding} * 2 + 6) x 2 |
1347 | } |
1508 | } |
1348 | |
1509 | |
1349 | sub size_allocate { |
|
|
1350 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1351 | |
|
|
1352 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
1353 | } |
|
|
1354 | |
|
|
1355 | sub button_down { |
1510 | sub button_down { |
1356 | my ($self, $ev, $x, $y) = @_; |
1511 | my ($self, $ev, $x, $y) = @_; |
1357 | |
1512 | |
1358 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1513 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1359 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1514 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
… | |
… | |
1385 | glDisable GL_BLEND; |
1540 | glDisable GL_BLEND; |
1386 | } |
1541 | } |
1387 | |
1542 | |
1388 | ############################################################################# |
1543 | ############################################################################# |
1389 | |
1544 | |
|
|
1545 | package CFClient::UI::Image; |
|
|
1546 | |
|
|
1547 | our @ISA = CFClient::UI::Base::; |
|
|
1548 | |
|
|
1549 | use CFClient::OpenGL; |
|
|
1550 | use Carp qw/confess/; |
|
|
1551 | |
|
|
1552 | our %loaded_images; |
|
|
1553 | |
|
|
1554 | sub new { |
|
|
1555 | my $class = shift; |
|
|
1556 | |
|
|
1557 | my $self = $class->SUPER::new (can_events => 0, @_); |
|
|
1558 | |
|
|
1559 | $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; |
|
|
1560 | |
|
|
1561 | $loaded_images{$self->{image}} ||= |
|
|
1562 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; |
|
|
1563 | |
|
|
1564 | my $tex = $self->{tex} = $loaded_images{$self->{image}}; |
|
|
1565 | |
|
|
1566 | Scalar::Util::weaken $loaded_images{$self->{image}}; |
|
|
1567 | |
|
|
1568 | $self->{aspect} = $tex->{w} / $tex->{h}; |
|
|
1569 | |
|
|
1570 | $self |
|
|
1571 | } |
|
|
1572 | |
|
|
1573 | sub size_request { |
|
|
1574 | my ($self) = @_; |
|
|
1575 | |
|
|
1576 | ($self->{tex}->{w}, $self->{tex}->{h}) |
|
|
1577 | } |
|
|
1578 | |
|
|
1579 | sub _draw { |
|
|
1580 | my ($self) = @_; |
|
|
1581 | |
|
|
1582 | my $tex = $self->{tex}; |
|
|
1583 | |
|
|
1584 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1585 | |
|
|
1586 | if ($self->{rot90}) { |
|
|
1587 | glRotate 90, 0, 0, 1; |
|
|
1588 | glTranslate 0, -$self->{w}, 0; |
|
|
1589 | |
|
|
1590 | ($w, $h) = ($h, $w); |
|
|
1591 | } |
|
|
1592 | |
|
|
1593 | glEnable GL_BLEND; |
|
|
1594 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1595 | glEnable GL_TEXTURE_2D; |
|
|
1596 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1597 | |
|
|
1598 | $tex->draw_quad (0, 0, $w, $h); |
|
|
1599 | |
|
|
1600 | glDisable GL_BLEND; |
|
|
1601 | glDisable GL_TEXTURE_2D; |
|
|
1602 | } |
|
|
1603 | |
|
|
1604 | ############################################################################# |
|
|
1605 | |
|
|
1606 | package CFClient::UI::VGauge; |
|
|
1607 | |
|
|
1608 | our @ISA = CFClient::UI::Base::; |
|
|
1609 | |
|
|
1610 | use CFClient::OpenGL; |
|
|
1611 | |
|
|
1612 | my %tex = ( |
|
|
1613 | food => [ |
|
|
1614 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1615 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
|
|
1616 | ], |
|
|
1617 | grace => [ |
|
|
1618 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1619 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ |
|
|
1620 | ], |
|
|
1621 | hp => [ |
|
|
1622 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1623 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
|
|
1624 | ], |
|
|
1625 | mana => [ |
|
|
1626 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1627 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ |
|
|
1628 | ], |
|
|
1629 | ); |
|
|
1630 | |
|
|
1631 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
|
|
1632 | sub new { |
|
|
1633 | my $class = shift; |
|
|
1634 | |
|
|
1635 | my $self = $class->SUPER::new ( |
|
|
1636 | type => 'food', |
|
|
1637 | @_ |
|
|
1638 | ); |
|
|
1639 | |
|
|
1640 | $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h}; |
|
|
1641 | |
|
|
1642 | $self |
|
|
1643 | } |
|
|
1644 | |
|
|
1645 | sub size_request { |
|
|
1646 | my ($self) = @_; |
|
|
1647 | |
|
|
1648 | #my $tex = $tex{$self->{type}}[0]; |
|
|
1649 | #@$tex{qw(w h)} |
|
|
1650 | (0, 0) |
|
|
1651 | } |
|
|
1652 | |
|
|
1653 | sub set_max { |
|
|
1654 | my ($self, $max) = @_; |
|
|
1655 | |
|
|
1656 | $self->{max_val} = $max; |
|
|
1657 | } |
|
|
1658 | |
|
|
1659 | sub set_value { |
|
|
1660 | my ($self, $val, $max) = @_; |
|
|
1661 | |
|
|
1662 | $self->set_max ($max) |
|
|
1663 | if defined $max; |
|
|
1664 | |
|
|
1665 | $max = $self->{max_val}; |
|
|
1666 | $self->{val} = $val; |
|
|
1667 | |
|
|
1668 | $self->update; |
|
|
1669 | } |
|
|
1670 | |
|
|
1671 | sub _draw { |
|
|
1672 | my ($self) = @_; |
|
|
1673 | |
|
|
1674 | my $tex = $tex{$self->{type}}; |
|
|
1675 | |
|
|
1676 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1677 | |
|
|
1678 | if ($self->{vertical}) { |
|
|
1679 | glRotate 90, 0, 0, 1; |
|
|
1680 | glTranslate 0, -$self->{w}, 0; |
|
|
1681 | |
|
|
1682 | ($w, $h) = ($h, $w); |
|
|
1683 | } |
|
|
1684 | |
|
|
1685 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
|
|
1686 | $ycut = 1 if $self->{val} > $self->{max_val}; |
|
|
1687 | |
|
|
1688 | my $t1 = $tex->[0]; |
|
|
1689 | my $t2 = $tex->[1]; |
|
|
1690 | |
|
|
1691 | glEnable GL_BLEND; |
|
|
1692 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1693 | glEnable GL_TEXTURE_2D; |
|
|
1694 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1695 | |
|
|
1696 | my $h1 = $self->{h} - $ycut * $self->{h}; |
|
|
1697 | my $h2 = $ycut * $self->{h}; |
|
|
1698 | |
|
|
1699 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
|
|
1700 | glBegin GL_QUADS; |
|
|
1701 | glTexCoord 0 , 0; glVertex 0 , 0; |
|
|
1702 | glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; |
|
|
1703 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; |
|
|
1704 | glTexCoord $t1->{s}, 0; glVertex $w, 0; |
|
|
1705 | glEnd; |
|
|
1706 | |
|
|
1707 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
|
|
1708 | glBegin GL_QUADS; |
|
|
1709 | glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; |
|
|
1710 | glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; |
|
|
1711 | glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; |
|
|
1712 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; |
|
|
1713 | glEnd; |
|
|
1714 | |
|
|
1715 | glDisable GL_BLEND; |
|
|
1716 | glDisable GL_TEXTURE_2D; |
|
|
1717 | } |
|
|
1718 | |
|
|
1719 | ############################################################################# |
|
|
1720 | |
|
|
1721 | package CFClient::UI::Gauge; |
|
|
1722 | |
|
|
1723 | our @ISA = CFClient::UI::VBox::; |
|
|
1724 | |
|
|
1725 | sub new { |
|
|
1726 | my ($class, %arg) = @_; |
|
|
1727 | |
|
|
1728 | my $self = $class->SUPER::new ( |
|
|
1729 | tooltip => $arg{type}, |
|
|
1730 | %arg, |
|
|
1731 | ); |
|
|
1732 | |
|
|
1733 | $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1); |
|
|
1734 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1); |
|
|
1735 | $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1); |
|
|
1736 | |
|
|
1737 | $self |
|
|
1738 | } |
|
|
1739 | |
|
|
1740 | sub set_fontsize { |
|
|
1741 | my ($self, $fsize) = @_; |
|
|
1742 | |
|
|
1743 | $self->{value}->set_fontsize ($fsize); |
|
|
1744 | $self->{max} ->set_fontsize ($fsize); |
|
|
1745 | } |
|
|
1746 | |
|
|
1747 | sub set_value { |
|
|
1748 | my ($self, $val, $max) = @_; |
|
|
1749 | |
|
|
1750 | $self->set_max ($max) |
|
|
1751 | if defined $max; |
|
|
1752 | |
|
|
1753 | $self->{gauge}->set_value ($val, $max); |
|
|
1754 | $self->{value}->set_text ($val); |
|
|
1755 | } |
|
|
1756 | |
|
|
1757 | sub set_max { |
|
|
1758 | my ($self, $max) = @_; |
|
|
1759 | |
|
|
1760 | $self->{gauge}->set_max ($max); |
|
|
1761 | $self->{max}->set_text ($max); |
|
|
1762 | } |
|
|
1763 | |
|
|
1764 | ############################################################################# |
|
|
1765 | |
1390 | package CFClient::UI::Slider; |
1766 | package CFClient::UI::Slider; |
1391 | |
1767 | |
1392 | use strict; |
1768 | use strict; |
1393 | |
1769 | |
1394 | use SDL::OpenGL; |
1770 | use CFClient::OpenGL; |
1395 | |
1771 | |
1396 | our @ISA = CFClient::UI::DrawBG::; |
1772 | our @ISA = CFClient::UI::DrawBG::; |
1397 | |
1773 | |
1398 | my @tex = |
1774 | my @tex = |
1399 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1775 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
… | |
… | |
1409 | # TODO: calculations are off |
1785 | # TODO: calculations are off |
1410 | my $self = $class->SUPER::new ( |
1786 | my $self = $class->SUPER::new ( |
1411 | fg => [1, 1, 1], |
1787 | fg => [1, 1, 1], |
1412 | active_fg => [0, 0, 0], |
1788 | active_fg => [0, 0, 0], |
1413 | range => [0, 0, 100, 10], |
1789 | range => [0, 0, 100, 10], |
1414 | req_w => 40, |
1790 | req_w => 20, |
1415 | req_h => 13, |
1791 | req_h => 20, |
1416 | vertical => 0, |
1792 | vertical => 0, |
1417 | can_hover => 1, |
1793 | can_hover => 1, |
1418 | inner_pad => 5, |
1794 | inner_pad => 5, |
1419 | @_ |
1795 | @_ |
1420 | ); |
1796 | ); |
… | |
… | |
1524 | |
1900 | |
1525 | package CFClient::UI::TextView; |
1901 | package CFClient::UI::TextView; |
1526 | |
1902 | |
1527 | our @ISA = CFClient::UI::HBox::; |
1903 | our @ISA = CFClient::UI::HBox::; |
1528 | |
1904 | |
1529 | use SDL::OpenGL; |
1905 | use CFClient::OpenGL; |
1530 | |
1906 | |
1531 | sub new { |
1907 | sub new { |
1532 | my $class = shift; |
1908 | my $class = shift; |
1533 | |
1909 | |
1534 | my $self = $class->SUPER::new ( |
1910 | my $self = $class->SUPER::new ( |
1535 | req_w => $::WIDTH / 6, |
|
|
1536 | req_h => $::HEIGHT / 6, |
|
|
1537 | fontsize => $::FONTSIZE, |
1911 | fontsize => 1, |
1538 | @_, |
1912 | @_, |
1539 | |
1913 | |
1540 | layout => (new CFClient::Layout), |
1914 | layout => (new CFClient::Layout), |
1541 | par => [], |
1915 | par => [], |
1542 | height => 0, |
1916 | height => 0, |
… | |
… | |
1563 | sub text_height { |
1937 | sub text_height { |
1564 | my ($self, $text) = @_; |
1938 | my ($self, $text) = @_; |
1565 | |
1939 | |
1566 | my $layout = $self->{layout}; |
1940 | my $layout = $self->{layout}; |
1567 | |
1941 | |
1568 | $layout->set_height ($self->{fontsize}); |
1942 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
1569 | $layout->set_width ($self->{w}); |
1943 | $layout->set_width ($self->{w}); |
1570 | $layout->set_text ($text); |
1944 | $layout->set_text ($text); |
1571 | |
1945 | |
1572 | ($layout->size)[1] |
1946 | ($layout->size)[1] |
1573 | } |
1947 | } |
… | |
… | |
1577 | |
1951 | |
1578 | $self->{need_reflow}++; |
1952 | $self->{need_reflow}++; |
1579 | $self->update; |
1953 | $self->update; |
1580 | } |
1954 | } |
1581 | |
1955 | |
1582 | sub size_request { |
|
|
1583 | my ($self) = @_; |
|
|
1584 | |
|
|
1585 | ($self->{req_w}, $self->{req_h}) |
|
|
1586 | } |
|
|
1587 | |
|
|
1588 | sub size_allocate { |
1956 | sub size_allocate { |
1589 | my ($self, $x, $y, $w, $h) = @_; |
1957 | my ($self, $w, $h) = @_; |
1590 | |
1958 | |
1591 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
1959 | $self->SUPER::size_allocate ($w, $h); |
1592 | |
1960 | |
1593 | $self->{layout}->set_height ($self->{fontsize}); |
1961 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1594 | $self->{layout}->set_width ($self->{w}); |
1962 | $self->{layout}->set_width ($self->{children}[0]{w}); |
1595 | |
1963 | |
1596 | $self->reflow; |
1964 | $self->reflow; |
1597 | } |
1965 | } |
1598 | |
1966 | |
1599 | sub add_paragraph { |
1967 | sub add_paragraph { |
… | |
… | |
1686 | |
2054 | |
1687 | } |
2055 | } |
1688 | |
2056 | |
1689 | ############################################################################# |
2057 | ############################################################################# |
1690 | |
2058 | |
1691 | package CFClient::UI::MapWidget; |
|
|
1692 | |
|
|
1693 | use strict; |
|
|
1694 | |
|
|
1695 | use List::Util qw(min max); |
|
|
1696 | |
|
|
1697 | use SDL; |
|
|
1698 | use SDL::OpenGL; |
|
|
1699 | |
|
|
1700 | our @ISA = CFClient::UI::Base::; |
|
|
1701 | |
|
|
1702 | sub new { |
|
|
1703 | my $class = shift; |
|
|
1704 | |
|
|
1705 | $class->SUPER::new ( |
|
|
1706 | z => -1, |
|
|
1707 | can_focus => 1, |
|
|
1708 | list => (glGenLists 1), |
|
|
1709 | @_ |
|
|
1710 | ) |
|
|
1711 | } |
|
|
1712 | |
|
|
1713 | sub key_down { |
|
|
1714 | print "MAPKEYDOWN\n"; |
|
|
1715 | } |
|
|
1716 | |
|
|
1717 | sub key_up { |
|
|
1718 | } |
|
|
1719 | |
|
|
1720 | sub button_down { |
|
|
1721 | my ($self, $ev, $x, $y) = @_; |
|
|
1722 | |
|
|
1723 | $self->focus_in; |
|
|
1724 | |
|
|
1725 | if ($ev->button == 2) { |
|
|
1726 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
1727 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
|
|
1728 | |
|
|
1729 | $self->{motion} = sub { |
|
|
1730 | my ($ev, $x, $y) = @_; |
|
|
1731 | |
|
|
1732 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
1733 | |
|
|
1734 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
|
|
1735 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
|
|
1736 | |
|
|
1737 | $self->update; |
|
|
1738 | }; |
|
|
1739 | } |
|
|
1740 | } |
|
|
1741 | |
|
|
1742 | sub button_up { |
|
|
1743 | my ($self, $ev, $x, $y) = @_; |
|
|
1744 | |
|
|
1745 | delete $self->{motion}; |
|
|
1746 | } |
|
|
1747 | |
|
|
1748 | sub mouse_motion { |
|
|
1749 | my ($self, $ev, $x, $y) = @_; |
|
|
1750 | |
|
|
1751 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
|
|
1752 | } |
|
|
1753 | |
|
|
1754 | sub size_request { |
|
|
1755 | ( |
|
|
1756 | 1 + 32 * int $::WIDTH / 32, |
|
|
1757 | 1 + 32 * int $::HEIGHT / 32, |
|
|
1758 | ) |
|
|
1759 | } |
|
|
1760 | |
|
|
1761 | sub update { |
|
|
1762 | my ($self) = @_; |
|
|
1763 | |
|
|
1764 | $self->{need_update} = 1; |
|
|
1765 | $self->SUPER::update; |
|
|
1766 | } |
|
|
1767 | |
|
|
1768 | sub draw { |
|
|
1769 | my ($self) = @_; |
|
|
1770 | |
|
|
1771 | if (delete $self->{need_update}) { |
|
|
1772 | glNewList $self->{list}, GL_COMPILE; |
|
|
1773 | |
|
|
1774 | if ($::MAP) { |
|
|
1775 | my $sw = int $::WIDTH / 32; |
|
|
1776 | my $sh = int $::HEIGHT / 32; |
|
|
1777 | |
|
|
1778 | my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
|
|
1779 | my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
|
|
1780 | |
|
|
1781 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
|
|
1782 | |
|
|
1783 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
|
|
1784 | |
|
|
1785 | if ($::CFG->{fow_enable}) { |
|
|
1786 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
|
|
1787 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
|
|
1788 | glConvolutionFilter2D |
|
|
1789 | GL_CONVOLUTION_2D, |
|
|
1790 | GL_ALPHA, |
|
|
1791 | 3, 3, |
|
|
1792 | GL_ALPHA, GL_FLOAT, |
|
|
1793 | pack "f*", |
|
|
1794 | 0.1, 0.1, 0.1, |
|
|
1795 | 0.1, 0.2, 0.1, |
|
|
1796 | 0.1, 0.1, 0.1, |
|
|
1797 | ; |
|
|
1798 | glEnable GL_CONVOLUTION_2D; |
|
|
1799 | } |
|
|
1800 | |
|
|
1801 | $self->{fow_texture} = new CFClient::Texture |
|
|
1802 | w => $w, |
|
|
1803 | h => $h, |
|
|
1804 | data => $data, |
|
|
1805 | internalformat => GL_ALPHA, |
|
|
1806 | format => GL_ALPHA; |
|
|
1807 | |
|
|
1808 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
|
|
1809 | |
|
|
1810 | glEnable GL_BLEND; |
|
|
1811 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1812 | glEnable GL_TEXTURE_2D; |
|
|
1813 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1814 | |
|
|
1815 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1816 | $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); |
|
|
1817 | |
|
|
1818 | glDisable GL_TEXTURE_2D; |
|
|
1819 | glDisable GL_BLEND; |
|
|
1820 | } |
|
|
1821 | |
|
|
1822 | # HACK BEGIN |
|
|
1823 | { |
|
|
1824 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
1825 | my ($w, $h) = (250, 250); |
|
|
1826 | |
|
|
1827 | glEnable GL_BLEND; |
|
|
1828 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1829 | glEnable GL_TEXTURE_2D; |
|
|
1830 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1831 | |
|
|
1832 | $self->{mapmap_texture} = |
|
|
1833 | new CFClient::Texture |
|
|
1834 | w => $w, |
|
|
1835 | h => $h, |
|
|
1836 | data => $::MAP->mapmap ($w, $h), |
|
|
1837 | type => GL_UNSIGNED_INT_8_8_8_8_REV; |
|
|
1838 | |
|
|
1839 | $self->{mapmap_texture}->draw_quad (100, 100); |
|
|
1840 | |
|
|
1841 | glDisable GL_TEXTURE_2D; |
|
|
1842 | glDisable GL_BLEND; |
|
|
1843 | } |
|
|
1844 | # HACK END |
|
|
1845 | } |
|
|
1846 | |
|
|
1847 | glEndList; |
|
|
1848 | } |
|
|
1849 | |
|
|
1850 | glPushMatrix; |
|
|
1851 | glCallList $self->{list}; |
|
|
1852 | glPopMatrix; |
|
|
1853 | |
|
|
1854 | if ($FOCUS != $self) { |
|
|
1855 | glColor 64/255, 64/255, 64/255; |
|
|
1856 | glLogicOp GL_AND; |
|
|
1857 | glEnable GL_COLOR_LOGIC_OP; |
|
|
1858 | glBegin GL_QUADS; |
|
|
1859 | glVertex 0, 0; |
|
|
1860 | glVertex 0, $::HEIGHT; |
|
|
1861 | glVertex $::WIDTH, $::HEIGHT; |
|
|
1862 | glVertex $::WIDTH, 0; |
|
|
1863 | glEnd; |
|
|
1864 | glDisable GL_COLOR_LOGIC_OP; |
|
|
1865 | } |
|
|
1866 | } |
|
|
1867 | |
|
|
1868 | my %DIR = ( |
|
|
1869 | SDLK_KP8, [1, "north"], |
|
|
1870 | SDLK_KP9, [2, "northeast"], |
|
|
1871 | SDLK_KP6, [3, "east"], |
|
|
1872 | SDLK_KP3, [4, "southeast"], |
|
|
1873 | SDLK_KP2, [5, "south"], |
|
|
1874 | SDLK_KP1, [6, "southwest"], |
|
|
1875 | SDLK_KP4, [7, "west"], |
|
|
1876 | SDLK_KP7, [8, "northwest"], |
|
|
1877 | |
|
|
1878 | SDLK_UP, [1, "north"], |
|
|
1879 | SDLK_RIGHT, [3, "east"], |
|
|
1880 | SDLK_DOWN, [5, "south"], |
|
|
1881 | SDLK_LEFT, [7, "west"], |
|
|
1882 | ); |
|
|
1883 | |
|
|
1884 | sub key_down { |
|
|
1885 | my ($self, $ev) = @_; |
|
|
1886 | |
|
|
1887 | my $mod = $ev->key_mod; |
|
|
1888 | my $sym = $ev->key_sym; |
|
|
1889 | |
|
|
1890 | if ($sym == SDLK_KP5) { |
|
|
1891 | $::CONN->user_send ("stay fire"); |
|
|
1892 | } elsif ($sym == SDLK_a) { |
|
|
1893 | $::CONN->user_send ("apply"); |
|
|
1894 | } elsif ($sym == SDLK_QUOTE) { |
|
|
1895 | $self->emit ('activate_console'); |
|
|
1896 | } elsif ($sym == SDLK_SLASH) { |
|
|
1897 | $self->emit ('activate_console' => '/'); |
|
|
1898 | } elsif (exists $DIR{$sym}) { |
|
|
1899 | if ($mod & KMOD_SHIFT) { |
|
|
1900 | $self->{shft}++; |
|
|
1901 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
|
|
1902 | } elsif ($mod & KMOD_CTRL) { |
|
|
1903 | $self->{ctrl}++; |
|
|
1904 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
|
|
1905 | } else { |
|
|
1906 | $::CONN->user_send ("$DIR{$sym}[1]"); |
|
|
1907 | } |
|
|
1908 | } |
|
|
1909 | } |
|
|
1910 | |
|
|
1911 | sub key_up { |
|
|
1912 | my ($self, $ev) = @_; |
|
|
1913 | |
|
|
1914 | my $mod = $ev->key_mod; |
|
|
1915 | my $sym = $ev->key_sym; |
|
|
1916 | |
|
|
1917 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
|
|
1918 | $::CONN->user_send ("fire_stop"); |
|
|
1919 | } |
|
|
1920 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
|
|
1921 | $::CONN->user_send ("run_stop"); |
|
|
1922 | } |
|
|
1923 | } |
|
|
1924 | |
|
|
1925 | ############################################################################# |
|
|
1926 | |
|
|
1927 | package CFClient::UI::Animator; |
2059 | package CFClient::UI::Animator; |
1928 | |
2060 | |
1929 | use SDL::OpenGL; |
2061 | use CFClient::OpenGL; |
1930 | |
2062 | |
1931 | our @ISA = CFClient::UI::Bin::; |
2063 | our @ISA = CFClient::UI::Bin::; |
1932 | |
2064 | |
1933 | sub moveto { |
2065 | sub moveto { |
1934 | my ($self, $x, $y) = @_; |
2066 | my ($self, $x, $y) = @_; |
… | |
… | |
1974 | my $class = shift; |
2106 | my $class = shift; |
1975 | |
2107 | |
1976 | my $self = $class->SUPER::new ( |
2108 | my $self = $class->SUPER::new ( |
1977 | state => 0, |
2109 | state => 0, |
1978 | connect_activate => \&toggle_flopper, |
2110 | connect_activate => \&toggle_flopper, |
|
|
2111 | can_events => 1, |
1979 | @_ |
2112 | @_ |
1980 | ); |
2113 | ); |
1981 | |
2114 | |
1982 | if ($self->{state}) { |
2115 | if ($self->{state}) { |
1983 | $self->{state} = 0; |
2116 | $self->{state} = 0; |
… | |
… | |
2003 | $self->emit (changed => $self->{state}); |
2136 | $self->emit (changed => $self->{state}); |
2004 | } |
2137 | } |
2005 | |
2138 | |
2006 | ############################################################################# |
2139 | ############################################################################# |
2007 | |
2140 | |
|
|
2141 | package CFClient::UI::Tooltip; |
|
|
2142 | |
|
|
2143 | our @ISA = CFClient::UI::Bin::; |
|
|
2144 | |
|
|
2145 | use CFClient::OpenGL; |
|
|
2146 | |
|
|
2147 | sub new { |
|
|
2148 | my $class = shift; |
|
|
2149 | |
|
|
2150 | $class->SUPER::new ( |
|
|
2151 | @_, |
|
|
2152 | can_events => 0, |
|
|
2153 | ) |
|
|
2154 | } |
|
|
2155 | |
|
|
2156 | sub set_text { |
|
|
2157 | my ($self, $text) = @_; |
|
|
2158 | |
|
|
2159 | $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; |
|
|
2160 | $self->{label}->set_text ($text); |
|
|
2161 | $self->add ($self->{label}); |
|
|
2162 | } |
|
|
2163 | |
|
|
2164 | sub size_request { |
|
|
2165 | my ($self) = @_; |
|
|
2166 | |
|
|
2167 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
|
|
2168 | |
|
|
2169 | $w = List::Util::min $::WIDTH * 0.2, $w; |
|
|
2170 | $h = List::Util::max $::HEIGHT * 0.2, $h; |
|
|
2171 | |
|
|
2172 | ($w + 4, $h + 4) |
|
|
2173 | } |
|
|
2174 | |
|
|
2175 | sub _draw { |
|
|
2176 | my ($self) = @_; |
|
|
2177 | |
|
|
2178 | glPushMatrix; |
|
|
2179 | glTranslate 0.375, 0.375; |
|
|
2180 | |
|
|
2181 | my ($w, $h) = @$self{qw(w h)}; |
|
|
2182 | |
|
|
2183 | glColor 1, 0.8, 0.4; |
|
|
2184 | glBegin GL_QUADS; |
|
|
2185 | glVertex 0 , 0; |
|
|
2186 | glVertex 0 , $h; |
|
|
2187 | glVertex $w, $h; |
|
|
2188 | glVertex $w, 0; |
|
|
2189 | glEnd; |
|
|
2190 | |
|
|
2191 | glColor 0, 0, 0; |
|
|
2192 | glBegin GL_LINE_LOOP; |
|
|
2193 | glVertex 0 , 0; |
|
|
2194 | glVertex 0 , $h; |
|
|
2195 | glVertex $w, $h; |
|
|
2196 | glVertex $w, 0; |
|
|
2197 | glEnd; |
|
|
2198 | |
|
|
2199 | glPopMatrix; |
|
|
2200 | |
|
|
2201 | glTranslate 2, 2; |
|
|
2202 | $self->SUPER::_draw; |
|
|
2203 | } |
|
|
2204 | |
|
|
2205 | ############################################################################# |
|
|
2206 | |
2008 | package CFClient::UI::Root; |
2207 | package CFClient::UI::Root; |
2009 | |
2208 | |
2010 | our @ISA = CFClient::UI::Container::; |
2209 | our @ISA = CFClient::UI::Container::; |
2011 | |
2210 | |
2012 | use SDL::OpenGL; |
2211 | use CFClient::OpenGL; |
|
|
2212 | |
|
|
2213 | sub check_size { |
|
|
2214 | my ($self) = @_; |
|
|
2215 | |
|
|
2216 | $self->configure (0, 0, $::WIDTH, $::HEIGHT); |
|
|
2217 | } |
2013 | |
2218 | |
2014 | sub size_request { |
2219 | sub size_request { |
2015 | ($::WIDTH, $::HEIGHT) |
2220 | ($::WIDTH, $::HEIGHT) |
2016 | } |
2221 | } |
2017 | |
2222 | |
2018 | sub size_allocate { |
2223 | sub configure { |
2019 | my ($self, $x, $y, $w, $h) = @_; |
2224 | my ($self, $x, $y, $w, $h) = @_; |
2020 | |
2225 | |
2021 | $self->_size_allocate ($x, $y, $w, $h); |
2226 | $self->SUPER::configure ($x, $y, $w, $h); |
2022 | |
2227 | |
2023 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
|
|
2024 | for @{$self->{children}}; |
2228 | for my $child (@{$self->{children}}) { |
|
|
2229 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
|
|
2230 | |
|
|
2231 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
|
|
2232 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
|
|
2233 | $child->configure ($X, $Y, $W,$H); |
|
|
2234 | } |
2025 | } |
2235 | } |
2026 | |
2236 | |
2027 | sub _topleft { |
2237 | sub _topleft { |
2028 | my ($self, $x, $y) = @_; |
2238 | my ($self, $x, $y) = @_; |
2029 | |
2239 | |
… | |
… | |
2031 | } |
2241 | } |
2032 | |
2242 | |
2033 | sub update { |
2243 | sub update { |
2034 | my ($self) = @_; |
2244 | my ($self) = @_; |
2035 | |
2245 | |
2036 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
2246 | $self->check_size; |
2037 | ::refresh (); |
2247 | ::refresh (); |
2038 | } |
2248 | } |
2039 | |
2249 | |
2040 | sub add { |
2250 | sub add { |
2041 | my ($self, $widget) = @_; |
2251 | my ($self, $child) = @_; |
2042 | |
2252 | |
|
|
2253 | # integerize window positions |
|
|
2254 | $child->{x} = int $child->{x}; |
|
|
2255 | $child->{y} = int $child->{y}; |
|
|
2256 | |
2043 | $self->SUPER::add ($widget); |
2257 | $self->SUPER::add ($child); |
2044 | |
|
|
2045 | $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request); |
|
|
2046 | } |
2258 | } |
2047 | |
2259 | |
2048 | sub on_refresh { |
2260 | sub on_refresh { |
2049 | my ($self, $id, $cb) = @_; |
2261 | my ($self, $id, $cb) = @_; |
2050 | |
2262 | |
… | |
… | |
2074 | ############################################################################# |
2286 | ############################################################################# |
2075 | |
2287 | |
2076 | package CFClient::UI; |
2288 | package CFClient::UI; |
2077 | |
2289 | |
2078 | $ROOT = new CFClient::UI::Root; |
2290 | $ROOT = new CFClient::UI::Root; |
|
|
2291 | $TOOLTIP = new CFClient::UI::Tooltip; |
2079 | |
2292 | |
2080 | 1 |
2293 | 1 |
2081 | |
2294 | |