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.434 by root, Wed Aug 22 21:32:09 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 label => "%d%%",
2806 %arg,
2807 );
2808
2809 $self->set_value ($arg{value} || -1);
2810
2811 $self
2812}
2813
2814sub set_label {
2815 my ($self, $label) = @_;
2816
2817 return if $self->{label} eq $label;
2818 $self->{label} = $label;
2819
2820 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value});
2821}
2822
2823sub set_value {
2824 my ($self, $value) = @_;
2825
2826 if ($self->{value} ne $value) {
2827 $self->{value} = $value;
2828
2829 if ($value < 0) {
2830 $self->set_text ("-");
2831 } else {
2832 $self->set_text (sprintf $self->{label}, $value * 100);
2833 }
2834
2835 $self->update;
2836 }
2837}
2838
2839sub _draw {
2840 my ($self) = @_;
2841
2842 glEnable GL_BLEND;
2843 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2844
2845 if ($self->{value} >= 0) {
2846 my $s = 3 + ($self->{w} - 6) * $self->{value};
2847
2848 glColor_premultiply @{$self->{bar}};
2849 glRect 2, 2, $s, $self->{h} - 2;
2850 glColor_premultiply @{$self->{bg}};
2851 glRect $s + 1, 0, $self->{w} - 2, $self->{h} - 2;
2852 }
2853
2854 glColor_premultiply @{$self->{outline}};
2855 glRect_lineloop 1, 1, $self->{w} - 2, $self->{h} - 2;
2856
2857 glDisable GL_BLEND;
2858
2859 {
2860 local $self->{bg}; # do not draw background
2861 $self->SUPER::_draw;
2862 }
2863}
2864
2865#############################################################################
2866
2867package CFPlus::UI::ExperienceProgress;
2868
2869our @ISA = CFPlus::UI::Progress::;
2870
2871sub new {
2872 my ($class, %arg) = @_;
2873
2874 my $self = $class->SUPER::new (
2875 tooltip => sub {
2876 my ($self) = @_;
2877
2878 sprintf "%s points experience (level %d).\n%s points to next level %s.",
2879 ::formsep $self->{exp},
2880 $self->{lvl},
2881 ::formsep $self->{nxt}
2882 },
2883 %arg
2884 );
2885
2886 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2887 if $::CONN;
2888
2889 $self
2890}
2891
2892sub DESTROY {
2893 my ($self) = @_;
2894
2895 delete $::CONN->{on_exp_update}{$self+0}
2896 if $::CONN;
2897
2898 $self->SUPER::DESTROY;
2899}
2900
2901sub set_value {
2902 my ($self, $lvl, $exp) = @_;
2903
2904 $self->{lvl} = $exp;
2905 $self->{exp} = $exp;
2906
2907 my $v = -1;
2908
2909 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2910 my $l0 = $table->[$lvl - 1];
2911 my $l1 = $table->[$lvl];
2912
2913 $self->{nxt} = $l1;
2914
2915 $v = ($exp - $l0) / ($l1 - $l0);
2916 }
2917
2918 $self->SUPER::set_value ($v);
2919}
2920
2921#############################################################################
2922
2727package CFPlus::UI::Gauge; 2923package CFPlus::UI::Gauge;
2728 2924
2729our @ISA = CFPlus::UI::VBox::; 2925our @ISA = CFPlus::UI::VBox::;
2730 2926
2731sub new { 2927sub new {
2856 3052
2857 $self->SUPER::invoke_button_down ($ev, $x, $y); 3053 $self->SUPER::invoke_button_down ($ev, $x, $y);
2858 3054
2859 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3055 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2860 3056
2861 $self->invoke_mouse_motion ($ev, $x, $y) 3057 $self->invoke_mouse_motion ($ev, $x, $y);
3058
3059 1
2862} 3060}
2863 3061
2864sub invoke_mouse_motion { 3062sub invoke_mouse_motion {
2865 my ($self, $ev, $x, $y) = @_; 3063 my ($self, $ev, $x, $y) = @_;
2866 3064
2886 3084
2887 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3085 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2888 3086
2889 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3087 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2890 3088
2891 ! ! $delta 3089 1
2892} 3090}
2893 3091
2894sub update { 3092sub update {
2895 my ($self) = @_; 3093 my ($self) = @_;
2896 3094
3027} 3225}
3028 3226
3029sub size_request { 3227sub size_request {
3030 my ($self) = @_; 3228 my ($self) = @_;
3031 3229
3032 my ($empty, $slider) = @{ $self->{children} }; 3230 my ($empty, $slider) = $self->visible_children;
3033 3231
3034 local $self->{children} = [$empty, $slider]; 3232 local $self->{children} = [$empty, $slider];
3035 $self->SUPER::size_request 3233 $self->SUPER::size_request
3036} 3234}
3037 3235
3226 3424
3227 if ($y0 < $y + $h && $y < $y1) { 3425 if ($y0 < $y + $h && $y < $y1) {
3228 my $layout = $self->get_layout ($para); 3426 my $layout = $self->get_layout ($para);
3229 3427
3230 $layout->render ($para->{indent}, $y - $y0); 3428 $layout->render ($para->{indent}, $y - $y0);
3429 $layout->draw;
3231 3430
3232 if (my @w = @{ $para->{widget} }) { 3431 if (my @w = @{ $para->{widget} }) {
3233 my @s = $layout->get_shapes; 3432 my @s = $layout->get_shapes;
3234 3433
3235 for (@w) { 3434 for (@w) {
3426 glTranslate 0.375, 0.375; 3625 glTranslate 0.375, 0.375;
3427 3626
3428 my ($w, $h) = @$self{qw(w h)}; 3627 my ($w, $h) = @$self{qw(w h)};
3429 3628
3430 glColor 1, 0.8, 0.4; 3629 glColor 1, 0.8, 0.4;
3431 glBegin GL_QUADS; 3630 glRect 0, 0, $w, $h;
3432 glVertex 0 , 0;
3433 glVertex 0 , $h;
3434 glVertex $w, $h;
3435 glVertex $w, 0;
3436 glEnd;
3437 3631
3438 glColor 0, 0, 0; 3632 glColor 0, 0, 0;
3439 glBegin GL_LINE_LOOP; 3633 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 3634
3446 glTranslate 2 - 0.375, 2 - 0.375; 3635 glTranslate 2 - 0.375, 2 - 0.375;
3447 3636
3448 $self->SUPER::_draw; 3637 $self->SUPER::_draw;
3449} 3638}
3469 3658
3470 if ($self->{anim} && $self->{animspeed}) { 3659 if ($self->{anim} && $self->{animspeed}) {
3471 CFPlus::weaken (my $widget = $self); 3660 CFPlus::weaken (my $widget = $self);
3472 3661
3473 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3662 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3474 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed}; 3663 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3475 $self->{timer} = Event->timer ( 3664 $self->{timer} = Event->timer (
3476 parked => 1, 3665 parked => 1,
3477 cb => sub { 3666 cb => sub {
3478 return unless $::CONN && $widget; 3667 return unless $::CONN;
3479 3668
3669 my $w = $widget
3670 or return;
3671
3480 ++$widget->{frame}; 3672 ++$w->{frame};
3481 $widget->update_face; 3673 $w->update_face;
3674
3675 # somehow, $widget can go away
3482 $widget->update; 3676 $w->update;
3483
3484 $widget->update_timer; 3677 $w->update_timer;
3485 }, 3678 },
3486 ); 3679 );
3487 3680
3488 $self->update_face; 3681 $self->update_face;
3489 $self->update_timer; 3682 $self->update_timer;
3510} 3703}
3511 3704
3512sub update_face { 3705sub update_face {
3513 my ($self) = @_; 3706 my ($self) = @_;
3514 3707
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) { 3708 if ($::CONN) {
3529 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3709 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3530 if (my $tex = $::CONN->{texture}[$faceid]) { 3710 if ($anim && @$anim) {
3531 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3711 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3532 } else { 3712 delete $self->{face_change_cb};
3533 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3713
3534 $self->realloc; 3714 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3715 unless ($tex->{name} || $tex->{loading}) {
3716 $tex->upload (sub { $self->reconfigure });
3717 }
3535 }); 3718 }
3536 } 3719 }
3537 } 3720 }
3538 } 3721 }
3722}
3723
3724sub size_request {
3725 my ($self) = @_;
3726
3727 if ($::CONN) {
3728 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3729 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3730 if ($tex->{name}) {
3731 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3732 } elsif (!$tex->{loading}) {
3733 $tex->upload (sub { $self->reconfigure });
3734 }
3735 }
3736
3737 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3738 }
3739 }
3539 3740
3540 ($self->{size_w} || 8, $self->{size_h} || 8) 3741 ($self->{size_w} || 8, $self->{size_h} || 8)
3541} 3742}
3542 3743
3543sub update { 3744sub update {
3557} 3758}
3558 3759
3559sub _draw { 3760sub _draw {
3560 my ($self) = @_; 3761 my ($self) = @_;
3561 3762
3562 return unless $::CONN;
3563
3564 $self->SUPER::_draw; 3763 $self->SUPER::_draw;
3565 3764
3566 my $faceid = $::CONN->{faceid}[$self->{face}] 3765 if (my $tex = $self->{tex}) {
3567 or return;
3568
3569 my $tex = $::CONN->{texture}[$faceid];
3570
3571 if ($tex) {
3572 glEnable GL_TEXTURE_2D; 3766 glEnable GL_TEXTURE_2D;
3573 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3767 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3574 glColor 0, 0, 0, 1; 3768 glColor 0, 0, 0, 1;
3575 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3769 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3576 glDisable GL_TEXTURE_2D; 3770 glDisable GL_TEXTURE_2D;
3628 children => [ 3822 children => [
3629 (new CFPlus::UI::Label markup => $left, expand => 1), 3823 (new CFPlus::UI::Label markup => $left, expand => 1),
3630 (new CFPlus::UI::Label markup => $right, align => +1), 3824 (new CFPlus::UI::Label markup => $right, align => +1),
3631 ], 3825 ],
3632 ; 3826 ;
3633 3827
3634 } else { 3828 } else {
3635 $widget = new CFPlus::UI::Label 3829 $widget = new CFPlus::UI::Label
3636 can_hover => 1, 3830 can_hover => 1,
3637 can_events => 1, 3831 can_events => 1,
3638 markup => $widget, 3832 markup => $widget,
3763 3957
3764############################################################################# 3958#############################################################################
3765 3959
3766package CFPlus::UI::Notebook; 3960package CFPlus::UI::Notebook;
3767 3961
3962use CFPlus::OpenGL;
3963
3768our @ISA = CFPlus::UI::VBox::; 3964our @ISA = CFPlus::UI::VBox::;
3769 3965
3770sub new { 3966sub new {
3771 my $class = shift; 3967 my $class = shift;
3772 3968
3773 my $self = $class->SUPER::new ( 3969 my $self = $class->SUPER::new (
3774 buttonbar => (new CFPlus::UI::Buttonbar), 3970 buttonbar => (new CFPlus::UI::Buttonbar),
3775 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3971 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3972 active_outline => [1, 1, 0],
3776 # filter => # will be put between multiplexer and $self 3973 # filter => # will be put between multiplexer and $self
3777 @_, 3974 @_,
3778 ); 3975 );
3779 3976
3780 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3977 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3781 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3978 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3782 3979
3980 {
3981 Scalar::Util::weaken (my $wself = $self);
3982
3983 $self->{multiplexer}->connect (c_add => sub {
3984 my ($mplex, $widgets) = @_;
3985
3986 for my $child (@$widgets) {
3987 Scalar::Util::weaken $child;
3988 $child->{c_tab_} ||= do {
3989 my $tab =
3990 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base")
3991 ? $child->{c_tab}
3992 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
3993
3994 $tab->connect (activate => sub {
3995 $wself->set_current_page ($child);
3996 });
3997
3998 $tab
3999 };
4000
4001 $self->{buttonbar}->add ($child->{c_tab_});
4002 }
4003 });
4004
4005 $self->{multiplexer}->connect (c_remove => sub {
4006 my ($mplex, $widgets) = @_;
4007
4008 for my $child (@$widgets) {
4009 $wself->{buttonbar}->remove ($child->{c_tab_});
4010 }
4011 });
4012 }
4013
3783 $self 4014 $self
3784} 4015}
3785 4016
3786sub add { 4017sub add {
4018 my ($self, @widgets) = @_;
4019
4020 $self->{multiplexer}->add (@widgets)
4021}
4022
4023sub remove {
4024 my ($self, @widgets) = @_;
4025
4026 $self->{multiplexer}->remove (@widgets)
4027}
4028
4029sub pages {
4030 my ($self) = @_;
4031 $self->{multiplexer}->children
4032}
4033
4034sub add_tab {
3787 my ($self, $title, $widget, $tooltip) = @_; 4035 my ($self, $title, $widget, $tooltip) = @_;
3788 4036
3789 CFPlus::weaken $self; 4037 $title = [$title, $tooltip] unless ref $title;
4038 $widget->{c_tab} = $title;
3790 4039
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); 4040 $self->add ($widget);
3798} 4041}
3799 4042
3800sub get_current_page { 4043sub get_current_page {
3801 my ($self) = @_; 4044 my ($self) = @_;
3802 4045
3806sub set_current_page { 4049sub set_current_page {
3807 my ($self, $page) = @_; 4050 my ($self, $page) = @_;
3808 4051
3809 $self->{multiplexer}->set_current_page ($page); 4052 $self->{multiplexer}->set_current_page ($page);
3810 $self->emit (page_changed => $self->{multiplexer}{current}); 4053 $self->emit (page_changed => $self->{multiplexer}{current});
4054}
4055
4056sub _draw {
4057 my ($self) = @_;
4058
4059 $self->SUPER::_draw ();
4060
4061 if (my $cur = $self->{multiplexer}{current}) {
4062 if ($cur = $cur->{c_tab_}) {
4063 glTranslate $cur->{x}, $cur->{y};
4064 glLineWidth 3;
4065 glColor @{$self->{active_outline}};
4066 glRect_lineloop 1, 1, $cur->{w} - 2, $cur->{h} - 2;
4067 glLineWidth 1;
4068 }
4069 }
3811} 4070}
3812 4071
3813############################################################################# 4072#############################################################################
3814 4073
3815package CFPlus::UI::Selector; 4074package CFPlus::UI::Selector;
4085} 4344}
4086 4345
4087sub update { 4346sub update {
4088 my ($self) = @_; 4347 my ($self) = @_;
4089 4348
4090 $::WANT_REFRESH++; 4349 $::WANT_REFRESH->start;
4091} 4350}
4092 4351
4093sub add { 4352sub add {
4094 my ($self, @children) = @_; 4353 my ($self, @children) = @_;
4095 4354
4132 while ($self->{refresh_hook}) { 4391 while ($self->{refresh_hook}) {
4133 $_->() 4392 $_->()
4134 for values %{delete $self->{refresh_hook}}; 4393 for values %{delete $self->{refresh_hook}};
4135 } 4394 }
4136 4395
4137 if ($self->{realloc}) { 4396 while ($self->{realloc}) {
4138 my %queue; 4397 my %queue;
4139 my @queue; 4398 my @queue;
4140 my $widget; 4399 my $widget;
4141 4400
4142 outer: 4401 outer:
4189 } 4448 }
4190 } 4449 }
4191 4450
4192 delete $self->{realloc}{$widget+0}; 4451 delete $self->{realloc}{$widget+0};
4193 } 4452 }
4194 }
4195 4453
4196 while (my $size_alloc = delete $self->{size_alloc}) { 4454 while (my $size_alloc = delete $self->{size_alloc}) {
4197 my @queue = sort { $b->{visible} <=> $a->{visible} } 4455 my @queue = sort { $a->{visible} <=> $b->{visible} }
4198 values %$size_alloc; 4456 values %$size_alloc;
4199 4457
4200 while () { 4458 while () {
4201 my $widget = pop @queue || last; 4459 my $widget = pop @queue || last;
4202 4460
4203 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4461 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4204 4462
4205 $w = 0 if $w < 0;
4206 $h = 0 if $h < 0;
4207
4208 $w = max $widget->{min_w}, $w; 4463 $w = max $widget->{min_w}, $w;
4209 $h = max $widget->{min_h}, $h; 4464 $h = max $widget->{min_h}, $h;
4210 4465
4211# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4466# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4212# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4467# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4213 4468
4214 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4469 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4215 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4470 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4216 4471
4217 $w = int $w + 0.5; 4472 $w = int $w + 0.5;
4218 $h = int $h + 0.5; 4473 $h = int $h + 0.5;
4219 4474
4220 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4475 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
4221 $widget->{old_w} = $widget->{w}; 4476 $widget->{old_w} = $widget->{w};
4222 $widget->{old_h} = $widget->{h}; 4477 $widget->{old_h} = $widget->{h};
4223 4478
4224 $widget->{w} = $w; 4479 $widget->{w} = $w;
4225 $widget->{h} = $h; 4480 $widget->{h} = $h;
4226 4481
4227 $widget->emit (size_allocate => $w, $h); 4482 $widget->emit (size_allocate => $w, $h);
4483 }
4228 } 4484 }
4229 } 4485 }
4230 } 4486 }
4231 4487
4232 while ($self->{post_alloc_hook}) { 4488 while ($self->{post_alloc_hook}) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines