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.395 by root, Sat Jul 21 13:56:00 2007 UTC vs.
Revision 1.432 by root, Tue Aug 21 23:42:02 2007 UTC

273 $self->emit ("destroy"); 273 $self->emit ("destroy");
274 %$self = (); 274 %$self = ();
275} 275}
276 276
277sub TO_JSON { 277sub TO_JSON {
278 { __widget_ref__ => $_[0]{s_id} } 278 { __w_ => $_[0]{s_id} }
279} 279}
280 280
281sub show { 281sub show {
282 my ($self) = @_; 282 my ($self) = @_;
283 283
593 593
594# using global variables seems a bit hacky, but passing through all drawing 594# using global variables seems a bit hacky, but passing through all drawing
595# functions seems pointless. 595# functions seems pointless.
596our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn 596our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
597 597
598sub draw { 598#sub draw {
599 my ($self) = @_; 599#CFPlus.xs
600
601 return unless $self->{h} && $self->{w};
602
603 # update screen rectangle
604 local $draw_x = $draw_x + $self->{x};
605 local $draw_y = $draw_y + $self->{y};
606
607 # skip widgets that are entirely outside the drawing area
608 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
609 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
610
611 glPushMatrix;
612 glTranslate $self->{x}, $self->{y}, 0;
613
614 if ($self == $HOVER && $self->{can_hover}) {
615 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
616 glEnable GL_BLEND;
617 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
618 glBegin GL_QUADS;
619 glVertex 0 , 0;
620 glVertex $self->{w}, 0;
621 glVertex $self->{w}, $self->{h};
622 glVertex 0 , $self->{h};
623 glEnd;
624 glDisable GL_BLEND;
625 }
626
627 if ($ENV{CFPLUS_DEBUG} & 1) {
628 glPushMatrix;
629 glColor 1, 1, 0, 1;
630 glTranslate 0.375, 0.375;
631 glBegin GL_LINE_LOOP;
632 glVertex 0 , 0;
633 glVertex $self->{w} - 1, 0;
634 glVertex $self->{w} - 1, $self->{h} - 1;
635 glVertex 0 , $self->{h} - 1;
636 glEnd;
637 glPopMatrix;
638 #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
639 }
640
641 $self->_draw;
642 glPopMatrix;
643}
644 600
645sub _draw { 601sub _draw {
646 my ($self) = @_; 602 my ($self) = @_;
647 603
648 warn "no draw defined for $self\n"; 604 warn "no draw defined for $self\n";
649} 605}
650 606
651my $cntx;#d#
652sub DESTROY { 607sub DESTROY {
653 my ($self) = @_; 608 my ($self) = @_;
654 609
655 return if CFPlus::in_destruct; 610 return if CFPlus::in_destruct;
656 611
612 local $@;
657 eval { $self->destroy }; 613 eval { $self->destroy };
658 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 614 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
659 615
660 delete $WIDGET{$self+0}; 616 delete $WIDGET{$self+0};
661} 617}
669use strict; 625use strict;
670use CFPlus::OpenGL; 626use CFPlus::OpenGL;
671 627
672sub new { 628sub new {
673 my $class = shift; 629 my $class = shift;
674
675 # range [value, low, high, page]
676 630
677 $class->SUPER::new ( 631 $class->SUPER::new (
678 #bg => [0, 0, 0, 0.2], 632 #bg => [0, 0, 0, 0.2],
679 #active_bg => [1, 1, 1, 0.5], 633 #active_bg => [1, 1, 1, 0.5],
680 @_ 634 @_
692 my ($w, $h) = @$self{qw(w h)}; 646 my ($w, $h) = @$self{qw(w h)};
693 647
694 glEnable GL_BLEND; 648 glEnable GL_BLEND;
695 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 649 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
696 glColor_premultiply @$color; 650 glColor_premultiply @$color;
697
698 glBegin GL_QUADS;
699 glVertex 0 , 0;
700 glVertex 0 , $h;
701 glVertex $w, $h; 651 glRect 0, 0, $w, $h;
702 glVertex $w, 0;
703 glEnd;
704
705 glDisable GL_BLEND; 652 glDisable GL_BLEND;
706 } 653 }
707} 654}
708 655
709############################################################################# 656#############################################################################
769 sort { $a->{z} <=> $b->{z} } 716 sort { $a->{z} <=> $b->{z} }
770 @{$self->{children}}, @widgets 717 @{$self->{children}}, @widgets
771 ]; 718 ];
772 719
773 $self->realloc; 720 $self->realloc;
721
722 $self->emit (c_add => \@widgets);
723
724 map $_+0, @widgets
774} 725}
775 726
776sub children { 727sub children {
777 @{ $_[0]{children} } 728 @{ $_[0]{children} }
778} 729}
779 730
780sub remove { 731sub remove {
781 my ($self, $child) = @_; 732 my ($self, @widgets) = @_;
782 733
734 $self->emit (c_remove => \@widgets);
735
736 for my $child (@widgets) {
783 delete $child->{parent}; 737 delete $child->{parent};
784 $child->hide; 738 $child->hide;
785
786 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 739 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
740 }
787 741
788 $self->realloc; 742 $self->realloc;
789} 743}
790 744
791sub clear { 745sub clear {
819} 773}
820 774
821sub _draw { 775sub _draw {
822 my ($self) = @_; 776 my ($self) = @_;
823 777
824 $_->draw for @{$self->{children}}; 778 $_->draw for $self->visible_children;
825} 779}
826 780
827############################################################################# 781#############################################################################
828 782
829package CFPlus::UI::Bin; 783package CFPlus::UI::Bin;
839} 793}
840 794
841sub add { 795sub add {
842 my ($self, $child) = @_; 796 my ($self, $child) = @_;
843 797
844 $self->SUPER::remove ($_) for @{ $self->{children} }; 798 $self->clear;
845 $self->SUPER::add ($child); 799 $self->SUPER::add ($child);
846} 800}
847 801
848sub remove { 802sub remove {
849 my ($self, $widget) = @_; 803 my ($self, $widget) = @_;
991 $self->{view_x} = $x; 945 $self->{view_x} = $x;
992 $self->{view_y} = $y; 946 $self->{view_y} = $y;
993 947
994 $self->emit (changed => $x, $y); 948 $self->emit (changed => $x, $y);
995 $self->update; 949 $self->update;
950 }
951}
952
953sub set_center {
954 my ($self, $x, $y) = @_;
955
956 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
957}
958
959sub make_visible {
960 my ($self, $x, $y, $border) = @_;
961
962 if ( $x < $self->{view_x} + $self->{w} * $border
963 || $x > $self->{view_x} + $self->{w} * (1 - $border)
964 || $y < $self->{view_y} + $self->{h} * $border
965 || $y > $self->{view_y} + $self->{h} * (1 - $border)
966 ) {
967 $self->set_center ($x, $y);
996 } 968 }
997} 969}
998 970
999# hmm, this does not work for topleft of $self... but we should not ask for that 971# hmm, this does not work for topleft of $self... but we should not ask for that
1000sub coord2local { 972sub coord2local {
1047 my $child = delete $arg{child}; 1019 my $child = delete $arg{child};
1048 1020
1049 my $self; 1021 my $self;
1050 1022
1051 my $hslider = new CFPlus::UI::Slider 1023 my $hslider = new CFPlus::UI::Slider
1052 col => 0, 1024 c_col => 0,
1053 row => 1, 1025 c_row => 1,
1054 vertical => 0, 1026 vertical => 0,
1055 range => [0, 0, 1, 0.01], # HACK fix 1027 range => [0, 0, 1, 0.01], # HACK fix
1056 on_changed => sub { 1028 on_changed => sub {
1057 $self->{hpos} = $_[1]; 1029 $self->{hpos} = $_[1];
1058 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1030 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1059 }, 1031 },
1060 ; 1032 ;
1061 1033
1062 my $vslider = new CFPlus::UI::Slider 1034 my $vslider = new CFPlus::UI::Slider
1063 col => 1, 1035 c_col => 1,
1064 row => 0, 1036 c_row => 0,
1065 vertical => 1, 1037 vertical => 1,
1066 range => [0, 0, 1, 0.01], # HACK fix 1038 range => [0, 0, 1, 0.01], # HACK fix
1067 on_changed => sub { 1039 on_changed => sub {
1068 $self->{vpos} = $_[1]; 1040 $self->{vpos} = $_[1];
1069 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1041 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1080 row_expand => [1, 0], 1052 row_expand => [1, 0],
1081 %arg, 1053 %arg,
1082 ); 1054 );
1083 1055
1084 $self->{vp} = new CFPlus::UI::ViewPort 1056 $self->{vp} = new CFPlus::UI::ViewPort
1085 col => 0, 1057 c_col => 0,
1086 row => 0, 1058 c_row => 0,
1087 expand => 1, 1059 expand => 1,
1088 scroll_x => $self->{scroll_x}, 1060 scroll_x => $self->{scroll_x},
1089 scroll_y => $self->{scroll_y}, 1061 scroll_y => $self->{scroll_y},
1090 on_changed => sub { 1062 on_changed => sub {
1091 my ($vp, $x, $y) = @_; 1063 my ($vp, $x, $y) = @_;
1093 $vp->{parent}{hslider}->set_value ($x); 1065 $vp->{parent}{hslider}->set_value ($x);
1094 $vp->{parent}{vslider}->set_value ($y); 1066 $vp->{parent}{vslider}->set_value ($y);
1095 1067
1096 0 1068 0
1097 }, 1069 },
1070 on_size_allocate => sub {
1071 my ($vp, $w, $h) = @_;
1072 $vp->{parent}->update_slider;
1073 0
1074 },
1098 ; 1075 ;
1099 1076
1100 $self->SUPER::add ($self->{vp}); 1077 $self->SUPER::add ($self->{vp});
1101 1078
1102 $self->add ($child) if $child; 1079 $self->add ($child) if $child;
1108 my ($self, $widget) = @_; 1085 my ($self, $widget) = @_;
1109 1086
1110 $self->{vp}->add ($self->{child} = $widget); 1087 $self->{vp}->add ($self->{child} = $widget);
1111} 1088}
1112 1089
1090sub set_offset { shift->{vp}->set_offset (@_) }
1091sub set_center { shift->{vp}->set_center (@_) }
1092sub make_visible { shift->{vp}->make_visible (@_) }
1093
1113sub update_slider { 1094sub update_slider {
1114 my ($self) = @_; 1095 my ($self) = @_;
1115 1096
1116 my $child = ($self->{vp} or return)->child; 1097 my $child = ($self->{vp} or return)->child;
1117 1098
1099 if ($self->{scroll_x}) {
1118 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1100 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1119 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); 1101 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1120 1102
1121 my $visible = $w1 > $w2; 1103 my $visible = $w1 > $w2;
1122 if ($visible != $self->{hslider_visible}) { 1104 if ($visible != $self->{hslider_visible}) {
1123 $self->{hslider_visible} = $visible; 1105 $self->{hslider_visible} = $visible;
1124 $visible ? $self->SUPER::add ($self->{hslider}) 1106 $visible ? $self->SUPER::add ($self->{hslider})
1125 : $self->SUPER::remove ($self->{hslider}); 1107 : $self->SUPER::remove ($self->{hslider});
1108 }
1126 } 1109 }
1127 1110
1111 if ($self->{scroll_y}) {
1128 my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); 1112 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1129 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); 1113 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1130 1114
1131 my $visible = $h1 > $h2; 1115 my $visible = $h1 > $h2;
1132 if ($visible != $self->{vslider_visible}) { 1116 if ($visible != $self->{vslider_visible}) {
1133 $self->{vslider_visible} = $visible; 1117 $self->{vslider_visible} = $visible;
1134 $visible ? $self->SUPER::add ($self->{vslider}) 1118 $visible ? $self->SUPER::add ($self->{vslider})
1135 : $self->SUPER::remove ($self->{vslider}); 1119 : $self->SUPER::remove ($self->{vslider});
1120 }
1121 }
1122}
1123
1124sub start_dragging {
1125 my ($self, $ev) = @_;
1126
1127 $self->grab_focus;
1128
1129 my $ox = $self->{vp}{view_x};
1130 my $oy = $self->{vp}{view_y};
1131
1132 $self->{motion} = sub {
1133 my ($ev, $x, $y) = @_;
1134
1135 $ox -= $ev->{xrel};
1136 $oy -= $ev->{yrel};
1137
1138 $self->{vp}->set_offset ($ox, $oy);
1136 } 1139 };
1137} 1140}
1138 1141
1139sub invoke_mouse_wheel { 1142sub invoke_mouse_wheel {
1140 my ($self, $ev) = @_; 1143 my ($self, $ev) = @_;
1141 1144
1142 $self->{vslider}->emit (mouse_wheel => $ev) 1145 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1143 if $ev->{dy};
1144
1145 $self->{hslider}->emit (mouse_wheel => $ev) 1146 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1146 if $ev->{dx};
1147 1147
1148 1 1148 1
1149} 1149}
1150 1150
1151sub invoke_button_down { 1151sub invoke_button_down {
1152 my ($self, $ev, $x, $y) = @_; 1152 my ($self, $ev, $x, $y) = @_;
1153 1153
1154 if ($ev->{button} == 2) { 1154 if ($ev->{button} == 2) {
1155 $self->grab_focus; 1155 $self->start_dragging ($ev);
1156
1157 my $ox = $self->{vp}{view_x} + $ev->{x};
1158 my $oy = $self->{vp}{view_y} + $ev->{y};
1159
1160 $self->{motion} = sub {
1161 my ($ev, $x, $y) = @_;
1162
1163 $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y});
1164 $self->update;
1165 };
1166
1167 return 1; 1156 return 1;
1168 } 1157 }
1169 1158
1170 0 1159 0
1171} 1160}
1222 my ($w, $h) = @$self{qw(w h)}; 1211 my ($w, $h) = @$self{qw(w h)};
1223 1212
1224 glEnable GL_BLEND; 1213 glEnable GL_BLEND;
1225 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1214 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1226 glColor_premultiply @{ $self->{bg} }; 1215 glColor_premultiply @{ $self->{bg} };
1227
1228 glBegin GL_QUADS;
1229 glVertex 0 , 0;
1230 glVertex 0 , $h;
1231 glVertex $w, $h; 1216 glRect 0, 0, $w, $h;
1232 glVertex $w, 0;
1233 glEnd;
1234
1235 glDisable GL_BLEND; 1217 glDisable GL_BLEND;
1236 } 1218 }
1237 1219
1238 $self->SUPER::_draw; 1220 $self->SUPER::_draw;
1239} 1221}
1601 1583
1602sub add { 1584sub add {
1603 my ($self, @widgets) = @_; 1585 my ($self, @widgets) = @_;
1604 1586
1605 for my $child (@widgets) { 1587 for my $child (@widgets) {
1606 $child->{rowspan} ||= 1; 1588 $child->{c_rowspan} ||= 1;
1607 $child->{colspan} ||= 1; 1589 $child->{c_colspan} ||= 1;
1608 } 1590 }
1609 1591
1610 $self->SUPER::add (@widgets); 1592 $self->SUPER::add (@widgets);
1611} 1593}
1612 1594
1616 my @widgets; 1598 my @widgets;
1617 1599
1618 while (@_) { 1600 while (@_) {
1619 my ($col, $row, $child) = splice @_, 0, 3, (); 1601 my ($col, $row, $child) = splice @_, 0, 3, ();
1620 1602
1621 $child->{row} = $row; 1603 $child->{c_row} = $row;
1622 $child->{col} = $col; 1604 $child->{c_col} = $col;
1623 1605
1624 push @widgets, $child; 1606 push @widgets, $child;
1625 } 1607 }
1626 1608
1627 $self->add (@widgets); 1609 $self->add (@widgets);
1633 my (@w, @h); 1615 my (@w, @h);
1634 1616
1635 my @children = $self->children; 1617 my @children = $self->children;
1636 1618
1637 # first pass, columns 1619 # first pass, columns
1638 for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) { 1620 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1639 my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; 1621 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1640 1622
1641 my $sw = sum @w[$c .. $c + $cs - 1]; 1623 my $sw = sum @w[$c .. $c + $cs - 1];
1642 1624
1643 if ($w > $sw) { 1625 if ($w > $sw) {
1644 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1626 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1645 } 1627 }
1646 } 1628 }
1647 1629
1648 # second pass, rows 1630 # second pass, rows
1649 for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) { 1631 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1650 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; 1632 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1651 1633
1652 my $sh = sum @h[$r .. $r + $rs - 1]; 1634 my $sh = sum @h[$r .. $r + $rs - 1];
1653 1635
1654 if ($h > $sh) { 1636 if ($h > $sh) {
1655 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1637 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1697 1679
1698 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1680 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1699 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1681 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1700 1682
1701 for my $widget ($self->children) { 1683 for my $widget ($self->children) {
1702 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)}; 1684 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1703 1685
1704 $widget->configure ( 1686 $widget->configure (
1705 $x[$c], $y[$r], 1687 $x[$c], $y[$r],
1706 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], 1688 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1707 ); 1689 );
1716 1698
1717use List::Util qw(min max); 1699use List::Util qw(min max);
1718 1700
1719our @ISA = CFPlus::UI::Container::; 1701our @ISA = CFPlus::UI::Container::;
1720 1702
1721sub add {
1722 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1723
1724 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1725 $self->SUPER::add ($child);
1726}
1727
1728sub _scale($$$) { 1703sub _scale($$$) {
1729 my ($mode, $val, $max) = @_; 1704 my ($rel, $val, $max) = @_;
1730 1705
1731 $mode eq "abs" ? $val 1706 $rel ? $val * $max : $val
1732 : $mode eq "rel" ? $val * $max
1733 : 0
1734} 1707}
1735 1708
1736sub size_request { 1709sub size_request {
1737 my ($self) = @_; 1710 my ($self) = @_;
1738 1711
1739 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1712 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1740 1713
1741 # determine overall size by querying abs widgets 1714 # determine overall size by querying abs widgets
1742 for my $child ($self->visible_children) { 1715 for my $child ($self->visible_children) {
1743 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1716 unless ($child->{c_rel}) {
1717 my $x = $child->{c_x};
1718 my $y = $child->{c_y};
1744 1719
1745 if ($pos eq "abs") {
1746 $w = _scale $size, $w, $child->{req_w};
1747 $h = _scale $size, $h, $child->{req_h};
1748
1749 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1720 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1750 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1721 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1751 } 1722 }
1752 } 1723 }
1753 1724
1754 my $W = $x2 - $x1; 1725 my $W = $x2 - $x1;
1755 my $H = $y2 - $y1; 1726 my $H = $y2 - $y1;
1756 1727
1757 # now layout remaining widgets 1728 # now layout remaining widgets
1758 for my $child ($self->visible_children) { 1729 for my $child ($self->visible_children) {
1759 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1730 if ($child->{c_rel}) {
1731 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1732 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1760 1733
1761 if ($pos ne "abs") {
1762 $x = _scale $pos, $x, $W;
1763 $y = _scale $pos, $x, $H;
1764 $w = _scale $size, $w, $child->{req_w};
1765 $h = _scale $size, $h, $child->{req_h};
1766
1767 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1734 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1768 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1735 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1769 } 1736 }
1770 } 1737 }
1771 1738
1772 my $W = $x2 - $x1; 1739 my $W = $x2 - $x1;
1773 my $H = $y2 - $y1; 1740 my $H = $y2 - $y1;
1777 1744
1778sub invoke_size_allocate { 1745sub invoke_size_allocate {
1779 my ($self, $W, $H) = @_; 1746 my ($self, $W, $H) = @_;
1780 1747
1781 for my $child ($self->visible_children) { 1748 for my $child ($self->visible_children) {
1782 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1749 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1750 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1783 1751
1784 $x = _scale $pos, $x, $W; 1752 $x += $child->{c_halign} * $child->{req_w};
1785 $y = _scale $pos, $x, $H; 1753 $y += $child->{c_valign} * $child->{req_h};
1786 $w = _scale $size, $w, $child->{req_w};
1787 $h = _scale $size, $h, $child->{req_h};
1788 1754
1789 $child->configure ($x, $y, $w, $h); 1755 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1790 } 1756 }
1791 1757
1792 1 1758 1
1793} 1759}
1794 1760
1798 1764
1799our @ISA = CFPlus::UI::Container::; 1765our @ISA = CFPlus::UI::Container::;
1800 1766
1801sub size_request { 1767sub size_request {
1802 my ($self) = @_; 1768 my ($self) = @_;
1769
1770 my @children = $self->visible_children;
1803 1771
1804 $self->{vertical} 1772 $self->{vertical}
1805 ? ( 1773 ? (
1806 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1774 (List::Util::max map $_->{req_w}, @children),
1807 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1775 (List::Util::sum map $_->{req_h}, @children),
1808 ) 1776 )
1809 : ( 1777 : (
1810 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1778 (List::Util::sum map $_->{req_w}, @children),
1811 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1779 (List::Util::max map $_->{req_h}, @children),
1812 ) 1780 )
1813} 1781}
1814 1782
1815sub invoke_size_allocate { 1783sub invoke_size_allocate {
1816 my ($self, $w, $h) = @_; 1784 my ($self, $w, $h) = @_;
1940 1908
1941 delete $self->{ox}; 1909 delete $self->{ox};
1942 $self->SUPER::realloc; 1910 $self->SUPER::realloc;
1943} 1911}
1944 1912
1913sub clear {
1914 my ($self) = @_;
1915
1916 $self->set_text ("");
1917}
1918
1945sub set_text { 1919sub set_text {
1946 my ($self, $text) = @_; 1920 my ($self, $text) = @_;
1947 1921
1948 return if $self->{text} eq "T$text"; 1922 return if $self->{text} eq "T$text";
1949 $self->{text} = "T$text"; 1923 $self->{text} = "T$text";
2055 : ($self->{w} - $size->[0]) * 0.5); 2029 : ($self->{w} - $size->[0]) * 0.5);
2056 2030
2057 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2031 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2058 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2032 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2059 : ($self->{h} - $size->[1]) * 0.5); 2033 : ($self->{h} - $size->[1]) * 0.5);
2034
2035 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2060 }; 2036 };
2061 2037
2062 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2038# unless ($self->{list}) {
2063 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2039# $self->{list} = CFPlus::OpenGL::glGenList;
2064 2040# CFPlus::OpenGL::glNewList $self->{list};
2065 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2041# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2042# CFPlus::OpenGL::glEndList;
2043# }
2044#
2045# CFPlus::OpenGL::glCallList $self->{list};
2046
2047 $self->{layout}->draw;
2066} 2048}
2049
2050#sub destroy {
2051# my ($self) = @_;
2052#
2053# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2054#
2055# $self->SUPER::destroy;
2056#}
2067 2057
2068############################################################################# 2058#############################################################################
2069 2059
2070package CFPlus::UI::EntryBase; 2060package CFPlus::UI::EntryBase;
2071 2061
2077 my $class = shift; 2067 my $class = shift;
2078 2068
2079 $class->SUPER::new ( 2069 $class->SUPER::new (
2080 fg => [1, 1, 1], 2070 fg => [1, 1, 1],
2081 bg => [0, 0, 0, 0.2], 2071 bg => [0, 0, 0, 0.2],
2072 outline => [0.6, 0.3, 0.1],
2082 active_bg => [1, 1, 1, 0.5], 2073 active_bg => [0, 0, 1, .2],
2083 active_fg => [0, 0, 0], 2074 active_fg => [1, 1, 1],
2075 active_outline => [1, 1, 0],
2084 can_hover => 1, 2076 can_hover => 1,
2085 can_focus => 1, 2077 can_focus => 1,
2086 valign => 0, 2078 valign => 0,
2087 can_events => 1, 2079 can_events => 1,
2088 ellipsise => 0, 2080 ellipsise => 0,
2228 glColor_premultiply @{$self->{bg}}; 2220 glColor_premultiply @{$self->{bg}};
2229 } 2221 }
2230 2222
2231 glEnable GL_BLEND; 2223 glEnable GL_BLEND;
2232 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2224 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2233 glBegin GL_QUADS;
2234 glVertex 0 , 0;
2235 glVertex 0 , $self->{h};
2236 glVertex $self->{w}, $self->{h}; 2225 glRect 0, 0, $self->{w}, $self->{h};
2237 glVertex $self->{w}, 0;
2238 glEnd;
2239 glDisable GL_BLEND; 2226 glDisable GL_BLEND;
2240 2227
2241 $self->SUPER::_draw; 2228 $self->SUPER::_draw;
2242 2229
2243 #TODO: force update every cursor change :( 2230 #TODO: force update every cursor change :(
2245 2232
2246 unless (exists $self->{cur_h}) { 2233 unless (exists $self->{cur_h}) {
2247 my $text = substr $self->{text}, 0, $self->{cursor}; 2234 my $text = substr $self->{text}, 0, $self->{cursor};
2248 utf8::encode $text; 2235 utf8::encode $text;
2249 2236
2250 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2237 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2251 } 2238 }
2252 2239
2240 glColor_premultiply @{$self->{active_fg}};
2253 glBegin GL_LINES; 2241 glBegin GL_LINES;
2254 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2242 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
2255 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2243 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2244 glEnd;
2245
2246 glLineWidth 3;
2247 glColor @{$self->{active_outline}};
2248 glRect_lineloop 0, 0, $self->{w} - 1, $self->{h} - 1;
2249 glLineWidth 1;
2250
2251 } else {
2252 glColor @{$self->{outline}};
2253 glTranslate .375, .375;
2254 glBegin GL_LINE_STRIP;
2255 glVertex 0, $self->{h} * .5;
2256 glVertex 0, $self->{h} - 3;
2257 glVertex $self->{w} - 1, $self->{h} - 3;
2258 glVertex $self->{w} - 1, $self->{h} * .5;
2256 glEnd; 2259 glEnd;
2257 } 2260 }
2258} 2261}
2259 2262
2260############################################################################# 2263#############################################################################
2347 1 2350 1
2348} 2351}
2349 2352
2350############################################################################# 2353#############################################################################
2351 2354
2355package CFPlus::UI::ButtonBin;
2356
2357our @ISA = CFPlus::UI::Bin::;
2358
2359use CFPlus::OpenGL;
2360
2361my @tex =
2362 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2363 qw(b1_button_inactive.png b1_button_active.png);
2364
2365sub new {
2366 my $class = shift;
2367
2368 $class->SUPER::new (
2369 can_hover => 1,
2370 align => 0,
2371 valign => 0,
2372 can_events => 1,
2373 @_
2374 )
2375}
2376
2377sub invoke_button_up {
2378 my ($self, $ev, $x, $y) = @_;
2379
2380 $self->emit ("activate")
2381 if $x >= 0 && $x < $self->{w}
2382 && $y >= 0 && $y < $self->{h};
2383
2384 1
2385}
2386
2387sub _draw {
2388 my ($self) = @_;
2389
2390 glEnable GL_TEXTURE_2D;
2391 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2392 glColor 0, 0, 0, 1;
2393
2394 my $tex = $tex[$GRAB == $self];
2395 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2396
2397 glDisable GL_TEXTURE_2D;
2398
2399 $self->SUPER::_draw;
2400}
2401
2402#############################################################################
2403
2352package CFPlus::UI::Button; 2404package CFPlus::UI::Button;
2353 2405
2354our @ISA = CFPlus::UI::Label::; 2406our @ISA = CFPlus::UI::Label::;
2355 2407
2356use CFPlus::OpenGL; 2408use CFPlus::OpenGL;
2490sub new { 2542sub new {
2491 my $class = shift; 2543 my $class = shift;
2492 2544
2493 my $self = $class->SUPER::new ( 2545 my $self = $class->SUPER::new (
2494 can_events => 0, 2546 can_events => 0,
2547 scale => 1,
2495 @_, 2548 @_,
2496 ); 2549 );
2497 2550
2498 $self->{path} || $self->{tex} 2551 $self->{path} || $self->{tex}
2499 or Carp::croak "'path' or 'tex' attributes required"; 2552 or Carp::croak "'path' or 'tex' attributes required";
2524} 2577}
2525 2578
2526sub size_request { 2579sub size_request {
2527 my ($self) = @_; 2580 my ($self) = @_;
2528 2581
2529 ($self->{tex}{w}, $self->{tex}{h}) 2582 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2530} 2583}
2531 2584
2532sub _draw { 2585sub _draw {
2533 my ($self) = @_; 2586 my ($self) = @_;
2534 2587
2544 } 2597 }
2545 2598
2546 glEnable GL_TEXTURE_2D; 2599 glEnable GL_TEXTURE_2D;
2547 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2600 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2548 2601
2549 $tex->draw_quad (0, 0, $w, $h); 2602 $tex->draw_quad_alpha (0, 0, $w, $h);
2550 2603
2551 glDisable GL_TEXTURE_2D; 2604 glDisable GL_TEXTURE_2D;
2552} 2605}
2553 2606
2554############################################################################# 2607#############################################################################
2573 align => 0, 2626 align => 0,
2574 valign => 0, 2627 valign => 0,
2575 can_events => 1, 2628 can_events => 1,
2576 @_ 2629 @_
2577 ); 2630 );
2631}
2632
2633sub invoke_button_down {
2634 my ($self, $ev, $x, $y) = @_;
2635
2636 1
2578} 2637}
2579 2638
2580sub invoke_button_up { 2639sub invoke_button_up {
2581 my ($self, $ev, $x, $y) = @_; 2640 my ($self, $ev, $x, $y) = @_;
2582 2641
2722 glDisable GL_TEXTURE_2D; 2781 glDisable GL_TEXTURE_2D;
2723} 2782}
2724 2783
2725############################################################################# 2784#############################################################################
2726 2785
2786package CFPlus::UI::Progress;
2787
2788our @ISA = CFPlus::UI::Label::;
2789
2790use CFPlus::OpenGL;
2791
2792sub new {
2793 my ($class, %arg) = @_;
2794
2795 my $self = $class->SUPER::new (
2796 fg => [1, 1, 1],
2797 bg => [0, 0, 1, 0.2],
2798 bar => [0.7, 0.5, 0.1, 0.8],
2799 outline => [0.4, 0.3, 0],
2800 fontsize => 0.9,
2801 valign => 0,
2802 align => 0,
2803 can_events => 1,
2804 ellipsise => 1,
2805 %arg,
2806 );
2807
2808 $self->set_value ($arg{value} || -1);
2809
2810 $self
2811}
2812
2813sub set_value {
2814 my ($self, $value) = @_;
2815
2816 if ($self->{value} != $value) {
2817 $self->{value} = $value;
2818
2819 if ($value < 0) {
2820 $self->set_text ("-");
2821 } else {
2822 $self->set_text (sprintf "%d%%", $value * 100);
2823 }
2824 $self->update;
2825 }
2826}
2827
2828sub _draw {
2829 my ($self) = @_;
2830
2831 glEnable GL_BLEND;
2832 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2833
2834 if ($self->{value} >= 0) {
2835 my $s = 3 + ($self->{w} - 6) * $self->{value};
2836
2837 glColor_premultiply @{$self->{bar}};
2838 glRect 2, 2, $s, $self->{h} - 2;
2839 glColor_premultiply @{$self->{bg}};
2840 glRect $s + 1, 0, $self->{w} - 2, $self->{h} - 2;
2841 }
2842
2843 glColor_premultiply @{$self->{outline}};
2844 glRect_lineloop 1, 1, $self->{w} - 2, $self->{h} - 2;
2845
2846 glDisable GL_BLEND;
2847
2848 {
2849 local $self->{bg}; # do not draw background
2850 $self->SUPER::_draw;
2851 }
2852}
2853
2854#############################################################################
2855
2856package CFPlus::UI::ExperienceProgress;
2857
2858our @ISA = CFPlus::UI::Progress::;
2859
2860sub new {
2861 my ($class, %arg) = @_;
2862
2863 my $self = $class->SUPER::new (
2864 %arg
2865 );
2866
2867 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2868 if $::CONN;
2869
2870 $self
2871}
2872
2873sub DESTROY {
2874 my ($self) = @_;
2875
2876 delete $::CONN->{on_exp_update}{$self+0}
2877 if $::CONN;
2878
2879 $self->SUPER::DESTROY;
2880}
2881
2882sub set_value {
2883 my ($self, $lvl, $exp) = @_;
2884
2885 my $v = -1;
2886
2887 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2888 my $l0 = $table->[$lvl - 1];
2889 my $l1 = $table->[$lvl];
2890
2891 $v = ($exp - $l0) / ($l1 - $l0);
2892 }
2893
2894 $self->SUPER::set_value ($v);
2895}
2896
2897#############################################################################
2898
2727package CFPlus::UI::Gauge; 2899package CFPlus::UI::Gauge;
2728 2900
2729our @ISA = CFPlus::UI::VBox::; 2901our @ISA = CFPlus::UI::VBox::;
2730 2902
2731sub new { 2903sub new {
2856 3028
2857 $self->SUPER::invoke_button_down ($ev, $x, $y); 3029 $self->SUPER::invoke_button_down ($ev, $x, $y);
2858 3030
2859 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3031 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2860 3032
2861 $self->invoke_mouse_motion ($ev, $x, $y) 3033 $self->invoke_mouse_motion ($ev, $x, $y);
3034
3035 1
2862} 3036}
2863 3037
2864sub invoke_mouse_motion { 3038sub invoke_mouse_motion {
2865 my ($self, $ev, $x, $y) = @_; 3039 my ($self, $ev, $x, $y) = @_;
2866 3040
2886 3060
2887 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3061 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2888 3062
2889 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3063 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2890 3064
2891 ! ! $delta 3065 1
2892} 3066}
2893 3067
2894sub update { 3068sub update {
2895 my ($self) = @_; 3069 my ($self) = @_;
2896 3070
3027} 3201}
3028 3202
3029sub size_request { 3203sub size_request {
3030 my ($self) = @_; 3204 my ($self) = @_;
3031 3205
3032 my ($empty, $slider) = @{ $self->{children} }; 3206 my ($empty, $slider) = $self->visible_children;
3033 3207
3034 local $self->{children} = [$empty, $slider]; 3208 local $self->{children} = [$empty, $slider];
3035 $self->SUPER::size_request 3209 $self->SUPER::size_request
3036} 3210}
3037 3211
3226 3400
3227 if ($y0 < $y + $h && $y < $y1) { 3401 if ($y0 < $y + $h && $y < $y1) {
3228 my $layout = $self->get_layout ($para); 3402 my $layout = $self->get_layout ($para);
3229 3403
3230 $layout->render ($para->{indent}, $y - $y0); 3404 $layout->render ($para->{indent}, $y - $y0);
3405 $layout->draw;
3231 3406
3232 if (my @w = @{ $para->{widget} }) { 3407 if (my @w = @{ $para->{widget} }) {
3233 my @s = $layout->get_shapes; 3408 my @s = $layout->get_shapes;
3234 3409
3235 for (@w) { 3410 for (@w) {
3426 glTranslate 0.375, 0.375; 3601 glTranslate 0.375, 0.375;
3427 3602
3428 my ($w, $h) = @$self{qw(w h)}; 3603 my ($w, $h) = @$self{qw(w h)};
3429 3604
3430 glColor 1, 0.8, 0.4; 3605 glColor 1, 0.8, 0.4;
3431 glBegin GL_QUADS; 3606 glRect 0, 0, $w, $h;
3432 glVertex 0 , 0;
3433 glVertex 0 , $h;
3434 glVertex $w, $h;
3435 glVertex $w, 0;
3436 glEnd;
3437 3607
3438 glColor 0, 0, 0; 3608 glColor 0, 0, 0;
3439 glBegin GL_LINE_LOOP; 3609 glRect_lineloop 0, 0, $w, $h;
3440 glVertex 0 , 0;
3441 glVertex 0 , $h;
3442 glVertex $w, $h;
3443 glVertex $w, 0;
3444 glEnd;
3445 3610
3446 glTranslate 2 - 0.375, 2 - 0.375; 3611 glTranslate 2 - 0.375, 2 - 0.375;
3447 3612
3448 $self->SUPER::_draw; 3613 $self->SUPER::_draw;
3449} 3614}
3469 3634
3470 if ($self->{anim} && $self->{animspeed}) { 3635 if ($self->{anim} && $self->{animspeed}) {
3471 CFPlus::weaken (my $widget = $self); 3636 CFPlus::weaken (my $widget = $self);
3472 3637
3473 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3638 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3474 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed}; 3639 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3475 $self->{timer} = Event->timer ( 3640 $self->{timer} = Event->timer (
3476 parked => 1, 3641 parked => 1,
3477 cb => sub { 3642 cb => sub {
3478 return unless $::CONN && $widget; 3643 return unless $::CONN;
3479 3644
3645 my $w = $widget
3646 or return;
3647
3480 ++$widget->{frame}; 3648 ++$w->{frame};
3481 $widget->update_face; 3649 $w->update_face;
3650
3651 # somehow, $widget can go away
3482 $widget->update; 3652 $w->update;
3483
3484 $widget->update_timer; 3653 $w->update_timer;
3485 }, 3654 },
3486 ); 3655 );
3487 3656
3488 $self->update_face; 3657 $self->update_face;
3489 $self->update_timer; 3658 $self->update_timer;
3510} 3679}
3511 3680
3512sub update_face { 3681sub update_face {
3513 my ($self) = @_; 3682 my ($self) = @_;
3514 3683
3515 return unless $::CONN;
3516
3517 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3518 if ($anim && @$anim) {
3519 delete $self->{wait_face};
3520 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3521 }
3522 }
3523}
3524
3525sub size_request {
3526 my ($self) = @_;
3527
3528 if ($::CONN) { 3684 if ($::CONN) {
3529 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3685 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3530 if (my $tex = $::CONN->{texture}[$faceid]) { 3686 if ($anim && @$anim) {
3531 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3687 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3532 } else { 3688 delete $self->{face_change_cb};
3533 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3689
3534 $self->realloc; 3690 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3691 unless ($tex->{name} || $tex->{loading}) {
3692 $tex->upload (sub { $self->reconfigure });
3693 }
3535 }); 3694 }
3536 } 3695 }
3537 } 3696 }
3538 } 3697 }
3698}
3699
3700sub size_request {
3701 my ($self) = @_;
3702
3703 if ($::CONN) {
3704 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3705 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3706 if ($tex->{name}) {
3707 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3708 } elsif (!$tex->{loading}) {
3709 $tex->upload (sub { $self->reconfigure });
3710 }
3711 }
3712
3713 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3714 }
3715 }
3539 3716
3540 ($self->{size_w} || 8, $self->{size_h} || 8) 3717 ($self->{size_w} || 8, $self->{size_h} || 8)
3541} 3718}
3542 3719
3543sub update { 3720sub update {
3557} 3734}
3558 3735
3559sub _draw { 3736sub _draw {
3560 my ($self) = @_; 3737 my ($self) = @_;
3561 3738
3562 return unless $::CONN;
3563
3564 $self->SUPER::_draw; 3739 $self->SUPER::_draw;
3565 3740
3566 my $faceid = $::CONN->{faceid}[$self->{face}] 3741 if (my $tex = $self->{tex}) {
3567 or return;
3568
3569 my $tex = $::CONN->{texture}[$faceid];
3570
3571 if ($tex) {
3572 glEnable GL_TEXTURE_2D; 3742 glEnable GL_TEXTURE_2D;
3573 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3743 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3574 glColor 0, 0, 0, 1; 3744 glColor 0, 0, 0, 1;
3575 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3745 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3576 glDisable GL_TEXTURE_2D; 3746 glDisable GL_TEXTURE_2D;
3628 children => [ 3798 children => [
3629 (new CFPlus::UI::Label markup => $left, expand => 1), 3799 (new CFPlus::UI::Label markup => $left, expand => 1),
3630 (new CFPlus::UI::Label markup => $right, align => +1), 3800 (new CFPlus::UI::Label markup => $right, align => +1),
3631 ], 3801 ],
3632 ; 3802 ;
3633 3803
3634 } else { 3804 } else {
3635 $widget = new CFPlus::UI::Label 3805 $widget = new CFPlus::UI::Label
3636 can_hover => 1, 3806 can_hover => 1,
3637 can_events => 1, 3807 can_events => 1,
3638 markup => $widget, 3808 markup => $widget,
3763 3933
3764############################################################################# 3934#############################################################################
3765 3935
3766package CFPlus::UI::Notebook; 3936package CFPlus::UI::Notebook;
3767 3937
3938use CFPlus::OpenGL;
3939
3768our @ISA = CFPlus::UI::VBox::; 3940our @ISA = CFPlus::UI::VBox::;
3769 3941
3770sub new { 3942sub new {
3771 my $class = shift; 3943 my $class = shift;
3772 3944
3773 my $self = $class->SUPER::new ( 3945 my $self = $class->SUPER::new (
3774 buttonbar => (new CFPlus::UI::Buttonbar), 3946 buttonbar => (new CFPlus::UI::Buttonbar),
3775 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3947 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3948 active_outline => [1, 1, 0],
3776 # filter => # will be put between multiplexer and $self 3949 # filter => # will be put between multiplexer and $self
3777 @_, 3950 @_,
3778 ); 3951 );
3779 3952
3780 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3953 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3781 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3954 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3782 3955
3956 {
3957 Scalar::Util::weaken (my $wself = $self);
3958
3959 $self->{multiplexer}->connect (c_add => sub {
3960 my ($mplex, $widgets) = @_;
3961
3962 for my $child (@$widgets) {
3963 Scalar::Util::weaken $child;
3964 $child->{c_tab_} ||= do {
3965 my $tab =
3966 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base")
3967 ? $child->{c_tab}
3968 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
3969
3970 $tab->connect (activate => sub {
3971 $wself->set_current_page ($child);
3972 });
3973
3974 $tab
3975 };
3976
3977 $self->{buttonbar}->add ($child->{c_tab_});
3978 }
3979 });
3980
3981 $self->{multiplexer}->connect (c_remove => sub {
3982 my ($mplex, $widgets) = @_;
3983
3984 for my $child (@$widgets) {
3985 $wself->{buttonbar}->remove ($child->{c_tab_});
3986 }
3987 });
3988 }
3989
3783 $self 3990 $self
3784} 3991}
3785 3992
3786sub add { 3993sub add {
3994 my ($self, @widgets) = @_;
3995
3996 $self->{multiplexer}->add (@widgets)
3997}
3998
3999sub remove {
4000 my ($self, @widgets) = @_;
4001
4002 $self->{multiplexer}->remove (@widgets)
4003}
4004
4005sub pages {
4006 my ($self) = @_;
4007 $self->{multiplexer}->children
4008}
4009
4010sub add_tab {
3787 my ($self, $title, $widget, $tooltip) = @_; 4011 my ($self, $title, $widget, $tooltip) = @_;
3788 4012
3789 CFPlus::weaken $self; 4013 $title = [$title, $tooltip] unless ref $title;
4014 $widget->{c_tab} = $title;
3790 4015
3791 $self->{buttonbar}->add (new CFPlus::UI::Button
3792 markup => $title,
3793 tooltip => $tooltip,
3794 on_activate => sub { $self->set_current_page ($widget) },
3795 );
3796
3797 $self->{multiplexer}->add ($widget); 4016 $self->add ($widget);
3798} 4017}
3799 4018
3800sub get_current_page { 4019sub get_current_page {
3801 my ($self) = @_; 4020 my ($self) = @_;
3802 4021
3806sub set_current_page { 4025sub set_current_page {
3807 my ($self, $page) = @_; 4026 my ($self, $page) = @_;
3808 4027
3809 $self->{multiplexer}->set_current_page ($page); 4028 $self->{multiplexer}->set_current_page ($page);
3810 $self->emit (page_changed => $self->{multiplexer}{current}); 4029 $self->emit (page_changed => $self->{multiplexer}{current});
4030}
4031
4032sub _draw {
4033 my ($self) = @_;
4034
4035 $self->SUPER::_draw ();
4036
4037 if (my $cur = $self->{multiplexer}{current}) {
4038 if ($cur = $cur->{c_tab_}) {
4039 glTranslate $cur->{x}, $cur->{y};
4040 glLineWidth 3;
4041 glColor @{$self->{active_outline}};
4042 glRect_lineloop 1, 1, $cur->{w} - 2, $cur->{h} - 2;
4043 glLineWidth 1;
4044 }
4045 }
3811} 4046}
3812 4047
3813############################################################################# 4048#############################################################################
3814 4049
3815package CFPlus::UI::Selector; 4050package CFPlus::UI::Selector;
4085} 4320}
4086 4321
4087sub update { 4322sub update {
4088 my ($self) = @_; 4323 my ($self) = @_;
4089 4324
4090 $::WANT_REFRESH++; 4325 $::WANT_REFRESH->start;
4091} 4326}
4092 4327
4093sub add { 4328sub add {
4094 my ($self, @children) = @_; 4329 my ($self, @children) = @_;
4095 4330
4132 while ($self->{refresh_hook}) { 4367 while ($self->{refresh_hook}) {
4133 $_->() 4368 $_->()
4134 for values %{delete $self->{refresh_hook}}; 4369 for values %{delete $self->{refresh_hook}};
4135 } 4370 }
4136 4371
4137 if ($self->{realloc}) { 4372 while ($self->{realloc}) {
4138 my %queue; 4373 my %queue;
4139 my @queue; 4374 my @queue;
4140 my $widget; 4375 my $widget;
4141 4376
4142 outer: 4377 outer:
4189 } 4424 }
4190 } 4425 }
4191 4426
4192 delete $self->{realloc}{$widget+0}; 4427 delete $self->{realloc}{$widget+0};
4193 } 4428 }
4194 }
4195 4429
4196 while (my $size_alloc = delete $self->{size_alloc}) { 4430 while (my $size_alloc = delete $self->{size_alloc}) {
4197 my @queue = sort { $b->{visible} <=> $a->{visible} } 4431 my @queue = sort { $a->{visible} <=> $b->{visible} }
4198 values %$size_alloc; 4432 values %$size_alloc;
4199 4433
4200 while () { 4434 while () {
4201 my $widget = pop @queue || last; 4435 my $widget = pop @queue || last;
4202 4436
4203 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4437 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4204 4438
4205 $w = 0 if $w < 0;
4206 $h = 0 if $h < 0;
4207
4208 $w = max $widget->{min_w}, $w; 4439 $w = max $widget->{min_w}, $w;
4209 $h = max $widget->{min_h}, $h; 4440 $h = max $widget->{min_h}, $h;
4210 4441
4211# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4442# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4212# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4443# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4213 4444
4214 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4445 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4215 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4446 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4216 4447
4217 $w = int $w + 0.5; 4448 $w = int $w + 0.5;
4218 $h = int $h + 0.5; 4449 $h = int $h + 0.5;
4219 4450
4220 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4451 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
4221 $widget->{old_w} = $widget->{w}; 4452 $widget->{old_w} = $widget->{w};
4222 $widget->{old_h} = $widget->{h}; 4453 $widget->{old_h} = $widget->{h};
4223 4454
4224 $widget->{w} = $w; 4455 $widget->{w} = $w;
4225 $widget->{h} = $h; 4456 $widget->{h} = $h;
4226 4457
4227 $widget->emit (size_allocate => $w, $h); 4458 $widget->emit (size_allocate => $w, $h);
4459 }
4228 } 4460 }
4229 } 4461 }
4230 } 4462 }
4231 4463
4232 while ($self->{post_alloc_hook}) { 4464 while ($self->{post_alloc_hook}) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines