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.324 by root, Wed Jul 12 17:20:04 2006 UTC vs.
Revision 1.330 by root, Sun Jul 23 04:37:51 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;
452 474
453 $FOCUS->emit ("focus_out") if $FOCUS; 475 $FOCUS->emit ("focus_out") if $FOCUS;
454 $self->emit ("focus_in"); 476 $self->emit ("focus_in");
455} 477}
456 478
457sub invoke_mouse_motion { 1 } 479sub invoke_mouse_motion { 0 }
458sub invoke_button_up { 1 } 480sub invoke_button_up { 0 }
459sub invoke_key_down { 1 } 481sub invoke_key_down { 0 }
460sub invoke_key_up { 1 } 482sub invoke_key_up { 0 }
483sub invoke_mouse_wheel { 0 }
461 484
462sub invoke_button_down { 485sub invoke_button_down {
463 my ($self, $ev, $x, $y) = @_; 486 my ($self, $ev, $x, $y) = @_;
464 487
465 $self->grab_focus; 488 $self->grab_focus;
466 489
467 1 490 0
468} 491}
469 492
470sub connect { 493sub connect {
471 my ($self, $signal, $cb) = @_; 494 my ($self, $signal, $cb) = @_;
472 495
474} 497}
475 498
476sub emit { 499sub emit {
477 my ($self, $signal, @args) = @_; 500 my ($self, $signal, @args) = @_;
478 501
502 my @append
503 = ref $args[0] && $args[0]->isa ("CFClient::UI::Event")
504 ? $args[0]->xy ($self)
505 : ();
506
507 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d#
508
479 #d##TODO# stop propagating at first true, do not use sum 509 #d##TODO# stop propagating at first true, do not use sum
480 (List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}) # before 510 (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before
481 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args) # closure 511 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure
482 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent 512 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent
483} 513}
484 514
485sub find_widget { 515sub find_widget {
486 my ($self, $x, $y) = @_; 516 my ($self, $x, $y) = @_;
487 517
977 $self->{vp}->set_offset (0, $_[1]); 1007 $self->{vp}->set_offset (0, $_[1]);
978 }, 1008 },
979 ; 1009 ;
980 1010
981 $self = $class->SUPER::new ( 1011 $self = $class->SUPER::new (
982 vp => (new CFClient::UI::ViewPort expand => 1), 1012 vp => (new CFClient::UI::ViewPort expand => 1),
1013 can_events => 1,
983 slider => $slider, 1014 slider => $slider,
984 %arg, 1015 %arg,
985 ); 1016 );
986 1017
987 $self->SUPER::add ($self->{vp}, $self->{slider}); 1018 $self->SUPER::add ($self->{vp}, $self->{slider});
988 $self->add ($child) if $child; 1019 $self->add ($child) if $child;
994 1025
995sub add { 1026sub add {
996 my ($self, $widget) = @_; 1027 my ($self, $widget) = @_;
997 1028
998 $self->{vp}->add ($self->{child} = $widget); 1029 $self->{vp}->add ($self->{child} = $widget);
1030}
1031
1032sub invoke_mouse_wheel {
1033 my ($self, $ev) = @_;
1034
1035 return 0 unless $ev->{dy}; # only vertical movements
1036
1037 $self->{slider}->emit (mouse_wheel => $ev);
1038
1039 1
999} 1040}
1000 1041
1001sub update_slider { 1042sub update_slider {
1002 my ($self) = @_; 1043 my ($self) = @_;
1003 1044
1082 my $self = $class->SUPER::new ( 1123 my $self = $class->SUPER::new (
1083 bg => [1, 1, 1, 1], 1124 bg => [1, 1, 1, 1],
1084 border_bg => [1, 1, 1, 1], 1125 border_bg => [1, 1, 1, 1],
1085 border => 0.6, 1126 border => 0.6,
1086 can_events => 1, 1127 can_events => 1,
1087 min_w => 16, 1128 min_w => 64,
1088 min_h => 16, 1129 min_h => 32,
1089 %arg, 1130 %arg,
1090 ); 1131 );
1091 1132
1092 $self->{title_widget} = new CFClient::UI::Label 1133 $self->{title_widget} = new CFClient::UI::Label
1093 align => 0, 1134 align => 0,
1828 1869
1829 my $idx = $self->{layout}->xy_to_index ($x, $y); 1870 my $idx = $self->{layout}->xy_to_index ($x, $y);
1830 1871
1831 # byte-index to char-index 1872 # byte-index to char-index
1832 my $text = $self->{text}; 1873 my $text = $self->{text};
1833 utf8::encode $text; 1874 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text;
1834 $self->{cursor} = length substr $text, 0, $idx; 1875 $self->{cursor} = length $text;
1835 1876
1836 $self->_set_text ($self->{text}); 1877 $self->_set_text ($self->{text});
1837 $self->update; 1878 $self->update;
1838 1879
1839 1 1880 1
2118 my $self = $class->SUPER::new ( 2159 my $self = $class->SUPER::new (
2119 can_events => 0, 2160 can_events => 0,
2120 @_, 2161 @_,
2121 ); 2162 );
2122 2163
2123 $self->{path} 2164 $self->{path} || $self->{tex}
2124 or Carp::croak "required attribute 'path' not set"; 2165 or Carp::croak "'path' or 'tex' attributes required";
2125 2166
2126 $self->{tex} = $texture_cache{$self->{path}} ||= 2167 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2127 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1; 2168 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1;
2128 2169
2129 Scalar::Util::weaken $texture_cache{$self->{path}}; 2170 Scalar::Util::weaken $texture_cache{$self->{path}};
2130 2171
2131 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2172 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2451 } 2492 }
2452 2493
2453 1 2494 1
2454} 2495}
2455 2496
2497sub invoke_mouse_wheel {
2498 my ($self, $ev) = @_;
2499
2500 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2501
2502 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.1);
2503
2504 ! ! $delta
2505}
2506
2456sub update { 2507sub update {
2457 my ($self) = @_; 2508 my ($self) = @_;
2458 2509
2459 delete $self->{knob_w}; 2510 delete $self->{knob_w};
2460 $self->SUPER::update; 2511 $self->SUPER::update;
2559sub new { 2610sub new {
2560 my $class = shift; 2611 my $class = shift;
2561 2612
2562 my $self = $class->SUPER::new ( 2613 my $self = $class->SUPER::new (
2563 fontsize => 1, 2614 fontsize => 1,
2564 can_events => 0, 2615 can_events => 1,
2565 indent => 0, 2616 indent => 0,
2566 #font => default_font 2617 #font => default_font
2567 @_, 2618 @_,
2568 2619
2569 layout => (new CFClient::Layout), 2620 layout => (new CFClient::Layout),
2609 2660
2610 $self->reflow; 2661 $self->reflow;
2611 2662
2612 local $self->{children} = [$empty, $slider]; 2663 local $self->{children} = [$empty, $slider];
2613 $self->SUPER::invoke_size_allocate ($w, $h) 2664 $self->SUPER::invoke_size_allocate ($w, $h)
2665}
2666
2667sub invoke_mouse_wheel {
2668 my ($self, $ev) = @_;
2669
2670 return 0 unless $ev->{dy}; # only vertical movements
2671
2672 $self->{children}[1]->emit (mouse_wheel => $ev);
2673
2674 1
2614} 2675}
2615 2676
2616sub get_layout { 2677sub get_layout {
2617 my ($self, $para) = @_; 2678 my ($self, $para) = @_;
2618 2679
3816 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 3877 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3817 3878
3818 $w = 0 if $w < 0; 3879 $w = 0 if $w < 0;
3819 $h = 0 if $h < 0; 3880 $h = 0 if $h < 0;
3820 3881
3882 $w = max $widget->{min_w}, $w;
3883 $h = max $widget->{min_h}, $h;
3884
3885 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3886 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3887
3821 $w = int $w + 0.5; 3888 $w = int $w + 0.5;
3822 $h = int $h + 0.5; 3889 $h = int $h + 0.5;
3823 3890
3824 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 3891 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3825 $widget->{old_w} = $widget->{w}; 3892 $widget->{old_w} = $widget->{w};

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines