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.435 by root, Wed Aug 22 21:40:43 2007 UTC

30 $TOOLTIP->{owner} = $widget; 30 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; 31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
32 32
33 return if $ENV{CFPLUS_DEBUG} & 8; 33 return if $ENV{CFPLUS_DEBUG} & 8;
34 34
35 my $tip = $widget->{tooltip};
36
37 $tip = $tip->($widget) if CODE:: eq ref $tip;
38
39 $TOOLTIP->set_tooltip_from ($widget); 35 $TOOLTIP->set_tooltip_from ($widget);
40 $TOOLTIP->show; 36 $TOOLTIP->show;
41 } 37 }
42 38
43 return; 39 return;
113 } 109 }
114 110
115 if ($GRAB) { 111 if ($GRAB) {
116 if ($ev->{button} == 4 || $ev->{button} == 5) { 112 if ($ev->{button} == 4 || $ev->{button} == 5) {
117 # mousewheel 113 # mousewheel
118 $ev->{dx} = 0;
119 $ev->{dy} = $ev->{button} * 2 - 9; 114 my $delta = $ev->{button} * 2 - 9;
115 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT;
116
117 $ev->{dx} = $shift ? $delta : 0;
118 $ev->{dy} = $shift ? 0 : $delta;
119
120 $GRAB->emit (mouse_wheel => $ev); 120 $GRAB->emit (mouse_wheel => $ev);
121 } else { 121 } else {
122 $GRAB->emit (button_down => $ev) 122 $GRAB->emit (button_down => $ev)
123 } 123 }
124 } 124 }
269 $self->emit ("destroy"); 269 $self->emit ("destroy");
270 %$self = (); 270 %$self = ();
271} 271}
272 272
273sub TO_JSON { 273sub TO_JSON {
274 { __widget_ref__ => $_[0]{s_id} } 274 { __w_ => $_[0]{s_id} }
275} 275}
276 276
277sub show { 277sub show {
278 my ($self) = @_; 278 my ($self) = @_;
279 279
589 589
590# using global variables seems a bit hacky, but passing through all drawing 590# using global variables seems a bit hacky, but passing through all drawing
591# functions seems pointless. 591# functions seems pointless.
592our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn 592our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
593 593
594sub draw { 594#sub draw {
595 my ($self) = @_; 595#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 596
641sub _draw { 597sub _draw {
642 my ($self) = @_; 598 my ($self) = @_;
643 599
644 warn "no draw defined for $self\n"; 600 warn "no draw defined for $self\n";
645} 601}
646 602
647my $cntx;#d#
648sub DESTROY { 603sub DESTROY {
649 my ($self) = @_; 604 my ($self) = @_;
650 605
651 return if CFPlus::in_destruct; 606 return if CFPlus::in_destruct;
652 607
608 local $@;
653 eval { $self->destroy }; 609 eval { $self->destroy };
654 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 610 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
655 611
656 delete $WIDGET{$self+0}; 612 delete $WIDGET{$self+0};
657} 613}
665use strict; 621use strict;
666use CFPlus::OpenGL; 622use CFPlus::OpenGL;
667 623
668sub new { 624sub new {
669 my $class = shift; 625 my $class = shift;
670
671 # range [value, low, high, page]
672 626
673 $class->SUPER::new ( 627 $class->SUPER::new (
674 #bg => [0, 0, 0, 0.2], 628 #bg => [0, 0, 0, 0.2],
675 #active_bg => [1, 1, 1, 0.5], 629 #active_bg => [1, 1, 1, 0.5],
676 @_ 630 @_
688 my ($w, $h) = @$self{qw(w h)}; 642 my ($w, $h) = @$self{qw(w h)};
689 643
690 glEnable GL_BLEND; 644 glEnable GL_BLEND;
691 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 645 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
692 glColor_premultiply @$color; 646 glColor_premultiply @$color;
693
694 glBegin GL_QUADS;
695 glVertex 0 , 0;
696 glVertex 0 , $h;
697 glVertex $w, $h; 647 glRect 0, 0, $w, $h;
698 glVertex $w, 0;
699 glEnd;
700
701 glDisable GL_BLEND; 648 glDisable GL_BLEND;
702 } 649 }
703} 650}
704 651
705############################################################################# 652#############################################################################
737 can_events => 0, 684 can_events => 0,
738 %arg, 685 %arg,
739 ); 686 );
740 687
741 $self->add (@$children) 688 $self->add (@$children)
742 if $children; 689 if $children && @$children;
743 690
744 $self 691 $self
745} 692}
746 693
747sub realloc { 694sub realloc {
756 my ($self, @widgets) = @_; 703 my ($self, @widgets) = @_;
757 704
758 $_->set_parent ($self) 705 $_->set_parent ($self)
759 for @widgets; 706 for @widgets;
760 707
708 # TODO: only do this in widgets that need it, e.g. root, fixed
761 use sort 'stable'; 709 use sort 'stable';
762 710
763 $self->{children} = [ 711 $self->{children} = [
764 sort { $a->{z} <=> $b->{z} } 712 sort { $a->{z} <=> $b->{z} }
765 @{$self->{children}}, @widgets 713 @{$self->{children}}, @widgets
766 ]; 714 ];
767 715
768 $self->realloc; 716 $self->realloc;
717
718 $self->emit (c_add => \@widgets);
719
720 map $_+0, @widgets
769} 721}
770 722
771sub children { 723sub children {
772 @{ $_[0]{children} } 724 @{ $_[0]{children} }
773} 725}
774 726
775sub remove { 727sub remove {
776 my ($self, $child) = @_; 728 my ($self, @widgets) = @_;
777 729
730 $self->emit (c_remove => \@widgets);
731
732 for my $child (@widgets) {
778 delete $child->{parent}; 733 delete $child->{parent};
779 $child->hide; 734 $child->hide;
780
781 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 735 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
736 }
782 737
783 $self->realloc; 738 $self->realloc;
784} 739}
785 740
786sub clear { 741sub clear {
787 my ($self) = @_; 742 my ($self) = @_;
788 743
789 my $children = delete $self->{children}; 744 my $children = $self->{children};
790 $self->{children} = []; 745 $self->{children} = [];
791 746
792 for (@$children) { 747 for (@$children) {
793 delete $_->{parent}; 748 delete $_->{parent};
794 $_->hide; 749 $_->hide;
814} 769}
815 770
816sub _draw { 771sub _draw {
817 my ($self) = @_; 772 my ($self) = @_;
818 773
819 $_->draw for @{$self->{children}}; 774 $_->draw for $self->visible_children;
820} 775}
821 776
822############################################################################# 777#############################################################################
823 778
824package CFPlus::UI::Bin; 779package CFPlus::UI::Bin;
834} 789}
835 790
836sub add { 791sub add {
837 my ($self, $child) = @_; 792 my ($self, $child) = @_;
838 793
839 $self->SUPER::remove ($_) for @{ $self->{children} }; 794 $self->clear;
840 $self->SUPER::add ($child); 795 $self->SUPER::add ($child);
841} 796}
842 797
843sub remove { 798sub remove {
844 my ($self, $widget) = @_; 799 my ($self, $widget) = @_;
954sub size_request { 909sub size_request {
955 my ($self) = @_; 910 my ($self) = @_;
956 911
957 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 912 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
958 913
959 $w = 10 if $self->{scroll_x}; 914 $w = 1 if $self->{scroll_x};
960 $h = 10 if $self->{scroll_y}; 915 $h = 1 if $self->{scroll_y};
961 916
962 ($w, $h) 917 ($w, $h)
963} 918}
964 919
965sub invoke_size_allocate { 920sub invoke_size_allocate {
986 $self->{view_x} = $x; 941 $self->{view_x} = $x;
987 $self->{view_y} = $y; 942 $self->{view_y} = $y;
988 943
989 $self->emit (changed => $x, $y); 944 $self->emit (changed => $x, $y);
990 $self->update; 945 $self->update;
946 }
947}
948
949sub set_center {
950 my ($self, $x, $y) = @_;
951
952 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
953}
954
955sub make_visible {
956 my ($self, $x, $y, $border) = @_;
957
958 if ( $x < $self->{view_x} + $self->{w} * $border
959 || $x > $self->{view_x} + $self->{w} * (1 - $border)
960 || $y < $self->{view_y} + $self->{h} * $border
961 || $y > $self->{view_y} + $self->{h} * (1 - $border)
962 ) {
963 $self->set_center ($x, $y);
991 } 964 }
992} 965}
993 966
994# hmm, this does not work for topleft of $self... but we should not ask for that 967# hmm, this does not work for topleft of $self... but we should not ask for that
995sub coord2local { 968sub coord2local {
1042 my $child = delete $arg{child}; 1015 my $child = delete $arg{child};
1043 1016
1044 my $self; 1017 my $self;
1045 1018
1046 my $hslider = new CFPlus::UI::Slider 1019 my $hslider = new CFPlus::UI::Slider
1020 c_col => 0,
1021 c_row => 1,
1047 vertical => 0, 1022 vertical => 0,
1048 range => [0, 0, 1, 0.01], # HACK fix 1023 range => [0, 0, 1, 0.01], # HACK fix
1049 on_changed => sub { 1024 on_changed => sub {
1050 $self->{hpos} = $_[1]; 1025 $self->{hpos} = $_[1];
1051 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1026 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1052 }, 1027 },
1053 ; 1028 ;
1054 1029
1055 my $vslider = new CFPlus::UI::Slider 1030 my $vslider = new CFPlus::UI::Slider
1031 c_col => 1,
1032 c_row => 0,
1056 vertical => 1, 1033 vertical => 1,
1057 range => [0, 0, 1, 0.01], # HACK fix 1034 range => [0, 0, 1, 0.01], # HACK fix
1058 on_changed => sub { 1035 on_changed => sub {
1059 $self->{vpos} = $_[1]; 1036 $self->{vpos} = $_[1];
1060 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1037 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1071 row_expand => [1, 0], 1048 row_expand => [1, 0],
1072 %arg, 1049 %arg,
1073 ); 1050 );
1074 1051
1075 $self->{vp} = new CFPlus::UI::ViewPort 1052 $self->{vp} = new CFPlus::UI::ViewPort
1053 c_col => 0,
1054 c_row => 0,
1076 expand => 1, 1055 expand => 1,
1077 scroll_x => $self->{scroll_x}, 1056 scroll_x => $self->{scroll_x},
1078 scroll_y => $self->{scroll_y}, 1057 scroll_y => $self->{scroll_y},
1079 on_changed => sub { 1058 on_changed => sub {
1080 my ($vp, $x, $y) = @_; 1059 my ($vp, $x, $y) = @_;
1082 $vp->{parent}{hslider}->set_value ($x); 1061 $vp->{parent}{hslider}->set_value ($x);
1083 $vp->{parent}{vslider}->set_value ($y); 1062 $vp->{parent}{vslider}->set_value ($y);
1084 1063
1085 0 1064 0
1086 }, 1065 },
1066 on_size_allocate => sub {
1067 my ($vp, $w, $h) = @_;
1068 $vp->{parent}->update_slider;
1069 0
1070 },
1087 ; 1071 ;
1088 1072
1089 $self->SUPER::add_at (0, 0, $self->{vp}); 1073 $self->SUPER::add ($self->{vp});
1090 1074
1091 $self->add ($child) if $child; 1075 $self->add ($child) if $child;
1092 1076
1093 $self 1077 $self
1094} 1078}
1095
1096#TODO# update range on size_allocate depending on child
1097 1079
1098sub add { 1080sub add {
1099 my ($self, $widget) = @_; 1081 my ($self, $widget) = @_;
1100 1082
1101 $self->{vp}->add ($self->{child} = $widget); 1083 $self->{vp}->add ($self->{child} = $widget);
1102} 1084}
1103 1085
1086sub set_offset { shift->{vp}->set_offset (@_) }
1087sub set_center { shift->{vp}->set_center (@_) }
1088sub make_visible { shift->{vp}->make_visible (@_) }
1089
1104sub update_slider { 1090sub update_slider {
1105 my ($self) = @_; 1091 my ($self) = @_;
1106 1092
1107 my $child = ($self->{vp} or return)->child; 1093 my $child = ($self->{vp} or return)->child;
1108 1094
1095 if ($self->{scroll_x}) {
1109 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1096 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1110 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); 1097 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1111 1098
1112 my $visible = $w1 > $w2; 1099 my $visible = $w1 > $w2;
1113 if ($visible != $self->{hslider}{visible}) { 1100 if ($visible != $self->{hslider_visible}) {
1101 $self->{hslider_visible} = $visible;
1114 $visible ? $self->SUPER::add_at (0, 1, $self->{hslider}) 1102 $visible ? $self->SUPER::add ($self->{hslider})
1115 : $self->{hslider}->hide; 1103 : $self->SUPER::remove ($self->{hslider});
1104 }
1116 } 1105 }
1117 1106
1107 if ($self->{scroll_y}) {
1118 my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); 1108 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1119 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); 1109 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1120 1110
1121 my $visible = $h1 > $h2; 1111 my $visible = $h1 > $h2;
1122 if ($visible != $self->{vslider}{visible}) { 1112 if ($visible != $self->{vslider_visible}) {
1113 $self->{vslider_visible} = $visible;
1123 $visible ? $self->SUPER::add_at (1, 0, $self->{vslider}) 1114 $visible ? $self->SUPER::add ($self->{vslider})
1124 : $self->{vslider}->hide; 1115 : $self->SUPER::remove ($self->{vslider});
1116 }
1117 }
1118}
1119
1120sub start_dragging {
1121 my ($self, $ev) = @_;
1122
1123 $self->grab_focus;
1124
1125 my $ox = $self->{vp}{view_x};
1126 my $oy = $self->{vp}{view_y};
1127
1128 $self->{motion} = sub {
1129 my ($ev, $x, $y) = @_;
1130
1131 $ox -= $ev->{xrel};
1132 $oy -= $ev->{yrel};
1133
1134 $self->{vp}->set_offset ($ox, $oy);
1125 } 1135 };
1126}
1127
1128sub update {
1129 my ($self) = @_;
1130
1131 $self->update_slider;
1132 $self->SUPER::update;
1133} 1136}
1134 1137
1135sub invoke_mouse_wheel { 1138sub invoke_mouse_wheel {
1136 my ($self, $ev) = @_; 1139 my ($self, $ev) = @_;
1137 1140
1138 return 0 unless $ev->{dy}; # only vertical movements for now
1139
1140 $self->{vslider}->emit (mouse_wheel => $ev); 1141 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1142 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1141 1143
1142 1 1144 1
1143} 1145}
1144 1146
1145sub invoke_button_down { 1147sub invoke_button_down {
1146 my ($self, $ev, $x, $y) = @_; 1148 my ($self, $ev, $x, $y) = @_;
1147 1149
1148 if ($ev->{button} == 2) { 1150 if ($ev->{button} == 2) {
1149 $self->grab_focus; 1151 $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; 1152 return 1;
1162 } 1153 }
1163 1154
1164 0 1155 0
1165} 1156}
1216 my ($w, $h) = @$self{qw(w h)}; 1207 my ($w, $h) = @$self{qw(w h)};
1217 1208
1218 glEnable GL_BLEND; 1209 glEnable GL_BLEND;
1219 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1210 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1220 glColor_premultiply @{ $self->{bg} }; 1211 glColor_premultiply @{ $self->{bg} };
1221
1222 glBegin GL_QUADS;
1223 glVertex 0 , 0;
1224 glVertex 0 , $h;
1225 glVertex $w, $h; 1212 glRect 0, 0, $w, $h;
1226 glVertex $w, 0;
1227 glEnd;
1228
1229 glDisable GL_BLEND; 1213 glDisable GL_BLEND;
1230 } 1214 }
1231 1215
1232 $self->SUPER::_draw; 1216 $self->SUPER::_draw;
1233} 1217}
1575 1559
1576############################################################################# 1560#############################################################################
1577 1561
1578package CFPlus::UI::Table; 1562package CFPlus::UI::Table;
1579 1563
1580our @ISA = CFPlus::UI::Base::; 1564our @ISA = CFPlus::UI::Container::;
1581 1565
1582use List::Util qw(max sum); 1566use List::Util qw(max sum);
1583 1567
1584use CFPlus::OpenGL; 1568use CFPlus::OpenGL;
1585 1569
1586sub new { 1570sub new {
1587 my $class = shift; 1571 my $class = shift;
1588 1572
1589 $class->SUPER::new ( 1573 $class->SUPER::new (
1590 children => [],
1591 col_expand => [], 1574 col_expand => [],
1592 row_expand => [], 1575 row_expand => [],
1593 @_, 1576 @_,
1594 ) 1577 )
1595} 1578}
1596 1579
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 { 1580sub add {
1603 my $self = shift; 1581 my ($self, @widgets) = @_;
1604 1582
1605 Carp::cluck "please use the add_at method instead of calling add, thank you.\n";#d# 1583 for my $child (@widgets) {
1606 $self->add_at (@_); 1584 $child->{c_rowspan} ||= 1;
1585 $child->{c_colspan} ||= 1;
1586 }
1587
1588 $self->SUPER::add (@widgets);
1607} 1589}
1608 1590
1609sub add_at { 1591sub add_at {
1610 my $self = shift; 1592 my $self = shift;
1611 1593
1594 my @widgets;
1595
1612 while (@_) { 1596 while (@_) {
1613 my ($col, $row, $child) = splice @_, 0, 3, (); 1597 my ($col, $row, $child) = splice @_, 0, 3, ();
1614 1598
1615 $child->set_parent ($self); 1599 $child->{c_row} = $row;
1616 $self->{children}[$row][$col] = $child; 1600 $child->{c_col} = $col;
1617 }
1618 1601
1619 $self->{force_realloc} = 1; 1602 push @widgets, $child;
1620 $self->{force_size_alloc} = 1; 1603 }
1621 $self->realloc;
1622}
1623 1604
1624sub remove { 1605 $self->add (@widgets);
1606}
1607
1608sub get_wh {
1625 my ($self, $child) = @_; 1609 my ($self) = @_;
1626 1610
1627 for (@{ $self->{children} }) { 1611 my (@w, @h);
1628 for (@{ $_ || [] }) { 1612
1629 $_ = undef if $_ == $child; 1613 my @children = $self->children;
1614
1615 # first pass, columns
1616 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1617 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1618
1619 my $sw = sum @w[$c .. $c + $cs - 1];
1620
1621 if ($w > $sw) {
1622 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1630 } 1623 }
1631 } 1624 }
1632}
1633 1625
1634# TODO: move to container class maybe? send children a signal on removal? 1626 # second pass, rows
1635sub clear { 1627 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)}; 1628 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1662 1629
1663 $w[$x] = max $w[$x], $w; 1630 my $sh = sum @h[$r .. $r + $rs - 1];
1664 $h[$y] = max $h[$y], $h; 1631
1632 if ($h > $sh) {
1633 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1665 } 1634 }
1666 } 1635 }
1667 1636
1668 (\@w, \@h) 1637 (\@w, \@h)
1669} 1638}
1685 my ($ws, $hs) = $self->get_wh; 1654 my ($ws, $hs) = $self->get_wh;
1686 1655
1687 my $req_w = (sum @$ws) || 1; 1656 my $req_w = (sum @$ws) || 1;
1688 my $req_h = (sum @$hs) || 1; 1657 my $req_h = (sum @$hs) || 1;
1689 1658
1690 # TODO: nicer code 1659 # now linearly scale the rows/columns to the allocated size
1691 my @col_expand = @{$self->{col_expand}}; 1660 my @col_expand = @{$self->{col_expand}};
1692 @col_expand = (1) x @$ws unless @col_expand; 1661 @col_expand = (1) x @$ws unless @col_expand;
1693 my $col_expand = (sum @col_expand) || 1; 1662 my $col_expand = (sum @col_expand) || 1;
1694 1663
1695 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1664 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1702 1671
1703 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1672 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1704 1673
1705 CFPlus::UI::harmonize $hs; 1674 CFPlus::UI::harmonize $hs;
1706 1675
1707 my $y; 1676 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1677 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1708 1678
1709 for my $r (0 .. $#{$self->{children}}) { 1679 for my $widget ($self->children) {
1710 my $row = $self->{children}[$r] 1680 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 1681
1713 my $x = 0; 1682 $widget->configure (
1714 my $row_h = $hs->[$r]; 1683 $x[$c], $y[$r],
1684 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1715 1685 );
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 } 1686 }
1728 1687
1729 1 1688 1
1730} 1689}
1731 1690
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############################################################################# 1691#############################################################################
1757 1692
1758package CFPlus::UI::Fixed; 1693package CFPlus::UI::Fixed;
1759 1694
1760use List::Util qw(min max); 1695use List::Util qw(min max);
1761 1696
1762our @ISA = CFPlus::UI::Container::; 1697our @ISA = CFPlus::UI::Container::;
1763 1698
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($$$) { 1699sub _scale($$$) {
1772 my ($mode, $val, $max) = @_; 1700 my ($rel, $val, $max) = @_;
1773 1701
1774 $mode eq "abs" ? $val 1702 $rel ? $val * $max : $val
1775 : $mode eq "rel" ? $val * $max
1776 : 0
1777} 1703}
1778 1704
1779sub size_request { 1705sub size_request {
1780 my ($self) = @_; 1706 my ($self) = @_;
1781 1707
1782 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1708 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1783 1709
1784 # determine overall size by querying abs widgets 1710 # determine overall size by querying abs widgets
1785 for my $child ($self->visible_children) { 1711 for my $child ($self->visible_children) {
1786 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1712 unless ($child->{c_rel}) {
1713 my $x = $child->{c_x};
1714 my $y = $child->{c_y};
1787 1715
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; 1716 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1793 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1717 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1794 } 1718 }
1795 } 1719 }
1796 1720
1797 my $W = $x2 - $x1; 1721 my $W = $x2 - $x1;
1798 my $H = $y2 - $y1; 1722 my $H = $y2 - $y1;
1799 1723
1800 # now layout remaining widgets 1724 # now layout remaining widgets
1801 for my $child ($self->visible_children) { 1725 for my $child ($self->visible_children) {
1802 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1726 if ($child->{c_rel}) {
1727 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1728 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1803 1729
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; 1730 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1811 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1731 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1812 } 1732 }
1813 } 1733 }
1814 1734
1815 my $W = $x2 - $x1; 1735 my $W = $x2 - $x1;
1816 my $H = $y2 - $y1; 1736 my $H = $y2 - $y1;
1820 1740
1821sub invoke_size_allocate { 1741sub invoke_size_allocate {
1822 my ($self, $W, $H) = @_; 1742 my ($self, $W, $H) = @_;
1823 1743
1824 for my $child ($self->visible_children) { 1744 for my $child ($self->visible_children) {
1825 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1745 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1746 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1826 1747
1827 $x = _scale $pos, $x, $W; 1748 $x += $child->{c_halign} * $child->{req_w};
1828 $y = _scale $pos, $x, $H; 1749 $y += $child->{c_valign} * $child->{req_h};
1829 $w = _scale $size, $w, $child->{req_w};
1830 $h = _scale $size, $h, $child->{req_h};
1831 1750
1832 $child->configure ($x, $y, $w, $h); 1751 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1833 } 1752 }
1834 1753
1835 1 1754 1
1836} 1755}
1837 1756
1841 1760
1842our @ISA = CFPlus::UI::Container::; 1761our @ISA = CFPlus::UI::Container::;
1843 1762
1844sub size_request { 1763sub size_request {
1845 my ($self) = @_; 1764 my ($self) = @_;
1765
1766 my @children = $self->visible_children;
1846 1767
1847 $self->{vertical} 1768 $self->{vertical}
1848 ? ( 1769 ? (
1849 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1770 (List::Util::max map $_->{req_w}, @children),
1850 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1771 (List::Util::sum map $_->{req_h}, @children),
1851 ) 1772 )
1852 : ( 1773 : (
1853 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1774 (List::Util::sum map $_->{req_w}, @children),
1854 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1775 (List::Util::max map $_->{req_h}, @children),
1855 ) 1776 )
1856} 1777}
1857 1778
1858sub invoke_size_allocate { 1779sub invoke_size_allocate {
1859 my ($self, $w, $h) = @_; 1780 my ($self, $w, $h) = @_;
1983 1904
1984 delete $self->{ox}; 1905 delete $self->{ox};
1985 $self->SUPER::realloc; 1906 $self->SUPER::realloc;
1986} 1907}
1987 1908
1909sub clear {
1910 my ($self) = @_;
1911
1912 $self->set_text ("");
1913}
1914
1988sub set_text { 1915sub set_text {
1989 my ($self, $text) = @_; 1916 my ($self, $text) = @_;
1990 1917
1991 return if $self->{text} eq "T$text"; 1918 return if $self->{text} eq "T$text";
1992 $self->{text} = "T$text"; 1919 $self->{text} = "T$text";
2098 : ($self->{w} - $size->[0]) * 0.5); 2025 : ($self->{w} - $size->[0]) * 0.5);
2099 2026
2100 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2027 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2101 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2028 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2102 : ($self->{h} - $size->[1]) * 0.5); 2029 : ($self->{h} - $size->[1]) * 0.5);
2030
2031 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2103 }; 2032 };
2104 2033
2105 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2034# unless ($self->{list}) {
2106 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2035# $self->{list} = CFPlus::OpenGL::glGenList;
2107 2036# CFPlus::OpenGL::glNewList $self->{list};
2108 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2037# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2038# CFPlus::OpenGL::glEndList;
2039# }
2040#
2041# CFPlus::OpenGL::glCallList $self->{list};
2042
2043 $self->{layout}->draw;
2109} 2044}
2045
2046#sub destroy {
2047# my ($self) = @_;
2048#
2049# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2050#
2051# $self->SUPER::destroy;
2052#}
2110 2053
2111############################################################################# 2054#############################################################################
2112 2055
2113package CFPlus::UI::EntryBase; 2056package CFPlus::UI::EntryBase;
2114 2057
2120 my $class = shift; 2063 my $class = shift;
2121 2064
2122 $class->SUPER::new ( 2065 $class->SUPER::new (
2123 fg => [1, 1, 1], 2066 fg => [1, 1, 1],
2124 bg => [0, 0, 0, 0.2], 2067 bg => [0, 0, 0, 0.2],
2068 outline => [0.6, 0.3, 0.1],
2125 active_bg => [1, 1, 1, 0.5], 2069 active_bg => [0, 0, 1, .2],
2126 active_fg => [0, 0, 0], 2070 active_fg => [1, 1, 1],
2071 active_outline => [1, 1, 0],
2127 can_hover => 1, 2072 can_hover => 1,
2128 can_focus => 1, 2073 can_focus => 1,
2129 valign => 0, 2074 valign => 0,
2130 can_events => 1, 2075 can_events => 1,
2131 ellipsise => 0, 2076 ellipsise => 0,
2271 glColor_premultiply @{$self->{bg}}; 2216 glColor_premultiply @{$self->{bg}};
2272 } 2217 }
2273 2218
2274 glEnable GL_BLEND; 2219 glEnable GL_BLEND;
2275 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2220 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}; 2221 glRect 0, 0, $self->{w}, $self->{h};
2280 glVertex $self->{w}, 0;
2281 glEnd;
2282 glDisable GL_BLEND; 2222 glDisable GL_BLEND;
2283 2223
2284 $self->SUPER::_draw; 2224 $self->SUPER::_draw;
2285 2225
2286 #TODO: force update every cursor change :( 2226 #TODO: force update every cursor change :(
2288 2228
2289 unless (exists $self->{cur_h}) { 2229 unless (exists $self->{cur_h}) {
2290 my $text = substr $self->{text}, 0, $self->{cursor}; 2230 my $text = substr $self->{text}, 0, $self->{cursor};
2291 utf8::encode $text; 2231 utf8::encode $text;
2292 2232
2293 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2233 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2294 } 2234 }
2295 2235
2236 glColor_premultiply @{$self->{active_fg}};
2296 glBegin GL_LINES; 2237 glBegin GL_LINES;
2297 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2238 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
2298 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2239 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2240 glEnd;
2241
2242 glLineWidth 3;
2243 glColor @{$self->{active_outline}};
2244 glRect_lineloop 0, 0, $self->{w} - 1, $self->{h} - 1;
2245 glLineWidth 1;
2246
2247 } else {
2248 glColor @{$self->{outline}};
2249 glTranslate .375, .375;
2250 glBegin GL_LINE_STRIP;
2251 glVertex 0, $self->{h} * .5;
2252 glVertex 0, $self->{h} - 3;
2253 glVertex $self->{w} - 1, $self->{h} - 3;
2254 glVertex $self->{w} - 1, $self->{h} * .5;
2299 glEnd; 2255 glEnd;
2300 } 2256 }
2301} 2257}
2302 2258
2303############################################################################# 2259#############################################################################
2390 1 2346 1
2391} 2347}
2392 2348
2393############################################################################# 2349#############################################################################
2394 2350
2351package CFPlus::UI::ButtonBin;
2352
2353our @ISA = CFPlus::UI::Bin::;
2354
2355use CFPlus::OpenGL;
2356
2357my @tex =
2358 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2359 qw(b1_button_inactive.png b1_button_active.png);
2360
2361sub new {
2362 my $class = shift;
2363
2364 $class->SUPER::new (
2365 can_hover => 1,
2366 align => 0,
2367 valign => 0,
2368 can_events => 1,
2369 @_
2370 )
2371}
2372
2373sub invoke_button_up {
2374 my ($self, $ev, $x, $y) = @_;
2375
2376 $self->emit ("activate")
2377 if $x >= 0 && $x < $self->{w}
2378 && $y >= 0 && $y < $self->{h};
2379
2380 1
2381}
2382
2383sub _draw {
2384 my ($self) = @_;
2385
2386 glEnable GL_TEXTURE_2D;
2387 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2388 glColor 0, 0, 0, 1;
2389
2390 my $tex = $tex[$GRAB == $self];
2391 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2392
2393 glDisable GL_TEXTURE_2D;
2394
2395 $self->SUPER::_draw;
2396}
2397
2398#############################################################################
2399
2395package CFPlus::UI::Button; 2400package CFPlus::UI::Button;
2396 2401
2397our @ISA = CFPlus::UI::Label::; 2402our @ISA = CFPlus::UI::Label::;
2398 2403
2399use CFPlus::OpenGL; 2404use CFPlus::OpenGL;
2533sub new { 2538sub new {
2534 my $class = shift; 2539 my $class = shift;
2535 2540
2536 my $self = $class->SUPER::new ( 2541 my $self = $class->SUPER::new (
2537 can_events => 0, 2542 can_events => 0,
2543 scale => 1,
2538 @_, 2544 @_,
2539 ); 2545 );
2540 2546
2541 $self->{path} || $self->{tex} 2547 $self->{path} || $self->{tex}
2542 or Carp::croak "'path' or 'tex' attributes required"; 2548 or Carp::croak "'path' or 'tex' attributes required";
2567} 2573}
2568 2574
2569sub size_request { 2575sub size_request {
2570 my ($self) = @_; 2576 my ($self) = @_;
2571 2577
2572 ($self->{tex}{w}, $self->{tex}{h}) 2578 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2573} 2579}
2574 2580
2575sub _draw { 2581sub _draw {
2576 my ($self) = @_; 2582 my ($self) = @_;
2577 2583
2587 } 2593 }
2588 2594
2589 glEnable GL_TEXTURE_2D; 2595 glEnable GL_TEXTURE_2D;
2590 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2596 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2591 2597
2592 $tex->draw_quad (0, 0, $w, $h); 2598 $tex->draw_quad_alpha (0, 0, $w, $h);
2593 2599
2594 glDisable GL_TEXTURE_2D; 2600 glDisable GL_TEXTURE_2D;
2595} 2601}
2596 2602
2597############################################################################# 2603#############################################################################
2616 align => 0, 2622 align => 0,
2617 valign => 0, 2623 valign => 0,
2618 can_events => 1, 2624 can_events => 1,
2619 @_ 2625 @_
2620 ); 2626 );
2627}
2628
2629sub invoke_button_down {
2630 my ($self, $ev, $x, $y) = @_;
2631
2632 1
2621} 2633}
2622 2634
2623sub invoke_button_up { 2635sub invoke_button_up {
2624 my ($self, $ev, $x, $y) = @_; 2636 my ($self, $ev, $x, $y) = @_;
2625 2637
2765 glDisable GL_TEXTURE_2D; 2777 glDisable GL_TEXTURE_2D;
2766} 2778}
2767 2779
2768############################################################################# 2780#############################################################################
2769 2781
2782package CFPlus::UI::Progress;
2783
2784our @ISA = CFPlus::UI::Label::;
2785
2786use CFPlus::OpenGL;
2787
2788sub new {
2789 my ($class, %arg) = @_;
2790
2791 my $self = $class->SUPER::new (
2792 fg => [1, 1, 1],
2793 bg => [0, 0, 1, 0.2],
2794 bar => [0.7, 0.5, 0.1, 0.8],
2795 outline => [0.4, 0.3, 0],
2796 fontsize => 0.9,
2797 valign => 0,
2798 align => 0,
2799 can_events => 1,
2800 ellipsise => 1,
2801 label => "%d%%",
2802 %arg,
2803 );
2804
2805 $self->set_value ($arg{value} || -1);
2806
2807 $self
2808}
2809
2810sub set_label {
2811 my ($self, $label) = @_;
2812
2813 return if $self->{label} eq $label;
2814 $self->{label} = $label;
2815
2816 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value});
2817}
2818
2819sub set_value {
2820 my ($self, $value) = @_;
2821
2822 if ($self->{value} ne $value) {
2823 $self->{value} = $value;
2824
2825 if ($value < 0) {
2826 $self->set_text ("-");
2827 } else {
2828 $self->set_text (sprintf $self->{label}, $value * 100);
2829 }
2830
2831 $self->update;
2832 }
2833}
2834
2835sub _draw {
2836 my ($self) = @_;
2837
2838 glEnable GL_BLEND;
2839 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2840
2841 if ($self->{value} >= 0) {
2842 my $s = 3 + ($self->{w} - 6) * $self->{value};
2843
2844 glColor_premultiply @{$self->{bar}};
2845 glRect 2, 2, $s, $self->{h} - 2;
2846 glColor_premultiply @{$self->{bg}};
2847 glRect $s + 1, 0, $self->{w} - 2, $self->{h} - 2;
2848 }
2849
2850 glColor_premultiply @{$self->{outline}};
2851 glRect_lineloop 1, 1, $self->{w} - 2, $self->{h} - 2;
2852
2853 glDisable GL_BLEND;
2854
2855 {
2856 local $self->{bg}; # do not draw background
2857 $self->SUPER::_draw;
2858 }
2859}
2860
2861#############################################################################
2862
2863package CFPlus::UI::ExperienceProgress;
2864
2865our @ISA = CFPlus::UI::Progress::;
2866
2867sub new {
2868 my ($class, %arg) = @_;
2869
2870 my $self = $class->SUPER::new (
2871 tooltip => sub {
2872 my ($self) = @_;
2873
2874 sprintf "level %d\n%s points\n%s next level\n%s to go",
2875 $self->{lvl},
2876 ::formsep ($self->{exp}),
2877 ::formsep ($self->{nxt}),
2878 ::formsep ($self->{nxt} - $self->{exp}),
2879 },
2880 %arg
2881 );
2882
2883 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2884 if $::CONN;
2885
2886 $self
2887}
2888
2889sub DESTROY {
2890 my ($self) = @_;
2891
2892 delete $::CONN->{on_exp_update}{$self+0}
2893 if $::CONN;
2894
2895 $self->SUPER::DESTROY;
2896}
2897
2898sub set_value {
2899 my ($self, $lvl, $exp) = @_;
2900
2901<<<<<<< UI.pm
2902 $self->{lvl} = $lvl;
2903 $self->{exp} = $exp;
2904
2905=======
2906 $self->{lvl} = $exp;
2907 $self->{exp} = $exp;
2908
2909>>>>>>> 1.434
2910 my $v = -1;
2911
2912 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2913 my $l0 = $table->[$lvl - 1];
2914 my $l1 = $table->[$lvl];
2915
2916 $self->{nxt} = $l1;
2917
2918 $v = ($exp - $l0) / ($l1 - $l0);
2919 }
2920
2921 $self->SUPER::set_value ($v);
2922}
2923
2924#############################################################################
2925
2770package CFPlus::UI::Gauge; 2926package CFPlus::UI::Gauge;
2771 2927
2772our @ISA = CFPlus::UI::VBox::; 2928our @ISA = CFPlus::UI::VBox::;
2773 2929
2774sub new { 2930sub new {
2899 3055
2900 $self->SUPER::invoke_button_down ($ev, $x, $y); 3056 $self->SUPER::invoke_button_down ($ev, $x, $y);
2901 3057
2902 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3058 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2903 3059
2904 $self->invoke_mouse_motion ($ev, $x, $y) 3060 $self->invoke_mouse_motion ($ev, $x, $y);
3061
3062 1
2905} 3063}
2906 3064
2907sub invoke_mouse_motion { 3065sub invoke_mouse_motion {
2908 my ($self, $ev, $x, $y) = @_; 3066 my ($self, $ev, $x, $y) = @_;
2909 3067
2929 3087
2930 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3088 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2931 3089
2932 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3090 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2933 3091
2934 ! ! $delta 3092 1
2935} 3093}
2936 3094
2937sub update { 3095sub update {
2938 my ($self) = @_; 3096 my ($self) = @_;
2939 3097
3070} 3228}
3071 3229
3072sub size_request { 3230sub size_request {
3073 my ($self) = @_; 3231 my ($self) = @_;
3074 3232
3075 my ($empty, $slider) = @{ $self->{children} }; 3233 my ($empty, $slider) = $self->visible_children;
3076 3234
3077 local $self->{children} = [$empty, $slider]; 3235 local $self->{children} = [$empty, $slider];
3078 $self->SUPER::size_request 3236 $self->SUPER::size_request
3079} 3237}
3080 3238
3269 3427
3270 if ($y0 < $y + $h && $y < $y1) { 3428 if ($y0 < $y + $h && $y < $y1) {
3271 my $layout = $self->get_layout ($para); 3429 my $layout = $self->get_layout ($para);
3272 3430
3273 $layout->render ($para->{indent}, $y - $y0); 3431 $layout->render ($para->{indent}, $y - $y0);
3432 $layout->draw;
3274 3433
3275 if (my @w = @{ $para->{widget} }) { 3434 if (my @w = @{ $para->{widget} }) {
3276 my @s = $layout->get_shapes; 3435 my @s = $layout->get_shapes;
3277 3436
3278 for (@w) { 3437 for (@w) {
3400} 3559}
3401 3560
3402sub set_tooltip_from { 3561sub set_tooltip_from {
3403 my ($self, $widget) = @_; 3562 my ($self, $widget) = @_;
3404 3563
3405 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3406 if $widget->{tooltip} =~ /^#(.*)$/;
3407
3408 my $tooltip = $widget->{tooltip}; 3564 my $tip = $widget->{tooltip};
3565 $tip = $tip->($widget) if "CODE" eq ref $tip;
3566
3567 $tip = CFPlus::Pod::section_label tooltip => $1
3568 if $tip =~ /^#(.*)$/;
3409 3569
3410 if ($ENV{CFPLUS_DEBUG} & 2) { 3570 if ($ENV{CFPLUS_DEBUG} & 2) {
3411 $tooltip .= "\n\n" . (ref $widget) . "\n" 3571 $tip .= "\n\n" . (ref $widget) . "\n"
3412 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3572 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3413 . "req $widget->{req_w} $widget->{req_h}\n" 3573 . "req $widget->{req_w} $widget->{req_h}\n"
3414 . "visible $widget->{visible}"; 3574 . "visible $widget->{visible}";
3415 } 3575 }
3416 3576
3417 $tooltip =~ s/^\n+//; 3577 $tip =~ s/^\n+//;
3418 $tooltip =~ s/\n+$//; 3578 $tip =~ s/\n+$//;
3419 3579
3420 $self->add (new CFPlus::UI::Label 3580 $self->add (new CFPlus::UI::Label
3421 markup => $tooltip, 3581 markup => $tip,
3422 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3582 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3423 fontsize => 0.8, 3583 fontsize => 0.8,
3424 style => 1, # FLAG_INVERSE 3584 style => 1, # FLAG_INVERSE
3425 ellipsise => 0, 3585 ellipsise => 0,
3426 font => ($widget->{tooltip_font} || $::FONT_PROP), 3586 font => ($widget->{tooltip_font} || $::FONT_PROP),
3469 glTranslate 0.375, 0.375; 3629 glTranslate 0.375, 0.375;
3470 3630
3471 my ($w, $h) = @$self{qw(w h)}; 3631 my ($w, $h) = @$self{qw(w h)};
3472 3632
3473 glColor 1, 0.8, 0.4; 3633 glColor 1, 0.8, 0.4;
3474 glBegin GL_QUADS; 3634 glRect 0, 0, $w, $h;
3475 glVertex 0 , 0;
3476 glVertex 0 , $h;
3477 glVertex $w, $h;
3478 glVertex $w, 0;
3479 glEnd;
3480 3635
3481 glColor 0, 0, 0; 3636 glColor 0, 0, 0;
3482 glBegin GL_LINE_LOOP; 3637 glRect_lineloop 0, 0, $w, $h;
3483 glVertex 0 , 0;
3484 glVertex 0 , $h;
3485 glVertex $w, $h;
3486 glVertex $w, 0;
3487 glEnd;
3488 3638
3489 glTranslate 2 - 0.375, 2 - 0.375; 3639 glTranslate 2 - 0.375, 2 - 0.375;
3490 3640
3491 $self->SUPER::_draw; 3641 $self->SUPER::_draw;
3492} 3642}
3512 3662
3513 if ($self->{anim} && $self->{animspeed}) { 3663 if ($self->{anim} && $self->{animspeed}) {
3514 CFPlus::weaken (my $widget = $self); 3664 CFPlus::weaken (my $widget = $self);
3515 3665
3516 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3666 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3517 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed}; 3667 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3518 $self->{timer} = Event->timer ( 3668 $self->{timer} = Event->timer (
3519 parked => 1, 3669 parked => 1,
3520 cb => sub { 3670 cb => sub {
3521 return unless $::CONN && $widget; 3671 return unless $::CONN;
3522 3672
3673 my $w = $widget
3674 or return;
3675
3523 ++$widget->{frame}; 3676 ++$w->{frame};
3524 $widget->update_face; 3677 $w->update_face;
3678
3679 # somehow, $widget can go away
3525 $widget->update; 3680 $w->update;
3526
3527 $widget->update_timer; 3681 $w->update_timer;
3528 }, 3682 },
3529 ); 3683 );
3530 3684
3531 $self->update_face; 3685 $self->update_face;
3532 $self->update_timer; 3686 $self->update_timer;
3553} 3707}
3554 3708
3555sub update_face { 3709sub update_face {
3556 my ($self) = @_; 3710 my ($self) = @_;
3557 3711
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) { 3712 if ($::CONN) {
3572 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3713 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3573 if (my $tex = $::CONN->{texture}[$faceid]) { 3714 if ($anim && @$anim) {
3574 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3715 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3575 } else { 3716 delete $self->{face_change_cb};
3576 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3717
3577 $self->realloc; 3718 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3719 unless ($tex->{name} || $tex->{loading}) {
3720 $tex->upload (sub { $self->reconfigure });
3721 }
3578 }); 3722 }
3579 } 3723 }
3580 } 3724 }
3581 } 3725 }
3726}
3727
3728sub size_request {
3729 my ($self) = @_;
3730
3731 if ($::CONN) {
3732 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3733 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3734 if ($tex->{name}) {
3735 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3736 } elsif (!$tex->{loading}) {
3737 $tex->upload (sub { $self->reconfigure });
3738 }
3739 }
3740
3741 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3742 }
3743 }
3582 3744
3583 ($self->{size_w} || 8, $self->{size_h} || 8) 3745 ($self->{size_w} || 8, $self->{size_h} || 8)
3584} 3746}
3585 3747
3586sub update { 3748sub update {
3600} 3762}
3601 3763
3602sub _draw { 3764sub _draw {
3603 my ($self) = @_; 3765 my ($self) = @_;
3604 3766
3605 return unless $::CONN;
3606
3607 $self->SUPER::_draw; 3767 $self->SUPER::_draw;
3608 3768
3609 my $faceid = $::CONN->{faceid}[$self->{face}] 3769 if (my $tex = $self->{tex}) {
3610 or return;
3611
3612 my $tex = $::CONN->{texture}[$faceid];
3613
3614 if ($tex) {
3615 glEnable GL_TEXTURE_2D; 3770 glEnable GL_TEXTURE_2D;
3616 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3771 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3617 glColor 0, 0, 0, 1; 3772 glColor 0, 0, 0, 1;
3618 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3773 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3619 glDisable GL_TEXTURE_2D; 3774 glDisable GL_TEXTURE_2D;
3671 children => [ 3826 children => [
3672 (new CFPlus::UI::Label markup => $left, expand => 1), 3827 (new CFPlus::UI::Label markup => $left, expand => 1),
3673 (new CFPlus::UI::Label markup => $right, align => +1), 3828 (new CFPlus::UI::Label markup => $right, align => +1),
3674 ], 3829 ],
3675 ; 3830 ;
3676 3831
3677 } else { 3832 } else {
3678 $widget = new CFPlus::UI::Label 3833 $widget = new CFPlus::UI::Label
3679 can_hover => 1, 3834 can_hover => 1,
3680 can_events => 1, 3835 can_events => 1,
3681 markup => $widget, 3836 markup => $widget,
3806 3961
3807############################################################################# 3962#############################################################################
3808 3963
3809package CFPlus::UI::Notebook; 3964package CFPlus::UI::Notebook;
3810 3965
3966use CFPlus::OpenGL;
3967
3811our @ISA = CFPlus::UI::VBox::; 3968our @ISA = CFPlus::UI::VBox::;
3812 3969
3813sub new { 3970sub new {
3814 my $class = shift; 3971 my $class = shift;
3815 3972
3816 my $self = $class->SUPER::new ( 3973 my $self = $class->SUPER::new (
3817 buttonbar => (new CFPlus::UI::Buttonbar), 3974 buttonbar => (new CFPlus::UI::Buttonbar),
3818 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3975 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3976 active_outline => [1, 1, 0],
3819 # filter => # will be put between multiplexer and $self 3977 # filter => # will be put between multiplexer and $self
3820 @_, 3978 @_,
3821 ); 3979 );
3822 3980
3823 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3981 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3824 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3982 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3825 3983
3984 {
3985 Scalar::Util::weaken (my $wself = $self);
3986
3987 $self->{multiplexer}->connect (c_add => sub {
3988 my ($mplex, $widgets) = @_;
3989
3990 for my $child (@$widgets) {
3991 Scalar::Util::weaken $child;
3992 $child->{c_tab_} ||= do {
3993 my $tab =
3994 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base")
3995 ? $child->{c_tab}
3996 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
3997
3998 $tab->connect (activate => sub {
3999 $wself->set_current_page ($child);
4000 });
4001
4002 $tab
4003 };
4004
4005 $self->{buttonbar}->add ($child->{c_tab_});
4006 }
4007 });
4008
4009 $self->{multiplexer}->connect (c_remove => sub {
4010 my ($mplex, $widgets) = @_;
4011
4012 for my $child (@$widgets) {
4013 $wself->{buttonbar}->remove ($child->{c_tab_});
4014 }
4015 });
4016 }
4017
3826 $self 4018 $self
3827} 4019}
3828 4020
3829sub add { 4021sub add {
4022 my ($self, @widgets) = @_;
4023
4024 $self->{multiplexer}->add (@widgets)
4025}
4026
4027sub remove {
4028 my ($self, @widgets) = @_;
4029
4030 $self->{multiplexer}->remove (@widgets)
4031}
4032
4033sub pages {
4034 my ($self) = @_;
4035 $self->{multiplexer}->children
4036}
4037
4038sub add_tab {
3830 my ($self, $title, $widget, $tooltip) = @_; 4039 my ($self, $title, $widget, $tooltip) = @_;
3831 4040
3832 CFPlus::weaken $self; 4041 $title = [$title, $tooltip] unless ref $title;
4042 $widget->{c_tab} = $title;
3833 4043
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); 4044 $self->add ($widget);
3841} 4045}
3842 4046
3843sub get_current_page { 4047sub get_current_page {
3844 my ($self) = @_; 4048 my ($self) = @_;
3845 4049
3849sub set_current_page { 4053sub set_current_page {
3850 my ($self, $page) = @_; 4054 my ($self, $page) = @_;
3851 4055
3852 $self->{multiplexer}->set_current_page ($page); 4056 $self->{multiplexer}->set_current_page ($page);
3853 $self->emit (page_changed => $self->{multiplexer}{current}); 4057 $self->emit (page_changed => $self->{multiplexer}{current});
4058}
4059
4060sub _draw {
4061 my ($self) = @_;
4062
4063 $self->SUPER::_draw ();
4064
4065 if (my $cur = $self->{multiplexer}{current}) {
4066 if ($cur = $cur->{c_tab_}) {
4067 glTranslate $cur->{x}, $cur->{y};
4068 glLineWidth 3;
4069 glColor @{$self->{active_outline}};
4070 glRect_lineloop 1, 1, $cur->{w} - 2, $cur->{h} - 2;
4071 glLineWidth 1;
4072 }
4073 }
3854} 4074}
3855 4075
3856############################################################################# 4076#############################################################################
3857 4077
3858package CFPlus::UI::Selector; 4078package CFPlus::UI::Selector;
4128} 4348}
4129 4349
4130sub update { 4350sub update {
4131 my ($self) = @_; 4351 my ($self) = @_;
4132 4352
4133 $::WANT_REFRESH++; 4353 $::WANT_REFRESH->start;
4134} 4354}
4135 4355
4136sub add { 4356sub add {
4137 my ($self, @children) = @_; 4357 my ($self, @children) = @_;
4138 4358
4175 while ($self->{refresh_hook}) { 4395 while ($self->{refresh_hook}) {
4176 $_->() 4396 $_->()
4177 for values %{delete $self->{refresh_hook}}; 4397 for values %{delete $self->{refresh_hook}};
4178 } 4398 }
4179 4399
4180 if ($self->{realloc}) { 4400 while ($self->{realloc}) {
4181 my %queue; 4401 my %queue;
4182 my @queue; 4402 my @queue;
4183 my $widget; 4403 my $widget;
4184 4404
4185 outer: 4405 outer:
4232 } 4452 }
4233 } 4453 }
4234 4454
4235 delete $self->{realloc}{$widget+0}; 4455 delete $self->{realloc}{$widget+0};
4236 } 4456 }
4237 }
4238 4457
4239 while (my $size_alloc = delete $self->{size_alloc}) { 4458 while (my $size_alloc = delete $self->{size_alloc}) {
4240 my @queue = sort { $b->{visible} <=> $a->{visible} } 4459 my @queue = sort { $a->{visible} <=> $b->{visible} }
4241 values %$size_alloc; 4460 values %$size_alloc;
4242 4461
4243 while () { 4462 while () {
4244 my $widget = pop @queue || last; 4463 my $widget = pop @queue || last;
4245 4464
4246 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4465 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4247 4466
4248 $w = 0 if $w < 0;
4249 $h = 0 if $h < 0;
4250
4251 $w = max $widget->{min_w}, $w; 4467 $w = max $widget->{min_w}, $w;
4252 $h = max $widget->{min_h}, $h; 4468 $h = max $widget->{min_h}, $h;
4253 4469
4254# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4470# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4255# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4471# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4256 4472
4257 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4473 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4258 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4474 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4259 4475
4260 $w = int $w + 0.5; 4476 $w = int $w + 0.5;
4261 $h = int $h + 0.5; 4477 $h = int $h + 0.5;
4262 4478
4263 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4479 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
4264 $widget->{old_w} = $widget->{w}; 4480 $widget->{old_w} = $widget->{w};
4265 $widget->{old_h} = $widget->{h}; 4481 $widget->{old_h} = $widget->{h};
4266 4482
4267 $widget->{w} = $w; 4483 $widget->{w} = $w;
4268 $widget->{h} = $h; 4484 $widget->{h} = $h;
4269 4485
4270 $widget->emit (size_allocate => $w, $h); 4486 $widget->emit (size_allocate => $w, $h);
4487 }
4271 } 4488 }
4272 } 4489 }
4273 } 4490 }
4274 4491
4275 while ($self->{post_alloc_hook}) { 4492 while ($self->{post_alloc_hook}) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines