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.399 by root, Sat Jul 21 16:07:53 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#############################################################################
770 @{$self->{children}}, @widgets 717 @{$self->{children}}, @widgets
771 ]; 718 ];
772 719
773 $self->realloc; 720 $self->realloc;
774 721
722 $self->emit (c_add => \@widgets);
723
775 map $_+0, @widgets 724 map $_+0, @widgets
776} 725}
777 726
778sub children { 727sub children {
779 @{ $_[0]{children} } 728 @{ $_[0]{children} }
780} 729}
781 730
782sub remove { 731sub remove {
783 my ($self, $child) = @_; 732 my ($self, @widgets) = @_;
784 733
734 $self->emit (c_remove => \@widgets);
735
736 for my $child (@widgets) {
785 delete $child->{parent}; 737 delete $child->{parent};
786 $child->hide; 738 $child->hide;
787
788 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 739 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
740 }
789 741
790 $self->realloc; 742 $self->realloc;
791} 743}
792 744
793sub clear { 745sub clear {
821} 773}
822 774
823sub _draw { 775sub _draw {
824 my ($self) = @_; 776 my ($self) = @_;
825 777
826 $_->draw for @{$self->{children}}; 778 $_->draw for $self->visible_children;
827} 779}
828 780
829############################################################################# 781#############################################################################
830 782
831package CFPlus::UI::Bin; 783package CFPlus::UI::Bin;
841} 793}
842 794
843sub add { 795sub add {
844 my ($self, $child) = @_; 796 my ($self, $child) = @_;
845 797
846 $self->SUPER::remove ($_) for @{ $self->{children} }; 798 $self->clear;
847 $self->SUPER::add ($child); 799 $self->SUPER::add ($child);
848} 800}
849 801
850sub remove { 802sub remove {
851 my ($self, $widget) = @_; 803 my ($self, $widget) = @_;
993 $self->{view_x} = $x; 945 $self->{view_x} = $x;
994 $self->{view_y} = $y; 946 $self->{view_y} = $y;
995 947
996 $self->emit (changed => $x, $y); 948 $self->emit (changed => $x, $y);
997 $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);
998 } 968 }
999} 969}
1000 970
1001# 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
1002sub coord2local { 972sub coord2local {
1049 my $child = delete $arg{child}; 1019 my $child = delete $arg{child};
1050 1020
1051 my $self; 1021 my $self;
1052 1022
1053 my $hslider = new CFPlus::UI::Slider 1023 my $hslider = new CFPlus::UI::Slider
1054 col => 0, 1024 c_col => 0,
1055 row => 1, 1025 c_row => 1,
1056 vertical => 0, 1026 vertical => 0,
1057 range => [0, 0, 1, 0.01], # HACK fix 1027 range => [0, 0, 1, 0.01], # HACK fix
1058 on_changed => sub { 1028 on_changed => sub {
1059 $self->{hpos} = $_[1]; 1029 $self->{hpos} = $_[1];
1060 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1030 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1061 }, 1031 },
1062 ; 1032 ;
1063 1033
1064 my $vslider = new CFPlus::UI::Slider 1034 my $vslider = new CFPlus::UI::Slider
1065 col => 1, 1035 c_col => 1,
1066 row => 0, 1036 c_row => 0,
1067 vertical => 1, 1037 vertical => 1,
1068 range => [0, 0, 1, 0.01], # HACK fix 1038 range => [0, 0, 1, 0.01], # HACK fix
1069 on_changed => sub { 1039 on_changed => sub {
1070 $self->{vpos} = $_[1]; 1040 $self->{vpos} = $_[1];
1071 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1041 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1082 row_expand => [1, 0], 1052 row_expand => [1, 0],
1083 %arg, 1053 %arg,
1084 ); 1054 );
1085 1055
1086 $self->{vp} = new CFPlus::UI::ViewPort 1056 $self->{vp} = new CFPlus::UI::ViewPort
1087 col => 0, 1057 c_col => 0,
1088 row => 0, 1058 c_row => 0,
1089 expand => 1, 1059 expand => 1,
1090 scroll_x => $self->{scroll_x}, 1060 scroll_x => $self->{scroll_x},
1091 scroll_y => $self->{scroll_y}, 1061 scroll_y => $self->{scroll_y},
1092 on_changed => sub { 1062 on_changed => sub {
1093 my ($vp, $x, $y) = @_; 1063 my ($vp, $x, $y) = @_;
1095 $vp->{parent}{hslider}->set_value ($x); 1065 $vp->{parent}{hslider}->set_value ($x);
1096 $vp->{parent}{vslider}->set_value ($y); 1066 $vp->{parent}{vslider}->set_value ($y);
1097 1067
1098 0 1068 0
1099 }, 1069 },
1070 on_size_allocate => sub {
1071 my ($vp, $w, $h) = @_;
1072 $vp->{parent}->update_slider;
1073 0
1074 },
1100 ; 1075 ;
1101 1076
1102 $self->SUPER::add ($self->{vp}); 1077 $self->SUPER::add ($self->{vp});
1103 1078
1104 $self->add ($child) if $child; 1079 $self->add ($child) if $child;
1110 my ($self, $widget) = @_; 1085 my ($self, $widget) = @_;
1111 1086
1112 $self->{vp}->add ($self->{child} = $widget); 1087 $self->{vp}->add ($self->{child} = $widget);
1113} 1088}
1114 1089
1090sub set_offset { shift->{vp}->set_offset (@_) }
1091sub set_center { shift->{vp}->set_center (@_) }
1092sub make_visible { shift->{vp}->make_visible (@_) }
1093
1115sub update_slider { 1094sub update_slider {
1116 my ($self) = @_; 1095 my ($self) = @_;
1117 1096
1118 my $child = ($self->{vp} or return)->child; 1097 my $child = ($self->{vp} or return)->child;
1119 1098
1099 if ($self->{scroll_x}) {
1120 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1100 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1121 $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]);
1122 1102
1123 my $visible = $w1 > $w2; 1103 my $visible = $w1 > $w2;
1124 if ($visible != $self->{hslider_visible}) { 1104 if ($visible != $self->{hslider_visible}) {
1125 $self->{hslider_visible} = $visible; 1105 $self->{hslider_visible} = $visible;
1126 $visible ? $self->SUPER::add ($self->{hslider}) 1106 $visible ? $self->SUPER::add ($self->{hslider})
1127 : $self->SUPER::remove ($self->{hslider}); 1107 : $self->SUPER::remove ($self->{hslider});
1108 }
1128 } 1109 }
1129 1110
1111 if ($self->{scroll_y}) {
1130 my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); 1112 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1131 $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]);
1132 1114
1133 my $visible = $h1 > $h2; 1115 my $visible = $h1 > $h2;
1134 if ($visible != $self->{vslider_visible}) { 1116 if ($visible != $self->{vslider_visible}) {
1135 $self->{vslider_visible} = $visible; 1117 $self->{vslider_visible} = $visible;
1136 $visible ? $self->SUPER::add ($self->{vslider}) 1118 $visible ? $self->SUPER::add ($self->{vslider})
1137 : $self->SUPER::remove ($self->{vslider}); 1119 : $self->SUPER::remove ($self->{vslider});
1120 }
1138 } 1121 }
1139} 1122}
1140 1123
1141sub start_dragging { 1124sub start_dragging {
1142 my ($self, $ev) = @_; 1125 my ($self, $ev) = @_;
1228 my ($w, $h) = @$self{qw(w h)}; 1211 my ($w, $h) = @$self{qw(w h)};
1229 1212
1230 glEnable GL_BLEND; 1213 glEnable GL_BLEND;
1231 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1214 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1232 glColor_premultiply @{ $self->{bg} }; 1215 glColor_premultiply @{ $self->{bg} };
1233
1234 glBegin GL_QUADS;
1235 glVertex 0 , 0;
1236 glVertex 0 , $h;
1237 glVertex $w, $h; 1216 glRect 0, 0, $w, $h;
1238 glVertex $w, 0;
1239 glEnd;
1240
1241 glDisable GL_BLEND; 1217 glDisable GL_BLEND;
1242 } 1218 }
1243 1219
1244 $self->SUPER::_draw; 1220 $self->SUPER::_draw;
1245} 1221}
1607 1583
1608sub add { 1584sub add {
1609 my ($self, @widgets) = @_; 1585 my ($self, @widgets) = @_;
1610 1586
1611 for my $child (@widgets) { 1587 for my $child (@widgets) {
1612 $child->{rowspan} ||= 1; 1588 $child->{c_rowspan} ||= 1;
1613 $child->{colspan} ||= 1; 1589 $child->{c_colspan} ||= 1;
1614 } 1590 }
1615 1591
1616 $self->SUPER::add (@widgets); 1592 $self->SUPER::add (@widgets);
1617} 1593}
1618 1594
1622 my @widgets; 1598 my @widgets;
1623 1599
1624 while (@_) { 1600 while (@_) {
1625 my ($col, $row, $child) = splice @_, 0, 3, (); 1601 my ($col, $row, $child) = splice @_, 0, 3, ();
1626 1602
1627 $child->{row} = $row; 1603 $child->{c_row} = $row;
1628 $child->{col} = $col; 1604 $child->{c_col} = $col;
1629 1605
1630 push @widgets, $child; 1606 push @widgets, $child;
1631 } 1607 }
1632 1608
1633 $self->add (@widgets); 1609 $self->add (@widgets);
1639 my (@w, @h); 1615 my (@w, @h);
1640 1616
1641 my @children = $self->children; 1617 my @children = $self->children;
1642 1618
1643 # first pass, columns 1619 # first pass, columns
1644 for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) { 1620 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1645 my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; 1621 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1646 1622
1647 my $sw = sum @w[$c .. $c + $cs - 1]; 1623 my $sw = sum @w[$c .. $c + $cs - 1];
1648 1624
1649 if ($w > $sw) { 1625 if ($w > $sw) {
1650 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1626 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1651 } 1627 }
1652 } 1628 }
1653 1629
1654 # second pass, rows 1630 # second pass, rows
1655 for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) { 1631 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1656 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; 1632 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1657 1633
1658 my $sh = sum @h[$r .. $r + $rs - 1]; 1634 my $sh = sum @h[$r .. $r + $rs - 1];
1659 1635
1660 if ($h > $sh) { 1636 if ($h > $sh) {
1661 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1637 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1703 1679
1704 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1680 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1705 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1681 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1706 1682
1707 for my $widget ($self->children) { 1683 for my $widget ($self->children) {
1708 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)};
1709 1685
1710 $widget->configure ( 1686 $widget->configure (
1711 $x[$c], $y[$r], 1687 $x[$c], $y[$r],
1712 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], 1688 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1713 ); 1689 );
1722 1698
1723use List::Util qw(min max); 1699use List::Util qw(min max);
1724 1700
1725our @ISA = CFPlus::UI::Container::; 1701our @ISA = CFPlus::UI::Container::;
1726 1702
1727sub add {
1728 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1729
1730 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1731 $self->SUPER::add ($child);
1732}
1733
1734sub _scale($$$) { 1703sub _scale($$$) {
1735 my ($mode, $val, $max) = @_; 1704 my ($rel, $val, $max) = @_;
1736 1705
1737 $mode eq "abs" ? $val 1706 $rel ? $val * $max : $val
1738 : $mode eq "rel" ? $val * $max
1739 : 0
1740} 1707}
1741 1708
1742sub size_request { 1709sub size_request {
1743 my ($self) = @_; 1710 my ($self) = @_;
1744 1711
1745 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1712 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1746 1713
1747 # determine overall size by querying abs widgets 1714 # determine overall size by querying abs widgets
1748 for my $child ($self->visible_children) { 1715 for my $child ($self->visible_children) {
1749 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};
1750 1719
1751 if ($pos eq "abs") {
1752 $w = _scale $size, $w, $child->{req_w};
1753 $h = _scale $size, $h, $child->{req_h};
1754
1755 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1720 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1756 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1721 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1757 } 1722 }
1758 } 1723 }
1759 1724
1760 my $W = $x2 - $x1; 1725 my $W = $x2 - $x1;
1761 my $H = $y2 - $y1; 1726 my $H = $y2 - $y1;
1762 1727
1763 # now layout remaining widgets 1728 # now layout remaining widgets
1764 for my $child ($self->visible_children) { 1729 for my $child ($self->visible_children) {
1765 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;
1766 1733
1767 if ($pos ne "abs") {
1768 $x = _scale $pos, $x, $W;
1769 $y = _scale $pos, $x, $H;
1770 $w = _scale $size, $w, $child->{req_w};
1771 $h = _scale $size, $h, $child->{req_h};
1772
1773 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1734 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1774 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1735 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1775 } 1736 }
1776 } 1737 }
1777 1738
1778 my $W = $x2 - $x1; 1739 my $W = $x2 - $x1;
1779 my $H = $y2 - $y1; 1740 my $H = $y2 - $y1;
1783 1744
1784sub invoke_size_allocate { 1745sub invoke_size_allocate {
1785 my ($self, $W, $H) = @_; 1746 my ($self, $W, $H) = @_;
1786 1747
1787 for my $child ($self->visible_children) { 1748 for my $child ($self->visible_children) {
1788 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;
1789 1751
1790 $x = _scale $pos, $x, $W; 1752 $x += $child->{c_halign} * $child->{req_w};
1791 $y = _scale $pos, $x, $H; 1753 $y += $child->{c_valign} * $child->{req_h};
1792 $w = _scale $size, $w, $child->{req_w};
1793 $h = _scale $size, $h, $child->{req_h};
1794 1754
1795 $child->configure ($x, $y, $w, $h); 1755 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1796 } 1756 }
1797 1757
1798 1 1758 1
1799} 1759}
1800 1760
1804 1764
1805our @ISA = CFPlus::UI::Container::; 1765our @ISA = CFPlus::UI::Container::;
1806 1766
1807sub size_request { 1767sub size_request {
1808 my ($self) = @_; 1768 my ($self) = @_;
1769
1770 my @children = $self->visible_children;
1809 1771
1810 $self->{vertical} 1772 $self->{vertical}
1811 ? ( 1773 ? (
1812 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1774 (List::Util::max map $_->{req_w}, @children),
1813 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1775 (List::Util::sum map $_->{req_h}, @children),
1814 ) 1776 )
1815 : ( 1777 : (
1816 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1778 (List::Util::sum map $_->{req_w}, @children),
1817 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1779 (List::Util::max map $_->{req_h}, @children),
1818 ) 1780 )
1819} 1781}
1820 1782
1821sub invoke_size_allocate { 1783sub invoke_size_allocate {
1822 my ($self, $w, $h) = @_; 1784 my ($self, $w, $h) = @_;
1946 1908
1947 delete $self->{ox}; 1909 delete $self->{ox};
1948 $self->SUPER::realloc; 1910 $self->SUPER::realloc;
1949} 1911}
1950 1912
1913sub clear {
1914 my ($self) = @_;
1915
1916 $self->set_text ("");
1917}
1918
1951sub set_text { 1919sub set_text {
1952 my ($self, $text) = @_; 1920 my ($self, $text) = @_;
1953 1921
1954 return if $self->{text} eq "T$text"; 1922 return if $self->{text} eq "T$text";
1955 $self->{text} = "T$text"; 1923 $self->{text} = "T$text";
2061 : ($self->{w} - $size->[0]) * 0.5); 2029 : ($self->{w} - $size->[0]) * 0.5);
2062 2030
2063 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2031 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2064 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2032 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2065 : ($self->{h} - $size->[1]) * 0.5); 2033 : ($self->{h} - $size->[1]) * 0.5);
2034
2035 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2066 }; 2036 };
2067 2037
2068 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2038# unless ($self->{list}) {
2069 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2039# $self->{list} = CFPlus::OpenGL::glGenList;
2070 2040# CFPlus::OpenGL::glNewList $self->{list};
2071 $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;
2072} 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#}
2073 2057
2074############################################################################# 2058#############################################################################
2075 2059
2076package CFPlus::UI::EntryBase; 2060package CFPlus::UI::EntryBase;
2077 2061
2083 my $class = shift; 2067 my $class = shift;
2084 2068
2085 $class->SUPER::new ( 2069 $class->SUPER::new (
2086 fg => [1, 1, 1], 2070 fg => [1, 1, 1],
2087 bg => [0, 0, 0, 0.2], 2071 bg => [0, 0, 0, 0.2],
2072 outline => [0.6, 0.3, 0.1],
2088 active_bg => [1, 1, 1, 0.5], 2073 active_bg => [0, 0, 1, .2],
2089 active_fg => [0, 0, 0], 2074 active_fg => [1, 1, 1],
2075 active_outline => [1, 1, 0],
2090 can_hover => 1, 2076 can_hover => 1,
2091 can_focus => 1, 2077 can_focus => 1,
2092 valign => 0, 2078 valign => 0,
2093 can_events => 1, 2079 can_events => 1,
2094 ellipsise => 0, 2080 ellipsise => 0,
2234 glColor_premultiply @{$self->{bg}}; 2220 glColor_premultiply @{$self->{bg}};
2235 } 2221 }
2236 2222
2237 glEnable GL_BLEND; 2223 glEnable GL_BLEND;
2238 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2224 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2239 glBegin GL_QUADS;
2240 glVertex 0 , 0;
2241 glVertex 0 , $self->{h};
2242 glVertex $self->{w}, $self->{h}; 2225 glRect 0, 0, $self->{w}, $self->{h};
2243 glVertex $self->{w}, 0;
2244 glEnd;
2245 glDisable GL_BLEND; 2226 glDisable GL_BLEND;
2246 2227
2247 $self->SUPER::_draw; 2228 $self->SUPER::_draw;
2248 2229
2249 #TODO: force update every cursor change :( 2230 #TODO: force update every cursor change :(
2251 2232
2252 unless (exists $self->{cur_h}) { 2233 unless (exists $self->{cur_h}) {
2253 my $text = substr $self->{text}, 0, $self->{cursor}; 2234 my $text = substr $self->{text}, 0, $self->{cursor};
2254 utf8::encode $text; 2235 utf8::encode $text;
2255 2236
2256 @$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);
2257 } 2238 }
2258 2239
2240 glColor_premultiply @{$self->{active_fg}};
2259 glBegin GL_LINES; 2241 glBegin GL_LINES;
2260 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};
2261 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;
2262 glEnd; 2259 glEnd;
2263 } 2260 }
2264} 2261}
2265 2262
2266############################################################################# 2263#############################################################################
2353 1 2350 1
2354} 2351}
2355 2352
2356############################################################################# 2353#############################################################################
2357 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
2358package CFPlus::UI::Button; 2404package CFPlus::UI::Button;
2359 2405
2360our @ISA = CFPlus::UI::Label::; 2406our @ISA = CFPlus::UI::Label::;
2361 2407
2362use CFPlus::OpenGL; 2408use CFPlus::OpenGL;
2496sub new { 2542sub new {
2497 my $class = shift; 2543 my $class = shift;
2498 2544
2499 my $self = $class->SUPER::new ( 2545 my $self = $class->SUPER::new (
2500 can_events => 0, 2546 can_events => 0,
2547 scale => 1,
2501 @_, 2548 @_,
2502 ); 2549 );
2503 2550
2504 $self->{path} || $self->{tex} 2551 $self->{path} || $self->{tex}
2505 or Carp::croak "'path' or 'tex' attributes required"; 2552 or Carp::croak "'path' or 'tex' attributes required";
2530} 2577}
2531 2578
2532sub size_request { 2579sub size_request {
2533 my ($self) = @_; 2580 my ($self) = @_;
2534 2581
2535 ($self->{tex}{w}, $self->{tex}{h}) 2582 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2536} 2583}
2537 2584
2538sub _draw { 2585sub _draw {
2539 my ($self) = @_; 2586 my ($self) = @_;
2540 2587
2550 } 2597 }
2551 2598
2552 glEnable GL_TEXTURE_2D; 2599 glEnable GL_TEXTURE_2D;
2553 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2600 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2554 2601
2555 $tex->draw_quad (0, 0, $w, $h); 2602 $tex->draw_quad_alpha (0, 0, $w, $h);
2556 2603
2557 glDisable GL_TEXTURE_2D; 2604 glDisable GL_TEXTURE_2D;
2558} 2605}
2559 2606
2560############################################################################# 2607#############################################################################
2579 align => 0, 2626 align => 0,
2580 valign => 0, 2627 valign => 0,
2581 can_events => 1, 2628 can_events => 1,
2582 @_ 2629 @_
2583 ); 2630 );
2631}
2632
2633sub invoke_button_down {
2634 my ($self, $ev, $x, $y) = @_;
2635
2636 1
2584} 2637}
2585 2638
2586sub invoke_button_up { 2639sub invoke_button_up {
2587 my ($self, $ev, $x, $y) = @_; 2640 my ($self, $ev, $x, $y) = @_;
2588 2641
2728 glDisable GL_TEXTURE_2D; 2781 glDisable GL_TEXTURE_2D;
2729} 2782}
2730 2783
2731############################################################################# 2784#############################################################################
2732 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
2733package CFPlus::UI::Gauge; 2923package CFPlus::UI::Gauge;
2734 2924
2735our @ISA = CFPlus::UI::VBox::; 2925our @ISA = CFPlus::UI::VBox::;
2736 2926
2737sub new { 2927sub new {
2862 3052
2863 $self->SUPER::invoke_button_down ($ev, $x, $y); 3053 $self->SUPER::invoke_button_down ($ev, $x, $y);
2864 3054
2865 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3055 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2866 3056
2867 $self->invoke_mouse_motion ($ev, $x, $y) 3057 $self->invoke_mouse_motion ($ev, $x, $y);
3058
3059 1
2868} 3060}
2869 3061
2870sub invoke_mouse_motion { 3062sub invoke_mouse_motion {
2871 my ($self, $ev, $x, $y) = @_; 3063 my ($self, $ev, $x, $y) = @_;
2872 3064
2892 3084
2893 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3085 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2894 3086
2895 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3087 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2896 3088
2897 ! ! $delta 3089 1
2898} 3090}
2899 3091
2900sub update { 3092sub update {
2901 my ($self) = @_; 3093 my ($self) = @_;
2902 3094
3033} 3225}
3034 3226
3035sub size_request { 3227sub size_request {
3036 my ($self) = @_; 3228 my ($self) = @_;
3037 3229
3038 my ($empty, $slider) = @{ $self->{children} }; 3230 my ($empty, $slider) = $self->visible_children;
3039 3231
3040 local $self->{children} = [$empty, $slider]; 3232 local $self->{children} = [$empty, $slider];
3041 $self->SUPER::size_request 3233 $self->SUPER::size_request
3042} 3234}
3043 3235
3232 3424
3233 if ($y0 < $y + $h && $y < $y1) { 3425 if ($y0 < $y + $h && $y < $y1) {
3234 my $layout = $self->get_layout ($para); 3426 my $layout = $self->get_layout ($para);
3235 3427
3236 $layout->render ($para->{indent}, $y - $y0); 3428 $layout->render ($para->{indent}, $y - $y0);
3429 $layout->draw;
3237 3430
3238 if (my @w = @{ $para->{widget} }) { 3431 if (my @w = @{ $para->{widget} }) {
3239 my @s = $layout->get_shapes; 3432 my @s = $layout->get_shapes;
3240 3433
3241 for (@w) { 3434 for (@w) {
3432 glTranslate 0.375, 0.375; 3625 glTranslate 0.375, 0.375;
3433 3626
3434 my ($w, $h) = @$self{qw(w h)}; 3627 my ($w, $h) = @$self{qw(w h)};
3435 3628
3436 glColor 1, 0.8, 0.4; 3629 glColor 1, 0.8, 0.4;
3437 glBegin GL_QUADS; 3630 glRect 0, 0, $w, $h;
3438 glVertex 0 , 0;
3439 glVertex 0 , $h;
3440 glVertex $w, $h;
3441 glVertex $w, 0;
3442 glEnd;
3443 3631
3444 glColor 0, 0, 0; 3632 glColor 0, 0, 0;
3445 glBegin GL_LINE_LOOP; 3633 glRect_lineloop 0, 0, $w, $h;
3446 glVertex 0 , 0;
3447 glVertex 0 , $h;
3448 glVertex $w, $h;
3449 glVertex $w, 0;
3450 glEnd;
3451 3634
3452 glTranslate 2 - 0.375, 2 - 0.375; 3635 glTranslate 2 - 0.375, 2 - 0.375;
3453 3636
3454 $self->SUPER::_draw; 3637 $self->SUPER::_draw;
3455} 3638}
3475 3658
3476 if ($self->{anim} && $self->{animspeed}) { 3659 if ($self->{anim} && $self->{animspeed}) {
3477 CFPlus::weaken (my $widget = $self); 3660 CFPlus::weaken (my $widget = $self);
3478 3661
3479 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3662 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3480 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed}; 3663 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3481 $self->{timer} = Event->timer ( 3664 $self->{timer} = Event->timer (
3482 parked => 1, 3665 parked => 1,
3483 cb => sub { 3666 cb => sub {
3484 return unless $::CONN && $widget; 3667 return unless $::CONN;
3485 3668
3669 my $w = $widget
3670 or return;
3671
3486 ++$widget->{frame}; 3672 ++$w->{frame};
3487 $widget->update_face; 3673 $w->update_face;
3674
3675 # somehow, $widget can go away
3488 $widget->update; 3676 $w->update;
3489
3490 $widget->update_timer; 3677 $w->update_timer;
3491 }, 3678 },
3492 ); 3679 );
3493 3680
3494 $self->update_face; 3681 $self->update_face;
3495 $self->update_timer; 3682 $self->update_timer;
3516} 3703}
3517 3704
3518sub update_face { 3705sub update_face {
3519 my ($self) = @_; 3706 my ($self) = @_;
3520 3707
3521 return unless $::CONN;
3522
3523 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3524 if ($anim && @$anim) {
3525 delete $self->{wait_face};
3526 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3527 }
3528 }
3529}
3530
3531sub size_request {
3532 my ($self) = @_;
3533
3534 if ($::CONN) { 3708 if ($::CONN) {
3535 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3709 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3536 if (my $tex = $::CONN->{texture}[$faceid]) { 3710 if ($anim && @$anim) {
3537 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3711 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3538 } else { 3712 delete $self->{face_change_cb};
3539 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3713
3540 $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 }
3541 }); 3718 }
3542 } 3719 }
3543 } 3720 }
3544 } 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 }
3545 3740
3546 ($self->{size_w} || 8, $self->{size_h} || 8) 3741 ($self->{size_w} || 8, $self->{size_h} || 8)
3547} 3742}
3548 3743
3549sub update { 3744sub update {
3563} 3758}
3564 3759
3565sub _draw { 3760sub _draw {
3566 my ($self) = @_; 3761 my ($self) = @_;
3567 3762
3568 return unless $::CONN;
3569
3570 $self->SUPER::_draw; 3763 $self->SUPER::_draw;
3571 3764
3572 my $faceid = $::CONN->{faceid}[$self->{face}] 3765 if (my $tex = $self->{tex}) {
3573 or return;
3574
3575 my $tex = $::CONN->{texture}[$faceid];
3576
3577 if ($tex) {
3578 glEnable GL_TEXTURE_2D; 3766 glEnable GL_TEXTURE_2D;
3579 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3767 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3580 glColor 0, 0, 0, 1; 3768 glColor 0, 0, 0, 1;
3581 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3769 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3582 glDisable GL_TEXTURE_2D; 3770 glDisable GL_TEXTURE_2D;
3634 children => [ 3822 children => [
3635 (new CFPlus::UI::Label markup => $left, expand => 1), 3823 (new CFPlus::UI::Label markup => $left, expand => 1),
3636 (new CFPlus::UI::Label markup => $right, align => +1), 3824 (new CFPlus::UI::Label markup => $right, align => +1),
3637 ], 3825 ],
3638 ; 3826 ;
3639 3827
3640 } else { 3828 } else {
3641 $widget = new CFPlus::UI::Label 3829 $widget = new CFPlus::UI::Label
3642 can_hover => 1, 3830 can_hover => 1,
3643 can_events => 1, 3831 can_events => 1,
3644 markup => $widget, 3832 markup => $widget,
3769 3957
3770############################################################################# 3958#############################################################################
3771 3959
3772package CFPlus::UI::Notebook; 3960package CFPlus::UI::Notebook;
3773 3961
3962use CFPlus::OpenGL;
3963
3774our @ISA = CFPlus::UI::VBox::; 3964our @ISA = CFPlus::UI::VBox::;
3775 3965
3776sub new { 3966sub new {
3777 my $class = shift; 3967 my $class = shift;
3778 3968
3779 my $self = $class->SUPER::new ( 3969 my $self = $class->SUPER::new (
3780 buttonbar => (new CFPlus::UI::Buttonbar), 3970 buttonbar => (new CFPlus::UI::Buttonbar),
3781 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3971 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3972 active_outline => [1, 1, 0],
3782 # filter => # will be put between multiplexer and $self 3973 # filter => # will be put between multiplexer and $self
3783 @_, 3974 @_,
3784 ); 3975 );
3785 3976
3786 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3977 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3787 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3978 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3788 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
3789 $self 4014 $self
3790} 4015}
3791 4016
3792sub 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 {
3793 my ($self, $title, $widget, $tooltip) = @_; 4035 my ($self, $title, $widget, $tooltip) = @_;
3794 4036
3795 CFPlus::weaken $self; 4037 $title = [$title, $tooltip] unless ref $title;
4038 $widget->{c_tab} = $title;
3796 4039
3797 $self->{buttonbar}->add (new CFPlus::UI::Button
3798 markup => $title,
3799 tooltip => $tooltip,
3800 on_activate => sub { $self->set_current_page ($widget) },
3801 );
3802
3803 $self->{multiplexer}->add ($widget); 4040 $self->add ($widget);
3804} 4041}
3805 4042
3806sub get_current_page { 4043sub get_current_page {
3807 my ($self) = @_; 4044 my ($self) = @_;
3808 4045
3812sub set_current_page { 4049sub set_current_page {
3813 my ($self, $page) = @_; 4050 my ($self, $page) = @_;
3814 4051
3815 $self->{multiplexer}->set_current_page ($page); 4052 $self->{multiplexer}->set_current_page ($page);
3816 $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 }
3817} 4070}
3818 4071
3819############################################################################# 4072#############################################################################
3820 4073
3821package CFPlus::UI::Selector; 4074package CFPlus::UI::Selector;
4091} 4344}
4092 4345
4093sub update { 4346sub update {
4094 my ($self) = @_; 4347 my ($self) = @_;
4095 4348
4096 $::WANT_REFRESH++; 4349 $::WANT_REFRESH->start;
4097} 4350}
4098 4351
4099sub add { 4352sub add {
4100 my ($self, @children) = @_; 4353 my ($self, @children) = @_;
4101 4354
4138 while ($self->{refresh_hook}) { 4391 while ($self->{refresh_hook}) {
4139 $_->() 4392 $_->()
4140 for values %{delete $self->{refresh_hook}}; 4393 for values %{delete $self->{refresh_hook}};
4141 } 4394 }
4142 4395
4143 if ($self->{realloc}) { 4396 while ($self->{realloc}) {
4144 my %queue; 4397 my %queue;
4145 my @queue; 4398 my @queue;
4146 my $widget; 4399 my $widget;
4147 4400
4148 outer: 4401 outer:
4195 } 4448 }
4196 } 4449 }
4197 4450
4198 delete $self->{realloc}{$widget+0}; 4451 delete $self->{realloc}{$widget+0};
4199 } 4452 }
4200 }
4201 4453
4202 while (my $size_alloc = delete $self->{size_alloc}) { 4454 while (my $size_alloc = delete $self->{size_alloc}) {
4203 my @queue = sort { $b->{visible} <=> $a->{visible} } 4455 my @queue = sort { $a->{visible} <=> $b->{visible} }
4204 values %$size_alloc; 4456 values %$size_alloc;
4205 4457
4206 while () { 4458 while () {
4207 my $widget = pop @queue || last; 4459 my $widget = pop @queue || last;
4208 4460
4209 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4461 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4210 4462
4211 $w = 0 if $w < 0;
4212 $h = 0 if $h < 0;
4213
4214 $w = max $widget->{min_w}, $w; 4463 $w = max $widget->{min_w}, $w;
4215 $h = max $widget->{min_h}, $h; 4464 $h = max $widget->{min_h}, $h;
4216 4465
4217# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4466# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4218# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4467# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4219 4468
4220 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4469 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4221 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4470 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4222 4471
4223 $w = int $w + 0.5; 4472 $w = int $w + 0.5;
4224 $h = int $h + 0.5; 4473 $h = int $h + 0.5;
4225 4474
4226 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}) {
4227 $widget->{old_w} = $widget->{w}; 4476 $widget->{old_w} = $widget->{w};
4228 $widget->{old_h} = $widget->{h}; 4477 $widget->{old_h} = $widget->{h};
4229 4478
4230 $widget->{w} = $w; 4479 $widget->{w} = $w;
4231 $widget->{h} = $h; 4480 $widget->{h} = $h;
4232 4481
4233 $widget->emit (size_allocate => $w, $h); 4482 $widget->emit (size_allocate => $w, $h);
4483 }
4234 } 4484 }
4235 } 4485 }
4236 } 4486 }
4237 4487
4238 while ($self->{post_alloc_hook}) { 4488 while ($self->{post_alloc_hook}) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines