… | |
… | |
2 | |
2 | |
3 | use utf8; |
3 | use utf8; |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use List::Util (); |
6 | use List::Util (); |
7 | use Event; |
|
|
8 | |
7 | |
9 | use CFPlus; |
8 | use CFPlus; |
10 | use CFPlus::Pod; |
9 | use CFPlus::Pod; |
11 | use CFPlus::Texture; |
10 | use CFPlus::Texture; |
12 | |
11 | |
… | |
… | |
17 | our $TOOLTIP; |
16 | our $TOOLTIP; |
18 | our $BUTTON_STATE; |
17 | our $BUTTON_STATE; |
19 | |
18 | |
20 | our %WIDGET; # all widgets, weak-referenced |
19 | our %WIDGET; # all widgets, weak-referenced |
21 | |
20 | |
22 | our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { |
21 | our $TOOLTIP_WATCHER = EV::timer_ns 0, 1/60, sub { |
|
|
22 | $_[0]->stop; |
|
|
23 | |
23 | if (!$GRAB) { |
24 | if (!$GRAB) { |
24 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
25 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
25 | if (length $widget->{tooltip}) { |
26 | if (length $widget->{tooltip}) { |
26 | if ($TOOLTIP->{owner} != $widget) { |
27 | if ($TOOLTIP->{owner} != $widget) { |
27 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
28 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
… | |
… | |
30 | $TOOLTIP->{owner} = $widget; |
31 | $TOOLTIP->{owner} = $widget; |
31 | $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; |
32 | $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; |
32 | |
33 | |
33 | return if $ENV{CFPLUS_DEBUG} & 8; |
34 | return if $ENV{CFPLUS_DEBUG} & 8; |
34 | |
35 | |
35 | my $tip = $widget->{tooltip}; |
|
|
36 | |
|
|
37 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
38 | |
|
|
39 | $TOOLTIP->set_tooltip_from ($widget); |
36 | $TOOLTIP->set_tooltip_from ($widget); |
40 | $TOOLTIP->show; |
37 | $TOOLTIP->show; |
41 | } |
38 | } |
42 | |
39 | |
43 | return; |
40 | return; |
… | |
… | |
46 | } |
43 | } |
47 | |
44 | |
48 | $TOOLTIP->hide; |
45 | $TOOLTIP->hide; |
49 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
46 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
50 | delete $TOOLTIP->{owner}; |
47 | delete $TOOLTIP->{owner}; |
51 | }); |
48 | }; |
52 | |
49 | |
53 | sub get_layout { |
50 | sub get_layout { |
54 | my $layout; |
51 | my $layout; |
55 | |
52 | |
56 | for (grep { $_->{name} } values %WIDGET) { |
53 | for (grep { $_->{name} } values %WIDGET) { |
… | |
… | |
91 | my $hover = $HOVER; $HOVER = $widget; |
88 | my $hover = $HOVER; $HOVER = $widget; |
92 | |
89 | |
93 | $hover->update if $hover && $hover->{can_hover}; |
90 | $hover->update if $hover && $hover->{can_hover}; |
94 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
91 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
95 | |
92 | |
96 | $TOOLTIP_WATCHER->start; |
93 | $TOOLTIP_WATCHER->again; |
97 | } |
94 | } |
98 | } |
95 | } |
99 | |
96 | |
100 | sub feed_sdl_button_down_event { |
97 | sub feed_sdl_button_down_event { |
101 | my ($ev) = @_; |
98 | my ($ev) = @_; |
… | |
… | |
107 | my $widget = $ROOT->find_widget ($x, $y); |
104 | my $widget = $ROOT->find_widget ($x, $y); |
108 | |
105 | |
109 | $GRAB = $widget; |
106 | $GRAB = $widget; |
110 | $GRAB->update if $GRAB; |
107 | $GRAB->update if $GRAB; |
111 | |
108 | |
112 | $TOOLTIP_WATCHER->cb->(); |
109 | $TOOLTIP_WATCHER->invoke; |
113 | } |
110 | } |
114 | |
111 | |
115 | if ($GRAB) { |
112 | if ($GRAB) { |
116 | if ($ev->{button} == 4 || $ev->{button} == 5) { |
113 | if ($ev->{button} == 4 || $ev->{button} == 5) { |
117 | # mousewheel |
114 | # mousewheel |
… | |
… | |
142 | my $grab = $GRAB; undef $GRAB; |
139 | my $grab = $GRAB; undef $GRAB; |
143 | $grab->update if $grab; |
140 | $grab->update if $grab; |
144 | $GRAB->update if $GRAB; |
141 | $GRAB->update if $GRAB; |
145 | |
142 | |
146 | check_hover $widget; |
143 | check_hover $widget; |
147 | $TOOLTIP_WATCHER->cb->(); |
144 | $TOOLTIP_WATCHER->invoke; |
148 | } |
145 | } |
149 | } |
146 | } |
150 | |
147 | |
151 | sub feed_sdl_motion_event { |
148 | sub feed_sdl_motion_event { |
152 | my ($ev) = @_; |
149 | my ($ev) = @_; |
… | |
… | |
273 | $self->emit ("destroy"); |
270 | $self->emit ("destroy"); |
274 | %$self = (); |
271 | %$self = (); |
275 | } |
272 | } |
276 | |
273 | |
277 | sub TO_JSON { |
274 | sub TO_JSON { |
278 | { __widget_ref__ => $_[0]{s_id} } |
275 | { "\fw" => $_[0]{s_id} } |
279 | } |
276 | } |
280 | |
277 | |
281 | sub show { |
278 | sub show { |
282 | my ($self) = @_; |
279 | my ($self) = @_; |
283 | |
280 | |
… | |
… | |
312 | delete $self->{root}; |
309 | delete $self->{root}; |
313 | |
310 | |
314 | undef $GRAB if $GRAB == $self; |
311 | undef $GRAB if $GRAB == $self; |
315 | undef $HOVER if $HOVER == $self; |
312 | undef $HOVER if $HOVER == $self; |
316 | |
313 | |
317 | $CFPlus::UI::TOOLTIP_WATCHER->cb->() |
314 | $CFPlus::UI::TOOLTIP_WATCHER->invoke |
318 | if $TOOLTIP->{owner} == $self; |
315 | if $TOOLTIP->{owner} == $self; |
319 | |
316 | |
320 | $self->emit ("focus_out"); |
317 | $self->emit ("focus_out"); |
321 | $self->emit (visibility_change => 0); |
318 | $self->emit (visibility_change => 0); |
322 | } |
319 | } |
… | |
… | |
362 | |
359 | |
363 | $self->{force_w} = $w; |
360 | $self->{force_w} = $w; |
364 | $self->{force_h} = $h; |
361 | $self->{force_h} = $h; |
365 | |
362 | |
366 | $self->realloc; |
363 | $self->realloc; |
|
|
364 | } |
|
|
365 | |
|
|
366 | # traverse the widget chain up to find the maximum "physical" size constraints |
|
|
367 | sub get_max_wh { |
|
|
368 | my ($self) = @_; |
|
|
369 | |
|
|
370 | return $self->{parent}->get_max_wh |
|
|
371 | if $self->{parent}; |
|
|
372 | |
|
|
373 | ($::WIDTH, $::HEIGHT) |
367 | } |
374 | } |
368 | |
375 | |
369 | sub size_request { |
376 | sub size_request { |
370 | require Carp; |
377 | require Carp; |
371 | Carp::confess "size_request is abstract"; |
378 | Carp::confess "size_request is abstract"; |
… | |
… | |
433 | |
440 | |
434 | $self->{tooltip} = $tooltip; |
441 | $self->{tooltip} = $tooltip; |
435 | |
442 | |
436 | if ($CFPlus::UI::TOOLTIP->{owner} == $self) { |
443 | if ($CFPlus::UI::TOOLTIP->{owner} == $self) { |
437 | delete $CFPlus::UI::TOOLTIP->{owner}; |
444 | delete $CFPlus::UI::TOOLTIP->{owner}; |
438 | $CFPlus::UI::TOOLTIP_WATCHER->cb->(); |
445 | $CFPlus::UI::TOOLTIP_WATCHER->invoke; |
439 | } |
446 | } |
440 | } |
447 | } |
441 | |
448 | |
442 | # translate global coordinates to local coordinate system |
449 | # translate global coordinates to local coordinate system |
443 | sub coord2local { |
450 | sub coord2local { |
… | |
… | |
593 | |
600 | |
594 | # using global variables seems a bit hacky, but passing through all drawing |
601 | # using global variables seems a bit hacky, but passing through all drawing |
595 | # functions seems pointless. |
602 | # functions seems pointless. |
596 | our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn |
603 | our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn |
597 | |
604 | |
598 | sub draw { |
605 | #sub draw { |
599 | my ($self) = @_; |
606 | #CFPlus.xs |
600 | |
|
|
601 | return unless $self->{h} && $self->{w}; |
|
|
602 | |
|
|
603 | # update screen rectangle |
|
|
604 | local $draw_x = $draw_x + $self->{x}; |
|
|
605 | local $draw_y = $draw_y + $self->{y}; |
|
|
606 | |
|
|
607 | # skip widgets that are entirely outside the drawing area |
|
|
608 | return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w) |
|
|
609 | || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h); |
|
|
610 | |
|
|
611 | glPushMatrix; |
|
|
612 | glTranslate $self->{x}, $self->{y}, 0; |
|
|
613 | |
|
|
614 | if ($self == $HOVER && $self->{can_hover}) { |
|
|
615 | glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2; |
|
|
616 | glEnable GL_BLEND; |
|
|
617 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
618 | glBegin GL_QUADS; |
|
|
619 | glVertex 0 , 0; |
|
|
620 | glVertex $self->{w}, 0; |
|
|
621 | glVertex $self->{w}, $self->{h}; |
|
|
622 | glVertex 0 , $self->{h}; |
|
|
623 | glEnd; |
|
|
624 | glDisable GL_BLEND; |
|
|
625 | } |
|
|
626 | |
|
|
627 | if ($ENV{CFPLUS_DEBUG} & 1) { |
|
|
628 | glPushMatrix; |
|
|
629 | glColor 1, 1, 0, 1; |
|
|
630 | glTranslate 0.375, 0.375; |
|
|
631 | glBegin GL_LINE_LOOP; |
|
|
632 | glVertex 0 , 0; |
|
|
633 | glVertex $self->{w} - 1, 0; |
|
|
634 | glVertex $self->{w} - 1, $self->{h} - 1; |
|
|
635 | glVertex 0 , $self->{h} - 1; |
|
|
636 | glEnd; |
|
|
637 | glPopMatrix; |
|
|
638 | #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
|
|
639 | } |
|
|
640 | |
|
|
641 | $self->_draw; |
|
|
642 | glPopMatrix; |
|
|
643 | } |
|
|
644 | |
607 | |
645 | sub _draw { |
608 | sub _draw { |
646 | my ($self) = @_; |
609 | my ($self) = @_; |
647 | |
610 | |
648 | warn "no draw defined for $self\n"; |
611 | warn "no draw defined for $self\n"; |
… | |
… | |
651 | sub DESTROY { |
614 | sub DESTROY { |
652 | my ($self) = @_; |
615 | my ($self) = @_; |
653 | |
616 | |
654 | return if CFPlus::in_destruct; |
617 | return if CFPlus::in_destruct; |
655 | |
618 | |
|
|
619 | local $@; |
656 | eval { $self->destroy }; |
620 | eval { $self->destroy }; |
657 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
621 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
658 | |
622 | |
659 | delete $WIDGET{$self+0}; |
623 | delete $WIDGET{$self+0}; |
660 | } |
624 | } |
… | |
… | |
668 | use strict; |
632 | use strict; |
669 | use CFPlus::OpenGL; |
633 | use CFPlus::OpenGL; |
670 | |
634 | |
671 | sub new { |
635 | sub new { |
672 | my $class = shift; |
636 | my $class = shift; |
673 | |
|
|
674 | # range [value, low, high, page] |
|
|
675 | |
637 | |
676 | $class->SUPER::new ( |
638 | $class->SUPER::new ( |
677 | #bg => [0, 0, 0, 0.2], |
639 | #bg => [0, 0, 0, 0.2], |
678 | #active_bg => [1, 1, 1, 0.5], |
640 | #active_bg => [1, 1, 1, 0.5], |
679 | @_ |
641 | @_ |
… | |
… | |
691 | my ($w, $h) = @$self{qw(w h)}; |
653 | my ($w, $h) = @$self{qw(w h)}; |
692 | |
654 | |
693 | glEnable GL_BLEND; |
655 | glEnable GL_BLEND; |
694 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
656 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
695 | glColor_premultiply @$color; |
657 | glColor_premultiply @$color; |
696 | |
|
|
697 | glBegin GL_QUADS; |
|
|
698 | glVertex 0 , 0; |
|
|
699 | glVertex 0 , $h; |
|
|
700 | glVertex $w, $h; |
658 | glRect 0, 0, $w, $h; |
701 | glVertex $w, 0; |
|
|
702 | glEnd; |
|
|
703 | |
|
|
704 | glDisable GL_BLEND; |
659 | glDisable GL_BLEND; |
705 | } |
660 | } |
706 | } |
661 | } |
707 | |
662 | |
708 | ############################################################################# |
663 | ############################################################################# |
… | |
… | |
769 | @{$self->{children}}, @widgets |
724 | @{$self->{children}}, @widgets |
770 | ]; |
725 | ]; |
771 | |
726 | |
772 | $self->realloc; |
727 | $self->realloc; |
773 | |
728 | |
|
|
729 | $self->emit (c_add => \@widgets); |
|
|
730 | |
774 | map $_+0, @widgets |
731 | map $_+0, @widgets |
775 | } |
732 | } |
776 | |
733 | |
777 | sub children { |
734 | sub children { |
778 | @{ $_[0]{children} } |
735 | @{ $_[0]{children} } |
779 | } |
736 | } |
780 | |
737 | |
781 | sub remove { |
738 | sub remove { |
782 | my ($self, $child) = @_; |
739 | my ($self, @widgets) = @_; |
783 | |
740 | |
|
|
741 | $self->emit (c_remove => \@widgets); |
|
|
742 | |
|
|
743 | for my $child (@widgets) { |
784 | delete $child->{parent}; |
744 | delete $child->{parent}; |
785 | $child->hide; |
745 | $child->hide; |
786 | |
|
|
787 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
746 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
|
|
747 | } |
788 | |
748 | |
789 | $self->realloc; |
749 | $self->realloc; |
790 | } |
750 | } |
791 | |
751 | |
792 | sub clear { |
752 | sub clear { |
… | |
… | |
820 | } |
780 | } |
821 | |
781 | |
822 | sub _draw { |
782 | sub _draw { |
823 | my ($self) = @_; |
783 | my ($self) = @_; |
824 | |
784 | |
825 | $_->draw for @{$self->{children}}; |
785 | $_->draw for $self->visible_children; |
826 | } |
786 | } |
827 | |
787 | |
828 | ############################################################################# |
788 | ############################################################################# |
829 | |
789 | |
830 | package CFPlus::UI::Bin; |
790 | package CFPlus::UI::Bin; |
… | |
… | |
840 | } |
800 | } |
841 | |
801 | |
842 | sub add { |
802 | sub add { |
843 | my ($self, $child) = @_; |
803 | my ($self, $child) = @_; |
844 | |
804 | |
845 | $self->SUPER::remove ($_) for @{ $self->{children} }; |
805 | $self->clear; |
846 | $self->SUPER::add ($child); |
806 | $self->SUPER::add ($child); |
847 | } |
807 | } |
848 | |
808 | |
849 | sub remove { |
809 | sub remove { |
850 | my ($self, $widget) = @_; |
810 | my ($self, $widget) = @_; |
… | |
… | |
868 | |
828 | |
869 | 1 |
829 | 1 |
870 | } |
830 | } |
871 | |
831 | |
872 | ############################################################################# |
832 | ############################################################################# |
873 | |
|
|
874 | # back-buffered drawing area |
833 | # back-buffered drawing area |
875 | |
834 | |
876 | package CFPlus::UI::Window; |
835 | package CFPlus::UI::Window; |
877 | |
836 | |
878 | our @ISA = CFPlus::UI::Bin::; |
837 | our @ISA = CFPlus::UI::Bin::; |
… | |
… | |
992 | $self->{view_x} = $x; |
951 | $self->{view_x} = $x; |
993 | $self->{view_y} = $y; |
952 | $self->{view_y} = $y; |
994 | |
953 | |
995 | $self->emit (changed => $x, $y); |
954 | $self->emit (changed => $x, $y); |
996 | $self->update; |
955 | $self->update; |
|
|
956 | } |
|
|
957 | } |
|
|
958 | |
|
|
959 | sub set_center { |
|
|
960 | my ($self, $x, $y) = @_; |
|
|
961 | |
|
|
962 | $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5); |
|
|
963 | } |
|
|
964 | |
|
|
965 | sub make_visible { |
|
|
966 | my ($self, $x, $y, $border) = @_; |
|
|
967 | |
|
|
968 | if ( $x < $self->{view_x} + $self->{w} * $border |
|
|
969 | || $x > $self->{view_x} + $self->{w} * (1 - $border) |
|
|
970 | || $y < $self->{view_y} + $self->{h} * $border |
|
|
971 | || $y > $self->{view_y} + $self->{h} * (1 - $border) |
|
|
972 | ) { |
|
|
973 | $self->set_center ($x, $y); |
997 | } |
974 | } |
998 | } |
975 | } |
999 | |
976 | |
1000 | # hmm, this does not work for topleft of $self... but we should not ask for that |
977 | # hmm, this does not work for topleft of $self... but we should not ask for that |
1001 | sub coord2local { |
978 | sub coord2local { |
… | |
… | |
1048 | my $child = delete $arg{child}; |
1025 | my $child = delete $arg{child}; |
1049 | |
1026 | |
1050 | my $self; |
1027 | my $self; |
1051 | |
1028 | |
1052 | my $hslider = new CFPlus::UI::Slider |
1029 | my $hslider = new CFPlus::UI::Slider |
1053 | col => 0, |
1030 | c_col => 0, |
1054 | row => 1, |
1031 | c_row => 1, |
1055 | vertical => 0, |
1032 | vertical => 0, |
1056 | range => [0, 0, 1, 0.01], # HACK fix |
1033 | range => [0, 0, 1, 0.01], # HACK fix |
1057 | on_changed => sub { |
1034 | on_changed => sub { |
1058 | $self->{hpos} = $_[1]; |
1035 | $self->{hpos} = $_[1]; |
1059 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1036 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1060 | }, |
1037 | }, |
1061 | ; |
1038 | ; |
1062 | |
1039 | |
1063 | my $vslider = new CFPlus::UI::Slider |
1040 | my $vslider = new CFPlus::UI::Slider |
1064 | col => 1, |
1041 | c_col => 1, |
1065 | row => 0, |
1042 | c_row => 0, |
1066 | vertical => 1, |
1043 | vertical => 1, |
1067 | range => [0, 0, 1, 0.01], # HACK fix |
1044 | range => [0, 0, 1, 0.01], # HACK fix |
1068 | on_changed => sub { |
1045 | on_changed => sub { |
1069 | $self->{vpos} = $_[1]; |
1046 | $self->{vpos} = $_[1]; |
1070 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1047 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
… | |
… | |
1081 | row_expand => [1, 0], |
1058 | row_expand => [1, 0], |
1082 | %arg, |
1059 | %arg, |
1083 | ); |
1060 | ); |
1084 | |
1061 | |
1085 | $self->{vp} = new CFPlus::UI::ViewPort |
1062 | $self->{vp} = new CFPlus::UI::ViewPort |
1086 | col => 0, |
1063 | c_col => 0, |
1087 | row => 0, |
1064 | c_row => 0, |
1088 | expand => 1, |
1065 | expand => 1, |
1089 | scroll_x => $self->{scroll_x}, |
1066 | scroll_x => $self->{scroll_x}, |
1090 | scroll_y => $self->{scroll_y}, |
1067 | scroll_y => $self->{scroll_y}, |
1091 | on_changed => sub { |
1068 | on_changed => sub { |
1092 | my ($vp, $x, $y) = @_; |
1069 | my ($vp, $x, $y) = @_; |
… | |
… | |
1114 | my ($self, $widget) = @_; |
1091 | my ($self, $widget) = @_; |
1115 | |
1092 | |
1116 | $self->{vp}->add ($self->{child} = $widget); |
1093 | $self->{vp}->add ($self->{child} = $widget); |
1117 | } |
1094 | } |
1118 | |
1095 | |
|
|
1096 | sub set_offset { shift->{vp}->set_offset (@_) } |
|
|
1097 | sub set_center { shift->{vp}->set_center (@_) } |
|
|
1098 | sub make_visible { shift->{vp}->make_visible (@_) } |
|
|
1099 | |
1119 | sub update_slider { |
1100 | sub update_slider { |
1120 | my ($self) = @_; |
1101 | my ($self) = @_; |
1121 | |
1102 | |
1122 | my $child = ($self->{vp} or return)->child; |
1103 | my $child = ($self->{vp} or return)->child; |
1123 | |
1104 | |
… | |
… | |
1236 | my ($w, $h) = @$self{qw(w h)}; |
1217 | my ($w, $h) = @$self{qw(w h)}; |
1237 | |
1218 | |
1238 | glEnable GL_BLEND; |
1219 | glEnable GL_BLEND; |
1239 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
1220 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
1240 | glColor_premultiply @{ $self->{bg} }; |
1221 | glColor_premultiply @{ $self->{bg} }; |
1241 | |
|
|
1242 | glBegin GL_QUADS; |
|
|
1243 | glVertex 0 , 0; |
|
|
1244 | glVertex 0 , $h; |
|
|
1245 | glVertex $w, $h; |
1222 | glRect 0, 0, $w, $h; |
1246 | glVertex $w, 0; |
|
|
1247 | glEnd; |
|
|
1248 | |
|
|
1249 | glDisable GL_BLEND; |
1223 | glDisable GL_BLEND; |
1250 | } |
1224 | } |
1251 | |
1225 | |
1252 | $self->SUPER::_draw; |
1226 | $self->SUPER::_draw; |
1253 | } |
1227 | } |
… | |
… | |
1414 | |
1388 | |
1415 | sub border { |
1389 | sub border { |
1416 | int $_[0]{border} * $::FONTSIZE |
1390 | int $_[0]{border} * $::FONTSIZE |
1417 | } |
1391 | } |
1418 | |
1392 | |
|
|
1393 | sub get_max_wh { |
|
|
1394 | my ($self) = @_; |
|
|
1395 | |
|
|
1396 | return ($self->{w}, $self->{h}) |
|
|
1397 | if $self->{visible} && $self->{w}; |
|
|
1398 | |
|
|
1399 | $self->SUPER::get_max_wh |
|
|
1400 | } |
|
|
1401 | |
1419 | sub size_request { |
1402 | sub size_request { |
1420 | my ($self) = @_; |
1403 | my ($self) = @_; |
1421 | |
1404 | |
1422 | $self->{title_widget}->size_request |
1405 | $self->{title_widget}->size_request |
1423 | if $self->{title_widget}; |
1406 | if $self->{title_widget}; |
… | |
… | |
1615 | |
1598 | |
1616 | sub add { |
1599 | sub add { |
1617 | my ($self, @widgets) = @_; |
1600 | my ($self, @widgets) = @_; |
1618 | |
1601 | |
1619 | for my $child (@widgets) { |
1602 | for my $child (@widgets) { |
1620 | $child->{rowspan} ||= 1; |
1603 | $child->{c_rowspan} ||= 1; |
1621 | $child->{colspan} ||= 1; |
1604 | $child->{c_colspan} ||= 1; |
1622 | } |
1605 | } |
1623 | |
1606 | |
1624 | $self->SUPER::add (@widgets); |
1607 | $self->SUPER::add (@widgets); |
1625 | } |
1608 | } |
1626 | |
1609 | |
… | |
… | |
1630 | my @widgets; |
1613 | my @widgets; |
1631 | |
1614 | |
1632 | while (@_) { |
1615 | while (@_) { |
1633 | my ($col, $row, $child) = splice @_, 0, 3, (); |
1616 | my ($col, $row, $child) = splice @_, 0, 3, (); |
1634 | |
1617 | |
1635 | $child->{row} = $row; |
1618 | $child->{c_row} = $row; |
1636 | $child->{col} = $col; |
1619 | $child->{c_col} = $col; |
1637 | |
1620 | |
1638 | push @widgets, $child; |
1621 | push @widgets, $child; |
1639 | } |
1622 | } |
1640 | |
1623 | |
1641 | $self->add (@widgets); |
1624 | $self->add (@widgets); |
… | |
… | |
1647 | my (@w, @h); |
1630 | my (@w, @h); |
1648 | |
1631 | |
1649 | my @children = $self->children; |
1632 | my @children = $self->children; |
1650 | |
1633 | |
1651 | # first pass, columns |
1634 | # first pass, columns |
1652 | for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) { |
1635 | for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) { |
1653 | my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; |
1636 | my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; |
1654 | |
1637 | |
1655 | my $sw = sum @w[$c .. $c + $cs - 1]; |
1638 | my $sw = sum @w[$c .. $c + $cs - 1]; |
1656 | |
1639 | |
1657 | if ($w > $sw) { |
1640 | if ($w > $sw) { |
1658 | $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; |
1641 | $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; |
1659 | } |
1642 | } |
1660 | } |
1643 | } |
1661 | |
1644 | |
1662 | # second pass, rows |
1645 | # second pass, rows |
1663 | for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) { |
1646 | for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { |
1664 | my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; |
1647 | my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; |
1665 | |
1648 | |
1666 | my $sh = sum @h[$r .. $r + $rs - 1]; |
1649 | my $sh = sum @h[$r .. $r + $rs - 1]; |
1667 | |
1650 | |
1668 | if ($h > $sh) { |
1651 | if ($h > $sh) { |
1669 | $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; |
1652 | $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; |
… | |
… | |
1711 | |
1694 | |
1712 | my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } |
1695 | my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } |
1713 | my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } |
1696 | my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } |
1714 | |
1697 | |
1715 | for my $widget ($self->children) { |
1698 | for my $widget ($self->children) { |
1716 | my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)}; |
1699 | my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)}; |
1717 | |
1700 | |
1718 | $widget->configure ( |
1701 | $widget->configure ( |
1719 | $x[$c], $y[$r], |
1702 | $x[$c], $y[$r], |
1720 | $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], |
1703 | $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], |
1721 | ); |
1704 | ); |
… | |
… | |
1730 | |
1713 | |
1731 | use List::Util qw(min max); |
1714 | use List::Util qw(min max); |
1732 | |
1715 | |
1733 | our @ISA = CFPlus::UI::Container::; |
1716 | our @ISA = CFPlus::UI::Container::; |
1734 | |
1717 | |
1735 | sub add_fixed { |
|
|
1736 | my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_; |
|
|
1737 | |
|
|
1738 | $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h]; |
|
|
1739 | $self->SUPER::add ($child); |
|
|
1740 | } |
|
|
1741 | |
|
|
1742 | sub _scale($$$) { |
1718 | sub _scale($$$) { |
1743 | my ($mode, $val, $max) = @_; |
1719 | my ($rel, $val, $max) = @_; |
1744 | |
1720 | |
1745 | $mode eq "abs" ? $val |
1721 | $rel ? $val * $max : $val |
1746 | : $mode eq "rel" ? $val * $max |
|
|
1747 | : 0 |
|
|
1748 | } |
1722 | } |
1749 | |
1723 | |
1750 | sub size_request { |
1724 | sub size_request { |
1751 | my ($self) = @_; |
1725 | my ($self) = @_; |
1752 | |
1726 | |
1753 | my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); |
1727 | my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); |
1754 | |
1728 | |
1755 | # determine overall size by querying abs widgets |
1729 | # determine overall size by querying abs widgets |
1756 | for my $child ($self->visible_children) { |
1730 | for my $child ($self->visible_children) { |
1757 | my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; |
1731 | unless ($child->{c_rel}) { |
|
|
1732 | my $x = $child->{c_x}; |
|
|
1733 | my $y = $child->{c_y}; |
1758 | |
1734 | |
1759 | if ($pos eq "abs") { |
|
|
1760 | $w = _scale $size, $w, $child->{req_w}; |
|
|
1761 | $h = _scale $size, $h, $child->{req_h}; |
|
|
1762 | |
|
|
1763 | $x1 = min $x1, $x; $x2 = max $x2, $x + $w; |
1735 | $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w}; |
1764 | $y1 = min $y1, $y; $y2 = max $y2, $y + $h; |
1736 | $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h}; |
1765 | } |
1737 | } |
1766 | } |
1738 | } |
1767 | |
1739 | |
1768 | my $W = $x2 - $x1; |
1740 | my $W = $x2 - $x1; |
1769 | my $H = $y2 - $y1; |
1741 | my $H = $y2 - $y1; |
1770 | |
1742 | |
1771 | # now layout remaining widgets |
1743 | # now layout remaining widgets |
1772 | for my $child ($self->visible_children) { |
1744 | for my $child ($self->visible_children) { |
1773 | my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; |
1745 | if ($child->{c_rel}) { |
|
|
1746 | my $x = _scale $child->{c_rel}, $child->{c_x}, $W; |
|
|
1747 | my $y = _scale $child->{c_rel}, $child->{c_y}, $H; |
1774 | |
1748 | |
1775 | if ($pos ne "abs") { |
|
|
1776 | $x = _scale $pos, $x, $W; |
|
|
1777 | $y = _scale $pos, $x, $H; |
|
|
1778 | $w = _scale $size, $w, $child->{req_w}; |
|
|
1779 | $h = _scale $size, $h, $child->{req_h}; |
|
|
1780 | |
|
|
1781 | $x1 = min $x1, $x; $x2 = max $x2, $x + $w; |
1749 | $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w}; |
1782 | $y1 = min $y1, $y; $y2 = max $y2, $y + $h; |
1750 | $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h}; |
1783 | } |
1751 | } |
1784 | } |
1752 | } |
1785 | |
1753 | |
1786 | my $W = $x2 - $x1; |
1754 | my $W = $x2 - $x1; |
1787 | my $H = $y2 - $y1; |
1755 | my $H = $y2 - $y1; |
… | |
… | |
1791 | |
1759 | |
1792 | sub invoke_size_allocate { |
1760 | sub invoke_size_allocate { |
1793 | my ($self, $W, $H) = @_; |
1761 | my ($self, $W, $H) = @_; |
1794 | |
1762 | |
1795 | for my $child ($self->visible_children) { |
1763 | for my $child ($self->visible_children) { |
1796 | my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; |
1764 | my $x = _scale $child->{c_rel}, $child->{c_x}, $W; |
|
|
1765 | my $y = _scale $child->{c_rel}, $child->{c_y}, $H; |
1797 | |
1766 | |
1798 | $x = _scale $pos, $x, $W; |
1767 | $x += $child->{c_halign} * $child->{req_w}; |
1799 | $y = _scale $pos, $x, $H; |
1768 | $y += $child->{c_valign} * $child->{req_h}; |
1800 | $w = _scale $size, $w, $child->{req_w}; |
|
|
1801 | $h = _scale $size, $h, $child->{req_h}; |
|
|
1802 | |
1769 | |
1803 | $child->configure ($x, $y, $w, $h); |
1770 | $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h}); |
1804 | } |
1771 | } |
1805 | |
1772 | |
1806 | 1 |
1773 | 1 |
1807 | } |
1774 | } |
1808 | |
1775 | |
… | |
… | |
1812 | |
1779 | |
1813 | our @ISA = CFPlus::UI::Container::; |
1780 | our @ISA = CFPlus::UI::Container::; |
1814 | |
1781 | |
1815 | sub size_request { |
1782 | sub size_request { |
1816 | my ($self) = @_; |
1783 | my ($self) = @_; |
|
|
1784 | |
|
|
1785 | my @children = $self->visible_children; |
1817 | |
1786 | |
1818 | $self->{vertical} |
1787 | $self->{vertical} |
1819 | ? ( |
1788 | ? ( |
1820 | (List::Util::max map $_->{req_w}, @{$self->{children}}), |
1789 | (List::Util::max map $_->{req_w}, @children), |
1821 | (List::Util::sum map $_->{req_h}, @{$self->{children}}), |
1790 | (List::Util::sum map $_->{req_h}, @children), |
1822 | ) |
1791 | ) |
1823 | : ( |
1792 | : ( |
1824 | (List::Util::sum map $_->{req_w}, @{$self->{children}}), |
1793 | (List::Util::sum map $_->{req_w}, @children), |
1825 | (List::Util::max map $_->{req_h}, @{$self->{children}}), |
1794 | (List::Util::max map $_->{req_h}, @children), |
1826 | ) |
1795 | ) |
1827 | } |
1796 | } |
1828 | |
1797 | |
1829 | sub invoke_size_allocate { |
1798 | sub invoke_size_allocate { |
1830 | my ($self, $w, $h) = @_; |
1799 | my ($self, $w, $h) = @_; |
… | |
… | |
1954 | |
1923 | |
1955 | delete $self->{ox}; |
1924 | delete $self->{ox}; |
1956 | $self->SUPER::realloc; |
1925 | $self->SUPER::realloc; |
1957 | } |
1926 | } |
1958 | |
1927 | |
|
|
1928 | sub clear { |
|
|
1929 | my ($self) = @_; |
|
|
1930 | |
|
|
1931 | $self->set_text (""); |
|
|
1932 | } |
|
|
1933 | |
1959 | sub set_text { |
1934 | sub set_text { |
1960 | my ($self, $text) = @_; |
1935 | my ($self, $text) = @_; |
1961 | |
1936 | |
1962 | return if $self->{text} eq "T$text"; |
1937 | return if $self->{text} eq "T$text"; |
1963 | $self->{text} = "T$text"; |
1938 | $self->{text} = "T$text"; |
… | |
… | |
1986 | |
1961 | |
1987 | sub size_request { |
1962 | sub size_request { |
1988 | my ($self) = @_; |
1963 | my ($self) = @_; |
1989 | |
1964 | |
1990 | $self->{size_req} ||= do { |
1965 | $self->{size_req} ||= do { |
|
|
1966 | my ($max_w, $max_h) = $self->get_max_wh; |
|
|
1967 | |
1991 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1968 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1992 | $self->{layout}->set_width ($self->{max_w} || -1); |
1969 | $self->{layout}->set_width ($self->{max_w} || $max_w || -1); |
1993 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1970 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1994 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1971 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1995 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1972 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1996 | |
1973 | |
1997 | my ($w, $h) = $self->{layout}->size; |
1974 | my ($w, $h) = $self->{layout}->size; |
… | |
… | |
2069 | : ($self->{w} - $size->[0]) * 0.5); |
2046 | : ($self->{w} - $size->[0]) * 0.5); |
2070 | |
2047 | |
2071 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} |
2048 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} |
2072 | : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} |
2049 | : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} |
2073 | : ($self->{h} - $size->[1]) * 0.5); |
2050 | : ($self->{h} - $size->[1]) * 0.5); |
|
|
2051 | |
|
|
2052 | $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
2074 | }; |
2053 | }; |
2075 | |
2054 | |
2076 | my $w = List::Util::min $self->{w} + 4, $size->[0]; |
2055 | # unless ($self->{list}) { |
2077 | my $h = List::Util::min $self->{h} + 2, $size->[1]; |
2056 | # $self->{list} = CFPlus::OpenGL::glGenList; |
2078 | |
2057 | # CFPlus::OpenGL::glNewList $self->{list}; |
2079 | $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
2058 | # $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
|
|
2059 | # CFPlus::OpenGL::glEndList; |
|
|
2060 | # } |
|
|
2061 | # |
|
|
2062 | # CFPlus::OpenGL::glCallList $self->{list}; |
|
|
2063 | |
|
|
2064 | $self->{layout}->draw; |
2080 | } |
2065 | } |
|
|
2066 | |
|
|
2067 | #sub destroy { |
|
|
2068 | # my ($self) = @_; |
|
|
2069 | # |
|
|
2070 | # CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; |
|
|
2071 | # |
|
|
2072 | # $self->SUPER::destroy; |
|
|
2073 | #} |
2081 | |
2074 | |
2082 | ############################################################################# |
2075 | ############################################################################# |
2083 | |
2076 | |
2084 | package CFPlus::UI::EntryBase; |
2077 | package CFPlus::UI::EntryBase; |
2085 | |
2078 | |
… | |
… | |
2091 | my $class = shift; |
2084 | my $class = shift; |
2092 | |
2085 | |
2093 | $class->SUPER::new ( |
2086 | $class->SUPER::new ( |
2094 | fg => [1, 1, 1], |
2087 | fg => [1, 1, 1], |
2095 | bg => [0, 0, 0, 0.2], |
2088 | bg => [0, 0, 0, 0.2], |
|
|
2089 | outline => [0.6, 0.3, 0.1], |
2096 | active_bg => [1, 1, 1, 0.5], |
2090 | active_bg => [0, 0, 1, .2], |
2097 | active_fg => [0, 0, 0], |
2091 | active_fg => [1, 1, 1], |
|
|
2092 | active_outline => [1, 1, 0], |
2098 | can_hover => 1, |
2093 | can_hover => 1, |
2099 | can_focus => 1, |
2094 | can_focus => 1, |
2100 | valign => 0, |
2095 | valign => 0, |
2101 | can_events => 1, |
2096 | can_events => 1, |
2102 | ellipsise => 0, |
2097 | ellipsise => 0, |
… | |
… | |
2242 | glColor_premultiply @{$self->{bg}}; |
2237 | glColor_premultiply @{$self->{bg}}; |
2243 | } |
2238 | } |
2244 | |
2239 | |
2245 | glEnable GL_BLEND; |
2240 | glEnable GL_BLEND; |
2246 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2241 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2247 | glBegin GL_QUADS; |
|
|
2248 | glVertex 0 , 0; |
|
|
2249 | glVertex 0 , $self->{h}; |
|
|
2250 | glVertex $self->{w}, $self->{h}; |
2242 | glRect 0, 0, $self->{w}, $self->{h}; |
2251 | glVertex $self->{w}, 0; |
|
|
2252 | glEnd; |
|
|
2253 | glDisable GL_BLEND; |
2243 | glDisable GL_BLEND; |
2254 | |
2244 | |
2255 | $self->SUPER::_draw; |
2245 | $self->SUPER::_draw; |
2256 | |
2246 | |
2257 | #TODO: force update every cursor change :( |
2247 | #TODO: force update every cursor change :( |
… | |
… | |
2259 | |
2249 | |
2260 | unless (exists $self->{cur_h}) { |
2250 | unless (exists $self->{cur_h}) { |
2261 | my $text = substr $self->{text}, 0, $self->{cursor}; |
2251 | my $text = substr $self->{text}, 0, $self->{cursor}; |
2262 | utf8::encode $text; |
2252 | utf8::encode $text; |
2263 | |
2253 | |
2264 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) |
2254 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
2265 | } |
2255 | } |
2266 | |
2256 | |
|
|
2257 | glColor_premultiply @{$self->{active_fg}}; |
2267 | glBegin GL_LINES; |
2258 | glBegin GL_LINES; |
2268 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; |
2259 | glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy}; |
2269 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
2260 | glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
|
|
2261 | glEnd; |
|
|
2262 | |
|
|
2263 | glLineWidth 3; |
|
|
2264 | glColor @{$self->{active_outline}}; |
|
|
2265 | glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; |
|
|
2266 | glLineWidth 1; |
|
|
2267 | |
|
|
2268 | } else { |
|
|
2269 | glColor @{$self->{outline}}; |
|
|
2270 | glBegin GL_LINE_STRIP; |
|
|
2271 | glVertex .5, $self->{h} * .5; |
|
|
2272 | glVertex .5, $self->{h} - 2.5; |
|
|
2273 | glVertex $self->{w} - .5, $self->{h} - 2.5; |
|
|
2274 | glVertex $self->{w} - .5, $self->{h} * .5; |
2270 | glEnd; |
2275 | glEnd; |
2271 | } |
2276 | } |
2272 | } |
2277 | } |
2273 | |
2278 | |
2274 | ############################################################################# |
2279 | ############################################################################# |
… | |
… | |
2361 | 1 |
2366 | 1 |
2362 | } |
2367 | } |
2363 | |
2368 | |
2364 | ############################################################################# |
2369 | ############################################################################# |
2365 | |
2370 | |
|
|
2371 | package CFPlus::UI::ButtonBin; |
|
|
2372 | |
|
|
2373 | our @ISA = CFPlus::UI::Bin::; |
|
|
2374 | |
|
|
2375 | use CFPlus::OpenGL; |
|
|
2376 | |
|
|
2377 | my @tex = |
|
|
2378 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
|
|
2379 | qw(b1_button_inactive.png b1_button_active.png); |
|
|
2380 | |
|
|
2381 | sub new { |
|
|
2382 | my $class = shift; |
|
|
2383 | |
|
|
2384 | $class->SUPER::new ( |
|
|
2385 | can_hover => 1, |
|
|
2386 | align => 0, |
|
|
2387 | valign => 0, |
|
|
2388 | can_events => 1, |
|
|
2389 | @_ |
|
|
2390 | ) |
|
|
2391 | } |
|
|
2392 | |
|
|
2393 | sub invoke_button_up { |
|
|
2394 | my ($self, $ev, $x, $y) = @_; |
|
|
2395 | |
|
|
2396 | $self->emit ("activate") |
|
|
2397 | if $x >= 0 && $x < $self->{w} |
|
|
2398 | && $y >= 0 && $y < $self->{h}; |
|
|
2399 | |
|
|
2400 | 1 |
|
|
2401 | } |
|
|
2402 | |
|
|
2403 | sub _draw { |
|
|
2404 | my ($self) = @_; |
|
|
2405 | |
|
|
2406 | glEnable GL_TEXTURE_2D; |
|
|
2407 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
2408 | glColor 0, 0, 0, 1; |
|
|
2409 | |
|
|
2410 | my $tex = $tex[$GRAB == $self]; |
|
|
2411 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
|
|
2412 | |
|
|
2413 | glDisable GL_TEXTURE_2D; |
|
|
2414 | |
|
|
2415 | $self->SUPER::_draw; |
|
|
2416 | } |
|
|
2417 | |
|
|
2418 | ############################################################################# |
|
|
2419 | |
2366 | package CFPlus::UI::Button; |
2420 | package CFPlus::UI::Button; |
2367 | |
2421 | |
2368 | our @ISA = CFPlus::UI::Label::; |
2422 | our @ISA = CFPlus::UI::Label::; |
2369 | |
2423 | |
2370 | use CFPlus::OpenGL; |
2424 | use CFPlus::OpenGL; |
… | |
… | |
2474 | sub _draw { |
2528 | sub _draw { |
2475 | my ($self) = @_; |
2529 | my ($self) = @_; |
2476 | |
2530 | |
2477 | $self->SUPER::_draw; |
2531 | $self->SUPER::_draw; |
2478 | |
2532 | |
2479 | glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; |
2533 | glTranslate $self->{padding_x}, $self->{padding_y}, 0; |
2480 | |
2534 | |
2481 | my ($w, $h) = @$self{qw(w h)}; |
2535 | my ($w, $h) = @$self{qw(w h)}; |
2482 | |
2536 | |
2483 | my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; |
2537 | my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; |
2484 | |
2538 | |
… | |
… | |
2504 | sub new { |
2558 | sub new { |
2505 | my $class = shift; |
2559 | my $class = shift; |
2506 | |
2560 | |
2507 | my $self = $class->SUPER::new ( |
2561 | my $self = $class->SUPER::new ( |
2508 | can_events => 0, |
2562 | can_events => 0, |
|
|
2563 | scale => 1, |
2509 | @_, |
2564 | @_, |
2510 | ); |
2565 | ); |
2511 | |
2566 | |
2512 | $self->{path} || $self->{tex} |
2567 | $self->{path} || $self->{tex} |
2513 | or Carp::croak "'path' or 'tex' attributes required"; |
2568 | or Carp::croak "'path' or 'tex' attributes required"; |
… | |
… | |
2538 | } |
2593 | } |
2539 | |
2594 | |
2540 | sub size_request { |
2595 | sub size_request { |
2541 | my ($self) = @_; |
2596 | my ($self) = @_; |
2542 | |
2597 | |
2543 | ($self->{tex}{w}, $self->{tex}{h}) |
2598 | (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) |
2544 | } |
2599 | } |
2545 | |
2600 | |
2546 | sub _draw { |
2601 | sub _draw { |
2547 | my ($self) = @_; |
2602 | my ($self) = @_; |
2548 | |
2603 | |
… | |
… | |
2558 | } |
2613 | } |
2559 | |
2614 | |
2560 | glEnable GL_TEXTURE_2D; |
2615 | glEnable GL_TEXTURE_2D; |
2561 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2616 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2562 | |
2617 | |
2563 | $tex->draw_quad (0, 0, $w, $h); |
2618 | $tex->draw_quad_alpha (0, 0, $w, $h); |
2564 | |
2619 | |
2565 | glDisable GL_TEXTURE_2D; |
2620 | glDisable GL_TEXTURE_2D; |
2566 | } |
2621 | } |
2567 | |
2622 | |
2568 | ############################################################################# |
2623 | ############################################################################# |
… | |
… | |
2587 | align => 0, |
2642 | align => 0, |
2588 | valign => 0, |
2643 | valign => 0, |
2589 | can_events => 1, |
2644 | can_events => 1, |
2590 | @_ |
2645 | @_ |
2591 | ); |
2646 | ); |
|
|
2647 | } |
|
|
2648 | |
|
|
2649 | sub invoke_button_down { |
|
|
2650 | my ($self, $ev, $x, $y) = @_; |
|
|
2651 | |
|
|
2652 | 1 |
2592 | } |
2653 | } |
2593 | |
2654 | |
2594 | sub invoke_button_up { |
2655 | sub invoke_button_up { |
2595 | my ($self, $ev, $x, $y) = @_; |
2656 | my ($self, $ev, $x, $y) = @_; |
2596 | |
2657 | |
… | |
… | |
2736 | glDisable GL_TEXTURE_2D; |
2797 | glDisable GL_TEXTURE_2D; |
2737 | } |
2798 | } |
2738 | |
2799 | |
2739 | ############################################################################# |
2800 | ############################################################################# |
2740 | |
2801 | |
|
|
2802 | package CFPlus::UI::Progress; |
|
|
2803 | |
|
|
2804 | our @ISA = CFPlus::UI::Label::; |
|
|
2805 | |
|
|
2806 | use CFPlus::OpenGL; |
|
|
2807 | |
|
|
2808 | sub new { |
|
|
2809 | my ($class, %arg) = @_; |
|
|
2810 | |
|
|
2811 | my $self = $class->SUPER::new ( |
|
|
2812 | fg => [1, 1, 1], |
|
|
2813 | bg => [0, 0, 1, 0.2], |
|
|
2814 | bar => [0.7, 0.5, 0.1, 0.8], |
|
|
2815 | outline => [0.4, 0.3, 0], |
|
|
2816 | fontsize => 0.9, |
|
|
2817 | valign => 0, |
|
|
2818 | align => 0, |
|
|
2819 | can_events => 1, |
|
|
2820 | ellipsise => 1, |
|
|
2821 | label => "%d%%", |
|
|
2822 | %arg, |
|
|
2823 | ); |
|
|
2824 | |
|
|
2825 | $self->set_value ($arg{value} || -1); |
|
|
2826 | |
|
|
2827 | $self |
|
|
2828 | } |
|
|
2829 | |
|
|
2830 | sub set_label { |
|
|
2831 | my ($self, $label) = @_; |
|
|
2832 | |
|
|
2833 | return if $self->{label} eq $label; |
|
|
2834 | $self->{label} = $label; |
|
|
2835 | |
|
|
2836 | $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); |
|
|
2837 | } |
|
|
2838 | |
|
|
2839 | sub set_value { |
|
|
2840 | my ($self, $value) = @_; |
|
|
2841 | |
|
|
2842 | if ($self->{value} ne $value) { |
|
|
2843 | $self->{value} = $value; |
|
|
2844 | |
|
|
2845 | if ($value < 0) { |
|
|
2846 | $self->set_text ("-"); |
|
|
2847 | } else { |
|
|
2848 | $self->set_text (sprintf $self->{label}, $value * 100); |
|
|
2849 | } |
|
|
2850 | |
|
|
2851 | $self->update; |
|
|
2852 | } |
|
|
2853 | } |
|
|
2854 | |
|
|
2855 | sub _draw { |
|
|
2856 | my ($self) = @_; |
|
|
2857 | |
|
|
2858 | glEnable GL_BLEND; |
|
|
2859 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2860 | |
|
|
2861 | if ($self->{value} >= 0) { |
|
|
2862 | my $s = int 2 + ($self->{w} - 4) * $self->{value}; |
|
|
2863 | |
|
|
2864 | glColor_premultiply @{$self->{bar}}; |
|
|
2865 | glRect 2, 2, $s, $self->{h} - 2; |
|
|
2866 | glColor_premultiply @{$self->{bg}}; |
|
|
2867 | glRect $s, 2, $self->{w} - 2, $self->{h} - 2; |
|
|
2868 | } |
|
|
2869 | |
|
|
2870 | glColor_premultiply @{$self->{outline}}; |
|
|
2871 | glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; |
|
|
2872 | |
|
|
2873 | glDisable GL_BLEND; |
|
|
2874 | |
|
|
2875 | { |
|
|
2876 | local $self->{bg}; # do not draw background |
|
|
2877 | $self->SUPER::_draw; |
|
|
2878 | } |
|
|
2879 | } |
|
|
2880 | |
|
|
2881 | ############################################################################# |
|
|
2882 | |
|
|
2883 | package CFPlus::UI::ExperienceProgress; |
|
|
2884 | |
|
|
2885 | our @ISA = CFPlus::UI::Progress::; |
|
|
2886 | |
|
|
2887 | sub new { |
|
|
2888 | my ($class, %arg) = @_; |
|
|
2889 | |
|
|
2890 | my $self = $class->SUPER::new ( |
|
|
2891 | tooltip => sub { |
|
|
2892 | my ($self) = @_; |
|
|
2893 | |
|
|
2894 | sprintf "level %d\n%s points\n%s next level\n%s to go", |
|
|
2895 | $self->{lvl}, |
|
|
2896 | ::formsep ($self->{exp}), |
|
|
2897 | ::formsep ($self->{nxt}), |
|
|
2898 | ::formsep ($self->{nxt} - $self->{exp}), |
|
|
2899 | }, |
|
|
2900 | %arg |
|
|
2901 | ); |
|
|
2902 | |
|
|
2903 | $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } |
|
|
2904 | if $::CONN; |
|
|
2905 | |
|
|
2906 | $self |
|
|
2907 | } |
|
|
2908 | |
|
|
2909 | sub DESTROY { |
|
|
2910 | my ($self) = @_; |
|
|
2911 | |
|
|
2912 | delete $::CONN->{on_exp_update}{$self+0} |
|
|
2913 | if $::CONN; |
|
|
2914 | |
|
|
2915 | $self->SUPER::DESTROY; |
|
|
2916 | } |
|
|
2917 | |
|
|
2918 | sub set_value { |
|
|
2919 | my ($self, $lvl, $exp) = @_; |
|
|
2920 | |
|
|
2921 | $self->{lvl} = $lvl; |
|
|
2922 | $self->{exp} = $exp; |
|
|
2923 | |
|
|
2924 | my $v = -1; |
|
|
2925 | |
|
|
2926 | if ($::CONN && (my $table = $::CONN->{exp_table})) { |
|
|
2927 | my $l0 = $table->[$lvl - 1]; |
|
|
2928 | my $l1 = $table->[$lvl]; |
|
|
2929 | |
|
|
2930 | $self->{nxt} = $l1; |
|
|
2931 | |
|
|
2932 | $v = ($exp - $l0) / ($l1 - $l0); |
|
|
2933 | } |
|
|
2934 | |
|
|
2935 | $self->SUPER::set_value ($v); |
|
|
2936 | } |
|
|
2937 | |
|
|
2938 | ############################################################################# |
|
|
2939 | |
2741 | package CFPlus::UI::Gauge; |
2940 | package CFPlus::UI::Gauge; |
2742 | |
2941 | |
2743 | our @ISA = CFPlus::UI::VBox::; |
2942 | our @ISA = CFPlus::UI::VBox::; |
2744 | |
2943 | |
2745 | sub new { |
2944 | sub new { |
… | |
… | |
2870 | |
3069 | |
2871 | $self->SUPER::invoke_button_down ($ev, $x, $y); |
3070 | $self->SUPER::invoke_button_down ($ev, $x, $y); |
2872 | |
3071 | |
2873 | $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; |
3072 | $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; |
2874 | |
3073 | |
2875 | $self->invoke_mouse_motion ($ev, $x, $y) |
3074 | $self->invoke_mouse_motion ($ev, $x, $y); |
|
|
3075 | |
|
|
3076 | 1 |
2876 | } |
3077 | } |
2877 | |
3078 | |
2878 | sub invoke_mouse_motion { |
3079 | sub invoke_mouse_motion { |
2879 | my ($self, $ev, $x, $y) = @_; |
3080 | my ($self, $ev, $x, $y) = @_; |
2880 | |
3081 | |
… | |
… | |
2900 | |
3101 | |
2901 | my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; |
3102 | my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; |
2902 | |
3103 | |
2903 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); |
3104 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); |
2904 | |
3105 | |
2905 | ! ! $delta |
3106 | 1 |
2906 | } |
3107 | } |
2907 | |
3108 | |
2908 | sub update { |
3109 | sub update { |
2909 | my ($self) = @_; |
3110 | my ($self) = @_; |
2910 | |
3111 | |
… | |
… | |
3041 | } |
3242 | } |
3042 | |
3243 | |
3043 | sub size_request { |
3244 | sub size_request { |
3044 | my ($self) = @_; |
3245 | my ($self) = @_; |
3045 | |
3246 | |
3046 | my ($empty, $slider) = @{ $self->{children} }; |
3247 | my ($empty, $slider) = $self->visible_children; |
3047 | |
3248 | |
3048 | local $self->{children} = [$empty, $slider]; |
3249 | local $self->{children} = [$empty, $slider]; |
3049 | $self->SUPER::size_request |
3250 | $self->SUPER::size_request |
3050 | } |
3251 | } |
3051 | |
3252 | |
… | |
… | |
3240 | |
3441 | |
3241 | if ($y0 < $y + $h && $y < $y1) { |
3442 | if ($y0 < $y + $h && $y < $y1) { |
3242 | my $layout = $self->get_layout ($para); |
3443 | my $layout = $self->get_layout ($para); |
3243 | |
3444 | |
3244 | $layout->render ($para->{indent}, $y - $y0); |
3445 | $layout->render ($para->{indent}, $y - $y0); |
|
|
3446 | $layout->draw; |
3245 | |
3447 | |
3246 | if (my @w = @{ $para->{widget} }) { |
3448 | if (my @w = @{ $para->{widget} }) { |
3247 | my @s = $layout->get_shapes; |
3449 | my @s = $layout->get_shapes; |
3248 | |
3450 | |
3249 | for (@w) { |
3451 | for (@w) { |
… | |
… | |
3371 | } |
3573 | } |
3372 | |
3574 | |
3373 | sub set_tooltip_from { |
3575 | sub set_tooltip_from { |
3374 | my ($self, $widget) = @_; |
3576 | my ($self, $widget) = @_; |
3375 | |
3577 | |
3376 | $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1 |
|
|
3377 | if $widget->{tooltip} =~ /^#(.*)$/; |
|
|
3378 | |
|
|
3379 | my $tooltip = $widget->{tooltip}; |
3578 | my $tip = $widget->{tooltip}; |
|
|
3579 | $tip = $tip->($widget) if "CODE" eq ref $tip; |
|
|
3580 | |
|
|
3581 | $tip = CFPlus::Pod::section_label tooltip => $1 |
|
|
3582 | if $tip =~ /^#(.*)$/; |
3380 | |
3583 | |
3381 | if ($ENV{CFPLUS_DEBUG} & 2) { |
3584 | if ($ENV{CFPLUS_DEBUG} & 2) { |
3382 | $tooltip .= "\n\n" . (ref $widget) . "\n" |
3585 | $tip .= "\n\n" . (ref $widget) . "\n" |
3383 | . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" |
3586 | . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" |
3384 | . "req $widget->{req_w} $widget->{req_h}\n" |
3587 | . "req $widget->{req_w} $widget->{req_h}\n" |
3385 | . "visible $widget->{visible}"; |
3588 | . "visible $widget->{visible}"; |
3386 | } |
3589 | } |
3387 | |
3590 | |
3388 | $tooltip =~ s/^\n+//; |
3591 | $tip =~ s/^\n+//; |
3389 | $tooltip =~ s/\n+$//; |
3592 | $tip =~ s/\n+$//; |
3390 | |
3593 | |
3391 | $self->add (new CFPlus::UI::Label |
3594 | $self->add (new CFPlus::UI::Label |
3392 | markup => $tooltip, |
3595 | markup => $tip, |
3393 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
3596 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
3394 | fontsize => 0.8, |
3597 | fontsize => 0.8, |
3395 | style => 1, # FLAG_INVERSE |
3598 | style => 1, # FLAG_INVERSE |
3396 | ellipsise => 0, |
3599 | ellipsise => 0, |
3397 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
3600 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
… | |
… | |
3435 | } |
3638 | } |
3436 | |
3639 | |
3437 | sub _draw { |
3640 | sub _draw { |
3438 | my ($self) = @_; |
3641 | my ($self) = @_; |
3439 | |
3642 | |
3440 | glTranslate 0.375, 0.375; |
|
|
3441 | |
|
|
3442 | my ($w, $h) = @$self{qw(w h)}; |
3643 | my ($w, $h) = @$self{qw(w h)}; |
3443 | |
3644 | |
3444 | glColor 1, 0.8, 0.4; |
3645 | glColor 1, 0.8, 0.4; |
3445 | glBegin GL_QUADS; |
3646 | glRect 0, 0, $w, $h; |
3446 | glVertex 0 , 0; |
|
|
3447 | glVertex 0 , $h; |
|
|
3448 | glVertex $w, $h; |
|
|
3449 | glVertex $w, 0; |
|
|
3450 | glEnd; |
|
|
3451 | |
3647 | |
3452 | glColor 0, 0, 0; |
3648 | glColor 0, 0, 0; |
3453 | glBegin GL_LINE_LOOP; |
3649 | glRect_lineloop .5, .5, $w + .5, $h + .5; |
3454 | glVertex 0 , 0; |
|
|
3455 | glVertex 0 , $h; |
|
|
3456 | glVertex $w, $h; |
|
|
3457 | glVertex $w, 0; |
|
|
3458 | glEnd; |
|
|
3459 | |
3650 | |
3460 | glTranslate 2 - 0.375, 2 - 0.375; |
3651 | glTranslate 2, 2; |
3461 | |
3652 | |
3462 | $self->SUPER::_draw; |
3653 | $self->SUPER::_draw; |
3463 | } |
3654 | } |
3464 | |
3655 | |
3465 | ############################################################################# |
3656 | ############################################################################# |
… | |
… | |
3483 | |
3674 | |
3484 | if ($self->{anim} && $self->{animspeed}) { |
3675 | if ($self->{anim} && $self->{animspeed}) { |
3485 | CFPlus::weaken (my $widget = $self); |
3676 | CFPlus::weaken (my $widget = $self); |
3486 | |
3677 | |
3487 | $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; |
3678 | $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; |
3488 | $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed}; |
3679 | $widget->{anim_start} = $self->{animspeed} * int EV::now / $self->{animspeed}; |
3489 | $self->{timer} = Event->timer ( |
3680 | $self->{timer} = EV::timer_ns 0, 0, sub { |
3490 | parked => 1, |
|
|
3491 | cb => sub { |
|
|
3492 | return unless $::CONN && $widget; |
3681 | return unless $::CONN; |
3493 | |
3682 | |
|
|
3683 | my $w = $widget |
|
|
3684 | or return; |
|
|
3685 | |
3494 | ++$widget->{frame}; |
3686 | ++$w->{frame}; |
3495 | $widget->update_face; |
3687 | $w->update_face; |
|
|
3688 | |
|
|
3689 | # somehow, $widget can go away |
3496 | $widget->update; |
3690 | $w->update; |
3497 | |
|
|
3498 | $widget->update_timer; |
3691 | $w->update_timer; |
3499 | }, |
|
|
3500 | ); |
3692 | }; |
3501 | |
3693 | |
3502 | $self->update_face; |
3694 | $self->update_face; |
3503 | $self->update_timer; |
3695 | $self->update_timer; |
3504 | } |
3696 | } |
3505 | |
3697 | |
… | |
… | |
3510 | my ($self) = @_; |
3702 | my ($self) = @_; |
3511 | |
3703 | |
3512 | return unless $self->{timer}; |
3704 | return unless $self->{timer}; |
3513 | |
3705 | |
3514 | if ($self->{visible}) { |
3706 | if ($self->{visible}) { |
3515 | $self->{timer}->at ( |
3707 | $self->{timer}->set ( |
3516 | $self->{anim_start} |
3708 | $self->{anim_start} |
3517 | + $self->{animspeed} |
3709 | + $self->{animspeed} |
3518 | * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed} |
3710 | * int 1.5 + (EV::now - $self->{anim_start}) / $self->{animspeed} |
3519 | ); |
3711 | ); |
3520 | $self->{timer}->start; |
3712 | $self->{timer}->start; |
3521 | } else { |
3713 | } else { |
3522 | $self->{timer}->stop; |
3714 | $self->{timer}->stop; |
3523 | } |
3715 | } |
3524 | } |
3716 | } |
3525 | |
3717 | |
3526 | sub update_face { |
3718 | sub update_face { |
3527 | my ($self) = @_; |
3719 | my ($self) = @_; |
3528 | |
3720 | |
3529 | return unless $::CONN; |
|
|
3530 | |
|
|
3531 | if (my $anim = $::CONN->{anim}[$self->{anim}]) { |
|
|
3532 | if ($anim && @$anim) { |
|
|
3533 | delete $self->{wait_face}; |
|
|
3534 | $self->{face} = $anim->[ $self->{frame} % @$anim ]; |
|
|
3535 | } |
|
|
3536 | } |
|
|
3537 | } |
|
|
3538 | |
|
|
3539 | sub size_request { |
|
|
3540 | my ($self) = @_; |
|
|
3541 | |
|
|
3542 | if ($::CONN) { |
3721 | if ($::CONN) { |
3543 | if (my $faceid = $::CONN->{faceid}[$self->{face}]) { |
3722 | if (my $anim = $::CONN->{anim}[$self->{anim}]) { |
3544 | if (my $tex = $::CONN->{texture}[$faceid]) { |
3723 | if ($anim && @$anim) { |
3545 | return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); |
3724 | $self->{face} = $anim->[ $self->{frame} % @$anim ]; |
3546 | } else { |
3725 | delete $self->{face_change_cb}; |
3547 | $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { |
3726 | |
3548 | $self->realloc; |
3727 | if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) { |
|
|
3728 | unless ($tex->{name} || $tex->{loading}) { |
|
|
3729 | $tex->upload (sub { $self->reconfigure }); |
|
|
3730 | } |
3549 | }); |
3731 | } |
3550 | } |
3732 | } |
3551 | } |
3733 | } |
3552 | } |
3734 | } |
|
|
3735 | } |
|
|
3736 | |
|
|
3737 | sub size_request { |
|
|
3738 | my ($self) = @_; |
|
|
3739 | |
|
|
3740 | if ($::CONN) { |
|
|
3741 | if (my $faceid = $::CONN->{face}[$self->{face}]{id}) { |
|
|
3742 | if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) { |
|
|
3743 | if ($tex->{name}) { |
|
|
3744 | return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); |
|
|
3745 | } elsif (!$tex->{loading}) { |
|
|
3746 | $tex->upload (sub { $self->reconfigure }); |
|
|
3747 | } |
|
|
3748 | } |
|
|
3749 | |
|
|
3750 | $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure }); |
|
|
3751 | } |
|
|
3752 | } |
3553 | |
3753 | |
3554 | ($self->{size_w} || 8, $self->{size_h} || 8) |
3754 | ($self->{size_w} || 8, $self->{size_h} || 8) |
3555 | } |
3755 | } |
3556 | |
3756 | |
3557 | sub update { |
3757 | sub update { |
… | |
… | |
3571 | } |
3771 | } |
3572 | |
3772 | |
3573 | sub _draw { |
3773 | sub _draw { |
3574 | my ($self) = @_; |
3774 | my ($self) = @_; |
3575 | |
3775 | |
3576 | return unless $::CONN; |
|
|
3577 | |
|
|
3578 | $self->SUPER::_draw; |
3776 | $self->SUPER::_draw; |
3579 | |
3777 | |
3580 | my $faceid = $::CONN->{faceid}[$self->{face}] |
3778 | if (my $tex = $self->{tex}) { |
3581 | or return; |
|
|
3582 | |
|
|
3583 | my $tex = $::CONN->{texture}[$faceid]; |
|
|
3584 | |
|
|
3585 | if ($tex) { |
|
|
3586 | glEnable GL_TEXTURE_2D; |
3779 | glEnable GL_TEXTURE_2D; |
3587 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
3780 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
3588 | glColor 0, 0, 0, 1; |
3781 | glColor 0, 0, 0, 1; |
3589 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
3782 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
3590 | glDisable GL_TEXTURE_2D; |
3783 | glDisable GL_TEXTURE_2D; |
… | |
… | |
3642 | children => [ |
3835 | children => [ |
3643 | (new CFPlus::UI::Label markup => $left, expand => 1), |
3836 | (new CFPlus::UI::Label markup => $left, expand => 1), |
3644 | (new CFPlus::UI::Label markup => $right, align => +1), |
3837 | (new CFPlus::UI::Label markup => $right, align => +1), |
3645 | ], |
3838 | ], |
3646 | ; |
3839 | ; |
3647 | |
3840 | |
3648 | } else { |
3841 | } else { |
3649 | $widget = new CFPlus::UI::Label |
3842 | $widget = new CFPlus::UI::Label |
3650 | can_hover => 1, |
3843 | can_hover => 1, |
3651 | can_events => 1, |
3844 | can_events => 1, |
3652 | markup => $widget, |
3845 | markup => $widget, |
… | |
… | |
3777 | |
3970 | |
3778 | ############################################################################# |
3971 | ############################################################################# |
3779 | |
3972 | |
3780 | package CFPlus::UI::Notebook; |
3973 | package CFPlus::UI::Notebook; |
3781 | |
3974 | |
|
|
3975 | use CFPlus::OpenGL; |
|
|
3976 | |
3782 | our @ISA = CFPlus::UI::VBox::; |
3977 | our @ISA = CFPlus::UI::VBox::; |
3783 | |
3978 | |
3784 | sub new { |
3979 | sub new { |
3785 | my $class = shift; |
3980 | my $class = shift; |
3786 | |
3981 | |
3787 | my $self = $class->SUPER::new ( |
3982 | my $self = $class->SUPER::new ( |
3788 | buttonbar => (new CFPlus::UI::Buttonbar), |
3983 | buttonbar => (new CFPlus::UI::Buttonbar), |
3789 | multiplexer => (new CFPlus::UI::Multiplexer expand => 1), |
3984 | multiplexer => (new CFPlus::UI::Multiplexer expand => 1), |
|
|
3985 | active_outline => [.7, .7, 0.2], |
3790 | # filter => # will be put between multiplexer and $self |
3986 | # filter => # will be put between multiplexer and $self |
3791 | @_, |
3987 | @_, |
3792 | ); |
3988 | ); |
3793 | |
3989 | |
3794 | $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; |
3990 | $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; |
3795 | $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); |
3991 | $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); |
3796 | |
3992 | |
|
|
3993 | { |
|
|
3994 | Scalar::Util::weaken (my $wself = $self); |
|
|
3995 | |
|
|
3996 | $self->{multiplexer}->connect (c_add => sub { |
|
|
3997 | my ($mplex, $widgets) = @_; |
|
|
3998 | |
|
|
3999 | for my $child (@$widgets) { |
|
|
4000 | Scalar::Util::weaken $child; |
|
|
4001 | $child->{c_tab_} ||= do { |
|
|
4002 | my $tab = |
|
|
4003 | (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") |
|
|
4004 | ? $child->{c_tab} |
|
|
4005 | : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; |
|
|
4006 | |
|
|
4007 | $tab->connect (activate => sub { |
|
|
4008 | $wself->set_current_page ($child); |
|
|
4009 | }); |
|
|
4010 | |
|
|
4011 | $tab |
|
|
4012 | }; |
|
|
4013 | |
|
|
4014 | $self->{buttonbar}->add ($child->{c_tab_}); |
|
|
4015 | } |
|
|
4016 | }); |
|
|
4017 | |
|
|
4018 | $self->{multiplexer}->connect (c_remove => sub { |
|
|
4019 | my ($mplex, $widgets) = @_; |
|
|
4020 | |
|
|
4021 | for my $child (@$widgets) { |
|
|
4022 | $wself->{buttonbar}->remove ($child->{c_tab_}); |
|
|
4023 | } |
|
|
4024 | }); |
|
|
4025 | } |
|
|
4026 | |
3797 | $self |
4027 | $self |
3798 | } |
4028 | } |
3799 | |
4029 | |
3800 | sub add { |
4030 | sub add { |
|
|
4031 | my ($self, @widgets) = @_; |
|
|
4032 | |
|
|
4033 | $self->{multiplexer}->add (@widgets) |
|
|
4034 | } |
|
|
4035 | |
|
|
4036 | sub remove { |
|
|
4037 | my ($self, @widgets) = @_; |
|
|
4038 | |
|
|
4039 | $self->{multiplexer}->remove (@widgets) |
|
|
4040 | } |
|
|
4041 | |
|
|
4042 | sub pages { |
|
|
4043 | my ($self) = @_; |
|
|
4044 | $self->{multiplexer}->children |
|
|
4045 | } |
|
|
4046 | |
|
|
4047 | sub add_tab { |
3801 | my ($self, $title, $widget, $tooltip) = @_; |
4048 | my ($self, $title, $widget, $tooltip) = @_; |
3802 | |
4049 | |
3803 | CFPlus::weaken $self; |
4050 | $title = [$title, $tooltip] unless ref $title; |
|
|
4051 | $widget->{c_tab} = $title; |
3804 | |
4052 | |
3805 | $self->{buttonbar}->add (new CFPlus::UI::Button |
|
|
3806 | markup => $title, |
|
|
3807 | tooltip => $tooltip, |
|
|
3808 | on_activate => sub { $self->set_current_page ($widget) }, |
|
|
3809 | ); |
|
|
3810 | |
|
|
3811 | $self->{multiplexer}->add ($widget); |
4053 | $self->add ($widget); |
3812 | } |
4054 | } |
3813 | |
4055 | |
3814 | sub get_current_page { |
4056 | sub get_current_page { |
3815 | my ($self) = @_; |
4057 | my ($self) = @_; |
3816 | |
4058 | |
… | |
… | |
3820 | sub set_current_page { |
4062 | sub set_current_page { |
3821 | my ($self, $page) = @_; |
4063 | my ($self, $page) = @_; |
3822 | |
4064 | |
3823 | $self->{multiplexer}->set_current_page ($page); |
4065 | $self->{multiplexer}->set_current_page ($page); |
3824 | $self->emit (page_changed => $self->{multiplexer}{current}); |
4066 | $self->emit (page_changed => $self->{multiplexer}{current}); |
|
|
4067 | } |
|
|
4068 | |
|
|
4069 | sub _draw { |
|
|
4070 | my ($self) = @_; |
|
|
4071 | |
|
|
4072 | $self->SUPER::_draw (); |
|
|
4073 | |
|
|
4074 | if (my $cur = $self->{multiplexer}{current}) { |
|
|
4075 | if ($cur = $cur->{c_tab_}) { |
|
|
4076 | glTranslate $self->{buttonbar}{x} + $cur->{x}, |
|
|
4077 | $self->{buttonbar}{y} + $cur->{y}; |
|
|
4078 | glLineWidth 3; |
|
|
4079 | #glEnable GL_BLEND; |
|
|
4080 | #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
4081 | glColor @{$self->{active_outline}}; |
|
|
4082 | glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5; |
|
|
4083 | glLineWidth 1; |
|
|
4084 | #glDisable GL_BLEND; |
|
|
4085 | } |
|
|
4086 | } |
3825 | } |
4087 | } |
3826 | |
4088 | |
3827 | ############################################################################# |
4089 | ############################################################################# |
3828 | |
4090 | |
3829 | package CFPlus::UI::Selector; |
4091 | package CFPlus::UI::Selector; |
… | |
… | |
3861 | } |
4123 | } |
3862 | |
4124 | |
3863 | sub _set_value { |
4125 | sub _set_value { |
3864 | my ($self, $value) = @_; |
4126 | my ($self, $value) = @_; |
3865 | |
4127 | |
3866 | my ($item) = grep $_->[0] eq $value, @{ $self->{options} } |
4128 | my ($item) = grep $_->[0] eq $value, @{ $self->{options} }; |
|
|
4129 | $item ||= $self->{options}[0] |
3867 | or return; |
4130 | or return; |
3868 | |
4131 | |
3869 | $self->{value} = $item->[0]; |
4132 | $self->{value} = $item->[0]; |
3870 | $self->set_markup ("$item->[1] ⇓"); |
4133 | $self->set_markup ("$item->[1] ⇓"); |
3871 | $self->set_tooltip ($item->[2]); |
4134 | # $self->set_tooltip ($item->[2]); |
3872 | } |
4135 | } |
3873 | |
4136 | |
3874 | sub set_value { |
4137 | sub set_value { |
3875 | my ($self, $value) = @_; |
4138 | my ($self, $value) = @_; |
3876 | |
4139 | |
3877 | return unless $self->{value} ne $value; |
4140 | return unless $self->{value} ne $value; |
3878 | |
4141 | |
3879 | $self->_set_value ($value); |
4142 | $self->_set_value ($value); |
3880 | $self->emit (changed => $value); |
4143 | $self->emit (changed => $value); |
|
|
4144 | } |
|
|
4145 | |
|
|
4146 | sub set_options { |
|
|
4147 | my ($self, $options) = @_; |
|
|
4148 | |
|
|
4149 | $self->{options} = $options; |
|
|
4150 | $self->_set_value ($self->{value}); |
3881 | } |
4151 | } |
3882 | |
4152 | |
3883 | ############################################################################# |
4153 | ############################################################################# |
3884 | |
4154 | |
3885 | package CFPlus::UI::Statusbox; |
4155 | package CFPlus::UI::Statusbox; |
… | |
… | |
3894 | @_, |
4164 | @_, |
3895 | ); |
4165 | ); |
3896 | |
4166 | |
3897 | CFPlus::weaken (my $this = $self); |
4167 | CFPlus::weaken (my $this = $self); |
3898 | |
4168 | |
3899 | $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); |
4169 | $self->{timer} = EV::timer 1, 1, sub { $this->reorder }; |
3900 | |
4170 | |
3901 | $self |
4171 | $self |
3902 | } |
4172 | } |
3903 | |
4173 | |
3904 | sub reorder { |
4174 | sub reorder { |
… | |
… | |
3912 | |
4182 | |
3913 | while (my ($k, $v) = each %{ $self->{item} }) { |
4183 | while (my ($k, $v) = each %{ $self->{item} }) { |
3914 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
4184 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
3915 | } |
4185 | } |
3916 | |
4186 | |
|
|
4187 | $self->{timer}->set (1, 1); |
|
|
4188 | |
3917 | my @widgets; |
4189 | my @widgets; |
3918 | |
4190 | |
3919 | my @items = sort { |
4191 | my @items = sort { |
3920 | $a->{pri} <=> $b->{pri} |
4192 | $a->{pri} <=> $b->{pri} |
3921 | or $b->{id} <=> $a->{id} |
4193 | or $b->{id} <=> $a->{id} |
3922 | } values %{ $self->{item} }; |
4194 | } values %{ $self->{item} }; |
3923 | |
|
|
3924 | $self->{timer}->interval (1); |
|
|
3925 | |
4195 | |
3926 | my $count = 10 + 1; |
4196 | my $count = 10 + 1; |
3927 | for my $item (@items) { |
4197 | for my $item (@items) { |
3928 | last unless --$count; |
4198 | last unless --$count; |
3929 | |
4199 | |
… | |
… | |
3953 | if ((my $diff = $item->{timeout} - $NOW) < 2) { |
4223 | if ((my $diff = $item->{timeout} - $NOW) < 2) { |
3954 | $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; |
4224 | $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; |
3955 | $label->update; |
4225 | $label->update; |
3956 | $label->set_max_size (undef, $label->{req_h} * $diff) |
4226 | $label->set_max_size (undef, $label->{req_h} * $diff) |
3957 | if $diff < 1; |
4227 | if $diff < 1; |
3958 | $self->{timer}->interval (1/30); |
4228 | $self->{timer}->set (1/30, 1/30); |
3959 | } else { |
4229 | } else { |
3960 | $label->{fg}[3] = $item->{fg}[3] || 1; |
4230 | $label->{fg}[3] = $item->{fg}[3] || 1; |
3961 | } |
4231 | } |
3962 | |
4232 | |
3963 | push @widgets, $label; |
4233 | push @widgets, $label; |
… | |
… | |
4099 | } |
4369 | } |
4100 | |
4370 | |
4101 | sub update { |
4371 | sub update { |
4102 | my ($self) = @_; |
4372 | my ($self) = @_; |
4103 | |
4373 | |
4104 | $::WANT_REFRESH++; |
4374 | $::WANT_REFRESH->start; |
4105 | } |
4375 | } |
4106 | |
4376 | |
4107 | sub add { |
4377 | sub add { |
4108 | my ($self, @children) = @_; |
4378 | my ($self, @children) = @_; |
4109 | |
4379 | |