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.391 by root, Sat Jul 21 13:33:10 2007 UTC vs.
Revision 1.447 by root, Sat Dec 22 06:50:45 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->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
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->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
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->invoke;
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 {
444 my ($self, $x, $y) = @_; 451 my ($self, $x, $y) = @_;
445 452
446 Carp::confess unless $self->{parent};#d# 453 return (undef, undef) unless $self->{parent};
447 454
448 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 455 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
449} 456}
450 457
451# translate local coordinates to global coordinate system 458# translate local coordinates to global coordinate system
452sub coord2global { 459sub coord2global {
453 my ($self, $x, $y) = @_; 460 my ($self, $x, $y) = @_;
454 461
455 Carp::confess unless $self->{parent};#d# 462 return (undef, undef) unless $self->{parent};
456 463
457 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 464 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
458} 465}
459 466
460sub invoke_focus_in { 467sub invoke_focus_in {
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#############################################################################
769 sort { $a->{z} <=> $b->{z} } 723 sort { $a->{z} <=> $b->{z} }
770 @{$self->{children}}, @widgets 724 @{$self->{children}}, @widgets
771 ]; 725 ];
772 726
773 $self->realloc; 727 $self->realloc;
728
729 $self->emit (c_add => \@widgets);
730
731 map $_+0, @widgets
774} 732}
775 733
776sub children { 734sub children {
777 @{ $_[0]{children} } 735 @{ $_[0]{children} }
778} 736}
779 737
780sub remove { 738sub remove {
781 my ($self, $child) = @_; 739 my ($self, @widgets) = @_;
782 740
741 $self->emit (c_remove => \@widgets);
742
743 for my $child (@widgets) {
783 delete $child->{parent}; 744 delete $child->{parent};
784 $child->hide; 745 $child->hide;
785
786 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 746 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
747 }
787 748
788 $self->realloc; 749 $self->realloc;
789} 750}
790 751
791sub clear { 752sub clear {
792 my ($self) = @_; 753 my ($self) = @_;
793 754
794 my $children = delete $self->{children}; 755 my $children = $self->{children};
795 $self->{children} = []; 756 $self->{children} = [];
796 757
797 for (@$children) { 758 for (@$children) {
798 delete $_->{parent}; 759 delete $_->{parent};
799 $_->hide; 760 $_->hide;
819} 780}
820 781
821sub _draw { 782sub _draw {
822 my ($self) = @_; 783 my ($self) = @_;
823 784
824 $_->draw for @{$self->{children}}; 785 $_->draw for $self->visible_children;
825} 786}
826 787
827############################################################################# 788#############################################################################
828 789
829package CFPlus::UI::Bin; 790package CFPlus::UI::Bin;
839} 800}
840 801
841sub add { 802sub add {
842 my ($self, $child) = @_; 803 my ($self, $child) = @_;
843 804
844 $self->SUPER::remove ($_) for @{ $self->{children} }; 805 $self->clear;
845 $self->SUPER::add ($child); 806 $self->SUPER::add ($child);
846} 807}
847 808
848sub remove { 809sub remove {
849 my ($self, $widget) = @_; 810 my ($self, $widget) = @_;
867 828
868 1 829 1
869} 830}
870 831
871############################################################################# 832#############################################################################
872
873# back-buffered drawing area 833# back-buffered drawing area
874 834
875package CFPlus::UI::Window; 835package CFPlus::UI::Window;
876 836
877our @ISA = CFPlus::UI::Bin::; 837our @ISA = CFPlus::UI::Bin::;
991 $self->{view_x} = $x; 951 $self->{view_x} = $x;
992 $self->{view_y} = $y; 952 $self->{view_y} = $y;
993 953
994 $self->emit (changed => $x, $y); 954 $self->emit (changed => $x, $y);
995 $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);
996 } 974 }
997} 975}
998 976
999# 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
1000sub coord2local { 978sub coord2local {
1047 my $child = delete $arg{child}; 1025 my $child = delete $arg{child};
1048 1026
1049 my $self; 1027 my $self;
1050 1028
1051 my $hslider = new CFPlus::UI::Slider 1029 my $hslider = new CFPlus::UI::Slider
1052 col => 0, 1030 c_col => 0,
1053 row => 1, 1031 c_row => 1,
1054 vertical => 0, 1032 vertical => 0,
1055 range => [0, 0, 1, 0.01], # HACK fix 1033 range => [0, 0, 1, 0.01], # HACK fix
1056 on_changed => sub { 1034 on_changed => sub {
1057 $self->{hpos} = $_[1]; 1035 $self->{hpos} = $_[1];
1058 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1036 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1059 }, 1037 },
1060 ; 1038 ;
1061 1039
1062 my $vslider = new CFPlus::UI::Slider 1040 my $vslider = new CFPlus::UI::Slider
1063 col => 1, 1041 c_col => 1,
1064 row => 0, 1042 c_row => 0,
1065 vertical => 1, 1043 vertical => 1,
1066 range => [0, 0, 1, 0.01], # HACK fix 1044 range => [0, 0, 1, 0.01], # HACK fix
1067 on_changed => sub { 1045 on_changed => sub {
1068 $self->{vpos} = $_[1]; 1046 $self->{vpos} = $_[1];
1069 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1047 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1080 row_expand => [1, 0], 1058 row_expand => [1, 0],
1081 %arg, 1059 %arg,
1082 ); 1060 );
1083 1061
1084 $self->{vp} = new CFPlus::UI::ViewPort 1062 $self->{vp} = new CFPlus::UI::ViewPort
1085 col => 0, 1063 c_col => 0,
1086 row => 0, 1064 c_row => 0,
1087 expand => 1, 1065 expand => 1,
1088 scroll_x => $self->{scroll_x}, 1066 scroll_x => $self->{scroll_x},
1089 scroll_y => $self->{scroll_y}, 1067 scroll_y => $self->{scroll_y},
1090 on_changed => sub { 1068 on_changed => sub {
1091 my ($vp, $x, $y) = @_; 1069 my ($vp, $x, $y) = @_;
1093 $vp->{parent}{hslider}->set_value ($x); 1071 $vp->{parent}{hslider}->set_value ($x);
1094 $vp->{parent}{vslider}->set_value ($y); 1072 $vp->{parent}{vslider}->set_value ($y);
1095 1073
1096 0 1074 0
1097 }, 1075 },
1076 on_size_allocate => sub {
1077 my ($vp, $w, $h) = @_;
1078 $vp->{parent}->update_slider;
1079 0
1080 },
1098 ; 1081 ;
1099 1082
1100 $self->SUPER::add ($self->{vp}); 1083 $self->SUPER::add ($self->{vp});
1101 1084
1102 $self->add ($child) if $child; 1085 $self->add ($child) if $child;
1103 1086
1104 $self 1087 $self
1105} 1088}
1106
1107#TODO# update range on size_allocate depending on child?
1108 1089
1109sub add { 1090sub add {
1110 my ($self, $widget) = @_; 1091 my ($self, $widget) = @_;
1111 1092
1112 $self->{vp}->add ($self->{child} = $widget); 1093 $self->{vp}->add ($self->{child} = $widget);
1113} 1094}
1114 1095
1096sub set_offset { shift->{vp}->set_offset (@_) }
1097sub set_center { shift->{vp}->set_center (@_) }
1098sub make_visible { shift->{vp}->make_visible (@_) }
1099
1115sub update_slider { 1100sub update_slider {
1116 my ($self) = @_; 1101 my ($self) = @_;
1117 1102
1118 my $child = ($self->{vp} or return)->child; 1103 my $child = ($self->{vp} or return)->child;
1119 1104
1105 if ($self->{scroll_x}) {
1120 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1106 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1121 $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]);
1122 1108
1123 my $visible = $w1 > $w2; 1109 my $visible = $w1 > $w2;
1124 if ($visible != $self->{hslider}{visible}) { 1110 if ($visible != $self->{hslider_visible}) {
1111 $self->{hslider_visible} = $visible;
1125 $visible ? $self->SUPER::add ($self->{hslider}) 1112 $visible ? $self->SUPER::add ($self->{hslider})
1126 : $self->SUPER::remove ($self->{hslider}); 1113 : $self->SUPER::remove ($self->{hslider});
1114 }
1127 } 1115 }
1128 1116
1117 if ($self->{scroll_y}) {
1129 my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); 1118 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1130 $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]);
1131 1120
1132 my $visible = $h1 > $h2; 1121 my $visible = $h1 > $h2;
1133 if ($visible != $self->{vslider}{visible}) { 1122 if ($visible != $self->{vslider_visible}) {
1123 $self->{vslider_visible} = $visible;
1134 $visible ? $self->SUPER::add ($self->{vslider}) 1124 $visible ? $self->SUPER::add ($self->{vslider})
1135 : $self->SUPER::remove ($self->{vslider}); 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);
1136 } 1145 };
1137}
1138
1139sub update {
1140 my ($self) = @_;
1141
1142 $self->update_slider;
1143 $self->SUPER::update;
1144} 1146}
1145 1147
1146sub invoke_mouse_wheel { 1148sub invoke_mouse_wheel {
1147 my ($self, $ev) = @_; 1149 my ($self, $ev) = @_;
1148 1150
1149 $self->{vslider}->emit (mouse_wheel => $ev) 1151 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1150 if $ev->{dy};
1151
1152 $self->{hslider}->emit (mouse_wheel => $ev) 1152 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1153 if $ev->{dx};
1154 1153
1155 1 1154 1
1156} 1155}
1157 1156
1158sub invoke_button_down { 1157sub invoke_button_down {
1159 my ($self, $ev, $x, $y) = @_; 1158 my ($self, $ev, $x, $y) = @_;
1160 1159
1161 if ($ev->{button} == 2) { 1160 if ($ev->{button} == 2) {
1162 $self->grab_focus; 1161 $self->start_dragging ($ev);
1163
1164 my $ox = $self->{vp}{view_x} + $ev->{x};
1165 my $oy = $self->{vp}{view_y} + $ev->{y};
1166
1167 $self->{motion} = sub {
1168 my ($ev, $x, $y) = @_;
1169
1170 $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y});
1171 $self->update;
1172 };
1173
1174 return 1; 1162 return 1;
1175 } 1163 }
1176 1164
1177 0 1165 0
1178} 1166}
1229 my ($w, $h) = @$self{qw(w h)}; 1217 my ($w, $h) = @$self{qw(w h)};
1230 1218
1231 glEnable GL_BLEND; 1219 glEnable GL_BLEND;
1232 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1220 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1233 glColor_premultiply @{ $self->{bg} }; 1221 glColor_premultiply @{ $self->{bg} };
1234
1235 glBegin GL_QUADS;
1236 glVertex 0 , 0;
1237 glVertex 0 , $h;
1238 glVertex $w, $h; 1222 glRect 0, 0, $w, $h;
1239 glVertex $w, 0;
1240 glEnd;
1241
1242 glDisable GL_BLEND; 1223 glDisable GL_BLEND;
1243 } 1224 }
1244 1225
1245 $self->SUPER::_draw; 1226 $self->SUPER::_draw;
1246} 1227}
1407 1388
1408sub border { 1389sub border {
1409 int $_[0]{border} * $::FONTSIZE 1390 int $_[0]{border} * $::FONTSIZE
1410} 1391}
1411 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
1412sub size_request { 1402sub size_request {
1413 my ($self) = @_; 1403 my ($self) = @_;
1414 1404
1415 $self->{title_widget}->size_request 1405 $self->{title_widget}->size_request
1416 if $self->{title_widget}; 1406 if $self->{title_widget};
1608 1598
1609sub add { 1599sub add {
1610 my ($self, @widgets) = @_; 1600 my ($self, @widgets) = @_;
1611 1601
1612 for my $child (@widgets) { 1602 for my $child (@widgets) {
1613 $child->{rowspan} ||= 1; 1603 $child->{c_rowspan} ||= 1;
1614 $child->{colspan} ||= 1; 1604 $child->{c_colspan} ||= 1;
1615 } 1605 }
1616 1606
1617 $self->SUPER::add (@widgets); 1607 $self->SUPER::add (@widgets);
1618} 1608}
1619 1609
1623 my @widgets; 1613 my @widgets;
1624 1614
1625 while (@_) { 1615 while (@_) {
1626 my ($col, $row, $child) = splice @_, 0, 3, (); 1616 my ($col, $row, $child) = splice @_, 0, 3, ();
1627 1617
1628 $child->{row} = $row; 1618 $child->{c_row} = $row;
1629 $child->{col} = $col; 1619 $child->{c_col} = $col;
1630 1620
1631 push @widgets, $child; 1621 push @widgets, $child;
1632 } 1622 }
1633 1623
1634 $self->add (@widgets); 1624 $self->add (@widgets);
1640 my (@w, @h); 1630 my (@w, @h);
1641 1631
1642 my @children = $self->children; 1632 my @children = $self->children;
1643 1633
1644 # first pass, columns 1634 # first pass, columns
1645 for my $widget (sort { $b->{colspan} <=> $a->{colspan} } @children) { 1635 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1646 my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; 1636 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1647 1637
1648 my $sw = sum @w[$c .. $c + $cs - 1]; 1638 my $sw = sum @w[$c .. $c + $cs - 1];
1649 1639
1650 if ($w > $sw) { 1640 if ($w > $sw) {
1651 $_ += ($w - $sw) / $cs for @w[$c .. $c + $cs - 1]; 1641 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1652 } 1642 }
1653 } 1643 }
1654 1644
1655 # second pass, rows 1645 # second pass, rows
1656 for my $widget (sort { $b->{rowspan} <=> $a->{rowspan} } @children) { 1646 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1657 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; 1647 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1658 1648
1659 my $sh = sum @h[$r .. $r + $rs - 1]; 1649 my $sh = sum @h[$r .. $r + $rs - 1];
1660 1650
1661 if ($h > $sh) { 1651 if ($h > $sh) {
1662 $_ += ($h - $sh) / $rs for @h[$r .. $r + $rs - 1]; 1652 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1663 } 1653 }
1664 } 1654 }
1665 1655
1666 (\@w, \@h) 1656 (\@w, \@h)
1667} 1657}
1704 1694
1705 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1695 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1706 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1696 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1707 1697
1708 for my $widget ($self->children) { 1698 for my $widget ($self->children) {
1709 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)};
1710 1700
1711 $widget->configure ( 1701 $widget->configure (
1712 $x[$c], $y[$r], 1702 $x[$c], $y[$r],
1713 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], 1703 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1714 ); 1704 );
1723 1713
1724use List::Util qw(min max); 1714use List::Util qw(min max);
1725 1715
1726our @ISA = CFPlus::UI::Container::; 1716our @ISA = CFPlus::UI::Container::;
1727 1717
1728sub add {
1729 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1730
1731 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1732 $self->SUPER::add ($child);
1733}
1734
1735sub _scale($$$) { 1718sub _scale($$$) {
1736 my ($mode, $val, $max) = @_; 1719 my ($rel, $val, $max) = @_;
1737 1720
1738 $mode eq "abs" ? $val 1721 $rel ? $val * $max : $val
1739 : $mode eq "rel" ? $val * $max
1740 : 0
1741} 1722}
1742 1723
1743sub size_request { 1724sub size_request {
1744 my ($self) = @_; 1725 my ($self) = @_;
1745 1726
1746 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1727 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1747 1728
1748 # determine overall size by querying abs widgets 1729 # determine overall size by querying abs widgets
1749 for my $child ($self->visible_children) { 1730 for my $child ($self->visible_children) {
1750 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};
1751 1734
1752 if ($pos eq "abs") {
1753 $w = _scale $size, $w, $child->{req_w};
1754 $h = _scale $size, $h, $child->{req_h};
1755
1756 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1735 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1757 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1736 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1758 } 1737 }
1759 } 1738 }
1760 1739
1761 my $W = $x2 - $x1; 1740 my $W = $x2 - $x1;
1762 my $H = $y2 - $y1; 1741 my $H = $y2 - $y1;
1763 1742
1764 # now layout remaining widgets 1743 # now layout remaining widgets
1765 for my $child ($self->visible_children) { 1744 for my $child ($self->visible_children) {
1766 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;
1767 1748
1768 if ($pos ne "abs") {
1769 $x = _scale $pos, $x, $W;
1770 $y = _scale $pos, $x, $H;
1771 $w = _scale $size, $w, $child->{req_w};
1772 $h = _scale $size, $h, $child->{req_h};
1773
1774 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1749 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1775 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1750 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1776 } 1751 }
1777 } 1752 }
1778 1753
1779 my $W = $x2 - $x1; 1754 my $W = $x2 - $x1;
1780 my $H = $y2 - $y1; 1755 my $H = $y2 - $y1;
1784 1759
1785sub invoke_size_allocate { 1760sub invoke_size_allocate {
1786 my ($self, $W, $H) = @_; 1761 my ($self, $W, $H) = @_;
1787 1762
1788 for my $child ($self->visible_children) { 1763 for my $child ($self->visible_children) {
1789 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;
1790 1766
1791 $x = _scale $pos, $x, $W; 1767 $x += $child->{c_halign} * $child->{req_w};
1792 $y = _scale $pos, $x, $H; 1768 $y += $child->{c_valign} * $child->{req_h};
1793 $w = _scale $size, $w, $child->{req_w};
1794 $h = _scale $size, $h, $child->{req_h};
1795 1769
1796 $child->configure ($x, $y, $w, $h); 1770 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1797 } 1771 }
1798 1772
1799 1 1773 1
1800} 1774}
1801 1775
1805 1779
1806our @ISA = CFPlus::UI::Container::; 1780our @ISA = CFPlus::UI::Container::;
1807 1781
1808sub size_request { 1782sub size_request {
1809 my ($self) = @_; 1783 my ($self) = @_;
1784
1785 my @children = $self->visible_children;
1810 1786
1811 $self->{vertical} 1787 $self->{vertical}
1812 ? ( 1788 ? (
1813 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1789 (List::Util::max map $_->{req_w}, @children),
1814 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1790 (List::Util::sum map $_->{req_h}, @children),
1815 ) 1791 )
1816 : ( 1792 : (
1817 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1793 (List::Util::sum map $_->{req_w}, @children),
1818 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1794 (List::Util::max map $_->{req_h}, @children),
1819 ) 1795 )
1820} 1796}
1821 1797
1822sub invoke_size_allocate { 1798sub invoke_size_allocate {
1823 my ($self, $w, $h) = @_; 1799 my ($self, $w, $h) = @_;
1947 1923
1948 delete $self->{ox}; 1924 delete $self->{ox};
1949 $self->SUPER::realloc; 1925 $self->SUPER::realloc;
1950} 1926}
1951 1927
1928sub clear {
1929 my ($self) = @_;
1930
1931 $self->set_text ("");
1932}
1933
1952sub set_text { 1934sub set_text {
1953 my ($self, $text) = @_; 1935 my ($self, $text) = @_;
1954 1936
1955 return if $self->{text} eq "T$text"; 1937 return if $self->{text} eq "T$text";
1956 $self->{text} = "T$text"; 1938 $self->{text} = "T$text";
1979 1961
1980sub size_request { 1962sub size_request {
1981 my ($self) = @_; 1963 my ($self) = @_;
1982 1964
1983 $self->{size_req} ||= do { 1965 $self->{size_req} ||= do {
1966 my ($max_w, $max_h) = $self->get_max_wh;
1967
1984 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1968 $self->{layout}->set_font ($self->{font}) if $self->{font};
1985 $self->{layout}->set_width ($self->{max_w} || -1); 1969 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1986 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1970 $self->{layout}->set_ellipsise ($self->{ellipsise});
1987 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1971 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1988 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1972 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1989 1973
1990 my ($w, $h) = $self->{layout}->size; 1974 my ($w, $h) = $self->{layout}->size;
2062 : ($self->{w} - $size->[0]) * 0.5); 2046 : ($self->{w} - $size->[0]) * 0.5);
2063 2047
2064 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2048 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2065 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2049 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2066 : ($self->{h} - $size->[1]) * 0.5); 2050 : ($self->{h} - $size->[1]) * 0.5);
2051
2052 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2067 }; 2053 };
2068 2054
2069 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2055# unless ($self->{list}) {
2070 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2056# $self->{list} = CFPlus::OpenGL::glGenList;
2071 2057# CFPlus::OpenGL::glNewList $self->{list};
2072 $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;
2073} 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#}
2074 2074
2075############################################################################# 2075#############################################################################
2076 2076
2077package CFPlus::UI::EntryBase; 2077package CFPlus::UI::EntryBase;
2078 2078
2084 my $class = shift; 2084 my $class = shift;
2085 2085
2086 $class->SUPER::new ( 2086 $class->SUPER::new (
2087 fg => [1, 1, 1], 2087 fg => [1, 1, 1],
2088 bg => [0, 0, 0, 0.2], 2088 bg => [0, 0, 0, 0.2],
2089 outline => [0.6, 0.3, 0.1],
2089 active_bg => [1, 1, 1, 0.5], 2090 active_bg => [0, 0, 1, .2],
2090 active_fg => [0, 0, 0], 2091 active_fg => [1, 1, 1],
2092 active_outline => [1, 1, 0],
2091 can_hover => 1, 2093 can_hover => 1,
2092 can_focus => 1, 2094 can_focus => 1,
2093 valign => 0, 2095 valign => 0,
2094 can_events => 1, 2096 can_events => 1,
2095 ellipsise => 0, 2097 ellipsise => 0,
2235 glColor_premultiply @{$self->{bg}}; 2237 glColor_premultiply @{$self->{bg}};
2236 } 2238 }
2237 2239
2238 glEnable GL_BLEND; 2240 glEnable GL_BLEND;
2239 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2241 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2240 glBegin GL_QUADS;
2241 glVertex 0 , 0;
2242 glVertex 0 , $self->{h};
2243 glVertex $self->{w}, $self->{h}; 2242 glRect 0, 0, $self->{w}, $self->{h};
2244 glVertex $self->{w}, 0;
2245 glEnd;
2246 glDisable GL_BLEND; 2243 glDisable GL_BLEND;
2247 2244
2248 $self->SUPER::_draw; 2245 $self->SUPER::_draw;
2249 2246
2250 #TODO: force update every cursor change :( 2247 #TODO: force update every cursor change :(
2252 2249
2253 unless (exists $self->{cur_h}) { 2250 unless (exists $self->{cur_h}) {
2254 my $text = substr $self->{text}, 0, $self->{cursor}; 2251 my $text = substr $self->{text}, 0, $self->{cursor};
2255 utf8::encode $text; 2252 utf8::encode $text;
2256 2253
2257 @$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);
2258 } 2255 }
2259 2256
2257 glColor_premultiply @{$self->{active_fg}};
2260 glBegin GL_LINES; 2258 glBegin GL_LINES;
2261 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};
2262 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;
2263 glEnd; 2275 glEnd;
2264 } 2276 }
2265} 2277}
2266 2278
2267############################################################################# 2279#############################################################################
2354 1 2366 1
2355} 2367}
2356 2368
2357############################################################################# 2369#############################################################################
2358 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
2359package CFPlus::UI::Button; 2420package CFPlus::UI::Button;
2360 2421
2361our @ISA = CFPlus::UI::Label::; 2422our @ISA = CFPlus::UI::Label::;
2362 2423
2363use CFPlus::OpenGL; 2424use CFPlus::OpenGL;
2467sub _draw { 2528sub _draw {
2468 my ($self) = @_; 2529 my ($self) = @_;
2469 2530
2470 $self->SUPER::_draw; 2531 $self->SUPER::_draw;
2471 2532
2472 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2533 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2473 2534
2474 my ($w, $h) = @$self{qw(w h)}; 2535 my ($w, $h) = @$self{qw(w h)};
2475 2536
2476 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;
2477 2538
2497sub new { 2558sub new {
2498 my $class = shift; 2559 my $class = shift;
2499 2560
2500 my $self = $class->SUPER::new ( 2561 my $self = $class->SUPER::new (
2501 can_events => 0, 2562 can_events => 0,
2563 scale => 1,
2502 @_, 2564 @_,
2503 ); 2565 );
2504 2566
2505 $self->{path} || $self->{tex} 2567 $self->{path} || $self->{tex}
2506 or Carp::croak "'path' or 'tex' attributes required"; 2568 or Carp::croak "'path' or 'tex' attributes required";
2531} 2593}
2532 2594
2533sub size_request { 2595sub size_request {
2534 my ($self) = @_; 2596 my ($self) = @_;
2535 2597
2536 ($self->{tex}{w}, $self->{tex}{h}) 2598 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2537} 2599}
2538 2600
2539sub _draw { 2601sub _draw {
2540 my ($self) = @_; 2602 my ($self) = @_;
2541 2603
2551 } 2613 }
2552 2614
2553 glEnable GL_TEXTURE_2D; 2615 glEnable GL_TEXTURE_2D;
2554 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2616 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2555 2617
2556 $tex->draw_quad (0, 0, $w, $h); 2618 $tex->draw_quad_alpha (0, 0, $w, $h);
2557 2619
2558 glDisable GL_TEXTURE_2D; 2620 glDisable GL_TEXTURE_2D;
2559} 2621}
2560 2622
2561############################################################################# 2623#############################################################################
2580 align => 0, 2642 align => 0,
2581 valign => 0, 2643 valign => 0,
2582 can_events => 1, 2644 can_events => 1,
2583 @_ 2645 @_
2584 ); 2646 );
2647}
2648
2649sub invoke_button_down {
2650 my ($self, $ev, $x, $y) = @_;
2651
2652 1
2585} 2653}
2586 2654
2587sub invoke_button_up { 2655sub invoke_button_up {
2588 my ($self, $ev, $x, $y) = @_; 2656 my ($self, $ev, $x, $y) = @_;
2589 2657
2729 glDisable GL_TEXTURE_2D; 2797 glDisable GL_TEXTURE_2D;
2730} 2798}
2731 2799
2732############################################################################# 2800#############################################################################
2733 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
2734package CFPlus::UI::Gauge; 2940package CFPlus::UI::Gauge;
2735 2941
2736our @ISA = CFPlus::UI::VBox::; 2942our @ISA = CFPlus::UI::VBox::;
2737 2943
2738sub new { 2944sub new {
2863 3069
2864 $self->SUPER::invoke_button_down ($ev, $x, $y); 3070 $self->SUPER::invoke_button_down ($ev, $x, $y);
2865 3071
2866 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3072 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2867 3073
2868 $self->invoke_mouse_motion ($ev, $x, $y) 3074 $self->invoke_mouse_motion ($ev, $x, $y);
3075
3076 1
2869} 3077}
2870 3078
2871sub invoke_mouse_motion { 3079sub invoke_mouse_motion {
2872 my ($self, $ev, $x, $y) = @_; 3080 my ($self, $ev, $x, $y) = @_;
2873 3081
2893 3101
2894 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3102 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2895 3103
2896 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3104 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2897 3105
2898 ! ! $delta 3106 1
2899} 3107}
2900 3108
2901sub update { 3109sub update {
2902 my ($self) = @_; 3110 my ($self) = @_;
2903 3111
3034} 3242}
3035 3243
3036sub size_request { 3244sub size_request {
3037 my ($self) = @_; 3245 my ($self) = @_;
3038 3246
3039 my ($empty, $slider) = @{ $self->{children} }; 3247 my ($empty, $slider) = $self->visible_children;
3040 3248
3041 local $self->{children} = [$empty, $slider]; 3249 local $self->{children} = [$empty, $slider];
3042 $self->SUPER::size_request 3250 $self->SUPER::size_request
3043} 3251}
3044 3252
3233 3441
3234 if ($y0 < $y + $h && $y < $y1) { 3442 if ($y0 < $y + $h && $y < $y1) {
3235 my $layout = $self->get_layout ($para); 3443 my $layout = $self->get_layout ($para);
3236 3444
3237 $layout->render ($para->{indent}, $y - $y0); 3445 $layout->render ($para->{indent}, $y - $y0);
3446 $layout->draw;
3238 3447
3239 if (my @w = @{ $para->{widget} }) { 3448 if (my @w = @{ $para->{widget} }) {
3240 my @s = $layout->get_shapes; 3449 my @s = $layout->get_shapes;
3241 3450
3242 for (@w) { 3451 for (@w) {
3364} 3573}
3365 3574
3366sub set_tooltip_from { 3575sub set_tooltip_from {
3367 my ($self, $widget) = @_; 3576 my ($self, $widget) = @_;
3368 3577
3369 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3370 if $widget->{tooltip} =~ /^#(.*)$/;
3371
3372 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 =~ /^#(.*)$/;
3373 3583
3374 if ($ENV{CFPLUS_DEBUG} & 2) { 3584 if ($ENV{CFPLUS_DEBUG} & 2) {
3375 $tooltip .= "\n\n" . (ref $widget) . "\n" 3585 $tip .= "\n\n" . (ref $widget) . "\n"
3376 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3586 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3377 . "req $widget->{req_w} $widget->{req_h}\n" 3587 . "req $widget->{req_w} $widget->{req_h}\n"
3378 . "visible $widget->{visible}"; 3588 . "visible $widget->{visible}";
3379 } 3589 }
3380 3590
3381 $tooltip =~ s/^\n+//; 3591 $tip =~ s/^\n+//;
3382 $tooltip =~ s/\n+$//; 3592 $tip =~ s/\n+$//;
3383 3593
3384 $self->add (new CFPlus::UI::Label 3594 $self->add (new CFPlus::UI::Label
3385 markup => $tooltip, 3595 markup => $tip,
3386 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3596 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3387 fontsize => 0.8, 3597 fontsize => 0.8,
3388 style => 1, # FLAG_INVERSE 3598 style => 1, # FLAG_INVERSE
3389 ellipsise => 0, 3599 ellipsise => 0,
3390 font => ($widget->{tooltip_font} || $::FONT_PROP), 3600 font => ($widget->{tooltip_font} || $::FONT_PROP),
3428} 3638}
3429 3639
3430sub _draw { 3640sub _draw {
3431 my ($self) = @_; 3641 my ($self) = @_;
3432 3642
3433 glTranslate 0.375, 0.375;
3434
3435 my ($w, $h) = @$self{qw(w h)}; 3643 my ($w, $h) = @$self{qw(w h)};
3436 3644
3437 glColor 1, 0.8, 0.4; 3645 glColor 1, 0.8, 0.4;
3438 glBegin GL_QUADS; 3646 glRect 0, 0, $w, $h;
3439 glVertex 0 , 0;
3440 glVertex 0 , $h;
3441 glVertex $w, $h;
3442 glVertex $w, 0;
3443 glEnd;
3444 3647
3445 glColor 0, 0, 0; 3648 glColor 0, 0, 0;
3446 glBegin GL_LINE_LOOP; 3649 glRect_lineloop .5, .5, $w + .5, $h + .5;
3447 glVertex 0 , 0;
3448 glVertex 0 , $h;
3449 glVertex $w, $h;
3450 glVertex $w, 0;
3451 glEnd;
3452 3650
3453 glTranslate 2 - 0.375, 2 - 0.375; 3651 glTranslate 2, 2;
3454 3652
3455 $self->SUPER::_draw; 3653 $self->SUPER::_draw;
3456} 3654}
3457 3655
3458############################################################################# 3656#############################################################################
3476 3674
3477 if ($self->{anim} && $self->{animspeed}) { 3675 if ($self->{anim} && $self->{animspeed}) {
3478 CFPlus::weaken (my $widget = $self); 3676 CFPlus::weaken (my $widget = $self);
3479 3677
3480 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3678 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3481 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed}; 3679 $widget->{anim_start} = $self->{animspeed} * int EV::now / $self->{animspeed};
3482 $self->{timer} = Event->timer ( 3680 $self->{timer} = EV::timer_ns 0, 0, sub {
3483 parked => 1,
3484 cb => sub {
3485 return unless $::CONN && $widget; 3681 return unless $::CONN;
3486 3682
3683 my $w = $widget
3684 or return;
3685
3487 ++$widget->{frame}; 3686 ++$w->{frame};
3488 $widget->update_face; 3687 $w->update_face;
3688
3689 # somehow, $widget can go away
3489 $widget->update; 3690 $w->update;
3490
3491 $widget->update_timer; 3691 $w->update_timer;
3492 },
3493 ); 3692 };
3494 3693
3495 $self->update_face; 3694 $self->update_face;
3496 $self->update_timer; 3695 $self->update_timer;
3497 } 3696 }
3498 3697
3502sub update_timer { 3701sub update_timer {
3503 my ($self) = @_; 3702 my ($self) = @_;
3504 3703
3505 return unless $self->{timer}; 3704 return unless $self->{timer};
3506 3705
3706 $self->{timer}->stop;
3507 if ($self->{visible}) { 3707 if ($self->{visible}) {
3508 $self->{timer}->at ( 3708 $self->{timer}->set (
3509 $self->{anim_start} 3709 $self->{anim_start}
3510 + $self->{animspeed} 3710 + $self->{animspeed}
3511 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed} 3711 * int 1.5 + (EV::now - $self->{anim_start}) / $self->{animspeed}
3512 ); 3712 );
3513 $self->{timer}->start; 3713 $self->{timer}->start;
3514 } else {
3515 $self->{timer}->stop;
3516 } 3714 }
3517} 3715}
3518 3716
3519sub update_face { 3717sub update_face {
3520 my ($self) = @_; 3718 my ($self) = @_;
3521 3719
3522 return unless $::CONN;
3523
3524 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3525 if ($anim && @$anim) {
3526 delete $self->{wait_face};
3527 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3528 }
3529 }
3530}
3531
3532sub size_request {
3533 my ($self) = @_;
3534
3535 if ($::CONN) { 3720 if ($::CONN) {
3536 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3721 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3537 if (my $tex = $::CONN->{texture}[$faceid]) { 3722 if ($anim && @$anim) {
3538 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3723 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3539 } else { 3724 delete $self->{face_change_cb};
3540 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3725
3541 $self->realloc; 3726 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3727 unless ($tex->{name} || $tex->{loading}) {
3728 $tex->upload (sub { $self->reconfigure });
3729 }
3542 }); 3730 }
3543 } 3731 }
3544 } 3732 }
3545 } 3733 }
3734}
3735
3736sub size_request {
3737 my ($self) = @_;
3738
3739 if ($::CONN) {
3740 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3741 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3742 if ($tex->{name}) {
3743 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3744 } elsif (!$tex->{loading}) {
3745 $tex->upload (sub { $self->reconfigure });
3746 }
3747 }
3748
3749 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3750 }
3751 }
3546 3752
3547 ($self->{size_w} || 8, $self->{size_h} || 8) 3753 ($self->{size_w} || 8, $self->{size_h} || 8)
3548} 3754}
3549 3755
3550sub update { 3756sub update {
3564} 3770}
3565 3771
3566sub _draw { 3772sub _draw {
3567 my ($self) = @_; 3773 my ($self) = @_;
3568 3774
3569 return unless $::CONN;
3570
3571 $self->SUPER::_draw; 3775 $self->SUPER::_draw;
3572 3776
3573 my $faceid = $::CONN->{faceid}[$self->{face}] 3777 if (my $tex = $self->{tex}) {
3574 or return;
3575
3576 my $tex = $::CONN->{texture}[$faceid];
3577
3578 if ($tex) {
3579 glEnable GL_TEXTURE_2D; 3778 glEnable GL_TEXTURE_2D;
3580 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3779 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3581 glColor 0, 0, 0, 1; 3780 glColor 0, 0, 0, 1;
3582 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3781 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3583 glDisable GL_TEXTURE_2D; 3782 glDisable GL_TEXTURE_2D;
3635 children => [ 3834 children => [
3636 (new CFPlus::UI::Label markup => $left, expand => 1), 3835 (new CFPlus::UI::Label markup => $left, expand => 1),
3637 (new CFPlus::UI::Label markup => $right, align => +1), 3836 (new CFPlus::UI::Label markup => $right, align => +1),
3638 ], 3837 ],
3639 ; 3838 ;
3640 3839
3641 } else { 3840 } else {
3642 $widget = new CFPlus::UI::Label 3841 $widget = new CFPlus::UI::Label
3643 can_hover => 1, 3842 can_hover => 1,
3644 can_events => 1, 3843 can_events => 1,
3645 markup => $widget, 3844 markup => $widget,
3770 3969
3771############################################################################# 3970#############################################################################
3772 3971
3773package CFPlus::UI::Notebook; 3972package CFPlus::UI::Notebook;
3774 3973
3974use CFPlus::OpenGL;
3975
3775our @ISA = CFPlus::UI::VBox::; 3976our @ISA = CFPlus::UI::VBox::;
3776 3977
3777sub new { 3978sub new {
3778 my $class = shift; 3979 my $class = shift;
3779 3980
3780 my $self = $class->SUPER::new ( 3981 my $self = $class->SUPER::new (
3781 buttonbar => (new CFPlus::UI::Buttonbar), 3982 buttonbar => (new CFPlus::UI::Buttonbar),
3782 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3983 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3984 active_outline => [.7, .7, 0.2],
3783 # filter => # will be put between multiplexer and $self 3985 # filter => # will be put between multiplexer and $self
3784 @_, 3986 @_,
3785 ); 3987 );
3786 3988
3787 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3989 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3788 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3990 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3789 3991
3992 {
3993 Scalar::Util::weaken (my $wself = $self);
3994
3995 $self->{multiplexer}->connect (c_add => sub {
3996 my ($mplex, $widgets) = @_;
3997
3998 for my $child (@$widgets) {
3999 Scalar::Util::weaken $child;
4000 $child->{c_tab_} ||= do {
4001 my $tab =
4002 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base")
4003 ? $child->{c_tab}
4004 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4005
4006 $tab->connect (activate => sub {
4007 $wself->set_current_page ($child);
4008 });
4009
4010 $tab
4011 };
4012
4013 $self->{buttonbar}->add ($child->{c_tab_});
4014 }
4015 });
4016
4017 $self->{multiplexer}->connect (c_remove => sub {
4018 my ($mplex, $widgets) = @_;
4019
4020 for my $child (@$widgets) {
4021 $wself->{buttonbar}->remove ($child->{c_tab_});
4022 }
4023 });
4024 }
4025
3790 $self 4026 $self
3791} 4027}
3792 4028
3793sub add { 4029sub add {
4030 my ($self, @widgets) = @_;
4031
4032 $self->{multiplexer}->add (@widgets)
4033}
4034
4035sub remove {
4036 my ($self, @widgets) = @_;
4037
4038 $self->{multiplexer}->remove (@widgets)
4039}
4040
4041sub pages {
4042 my ($self) = @_;
4043 $self->{multiplexer}->children
4044}
4045
4046sub add_tab {
3794 my ($self, $title, $widget, $tooltip) = @_; 4047 my ($self, $title, $widget, $tooltip) = @_;
3795 4048
3796 CFPlus::weaken $self; 4049 $title = [$title, $tooltip] unless ref $title;
4050 $widget->{c_tab} = $title;
3797 4051
3798 $self->{buttonbar}->add (new CFPlus::UI::Button
3799 markup => $title,
3800 tooltip => $tooltip,
3801 on_activate => sub { $self->set_current_page ($widget) },
3802 );
3803
3804 $self->{multiplexer}->add ($widget); 4052 $self->add ($widget);
3805} 4053}
3806 4054
3807sub get_current_page { 4055sub get_current_page {
3808 my ($self) = @_; 4056 my ($self) = @_;
3809 4057
3813sub set_current_page { 4061sub set_current_page {
3814 my ($self, $page) = @_; 4062 my ($self, $page) = @_;
3815 4063
3816 $self->{multiplexer}->set_current_page ($page); 4064 $self->{multiplexer}->set_current_page ($page);
3817 $self->emit (page_changed => $self->{multiplexer}{current}); 4065 $self->emit (page_changed => $self->{multiplexer}{current});
4066}
4067
4068sub _draw {
4069 my ($self) = @_;
4070
4071 $self->SUPER::_draw ();
4072
4073 if (my $cur = $self->{multiplexer}{current}) {
4074 if ($cur = $cur->{c_tab_}) {
4075 glTranslate $self->{buttonbar}{x} + $cur->{x},
4076 $self->{buttonbar}{y} + $cur->{y};
4077 glLineWidth 3;
4078 #glEnable GL_BLEND;
4079 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4080 glColor @{$self->{active_outline}};
4081 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4082 glLineWidth 1;
4083 #glDisable GL_BLEND;
4084 }
4085 }
3818} 4086}
3819 4087
3820############################################################################# 4088#############################################################################
3821 4089
3822package CFPlus::UI::Selector; 4090package CFPlus::UI::Selector;
3854} 4122}
3855 4123
3856sub _set_value { 4124sub _set_value {
3857 my ($self, $value) = @_; 4125 my ($self, $value) = @_;
3858 4126
3859 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4127 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4128 $item ||= $self->{options}[0]
3860 or return; 4129 or return;
3861 4130
3862 $self->{value} = $item->[0]; 4131 $self->{value} = $item->[0];
3863 $self->set_markup ("$item->[1] ⇓"); 4132 $self->set_markup ("$item->[1] ⇓");
3864 $self->set_tooltip ($item->[2]); 4133# $self->set_tooltip ($item->[2]);
3865} 4134}
3866 4135
3867sub set_value { 4136sub set_value {
3868 my ($self, $value) = @_; 4137 my ($self, $value) = @_;
3869 4138
3870 return unless $self->{value} ne $value; 4139 return unless $self->{value} ne $value;
3871 4140
3872 $self->_set_value ($value); 4141 $self->_set_value ($value);
3873 $self->emit (changed => $value); 4142 $self->emit (changed => $value);
4143}
4144
4145sub set_options {
4146 my ($self, $options) = @_;
4147
4148 $self->{options} = $options;
4149 $self->_set_value ($self->{value});
3874} 4150}
3875 4151
3876############################################################################# 4152#############################################################################
3877 4153
3878package CFPlus::UI::Statusbox; 4154package CFPlus::UI::Statusbox;
3887 @_, 4163 @_,
3888 ); 4164 );
3889 4165
3890 CFPlus::weaken (my $this = $self); 4166 CFPlus::weaken (my $this = $self);
3891 4167
3892 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4168 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3893 4169
3894 $self 4170 $self
3895} 4171}
3896 4172
3897sub reorder { 4173sub reorder {
3911 4187
3912 my @items = sort { 4188 my @items = sort {
3913 $a->{pri} <=> $b->{pri} 4189 $a->{pri} <=> $b->{pri}
3914 or $b->{id} <=> $a->{id} 4190 or $b->{id} <=> $a->{id}
3915 } values %{ $self->{item} }; 4191 } values %{ $self->{item} };
3916
3917 $self->{timer}->interval (1);
3918 4192
3919 my $count = 10 + 1; 4193 my $count = 10 + 1;
3920 for my $item (@items) { 4194 for my $item (@items) {
3921 last unless --$count; 4195 last unless --$count;
3922 4196
3946 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4220 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3947 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4221 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3948 $label->update; 4222 $label->update;
3949 $label->set_max_size (undef, $label->{req_h} * $diff) 4223 $label->set_max_size (undef, $label->{req_h} * $diff)
3950 if $diff < 1; 4224 if $diff < 1;
3951 $self->{timer}->interval (1/30); 4225 $self->{timer}->set (1/30, 1/30);
3952 } else { 4226 } else {
3953 $label->{fg}[3] = $item->{fg}[3] || 1; 4227 $label->{fg}[3] = $item->{fg}[3] || 1;
3954 } 4228 }
3955 4229
3956 push @widgets, $label; 4230 push @widgets, $label;
4092} 4366}
4093 4367
4094sub update { 4368sub update {
4095 my ($self) = @_; 4369 my ($self) = @_;
4096 4370
4097 $::WANT_REFRESH++; 4371 $::WANT_REFRESH->start;
4098} 4372}
4099 4373
4100sub add { 4374sub add {
4101 my ($self, @children) = @_; 4375 my ($self, @children) = @_;
4102 4376
4139 while ($self->{refresh_hook}) { 4413 while ($self->{refresh_hook}) {
4140 $_->() 4414 $_->()
4141 for values %{delete $self->{refresh_hook}}; 4415 for values %{delete $self->{refresh_hook}};
4142 } 4416 }
4143 4417
4144 if ($self->{realloc}) { 4418 while ($self->{realloc}) {
4145 my %queue; 4419 my %queue;
4146 my @queue; 4420 my @queue;
4147 my $widget; 4421 my $widget;
4148 4422
4149 outer: 4423 outer:
4196 } 4470 }
4197 } 4471 }
4198 4472
4199 delete $self->{realloc}{$widget+0}; 4473 delete $self->{realloc}{$widget+0};
4200 } 4474 }
4201 }
4202 4475
4203 while (my $size_alloc = delete $self->{size_alloc}) { 4476 while (my $size_alloc = delete $self->{size_alloc}) {
4204 my @queue = sort { $b->{visible} <=> $a->{visible} } 4477 my @queue = sort { $a->{visible} <=> $b->{visible} }
4205 values %$size_alloc; 4478 values %$size_alloc;
4206 4479
4207 while () { 4480 while () {
4208 my $widget = pop @queue || last; 4481 my $widget = pop @queue || last;
4209 4482
4210 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4483 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4211 4484
4212 $w = 0 if $w < 0;
4213 $h = 0 if $h < 0;
4214
4215 $w = max $widget->{min_w}, $w; 4485 $w = max $widget->{min_w}, $w;
4216 $h = max $widget->{min_h}, $h; 4486 $h = max $widget->{min_h}, $h;
4217 4487
4218# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4488# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4219# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4489# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4220 4490
4221 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4491 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4222 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4492 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4223 4493
4224 $w = int $w + 0.5; 4494 $w = int $w + 0.5;
4225 $h = int $h + 0.5; 4495 $h = int $h + 0.5;
4226 4496
4227 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4497 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
4228 $widget->{old_w} = $widget->{w}; 4498 $widget->{old_w} = $widget->{w};
4229 $widget->{old_h} = $widget->{h}; 4499 $widget->{old_h} = $widget->{h};
4230 4500
4231 $widget->{w} = $w; 4501 $widget->{w} = $w;
4232 $widget->{h} = $h; 4502 $widget->{h} = $h;
4233 4503
4234 $widget->emit (size_allocate => $w, $h); 4504 $widget->emit (size_allocate => $w, $h);
4505 }
4235 } 4506 }
4236 } 4507 }
4237 } 4508 }
4238 4509
4239 while ($self->{post_alloc_hook}) { 4510 while ($self->{post_alloc_hook}) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines