1 | package CFPlus::UI; |
1 | package CFPlus::UI; |
2 | |
2 | |
3 | use utf8; |
3 | use utf8; |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use Scalar::Util (); |
|
|
7 | use List::Util (); |
6 | use List::Util (); |
8 | use Event; |
7 | use Event; |
9 | |
8 | |
10 | use CFPlus; |
9 | use CFPlus; |
11 | use CFPlus::Pod; |
10 | use CFPlus::Pod; |
… | |
… | |
23 | our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { |
22 | our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { |
24 | if (!$GRAB) { |
23 | if (!$GRAB) { |
25 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
24 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
26 | if (length $widget->{tooltip}) { |
25 | if (length $widget->{tooltip}) { |
27 | if ($TOOLTIP->{owner} != $widget) { |
26 | if ($TOOLTIP->{owner} != $widget) { |
|
|
27 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
28 | $TOOLTIP->hide; |
28 | $TOOLTIP->hide; |
29 | |
29 | |
30 | $TOOLTIP->{owner} = $widget; |
30 | $TOOLTIP->{owner} = $widget; |
|
|
31 | $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; |
31 | |
32 | |
32 | return if $ENV{CFPLUS_DEBUG} & 8; |
33 | return if $ENV{CFPLUS_DEBUG} & 8; |
33 | |
34 | |
34 | my $tip = $widget->{tooltip}; |
35 | my $tip = $widget->{tooltip}; |
35 | |
36 | |
… | |
… | |
43 | } |
44 | } |
44 | } |
45 | } |
45 | } |
46 | } |
46 | |
47 | |
47 | $TOOLTIP->hide; |
48 | $TOOLTIP->hide; |
|
|
49 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
48 | delete $TOOLTIP->{owner}; |
50 | delete $TOOLTIP->{owner}; |
49 | }); |
51 | }); |
50 | |
52 | |
51 | sub get_layout { |
53 | sub get_layout { |
52 | my $layout; |
54 | my $layout; |
… | |
… | |
235 | h => undef, |
237 | h => undef, |
236 | can_events => 1, |
238 | can_events => 1, |
237 | @_ |
239 | @_ |
238 | }, $class; |
240 | }, $class; |
239 | |
241 | |
240 | Scalar::Util::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); |
242 | CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); |
241 | |
243 | |
242 | for (keys %$self) { |
244 | for (keys %$self) { |
243 | if (/^on_(.*)$/) { |
245 | if (/^on_(.*)$/) { |
244 | $self->connect ($1 => delete $self->{$_}); |
246 | $self->connect ($1 => delete $self->{$_}); |
245 | } |
247 | } |
… | |
… | |
530 | ? $args[0]->xy ($self) |
532 | ? $args[0]->xy ($self) |
531 | : (); |
533 | : (); |
532 | |
534 | |
533 | #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# |
535 | #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# |
534 | |
536 | |
535 | #d##TODO# stop propagating at first true, do not use sum |
537 | for my $cb ( |
536 | (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before |
538 | @{$self->{signal_cb}{$signal} || []}, # before |
537 | || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure |
539 | ($self->can ("invoke_$signal") || sub { 1 }), # closure |
|
|
540 | ) { |
|
|
541 | return $cb->($self, @args, @append) || next; |
|
|
542 | } |
|
|
543 | |
|
|
544 | # parent |
538 | || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent |
545 | $self->{parent} && $self->{parent}->emit ($signal, @args) |
539 | } |
546 | } |
540 | |
547 | |
541 | sub find_widget { |
548 | sub find_widget { |
542 | my ($self, $x, $y) = @_; |
549 | my ($self, $x, $y) = @_; |
543 | |
550 | |
… | |
… | |
551 | } |
558 | } |
552 | |
559 | |
553 | sub set_parent { |
560 | sub set_parent { |
554 | my ($self, $parent) = @_; |
561 | my ($self, $parent) = @_; |
555 | |
562 | |
556 | Scalar::Util::weaken ($self->{parent} = $parent); |
563 | CFPlus::weaken ($self->{parent} = $parent); |
557 | $self->set_visible if $parent->{visible}; |
564 | $self->set_visible if $parent->{visible}; |
558 | } |
565 | } |
559 | |
566 | |
560 | sub realloc { |
567 | sub realloc { |
561 | my ($self) = @_; |
568 | my ($self) = @_; |
… | |
… | |
640 | my ($self) = @_; |
647 | my ($self) = @_; |
641 | |
648 | |
642 | warn "no draw defined for $self\n"; |
649 | warn "no draw defined for $self\n"; |
643 | } |
650 | } |
644 | |
651 | |
|
|
652 | my $cntx;#d# |
645 | sub DESTROY { |
653 | sub DESTROY { |
646 | my ($self) = @_; |
654 | my ($self) = @_; |
647 | |
655 | |
648 | return if CFPlus::in_destruct; |
656 | return if CFPlus::in_destruct; |
649 | |
657 | |
… | |
… | |
1971 | } elsif ($sym == CFPlus::SDLK_LEFT) { |
1979 | } elsif ($sym == CFPlus::SDLK_LEFT) { |
1972 | --$self->{cursor} if $self->{cursor}; |
1980 | --$self->{cursor} if $self->{cursor}; |
1973 | } elsif ($sym == CFPlus::SDLK_RIGHT) { |
1981 | } elsif ($sym == CFPlus::SDLK_RIGHT) { |
1974 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1982 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1975 | } elsif ($sym == CFPlus::SDLK_HOME) { |
1983 | } elsif ($sym == CFPlus::SDLK_HOME) { |
|
|
1984 | # what a hack |
1976 | $self->{cursor} = 0; |
1985 | $self->{cursor} = |
|
|
1986 | (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ |
|
|
1987 | ? length $1 |
|
|
1988 | : 0; |
1977 | } elsif ($sym == CFPlus::SDLK_END) { |
1989 | } elsif ($sym == CFPlus::SDLK_END) { |
|
|
1990 | # uh, again |
|
|
1991 | $self->{cursor} = |
|
|
1992 | (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ |
1978 | $self->{cursor} = length $text; |
1993 | ? $self->{cursor} + length $1 |
|
|
1994 | : length $self->{text}; |
1979 | } elsif ($uni == 21) { # ctrl-u |
1995 | } elsif ($uni == 21) { # ctrl-u |
1980 | $text = ""; |
1996 | $text = ""; |
1981 | $self->{cursor} = 0; |
1997 | $self->{cursor} = 0; |
1982 | } elsif ($uni == 27) { |
1998 | } elsif ($uni == 27) { |
1983 | $self->emit ('escape'); |
1999 | $self->emit ('escape'); |
1984 | } elsif ($uni >= 0x20 || $uni == 0x0d) { |
2000 | } elsif ($uni == 0x0d) { |
|
|
2001 | substr $text, $self->{cursor}++, 0, "\012"; |
|
|
2002 | } elsif ($uni >= 0x20) { |
1985 | substr $text, $self->{cursor}++, 0, chr $uni; |
2003 | substr $text, $self->{cursor}++, 0, chr $uni; |
1986 | } else { |
2004 | } else { |
1987 | return 0; |
2005 | return 0; |
1988 | } |
2006 | } |
1989 | |
2007 | |
… | |
… | |
2310 | or Carp::croak "'path' or 'tex' attributes required"; |
2328 | or Carp::croak "'path' or 'tex' attributes required"; |
2311 | |
2329 | |
2312 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2330 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2313 | new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; |
2331 | new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; |
2314 | |
2332 | |
2315 | Scalar::Util::weaken $texture_cache{$self->{path}}; |
2333 | CFPlus::weaken $texture_cache{$self->{path}}; |
2316 | |
2334 | |
2317 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2335 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2318 | |
2336 | |
2319 | $self |
2337 | $self |
2320 | } |
2338 | } |
… | |
… | |
2693 | sub invoke_mouse_wheel { |
2711 | sub invoke_mouse_wheel { |
2694 | my ($self, $ev) = @_; |
2712 | my ($self, $ev) = @_; |
2695 | |
2713 | |
2696 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
2714 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
2697 | |
2715 | |
|
|
2716 | my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; |
|
|
2717 | |
2698 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2); |
2718 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); |
2699 | |
2719 | |
2700 | ! ! $delta |
2720 | ! ! $delta |
2701 | } |
2721 | } |
2702 | |
2722 | |
2703 | sub update { |
2723 | sub update { |
… | |
… | |
2813 | #font => default_font |
2833 | #font => default_font |
2814 | @_, |
2834 | @_, |
2815 | |
2835 | |
2816 | layout => (new CFPlus::Layout), |
2836 | layout => (new CFPlus::Layout), |
2817 | par => [], |
2837 | par => [], |
|
|
2838 | max_par => 0, |
2818 | height => 0, |
2839 | height => 0, |
2819 | children => [ |
2840 | children => [ |
2820 | (new CFPlus::UI::Empty expand => 1), |
2841 | (new CFPlus::UI::Empty expand => 1), |
2821 | (new CFPlus::UI::Slider vertical => 1), |
2842 | (new CFPlus::UI::Slider vertical => 1), |
2822 | ], |
2843 | ], |
… | |
… | |
2945 | wrapped => 1, |
2966 | wrapped => 1, |
2946 | }; |
2967 | }; |
2947 | |
2968 | |
2948 | $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; |
2969 | $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; |
2949 | push @{$self->{par}}, $para; |
2970 | push @{$self->{par}}, $para; |
|
|
2971 | } |
|
|
2972 | |
|
|
2973 | if (my $max = $self->{max_par}) { |
|
|
2974 | shift @{$self->{par}} while @{$self->{par}} > $max; |
2950 | } |
2975 | } |
2951 | |
2976 | |
2952 | $self->{need_reflow}++; |
2977 | $self->{need_reflow}++; |
2953 | $self->update; |
2978 | $self->update; |
2954 | } |
2979 | } |
… | |
… | |
3268 | can_events => 0, |
3293 | can_events => 0, |
3269 | @_, |
3294 | @_, |
3270 | ); |
3295 | ); |
3271 | |
3296 | |
3272 | if ($self->{anim} && $self->{animspeed}) { |
3297 | if ($self->{anim} && $self->{animspeed}) { |
3273 | Scalar::Util::weaken (my $widget = $self); |
3298 | CFPlus::weaken (my $widget = $self); |
3274 | |
3299 | |
3275 | $self->{timer} = Event->timer ( |
3300 | $self->{timer} = Event->timer ( |
3276 | at => $self->{animspeed} * int $::NOW / $self->{animspeed}, |
3301 | at => $self->{animspeed} * int $::NOW / $self->{animspeed}, |
3277 | hard => 1, |
3302 | hard => 1, |
3278 | interval => $self->{animspeed}, |
3303 | interval => $self->{animspeed}, |
… | |
… | |
3532 | } |
3557 | } |
3533 | |
3558 | |
3534 | sub add { |
3559 | sub add { |
3535 | my ($self, $title, $widget, $tooltip) = @_; |
3560 | my ($self, $title, $widget, $tooltip) = @_; |
3536 | |
3561 | |
3537 | Scalar::Util::weaken $self; |
3562 | CFPlus::weaken $self; |
3538 | |
3563 | |
3539 | $self->{buttonbar}->add (new CFPlus::UI::Button |
3564 | $self->{buttonbar}->add (new CFPlus::UI::Button |
3540 | markup => $title, |
3565 | markup => $title, |
3541 | tooltip => $tooltip, |
3566 | tooltip => $tooltip, |
3542 | on_activate => sub { $self->set_current_page ($widget) }, |
3567 | on_activate => sub { $self->set_current_page ($widget) }, |
… | |
… | |
3626 | my $self = $class->SUPER::new ( |
3651 | my $self = $class->SUPER::new ( |
3627 | fontsize => 0.8, |
3652 | fontsize => 0.8, |
3628 | @_, |
3653 | @_, |
3629 | ); |
3654 | ); |
3630 | |
3655 | |
3631 | Scalar::Util::weaken (my $this = $self); |
3656 | CFPlus::weaken (my $this = $self); |
3632 | |
3657 | |
3633 | $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); |
3658 | $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); |
3634 | |
3659 | |
3635 | $self |
3660 | $self |
3636 | } |
3661 | } |
… | |
… | |
3775 | my $self = $class->SUPER::new ( |
3800 | my $self = $class->SUPER::new ( |
3776 | visible => 1, |
3801 | visible => 1, |
3777 | @_, |
3802 | @_, |
3778 | ); |
3803 | ); |
3779 | |
3804 | |
3780 | Scalar::Util::weaken ($self->{root} = $self); |
3805 | CFPlus::weaken ($self->{root} = $self); |
3781 | |
3806 | |
3782 | $self |
3807 | $self |
3783 | } |
3808 | } |
3784 | |
3809 | |
3785 | sub size_request { |
3810 | sub size_request { |