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.421 by root, Wed Aug 1 14:27:21 2007 UTC vs.
Revision 1.447 by root, Sat Dec 22 06:50:45 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 { __w_ => $_[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#############################################################################
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::;
1219 my ($w, $h) = @$self{qw(w h)}; 1217 my ($w, $h) = @$self{qw(w h)};
1220 1218
1221 glEnable GL_BLEND; 1219 glEnable GL_BLEND;
1222 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1220 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1223 glColor_premultiply @{ $self->{bg} }; 1221 glColor_premultiply @{ $self->{bg} };
1224
1225 glBegin GL_QUADS;
1226 glVertex 0 , 0;
1227 glVertex 0 , $h;
1228 glVertex $w, $h; 1222 glRect 0, 0, $w, $h;
1229 glVertex $w, 0;
1230 glEnd;
1231
1232 glDisable GL_BLEND; 1223 glDisable GL_BLEND;
1233 } 1224 }
1234 1225
1235 $self->SUPER::_draw; 1226 $self->SUPER::_draw;
1236} 1227}
1395 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1386 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1396} 1387}
1397 1388
1398sub border { 1389sub border {
1399 int $_[0]{border} * $::FONTSIZE 1390 int $_[0]{border} * $::FONTSIZE
1391}
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} 1400}
1401 1401
1402sub size_request { 1402sub size_request {
1403 my ($self) = @_; 1403 my ($self) = @_;
1404 1404
1923 1923
1924 delete $self->{ox}; 1924 delete $self->{ox};
1925 $self->SUPER::realloc; 1925 $self->SUPER::realloc;
1926} 1926}
1927 1927
1928sub clear {
1929 my ($self) = @_;
1930
1931 $self->set_text ("");
1932}
1933
1928sub set_text { 1934sub set_text {
1929 my ($self, $text) = @_; 1935 my ($self, $text) = @_;
1930 1936
1931 return if $self->{text} eq "T$text"; 1937 return if $self->{text} eq "T$text";
1932 $self->{text} = "T$text"; 1938 $self->{text} = "T$text";
1955 1961
1956sub size_request { 1962sub size_request {
1957 my ($self) = @_; 1963 my ($self) = @_;
1958 1964
1959 $self->{size_req} ||= do { 1965 $self->{size_req} ||= do {
1966 my ($max_w, $max_h) = $self->get_max_wh;
1967
1960 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1968 $self->{layout}->set_font ($self->{font}) if $self->{font};
1961 $self->{layout}->set_width ($self->{max_w} || -1); 1969 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1962 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1970 $self->{layout}->set_ellipsise ($self->{ellipsise});
1963 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1971 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1964 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1972 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1965 1973
1966 my ($w, $h) = $self->{layout}->size; 1974 my ($w, $h) = $self->{layout}->size;
2038 : ($self->{w} - $size->[0]) * 0.5); 2046 : ($self->{w} - $size->[0]) * 0.5);
2039 2047
2040 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2048 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2041 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2049 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2042 : ($self->{h} - $size->[1]) * 0.5); 2050 : ($self->{h} - $size->[1]) * 0.5);
2051
2052 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2043 }; 2053 };
2044 2054
2045# unless ($self->{list}) { 2055# unless ($self->{list}) {
2046# $self->{list} = CFPlus::OpenGL::glGenList; 2056# $self->{list} = CFPlus::OpenGL::glGenList;
2047# CFPlus::OpenGL::glNewList $self->{list}; 2057# CFPlus::OpenGL::glNewList $self->{list};
2049# CFPlus::OpenGL::glEndList; 2059# CFPlus::OpenGL::glEndList;
2050# } 2060# }
2051# 2061#
2052# CFPlus::OpenGL::glCallList $self->{list}; 2062# CFPlus::OpenGL::glCallList $self->{list};
2053 2063
2054 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2064 $self->{layout}->draw;
2055} 2065}
2056 2066
2057#sub destroy { 2067#sub destroy {
2058# my ($self) = @_; 2068# my ($self) = @_;
2059# 2069#
2227 glColor_premultiply @{$self->{bg}}; 2237 glColor_premultiply @{$self->{bg}};
2228 } 2238 }
2229 2239
2230 glEnable GL_BLEND; 2240 glEnable GL_BLEND;
2231 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2241 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2232 glBegin GL_QUADS;
2233 glVertex 0 , 0;
2234 glVertex 0 , $self->{h};
2235 glVertex $self->{w}, $self->{h}; 2242 glRect 0, 0, $self->{w}, $self->{h};
2236 glVertex $self->{w}, 0;
2237 glEnd;
2238 glDisable GL_BLEND; 2243 glDisable GL_BLEND;
2239 2244
2240 $self->SUPER::_draw; 2245 $self->SUPER::_draw;
2241 2246
2242 #TODO: force update every cursor change :( 2247 #TODO: force update every cursor change :(
2249 @$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);
2250 } 2255 }
2251 2256
2252 glColor_premultiply @{$self->{active_fg}}; 2257 glColor_premultiply @{$self->{active_fg}};
2253 glBegin GL_LINES; 2258 glBegin GL_LINES;
2254 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};
2255 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};
2256 glEnd; 2261 glEnd;
2257 2262
2258 glLineWidth 3; 2263 glLineWidth 3;
2259 glColor @{$self->{active_outline}}; 2264 glColor @{$self->{active_outline}};
2260 glBegin GL_LINE_LOOP;
2261 glVertex 0, 0;
2262 glVertex $self->{w} - 1, 0;
2263 glVertex $self->{w} - 1, $self->{h} - 1; 2265 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2264 glVertex 0, $self->{h} - 1;
2265 glEnd;
2266 glLineWidth 1; 2266 glLineWidth 1;
2267 2267
2268 } else { 2268 } else {
2269 glColor @{$self->{outline}}; 2269 glColor @{$self->{outline}};
2270 glTranslate .375, .375;
2271 glBegin GL_LINE_STRIP; 2270 glBegin GL_LINE_STRIP;
2272 glVertex 0, $self->{h} * .5; 2271 glVertex .5, $self->{h} * .5;
2273 glVertex 0, $self->{h} - 3; 2272 glVertex .5, $self->{h} - 2.5;
2274 glVertex $self->{w} - 1, $self->{h} - 3; 2273 glVertex $self->{w} - .5, $self->{h} - 2.5;
2275 glVertex $self->{w} - 1, $self->{h} * .5; 2274 glVertex $self->{w} - .5, $self->{h} * .5;
2276 glEnd; 2275 glEnd;
2277 } 2276 }
2278} 2277}
2279 2278
2280############################################################################# 2279#############################################################################
2529sub _draw { 2528sub _draw {
2530 my ($self) = @_; 2529 my ($self) = @_;
2531 2530
2532 $self->SUPER::_draw; 2531 $self->SUPER::_draw;
2533 2532
2534 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2533 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2535 2534
2536 my ($w, $h) = @$self{qw(w h)}; 2535 my ($w, $h) = @$self{qw(w h)};
2537 2536
2538 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;
2539 2538
2643 align => 0, 2642 align => 0,
2644 valign => 0, 2643 valign => 0,
2645 can_events => 1, 2644 can_events => 1,
2646 @_ 2645 @_
2647 ); 2646 );
2647}
2648
2649sub invoke_button_down {
2650 my ($self, $ev, $x, $y) = @_;
2651
2652 1
2648} 2653}
2649 2654
2650sub invoke_button_up { 2655sub invoke_button_up {
2651 my ($self, $ev, $x, $y) = @_; 2656 my ($self, $ev, $x, $y) = @_;
2652 2657
2792 glDisable GL_TEXTURE_2D; 2797 glDisable GL_TEXTURE_2D;
2793} 2798}
2794 2799
2795############################################################################# 2800#############################################################################
2796 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
2797package CFPlus::UI::Gauge; 2940package CFPlus::UI::Gauge;
2798 2941
2799our @ISA = CFPlus::UI::VBox::; 2942our @ISA = CFPlus::UI::VBox::;
2800 2943
2801sub new { 2944sub new {
3298 3441
3299 if ($y0 < $y + $h && $y < $y1) { 3442 if ($y0 < $y + $h && $y < $y1) {
3300 my $layout = $self->get_layout ($para); 3443 my $layout = $self->get_layout ($para);
3301 3444
3302 $layout->render ($para->{indent}, $y - $y0); 3445 $layout->render ($para->{indent}, $y - $y0);
3446 $layout->draw;
3303 3447
3304 if (my @w = @{ $para->{widget} }) { 3448 if (my @w = @{ $para->{widget} }) {
3305 my @s = $layout->get_shapes; 3449 my @s = $layout->get_shapes;
3306 3450
3307 for (@w) { 3451 for (@w) {
3429} 3573}
3430 3574
3431sub set_tooltip_from { 3575sub set_tooltip_from {
3432 my ($self, $widget) = @_; 3576 my ($self, $widget) = @_;
3433 3577
3434 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3435 if $widget->{tooltip} =~ /^#(.*)$/;
3436
3437 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 =~ /^#(.*)$/;
3438 3583
3439 if ($ENV{CFPLUS_DEBUG} & 2) { 3584 if ($ENV{CFPLUS_DEBUG} & 2) {
3440 $tooltip .= "\n\n" . (ref $widget) . "\n" 3585 $tip .= "\n\n" . (ref $widget) . "\n"
3441 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3586 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3442 . "req $widget->{req_w} $widget->{req_h}\n" 3587 . "req $widget->{req_w} $widget->{req_h}\n"
3443 . "visible $widget->{visible}"; 3588 . "visible $widget->{visible}";
3444 } 3589 }
3445 3590
3446 $tooltip =~ s/^\n+//; 3591 $tip =~ s/^\n+//;
3447 $tooltip =~ s/\n+$//; 3592 $tip =~ s/\n+$//;
3448 3593
3449 $self->add (new CFPlus::UI::Label 3594 $self->add (new CFPlus::UI::Label
3450 markup => $tooltip, 3595 markup => $tip,
3451 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3596 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3452 fontsize => 0.8, 3597 fontsize => 0.8,
3453 style => 1, # FLAG_INVERSE 3598 style => 1, # FLAG_INVERSE
3454 ellipsise => 0, 3599 ellipsise => 0,
3455 font => ($widget->{tooltip_font} || $::FONT_PROP), 3600 font => ($widget->{tooltip_font} || $::FONT_PROP),
3493} 3638}
3494 3639
3495sub _draw { 3640sub _draw {
3496 my ($self) = @_; 3641 my ($self) = @_;
3497 3642
3498 glTranslate 0.375, 0.375;
3499
3500 my ($w, $h) = @$self{qw(w h)}; 3643 my ($w, $h) = @$self{qw(w h)};
3501 3644
3502 glColor 1, 0.8, 0.4; 3645 glColor 1, 0.8, 0.4;
3503 glRect 0, 0, $w, $h; 3646 glRect 0, 0, $w, $h;
3504 3647
3505 glColor 0, 0, 0; 3648 glColor 0, 0, 0;
3506 glBegin GL_LINE_LOOP; 3649 glRect_lineloop .5, .5, $w + .5, $h + .5;
3507 glVertex 0 , 0;
3508 glVertex 0 , $h;
3509 glVertex $w, $h;
3510 glVertex $w, 0;
3511 glEnd;
3512 3650
3513 glTranslate 2 - 0.375, 2 - 0.375; 3651 glTranslate 2, 2;
3514 3652
3515 $self->SUPER::_draw; 3653 $self->SUPER::_draw;
3516} 3654}
3517 3655
3518############################################################################# 3656#############################################################################
3536 3674
3537 if ($self->{anim} && $self->{animspeed}) { 3675 if ($self->{anim} && $self->{animspeed}) {
3538 CFPlus::weaken (my $widget = $self); 3676 CFPlus::weaken (my $widget = $self);
3539 3677
3540 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3678 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3541 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3679 $widget->{anim_start} = $self->{animspeed} * int EV::now / $self->{animspeed};
3542 $self->{timer} = Event->timer ( 3680 $self->{timer} = EV::timer_ns 0, 0, sub {
3543 parked => 1,
3544 cb => sub {
3545 return unless $::CONN && $widget; 3681 return unless $::CONN;
3546 3682
3683 my $w = $widget
3684 or return;
3685
3547 ++$widget->{frame}; 3686 ++$w->{frame};
3548 $widget->update_face; 3687 $w->update_face;
3688
3689 # somehow, $widget can go away
3549 $widget->update; 3690 $w->update;
3550
3551 $widget->update_timer; 3691 $w->update_timer;
3552 },
3553 ); 3692 };
3554 3693
3555 $self->update_face; 3694 $self->update_face;
3556 $self->update_timer; 3695 $self->update_timer;
3557 } 3696 }
3558 3697
3562sub update_timer { 3701sub update_timer {
3563 my ($self) = @_; 3702 my ($self) = @_;
3564 3703
3565 return unless $self->{timer}; 3704 return unless $self->{timer};
3566 3705
3706 $self->{timer}->stop;
3567 if ($self->{visible}) { 3707 if ($self->{visible}) {
3568 $self->{timer}->at ( 3708 $self->{timer}->set (
3569 $self->{anim_start} 3709 $self->{anim_start}
3570 + $self->{animspeed} 3710 + $self->{animspeed}
3571 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed} 3711 * int 1.5 + (EV::now - $self->{anim_start}) / $self->{animspeed}
3572 ); 3712 );
3573 $self->{timer}->start; 3713 $self->{timer}->start;
3574 } else {
3575 $self->{timer}->stop;
3576 } 3714 }
3577} 3715}
3578 3716
3579sub update_face { 3717sub update_face {
3580 my ($self) = @_; 3718 my ($self) = @_;
3581 3719
3582 return unless $::CONN; 3720 if ($::CONN) {
3583
3584 if (my $anim = $::CONN->{anim}[$self->{anim}]) { 3721 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3585 if ($anim && @$anim) { 3722 if ($anim && @$anim) {
3586 delete $self->{wait_face};
3587 $self->{face} = $anim->[ $self->{frame} % @$anim ]; 3723 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3724 delete $self->{face_change_cb};
3725
3588 $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ]; 3726 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3727 unless ($tex->{name} || $tex->{loading}) {
3728 $tex->upload (sub { $self->reconfigure });
3729 }
3730 }
3731 }
3589 } 3732 }
3590 } 3733 }
3591} 3734}
3592 3735
3593sub size_request { 3736sub size_request {
3594 my ($self) = @_; 3737 my ($self) = @_;
3595 3738
3596 if ($::CONN) { 3739 if ($::CONN) {
3597 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3740 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3598 if (my $tex = $::CONN->{texture}[$faceid]) { 3741 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3599 $self->{tex} = $tex; 3742 if ($tex->{name}) {
3600 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3743 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3601 } else { 3744 } elsif (!$tex->{loading}) {
3602 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3745 $tex->upload (sub { $self->reconfigure });
3603 $self->realloc;
3604 }); 3746 }
3605 } 3747 }
3748
3749 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3606 } 3750 }
3607 } 3751 }
3608 3752
3609 ($self->{size_w} || 8, $self->{size_h} || 8) 3753 ($self->{size_w} || 8, $self->{size_h} || 8)
3610} 3754}
3825 3969
3826############################################################################# 3970#############################################################################
3827 3971
3828package CFPlus::UI::Notebook; 3972package CFPlus::UI::Notebook;
3829 3973
3974use CFPlus::OpenGL;
3975
3830our @ISA = CFPlus::UI::VBox::; 3976our @ISA = CFPlus::UI::VBox::;
3831 3977
3832sub new { 3978sub new {
3833 my $class = shift; 3979 my $class = shift;
3834 3980
3835 my $self = $class->SUPER::new ( 3981 my $self = $class->SUPER::new (
3836 buttonbar => (new CFPlus::UI::Buttonbar), 3982 buttonbar => (new CFPlus::UI::Buttonbar),
3837 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3983 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3984 active_outline => [.7, .7, 0.2],
3838 # filter => # will be put between multiplexer and $self 3985 # filter => # will be put between multiplexer and $self
3839 @_, 3986 @_,
3840 ); 3987 );
3841 3988
3842 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3989 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3916 4063
3917 $self->{multiplexer}->set_current_page ($page); 4064 $self->{multiplexer}->set_current_page ($page);
3918 $self->emit (page_changed => $self->{multiplexer}{current}); 4065 $self->emit (page_changed => $self->{multiplexer}{current});
3919} 4066}
3920 4067
4068sub _draw {
4069 my ($self) = @_;
4070
4071 $self->SUPER::_draw ();
4072
4073 if (my $cur = $self->{multiplexer}{current}) {
4074 if ($cur = $cur->{c_tab_}) {
4075 glTranslate $self->{buttonbar}{x} + $cur->{x},
4076 $self->{buttonbar}{y} + $cur->{y};
4077 glLineWidth 3;
4078 #glEnable GL_BLEND;
4079 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4080 glColor @{$self->{active_outline}};
4081 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4082 glLineWidth 1;
4083 #glDisable GL_BLEND;
4084 }
4085 }
4086}
4087
3921############################################################################# 4088#############################################################################
3922 4089
3923package CFPlus::UI::Selector; 4090package CFPlus::UI::Selector;
3924 4091
3925use utf8; 4092use utf8;
3955} 4122}
3956 4123
3957sub _set_value { 4124sub _set_value {
3958 my ($self, $value) = @_; 4125 my ($self, $value) = @_;
3959 4126
3960 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4127 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4128 $item ||= $self->{options}[0]
3961 or return; 4129 or return;
3962 4130
3963 $self->{value} = $item->[0]; 4131 $self->{value} = $item->[0];
3964 $self->set_markup ("$item->[1] ⇓"); 4132 $self->set_markup ("$item->[1] ⇓");
3965 $self->set_tooltip ($item->[2]); 4133# $self->set_tooltip ($item->[2]);
3966} 4134}
3967 4135
3968sub set_value { 4136sub set_value {
3969 my ($self, $value) = @_; 4137 my ($self, $value) = @_;
3970 4138
3971 return unless $self->{value} ne $value; 4139 return unless $self->{value} ne $value;
3972 4140
3973 $self->_set_value ($value); 4141 $self->_set_value ($value);
3974 $self->emit (changed => $value); 4142 $self->emit (changed => $value);
4143}
4144
4145sub set_options {
4146 my ($self, $options) = @_;
4147
4148 $self->{options} = $options;
4149 $self->_set_value ($self->{value});
3975} 4150}
3976 4151
3977############################################################################# 4152#############################################################################
3978 4153
3979package CFPlus::UI::Statusbox; 4154package CFPlus::UI::Statusbox;
3988 @_, 4163 @_,
3989 ); 4164 );
3990 4165
3991 CFPlus::weaken (my $this = $self); 4166 CFPlus::weaken (my $this = $self);
3992 4167
3993 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4168 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3994 4169
3995 $self 4170 $self
3996} 4171}
3997 4172
3998sub reorder { 4173sub reorder {
4012 4187
4013 my @items = sort { 4188 my @items = sort {
4014 $a->{pri} <=> $b->{pri} 4189 $a->{pri} <=> $b->{pri}
4015 or $b->{id} <=> $a->{id} 4190 or $b->{id} <=> $a->{id}
4016 } values %{ $self->{item} }; 4191 } values %{ $self->{item} };
4017
4018 $self->{timer}->interval (1);
4019 4192
4020 my $count = 10 + 1; 4193 my $count = 10 + 1;
4021 for my $item (@items) { 4194 for my $item (@items) {
4022 last unless --$count; 4195 last unless --$count;
4023 4196
4047 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4220 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4048 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4221 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4049 $label->update; 4222 $label->update;
4050 $label->set_max_size (undef, $label->{req_h} * $diff) 4223 $label->set_max_size (undef, $label->{req_h} * $diff)
4051 if $diff < 1; 4224 if $diff < 1;
4052 $self->{timer}->interval (1/30); 4225 $self->{timer}->set (1/30, 1/30);
4053 } else { 4226 } else {
4054 $label->{fg}[3] = $item->{fg}[3] || 1; 4227 $label->{fg}[3] = $item->{fg}[3] || 1;
4055 } 4228 }
4056 4229
4057 push @widgets, $label; 4230 push @widgets, $label;
4193} 4366}
4194 4367
4195sub update { 4368sub update {
4196 my ($self) = @_; 4369 my ($self) = @_;
4197 4370
4198 $::WANT_REFRESH++; 4371 $::WANT_REFRESH->start;
4199} 4372}
4200 4373
4201sub add { 4374sub add {
4202 my ($self, @children) = @_; 4375 my ($self, @children) = @_;
4203 4376

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines