… | |
… | |
21 | $FOCUS->key_up ($_[0]) if $FOCUS; |
21 | $FOCUS->key_up ($_[0]) if $FOCUS; |
22 | } |
22 | } |
23 | |
23 | |
24 | sub feed_sdl_button_down_event { |
24 | sub feed_sdl_button_down_event { |
25 | my ($ev) = @_; |
25 | my ($ev) = @_; |
26 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
26 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
27 | |
27 | |
28 | if (!$BUTTON_STATE) { |
28 | if (!$BUTTON_STATE) { |
29 | my $widget = $ROOT->find_widget ($x, $y); |
29 | my $widget = $ROOT->find_widget ($x, $y); |
30 | |
30 | |
31 | $GRAB = $widget; |
31 | $GRAB = $widget; |
32 | $GRAB->update if $GRAB; |
32 | $GRAB->update if $GRAB; |
33 | } |
33 | } |
34 | |
34 | |
35 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
35 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
36 | |
36 | |
37 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
37 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
38 | } |
38 | } |
39 | |
39 | |
40 | sub feed_sdl_button_up_event { |
40 | sub feed_sdl_button_up_event { |
41 | my ($ev) = @_; |
41 | my ($ev) = @_; |
42 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
42 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
43 | |
43 | |
44 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
44 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
45 | |
45 | |
46 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
46 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
47 | |
47 | |
48 | $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
48 | $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
49 | |
49 | |
50 | if (!$BUTTON_STATE) { |
50 | if (!$BUTTON_STATE) { |
51 | my $grab = $GRAB; undef $GRAB; |
51 | my $grab = $GRAB; undef $GRAB; |
… | |
… | |
54 | } |
54 | } |
55 | } |
55 | } |
56 | |
56 | |
57 | sub feed_sdl_motion_event { |
57 | sub feed_sdl_motion_event { |
58 | my ($ev) = @_; |
58 | my ($ev) = @_; |
59 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
59 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
60 | |
60 | |
61 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
61 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
62 | |
62 | |
63 | if ($widget != $HOVER) { |
63 | if ($widget != $HOVER) { |
64 | my $hover = $HOVER; $HOVER = $widget; |
64 | my $hover = $HOVER; $HOVER = $widget; |
… | |
… | |
87 | |
87 | |
88 | package CFClient::UI::Base; |
88 | package CFClient::UI::Base; |
89 | |
89 | |
90 | use strict; |
90 | use strict; |
91 | |
91 | |
92 | use SDL::OpenGL; |
92 | use CFClient::OpenGL; |
93 | |
93 | |
94 | sub new { |
94 | sub new { |
95 | my $class = shift; |
95 | my $class = shift; |
96 | |
96 | |
97 | my $self = bless { |
97 | my $self = bless { |
98 | x => 0, |
98 | x => 0, |
99 | y => 0, |
99 | y => 0, |
100 | z => 0, |
100 | z => 0, |
|
|
101 | can_events => 1, |
101 | @_ |
102 | @_ |
102 | }, $class; |
103 | }, $class; |
103 | |
104 | |
104 | for (keys %$self) { |
105 | for (keys %$self) { |
105 | if (/^connect_(.*)$/) { |
106 | if (/^connect_(.*)$/) { |
… | |
… | |
140 | 0 |
141 | 0 |
141 | } |
142 | } |
142 | |
143 | |
143 | sub size_request { |
144 | sub size_request { |
144 | require Carp; |
145 | require Carp; |
145 | Carp::confess "size_request is abtract"; |
146 | Carp::confess "size_request is abstract"; |
146 | } |
147 | } |
147 | |
148 | |
148 | sub configure { |
149 | sub configure { |
149 | my ($self, $x, $y, $w, $h) = @_; |
150 | my ($self, $x, $y, $w, $h) = @_; |
150 | |
151 | |
|
|
152 | if ($self->{aspect}) { |
|
|
153 | my $w2 = List::Util::min $w, int $h * $self->{aspect}; |
|
|
154 | my $h2 = List::Util::min $h, int $w / $self->{aspect}; |
|
|
155 | |
|
|
156 | # use alignment to adjust x, y |
|
|
157 | |
|
|
158 | $x += int +($w - $w2) * 0.5; |
|
|
159 | $y += int +($h - $h2) * 0.5; |
|
|
160 | |
|
|
161 | ($w, $h) = ($w2, $h2); |
|
|
162 | } |
|
|
163 | |
|
|
164 | if ($self->{x} != $x || $self->{y} != $y) { |
151 | $self->{x} = $x; |
165 | $self->{x} = $x; |
152 | $self->{y} = $y; |
166 | $self->{y} = $y; |
|
|
167 | $self->update; |
|
|
168 | } |
153 | |
169 | |
154 | return unless $self->{w} != $w || $self->{h} != $h; |
170 | if ($self->{w} != $w || $self->{h} != $h) { |
155 | |
|
|
156 | $self->{w} = $w; |
171 | $self->{w} = $w; |
157 | $self->{h} = $h; |
172 | $self->{h} = $h; |
158 | |
173 | |
159 | $self->size_allocate ($w, $h); |
174 | $self->size_allocate ($w, $h); |
160 | $self->update; |
175 | $self->update; |
|
|
176 | } |
161 | } |
177 | } |
162 | |
178 | |
163 | sub size_allocate { |
179 | sub size_allocate { |
164 | # nothing to be done |
180 | # nothing to be done |
165 | } |
181 | } |
… | |
… | |
263 | } |
279 | } |
264 | |
280 | |
265 | sub find_widget { |
281 | sub find_widget { |
266 | my ($self, $x, $y) = @_; |
282 | my ($self, $x, $y) = @_; |
267 | |
283 | |
|
|
284 | return () unless $self->{can_events}; |
|
|
285 | |
268 | return $self |
286 | return $self |
269 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
287 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
270 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
288 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
271 | |
289 | |
272 | () |
290 | () |
… | |
… | |
279 | } |
297 | } |
280 | |
298 | |
281 | sub check_size { |
299 | sub check_size { |
282 | my ($self) = @_; |
300 | my ($self) = @_; |
283 | |
301 | |
|
|
302 | return unless $self->{parent}; |
|
|
303 | |
284 | my ($w, $h) = $self->size_request; |
304 | my ($w, $h) = $self->size_request; |
285 | |
305 | |
286 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
306 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
287 | $self->{req_w} = $w; |
307 | $self->{req_w} = $w; |
288 | $self->{req_h} = $h; |
308 | $self->{req_h} = $h; |
289 | |
309 | |
290 | $self->{parent}->check_size |
310 | $self->{parent}->check_size; |
291 | if $self->{parent}; |
|
|
292 | } |
311 | } |
293 | } |
312 | } |
294 | |
313 | |
295 | sub update { |
314 | sub update { |
296 | my ($self) = @_; |
315 | my ($self) = @_; |
… | |
… | |
324 | package CFClient::UI::DrawBG; |
343 | package CFClient::UI::DrawBG; |
325 | |
344 | |
326 | our @ISA = CFClient::UI::Base::; |
345 | our @ISA = CFClient::UI::Base::; |
327 | |
346 | |
328 | use strict; |
347 | use strict; |
329 | use SDL::OpenGL; |
348 | use CFClient::OpenGL; |
330 | |
349 | |
331 | sub new { |
350 | sub new { |
332 | my $class = shift; |
351 | my $class = shift; |
333 | |
352 | |
334 | # range [value, low, high, page] |
353 | # range [value, low, high, page] |
… | |
… | |
363 | |
382 | |
364 | package CFClient::UI::Empty; |
383 | package CFClient::UI::Empty; |
365 | |
384 | |
366 | our @ISA = CFClient::UI::Base::; |
385 | our @ISA = CFClient::UI::Base::; |
367 | |
386 | |
|
|
387 | sub new { |
|
|
388 | my ($class, %arg) = @_; |
|
|
389 | $class->SUPER::new (can_events => 0, %arg); |
|
|
390 | } |
|
|
391 | |
368 | sub size_request { |
392 | sub size_request { |
369 | (0, 0) |
393 | (0, 0) |
370 | } |
394 | } |
371 | |
395 | |
372 | sub draw { } |
396 | sub draw { } |
… | |
… | |
380 | sub new { |
404 | sub new { |
381 | my ($class, %arg) = @_; |
405 | my ($class, %arg) = @_; |
382 | |
406 | |
383 | my $children = delete $arg{children} || []; |
407 | my $children = delete $arg{children} || []; |
384 | |
408 | |
385 | my $self = $class->SUPER::new (children => [], %arg); |
409 | my $self = $class->SUPER::new (children => [], can_events => 0, %arg); |
386 | $self->add ($_) for @$children; |
410 | $self->add ($_) for @$children; |
387 | |
411 | |
388 | $self |
412 | $self |
389 | } |
413 | } |
390 | |
414 | |
… | |
… | |
482 | |
506 | |
483 | package CFClient::UI::Window; |
507 | package CFClient::UI::Window; |
484 | |
508 | |
485 | our @ISA = CFClient::UI::Bin::; |
509 | our @ISA = CFClient::UI::Bin::; |
486 | |
510 | |
487 | use SDL::OpenGL; |
511 | use CFClient::OpenGL; |
488 | |
512 | |
489 | sub new { |
513 | sub new { |
490 | my ($class, %arg) = @_; |
514 | my ($class, %arg) = @_; |
491 | |
515 | |
492 | my $self = $class->SUPER::new (%arg); |
516 | my $self = $class->SUPER::new (%arg); |
… | |
… | |
546 | sub new { die } |
570 | sub new { die } |
547 | |
571 | |
548 | sub size_request { |
572 | sub size_request { |
549 | my ($self) = @_; |
573 | my ($self) = @_; |
550 | |
574 | |
551 | @$self{qw(child_w child_h)} = $self->child->size_request; |
575 | @$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)}); |
576 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
553 | |
577 | |
554 | @$self{qw(child_w child_h)} |
578 | @$self{qw(child_w child_h)} |
555 | } |
579 | } |
556 | |
580 | |
… | |
… | |
565 | |
589 | |
566 | package CFClient::UI::Frame; |
590 | package CFClient::UI::Frame; |
567 | |
591 | |
568 | our @ISA = CFClient::UI::Bin::; |
592 | our @ISA = CFClient::UI::Bin::; |
569 | |
593 | |
570 | use SDL::OpenGL; |
594 | 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 | |
595 | |
618 | sub new { |
596 | sub new { |
619 | my $class = shift; |
597 | my $class = shift; |
620 | |
598 | |
621 | # TODO: user_x, user_y, overwrite moveto? |
|
|
622 | |
|
|
623 | $class->SUPER::new ( |
599 | my $self = $class->SUPER::new ( |
624 | bg => [1, 1, 1, 1], |
600 | bg => [1, 1, 1, 1], |
625 | border_bg => [1, 1, 1, 1], |
601 | border_bg => [1, 1, 1, 1], |
626 | border => 0.8, |
602 | border => 0.8, |
627 | @_ |
603 | @_ |
628 | ) |
604 | ); |
|
|
605 | |
|
|
606 | $self |
|
|
607 | } |
|
|
608 | |
|
|
609 | sub set_size { |
|
|
610 | my ($self, $w, $h) = @_; |
|
|
611 | $self->{req_w} = $w; |
|
|
612 | $self->{req_h} = $h; |
|
|
613 | $self->check_size; |
|
|
614 | } |
|
|
615 | |
|
|
616 | sub size_request { |
|
|
617 | my ($self) = @_; |
|
|
618 | ($self->{req_w}, $self->{req_h}) |
|
|
619 | } |
|
|
620 | |
|
|
621 | sub size_allocate { |
|
|
622 | my ($self, $w, $h) = @_; |
|
|
623 | $self->{w} = $w; |
|
|
624 | $self->{h} = $h; |
|
|
625 | $self->child->configure (0, 0, $w, $h); |
|
|
626 | } |
|
|
627 | |
|
|
628 | sub _draw { |
|
|
629 | my ($self) = @_; |
|
|
630 | |
|
|
631 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
632 | |
|
|
633 | glEnable GL_BLEND; |
|
|
634 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
635 | glEnable GL_TEXTURE_2D; |
|
|
636 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
637 | |
|
|
638 | # glBegin GL_QUADS; |
|
|
639 | # glColor 0, 0, 0, 0; |
|
|
640 | # glVertex 0 , 0; |
|
|
641 | # glVertex 0 , $h; |
|
|
642 | # glVertex $w, $h; |
|
|
643 | # glVertex $w, 0; |
|
|
644 | # glEnd; |
|
|
645 | |
|
|
646 | |
|
|
647 | $self->child->draw; |
|
|
648 | glDisable GL_BLEND; |
|
|
649 | glDisable GL_TEXTURE_2D; |
|
|
650 | } |
|
|
651 | |
|
|
652 | ############################################################################# |
|
|
653 | |
|
|
654 | package CFClient::UI::FancyFrame; |
|
|
655 | |
|
|
656 | our @ISA = CFClient::UI::Bin::; |
|
|
657 | |
|
|
658 | use CFClient::OpenGL; |
|
|
659 | |
|
|
660 | my @tex = |
|
|
661 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
662 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
663 | |
|
|
664 | sub new { |
|
|
665 | my $class = shift; |
|
|
666 | |
|
|
667 | # TODO: user_x, user_y, overwrite moveto? |
|
|
668 | |
|
|
669 | my $self = $class->SUPER::new ( |
|
|
670 | bg => [1, 1, 1, 1], |
|
|
671 | border_bg => [1, 1, 1, 1], |
|
|
672 | border => 0.8, |
|
|
673 | can_events => 1, |
|
|
674 | @_ |
|
|
675 | ); |
|
|
676 | |
|
|
677 | $self->{title} &&= new CFClient::UI::Label |
|
|
678 | align => 0, |
|
|
679 | valign => 1, |
|
|
680 | text => $self->{title}, |
|
|
681 | fontsize => 1; |
|
|
682 | |
|
|
683 | $self |
629 | } |
684 | } |
630 | |
685 | |
631 | sub border { |
686 | sub border { |
632 | int $_[0]{border} * $::FONTSIZE |
687 | int $_[0]{border} * $::FONTSIZE |
633 | } |
688 | } |
… | |
… | |
649 | my ($self, $w, $h) = @_; |
704 | my ($self, $w, $h) = @_; |
650 | |
705 | |
651 | $h -= List::Util::max 0, $self->border * 2; |
706 | $h -= List::Util::max 0, $self->border * 2; |
652 | $w -= List::Util::max 0, $self->border * 2; |
707 | $w -= List::Util::max 0, $self->border * 2; |
653 | |
708 | |
|
|
709 | $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) |
|
|
710 | if $self->{title}; |
|
|
711 | |
654 | $self->child->configure ($self->border, $self->border, $w, $h); |
712 | $self->child->configure ($self->border, $self->border, $w, $h); |
655 | } |
713 | } |
656 | |
714 | |
657 | sub button_down { |
715 | sub button_down { |
658 | my ($self, $ev, $x, $y) = @_; |
716 | my ($self, $ev, $x, $y) = @_; |
… | |
… | |
660 | my $border = $self->border; |
718 | my $border = $self->border; |
661 | |
719 | |
662 | if ($x < $self->{w} && $x >= $self->{w} - $border |
720 | if ($x < $self->{w} && $x >= $self->{w} - $border |
663 | && $y < $self->{h} && $y >= $self->{h} - $border) { |
721 | && $y < $self->{h} && $y >= $self->{h} - $border) { |
664 | |
722 | |
665 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
723 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
666 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
724 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
667 | |
725 | |
668 | $self->{motion} = sub { |
726 | $self->{motion} = sub { |
669 | my ($ev, $x, $y) = @_; |
727 | my ($ev, $x, $y) = @_; |
670 | |
728 | |
671 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
729 | ($x, $y) = ($ev->{x}, $ev->{y}); |
672 | |
730 | |
673 | $self->{user_w} = $bw + $x - $ox; |
731 | $self->{user_w} = $bw + $x - $ox; |
674 | $self->{user_h} = $bh + $y - $oy; |
732 | $self->{user_h} = $bh + $y - $oy; |
675 | $self->check_size; |
733 | $self->check_size; |
676 | }; |
734 | }; |
677 | |
735 | |
678 | } elsif ($x >= 0 && $x < $self->{w} |
736 | } elsif ($x >= 0 && $x < $self->{w} |
679 | && $y >= 0 && $y < $border) { |
737 | && $y >= 0 && $y < $border) { |
680 | |
738 | |
681 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
739 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
682 | my ($bx, $by) = ($self->{x}, $self->{y}); |
740 | my ($bx, $by) = ($self->{x}, $self->{y}); |
683 | |
741 | |
684 | $self->{motion} = sub { |
742 | $self->{motion} = sub { |
685 | my ($ev, $x, $y) = @_; |
743 | my ($ev, $x, $y) = @_; |
686 | |
744 | |
687 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
745 | ($x, $y) = ($ev->{x}, $ev->{y}); |
688 | |
746 | |
689 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
747 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
690 | $self->update; |
748 | $self->update; |
691 | }; |
749 | }; |
692 | } |
750 | } |
… | |
… | |
737 | $bg->draw_quad ($border, $border, $cw, $ch); |
795 | $bg->draw_quad ($border, $border, $cw, $ch); |
738 | |
796 | |
739 | glDisable GL_TEXTURE_2D; |
797 | glDisable GL_TEXTURE_2D; |
740 | glDisable GL_BLEND; |
798 | glDisable GL_BLEND; |
741 | |
799 | |
|
|
800 | $self->{title}->draw if $self->{title}; |
742 | $self->child->draw; |
801 | $self->child->draw; |
743 | } |
802 | } |
744 | |
803 | |
745 | ############################################################################# |
804 | ############################################################################# |
746 | |
805 | |
… | |
… | |
748 | |
807 | |
749 | our @ISA = CFClient::UI::Base::; |
808 | our @ISA = CFClient::UI::Base::; |
750 | |
809 | |
751 | use List::Util qw(max sum); |
810 | use List::Util qw(max sum); |
752 | |
811 | |
753 | use SDL::OpenGL; |
812 | use CFClient::OpenGL; |
754 | |
813 | |
755 | sub new { |
814 | sub new { |
756 | my $class = shift; |
815 | my $class = shift; |
757 | |
816 | |
758 | $class->SUPER::new ( |
817 | $class->SUPER::new ( |
… | |
… | |
788 | or next; |
847 | or next; |
789 | |
848 | |
790 | for my $x (0 .. $#$row) { |
849 | for my $x (0 .. $#$row) { |
791 | my $widget = $row->[$x] |
850 | my $widget = $row->[$x] |
792 | or next; |
851 | or next; |
793 | my ($w, $h) = $widget->size_request; |
852 | my ($w, $h) = @$widget{qw(req_w req_h)}; |
794 | |
853 | |
795 | $w[$x] = max $w[$x], $w; |
854 | $w[$x] = max $w[$x], $w; |
796 | $h[$y] = max $h[$y], $h; |
855 | $h[$y] = max $h[$y], $h; |
797 | } |
856 | } |
798 | } |
857 | } |
… | |
… | |
903 | |
962 | |
904 | ($h, $w) = ($w, $h); |
963 | ($h, $w) = ($w, $h); |
905 | |
964 | |
906 | my $children = $self->{children}; |
965 | my $children = $self->{children}; |
907 | |
966 | |
908 | my @h = map +($_->size_request)[0], @$children; |
967 | my @h = map $_->{req_w}, @$children; |
909 | |
968 | |
910 | my $req_h = List::Util::sum @h; |
969 | my $req_h = List::Util::sum @h; |
911 | |
970 | |
912 | if ($req_h > $h) { |
971 | if ($req_h > $h) { |
913 | # ah well, not enough space |
972 | # ah well, not enough space |
… | |
… | |
961 | sub size_allocate { |
1020 | sub size_allocate { |
962 | my ($self, $w, $h) = @_; |
1021 | my ($self, $w, $h) = @_; |
963 | |
1022 | |
964 | my $children = $self->{children}; |
1023 | my $children = $self->{children}; |
965 | |
1024 | |
966 | my @h = map +($_->size_request)[1], @$children; |
1025 | my @h = map $_->{req_h}, @$children; |
967 | |
1026 | |
968 | my $req_h = List::Util::sum @h; |
1027 | my $req_h = List::Util::sum @h; |
969 | |
1028 | |
970 | if ($req_h > $h) { |
1029 | if ($req_h > $h) { |
971 | # ah well, not enough space |
1030 | # ah well, not enough space |
… | |
… | |
999 | |
1058 | |
1000 | package CFClient::UI::Label; |
1059 | package CFClient::UI::Label; |
1001 | |
1060 | |
1002 | our @ISA = CFClient::UI::Base::; |
1061 | our @ISA = CFClient::UI::Base::; |
1003 | |
1062 | |
1004 | use SDL::OpenGL; |
1063 | use CFClient::OpenGL; |
1005 | |
1064 | |
1006 | sub new { |
1065 | sub new { |
1007 | my ($class, %arg) = @_; |
1066 | my ($class, %arg) = @_; |
1008 | |
1067 | |
1009 | my $self = $class->SUPER::new ( |
1068 | my $self = $class->SUPER::new ( |
… | |
… | |
1012 | text => "", |
1071 | text => "", |
1013 | align => -1, |
1072 | align => -1, |
1014 | valign => -1, |
1073 | valign => -1, |
1015 | padding => 2, |
1074 | padding => 2, |
1016 | layout => new CFClient::Layout, |
1075 | layout => new CFClient::Layout, |
|
|
1076 | can_events => 0, |
1017 | %arg |
1077 | %arg |
1018 | ); |
1078 | ); |
1019 | |
1079 | |
1020 | $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# |
1080 | if (exists $self->{template}) { |
|
|
1081 | my $layout = new CFClient::Layout; |
|
|
1082 | $layout->set_text (delete $self->{template}); |
|
|
1083 | $self->{template} = $layout; |
|
|
1084 | } |
1021 | |
1085 | |
1022 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
1086 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
1023 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
1087 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
1024 | |
1088 | |
1025 | $self |
1089 | $self |
… | |
… | |
1039 | my ($self, $text) = @_; |
1103 | my ($self, $text) = @_; |
1040 | |
1104 | |
1041 | $self->{layout}->set_text ($text); |
1105 | $self->{layout}->set_text ($text); |
1042 | |
1106 | |
1043 | delete $self->{texture}; |
1107 | delete $self->{texture}; |
|
|
1108 | $self->check_size; |
1044 | $self->update; |
1109 | $self->update; |
1045 | } |
1110 | } |
1046 | |
1111 | |
1047 | sub set_markup { |
1112 | sub set_markup { |
1048 | my ($self, $markup) = @_; |
1113 | my ($self, $markup) = @_; |
1049 | |
1114 | |
1050 | $self->{layout}->set_markup ($markup); |
1115 | $self->{layout}->set_markup ($markup); |
1051 | |
1116 | |
1052 | delete $self->{texture}; |
1117 | delete $self->{texture}; |
|
|
1118 | $self->check_size; |
1053 | $self->update; |
1119 | $self->update; |
1054 | } |
1120 | } |
1055 | |
1121 | |
1056 | sub size_request { |
1122 | sub size_request { |
1057 | my ($self) = @_; |
1123 | my ($self) = @_; |
1058 | |
1124 | |
1059 | $self->{layout}->set_width; |
1125 | $self->{layout}->set_width; |
1060 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1126 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1061 | |
1127 | |
1062 | my ($w, $h) = $self->{layout}->size; |
1128 | my ($w, $h) = $self->{layout}->size; |
|
|
1129 | |
|
|
1130 | if (exists $self->{template}) { |
|
|
1131 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1132 | |
|
|
1133 | my ($w2, $h2) = $self->{template}->size; |
|
|
1134 | |
|
|
1135 | $w = List::Util::max $w, $w2; |
|
|
1136 | $h = List::Util::max $h, $h2; |
|
|
1137 | } |
1063 | |
1138 | |
1064 | ( |
1139 | ( |
1065 | $w + $self->{padding} * 2, |
1140 | $w + $self->{padding} * 2, |
1066 | $h + $self->{padding} * 2, |
1141 | $h + $self->{padding} * 2, |
1067 | ) |
1142 | ) |
… | |
… | |
1069 | |
1144 | |
1070 | sub size_allocate { |
1145 | sub size_allocate { |
1071 | my ($self, $w, $h) = @_; |
1146 | my ($self, $w, $h) = @_; |
1072 | |
1147 | |
1073 | delete $self->{texture}; |
1148 | delete $self->{texture}; |
|
|
1149 | } |
|
|
1150 | |
|
|
1151 | sub set_fontsize { |
|
|
1152 | my ($self, $fontsize) = @_; |
|
|
1153 | |
|
|
1154 | $self->{fontsize} = $fontsize; |
|
|
1155 | $self->check_size; |
1074 | } |
1156 | } |
1075 | |
1157 | |
1076 | sub _draw { |
1158 | sub _draw { |
1077 | my ($self) = @_; |
1159 | my ($self) = @_; |
1078 | |
1160 | |
… | |
… | |
1111 | |
1193 | |
1112 | package CFClient::UI::EntryBase; |
1194 | package CFClient::UI::EntryBase; |
1113 | |
1195 | |
1114 | our @ISA = CFClient::UI::Label::; |
1196 | our @ISA = CFClient::UI::Label::; |
1115 | |
1197 | |
1116 | use SDL; |
1198 | use CFClient::OpenGL; |
1117 | use SDL::OpenGL; |
|
|
1118 | |
1199 | |
1119 | sub new { |
1200 | sub new { |
1120 | my $class = shift; |
1201 | my $class = shift; |
1121 | |
1202 | |
1122 | $class->SUPER::new ( |
1203 | $class->SUPER::new ( |
… | |
… | |
1125 | active_bg => [1, 1, 1, 0.5], |
1206 | active_bg => [1, 1, 1, 0.5], |
1126 | active_fg => [0, 0, 0], |
1207 | active_fg => [0, 0, 0], |
1127 | can_hover => 1, |
1208 | can_hover => 1, |
1128 | can_focus => 1, |
1209 | can_focus => 1, |
1129 | valign => 0, |
1210 | valign => 0, |
|
|
1211 | can_events => 1, |
1130 | @_ |
1212 | @_ |
1131 | ) |
1213 | ) |
1132 | } |
1214 | } |
1133 | |
1215 | |
1134 | sub _set_text { |
1216 | sub _set_text { |
… | |
… | |
1162 | } |
1244 | } |
1163 | |
1245 | |
1164 | sub size_allocate { |
1246 | sub size_allocate { |
1165 | my ($self, $w, $h) = @_; |
1247 | my ($self, $w, $h) = @_; |
1166 | |
1248 | |
1167 | $self->_set_text ($self->{text}); |
1249 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
1168 | } |
1250 | } |
1169 | |
1251 | |
1170 | sub set_text { |
1252 | sub set_text { |
1171 | my ($self, $text) = @_; |
1253 | my ($self, $text) = @_; |
1172 | |
1254 | |
… | |
… | |
1176 | } |
1258 | } |
1177 | |
1259 | |
1178 | sub key_down { |
1260 | sub key_down { |
1179 | my ($self, $ev) = @_; |
1261 | my ($self, $ev) = @_; |
1180 | |
1262 | |
1181 | my $mod = $ev->key_mod; |
1263 | my $mod = $ev->{mod}; |
1182 | my $sym = $ev->key_sym; |
1264 | my $sym = $ev->{sym}; |
1183 | |
|
|
1184 | my $uni = $ev->key_unicode; |
1265 | my $uni = $ev->{unicode}; |
1185 | |
1266 | |
1186 | my $text = $self->get_text; |
1267 | my $text = $self->get_text; |
1187 | |
1268 | |
1188 | if ($sym == SDLK_BACKSPACE) { |
1269 | if ($sym == 8) { |
1189 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1270 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1190 | } elsif ($sym == SDLK_DELETE) { |
1271 | } elsif ($sym == 127) { |
1191 | substr $text, $self->{cursor}, 1, ""; |
1272 | substr $text, $self->{cursor}, 1, ""; |
1192 | } elsif ($sym == SDLK_LEFT) { |
1273 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1193 | --$self->{cursor} if $self->{cursor}; |
1274 | --$self->{cursor} if $self->{cursor}; |
1194 | } elsif ($sym == SDLK_RIGHT) { |
1275 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1195 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1276 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1196 | } elsif ($sym == SDLK_HOME) { |
1277 | } elsif ($sym == CFClient::SDLK_HOME) { |
1197 | $self->{cursor} = 0; |
1278 | $self->{cursor} = 0; |
1198 | } elsif ($sym == SDLK_END) { |
1279 | } elsif ($sym == CFClient::SDLK_END) { |
1199 | $self->{cursor} = length $text; |
1280 | $self->{cursor} = length $text; |
1200 | } elsif ($sym == SDLK_ESCAPE) { |
1281 | } elsif ($sym == 27) { |
1201 | $self->emit ('escape'); |
1282 | $self->emit ('escape'); |
1202 | } elsif ($uni) { |
1283 | } elsif ($uni) { |
1203 | substr $text, $self->{cursor}++, 0, chr $uni; |
1284 | substr $text, $self->{cursor}++, 0, chr $uni; |
1204 | } |
1285 | } |
1205 | |
1286 | |
… | |
… | |
1280 | |
1361 | |
1281 | package CFClient::UI::Entry; |
1362 | package CFClient::UI::Entry; |
1282 | |
1363 | |
1283 | our @ISA = CFClient::UI::EntryBase::; |
1364 | our @ISA = CFClient::UI::EntryBase::; |
1284 | |
1365 | |
1285 | use SDL; |
1366 | use CFClient::OpenGL; |
1286 | use SDL::OpenGL; |
|
|
1287 | |
1367 | |
1288 | sub key_down { |
1368 | sub key_down { |
1289 | my ($self, $ev) = @_; |
1369 | my ($self, $ev) = @_; |
1290 | |
1370 | |
1291 | my $sym = $ev->key_sym; |
1371 | my $sym = $ev->{sym}; |
1292 | |
1372 | |
1293 | if ($sym == SDLK_RETURN) { |
1373 | if ($sym == 13) { |
1294 | $self->emit (activate => $self->get_text); |
1374 | $self->emit (activate => $self->get_text); |
1295 | $self->update; |
1375 | $self->update; |
1296 | |
1376 | |
1297 | } else { |
1377 | } else { |
1298 | $self->SUPER::key_down ($ev); |
1378 | $self->SUPER::key_down ($ev); |
… | |
… | |
1304 | |
1384 | |
1305 | package CFClient::UI::Button; |
1385 | package CFClient::UI::Button; |
1306 | |
1386 | |
1307 | our @ISA = CFClient::UI::Label::; |
1387 | our @ISA = CFClient::UI::Label::; |
1308 | |
1388 | |
1309 | use SDL; |
1389 | use CFClient::OpenGL; |
1310 | use SDL::OpenGL; |
|
|
1311 | |
1390 | |
1312 | my @tex = |
1391 | my @tex = |
1313 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1392 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1314 | qw(b1_button_active.png); |
1393 | qw(b1_button_active.png); |
1315 | |
1394 | |
1316 | sub new { |
1395 | sub new { |
1317 | my $class = shift; |
1396 | my $class = shift; |
1318 | |
1397 | |
… | |
… | |
1322 | bg => [1, 1, 1, 0.2], |
1401 | bg => [1, 1, 1, 0.2], |
1323 | active_fg => [0, 0, 1], |
1402 | active_fg => [0, 0, 1], |
1324 | can_hover => 1, |
1403 | can_hover => 1, |
1325 | align => 0, |
1404 | align => 0, |
1326 | valign => 0, |
1405 | valign => 0, |
|
|
1406 | can_events => 1, |
1327 | @_ |
1407 | @_ |
1328 | ) |
1408 | ) |
1329 | } |
1409 | } |
1330 | |
1410 | |
1331 | sub button_up { |
1411 | sub button_up { |
… | |
… | |
1365 | package CFClient::UI::CheckBox; |
1445 | package CFClient::UI::CheckBox; |
1366 | |
1446 | |
1367 | our @ISA = CFClient::UI::DrawBG::; |
1447 | our @ISA = CFClient::UI::DrawBG::; |
1368 | |
1448 | |
1369 | my @tex = |
1449 | my @tex = |
1370 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1450 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1371 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1451 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1372 | |
1452 | |
1373 | use SDL; |
1453 | use CFClient::OpenGL; |
1374 | use SDL::OpenGL; |
|
|
1375 | |
1454 | |
1376 | sub new { |
1455 | sub new { |
1377 | my $class = shift; |
1456 | my $class = shift; |
1378 | |
1457 | |
1379 | $class->SUPER::new ( |
1458 | $class->SUPER::new ( |
… | |
… | |
1425 | glDisable GL_BLEND; |
1504 | glDisable GL_BLEND; |
1426 | } |
1505 | } |
1427 | |
1506 | |
1428 | ############################################################################# |
1507 | ############################################################################# |
1429 | |
1508 | |
1430 | package CFClient::UI::VGauge; |
1509 | package CFClient::UI::Image; |
1431 | |
1510 | |
1432 | our @ISA = CFClient::UI::Base::; |
1511 | our @ISA = CFClient::UI::Base::; |
1433 | |
1512 | |
1434 | use SDL::OpenGL; |
1513 | use CFClient::OpenGL; |
|
|
1514 | use Carp qw/confess/; |
1435 | |
1515 | |
1436 | my %tex = ( |
1516 | 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 | |
1517 | |
1455 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
|
|
1456 | sub new { |
1518 | sub new { |
1457 | my $class = shift; |
1519 | my $class = shift; |
1458 | |
1520 | |
1459 | my $self = $class->SUPER::new (gauge => 'food', @_); |
1521 | my $self = $class->SUPER::new (can_events => 0, @_); |
|
|
1522 | |
|
|
1523 | $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; |
|
|
1524 | |
|
|
1525 | $loaded_images{$self->{image}} ||= |
|
|
1526 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; |
|
|
1527 | |
|
|
1528 | my $tex = $self->{tex} = $loaded_images{$self->{image}}; |
|
|
1529 | |
|
|
1530 | Scalar::Util::weaken $loaded_images{$self->{image}}; |
|
|
1531 | |
|
|
1532 | $self->{aspect} = $tex->{w} / $tex->{h}; |
1460 | |
1533 | |
1461 | $self |
1534 | $self |
1462 | } |
1535 | } |
1463 | |
1536 | |
1464 | sub size_request { |
1537 | sub size_request { |
1465 | my ($self) = @_; |
1538 | my ($self) = @_; |
1466 | |
1539 | |
1467 | my $tex = $tex{$self->{gauge}}[0]; |
1540 | ($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 | } |
1541 | } |
1489 | |
1542 | |
1490 | sub _draw { |
1543 | sub _draw { |
1491 | my ($self) = @_; |
1544 | my ($self) = @_; |
1492 | |
1545 | |
1493 | my $tex = $tex{$self->{gauge}}; |
1546 | my $tex = $self->{tex}; |
1494 | |
1547 | |
1495 | my ($w, $h) = ($self->{w}, $self->{h}); |
1548 | my ($w, $h) = ($self->{w}, $self->{h}); |
1496 | |
1549 | |
1497 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
1550 | if ($self->{rot90}) { |
1498 | $ycut = 1 if $self->{val} > $self->{max_val}; |
1551 | glRotate 90, 0, 0, 1; |
|
|
1552 | glTranslate 0, -$self->{w}, 0; |
1499 | |
1553 | |
1500 | my $t1 = $tex->[0]; |
1554 | ($w, $h) = ($h, $w); |
1501 | my $t2 = $tex->[1]; |
1555 | } |
1502 | |
1556 | |
1503 | glEnable GL_BLEND; |
1557 | glEnable GL_BLEND; |
1504 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1558 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1505 | glEnable GL_TEXTURE_2D; |
1559 | glEnable GL_TEXTURE_2D; |
1506 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1560 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1507 | |
1561 | |
|
|
1562 | $tex->draw_quad (0, 0, $w, $h); |
|
|
1563 | |
|
|
1564 | glDisable GL_BLEND; |
|
|
1565 | glDisable GL_TEXTURE_2D; |
|
|
1566 | } |
|
|
1567 | |
|
|
1568 | ############################################################################# |
|
|
1569 | |
|
|
1570 | package CFClient::UI::VGauge; |
|
|
1571 | |
|
|
1572 | our @ISA = CFClient::UI::Base::; |
|
|
1573 | |
|
|
1574 | use CFClient::OpenGL; |
|
|
1575 | |
|
|
1576 | my %tex = ( |
|
|
1577 | food => [ |
|
|
1578 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1579 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
|
|
1580 | ], |
|
|
1581 | grace => [ |
|
|
1582 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1583 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ |
|
|
1584 | ], |
|
|
1585 | hp => [ |
|
|
1586 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1587 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
|
|
1588 | ], |
|
|
1589 | mana => [ |
|
|
1590 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1591 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ |
|
|
1592 | ], |
|
|
1593 | ); |
|
|
1594 | |
|
|
1595 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
|
|
1596 | sub new { |
|
|
1597 | my $class = shift; |
|
|
1598 | |
|
|
1599 | my $self = $class->SUPER::new ( |
|
|
1600 | type => 'food', |
|
|
1601 | @_ |
|
|
1602 | ); |
|
|
1603 | |
|
|
1604 | $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h}; |
|
|
1605 | |
|
|
1606 | $self |
|
|
1607 | } |
|
|
1608 | |
|
|
1609 | sub size_request { |
|
|
1610 | my ($self) = @_; |
|
|
1611 | |
|
|
1612 | #my $tex = $tex{$self->{type}}[0]; |
|
|
1613 | #@$tex{qw(w h)} |
|
|
1614 | (0, 0) |
|
|
1615 | } |
|
|
1616 | |
|
|
1617 | sub set_max { |
|
|
1618 | my ($self, $max) = @_; |
|
|
1619 | |
|
|
1620 | $self->{max_val} = $max; |
|
|
1621 | } |
|
|
1622 | |
|
|
1623 | sub set_value { |
|
|
1624 | my ($self, $val, $max) = @_; |
|
|
1625 | |
|
|
1626 | $self->set_max ($max) |
|
|
1627 | if defined $max; |
|
|
1628 | |
|
|
1629 | $max = $self->{max_val}; |
|
|
1630 | $self->{val} = $val; |
|
|
1631 | |
|
|
1632 | $self->update; |
|
|
1633 | } |
|
|
1634 | |
|
|
1635 | sub _draw { |
|
|
1636 | my ($self) = @_; |
|
|
1637 | |
|
|
1638 | my $tex = $tex{$self->{type}}; |
|
|
1639 | |
|
|
1640 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1641 | |
|
|
1642 | if ($self->{vertical}) { |
|
|
1643 | glRotate 90, 0, 0, 1; |
|
|
1644 | glTranslate 0, -$self->{w}, 0; |
|
|
1645 | |
|
|
1646 | ($w, $h) = ($h, $w); |
|
|
1647 | } |
|
|
1648 | |
|
|
1649 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
|
|
1650 | $ycut = 1 if $self->{val} > $self->{max_val}; |
|
|
1651 | |
|
|
1652 | my $t1 = $tex->[0]; |
|
|
1653 | my $t2 = $tex->[1]; |
|
|
1654 | |
|
|
1655 | glEnable GL_BLEND; |
|
|
1656 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1657 | glEnable GL_TEXTURE_2D; |
|
|
1658 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1659 | |
1508 | my $h1 = $self->{h} - $ycut * $self->{h}; |
1660 | my $h1 = $self->{h} - $ycut * $self->{h}; |
1509 | my $h2 = $ycut * $self->{h}; |
1661 | my $h2 = $ycut * $self->{h}; |
1510 | |
1662 | |
1511 | my $yp = 0; |
|
|
1512 | |
|
|
1513 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
1663 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
1514 | glBegin GL_QUADS; |
1664 | glBegin GL_QUADS; |
1515 | glTexCoord 0 , 0; glVertex 0 , $yp; |
1665 | glTexCoord 0 , 0; glVertex 0 , 0; |
1516 | glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; |
1666 | glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; |
1517 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; |
1667 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; |
1518 | glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; |
1668 | glTexCoord $t1->{s}, 0; glVertex $w, 0; |
1519 | glEnd; |
1669 | glEnd; |
1520 | |
|
|
1521 | $yp += $h1; |
|
|
1522 | |
1670 | |
1523 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
1671 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
1524 | glBegin GL_QUADS; |
1672 | glBegin GL_QUADS; |
1525 | glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; |
1673 | glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; |
1526 | glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; |
1674 | glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; |
1527 | glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2; |
1675 | glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; |
1528 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; |
1676 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; |
1529 | glEnd; |
1677 | glEnd; |
1530 | |
1678 | |
1531 | glDisable GL_BLEND; |
1679 | glDisable GL_BLEND; |
1532 | glDisable GL_TEXTURE_2D; |
1680 | glDisable GL_TEXTURE_2D; |
1533 | } |
1681 | } |
1534 | |
1682 | |
1535 | ############################################################################# |
1683 | ############################################################################# |
1536 | |
1684 | |
|
|
1685 | package CFClient::UI::Gauge; |
|
|
1686 | |
|
|
1687 | our @ISA = CFClient::UI::VBox::; |
|
|
1688 | |
|
|
1689 | sub new { |
|
|
1690 | my ($class, %arg) = shift; |
|
|
1691 | |
|
|
1692 | my $self = $class->SUPER::new ( |
|
|
1693 | @_, |
|
|
1694 | ); |
|
|
1695 | |
|
|
1696 | $self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"; |
|
|
1697 | $self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999"; |
|
|
1698 | $self->add ($self->{value}); |
|
|
1699 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); |
|
|
1700 | $self->add ($self->{max}); |
|
|
1701 | |
|
|
1702 | $self |
|
|
1703 | } |
|
|
1704 | |
|
|
1705 | sub size_request { |
|
|
1706 | my ($self) = @_; |
|
|
1707 | (($self->{max}->size_request)[0], 0) |
|
|
1708 | } |
|
|
1709 | |
|
|
1710 | sub set_fontsize { |
|
|
1711 | my ($self, $fsize) = @_; |
|
|
1712 | |
|
|
1713 | $self->{value}->set_fontsize ($fsize); |
|
|
1714 | $self->{max} ->set_fontsize ($fsize); |
|
|
1715 | } |
|
|
1716 | |
|
|
1717 | sub set_value { |
|
|
1718 | my ($self, $val, $max) = @_; |
|
|
1719 | |
|
|
1720 | $self->set_max ($max) |
|
|
1721 | if defined $max; |
|
|
1722 | |
|
|
1723 | $self->{gauge}->set_value ($val, $max); |
|
|
1724 | $self->{value}->set_text ($val); |
|
|
1725 | } |
|
|
1726 | |
|
|
1727 | sub set_max { |
|
|
1728 | my ($self, $max) = @_; |
|
|
1729 | |
|
|
1730 | $self->{gauge}->set_max ($max); |
|
|
1731 | $self->{max}->set_text ($max); |
|
|
1732 | } |
|
|
1733 | |
|
|
1734 | ############################################################################# |
|
|
1735 | |
1537 | package CFClient::UI::Slider; |
1736 | package CFClient::UI::Slider; |
1538 | |
1737 | |
1539 | use strict; |
1738 | use strict; |
1540 | |
1739 | |
1541 | use SDL::OpenGL; |
1740 | use CFClient::OpenGL; |
1542 | |
1741 | |
1543 | our @ISA = CFClient::UI::DrawBG::; |
1742 | our @ISA = CFClient::UI::DrawBG::; |
1544 | |
1743 | |
1545 | my @tex = |
1744 | my @tex = |
1546 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1745 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
… | |
… | |
1671 | |
1870 | |
1672 | package CFClient::UI::TextView; |
1871 | package CFClient::UI::TextView; |
1673 | |
1872 | |
1674 | our @ISA = CFClient::UI::HBox::; |
1873 | our @ISA = CFClient::UI::HBox::; |
1675 | |
1874 | |
1676 | use SDL::OpenGL; |
1875 | use CFClient::OpenGL; |
1677 | |
1876 | |
1678 | sub new { |
1877 | sub new { |
1679 | my $class = shift; |
1878 | my $class = shift; |
1680 | |
1879 | |
1681 | my $self = $class->SUPER::new ( |
1880 | my $self = $class->SUPER::new ( |
… | |
… | |
1825 | |
2024 | |
1826 | } |
2025 | } |
1827 | |
2026 | |
1828 | ############################################################################# |
2027 | ############################################################################# |
1829 | |
2028 | |
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; |
2029 | package CFClient::UI::Animator; |
2067 | |
2030 | |
2068 | use SDL::OpenGL; |
2031 | use CFClient::OpenGL; |
2069 | |
2032 | |
2070 | our @ISA = CFClient::UI::Bin::; |
2033 | our @ISA = CFClient::UI::Bin::; |
2071 | |
2034 | |
2072 | sub moveto { |
2035 | sub moveto { |
2073 | my ($self, $x, $y) = @_; |
2036 | my ($self, $x, $y) = @_; |
… | |
… | |
2113 | my $class = shift; |
2076 | my $class = shift; |
2114 | |
2077 | |
2115 | my $self = $class->SUPER::new ( |
2078 | my $self = $class->SUPER::new ( |
2116 | state => 0, |
2079 | state => 0, |
2117 | connect_activate => \&toggle_flopper, |
2080 | connect_activate => \&toggle_flopper, |
|
|
2081 | can_events => 1, |
2118 | @_ |
2082 | @_ |
2119 | ); |
2083 | ); |
2120 | |
2084 | |
2121 | if ($self->{state}) { |
2085 | if ($self->{state}) { |
2122 | $self->{state} = 0; |
2086 | $self->{state} = 0; |
… | |
… | |
2146 | |
2110 | |
2147 | package CFClient::UI::Root; |
2111 | package CFClient::UI::Root; |
2148 | |
2112 | |
2149 | our @ISA = CFClient::UI::Container::; |
2113 | our @ISA = CFClient::UI::Container::; |
2150 | |
2114 | |
2151 | use SDL::OpenGL; |
2115 | use CFClient::OpenGL; |
2152 | |
2116 | |
2153 | sub check_size { |
2117 | sub check_size { |
2154 | my ($self) = @_; |
2118 | my ($self) = @_; |
2155 | |
2119 | |
2156 | $self->configure (0, 0, $::WITH, $::HEIGHT); |
2120 | $self->configure (0, 0, $::WIDTH, $::HEIGHT); |
2157 | } |
2121 | } |
2158 | |
2122 | |
2159 | sub size_request { |
2123 | sub size_request { |
2160 | ($::WIDTH, $::HEIGHT) |
2124 | ($::WIDTH, $::HEIGHT) |
2161 | } |
2125 | } |
… | |
… | |
2163 | sub configure { |
2127 | sub configure { |
2164 | my ($self, $x, $y, $w, $h) = @_; |
2128 | my ($self, $x, $y, $w, $h) = @_; |
2165 | |
2129 | |
2166 | $self->SUPER::configure ($x, $y, $w, $h); |
2130 | $self->SUPER::configure ($x, $y, $w, $h); |
2167 | |
2131 | |
2168 | $_->configure ($_->{x}, $_->{y}, $_->size_request) |
|
|
2169 | for @{$self->{children}}; |
2132 | for my $child (@{$self->{children}}) { |
|
|
2133 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
|
|
2134 | |
|
|
2135 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
|
|
2136 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
|
|
2137 | $child->configure ($X, $Y, $W,$H); |
|
|
2138 | } |
2170 | } |
2139 | } |
2171 | |
2140 | |
2172 | sub _topleft { |
2141 | sub _topleft { |
2173 | my ($self, $x, $y) = @_; |
2142 | my ($self, $x, $y) = @_; |
2174 | |
2143 | |
… | |
… | |
2182 | ::refresh (); |
2151 | ::refresh (); |
2183 | } |
2152 | } |
2184 | |
2153 | |
2185 | sub add { |
2154 | sub add { |
2186 | my ($self, $child) = @_; |
2155 | my ($self, $child) = @_; |
|
|
2156 | |
|
|
2157 | # integerize window positions |
|
|
2158 | $child->{x} = int $child->{x}; |
|
|
2159 | $child->{y} = int $child->{y}; |
2187 | |
2160 | |
2188 | $self->SUPER::add ($child); |
2161 | $self->SUPER::add ($child); |
2189 | } |
2162 | } |
2190 | |
2163 | |
2191 | sub on_refresh { |
2164 | sub on_refresh { |