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.413 by root, Mon Jul 23 15:30:45 2007 UTC vs.
Revision 1.444 by root, Sat Dec 8 18:38:13 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->trigger;
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->trigger
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->trigger;
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 {
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#############################################################################
829 828
830 1 829 1
831} 830}
832 831
833############################################################################# 832#############################################################################
834
835# back-buffered drawing area 833# back-buffered drawing area
836 834
837package CFPlus::UI::Window; 835package CFPlus::UI::Window;
838 836
839our @ISA = CFPlus::UI::Bin::; 837our @ISA = CFPlus::UI::Bin::;
953 $self->{view_x} = $x; 951 $self->{view_x} = $x;
954 $self->{view_y} = $y; 952 $self->{view_y} = $y;
955 953
956 $self->emit (changed => $x, $y); 954 $self->emit (changed => $x, $y);
957 $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);
958 } 974 }
959} 975}
960 976
961# 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
962sub coord2local { 978sub coord2local {
1009 my $child = delete $arg{child}; 1025 my $child = delete $arg{child};
1010 1026
1011 my $self; 1027 my $self;
1012 1028
1013 my $hslider = new CFPlus::UI::Slider 1029 my $hslider = new CFPlus::UI::Slider
1014 col => 0, 1030 c_col => 0,
1015 row => 1, 1031 c_row => 1,
1016 vertical => 0, 1032 vertical => 0,
1017 range => [0, 0, 1, 0.01], # HACK fix 1033 range => [0, 0, 1, 0.01], # HACK fix
1018 on_changed => sub { 1034 on_changed => sub {
1019 $self->{hpos} = $_[1]; 1035 $self->{hpos} = $_[1];
1020 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1036 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1021 }, 1037 },
1022 ; 1038 ;
1023 1039
1024 my $vslider = new CFPlus::UI::Slider 1040 my $vslider = new CFPlus::UI::Slider
1025 col => 1, 1041 c_col => 1,
1026 row => 0, 1042 c_row => 0,
1027 vertical => 1, 1043 vertical => 1,
1028 range => [0, 0, 1, 0.01], # HACK fix 1044 range => [0, 0, 1, 0.01], # HACK fix
1029 on_changed => sub { 1045 on_changed => sub {
1030 $self->{vpos} = $_[1]; 1046 $self->{vpos} = $_[1];
1031 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1047 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1042 row_expand => [1, 0], 1058 row_expand => [1, 0],
1043 %arg, 1059 %arg,
1044 ); 1060 );
1045 1061
1046 $self->{vp} = new CFPlus::UI::ViewPort 1062 $self->{vp} = new CFPlus::UI::ViewPort
1047 col => 0, 1063 c_col => 0,
1048 row => 0, 1064 c_row => 0,
1049 expand => 1, 1065 expand => 1,
1050 scroll_x => $self->{scroll_x}, 1066 scroll_x => $self->{scroll_x},
1051 scroll_y => $self->{scroll_y}, 1067 scroll_y => $self->{scroll_y},
1052 on_changed => sub { 1068 on_changed => sub {
1053 my ($vp, $x, $y) = @_; 1069 my ($vp, $x, $y) = @_;
1075 my ($self, $widget) = @_; 1091 my ($self, $widget) = @_;
1076 1092
1077 $self->{vp}->add ($self->{child} = $widget); 1093 $self->{vp}->add ($self->{child} = $widget);
1078} 1094}
1079 1095
1096sub set_offset { shift->{vp}->set_offset (@_) }
1097sub set_center { shift->{vp}->set_center (@_) }
1098sub make_visible { shift->{vp}->make_visible (@_) }
1099
1080sub update_slider { 1100sub update_slider {
1081 my ($self) = @_; 1101 my ($self) = @_;
1082 1102
1083 my $child = ($self->{vp} or return)->child; 1103 my $child = ($self->{vp} or return)->child;
1084 1104
1197 my ($w, $h) = @$self{qw(w h)}; 1217 my ($w, $h) = @$self{qw(w h)};
1198 1218
1199 glEnable GL_BLEND; 1219 glEnable GL_BLEND;
1200 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1220 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1201 glColor_premultiply @{ $self->{bg} }; 1221 glColor_premultiply @{ $self->{bg} };
1202
1203 glBegin GL_QUADS;
1204 glVertex 0 , 0;
1205 glVertex 0 , $h;
1206 glVertex $w, $h; 1222 glRect 0, 0, $w, $h;
1207 glVertex $w, 0;
1208 glEnd;
1209
1210 glDisable GL_BLEND; 1223 glDisable GL_BLEND;
1211 } 1224 }
1212 1225
1213 $self->SUPER::_draw; 1226 $self->SUPER::_draw;
1214} 1227}
1375 1388
1376sub border { 1389sub border {
1377 int $_[0]{border} * $::FONTSIZE 1390 int $_[0]{border} * $::FONTSIZE
1378} 1391}
1379 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
1380sub size_request { 1402sub size_request {
1381 my ($self) = @_; 1403 my ($self) = @_;
1382 1404
1383 $self->{title_widget}->size_request 1405 $self->{title_widget}->size_request
1384 if $self->{title_widget}; 1406 if $self->{title_widget};
1576 1598
1577sub add { 1599sub add {
1578 my ($self, @widgets) = @_; 1600 my ($self, @widgets) = @_;
1579 1601
1580 for my $child (@widgets) { 1602 for my $child (@widgets) {
1581 $child->{rowspan} ||= 1; 1603 $child->{c_rowspan} ||= 1;
1582 $child->{colspan} ||= 1; 1604 $child->{c_colspan} ||= 1;
1583 } 1605 }
1584 1606
1585 $self->SUPER::add (@widgets); 1607 $self->SUPER::add (@widgets);
1586} 1608}
1587 1609
1591 my @widgets; 1613 my @widgets;
1592 1614
1593 while (@_) { 1615 while (@_) {
1594 my ($col, $row, $child) = splice @_, 0, 3, (); 1616 my ($col, $row, $child) = splice @_, 0, 3, ();
1595 1617
1596 $child->{row} = $row; 1618 $child->{c_row} = $row;
1597 $child->{col} = $col; 1619 $child->{c_col} = $col;
1598 1620
1599 push @widgets, $child; 1621 push @widgets, $child;
1600 } 1622 }
1601 1623
1602 $self->add (@widgets); 1624 $self->add (@widgets);
1608 my (@w, @h); 1630 my (@w, @h);
1609 1631
1610 my @children = $self->children; 1632 my @children = $self->children;
1611 1633
1612 # first pass, columns 1634 # first pass, columns
1613 for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) { 1635 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1614 my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; 1636 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1615 1637
1616 my $sw = sum @w[$c .. $c + $cs - 1]; 1638 my $sw = sum @w[$c .. $c + $cs - 1];
1617 1639
1618 if ($w > $sw) { 1640 if ($w > $sw) {
1619 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1641 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1620 } 1642 }
1621 } 1643 }
1622 1644
1623 # second pass, rows 1645 # second pass, rows
1624 for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) { 1646 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1625 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; 1647 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1626 1648
1627 my $sh = sum @h[$r .. $r + $rs - 1]; 1649 my $sh = sum @h[$r .. $r + $rs - 1];
1628 1650
1629 if ($h > $sh) { 1651 if ($h > $sh) {
1630 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1652 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1672 1694
1673 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1695 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1674 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1696 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1675 1697
1676 for my $widget ($self->children) { 1698 for my $widget ($self->children) {
1677 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)};
1678 1700
1679 $widget->configure ( 1701 $widget->configure (
1680 $x[$c], $y[$r], 1702 $x[$c], $y[$r],
1681 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], 1703 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1682 ); 1704 );
1901 1923
1902 delete $self->{ox}; 1924 delete $self->{ox};
1903 $self->SUPER::realloc; 1925 $self->SUPER::realloc;
1904} 1926}
1905 1927
1928sub clear {
1929 my ($self) = @_;
1930
1931 $self->set_text ("");
1932}
1933
1906sub set_text { 1934sub set_text {
1907 my ($self, $text) = @_; 1935 my ($self, $text) = @_;
1908 1936
1909 return if $self->{text} eq "T$text"; 1937 return if $self->{text} eq "T$text";
1910 $self->{text} = "T$text"; 1938 $self->{text} = "T$text";
1933 1961
1934sub size_request { 1962sub size_request {
1935 my ($self) = @_; 1963 my ($self) = @_;
1936 1964
1937 $self->{size_req} ||= do { 1965 $self->{size_req} ||= do {
1966 my ($max_w, $max_h) = $self->get_max_wh;
1967
1938 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1968 $self->{layout}->set_font ($self->{font}) if $self->{font};
1939 $self->{layout}->set_width ($self->{max_w} || -1); 1969 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1940 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1970 $self->{layout}->set_ellipsise ($self->{ellipsise});
1941 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1971 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1942 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1972 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1943 1973
1944 my ($w, $h) = $self->{layout}->size; 1974 my ($w, $h) = $self->{layout}->size;
2016 : ($self->{w} - $size->[0]) * 0.5); 2046 : ($self->{w} - $size->[0]) * 0.5);
2017 2047
2018 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2048 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2019 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2049 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2020 : ($self->{h} - $size->[1]) * 0.5); 2050 : ($self->{h} - $size->[1]) * 0.5);
2051
2052 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2021 }; 2053 };
2022 2054
2023# unless ($self->{list}) { 2055# unless ($self->{list}) {
2024# $self->{list} = CFPlus::OpenGL::glGenList; 2056# $self->{list} = CFPlus::OpenGL::glGenList;
2025# CFPlus::OpenGL::glNewList $self->{list}; 2057# CFPlus::OpenGL::glNewList $self->{list};
2027# CFPlus::OpenGL::glEndList; 2059# CFPlus::OpenGL::glEndList;
2028# } 2060# }
2029# 2061#
2030# CFPlus::OpenGL::glCallList $self->{list}; 2062# CFPlus::OpenGL::glCallList $self->{list};
2031 2063
2032 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2064 $self->{layout}->draw;
2033} 2065}
2034 2066
2035#sub destroy { 2067#sub destroy {
2036# my ($self) = @_; 2068# my ($self) = @_;
2037# 2069#
2052 my $class = shift; 2084 my $class = shift;
2053 2085
2054 $class->SUPER::new ( 2086 $class->SUPER::new (
2055 fg => [1, 1, 1], 2087 fg => [1, 1, 1],
2056 bg => [0, 0, 0, 0.2], 2088 bg => [0, 0, 0, 0.2],
2089 outline => [0.6, 0.3, 0.1],
2057 active_bg => [1, 1, 1, 0.5], 2090 active_bg => [0, 0, 1, .2],
2058 active_fg => [0, 0, 0], 2091 active_fg => [1, 1, 1],
2092 active_outline => [1, 1, 0],
2059 can_hover => 1, 2093 can_hover => 1,
2060 can_focus => 1, 2094 can_focus => 1,
2061 valign => 0, 2095 valign => 0,
2062 can_events => 1, 2096 can_events => 1,
2063 ellipsise => 0, 2097 ellipsise => 0,
2203 glColor_premultiply @{$self->{bg}}; 2237 glColor_premultiply @{$self->{bg}};
2204 } 2238 }
2205 2239
2206 glEnable GL_BLEND; 2240 glEnable GL_BLEND;
2207 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2241 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2208 glBegin GL_QUADS;
2209 glVertex 0 , 0;
2210 glVertex 0 , $self->{h};
2211 glVertex $self->{w}, $self->{h}; 2242 glRect 0, 0, $self->{w}, $self->{h};
2212 glVertex $self->{w}, 0;
2213 glEnd;
2214 glDisable GL_BLEND; 2243 glDisable GL_BLEND;
2215 2244
2216 $self->SUPER::_draw; 2245 $self->SUPER::_draw;
2217 2246
2218 #TODO: force update every cursor change :( 2247 #TODO: force update every cursor change :(
2220 2249
2221 unless (exists $self->{cur_h}) { 2250 unless (exists $self->{cur_h}) {
2222 my $text = substr $self->{text}, 0, $self->{cursor}; 2251 my $text = substr $self->{text}, 0, $self->{cursor};
2223 utf8::encode $text; 2252 utf8::encode $text;
2224 2253
2225 @$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);
2226 } 2255 }
2227 2256
2257 glColor_premultiply @{$self->{active_fg}};
2228 glBegin GL_LINES; 2258 glBegin GL_LINES;
2229 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};
2230 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;
2231 glEnd; 2275 glEnd;
2232 } 2276 }
2233} 2277}
2234 2278
2235############################################################################# 2279#############################################################################
2322 1 2366 1
2323} 2367}
2324 2368
2325############################################################################# 2369#############################################################################
2326 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
2327package CFPlus::UI::Button; 2420package CFPlus::UI::Button;
2328 2421
2329our @ISA = CFPlus::UI::Label::; 2422our @ISA = CFPlus::UI::Label::;
2330 2423
2331use CFPlus::OpenGL; 2424use CFPlus::OpenGL;
2435sub _draw { 2528sub _draw {
2436 my ($self) = @_; 2529 my ($self) = @_;
2437 2530
2438 $self->SUPER::_draw; 2531 $self->SUPER::_draw;
2439 2532
2440 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2533 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2441 2534
2442 my ($w, $h) = @$self{qw(w h)}; 2535 my ($w, $h) = @$self{qw(w h)};
2443 2536
2444 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;
2445 2538
2465sub new { 2558sub new {
2466 my $class = shift; 2559 my $class = shift;
2467 2560
2468 my $self = $class->SUPER::new ( 2561 my $self = $class->SUPER::new (
2469 can_events => 0, 2562 can_events => 0,
2563 scale => 1,
2470 @_, 2564 @_,
2471 ); 2565 );
2472 2566
2473 $self->{path} || $self->{tex} 2567 $self->{path} || $self->{tex}
2474 or Carp::croak "'path' or 'tex' attributes required"; 2568 or Carp::croak "'path' or 'tex' attributes required";
2499} 2593}
2500 2594
2501sub size_request { 2595sub size_request {
2502 my ($self) = @_; 2596 my ($self) = @_;
2503 2597
2504 ($self->{tex}{w}, $self->{tex}{h}) 2598 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2505} 2599}
2506 2600
2507sub _draw { 2601sub _draw {
2508 my ($self) = @_; 2602 my ($self) = @_;
2509 2603
2519 } 2613 }
2520 2614
2521 glEnable GL_TEXTURE_2D; 2615 glEnable GL_TEXTURE_2D;
2522 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2616 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2523 2617
2524 $tex->draw_quad (0, 0, $w, $h); 2618 $tex->draw_quad_alpha (0, 0, $w, $h);
2525 2619
2526 glDisable GL_TEXTURE_2D; 2620 glDisable GL_TEXTURE_2D;
2527} 2621}
2528 2622
2529############################################################################# 2623#############################################################################
2548 align => 0, 2642 align => 0,
2549 valign => 0, 2643 valign => 0,
2550 can_events => 1, 2644 can_events => 1,
2551 @_ 2645 @_
2552 ); 2646 );
2647}
2648
2649sub invoke_button_down {
2650 my ($self, $ev, $x, $y) = @_;
2651
2652 1
2553} 2653}
2554 2654
2555sub invoke_button_up { 2655sub invoke_button_up {
2556 my ($self, $ev, $x, $y) = @_; 2656 my ($self, $ev, $x, $y) = @_;
2557 2657
2697 glDisable GL_TEXTURE_2D; 2797 glDisable GL_TEXTURE_2D;
2698} 2798}
2699 2799
2700############################################################################# 2800#############################################################################
2701 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
2702package CFPlus::UI::Gauge; 2940package CFPlus::UI::Gauge;
2703 2941
2704our @ISA = CFPlus::UI::VBox::; 2942our @ISA = CFPlus::UI::VBox::;
2705 2943
2706sub new { 2944sub new {
3203 3441
3204 if ($y0 < $y + $h && $y < $y1) { 3442 if ($y0 < $y + $h && $y < $y1) {
3205 my $layout = $self->get_layout ($para); 3443 my $layout = $self->get_layout ($para);
3206 3444
3207 $layout->render ($para->{indent}, $y - $y0); 3445 $layout->render ($para->{indent}, $y - $y0);
3446 $layout->draw;
3208 3447
3209 if (my @w = @{ $para->{widget} }) { 3448 if (my @w = @{ $para->{widget} }) {
3210 my @s = $layout->get_shapes; 3449 my @s = $layout->get_shapes;
3211 3450
3212 for (@w) { 3451 for (@w) {
3334} 3573}
3335 3574
3336sub set_tooltip_from { 3575sub set_tooltip_from {
3337 my ($self, $widget) = @_; 3576 my ($self, $widget) = @_;
3338 3577
3339 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3340 if $widget->{tooltip} =~ /^#(.*)$/;
3341
3342 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 =~ /^#(.*)$/;
3343 3583
3344 if ($ENV{CFPLUS_DEBUG} & 2) { 3584 if ($ENV{CFPLUS_DEBUG} & 2) {
3345 $tooltip .= "\n\n" . (ref $widget) . "\n" 3585 $tip .= "\n\n" . (ref $widget) . "\n"
3346 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3586 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3347 . "req $widget->{req_w} $widget->{req_h}\n" 3587 . "req $widget->{req_w} $widget->{req_h}\n"
3348 . "visible $widget->{visible}"; 3588 . "visible $widget->{visible}";
3349 } 3589 }
3350 3590
3351 $tooltip =~ s/^\n+//; 3591 $tip =~ s/^\n+//;
3352 $tooltip =~ s/\n+$//; 3592 $tip =~ s/\n+$//;
3353 3593
3354 $self->add (new CFPlus::UI::Label 3594 $self->add (new CFPlus::UI::Label
3355 markup => $tooltip, 3595 markup => $tip,
3356 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3596 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3357 fontsize => 0.8, 3597 fontsize => 0.8,
3358 style => 1, # FLAG_INVERSE 3598 style => 1, # FLAG_INVERSE
3359 ellipsise => 0, 3599 ellipsise => 0,
3360 font => ($widget->{tooltip_font} || $::FONT_PROP), 3600 font => ($widget->{tooltip_font} || $::FONT_PROP),
3398} 3638}
3399 3639
3400sub _draw { 3640sub _draw {
3401 my ($self) = @_; 3641 my ($self) = @_;
3402 3642
3403 glTranslate 0.375, 0.375;
3404
3405 my ($w, $h) = @$self{qw(w h)}; 3643 my ($w, $h) = @$self{qw(w h)};
3406 3644
3407 glColor 1, 0.8, 0.4; 3645 glColor 1, 0.8, 0.4;
3408 glBegin GL_QUADS; 3646 glRect 0, 0, $w, $h;
3409 glVertex 0 , 0;
3410 glVertex 0 , $h;
3411 glVertex $w, $h;
3412 glVertex $w, 0;
3413 glEnd;
3414 3647
3415 glColor 0, 0, 0; 3648 glColor 0, 0, 0;
3416 glBegin GL_LINE_LOOP; 3649 glRect_lineloop .5, .5, $w + .5, $h + .5;
3417 glVertex 0 , 0;
3418 glVertex 0 , $h;
3419 glVertex $w, $h;
3420 glVertex $w, 0;
3421 glEnd;
3422 3650
3423 glTranslate 2 - 0.375, 2 - 0.375; 3651 glTranslate 2, 2;
3424 3652
3425 $self->SUPER::_draw; 3653 $self->SUPER::_draw;
3426} 3654}
3427 3655
3428############################################################################# 3656#############################################################################
3446 3674
3447 if ($self->{anim} && $self->{animspeed}) { 3675 if ($self->{anim} && $self->{animspeed}) {
3448 CFPlus::weaken (my $widget = $self); 3676 CFPlus::weaken (my $widget = $self);
3449 3677
3450 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3678 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3451 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3679 $widget->{anim_start} = $self->{animspeed} * int EV::now / $self->{animspeed};
3452 $self->{timer} = Event->timer ( 3680 $self->{timer} = EV::timer_ns 0, 0, sub {
3453 parked => 1,
3454 cb => sub {
3455 return unless $::CONN && $widget; 3681 return unless $::CONN;
3456 3682
3683 my $w = $widget
3684 or return;
3685
3457 ++$widget->{frame}; 3686 ++$w->{frame};
3458 $widget->update_face; 3687 $w->update_face;
3688
3689 # somehow, $widget can go away
3459 $widget->update; 3690 $w->update;
3460
3461 $widget->update_timer; 3691 $w->update_timer;
3462 },
3463 ); 3692 };
3464 3693
3465 $self->update_face; 3694 $self->update_face;
3466 $self->update_timer; 3695 $self->update_timer;
3467 } 3696 }
3468 3697
3473 my ($self) = @_; 3702 my ($self) = @_;
3474 3703
3475 return unless $self->{timer}; 3704 return unless $self->{timer};
3476 3705
3477 if ($self->{visible}) { 3706 if ($self->{visible}) {
3478 $self->{timer}->at ( 3707 $self->{timer}->set (
3479 $self->{anim_start} 3708 $self->{anim_start}
3480 + $self->{animspeed} 3709 + $self->{animspeed}
3481 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed} 3710 * int 1.5 + (EV::now - $self->{anim_start}) / $self->{animspeed}
3482 ); 3711 );
3483 $self->{timer}->start; 3712 $self->{timer}->start;
3484 } else { 3713 } else {
3485 $self->{timer}->stop; 3714 $self->{timer}->stop;
3486 } 3715 }
3487} 3716}
3488 3717
3489sub update_face { 3718sub update_face {
3490 my ($self) = @_; 3719 my ($self) = @_;
3491 3720
3492 return unless $::CONN; 3721 if ($::CONN) {
3493
3494 if (my $anim = $::CONN->{anim}[$self->{anim}]) { 3722 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3495 if ($anim && @$anim) { 3723 if ($anim && @$anim) {
3496 delete $self->{wait_face};
3497 $self->{face} = $anim->[ $self->{frame} % @$anim ]; 3724 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3725 delete $self->{face_change_cb};
3726
3498 $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ]; 3727 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3728 unless ($tex->{name} || $tex->{loading}) {
3729 $tex->upload (sub { $self->reconfigure });
3730 }
3731 }
3732 }
3499 } 3733 }
3500 } 3734 }
3501} 3735}
3502 3736
3503sub size_request { 3737sub size_request {
3504 my ($self) = @_; 3738 my ($self) = @_;
3505 3739
3506 if ($::CONN) { 3740 if ($::CONN) {
3507 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3741 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3508 if (my $tex = $::CONN->{texture}[$faceid]) { 3742 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3509 $self->{tex} = $tex; 3743 if ($tex->{name}) {
3510 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3744 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3511 } else { 3745 } elsif (!$tex->{loading}) {
3512 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3746 $tex->upload (sub { $self->reconfigure });
3513 $self->realloc;
3514 }); 3747 }
3515 } 3748 }
3749
3750 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3516 } 3751 }
3517 } 3752 }
3518 3753
3519 ($self->{size_w} || 8, $self->{size_h} || 8) 3754 ($self->{size_w} || 8, $self->{size_h} || 8)
3520} 3755}
3600 children => [ 3835 children => [
3601 (new CFPlus::UI::Label markup => $left, expand => 1), 3836 (new CFPlus::UI::Label markup => $left, expand => 1),
3602 (new CFPlus::UI::Label markup => $right, align => +1), 3837 (new CFPlus::UI::Label markup => $right, align => +1),
3603 ], 3838 ],
3604 ; 3839 ;
3605 3840
3606 } else { 3841 } else {
3607 $widget = new CFPlus::UI::Label 3842 $widget = new CFPlus::UI::Label
3608 can_hover => 1, 3843 can_hover => 1,
3609 can_events => 1, 3844 can_events => 1,
3610 markup => $widget, 3845 markup => $widget,
3735 3970
3736############################################################################# 3971#############################################################################
3737 3972
3738package CFPlus::UI::Notebook; 3973package CFPlus::UI::Notebook;
3739 3974
3975use CFPlus::OpenGL;
3976
3740our @ISA = CFPlus::UI::VBox::; 3977our @ISA = CFPlus::UI::VBox::;
3741 3978
3742sub new { 3979sub new {
3743 my $class = shift; 3980 my $class = shift;
3744 3981
3745 my $self = $class->SUPER::new ( 3982 my $self = $class->SUPER::new (
3746 buttonbar => (new CFPlus::UI::Buttonbar), 3983 buttonbar => (new CFPlus::UI::Buttonbar),
3747 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3984 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3985 active_outline => [.7, .7, 0.2],
3748 # filter => # will be put between multiplexer and $self 3986 # filter => # will be put between multiplexer and $self
3749 @_, 3987 @_,
3750 ); 3988 );
3751 3989
3752 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3990 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3753 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3991 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3754 3992
3755 { 3993 {
3756 Scalar::Util::weaken (my $wself = $self); 3994 Scalar::Util::weaken (my $wself = $self);
3799 my ($self, @widgets) = @_; 4037 my ($self, @widgets) = @_;
3800 4038
3801 $self->{multiplexer}->remove (@widgets) 4039 $self->{multiplexer}->remove (@widgets)
3802} 4040}
3803 4041
4042sub pages {
4043 my ($self) = @_;
4044 $self->{multiplexer}->children
4045}
4046
3804sub add_tab { 4047sub add_tab {
3805 my ($self, $title, $widget, $tooltip) = @_; 4048 my ($self, $title, $widget, $tooltip) = @_;
3806 4049
3807 $title = [$title, $tooltip] unless ref $title; 4050 $title = [$title, $tooltip] unless ref $title;
3808 $widget->{c_tab} = $title; 4051 $widget->{c_tab} = $title;
3819sub set_current_page { 4062sub set_current_page {
3820 my ($self, $page) = @_; 4063 my ($self, $page) = @_;
3821 4064
3822 $self->{multiplexer}->set_current_page ($page); 4065 $self->{multiplexer}->set_current_page ($page);
3823 $self->emit (page_changed => $self->{multiplexer}{current}); 4066 $self->emit (page_changed => $self->{multiplexer}{current});
4067}
4068
4069sub _draw {
4070 my ($self) = @_;
4071
4072 $self->SUPER::_draw ();
4073
4074 if (my $cur = $self->{multiplexer}{current}) {
4075 if ($cur = $cur->{c_tab_}) {
4076 glTranslate $self->{buttonbar}{x} + $cur->{x},
4077 $self->{buttonbar}{y} + $cur->{y};
4078 glLineWidth 3;
4079 #glEnable GL_BLEND;
4080 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4081 glColor @{$self->{active_outline}};
4082 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4083 glLineWidth 1;
4084 #glDisable GL_BLEND;
4085 }
4086 }
3824} 4087}
3825 4088
3826############################################################################# 4089#############################################################################
3827 4090
3828package CFPlus::UI::Selector; 4091package CFPlus::UI::Selector;
3860} 4123}
3861 4124
3862sub _set_value { 4125sub _set_value {
3863 my ($self, $value) = @_; 4126 my ($self, $value) = @_;
3864 4127
3865 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4128 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4129 $item ||= $self->{options}[0]
3866 or return; 4130 or return;
3867 4131
3868 $self->{value} = $item->[0]; 4132 $self->{value} = $item->[0];
3869 $self->set_markup ("$item->[1] ⇓"); 4133 $self->set_markup ("$item->[1] ⇓");
3870 $self->set_tooltip ($item->[2]); 4134# $self->set_tooltip ($item->[2]);
3871} 4135}
3872 4136
3873sub set_value { 4137sub set_value {
3874 my ($self, $value) = @_; 4138 my ($self, $value) = @_;
3875 4139
3876 return unless $self->{value} ne $value; 4140 return unless $self->{value} ne $value;
3877 4141
3878 $self->_set_value ($value); 4142 $self->_set_value ($value);
3879 $self->emit (changed => $value); 4143 $self->emit (changed => $value);
4144}
4145
4146sub set_options {
4147 my ($self, $options) = @_;
4148
4149 $self->{options} = $options;
4150 $self->_set_value ($self->{value});
3880} 4151}
3881 4152
3882############################################################################# 4153#############################################################################
3883 4154
3884package CFPlus::UI::Statusbox; 4155package CFPlus::UI::Statusbox;
3893 @_, 4164 @_,
3894 ); 4165 );
3895 4166
3896 CFPlus::weaken (my $this = $self); 4167 CFPlus::weaken (my $this = $self);
3897 4168
3898 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4169 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3899 4170
3900 $self 4171 $self
3901} 4172}
3902 4173
3903sub reorder { 4174sub reorder {
3911 4182
3912 while (my ($k, $v) = each %{ $self->{item} }) { 4183 while (my ($k, $v) = each %{ $self->{item} }) {
3913 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4184 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3914 } 4185 }
3915 4186
4187 $self->{timer}->set (1, 1);
4188
3916 my @widgets; 4189 my @widgets;
3917 4190
3918 my @items = sort { 4191 my @items = sort {
3919 $a->{pri} <=> $b->{pri} 4192 $a->{pri} <=> $b->{pri}
3920 or $b->{id} <=> $a->{id} 4193 or $b->{id} <=> $a->{id}
3921 } values %{ $self->{item} }; 4194 } values %{ $self->{item} };
3922
3923 $self->{timer}->interval (1);
3924 4195
3925 my $count = 10 + 1; 4196 my $count = 10 + 1;
3926 for my $item (@items) { 4197 for my $item (@items) {
3927 last unless --$count; 4198 last unless --$count;
3928 4199
3952 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4223 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3953 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4224 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3954 $label->update; 4225 $label->update;
3955 $label->set_max_size (undef, $label->{req_h} * $diff) 4226 $label->set_max_size (undef, $label->{req_h} * $diff)
3956 if $diff < 1; 4227 if $diff < 1;
3957 $self->{timer}->interval (1/30); 4228 $self->{timer}->set (1/30, 1/30);
3958 } else { 4229 } else {
3959 $label->{fg}[3] = $item->{fg}[3] || 1; 4230 $label->{fg}[3] = $item->{fg}[3] || 1;
3960 } 4231 }
3961 4232
3962 push @widgets, $label; 4233 push @widgets, $label;
4098} 4369}
4099 4370
4100sub update { 4371sub update {
4101 my ($self) = @_; 4372 my ($self) = @_;
4102 4373
4103 $::WANT_REFRESH++; 4374 $::WANT_REFRESH->start;
4104} 4375}
4105 4376
4106sub add { 4377sub add {
4107 my ($self, @children) = @_; 4378 my ($self, @children) = @_;
4108 4379

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines