… | |
… | |
79 | sub feed_sdl_key_up_event { |
79 | sub feed_sdl_key_up_event { |
80 | $FOCUS->emit (key_up => $_[0]) |
80 | $FOCUS->emit (key_up => $_[0]) |
81 | if $FOCUS; |
81 | if $FOCUS; |
82 | } |
82 | } |
83 | |
83 | |
|
|
84 | sub check_hover { |
|
|
85 | my ($widget) = @_; |
|
|
86 | |
|
|
87 | if ($widget != $HOVER) { |
|
|
88 | my $hover = $HOVER; $HOVER = $widget; |
|
|
89 | |
|
|
90 | $hover->update if $hover && $hover->{can_hover}; |
|
|
91 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
|
|
92 | |
|
|
93 | $TOOLTIP_WATCHER->start; |
|
|
94 | } |
|
|
95 | } |
|
|
96 | |
84 | sub feed_sdl_button_down_event { |
97 | sub feed_sdl_button_down_event { |
85 | my ($ev) = @_; |
98 | my ($ev) = @_; |
86 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
99 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
87 | |
100 | |
88 | warn "button down $ev->{button}\n";#d# |
|
|
89 | |
|
|
90 | if (!$BUTTON_STATE) { |
101 | unless ($BUTTON_STATE) { |
91 | my $widget = $ROOT->find_widget ($x, $y); |
102 | my $widget = $ROOT->find_widget ($x, $y); |
92 | |
103 | |
93 | $GRAB = $widget; |
104 | $GRAB = $widget; |
94 | $GRAB->update if $GRAB; |
105 | $GRAB->update if $GRAB; |
95 | |
106 | |
96 | $TOOLTIP_WATCHER->cb->(); |
107 | $TOOLTIP_WATCHER->cb->(); |
97 | } |
108 | } |
98 | |
109 | |
99 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
110 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
100 | |
111 | |
101 | $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y)) |
112 | if ($GRAB) { |
102 | if $GRAB; |
113 | if ($ev->{button} == 4 || $ev->{button} == 5) { |
|
|
114 | # mousewheel |
|
|
115 | $ev->{dx} = 0; |
|
|
116 | $ev->{dy} = $ev->{button} * 2 - 9; |
|
|
117 | $GRAB->emit (mouse_wheel => $ev); |
|
|
118 | } else { |
|
|
119 | $GRAB->emit (button_down => $ev) |
|
|
120 | } |
|
|
121 | } |
103 | } |
122 | } |
104 | |
123 | |
105 | sub feed_sdl_button_up_event { |
124 | sub feed_sdl_button_up_event { |
106 | my ($ev) = @_; |
125 | my ($ev) = @_; |
107 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
|
|
108 | |
126 | |
109 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
127 | my $widget = $GRAB || $ROOT->find_widget ($ev->{x}, $ev->{y}); |
110 | |
128 | |
111 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
129 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
112 | |
130 | |
113 | $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y)) |
131 | $GRAB->emit (button_up => $ev) |
114 | if $GRAB; |
132 | if $GRAB && $ev->{button} != 4 && $ev->{button} != 5; |
115 | |
133 | |
116 | if (!$BUTTON_STATE) { |
134 | unless ($BUTTON_STATE) { |
117 | my $grab = $GRAB; undef $GRAB; |
135 | my $grab = $GRAB; undef $GRAB; |
118 | $grab->update if $grab; |
136 | $grab->update if $grab; |
119 | $GRAB->update if $GRAB; |
137 | $GRAB->update if $GRAB; |
120 | |
138 | |
|
|
139 | check_hover $widget; |
121 | $TOOLTIP_WATCHER->cb->(); |
140 | $TOOLTIP_WATCHER->cb->(); |
122 | } |
141 | } |
123 | } |
142 | } |
124 | |
143 | |
125 | sub feed_sdl_motion_event { |
144 | sub feed_sdl_motion_event { |
126 | my ($ev) = @_; |
145 | my ($ev) = @_; |
127 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
146 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
128 | |
147 | |
129 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
148 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
130 | |
149 | |
131 | if ($widget != $HOVER) { |
150 | check_hover $widget; |
132 | my $hover = $HOVER; $HOVER = $widget; |
|
|
133 | |
151 | |
134 | $hover->update if $hover && $hover->{can_hover}; |
152 | $HOVER->emit (mouse_motion => $ev) |
135 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
|
|
136 | |
|
|
137 | $TOOLTIP_WATCHER->start; |
|
|
138 | } |
|
|
139 | |
|
|
140 | $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y)) |
|
|
141 | if $HOVER; |
153 | if $HOVER; |
142 | } |
154 | } |
143 | |
155 | |
144 | # convert position array to integers |
156 | # convert position array to integers |
145 | sub harmonize { |
157 | sub harmonize { |
… | |
… | |
191 | |
203 | |
192 | } |
204 | } |
193 | } |
205 | } |
194 | |
206 | |
195 | reconfigure_widgets; |
207 | reconfigure_widgets; |
|
|
208 | } |
|
|
209 | |
|
|
210 | ############################################################################# |
|
|
211 | |
|
|
212 | package CFClient::UI::Event; |
|
|
213 | |
|
|
214 | sub xy { |
|
|
215 | $_[1]->coord2local ($_[0]{x}, $_[0]{y}) |
196 | } |
216 | } |
197 | |
217 | |
198 | ############################################################################# |
218 | ############################################################################# |
199 | |
219 | |
200 | package CFClient::UI::Base; |
220 | package CFClient::UI::Base; |
… | |
… | |
409 | |
429 | |
410 | # translate global coordinates to local coordinate system |
430 | # translate global coordinates to local coordinate system |
411 | sub coord2local { |
431 | sub coord2local { |
412 | my ($self, $x, $y) = @_; |
432 | my ($self, $x, $y) = @_; |
413 | |
433 | |
|
|
434 | Carp::confess unless $self->{parent};#d# |
|
|
435 | |
414 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
436 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
415 | } |
437 | } |
416 | |
438 | |
417 | # translate local coordinates to global coordinate system |
439 | # translate local coordinates to global coordinate system |
418 | sub coord2global { |
440 | sub coord2global { |
… | |
… | |
458 | |
480 | |
459 | sub invoke_mouse_motion { 0 } |
481 | sub invoke_mouse_motion { 0 } |
460 | sub invoke_button_up { 0 } |
482 | sub invoke_button_up { 0 } |
461 | sub invoke_key_down { 0 } |
483 | sub invoke_key_down { 0 } |
462 | sub invoke_key_up { 0 } |
484 | sub invoke_key_up { 0 } |
|
|
485 | sub invoke_mouse_wheel { 0 } |
463 | |
486 | |
464 | sub invoke_button_down { |
487 | sub invoke_button_down { |
465 | my ($self, $ev, $x, $y) = @_; |
488 | my ($self, $ev, $x, $y) = @_; |
466 | |
489 | |
467 | $self->grab_focus; |
490 | $self->grab_focus; |
468 | |
491 | |
469 | warn "button down $ev->{button} $x $y\n";#d# |
|
|
470 | |
|
|
471 | 0 |
492 | 0 |
472 | } |
493 | } |
473 | |
494 | |
474 | sub connect { |
495 | sub connect { |
475 | my ($self, $signal, $cb) = @_; |
496 | my ($self, $signal, $cb) = @_; |
476 | |
497 | |
477 | push @{ $self->{signal_cb}{$signal} }, $cb; |
498 | push @{ $self->{signal_cb}{$signal} }, $cb; |
478 | } |
499 | } |
479 | |
500 | |
|
|
501 | my %has_coords = ( |
|
|
502 | button_down => 1, |
|
|
503 | button_up => 1, |
|
|
504 | mouse_motion => 1, |
|
|
505 | mouse_wheel => 1, |
|
|
506 | ); |
|
|
507 | |
480 | sub emit { |
508 | sub emit { |
481 | my ($self, $signal, @args) = @_; |
509 | my ($self, $signal, @args) = @_; |
482 | |
510 | |
|
|
511 | my @append |
|
|
512 | = $has_coords{$signal} |
|
|
513 | ? $args[0]->xy ($self) |
|
|
514 | : (); |
|
|
515 | |
|
|
516 | #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# |
|
|
517 | |
483 | #d##TODO# stop propagating at first true, do not use sum |
518 | #d##TODO# stop propagating at first true, do not use sum |
484 | (List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}) # before |
519 | (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before |
485 | || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args) # closure |
520 | || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure |
486 | || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent |
521 | || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent |
487 | } |
522 | } |
488 | |
523 | |
489 | sub find_widget { |
524 | sub find_widget { |
490 | my ($self, $x, $y) = @_; |
525 | my ($self, $x, $y) = @_; |
491 | |
526 | |
… | |
… | |
981 | $self->{vp}->set_offset (0, $_[1]); |
1016 | $self->{vp}->set_offset (0, $_[1]); |
982 | }, |
1017 | }, |
983 | ; |
1018 | ; |
984 | |
1019 | |
985 | $self = $class->SUPER::new ( |
1020 | $self = $class->SUPER::new ( |
986 | vp => (new CFClient::UI::ViewPort expand => 1), |
1021 | vp => (new CFClient::UI::ViewPort expand => 1), |
|
|
1022 | can_events => 1, |
987 | slider => $slider, |
1023 | slider => $slider, |
988 | %arg, |
1024 | %arg, |
989 | ); |
1025 | ); |
990 | |
1026 | |
991 | $self->SUPER::add ($self->{vp}, $self->{slider}); |
1027 | $self->SUPER::add ($self->{vp}, $self->{slider}); |
992 | $self->add ($child) if $child; |
1028 | $self->add ($child) if $child; |
… | |
… | |
1000 | my ($self, $widget) = @_; |
1036 | my ($self, $widget) = @_; |
1001 | |
1037 | |
1002 | $self->{vp}->add ($self->{child} = $widget); |
1038 | $self->{vp}->add ($self->{child} = $widget); |
1003 | } |
1039 | } |
1004 | |
1040 | |
1005 | sub invoke_button_down { |
1041 | sub invoke_mouse_wheel { |
1006 | my ($self, $ev) = @_; |
1042 | my ($self, $ev) = @_; |
1007 | |
1043 | |
1008 | warn "button down $ev->{button}\n";#d# |
1044 | return 0 unless $ev->{dy}; # only vertical movements |
|
|
1045 | |
|
|
1046 | $self->{slider}->emit (mouse_wheel => $ev); |
|
|
1047 | |
1009 | |
1048 | 1 |
1010 | 0 |
|
|
1011 | } |
1049 | } |
1012 | |
1050 | |
1013 | sub update_slider { |
1051 | sub update_slider { |
1014 | my ($self) = @_; |
1052 | my ($self) = @_; |
1015 | |
1053 | |
… | |
… | |
2463 | } |
2501 | } |
2464 | |
2502 | |
2465 | 1 |
2503 | 1 |
2466 | } |
2504 | } |
2467 | |
2505 | |
|
|
2506 | sub invoke_mouse_wheel { |
|
|
2507 | my ($self, $ev) = @_; |
|
|
2508 | |
|
|
2509 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
|
|
2510 | |
|
|
2511 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.1); |
|
|
2512 | |
|
|
2513 | ! ! $delta |
|
|
2514 | } |
|
|
2515 | |
2468 | sub update { |
2516 | sub update { |
2469 | my ($self) = @_; |
2517 | my ($self) = @_; |
2470 | |
2518 | |
2471 | delete $self->{knob_w}; |
2519 | delete $self->{knob_w}; |
2472 | $self->SUPER::update; |
2520 | $self->SUPER::update; |
… | |
… | |
2571 | sub new { |
2619 | sub new { |
2572 | my $class = shift; |
2620 | my $class = shift; |
2573 | |
2621 | |
2574 | my $self = $class->SUPER::new ( |
2622 | my $self = $class->SUPER::new ( |
2575 | fontsize => 1, |
2623 | fontsize => 1, |
2576 | can_events => 0, |
2624 | can_events => 1, |
2577 | indent => 0, |
2625 | indent => 0, |
2578 | #font => default_font |
2626 | #font => default_font |
2579 | @_, |
2627 | @_, |
2580 | |
2628 | |
2581 | layout => (new CFClient::Layout), |
2629 | layout => (new CFClient::Layout), |
… | |
… | |
2621 | |
2669 | |
2622 | $self->reflow; |
2670 | $self->reflow; |
2623 | |
2671 | |
2624 | local $self->{children} = [$empty, $slider]; |
2672 | local $self->{children} = [$empty, $slider]; |
2625 | $self->SUPER::invoke_size_allocate ($w, $h) |
2673 | $self->SUPER::invoke_size_allocate ($w, $h) |
|
|
2674 | } |
|
|
2675 | |
|
|
2676 | sub invoke_mouse_wheel { |
|
|
2677 | my ($self, $ev) = @_; |
|
|
2678 | |
|
|
2679 | return 0 unless $ev->{dy}; # only vertical movements |
|
|
2680 | |
|
|
2681 | $self->{children}[1]->emit (mouse_wheel => $ev); |
|
|
2682 | |
|
|
2683 | 1 |
2626 | } |
2684 | } |
2627 | |
2685 | |
2628 | sub get_layout { |
2686 | sub get_layout { |
2629 | my ($self, $para) = @_; |
2687 | my ($self, $para) = @_; |
2630 | |
2688 | |