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.429 by root, Tue Aug 21 00:59:34 2007 UTC vs.
Revision 1.452 by root, Wed Dec 26 20:09:38 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, 0.03, 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}
341sub hide { 338sub hide {
342 my ($self) = @_; 339 my ($self) = @_;
343 340
344 $self->set_invisible; 341 $self->set_invisible;
345 342
343 # extra $parent copy for 5.8.8+ bug workaround
344 # (otherwise $_[0] in remove gets freed
345 if (my $parent = $self->{parent}) {
346 $self->{parent}->remove ($self) 346 $parent->remove ($self);
347 if $self->{parent}; 347 }
348} 348}
349 349
350sub move_abs { 350sub move_abs {
351 my ($self, $x, $y, $z) = @_; 351 my ($self, $x, $y, $z) = @_;
352 352
362 362
363 $self->{force_w} = $w; 363 $self->{force_w} = $w;
364 $self->{force_h} = $h; 364 $self->{force_h} = $h;
365 365
366 $self->realloc; 366 $self->realloc;
367}
368
369# traverse the widget chain up to find the maximum "physical" size constraints
370sub get_max_wh {
371 my ($self) = @_;
372
373 return $self->{parent}->get_max_wh
374 if $self->{parent};
375
376 ($::WIDTH, $::HEIGHT)
367} 377}
368 378
369sub size_request { 379sub size_request {
370 require Carp; 380 require Carp;
371 Carp::confess "size_request is abstract"; 381 Carp::confess "size_request is abstract";
433 443
434 $self->{tooltip} = $tooltip; 444 $self->{tooltip} = $tooltip;
435 445
436 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 446 if ($CFPlus::UI::TOOLTIP->{owner} == $self) {
437 delete $CFPlus::UI::TOOLTIP->{owner}; 447 delete $CFPlus::UI::TOOLTIP->{owner};
438 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 448 $CFPlus::UI::TOOLTIP_WATCHER->invoke;
439 } 449 }
440} 450}
441 451
442# translate global coordinates to local coordinate system 452# translate global coordinates to local coordinate system
443sub coord2local { 453sub coord2local {
444 my ($self, $x, $y) = @_; 454 my ($self, $x, $y) = @_;
445 455
446 Carp::confess unless $self->{parent};#d# 456 return (undef, undef) unless $self->{parent};
447 457
448 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 458 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
449} 459}
450 460
451# translate local coordinates to global coordinate system 461# translate local coordinates to global coordinate system
452sub coord2global { 462sub coord2global {
453 my ($self, $x, $y) = @_; 463 my ($self, $x, $y) = @_;
454 464
455 Carp::confess unless $self->{parent};#d# 465 return (undef, undef) unless $self->{parent};
456 466
457 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 467 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
458} 468}
459 469
460sub invoke_focus_in { 470sub invoke_focus_in {
607sub DESTROY { 617sub DESTROY {
608 my ($self) = @_; 618 my ($self) = @_;
609 619
610 return if CFPlus::in_destruct; 620 return if CFPlus::in_destruct;
611 621
622 local $@;
612 eval { $self->destroy }; 623 eval { $self->destroy };
613 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 624 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
614 625
615 delete $WIDGET{$self+0}; 626 delete $WIDGET{$self+0};
616} 627}
624use strict; 635use strict;
625use CFPlus::OpenGL; 636use CFPlus::OpenGL;
626 637
627sub new { 638sub new {
628 my $class = shift; 639 my $class = shift;
629
630 # range [value, low, high, page]
631 640
632 $class->SUPER::new ( 641 $class->SUPER::new (
633 #bg => [0, 0, 0, 0.2], 642 #bg => [0, 0, 0, 0.2],
634 #active_bg => [1, 1, 1, 0.5], 643 #active_bg => [1, 1, 1, 0.5],
635 @_ 644 @_
647 my ($w, $h) = @$self{qw(w h)}; 656 my ($w, $h) = @$self{qw(w h)};
648 657
649 glEnable GL_BLEND; 658 glEnable GL_BLEND;
650 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 659 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
651 glColor_premultiply @$color; 660 glColor_premultiply @$color;
652
653 glBegin GL_QUADS;
654 glVertex 0 , 0;
655 glVertex 0 , $h;
656 glVertex $w, $h; 661 glRect 0, 0, $w, $h;
657 glVertex $w, 0;
658 glEnd;
659
660 glDisable GL_BLEND; 662 glDisable GL_BLEND;
661 } 663 }
662} 664}
663 665
664############################################################################# 666#############################################################################
829 831
830 1 832 1
831} 833}
832 834
833############################################################################# 835#############################################################################
834
835# back-buffered drawing area 836# back-buffered drawing area
836 837
837package CFPlus::UI::Window; 838package CFPlus::UI::Window;
838 839
839our @ISA = CFPlus::UI::Bin::; 840our @ISA = CFPlus::UI::Bin::;
1219 my ($w, $h) = @$self{qw(w h)}; 1220 my ($w, $h) = @$self{qw(w h)};
1220 1221
1221 glEnable GL_BLEND; 1222 glEnable GL_BLEND;
1222 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1223 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1223 glColor_premultiply @{ $self->{bg} }; 1224 glColor_premultiply @{ $self->{bg} };
1224
1225 glBegin GL_QUADS;
1226 glVertex 0 , 0;
1227 glVertex 0 , $h;
1228 glVertex $w, $h; 1225 glRect 0, 0, $w, $h;
1229 glVertex $w, 0;
1230 glEnd;
1231
1232 glDisable GL_BLEND; 1226 glDisable GL_BLEND;
1233 } 1227 }
1234 1228
1235 $self->SUPER::_draw; 1229 $self->SUPER::_draw;
1236} 1230}
1395 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1389 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1396} 1390}
1397 1391
1398sub border { 1392sub border {
1399 int $_[0]{border} * $::FONTSIZE 1393 int $_[0]{border} * $::FONTSIZE
1394}
1395
1396sub get_max_wh {
1397 my ($self) = @_;
1398
1399 return ($self->{w}, $self->{h})
1400 if $self->{visible} && $self->{w};
1401
1402 $self->SUPER::get_max_wh
1400} 1403}
1401 1404
1402sub size_request { 1405sub size_request {
1403 my ($self) = @_; 1406 my ($self) = @_;
1404 1407
1961 1964
1962sub size_request { 1965sub size_request {
1963 my ($self) = @_; 1966 my ($self) = @_;
1964 1967
1965 $self->{size_req} ||= do { 1968 $self->{size_req} ||= do {
1969 my ($max_w, $max_h) = $self->get_max_wh;
1970
1966 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1971 $self->{layout}->set_font ($self->{font}) if $self->{font};
1967 $self->{layout}->set_width ($self->{max_w} || -1); 1972 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1968 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1973 $self->{layout}->set_ellipsise ($self->{ellipsise});
1969 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1974 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1970 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1975 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1971 1976
1972 my ($w, $h) = $self->{layout}->size; 1977 my ($w, $h) = $self->{layout}->size;
2235 glColor_premultiply @{$self->{bg}}; 2240 glColor_premultiply @{$self->{bg}};
2236 } 2241 }
2237 2242
2238 glEnable GL_BLEND; 2243 glEnable GL_BLEND;
2239 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2244 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2240 glBegin GL_QUADS;
2241 glVertex 0 , 0;
2242 glVertex 0 , $self->{h};
2243 glVertex $self->{w}, $self->{h}; 2245 glRect 0, 0, $self->{w}, $self->{h};
2244 glVertex $self->{w}, 0;
2245 glEnd;
2246 glDisable GL_BLEND; 2246 glDisable GL_BLEND;
2247 2247
2248 $self->SUPER::_draw; 2248 $self->SUPER::_draw;
2249 2249
2250 #TODO: force update every cursor change :( 2250 #TODO: force update every cursor change :(
2257 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 2257 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2258 } 2258 }
2259 2259
2260 glColor_premultiply @{$self->{active_fg}}; 2260 glColor_premultiply @{$self->{active_fg}};
2261 glBegin GL_LINES; 2261 glBegin GL_LINES;
2262 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2262 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2263 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2263 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2264 glEnd; 2264 glEnd;
2265 2265
2266 glLineWidth 3; 2266 glLineWidth 3;
2267 glColor @{$self->{active_outline}}; 2267 glColor @{$self->{active_outline}};
2268 glBegin GL_LINE_LOOP;
2269 glVertex 0, 0;
2270 glVertex $self->{w} - 1, 0;
2271 glVertex $self->{w} - 1, $self->{h} - 1; 2268 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2272 glVertex 0, $self->{h} - 1;
2273 glEnd;
2274 glLineWidth 1; 2269 glLineWidth 1;
2275 2270
2276 } else { 2271 } else {
2277 glColor @{$self->{outline}}; 2272 glColor @{$self->{outline}};
2278 glTranslate .375, .375;
2279 glBegin GL_LINE_STRIP; 2273 glBegin GL_LINE_STRIP;
2280 glVertex 0, $self->{h} * .5; 2274 glVertex .5, $self->{h} * .5;
2281 glVertex 0, $self->{h} - 3; 2275 glVertex .5, $self->{h} - 2.5;
2282 glVertex $self->{w} - 1, $self->{h} - 3; 2276 glVertex $self->{w} - .5, $self->{h} - 2.5;
2283 glVertex $self->{w} - 1, $self->{h} * .5; 2277 glVertex $self->{w} - .5, $self->{h} * .5;
2284 glEnd; 2278 glEnd;
2285 } 2279 }
2286} 2280}
2287 2281
2288############################################################################# 2282#############################################################################
2537sub _draw { 2531sub _draw {
2538 my ($self) = @_; 2532 my ($self) = @_;
2539 2533
2540 $self->SUPER::_draw; 2534 $self->SUPER::_draw;
2541 2535
2542 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2536 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2543 2537
2544 my ($w, $h) = @$self{qw(w h)}; 2538 my ($w, $h) = @$self{qw(w h)};
2545 2539
2546 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2540 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2547 2541
2806 glDisable GL_TEXTURE_2D; 2800 glDisable GL_TEXTURE_2D;
2807} 2801}
2808 2802
2809############################################################################# 2803#############################################################################
2810 2804
2805package CFPlus::UI::Progress;
2806
2807our @ISA = CFPlus::UI::Label::;
2808
2809use CFPlus::OpenGL;
2810
2811sub new {
2812 my ($class, %arg) = @_;
2813
2814 my $self = $class->SUPER::new (
2815 fg => [1, 1, 1],
2816 bg => [0, 0, 1, 0.2],
2817 bar => [0.7, 0.5, 0.1, 0.8],
2818 outline => [0.4, 0.3, 0],
2819 fontsize => 0.9,
2820 valign => 0,
2821 align => 0,
2822 can_events => 1,
2823 ellipsise => 1,
2824 label => "%d%%",
2825 %arg,
2826 );
2827
2828 $self->set_value ($arg{value} || -1);
2829
2830 $self
2831}
2832
2833sub set_label {
2834 my ($self, $label) = @_;
2835
2836 return if $self->{label} eq $label;
2837 $self->{label} = $label;
2838
2839 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value});
2840}
2841
2842sub set_value {
2843 my ($self, $value) = @_;
2844
2845 if ($self->{value} ne $value) {
2846 $self->{value} = $value;
2847
2848 if ($value < 0) {
2849 $self->set_text ("-");
2850 } else {
2851 $self->set_text (sprintf $self->{label}, $value * 100);
2852 }
2853
2854 $self->update;
2855 }
2856}
2857
2858sub _draw {
2859 my ($self) = @_;
2860
2861 glEnable GL_BLEND;
2862 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2863
2864 if ($self->{value} >= 0) {
2865 my $s = int 2 + ($self->{w} - 4) * $self->{value};
2866
2867 glColor_premultiply @{$self->{bar}};
2868 glRect 2, 2, $s, $self->{h} - 2;
2869 glColor_premultiply @{$self->{bg}};
2870 glRect $s, 2, $self->{w} - 2, $self->{h} - 2;
2871 }
2872
2873 glColor_premultiply @{$self->{outline}};
2874 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2875
2876 glDisable GL_BLEND;
2877
2878 {
2879 local $self->{bg}; # do not draw background
2880 $self->SUPER::_draw;
2881 }
2882}
2883
2884#############################################################################
2885
2886package CFPlus::UI::ExperienceProgress;
2887
2888our @ISA = CFPlus::UI::Progress::;
2889
2890sub new {
2891 my ($class, %arg) = @_;
2892
2893 my $self = $class->SUPER::new (
2894 tooltip => sub {
2895 my ($self) = @_;
2896
2897 sprintf "level %d\n%s points\n%s next level\n%s to go",
2898 $self->{lvl},
2899 ::formsep ($self->{exp}),
2900 ::formsep ($self->{nxt}),
2901 ::formsep ($self->{nxt} - $self->{exp}),
2902 },
2903 %arg
2904 );
2905
2906 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2907 if $::CONN;
2908
2909 $self
2910}
2911
2912sub DESTROY {
2913 my ($self) = @_;
2914
2915 delete $::CONN->{on_exp_update}{$self+0}
2916 if $::CONN;
2917
2918 $self->SUPER::DESTROY;
2919}
2920
2921sub set_value {
2922 my ($self, $lvl, $exp) = @_;
2923
2924 $self->{lvl} = $lvl;
2925 $self->{exp} = $exp;
2926
2927 my $v = -1;
2928
2929 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2930 my $l0 = $table->[$lvl - 1];
2931 my $l1 = $table->[$lvl];
2932
2933 $self->{nxt} = $l1;
2934
2935 $v = ($exp - $l0) / ($l1 - $l0);
2936 }
2937
2938 $self->SUPER::set_value ($v);
2939}
2940
2941#############################################################################
2942
2811package CFPlus::UI::Gauge; 2943package CFPlus::UI::Gauge;
2812 2944
2813our @ISA = CFPlus::UI::VBox::; 2945our @ISA = CFPlus::UI::VBox::;
2814 2946
2815sub new { 2947sub new {
3444} 3576}
3445 3577
3446sub set_tooltip_from { 3578sub set_tooltip_from {
3447 my ($self, $widget) = @_; 3579 my ($self, $widget) = @_;
3448 3580
3449 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3450 if $widget->{tooltip} =~ /^#(.*)$/;
3451
3452 my $tooltip = $widget->{tooltip}; 3581 my $tip = $widget->{tooltip};
3582 $tip = $tip->($widget) if "CODE" eq ref $tip;
3583
3584 $tip = CFPlus::Pod::section_label tooltip => $1
3585 if $tip =~ /^#(.*)$/;
3453 3586
3454 if ($ENV{CFPLUS_DEBUG} & 2) { 3587 if ($ENV{CFPLUS_DEBUG} & 2) {
3455 $tooltip .= "\n\n" . (ref $widget) . "\n" 3588 $tip .= "\n\n" . (ref $widget) . "\n"
3456 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3589 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3457 . "req $widget->{req_w} $widget->{req_h}\n" 3590 . "req $widget->{req_w} $widget->{req_h}\n"
3458 . "visible $widget->{visible}"; 3591 . "visible $widget->{visible}";
3459 } 3592 }
3460 3593
3461 $tooltip =~ s/^\n+//; 3594 $tip =~ s/^\n+//;
3462 $tooltip =~ s/\n+$//; 3595 $tip =~ s/\n+$//;
3463 3596
3464 $self->add (new CFPlus::UI::Label 3597 $self->add (new CFPlus::UI::Label
3465 markup => $tooltip, 3598 markup => $tip,
3466 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3599 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3467 fontsize => 0.8, 3600 fontsize => 0.8,
3468 style => 1, # FLAG_INVERSE 3601 style => 1, # FLAG_INVERSE
3469 ellipsise => 0, 3602 ellipsise => 0,
3470 font => ($widget->{tooltip_font} || $::FONT_PROP), 3603 font => ($widget->{tooltip_font} || $::FONT_PROP),
3508} 3641}
3509 3642
3510sub _draw { 3643sub _draw {
3511 my ($self) = @_; 3644 my ($self) = @_;
3512 3645
3513 glTranslate 0.375, 0.375;
3514
3515 my ($w, $h) = @$self{qw(w h)}; 3646 my ($w, $h) = @$self{qw(w h)};
3516 3647
3517 glColor 1, 0.8, 0.4; 3648 glColor 1, 0.8, 0.4;
3518 glRect 0, 0, $w, $h; 3649 glRect 0, 0, $w, $h;
3519 3650
3520 glColor 0, 0, 0; 3651 glColor 0, 0, 0;
3521 glBegin GL_LINE_LOOP; 3652 glRect_lineloop .5, .5, $w + .5, $h + .5;
3522 glVertex 0 , 0;
3523 glVertex 0 , $h;
3524 glVertex $w, $h;
3525 glVertex $w, 0;
3526 glEnd;
3527 3653
3528 glTranslate 2 - 0.375, 2 - 0.375; 3654 glTranslate 2, 2;
3529 3655
3530 $self->SUPER::_draw; 3656 $self->SUPER::_draw;
3531} 3657}
3532 3658
3533############################################################################# 3659#############################################################################
3550 ); 3676 );
3551 3677
3552 if ($self->{anim} && $self->{animspeed}) { 3678 if ($self->{anim} && $self->{animspeed}) {
3553 CFPlus::weaken (my $widget = $self); 3679 CFPlus::weaken (my $widget = $self);
3554 3680
3555 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3681 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3556 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3682 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3557 $self->{timer} = Event->timer (
3558 parked => 1,
3559 cb => sub {
3560 return unless $::CONN; 3683 return unless $::CONN;
3561 3684
3562 my $w = $widget 3685 my $w = $widget
3563 or return; 3686 or return;
3564 3687
3565 ++$w->{frame}; 3688 ++$w->{frame};
3566 $w->update_face; 3689 $w->update_face;
3567 3690
3568 # somehow, $widget can go away 3691 # somehow, $widget can go away
3569 $w->update; 3692 $w->update;
3570 $w->update_timer; 3693 $w->update_timer;
3571 },
3572 ); 3694 };
3573 3695
3574 $self->update_face; 3696 $self->update_face;
3575 $self->update_timer; 3697 $self->update_timer;
3576 } 3698 }
3577 3699
3582 my ($self) = @_; 3704 my ($self) = @_;
3583 3705
3584 return unless $self->{timer}; 3706 return unless $self->{timer};
3585 3707
3586 if ($self->{visible}) { 3708 if ($self->{visible}) {
3587 $self->{timer}->at (
3588 $self->{anim_start}
3589 + $self->{animspeed}
3590 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3591 );
3592 $self->{timer}->start; 3709 $self->{timer}->start;
3593 } else { 3710 } else {
3594 $self->{timer}->stop; 3711 $self->{timer}->stop;
3595 } 3712 }
3596} 3713}
3850 3967
3851############################################################################# 3968#############################################################################
3852 3969
3853package CFPlus::UI::Notebook; 3970package CFPlus::UI::Notebook;
3854 3971
3972use CFPlus::OpenGL;
3973
3855our @ISA = CFPlus::UI::VBox::; 3974our @ISA = CFPlus::UI::VBox::;
3856 3975
3857sub new { 3976sub new {
3858 my $class = shift; 3977 my $class = shift;
3859 3978
3860 my $self = $class->SUPER::new ( 3979 my $self = $class->SUPER::new (
3861 buttonbar => (new CFPlus::UI::Buttonbar), 3980 buttonbar => (new CFPlus::UI::Buttonbar),
3862 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3981 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3982 active_outline => [.7, .7, 0.2],
3863 # filter => # will be put between multiplexer and $self 3983 # filter => # will be put between multiplexer and $self
3864 @_, 3984 @_,
3865 ); 3985 );
3866 3986
3867 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3987 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3941 4061
3942 $self->{multiplexer}->set_current_page ($page); 4062 $self->{multiplexer}->set_current_page ($page);
3943 $self->emit (page_changed => $self->{multiplexer}{current}); 4063 $self->emit (page_changed => $self->{multiplexer}{current});
3944} 4064}
3945 4065
4066sub _draw {
4067 my ($self) = @_;
4068
4069 $self->SUPER::_draw ();
4070
4071 if (my $cur = $self->{multiplexer}{current}) {
4072 if ($cur = $cur->{c_tab_}) {
4073 glTranslate $self->{buttonbar}{x} + $cur->{x},
4074 $self->{buttonbar}{y} + $cur->{y};
4075 glLineWidth 3;
4076 #glEnable GL_BLEND;
4077 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4078 glColor @{$self->{active_outline}};
4079 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4080 glLineWidth 1;
4081 #glDisable GL_BLEND;
4082 }
4083 }
4084}
4085
3946############################################################################# 4086#############################################################################
3947 4087
3948package CFPlus::UI::Selector; 4088package CFPlus::UI::Selector;
3949 4089
3950use utf8; 4090use utf8;
3980} 4120}
3981 4121
3982sub _set_value { 4122sub _set_value {
3983 my ($self, $value) = @_; 4123 my ($self, $value) = @_;
3984 4124
3985 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4125 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4126 $item ||= $self->{options}[0]
3986 or return; 4127 or return;
3987 4128
3988 $self->{value} = $item->[0]; 4129 $self->{value} = $item->[0];
3989 $self->set_markup ("$item->[1] ⇓"); 4130 $self->set_markup ("$item->[1] ⇓");
3990 $self->set_tooltip ($item->[2]); 4131# $self->set_tooltip ($item->[2]);
3991} 4132}
3992 4133
3993sub set_value { 4134sub set_value {
3994 my ($self, $value) = @_; 4135 my ($self, $value) = @_;
3995 4136
3996 return unless $self->{value} ne $value; 4137 return unless $self->{value} ne $value;
3997 4138
3998 $self->_set_value ($value); 4139 $self->_set_value ($value);
3999 $self->emit (changed => $value); 4140 $self->emit (changed => $value);
4141}
4142
4143sub set_options {
4144 my ($self, $options) = @_;
4145
4146 $self->{options} = $options;
4147 $self->_set_value ($self->{value});
4000} 4148}
4001 4149
4002############################################################################# 4150#############################################################################
4003 4151
4004package CFPlus::UI::Statusbox; 4152package CFPlus::UI::Statusbox;
4013 @_, 4161 @_,
4014 ); 4162 );
4015 4163
4016 CFPlus::weaken (my $this = $self); 4164 CFPlus::weaken (my $this = $self);
4017 4165
4018 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4166 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4019 4167
4020 $self 4168 $self
4021} 4169}
4022 4170
4023sub reorder { 4171sub reorder {
4031 4179
4032 while (my ($k, $v) = each %{ $self->{item} }) { 4180 while (my ($k, $v) = each %{ $self->{item} }) {
4033 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4181 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4034 } 4182 }
4035 4183
4184 $self->{timer}->set (1, 1);
4185
4036 my @widgets; 4186 my @widgets;
4037 4187
4038 my @items = sort { 4188 my @items = sort {
4039 $a->{pri} <=> $b->{pri} 4189 $a->{pri} <=> $b->{pri}
4040 or $b->{id} <=> $a->{id} 4190 or $b->{id} <=> $a->{id}
4041 } values %{ $self->{item} }; 4191 } values %{ $self->{item} };
4042
4043 $self->{timer}->interval (1);
4044 4192
4045 my $count = 10 + 1; 4193 my $count = 10 + 1;
4046 for my $item (@items) { 4194 for my $item (@items) {
4047 last unless --$count; 4195 last unless --$count;
4048 4196
4072 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4220 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4073 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4221 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4074 $label->update; 4222 $label->update;
4075 $label->set_max_size (undef, $label->{req_h} * $diff) 4223 $label->set_max_size (undef, $label->{req_h} * $diff)
4076 if $diff < 1; 4224 if $diff < 1;
4077 $self->{timer}->interval (1/30); 4225 $self->{timer}->set (1/30, 1/30);
4078 } else { 4226 } else {
4079 $label->{fg}[3] = $item->{fg}[3] || 1; 4227 $label->{fg}[3] = $item->{fg}[3] || 1;
4080 } 4228 }
4081 4229
4082 push @widgets, $label; 4230 push @widgets, $label;
4218} 4366}
4219 4367
4220sub update { 4368sub update {
4221 my ($self) = @_; 4369 my ($self) = @_;
4222 4370
4223 $::WANT_REFRESH->start; 4371 $::WANT_REFRESH = 1;
4224} 4372}
4225 4373
4226sub add { 4374sub add {
4227 my ($self, @children) = @_; 4375 my ($self, @children) = @_;
4228 4376

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines