… | |
… | |
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 | if (!$BUTTON_STATE) { |
101 | unless ($BUTTON_STATE) { |
89 | my $widget = $ROOT->find_widget ($x, $y); |
102 | my $widget = $ROOT->find_widget ($x, $y); |
90 | |
103 | |
91 | $GRAB = $widget; |
104 | $GRAB = $widget; |
92 | $GRAB->update if $GRAB; |
105 | $GRAB->update if $GRAB; |
93 | |
106 | |
94 | $TOOLTIP_WATCHER->cb->(); |
107 | $TOOLTIP_WATCHER->cb->(); |
95 | } |
108 | } |
96 | |
109 | |
97 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
110 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
98 | |
111 | |
99 | $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y)) |
112 | if ($GRAB) { |
100 | 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 | } |
101 | } |
122 | } |
102 | |
123 | |
103 | sub feed_sdl_button_up_event { |
124 | sub feed_sdl_button_up_event { |
104 | my ($ev) = @_; |
125 | my ($ev) = @_; |
105 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
|
|
106 | |
126 | |
107 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
127 | my $widget = $GRAB || $ROOT->find_widget ($ev->{x}, $ev->{y}); |
108 | |
128 | |
109 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
129 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
110 | |
130 | |
111 | $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y)) |
131 | $GRAB->emit (button_up => $ev) |
112 | if $GRAB; |
132 | if $GRAB && $ev->{button} != 4 && $ev->{button} != 5; |
113 | |
133 | |
114 | if (!$BUTTON_STATE) { |
134 | unless ($BUTTON_STATE) { |
115 | my $grab = $GRAB; undef $GRAB; |
135 | my $grab = $GRAB; undef $GRAB; |
116 | $grab->update if $grab; |
136 | $grab->update if $grab; |
117 | $GRAB->update if $GRAB; |
137 | $GRAB->update if $GRAB; |
118 | |
138 | |
|
|
139 | check_hover $widget; |
119 | $TOOLTIP_WATCHER->cb->(); |
140 | $TOOLTIP_WATCHER->cb->(); |
120 | } |
141 | } |
121 | } |
142 | } |
122 | |
143 | |
123 | sub feed_sdl_motion_event { |
144 | sub feed_sdl_motion_event { |
124 | my ($ev) = @_; |
145 | my ($ev) = @_; |
125 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
146 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
126 | |
147 | |
127 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
148 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
128 | |
149 | |
129 | if ($widget != $HOVER) { |
150 | check_hover $widget; |
130 | my $hover = $HOVER; $HOVER = $widget; |
|
|
131 | |
151 | |
132 | $hover->update if $hover && $hover->{can_hover}; |
152 | $HOVER->emit (mouse_motion => $ev) |
133 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
|
|
134 | |
|
|
135 | $TOOLTIP_WATCHER->start; |
|
|
136 | } |
|
|
137 | |
|
|
138 | $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y)) |
|
|
139 | if $HOVER; |
153 | if $HOVER; |
140 | } |
154 | } |
141 | |
155 | |
142 | # convert position array to integers |
156 | # convert position array to integers |
143 | sub harmonize { |
157 | sub harmonize { |
… | |
… | |
189 | |
203 | |
190 | } |
204 | } |
191 | } |
205 | } |
192 | |
206 | |
193 | 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}) |
194 | } |
216 | } |
195 | |
217 | |
196 | ############################################################################# |
218 | ############################################################################# |
197 | |
219 | |
198 | package CFClient::UI::Base; |
220 | package CFClient::UI::Base; |
… | |
… | |
407 | |
429 | |
408 | # translate global coordinates to local coordinate system |
430 | # translate global coordinates to local coordinate system |
409 | sub coord2local { |
431 | sub coord2local { |
410 | my ($self, $x, $y) = @_; |
432 | my ($self, $x, $y) = @_; |
411 | |
433 | |
|
|
434 | Carp::confess unless $self->{parent};#d# |
|
|
435 | |
412 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
436 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
413 | } |
437 | } |
414 | |
438 | |
415 | # translate local coordinates to global coordinate system |
439 | # translate local coordinates to global coordinate system |
416 | sub coord2global { |
440 | sub coord2global { |
… | |
… | |
452 | |
476 | |
453 | $FOCUS->emit ("focus_out") if $FOCUS; |
477 | $FOCUS->emit ("focus_out") if $FOCUS; |
454 | $self->emit ("focus_in"); |
478 | $self->emit ("focus_in"); |
455 | } |
479 | } |
456 | |
480 | |
457 | sub invoke_mouse_motion { 1 } |
481 | sub invoke_mouse_motion { 0 } |
458 | sub invoke_button_up { 1 } |
482 | sub invoke_button_up { 0 } |
459 | sub invoke_key_down { 1 } |
483 | sub invoke_key_down { 0 } |
460 | sub invoke_key_up { 1 } |
484 | sub invoke_key_up { 0 } |
|
|
485 | sub invoke_mouse_wheel { 0 } |
461 | |
486 | |
462 | sub invoke_button_down { |
487 | sub invoke_button_down { |
463 | my ($self, $ev, $x, $y) = @_; |
488 | my ($self, $ev, $x, $y) = @_; |
464 | |
489 | |
465 | $self->grab_focus; |
490 | $self->grab_focus; |
466 | |
491 | |
467 | 1 |
492 | 0 |
468 | } |
493 | } |
469 | |
494 | |
470 | sub connect { |
495 | sub connect { |
471 | my ($self, $signal, $cb) = @_; |
496 | my ($self, $signal, $cb) = @_; |
472 | |
497 | |
473 | push @{ $self->{signal_cb}{$signal} }, $cb; |
498 | push @{ $self->{signal_cb}{$signal} }, $cb; |
474 | } |
499 | } |
475 | |
500 | |
|
|
501 | my %has_coords = ( |
|
|
502 | button_down => 1, |
|
|
503 | button_up => 1, |
|
|
504 | mouse_motion => 1, |
|
|
505 | mouse_wheel => 1, |
|
|
506 | ); |
|
|
507 | |
476 | sub emit { |
508 | sub emit { |
477 | my ($self, $signal, @args) = @_; |
509 | my ($self, $signal, @args) = @_; |
478 | |
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 | |
479 | #d##TODO# stop propagating at first true, do not use sum |
518 | #d##TODO# stop propagating at first true, do not use sum |
480 | (List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}) # before |
519 | (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before |
481 | || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args) # closure |
520 | || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure |
482 | || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent |
521 | || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent |
483 | } |
522 | } |
484 | |
523 | |
485 | sub find_widget { |
524 | sub find_widget { |
486 | my ($self, $x, $y) = @_; |
525 | my ($self, $x, $y) = @_; |
487 | |
526 | |
… | |
… | |
587 | } |
626 | } |
588 | |
627 | |
589 | sub DESTROY { |
628 | sub DESTROY { |
590 | my ($self) = @_; |
629 | my ($self) = @_; |
591 | |
630 | |
|
|
631 | return if CFClient::in_destruct; |
|
|
632 | |
592 | delete $WIDGET{$self+0}; |
633 | delete $WIDGET{$self+0}; |
593 | |
634 | |
594 | eval { $self->destroy }; |
635 | eval { $self->destroy }; |
595 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
636 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
596 | } |
637 | } |
… | |
… | |
975 | $self->{vp}->set_offset (0, $_[1]); |
1016 | $self->{vp}->set_offset (0, $_[1]); |
976 | }, |
1017 | }, |
977 | ; |
1018 | ; |
978 | |
1019 | |
979 | $self = $class->SUPER::new ( |
1020 | $self = $class->SUPER::new ( |
980 | vp => (new CFClient::UI::ViewPort expand => 1), |
1021 | vp => (new CFClient::UI::ViewPort expand => 1), |
|
|
1022 | can_events => 1, |
981 | slider => $slider, |
1023 | slider => $slider, |
982 | %arg, |
1024 | %arg, |
983 | ); |
1025 | ); |
984 | |
1026 | |
985 | $self->SUPER::add ($self->{vp}, $self->{slider}); |
1027 | $self->SUPER::add ($self->{vp}, $self->{slider}); |
986 | $self->add ($child) if $child; |
1028 | $self->add ($child) if $child; |
… | |
… | |
992 | |
1034 | |
993 | sub add { |
1035 | sub add { |
994 | my ($self, $widget) = @_; |
1036 | my ($self, $widget) = @_; |
995 | |
1037 | |
996 | $self->{vp}->add ($self->{child} = $widget); |
1038 | $self->{vp}->add ($self->{child} = $widget); |
|
|
1039 | } |
|
|
1040 | |
|
|
1041 | sub invoke_mouse_wheel { |
|
|
1042 | my ($self, $ev) = @_; |
|
|
1043 | |
|
|
1044 | return 0 unless $ev->{dy}; # only vertical movements |
|
|
1045 | |
|
|
1046 | $self->{slider}->emit (mouse_wheel => $ev); |
|
|
1047 | |
|
|
1048 | 1 |
997 | } |
1049 | } |
998 | |
1050 | |
999 | sub update_slider { |
1051 | sub update_slider { |
1000 | my ($self) = @_; |
1052 | my ($self) = @_; |
1001 | |
1053 | |
… | |
… | |
1080 | my $self = $class->SUPER::new ( |
1132 | my $self = $class->SUPER::new ( |
1081 | bg => [1, 1, 1, 1], |
1133 | bg => [1, 1, 1, 1], |
1082 | border_bg => [1, 1, 1, 1], |
1134 | border_bg => [1, 1, 1, 1], |
1083 | border => 0.6, |
1135 | border => 0.6, |
1084 | can_events => 1, |
1136 | can_events => 1, |
1085 | min_w => 16, |
1137 | min_w => 64, |
1086 | min_h => 16, |
1138 | min_h => 32, |
1087 | %arg, |
1139 | %arg, |
1088 | ); |
1140 | ); |
1089 | |
1141 | |
1090 | $self->{title_widget} = new CFClient::UI::Label |
1142 | $self->{title_widget} = new CFClient::UI::Label |
1091 | align => 0, |
1143 | align => 0, |
… | |
… | |
1826 | |
1878 | |
1827 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
1879 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
1828 | |
1880 | |
1829 | # byte-index to char-index |
1881 | # byte-index to char-index |
1830 | my $text = $self->{text}; |
1882 | my $text = $self->{text}; |
1831 | utf8::encode $text; |
1883 | utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text; |
1832 | $self->{cursor} = length substr $text, 0, $idx; |
1884 | $self->{cursor} = length $text; |
1833 | |
1885 | |
1834 | $self->_set_text ($self->{text}); |
1886 | $self->_set_text ($self->{text}); |
1835 | $self->update; |
1887 | $self->update; |
1836 | |
1888 | |
1837 | 1 |
1889 | 1 |
… | |
… | |
2116 | my $self = $class->SUPER::new ( |
2168 | my $self = $class->SUPER::new ( |
2117 | can_events => 0, |
2169 | can_events => 0, |
2118 | @_, |
2170 | @_, |
2119 | ); |
2171 | ); |
2120 | |
2172 | |
2121 | $self->{path} |
2173 | $self->{path} || $self->{tex} |
2122 | or Carp::croak "required attribute 'path' not set"; |
2174 | or Carp::croak "'path' or 'tex' attributes required"; |
2123 | |
2175 | |
2124 | $self->{tex} = $texture_cache{$self->{path}} ||= |
2176 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2125 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1; |
2177 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1; |
2126 | |
2178 | |
2127 | Scalar::Util::weaken $texture_cache{$self->{path}}; |
2179 | Scalar::Util::weaken $texture_cache{$self->{path}}; |
2128 | |
2180 | |
2129 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2181 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
… | |
… | |
2449 | } |
2501 | } |
2450 | |
2502 | |
2451 | 1 |
2503 | 1 |
2452 | } |
2504 | } |
2453 | |
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 | |
2454 | sub update { |
2516 | sub update { |
2455 | my ($self) = @_; |
2517 | my ($self) = @_; |
2456 | |
2518 | |
2457 | delete $self->{knob_w}; |
2519 | delete $self->{knob_w}; |
2458 | $self->SUPER::update; |
2520 | $self->SUPER::update; |
… | |
… | |
2557 | sub new { |
2619 | sub new { |
2558 | my $class = shift; |
2620 | my $class = shift; |
2559 | |
2621 | |
2560 | my $self = $class->SUPER::new ( |
2622 | my $self = $class->SUPER::new ( |
2561 | fontsize => 1, |
2623 | fontsize => 1, |
2562 | can_events => 0, |
2624 | can_events => 1, |
2563 | indent => 0, |
2625 | indent => 0, |
2564 | #font => default_font |
2626 | #font => default_font |
2565 | @_, |
2627 | @_, |
2566 | |
2628 | |
2567 | layout => (new CFClient::Layout), |
2629 | layout => (new CFClient::Layout), |
… | |
… | |
2607 | |
2669 | |
2608 | $self->reflow; |
2670 | $self->reflow; |
2609 | |
2671 | |
2610 | local $self->{children} = [$empty, $slider]; |
2672 | local $self->{children} = [$empty, $slider]; |
2611 | $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 |
2612 | } |
2684 | } |
2613 | |
2685 | |
2614 | sub get_layout { |
2686 | sub get_layout { |
2615 | my ($self, $para) = @_; |
2687 | my ($self, $para) = @_; |
2616 | |
2688 | |
… | |
… | |
3814 | my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; |
3886 | my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; |
3815 | |
3887 | |
3816 | $w = 0 if $w < 0; |
3888 | $w = 0 if $w < 0; |
3817 | $h = 0 if $h < 0; |
3889 | $h = 0 if $h < 0; |
3818 | |
3890 | |
|
|
3891 | $w = max $widget->{min_w}, $w; |
|
|
3892 | $h = max $widget->{min_h}, $h; |
|
|
3893 | |
|
|
3894 | $w = min $widget->{max_w}, $w if exists $widget->{max_w}; |
|
|
3895 | $h = min $widget->{max_h}, $h if exists $widget->{max_h}; |
|
|
3896 | |
3819 | $w = int $w + 0.5; |
3897 | $w = int $w + 0.5; |
3820 | $h = int $h + 0.5; |
3898 | $h = int $h + 0.5; |
3821 | |
3899 | |
3822 | if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { |
3900 | if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { |
3823 | $widget->{old_w} = $widget->{w}; |
3901 | $widget->{old_w} = $widget->{w}; |