… | |
… | |
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 | w => -1, |
|
|
102 | h => -1, |
|
|
103 | @_ |
101 | @_ |
104 | }, $class; |
102 | }, $class; |
105 | |
103 | |
106 | for (keys %$self) { |
104 | for (keys %$self) { |
107 | if (/^connect_(.*)$/) { |
105 | if (/^connect_(.*)$/) { |
… | |
… | |
110 | } |
108 | } |
111 | |
109 | |
112 | $self |
110 | $self |
113 | } |
111 | } |
114 | |
112 | |
|
|
113 | sub show { |
|
|
114 | my ($self) = @_; |
|
|
115 | |
|
|
116 | return if $self->{parent}; |
|
|
117 | |
|
|
118 | $CFClient::UI::ROOT->add ($self); |
|
|
119 | } |
|
|
120 | |
|
|
121 | sub hide { |
|
|
122 | my ($self) = @_; |
|
|
123 | |
|
|
124 | return unless $self->{parent}; |
|
|
125 | |
|
|
126 | $self->{parent}->remove ($self); |
|
|
127 | } |
|
|
128 | |
115 | sub move { |
129 | sub move { |
116 | my ($self, $x, $y, $z) = @_; |
130 | my ($self, $x, $y, $z) = @_; |
|
|
131 | |
117 | $self->{x} = int $x; |
132 | $self->{x} = int $x; |
118 | $self->{y} = int $y; |
133 | $self->{y} = int $y; |
119 | $self->{z} = $z if defined $z; |
134 | $self->{z} = $z if defined $z; |
|
|
135 | |
|
|
136 | $self->update; |
120 | } |
137 | } |
121 | |
138 | |
122 | sub needs_redraw { |
139 | sub needs_redraw { |
123 | 0 |
140 | 0 |
124 | } |
141 | } |
… | |
… | |
126 | sub size_request { |
143 | sub size_request { |
127 | require Carp; |
144 | require Carp; |
128 | Carp::confess "size_request is abtract"; |
145 | Carp::confess "size_request is abtract"; |
129 | } |
146 | } |
130 | |
147 | |
131 | sub _size_allocate { |
148 | sub configure { |
132 | my ($self, $x, $y, $w, $h) = @_; |
149 | my ($self, $x, $y, $w, $h) = @_; |
133 | |
150 | |
134 | $self->{x} = $x; |
151 | $self->{x} = $x; |
135 | $self->{y} = $y; |
152 | $self->{y} = $y; |
136 | |
153 | |
|
|
154 | if ($self->{aspect}) { |
|
|
155 | $w = List::Util::min $w, int $h * $self->{aspect}; |
|
|
156 | $h = List::Util::min $h, int $w / $self->{aspect}; |
|
|
157 | } |
|
|
158 | |
137 | return unless $self->{w} != $w || $self->{h} != $h; |
159 | return unless $self->{w} != $w || $self->{h} != $h; |
138 | |
160 | |
139 | $self->{w} = $w; |
161 | $self->{w} = $w; |
140 | $self->{h} = $h; |
162 | $self->{h} = $h; |
141 | |
163 | |
142 | 1 |
164 | $self->size_allocate ($w, $h); |
|
|
165 | $self->update; |
143 | } |
166 | } |
144 | |
167 | |
145 | sub size_allocate { |
168 | sub size_allocate { |
146 | my ($self, $x, $y, $w, $h) = @_; |
169 | # nothing to be done |
147 | |
|
|
148 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
149 | } |
170 | } |
150 | |
171 | |
151 | # return top left coordinates |
172 | # return top left coordinates |
152 | sub _topleft { |
173 | sub _topleft { |
153 | my ($self, $x, $y) = @_; |
174 | my ($self, $x, $y) = @_; |
… | |
… | |
284 | } |
305 | } |
285 | |
306 | |
286 | sub connect { |
307 | sub connect { |
287 | my ($self, $signal, $cb) = @_; |
308 | my ($self, $signal, $cb) = @_; |
288 | |
309 | |
289 | push @{ $self->{cb}{$signal} }, $cb; |
310 | push @{ $self->{signal_cb}{$signal} }, $cb; |
290 | } |
311 | } |
291 | |
312 | |
292 | sub emit { |
313 | sub emit { |
293 | my ($self, $signal, @args) = @_; |
314 | my ($self, $signal, @args) = @_; |
294 | |
315 | |
|
|
316 | for my $cb (@{$self->{signal_cb}{$signal} || []}) { |
295 | $_->($self, @args) |
317 | $cb->($self, @args); |
296 | for @{$self->{cb}{$signal} || []}; |
318 | } |
297 | } |
319 | } |
298 | |
320 | |
299 | sub DESTROY { |
321 | sub DESTROY { |
300 | my ($self) = @_; |
322 | my ($self) = @_; |
301 | |
323 | |
… | |
… | |
307 | package CFClient::UI::DrawBG; |
329 | package CFClient::UI::DrawBG; |
308 | |
330 | |
309 | our @ISA = CFClient::UI::Base::; |
331 | our @ISA = CFClient::UI::Base::; |
310 | |
332 | |
311 | use strict; |
333 | use strict; |
312 | use SDL::OpenGL; |
334 | use CFClient::OpenGL; |
313 | |
335 | |
314 | sub new { |
336 | sub new { |
315 | my $class = shift; |
337 | my $class = shift; |
316 | |
338 | |
317 | # range [value, low, high, page] |
339 | # range [value, low, high, page] |
… | |
… | |
381 | $self->{children} = [ |
403 | $self->{children} = [ |
382 | sort { $a->{z} <=> $b->{z} } |
404 | sort { $a->{z} <=> $b->{z} } |
383 | @{$self->{children}}, $child |
405 | @{$self->{children}}, $child |
384 | ]; |
406 | ]; |
385 | |
407 | |
386 | $self->{w} = $self->{h} = -1; |
|
|
387 | |
|
|
388 | $child->check_size; |
408 | $child->check_size; |
389 | } |
409 | } |
390 | |
410 | |
391 | sub remove { |
411 | sub remove { |
392 | my ($self, $widget) = @_; |
412 | my ($self, $child) = @_; |
393 | |
413 | |
|
|
414 | delete $child->{parent}; |
|
|
415 | |
394 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
416 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
395 | |
417 | |
396 | $self->check_size; |
418 | $self->check_size; |
397 | } |
419 | } |
398 | |
420 | |
399 | sub find_widget { |
421 | sub find_widget { |
… | |
… | |
431 | |
453 | |
432 | $class->SUPER::new (children => [$child], %arg) |
454 | $class->SUPER::new (children => [$child], %arg) |
433 | } |
455 | } |
434 | |
456 | |
435 | sub add { |
457 | sub add { |
436 | my ($self, $widget) = @_; |
458 | my ($self, $child) = @_; |
437 | |
459 | |
438 | $self->{children} = []; |
460 | $self->{children} = []; |
439 | |
461 | |
440 | $self->SUPER::add ($widget); |
462 | $self->SUPER::add ($child); |
441 | } |
463 | } |
442 | |
464 | |
443 | sub remove { |
465 | sub remove { |
444 | my ($self, $widget) = @_; |
466 | my ($self, $widget) = @_; |
445 | |
467 | |
… | |
… | |
454 | sub size_request { |
476 | sub size_request { |
455 | $_[0]{children}[0]->size_request |
477 | $_[0]{children}[0]->size_request |
456 | } |
478 | } |
457 | |
479 | |
458 | sub size_allocate { |
480 | sub size_allocate { |
459 | my ($self, $x, $y, $w, $h) = @_; |
481 | my ($self, $w, $h) = @_; |
460 | |
482 | |
461 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
462 | |
|
|
463 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
483 | $self->{children}[0]->configure (0, 0, $w, $h); |
464 | } |
484 | } |
465 | |
485 | |
466 | ############################################################################# |
486 | ############################################################################# |
467 | |
487 | |
468 | package CFClient::UI::Window; |
488 | package CFClient::UI::Window; |
469 | |
489 | |
470 | our @ISA = CFClient::UI::Bin::; |
490 | our @ISA = CFClient::UI::Bin::; |
471 | |
491 | |
472 | use SDL::OpenGL; |
492 | use CFClient::OpenGL; |
473 | |
493 | |
474 | sub new { |
494 | sub new { |
475 | my ($class, %arg) = @_; |
495 | my ($class, %arg) = @_; |
476 | |
496 | |
477 | my $self = $class->SUPER::new (%arg); |
497 | my $self = $class->SUPER::new (%arg); |
… | |
… | |
494 | $self->child->draw; |
514 | $self->child->draw; |
495 | }; |
515 | }; |
496 | } |
516 | } |
497 | |
517 | |
498 | sub size_allocate { |
518 | sub size_allocate { |
499 | my ($self, $x, $y, $w, $h) = @_; |
519 | my ($self, $w, $h) = @_; |
500 | |
520 | |
501 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
502 | |
|
|
503 | $self->child->size_allocate (0, 0, $w, $h); |
521 | $self->child->configure (0, 0, $w, $h); |
504 | |
522 | |
505 | $self->render_chld; |
523 | $self->render_chld; |
506 | } |
524 | } |
507 | |
525 | |
508 | sub _draw { |
526 | sub _draw { |
… | |
… | |
539 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
557 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
540 | |
558 | |
541 | @$self{qw(child_w child_h)} |
559 | @$self{qw(child_w child_h)} |
542 | } |
560 | } |
543 | |
561 | |
544 | sub size_allocate { |
|
|
545 | my ($self, $x, $y, $w, $h) = @_; |
|
|
546 | |
|
|
547 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
548 | } |
|
|
549 | |
|
|
550 | sub _draw { |
562 | sub _draw { |
551 | my ($self) = @_; |
563 | my ($self) = @_; |
552 | |
564 | |
553 | $self->{children}[1]->draw; |
565 | $self->{children}[1]->draw; |
554 | } |
566 | } |
… | |
… | |
558 | |
570 | |
559 | package CFClient::UI::Frame; |
571 | package CFClient::UI::Frame; |
560 | |
572 | |
561 | our @ISA = CFClient::UI::Bin::; |
573 | our @ISA = CFClient::UI::Bin::; |
562 | |
574 | |
563 | use SDL::OpenGL; |
575 | use CFClient::OpenGL; |
564 | |
576 | |
565 | sub size_request { |
577 | sub size_request { |
566 | my ($self) = @_; |
578 | my ($self) = @_; |
567 | my $chld = $self->child |
579 | my $chld = $self->child |
568 | or return (0, 0); |
580 | or return (0, 0); |
… | |
… | |
573 | } |
585 | } |
574 | |
586 | |
575 | sub size_allocate { |
587 | sub size_allocate { |
576 | my ($self, $x, $y, $w, $h) = @_; |
588 | my ($self, $x, $y, $w, $h) = @_; |
577 | |
589 | |
578 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
579 | |
|
|
580 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
590 | $self->child->configure (2, 2, $w - 4, $h - 4); |
581 | } |
591 | } |
582 | |
592 | |
583 | sub _draw { |
593 | sub _draw { |
584 | my ($self) = @_; |
594 | my ($self) = @_; |
585 | |
595 | |
… | |
… | |
602 | |
612 | |
603 | package CFClient::UI::FancyFrame; |
613 | package CFClient::UI::FancyFrame; |
604 | |
614 | |
605 | our @ISA = CFClient::UI::Bin::; |
615 | our @ISA = CFClient::UI::Bin::; |
606 | |
616 | |
607 | use SDL::OpenGL; |
617 | use CFClient::OpenGL; |
608 | |
618 | |
609 | my @tex = |
619 | my @tex = |
610 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
620 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
611 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
621 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
612 | |
622 | |
… | |
… | |
616 | # TODO: user_x, user_y, overwrite moveto? |
626 | # TODO: user_x, user_y, overwrite moveto? |
617 | |
627 | |
618 | $class->SUPER::new ( |
628 | $class->SUPER::new ( |
619 | bg => [1, 1, 1, 1], |
629 | bg => [1, 1, 1, 1], |
620 | border_bg => [1, 1, 1, 1], |
630 | border_bg => [1, 1, 1, 1], |
621 | border => int $::FONTSIZE * 0.8, |
631 | border => 0.8, |
622 | @_ |
632 | @_ |
623 | ) |
633 | ) |
624 | } |
634 | } |
625 | |
635 | |
|
|
636 | sub border { |
|
|
637 | int $_[0]{border} * $::FONTSIZE |
|
|
638 | } |
|
|
639 | |
626 | sub size_request { |
640 | sub size_request { |
627 | my ($self) = @_; |
641 | my ($self) = @_; |
628 | |
642 | |
629 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
643 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
630 | |
644 | |
631 | my ($w, $h) = $self->SUPER::size_request; |
645 | my ($w, $h) = $self->SUPER::size_request; |
632 | |
646 | |
633 | ( |
647 | ( |
634 | $w + $self->{border} * 2, |
648 | $w + $self->border * 2, |
635 | $h + $self->{border} * 2, |
649 | $h + $self->border * 2, |
636 | ) |
650 | ) |
637 | } |
651 | } |
638 | |
652 | |
639 | sub size_allocate { |
653 | sub size_allocate { |
640 | my ($self, $x, $y, $w, $h) = @_; |
654 | my ($self, $w, $h) = @_; |
641 | |
655 | |
642 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
643 | |
|
|
644 | $h -= List::Util::max 0, $self->{border} * 2; |
656 | $h -= List::Util::max 0, $self->border * 2; |
645 | $w -= List::Util::max 0, $self->{border} * 2; |
657 | $w -= List::Util::max 0, $self->border * 2; |
646 | |
658 | |
647 | $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); |
659 | $self->child->configure ($self->border, $self->border, $w, $h); |
648 | } |
660 | } |
649 | |
661 | |
650 | sub button_down { |
662 | sub button_down { |
651 | my ($self, $ev, $x, $y) = @_; |
663 | my ($self, $ev, $x, $y) = @_; |
652 | |
664 | |
|
|
665 | my $border = $self->border; |
|
|
666 | |
653 | if ($x < $self->{w} && $x >= $self->{w} - $self->{border} |
667 | if ($x < $self->{w} && $x >= $self->{w} - $border |
654 | && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { |
668 | && $y < $self->{h} && $y >= $self->{h} - $border) { |
655 | |
669 | |
656 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
670 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
657 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
671 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
658 | |
672 | |
659 | $self->{motion} = sub { |
673 | $self->{motion} = sub { |
660 | my ($ev, $x, $y) = @_; |
674 | my ($ev, $x, $y) = @_; |
661 | |
675 | |
662 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
676 | ($x, $y) = ($ev->{x}, $ev->{y}); |
663 | |
677 | |
664 | $self->{user_w} = $bw + $x - $ox; |
678 | $self->{user_w} = $bw + $x - $ox; |
665 | $self->{user_h} = $bh + $y - $oy; |
679 | $self->{user_h} = $bh + $y - $oy; |
666 | $self->update; |
680 | $self->check_size; |
667 | }; |
681 | }; |
668 | |
682 | |
669 | } elsif ($x >= 0 && $x < $self->{w} |
683 | } elsif ($x >= 0 && $x < $self->{w} |
670 | && $y >= 0 && $y < $self->{border}) { |
684 | && $y >= 0 && $y < $border) { |
671 | |
685 | |
672 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
686 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
673 | my ($bx, $by) = ($self->{x}, $self->{y}); |
687 | my ($bx, $by) = ($self->{x}, $self->{y}); |
674 | |
688 | |
675 | $self->{motion} = sub { |
689 | $self->{motion} = sub { |
676 | my ($ev, $x, $y) = @_; |
690 | my ($ev, $x, $y) = @_; |
677 | |
691 | |
678 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
692 | ($x, $y) = ($ev->{x}, $ev->{y}); |
679 | |
693 | |
680 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
694 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
681 | $self->update; |
695 | $self->update; |
682 | }; |
696 | }; |
683 | } |
697 | } |
… | |
… | |
704 | glEnable GL_BLEND; |
718 | glEnable GL_BLEND; |
705 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
719 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
706 | glEnable GL_TEXTURE_2D; |
720 | glEnable GL_TEXTURE_2D; |
707 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
721 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
708 | |
722 | |
|
|
723 | my $border = $self->border; |
|
|
724 | |
709 | glColor @{ $self->{border_bg} }; |
725 | glColor @{ $self->{border_bg} }; |
710 | $tex[1]->draw_quad (0, 0, $w, $self->{border}); |
726 | $tex[1]->draw_quad (0, 0, $w, $border); |
711 | $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); |
727 | $tex[3]->draw_quad (0, $border, $border, $ch); |
712 | $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); |
728 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
713 | $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); |
729 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
714 | |
730 | |
715 | my $bg = $tex[0]; |
731 | my $bg = $tex[0]; |
716 | |
732 | |
717 | # TODO: repeat texture not scale |
733 | # TODO: repeat texture not scale |
718 | my $rep_x = $cw / $bg->{w}; |
734 | my $rep_x = $cw / $bg->{w}; |
… | |
… | |
721 | glColor @{ $self->{bg} }; |
737 | glColor @{ $self->{bg} }; |
722 | |
738 | |
723 | $bg->{s} = $rep_x; |
739 | $bg->{s} = $rep_x; |
724 | $bg->{t} = $rep_y; |
740 | $bg->{t} = $rep_y; |
725 | $bg->{wrap_mode} = 1; |
741 | $bg->{wrap_mode} = 1; |
726 | $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); |
742 | $bg->draw_quad ($border, $border, $cw, $ch); |
727 | |
743 | |
728 | glDisable GL_TEXTURE_2D; |
744 | glDisable GL_TEXTURE_2D; |
729 | glDisable GL_BLEND; |
745 | glDisable GL_BLEND; |
730 | |
746 | |
731 | $self->child->draw; |
747 | $self->child->draw; |
… | |
… | |
737 | |
753 | |
738 | our @ISA = CFClient::UI::Base::; |
754 | our @ISA = CFClient::UI::Base::; |
739 | |
755 | |
740 | use List::Util qw(max sum); |
756 | use List::Util qw(max sum); |
741 | |
757 | |
742 | use SDL::OpenGL; |
758 | use CFClient::OpenGL; |
743 | |
759 | |
744 | sub new { |
760 | sub new { |
745 | my $class = shift; |
761 | my $class = shift; |
746 | |
762 | |
747 | $class->SUPER::new ( |
763 | $class->SUPER::new ( |
… | |
… | |
754 | my ($self, $x, $y, $child) = @_; |
770 | my ($self, $x, $y, $child) = @_; |
755 | |
771 | |
756 | $child->set_parent ($self); |
772 | $child->set_parent ($self); |
757 | $self->{children}[$y][$x] = $child; |
773 | $self->{children}[$y][$x] = $child; |
758 | |
774 | |
759 | $self->{w} = $self->{h} = -1; |
|
|
760 | |
|
|
761 | $child->check_size; |
775 | $child->check_size; |
762 | } |
776 | } |
763 | |
777 | |
764 | # TODO: move to container class maybe? send childs a signal on removal? |
778 | # TODO: move to container class maybe? send childs a signal on removal? |
765 | sub clear { |
779 | sub clear { |
… | |
… | |
801 | (sum @$hs), |
815 | (sum @$hs), |
802 | ) |
816 | ) |
803 | } |
817 | } |
804 | |
818 | |
805 | sub size_allocate { |
819 | sub size_allocate { |
806 | my ($self, $x, $y, $w, $h) = @_; |
820 | my ($self, $w, $h) = @_; |
807 | |
|
|
808 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
809 | |
821 | |
810 | my ($ws, $hs) = $self->get_wh; |
822 | my ($ws, $hs) = $self->get_wh; |
811 | |
823 | |
812 | my $req_w = sum @$ws; |
824 | my $req_w = sum @$ws; |
813 | my $req_h = sum @$hs; |
825 | my $req_h = sum @$hs; |
… | |
… | |
835 | |
847 | |
836 | for my $c (0 .. $#$row) { |
848 | for my $c (0 .. $#$row) { |
837 | my $col_w = $ws->[$c]; |
849 | my $col_w = $ws->[$c]; |
838 | |
850 | |
839 | if (my $widget = $row->[$c]) { |
851 | if (my $widget = $row->[$c]) { |
840 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
852 | $widget->configure ($x, $y, $col_w, $row_h); |
841 | } |
853 | } |
842 | |
854 | |
843 | $x += $col_w; |
855 | $x += $col_w; |
844 | } |
856 | } |
845 | |
857 | |
… | |
… | |
890 | (List::Util::max map $_->[1], @alloc), |
902 | (List::Util::max map $_->[1], @alloc), |
891 | ) |
903 | ) |
892 | } |
904 | } |
893 | |
905 | |
894 | sub size_allocate { |
906 | sub size_allocate { |
895 | my ($self, $x, $y, $w, $h) = @_; |
907 | my ($self, $w, $h) = @_; |
896 | |
|
|
897 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
898 | |
908 | |
899 | ($h, $w) = ($w, $h); |
909 | ($h, $w) = ($w, $h); |
900 | |
910 | |
901 | my $children = $self->{children}; |
911 | my $children = $self->{children}; |
902 | |
912 | |
… | |
… | |
924 | |
934 | |
925 | my $y = 0; |
935 | my $y = 0; |
926 | for (0 .. $#$children) { |
936 | for (0 .. $#$children) { |
927 | my $child = $children->[$_]; |
937 | my $child = $children->[$_]; |
928 | my $h = $h[$_]; |
938 | my $h = $h[$_]; |
929 | $child->size_allocate ($y, 0, $h, $w); |
939 | $child->configure ($y, 0, $h, $w); |
930 | |
940 | |
931 | $y += $h; |
941 | $y += $h; |
932 | } |
942 | } |
933 | |
943 | |
934 | 1 |
944 | 1 |
… | |
… | |
952 | (List::Util::sum map $_->[1], @alloc), |
962 | (List::Util::sum map $_->[1], @alloc), |
953 | ) |
963 | ) |
954 | } |
964 | } |
955 | |
965 | |
956 | sub size_allocate { |
966 | sub size_allocate { |
957 | my ($self, $x, $y, $w, $h) = @_; |
967 | my ($self, $w, $h) = @_; |
958 | |
|
|
959 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
960 | |
968 | |
961 | my $children = $self->{children}; |
969 | my $children = $self->{children}; |
962 | |
970 | |
963 | my @h = map +($_->size_request)[1], @$children; |
971 | my @h = map +($_->size_request)[1], @$children; |
964 | |
972 | |
… | |
… | |
982 | |
990 | |
983 | my $y = 0; |
991 | my $y = 0; |
984 | for (0 .. $#$children) { |
992 | for (0 .. $#$children) { |
985 | my $child = $children->[$_]; |
993 | my $child = $children->[$_]; |
986 | my $h = $h[$_]; |
994 | my $h = $h[$_]; |
987 | $child->size_allocate (0, $y, $w, $h); |
995 | $child->configure (0, $y, $w, $h); |
988 | |
996 | |
989 | $y += $h; |
997 | $y += $h; |
990 | } |
998 | } |
991 | |
999 | |
992 | 1 |
1000 | 1 |
… | |
… | |
996 | |
1004 | |
997 | package CFClient::UI::Label; |
1005 | package CFClient::UI::Label; |
998 | |
1006 | |
999 | our @ISA = CFClient::UI::Base::; |
1007 | our @ISA = CFClient::UI::Base::; |
1000 | |
1008 | |
1001 | use SDL::OpenGL; |
1009 | use CFClient::OpenGL; |
1002 | |
1010 | |
1003 | sub new { |
1011 | sub new { |
1004 | my ($class, %arg) = @_; |
1012 | my ($class, %arg) = @_; |
1005 | |
1013 | |
1006 | my $self = $class->SUPER::new ( |
1014 | my $self = $class->SUPER::new ( |
1007 | fg => [1, 1, 1], |
1015 | fg => [1, 1, 1], |
1008 | fontsize => $::FONTSIZE, |
1016 | fontsize => 1, |
1009 | text => "", |
1017 | text => "", |
1010 | align => -1, |
1018 | align => -1, |
1011 | valign => -1, |
1019 | valign => -1, |
1012 | padding => 2, |
1020 | padding => 2, |
1013 | layout => new CFClient::Layout, |
1021 | layout => new CFClient::Layout, |
… | |
… | |
1052 | |
1060 | |
1053 | sub size_request { |
1061 | sub size_request { |
1054 | my ($self) = @_; |
1062 | my ($self) = @_; |
1055 | |
1063 | |
1056 | $self->{layout}->set_width; |
1064 | $self->{layout}->set_width; |
1057 | $self->{layout}->set_height ($self->{fontsize}); |
1065 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1058 | |
1066 | |
1059 | my ($w, $h) = $self->{layout}->size; |
1067 | my ($w, $h) = $self->{layout}->size; |
1060 | |
1068 | |
1061 | ( |
1069 | ( |
1062 | $w + $self->{padding} * 2, |
1070 | $w + $self->{padding} * 2, |
1063 | $h + $self->{padding} * 2, |
1071 | $h + $self->{padding} * 2, |
1064 | ) |
1072 | ) |
1065 | } |
1073 | } |
1066 | |
1074 | |
1067 | sub size_allocate { |
1075 | sub size_allocate { |
1068 | my ($self, $x, $y, $w, $h) = @_; |
1076 | my ($self, $w, $h) = @_; |
1069 | |
|
|
1070 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
1071 | |
1077 | |
1072 | delete $self->{texture}; |
1078 | delete $self->{texture}; |
1073 | } |
|
|
1074 | |
|
|
1075 | sub update { |
|
|
1076 | my ($self) = @_; |
|
|
1077 | |
|
|
1078 | delete $self->{texture}; |
|
|
1079 | $self->SUPER::update; |
|
|
1080 | } |
1079 | } |
1081 | |
1080 | |
1082 | sub _draw { |
1081 | sub _draw { |
1083 | my ($self) = @_; |
1082 | my ($self) = @_; |
1084 | |
1083 | |
1085 | my $tex = $self->{texture} ||= do { |
1084 | my $tex = $self->{texture} ||= do { |
1086 | $self->{layout}->set_width ($self->{w}); |
1085 | $self->{layout}->set_width ($self->{w}); |
1087 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); |
1086 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1088 | new_from_layout CFClient::Texture $self->{layout} |
1087 | new_from_layout CFClient::Texture $self->{layout} |
1089 | }; |
1088 | }; |
1090 | |
1089 | |
1091 | glEnable GL_BLEND; |
1090 | glEnable GL_BLEND; |
1092 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1091 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
… | |
… | |
1117 | |
1116 | |
1118 | package CFClient::UI::EntryBase; |
1117 | package CFClient::UI::EntryBase; |
1119 | |
1118 | |
1120 | our @ISA = CFClient::UI::Label::; |
1119 | our @ISA = CFClient::UI::Label::; |
1121 | |
1120 | |
1122 | use SDL; |
1121 | use CFClient::OpenGL; |
1123 | use SDL::OpenGL; |
|
|
1124 | |
1122 | |
1125 | sub new { |
1123 | sub new { |
1126 | my $class = shift; |
1124 | my $class = shift; |
1127 | |
1125 | |
1128 | $class->SUPER::new ( |
1126 | $class->SUPER::new ( |
… | |
… | |
1142 | |
1140 | |
1143 | delete $self->{cur_h}; |
1141 | delete $self->{cur_h}; |
1144 | |
1142 | |
1145 | return if $self->{text} eq $text; |
1143 | return if $self->{text} eq $text; |
1146 | |
1144 | |
|
|
1145 | delete $self->{texture}; |
|
|
1146 | |
1147 | $self->{last_activity} = $::NOW; |
1147 | $self->{last_activity} = $::NOW; |
1148 | $self->{text} = $text; |
1148 | $self->{text} = $text; |
1149 | |
1149 | |
1150 | $text =~ s/./*/g if $self->{hidden}; |
1150 | $text =~ s/./*/g if $self->{hidden}; |
1151 | $self->{layout}->set_text ("$text "); |
1151 | $self->{layout}->set_text ("$text "); |
… | |
… | |
1164 | |
1164 | |
1165 | ($w + 1, $h) # add 1 for cursor |
1165 | ($w + 1, $h) # add 1 for cursor |
1166 | } |
1166 | } |
1167 | |
1167 | |
1168 | sub size_allocate { |
1168 | sub size_allocate { |
1169 | my ($self, $x, $y, $w, $h) = @_; |
1169 | my ($self, $w, $h) = @_; |
1170 | |
|
|
1171 | $self->SUPER::size_allocate ($x, $y, $w, $h) or return; |
|
|
1172 | |
1170 | |
1173 | $self->_set_text ($self->{text}); |
1171 | $self->_set_text ($self->{text}); |
1174 | } |
1172 | } |
1175 | |
1173 | |
1176 | sub set_text { |
1174 | sub set_text { |
… | |
… | |
1182 | } |
1180 | } |
1183 | |
1181 | |
1184 | sub key_down { |
1182 | sub key_down { |
1185 | my ($self, $ev) = @_; |
1183 | my ($self, $ev) = @_; |
1186 | |
1184 | |
1187 | my $mod = $ev->key_mod; |
1185 | my $mod = $ev->{mod}; |
1188 | my $sym = $ev->key_sym; |
1186 | my $sym = $ev->{sym}; |
1189 | |
|
|
1190 | my $uni = $ev->key_unicode; |
1187 | my $uni = $ev->{unicode}; |
1191 | |
1188 | |
1192 | my $text = $self->get_text; |
1189 | my $text = $self->get_text; |
1193 | |
1190 | |
1194 | if ($sym == SDLK_BACKSPACE) { |
1191 | if ($sym == 8) { |
1195 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1192 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1196 | } elsif ($sym == SDLK_DELETE) { |
1193 | } elsif ($sym == 127) { |
1197 | substr $text, $self->{cursor}, 1, ""; |
1194 | substr $text, $self->{cursor}, 1, ""; |
1198 | } elsif ($sym == SDLK_LEFT) { |
1195 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1199 | --$self->{cursor} if $self->{cursor}; |
1196 | --$self->{cursor} if $self->{cursor}; |
1200 | } elsif ($sym == SDLK_RIGHT) { |
1197 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1201 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1198 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1202 | } elsif ($sym == SDLK_HOME) { |
1199 | } elsif ($sym == CFClient::SDLK_HOME) { |
1203 | $self->{cursor} = 0; |
1200 | $self->{cursor} = 0; |
1204 | } elsif ($sym == SDLK_END) { |
1201 | } elsif ($sym == CFClient::SDLK_END) { |
1205 | $self->{cursor} = length $text; |
1202 | $self->{cursor} = length $text; |
1206 | } elsif ($sym == SDLK_ESCAPE) { |
1203 | } elsif ($sym == 27) { |
1207 | $self->emit ('escape'); |
1204 | $self->emit ('escape'); |
1208 | } elsif ($uni) { |
1205 | } elsif ($uni) { |
1209 | substr $text, $self->{cursor}++, 0, chr $uni; |
1206 | substr $text, $self->{cursor}++, 0, chr $uni; |
1210 | } |
1207 | } |
1211 | |
1208 | |
… | |
… | |
1286 | |
1283 | |
1287 | package CFClient::UI::Entry; |
1284 | package CFClient::UI::Entry; |
1288 | |
1285 | |
1289 | our @ISA = CFClient::UI::EntryBase::; |
1286 | our @ISA = CFClient::UI::EntryBase::; |
1290 | |
1287 | |
1291 | use SDL; |
1288 | use CFClient::OpenGL; |
1292 | use SDL::OpenGL; |
|
|
1293 | |
1289 | |
1294 | sub key_down { |
1290 | sub key_down { |
1295 | my ($self, $ev) = @_; |
1291 | my ($self, $ev) = @_; |
1296 | |
1292 | |
1297 | my $sym = $ev->key_sym; |
1293 | my $sym = $ev->{sym}; |
1298 | |
1294 | |
1299 | if ($sym == SDLK_RETURN) { |
1295 | if ($sym == 13) { |
1300 | $self->emit (activate => $self->get_text); |
1296 | $self->emit (activate => $self->get_text); |
1301 | $self->update; |
1297 | $self->update; |
1302 | |
1298 | |
1303 | } else { |
1299 | } else { |
1304 | $self->SUPER::key_down ($ev); |
1300 | $self->SUPER::key_down ($ev); |
… | |
… | |
1310 | |
1306 | |
1311 | package CFClient::UI::Button; |
1307 | package CFClient::UI::Button; |
1312 | |
1308 | |
1313 | our @ISA = CFClient::UI::Label::; |
1309 | our @ISA = CFClient::UI::Label::; |
1314 | |
1310 | |
1315 | use SDL; |
1311 | use CFClient::OpenGL; |
1316 | use SDL::OpenGL; |
|
|
1317 | |
1312 | |
1318 | my @tex = |
1313 | my @tex = |
1319 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1314 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1320 | qw(b1_button_active.png); |
1315 | qw(b1_button_active.png); |
1321 | |
1316 | |
… | |
… | |
1374 | |
1369 | |
1375 | my @tex = |
1370 | my @tex = |
1376 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1371 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1377 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1372 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1378 | |
1373 | |
1379 | use SDL; |
1374 | use CFClient::OpenGL; |
1380 | use SDL::OpenGL; |
|
|
1381 | |
1375 | |
1382 | sub new { |
1376 | sub new { |
1383 | my $class = shift; |
1377 | my $class = shift; |
1384 | |
1378 | |
1385 | $class->SUPER::new ( |
1379 | $class->SUPER::new ( |
… | |
… | |
1396 | my ($self) = @_; |
1390 | my ($self) = @_; |
1397 | |
1391 | |
1398 | ($self->{padding} * 2 + 6) x 2 |
1392 | ($self->{padding} * 2 + 6) x 2 |
1399 | } |
1393 | } |
1400 | |
1394 | |
1401 | sub size_allocate { |
|
|
1402 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1403 | |
|
|
1404 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
1405 | } |
|
|
1406 | |
|
|
1407 | sub button_down { |
1395 | sub button_down { |
1408 | my ($self, $ev, $x, $y) = @_; |
1396 | my ($self, $ev, $x, $y) = @_; |
1409 | |
1397 | |
1410 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1398 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1411 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1399 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
… | |
… | |
1441 | |
1429 | |
1442 | package CFClient::UI::VGauge; |
1430 | package CFClient::UI::VGauge; |
1443 | |
1431 | |
1444 | our @ISA = CFClient::UI::Base::; |
1432 | our @ISA = CFClient::UI::Base::; |
1445 | |
1433 | |
1446 | use SDL::OpenGL; |
1434 | use CFClient::OpenGL; |
1447 | |
1435 | |
1448 | my %tex = ( |
1436 | my %tex = ( |
1449 | food => [ |
1437 | food => [ |
1450 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1438 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1451 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
1439 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
… | |
… | |
1466 | |
1454 | |
1467 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
1455 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
1468 | sub new { |
1456 | sub new { |
1469 | my $class = shift; |
1457 | my $class = shift; |
1470 | |
1458 | |
1471 | my $self = $class->SUPER::new (gauge => 'food', @_); |
1459 | my $self = $class->SUPER::new ( |
|
|
1460 | gauge => 'food', |
|
|
1461 | @_ |
|
|
1462 | ); |
|
|
1463 | |
|
|
1464 | $self->{aspect} = $tex{$self->{gauge}}[0]{w} / $tex{$self->{gauge}}[0]{h}; |
1472 | |
1465 | |
1473 | $self |
1466 | $self |
1474 | } |
1467 | } |
1475 | |
1468 | |
1476 | sub size_request { |
1469 | sub size_request { |
… | |
… | |
1520 | my $h1 = $self->{h} - $ycut * $self->{h}; |
1513 | my $h1 = $self->{h} - $ycut * $self->{h}; |
1521 | my $h2 = $ycut * $self->{h}; |
1514 | my $h2 = $ycut * $self->{h}; |
1522 | |
1515 | |
1523 | my $yp = 0; |
1516 | my $yp = 0; |
1524 | |
1517 | |
1525 | glBindTexture (GL_TEXTURE_2D, $t1->{name}); |
1518 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
1526 | glBegin (GL_QUADS); |
1519 | glBegin GL_QUADS; |
1527 | glTexCoord (0, 0); glVertex (0 , $yp); |
1520 | glTexCoord 0 , 0; glVertex 0 , $yp; |
1528 | glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1); |
1521 | glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; |
1529 | glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1); |
1522 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; |
1530 | glTexCoord (1, 0); glVertex (0 + $w, $yp); |
1523 | glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; |
1531 | glEnd (); |
1524 | glEnd; |
1532 | |
1525 | |
1533 | $yp += $h1; |
1526 | $yp += $h1; |
1534 | |
1527 | |
1535 | glBindTexture (GL_TEXTURE_2D, $t2->{name}); |
1528 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
1536 | glBegin (GL_QUADS); |
1529 | glBegin GL_QUADS; |
1537 | glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp); |
1530 | glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; |
1538 | glTexCoord (0, 1); glVertex (0 , $yp + $h2); |
1531 | glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; |
1539 | glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2); |
1532 | glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2; |
1540 | glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp); |
1533 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; |
1541 | glEnd (); |
1534 | glEnd; |
1542 | |
1535 | |
1543 | glDisable GL_BLEND; |
1536 | glDisable GL_BLEND; |
1544 | glDisable GL_TEXTURE_2D; |
1537 | glDisable GL_TEXTURE_2D; |
1545 | } |
1538 | } |
1546 | |
1539 | |
… | |
… | |
1548 | |
1541 | |
1549 | package CFClient::UI::Slider; |
1542 | package CFClient::UI::Slider; |
1550 | |
1543 | |
1551 | use strict; |
1544 | use strict; |
1552 | |
1545 | |
1553 | use SDL::OpenGL; |
1546 | use CFClient::OpenGL; |
1554 | |
1547 | |
1555 | our @ISA = CFClient::UI::DrawBG::; |
1548 | our @ISA = CFClient::UI::DrawBG::; |
1556 | |
1549 | |
1557 | my @tex = |
1550 | my @tex = |
1558 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1551 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
… | |
… | |
1568 | # TODO: calculations are off |
1561 | # TODO: calculations are off |
1569 | my $self = $class->SUPER::new ( |
1562 | my $self = $class->SUPER::new ( |
1570 | fg => [1, 1, 1], |
1563 | fg => [1, 1, 1], |
1571 | active_fg => [0, 0, 0], |
1564 | active_fg => [0, 0, 0], |
1572 | range => [0, 0, 100, 10], |
1565 | range => [0, 0, 100, 10], |
1573 | req_w => 40, |
1566 | req_w => 20, |
1574 | req_h => 13, |
1567 | req_h => 20, |
1575 | vertical => 0, |
1568 | vertical => 0, |
1576 | can_hover => 1, |
1569 | can_hover => 1, |
1577 | inner_pad => 5, |
1570 | inner_pad => 5, |
1578 | @_ |
1571 | @_ |
1579 | ); |
1572 | ); |
… | |
… | |
1683 | |
1676 | |
1684 | package CFClient::UI::TextView; |
1677 | package CFClient::UI::TextView; |
1685 | |
1678 | |
1686 | our @ISA = CFClient::UI::HBox::; |
1679 | our @ISA = CFClient::UI::HBox::; |
1687 | |
1680 | |
1688 | use SDL::OpenGL; |
1681 | use CFClient::OpenGL; |
1689 | |
1682 | |
1690 | sub new { |
1683 | sub new { |
1691 | my $class = shift; |
1684 | my $class = shift; |
1692 | |
1685 | |
1693 | my $self = $class->SUPER::new ( |
1686 | my $self = $class->SUPER::new ( |
1694 | req_w => $::WIDTH / 6, |
|
|
1695 | req_h => $::HEIGHT / 6, |
|
|
1696 | fontsize => $::FONTSIZE, |
1687 | fontsize => 1, |
1697 | @_, |
1688 | @_, |
1698 | |
1689 | |
1699 | layout => (new CFClient::Layout), |
1690 | layout => (new CFClient::Layout), |
1700 | par => [], |
1691 | par => [], |
1701 | height => 0, |
1692 | height => 0, |
… | |
… | |
1722 | sub text_height { |
1713 | sub text_height { |
1723 | my ($self, $text) = @_; |
1714 | my ($self, $text) = @_; |
1724 | |
1715 | |
1725 | my $layout = $self->{layout}; |
1716 | my $layout = $self->{layout}; |
1726 | |
1717 | |
1727 | $layout->set_height ($self->{fontsize}); |
1718 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
1728 | $layout->set_width ($self->{w}); |
1719 | $layout->set_width ($self->{w}); |
1729 | $layout->set_text ($text); |
1720 | $layout->set_text ($text); |
1730 | |
1721 | |
1731 | ($layout->size)[1] |
1722 | ($layout->size)[1] |
1732 | } |
1723 | } |
… | |
… | |
1736 | |
1727 | |
1737 | $self->{need_reflow}++; |
1728 | $self->{need_reflow}++; |
1738 | $self->update; |
1729 | $self->update; |
1739 | } |
1730 | } |
1740 | |
1731 | |
1741 | sub size_request { |
|
|
1742 | my ($self) = @_; |
|
|
1743 | |
|
|
1744 | ($self->{req_w}, $self->{req_h}) |
|
|
1745 | } |
|
|
1746 | |
|
|
1747 | sub size_allocate { |
1732 | sub size_allocate { |
1748 | my ($self, $x, $y, $w, $h) = @_; |
1733 | my ($self, $w, $h) = @_; |
1749 | |
1734 | |
1750 | $self->SUPER::size_allocate ($x, $y, $w, $h) or return; |
1735 | $self->SUPER::size_allocate ($w, $h); |
1751 | |
1736 | |
1752 | $self->{layout}->set_height ($self->{fontsize}); |
1737 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1753 | $self->{layout}->set_width ($self->{w}); |
1738 | $self->{layout}->set_width ($self->{children}[0]{w}); |
1754 | |
1739 | |
1755 | $self->reflow; |
1740 | $self->reflow; |
1756 | $self->update; |
|
|
1757 | } |
1741 | } |
1758 | |
1742 | |
1759 | sub add_paragraph { |
1743 | sub add_paragraph { |
1760 | my ($self, $color, $text) = @_; |
1744 | my ($self, $color, $text) = @_; |
1761 | |
1745 | |
… | |
… | |
1846 | |
1830 | |
1847 | } |
1831 | } |
1848 | |
1832 | |
1849 | ############################################################################# |
1833 | ############################################################################# |
1850 | |
1834 | |
1851 | package CFClient::UI::MapWidget; |
|
|
1852 | |
|
|
1853 | use strict; |
|
|
1854 | |
|
|
1855 | use List::Util qw(min max); |
|
|
1856 | |
|
|
1857 | use SDL; |
|
|
1858 | use SDL::OpenGL; |
|
|
1859 | |
|
|
1860 | our @ISA = CFClient::UI::Base::; |
|
|
1861 | |
|
|
1862 | sub new { |
|
|
1863 | my $class = shift; |
|
|
1864 | |
|
|
1865 | $class->SUPER::new ( |
|
|
1866 | z => -1, |
|
|
1867 | can_focus => 1, |
|
|
1868 | list => (glGenLists 1), |
|
|
1869 | @_ |
|
|
1870 | ) |
|
|
1871 | } |
|
|
1872 | |
|
|
1873 | sub key_down { |
|
|
1874 | print "MAPKEYDOWN\n"; |
|
|
1875 | } |
|
|
1876 | |
|
|
1877 | sub key_up { |
|
|
1878 | } |
|
|
1879 | |
|
|
1880 | sub button_down { |
|
|
1881 | my ($self, $ev, $x, $y) = @_; |
|
|
1882 | |
|
|
1883 | $self->focus_in; |
|
|
1884 | |
|
|
1885 | if ($ev->button == 2) { |
|
|
1886 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
1887 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
|
|
1888 | |
|
|
1889 | $self->{motion} = sub { |
|
|
1890 | my ($ev, $x, $y) = @_; |
|
|
1891 | |
|
|
1892 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
1893 | |
|
|
1894 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
|
|
1895 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
|
|
1896 | |
|
|
1897 | $self->update; |
|
|
1898 | }; |
|
|
1899 | } |
|
|
1900 | } |
|
|
1901 | |
|
|
1902 | sub button_up { |
|
|
1903 | my ($self, $ev, $x, $y) = @_; |
|
|
1904 | |
|
|
1905 | delete $self->{motion}; |
|
|
1906 | } |
|
|
1907 | |
|
|
1908 | sub mouse_motion { |
|
|
1909 | my ($self, $ev, $x, $y) = @_; |
|
|
1910 | |
|
|
1911 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
|
|
1912 | } |
|
|
1913 | |
|
|
1914 | sub size_request { |
|
|
1915 | ( |
|
|
1916 | 1 + 32 * int $::WIDTH / 32, |
|
|
1917 | 1 + 32 * int $::HEIGHT / 32, |
|
|
1918 | ) |
|
|
1919 | } |
|
|
1920 | |
|
|
1921 | sub update { |
|
|
1922 | my ($self) = @_; |
|
|
1923 | |
|
|
1924 | $self->{need_update} = 1; |
|
|
1925 | $self->SUPER::update; |
|
|
1926 | } |
|
|
1927 | |
|
|
1928 | sub draw { |
|
|
1929 | my ($self) = @_; |
|
|
1930 | |
|
|
1931 | if (delete $self->{need_update}) { |
|
|
1932 | glNewList $self->{list}, GL_COMPILE; |
|
|
1933 | |
|
|
1934 | if ($::MAP) { |
|
|
1935 | my $sw = int $::WIDTH / 32; |
|
|
1936 | my $sh = int $::HEIGHT / 32; |
|
|
1937 | |
|
|
1938 | my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
|
|
1939 | my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
|
|
1940 | |
|
|
1941 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
|
|
1942 | |
|
|
1943 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
|
|
1944 | |
|
|
1945 | if ($::CFG->{fow_enable}) { |
|
|
1946 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
|
|
1947 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
|
|
1948 | glConvolutionFilter2D |
|
|
1949 | GL_CONVOLUTION_2D, |
|
|
1950 | GL_ALPHA, |
|
|
1951 | 3, 3, |
|
|
1952 | GL_ALPHA, GL_FLOAT, |
|
|
1953 | pack "f*", |
|
|
1954 | 0.1, 0.1, 0.1, |
|
|
1955 | 0.1, 0.2, 0.1, |
|
|
1956 | 0.1, 0.1, 0.1, |
|
|
1957 | ; |
|
|
1958 | glEnable GL_CONVOLUTION_2D; |
|
|
1959 | } |
|
|
1960 | |
|
|
1961 | $self->{fow_texture} = new CFClient::Texture |
|
|
1962 | w => $w, |
|
|
1963 | h => $h, |
|
|
1964 | data => $data, |
|
|
1965 | internalformat => GL_ALPHA, |
|
|
1966 | format => GL_ALPHA; |
|
|
1967 | |
|
|
1968 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
|
|
1969 | |
|
|
1970 | glEnable GL_BLEND; |
|
|
1971 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1972 | glEnable GL_TEXTURE_2D; |
|
|
1973 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1974 | |
|
|
1975 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1976 | $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); |
|
|
1977 | |
|
|
1978 | glDisable GL_TEXTURE_2D; |
|
|
1979 | glDisable GL_BLEND; |
|
|
1980 | } |
|
|
1981 | |
|
|
1982 | # HACK BEGIN |
|
|
1983 | { |
|
|
1984 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
1985 | my ($w, $h) = (250, 250); |
|
|
1986 | |
|
|
1987 | glEnable GL_BLEND; |
|
|
1988 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1989 | glEnable GL_TEXTURE_2D; |
|
|
1990 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1991 | |
|
|
1992 | $self->{mapmap_texture} = |
|
|
1993 | new CFClient::Texture |
|
|
1994 | w => $w, |
|
|
1995 | h => $h, |
|
|
1996 | data => $::MAP->mapmap ($w, $h), |
|
|
1997 | type => GL_UNSIGNED_INT_8_8_8_8_REV; |
|
|
1998 | |
|
|
1999 | $self->{mapmap_texture}->draw_quad (100, 100); |
|
|
2000 | |
|
|
2001 | glDisable GL_TEXTURE_2D; |
|
|
2002 | glDisable GL_BLEND; |
|
|
2003 | } |
|
|
2004 | # HACK END |
|
|
2005 | } |
|
|
2006 | |
|
|
2007 | glEndList; |
|
|
2008 | } |
|
|
2009 | |
|
|
2010 | glPushMatrix; |
|
|
2011 | glCallList $self->{list}; |
|
|
2012 | glPopMatrix; |
|
|
2013 | |
|
|
2014 | if ($FOCUS != $self) { |
|
|
2015 | glColor 64/255, 64/255, 64/255; |
|
|
2016 | glLogicOp GL_AND; |
|
|
2017 | glEnable GL_COLOR_LOGIC_OP; |
|
|
2018 | glBegin GL_QUADS; |
|
|
2019 | glVertex 0, 0; |
|
|
2020 | glVertex 0, $::HEIGHT; |
|
|
2021 | glVertex $::WIDTH, $::HEIGHT; |
|
|
2022 | glVertex $::WIDTH, 0; |
|
|
2023 | glEnd; |
|
|
2024 | glDisable GL_COLOR_LOGIC_OP; |
|
|
2025 | } |
|
|
2026 | } |
|
|
2027 | |
|
|
2028 | my %DIR = ( |
|
|
2029 | SDLK_KP8, [1, "north"], |
|
|
2030 | SDLK_KP9, [2, "northeast"], |
|
|
2031 | SDLK_KP6, [3, "east"], |
|
|
2032 | SDLK_KP3, [4, "southeast"], |
|
|
2033 | SDLK_KP2, [5, "south"], |
|
|
2034 | SDLK_KP1, [6, "southwest"], |
|
|
2035 | SDLK_KP4, [7, "west"], |
|
|
2036 | SDLK_KP7, [8, "northwest"], |
|
|
2037 | |
|
|
2038 | SDLK_UP, [1, "north"], |
|
|
2039 | SDLK_RIGHT, [3, "east"], |
|
|
2040 | SDLK_DOWN, [5, "south"], |
|
|
2041 | SDLK_LEFT, [7, "west"], |
|
|
2042 | ); |
|
|
2043 | |
|
|
2044 | sub key_down { |
|
|
2045 | my ($self, $ev) = @_; |
|
|
2046 | |
|
|
2047 | my $mod = $ev->key_mod; |
|
|
2048 | my $sym = $ev->key_sym; |
|
|
2049 | |
|
|
2050 | if ($sym == SDLK_KP5) { |
|
|
2051 | $::CONN->user_send ("stay fire"); |
|
|
2052 | } elsif ($sym == SDLK_a) { |
|
|
2053 | $::CONN->user_send ("apply"); |
|
|
2054 | } elsif ($sym == SDLK_QUOTE) { |
|
|
2055 | $self->emit ('activate_console'); |
|
|
2056 | } elsif ($sym == SDLK_SLASH) { |
|
|
2057 | $self->emit ('activate_console' => '/'); |
|
|
2058 | } elsif (exists $DIR{$sym}) { |
|
|
2059 | if ($mod & KMOD_SHIFT) { |
|
|
2060 | $self->{shft}++; |
|
|
2061 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
|
|
2062 | } elsif ($mod & KMOD_CTRL) { |
|
|
2063 | $self->{ctrl}++; |
|
|
2064 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
|
|
2065 | } else { |
|
|
2066 | $::CONN->user_send ("$DIR{$sym}[1]"); |
|
|
2067 | } |
|
|
2068 | } |
|
|
2069 | } |
|
|
2070 | |
|
|
2071 | sub key_up { |
|
|
2072 | my ($self, $ev) = @_; |
|
|
2073 | |
|
|
2074 | my $mod = $ev->key_mod; |
|
|
2075 | my $sym = $ev->key_sym; |
|
|
2076 | |
|
|
2077 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
|
|
2078 | $::CONN->user_send ("fire_stop"); |
|
|
2079 | } |
|
|
2080 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
|
|
2081 | $::CONN->user_send ("run_stop"); |
|
|
2082 | } |
|
|
2083 | } |
|
|
2084 | |
|
|
2085 | ############################################################################# |
|
|
2086 | |
|
|
2087 | package CFClient::UI::Animator; |
1835 | package CFClient::UI::Animator; |
2088 | |
1836 | |
2089 | use SDL::OpenGL; |
1837 | use CFClient::OpenGL; |
2090 | |
1838 | |
2091 | our @ISA = CFClient::UI::Bin::; |
1839 | our @ISA = CFClient::UI::Bin::; |
2092 | |
1840 | |
2093 | sub moveto { |
1841 | sub moveto { |
2094 | my ($self, $x, $y) = @_; |
1842 | my ($self, $x, $y) = @_; |
… | |
… | |
2167 | |
1915 | |
2168 | package CFClient::UI::Root; |
1916 | package CFClient::UI::Root; |
2169 | |
1917 | |
2170 | our @ISA = CFClient::UI::Container::; |
1918 | our @ISA = CFClient::UI::Container::; |
2171 | |
1919 | |
2172 | use SDL::OpenGL; |
1920 | use CFClient::OpenGL; |
2173 | |
1921 | |
2174 | sub check_size { |
1922 | sub check_size { |
2175 | my ($self) = @_; |
1923 | my ($self) = @_; |
2176 | |
1924 | |
2177 | $self->size_allocate (0, 0, $::WITH, $::HEIGHT); |
1925 | $self->configure (0, 0, $::WITH, $::HEIGHT); |
2178 | } |
1926 | } |
2179 | |
1927 | |
2180 | sub size_request { |
1928 | sub size_request { |
2181 | ($::WIDTH, $::HEIGHT) |
1929 | ($::WIDTH, $::HEIGHT) |
2182 | } |
1930 | } |
2183 | |
1931 | |
2184 | sub size_allocate { |
1932 | sub configure { |
2185 | my ($self, $x, $y, $w, $h) = @_; |
1933 | my ($self, $x, $y, $w, $h) = @_; |
2186 | |
1934 | |
2187 | $self->_size_allocate ($x, $y, $w, $h); |
1935 | $self->SUPER::configure ($x, $y, $w, $h); |
2188 | |
1936 | |
2189 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
1937 | $_->configure ($_->{x}, $_->{y}, $_->size_request) |
2190 | for @{$self->{children}}; |
1938 | for @{$self->{children}}; |
2191 | } |
1939 | } |
2192 | |
1940 | |
2193 | sub _topleft { |
1941 | sub _topleft { |
2194 | my ($self, $x, $y) = @_; |
1942 | my ($self, $x, $y) = @_; |
… | |
… | |
2202 | $self->check_size; |
1950 | $self->check_size; |
2203 | ::refresh (); |
1951 | ::refresh (); |
2204 | } |
1952 | } |
2205 | |
1953 | |
2206 | sub add { |
1954 | sub add { |
2207 | my ($self, $widget) = @_; |
1955 | my ($self, $child) = @_; |
2208 | |
1956 | |
2209 | $self->SUPER::add ($widget); |
1957 | $self->SUPER::add ($child); |
2210 | |
|
|
2211 | $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request); |
|
|
2212 | } |
1958 | } |
2213 | |
1959 | |
2214 | sub on_refresh { |
1960 | sub on_refresh { |
2215 | my ($self, $id, $cb) = @_; |
1961 | my ($self, $id, $cb) = @_; |
2216 | |
1962 | |