… | |
… | |
3 | use utf8; |
3 | use utf8; |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use Scalar::Util (); |
6 | use Scalar::Util (); |
7 | use List::Util (); |
7 | use List::Util (); |
|
|
8 | use Event; |
8 | |
9 | |
9 | use CFClient; |
10 | use CFClient; |
10 | use CFClient::Texture; |
11 | use CFClient::Texture; |
11 | |
12 | |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
13 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
… | |
… | |
15 | our $ROOT; |
16 | our $ROOT; |
16 | our $TOOLTIP; |
17 | our $TOOLTIP; |
17 | our $BUTTON_STATE; |
18 | our $BUTTON_STATE; |
18 | |
19 | |
19 | our %WIDGET; # all widgets, weak-referenced |
20 | our %WIDGET; # all widgets, weak-referenced |
|
|
21 | |
|
|
22 | our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { |
|
|
23 | if (!$GRAB) { |
|
|
24 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
|
|
25 | if (length $widget->{tooltip}) { |
|
|
26 | if ($TOOLTIP->{owner} != $widget) { |
|
|
27 | $TOOLTIP->hide; |
|
|
28 | |
|
|
29 | $TOOLTIP->{owner} = $widget; |
|
|
30 | |
|
|
31 | return if $ENV{CFPLUS_DEBUG} & 8; |
|
|
32 | |
|
|
33 | my $tip = $widget->{tooltip}; |
|
|
34 | |
|
|
35 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
36 | |
|
|
37 | $TOOLTIP->set_tooltip_from ($widget); |
|
|
38 | $TOOLTIP->show; |
|
|
39 | } |
|
|
40 | |
|
|
41 | return; |
|
|
42 | } |
|
|
43 | } |
|
|
44 | } |
|
|
45 | |
|
|
46 | $TOOLTIP->hide; |
|
|
47 | delete $TOOLTIP->{owner}; |
|
|
48 | }); |
20 | |
49 | |
21 | sub get_layout { |
50 | sub get_layout { |
22 | my $layout; |
51 | my $layout; |
23 | |
52 | |
24 | for (grep { $_->{name} } values %WIDGET) { |
53 | for (grep { $_->{name} } values %WIDGET) { |
… | |
… | |
39 | my ($layout) = @_; |
68 | my ($layout) = @_; |
40 | |
69 | |
41 | $LAYOUT = $layout; |
70 | $LAYOUT = $layout; |
42 | } |
71 | } |
43 | |
72 | |
44 | sub check_tooltip { |
|
|
45 | return if $ENV{CFPLUS_DEBUG} & 8; |
|
|
46 | |
|
|
47 | if (!$GRAB) { |
|
|
48 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
|
|
49 | if (length $widget->{tooltip}) { |
|
|
50 | if ($TOOLTIP->{owner} != $widget) { |
|
|
51 | $TOOLTIP->hide; |
|
|
52 | |
|
|
53 | $TOOLTIP->{owner} = $widget; |
|
|
54 | |
|
|
55 | my $tip = $widget->{tooltip}; |
|
|
56 | |
|
|
57 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
58 | |
|
|
59 | $TOOLTIP->set_tooltip_from ($widget); |
|
|
60 | $TOOLTIP->show; |
|
|
61 | } |
|
|
62 | |
|
|
63 | return; |
|
|
64 | } |
|
|
65 | } |
|
|
66 | } |
|
|
67 | |
|
|
68 | $TOOLTIP->hide; |
|
|
69 | delete $TOOLTIP->{owner}; |
|
|
70 | } |
|
|
71 | |
|
|
72 | # class methods for events |
73 | # class methods for events |
73 | sub feed_sdl_key_down_event { |
74 | sub feed_sdl_key_down_event { |
74 | $FOCUS->emit (key_down => $_[0]) |
75 | $FOCUS->emit (key_down => $_[0]) |
75 | if $FOCUS; |
76 | if $FOCUS; |
76 | } |
77 | } |
… | |
… | |
88 | my $widget = $ROOT->find_widget ($x, $y); |
89 | my $widget = $ROOT->find_widget ($x, $y); |
89 | |
90 | |
90 | $GRAB = $widget; |
91 | $GRAB = $widget; |
91 | $GRAB->update if $GRAB; |
92 | $GRAB->update if $GRAB; |
92 | |
93 | |
93 | check_tooltip; |
94 | $TOOLTIP_WATCHER->cb->(); |
94 | } |
95 | } |
95 | |
96 | |
96 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
97 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
97 | |
98 | |
98 | $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y)) |
99 | $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y)) |
… | |
… | |
113 | if (!$BUTTON_STATE) { |
114 | if (!$BUTTON_STATE) { |
114 | my $grab = $GRAB; undef $GRAB; |
115 | my $grab = $GRAB; undef $GRAB; |
115 | $grab->update if $grab; |
116 | $grab->update if $grab; |
116 | $GRAB->update if $GRAB; |
117 | $GRAB->update if $GRAB; |
117 | |
118 | |
118 | check_tooltip; |
119 | $TOOLTIP_WATCHER->cb->(); |
119 | } |
120 | } |
120 | } |
121 | } |
121 | |
122 | |
122 | sub feed_sdl_motion_event { |
123 | sub feed_sdl_motion_event { |
123 | my ($ev) = @_; |
124 | my ($ev) = @_; |
… | |
… | |
129 | my $hover = $HOVER; $HOVER = $widget; |
130 | my $hover = $HOVER; $HOVER = $widget; |
130 | |
131 | |
131 | $hover->update if $hover && $hover->{can_hover}; |
132 | $hover->update if $hover && $hover->{can_hover}; |
132 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
133 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
133 | |
134 | |
134 | check_tooltip; |
135 | $TOOLTIP_WATCHER->start; |
135 | } |
136 | } |
136 | |
137 | |
137 | $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y)) |
138 | $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y)) |
138 | if $HOVER; |
139 | if $HOVER; |
139 | } |
140 | } |
… | |
… | |
277 | delete $self->{visible}; |
278 | delete $self->{visible}; |
278 | |
279 | |
279 | undef $GRAB if $GRAB == $self; |
280 | undef $GRAB if $GRAB == $self; |
280 | undef $HOVER if $HOVER == $self; |
281 | undef $HOVER if $HOVER == $self; |
281 | |
282 | |
282 | CFClient::UI::check_tooltip |
283 | $CFClient::UI::TOOLTIP_WATCHER->cb->() |
283 | if $TOOLTIP->{owner} == $self; |
284 | if $TOOLTIP->{owner} == $self; |
284 | |
285 | |
285 | $self->focus_out; |
286 | $self->focus_out; |
286 | |
287 | |
287 | $self->emit (visibility_change => 0); |
288 | $self->emit (visibility_change => 0); |
… | |
… | |
371 | sub size_allocate { |
372 | sub size_allocate { |
372 | # nothing to be done |
373 | # nothing to be done |
373 | } |
374 | } |
374 | |
375 | |
375 | sub children { |
376 | sub children { |
|
|
377 | # nop |
|
|
378 | } |
|
|
379 | |
|
|
380 | sub visible_children { |
|
|
381 | $_[0]->children |
376 | } |
382 | } |
377 | |
383 | |
378 | sub set_max_size { |
384 | sub set_max_size { |
379 | my ($self, $w, $h) = @_; |
385 | my ($self, $w, $h) = @_; |
380 | |
386 | |
381 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
387 | $self->{max_w} = int $w if defined $w; |
382 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
388 | $self->{max_h} = int $h if defined $h; |
|
|
389 | |
|
|
390 | $self->realloc; |
383 | } |
391 | } |
384 | |
392 | |
385 | sub set_tooltip { |
393 | sub set_tooltip { |
386 | my ($self, $tooltip) = @_; |
394 | my ($self, $tooltip) = @_; |
387 | |
395 | |
… | |
… | |
392 | |
400 | |
393 | $self->{tooltip} = $tooltip; |
401 | $self->{tooltip} = $tooltip; |
394 | |
402 | |
395 | if ($CFClient::UI::TOOLTIP->{owner} == $self) { |
403 | if ($CFClient::UI::TOOLTIP->{owner} == $self) { |
396 | delete $CFClient::UI::TOOLTIP->{owner}; |
404 | delete $CFClient::UI::TOOLTIP->{owner}; |
397 | CFClient::UI::check_tooltip; |
405 | $CFClient::UI::TOOLTIP_WATCHER->cb->(); |
398 | } |
406 | } |
399 | } |
407 | } |
400 | |
408 | |
401 | # translate global coordinates to local coordinate system |
409 | # translate global coordinates to local coordinate system |
402 | sub coord2local { |
410 | sub coord2local { |
… | |
… | |
439 | |
447 | |
440 | $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus |
448 | $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus |
441 | unless $FOCUS; |
449 | unless $FOCUS; |
442 | } |
450 | } |
443 | |
451 | |
444 | sub mouse_motion { } |
452 | sub mouse_motion { 0 } |
445 | sub button_up { } |
453 | sub button_up { 0 } |
446 | sub key_down { } |
454 | sub key_down { 0 } |
447 | sub key_up { } |
455 | sub key_up { 0 } |
448 | |
456 | |
449 | sub button_down { |
457 | sub button_down { |
450 | my ($self, $ev, $x, $y) = @_; |
458 | my ($self, $ev, $x, $y) = @_; |
451 | |
459 | |
452 | $self->focus_in; |
460 | $self->focus_in; |
|
|
461 | |
|
|
462 | 0 |
453 | } |
463 | } |
454 | |
464 | |
455 | sub find_widget { |
465 | sub find_widget { |
456 | my ($self, $x, $y) = @_; |
466 | my ($self, $x, $y) = @_; |
457 | |
467 | |
… | |
… | |
516 | } |
526 | } |
517 | |
527 | |
518 | sub reconfigure { |
528 | sub reconfigure { |
519 | my ($self) = @_; |
529 | my ($self) = @_; |
520 | |
530 | |
|
|
531 | # some widgets cache req_w and req_h |
|
|
532 | delete $self->{req_w}; |
|
|
533 | delete $self->{req_h}; |
|
|
534 | |
521 | $self->realloc; |
535 | $self->realloc; |
522 | $self->update; |
536 | $self->update; |
523 | } |
537 | } |
524 | |
538 | |
525 | # using global variables seems a bit hacky, but passing through all drawing |
539 | # using global variables seems a bit hacky, but passing through all drawing |
… | |
… | |
541 | return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w) |
555 | return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w) |
542 | || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h); |
556 | || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h); |
543 | |
557 | |
544 | glPushMatrix; |
558 | glPushMatrix; |
545 | glTranslate $self->{x}, $self->{y}, 0; |
559 | glTranslate $self->{x}, $self->{y}, 0; |
546 | $self->_draw; |
|
|
547 | glPopMatrix; |
|
|
548 | |
560 | |
549 | if ($self == $HOVER && $self->{can_hover}) { |
561 | if ($self == $HOVER && $self->{can_hover}) { |
550 | my ($x, $y) = @$self{qw(x y)}; |
|
|
551 | |
|
|
552 | glColor 1, 0.8, 0.5, 0.2; |
562 | glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2; |
553 | glEnable GL_BLEND; |
563 | glEnable GL_BLEND; |
554 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
564 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
555 | glBegin GL_QUADS; |
565 | glBegin GL_QUADS; |
556 | glVertex $x , $y; |
566 | glVertex 0 , 0; |
557 | glVertex $x + $self->{w}, $y; |
567 | glVertex $self->{w}, 0; |
558 | glVertex $x + $self->{w}, $y + $self->{h}; |
568 | glVertex $self->{w}, $self->{h}; |
559 | glVertex $x , $y + $self->{h}; |
569 | glVertex 0 , $self->{h}; |
560 | glEnd; |
570 | glEnd; |
561 | glDisable GL_BLEND; |
571 | glDisable GL_BLEND; |
562 | } |
572 | } |
563 | |
573 | |
564 | if ($ENV{CFPLUS_DEBUG} & 1) { |
574 | if ($ENV{CFPLUS_DEBUG} & 1) { |
565 | glPushMatrix; |
575 | glPushMatrix; |
566 | glColor 1, 1, 0, 1; |
576 | glColor 1, 1, 0, 1; |
567 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
577 | glTranslate 0.375, 0.375; |
568 | glBegin GL_LINE_LOOP; |
578 | glBegin GL_LINE_LOOP; |
569 | glVertex 0 , 0; |
579 | glVertex 0 , 0; |
570 | glVertex $self->{w} - 1, 0; |
580 | glVertex $self->{w} - 1, 0; |
571 | glVertex $self->{w} - 1, $self->{h} - 1; |
581 | glVertex $self->{w} - 1, $self->{h} - 1; |
572 | glVertex 0 , $self->{h} - 1; |
582 | glVertex 0 , $self->{h} - 1; |
573 | glEnd; |
583 | glEnd; |
574 | glPopMatrix; |
584 | glPopMatrix; |
575 | #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
585 | #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
576 | } |
586 | } |
|
|
587 | |
|
|
588 | $self->_draw; |
|
|
589 | glPopMatrix; |
577 | } |
590 | } |
578 | |
591 | |
579 | sub _draw { |
592 | sub _draw { |
580 | my ($self) = @_; |
593 | my ($self) = @_; |
581 | |
594 | |
… | |
… | |
619 | |
632 | |
620 | if ($color && (@$color < 4 || $color->[3])) { |
633 | if ($color && (@$color < 4 || $color->[3])) { |
621 | my ($w, $h) = @$self{qw(w h)}; |
634 | my ($w, $h) = @$self{qw(w h)}; |
622 | |
635 | |
623 | glEnable GL_BLEND; |
636 | glEnable GL_BLEND; |
624 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
637 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
625 | glColor @$color; |
638 | glColor_premultiply @$color; |
626 | |
639 | |
627 | glBegin GL_QUADS; |
640 | glBegin GL_QUADS; |
628 | glVertex 0 , 0; |
641 | glVertex 0 , 0; |
629 | glVertex 0 , $h; |
642 | glVertex 0 , $h; |
630 | glVertex $w, $h; |
643 | glVertex $w, $h; |
… | |
… | |
661 | our @ISA = CFClient::UI::Base::; |
674 | our @ISA = CFClient::UI::Base::; |
662 | |
675 | |
663 | sub new { |
676 | sub new { |
664 | my ($class, %arg) = @_; |
677 | my ($class, %arg) = @_; |
665 | |
678 | |
666 | my $children = delete $arg{children} || []; |
679 | my $children = delete $arg{children}; |
667 | |
680 | |
668 | my $self = $class->SUPER::new ( |
681 | my $self = $class->SUPER::new ( |
669 | children => [], |
682 | children => [], |
670 | can_events => 0, |
683 | can_events => 0, |
671 | %arg, |
684 | %arg, |
672 | ); |
685 | ); |
|
|
686 | |
673 | $self->add ($_) for @$children; |
687 | $self->add (@$children) |
|
|
688 | if $children; |
674 | |
689 | |
675 | $self |
690 | $self |
676 | } |
691 | } |
677 | |
692 | |
678 | sub add { |
693 | sub add { |
… | |
… | |
726 | $x -= $self->{x}; |
741 | $x -= $self->{x}; |
727 | $y -= $self->{y}; |
742 | $y -= $self->{y}; |
728 | |
743 | |
729 | my $res; |
744 | my $res; |
730 | |
745 | |
731 | for (reverse @{ $self->{children} }) { |
746 | for (reverse $self->visible_children) { |
732 | $res = $_->find_widget ($x, $y) |
747 | $res = $_->find_widget ($x, $y) |
733 | and return $res; |
748 | and return $res; |
734 | } |
749 | } |
735 | |
750 | |
736 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
751 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
… | |
… | |
785 | $self->{children}[0]->configure (0, 0, $w, $h); |
800 | $self->{children}[0]->configure (0, 0, $w, $h); |
786 | } |
801 | } |
787 | |
802 | |
788 | ############################################################################# |
803 | ############################################################################# |
789 | |
804 | |
|
|
805 | # back-buffered drawing area |
|
|
806 | |
790 | package CFClient::UI::Window; |
807 | package CFClient::UI::Window; |
791 | |
808 | |
792 | our @ISA = CFClient::UI::Bin::; |
809 | our @ISA = CFClient::UI::Bin::; |
793 | |
810 | |
794 | use CFClient::OpenGL; |
811 | use CFClient::OpenGL; |
… | |
… | |
845 | my $tex = $self->{texture} |
862 | my $tex = $self->{texture} |
846 | or return; |
863 | or return; |
847 | |
864 | |
848 | glEnable GL_TEXTURE_2D; |
865 | glEnable GL_TEXTURE_2D; |
849 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
866 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
850 | glColor 1, 1, 1, 1; |
867 | glColor 0, 0, 0, 1; |
851 | |
868 | |
852 | $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); |
869 | $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); |
853 | |
870 | |
854 | glDisable GL_TEXTURE_2D; |
871 | glDisable GL_TEXTURE_2D; |
855 | } |
872 | } |
… | |
… | |
946 | package CFClient::UI::ScrolledWindow; |
963 | package CFClient::UI::ScrolledWindow; |
947 | |
964 | |
948 | our @ISA = CFClient::UI::HBox::; |
965 | our @ISA = CFClient::UI::HBox::; |
949 | |
966 | |
950 | sub new { |
967 | sub new { |
951 | my $class = shift; |
968 | my ($class, %arg) = @_; |
|
|
969 | |
|
|
970 | my $child = delete $arg{child}; |
952 | |
971 | |
953 | my $self; |
972 | my $self; |
954 | |
973 | |
955 | my $slider = new CFClient::UI::Slider |
974 | my $slider = new CFClient::UI::Slider |
956 | vertical => 1, |
975 | vertical => 1, |
… | |
… | |
961 | ; |
980 | ; |
962 | |
981 | |
963 | $self = $class->SUPER::new ( |
982 | $self = $class->SUPER::new ( |
964 | vp => (new CFClient::UI::ViewPort expand => 1), |
983 | vp => (new CFClient::UI::ViewPort expand => 1), |
965 | slider => $slider, |
984 | slider => $slider, |
966 | @_, |
985 | %arg, |
967 | ); |
986 | ); |
968 | |
987 | |
969 | $self->{vp}->add ($self->{scrolled}); |
|
|
970 | $self->add ($self->{vp}); |
|
|
971 | $self->add ($self->{slider}); |
988 | $self->SUPER::add ($self->{vp}, $self->{slider}); |
|
|
989 | $self->add ($child) if $child; |
972 | |
990 | |
973 | $self |
991 | $self |
|
|
992 | } |
|
|
993 | |
|
|
994 | sub add { |
|
|
995 | my ($self, $widget) = @_; |
|
|
996 | |
|
|
997 | $self->{vp}->add ($self->{child} = $widget); |
974 | } |
998 | } |
975 | |
999 | |
976 | sub update { |
1000 | sub update { |
977 | my ($self) = @_; |
1001 | my ($self) = @_; |
978 | |
1002 | |
… | |
… | |
1017 | |
1041 | |
1018 | if ($self->{bg}) { |
1042 | if ($self->{bg}) { |
1019 | my ($w, $h) = @$self{qw(w h)}; |
1043 | my ($w, $h) = @$self{qw(w h)}; |
1020 | |
1044 | |
1021 | glEnable GL_BLEND; |
1045 | glEnable GL_BLEND; |
1022 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1046 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
1023 | glColor @{ $self->{bg} }; |
1047 | glColor_premultiply @{ $self->{bg} }; |
1024 | |
1048 | |
1025 | glBegin GL_QUADS; |
1049 | glBegin GL_QUADS; |
1026 | glVertex 0 , 0; |
1050 | glVertex 0 , 0; |
1027 | glVertex 0 , $h; |
1051 | glVertex 0 , $h; |
1028 | glVertex $w, $h; |
1052 | glVertex $w, $h; |
… | |
… | |
1142 | my $dy = $ev->{y} - $oy; |
1166 | my $dy = $ev->{y} - $oy; |
1143 | |
1167 | |
1144 | $self->{force_w} = $bw + $dx * ($mx ? -1 : 1); |
1168 | $self->{force_w} = $bw + $dx * ($mx ? -1 : 1); |
1145 | $self->{force_h} = $bh + $dy * ($my ? -1 : 1); |
1169 | $self->{force_h} = $bh + $dy * ($my ? -1 : 1); |
1146 | |
1170 | |
|
|
1171 | $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my); |
1147 | $self->realloc; |
1172 | $self->realloc; |
1148 | $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my); |
|
|
1149 | }; |
1173 | }; |
1150 | |
1174 | |
1151 | } elsif ($lr ^ $td) { |
1175 | } elsif ($lr ^ $td) { |
1152 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
1176 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
1153 | my ($bx, $by) = ($self->{x}, $self->{y}); |
1177 | my ($bx, $by) = ($self->{x}, $self->{y}); |
… | |
… | |
1156 | my ($ev, $x, $y) = @_; |
1180 | my ($ev, $x, $y) = @_; |
1157 | |
1181 | |
1158 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1182 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1159 | |
1183 | |
1160 | $self->move_abs ($bx + $x - $ox, $by + $y - $oy); |
1184 | $self->move_abs ($bx + $x - $ox, $by + $y - $oy); |
|
|
1185 | # HACK: the next line is required to enforce placement |
|
|
1186 | $self->{parent}->size_allocate ($self->{parent}{w}, $self->{parent}{h}); |
1161 | }; |
1187 | }; |
|
|
1188 | } else { |
|
|
1189 | return 0; |
|
|
1190 | } |
|
|
1191 | |
1162 | } |
1192 | 1 |
1163 | } |
1193 | } |
1164 | |
1194 | |
1165 | sub button_up { |
1195 | sub button_up { |
1166 | my ($self, $ev, $x, $y) = @_; |
1196 | my ($self, $ev, $x, $y) = @_; |
1167 | |
1197 | |
1168 | delete $self->{motion}; |
1198 | !!delete $self->{motion} |
1169 | } |
1199 | } |
1170 | |
1200 | |
1171 | sub mouse_motion { |
1201 | sub mouse_motion { |
1172 | my ($self, $ev, $x, $y) = @_; |
1202 | my ($self, $ev, $x, $y) = @_; |
1173 | |
1203 | |
1174 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
1204 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
|
|
1205 | |
|
|
1206 | !!$self->{motion} |
1175 | } |
1207 | } |
1176 | |
1208 | |
1177 | sub _draw { |
1209 | sub _draw { |
1178 | my ($self) = @_; |
1210 | my ($self) = @_; |
1179 | |
1211 | |
… | |
… | |
1511 | |
1543 | |
1512 | delete $self->{texture}; |
1544 | delete $self->{texture}; |
1513 | $self->SUPER::update; |
1545 | $self->SUPER::update; |
1514 | } |
1546 | } |
1515 | |
1547 | |
|
|
1548 | sub realloc { |
|
|
1549 | my ($self) = @_; |
|
|
1550 | |
|
|
1551 | delete $self->{ox}; |
|
|
1552 | $self->SUPER::realloc; |
|
|
1553 | } |
|
|
1554 | |
1516 | sub set_text { |
1555 | sub set_text { |
1517 | my ($self, $text) = @_; |
1556 | my ($self, $text) = @_; |
1518 | |
1557 | |
1519 | return if $self->{text} eq "T$text"; |
1558 | return if $self->{text} eq "T$text"; |
1520 | $self->{text} = "T$text"; |
1559 | $self->{text} = "T$text"; |
1521 | |
1560 | |
1522 | $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; |
1561 | $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; |
1523 | $self->{layout}->set_text ($text); |
1562 | $self->{layout}->set_text ($text); |
1524 | |
1563 | |
|
|
1564 | delete $self->{req_h}; |
1525 | $self->realloc; |
1565 | $self->realloc; |
1526 | $self->update; |
1566 | $self->update; |
1527 | } |
1567 | } |
1528 | |
1568 | |
1529 | sub set_markup { |
1569 | sub set_markup { |
… | |
… | |
1535 | my $rgba = $markup =~ /span.*(?:foreground|background)/; |
1575 | my $rgba = $markup =~ /span.*(?:foreground|background)/; |
1536 | |
1576 | |
1537 | $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; |
1577 | $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; |
1538 | $self->{layout}->set_markup ($markup); |
1578 | $self->{layout}->set_markup ($markup); |
1539 | |
1579 | |
|
|
1580 | delete $self->{req_h}; |
1540 | $self->realloc; |
1581 | $self->realloc; |
1541 | $self->update; |
1582 | $self->update; |
1542 | } |
1583 | } |
1543 | |
1584 | |
1544 | sub size_request { |
1585 | sub size_request { |
1545 | my ($self) = @_; |
1586 | my ($self) = @_; |
1546 | |
1587 | |
|
|
1588 | if (exists $self->{req_h}) { |
|
|
1589 | @$self{qw(req_w req_h)} |
|
|
1590 | } else { |
1547 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1591 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1548 | $self->{layout}->set_width ($self->{max_w} || -1); |
1592 | $self->{layout}->set_width ($self->{max_w} || -1); |
1549 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1593 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1550 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1594 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1551 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1595 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1552 | |
1596 | |
1553 | my ($w, $h) = $self->{layout}->size; |
1597 | my ($w, $h) = $self->{layout}->size; |
1554 | |
1598 | |
1555 | if (exists $self->{template}) { |
1599 | if (exists $self->{template}) { |
1556 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
1600 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
1557 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
1601 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
1558 | |
1602 | |
1559 | my ($w2, $h2) = $self->{template}->size; |
1603 | my ($w2, $h2) = $self->{template}->size; |
1560 | |
1604 | |
1561 | $w = List::Util::max $w, $w2; |
1605 | $w = List::Util::max $w, $w2; |
1562 | $h = List::Util::max $h, $h2; |
1606 | $h = List::Util::max $h, $h2; |
1563 | } |
1607 | } |
1564 | |
1608 | |
1565 | ($w, $h) |
1609 | ($w, $h) |
|
|
1610 | } |
1566 | } |
1611 | } |
1567 | |
1612 | |
1568 | sub size_allocate { |
1613 | sub size_allocate { |
1569 | my ($self, $w, $h) = @_; |
1614 | my ($self, $w, $h) = @_; |
1570 | |
1615 | |
… | |
… | |
1608 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y} |
1653 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y} |
1609 | : ($self->{h} - $tex->{h}) * 0.5); |
1654 | : ($self->{h} - $tex->{h}) * 0.5); |
1610 | }; |
1655 | }; |
1611 | |
1656 | |
1612 | glEnable GL_TEXTURE_2D; |
1657 | glEnable GL_TEXTURE_2D; |
1613 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1614 | |
1658 | |
1615 | if ($tex->{format} == GL_ALPHA) { |
1659 | if ($tex->{format} == GL_ALPHA) { |
|
|
1660 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1616 | glColor @{$self->{fg}}; |
1661 | glColor @{$self->{fg}}; |
1617 | $tex->draw_quad_alpha ($self->{ox}, $self->{oy}); |
1662 | $tex->draw_quad_alpha ($self->{ox}, $self->{oy}); |
1618 | } else { |
1663 | } else { |
|
|
1664 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1619 | $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}); |
1665 | $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}); |
1620 | } |
1666 | } |
1621 | |
1667 | |
1622 | glDisable GL_TEXTURE_2D; |
1668 | glDisable GL_TEXTURE_2D; |
1623 | } |
1669 | } |
… | |
… | |
1652 | |
1698 | |
1653 | delete $self->{cur_h}; |
1699 | delete $self->{cur_h}; |
1654 | |
1700 | |
1655 | return if $self->{text} eq $text; |
1701 | return if $self->{text} eq $text; |
1656 | |
1702 | |
1657 | delete $self->{texture}; |
|
|
1658 | |
|
|
1659 | $self->{last_activity} = $::NOW; |
1703 | $self->{last_activity} = $::NOW; |
1660 | $self->{text} = $text; |
1704 | $self->{text} = $text; |
1661 | |
1705 | |
1662 | $text =~ s/./*/g if $self->{hidden}; |
1706 | $text =~ s/./*/g if $self->{hidden}; |
1663 | $self->{layout}->set_text ("$text "); |
1707 | $self->{layout}->set_text ("$text "); |
|
|
1708 | delete $self->{req_h}; |
1664 | |
1709 | |
1665 | $self->_emit (changed => $self->{text}); |
1710 | $self->_emit (changed => $self->{text}); |
|
|
1711 | |
|
|
1712 | $self->realloc; |
|
|
1713 | $self->update; |
1666 | } |
1714 | } |
1667 | |
1715 | |
1668 | sub set_text { |
1716 | sub set_text { |
1669 | my ($self, $text) = @_; |
1717 | my ($self, $text) = @_; |
1670 | |
1718 | |
1671 | $self->{cursor} = length $text; |
1719 | $self->{cursor} = length $text; |
1672 | $self->_set_text ($text); |
1720 | $self->_set_text ($text); |
1673 | |
|
|
1674 | $self->realloc; |
|
|
1675 | } |
1721 | } |
1676 | |
1722 | |
1677 | sub get_text { |
1723 | sub get_text { |
1678 | $_[0]{text} |
1724 | $_[0]{text} |
1679 | } |
1725 | } |
… | |
… | |
1709 | $self->{cursor} = length $text; |
1755 | $self->{cursor} = length $text; |
1710 | } elsif ($uni == 27) { |
1756 | } elsif ($uni == 27) { |
1711 | $self->_emit ('escape'); |
1757 | $self->_emit ('escape'); |
1712 | } elsif ($uni) { |
1758 | } elsif ($uni) { |
1713 | substr $text, $self->{cursor}++, 0, chr $uni; |
1759 | substr $text, $self->{cursor}++, 0, chr $uni; |
|
|
1760 | } else { |
|
|
1761 | return 0; |
1714 | } |
1762 | } |
1715 | |
1763 | |
1716 | $self->_set_text ($text); |
1764 | $self->_set_text ($text); |
1717 | |
1765 | |
1718 | $self->realloc; |
1766 | $self->realloc; |
|
|
1767 | |
|
|
1768 | 1 |
1719 | } |
1769 | } |
1720 | |
1770 | |
1721 | sub focus_in { |
1771 | sub focus_in { |
1722 | my ($self) = @_; |
1772 | my ($self) = @_; |
1723 | |
1773 | |
… | |
… | |
1738 | utf8::encode $text; |
1788 | utf8::encode $text; |
1739 | $self->{cursor} = length substr $text, 0, $idx; |
1789 | $self->{cursor} = length substr $text, 0, $idx; |
1740 | |
1790 | |
1741 | $self->_set_text ($self->{text}); |
1791 | $self->_set_text ($self->{text}); |
1742 | $self->update; |
1792 | $self->update; |
|
|
1793 | |
|
|
1794 | 1 |
1743 | } |
1795 | } |
1744 | |
1796 | |
1745 | sub mouse_motion { |
1797 | sub mouse_motion { |
1746 | my ($self, $ev, $x, $y) = @_; |
1798 | my ($self, $ev, $x, $y) = @_; |
1747 | # printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
1799 | # printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
|
|
1800 | |
|
|
1801 | 0 |
1748 | } |
1802 | } |
1749 | |
1803 | |
1750 | sub _draw { |
1804 | sub _draw { |
1751 | my ($self) = @_; |
1805 | my ($self) = @_; |
1752 | |
1806 | |
1753 | local $self->{fg} = $self->{fg}; |
1807 | local $self->{fg} = $self->{fg}; |
1754 | |
1808 | |
1755 | if ($FOCUS == $self) { |
1809 | if ($FOCUS == $self) { |
1756 | glColor @{$self->{active_bg}}; |
1810 | glColor_premultiply @{$self->{active_bg}}; |
1757 | $self->{fg} = $self->{active_fg}; |
1811 | $self->{fg} = $self->{active_fg}; |
1758 | } else { |
1812 | } else { |
1759 | glColor @{$self->{bg}}; |
1813 | glColor_premultiply @{$self->{bg}}; |
1760 | } |
1814 | } |
1761 | |
1815 | |
1762 | glEnable GL_BLEND; |
1816 | glEnable GL_BLEND; |
1763 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1817 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
1764 | glBegin GL_QUADS; |
1818 | glBegin GL_QUADS; |
1765 | glVertex 0 , 0; |
1819 | glVertex 0 , 0; |
1766 | glVertex 0 , $self->{h}; |
1820 | glVertex 0 , $self->{h}; |
1767 | glVertex $self->{w}, $self->{h}; |
1821 | glVertex $self->{w}, $self->{h}; |
1768 | glVertex $self->{w}, 0; |
1822 | glVertex $self->{w}, 0; |
… | |
… | |
1829 | } else { |
1883 | } else { |
1830 | $self->set_text ($self->{history_saveback}); |
1884 | $self->set_text ($self->{history_saveback}); |
1831 | } |
1885 | } |
1832 | |
1886 | |
1833 | } else { |
1887 | } else { |
1834 | $self->SUPER::key_down ($ev); |
1888 | return $self->SUPER::key_down ($ev) |
|
|
1889 | } |
|
|
1890 | |
1835 | } |
1891 | 1 |
1836 | |
|
|
1837 | } |
1892 | } |
1838 | |
1893 | |
1839 | ############################################################################# |
1894 | ############################################################################# |
1840 | |
1895 | |
1841 | package CFClient::UI::Button; |
1896 | package CFClient::UI::Button; |
… | |
… | |
1870 | my ($self, $ev, $x, $y) = @_; |
1925 | my ($self, $ev, $x, $y) = @_; |
1871 | |
1926 | |
1872 | $self->emit ("activate") |
1927 | $self->emit ("activate") |
1873 | if $x >= 0 && $x < $self->{w} |
1928 | if $x >= 0 && $x < $self->{w} |
1874 | && $y >= 0 && $y < $self->{h}; |
1929 | && $y >= 0 && $y < $self->{h}; |
|
|
1930 | |
|
|
1931 | 1 |
1875 | } |
1932 | } |
1876 | |
1933 | |
1877 | sub _draw { |
1934 | sub _draw { |
1878 | my ($self) = @_; |
1935 | my ($self) = @_; |
1879 | |
1936 | |
1880 | local $self->{fg} = $self->{fg}; |
1937 | local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg}; |
1881 | |
|
|
1882 | if ($GRAB == $self) { |
|
|
1883 | $self->{fg} = $self->{active_fg}; |
|
|
1884 | } |
|
|
1885 | |
1938 | |
1886 | glEnable GL_TEXTURE_2D; |
1939 | glEnable GL_TEXTURE_2D; |
1887 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1940 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1888 | glColor 0, 0, 0, 1; |
1941 | glColor 0, 0, 0, 1; |
1889 | |
1942 | |
… | |
… | |
1933 | |
1986 | |
1934 | if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} |
1987 | if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} |
1935 | && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { |
1988 | && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { |
1936 | $self->{state} = !$self->{state}; |
1989 | $self->{state} = !$self->{state}; |
1937 | $self->_emit (changed => $self->{state}); |
1990 | $self->_emit (changed => $self->{state}); |
|
|
1991 | } else { |
|
|
1992 | return 0 |
|
|
1993 | } |
|
|
1994 | |
1938 | } |
1995 | 1 |
1939 | } |
1996 | } |
1940 | |
1997 | |
1941 | sub _draw { |
1998 | sub _draw { |
1942 | my ($self) = @_; |
1999 | my ($self) = @_; |
1943 | |
2000 | |
… | |
… | |
2110 | |
2167 | |
2111 | my $h1 = $self->{h} * (1 - $ycut1); |
2168 | my $h1 = $self->{h} * (1 - $ycut1); |
2112 | my $h2 = $self->{h} * (1 - $ycut2); |
2169 | my $h2 = $self->{h} * (1 - $ycut2); |
2113 | |
2170 | |
2114 | glEnable GL_BLEND; |
2171 | glEnable GL_BLEND; |
2115 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
2172 | glBlendFuncSeparate GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, |
|
|
2173 | GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2116 | glEnable GL_TEXTURE_2D; |
2174 | glEnable GL_TEXTURE_2D; |
2117 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2175 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2118 | |
2176 | |
2119 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
2177 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
2120 | glBegin GL_QUADS; |
2178 | glBegin GL_QUADS; |
… | |
… | |
2281 | |
2339 | |
2282 | $self->SUPER::button_down ($ev, $x, $y); |
2340 | $self->SUPER::button_down ($ev, $x, $y); |
2283 | |
2341 | |
2284 | $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; |
2342 | $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; |
2285 | |
2343 | |
2286 | $self->mouse_motion ($ev, $x, $y); |
2344 | $self->mouse_motion ($ev, $x, $y) |
2287 | } |
2345 | } |
2288 | |
2346 | |
2289 | sub mouse_motion { |
2347 | sub mouse_motion { |
2290 | my ($self, $ev, $x, $y) = @_; |
2348 | my ($self, $ev, $x, $y) = @_; |
2291 | |
2349 | |
… | |
… | |
2295 | my (undef, $lo, $hi, $page) = @{$self->{range}}; |
2353 | my (undef, $lo, $hi, $page) = @{$self->{range}}; |
2296 | |
2354 | |
2297 | $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); |
2355 | $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); |
2298 | |
2356 | |
2299 | $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); |
2357 | $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); |
|
|
2358 | } else { |
|
|
2359 | return 0; |
|
|
2360 | } |
|
|
2361 | |
2300 | } |
2362 | 1 |
2301 | } |
2363 | } |
2302 | |
2364 | |
2303 | sub update { |
2365 | sub update { |
2304 | my ($self) = @_; |
2366 | my ($self) = @_; |
2305 | |
2367 | |
2306 | $CFClient::UI::ROOT->on_post_alloc ($self => sub { |
2368 | delete $self->{knob_w}; |
|
|
2369 | $self->SUPER::update; |
|
|
2370 | } |
|
|
2371 | |
|
|
2372 | sub _draw { |
|
|
2373 | my ($self) = @_; |
|
|
2374 | |
|
|
2375 | unless ($self->{knob_w}) { |
2307 | $self->set_value ($self->{range}[0]); |
2376 | $self->set_value ($self->{range}[0]); |
2308 | |
2377 | |
2309 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
2378 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
2310 | my $range = ($hi - $page - $lo) || 1e-100; |
2379 | my $range = ($hi - $page - $lo) || 1e-100; |
2311 | |
2380 | |
… | |
… | |
2317 | $value = ($value - $lo) / $range; |
2386 | $value = ($value - $lo) / $range; |
2318 | $value = $value * $self->{scale} + $self->{offset}; |
2387 | $value = $value * $self->{scale} + $self->{offset}; |
2319 | |
2388 | |
2320 | $self->{knob_x} = $value - $knob_w * 0.5; |
2389 | $self->{knob_x} = $value - $knob_w * 0.5; |
2321 | $self->{knob_w} = $knob_w; |
2390 | $self->{knob_w} = $knob_w; |
2322 | }); |
2391 | } |
2323 | |
|
|
2324 | $self->SUPER::update; |
|
|
2325 | } |
|
|
2326 | |
|
|
2327 | sub _draw { |
|
|
2328 | my ($self) = @_; |
|
|
2329 | |
2392 | |
2330 | $self->SUPER::_draw (); |
2393 | $self->SUPER::_draw (); |
2331 | |
2394 | |
2332 | glScale $self->{w}, $self->{h}; |
2395 | glScale $self->{w}, $self->{h}; |
2333 | |
2396 | |
… | |
… | |
2527 | |
2590 | |
2528 | delete $self->{texture}; |
2591 | delete $self->{texture}; |
2529 | } |
2592 | } |
2530 | |
2593 | |
2531 | $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { |
2594 | $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { |
2532 | glClearColor 0.5, 0.5, 0.5, 0; |
2595 | glClearColor 0, 0, 0, 0; |
2533 | glClear GL_COLOR_BUFFER_BIT; |
2596 | glClear GL_COLOR_BUFFER_BIT; |
2534 | |
2597 | |
2535 | my $top = int $self->{children}[1]{range}[0]; |
2598 | my $top = int $self->{children}[1]{range}[0]; |
2536 | |
2599 | |
2537 | my $y0 = $top; |
2600 | my $y0 = $top; |
… | |
… | |
2572 | sub _draw { |
2635 | sub _draw { |
2573 | my ($self) = @_; |
2636 | my ($self) = @_; |
2574 | |
2637 | |
2575 | glEnable GL_TEXTURE_2D; |
2638 | glEnable GL_TEXTURE_2D; |
2576 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2639 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2577 | glColor 1, 1, 1, 1; |
2640 | glColor 0, 0, 0, 1; |
2578 | $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2641 | $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2579 | glDisable GL_TEXTURE_2D; |
2642 | glDisable GL_TEXTURE_2D; |
2580 | |
2643 | |
2581 | $self->{children}[1]->draw; |
2644 | $self->{children}[1]->draw; |
2582 | |
2645 | |
2583 | } |
2646 | } |
… | |
… | |
2810 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]]; |
2873 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]]; |
2811 | |
2874 | |
2812 | if ($tex) { |
2875 | if ($tex) { |
2813 | glEnable GL_TEXTURE_2D; |
2876 | glEnable GL_TEXTURE_2D; |
2814 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2877 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2815 | glColor 1, 1, 1, 1; |
2878 | glColor 0, 0, 0, 1; |
2816 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
2879 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
2817 | glDisable GL_TEXTURE_2D; |
2880 | glDisable GL_TEXTURE_2D; |
2818 | } |
2881 | } |
2819 | } |
2882 | } |
2820 | |
2883 | |
… | |
… | |
2824 | $self->{timer}->cancel |
2887 | $self->{timer}->cancel |
2825 | if $self->{timer}; |
2888 | if $self->{timer}; |
2826 | |
2889 | |
2827 | $self->SUPER::DESTROY; |
2890 | $self->SUPER::DESTROY; |
2828 | } |
2891 | } |
|
|
2892 | |
|
|
2893 | ############################################################################# |
|
|
2894 | |
|
|
2895 | package CFClient::UI::Buttonbar; |
|
|
2896 | |
|
|
2897 | our @ISA = CFClient::UI::HBox::; |
|
|
2898 | |
|
|
2899 | # TODO: should actualyl wrap buttons and other goodies. |
2829 | |
2900 | |
2830 | ############################################################################# |
2901 | ############################################################################# |
2831 | |
2902 | |
2832 | package CFClient::UI::Menu; |
2903 | package CFClient::UI::Menu; |
2833 | |
2904 | |
… | |
… | |
2883 | my ($self, $ev, $x, $y) = @_; |
2954 | my ($self, $ev, $x, $y) = @_; |
2884 | |
2955 | |
2885 | # TODO: should use vbox->find_widget or so |
2956 | # TODO: should use vbox->find_widget or so |
2886 | $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); |
2957 | $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); |
2887 | $self->{hover} = $self->{item}{$HOVER}; |
2958 | $self->{hover} = $self->{item}{$HOVER}; |
|
|
2959 | |
|
|
2960 | 0 |
2888 | } |
2961 | } |
2889 | |
2962 | |
2890 | sub button_up { |
2963 | sub button_up { |
2891 | my ($self, $ev, $x, $y) = @_; |
2964 | my ($self, $ev, $x, $y) = @_; |
2892 | |
2965 | |
… | |
… | |
2894 | undef $GRAB; |
2967 | undef $GRAB; |
2895 | $self->hide; |
2968 | $self->hide; |
2896 | |
2969 | |
2897 | $self->_emit ("popdown"); |
2970 | $self->_emit ("popdown"); |
2898 | $self->{hover}[1]->() if $self->{hover}; |
2971 | $self->{hover}[1]->() if $self->{hover}; |
|
|
2972 | } else { |
|
|
2973 | return 0 |
|
|
2974 | } |
|
|
2975 | |
2899 | } |
2976 | 1 |
2900 | } |
2977 | } |
2901 | |
2978 | |
2902 | ############################################################################# |
2979 | ############################################################################# |
2903 | |
2980 | |
2904 | package CFClient::UI::Statusbox; |
2981 | package CFClient::UI::Multiplexer; |
2905 | |
2982 | |
2906 | our @ISA = CFClient::UI::VBox::; |
2983 | our @ISA = CFClient::UI::Container::; |
2907 | |
2984 | |
2908 | sub new { |
2985 | sub new { |
2909 | my $class = shift; |
2986 | my $class = shift; |
2910 | |
2987 | |
2911 | $class->SUPER::new ( |
2988 | my $self = $class->SUPER::new ( |
|
|
2989 | @_, |
|
|
2990 | ); |
|
|
2991 | |
|
|
2992 | $self->{current} = $self->{children}[0] |
|
|
2993 | if @{ $self->{children} }; |
|
|
2994 | |
|
|
2995 | $self |
|
|
2996 | } |
|
|
2997 | |
|
|
2998 | sub add { |
|
|
2999 | my ($self, @widgets) = @_; |
|
|
3000 | |
|
|
3001 | $self->SUPER::add (@widgets); |
|
|
3002 | |
|
|
3003 | $self->{current} = $self->{children}[0] |
|
|
3004 | if @{ $self->{children} }; |
|
|
3005 | } |
|
|
3006 | |
|
|
3007 | sub set_current_page { |
|
|
3008 | my ($self, $page_or_widget) = @_; |
|
|
3009 | |
|
|
3010 | my $widget = ref $page_or_widget |
|
|
3011 | ? $page_or_widget |
|
|
3012 | : $self->{children}[$page_or_widget]; |
|
|
3013 | |
|
|
3014 | $self->{current} = $widget; |
|
|
3015 | $self->{current}->configure (0, 0, $self->{w}, $self->{h}); |
|
|
3016 | |
|
|
3017 | $self->_emit (page_changed => $self->{current}); |
|
|
3018 | |
|
|
3019 | $self->realloc; |
|
|
3020 | } |
|
|
3021 | |
|
|
3022 | sub visible_children { |
|
|
3023 | $_[0]{current} |
|
|
3024 | } |
|
|
3025 | |
|
|
3026 | sub size_request { |
|
|
3027 | my ($self) = @_; |
|
|
3028 | |
|
|
3029 | $self->{current}->size_request |
|
|
3030 | } |
|
|
3031 | |
|
|
3032 | sub size_allocate { |
|
|
3033 | my ($self, $w, $h) = @_; |
|
|
3034 | |
|
|
3035 | $self->{current}->configure (0, 0, $w, $h); |
|
|
3036 | } |
|
|
3037 | |
|
|
3038 | sub _draw { |
|
|
3039 | my ($self) = @_; |
|
|
3040 | |
|
|
3041 | $self->{current}->draw; |
|
|
3042 | } |
|
|
3043 | |
|
|
3044 | ############################################################################# |
|
|
3045 | |
|
|
3046 | package CFClient::UI::Notebook; |
|
|
3047 | |
|
|
3048 | our @ISA = CFClient::UI::VBox::; |
|
|
3049 | |
|
|
3050 | sub new { |
|
|
3051 | my $class = shift; |
|
|
3052 | |
|
|
3053 | my $self = $class->SUPER::new ( |
|
|
3054 | buttonbar => (new CFClient::UI::Buttonbar), |
|
|
3055 | multiplexer => (new CFClient::UI::Multiplexer expand => 1), |
|
|
3056 | # filter => # will be put between multiplexer and $self |
|
|
3057 | @_, |
|
|
3058 | ); |
|
|
3059 | |
|
|
3060 | $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; |
|
|
3061 | $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); |
|
|
3062 | |
|
|
3063 | $self |
|
|
3064 | } |
|
|
3065 | |
|
|
3066 | sub add { |
|
|
3067 | my ($self, $title, $widget, $tooltip) = @_; |
|
|
3068 | |
|
|
3069 | Scalar::Util::weaken $self; |
|
|
3070 | |
|
|
3071 | $self->{buttonbar}->add (new CFClient::UI::Button |
|
|
3072 | markup => $title, |
|
|
3073 | tooltip => $tooltip, |
|
|
3074 | on_activate => sub { $self->set_current_page ($widget) }, |
|
|
3075 | ); |
|
|
3076 | |
|
|
3077 | $self->{multiplexer}->add ($widget); |
|
|
3078 | } |
|
|
3079 | |
|
|
3080 | sub set_current_page { |
|
|
3081 | my ($self, $page) = @_; |
|
|
3082 | |
|
|
3083 | $self->{multiplexer}->set_current_page ($page); |
|
|
3084 | $self->_emit (page_changed => $self->{multiplexer}{current}); |
|
|
3085 | } |
|
|
3086 | |
|
|
3087 | ############################################################################# |
|
|
3088 | |
|
|
3089 | package CFClient::UI::Statusbox; |
|
|
3090 | |
|
|
3091 | our @ISA = CFClient::UI::VBox::; |
|
|
3092 | |
|
|
3093 | sub new { |
|
|
3094 | my $class = shift; |
|
|
3095 | |
|
|
3096 | my $self = $class->SUPER::new ( |
2912 | fontsize => 0.8, |
3097 | fontsize => 0.8, |
2913 | @_, |
3098 | @_, |
2914 | ) |
3099 | ); |
|
|
3100 | |
|
|
3101 | Scalar::Util::weaken (my $this = $self); |
|
|
3102 | |
|
|
3103 | $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); |
|
|
3104 | |
|
|
3105 | $self |
2915 | } |
3106 | } |
2916 | |
3107 | |
2917 | sub reorder { |
3108 | sub reorder { |
2918 | my ($self) = @_; |
3109 | my ($self) = @_; |
2919 | my $NOW = time; |
3110 | my $NOW = Time::HiRes::time; |
|
|
3111 | |
|
|
3112 | # freeze display when hovering over any label |
|
|
3113 | return if $CFClient::UI::TOOLTIP->{owner} |
|
|
3114 | && grep $CFClient::UI::TOOLTIP->{owner} == $_->{label}, |
|
|
3115 | values %{ $self->{item} }; |
2920 | |
3116 | |
2921 | while (my ($k, $v) = each %{ $self->{item} }) { |
3117 | while (my ($k, $v) = each %{ $self->{item} }) { |
2922 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
3118 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
2923 | } |
3119 | } |
2924 | |
3120 | |
… | |
… | |
2927 | my @items = sort { |
3123 | my @items = sort { |
2928 | $a->{pri} <=> $b->{pri} |
3124 | $a->{pri} <=> $b->{pri} |
2929 | or $b->{id} <=> $a->{id} |
3125 | or $b->{id} <=> $a->{id} |
2930 | } values %{ $self->{item} }; |
3126 | } values %{ $self->{item} }; |
2931 | |
3127 | |
|
|
3128 | $self->{timer}->interval (1); |
|
|
3129 | |
2932 | my $count = 10 + 1; |
3130 | my $count = 10 + 1; |
2933 | for my $item (@items) { |
3131 | for my $item (@items) { |
2934 | last unless --$count; |
3132 | last unless --$count; |
2935 | |
3133 | |
2936 | push @widgets, $item->{label} ||= do { |
3134 | my $label = $item->{label} ||= do { |
2937 | # TODO: doesn't handle markup well (read as: at all) |
3135 | # TODO: doesn't handle markup well (read as: at all) |
2938 | my $short = $item->{count} > 1 |
3136 | my $short = $item->{count} > 1 |
2939 | ? "<b>$item->{count} ×</b> $item->{text}" |
3137 | ? "<b>$item->{count} ×</b> $item->{text}" |
2940 | : $item->{text}; |
3138 | : $item->{text}; |
2941 | |
3139 | |
… | |
… | |
2949 | tooltip => $item->{tooltip}, |
3147 | tooltip => $item->{tooltip}, |
2950 | tooltip_font => $::FONT_PROP, |
3148 | tooltip_font => $::FONT_PROP, |
2951 | tooltip_width => 0.67, |
3149 | tooltip_width => 0.67, |
2952 | fontsize => $item->{fontsize} || $self->{fontsize}, |
3150 | fontsize => $item->{fontsize} || $self->{fontsize}, |
2953 | max_w => $::WIDTH * 0.44, |
3151 | max_w => $::WIDTH * 0.44, |
2954 | fg => $item->{fg}, |
3152 | fg => [@{ $item->{fg} }], |
2955 | can_events => 1, |
3153 | can_events => 1, |
2956 | can_hover => 1 |
3154 | can_hover => 1 |
2957 | }; |
3155 | }; |
|
|
3156 | |
|
|
3157 | if ((my $diff = $item->{timeout} - $NOW) < 2) { |
|
|
3158 | $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; |
|
|
3159 | $label->update; |
|
|
3160 | $label->set_max_size (undef, $label->{req_h} * $diff) |
|
|
3161 | if $diff < 1; |
|
|
3162 | $self->{timer}->interval (1/30); |
|
|
3163 | } else { |
|
|
3164 | $label->{fg}[3] = $item->{fg}[3] || 1; |
|
|
3165 | } |
|
|
3166 | |
|
|
3167 | push @widgets, $label; |
2958 | } |
3168 | } |
2959 | |
3169 | |
2960 | $self->clear; |
3170 | $self->clear; |
2961 | $self->SUPER::add (reverse @widgets); |
3171 | $self->SUPER::add (reverse @widgets); |
2962 | } |
3172 | } |
… | |
… | |
2967 | $text =~ s/^\s+//; |
3177 | $text =~ s/^\s+//; |
2968 | $text =~ s/\s+$//; |
3178 | $text =~ s/\s+$//; |
2969 | |
3179 | |
2970 | return unless $text; |
3180 | return unless $text; |
2971 | |
3181 | |
2972 | my $timeout = time + ((delete $arg{timeout}) || 60); |
3182 | my $timeout = (int time) + ((delete $arg{timeout}) || 60); |
2973 | |
3183 | |
2974 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
3184 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
2975 | |
3185 | |
2976 | if (my $item = $self->{item}{$group}) { |
3186 | if (my $item = $self->{item}{$group}) { |
2977 | if ($item->{text} eq $text) { |
3187 | if ($item->{text} eq $text) { |
… | |
… | |
3007 | |
3217 | |
3008 | $self->reorder; |
3218 | $self->reorder; |
3009 | $self->SUPER::reconfigure; |
3219 | $self->SUPER::reconfigure; |
3010 | } |
3220 | } |
3011 | |
3221 | |
|
|
3222 | sub DESTROY { |
|
|
3223 | my ($self) = @_; |
|
|
3224 | |
|
|
3225 | $self->{timer}->cancel; |
|
|
3226 | |
|
|
3227 | $self->SUPER::DESTROY; |
|
|
3228 | } |
|
|
3229 | |
3012 | ############################################################################# |
3230 | ############################################################################# |
3013 | |
3231 | |
3014 | package CFClient::UI::Inventory; |
3232 | package CFClient::UI::Inventory; |
3015 | |
3233 | |
3016 | our @ISA = CFClient::UI::ScrolledWindow::; |
3234 | our @ISA = CFClient::UI::ScrolledWindow::; |
3017 | |
3235 | |
3018 | sub new { |
3236 | sub new { |
3019 | my $class = shift; |
3237 | my $class = shift; |
3020 | |
3238 | |
3021 | my $self = $class->SUPER::new ( |
3239 | my $self = $class->SUPER::new ( |
3022 | scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]), |
3240 | child => (new CFClient::UI::Table col_expand => [0, 1, 0]), |
3023 | @_, |
3241 | @_, |
3024 | ); |
3242 | ); |
3025 | |
3243 | |
3026 | $self |
3244 | $self |
3027 | } |
3245 | } |
3028 | |
3246 | |
3029 | sub set_items { |
3247 | sub set_items { |
3030 | my ($self, $items) = @_; |
3248 | my ($self, $items) = @_; |
3031 | |
3249 | |
3032 | $self->{scrolled}->clear; |
3250 | $self->{child}->clear; |
3033 | return unless $items; |
3251 | return unless $items; |
3034 | |
3252 | |
3035 | my @items = sort { |
3253 | my @items = sort { |
3036 | ($a->{type} <=> $b->{type}) |
3254 | ($a->{type} <=> $b->{type}) |
3037 | or ($a->{name} cmp $b->{name}) |
3255 | or ($a->{name} cmp $b->{name}) |
… | |
… | |
3041 | |
3259 | |
3042 | my $row = 0; |
3260 | my $row = 0; |
3043 | for my $item (@items) { |
3261 | for my $item (@items) { |
3044 | CFClient::Item::update_widgets $item; |
3262 | CFClient::Item::update_widgets $item; |
3045 | |
3263 | |
3046 | $self->{scrolled}->add (0, $row, $item->{face_widget}); |
3264 | $self->{child}->add (0, $row, $item->{face_widget}); |
3047 | $self->{scrolled}->add (1, $row, $item->{desc_widget}); |
3265 | $self->{child}->add (1, $row, $item->{desc_widget}); |
3048 | $self->{scrolled}->add (2, $row, $item->{weight_widget}); |
3266 | $self->{child}->add (2, $row, $item->{weight_widget}); |
3049 | |
3267 | |
3050 | $row++; |
3268 | $row++; |
3051 | } |
3269 | } |
3052 | } |
3270 | } |
3053 | |
3271 | |
… | |
… | |
3151 | $rec = $::CONN->stop_record if $::CONN; |
3369 | $rec = $::CONN->stop_record if $::CONN; |
3152 | return unless ref $rec eq 'ARRAY'; |
3370 | return unless ref $rec eq 'ARRAY'; |
3153 | $self->set_command_list ($rec); |
3371 | $self->set_command_list ($rec); |
3154 | } |
3372 | } |
3155 | |
3373 | |
3156 | # if $commit is true, the binding will be set after the user entered a key combo |
3374 | |
|
|
3375 | sub ask_for_bind_and_commit { |
|
|
3376 | my ($self) = @_; |
|
|
3377 | $self->ask_for_bind (1); |
|
|
3378 | } |
|
|
3379 | |
3157 | sub ask_for_bind { |
3380 | sub ask_for_bind { |
3158 | my ($self, $commit) = @_; |
3381 | my ($self, $commit) = @_; |
3159 | |
3382 | |
3160 | CFClient::Binder::open_binding_dialog (sub { |
3383 | CFClient::Binder::open_binding_dialog (sub { |
3161 | my ($mod, $sym) = @_; |
3384 | my ($mod, $sym) = @_; |
… | |
… | |
3245 | |
3468 | |
3246 | ############################################################################# |
3469 | ############################################################################# |
3247 | |
3470 | |
3248 | package CFClient::UI::SpellList; |
3471 | package CFClient::UI::SpellList; |
3249 | |
3472 | |
3250 | our @ISA = CFClient::UI::FancyFrame::; |
3473 | our @ISA = CFClient::UI::Table::; |
3251 | |
3474 | |
3252 | sub new { |
3475 | sub new { |
3253 | my $class = shift; |
3476 | my $class = shift; |
3254 | |
3477 | |
3255 | my $self = $class->SUPER::new (binding => [], commands => [], @_); |
3478 | my $self = $class->SUPER::new ( |
3256 | |
3479 | binding => [], |
3257 | $self->add (new CFClient::UI::ScrolledWindow |
3480 | commands => [], |
3258 | scrolled => $self->{spellbox} = new CFClient::UI::Table); |
3481 | @_, |
3259 | |
3482 | ) |
3260 | $self; |
|
|
3261 | } |
3483 | } |
3262 | |
3484 | |
3263 | # XXX: Do sorting? Argl... |
3485 | # XXX: Do sorting? Argl... |
3264 | sub add_spell { |
3486 | sub add_spell { |
3265 | my ($self, $spell) = @_; |
3487 | my ($self, $spell) = @_; |
3266 | $self->{spells}->{$spell->{name}} = $spell; |
3488 | $self->{spells}->{$spell->{name}} = $spell; |
3267 | |
3489 | |
3268 | $self->{spellbox}->add (0, $self->{tbl_idx}, new CFClient::UI::Face |
3490 | $self->add (0, $self->{tbl_idx}, new CFClient::UI::Face |
3269 | face => $spell->{face}, |
3491 | face => $spell->{face}, |
3270 | can_hover => 1, |
3492 | can_hover => 1, |
3271 | can_events => 1, |
3493 | can_events => 1, |
3272 | tooltip => $spell->{message}); |
3494 | tooltip => $spell->{message}); |
3273 | |
3495 | |
3274 | $self->{spellbox}->add (1, $self->{tbl_idx}, new CFClient::UI::Label |
3496 | $self->add (1, $self->{tbl_idx}, new CFClient::UI::Label |
3275 | text => $spell->{name}, |
3497 | text => $spell->{name}, |
3276 | can_hover => 1, |
3498 | can_hover => 1, |
3277 | can_events => 1, |
3499 | can_events => 1, |
3278 | tooltip => $spell->{message}, |
3500 | tooltip => $spell->{message}, |
3279 | expand => 1); |
3501 | expand => 1); |
3280 | |
3502 | |
3281 | $self->{spellbox}->add (2, $self->{tbl_idx}, new CFClient::UI::Label |
3503 | $self->add (2, $self->{tbl_idx}, new CFClient::UI::Label |
3282 | text => (sprintf "lvl: %2d sp: %2d dmg: %2d", |
3504 | text => (sprintf "lvl: %2d sp: %2d dmg: %2d", |
3283 | $spell->{level}, ($spell->{mana} || $spell->{grace}), $spell->{damage}), |
3505 | $spell->{level}, ($spell->{mana} || $spell->{grace}), $spell->{damage}), |
3284 | expand => 1); |
3506 | expand => 1); |
3285 | |
3507 | |
3286 | $self->{spellbox}->add (3, $self->{tbl_idx}++, new CFClient::UI::Button |
3508 | $self->add (3, $self->{tbl_idx}++, new CFClient::UI::Button |
3287 | text => "bind to key", |
3509 | text => "bind to key", |
3288 | on_activate => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }); |
3510 | on_activate => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }); |
3289 | } |
3511 | } |
3290 | |
3512 | |
3291 | sub rebuild_spell_list { |
3513 | sub rebuild_spell_list { |
… | |
… | |
3303 | ############################################################################# |
3525 | ############################################################################# |
3304 | |
3526 | |
3305 | package CFClient::UI::Root; |
3527 | package CFClient::UI::Root; |
3306 | |
3528 | |
3307 | our @ISA = CFClient::UI::Container::; |
3529 | our @ISA = CFClient::UI::Container::; |
|
|
3530 | |
|
|
3531 | use List::Util qw(min max); |
3308 | |
3532 | |
3309 | use CFClient::OpenGL; |
3533 | use CFClient::OpenGL; |
3310 | |
3534 | |
3311 | sub new { |
3535 | sub new { |
3312 | my $class = shift; |
3536 | my $class = shift; |
… | |
… | |
3447 | |
3671 | |
3448 | delete $queue{$widget+0}; |
3672 | delete $queue{$widget+0}; |
3449 | |
3673 | |
3450 | my ($w, $h) = $widget->size_request; |
3674 | my ($w, $h) = $widget->size_request; |
3451 | |
3675 | |
3452 | $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2; |
3676 | $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; |
3453 | $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2; |
3677 | $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; |
|
|
3678 | |
|
|
3679 | $w = min $widget->{max_w}, $w if exists $widget->{max_w}; |
|
|
3680 | $h = min $widget->{max_h}, $h if exists $widget->{max_h}; |
3454 | |
3681 | |
3455 | $w = $widget->{force_w} if exists $widget->{force_w}; |
3682 | $w = $widget->{force_w} if exists $widget->{force_w}; |
3456 | $h = $widget->{force_h} if exists $widget->{force_h}; |
3683 | $h = $widget->{force_h} if exists $widget->{force_h}; |
3457 | |
3684 | |
3458 | if ($widget->{req_w} != $w || $widget->{req_h} != $h |
3685 | if ($widget->{req_w} != $w || $widget->{req_h} != $h |