… | |
… | |
2 | |
2 | |
3 | use utf8; |
3 | use utf8; |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use List::Util (); |
6 | use List::Util (); |
7 | use Event; |
|
|
8 | |
7 | |
9 | use CFPlus; |
8 | use CFPlus; |
10 | use CFPlus::Pod; |
9 | use CFPlus::Pod; |
11 | use CFPlus::Texture; |
10 | use CFPlus::Texture; |
12 | |
11 | |
… | |
… | |
17 | our $TOOLTIP; |
16 | our $TOOLTIP; |
18 | our $BUTTON_STATE; |
17 | our $BUTTON_STATE; |
19 | |
18 | |
20 | our %WIDGET; # all widgets, weak-referenced |
19 | our %WIDGET; # all widgets, weak-referenced |
21 | |
20 | |
22 | our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { |
21 | our $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 | |
53 | sub get_layout { |
50 | sub 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 | |
100 | sub feed_sdl_button_down_event { |
97 | sub 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 | |
151 | sub feed_sdl_motion_event { |
148 | sub 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 | |
277 | sub TO_JSON { |
274 | sub TO_JSON { |
278 | { __w_ => $_[0]{s_id} } |
275 | { "\fw" => $_[0]{s_id} } |
279 | } |
276 | } |
280 | |
277 | |
281 | sub show { |
278 | sub 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 |
|
|
367 | sub 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 | |
369 | sub size_request { |
376 | sub 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 |
443 | sub coord2local { |
450 | sub coord2local { |
… | |
… | |
607 | sub DESTROY { |
614 | sub 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 | } |
… | |
… | |
624 | use strict; |
632 | use strict; |
625 | use CFPlus::OpenGL; |
633 | use CFPlus::OpenGL; |
626 | |
634 | |
627 | sub new { |
635 | sub 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 | |
837 | package CFPlus::UI::Window; |
835 | package CFPlus::UI::Window; |
838 | |
836 | |
839 | our @ISA = CFPlus::UI::Bin::; |
837 | our @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 | |
1398 | sub border { |
1389 | sub border { |
1399 | int $_[0]{border} * $::FONTSIZE |
1390 | int $_[0]{border} * $::FONTSIZE |
|
|
1391 | } |
|
|
1392 | |
|
|
1393 | sub 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 | |
1402 | sub size_request { |
1402 | sub size_request { |
1403 | my ($self) = @_; |
1403 | my ($self) = @_; |
1404 | |
1404 | |
… | |
… | |
1961 | |
1961 | |
1962 | sub size_request { |
1962 | sub size_request { |
1963 | my ($self) = @_; |
1963 | my ($self) = @_; |
1964 | |
1964 | |
1965 | $self->{size_req} ||= do { |
1965 | $self->{size_req} ||= do { |
|
|
1966 | my ($max_w, $max_h) = $self->get_max_wh; |
|
|
1967 | |
1966 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1968 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1967 | $self->{layout}->set_width ($self->{max_w} || -1); |
1969 | $self->{layout}->set_width ($self->{max_w} || $max_w || -1); |
1968 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1970 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1969 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1971 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1970 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1972 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1971 | |
1973 | |
1972 | my ($w, $h) = $self->{layout}->size; |
1974 | my ($w, $h) = $self->{layout}->size; |
… | |
… | |
2235 | glColor_premultiply @{$self->{bg}}; |
2237 | glColor_premultiply @{$self->{bg}}; |
2236 | } |
2238 | } |
2237 | |
2239 | |
2238 | glEnable GL_BLEND; |
2240 | glEnable GL_BLEND; |
2239 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2241 | 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}; |
2242 | glRect 0, 0, $self->{w}, $self->{h}; |
2244 | glVertex $self->{w}, 0; |
|
|
2245 | glEnd; |
|
|
2246 | glDisable GL_BLEND; |
2243 | glDisable GL_BLEND; |
2247 | |
2244 | |
2248 | $self->SUPER::_draw; |
2245 | $self->SUPER::_draw; |
2249 | |
2246 | |
2250 | #TODO: force update every cursor change :( |
2247 | #TODO: force update every cursor change :( |
… | |
… | |
2257 | @$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); |
2258 | } |
2255 | } |
2259 | |
2256 | |
2260 | glColor_premultiply @{$self->{active_fg}}; |
2257 | glColor_premultiply @{$self->{active_fg}}; |
2261 | glBegin GL_LINES; |
2258 | glBegin GL_LINES; |
2262 | 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}; |
2263 | 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}; |
2264 | glEnd; |
2261 | glEnd; |
2265 | |
2262 | |
2266 | glLineWidth 3; |
2263 | glLineWidth 3; |
2267 | glColor @{$self->{active_outline}}; |
2264 | 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; |
2265 | 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; |
2266 | glLineWidth 1; |
2275 | |
2267 | |
2276 | } else { |
2268 | } else { |
2277 | glColor @{$self->{outline}}; |
2269 | glColor @{$self->{outline}}; |
2278 | glTranslate .375, .375; |
|
|
2279 | glBegin GL_LINE_STRIP; |
2270 | glBegin GL_LINE_STRIP; |
2280 | glVertex 0, $self->{h} * .5; |
2271 | glVertex .5, $self->{h} * .5; |
2281 | glVertex 0, $self->{h} - 3; |
2272 | glVertex .5, $self->{h} - 2.5; |
2282 | glVertex $self->{w} - 1, $self->{h} - 3; |
2273 | glVertex $self->{w} - .5, $self->{h} - 2.5; |
2283 | glVertex $self->{w} - 1, $self->{h} * .5; |
2274 | glVertex $self->{w} - .5, $self->{h} * .5; |
2284 | glEnd; |
2275 | glEnd; |
2285 | } |
2276 | } |
2286 | } |
2277 | } |
2287 | |
2278 | |
2288 | ############################################################################# |
2279 | ############################################################################# |
… | |
… | |
2537 | sub _draw { |
2528 | sub _draw { |
2538 | my ($self) = @_; |
2529 | my ($self) = @_; |
2539 | |
2530 | |
2540 | $self->SUPER::_draw; |
2531 | $self->SUPER::_draw; |
2541 | |
2532 | |
2542 | glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; |
2533 | glTranslate $self->{padding_x}, $self->{padding_y}, 0; |
2543 | |
2534 | |
2544 | my ($w, $h) = @$self{qw(w h)}; |
2535 | my ($w, $h) = @$self{qw(w h)}; |
2545 | |
2536 | |
2546 | 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; |
2547 | |
2538 | |
… | |
… | |
2806 | glDisable GL_TEXTURE_2D; |
2797 | glDisable GL_TEXTURE_2D; |
2807 | } |
2798 | } |
2808 | |
2799 | |
2809 | ############################################################################# |
2800 | ############################################################################# |
2810 | |
2801 | |
|
|
2802 | package CFPlus::UI::Progress; |
|
|
2803 | |
|
|
2804 | our @ISA = CFPlus::UI::Label::; |
|
|
2805 | |
|
|
2806 | use CFPlus::OpenGL; |
|
|
2807 | |
|
|
2808 | sub 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 | |
|
|
2830 | sub 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 | |
|
|
2839 | sub 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 | |
|
|
2855 | sub _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 | |
|
|
2883 | package CFPlus::UI::ExperienceProgress; |
|
|
2884 | |
|
|
2885 | our @ISA = CFPlus::UI::Progress::; |
|
|
2886 | |
|
|
2887 | sub 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 | |
|
|
2909 | sub DESTROY { |
|
|
2910 | my ($self) = @_; |
|
|
2911 | |
|
|
2912 | delete $::CONN->{on_exp_update}{$self+0} |
|
|
2913 | if $::CONN; |
|
|
2914 | |
|
|
2915 | $self->SUPER::DESTROY; |
|
|
2916 | } |
|
|
2917 | |
|
|
2918 | sub 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 | |
2811 | package CFPlus::UI::Gauge; |
2940 | package CFPlus::UI::Gauge; |
2812 | |
2941 | |
2813 | our @ISA = CFPlus::UI::VBox::; |
2942 | our @ISA = CFPlus::UI::VBox::; |
2814 | |
2943 | |
2815 | sub new { |
2944 | sub new { |
… | |
… | |
3444 | } |
3573 | } |
3445 | |
3574 | |
3446 | sub set_tooltip_from { |
3575 | sub set_tooltip_from { |
3447 | my ($self, $widget) = @_; |
3576 | my ($self, $widget) = @_; |
3448 | |
3577 | |
3449 | $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1 |
|
|
3450 | if $widget->{tooltip} =~ /^#(.*)$/; |
|
|
3451 | |
|
|
3452 | 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 =~ /^#(.*)$/; |
3453 | |
3583 | |
3454 | if ($ENV{CFPLUS_DEBUG} & 2) { |
3584 | if ($ENV{CFPLUS_DEBUG} & 2) { |
3455 | $tooltip .= "\n\n" . (ref $widget) . "\n" |
3585 | $tip .= "\n\n" . (ref $widget) . "\n" |
3456 | . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" |
3586 | . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" |
3457 | . "req $widget->{req_w} $widget->{req_h}\n" |
3587 | . "req $widget->{req_w} $widget->{req_h}\n" |
3458 | . "visible $widget->{visible}"; |
3588 | . "visible $widget->{visible}"; |
3459 | } |
3589 | } |
3460 | |
3590 | |
3461 | $tooltip =~ s/^\n+//; |
3591 | $tip =~ s/^\n+//; |
3462 | $tooltip =~ s/\n+$//; |
3592 | $tip =~ s/\n+$//; |
3463 | |
3593 | |
3464 | $self->add (new CFPlus::UI::Label |
3594 | $self->add (new CFPlus::UI::Label |
3465 | markup => $tooltip, |
3595 | markup => $tip, |
3466 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
3596 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
3467 | fontsize => 0.8, |
3597 | fontsize => 0.8, |
3468 | style => 1, # FLAG_INVERSE |
3598 | style => 1, # FLAG_INVERSE |
3469 | ellipsise => 0, |
3599 | ellipsise => 0, |
3470 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
3600 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
… | |
… | |
3508 | } |
3638 | } |
3509 | |
3639 | |
3510 | sub _draw { |
3640 | sub _draw { |
3511 | my ($self) = @_; |
3641 | my ($self) = @_; |
3512 | |
3642 | |
3513 | glTranslate 0.375, 0.375; |
|
|
3514 | |
|
|
3515 | my ($w, $h) = @$self{qw(w h)}; |
3643 | my ($w, $h) = @$self{qw(w h)}; |
3516 | |
3644 | |
3517 | glColor 1, 0.8, 0.4; |
3645 | glColor 1, 0.8, 0.4; |
3518 | glRect 0, 0, $w, $h; |
3646 | glRect 0, 0, $w, $h; |
3519 | |
3647 | |
3520 | glColor 0, 0, 0; |
3648 | glColor 0, 0, 0; |
3521 | glBegin GL_LINE_LOOP; |
3649 | 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 | |
3650 | |
3528 | glTranslate 2 - 0.375, 2 - 0.375; |
3651 | glTranslate 2, 2; |
3529 | |
3652 | |
3530 | $self->SUPER::_draw; |
3653 | $self->SUPER::_draw; |
3531 | } |
3654 | } |
3532 | |
3655 | |
3533 | ############################################################################# |
3656 | ############################################################################# |
… | |
… | |
3551 | |
3674 | |
3552 | if ($self->{anim} && $self->{animspeed}) { |
3675 | if ($self->{anim} && $self->{animspeed}) { |
3553 | CFPlus::weaken (my $widget = $self); |
3676 | CFPlus::weaken (my $widget = $self); |
3554 | |
3677 | |
3555 | $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; |
3678 | $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; |
3556 | $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; |
3679 | $widget->{anim_start} = $self->{animspeed} * int EV::now / $self->{animspeed}; |
3557 | $self->{timer} = Event->timer ( |
3680 | $self->{timer} = EV::timer_ns 0, 0, sub { |
3558 | parked => 1, |
|
|
3559 | cb => sub { |
|
|
3560 | return unless $::CONN; |
3681 | return unless $::CONN; |
3561 | |
3682 | |
3562 | my $w = $widget |
3683 | my $w = $widget |
3563 | or return; |
3684 | or return; |
3564 | |
3685 | |
3565 | ++$w->{frame}; |
3686 | ++$w->{frame}; |
3566 | $w->update_face; |
3687 | $w->update_face; |
3567 | |
3688 | |
3568 | # somehow, $widget can go away |
3689 | # somehow, $widget can go away |
3569 | $w->update; |
3690 | $w->update; |
3570 | $w->update_timer; |
3691 | $w->update_timer; |
3571 | }, |
|
|
3572 | ); |
3692 | }; |
3573 | |
3693 | |
3574 | $self->update_face; |
3694 | $self->update_face; |
3575 | $self->update_timer; |
3695 | $self->update_timer; |
3576 | } |
3696 | } |
3577 | |
3697 | |
… | |
… | |
3582 | my ($self) = @_; |
3702 | my ($self) = @_; |
3583 | |
3703 | |
3584 | return unless $self->{timer}; |
3704 | return unless $self->{timer}; |
3585 | |
3705 | |
3586 | if ($self->{visible}) { |
3706 | if ($self->{visible}) { |
3587 | $self->{timer}->at ( |
3707 | $self->{timer}->set ( |
3588 | $self->{anim_start} |
3708 | $self->{anim_start} |
3589 | + $self->{animspeed} |
3709 | + $self->{animspeed} |
3590 | * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed} |
3710 | * int 1.5 + (EV::now - $self->{anim_start}) / $self->{animspeed} |
3591 | ); |
3711 | ); |
3592 | $self->{timer}->start; |
3712 | $self->{timer}->start; |
3593 | } else { |
3713 | } else { |
3594 | $self->{timer}->stop; |
3714 | $self->{timer}->stop; |
3595 | } |
3715 | } |
3596 | } |
3716 | } |
3597 | |
3717 | |
3598 | sub update_face { |
3718 | sub update_face { |
3599 | my ($self) = @_; |
3719 | my ($self) = @_; |
3600 | |
3720 | |
3601 | return unless $::CONN; |
3721 | if ($::CONN) { |
3602 | |
|
|
3603 | if (my $anim = $::CONN->{anim}[$self->{anim}]) { |
3722 | if (my $anim = $::CONN->{anim}[$self->{anim}]) { |
3604 | if ($anim && @$anim) { |
3723 | if ($anim && @$anim) { |
3605 | delete $self->{wait_face}; |
|
|
3606 | $self->{face} = $anim->[ $self->{frame} % @$anim ]; |
3724 | $self->{face} = $anim->[ $self->{frame} % @$anim ]; |
|
|
3725 | delete $self->{face_change_cb}; |
|
|
3726 | |
3607 | if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ]) { |
3727 | if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) { |
3608 | unless ($tex->{name} || $tex->{loading}) { |
3728 | unless ($tex->{name} || $tex->{loading}) { |
3609 | $tex->upload (sub { $self->reconfigure }); |
3729 | $tex->upload (sub { $self->reconfigure }); |
|
|
3730 | } |
3610 | } |
3731 | } |
3611 | } |
3732 | } |
3612 | } |
3733 | } |
3613 | } |
3734 | } |
3614 | } |
3735 | } |
3615 | |
3736 | |
3616 | sub size_request { |
3737 | sub size_request { |
3617 | my ($self) = @_; |
3738 | my ($self) = @_; |
3618 | |
3739 | |
3619 | if ($::CONN) { |
3740 | if ($::CONN) { |
3620 | if (my $faceid = $::CONN->{faceid}[$self->{face}]) { |
3741 | if (my $faceid = $::CONN->{face}[$self->{face}]{id}) { |
3621 | if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) { |
3742 | if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) { |
3622 | if ($tex->{name}) { |
3743 | if ($tex->{name}) { |
3623 | return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); |
3744 | return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); |
3624 | } elsif (!$tex->{loading}) { |
3745 | } elsif (!$tex->{loading}) { |
3625 | $tex->upload (sub { $self->reconfigure }); |
3746 | $tex->upload (sub { $self->reconfigure }); |
3626 | } |
3747 | } |
3627 | } else { |
|
|
3628 | $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { |
|
|
3629 | $self->reconfigure; |
|
|
3630 | }); |
|
|
3631 | } |
3748 | } |
|
|
3749 | |
|
|
3750 | $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure }); |
3632 | } |
3751 | } |
3633 | } |
3752 | } |
3634 | |
3753 | |
3635 | ($self->{size_w} || 8, $self->{size_h} || 8) |
3754 | ($self->{size_w} || 8, $self->{size_h} || 8) |
3636 | } |
3755 | } |
… | |
… | |
3851 | |
3970 | |
3852 | ############################################################################# |
3971 | ############################################################################# |
3853 | |
3972 | |
3854 | package CFPlus::UI::Notebook; |
3973 | package CFPlus::UI::Notebook; |
3855 | |
3974 | |
|
|
3975 | use CFPlus::OpenGL; |
|
|
3976 | |
3856 | our @ISA = CFPlus::UI::VBox::; |
3977 | our @ISA = CFPlus::UI::VBox::; |
3857 | |
3978 | |
3858 | sub new { |
3979 | sub new { |
3859 | my $class = shift; |
3980 | my $class = shift; |
3860 | |
3981 | |
3861 | my $self = $class->SUPER::new ( |
3982 | my $self = $class->SUPER::new ( |
3862 | buttonbar => (new CFPlus::UI::Buttonbar), |
3983 | buttonbar => (new CFPlus::UI::Buttonbar), |
3863 | multiplexer => (new CFPlus::UI::Multiplexer expand => 1), |
3984 | multiplexer => (new CFPlus::UI::Multiplexer expand => 1), |
|
|
3985 | active_outline => [.7, .7, 0.2], |
3864 | # filter => # will be put between multiplexer and $self |
3986 | # filter => # will be put between multiplexer and $self |
3865 | @_, |
3987 | @_, |
3866 | ); |
3988 | ); |
3867 | |
3989 | |
3868 | $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; |
3990 | $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; |
… | |
… | |
3942 | |
4064 | |
3943 | $self->{multiplexer}->set_current_page ($page); |
4065 | $self->{multiplexer}->set_current_page ($page); |
3944 | $self->emit (page_changed => $self->{multiplexer}{current}); |
4066 | $self->emit (page_changed => $self->{multiplexer}{current}); |
3945 | } |
4067 | } |
3946 | |
4068 | |
|
|
4069 | sub _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 | } |
|
|
4087 | } |
|
|
4088 | |
3947 | ############################################################################# |
4089 | ############################################################################# |
3948 | |
4090 | |
3949 | package CFPlus::UI::Selector; |
4091 | package CFPlus::UI::Selector; |
3950 | |
4092 | |
3951 | use utf8; |
4093 | use utf8; |
… | |
… | |
3981 | } |
4123 | } |
3982 | |
4124 | |
3983 | sub _set_value { |
4125 | sub _set_value { |
3984 | my ($self, $value) = @_; |
4126 | my ($self, $value) = @_; |
3985 | |
4127 | |
3986 | my ($item) = grep $_->[0] eq $value, @{ $self->{options} } |
4128 | my ($item) = grep $_->[0] eq $value, @{ $self->{options} }; |
|
|
4129 | $item ||= $self->{options}[0] |
3987 | or return; |
4130 | or return; |
3988 | |
4131 | |
3989 | $self->{value} = $item->[0]; |
4132 | $self->{value} = $item->[0]; |
3990 | $self->set_markup ("$item->[1] ⇓"); |
4133 | $self->set_markup ("$item->[1] ⇓"); |
3991 | $self->set_tooltip ($item->[2]); |
4134 | # $self->set_tooltip ($item->[2]); |
3992 | } |
4135 | } |
3993 | |
4136 | |
3994 | sub set_value { |
4137 | sub set_value { |
3995 | my ($self, $value) = @_; |
4138 | my ($self, $value) = @_; |
3996 | |
4139 | |
3997 | return unless $self->{value} ne $value; |
4140 | return unless $self->{value} ne $value; |
3998 | |
4141 | |
3999 | $self->_set_value ($value); |
4142 | $self->_set_value ($value); |
4000 | $self->emit (changed => $value); |
4143 | $self->emit (changed => $value); |
|
|
4144 | } |
|
|
4145 | |
|
|
4146 | sub set_options { |
|
|
4147 | my ($self, $options) = @_; |
|
|
4148 | |
|
|
4149 | $self->{options} = $options; |
|
|
4150 | $self->_set_value ($self->{value}); |
4001 | } |
4151 | } |
4002 | |
4152 | |
4003 | ############################################################################# |
4153 | ############################################################################# |
4004 | |
4154 | |
4005 | package CFPlus::UI::Statusbox; |
4155 | package CFPlus::UI::Statusbox; |
… | |
… | |
4014 | @_, |
4164 | @_, |
4015 | ); |
4165 | ); |
4016 | |
4166 | |
4017 | CFPlus::weaken (my $this = $self); |
4167 | CFPlus::weaken (my $this = $self); |
4018 | |
4168 | |
4019 | $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); |
4169 | $self->{timer} = EV::timer 1, 1, sub { $this->reorder }; |
4020 | |
4170 | |
4021 | $self |
4171 | $self |
4022 | } |
4172 | } |
4023 | |
4173 | |
4024 | sub reorder { |
4174 | sub reorder { |
… | |
… | |
4032 | |
4182 | |
4033 | while (my ($k, $v) = each %{ $self->{item} }) { |
4183 | while (my ($k, $v) = each %{ $self->{item} }) { |
4034 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
4184 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
4035 | } |
4185 | } |
4036 | |
4186 | |
|
|
4187 | $self->{timer}->set (1, 1); |
|
|
4188 | |
4037 | my @widgets; |
4189 | my @widgets; |
4038 | |
4190 | |
4039 | my @items = sort { |
4191 | my @items = sort { |
4040 | $a->{pri} <=> $b->{pri} |
4192 | $a->{pri} <=> $b->{pri} |
4041 | or $b->{id} <=> $a->{id} |
4193 | or $b->{id} <=> $a->{id} |
4042 | } values %{ $self->{item} }; |
4194 | } values %{ $self->{item} }; |
4043 | |
|
|
4044 | $self->{timer}->interval (1); |
|
|
4045 | |
4195 | |
4046 | my $count = 10 + 1; |
4196 | my $count = 10 + 1; |
4047 | for my $item (@items) { |
4197 | for my $item (@items) { |
4048 | last unless --$count; |
4198 | last unless --$count; |
4049 | |
4199 | |
… | |
… | |
4073 | if ((my $diff = $item->{timeout} - $NOW) < 2) { |
4223 | if ((my $diff = $item->{timeout} - $NOW) < 2) { |
4074 | $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; |
4224 | $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; |
4075 | $label->update; |
4225 | $label->update; |
4076 | $label->set_max_size (undef, $label->{req_h} * $diff) |
4226 | $label->set_max_size (undef, $label->{req_h} * $diff) |
4077 | if $diff < 1; |
4227 | if $diff < 1; |
4078 | $self->{timer}->interval (1/30); |
4228 | $self->{timer}->set (1/30, 1/30); |
4079 | } else { |
4229 | } else { |
4080 | $label->{fg}[3] = $item->{fg}[3] || 1; |
4230 | $label->{fg}[3] = $item->{fg}[3] || 1; |
4081 | } |
4231 | } |
4082 | |
4232 | |
4083 | push @widgets, $label; |
4233 | push @widgets, $label; |