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.412 by root, Sun Jul 22 20:08:17 2007 UTC vs.
Revision 1.449 by root, Wed Dec 26 15:07:08 2007 UTC

2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7use Event;
8 7
9use CFPlus; 8use CFPlus;
10use CFPlus::Pod; 9use CFPlus::Pod;
11use CFPlus::Texture; 10use CFPlus::Texture;
12 11
17our $TOOLTIP; 16our $TOOLTIP;
18our $BUTTON_STATE; 17our $BUTTON_STATE;
19 18
20our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
21 20
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 21our $TOOLTIP_WATCHER = EV::timer_ns 0, 1/60, sub {
22 $_[0]->stop;
23
23 if (!$GRAB) { 24 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 26 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 27 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 28 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
30 $TOOLTIP->{owner} = $widget; 31 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; 32 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
32 33
33 return if $ENV{CFPLUS_DEBUG} & 8; 34 return if $ENV{CFPLUS_DEBUG} & 8;
34 35
35 my $tip = $widget->{tooltip};
36
37 $tip = $tip->($widget) if CODE:: eq ref $tip;
38
39 $TOOLTIP->set_tooltip_from ($widget); 36 $TOOLTIP->set_tooltip_from ($widget);
40 $TOOLTIP->show; 37 $TOOLTIP->show;
41 } 38 }
42 39
43 return; 40 return;
46 } 43 }
47 44
48 $TOOLTIP->hide; 45 $TOOLTIP->hide;
49 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 46 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
50 delete $TOOLTIP->{owner}; 47 delete $TOOLTIP->{owner};
51}); 48};
52 49
53sub get_layout { 50sub get_layout {
54 my $layout; 51 my $layout;
55 52
56 for (grep { $_->{name} } values %WIDGET) { 53 for (grep { $_->{name} } values %WIDGET) {
91 my $hover = $HOVER; $HOVER = $widget; 88 my $hover = $HOVER; $HOVER = $widget;
92 89
93 $hover->update if $hover && $hover->{can_hover}; 90 $hover->update if $hover && $hover->{can_hover};
94 $HOVER->update if $HOVER && $HOVER->{can_hover}; 91 $HOVER->update if $HOVER && $HOVER->{can_hover};
95 92
96 $TOOLTIP_WATCHER->start; 93 $TOOLTIP_WATCHER->again;
97 } 94 }
98} 95}
99 96
100sub feed_sdl_button_down_event { 97sub feed_sdl_button_down_event {
101 my ($ev) = @_; 98 my ($ev) = @_;
107 my $widget = $ROOT->find_widget ($x, $y); 104 my $widget = $ROOT->find_widget ($x, $y);
108 105
109 $GRAB = $widget; 106 $GRAB = $widget;
110 $GRAB->update if $GRAB; 107 $GRAB->update if $GRAB;
111 108
112 $TOOLTIP_WATCHER->cb->(); 109 $TOOLTIP_WATCHER->invoke;
113 } 110 }
114 111
115 if ($GRAB) { 112 if ($GRAB) {
116 if ($ev->{button} == 4 || $ev->{button} == 5) { 113 if ($ev->{button} == 4 || $ev->{button} == 5) {
117 # mousewheel 114 # mousewheel
142 my $grab = $GRAB; undef $GRAB; 139 my $grab = $GRAB; undef $GRAB;
143 $grab->update if $grab; 140 $grab->update if $grab;
144 $GRAB->update if $GRAB; 141 $GRAB->update if $GRAB;
145 142
146 check_hover $widget; 143 check_hover $widget;
147 $TOOLTIP_WATCHER->cb->(); 144 $TOOLTIP_WATCHER->invoke;
148 } 145 }
149} 146}
150 147
151sub feed_sdl_motion_event { 148sub feed_sdl_motion_event {
152 my ($ev) = @_; 149 my ($ev) = @_;
273 $self->emit ("destroy"); 270 $self->emit ("destroy");
274 %$self = (); 271 %$self = ();
275} 272}
276 273
277sub TO_JSON { 274sub TO_JSON {
278 { __widget_ref__ => $_[0]{s_id} } 275 { "\fw" => $_[0]{s_id} }
279} 276}
280 277
281sub show { 278sub show {
282 my ($self) = @_; 279 my ($self) = @_;
283 280
312 delete $self->{root}; 309 delete $self->{root};
313 310
314 undef $GRAB if $GRAB == $self; 311 undef $GRAB if $GRAB == $self;
315 undef $HOVER if $HOVER == $self; 312 undef $HOVER if $HOVER == $self;
316 313
317 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 314 $CFPlus::UI::TOOLTIP_WATCHER->invoke
318 if $TOOLTIP->{owner} == $self; 315 if $TOOLTIP->{owner} == $self;
319 316
320 $self->emit ("focus_out"); 317 $self->emit ("focus_out");
321 $self->emit (visibility_change => 0); 318 $self->emit (visibility_change => 0);
322} 319}
362 359
363 $self->{force_w} = $w; 360 $self->{force_w} = $w;
364 $self->{force_h} = $h; 361 $self->{force_h} = $h;
365 362
366 $self->realloc; 363 $self->realloc;
364}
365
366# traverse the widget chain up to find the maximum "physical" size constraints
367sub get_max_wh {
368 my ($self) = @_;
369
370 return $self->{parent}->get_max_wh
371 if $self->{parent};
372
373 ($::WIDTH, $::HEIGHT)
367} 374}
368 375
369sub size_request { 376sub size_request {
370 require Carp; 377 require Carp;
371 Carp::confess "size_request is abstract"; 378 Carp::confess "size_request is abstract";
433 440
434 $self->{tooltip} = $tooltip; 441 $self->{tooltip} = $tooltip;
435 442
436 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 443 if ($CFPlus::UI::TOOLTIP->{owner} == $self) {
437 delete $CFPlus::UI::TOOLTIP->{owner}; 444 delete $CFPlus::UI::TOOLTIP->{owner};
438 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 445 $CFPlus::UI::TOOLTIP_WATCHER->invoke;
439 } 446 }
440} 447}
441 448
442# translate global coordinates to local coordinate system 449# translate global coordinates to local coordinate system
443sub coord2local { 450sub coord2local {
444 my ($self, $x, $y) = @_; 451 my ($self, $x, $y) = @_;
445 452
446 Carp::confess unless $self->{parent};#d# 453 return (undef, undef) unless $self->{parent};
447 454
448 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 455 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
449} 456}
450 457
451# translate local coordinates to global coordinate system 458# translate local coordinates to global coordinate system
452sub coord2global { 459sub coord2global {
453 my ($self, $x, $y) = @_; 460 my ($self, $x, $y) = @_;
454 461
455 Carp::confess unless $self->{parent};#d# 462 return (undef, undef) unless $self->{parent};
456 463
457 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 464 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
458} 465}
459 466
460sub invoke_focus_in { 467sub invoke_focus_in {
607sub DESTROY { 614sub DESTROY {
608 my ($self) = @_; 615 my ($self) = @_;
609 616
610 return if CFPlus::in_destruct; 617 return if CFPlus::in_destruct;
611 618
619 local $@;
612 eval { $self->destroy }; 620 eval { $self->destroy };
613 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 621 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
614 622
615 delete $WIDGET{$self+0}; 623 delete $WIDGET{$self+0};
616} 624}
624use strict; 632use strict;
625use CFPlus::OpenGL; 633use CFPlus::OpenGL;
626 634
627sub new { 635sub new {
628 my $class = shift; 636 my $class = shift;
629
630 # range [value, low, high, page]
631 637
632 $class->SUPER::new ( 638 $class->SUPER::new (
633 #bg => [0, 0, 0, 0.2], 639 #bg => [0, 0, 0, 0.2],
634 #active_bg => [1, 1, 1, 0.5], 640 #active_bg => [1, 1, 1, 0.5],
635 @_ 641 @_
647 my ($w, $h) = @$self{qw(w h)}; 653 my ($w, $h) = @$self{qw(w h)};
648 654
649 glEnable GL_BLEND; 655 glEnable GL_BLEND;
650 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 656 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
651 glColor_premultiply @$color; 657 glColor_premultiply @$color;
652
653 glBegin GL_QUADS;
654 glVertex 0 , 0;
655 glVertex 0 , $h;
656 glVertex $w, $h; 658 glRect 0, 0, $w, $h;
657 glVertex $w, 0;
658 glEnd;
659
660 glDisable GL_BLEND; 659 glDisable GL_BLEND;
661 } 660 }
662} 661}
663 662
664############################################################################# 663#############################################################################
725 @{$self->{children}}, @widgets 724 @{$self->{children}}, @widgets
726 ]; 725 ];
727 726
728 $self->realloc; 727 $self->realloc;
729 728
729 $self->emit (c_add => \@widgets);
730
730 map $_+0, @widgets 731 map $_+0, @widgets
731} 732}
732 733
733sub children { 734sub children {
734 @{ $_[0]{children} } 735 @{ $_[0]{children} }
735} 736}
736 737
737sub remove { 738sub remove {
738 my ($self, $child) = @_; 739 my ($self, @widgets) = @_;
739 740
741 $self->emit (c_remove => \@widgets);
742
743 for my $child (@widgets) {
740 delete $child->{parent}; 744 delete $child->{parent};
741 $child->hide; 745 $child->hide;
742
743 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 746 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
747 }
744 748
745 $self->realloc; 749 $self->realloc;
746} 750}
747 751
748sub clear { 752sub clear {
776} 780}
777 781
778sub _draw { 782sub _draw {
779 my ($self) = @_; 783 my ($self) = @_;
780 784
781 $_->draw for @{$self->{children}}; 785 $_->draw for $self->visible_children;
782} 786}
783 787
784############################################################################# 788#############################################################################
785 789
786package CFPlus::UI::Bin; 790package CFPlus::UI::Bin;
796} 800}
797 801
798sub add { 802sub add {
799 my ($self, $child) = @_; 803 my ($self, $child) = @_;
800 804
801 $self->SUPER::remove ($_) for @{ $self->{children} }; 805 $self->clear;
802 $self->SUPER::add ($child); 806 $self->SUPER::add ($child);
803} 807}
804 808
805sub remove { 809sub remove {
806 my ($self, $widget) = @_; 810 my ($self, $widget) = @_;
824 828
825 1 829 1
826} 830}
827 831
828############################################################################# 832#############################################################################
829
830# back-buffered drawing area 833# back-buffered drawing area
831 834
832package CFPlus::UI::Window; 835package CFPlus::UI::Window;
833 836
834our @ISA = CFPlus::UI::Bin::; 837our @ISA = CFPlus::UI::Bin::;
948 $self->{view_x} = $x; 951 $self->{view_x} = $x;
949 $self->{view_y} = $y; 952 $self->{view_y} = $y;
950 953
951 $self->emit (changed => $x, $y); 954 $self->emit (changed => $x, $y);
952 $self->update; 955 $self->update;
956 }
957}
958
959sub set_center {
960 my ($self, $x, $y) = @_;
961
962 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
963}
964
965sub make_visible {
966 my ($self, $x, $y, $border) = @_;
967
968 if ( $x < $self->{view_x} + $self->{w} * $border
969 || $x > $self->{view_x} + $self->{w} * (1 - $border)
970 || $y < $self->{view_y} + $self->{h} * $border
971 || $y > $self->{view_y} + $self->{h} * (1 - $border)
972 ) {
973 $self->set_center ($x, $y);
953 } 974 }
954} 975}
955 976
956# hmm, this does not work for topleft of $self... but we should not ask for that 977# hmm, this does not work for topleft of $self... but we should not ask for that
957sub coord2local { 978sub coord2local {
1004 my $child = delete $arg{child}; 1025 my $child = delete $arg{child};
1005 1026
1006 my $self; 1027 my $self;
1007 1028
1008 my $hslider = new CFPlus::UI::Slider 1029 my $hslider = new CFPlus::UI::Slider
1009 col => 0, 1030 c_col => 0,
1010 row => 1, 1031 c_row => 1,
1011 vertical => 0, 1032 vertical => 0,
1012 range => [0, 0, 1, 0.01], # HACK fix 1033 range => [0, 0, 1, 0.01], # HACK fix
1013 on_changed => sub { 1034 on_changed => sub {
1014 $self->{hpos} = $_[1]; 1035 $self->{hpos} = $_[1];
1015 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1036 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1016 }, 1037 },
1017 ; 1038 ;
1018 1039
1019 my $vslider = new CFPlus::UI::Slider 1040 my $vslider = new CFPlus::UI::Slider
1020 col => 1, 1041 c_col => 1,
1021 row => 0, 1042 c_row => 0,
1022 vertical => 1, 1043 vertical => 1,
1023 range => [0, 0, 1, 0.01], # HACK fix 1044 range => [0, 0, 1, 0.01], # HACK fix
1024 on_changed => sub { 1045 on_changed => sub {
1025 $self->{vpos} = $_[1]; 1046 $self->{vpos} = $_[1];
1026 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1047 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1037 row_expand => [1, 0], 1058 row_expand => [1, 0],
1038 %arg, 1059 %arg,
1039 ); 1060 );
1040 1061
1041 $self->{vp} = new CFPlus::UI::ViewPort 1062 $self->{vp} = new CFPlus::UI::ViewPort
1042 col => 0, 1063 c_col => 0,
1043 row => 0, 1064 c_row => 0,
1044 expand => 1, 1065 expand => 1,
1045 scroll_x => $self->{scroll_x}, 1066 scroll_x => $self->{scroll_x},
1046 scroll_y => $self->{scroll_y}, 1067 scroll_y => $self->{scroll_y},
1047 on_changed => sub { 1068 on_changed => sub {
1048 my ($vp, $x, $y) = @_; 1069 my ($vp, $x, $y) = @_;
1070 my ($self, $widget) = @_; 1091 my ($self, $widget) = @_;
1071 1092
1072 $self->{vp}->add ($self->{child} = $widget); 1093 $self->{vp}->add ($self->{child} = $widget);
1073} 1094}
1074 1095
1096sub set_offset { shift->{vp}->set_offset (@_) }
1097sub set_center { shift->{vp}->set_center (@_) }
1098sub make_visible { shift->{vp}->make_visible (@_) }
1099
1075sub update_slider { 1100sub update_slider {
1076 my ($self) = @_; 1101 my ($self) = @_;
1077 1102
1078 my $child = ($self->{vp} or return)->child; 1103 my $child = ($self->{vp} or return)->child;
1079 1104
1192 my ($w, $h) = @$self{qw(w h)}; 1217 my ($w, $h) = @$self{qw(w h)};
1193 1218
1194 glEnable GL_BLEND; 1219 glEnable GL_BLEND;
1195 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1220 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1196 glColor_premultiply @{ $self->{bg} }; 1221 glColor_premultiply @{ $self->{bg} };
1197
1198 glBegin GL_QUADS;
1199 glVertex 0 , 0;
1200 glVertex 0 , $h;
1201 glVertex $w, $h; 1222 glRect 0, 0, $w, $h;
1202 glVertex $w, 0;
1203 glEnd;
1204
1205 glDisable GL_BLEND; 1223 glDisable GL_BLEND;
1206 } 1224 }
1207 1225
1208 $self->SUPER::_draw; 1226 $self->SUPER::_draw;
1209} 1227}
1370 1388
1371sub border { 1389sub border {
1372 int $_[0]{border} * $::FONTSIZE 1390 int $_[0]{border} * $::FONTSIZE
1373} 1391}
1374 1392
1393sub get_max_wh {
1394 my ($self) = @_;
1395
1396 return ($self->{w}, $self->{h})
1397 if $self->{visible} && $self->{w};
1398
1399 $self->SUPER::get_max_wh
1400}
1401
1375sub size_request { 1402sub size_request {
1376 my ($self) = @_; 1403 my ($self) = @_;
1377 1404
1378 $self->{title_widget}->size_request 1405 $self->{title_widget}->size_request
1379 if $self->{title_widget}; 1406 if $self->{title_widget};
1571 1598
1572sub add { 1599sub add {
1573 my ($self, @widgets) = @_; 1600 my ($self, @widgets) = @_;
1574 1601
1575 for my $child (@widgets) { 1602 for my $child (@widgets) {
1576 $child->{rowspan} ||= 1; 1603 $child->{c_rowspan} ||= 1;
1577 $child->{colspan} ||= 1; 1604 $child->{c_colspan} ||= 1;
1578 } 1605 }
1579 1606
1580 $self->SUPER::add (@widgets); 1607 $self->SUPER::add (@widgets);
1581} 1608}
1582 1609
1586 my @widgets; 1613 my @widgets;
1587 1614
1588 while (@_) { 1615 while (@_) {
1589 my ($col, $row, $child) = splice @_, 0, 3, (); 1616 my ($col, $row, $child) = splice @_, 0, 3, ();
1590 1617
1591 $child->{row} = $row; 1618 $child->{c_row} = $row;
1592 $child->{col} = $col; 1619 $child->{c_col} = $col;
1593 1620
1594 push @widgets, $child; 1621 push @widgets, $child;
1595 } 1622 }
1596 1623
1597 $self->add (@widgets); 1624 $self->add (@widgets);
1603 my (@w, @h); 1630 my (@w, @h);
1604 1631
1605 my @children = $self->children; 1632 my @children = $self->children;
1606 1633
1607 # first pass, columns 1634 # first pass, columns
1608 for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) { 1635 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1609 my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; 1636 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1610 1637
1611 my $sw = sum @w[$c .. $c + $cs - 1]; 1638 my $sw = sum @w[$c .. $c + $cs - 1];
1612 1639
1613 if ($w > $sw) { 1640 if ($w > $sw) {
1614 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1641 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1615 } 1642 }
1616 } 1643 }
1617 1644
1618 # second pass, rows 1645 # second pass, rows
1619 for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) { 1646 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1620 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; 1647 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1621 1648
1622 my $sh = sum @h[$r .. $r + $rs - 1]; 1649 my $sh = sum @h[$r .. $r + $rs - 1];
1623 1650
1624 if ($h > $sh) { 1651 if ($h > $sh) {
1625 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1652 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1667 1694
1668 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1695 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1669 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1696 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1670 1697
1671 for my $widget ($self->children) { 1698 for my $widget ($self->children) {
1672 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)}; 1699 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1673 1700
1674 $widget->configure ( 1701 $widget->configure (
1675 $x[$c], $y[$r], 1702 $x[$c], $y[$r],
1676 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], 1703 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1677 ); 1704 );
1753our @ISA = CFPlus::UI::Container::; 1780our @ISA = CFPlus::UI::Container::;
1754 1781
1755sub size_request { 1782sub size_request {
1756 my ($self) = @_; 1783 my ($self) = @_;
1757 1784
1785 my @children = $self->visible_children;
1786
1758 $self->{vertical} 1787 $self->{vertical}
1759 ? ( 1788 ? (
1760 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1789 (List::Util::max map $_->{req_w}, @children),
1761 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1790 (List::Util::sum map $_->{req_h}, @children),
1762 ) 1791 )
1763 : ( 1792 : (
1764 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1793 (List::Util::sum map $_->{req_w}, @children),
1765 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1794 (List::Util::max map $_->{req_h}, @children),
1766 ) 1795 )
1767} 1796}
1768 1797
1769sub invoke_size_allocate { 1798sub invoke_size_allocate {
1770 my ($self, $w, $h) = @_; 1799 my ($self, $w, $h) = @_;
1894 1923
1895 delete $self->{ox}; 1924 delete $self->{ox};
1896 $self->SUPER::realloc; 1925 $self->SUPER::realloc;
1897} 1926}
1898 1927
1928sub clear {
1929 my ($self) = @_;
1930
1931 $self->set_text ("");
1932}
1933
1899sub set_text { 1934sub set_text {
1900 my ($self, $text) = @_; 1935 my ($self, $text) = @_;
1901 1936
1902 return if $self->{text} eq "T$text"; 1937 return if $self->{text} eq "T$text";
1903 $self->{text} = "T$text"; 1938 $self->{text} = "T$text";
1926 1961
1927sub size_request { 1962sub size_request {
1928 my ($self) = @_; 1963 my ($self) = @_;
1929 1964
1930 $self->{size_req} ||= do { 1965 $self->{size_req} ||= do {
1966 my ($max_w, $max_h) = $self->get_max_wh;
1967
1931 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1968 $self->{layout}->set_font ($self->{font}) if $self->{font};
1932 $self->{layout}->set_width ($self->{max_w} || -1); 1969 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1933 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1970 $self->{layout}->set_ellipsise ($self->{ellipsise});
1934 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1971 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1935 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1972 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1936 1973
1937 my ($w, $h) = $self->{layout}->size; 1974 my ($w, $h) = $self->{layout}->size;
2009 : ($self->{w} - $size->[0]) * 0.5); 2046 : ($self->{w} - $size->[0]) * 0.5);
2010 2047
2011 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2048 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2012 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2049 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2013 : ($self->{h} - $size->[1]) * 0.5); 2050 : ($self->{h} - $size->[1]) * 0.5);
2051
2052 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2014 }; 2053 };
2015 2054
2016# unless ($self->{list}) { 2055# unless ($self->{list}) {
2017# $self->{list} = CFPlus::OpenGL::glGenList; 2056# $self->{list} = CFPlus::OpenGL::glGenList;
2018# CFPlus::OpenGL::glNewList $self->{list}; 2057# CFPlus::OpenGL::glNewList $self->{list};
2020# CFPlus::OpenGL::glEndList; 2059# CFPlus::OpenGL::glEndList;
2021# } 2060# }
2022# 2061#
2023# CFPlus::OpenGL::glCallList $self->{list}; 2062# CFPlus::OpenGL::glCallList $self->{list};
2024 2063
2025 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2064 $self->{layout}->draw;
2026} 2065}
2027 2066
2028#sub destroy { 2067#sub destroy {
2029# my ($self) = @_; 2068# my ($self) = @_;
2030# 2069#
2045 my $class = shift; 2084 my $class = shift;
2046 2085
2047 $class->SUPER::new ( 2086 $class->SUPER::new (
2048 fg => [1, 1, 1], 2087 fg => [1, 1, 1],
2049 bg => [0, 0, 0, 0.2], 2088 bg => [0, 0, 0, 0.2],
2089 outline => [0.6, 0.3, 0.1],
2050 active_bg => [1, 1, 1, 0.5], 2090 active_bg => [0, 0, 1, .2],
2051 active_fg => [0, 0, 0], 2091 active_fg => [1, 1, 1],
2092 active_outline => [1, 1, 0],
2052 can_hover => 1, 2093 can_hover => 1,
2053 can_focus => 1, 2094 can_focus => 1,
2054 valign => 0, 2095 valign => 0,
2055 can_events => 1, 2096 can_events => 1,
2056 ellipsise => 0, 2097 ellipsise => 0,
2196 glColor_premultiply @{$self->{bg}}; 2237 glColor_premultiply @{$self->{bg}};
2197 } 2238 }
2198 2239
2199 glEnable GL_BLEND; 2240 glEnable GL_BLEND;
2200 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2241 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2201 glBegin GL_QUADS;
2202 glVertex 0 , 0;
2203 glVertex 0 , $self->{h};
2204 glVertex $self->{w}, $self->{h}; 2242 glRect 0, 0, $self->{w}, $self->{h};
2205 glVertex $self->{w}, 0;
2206 glEnd;
2207 glDisable GL_BLEND; 2243 glDisable GL_BLEND;
2208 2244
2209 $self->SUPER::_draw; 2245 $self->SUPER::_draw;
2210 2246
2211 #TODO: force update every cursor change :( 2247 #TODO: force update every cursor change :(
2213 2249
2214 unless (exists $self->{cur_h}) { 2250 unless (exists $self->{cur_h}) {
2215 my $text = substr $self->{text}, 0, $self->{cursor}; 2251 my $text = substr $self->{text}, 0, $self->{cursor};
2216 utf8::encode $text; 2252 utf8::encode $text;
2217 2253
2218 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2254 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2219 } 2255 }
2220 2256
2257 glColor_premultiply @{$self->{active_fg}};
2221 glBegin GL_LINES; 2258 glBegin GL_LINES;
2222 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2259 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2223 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2260 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2261 glEnd;
2262
2263 glLineWidth 3;
2264 glColor @{$self->{active_outline}};
2265 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2266 glLineWidth 1;
2267
2268 } else {
2269 glColor @{$self->{outline}};
2270 glBegin GL_LINE_STRIP;
2271 glVertex .5, $self->{h} * .5;
2272 glVertex .5, $self->{h} - 2.5;
2273 glVertex $self->{w} - .5, $self->{h} - 2.5;
2274 glVertex $self->{w} - .5, $self->{h} * .5;
2224 glEnd; 2275 glEnd;
2225 } 2276 }
2226} 2277}
2227 2278
2228############################################################################# 2279#############################################################################
2315 1 2366 1
2316} 2367}
2317 2368
2318############################################################################# 2369#############################################################################
2319 2370
2371package CFPlus::UI::ButtonBin;
2372
2373our @ISA = CFPlus::UI::Bin::;
2374
2375use CFPlus::OpenGL;
2376
2377my @tex =
2378 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2379 qw(b1_button_inactive.png b1_button_active.png);
2380
2381sub new {
2382 my $class = shift;
2383
2384 $class->SUPER::new (
2385 can_hover => 1,
2386 align => 0,
2387 valign => 0,
2388 can_events => 1,
2389 @_
2390 )
2391}
2392
2393sub invoke_button_up {
2394 my ($self, $ev, $x, $y) = @_;
2395
2396 $self->emit ("activate")
2397 if $x >= 0 && $x < $self->{w}
2398 && $y >= 0 && $y < $self->{h};
2399
2400 1
2401}
2402
2403sub _draw {
2404 my ($self) = @_;
2405
2406 glEnable GL_TEXTURE_2D;
2407 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2408 glColor 0, 0, 0, 1;
2409
2410 my $tex = $tex[$GRAB == $self];
2411 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2412
2413 glDisable GL_TEXTURE_2D;
2414
2415 $self->SUPER::_draw;
2416}
2417
2418#############################################################################
2419
2320package CFPlus::UI::Button; 2420package CFPlus::UI::Button;
2321 2421
2322our @ISA = CFPlus::UI::Label::; 2422our @ISA = CFPlus::UI::Label::;
2323 2423
2324use CFPlus::OpenGL; 2424use CFPlus::OpenGL;
2428sub _draw { 2528sub _draw {
2429 my ($self) = @_; 2529 my ($self) = @_;
2430 2530
2431 $self->SUPER::_draw; 2531 $self->SUPER::_draw;
2432 2532
2433 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2533 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2434 2534
2435 my ($w, $h) = @$self{qw(w h)}; 2535 my ($w, $h) = @$self{qw(w h)};
2436 2536
2437 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2537 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2438 2538
2458sub new { 2558sub new {
2459 my $class = shift; 2559 my $class = shift;
2460 2560
2461 my $self = $class->SUPER::new ( 2561 my $self = $class->SUPER::new (
2462 can_events => 0, 2562 can_events => 0,
2563 scale => 1,
2463 @_, 2564 @_,
2464 ); 2565 );
2465 2566
2466 $self->{path} || $self->{tex} 2567 $self->{path} || $self->{tex}
2467 or Carp::croak "'path' or 'tex' attributes required"; 2568 or Carp::croak "'path' or 'tex' attributes required";
2492} 2593}
2493 2594
2494sub size_request { 2595sub size_request {
2495 my ($self) = @_; 2596 my ($self) = @_;
2496 2597
2497 ($self->{tex}{w}, $self->{tex}{h}) 2598 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2498} 2599}
2499 2600
2500sub _draw { 2601sub _draw {
2501 my ($self) = @_; 2602 my ($self) = @_;
2502 2603
2512 } 2613 }
2513 2614
2514 glEnable GL_TEXTURE_2D; 2615 glEnable GL_TEXTURE_2D;
2515 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2616 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2516 2617
2517 $tex->draw_quad (0, 0, $w, $h); 2618 $tex->draw_quad_alpha (0, 0, $w, $h);
2518 2619
2519 glDisable GL_TEXTURE_2D; 2620 glDisable GL_TEXTURE_2D;
2520} 2621}
2521 2622
2522############################################################################# 2623#############################################################################
2541 align => 0, 2642 align => 0,
2542 valign => 0, 2643 valign => 0,
2543 can_events => 1, 2644 can_events => 1,
2544 @_ 2645 @_
2545 ); 2646 );
2647}
2648
2649sub invoke_button_down {
2650 my ($self, $ev, $x, $y) = @_;
2651
2652 1
2546} 2653}
2547 2654
2548sub invoke_button_up { 2655sub invoke_button_up {
2549 my ($self, $ev, $x, $y) = @_; 2656 my ($self, $ev, $x, $y) = @_;
2550 2657
2690 glDisable GL_TEXTURE_2D; 2797 glDisable GL_TEXTURE_2D;
2691} 2798}
2692 2799
2693############################################################################# 2800#############################################################################
2694 2801
2802package CFPlus::UI::Progress;
2803
2804our @ISA = CFPlus::UI::Label::;
2805
2806use CFPlus::OpenGL;
2807
2808sub new {
2809 my ($class, %arg) = @_;
2810
2811 my $self = $class->SUPER::new (
2812 fg => [1, 1, 1],
2813 bg => [0, 0, 1, 0.2],
2814 bar => [0.7, 0.5, 0.1, 0.8],
2815 outline => [0.4, 0.3, 0],
2816 fontsize => 0.9,
2817 valign => 0,
2818 align => 0,
2819 can_events => 1,
2820 ellipsise => 1,
2821 label => "%d%%",
2822 %arg,
2823 );
2824
2825 $self->set_value ($arg{value} || -1);
2826
2827 $self
2828}
2829
2830sub set_label {
2831 my ($self, $label) = @_;
2832
2833 return if $self->{label} eq $label;
2834 $self->{label} = $label;
2835
2836 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value});
2837}
2838
2839sub set_value {
2840 my ($self, $value) = @_;
2841
2842 if ($self->{value} ne $value) {
2843 $self->{value} = $value;
2844
2845 if ($value < 0) {
2846 $self->set_text ("-");
2847 } else {
2848 $self->set_text (sprintf $self->{label}, $value * 100);
2849 }
2850
2851 $self->update;
2852 }
2853}
2854
2855sub _draw {
2856 my ($self) = @_;
2857
2858 glEnable GL_BLEND;
2859 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2860
2861 if ($self->{value} >= 0) {
2862 my $s = int 2 + ($self->{w} - 4) * $self->{value};
2863
2864 glColor_premultiply @{$self->{bar}};
2865 glRect 2, 2, $s, $self->{h} - 2;
2866 glColor_premultiply @{$self->{bg}};
2867 glRect $s, 2, $self->{w} - 2, $self->{h} - 2;
2868 }
2869
2870 glColor_premultiply @{$self->{outline}};
2871 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2872
2873 glDisable GL_BLEND;
2874
2875 {
2876 local $self->{bg}; # do not draw background
2877 $self->SUPER::_draw;
2878 }
2879}
2880
2881#############################################################################
2882
2883package CFPlus::UI::ExperienceProgress;
2884
2885our @ISA = CFPlus::UI::Progress::;
2886
2887sub new {
2888 my ($class, %arg) = @_;
2889
2890 my $self = $class->SUPER::new (
2891 tooltip => sub {
2892 my ($self) = @_;
2893
2894 sprintf "level %d\n%s points\n%s next level\n%s to go",
2895 $self->{lvl},
2896 ::formsep ($self->{exp}),
2897 ::formsep ($self->{nxt}),
2898 ::formsep ($self->{nxt} - $self->{exp}),
2899 },
2900 %arg
2901 );
2902
2903 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2904 if $::CONN;
2905
2906 $self
2907}
2908
2909sub DESTROY {
2910 my ($self) = @_;
2911
2912 delete $::CONN->{on_exp_update}{$self+0}
2913 if $::CONN;
2914
2915 $self->SUPER::DESTROY;
2916}
2917
2918sub set_value {
2919 my ($self, $lvl, $exp) = @_;
2920
2921 $self->{lvl} = $lvl;
2922 $self->{exp} = $exp;
2923
2924 my $v = -1;
2925
2926 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2927 my $l0 = $table->[$lvl - 1];
2928 my $l1 = $table->[$lvl];
2929
2930 $self->{nxt} = $l1;
2931
2932 $v = ($exp - $l0) / ($l1 - $l0);
2933 }
2934
2935 $self->SUPER::set_value ($v);
2936}
2937
2938#############################################################################
2939
2695package CFPlus::UI::Gauge; 2940package CFPlus::UI::Gauge;
2696 2941
2697our @ISA = CFPlus::UI::VBox::; 2942our @ISA = CFPlus::UI::VBox::;
2698 2943
2699sub new { 2944sub new {
2997} 3242}
2998 3243
2999sub size_request { 3244sub size_request {
3000 my ($self) = @_; 3245 my ($self) = @_;
3001 3246
3002 my ($empty, $slider) = @{ $self->{children} }; 3247 my ($empty, $slider) = $self->visible_children;
3003 3248
3004 local $self->{children} = [$empty, $slider]; 3249 local $self->{children} = [$empty, $slider];
3005 $self->SUPER::size_request 3250 $self->SUPER::size_request
3006} 3251}
3007 3252
3196 3441
3197 if ($y0 < $y + $h && $y < $y1) { 3442 if ($y0 < $y + $h && $y < $y1) {
3198 my $layout = $self->get_layout ($para); 3443 my $layout = $self->get_layout ($para);
3199 3444
3200 $layout->render ($para->{indent}, $y - $y0); 3445 $layout->render ($para->{indent}, $y - $y0);
3446 $layout->draw;
3201 3447
3202 if (my @w = @{ $para->{widget} }) { 3448 if (my @w = @{ $para->{widget} }) {
3203 my @s = $layout->get_shapes; 3449 my @s = $layout->get_shapes;
3204 3450
3205 for (@w) { 3451 for (@w) {
3327} 3573}
3328 3574
3329sub set_tooltip_from { 3575sub set_tooltip_from {
3330 my ($self, $widget) = @_; 3576 my ($self, $widget) = @_;
3331 3577
3332 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3333 if $widget->{tooltip} =~ /^#(.*)$/;
3334
3335 my $tooltip = $widget->{tooltip}; 3578 my $tip = $widget->{tooltip};
3579 $tip = $tip->($widget) if "CODE" eq ref $tip;
3580
3581 $tip = CFPlus::Pod::section_label tooltip => $1
3582 if $tip =~ /^#(.*)$/;
3336 3583
3337 if ($ENV{CFPLUS_DEBUG} & 2) { 3584 if ($ENV{CFPLUS_DEBUG} & 2) {
3338 $tooltip .= "\n\n" . (ref $widget) . "\n" 3585 $tip .= "\n\n" . (ref $widget) . "\n"
3339 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3586 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3340 . "req $widget->{req_w} $widget->{req_h}\n" 3587 . "req $widget->{req_w} $widget->{req_h}\n"
3341 . "visible $widget->{visible}"; 3588 . "visible $widget->{visible}";
3342 } 3589 }
3343 3590
3344 $tooltip =~ s/^\n+//; 3591 $tip =~ s/^\n+//;
3345 $tooltip =~ s/\n+$//; 3592 $tip =~ s/\n+$//;
3346 3593
3347 $self->add (new CFPlus::UI::Label 3594 $self->add (new CFPlus::UI::Label
3348 markup => $tooltip, 3595 markup => $tip,
3349 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3596 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3350 fontsize => 0.8, 3597 fontsize => 0.8,
3351 style => 1, # FLAG_INVERSE 3598 style => 1, # FLAG_INVERSE
3352 ellipsise => 0, 3599 ellipsise => 0,
3353 font => ($widget->{tooltip_font} || $::FONT_PROP), 3600 font => ($widget->{tooltip_font} || $::FONT_PROP),
3391} 3638}
3392 3639
3393sub _draw { 3640sub _draw {
3394 my ($self) = @_; 3641 my ($self) = @_;
3395 3642
3396 glTranslate 0.375, 0.375;
3397
3398 my ($w, $h) = @$self{qw(w h)}; 3643 my ($w, $h) = @$self{qw(w h)};
3399 3644
3400 glColor 1, 0.8, 0.4; 3645 glColor 1, 0.8, 0.4;
3401 glBegin GL_QUADS; 3646 glRect 0, 0, $w, $h;
3402 glVertex 0 , 0;
3403 glVertex 0 , $h;
3404 glVertex $w, $h;
3405 glVertex $w, 0;
3406 glEnd;
3407 3647
3408 glColor 0, 0, 0; 3648 glColor 0, 0, 0;
3409 glBegin GL_LINE_LOOP; 3649 glRect_lineloop .5, .5, $w + .5, $h + .5;
3410 glVertex 0 , 0;
3411 glVertex 0 , $h;
3412 glVertex $w, $h;
3413 glVertex $w, 0;
3414 glEnd;
3415 3650
3416 glTranslate 2 - 0.375, 2 - 0.375; 3651 glTranslate 2, 2;
3417 3652
3418 $self->SUPER::_draw; 3653 $self->SUPER::_draw;
3419} 3654}
3420 3655
3421############################################################################# 3656#############################################################################
3438 ); 3673 );
3439 3674
3440 if ($self->{anim} && $self->{animspeed}) { 3675 if ($self->{anim} && $self->{animspeed}) {
3441 CFPlus::weaken (my $widget = $self); 3676 CFPlus::weaken (my $widget = $self);
3442 3677
3443 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3678 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3444 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3679 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3445 $self->{timer} = Event->timer (
3446 parked => 1,
3447 cb => sub {
3448 return unless $::CONN && $widget; 3680 return unless $::CONN;
3449 3681
3682 my $w = $widget
3683 or return;
3684
3450 ++$widget->{frame}; 3685 ++$w->{frame};
3451 $widget->update_face; 3686 $w->update_face;
3687
3688 # somehow, $widget can go away
3452 $widget->update; 3689 $w->update;
3453
3454 $widget->update_timer; 3690 $w->update_timer;
3455 },
3456 ); 3691 };
3457 3692
3458 $self->update_face; 3693 $self->update_face;
3459 $self->update_timer; 3694 $self->update_timer;
3460 } 3695 }
3461 3696
3466 my ($self) = @_; 3701 my ($self) = @_;
3467 3702
3468 return unless $self->{timer}; 3703 return unless $self->{timer};
3469 3704
3470 if ($self->{visible}) { 3705 if ($self->{visible}) {
3471 $self->{timer}->at (
3472 $self->{anim_start}
3473 + $self->{animspeed}
3474 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3475 );
3476 $self->{timer}->start; 3706 $self->{timer}->start;
3477 } else { 3707 } else {
3478 $self->{timer}->stop; 3708 $self->{timer}->stop;
3479 } 3709 }
3480} 3710}
3481 3711
3482sub update_face { 3712sub update_face {
3483 my ($self) = @_; 3713 my ($self) = @_;
3484 3714
3485 return unless $::CONN; 3715 if ($::CONN) {
3486
3487 if (my $anim = $::CONN->{anim}[$self->{anim}]) { 3716 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3488 if ($anim && @$anim) { 3717 if ($anim && @$anim) {
3489 delete $self->{wait_face};
3490 $self->{face} = $anim->[ $self->{frame} % @$anim ]; 3718 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3719 delete $self->{face_change_cb};
3720
3491 $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ]; 3721 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3722 unless ($tex->{name} || $tex->{loading}) {
3723 $tex->upload (sub { $self->reconfigure });
3724 }
3725 }
3726 }
3492 } 3727 }
3493 } 3728 }
3494} 3729}
3495 3730
3496sub size_request { 3731sub size_request {
3497 my ($self) = @_; 3732 my ($self) = @_;
3498 3733
3499 if ($::CONN) { 3734 if ($::CONN) {
3500 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3735 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3501 if (my $tex = $::CONN->{texture}[$faceid]) { 3736 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3502 $self->{tex} = $tex; 3737 if ($tex->{name}) {
3503 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3738 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3504 } else { 3739 } elsif (!$tex->{loading}) {
3505 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3740 $tex->upload (sub { $self->reconfigure });
3506 $self->realloc;
3507 }); 3741 }
3508 } 3742 }
3743
3744 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3509 } 3745 }
3510 } 3746 }
3511 3747
3512 ($self->{size_w} || 8, $self->{size_h} || 8) 3748 ($self->{size_w} || 8, $self->{size_h} || 8)
3513} 3749}
3593 children => [ 3829 children => [
3594 (new CFPlus::UI::Label markup => $left, expand => 1), 3830 (new CFPlus::UI::Label markup => $left, expand => 1),
3595 (new CFPlus::UI::Label markup => $right, align => +1), 3831 (new CFPlus::UI::Label markup => $right, align => +1),
3596 ], 3832 ],
3597 ; 3833 ;
3598 3834
3599 } else { 3835 } else {
3600 $widget = new CFPlus::UI::Label 3836 $widget = new CFPlus::UI::Label
3601 can_hover => 1, 3837 can_hover => 1,
3602 can_events => 1, 3838 can_events => 1,
3603 markup => $widget, 3839 markup => $widget,
3728 3964
3729############################################################################# 3965#############################################################################
3730 3966
3731package CFPlus::UI::Notebook; 3967package CFPlus::UI::Notebook;
3732 3968
3969use CFPlus::OpenGL;
3970
3733our @ISA = CFPlus::UI::VBox::; 3971our @ISA = CFPlus::UI::VBox::;
3734 3972
3735sub new { 3973sub new {
3736 my $class = shift; 3974 my $class = shift;
3737 3975
3738 my $self = $class->SUPER::new ( 3976 my $self = $class->SUPER::new (
3739 buttonbar => (new CFPlus::UI::Buttonbar), 3977 buttonbar => (new CFPlus::UI::Buttonbar),
3740 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3978 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3979 active_outline => [.7, .7, 0.2],
3741 # filter => # will be put between multiplexer and $self 3980 # filter => # will be put between multiplexer and $self
3742 @_, 3981 @_,
3743 ); 3982 );
3744 3983
3745 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3984 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3746 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3985 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3747 3986
3987 {
3988 Scalar::Util::weaken (my $wself = $self);
3989
3990 $self->{multiplexer}->connect (c_add => sub {
3991 my ($mplex, $widgets) = @_;
3992
3993 for my $child (@$widgets) {
3994 Scalar::Util::weaken $child;
3995 $child->{c_tab_} ||= do {
3996 my $tab =
3997 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base")
3998 ? $child->{c_tab}
3999 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4000
4001 $tab->connect (activate => sub {
4002 $wself->set_current_page ($child);
4003 });
4004
4005 $tab
4006 };
4007
4008 $self->{buttonbar}->add ($child->{c_tab_});
4009 }
4010 });
4011
4012 $self->{multiplexer}->connect (c_remove => sub {
4013 my ($mplex, $widgets) = @_;
4014
4015 for my $child (@$widgets) {
4016 $wself->{buttonbar}->remove ($child->{c_tab_});
4017 }
4018 });
4019 }
4020
3748 $self 4021 $self
3749} 4022}
3750 4023
3751sub add { 4024sub add {
4025 my ($self, @widgets) = @_;
4026
4027 $self->{multiplexer}->add (@widgets)
4028}
4029
4030sub remove {
4031 my ($self, @widgets) = @_;
4032
4033 $self->{multiplexer}->remove (@widgets)
4034}
4035
4036sub pages {
4037 my ($self) = @_;
4038 $self->{multiplexer}->children
4039}
4040
4041sub add_tab {
3752 my ($self, $title, $widget, $tooltip) = @_; 4042 my ($self, $title, $widget, $tooltip) = @_;
3753 4043
3754 CFPlus::weaken $self; 4044 $title = [$title, $tooltip] unless ref $title;
4045 $widget->{c_tab} = $title;
3755 4046
3756 unless (ref $title) {
3757 $title = new CFPlus::UI::Button
3758 markup => $title,
3759 tooltip => $tooltip,
3760 ;
3761 }
3762
3763 $title->connect (activate => sub { $self->set_current_page ($widget) });
3764 $self->{buttonbar}->add ($title);
3765
3766 $self->{multiplexer}->add ($widget); 4047 $self->add ($widget);
3767} 4048}
3768 4049
3769sub get_current_page { 4050sub get_current_page {
3770 my ($self) = @_; 4051 my ($self) = @_;
3771 4052
3775sub set_current_page { 4056sub set_current_page {
3776 my ($self, $page) = @_; 4057 my ($self, $page) = @_;
3777 4058
3778 $self->{multiplexer}->set_current_page ($page); 4059 $self->{multiplexer}->set_current_page ($page);
3779 $self->emit (page_changed => $self->{multiplexer}{current}); 4060 $self->emit (page_changed => $self->{multiplexer}{current});
4061}
4062
4063sub _draw {
4064 my ($self) = @_;
4065
4066 $self->SUPER::_draw ();
4067
4068 if (my $cur = $self->{multiplexer}{current}) {
4069 if ($cur = $cur->{c_tab_}) {
4070 glTranslate $self->{buttonbar}{x} + $cur->{x},
4071 $self->{buttonbar}{y} + $cur->{y};
4072 glLineWidth 3;
4073 #glEnable GL_BLEND;
4074 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4075 glColor @{$self->{active_outline}};
4076 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4077 glLineWidth 1;
4078 #glDisable GL_BLEND;
4079 }
4080 }
3780} 4081}
3781 4082
3782############################################################################# 4083#############################################################################
3783 4084
3784package CFPlus::UI::Selector; 4085package CFPlus::UI::Selector;
3816} 4117}
3817 4118
3818sub _set_value { 4119sub _set_value {
3819 my ($self, $value) = @_; 4120 my ($self, $value) = @_;
3820 4121
3821 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4122 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4123 $item ||= $self->{options}[0]
3822 or return; 4124 or return;
3823 4125
3824 $self->{value} = $item->[0]; 4126 $self->{value} = $item->[0];
3825 $self->set_markup ("$item->[1] ⇓"); 4127 $self->set_markup ("$item->[1] ⇓");
3826 $self->set_tooltip ($item->[2]); 4128# $self->set_tooltip ($item->[2]);
3827} 4129}
3828 4130
3829sub set_value { 4131sub set_value {
3830 my ($self, $value) = @_; 4132 my ($self, $value) = @_;
3831 4133
3832 return unless $self->{value} ne $value; 4134 return unless $self->{value} ne $value;
3833 4135
3834 $self->_set_value ($value); 4136 $self->_set_value ($value);
3835 $self->emit (changed => $value); 4137 $self->emit (changed => $value);
4138}
4139
4140sub set_options {
4141 my ($self, $options) = @_;
4142
4143 $self->{options} = $options;
4144 $self->_set_value ($self->{value});
3836} 4145}
3837 4146
3838############################################################################# 4147#############################################################################
3839 4148
3840package CFPlus::UI::Statusbox; 4149package CFPlus::UI::Statusbox;
3849 @_, 4158 @_,
3850 ); 4159 );
3851 4160
3852 CFPlus::weaken (my $this = $self); 4161 CFPlus::weaken (my $this = $self);
3853 4162
3854 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4163 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3855 4164
3856 $self 4165 $self
3857} 4166}
3858 4167
3859sub reorder { 4168sub reorder {
3867 4176
3868 while (my ($k, $v) = each %{ $self->{item} }) { 4177 while (my ($k, $v) = each %{ $self->{item} }) {
3869 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4178 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3870 } 4179 }
3871 4180
4181 $self->{timer}->set (1, 1);
4182
3872 my @widgets; 4183 my @widgets;
3873 4184
3874 my @items = sort { 4185 my @items = sort {
3875 $a->{pri} <=> $b->{pri} 4186 $a->{pri} <=> $b->{pri}
3876 or $b->{id} <=> $a->{id} 4187 or $b->{id} <=> $a->{id}
3877 } values %{ $self->{item} }; 4188 } values %{ $self->{item} };
3878
3879 $self->{timer}->interval (1);
3880 4189
3881 my $count = 10 + 1; 4190 my $count = 10 + 1;
3882 for my $item (@items) { 4191 for my $item (@items) {
3883 last unless --$count; 4192 last unless --$count;
3884 4193
3908 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4217 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3909 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4218 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3910 $label->update; 4219 $label->update;
3911 $label->set_max_size (undef, $label->{req_h} * $diff) 4220 $label->set_max_size (undef, $label->{req_h} * $diff)
3912 if $diff < 1; 4221 if $diff < 1;
3913 $self->{timer}->interval (1/30); 4222 $self->{timer}->set (1/30, 1/30);
3914 } else { 4223 } else {
3915 $label->{fg}[3] = $item->{fg}[3] || 1; 4224 $label->{fg}[3] = $item->{fg}[3] || 1;
3916 } 4225 }
3917 4226
3918 push @widgets, $label; 4227 push @widgets, $label;
4054} 4363}
4055 4364
4056sub update { 4365sub update {
4057 my ($self) = @_; 4366 my ($self) = @_;
4058 4367
4059 $::WANT_REFRESH++; 4368 $::WANT_REFRESH->start;
4060} 4369}
4061 4370
4062sub add { 4371sub add {
4063 my ($self, @children) = @_; 4372 my ($self, @children) = @_;
4064 4373

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines