1 | package Crossfire::Client::Widget; |
1 | package CFClient::Widget; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | |
4 | |
5 | use Scalar::Util; |
5 | use Scalar::Util; |
6 | |
6 | |
7 | use SDL::OpenGL; |
7 | use SDL::OpenGL; |
8 | use SDL::OpenGL::Constants; |
8 | use SDL::OpenGL::Constants; |
9 | |
9 | |
10 | our $FOCUS; # the widget with current focus |
10 | use CFClient; |
11 | our @ACTIVE_WIDGETS; |
11 | |
|
|
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
|
|
13 | |
|
|
14 | our $TOPLEVEL; |
|
|
15 | our $BUTTON_STATE; |
12 | |
16 | |
13 | # class methods for events |
17 | # class methods for events |
14 | sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } |
18 | sub feed_sdl_key_down_event { |
15 | sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } |
19 | $FOCUS->key_down ($_[0]) if $FOCUS; |
16 | sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS } |
20 | } |
17 | sub feed_sdl_button_up_event { $FOCUS->button_up ($_[0]) if $FOCUS } |
21 | |
|
|
22 | sub feed_sdl_key_up_event { |
|
|
23 | $FOCUS->key_up ($_[0]) if $FOCUS; |
|
|
24 | } |
|
|
25 | |
|
|
26 | sub feed_sdl_button_down_event { |
|
|
27 | my ($ev) = @_; |
|
|
28 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
29 | |
|
|
30 | if (!$BUTTON_STATE) { |
|
|
31 | my $widget = $TOPLEVEL->find_widget ($x, $y); |
|
|
32 | |
|
|
33 | $GRAB = $widget; |
|
|
34 | $GRAB->update if $GRAB; |
|
|
35 | } |
|
|
36 | |
|
|
37 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
|
|
38 | |
|
|
39 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
|
|
40 | } |
|
|
41 | |
|
|
42 | sub feed_sdl_button_up_event { |
|
|
43 | my ($ev) = @_; |
|
|
44 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
45 | |
|
|
46 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
|
|
47 | |
|
|
48 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
|
|
49 | |
|
|
50 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
|
|
51 | |
|
|
52 | if (!$BUTTON_STATE) { |
|
|
53 | my $grab = $GRAB; undef $GRAB; |
|
|
54 | $grab->update if $grab; |
|
|
55 | $GRAB->update if $GRAB; |
|
|
56 | } |
|
|
57 | } |
|
|
58 | |
|
|
59 | sub feed_sdl_motion_event { |
|
|
60 | my ($ev) = @_; |
|
|
61 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
62 | |
|
|
63 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
|
|
64 | |
|
|
65 | if ($widget != $HOVER) { |
|
|
66 | my $hover = $HOVER; $HOVER = $widget; |
|
|
67 | |
|
|
68 | $hover->update if $hover; |
|
|
69 | $HOVER->update if $HOVER; |
|
|
70 | } |
|
|
71 | |
|
|
72 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
|
|
73 | } |
18 | |
74 | |
19 | sub new { |
75 | sub new { |
20 | my $class = shift; |
76 | my $class = shift; |
21 | |
77 | |
22 | bless { @_ }, $class |
78 | bless { |
23 | } |
79 | x => 0, |
24 | |
80 | y => 0, |
25 | sub activate { |
81 | z => 0, |
26 | push @ACTIVE_WIDGETS, $_[0]; |
82 | @_ |
27 | Scalar::Util::weaken $ACTIVE_WIDGETS[-1]; |
83 | }, $class |
28 | } |
|
|
29 | |
|
|
30 | sub deactivate { |
|
|
31 | @ACTIVE_WIDGETS = |
|
|
32 | sort { $a->{z} <=> $b->{z} } |
|
|
33 | grep { $_ && $_ != $_[0] } |
|
|
34 | @ACTIVE_WIDGETS; |
|
|
35 | } |
84 | } |
36 | |
85 | |
37 | sub move { |
86 | sub move { |
38 | my ($self, $x, $y, $z) = @_; |
87 | my ($self, $x, $y, $z) = @_; |
39 | $self->{x} = $x; |
88 | $self->{x} = $x; |
… | |
… | |
44 | sub needs_redraw { |
93 | sub needs_redraw { |
45 | 0 |
94 | 0 |
46 | } |
95 | } |
47 | |
96 | |
48 | sub size_request { |
97 | sub size_request { |
|
|
98 | require Carp; |
49 | die "size_request is abtract"; |
99 | Carp::confess "size_request is abtract"; |
|
|
100 | } |
|
|
101 | |
|
|
102 | sub size_allocate { |
|
|
103 | my ($self, $w, $h) = @_; |
|
|
104 | |
|
|
105 | $self->{w} = $w; |
|
|
106 | $self->{h} = $h; |
|
|
107 | } |
|
|
108 | |
|
|
109 | # translate global koordinates to local coordinate system |
|
|
110 | sub translate { |
|
|
111 | my ($self, $x, $y) = @_; |
|
|
112 | |
|
|
113 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
50 | } |
114 | } |
51 | |
115 | |
52 | sub focus_in { |
116 | sub focus_in { |
53 | my ($widget) = @_; |
117 | my ($self) = @_; |
54 | $FOCUS = $widget; |
118 | |
|
|
119 | return if $FOCUS == $self; |
|
|
120 | |
|
|
121 | my $focus = $FOCUS; $FOCUS = $self; |
|
|
122 | $focus->update if $focus; |
|
|
123 | $FOCUS->update; |
55 | } |
124 | } |
56 | |
125 | |
57 | sub focus_out { |
126 | sub focus_out { |
58 | my ($widget) = @_; |
127 | my ($self) = @_; |
59 | } |
|
|
60 | |
128 | |
|
|
129 | return unless $FOCUS == $self; |
|
|
130 | |
|
|
131 | my $focus = $FOCUS; undef $FOCUS; |
|
|
132 | $focus->update if $focus; #? |
|
|
133 | } |
|
|
134 | |
|
|
135 | sub mouse_motion { } |
|
|
136 | sub button_up { } |
61 | sub key_down { |
137 | sub key_down { } |
62 | my ($widget, $sdlev) = @_; |
|
|
63 | } |
|
|
64 | |
|
|
65 | sub key_up { |
138 | sub key_up { } |
66 | my ($widget, $sdlev) = @_; |
|
|
67 | } |
|
|
68 | |
139 | |
69 | sub button_down { |
140 | sub button_down { |
70 | my ($widget, $sdlev) = @_; |
141 | my ($self, $ev, $x, $y) = @_; |
71 | } |
|
|
72 | |
142 | |
73 | sub button_up { |
143 | $self->focus_in; |
74 | my ($widget, $sdlev) = @_; |
|
|
75 | } |
144 | } |
76 | |
145 | |
77 | sub w { $_[0]->{w} } |
146 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
78 | sub h { $_[0]->{h} } |
147 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
79 | sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } |
148 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
80 | sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } |
149 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
81 | sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } |
150 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
82 | |
151 | |
83 | sub draw { |
152 | sub draw { |
84 | my ($self) = @_; |
153 | my ($self) = @_; |
|
|
154 | |
|
|
155 | return unless $self->{h} && $self->{w}; |
85 | |
156 | |
86 | glPushMatrix; |
157 | glPushMatrix; |
87 | glTranslate $self->{x}, $self->{y}, 0; |
158 | glTranslate $self->{x}, $self->{y}, 0; |
88 | $self->_draw; |
159 | $self->_draw; |
89 | glPopMatrix; |
160 | glPopMatrix; |
|
|
161 | |
|
|
162 | if ($self == $HOVER) { |
|
|
163 | my ($x, $y) = @$self->{qw(x y)}; |
|
|
164 | |
|
|
165 | glColor 1, 1, 1, 0.1; |
|
|
166 | glEnable GL_BLEND; |
|
|
167 | glBegin GL_QUADS; |
|
|
168 | glVertex $x , $y; |
|
|
169 | glVertex $x + $self->{w}, $y; |
|
|
170 | glVertex $x + $self->{w}, $y + $self->{h}; |
|
|
171 | glVertex $x , $y + $self->{h}; |
|
|
172 | glEnd; |
|
|
173 | glDisable GL_BLEND; |
|
|
174 | } |
90 | } |
175 | } |
91 | |
176 | |
92 | sub _draw { |
177 | sub _draw { |
93 | my ($widget) = @_; |
178 | my ($self) = @_; |
|
|
179 | |
|
|
180 | warn "no draw defined for $self\n"; |
94 | } |
181 | } |
95 | |
182 | |
96 | sub bbox { |
183 | sub bbox { |
97 | my ($widget) = @_; |
184 | my ($self) = @_; |
|
|
185 | my ($w, $h) = $self->size_request; |
|
|
186 | ( |
|
|
187 | $self->{x}, |
|
|
188 | $self->{y}, |
|
|
189 | $self->{x} = $w, |
|
|
190 | $self->{y} = $h |
|
|
191 | ) |
|
|
192 | } |
|
|
193 | |
|
|
194 | sub find_widget { |
|
|
195 | my ($self, $x, $y) = @_; |
|
|
196 | |
|
|
197 | return $self |
|
|
198 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
|
|
199 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
|
|
200 | |
|
|
201 | () |
|
|
202 | } |
|
|
203 | |
|
|
204 | sub del_parent { $_[0]->{parent} = undef } |
|
|
205 | |
|
|
206 | sub set_parent { |
|
|
207 | my ($self, $par) = @_; |
|
|
208 | |
|
|
209 | $self->{parent} = $par; |
|
|
210 | Scalar::Util::weaken $self->{parent}; |
|
|
211 | } |
|
|
212 | |
|
|
213 | sub get_parent { |
|
|
214 | $_[0]->{parent} |
|
|
215 | } |
|
|
216 | |
|
|
217 | sub update { |
|
|
218 | my ($self) = @_; |
|
|
219 | |
|
|
220 | $self->{parent}->update |
|
|
221 | if $self->{parent}; |
|
|
222 | } |
|
|
223 | |
|
|
224 | sub connect { |
|
|
225 | my ($self, $signal, $cb) = @_; |
|
|
226 | |
|
|
227 | push @{ $self->{cb}{$signal} }, $cb; |
|
|
228 | } |
|
|
229 | |
|
|
230 | sub emit { |
|
|
231 | my ($self, $signal, @args) = @_; |
|
|
232 | |
|
|
233 | $_->($self, @args) |
|
|
234 | for @{$self->{cb}{$signal} || []}; |
98 | } |
235 | } |
99 | |
236 | |
100 | sub DESTROY { |
237 | sub DESTROY { |
101 | my ($self) = @_; |
238 | my ($self) = @_; |
102 | |
239 | |
103 | $self->deactivate; |
240 | #$self->deactivate; |
104 | } |
241 | } |
105 | |
242 | |
106 | package Crossfire::Client::Widget::Container; |
243 | ############################################################################# |
107 | |
244 | |
|
|
245 | package CFClient::Widget::DrawBG; |
|
|
246 | |
108 | our @ISA = Crossfire::Client::Widget::; |
247 | our @ISA = CFClient::Widget::; |
109 | |
248 | |
|
|
249 | use strict; |
110 | use SDL::OpenGL; |
250 | use SDL::OpenGL; |
111 | |
251 | |
112 | sub add { $_[0]->{child} = $_[1] } |
252 | sub new { |
113 | sub get { $_[0]->{child} } |
253 | my $class = shift; |
114 | |
254 | |
115 | sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } |
255 | # range [value, low, high, page] |
116 | |
256 | |
117 | sub _draw { die "Containers can't be drawn!" } |
257 | $class->SUPER::new ( |
|
|
258 | bg => [0, 0, 0, 0.4], |
|
|
259 | active_bg => [1, 1, 1], |
|
|
260 | @_ |
|
|
261 | ) |
|
|
262 | } |
118 | |
263 | |
|
|
264 | sub _draw { |
|
|
265 | my ($self) = @_; |
|
|
266 | |
|
|
267 | my ($w, $h) = @$self{qw(w h)}; |
|
|
268 | |
|
|
269 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
|
|
270 | glBegin GL_QUADS; |
|
|
271 | glVertex 0 , 0; |
|
|
272 | glVertex 0 , $h; |
|
|
273 | glVertex $w, $h; |
|
|
274 | glVertex $w, 0; |
|
|
275 | glEnd; |
|
|
276 | } |
|
|
277 | |
|
|
278 | ############################################################################# |
|
|
279 | |
|
|
280 | package CFClient::Widget::Empty; |
|
|
281 | |
|
|
282 | our @ISA = CFClient::Widget::; |
|
|
283 | |
|
|
284 | sub size_request { |
|
|
285 | (0, 0) |
|
|
286 | } |
|
|
287 | |
|
|
288 | sub draw { } |
|
|
289 | |
|
|
290 | ############################################################################# |
|
|
291 | |
|
|
292 | package CFClient::Widget::Container; |
|
|
293 | |
|
|
294 | our @ISA = CFClient::Widget::; |
|
|
295 | |
|
|
296 | sub new { |
|
|
297 | my ($class, %arg) = @_; |
|
|
298 | |
|
|
299 | my $children = delete $arg{children} || []; |
|
|
300 | |
|
|
301 | my $self = $class->SUPER::new (children => [], %arg); |
|
|
302 | $self->add ($_) for @$children; |
|
|
303 | |
|
|
304 | $self |
|
|
305 | } |
|
|
306 | |
|
|
307 | sub add { |
|
|
308 | my ($self, $chld, $expand) = @_; |
|
|
309 | |
|
|
310 | $chld->{expand} = $expand; |
|
|
311 | $chld->set_parent ($self); |
|
|
312 | |
|
|
313 | $self->{children} = [ |
|
|
314 | sort { $a->{z} <=> $b->{z} } |
|
|
315 | @{$self->{children}}, $chld |
|
|
316 | ]; |
|
|
317 | |
|
|
318 | $self->size_allocate ($self->{w}, $self->{h}) |
|
|
319 | if $self->{w}; #TODO: check for "realised state" |
|
|
320 | } |
|
|
321 | |
|
|
322 | sub remove { |
|
|
323 | my ($self, $widget) = @_; |
|
|
324 | |
|
|
325 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
|
|
326 | |
|
|
327 | $self->size_allocate ($self->{w}, $self->{h}); |
|
|
328 | } |
|
|
329 | |
|
|
330 | sub find_widget { |
|
|
331 | my ($self, $x, $y) = @_; |
|
|
332 | |
|
|
333 | $x -= $self->{x}; |
|
|
334 | $y -= $self->{y}; |
|
|
335 | |
|
|
336 | my $res; |
|
|
337 | |
|
|
338 | for (reverse @{ $self->{children} }) { |
|
|
339 | $res = $_->find_widget ($x, $y) |
|
|
340 | and return $res; |
|
|
341 | } |
|
|
342 | |
|
|
343 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
|
|
344 | } |
|
|
345 | |
|
|
346 | sub _draw { |
|
|
347 | my ($self) = @_; |
|
|
348 | |
|
|
349 | $_->draw for @{$self->{children}}; |
|
|
350 | } |
|
|
351 | |
|
|
352 | ############################################################################# |
|
|
353 | |
119 | package Crossfire::Client::Widget::Window; |
354 | package CFClient::Widget::Bin; |
120 | |
355 | |
121 | our @ISA = Crossfire::Client::Widget::Container::; |
356 | our @ISA = CFClient::Widget::Container::; |
|
|
357 | |
|
|
358 | sub new { |
|
|
359 | my ($class, %arg) = @_; |
|
|
360 | |
|
|
361 | my $child = (delete $arg{child}) || new CFClient::Widget::Empty::; |
|
|
362 | |
|
|
363 | $class->SUPER::new (children => [$child], %arg) |
|
|
364 | } |
|
|
365 | |
|
|
366 | sub add { |
|
|
367 | my ($self, $widget) = @_; |
|
|
368 | |
|
|
369 | $self->{children} = []; |
|
|
370 | |
|
|
371 | $self->SUPER::add ($widget); |
|
|
372 | } |
|
|
373 | |
|
|
374 | sub remove { |
|
|
375 | my ($self, $widget) = @_; |
|
|
376 | |
|
|
377 | $self->SUPER::remove ($widget); |
|
|
378 | |
|
|
379 | $self->{children} = [new CFClient::Widget::Empty] |
|
|
380 | unless @{$self->{children}}; |
|
|
381 | } |
|
|
382 | |
|
|
383 | sub child { $_[0]->{children}[0] } |
|
|
384 | |
|
|
385 | sub size_request { |
|
|
386 | $_[0]{children}[0]->size_request |
|
|
387 | } |
|
|
388 | |
|
|
389 | sub size_allocate { |
|
|
390 | my ($self, $w, $h) = @_; |
|
|
391 | |
|
|
392 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
393 | |
|
|
394 | $self->SUPER::size_allocate ($w, $h); |
|
|
395 | $self->{children}[0]->size_allocate ($w, $h); |
|
|
396 | } |
|
|
397 | |
|
|
398 | ############################################################################# |
|
|
399 | |
|
|
400 | package CFClient::Widget::Window; |
|
|
401 | |
|
|
402 | our @ISA = CFClient::Widget::Bin::; |
122 | |
403 | |
123 | use SDL::OpenGL; |
404 | use SDL::OpenGL; |
124 | |
405 | |
125 | sub add { |
406 | sub new { |
|
|
407 | my ($class, %arg) = @_; |
|
|
408 | |
|
|
409 | my $self = $class->SUPER::new (%arg); |
|
|
410 | } |
|
|
411 | |
|
|
412 | sub update { |
126 | my ($self, $chld) = @_; |
413 | my ($self) = @_; |
127 | $self->SUPER::add ($chld); |
414 | |
|
|
415 | # we want to do this delayed... |
128 | $self->render_chld; |
416 | $self->render_chld; |
|
|
417 | $self->SUPER::update; |
129 | } |
418 | } |
130 | |
419 | |
131 | sub render_chld { |
420 | sub render_chld { |
132 | my ($self) = @_; |
421 | my ($self) = @_; |
133 | my $chld = $self->get; |
|
|
134 | my ($w, $h) = $self->size_request; |
|
|
135 | |
422 | |
136 | $self->{texture} = |
423 | $self->{texture} = |
137 | Crossfire::Client::Texture->new_from_opengl ( |
424 | CFClient::Texture->new_from_opengl ( |
138 | $w, $h, sub { |
425 | $self->{w}, $self->{h}, sub { $self->child->draw } |
139 | my ($txt, $w, $h) = @_; |
|
|
140 | $chld->_draw; |
|
|
141 | } |
|
|
142 | ); |
426 | ); |
143 | $self->{texture}->upload; |
|
|
144 | } |
427 | } |
145 | |
428 | |
146 | sub size_request { |
429 | sub size_allocate { |
147 | my ($self) = @_; |
430 | my ($self, $w, $h) = @_; |
148 | my $chld = $self->get |
431 | |
149 | or return (0, 0); |
432 | return unless $self->{w} != $w || $self->{h} != $h; |
150 | $chld->size_request |
433 | |
|
|
434 | $self->{w} = $w; |
|
|
435 | $self->{h} = $h; |
|
|
436 | |
|
|
437 | $self->child->size_allocate ($w, $h); |
|
|
438 | |
|
|
439 | $self->render_chld; |
151 | } |
440 | } |
152 | |
441 | |
153 | sub _draw { |
442 | sub _draw { |
154 | my ($self) = @_; |
443 | my ($self) = @_; |
|
|
444 | |
|
|
445 | my ($w, $h) = ($self->w, $self->h); |
155 | |
446 | |
156 | my $tex = $self->{texture} |
447 | my $tex = $self->{texture} |
157 | or return; |
448 | or return; |
158 | |
449 | |
159 | warn "DRAW TEX: $tex->{width} $tex->{height}\n"; |
|
|
160 | glEnable GL_BLEND; |
450 | glEnable GL_BLEND; |
161 | glEnable GL_TEXTURE_2D; |
451 | glEnable GL_TEXTURE_2D; |
162 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
452 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
163 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
164 | |
453 | |
165 | glColor 1, 1, 1; |
454 | $tex->draw_quad (0, 0, $w, $h); |
166 | |
|
|
167 | glBegin GL_QUADS; |
|
|
168 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
169 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
|
|
170 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
|
|
171 | glTexCoord 1, 0; glVertex $tex->{width}, 0; |
|
|
172 | glEnd; |
|
|
173 | |
455 | |
174 | glDisable GL_BLEND; |
456 | glDisable GL_BLEND; |
175 | glDisable GL_TEXTURE_2D; |
457 | glDisable GL_TEXTURE_2D; |
176 | } |
458 | } |
177 | |
459 | |
|
|
460 | ############################################################################# |
|
|
461 | |
178 | package Crossfire::Client::Widget::Frame; |
462 | package CFClient::Widget::Frame; |
179 | |
463 | |
180 | our @ISA = Crossfire::Client::Widget::Container::; |
464 | our @ISA = CFClient::Widget::Bin::; |
181 | |
465 | |
182 | use SDL::OpenGL; |
466 | use SDL::OpenGL; |
183 | |
467 | |
184 | sub size_request { |
468 | sub size_request { |
185 | my ($self) = @_; |
469 | my ($self) = @_; |
186 | my $chld = $self->get |
470 | my $chld = $self->child |
187 | or return (0, 0); |
471 | or return (0, 0); |
|
|
472 | |
|
|
473 | $chld->move (2, 2); |
|
|
474 | |
188 | map { $_ + 4 } $chld->size_request; |
475 | map { $_ + 4 } $chld->size_request; |
189 | } |
476 | } |
190 | |
477 | |
|
|
478 | sub size_allocate { |
|
|
479 | my ($self, $w, $h) = @_; |
|
|
480 | |
|
|
481 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
482 | |
|
|
483 | $self->{w} = $w; |
|
|
484 | $self->{h} = $h; |
|
|
485 | |
|
|
486 | $self->child->size_allocate ($w - 4, $h - 4); |
|
|
487 | $self->child->move (2, 2); |
|
|
488 | } |
|
|
489 | |
191 | sub _draw { |
490 | sub _draw { |
192 | my ($self) = @_; |
491 | my ($self) = @_; |
193 | |
492 | |
194 | my $chld = $self->get; |
493 | my $chld = $self->child; |
195 | |
494 | |
196 | my ($w, $h) = $chld->size_request; |
495 | my ($w, $h) = $chld->size_request; |
197 | |
496 | |
198 | glColor 1, 0, 0; |
|
|
199 | glBegin GL_QUADS; |
497 | glBegin GL_QUADS; |
200 | glTexCoord 0, 0; glVertex 0 , 0; |
498 | glColor 0, 0, 0; |
|
|
499 | glVertex 0 , 0; |
201 | glTexCoord 0, 1; glVertex 0 , $h + 4; |
500 | glVertex 0 , $h + 4; |
202 | glTexCoord 1, 1; glVertex $w + 4 , $h + 4; |
501 | glVertex $w + 4 , $h + 4; |
203 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
502 | glVertex $w + 4 , 0; |
204 | glEnd; |
503 | glEnd; |
205 | |
504 | |
206 | glPushMatrix; |
|
|
207 | glTranslate (2, 2, 0); |
|
|
208 | $chld->_draw; |
505 | $chld->draw; |
209 | glPopMatrix; |
|
|
210 | } |
506 | } |
211 | |
507 | |
|
|
508 | ############################################################################# |
|
|
509 | |
|
|
510 | package CFClient::Widget::FancyFrame; |
|
|
511 | |
|
|
512 | our @ISA = CFClient::Widget::Bin::; |
|
|
513 | |
|
|
514 | use SDL::OpenGL; |
|
|
515 | |
|
|
516 | my @tex = |
|
|
517 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
518 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
519 | |
|
|
520 | sub size_request { |
|
|
521 | my ($self) = @_; |
|
|
522 | |
|
|
523 | my ($w, $h) = $self->SUPER::size_request; |
|
|
524 | |
|
|
525 | $h += $tex[1]->{h}; |
|
|
526 | $h += $tex[4]->{h}; |
|
|
527 | $w += $tex[2]->{w}; |
|
|
528 | $w += $tex[3]->{w}; |
|
|
529 | |
|
|
530 | ($w, $h) |
|
|
531 | } |
|
|
532 | |
|
|
533 | sub size_allocate { |
|
|
534 | my ($self, $w, $h) = @_; |
|
|
535 | |
|
|
536 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
537 | |
|
|
538 | $self->SUPER::size_allocate ($w, $h); |
|
|
539 | |
|
|
540 | $h -= $tex[1]->{h}; |
|
|
541 | $h -= $tex[4]->{h}; |
|
|
542 | $w -= $tex[2]->{w}; |
|
|
543 | $w -= $tex[3]->{w}; |
|
|
544 | |
|
|
545 | $h = $h < 0 ? 0 : $h; |
|
|
546 | $w = $w < 0 ? 0 : $w; |
|
|
547 | |
|
|
548 | my $child = $self->child; |
|
|
549 | |
|
|
550 | $child->size_allocate ($w, $h); |
|
|
551 | $child->move ($tex[3]->{w}, $tex[1]->{h}); |
|
|
552 | } |
|
|
553 | |
|
|
554 | sub _draw { |
|
|
555 | my ($self) = @_; |
|
|
556 | |
|
|
557 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
558 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
|
|
559 | |
|
|
560 | glEnable GL_BLEND; |
|
|
561 | glEnable GL_TEXTURE_2D; |
|
|
562 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
563 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
564 | |
|
|
565 | my $top = $tex[1]; |
|
|
566 | $top->draw_quad (0, 0, $w, $top->{h}); |
|
|
567 | |
|
|
568 | my $left = $tex[3]; |
|
|
569 | $left->draw_quad (0, $top->{h}, $left->{w}, $ch); |
|
|
570 | |
|
|
571 | my $right = $tex[2]; |
|
|
572 | $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch); |
|
|
573 | |
|
|
574 | my $bottom = $tex[4]; |
|
|
575 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
|
|
576 | |
|
|
577 | my $bg = $tex[0]; |
|
|
578 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
|
|
579 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
580 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
|
|
581 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
|
|
582 | |
|
|
583 | my $rep_x = $cw / $bg->{w}; |
|
|
584 | my $rep_y = $ch / $bg->{h}; |
|
|
585 | |
|
|
586 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
|
|
587 | |
|
|
588 | glDisable GL_BLEND; |
|
|
589 | glDisable GL_TEXTURE_2D; |
|
|
590 | |
|
|
591 | $self->child->draw; |
|
|
592 | |
|
|
593 | } |
|
|
594 | |
|
|
595 | ############################################################################# |
|
|
596 | |
212 | package Crossfire::Client::Widget::Table; |
597 | package CFClient::Widget::Table; |
213 | |
598 | |
214 | our @ISA = Crossfire::Client::Widget::Container::; |
599 | our @ISA = CFClient::Widget::; |
215 | |
600 | |
216 | use SDL::OpenGL; |
601 | use SDL::OpenGL; |
217 | |
602 | |
218 | sub add { |
603 | sub add { |
219 | my ($self, $x, $y, $chld) = @_; |
604 | my ($self, $x, $y, $chld) = @_; |
|
|
605 | my $old_chld = $self->{children}[$y][$x]; |
|
|
606 | |
220 | $self->{childs}[$y][$x] = $chld; |
607 | $self->{children}[$y][$x] = $chld; |
|
|
608 | $chld->set_parent ($self); |
|
|
609 | $self->update; |
221 | } |
610 | } |
222 | |
611 | |
223 | sub max_row_height { |
612 | sub max_row_height { |
224 | my ($self, $row) = @_; |
613 | my ($self, $row) = @_; |
225 | |
614 | |
226 | my $hs = 0; |
615 | my $hs = 0; |
227 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { |
616 | for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { |
228 | my $c = $self->{childs}->[$row]->[$xi]; |
617 | my $c = $self->{children}->[$row]->[$xi]; |
229 | if ($c) { |
618 | if ($c) { |
230 | my ($w, $h) = $c->size_request; |
619 | my ($w, $h) = $c->size_request; |
231 | if ($hs < $h) { $hs = $h } |
620 | if ($hs < $h) { $hs = $h } |
232 | } |
621 | } |
233 | } |
622 | } |
… | |
… | |
236 | |
625 | |
237 | sub max_col_width { |
626 | sub max_col_width { |
238 | my ($self, $col) = @_; |
627 | my ($self, $col) = @_; |
239 | |
628 | |
240 | my $ws = 0; |
629 | my $ws = 0; |
241 | for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { |
630 | for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { |
242 | my $c = ($self->{childs}->[$yi] || [])->[$col]; |
631 | my $c = ($self->{children}->[$yi] || [])->[$col]; |
243 | if ($c) { |
632 | if ($c) { |
244 | my ($w, $h) = $c->size_request; |
633 | my ($w, $h) = $c->size_request; |
245 | if ($ws < $w) { $ws = $w } |
634 | if ($ws < $w) { $ws = $w } |
246 | } |
635 | } |
247 | } |
636 | } |
… | |
… | |
251 | sub size_request { |
640 | sub size_request { |
252 | my ($self) = @_; |
641 | my ($self) = @_; |
253 | |
642 | |
254 | my ($hs, $ws) = (0, 0); |
643 | my ($hs, $ws) = (0, 0); |
255 | |
644 | |
256 | for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { |
645 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
257 | $hs += $self->max_row_height ($yi); |
646 | $hs += $self->max_row_height ($yi); |
258 | } |
647 | } |
259 | |
648 | |
260 | for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { |
649 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
261 | my $wm = 0; |
650 | my $wm = 0; |
262 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { |
651 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
263 | $wm += $self->max_col_width ($xi) |
652 | $wm += $self->max_col_width ($xi) |
264 | } |
653 | } |
265 | if ($ws < $wm) { $ws = $wm } |
654 | if ($ws < $wm) { $ws = $wm } |
266 | } |
655 | } |
267 | |
656 | |
… | |
… | |
270 | |
659 | |
271 | sub _draw { |
660 | sub _draw { |
272 | my ($self) = @_; |
661 | my ($self) = @_; |
273 | |
662 | |
274 | my $y = 0; |
663 | my $y = 0; |
275 | for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { |
664 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
276 | my $x = 0; |
665 | my $x = 0; |
277 | |
666 | |
278 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { |
667 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
279 | |
668 | |
280 | glPushMatrix; |
|
|
281 | glTranslate ($x, $y, 0); |
|
|
282 | my $c = $self->{childs}->[$yi]->[$xi]; |
669 | my $c = $self->{children}->[$yi]->[$xi]; |
|
|
670 | if ($c) { |
|
|
671 | $c->move ($x, $y, 0); #TODO: Move to size_request |
283 | $c->_draw if $c; |
672 | $c->draw if $c; |
284 | glPopMatrix; |
673 | } |
285 | |
674 | |
286 | $x += $self->max_col_width ($xi); |
675 | $x += $self->max_col_width ($xi); |
287 | } |
676 | } |
288 | |
677 | |
289 | $y += $self->max_row_height ($yi); |
678 | $y += $self->max_row_height ($yi); |
290 | } |
679 | } |
291 | } |
680 | } |
292 | |
681 | |
|
|
682 | ############################################################################# |
|
|
683 | |
293 | package Crossfire::Client::Widget::VBox; |
684 | package CFClient::Widget::VBox; |
294 | |
685 | |
295 | our @ISA = Crossfire::Client::Widget::Container::; |
686 | our @ISA = CFClient::Widget::Container::; |
296 | |
687 | |
297 | use SDL::OpenGL; |
688 | use SDL::OpenGL; |
298 | |
689 | |
299 | sub add { |
|
|
300 | my ($self, $chld) = @_; |
|
|
301 | push @{$self->{childs}}, $chld; |
|
|
302 | } |
|
|
303 | |
|
|
304 | sub size_request { |
690 | sub size_request { |
305 | my ($self) = @_; |
691 | my ($self) = @_; |
306 | |
692 | |
307 | my ($hs, $ws) = (0, 0); |
693 | my @alloc = map [$_->size_request], @{$self->{children}}; |
308 | for (@{$self->{childs} || []}) { |
694 | |
309 | my ($w, $h) = $_->size_request; |
|
|
310 | $hs += $h; |
|
|
311 | if ($ws < $w) { $ws = $w } |
|
|
312 | } |
695 | ( |
313 | |
696 | (List::Util::max map $_->[0], @alloc), |
314 | return ($ws, $hs); |
697 | (List::Util::sum map $_->[1], @alloc), |
|
|
698 | ) |
315 | } |
699 | } |
316 | |
700 | |
317 | sub _draw { |
701 | sub size_allocate { |
318 | my ($self) = @_; |
702 | my ($self, $w, $h) = @_; |
319 | |
703 | |
320 | my ($x, $y); |
704 | return unless $self->{w} != $w || $self->{h} != $h; |
321 | for (@{$self->{childs} || []}) { |
705 | |
322 | glPushMatrix; |
706 | $self->{w} = $w; |
323 | glTranslate (0, $y, 0); |
707 | $self->{h} = $h; |
324 | $_->_draw; |
708 | |
325 | glPopMatrix; |
709 | return unless $self->{h}; |
326 | my ($w, $h) = $_->size_request; |
710 | |
|
|
711 | my $children = $self->{children}; |
|
|
712 | |
|
|
713 | my @h = map +($_->size_request)[1], @$children; |
|
|
714 | |
|
|
715 | my $req_h = List::Util::sum @h; |
|
|
716 | |
|
|
717 | if ($req_h > $h) { |
|
|
718 | # ah well, not enough space |
|
|
719 | $_ = $h[$_] * $h / $req_h for @h; |
|
|
720 | } else { |
|
|
721 | my @exp = grep $_->{expand}, @$children; |
|
|
722 | @exp = @$children unless @exp; |
|
|
723 | |
|
|
724 | my %exp = map +($_ => 1), @exp; |
|
|
725 | |
|
|
726 | for (0 .. $#$children) { |
|
|
727 | my $child = $children->[$_]; |
|
|
728 | |
|
|
729 | my $alloc_h = $h[$_]; |
|
|
730 | $alloc_h += ($h - $req_h) / @exp if $exp{$child}; |
|
|
731 | $h[$_] = $alloc_h; |
|
|
732 | } |
|
|
733 | } |
|
|
734 | |
|
|
735 | my $y = 0; |
|
|
736 | for (0 .. $#$children) { |
|
|
737 | my $child = $children->[$_]; |
|
|
738 | my $h = $h[$_]; |
|
|
739 | $child->move (0, $y); |
|
|
740 | $child->size_allocate ($w, $h); |
|
|
741 | |
327 | $y += $h; |
742 | $y += $h; |
328 | } |
743 | } |
329 | } |
744 | } |
330 | |
745 | |
|
|
746 | ############################################################################# |
|
|
747 | |
331 | package Crossfire::Client::Widget::Label; |
748 | package CFClient::Widget::Label; |
332 | |
749 | |
333 | our @ISA = Crossfire::Client::Widget::; |
750 | our @ISA = CFClient::Widget::; |
334 | |
751 | |
335 | use SDL::OpenGL; |
752 | use SDL::OpenGL; |
336 | |
753 | |
337 | sub new { |
754 | sub new { |
338 | my ($class, $x, $y, $z, $ttf, $text) = @_; |
755 | my ($class, %arg) = @_; |
339 | |
756 | |
340 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf); |
757 | my $self = $class->SUPER::new ( |
|
|
758 | fg => [1, 1, 1], |
|
|
759 | height => $::FONTSIZE, |
|
|
760 | text => "", |
|
|
761 | align => -1, |
|
|
762 | layout => new CFClient::Layout, |
|
|
763 | %arg |
|
|
764 | ); |
341 | |
765 | |
342 | $self->set_text ($text); |
766 | $self->set_text ($self->{text}); |
343 | |
767 | |
344 | $self |
768 | $self |
|
|
769 | } |
|
|
770 | |
|
|
771 | sub escape_text { |
|
|
772 | local $_ = $_[1]; |
|
|
773 | |
|
|
774 | s/&/&/g; |
|
|
775 | s/>/>/g; |
|
|
776 | s/</</g; |
|
|
777 | |
|
|
778 | $_[1] |
345 | } |
779 | } |
346 | |
780 | |
347 | sub set_text { |
781 | sub set_text { |
348 | my ($self, $text) = @_; |
782 | my ($self, $text) = @_; |
349 | $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text; |
783 | |
|
|
784 | $self->{text} = $text; |
|
|
785 | $self->{layout}->set_markup ($text); |
|
|
786 | |
|
|
787 | delete $self->{texture}; |
|
|
788 | $self->update; |
350 | } |
789 | } |
351 | |
790 | |
352 | sub get_text { |
791 | sub get_text { |
353 | my ($self, $text) = @_; |
792 | my ($self, $text) = @_; |
|
|
793 | |
354 | $self->{text} |
794 | $self->{text} |
355 | } |
795 | } |
356 | |
796 | |
357 | sub size_request { |
797 | sub size_request { |
358 | my ($self) = @_; |
798 | my ($self) = @_; |
359 | |
799 | |
360 | ( |
800 | $self->{layout}->set_width; |
|
|
801 | $self->{layout}->set_height ($self->{height}); |
|
|
802 | $self->{layout}->size |
|
|
803 | # if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
|
|
804 | # ( |
361 | $self->{texture}{width}, |
805 | # $self->{texture}{w}, |
362 | $self->{texture}{height}, |
806 | # $self->{texture}{h}, |
363 | ) |
807 | # ) |
|
|
808 | # } else { |
|
|
809 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h}; |
|
|
810 | # |
|
|
811 | # ($w, $h) |
|
|
812 | # } |
|
|
813 | } |
|
|
814 | |
|
|
815 | sub size_allocate { |
|
|
816 | my ($self, $w, $h) = @_; |
|
|
817 | |
|
|
818 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
819 | |
|
|
820 | $self->SUPER::size_allocate ($w, $h); |
|
|
821 | delete $self->{texture}; |
|
|
822 | } |
|
|
823 | |
|
|
824 | sub update { |
|
|
825 | my ($self) = @_; |
|
|
826 | |
|
|
827 | delete $self->{texture}; |
|
|
828 | $self->SUPER::update; |
364 | } |
829 | } |
365 | |
830 | |
366 | sub _draw { |
831 | sub _draw { |
367 | my ($self) = @_; |
832 | my ($self) = @_; |
368 | |
833 | |
369 | my $tex = $self->{texture}; |
834 | my $tex = $self->{texture} ||= do { |
|
|
835 | $self->{layout}->set_width ($self->{w}); |
|
|
836 | new_from_layout CFClient::Texture $self->{layout}; |
|
|
837 | }; |
370 | |
838 | |
371 | glEnable GL_BLEND; |
839 | glEnable GL_BLEND; |
372 | glEnable GL_TEXTURE_2D; |
840 | glEnable GL_TEXTURE_2D; |
|
|
841 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
373 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
842 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
374 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
375 | |
843 | |
376 | glColor 1, 1, 1; |
844 | glColor @{$self->{fg}}; |
377 | |
845 | |
378 | glBegin GL_QUADS; |
846 | my $x = |
379 | glTexCoord 0, 0; glVertex 0 , 0; |
847 | $self->{align} < 0 ? 0 |
380 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
848 | : $self->{align} > 0 ? $self->{w} - $self->{texture}{w} |
381 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
849 | : ($self->{w} + $self->{texture}{w}) * 0.5; |
382 | glTexCoord 1, 0; glVertex $tex->{width}, 0; |
850 | |
383 | glEnd; |
851 | $tex->draw_quad ($x, 0); |
384 | |
852 | |
385 | glDisable GL_BLEND; |
853 | glDisable GL_BLEND; |
386 | glDisable GL_TEXTURE_2D; |
854 | glDisable GL_TEXTURE_2D; |
387 | } |
855 | } |
388 | |
856 | |
389 | package Crossfire::Client::Widget::TextView; |
857 | ############################################################################# |
|
|
858 | |
|
|
859 | package CFClient::Widget::Entry; |
|
|
860 | |
|
|
861 | our @ISA = CFClient::Widget::Label::; |
|
|
862 | |
|
|
863 | use SDL; |
|
|
864 | use SDL::OpenGL; |
|
|
865 | |
|
|
866 | sub new { |
|
|
867 | my $class = shift; |
|
|
868 | |
|
|
869 | $class->SUPER::new ( |
|
|
870 | fg => [1, 1, 1], |
|
|
871 | bg => [0, 0, 0, 0.4], |
|
|
872 | active_bg => [1, 1, 1], |
|
|
873 | active_fg => [0, 0, 0], |
|
|
874 | @_ |
|
|
875 | ) |
|
|
876 | } |
|
|
877 | |
|
|
878 | sub _set_text { |
|
|
879 | my ($self, $text) = @_; |
|
|
880 | |
|
|
881 | $self->{last_activity} = $::NOW; |
|
|
882 | |
|
|
883 | $self->{text} = $text; |
|
|
884 | $self->{layout}->set_width ($self->{w}); |
|
|
885 | |
|
|
886 | $text =~ s/./*/g if $self->{hidden}; |
|
|
887 | |
|
|
888 | |
|
|
889 | $self->{layout}->set_markup ($self->escape_text ($text)); |
|
|
890 | |
|
|
891 | $text = substr $text, 0, $self->{cursor}; |
|
|
892 | utf8::encode $text; |
|
|
893 | |
|
|
894 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
895 | } |
|
|
896 | |
|
|
897 | sub size_request { |
|
|
898 | my ($self) = @_; |
|
|
899 | |
|
|
900 | my ($w, $h) = $self->SUPER::size_request; |
|
|
901 | |
|
|
902 | ($w + 1, $h) # add 1 for cursor |
|
|
903 | } |
|
|
904 | |
|
|
905 | sub size_allocate { |
|
|
906 | my ($self, $w, $h) = @_; |
|
|
907 | |
|
|
908 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
909 | |
|
|
910 | $self->SUPER::size_allocate ($w, $h); |
|
|
911 | |
|
|
912 | $self->_set_text ($self->{text}); |
|
|
913 | } |
|
|
914 | |
|
|
915 | sub set_text { |
|
|
916 | my ($self, $text) = @_; |
|
|
917 | |
|
|
918 | $self->{cursor} = length $text; |
|
|
919 | $self->_set_text ($text); |
|
|
920 | $self->update; |
|
|
921 | } |
|
|
922 | |
|
|
923 | sub key_down { |
|
|
924 | my ($self, $ev) = @_; |
|
|
925 | |
|
|
926 | my $mod = $ev->key_mod; |
|
|
927 | my $sym = $ev->key_sym; |
|
|
928 | |
|
|
929 | my $uni = $ev->key_unicode; |
|
|
930 | |
|
|
931 | my $text = $self->get_text; |
|
|
932 | |
|
|
933 | if ($sym == SDLK_BACKSPACE) { |
|
|
934 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
|
|
935 | } elsif ($sym == SDLK_DELETE) { |
|
|
936 | substr $text, $self->{cursor}, 1, ""; |
|
|
937 | } elsif ($sym == SDLK_LEFT) { |
|
|
938 | --$self->{cursor} if $self->{cursor}; |
|
|
939 | } elsif ($sym == SDLK_RIGHT) { |
|
|
940 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
|
|
941 | } elsif ($uni) { |
|
|
942 | substr $text, $self->{cursor}++, 0, chr $uni; |
|
|
943 | } |
|
|
944 | |
|
|
945 | $self->_set_text ($text); |
|
|
946 | $self->update; |
|
|
947 | } |
|
|
948 | |
|
|
949 | sub focus_in { |
|
|
950 | my ($self) = @_; |
|
|
951 | |
|
|
952 | $self->{last_activity} = $::NOW; |
|
|
953 | |
|
|
954 | $self->SUPER::focus_in; |
|
|
955 | } |
|
|
956 | |
|
|
957 | sub button_down { |
|
|
958 | my ($self, $ev, $x, $y) = @_; |
|
|
959 | |
|
|
960 | $self->SUPER::button_down ($ev, $x, $y); |
|
|
961 | |
|
|
962 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
|
|
963 | |
|
|
964 | # byte-index to char-index |
|
|
965 | my $text = $self->{layout}; |
|
|
966 | utf8::encode $text; |
|
|
967 | $self->{cursor} = length substr $text, 0, $idx; |
|
|
968 | |
|
|
969 | $self->_set_text ($self->{text}); |
|
|
970 | $self->update; |
|
|
971 | } |
|
|
972 | |
|
|
973 | sub mouse_motion { |
|
|
974 | my ($self, $ev, $x, $y) = @_; |
|
|
975 | # printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
|
|
976 | } |
|
|
977 | |
|
|
978 | sub _draw { |
|
|
979 | my ($self) = @_; |
|
|
980 | |
|
|
981 | local $self->{fg} = $self->{fg}; |
|
|
982 | |
|
|
983 | if ($FOCUS == $self) { |
|
|
984 | glColor @{$self->{active_bg}}; |
|
|
985 | $self->{fg} = $self->{active_fg}; |
|
|
986 | } else { |
|
|
987 | glColor @{$self->{bg}}; |
|
|
988 | } |
|
|
989 | |
|
|
990 | glBegin GL_QUADS; |
|
|
991 | glVertex 0 , 0; |
|
|
992 | glVertex 0 , $self->{h}; |
|
|
993 | glVertex $self->{w}, $self->{h}; |
|
|
994 | glVertex $self->{w}, 0; |
|
|
995 | glEnd; |
|
|
996 | |
|
|
997 | $self->SUPER::_draw; |
|
|
998 | |
|
|
999 | #TODO: force update every cursor change :( |
|
|
1000 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
|
|
1001 | glColor @{$self->{fg}}; |
|
|
1002 | glBegin GL_LINES; |
|
|
1003 | glVertex $self->{cur_x}, $self->{cur_y}; |
|
|
1004 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
|
|
1005 | glEnd; |
|
|
1006 | } |
|
|
1007 | } |
|
|
1008 | |
|
|
1009 | ############################################################################# |
|
|
1010 | |
|
|
1011 | package CFClient::Widget::Slider; |
390 | |
1012 | |
391 | use strict; |
1013 | use strict; |
392 | |
|
|
393 | our @ISA = qw/Crossfire::Client::Widget/; |
|
|
394 | |
1014 | |
395 | use SDL::OpenGL; |
1015 | use SDL::OpenGL; |
396 | use SDL::OpenGL::Constants; |
1016 | use SDL::OpenGL::Constants; |
397 | |
1017 | |
398 | sub add_line { |
1018 | our @ISA = CFClient::Widget::DrawBG::; |
|
|
1019 | |
|
|
1020 | sub size_request { |
399 | my ($self, $line) = @_; |
1021 | my ($self) = @_; |
400 | push @{$self->{lines}}, $line; |
1022 | |
|
|
1023 | my $w = 50; |
|
|
1024 | my $h = 10; |
|
|
1025 | |
|
|
1026 | $self->{vertical} ? ($h, $w) : ($w, $h) |
|
|
1027 | } |
|
|
1028 | |
|
|
1029 | sub new { |
|
|
1030 | my $class = shift; |
|
|
1031 | |
|
|
1032 | # range [value, low, high, page] |
|
|
1033 | |
|
|
1034 | $class->SUPER::new ( |
|
|
1035 | fg => [1, 1, 1], |
|
|
1036 | active_fg => [0, 0, 0], |
|
|
1037 | range => [0, 0, 100, 10], |
|
|
1038 | vertical => 1, |
|
|
1039 | @_ |
|
|
1040 | ) |
|
|
1041 | } |
|
|
1042 | |
|
|
1043 | sub button_down { |
|
|
1044 | my ($self, $ev, $x, $y) = @_; |
|
|
1045 | |
|
|
1046 | $self->SUPER::button_down ($ev, $x, $y); |
|
|
1047 | $self->mouse_motion ($ev, $x, $y); |
|
|
1048 | } |
|
|
1049 | |
|
|
1050 | sub mouse_motion { |
|
|
1051 | my ($self, $ev, $x, $y) = @_; |
|
|
1052 | |
|
|
1053 | if ($GRAB == $self) { |
|
|
1054 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
1055 | |
|
|
1056 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
|
|
1057 | |
|
|
1058 | $x = $x * ($hi - $lo) / $w + $lo; |
|
|
1059 | $x = $lo if $x < $lo; |
|
|
1060 | $x = $hi - $page if $x > $hi - $page; |
|
|
1061 | $self->{range}[0] = $x; |
|
|
1062 | |
|
|
1063 | $self->emit (changed => $x); |
|
|
1064 | $self->update; |
|
|
1065 | } |
401 | } |
1066 | } |
402 | |
1067 | |
403 | sub _draw { |
1068 | sub _draw { |
404 | my ($self) = @_; |
1069 | my ($self) = @_; |
405 | |
1070 | |
406 | } |
1071 | $self->SUPER::_draw (); |
407 | |
1072 | |
|
|
1073 | my ($w, $h) = @$self{qw(w h)}; |
|
|
1074 | |
|
|
1075 | if ($self->{vertical}) { |
|
|
1076 | # draw a vertical slider like a rotated horizontal slider |
|
|
1077 | |
|
|
1078 | glRotate 90, 0, 0, 1; |
|
|
1079 | glTranslate 0, -$self->{w}, 0; |
|
|
1080 | |
|
|
1081 | ($w, $h) = ($h, $w); |
|
|
1082 | } |
|
|
1083 | |
|
|
1084 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
|
|
1085 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
|
|
1086 | |
|
|
1087 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
1088 | |
|
|
1089 | $page = int $page * $w / ($hi - $lo); |
|
|
1090 | $value = int +($value - $lo) * $w / ($hi - $lo); |
|
|
1091 | |
|
|
1092 | $w -= $page; |
|
|
1093 | $page &= ~1; |
|
|
1094 | glTranslate $page * 0.5, 0, 0; |
|
|
1095 | |
|
|
1096 | glColor @$fg; |
|
|
1097 | glBegin GL_LINES; |
|
|
1098 | glVertex 0, 0; glVertex 0, $h; |
|
|
1099 | glVertex $w - 1, 0; glVertex $w - 1, $h; |
|
|
1100 | glVertex 0, $h * 0.5; glVertex $w, $h * 0.5; |
|
|
1101 | glEnd; |
|
|
1102 | |
|
|
1103 | my $knob_a = $value - $page * 0.5; |
|
|
1104 | my $knob_b = $value + $page * 0.5; |
|
|
1105 | |
|
|
1106 | glBegin GL_QUADS; |
|
|
1107 | glColor @$fg; |
|
|
1108 | glVertex $knob_a, 0; |
|
|
1109 | glVertex $knob_a, $h; |
|
|
1110 | glVertex $knob_b, $h; |
|
|
1111 | glVertex $knob_b, 0; |
|
|
1112 | |
|
|
1113 | if ($knob_a < $knob_b - 2) { |
|
|
1114 | glColor @$bg; |
|
|
1115 | glVertex $knob_a + 1, 1; |
|
|
1116 | glVertex $knob_a + 1, $h - 1; |
|
|
1117 | glVertex $knob_b - 1, $h - 1; |
|
|
1118 | glVertex $knob_b - 1, 1; |
|
|
1119 | } |
|
|
1120 | glEnd; |
|
|
1121 | } |
|
|
1122 | |
|
|
1123 | ############################################################################# |
|
|
1124 | |
408 | package Crossfire::Client::Widget::MapWidget; |
1125 | package CFClient::Widget::MapWidget; |
409 | |
1126 | |
410 | use strict; |
1127 | use strict; |
411 | |
1128 | |
412 | our @ISA = qw/Crossfire::Client::Widget/; |
1129 | use List::Util qw(min max); |
413 | |
1130 | |
414 | use SDL; |
1131 | use SDL; |
415 | use SDL::OpenGL; |
1132 | use SDL::OpenGL; |
416 | use SDL::OpenGL::Constants; |
1133 | use SDL::OpenGL::Constants; |
417 | |
1134 | |
|
|
1135 | our @ISA = CFClient::Widget::; |
|
|
1136 | |
|
|
1137 | sub new { |
|
|
1138 | my $class = shift; |
|
|
1139 | |
|
|
1140 | $class->SUPER::new ( |
|
|
1141 | z => -1, |
|
|
1142 | list => (glGenLists 1), |
|
|
1143 | @_ |
|
|
1144 | ) |
|
|
1145 | } |
|
|
1146 | |
418 | sub key_down { |
1147 | sub key_down { |
419 | print "MAPKEYDOWN\n"; |
1148 | print "MAPKEYDOWN\n"; |
420 | } |
1149 | } |
421 | |
1150 | |
422 | sub key_up { |
1151 | sub key_up { |
423 | } |
1152 | } |
424 | |
1153 | |
|
|
1154 | sub size_request { |
|
|
1155 | ( |
|
|
1156 | 1 + int $::WIDTH / 32, |
|
|
1157 | 1 + int $::HEIGHT / 32, |
|
|
1158 | ) |
|
|
1159 | } |
|
|
1160 | |
|
|
1161 | sub update { |
|
|
1162 | my ($self) = @_; |
|
|
1163 | |
|
|
1164 | $self->{need_update} = 1; |
|
|
1165 | } |
|
|
1166 | |
425 | sub _draw { |
1167 | sub _draw { |
426 | my ($self) = @_; |
1168 | my ($self) = @_; |
427 | |
1169 | |
428 | glEnable GL_TEXTURE_2D; |
1170 | if (delete $self->{need_update}) { |
429 | glEnable GL_BLEND; |
1171 | glNewList $self->{list}, GL_COMPILE; |
430 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
431 | |
1172 | |
|
|
1173 | my $mx = $::CONN->{mapx}; |
|
|
1174 | my $my = $::CONN->{mapy}; |
|
|
1175 | |
432 | my $map = $::CONN->{map}; |
1176 | my $map = $::CONN->{map}; |
433 | |
1177 | |
434 | for my $x (0 .. int $::WIDTH / 32) { |
1178 | my ($xofs, $yofs); |
435 | for my $y (0 .. int $::HEIGHT / 32) { |
|
|
436 | |
1179 | |
437 | my $cell = $map->[$x + $::CONN->{mapx}] |
1180 | my $sw = 1 + int $::WIDTH / 32; |
438 | [$y + $::CONN->{mapy}] |
1181 | my $sh = 1 + int $::HEIGHT / 32; |
|
|
1182 | |
|
|
1183 | if ($::CONN->{mapw} > $sw) { |
|
|
1184 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
|
|
1185 | } else { |
|
|
1186 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
|
|
1187 | } |
|
|
1188 | |
|
|
1189 | if ($::CONN->{maph} > $sh) { |
|
|
1190 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
|
|
1191 | } else { |
|
|
1192 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
|
|
1193 | } |
|
|
1194 | |
|
|
1195 | glEnable GL_TEXTURE_2D; |
|
|
1196 | glEnable GL_BLEND; |
|
|
1197 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1198 | |
|
|
1199 | my $sw4 = ($sw + 3) & ~3; |
|
|
1200 | my $darkness = "\x00" x ($sw4 * $sh); |
|
|
1201 | |
|
|
1202 | for my $x (0 .. $sw - 1) { |
|
|
1203 | my $row = $map->[$x + $xofs]; |
|
|
1204 | for my $y (0 .. $sh - 1) { |
|
|
1205 | |
|
|
1206 | my $cell = $row->[$y + $yofs] |
439 | or next; |
1207 | or next; |
440 | |
1208 | |
441 | my $darkness = $cell->[0] * (1 / 255); |
1209 | my $dark = $cell->[0]; |
442 | if ($darkness < 0) { |
1210 | if ($dark < 0) { |
443 | $darkness = 0.5; |
1211 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
|
|
1212 | } else { |
|
|
1213 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
444 | } |
1214 | } |
445 | glColor $darkness, $darkness, $darkness; |
|
|
446 | |
1215 | |
447 | for my $num (grep $_, @$cell[1,2,3]) { |
1216 | for my $num (grep $_, @$cell[1,2,3]) { |
448 | my $tex = $::CONN->{face}[$num]{texture} || next; |
1217 | my $tex = $::CONN->{face}[$num]{texture} || next; |
|
|
1218 | |
|
|
1219 | my ($w, $h) = @$tex{qw(w h)}; |
449 | |
1220 | |
450 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
1221 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
451 | |
|
|
452 | my $w = $tex->{width}; |
|
|
453 | my $h = $tex->{height}; |
|
|
454 | |
|
|
455 | my $px = ($x + 1) * 32 - $w; |
|
|
456 | my $py = ($y + 1) * 32 - $h; |
|
|
457 | |
|
|
458 | glBegin GL_QUADS; |
|
|
459 | glTexCoord 0, 0; glVertex $px , $py; |
|
|
460 | glTexCoord 0, 1; glVertex $px , $py + $h; |
|
|
461 | glTexCoord 1, 1; glVertex $px + $w, $py + $h; |
|
|
462 | glTexCoord 1, 0; glVertex $px + $w, $py; |
|
|
463 | glEnd; |
1222 | } |
464 | } |
1223 | } |
465 | } |
1224 | } |
|
|
1225 | |
|
|
1226 | # if (1) { # higher quality darkness |
|
|
1227 | # $lighting =~ s/(.)/$1$1$1/gs; |
|
|
1228 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
|
|
1229 | # |
|
|
1230 | # $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear"); |
|
|
1231 | # |
|
|
1232 | # $lighting = $pb->get_pixels; |
|
|
1233 | # $lighting =~ s/(.)../$1/gs; |
466 | } |
1234 | # } |
467 | |
1235 | |
|
|
1236 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1237 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1238 | |
|
|
1239 | $darkness = new CFClient::Texture |
|
|
1240 | width => $sw4, |
|
|
1241 | height => $sh, |
|
|
1242 | data => $darkness, |
|
|
1243 | internalformat => GL_ALPHA, |
|
|
1244 | format => GL_ALPHA; |
|
|
1245 | |
|
|
1246 | glColor 0.45, 0.45, 0.45, 1; |
|
|
1247 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
|
|
1248 | |
468 | glDisable GL_TEXTURE_2D; |
1249 | glDisable GL_TEXTURE_2D; |
469 | glDisable GL_BLEND; |
1250 | glDisable GL_BLEND; |
|
|
1251 | |
|
|
1252 | glEndList; |
|
|
1253 | } |
|
|
1254 | |
|
|
1255 | glCallList $self->{list}; |
470 | } |
1256 | } |
471 | |
1257 | |
472 | my %DIR = ( |
1258 | my %DIR = ( |
473 | SDLK_KP8, [1, "north"], |
1259 | SDLK_KP8, [1, "north"], |
474 | SDLK_KP9, [2, "northeast"], |
1260 | SDLK_KP9, [2, "northeast"], |
… | |
… | |
518 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
1304 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
519 | $::CONN->send ("command run_stop"); |
1305 | $::CONN->send ("command run_stop"); |
520 | } |
1306 | } |
521 | } |
1307 | } |
522 | |
1308 | |
523 | 1; |
1309 | ############################################################################# |
524 | |
1310 | |
|
|
1311 | package CFClient::Widget::Animator; |
|
|
1312 | |
|
|
1313 | use SDL::OpenGL; |
|
|
1314 | |
|
|
1315 | our @ISA = CFClient::Widget::Bin::; |
|
|
1316 | |
|
|
1317 | sub moveto { |
|
|
1318 | my ($self, $x, $y) = @_; |
|
|
1319 | |
|
|
1320 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
|
|
1321 | $self->{speed} = 0.001; |
|
|
1322 | $self->{time} = 1; |
|
|
1323 | |
|
|
1324 | ::animation_start $self; |
|
|
1325 | } |
|
|
1326 | |
|
|
1327 | sub animate { |
|
|
1328 | my ($self, $interval) = @_; |
|
|
1329 | |
|
|
1330 | $self->{time} -= $interval * $self->{speed}; |
|
|
1331 | if ($self->{time} <= 0) { |
|
|
1332 | $self->{time} = 0; |
|
|
1333 | ::animation_stop $self; |
|
|
1334 | } |
|
|
1335 | |
|
|
1336 | my ($x0, $y0, $x1, $y1) = @{$self->{moveto}}; |
|
|
1337 | |
|
|
1338 | $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time}); |
|
|
1339 | $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time}); |
|
|
1340 | } |
|
|
1341 | |
|
|
1342 | sub _draw { |
|
|
1343 | my ($self) = @_; |
|
|
1344 | |
|
|
1345 | glPushMatrix; |
|
|
1346 | glRotate $self->{time} * 1000, 0, 1, 0; |
|
|
1347 | $self->{children}[0]->draw; |
|
|
1348 | glPopMatrix; |
|
|
1349 | } |
|
|
1350 | |
|
|
1351 | ############################################################################# |
|
|
1352 | |
|
|
1353 | package CFClient::Widget::Toplevel; |
|
|
1354 | |
|
|
1355 | our @ISA = CFClient::Widget::Container::; |
|
|
1356 | |
|
|
1357 | sub size_request { |
|
|
1358 | ($::WIDTH, $::HEIGHT) |
|
|
1359 | } |
|
|
1360 | |
|
|
1361 | sub size_allocate { |
|
|
1362 | my ($self, $w, $h) = @_; |
|
|
1363 | |
|
|
1364 | $self->SUPER::size_allocate ($w, $h); |
|
|
1365 | |
|
|
1366 | $_->size_allocate ($_->size_request) |
|
|
1367 | for @{$self->{children}}; |
|
|
1368 | } |
|
|
1369 | |
|
|
1370 | sub translate { |
|
|
1371 | my ($self, $x, $y) = @_; |
|
|
1372 | |
|
|
1373 | ($x, $y) |
|
|
1374 | } |
|
|
1375 | |
|
|
1376 | sub update { |
|
|
1377 | my ($self) = @_; |
|
|
1378 | |
|
|
1379 | $self->size_allocate ($self->size_request); |
|
|
1380 | ::refresh (); |
|
|
1381 | } |
|
|
1382 | |
|
|
1383 | sub add { |
|
|
1384 | my ($self, $widget) = @_; |
|
|
1385 | |
|
|
1386 | $self->SUPER::add ($widget); |
|
|
1387 | |
|
|
1388 | $widget->size_allocate ($widget->size_request); |
|
|
1389 | } |
|
|
1390 | |
|
|
1391 | sub draw { |
|
|
1392 | my ($self) = @_; |
|
|
1393 | |
|
|
1394 | $self->_draw; |
|
|
1395 | } |
|
|
1396 | |
|
|
1397 | ############################################################################# |
|
|
1398 | |
|
|
1399 | package CFClient::Widget; |
|
|
1400 | |
|
|
1401 | $TOPLEVEL = new CFClient::Widget::Toplevel; |
|
|
1402 | |
|
|
1403 | 1 |
|
|
1404 | |