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.387 by root, Fri Jul 20 18:19:45 2007 UTC vs.
Revision 1.450 by root, Wed Dec 26 15:29:43 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, 0.03, 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
118 $ev->{dx} = 0;
119 $ev->{dy} = $ev->{button} * 2 - 9; 115 my $delta = $ev->{button} * 2 - 9;
116 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT;
117
118 $ev->{dx} = $shift ? $delta : 0;
119 $ev->{dy} = $shift ? 0 : $delta;
120
120 $GRAB->emit (mouse_wheel => $ev); 121 $GRAB->emit (mouse_wheel => $ev);
121 } else { 122 } else {
122 $GRAB->emit (button_down => $ev) 123 $GRAB->emit (button_down => $ev)
123 } 124 }
124 } 125 }
138 my $grab = $GRAB; undef $GRAB; 139 my $grab = $GRAB; undef $GRAB;
139 $grab->update if $grab; 140 $grab->update if $grab;
140 $GRAB->update if $GRAB; 141 $GRAB->update if $GRAB;
141 142
142 check_hover $widget; 143 check_hover $widget;
143 $TOOLTIP_WATCHER->cb->(); 144 $TOOLTIP_WATCHER->invoke;
144 } 145 }
145} 146}
146 147
147sub feed_sdl_motion_event { 148sub feed_sdl_motion_event {
148 my ($ev) = @_; 149 my ($ev) = @_;
269 $self->emit ("destroy"); 270 $self->emit ("destroy");
270 %$self = (); 271 %$self = ();
271} 272}
272 273
273sub TO_JSON { 274sub TO_JSON {
274 { __widget_ref__ => $_[0]{s_id} } 275 { "\fw" => $_[0]{s_id} }
275} 276}
276 277
277sub show { 278sub show {
278 my ($self) = @_; 279 my ($self) = @_;
279 280
308 delete $self->{root}; 309 delete $self->{root};
309 310
310 undef $GRAB if $GRAB == $self; 311 undef $GRAB if $GRAB == $self;
311 undef $HOVER if $HOVER == $self; 312 undef $HOVER if $HOVER == $self;
312 313
313 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 314 $CFPlus::UI::TOOLTIP_WATCHER->invoke
314 if $TOOLTIP->{owner} == $self; 315 if $TOOLTIP->{owner} == $self;
315 316
316 $self->emit ("focus_out"); 317 $self->emit ("focus_out");
317 $self->emit (visibility_change => 0); 318 $self->emit (visibility_change => 0);
318} 319}
358 359
359 $self->{force_w} = $w; 360 $self->{force_w} = $w;
360 $self->{force_h} = $h; 361 $self->{force_h} = $h;
361 362
362 $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)
363} 374}
364 375
365sub size_request { 376sub size_request {
366 require Carp; 377 require Carp;
367 Carp::confess "size_request is abstract"; 378 Carp::confess "size_request is abstract";
429 440
430 $self->{tooltip} = $tooltip; 441 $self->{tooltip} = $tooltip;
431 442
432 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 443 if ($CFPlus::UI::TOOLTIP->{owner} == $self) {
433 delete $CFPlus::UI::TOOLTIP->{owner}; 444 delete $CFPlus::UI::TOOLTIP->{owner};
434 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 445 $CFPlus::UI::TOOLTIP_WATCHER->invoke;
435 } 446 }
436} 447}
437 448
438# translate global coordinates to local coordinate system 449# translate global coordinates to local coordinate system
439sub coord2local { 450sub coord2local {
440 my ($self, $x, $y) = @_; 451 my ($self, $x, $y) = @_;
441 452
442 Carp::confess unless $self->{parent};#d# 453 return (undef, undef) unless $self->{parent};
443 454
444 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 455 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
445} 456}
446 457
447# translate local coordinates to global coordinate system 458# translate local coordinates to global coordinate system
448sub coord2global { 459sub coord2global {
449 my ($self, $x, $y) = @_; 460 my ($self, $x, $y) = @_;
450 461
451 Carp::confess unless $self->{parent};#d# 462 return (undef, undef) unless $self->{parent};
452 463
453 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 464 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
454} 465}
455 466
456sub invoke_focus_in { 467sub invoke_focus_in {
589 600
590# 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
591# functions seems pointless. 602# functions seems pointless.
592our ($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
593 604
594sub draw { 605#sub draw {
595 my ($self) = @_; 606#CFPlus.xs
596
597 return unless $self->{h} && $self->{w};
598
599 # update screen rectangle
600 local $draw_x = $draw_x + $self->{x};
601 local $draw_y = $draw_y + $self->{y};
602
603 # skip widgets that are entirely outside the drawing area
604 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
605 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
606
607 glPushMatrix;
608 glTranslate $self->{x}, $self->{y}, 0;
609
610 if ($self == $HOVER && $self->{can_hover}) {
611 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
612 glEnable GL_BLEND;
613 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
614 glBegin GL_QUADS;
615 glVertex 0 , 0;
616 glVertex $self->{w}, 0;
617 glVertex $self->{w}, $self->{h};
618 glVertex 0 , $self->{h};
619 glEnd;
620 glDisable GL_BLEND;
621 }
622
623 if ($ENV{CFPLUS_DEBUG} & 1) {
624 glPushMatrix;
625 glColor 1, 1, 0, 1;
626 glTranslate 0.375, 0.375;
627 glBegin GL_LINE_LOOP;
628 glVertex 0 , 0;
629 glVertex $self->{w} - 1, 0;
630 glVertex $self->{w} - 1, $self->{h} - 1;
631 glVertex 0 , $self->{h} - 1;
632 glEnd;
633 glPopMatrix;
634 #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
635 }
636
637 $self->_draw;
638 glPopMatrix;
639}
640 607
641sub _draw { 608sub _draw {
642 my ($self) = @_; 609 my ($self) = @_;
643 610
644 warn "no draw defined for $self\n"; 611 warn "no draw defined for $self\n";
645} 612}
646 613
647my $cntx;#d#
648sub DESTROY { 614sub DESTROY {
649 my ($self) = @_; 615 my ($self) = @_;
650 616
651 return if CFPlus::in_destruct; 617 return if CFPlus::in_destruct;
652 618
619 local $@;
653 eval { $self->destroy }; 620 eval { $self->destroy };
654 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 621 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
655 622
656 delete $WIDGET{$self+0}; 623 delete $WIDGET{$self+0};
657} 624}
665use strict; 632use strict;
666use CFPlus::OpenGL; 633use CFPlus::OpenGL;
667 634
668sub new { 635sub new {
669 my $class = shift; 636 my $class = shift;
670
671 # range [value, low, high, page]
672 637
673 $class->SUPER::new ( 638 $class->SUPER::new (
674 #bg => [0, 0, 0, 0.2], 639 #bg => [0, 0, 0, 0.2],
675 #active_bg => [1, 1, 1, 0.5], 640 #active_bg => [1, 1, 1, 0.5],
676 @_ 641 @_
688 my ($w, $h) = @$self{qw(w h)}; 653 my ($w, $h) = @$self{qw(w h)};
689 654
690 glEnable GL_BLEND; 655 glEnable GL_BLEND;
691 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 656 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
692 glColor_premultiply @$color; 657 glColor_premultiply @$color;
693
694 glBegin GL_QUADS;
695 glVertex 0 , 0;
696 glVertex 0 , $h;
697 glVertex $w, $h; 658 glRect 0, 0, $w, $h;
698 glVertex $w, 0;
699 glEnd;
700
701 glDisable GL_BLEND; 659 glDisable GL_BLEND;
702 } 660 }
703} 661}
704 662
705############################################################################# 663#############################################################################
737 can_events => 0, 695 can_events => 0,
738 %arg, 696 %arg,
739 ); 697 );
740 698
741 $self->add (@$children) 699 $self->add (@$children)
742 if $children; 700 if $children && @$children;
743 701
744 $self 702 $self
745} 703}
746 704
747sub realloc { 705sub realloc {
756 my ($self, @widgets) = @_; 714 my ($self, @widgets) = @_;
757 715
758 $_->set_parent ($self) 716 $_->set_parent ($self)
759 for @widgets; 717 for @widgets;
760 718
719 # TODO: only do this in widgets that need it, e.g. root, fixed
761 use sort 'stable'; 720 use sort 'stable';
762 721
763 $self->{children} = [ 722 $self->{children} = [
764 sort { $a->{z} <=> $b->{z} } 723 sort { $a->{z} <=> $b->{z} }
765 @{$self->{children}}, @widgets 724 @{$self->{children}}, @widgets
766 ]; 725 ];
767 726
768 $self->realloc; 727 $self->realloc;
728
729 $self->emit (c_add => \@widgets);
730
731 map $_+0, @widgets
769} 732}
770 733
771sub children { 734sub children {
772 @{ $_[0]{children} } 735 @{ $_[0]{children} }
773} 736}
774 737
775sub remove { 738sub remove {
776 my ($self, $child) = @_; 739 my ($self, @widgets) = @_;
777 740
741 $self->emit (c_remove => \@widgets);
742
743 for my $child (@widgets) {
778 delete $child->{parent}; 744 delete $child->{parent};
779 $child->hide; 745 $child->hide;
780
781 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 746 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
747 }
782 748
783 $self->realloc; 749 $self->realloc;
784} 750}
785 751
786sub clear { 752sub clear {
787 my ($self) = @_; 753 my ($self) = @_;
788 754
789 my $children = delete $self->{children}; 755 my $children = $self->{children};
790 $self->{children} = []; 756 $self->{children} = [];
791 757
792 for (@$children) { 758 for (@$children) {
793 delete $_->{parent}; 759 delete $_->{parent};
794 $_->hide; 760 $_->hide;
814} 780}
815 781
816sub _draw { 782sub _draw {
817 my ($self) = @_; 783 my ($self) = @_;
818 784
819 $_->draw for @{$self->{children}}; 785 $_->draw for $self->visible_children;
820} 786}
821 787
822############################################################################# 788#############################################################################
823 789
824package CFPlus::UI::Bin; 790package CFPlus::UI::Bin;
834} 800}
835 801
836sub add { 802sub add {
837 my ($self, $child) = @_; 803 my ($self, $child) = @_;
838 804
839 $self->SUPER::remove ($_) for @{ $self->{children} }; 805 $self->clear;
840 $self->SUPER::add ($child); 806 $self->SUPER::add ($child);
841} 807}
842 808
843sub remove { 809sub remove {
844 my ($self, $widget) = @_; 810 my ($self, $widget) = @_;
862 828
863 1 829 1
864} 830}
865 831
866############################################################################# 832#############################################################################
867
868# back-buffered drawing area 833# back-buffered drawing area
869 834
870package CFPlus::UI::Window; 835package CFPlus::UI::Window;
871 836
872our @ISA = CFPlus::UI::Bin::; 837our @ISA = CFPlus::UI::Bin::;
954sub size_request { 919sub size_request {
955 my ($self) = @_; 920 my ($self) = @_;
956 921
957 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 922 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
958 923
959 $w = 10 if $self->{scroll_x}; 924 $w = 1 if $self->{scroll_x};
960 $h = 10 if $self->{scroll_y}; 925 $h = 1 if $self->{scroll_y};
961 926
962 ($w, $h) 927 ($w, $h)
963} 928}
964 929
965sub invoke_size_allocate { 930sub invoke_size_allocate {
986 $self->{view_x} = $x; 951 $self->{view_x} = $x;
987 $self->{view_y} = $y; 952 $self->{view_y} = $y;
988 953
989 $self->emit (changed => $x, $y); 954 $self->emit (changed => $x, $y);
990 $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);
991 } 974 }
992} 975}
993 976
994# 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
995sub coord2local { 978sub coord2local {
1042 my $child = delete $arg{child}; 1025 my $child = delete $arg{child};
1043 1026
1044 my $self; 1027 my $self;
1045 1028
1046 my $hslider = new CFPlus::UI::Slider 1029 my $hslider = new CFPlus::UI::Slider
1030 c_col => 0,
1031 c_row => 1,
1047 vertical => 0, 1032 vertical => 0,
1048 range => [0, 0, 1, 0.01], # HACK fix 1033 range => [0, 0, 1, 0.01], # HACK fix
1049 on_changed => sub { 1034 on_changed => sub {
1050 $self->{hpos} = $_[1]; 1035 $self->{hpos} = $_[1];
1051 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1036 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1052 }, 1037 },
1053 ; 1038 ;
1054 1039
1055 my $vslider = new CFPlus::UI::Slider 1040 my $vslider = new CFPlus::UI::Slider
1041 c_col => 1,
1042 c_row => 0,
1056 vertical => 1, 1043 vertical => 1,
1057 range => [0, 0, 1, 0.01], # HACK fix 1044 range => [0, 0, 1, 0.01], # HACK fix
1058 on_changed => sub { 1045 on_changed => sub {
1059 $self->{vpos} = $_[1]; 1046 $self->{vpos} = $_[1];
1060 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1047 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1071 row_expand => [1, 0], 1058 row_expand => [1, 0],
1072 %arg, 1059 %arg,
1073 ); 1060 );
1074 1061
1075 $self->{vp} = new CFPlus::UI::ViewPort 1062 $self->{vp} = new CFPlus::UI::ViewPort
1063 c_col => 0,
1064 c_row => 0,
1076 expand => 1, 1065 expand => 1,
1077 scroll_x => $self->{scroll_x}, 1066 scroll_x => $self->{scroll_x},
1078 scroll_y => $self->{scroll_y}, 1067 scroll_y => $self->{scroll_y},
1079 on_changed => sub { 1068 on_changed => sub {
1080 my ($vp, $x, $y) = @_; 1069 my ($vp, $x, $y) = @_;
1082 $vp->{parent}{hslider}->set_value ($x); 1071 $vp->{parent}{hslider}->set_value ($x);
1083 $vp->{parent}{vslider}->set_value ($y); 1072 $vp->{parent}{vslider}->set_value ($y);
1084 1073
1085 0 1074 0
1086 }, 1075 },
1076 on_size_allocate => sub {
1077 my ($vp, $w, $h) = @_;
1078 $vp->{parent}->update_slider;
1079 0
1080 },
1087 ; 1081 ;
1088 1082
1089 $self->SUPER::add_at (0, 0, $self->{vp}); 1083 $self->SUPER::add ($self->{vp});
1090 1084
1091 $self->add ($child) if $child; 1085 $self->add ($child) if $child;
1092 1086
1093 $self 1087 $self
1094} 1088}
1095
1096#TODO# update range on size_allocate depending on child
1097 1089
1098sub add { 1090sub add {
1099 my ($self, $widget) = @_; 1091 my ($self, $widget) = @_;
1100 1092
1101 $self->{vp}->add ($self->{child} = $widget); 1093 $self->{vp}->add ($self->{child} = $widget);
1102} 1094}
1103 1095
1096sub set_offset { shift->{vp}->set_offset (@_) }
1097sub set_center { shift->{vp}->set_center (@_) }
1098sub make_visible { shift->{vp}->make_visible (@_) }
1099
1104sub update_slider { 1100sub update_slider {
1105 my ($self) = @_; 1101 my ($self) = @_;
1106 1102
1107 my $child = ($self->{vp} or return)->child; 1103 my $child = ($self->{vp} or return)->child;
1108 1104
1105 if ($self->{scroll_x}) {
1109 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1106 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1110 $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]);
1111 1108
1112 my $visible = $w1 > $w2; 1109 my $visible = $w1 > $w2;
1113 if ($visible != $self->{hslider}{visible}) { 1110 if ($visible != $self->{hslider_visible}) {
1111 $self->{hslider_visible} = $visible;
1114 $visible ? $self->SUPER::add_at (0, 1, $self->{hslider}) 1112 $visible ? $self->SUPER::add ($self->{hslider})
1115 : $self->{hslider}->hide; 1113 : $self->SUPER::remove ($self->{hslider});
1114 }
1116 } 1115 }
1117 1116
1117 if ($self->{scroll_y}) {
1118 my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); 1118 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1119 $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]);
1120 1120
1121 my $visible = $h1 > $h2; 1121 my $visible = $h1 > $h2;
1122 if ($visible != $self->{vslider}{visible}) { 1122 if ($visible != $self->{vslider_visible}) {
1123 $self->{vslider_visible} = $visible;
1123 $visible ? $self->SUPER::add_at (1, 0, $self->{vslider}) 1124 $visible ? $self->SUPER::add ($self->{vslider})
1124 : $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);
1125 } 1145 };
1126}
1127
1128sub update {
1129 my ($self) = @_;
1130
1131 $self->update_slider;
1132 $self->SUPER::update;
1133} 1146}
1134 1147
1135sub invoke_mouse_wheel { 1148sub invoke_mouse_wheel {
1136 my ($self, $ev) = @_; 1149 my ($self, $ev) = @_;
1137 1150
1138 return 0 unless $ev->{dy}; # only vertical movements for now
1139
1140 $self->{vslider}->emit (mouse_wheel => $ev); 1151 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1152 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1141 1153
1142 1 1154 1
1143} 1155}
1144 1156
1145sub invoke_button_down { 1157sub invoke_button_down {
1146 my ($self, $ev, $x, $y) = @_; 1158 my ($self, $ev, $x, $y) = @_;
1147 1159
1148 if ($ev->{button} == 2) { 1160 if ($ev->{button} == 2) {
1149 $self->grab_focus; 1161 $self->start_dragging ($ev);
1150
1151 my $ox = $self->{vp}{view_x} + $ev->{x};
1152 my $oy = $self->{vp}{view_y} + $ev->{y};
1153
1154 $self->{motion} = sub {
1155 my ($ev, $x, $y) = @_;
1156
1157 $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y});
1158 $self->update;
1159 };
1160
1161 return 1; 1162 return 1;
1162 } 1163 }
1163 1164
1164 0 1165 0
1165} 1166}
1216 my ($w, $h) = @$self{qw(w h)}; 1217 my ($w, $h) = @$self{qw(w h)};
1217 1218
1218 glEnable GL_BLEND; 1219 glEnable GL_BLEND;
1219 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1220 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1220 glColor_premultiply @{ $self->{bg} }; 1221 glColor_premultiply @{ $self->{bg} };
1221
1222 glBegin GL_QUADS;
1223 glVertex 0 , 0;
1224 glVertex 0 , $h;
1225 glVertex $w, $h; 1222 glRect 0, 0, $w, $h;
1226 glVertex $w, 0;
1227 glEnd;
1228
1229 glDisable GL_BLEND; 1223 glDisable GL_BLEND;
1230 } 1224 }
1231 1225
1232 $self->SUPER::_draw; 1226 $self->SUPER::_draw;
1233} 1227}
1394 1388
1395sub border { 1389sub border {
1396 int $_[0]{border} * $::FONTSIZE 1390 int $_[0]{border} * $::FONTSIZE
1397} 1391}
1398 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
1399sub size_request { 1402sub size_request {
1400 my ($self) = @_; 1403 my ($self) = @_;
1401 1404
1402 $self->{title_widget}->size_request 1405 $self->{title_widget}->size_request
1403 if $self->{title_widget}; 1406 if $self->{title_widget};
1575 1578
1576############################################################################# 1579#############################################################################
1577 1580
1578package CFPlus::UI::Table; 1581package CFPlus::UI::Table;
1579 1582
1580our @ISA = CFPlus::UI::Base::; 1583our @ISA = CFPlus::UI::Container::;
1581 1584
1582use List::Util qw(max sum); 1585use List::Util qw(max sum);
1583 1586
1584use CFPlus::OpenGL; 1587use CFPlus::OpenGL;
1585 1588
1586sub new { 1589sub new {
1587 my $class = shift; 1590 my $class = shift;
1588 1591
1589 $class->SUPER::new ( 1592 $class->SUPER::new (
1590 children => [],
1591 col_expand => [], 1593 col_expand => [],
1592 row_expand => [], 1594 row_expand => [],
1593 @_, 1595 @_,
1594 ) 1596 )
1595} 1597}
1596 1598
1597sub children {
1598 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1599}
1600
1601# TODO: store row/col info in child widget and use standard add/del
1602sub add { 1599sub add {
1603 my $self = shift; 1600 my ($self, @widgets) = @_;
1604 1601
1605 Carp::cluck "please use the add_at method instead of calling add, thank you.\n";#d# 1602 for my $child (@widgets) {
1606 $self->add_at (@_); 1603 $child->{c_rowspan} ||= 1;
1604 $child->{c_colspan} ||= 1;
1605 }
1606
1607 $self->SUPER::add (@widgets);
1607} 1608}
1608 1609
1609sub add_at { 1610sub add_at {
1610 my $self = shift; 1611 my $self = shift;
1611 1612
1613 my @widgets;
1614
1612 while (@_) { 1615 while (@_) {
1613 my ($col, $row, $child) = splice @_, 0, 3, (); 1616 my ($col, $row, $child) = splice @_, 0, 3, ();
1614 1617
1615 $child->set_parent ($self); 1618 $child->{c_row} = $row;
1616 $self->{children}[$row][$col] = $child; 1619 $child->{c_col} = $col;
1617 }
1618 1620
1619 $self->{force_realloc} = 1; 1621 push @widgets, $child;
1620 $self->{force_size_alloc} = 1; 1622 }
1621 $self->realloc;
1622}
1623 1623
1624sub remove { 1624 $self->add (@widgets);
1625}
1626
1627sub get_wh {
1625 my ($self, $child) = @_; 1628 my ($self) = @_;
1626 1629
1627 for (@{ $self->{children} }) { 1630 my (@w, @h);
1628 for (@{ $_ || [] }) { 1631
1629 $_ = 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];
1630 } 1642 }
1631 } 1643 }
1632}
1633 1644
1634# TODO: move to container class maybe? send children a signal on removal? 1645 # second pass, rows
1635sub clear { 1646 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1636 my ($self) = @_;
1637
1638 my @children = $self->children;
1639 delete $self->{children};
1640
1641 for (@children) {
1642 delete $_->{parent};
1643 $_->hide;
1644 }
1645
1646 $self->realloc;
1647}
1648
1649sub get_wh {
1650 my ($self) = @_;
1651
1652 my (@w, @h);
1653
1654 for my $y (0 .. $#{$self->{children}}) {
1655 my $row = $self->{children}[$y]
1656 or next;
1657
1658 for my $x (0 .. $#$row) {
1659 my $widget = $row->[$x]
1660 or next;
1661 my ($w, $h) = @$widget{qw(req_w req_h)}; 1647 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1662 1648
1663 $w[$x] = max $w[$x], $w; 1649 my $sh = sum @h[$r .. $r + $rs - 1];
1664 $h[$y] = max $h[$y], $h; 1650
1651 if ($h > $sh) {
1652 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1665 } 1653 }
1666 } 1654 }
1667 1655
1668 (\@w, \@h) 1656 (\@w, \@h)
1669} 1657}
1685 my ($ws, $hs) = $self->get_wh; 1673 my ($ws, $hs) = $self->get_wh;
1686 1674
1687 my $req_w = (sum @$ws) || 1; 1675 my $req_w = (sum @$ws) || 1;
1688 my $req_h = (sum @$hs) || 1; 1676 my $req_h = (sum @$hs) || 1;
1689 1677
1690 # TODO: nicer code 1678 # now linearly scale the rows/columns to the allocated size
1691 my @col_expand = @{$self->{col_expand}}; 1679 my @col_expand = @{$self->{col_expand}};
1692 @col_expand = (1) x @$ws unless @col_expand; 1680 @col_expand = (1) x @$ws unless @col_expand;
1693 my $col_expand = (sum @col_expand) || 1; 1681 my $col_expand = (sum @col_expand) || 1;
1694 1682
1695 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1683 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1702 1690
1703 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1691 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1704 1692
1705 CFPlus::UI::harmonize $hs; 1693 CFPlus::UI::harmonize $hs;
1706 1694
1707 my $y; 1695 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1696 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1708 1697
1709 for my $r (0 .. $#{$self->{children}}) { 1698 for my $widget ($self->children) {
1710 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)};
1711 or next;
1712 1700
1713 my $x = 0; 1701 $widget->configure (
1714 my $row_h = $hs->[$r]; 1702 $x[$c], $y[$r],
1703 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1715 1704 );
1716 for my $c (0 .. $#$row) {
1717 my $col_w = $ws->[$c];
1718
1719 if (my $widget = $row->[$c]) {
1720 $widget->configure ($x, $y, $col_w, $row_h);
1721 }
1722
1723 $x += $col_w;
1724 }
1725
1726 $y += $row_h;
1727 } 1705 }
1728 1706
1729 1 1707 1
1730} 1708}
1731 1709
1732sub find_widget {
1733 my ($self, $x, $y) = @_;
1734
1735 $x -= $self->{x};
1736 $y -= $self->{y};
1737
1738 my $res;
1739
1740 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1741 $res = $_->find_widget ($x, $y)
1742 and return $res;
1743 }
1744
1745 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1746}
1747
1748sub _draw {
1749 my ($self) = @_;
1750
1751 for (grep $_, @{$self->{children}}) {
1752 $_->draw for grep $_, @$_;
1753 }
1754}
1755
1756############################################################################# 1710#############################################################################
1757 1711
1758package CFPlus::UI::Fixed; 1712package CFPlus::UI::Fixed;
1759 1713
1760use List::Util qw(min max); 1714use List::Util qw(min max);
1761 1715
1762our @ISA = CFPlus::UI::Container::; 1716our @ISA = CFPlus::UI::Container::;
1763 1717
1764sub add {
1765 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1766
1767 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1768 $self->SUPER::add ($child);
1769}
1770
1771sub _scale($$$) { 1718sub _scale($$$) {
1772 my ($mode, $val, $max) = @_; 1719 my ($rel, $val, $max) = @_;
1773 1720
1774 $mode eq "abs" ? $val 1721 $rel ? $val * $max : $val
1775 : $mode eq "rel" ? $val * $max
1776 : 0
1777} 1722}
1778 1723
1779sub size_request { 1724sub size_request {
1780 my ($self) = @_; 1725 my ($self) = @_;
1781 1726
1782 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1727 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1783 1728
1784 # determine overall size by querying abs widgets 1729 # determine overall size by querying abs widgets
1785 for my $child ($self->visible_children) { 1730 for my $child ($self->visible_children) {
1786 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};
1787 1734
1788 if ($pos eq "abs") {
1789 $w = _scale $size, $w, $child->{req_w};
1790 $h = _scale $size, $h, $child->{req_h};
1791
1792 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1735 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1793 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1736 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1794 } 1737 }
1795 } 1738 }
1796 1739
1797 my $W = $x2 - $x1; 1740 my $W = $x2 - $x1;
1798 my $H = $y2 - $y1; 1741 my $H = $y2 - $y1;
1799 1742
1800 # now layout remaining widgets 1743 # now layout remaining widgets
1801 for my $child ($self->visible_children) { 1744 for my $child ($self->visible_children) {
1802 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;
1803 1748
1804 if ($pos ne "abs") {
1805 $x = _scale $pos, $x, $W;
1806 $y = _scale $pos, $x, $H;
1807 $w = _scale $size, $w, $child->{req_w};
1808 $h = _scale $size, $h, $child->{req_h};
1809
1810 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1749 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1811 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1750 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1812 } 1751 }
1813 } 1752 }
1814 1753
1815 my $W = $x2 - $x1; 1754 my $W = $x2 - $x1;
1816 my $H = $y2 - $y1; 1755 my $H = $y2 - $y1;
1820 1759
1821sub invoke_size_allocate { 1760sub invoke_size_allocate {
1822 my ($self, $W, $H) = @_; 1761 my ($self, $W, $H) = @_;
1823 1762
1824 for my $child ($self->visible_children) { 1763 for my $child ($self->visible_children) {
1825 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;
1826 1766
1827 $x = _scale $pos, $x, $W; 1767 $x += $child->{c_halign} * $child->{req_w};
1828 $y = _scale $pos, $x, $H; 1768 $y += $child->{c_valign} * $child->{req_h};
1829 $w = _scale $size, $w, $child->{req_w};
1830 $h = _scale $size, $h, $child->{req_h};
1831 1769
1832 $child->configure ($x, $y, $w, $h); 1770 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1833 } 1771 }
1834 1772
1835 1 1773 1
1836} 1774}
1837 1775
1841 1779
1842our @ISA = CFPlus::UI::Container::; 1780our @ISA = CFPlus::UI::Container::;
1843 1781
1844sub size_request { 1782sub size_request {
1845 my ($self) = @_; 1783 my ($self) = @_;
1784
1785 my @children = $self->visible_children;
1846 1786
1847 $self->{vertical} 1787 $self->{vertical}
1848 ? ( 1788 ? (
1849 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1789 (List::Util::max map $_->{req_w}, @children),
1850 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1790 (List::Util::sum map $_->{req_h}, @children),
1851 ) 1791 )
1852 : ( 1792 : (
1853 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1793 (List::Util::sum map $_->{req_w}, @children),
1854 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1794 (List::Util::max map $_->{req_h}, @children),
1855 ) 1795 )
1856} 1796}
1857 1797
1858sub invoke_size_allocate { 1798sub invoke_size_allocate {
1859 my ($self, $w, $h) = @_; 1799 my ($self, $w, $h) = @_;
1983 1923
1984 delete $self->{ox}; 1924 delete $self->{ox};
1985 $self->SUPER::realloc; 1925 $self->SUPER::realloc;
1986} 1926}
1987 1927
1928sub clear {
1929 my ($self) = @_;
1930
1931 $self->set_text ("");
1932}
1933
1988sub set_text { 1934sub set_text {
1989 my ($self, $text) = @_; 1935 my ($self, $text) = @_;
1990 1936
1991 return if $self->{text} eq "T$text"; 1937 return if $self->{text} eq "T$text";
1992 $self->{text} = "T$text"; 1938 $self->{text} = "T$text";
2015 1961
2016sub size_request { 1962sub size_request {
2017 my ($self) = @_; 1963 my ($self) = @_;
2018 1964
2019 $self->{size_req} ||= do { 1965 $self->{size_req} ||= do {
1966 my ($max_w, $max_h) = $self->get_max_wh;
1967
2020 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1968 $self->{layout}->set_font ($self->{font}) if $self->{font};
2021 $self->{layout}->set_width ($self->{max_w} || -1); 1969 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
2022 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1970 $self->{layout}->set_ellipsise ($self->{ellipsise});
2023 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1971 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
2024 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1972 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2025 1973
2026 my ($w, $h) = $self->{layout}->size; 1974 my ($w, $h) = $self->{layout}->size;
2098 : ($self->{w} - $size->[0]) * 0.5); 2046 : ($self->{w} - $size->[0]) * 0.5);
2099 2047
2100 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2048 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2101 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2049 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2102 : ($self->{h} - $size->[1]) * 0.5); 2050 : ($self->{h} - $size->[1]) * 0.5);
2051
2052 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2103 }; 2053 };
2104 2054
2105 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2055# unless ($self->{list}) {
2106 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2056# $self->{list} = CFPlus::OpenGL::glGenList;
2107 2057# CFPlus::OpenGL::glNewList $self->{list};
2108 $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;
2109} 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#}
2110 2074
2111############################################################################# 2075#############################################################################
2112 2076
2113package CFPlus::UI::EntryBase; 2077package CFPlus::UI::EntryBase;
2114 2078
2120 my $class = shift; 2084 my $class = shift;
2121 2085
2122 $class->SUPER::new ( 2086 $class->SUPER::new (
2123 fg => [1, 1, 1], 2087 fg => [1, 1, 1],
2124 bg => [0, 0, 0, 0.2], 2088 bg => [0, 0, 0, 0.2],
2089 outline => [0.6, 0.3, 0.1],
2125 active_bg => [1, 1, 1, 0.5], 2090 active_bg => [0, 0, 1, .2],
2126 active_fg => [0, 0, 0], 2091 active_fg => [1, 1, 1],
2092 active_outline => [1, 1, 0],
2127 can_hover => 1, 2093 can_hover => 1,
2128 can_focus => 1, 2094 can_focus => 1,
2129 valign => 0, 2095 valign => 0,
2130 can_events => 1, 2096 can_events => 1,
2131 ellipsise => 0, 2097 ellipsise => 0,
2271 glColor_premultiply @{$self->{bg}}; 2237 glColor_premultiply @{$self->{bg}};
2272 } 2238 }
2273 2239
2274 glEnable GL_BLEND; 2240 glEnable GL_BLEND;
2275 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2241 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2276 glBegin GL_QUADS;
2277 glVertex 0 , 0;
2278 glVertex 0 , $self->{h};
2279 glVertex $self->{w}, $self->{h}; 2242 glRect 0, 0, $self->{w}, $self->{h};
2280 glVertex $self->{w}, 0;
2281 glEnd;
2282 glDisable GL_BLEND; 2243 glDisable GL_BLEND;
2283 2244
2284 $self->SUPER::_draw; 2245 $self->SUPER::_draw;
2285 2246
2286 #TODO: force update every cursor change :( 2247 #TODO: force update every cursor change :(
2288 2249
2289 unless (exists $self->{cur_h}) { 2250 unless (exists $self->{cur_h}) {
2290 my $text = substr $self->{text}, 0, $self->{cursor}; 2251 my $text = substr $self->{text}, 0, $self->{cursor};
2291 utf8::encode $text; 2252 utf8::encode $text;
2292 2253
2293 @$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);
2294 } 2255 }
2295 2256
2257 glColor_premultiply @{$self->{active_fg}};
2296 glBegin GL_LINES; 2258 glBegin GL_LINES;
2297 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};
2298 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;
2299 glEnd; 2275 glEnd;
2300 } 2276 }
2301} 2277}
2302 2278
2303############################################################################# 2279#############################################################################
2390 1 2366 1
2391} 2367}
2392 2368
2393############################################################################# 2369#############################################################################
2394 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
2395package CFPlus::UI::Button; 2420package CFPlus::UI::Button;
2396 2421
2397our @ISA = CFPlus::UI::Label::; 2422our @ISA = CFPlus::UI::Label::;
2398 2423
2399use CFPlus::OpenGL; 2424use CFPlus::OpenGL;
2503sub _draw { 2528sub _draw {
2504 my ($self) = @_; 2529 my ($self) = @_;
2505 2530
2506 $self->SUPER::_draw; 2531 $self->SUPER::_draw;
2507 2532
2508 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2533 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2509 2534
2510 my ($w, $h) = @$self{qw(w h)}; 2535 my ($w, $h) = @$self{qw(w h)};
2511 2536
2512 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;
2513 2538
2533sub new { 2558sub new {
2534 my $class = shift; 2559 my $class = shift;
2535 2560
2536 my $self = $class->SUPER::new ( 2561 my $self = $class->SUPER::new (
2537 can_events => 0, 2562 can_events => 0,
2563 scale => 1,
2538 @_, 2564 @_,
2539 ); 2565 );
2540 2566
2541 $self->{path} || $self->{tex} 2567 $self->{path} || $self->{tex}
2542 or Carp::croak "'path' or 'tex' attributes required"; 2568 or Carp::croak "'path' or 'tex' attributes required";
2567} 2593}
2568 2594
2569sub size_request { 2595sub size_request {
2570 my ($self) = @_; 2596 my ($self) = @_;
2571 2597
2572 ($self->{tex}{w}, $self->{tex}{h}) 2598 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2573} 2599}
2574 2600
2575sub _draw { 2601sub _draw {
2576 my ($self) = @_; 2602 my ($self) = @_;
2577 2603
2587 } 2613 }
2588 2614
2589 glEnable GL_TEXTURE_2D; 2615 glEnable GL_TEXTURE_2D;
2590 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2616 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2591 2617
2592 $tex->draw_quad (0, 0, $w, $h); 2618 $tex->draw_quad_alpha (0, 0, $w, $h);
2593 2619
2594 glDisable GL_TEXTURE_2D; 2620 glDisable GL_TEXTURE_2D;
2595} 2621}
2596 2622
2597############################################################################# 2623#############################################################################
2616 align => 0, 2642 align => 0,
2617 valign => 0, 2643 valign => 0,
2618 can_events => 1, 2644 can_events => 1,
2619 @_ 2645 @_
2620 ); 2646 );
2647}
2648
2649sub invoke_button_down {
2650 my ($self, $ev, $x, $y) = @_;
2651
2652 1
2621} 2653}
2622 2654
2623sub invoke_button_up { 2655sub invoke_button_up {
2624 my ($self, $ev, $x, $y) = @_; 2656 my ($self, $ev, $x, $y) = @_;
2625 2657
2765 glDisable GL_TEXTURE_2D; 2797 glDisable GL_TEXTURE_2D;
2766} 2798}
2767 2799
2768############################################################################# 2800#############################################################################
2769 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
2770package CFPlus::UI::Gauge; 2940package CFPlus::UI::Gauge;
2771 2941
2772our @ISA = CFPlus::UI::VBox::; 2942our @ISA = CFPlus::UI::VBox::;
2773 2943
2774sub new { 2944sub new {
2899 3069
2900 $self->SUPER::invoke_button_down ($ev, $x, $y); 3070 $self->SUPER::invoke_button_down ($ev, $x, $y);
2901 3071
2902 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3072 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2903 3073
2904 $self->invoke_mouse_motion ($ev, $x, $y) 3074 $self->invoke_mouse_motion ($ev, $x, $y);
3075
3076 1
2905} 3077}
2906 3078
2907sub invoke_mouse_motion { 3079sub invoke_mouse_motion {
2908 my ($self, $ev, $x, $y) = @_; 3080 my ($self, $ev, $x, $y) = @_;
2909 3081
2929 3101
2930 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3102 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2931 3103
2932 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3104 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2933 3105
2934 ! ! $delta 3106 1
2935} 3107}
2936 3108
2937sub update { 3109sub update {
2938 my ($self) = @_; 3110 my ($self) = @_;
2939 3111
3070} 3242}
3071 3243
3072sub size_request { 3244sub size_request {
3073 my ($self) = @_; 3245 my ($self) = @_;
3074 3246
3075 my ($empty, $slider) = @{ $self->{children} }; 3247 my ($empty, $slider) = $self->visible_children;
3076 3248
3077 local $self->{children} = [$empty, $slider]; 3249 local $self->{children} = [$empty, $slider];
3078 $self->SUPER::size_request 3250 $self->SUPER::size_request
3079} 3251}
3080 3252
3269 3441
3270 if ($y0 < $y + $h && $y < $y1) { 3442 if ($y0 < $y + $h && $y < $y1) {
3271 my $layout = $self->get_layout ($para); 3443 my $layout = $self->get_layout ($para);
3272 3444
3273 $layout->render ($para->{indent}, $y - $y0); 3445 $layout->render ($para->{indent}, $y - $y0);
3446 $layout->draw;
3274 3447
3275 if (my @w = @{ $para->{widget} }) { 3448 if (my @w = @{ $para->{widget} }) {
3276 my @s = $layout->get_shapes; 3449 my @s = $layout->get_shapes;
3277 3450
3278 for (@w) { 3451 for (@w) {
3400} 3573}
3401 3574
3402sub set_tooltip_from { 3575sub set_tooltip_from {
3403 my ($self, $widget) = @_; 3576 my ($self, $widget) = @_;
3404 3577
3405 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3406 if $widget->{tooltip} =~ /^#(.*)$/;
3407
3408 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 =~ /^#(.*)$/;
3409 3583
3410 if ($ENV{CFPLUS_DEBUG} & 2) { 3584 if ($ENV{CFPLUS_DEBUG} & 2) {
3411 $tooltip .= "\n\n" . (ref $widget) . "\n" 3585 $tip .= "\n\n" . (ref $widget) . "\n"
3412 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3586 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3413 . "req $widget->{req_w} $widget->{req_h}\n" 3587 . "req $widget->{req_w} $widget->{req_h}\n"
3414 . "visible $widget->{visible}"; 3588 . "visible $widget->{visible}";
3415 } 3589 }
3416 3590
3417 $tooltip =~ s/^\n+//; 3591 $tip =~ s/^\n+//;
3418 $tooltip =~ s/\n+$//; 3592 $tip =~ s/\n+$//;
3419 3593
3420 $self->add (new CFPlus::UI::Label 3594 $self->add (new CFPlus::UI::Label
3421 markup => $tooltip, 3595 markup => $tip,
3422 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3596 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3423 fontsize => 0.8, 3597 fontsize => 0.8,
3424 style => 1, # FLAG_INVERSE 3598 style => 1, # FLAG_INVERSE
3425 ellipsise => 0, 3599 ellipsise => 0,
3426 font => ($widget->{tooltip_font} || $::FONT_PROP), 3600 font => ($widget->{tooltip_font} || $::FONT_PROP),
3464} 3638}
3465 3639
3466sub _draw { 3640sub _draw {
3467 my ($self) = @_; 3641 my ($self) = @_;
3468 3642
3469 glTranslate 0.375, 0.375;
3470
3471 my ($w, $h) = @$self{qw(w h)}; 3643 my ($w, $h) = @$self{qw(w h)};
3472 3644
3473 glColor 1, 0.8, 0.4; 3645 glColor 1, 0.8, 0.4;
3474 glBegin GL_QUADS; 3646 glRect 0, 0, $w, $h;
3475 glVertex 0 , 0;
3476 glVertex 0 , $h;
3477 glVertex $w, $h;
3478 glVertex $w, 0;
3479 glEnd;
3480 3647
3481 glColor 0, 0, 0; 3648 glColor 0, 0, 0;
3482 glBegin GL_LINE_LOOP; 3649 glRect_lineloop .5, .5, $w + .5, $h + .5;
3483 glVertex 0 , 0;
3484 glVertex 0 , $h;
3485 glVertex $w, $h;
3486 glVertex $w, 0;
3487 glEnd;
3488 3650
3489 glTranslate 2 - 0.375, 2 - 0.375; 3651 glTranslate 2, 2;
3490 3652
3491 $self->SUPER::_draw; 3653 $self->SUPER::_draw;
3492} 3654}
3493 3655
3494############################################################################# 3656#############################################################################
3511 ); 3673 );
3512 3674
3513 if ($self->{anim} && $self->{animspeed}) { 3675 if ($self->{anim} && $self->{animspeed}) {
3514 CFPlus::weaken (my $widget = $self); 3676 CFPlus::weaken (my $widget = $self);
3515 3677
3516 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3678 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3517 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed}; 3679 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3518 $self->{timer} = Event->timer (
3519 parked => 1,
3520 cb => sub {
3521 return unless $::CONN && $widget; 3680 return unless $::CONN;
3522 3681
3682 my $w = $widget
3683 or return;
3684
3523 ++$widget->{frame}; 3685 ++$w->{frame};
3524 $widget->update_face; 3686 $w->update_face;
3687
3688 # somehow, $widget can go away
3525 $widget->update; 3689 $w->update;
3526
3527 $widget->update_timer; 3690 $w->update_timer;
3528 },
3529 ); 3691 };
3530 3692
3531 $self->update_face; 3693 $self->update_face;
3532 $self->update_timer; 3694 $self->update_timer;
3533 } 3695 }
3534 3696
3539 my ($self) = @_; 3701 my ($self) = @_;
3540 3702
3541 return unless $self->{timer}; 3703 return unless $self->{timer};
3542 3704
3543 if ($self->{visible}) { 3705 if ($self->{visible}) {
3544 $self->{timer}->at (
3545 $self->{anim_start}
3546 + $self->{animspeed}
3547 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3548 );
3549 $self->{timer}->start; 3706 $self->{timer}->start;
3550 } else { 3707 } else {
3551 $self->{timer}->stop; 3708 $self->{timer}->stop;
3552 } 3709 }
3553} 3710}
3554 3711
3555sub update_face { 3712sub update_face {
3556 my ($self) = @_; 3713 my ($self) = @_;
3557 3714
3558 return unless $::CONN;
3559
3560 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3561 if ($anim && @$anim) {
3562 delete $self->{wait_face};
3563 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3564 }
3565 }
3566}
3567
3568sub size_request {
3569 my ($self) = @_;
3570
3571 if ($::CONN) { 3715 if ($::CONN) {
3572 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3716 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3573 if (my $tex = $::CONN->{texture}[$faceid]) { 3717 if ($anim && @$anim) {
3574 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3718 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3575 } else { 3719 delete $self->{face_change_cb};
3576 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3720
3577 $self->realloc; 3721 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3722 unless ($tex->{name} || $tex->{loading}) {
3723 $tex->upload (sub { $self->reconfigure });
3724 }
3578 }); 3725 }
3579 } 3726 }
3580 } 3727 }
3581 } 3728 }
3729}
3730
3731sub size_request {
3732 my ($self) = @_;
3733
3734 if ($::CONN) {
3735 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3736 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3737 if ($tex->{name}) {
3738 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3739 } elsif (!$tex->{loading}) {
3740 $tex->upload (sub { $self->reconfigure });
3741 }
3742 }
3743
3744 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3745 }
3746 }
3582 3747
3583 ($self->{size_w} || 8, $self->{size_h} || 8) 3748 ($self->{size_w} || 8, $self->{size_h} || 8)
3584} 3749}
3585 3750
3586sub update { 3751sub update {
3600} 3765}
3601 3766
3602sub _draw { 3767sub _draw {
3603 my ($self) = @_; 3768 my ($self) = @_;
3604 3769
3605 return unless $::CONN;
3606
3607 $self->SUPER::_draw; 3770 $self->SUPER::_draw;
3608 3771
3609 my $faceid = $::CONN->{faceid}[$self->{face}] 3772 if (my $tex = $self->{tex}) {
3610 or return;
3611
3612 my $tex = $::CONN->{texture}[$faceid];
3613
3614 if ($tex) {
3615 glEnable GL_TEXTURE_2D; 3773 glEnable GL_TEXTURE_2D;
3616 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3774 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3617 glColor 0, 0, 0, 1; 3775 glColor 0, 0, 0, 1;
3618 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3776 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3619 glDisable GL_TEXTURE_2D; 3777 glDisable GL_TEXTURE_2D;
3671 children => [ 3829 children => [
3672 (new CFPlus::UI::Label markup => $left, expand => 1), 3830 (new CFPlus::UI::Label markup => $left, expand => 1),
3673 (new CFPlus::UI::Label markup => $right, align => +1), 3831 (new CFPlus::UI::Label markup => $right, align => +1),
3674 ], 3832 ],
3675 ; 3833 ;
3676 3834
3677 } else { 3835 } else {
3678 $widget = new CFPlus::UI::Label 3836 $widget = new CFPlus::UI::Label
3679 can_hover => 1, 3837 can_hover => 1,
3680 can_events => 1, 3838 can_events => 1,
3681 markup => $widget, 3839 markup => $widget,
3806 3964
3807############################################################################# 3965#############################################################################
3808 3966
3809package CFPlus::UI::Notebook; 3967package CFPlus::UI::Notebook;
3810 3968
3969use CFPlus::OpenGL;
3970
3811our @ISA = CFPlus::UI::VBox::; 3971our @ISA = CFPlus::UI::VBox::;
3812 3972
3813sub new { 3973sub new {
3814 my $class = shift; 3974 my $class = shift;
3815 3975
3816 my $self = $class->SUPER::new ( 3976 my $self = $class->SUPER::new (
3817 buttonbar => (new CFPlus::UI::Buttonbar), 3977 buttonbar => (new CFPlus::UI::Buttonbar),
3818 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3978 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3979 active_outline => [.7, .7, 0.2],
3819 # filter => # will be put between multiplexer and $self 3980 # filter => # will be put between multiplexer and $self
3820 @_, 3981 @_,
3821 ); 3982 );
3822 3983
3823 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3984 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3824 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3985 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3825 3986
3987 {
3988 Scalar::Util::weaken (my $wself = $self);
3989
3990 $self->{multiplexer}->connect (c_add => sub {
3991 my ($mplex, $widgets) = @_;
3992
3993 for my $child (@$widgets) {
3994 Scalar::Util::weaken $child;
3995 $child->{c_tab_} ||= do {
3996 my $tab =
3997 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base")
3998 ? $child->{c_tab}
3999 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4000
4001 $tab->connect (activate => sub {
4002 $wself->set_current_page ($child);
4003 });
4004
4005 $tab
4006 };
4007
4008 $self->{buttonbar}->add ($child->{c_tab_});
4009 }
4010 });
4011
4012 $self->{multiplexer}->connect (c_remove => sub {
4013 my ($mplex, $widgets) = @_;
4014
4015 for my $child (@$widgets) {
4016 $wself->{buttonbar}->remove ($child->{c_tab_});
4017 }
4018 });
4019 }
4020
3826 $self 4021 $self
3827} 4022}
3828 4023
3829sub add { 4024sub add {
4025 my ($self, @widgets) = @_;
4026
4027 $self->{multiplexer}->add (@widgets)
4028}
4029
4030sub remove {
4031 my ($self, @widgets) = @_;
4032
4033 $self->{multiplexer}->remove (@widgets)
4034}
4035
4036sub pages {
4037 my ($self) = @_;
4038 $self->{multiplexer}->children
4039}
4040
4041sub add_tab {
3830 my ($self, $title, $widget, $tooltip) = @_; 4042 my ($self, $title, $widget, $tooltip) = @_;
3831 4043
3832 CFPlus::weaken $self; 4044 $title = [$title, $tooltip] unless ref $title;
4045 $widget->{c_tab} = $title;
3833 4046
3834 $self->{buttonbar}->add (new CFPlus::UI::Button
3835 markup => $title,
3836 tooltip => $tooltip,
3837 on_activate => sub { $self->set_current_page ($widget) },
3838 );
3839
3840 $self->{multiplexer}->add ($widget); 4047 $self->add ($widget);
3841} 4048}
3842 4049
3843sub get_current_page { 4050sub get_current_page {
3844 my ($self) = @_; 4051 my ($self) = @_;
3845 4052
3849sub set_current_page { 4056sub set_current_page {
3850 my ($self, $page) = @_; 4057 my ($self, $page) = @_;
3851 4058
3852 $self->{multiplexer}->set_current_page ($page); 4059 $self->{multiplexer}->set_current_page ($page);
3853 $self->emit (page_changed => $self->{multiplexer}{current}); 4060 $self->emit (page_changed => $self->{multiplexer}{current});
4061}
4062
4063sub _draw {
4064 my ($self) = @_;
4065
4066 $self->SUPER::_draw ();
4067
4068 if (my $cur = $self->{multiplexer}{current}) {
4069 if ($cur = $cur->{c_tab_}) {
4070 glTranslate $self->{buttonbar}{x} + $cur->{x},
4071 $self->{buttonbar}{y} + $cur->{y};
4072 glLineWidth 3;
4073 #glEnable GL_BLEND;
4074 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4075 glColor @{$self->{active_outline}};
4076 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4077 glLineWidth 1;
4078 #glDisable GL_BLEND;
4079 }
4080 }
3854} 4081}
3855 4082
3856############################################################################# 4083#############################################################################
3857 4084
3858package CFPlus::UI::Selector; 4085package CFPlus::UI::Selector;
3890} 4117}
3891 4118
3892sub _set_value { 4119sub _set_value {
3893 my ($self, $value) = @_; 4120 my ($self, $value) = @_;
3894 4121
3895 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4122 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4123 $item ||= $self->{options}[0]
3896 or return; 4124 or return;
3897 4125
3898 $self->{value} = $item->[0]; 4126 $self->{value} = $item->[0];
3899 $self->set_markup ("$item->[1] ⇓"); 4127 $self->set_markup ("$item->[1] ⇓");
3900 $self->set_tooltip ($item->[2]); 4128# $self->set_tooltip ($item->[2]);
3901} 4129}
3902 4130
3903sub set_value { 4131sub set_value {
3904 my ($self, $value) = @_; 4132 my ($self, $value) = @_;
3905 4133
3906 return unless $self->{value} ne $value; 4134 return unless $self->{value} ne $value;
3907 4135
3908 $self->_set_value ($value); 4136 $self->_set_value ($value);
3909 $self->emit (changed => $value); 4137 $self->emit (changed => $value);
4138}
4139
4140sub set_options {
4141 my ($self, $options) = @_;
4142
4143 $self->{options} = $options;
4144 $self->_set_value ($self->{value});
3910} 4145}
3911 4146
3912############################################################################# 4147#############################################################################
3913 4148
3914package CFPlus::UI::Statusbox; 4149package CFPlus::UI::Statusbox;
3923 @_, 4158 @_,
3924 ); 4159 );
3925 4160
3926 CFPlus::weaken (my $this = $self); 4161 CFPlus::weaken (my $this = $self);
3927 4162
3928 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4163 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3929 4164
3930 $self 4165 $self
3931} 4166}
3932 4167
3933sub reorder { 4168sub reorder {
3941 4176
3942 while (my ($k, $v) = each %{ $self->{item} }) { 4177 while (my ($k, $v) = each %{ $self->{item} }) {
3943 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4178 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3944 } 4179 }
3945 4180
4181 $self->{timer}->set (1, 1);
4182
3946 my @widgets; 4183 my @widgets;
3947 4184
3948 my @items = sort { 4185 my @items = sort {
3949 $a->{pri} <=> $b->{pri} 4186 $a->{pri} <=> $b->{pri}
3950 or $b->{id} <=> $a->{id} 4187 or $b->{id} <=> $a->{id}
3951 } values %{ $self->{item} }; 4188 } values %{ $self->{item} };
3952
3953 $self->{timer}->interval (1);
3954 4189
3955 my $count = 10 + 1; 4190 my $count = 10 + 1;
3956 for my $item (@items) { 4191 for my $item (@items) {
3957 last unless --$count; 4192 last unless --$count;
3958 4193
3982 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4217 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3983 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4218 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3984 $label->update; 4219 $label->update;
3985 $label->set_max_size (undef, $label->{req_h} * $diff) 4220 $label->set_max_size (undef, $label->{req_h} * $diff)
3986 if $diff < 1; 4221 if $diff < 1;
3987 $self->{timer}->interval (1/30); 4222 $self->{timer}->set (1/30, 1/30);
3988 } else { 4223 } else {
3989 $label->{fg}[3] = $item->{fg}[3] || 1; 4224 $label->{fg}[3] = $item->{fg}[3] || 1;
3990 } 4225 }
3991 4226
3992 push @widgets, $label; 4227 push @widgets, $label;
4128} 4363}
4129 4364
4130sub update { 4365sub update {
4131 my ($self) = @_; 4366 my ($self) = @_;
4132 4367
4133 $::WANT_REFRESH++; 4368 $::WANT_REFRESH->start;
4134} 4369}
4135 4370
4136sub add { 4371sub add {
4137 my ($self, @children) = @_; 4372 my ($self, @children) = @_;
4138 4373
4175 while ($self->{refresh_hook}) { 4410 while ($self->{refresh_hook}) {
4176 $_->() 4411 $_->()
4177 for values %{delete $self->{refresh_hook}}; 4412 for values %{delete $self->{refresh_hook}};
4178 } 4413 }
4179 4414
4180 if ($self->{realloc}) { 4415 while ($self->{realloc}) {
4181 my %queue; 4416 my %queue;
4182 my @queue; 4417 my @queue;
4183 my $widget; 4418 my $widget;
4184 4419
4185 outer: 4420 outer:
4232 } 4467 }
4233 } 4468 }
4234 4469
4235 delete $self->{realloc}{$widget+0}; 4470 delete $self->{realloc}{$widget+0};
4236 } 4471 }
4237 }
4238 4472
4239 while (my $size_alloc = delete $self->{size_alloc}) { 4473 while (my $size_alloc = delete $self->{size_alloc}) {
4240 my @queue = sort { $b->{visible} <=> $a->{visible} } 4474 my @queue = sort { $a->{visible} <=> $b->{visible} }
4241 values %$size_alloc; 4475 values %$size_alloc;
4242 4476
4243 while () { 4477 while () {
4244 my $widget = pop @queue || last; 4478 my $widget = pop @queue || last;
4245 4479
4246 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4480 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4247 4481
4248 $w = 0 if $w < 0;
4249 $h = 0 if $h < 0;
4250
4251 $w = max $widget->{min_w}, $w; 4482 $w = max $widget->{min_w}, $w;
4252 $h = max $widget->{min_h}, $h; 4483 $h = max $widget->{min_h}, $h;
4253 4484
4254# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4485# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4255# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4486# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4256 4487
4257 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4488 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4258 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4489 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4259 4490
4260 $w = int $w + 0.5; 4491 $w = int $w + 0.5;
4261 $h = int $h + 0.5; 4492 $h = int $h + 0.5;
4262 4493
4263 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4494 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
4264 $widget->{old_w} = $widget->{w}; 4495 $widget->{old_w} = $widget->{w};
4265 $widget->{old_h} = $widget->{h}; 4496 $widget->{old_h} = $widget->{h};
4266 4497
4267 $widget->{w} = $w; 4498 $widget->{w} = $w;
4268 $widget->{h} = $h; 4499 $widget->{h} = $h;
4269 4500
4270 $widget->emit (size_allocate => $w, $h); 4501 $widget->emit (size_allocate => $w, $h);
4502 }
4271 } 4503 }
4272 } 4504 }
4273 } 4505 }
4274 4506
4275 while ($self->{post_alloc_hook}) { 4507 while ($self->{post_alloc_hook}) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines