ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.388 by root, Fri Jul 20 18:30:03 2007 UTC vs.
Revision 1.443 by root, Mon Nov 12 23:25:37 2007 UTC

2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7use Event;
8 7
9use CFPlus; 8use CFPlus;
10use CFPlus::Pod; 9use CFPlus::Pod;
11use CFPlus::Texture; 10use CFPlus::Texture;
12 11
17our $TOOLTIP; 16our $TOOLTIP;
18our $BUTTON_STATE; 17our $BUTTON_STATE;
19 18
20our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
21 20
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 21our $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
53sub get_layout { 50sub 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
100sub feed_sdl_button_down_event { 97sub 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->trigger;
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->trigger;
148 } 145 }
149} 146}
150 147
151sub feed_sdl_motion_event { 148sub 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
277sub TO_JSON { 274sub TO_JSON {
278 { __widget_ref__ => $_[0]{s_id} } 275 { "\fw" => $_[0]{s_id} }
279} 276}
280 277
281sub show { 278sub 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->trigger
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
367sub 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
369sub size_request { 376sub 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->trigger;
439 } 446 }
440} 447}
441 448
442# translate global coordinates to local coordinate system 449# translate global coordinates to local coordinate system
443sub coord2local { 450sub 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.
596our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn 603our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
597 604
598sub 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
645sub _draw { 608sub _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";
649} 612}
650 613
651my $cntx;#d#
652sub DESTROY { 614sub DESTROY {
653 my ($self) = @_; 615 my ($self) = @_;
654 616
655 return if CFPlus::in_destruct; 617 return if CFPlus::in_destruct;
656 618
619 local $@;
657 eval { $self->destroy }; 620 eval { $self->destroy };
658 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 621 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
659 622
660 delete $WIDGET{$self+0}; 623 delete $WIDGET{$self+0};
661} 624}
669use strict; 632use strict;
670use CFPlus::OpenGL; 633use CFPlus::OpenGL;
671 634
672sub new { 635sub new {
673 my $class = shift; 636 my $class = shift;
674
675 # range [value, low, high, page]
676 637
677 $class->SUPER::new ( 638 $class->SUPER::new (
678 #bg => [0, 0, 0, 0.2], 639 #bg => [0, 0, 0, 0.2],
679 #active_bg => [1, 1, 1, 0.5], 640 #active_bg => [1, 1, 1, 0.5],
680 @_ 641 @_
692 my ($w, $h) = @$self{qw(w h)}; 653 my ($w, $h) = @$self{qw(w h)};
693 654
694 glEnable GL_BLEND; 655 glEnable GL_BLEND;
695 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 656 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
696 glColor_premultiply @$color; 657 glColor_premultiply @$color;
697
698 glBegin GL_QUADS;
699 glVertex 0 , 0;
700 glVertex 0 , $h;
701 glVertex $w, $h; 658 glRect 0, 0, $w, $h;
702 glVertex $w, 0;
703 glEnd;
704
705 glDisable GL_BLEND; 659 glDisable GL_BLEND;
706 } 660 }
707} 661}
708 662
709############################################################################# 663#############################################################################
741 can_events => 0, 695 can_events => 0,
742 %arg, 696 %arg,
743 ); 697 );
744 698
745 $self->add (@$children) 699 $self->add (@$children)
746 if $children; 700 if $children && @$children;
747 701
748 $self 702 $self
749} 703}
750 704
751sub realloc { 705sub realloc {
760 my ($self, @widgets) = @_; 714 my ($self, @widgets) = @_;
761 715
762 $_->set_parent ($self) 716 $_->set_parent ($self)
763 for @widgets; 717 for @widgets;
764 718
719 # TODO: only do this in widgets that need it, e.g. root, fixed
765 use sort 'stable'; 720 use sort 'stable';
766 721
767 $self->{children} = [ 722 $self->{children} = [
768 sort { $a->{z} <=> $b->{z} } 723 sort { $a->{z} <=> $b->{z} }
769 @{$self->{children}}, @widgets 724 @{$self->{children}}, @widgets
770 ]; 725 ];
771 726
772 $self->realloc; 727 $self->realloc;
728
729 $self->emit (c_add => \@widgets);
730
731 map $_+0, @widgets
773} 732}
774 733
775sub children { 734sub children {
776 @{ $_[0]{children} } 735 @{ $_[0]{children} }
777} 736}
778 737
779sub remove { 738sub remove {
780 my ($self, $child) = @_; 739 my ($self, @widgets) = @_;
781 740
741 $self->emit (c_remove => \@widgets);
742
743 for my $child (@widgets) {
782 delete $child->{parent}; 744 delete $child->{parent};
783 $child->hide; 745 $child->hide;
784
785 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 746 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
747 }
786 748
787 $self->realloc; 749 $self->realloc;
788} 750}
789 751
790sub clear { 752sub clear {
791 my ($self) = @_; 753 my ($self) = @_;
792 754
793 my $children = delete $self->{children}; 755 my $children = $self->{children};
794 $self->{children} = []; 756 $self->{children} = [];
795 757
796 for (@$children) { 758 for (@$children) {
797 delete $_->{parent}; 759 delete $_->{parent};
798 $_->hide; 760 $_->hide;
818} 780}
819 781
820sub _draw { 782sub _draw {
821 my ($self) = @_; 783 my ($self) = @_;
822 784
823 $_->draw for @{$self->{children}}; 785 $_->draw for $self->visible_children;
824} 786}
825 787
826############################################################################# 788#############################################################################
827 789
828package CFPlus::UI::Bin; 790package CFPlus::UI::Bin;
838} 800}
839 801
840sub add { 802sub add {
841 my ($self, $child) = @_; 803 my ($self, $child) = @_;
842 804
843 $self->SUPER::remove ($_) for @{ $self->{children} }; 805 $self->clear;
844 $self->SUPER::add ($child); 806 $self->SUPER::add ($child);
845} 807}
846 808
847sub remove { 809sub remove {
848 my ($self, $widget) = @_; 810 my ($self, $widget) = @_;
866 828
867 1 829 1
868} 830}
869 831
870############################################################################# 832#############################################################################
871
872# back-buffered drawing area 833# back-buffered drawing area
873 834
874package CFPlus::UI::Window; 835package CFPlus::UI::Window;
875 836
876our @ISA = CFPlus::UI::Bin::; 837our @ISA = CFPlus::UI::Bin::;
958sub size_request { 919sub size_request {
959 my ($self) = @_; 920 my ($self) = @_;
960 921
961 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 922 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
962 923
963 $w = 10 if $self->{scroll_x}; 924 $w = 1 if $self->{scroll_x};
964 $h = 10 if $self->{scroll_y}; 925 $h = 1 if $self->{scroll_y};
965 926
966 ($w, $h) 927 ($w, $h)
967} 928}
968 929
969sub invoke_size_allocate { 930sub invoke_size_allocate {
990 $self->{view_x} = $x; 951 $self->{view_x} = $x;
991 $self->{view_y} = $y; 952 $self->{view_y} = $y;
992 953
993 $self->emit (changed => $x, $y); 954 $self->emit (changed => $x, $y);
994 $self->update; 955 $self->update;
956 }
957}
958
959sub set_center {
960 my ($self, $x, $y) = @_;
961
962 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
963}
964
965sub 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);
995 } 974 }
996} 975}
997 976
998# 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
999sub coord2local { 978sub coord2local {
1046 my $child = delete $arg{child}; 1025 my $child = delete $arg{child};
1047 1026
1048 my $self; 1027 my $self;
1049 1028
1050 my $hslider = new CFPlus::UI::Slider 1029 my $hslider = new CFPlus::UI::Slider
1030 c_col => 0,
1031 c_row => 1,
1051 vertical => 0, 1032 vertical => 0,
1052 range => [0, 0, 1, 0.01], # HACK fix 1033 range => [0, 0, 1, 0.01], # HACK fix
1053 on_changed => sub { 1034 on_changed => sub {
1054 $self->{hpos} = $_[1]; 1035 $self->{hpos} = $_[1];
1055 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1036 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1056 }, 1037 },
1057 ; 1038 ;
1058 1039
1059 my $vslider = new CFPlus::UI::Slider 1040 my $vslider = new CFPlus::UI::Slider
1041 c_col => 1,
1042 c_row => 0,
1060 vertical => 1, 1043 vertical => 1,
1061 range => [0, 0, 1, 0.01], # HACK fix 1044 range => [0, 0, 1, 0.01], # HACK fix
1062 on_changed => sub { 1045 on_changed => sub {
1063 $self->{vpos} = $_[1]; 1046 $self->{vpos} = $_[1];
1064 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1047 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1075 row_expand => [1, 0], 1058 row_expand => [1, 0],
1076 %arg, 1059 %arg,
1077 ); 1060 );
1078 1061
1079 $self->{vp} = new CFPlus::UI::ViewPort 1062 $self->{vp} = new CFPlus::UI::ViewPort
1063 c_col => 0,
1064 c_row => 0,
1080 expand => 1, 1065 expand => 1,
1081 scroll_x => $self->{scroll_x}, 1066 scroll_x => $self->{scroll_x},
1082 scroll_y => $self->{scroll_y}, 1067 scroll_y => $self->{scroll_y},
1083 on_changed => sub { 1068 on_changed => sub {
1084 my ($vp, $x, $y) = @_; 1069 my ($vp, $x, $y) = @_;
1086 $vp->{parent}{hslider}->set_value ($x); 1071 $vp->{parent}{hslider}->set_value ($x);
1087 $vp->{parent}{vslider}->set_value ($y); 1072 $vp->{parent}{vslider}->set_value ($y);
1088 1073
1089 0 1074 0
1090 }, 1075 },
1076 on_size_allocate => sub {
1077 my ($vp, $w, $h) = @_;
1078 $vp->{parent}->update_slider;
1079 0
1080 },
1091 ; 1081 ;
1092 1082
1093 $self->SUPER::add_at (0, 0, $self->{vp}); 1083 $self->SUPER::add ($self->{vp});
1094 1084
1095 $self->add ($child) if $child; 1085 $self->add ($child) if $child;
1096 1086
1097 $self 1087 $self
1098} 1088}
1099
1100#TODO# update range on size_allocate depending on child
1101 1089
1102sub add { 1090sub add {
1103 my ($self, $widget) = @_; 1091 my ($self, $widget) = @_;
1104 1092
1105 $self->{vp}->add ($self->{child} = $widget); 1093 $self->{vp}->add ($self->{child} = $widget);
1106} 1094}
1107 1095
1096sub set_offset { shift->{vp}->set_offset (@_) }
1097sub set_center { shift->{vp}->set_center (@_) }
1098sub make_visible { shift->{vp}->make_visible (@_) }
1099
1108sub update_slider { 1100sub update_slider {
1109 my ($self) = @_; 1101 my ($self) = @_;
1110 1102
1111 my $child = ($self->{vp} or return)->child; 1103 my $child = ($self->{vp} or return)->child;
1112 1104
1105 if ($self->{scroll_x}) {
1113 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1106 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1114 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); 1107 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1115 1108
1116 my $visible = $w1 > $w2; 1109 my $visible = $w1 > $w2;
1117 if ($visible != $self->{hslider}{visible}) { 1110 if ($visible != $self->{hslider_visible}) {
1111 $self->{hslider_visible} = $visible;
1118 $visible ? $self->SUPER::add_at (0, 1, $self->{hslider}) 1112 $visible ? $self->SUPER::add ($self->{hslider})
1119 : $self->{hslider}->hide; 1113 : $self->SUPER::remove ($self->{hslider});
1114 }
1120 } 1115 }
1121 1116
1117 if ($self->{scroll_y}) {
1122 my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); 1118 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1123 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); 1119 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1124 1120
1125 my $visible = $h1 > $h2; 1121 my $visible = $h1 > $h2;
1126 if ($visible != $self->{vslider}{visible}) { 1122 if ($visible != $self->{vslider_visible}) {
1123 $self->{vslider_visible} = $visible;
1127 $visible ? $self->SUPER::add_at (1, 0, $self->{vslider}) 1124 $visible ? $self->SUPER::add ($self->{vslider})
1128 : $self->{vslider}->hide; 1125 : $self->SUPER::remove ($self->{vslider});
1126 }
1127 }
1128}
1129
1130sub start_dragging {
1131 my ($self, $ev) = @_;
1132
1133 $self->grab_focus;
1134
1135 my $ox = $self->{vp}{view_x};
1136 my $oy = $self->{vp}{view_y};
1137
1138 $self->{motion} = sub {
1139 my ($ev, $x, $y) = @_;
1140
1141 $ox -= $ev->{xrel};
1142 $oy -= $ev->{yrel};
1143
1144 $self->{vp}->set_offset ($ox, $oy);
1129 } 1145 };
1130}
1131
1132sub update {
1133 my ($self) = @_;
1134
1135 $self->update_slider;
1136 $self->SUPER::update;
1137} 1146}
1138 1147
1139sub invoke_mouse_wheel { 1148sub invoke_mouse_wheel {
1140 my ($self, $ev) = @_; 1149 my ($self, $ev) = @_;
1141 1150
1142 $self->{vslider}->emit (mouse_wheel => $ev) 1151 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1143 if $ev->{dy};
1144
1145 $self->{hslider}->emit (mouse_wheel => $ev) 1152 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1146 if $ev->{dx};
1147 1153
1148 1 1154 1
1149} 1155}
1150 1156
1151sub invoke_button_down { 1157sub invoke_button_down {
1152 my ($self, $ev, $x, $y) = @_; 1158 my ($self, $ev, $x, $y) = @_;
1153 1159
1154 if ($ev->{button} == 2) { 1160 if ($ev->{button} == 2) {
1155 $self->grab_focus; 1161 $self->start_dragging ($ev);
1156
1157 my $ox = $self->{vp}{view_x} + $ev->{x};
1158 my $oy = $self->{vp}{view_y} + $ev->{y};
1159
1160 $self->{motion} = sub {
1161 my ($ev, $x, $y) = @_;
1162
1163 $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y});
1164 $self->update;
1165 };
1166
1167 return 1; 1162 return 1;
1168 } 1163 }
1169 1164
1170 0 1165 0
1171} 1166}
1222 my ($w, $h) = @$self{qw(w h)}; 1217 my ($w, $h) = @$self{qw(w h)};
1223 1218
1224 glEnable GL_BLEND; 1219 glEnable GL_BLEND;
1225 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1220 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1226 glColor_premultiply @{ $self->{bg} }; 1221 glColor_premultiply @{ $self->{bg} };
1227
1228 glBegin GL_QUADS;
1229 glVertex 0 , 0;
1230 glVertex 0 , $h;
1231 glVertex $w, $h; 1222 glRect 0, 0, $w, $h;
1232 glVertex $w, 0;
1233 glEnd;
1234
1235 glDisable GL_BLEND; 1223 glDisable GL_BLEND;
1236 } 1224 }
1237 1225
1238 $self->SUPER::_draw; 1226 $self->SUPER::_draw;
1239} 1227}
1400 1388
1401sub border { 1389sub border {
1402 int $_[0]{border} * $::FONTSIZE 1390 int $_[0]{border} * $::FONTSIZE
1403} 1391}
1404 1392
1393sub 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
1405sub size_request { 1402sub size_request {
1406 my ($self) = @_; 1403 my ($self) = @_;
1407 1404
1408 $self->{title_widget}->size_request 1405 $self->{title_widget}->size_request
1409 if $self->{title_widget}; 1406 if $self->{title_widget};
1581 1578
1582############################################################################# 1579#############################################################################
1583 1580
1584package CFPlus::UI::Table; 1581package CFPlus::UI::Table;
1585 1582
1586our @ISA = CFPlus::UI::Base::; 1583our @ISA = CFPlus::UI::Container::;
1587 1584
1588use List::Util qw(max sum); 1585use List::Util qw(max sum);
1589 1586
1590use CFPlus::OpenGL; 1587use CFPlus::OpenGL;
1591 1588
1592sub new { 1589sub new {
1593 my $class = shift; 1590 my $class = shift;
1594 1591
1595 $class->SUPER::new ( 1592 $class->SUPER::new (
1596 children => [],
1597 col_expand => [], 1593 col_expand => [],
1598 row_expand => [], 1594 row_expand => [],
1599 @_, 1595 @_,
1600 ) 1596 )
1601} 1597}
1602 1598
1603sub children {
1604 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1605}
1606
1607# TODO: store row/col info in child widget and use standard add/del
1608sub add { 1599sub add {
1609 my $self = shift; 1600 my ($self, @widgets) = @_;
1610 1601
1611 Carp::cluck "please use the add_at method instead of calling add, thank you.\n";#d# 1602 for my $child (@widgets) {
1612 $self->add_at (@_); 1603 $child->{c_rowspan} ||= 1;
1604 $child->{c_colspan} ||= 1;
1605 }
1606
1607 $self->SUPER::add (@widgets);
1613} 1608}
1614 1609
1615sub add_at { 1610sub add_at {
1616 my $self = shift; 1611 my $self = shift;
1617 1612
1613 my @widgets;
1614
1618 while (@_) { 1615 while (@_) {
1619 my ($col, $row, $child) = splice @_, 0, 3, (); 1616 my ($col, $row, $child) = splice @_, 0, 3, ();
1620 1617
1621 $child->set_parent ($self); 1618 $child->{c_row} = $row;
1622 $self->{children}[$row][$col] = $child; 1619 $child->{c_col} = $col;
1623 }
1624 1620
1625 $self->{force_realloc} = 1; 1621 push @widgets, $child;
1626 $self->{force_size_alloc} = 1; 1622 }
1627 $self->realloc;
1628}
1629 1623
1630sub remove { 1624 $self->add (@widgets);
1625}
1626
1627sub get_wh {
1631 my ($self, $child) = @_; 1628 my ($self) = @_;
1632 1629
1633 for (@{ $self->{children} }) { 1630 my (@w, @h);
1634 for (@{ $_ || [] }) { 1631
1635 $_ = undef if $_ == $child; 1632 my @children = $self->children;
1633
1634 # first pass, columns
1635 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1636 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1637
1638 my $sw = sum @w[$c .. $c + $cs - 1];
1639
1640 if ($w > $sw) {
1641 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1636 } 1642 }
1637 } 1643 }
1638}
1639 1644
1640# TODO: move to container class maybe? send children a signal on removal? 1645 # second pass, rows
1641sub clear { 1646 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1642 my ($self) = @_;
1643
1644 my @children = $self->children;
1645 delete $self->{children};
1646
1647 for (@children) {
1648 delete $_->{parent};
1649 $_->hide;
1650 }
1651
1652 $self->realloc;
1653}
1654
1655sub get_wh {
1656 my ($self) = @_;
1657
1658 my (@w, @h);
1659
1660 for my $y (0 .. $#{$self->{children}}) {
1661 my $row = $self->{children}[$y]
1662 or next;
1663
1664 for my $x (0 .. $#$row) {
1665 my $widget = $row->[$x]
1666 or next;
1667 my ($w, $h) = @$widget{qw(req_w req_h)}; 1647 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1668 1648
1669 $w[$x] = max $w[$x], $w; 1649 my $sh = sum @h[$r .. $r + $rs - 1];
1670 $h[$y] = max $h[$y], $h; 1650
1651 if ($h > $sh) {
1652 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1671 } 1653 }
1672 } 1654 }
1673 1655
1674 (\@w, \@h) 1656 (\@w, \@h)
1675} 1657}
1691 my ($ws, $hs) = $self->get_wh; 1673 my ($ws, $hs) = $self->get_wh;
1692 1674
1693 my $req_w = (sum @$ws) || 1; 1675 my $req_w = (sum @$ws) || 1;
1694 my $req_h = (sum @$hs) || 1; 1676 my $req_h = (sum @$hs) || 1;
1695 1677
1696 # TODO: nicer code 1678 # now linearly scale the rows/columns to the allocated size
1697 my @col_expand = @{$self->{col_expand}}; 1679 my @col_expand = @{$self->{col_expand}};
1698 @col_expand = (1) x @$ws unless @col_expand; 1680 @col_expand = (1) x @$ws unless @col_expand;
1699 my $col_expand = (sum @col_expand) || 1; 1681 my $col_expand = (sum @col_expand) || 1;
1700 1682
1701 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1683 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1708 1690
1709 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1691 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1710 1692
1711 CFPlus::UI::harmonize $hs; 1693 CFPlus::UI::harmonize $hs;
1712 1694
1713 my $y; 1695 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1696 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1714 1697
1715 for my $r (0 .. $#{$self->{children}}) { 1698 for my $widget ($self->children) {
1716 my $row = $self->{children}[$r] 1699 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1717 or next;
1718 1700
1719 my $x = 0; 1701 $widget->configure (
1720 my $row_h = $hs->[$r]; 1702 $x[$c], $y[$r],
1703 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1721 1704 );
1722 for my $c (0 .. $#$row) {
1723 my $col_w = $ws->[$c];
1724
1725 if (my $widget = $row->[$c]) {
1726 $widget->configure ($x, $y, $col_w, $row_h);
1727 }
1728
1729 $x += $col_w;
1730 }
1731
1732 $y += $row_h;
1733 } 1705 }
1734 1706
1735 1 1707 1
1736} 1708}
1737 1709
1738sub find_widget {
1739 my ($self, $x, $y) = @_;
1740
1741 $x -= $self->{x};
1742 $y -= $self->{y};
1743
1744 my $res;
1745
1746 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1747 $res = $_->find_widget ($x, $y)
1748 and return $res;
1749 }
1750
1751 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1752}
1753
1754sub _draw {
1755 my ($self) = @_;
1756
1757 for (grep $_, @{$self->{children}}) {
1758 $_->draw for grep $_, @$_;
1759 }
1760}
1761
1762############################################################################# 1710#############################################################################
1763 1711
1764package CFPlus::UI::Fixed; 1712package CFPlus::UI::Fixed;
1765 1713
1766use List::Util qw(min max); 1714use List::Util qw(min max);
1767 1715
1768our @ISA = CFPlus::UI::Container::; 1716our @ISA = CFPlus::UI::Container::;
1769 1717
1770sub add {
1771 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1772
1773 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1774 $self->SUPER::add ($child);
1775}
1776
1777sub _scale($$$) { 1718sub _scale($$$) {
1778 my ($mode, $val, $max) = @_; 1719 my ($rel, $val, $max) = @_;
1779 1720
1780 $mode eq "abs" ? $val 1721 $rel ? $val * $max : $val
1781 : $mode eq "rel" ? $val * $max
1782 : 0
1783} 1722}
1784 1723
1785sub size_request { 1724sub size_request {
1786 my ($self) = @_; 1725 my ($self) = @_;
1787 1726
1788 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1727 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1789 1728
1790 # determine overall size by querying abs widgets 1729 # determine overall size by querying abs widgets
1791 for my $child ($self->visible_children) { 1730 for my $child ($self->visible_children) {
1792 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};
1793 1734
1794 if ($pos eq "abs") {
1795 $w = _scale $size, $w, $child->{req_w};
1796 $h = _scale $size, $h, $child->{req_h};
1797
1798 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1735 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1799 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1736 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1800 } 1737 }
1801 } 1738 }
1802 1739
1803 my $W = $x2 - $x1; 1740 my $W = $x2 - $x1;
1804 my $H = $y2 - $y1; 1741 my $H = $y2 - $y1;
1805 1742
1806 # now layout remaining widgets 1743 # now layout remaining widgets
1807 for my $child ($self->visible_children) { 1744 for my $child ($self->visible_children) {
1808 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;
1809 1748
1810 if ($pos ne "abs") {
1811 $x = _scale $pos, $x, $W;
1812 $y = _scale $pos, $x, $H;
1813 $w = _scale $size, $w, $child->{req_w};
1814 $h = _scale $size, $h, $child->{req_h};
1815
1816 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1749 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1817 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1750 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1818 } 1751 }
1819 } 1752 }
1820 1753
1821 my $W = $x2 - $x1; 1754 my $W = $x2 - $x1;
1822 my $H = $y2 - $y1; 1755 my $H = $y2 - $y1;
1826 1759
1827sub invoke_size_allocate { 1760sub invoke_size_allocate {
1828 my ($self, $W, $H) = @_; 1761 my ($self, $W, $H) = @_;
1829 1762
1830 for my $child ($self->visible_children) { 1763 for my $child ($self->visible_children) {
1831 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;
1832 1766
1833 $x = _scale $pos, $x, $W; 1767 $x += $child->{c_halign} * $child->{req_w};
1834 $y = _scale $pos, $x, $H; 1768 $y += $child->{c_valign} * $child->{req_h};
1835 $w = _scale $size, $w, $child->{req_w};
1836 $h = _scale $size, $h, $child->{req_h};
1837 1769
1838 $child->configure ($x, $y, $w, $h); 1770 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1839 } 1771 }
1840 1772
1841 1 1773 1
1842} 1774}
1843 1775
1847 1779
1848our @ISA = CFPlus::UI::Container::; 1780our @ISA = CFPlus::UI::Container::;
1849 1781
1850sub size_request { 1782sub size_request {
1851 my ($self) = @_; 1783 my ($self) = @_;
1784
1785 my @children = $self->visible_children;
1852 1786
1853 $self->{vertical} 1787 $self->{vertical}
1854 ? ( 1788 ? (
1855 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1789 (List::Util::max map $_->{req_w}, @children),
1856 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1790 (List::Util::sum map $_->{req_h}, @children),
1857 ) 1791 )
1858 : ( 1792 : (
1859 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1793 (List::Util::sum map $_->{req_w}, @children),
1860 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1794 (List::Util::max map $_->{req_h}, @children),
1861 ) 1795 )
1862} 1796}
1863 1797
1864sub invoke_size_allocate { 1798sub invoke_size_allocate {
1865 my ($self, $w, $h) = @_; 1799 my ($self, $w, $h) = @_;
1989 1923
1990 delete $self->{ox}; 1924 delete $self->{ox};
1991 $self->SUPER::realloc; 1925 $self->SUPER::realloc;
1992} 1926}
1993 1927
1928sub clear {
1929 my ($self) = @_;
1930
1931 $self->set_text ("");
1932}
1933
1994sub set_text { 1934sub set_text {
1995 my ($self, $text) = @_; 1935 my ($self, $text) = @_;
1996 1936
1997 return if $self->{text} eq "T$text"; 1937 return if $self->{text} eq "T$text";
1998 $self->{text} = "T$text"; 1938 $self->{text} = "T$text";
2021 1961
2022sub size_request { 1962sub size_request {
2023 my ($self) = @_; 1963 my ($self) = @_;
2024 1964
2025 $self->{size_req} ||= do { 1965 $self->{size_req} ||= do {
1966 my ($max_w, $max_h) = $self->get_max_wh;
1967
2026 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1968 $self->{layout}->set_font ($self->{font}) if $self->{font};
2027 $self->{layout}->set_width ($self->{max_w} || -1); 1969 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
2028 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1970 $self->{layout}->set_ellipsise ($self->{ellipsise});
2029 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1971 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
2030 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1972 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2031 1973
2032 my ($w, $h) = $self->{layout}->size; 1974 my ($w, $h) = $self->{layout}->size;
2104 : ($self->{w} - $size->[0]) * 0.5); 2046 : ($self->{w} - $size->[0]) * 0.5);
2105 2047
2106 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2048 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2107 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2049 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2108 : ($self->{h} - $size->[1]) * 0.5); 2050 : ($self->{h} - $size->[1]) * 0.5);
2051
2052 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2109 }; 2053 };
2110 2054
2111 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2055# unless ($self->{list}) {
2112 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2056# $self->{list} = CFPlus::OpenGL::glGenList;
2113 2057# CFPlus::OpenGL::glNewList $self->{list};
2114 $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;
2115} 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#}
2116 2074
2117############################################################################# 2075#############################################################################
2118 2076
2119package CFPlus::UI::EntryBase; 2077package CFPlus::UI::EntryBase;
2120 2078
2126 my $class = shift; 2084 my $class = shift;
2127 2085
2128 $class->SUPER::new ( 2086 $class->SUPER::new (
2129 fg => [1, 1, 1], 2087 fg => [1, 1, 1],
2130 bg => [0, 0, 0, 0.2], 2088 bg => [0, 0, 0, 0.2],
2089 outline => [0.6, 0.3, 0.1],
2131 active_bg => [1, 1, 1, 0.5], 2090 active_bg => [0, 0, 1, .2],
2132 active_fg => [0, 0, 0], 2091 active_fg => [1, 1, 1],
2092 active_outline => [1, 1, 0],
2133 can_hover => 1, 2093 can_hover => 1,
2134 can_focus => 1, 2094 can_focus => 1,
2135 valign => 0, 2095 valign => 0,
2136 can_events => 1, 2096 can_events => 1,
2137 ellipsise => 0, 2097 ellipsise => 0,
2277 glColor_premultiply @{$self->{bg}}; 2237 glColor_premultiply @{$self->{bg}};
2278 } 2238 }
2279 2239
2280 glEnable GL_BLEND; 2240 glEnable GL_BLEND;
2281 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2241 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2282 glBegin GL_QUADS;
2283 glVertex 0 , 0;
2284 glVertex 0 , $self->{h};
2285 glVertex $self->{w}, $self->{h}; 2242 glRect 0, 0, $self->{w}, $self->{h};
2286 glVertex $self->{w}, 0;
2287 glEnd;
2288 glDisable GL_BLEND; 2243 glDisable GL_BLEND;
2289 2244
2290 $self->SUPER::_draw; 2245 $self->SUPER::_draw;
2291 2246
2292 #TODO: force update every cursor change :( 2247 #TODO: force update every cursor change :(
2294 2249
2295 unless (exists $self->{cur_h}) { 2250 unless (exists $self->{cur_h}) {
2296 my $text = substr $self->{text}, 0, $self->{cursor}; 2251 my $text = substr $self->{text}, 0, $self->{cursor};
2297 utf8::encode $text; 2252 utf8::encode $text;
2298 2253
2299 @$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);
2300 } 2255 }
2301 2256
2257 glColor_premultiply @{$self->{active_fg}};
2302 glBegin GL_LINES; 2258 glBegin GL_LINES;
2303 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};
2304 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;
2305 glEnd; 2275 glEnd;
2306 } 2276 }
2307} 2277}
2308 2278
2309############################################################################# 2279#############################################################################
2396 1 2366 1
2397} 2367}
2398 2368
2399############################################################################# 2369#############################################################################
2400 2370
2371package CFPlus::UI::ButtonBin;
2372
2373our @ISA = CFPlus::UI::Bin::;
2374
2375use CFPlus::OpenGL;
2376
2377my @tex =
2378 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2379 qw(b1_button_inactive.png b1_button_active.png);
2380
2381sub 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
2393sub 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
2403sub _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
2401package CFPlus::UI::Button; 2420package CFPlus::UI::Button;
2402 2421
2403our @ISA = CFPlus::UI::Label::; 2422our @ISA = CFPlus::UI::Label::;
2404 2423
2405use CFPlus::OpenGL; 2424use CFPlus::OpenGL;
2509sub _draw { 2528sub _draw {
2510 my ($self) = @_; 2529 my ($self) = @_;
2511 2530
2512 $self->SUPER::_draw; 2531 $self->SUPER::_draw;
2513 2532
2514 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2533 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2515 2534
2516 my ($w, $h) = @$self{qw(w h)}; 2535 my ($w, $h) = @$self{qw(w h)};
2517 2536
2518 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;
2519 2538
2539sub new { 2558sub new {
2540 my $class = shift; 2559 my $class = shift;
2541 2560
2542 my $self = $class->SUPER::new ( 2561 my $self = $class->SUPER::new (
2543 can_events => 0, 2562 can_events => 0,
2563 scale => 1,
2544 @_, 2564 @_,
2545 ); 2565 );
2546 2566
2547 $self->{path} || $self->{tex} 2567 $self->{path} || $self->{tex}
2548 or Carp::croak "'path' or 'tex' attributes required"; 2568 or Carp::croak "'path' or 'tex' attributes required";
2573} 2593}
2574 2594
2575sub size_request { 2595sub size_request {
2576 my ($self) = @_; 2596 my ($self) = @_;
2577 2597
2578 ($self->{tex}{w}, $self->{tex}{h}) 2598 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2579} 2599}
2580 2600
2581sub _draw { 2601sub _draw {
2582 my ($self) = @_; 2602 my ($self) = @_;
2583 2603
2593 } 2613 }
2594 2614
2595 glEnable GL_TEXTURE_2D; 2615 glEnable GL_TEXTURE_2D;
2596 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2616 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2597 2617
2598 $tex->draw_quad (0, 0, $w, $h); 2618 $tex->draw_quad_alpha (0, 0, $w, $h);
2599 2619
2600 glDisable GL_TEXTURE_2D; 2620 glDisable GL_TEXTURE_2D;
2601} 2621}
2602 2622
2603############################################################################# 2623#############################################################################
2622 align => 0, 2642 align => 0,
2623 valign => 0, 2643 valign => 0,
2624 can_events => 1, 2644 can_events => 1,
2625 @_ 2645 @_
2626 ); 2646 );
2647}
2648
2649sub invoke_button_down {
2650 my ($self, $ev, $x, $y) = @_;
2651
2652 1
2627} 2653}
2628 2654
2629sub invoke_button_up { 2655sub invoke_button_up {
2630 my ($self, $ev, $x, $y) = @_; 2656 my ($self, $ev, $x, $y) = @_;
2631 2657
2771 glDisable GL_TEXTURE_2D; 2797 glDisable GL_TEXTURE_2D;
2772} 2798}
2773 2799
2774############################################################################# 2800#############################################################################
2775 2801
2802package CFPlus::UI::Progress;
2803
2804our @ISA = CFPlus::UI::Label::;
2805
2806use CFPlus::OpenGL;
2807
2808sub 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
2830sub 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
2839sub 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
2855sub _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
2883package CFPlus::UI::ExperienceProgress;
2884
2885our @ISA = CFPlus::UI::Progress::;
2886
2887sub 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
2909sub DESTROY {
2910 my ($self) = @_;
2911
2912 delete $::CONN->{on_exp_update}{$self+0}
2913 if $::CONN;
2914
2915 $self->SUPER::DESTROY;
2916}
2917
2918sub 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
2776package CFPlus::UI::Gauge; 2940package CFPlus::UI::Gauge;
2777 2941
2778our @ISA = CFPlus::UI::VBox::; 2942our @ISA = CFPlus::UI::VBox::;
2779 2943
2780sub new { 2944sub new {
2905 3069
2906 $self->SUPER::invoke_button_down ($ev, $x, $y); 3070 $self->SUPER::invoke_button_down ($ev, $x, $y);
2907 3071
2908 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3072 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2909 3073
2910 $self->invoke_mouse_motion ($ev, $x, $y) 3074 $self->invoke_mouse_motion ($ev, $x, $y);
3075
3076 1
2911} 3077}
2912 3078
2913sub invoke_mouse_motion { 3079sub invoke_mouse_motion {
2914 my ($self, $ev, $x, $y) = @_; 3080 my ($self, $ev, $x, $y) = @_;
2915 3081
2935 3101
2936 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3102 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2937 3103
2938 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3104 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2939 3105
2940 ! ! $delta 3106 1
2941} 3107}
2942 3108
2943sub update { 3109sub update {
2944 my ($self) = @_; 3110 my ($self) = @_;
2945 3111
3076} 3242}
3077 3243
3078sub size_request { 3244sub size_request {
3079 my ($self) = @_; 3245 my ($self) = @_;
3080 3246
3081 my ($empty, $slider) = @{ $self->{children} }; 3247 my ($empty, $slider) = $self->visible_children;
3082 3248
3083 local $self->{children} = [$empty, $slider]; 3249 local $self->{children} = [$empty, $slider];
3084 $self->SUPER::size_request 3250 $self->SUPER::size_request
3085} 3251}
3086 3252
3275 3441
3276 if ($y0 < $y + $h && $y < $y1) { 3442 if ($y0 < $y + $h && $y < $y1) {
3277 my $layout = $self->get_layout ($para); 3443 my $layout = $self->get_layout ($para);
3278 3444
3279 $layout->render ($para->{indent}, $y - $y0); 3445 $layout->render ($para->{indent}, $y - $y0);
3446 $layout->draw;
3280 3447
3281 if (my @w = @{ $para->{widget} }) { 3448 if (my @w = @{ $para->{widget} }) {
3282 my @s = $layout->get_shapes; 3449 my @s = $layout->get_shapes;
3283 3450
3284 for (@w) { 3451 for (@w) {
3406} 3573}
3407 3574
3408sub set_tooltip_from { 3575sub set_tooltip_from {
3409 my ($self, $widget) = @_; 3576 my ($self, $widget) = @_;
3410 3577
3411 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3412 if $widget->{tooltip} =~ /^#(.*)$/;
3413
3414 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 =~ /^#(.*)$/;
3415 3583
3416 if ($ENV{CFPLUS_DEBUG} & 2) { 3584 if ($ENV{CFPLUS_DEBUG} & 2) {
3417 $tooltip .= "\n\n" . (ref $widget) . "\n" 3585 $tip .= "\n\n" . (ref $widget) . "\n"
3418 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3586 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3419 . "req $widget->{req_w} $widget->{req_h}\n" 3587 . "req $widget->{req_w} $widget->{req_h}\n"
3420 . "visible $widget->{visible}"; 3588 . "visible $widget->{visible}";
3421 } 3589 }
3422 3590
3423 $tooltip =~ s/^\n+//; 3591 $tip =~ s/^\n+//;
3424 $tooltip =~ s/\n+$//; 3592 $tip =~ s/\n+$//;
3425 3593
3426 $self->add (new CFPlus::UI::Label 3594 $self->add (new CFPlus::UI::Label
3427 markup => $tooltip, 3595 markup => $tip,
3428 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3596 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3429 fontsize => 0.8, 3597 fontsize => 0.8,
3430 style => 1, # FLAG_INVERSE 3598 style => 1, # FLAG_INVERSE
3431 ellipsise => 0, 3599 ellipsise => 0,
3432 font => ($widget->{tooltip_font} || $::FONT_PROP), 3600 font => ($widget->{tooltip_font} || $::FONT_PROP),
3470} 3638}
3471 3639
3472sub _draw { 3640sub _draw {
3473 my ($self) = @_; 3641 my ($self) = @_;
3474 3642
3475 glTranslate 0.375, 0.375;
3476
3477 my ($w, $h) = @$self{qw(w h)}; 3643 my ($w, $h) = @$self{qw(w h)};
3478 3644
3479 glColor 1, 0.8, 0.4; 3645 glColor 1, 0.8, 0.4;
3480 glBegin GL_QUADS; 3646 glRect 0, 0, $w, $h;
3481 glVertex 0 , 0;
3482 glVertex 0 , $h;
3483 glVertex $w, $h;
3484 glVertex $w, 0;
3485 glEnd;
3486 3647
3487 glColor 0, 0, 0; 3648 glColor 0, 0, 0;
3488 glBegin GL_LINE_LOOP; 3649 glRect_lineloop .5, .5, $w + .5, $h + .5;
3489 glVertex 0 , 0;
3490 glVertex 0 , $h;
3491 glVertex $w, $h;
3492 glVertex $w, 0;
3493 glEnd;
3494 3650
3495 glTranslate 2 - 0.375, 2 - 0.375; 3651 glTranslate 2, 2;
3496 3652
3497 $self->SUPER::_draw; 3653 $self->SUPER::_draw;
3498} 3654}
3499 3655
3500############################################################################# 3656#############################################################################
3518 3674
3519 if ($self->{anim} && $self->{animspeed}) { 3675 if ($self->{anim} && $self->{animspeed}) {
3520 CFPlus::weaken (my $widget = $self); 3676 CFPlus::weaken (my $widget = $self);
3521 3677
3522 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3678 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3523 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed}; 3679 $widget->{anim_start} = $self->{animspeed} * int EV::now / $self->{animspeed};
3524 $self->{timer} = Event->timer ( 3680 $self->{timer} = EV::timer_ns 0, 0, sub {
3525 parked => 1,
3526 cb => sub {
3527 return unless $::CONN && $widget; 3681 return unless $::CONN;
3528 3682
3683 my $w = $widget
3684 or return;
3685
3529 ++$widget->{frame}; 3686 ++$w->{frame};
3530 $widget->update_face; 3687 $w->update_face;
3688
3689 # somehow, $widget can go away
3531 $widget->update; 3690 $w->update;
3532
3533 $widget->update_timer; 3691 $w->update_timer;
3534 },
3535 ); 3692 };
3536 3693
3537 $self->update_face; 3694 $self->update_face;
3538 $self->update_timer; 3695 $self->update_timer;
3539 } 3696 }
3540 3697
3545 my ($self) = @_; 3702 my ($self) = @_;
3546 3703
3547 return unless $self->{timer}; 3704 return unless $self->{timer};
3548 3705
3549 if ($self->{visible}) { 3706 if ($self->{visible}) {
3550 $self->{timer}->at ( 3707 $self->{timer}->set (
3551 $self->{anim_start} 3708 $self->{anim_start}
3552 + $self->{animspeed} 3709 + $self->{animspeed}
3553 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed} 3710 * int 1.5 + (EV::now - $self->{anim_start}) / $self->{animspeed}
3554 ); 3711 );
3555 $self->{timer}->start; 3712 $self->{timer}->start;
3556 } else { 3713 } else {
3557 $self->{timer}->stop; 3714 $self->{timer}->stop;
3558 } 3715 }
3559} 3716}
3560 3717
3561sub update_face { 3718sub update_face {
3562 my ($self) = @_; 3719 my ($self) = @_;
3563 3720
3564 return unless $::CONN;
3565
3566 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3567 if ($anim && @$anim) {
3568 delete $self->{wait_face};
3569 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3570 }
3571 }
3572}
3573
3574sub size_request {
3575 my ($self) = @_;
3576
3577 if ($::CONN) { 3721 if ($::CONN) {
3578 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3722 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3579 if (my $tex = $::CONN->{texture}[$faceid]) { 3723 if ($anim && @$anim) {
3580 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3724 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3581 } else { 3725 delete $self->{face_change_cb};
3582 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3726
3583 $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 }
3584 }); 3731 }
3585 } 3732 }
3586 } 3733 }
3587 } 3734 }
3735}
3736
3737sub 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 }
3588 3753
3589 ($self->{size_w} || 8, $self->{size_h} || 8) 3754 ($self->{size_w} || 8, $self->{size_h} || 8)
3590} 3755}
3591 3756
3592sub update { 3757sub update {
3606} 3771}
3607 3772
3608sub _draw { 3773sub _draw {
3609 my ($self) = @_; 3774 my ($self) = @_;
3610 3775
3611 return unless $::CONN;
3612
3613 $self->SUPER::_draw; 3776 $self->SUPER::_draw;
3614 3777
3615 my $faceid = $::CONN->{faceid}[$self->{face}] 3778 if (my $tex = $self->{tex}) {
3616 or return;
3617
3618 my $tex = $::CONN->{texture}[$faceid];
3619
3620 if ($tex) {
3621 glEnable GL_TEXTURE_2D; 3779 glEnable GL_TEXTURE_2D;
3622 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3780 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3623 glColor 0, 0, 0, 1; 3781 glColor 0, 0, 0, 1;
3624 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3782 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3625 glDisable GL_TEXTURE_2D; 3783 glDisable GL_TEXTURE_2D;
3677 children => [ 3835 children => [
3678 (new CFPlus::UI::Label markup => $left, expand => 1), 3836 (new CFPlus::UI::Label markup => $left, expand => 1),
3679 (new CFPlus::UI::Label markup => $right, align => +1), 3837 (new CFPlus::UI::Label markup => $right, align => +1),
3680 ], 3838 ],
3681 ; 3839 ;
3682 3840
3683 } else { 3841 } else {
3684 $widget = new CFPlus::UI::Label 3842 $widget = new CFPlus::UI::Label
3685 can_hover => 1, 3843 can_hover => 1,
3686 can_events => 1, 3844 can_events => 1,
3687 markup => $widget, 3845 markup => $widget,
3812 3970
3813############################################################################# 3971#############################################################################
3814 3972
3815package CFPlus::UI::Notebook; 3973package CFPlus::UI::Notebook;
3816 3974
3975use CFPlus::OpenGL;
3976
3817our @ISA = CFPlus::UI::VBox::; 3977our @ISA = CFPlus::UI::VBox::;
3818 3978
3819sub new { 3979sub new {
3820 my $class = shift; 3980 my $class = shift;
3821 3981
3822 my $self = $class->SUPER::new ( 3982 my $self = $class->SUPER::new (
3823 buttonbar => (new CFPlus::UI::Buttonbar), 3983 buttonbar => (new CFPlus::UI::Buttonbar),
3824 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3984 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3985 active_outline => [.7, .7, 0.2],
3825 # filter => # will be put between multiplexer and $self 3986 # filter => # will be put between multiplexer and $self
3826 @_, 3987 @_,
3827 ); 3988 );
3828 3989
3829 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3990 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3830 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3991 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3831 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
3832 $self 4027 $self
3833} 4028}
3834 4029
3835sub add { 4030sub add {
4031 my ($self, @widgets) = @_;
4032
4033 $self->{multiplexer}->add (@widgets)
4034}
4035
4036sub remove {
4037 my ($self, @widgets) = @_;
4038
4039 $self->{multiplexer}->remove (@widgets)
4040}
4041
4042sub pages {
4043 my ($self) = @_;
4044 $self->{multiplexer}->children
4045}
4046
4047sub add_tab {
3836 my ($self, $title, $widget, $tooltip) = @_; 4048 my ($self, $title, $widget, $tooltip) = @_;
3837 4049
3838 CFPlus::weaken $self; 4050 $title = [$title, $tooltip] unless ref $title;
4051 $widget->{c_tab} = $title;
3839 4052
3840 $self->{buttonbar}->add (new CFPlus::UI::Button
3841 markup => $title,
3842 tooltip => $tooltip,
3843 on_activate => sub { $self->set_current_page ($widget) },
3844 );
3845
3846 $self->{multiplexer}->add ($widget); 4053 $self->add ($widget);
3847} 4054}
3848 4055
3849sub get_current_page { 4056sub get_current_page {
3850 my ($self) = @_; 4057 my ($self) = @_;
3851 4058
3855sub set_current_page { 4062sub set_current_page {
3856 my ($self, $page) = @_; 4063 my ($self, $page) = @_;
3857 4064
3858 $self->{multiplexer}->set_current_page ($page); 4065 $self->{multiplexer}->set_current_page ($page);
3859 $self->emit (page_changed => $self->{multiplexer}{current}); 4066 $self->emit (page_changed => $self->{multiplexer}{current});
4067}
4068
4069sub _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 }
3860} 4087}
3861 4088
3862############################################################################# 4089#############################################################################
3863 4090
3864package CFPlus::UI::Selector; 4091package CFPlus::UI::Selector;
3896} 4123}
3897 4124
3898sub _set_value { 4125sub _set_value {
3899 my ($self, $value) = @_; 4126 my ($self, $value) = @_;
3900 4127
3901 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4128 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4129 $item ||= $self->{options}[0]
3902 or return; 4130 or return;
3903 4131
3904 $self->{value} = $item->[0]; 4132 $self->{value} = $item->[0];
3905 $self->set_markup ("$item->[1] ⇓"); 4133 $self->set_markup ("$item->[1] ⇓");
3906 $self->set_tooltip ($item->[2]); 4134# $self->set_tooltip ($item->[2]);
3907} 4135}
3908 4136
3909sub set_value { 4137sub set_value {
3910 my ($self, $value) = @_; 4138 my ($self, $value) = @_;
3911 4139
3912 return unless $self->{value} ne $value; 4140 return unless $self->{value} ne $value;
3913 4141
3914 $self->_set_value ($value); 4142 $self->_set_value ($value);
3915 $self->emit (changed => $value); 4143 $self->emit (changed => $value);
4144}
4145
4146sub set_options {
4147 my ($self, $options) = @_;
4148
4149 $self->{options} = $options;
4150 $self->_set_value ($self->{value});
3916} 4151}
3917 4152
3918############################################################################# 4153#############################################################################
3919 4154
3920package CFPlus::UI::Statusbox; 4155package CFPlus::UI::Statusbox;
3929 @_, 4164 @_,
3930 ); 4165 );
3931 4166
3932 CFPlus::weaken (my $this = $self); 4167 CFPlus::weaken (my $this = $self);
3933 4168
3934 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4169 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3935 4170
3936 $self 4171 $self
3937} 4172}
3938 4173
3939sub reorder { 4174sub reorder {
3947 4182
3948 while (my ($k, $v) = each %{ $self->{item} }) { 4183 while (my ($k, $v) = each %{ $self->{item} }) {
3949 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4184 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3950 } 4185 }
3951 4186
4187 $self->{timer}->set (1, 1);
4188
3952 my @widgets; 4189 my @widgets;
3953 4190
3954 my @items = sort { 4191 my @items = sort {
3955 $a->{pri} <=> $b->{pri} 4192 $a->{pri} <=> $b->{pri}
3956 or $b->{id} <=> $a->{id} 4193 or $b->{id} <=> $a->{id}
3957 } values %{ $self->{item} }; 4194 } values %{ $self->{item} };
3958
3959 $self->{timer}->interval (1);
3960 4195
3961 my $count = 10 + 1; 4196 my $count = 10 + 1;
3962 for my $item (@items) { 4197 for my $item (@items) {
3963 last unless --$count; 4198 last unless --$count;
3964 4199
3988 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4223 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3989 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4224 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3990 $label->update; 4225 $label->update;
3991 $label->set_max_size (undef, $label->{req_h} * $diff) 4226 $label->set_max_size (undef, $label->{req_h} * $diff)
3992 if $diff < 1; 4227 if $diff < 1;
3993 $self->{timer}->interval (1/30); 4228 $self->{timer}->set (1/30, 1/30);
3994 } else { 4229 } else {
3995 $label->{fg}[3] = $item->{fg}[3] || 1; 4230 $label->{fg}[3] = $item->{fg}[3] || 1;
3996 } 4231 }
3997 4232
3998 push @widgets, $label; 4233 push @widgets, $label;
4134} 4369}
4135 4370
4136sub update { 4371sub update {
4137 my ($self) = @_; 4372 my ($self) = @_;
4138 4373
4139 $::WANT_REFRESH++; 4374 $::WANT_REFRESH->start;
4140} 4375}
4141 4376
4142sub add { 4377sub add {
4143 my ($self, @children) = @_; 4378 my ($self, @children) = @_;
4144 4379
4181 while ($self->{refresh_hook}) { 4416 while ($self->{refresh_hook}) {
4182 $_->() 4417 $_->()
4183 for values %{delete $self->{refresh_hook}}; 4418 for values %{delete $self->{refresh_hook}};
4184 } 4419 }
4185 4420
4186 if ($self->{realloc}) { 4421 while ($self->{realloc}) {
4187 my %queue; 4422 my %queue;
4188 my @queue; 4423 my @queue;
4189 my $widget; 4424 my $widget;
4190 4425
4191 outer: 4426 outer:
4238 } 4473 }
4239 } 4474 }
4240 4475
4241 delete $self->{realloc}{$widget+0}; 4476 delete $self->{realloc}{$widget+0};
4242 } 4477 }
4243 }
4244 4478
4245 while (my $size_alloc = delete $self->{size_alloc}) { 4479 while (my $size_alloc = delete $self->{size_alloc}) {
4246 my @queue = sort { $b->{visible} <=> $a->{visible} } 4480 my @queue = sort { $a->{visible} <=> $b->{visible} }
4247 values %$size_alloc; 4481 values %$size_alloc;
4248 4482
4249 while () { 4483 while () {
4250 my $widget = pop @queue || last; 4484 my $widget = pop @queue || last;
4251 4485
4252 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4486 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4253 4487
4254 $w = 0 if $w < 0;
4255 $h = 0 if $h < 0;
4256
4257 $w = max $widget->{min_w}, $w; 4488 $w = max $widget->{min_w}, $w;
4258 $h = max $widget->{min_h}, $h; 4489 $h = max $widget->{min_h}, $h;
4259 4490
4260# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4491# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4261# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4492# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4262 4493
4263 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4494 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4264 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4495 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4265 4496
4266 $w = int $w + 0.5; 4497 $w = int $w + 0.5;
4267 $h = int $h + 0.5; 4498 $h = int $h + 0.5;
4268 4499
4269 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4500 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
4270 $widget->{old_w} = $widget->{w}; 4501 $widget->{old_w} = $widget->{w};
4271 $widget->{old_h} = $widget->{h}; 4502 $widget->{old_h} = $widget->{h};
4272 4503
4273 $widget->{w} = $w; 4504 $widget->{w} = $w;
4274 $widget->{h} = $h; 4505 $widget->{h} = $h;
4275 4506
4276 $widget->emit (size_allocate => $w, $h); 4507 $widget->emit (size_allocate => $w, $h);
4508 }
4277 } 4509 }
4278 } 4510 }
4279 } 4511 }
4280 4512
4281 while ($self->{post_alloc_hook}) { 4513 while ($self->{post_alloc_hook}) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines