ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.326 by root, Sun Jul 16 20:04:08 2006 UTC vs.
Revision 1.331 by root, Sun Jul 23 04:46:02 2006 UTC

79sub feed_sdl_key_up_event { 79sub 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
84sub 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
84sub feed_sdl_button_down_event { 97sub 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
103sub feed_sdl_button_up_event { 124sub 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
123sub feed_sdl_motion_event { 144sub 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
143sub harmonize { 157sub harmonize {
189 203
190 } 204 }
191 } 205 }
192 206
193 reconfigure_widgets; 207 reconfigure_widgets;
208}
209
210#############################################################################
211
212package CFClient::UI::Event;
213
214sub xy {
215 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
194} 216}
195 217
196############################################################################# 218#############################################################################
197 219
198package CFClient::UI::Base; 220package CFClient::UI::Base;
407 429
408# translate global coordinates to local coordinate system 430# translate global coordinates to local coordinate system
409sub coord2local { 431sub 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
416sub coord2global { 440sub 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
457sub invoke_mouse_motion { 1 } 481sub invoke_mouse_motion { 0 }
458sub invoke_button_up { 1 } 482sub invoke_button_up { 0 }
459sub invoke_key_down { 1 } 483sub invoke_key_down { 0 }
460sub invoke_key_up { 1 } 484sub invoke_key_up { 0 }
485sub invoke_mouse_wheel { 0 }
461 486
462sub invoke_button_down { 487sub 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
470sub connect { 495sub 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
501my %has_coords = (
502 button_down => 1,
503 button_up => 1,
504 mouse_motion => 1,
505 mouse_wheel => 1,
506);
507
476sub emit { 508sub 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
485sub find_widget { 524sub find_widget {
486 my ($self, $x, $y) = @_; 525 my ($self, $x, $y) = @_;
487 526
977 $self->{vp}->set_offset (0, $_[1]); 1016 $self->{vp}->set_offset (0, $_[1]);
978 }, 1017 },
979 ; 1018 ;
980 1019
981 $self = $class->SUPER::new ( 1020 $self = $class->SUPER::new (
982 vp => (new CFClient::UI::ViewPort expand => 1), 1021 vp => (new CFClient::UI::ViewPort expand => 1),
1022 can_events => 1,
983 slider => $slider, 1023 slider => $slider,
984 %arg, 1024 %arg,
985 ); 1025 );
986 1026
987 $self->SUPER::add ($self->{vp}, $self->{slider}); 1027 $self->SUPER::add ($self->{vp}, $self->{slider});
988 $self->add ($child) if $child; 1028 $self->add ($child) if $child;
994 1034
995sub add { 1035sub add {
996 my ($self, $widget) = @_; 1036 my ($self, $widget) = @_;
997 1037
998 $self->{vp}->add ($self->{child} = $widget); 1038 $self->{vp}->add ($self->{child} = $widget);
1039}
1040
1041sub 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
999} 1049}
1000 1050
1001sub update_slider { 1051sub update_slider {
1002 my ($self) = @_; 1052 my ($self) = @_;
1003 1053
2118 my $self = $class->SUPER::new ( 2168 my $self = $class->SUPER::new (
2119 can_events => 0, 2169 can_events => 0,
2120 @_, 2170 @_,
2121 ); 2171 );
2122 2172
2123 $self->{path} 2173 $self->{path} || $self->{tex}
2124 or Carp::croak "required attribute 'path' not set"; 2174 or Carp::croak "'path' or 'tex' attributes required";
2125 2175
2126 $self->{tex} = $texture_cache{$self->{path}} ||= 2176 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2127 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;
2128 2178
2129 Scalar::Util::weaken $texture_cache{$self->{path}}; 2179 Scalar::Util::weaken $texture_cache{$self->{path}};
2130 2180
2131 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2181 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2451 } 2501 }
2452 2502
2453 1 2503 1
2454} 2504}
2455 2505
2506sub 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
2456sub update { 2516sub update {
2457 my ($self) = @_; 2517 my ($self) = @_;
2458 2518
2459 delete $self->{knob_w}; 2519 delete $self->{knob_w};
2460 $self->SUPER::update; 2520 $self->SUPER::update;
2559sub new { 2619sub new {
2560 my $class = shift; 2620 my $class = shift;
2561 2621
2562 my $self = $class->SUPER::new ( 2622 my $self = $class->SUPER::new (
2563 fontsize => 1, 2623 fontsize => 1,
2564 can_events => 0, 2624 can_events => 1,
2565 indent => 0, 2625 indent => 0,
2566 #font => default_font 2626 #font => default_font
2567 @_, 2627 @_,
2568 2628
2569 layout => (new CFClient::Layout), 2629 layout => (new CFClient::Layout),
2609 2669
2610 $self->reflow; 2670 $self->reflow;
2611 2671
2612 local $self->{children} = [$empty, $slider]; 2672 local $self->{children} = [$empty, $slider];
2613 $self->SUPER::invoke_size_allocate ($w, $h) 2673 $self->SUPER::invoke_size_allocate ($w, $h)
2674}
2675
2676sub 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
2614} 2684}
2615 2685
2616sub get_layout { 2686sub get_layout {
2617 my ($self, $para) = @_; 2687 my ($self, $para) = @_;
2618 2688

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines