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; |
… | |
… | |
235 | h => undef, |
234 | h => undef, |
236 | can_events => 1, |
235 | can_events => 1, |
237 | @_ |
236 | @_ |
238 | }, $class; |
237 | }, $class; |
239 | |
238 | |
240 | Scalar::Util::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); |
239 | CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); |
241 | |
240 | |
242 | for (keys %$self) { |
241 | for (keys %$self) { |
243 | if (/^on_(.*)$/) { |
242 | if (/^on_(.*)$/) { |
244 | $self->connect ($1 => delete $self->{$_}); |
243 | $self->connect ($1 => delete $self->{$_}); |
245 | } |
244 | } |
… | |
… | |
530 | ? $args[0]->xy ($self) |
529 | ? $args[0]->xy ($self) |
531 | : (); |
530 | : (); |
532 | |
531 | |
533 | #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# |
532 | #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# |
534 | |
533 | |
535 | #d##TODO# stop propagating at first true, do not use sum |
534 | for my $cb ( |
536 | (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before |
535 | @{$self->{signal_cb}{$signal} || []}, # before |
537 | || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure |
536 | ($self->can ("invoke_$signal") || sub { 1 }), # closure |
|
|
537 | ) { |
|
|
538 | return $cb->($self, @args, @append) || next; |
|
|
539 | } |
|
|
540 | |
|
|
541 | # parent |
538 | || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent |
542 | $self->{parent} && $self->{parent}->emit ($signal, @args) |
539 | } |
543 | } |
540 | |
544 | |
541 | sub find_widget { |
545 | sub find_widget { |
542 | my ($self, $x, $y) = @_; |
546 | my ($self, $x, $y) = @_; |
543 | |
547 | |
… | |
… | |
551 | } |
555 | } |
552 | |
556 | |
553 | sub set_parent { |
557 | sub set_parent { |
554 | my ($self, $parent) = @_; |
558 | my ($self, $parent) = @_; |
555 | |
559 | |
556 | Scalar::Util::weaken ($self->{parent} = $parent); |
560 | CFPlus::weaken ($self->{parent} = $parent); |
557 | $self->set_visible if $parent->{visible}; |
561 | $self->set_visible if $parent->{visible}; |
558 | } |
562 | } |
559 | |
563 | |
560 | sub realloc { |
564 | sub realloc { |
561 | my ($self) = @_; |
565 | my ($self) = @_; |
… | |
… | |
640 | my ($self) = @_; |
644 | my ($self) = @_; |
641 | |
645 | |
642 | warn "no draw defined for $self\n"; |
646 | warn "no draw defined for $self\n"; |
643 | } |
647 | } |
644 | |
648 | |
|
|
649 | my $cntx;#d# |
645 | sub DESTROY { |
650 | sub DESTROY { |
646 | my ($self) = @_; |
651 | my ($self) = @_; |
647 | |
652 | |
648 | return if CFPlus::in_destruct; |
653 | return if CFPlus::in_destruct; |
649 | |
654 | |
… | |
… | |
2310 | or Carp::croak "'path' or 'tex' attributes required"; |
2315 | or Carp::croak "'path' or 'tex' attributes required"; |
2311 | |
2316 | |
2312 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2317 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2313 | new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; |
2318 | new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; |
2314 | |
2319 | |
2315 | Scalar::Util::weaken $texture_cache{$self->{path}}; |
2320 | CFPlus::weaken $texture_cache{$self->{path}}; |
2316 | |
2321 | |
2317 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2322 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2318 | |
2323 | |
2319 | $self |
2324 | $self |
2320 | } |
2325 | } |
… | |
… | |
3268 | can_events => 0, |
3273 | can_events => 0, |
3269 | @_, |
3274 | @_, |
3270 | ); |
3275 | ); |
3271 | |
3276 | |
3272 | if ($self->{anim} && $self->{animspeed}) { |
3277 | if ($self->{anim} && $self->{animspeed}) { |
3273 | Scalar::Util::weaken (my $widget = $self); |
3278 | CFPlus::weaken (my $widget = $self); |
3274 | |
3279 | |
3275 | $self->{timer} = Event->timer ( |
3280 | $self->{timer} = Event->timer ( |
3276 | at => $self->{animspeed} * int $::NOW / $self->{animspeed}, |
3281 | at => $self->{animspeed} * int $::NOW / $self->{animspeed}, |
3277 | hard => 1, |
3282 | hard => 1, |
3278 | interval => $self->{animspeed}, |
3283 | interval => $self->{animspeed}, |
… | |
… | |
3532 | } |
3537 | } |
3533 | |
3538 | |
3534 | sub add { |
3539 | sub add { |
3535 | my ($self, $title, $widget, $tooltip) = @_; |
3540 | my ($self, $title, $widget, $tooltip) = @_; |
3536 | |
3541 | |
3537 | Scalar::Util::weaken $self; |
3542 | CFPlus::weaken $self; |
3538 | |
3543 | |
3539 | $self->{buttonbar}->add (new CFPlus::UI::Button |
3544 | $self->{buttonbar}->add (new CFPlus::UI::Button |
3540 | markup => $title, |
3545 | markup => $title, |
3541 | tooltip => $tooltip, |
3546 | tooltip => $tooltip, |
3542 | on_activate => sub { $self->set_current_page ($widget) }, |
3547 | on_activate => sub { $self->set_current_page ($widget) }, |
… | |
… | |
3626 | my $self = $class->SUPER::new ( |
3631 | my $self = $class->SUPER::new ( |
3627 | fontsize => 0.8, |
3632 | fontsize => 0.8, |
3628 | @_, |
3633 | @_, |
3629 | ); |
3634 | ); |
3630 | |
3635 | |
3631 | Scalar::Util::weaken (my $this = $self); |
3636 | CFPlus::weaken (my $this = $self); |
3632 | |
3637 | |
3633 | $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); |
3638 | $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); |
3634 | |
3639 | |
3635 | $self |
3640 | $self |
3636 | } |
3641 | } |
… | |
… | |
3775 | my $self = $class->SUPER::new ( |
3780 | my $self = $class->SUPER::new ( |
3776 | visible => 1, |
3781 | visible => 1, |
3777 | @_, |
3782 | @_, |
3778 | ); |
3783 | ); |
3779 | |
3784 | |
3780 | Scalar::Util::weaken ($self->{root} = $self); |
3785 | CFPlus::weaken ($self->{root} = $self); |
3781 | |
3786 | |
3782 | $self |
3787 | $self |
3783 | } |
3788 | } |
3784 | |
3789 | |
3785 | sub size_request { |
3790 | sub size_request { |