1 |
package Crossfire::Client::Widget; |
2 |
|
3 |
use strict; |
4 |
|
5 |
use Scalar::Util; |
6 |
|
7 |
use SDL::OpenGL; |
8 |
use SDL::OpenGL::Constants; |
9 |
|
10 |
use Crossfire::Client; |
11 |
|
12 |
our ($FOCUS, $HOVER, $GRAB); # various widgets |
13 |
|
14 |
our $TOPLEVEL; |
15 |
our $BUTTON_STATE; |
16 |
|
17 |
# class methods for events |
18 |
sub feed_sdl_key_down_event { |
19 |
$FOCUS->key_down ($_[0]) if $FOCUS; |
20 |
} |
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) 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 |
if (!$BUTTON_STATE) { |
51 |
my $grab = $GRAB; undef $GRAB; |
52 |
$grab->update if $grab; |
53 |
$GRAB->update if $GRAB; |
54 |
} |
55 |
} |
56 |
|
57 |
sub feed_sdl_motion_event { |
58 |
my ($ev) = @_; |
59 |
my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
60 |
|
61 |
my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
62 |
|
63 |
if ($widget != $HOVER) { |
64 |
my $hover = $HOVER; $HOVER = $widget; |
65 |
|
66 |
$hover->update if $hover; |
67 |
$HOVER->update if $HOVER; |
68 |
} |
69 |
|
70 |
$HOVER->mouse_motion ($ev) if $HOVER; |
71 |
} |
72 |
|
73 |
sub new { |
74 |
my $class = shift; |
75 |
|
76 |
bless { @_ }, $class |
77 |
} |
78 |
|
79 |
sub move { |
80 |
my ($self, $x, $y, $z) = @_; |
81 |
$self->{x} = $x; |
82 |
$self->{y} = $y; |
83 |
$self->{z} = $z if defined $z; |
84 |
} |
85 |
|
86 |
sub needs_redraw { |
87 |
0 |
88 |
} |
89 |
|
90 |
sub size_request { |
91 |
require Carp; |
92 |
Carp::confess "size_request is abtract"; |
93 |
} |
94 |
|
95 |
sub size_allocate { |
96 |
my ($self, $w, $h) = @_; |
97 |
|
98 |
$self->{w} = $w; |
99 |
$self->{h} = $h; |
100 |
} |
101 |
|
102 |
sub focus_in { |
103 |
my ($self) = @_; |
104 |
|
105 |
my $focus = $FOCUS; $FOCUS = $self; |
106 |
$focus->update if $focus; |
107 |
$FOCUS->update; |
108 |
} |
109 |
|
110 |
sub focus_out { |
111 |
my ($self) = @_; |
112 |
|
113 |
return unless $FOCUS == $self; |
114 |
|
115 |
my $focus = $FOCUS; undef $FOCUS; |
116 |
$focus->update if $focus; #? |
117 |
} |
118 |
|
119 |
sub mouse_motion { } |
120 |
sub button_up { } |
121 |
sub button_down { } |
122 |
sub key_down { } |
123 |
sub key_up { } |
124 |
|
125 |
sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
126 |
sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
127 |
sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
128 |
sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
129 |
sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
130 |
|
131 |
sub draw { |
132 |
my ($self) = @_; |
133 |
|
134 |
glPushMatrix; |
135 |
glTranslate $self->{x}, $self->{y}, 0; |
136 |
$self->_draw; |
137 |
if ($self == $HOVER) { |
138 |
glColor 1, 1, 1, 0.4; |
139 |
glEnable GL_BLEND; |
140 |
glBegin GL_QUADS; |
141 |
glVertex 0 , 0; |
142 |
glVertex $self->{w}, 0; |
143 |
glVertex $self->{w}, $self->{h}; |
144 |
glVertex 0 , $self->{h}; |
145 |
glEnd; |
146 |
glDisable GL_BLEND; |
147 |
} |
148 |
glPopMatrix; |
149 |
} |
150 |
|
151 |
sub _draw { |
152 |
my ($self) = @_; |
153 |
|
154 |
warn "no draw defined for $self\n"; |
155 |
} |
156 |
|
157 |
sub bbox { |
158 |
my ($self) = @_; |
159 |
my ($w, $h) = $self->size_request; |
160 |
( |
161 |
$self->{x}, |
162 |
$self->{y}, |
163 |
$self->{x} = $w, |
164 |
$self->{y} = $h |
165 |
) |
166 |
} |
167 |
|
168 |
sub find_widget { |
169 |
my ($self, $x, $y) = @_; |
170 |
|
171 |
return $self |
172 |
if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
173 |
&& $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
174 |
|
175 |
() |
176 |
} |
177 |
|
178 |
sub del_parent { $_[0]->{parent} = undef } |
179 |
|
180 |
sub set_parent { |
181 |
my ($self, $par) = @_; |
182 |
|
183 |
$self->{parent} = $par; |
184 |
Scalar::Util::weaken $self->{parent}; |
185 |
} |
186 |
|
187 |
sub get_parent { |
188 |
$_[0]->{parent} |
189 |
} |
190 |
|
191 |
sub update { |
192 |
my ($self) = @_; |
193 |
|
194 |
$self->{parent}->update |
195 |
if $self->{parent}; |
196 |
} |
197 |
|
198 |
sub DESTROY { |
199 |
my ($self) = @_; |
200 |
|
201 |
#$self->deactivate; |
202 |
} |
203 |
|
204 |
############################################################################# |
205 |
|
206 |
package Crossfire::Client::Widget::Container; |
207 |
|
208 |
our @ISA = Crossfire::Client::Widget::; |
209 |
|
210 |
sub new { |
211 |
my ($class, @widgets) = @_; |
212 |
|
213 |
my $self = $class->SUPER::new (children => []); |
214 |
$self->add ($_) for @widgets; |
215 |
|
216 |
$self |
217 |
} |
218 |
|
219 |
sub add { |
220 |
my ($self, $chld, $expand) = @_; |
221 |
|
222 |
$chld->{expand} = $expand; |
223 |
$chld->set_parent ($self); |
224 |
|
225 |
@{$self->{children}} = |
226 |
sort { $a->{z} <=> $b->{z} } |
227 |
@{$self->{children}}, $chld; |
228 |
|
229 |
$self->size_allocate ($self->{w}, $self->{h}) |
230 |
if $self->{w}; #TODO: check for "realised state" |
231 |
} |
232 |
|
233 |
sub remove { |
234 |
my ($self, $widget) = @_; |
235 |
|
236 |
$self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
237 |
|
238 |
$self->size_allocate ($self->{w}, $self->{h}); |
239 |
} |
240 |
|
241 |
sub find_widget { |
242 |
my ($self, $x, $y) = @_; |
243 |
|
244 |
$x -= $self->{x}; |
245 |
$y -= $self->{y}; |
246 |
|
247 |
my $res; |
248 |
|
249 |
for (reverse @{ $self->{children} }) { |
250 |
$res = $_->find_widget ($x, $y) |
251 |
and return $res; |
252 |
} |
253 |
|
254 |
$self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
255 |
} |
256 |
|
257 |
sub _draw { |
258 |
my ($self) = @_; |
259 |
|
260 |
$_->draw for @{$self->{children}}; |
261 |
} |
262 |
|
263 |
############################################################################# |
264 |
|
265 |
package Crossfire::Client::Widget::Bin; |
266 |
|
267 |
our @ISA = Crossfire::Client::Widget::Container::; |
268 |
|
269 |
sub child { $_[0]->{children}[0] } |
270 |
|
271 |
sub size_request { |
272 |
$_[0]{children}[0]->size_request if $_[0]{children}[0]; |
273 |
} |
274 |
|
275 |
sub size_allocate { |
276 |
my ($self, $w, $h) = @_; |
277 |
|
278 |
$self->SUPER::size_allocate ($w, $h); |
279 |
$self->{children}[0]->size_allocate ($w, $h) |
280 |
if $self->{children}[0] |
281 |
} |
282 |
|
283 |
############################################################################# |
284 |
|
285 |
package Crossfire::Client::Widget::Window; |
286 |
|
287 |
our @ISA = Crossfire::Client::Widget::Bin::; |
288 |
|
289 |
use SDL::OpenGL; |
290 |
|
291 |
sub new { |
292 |
my ($class, $x, $y, $z, $w, $h) = @_; |
293 |
|
294 |
my $self = $class->SUPER::new; |
295 |
|
296 |
@$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); |
297 |
} |
298 |
|
299 |
sub update { |
300 |
my ($self) = @_; |
301 |
|
302 |
$self->render_chld; |
303 |
$self->SUPER::update; |
304 |
} |
305 |
|
306 |
sub render_chld { |
307 |
my ($self) = @_; |
308 |
|
309 |
$self->{texture} = |
310 |
Crossfire::Client::Texture->new_from_opengl ( |
311 |
$self->{w}, $self->{h}, sub { $self->child->draw } |
312 |
); |
313 |
} |
314 |
|
315 |
sub size_allocate { |
316 |
my ($self, $w, $h) = @_; |
317 |
|
318 |
$self->{w} = $w; |
319 |
$self->{h} = $h; |
320 |
|
321 |
$self->child->size_allocate ($w, $h); |
322 |
|
323 |
$self->render_chld; |
324 |
} |
325 |
|
326 |
sub _draw { |
327 |
my ($self) = @_; |
328 |
|
329 |
my ($w, $h) = ($self->w, $self->h); |
330 |
|
331 |
my $tex = $self->{texture} |
332 |
or return; |
333 |
|
334 |
glEnable GL_BLEND; |
335 |
glEnable GL_TEXTURE_2D; |
336 |
glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
337 |
|
338 |
$tex->draw_quad (0, 0, $w, $h); |
339 |
|
340 |
glDisable GL_BLEND; |
341 |
glDisable GL_TEXTURE_2D; |
342 |
} |
343 |
|
344 |
############################################################################# |
345 |
|
346 |
package Crossfire::Client::Widget::Frame; |
347 |
|
348 |
our @ISA = Crossfire::Client::Widget::Bin::; |
349 |
|
350 |
use SDL::OpenGL; |
351 |
|
352 |
sub size_request { |
353 |
my ($self) = @_; |
354 |
my $chld = $self->child |
355 |
or return (0, 0); |
356 |
|
357 |
$chld->move (2, 2); |
358 |
|
359 |
map { $_ + 4 } $chld->size_request; |
360 |
} |
361 |
|
362 |
sub size_allocate { |
363 |
my ($self, $w, $h) = @_; |
364 |
|
365 |
$self->{w} = $w; |
366 |
$self->{h} = $h; |
367 |
|
368 |
$self->child->size_allocate ($w - 4, $h - 4); |
369 |
$self->child->move (2, 2); |
370 |
} |
371 |
|
372 |
sub _draw { |
373 |
my ($self) = @_; |
374 |
|
375 |
my $chld = $self->child; |
376 |
|
377 |
my ($w, $h) = $chld->size_request; |
378 |
|
379 |
glBegin GL_QUADS; |
380 |
glColor 0, 0, 0; |
381 |
glVertex 0 , 0; |
382 |
glVertex 0 , $h + 4; |
383 |
glVertex $w + 4 , $h + 4; |
384 |
glVertex $w + 4 , 0; |
385 |
glEnd; |
386 |
|
387 |
$chld->draw; |
388 |
} |
389 |
|
390 |
############################################################################# |
391 |
|
392 |
package Crossfire::Client::Widget::FancyFrame; |
393 |
|
394 |
our @ISA = Crossfire::Client::Widget::Bin::; |
395 |
|
396 |
use SDL::OpenGL; |
397 |
|
398 |
my @tex = |
399 |
map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
400 |
qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
401 |
|
402 |
sub size_request { |
403 |
my ($self) = @_; |
404 |
|
405 |
my ($w, $h) = $self->SUPER::size_request; |
406 |
|
407 |
$h += $tex[1]->{height}; |
408 |
$h += $tex[4]->{height}; |
409 |
$w += $tex[2]->{width}; |
410 |
$w += $tex[3]->{width}; |
411 |
|
412 |
($w, $h) |
413 |
} |
414 |
|
415 |
sub size_allocate { |
416 |
my ($self, $w, $h) = @_; |
417 |
|
418 |
$self->SUPER::size_allocate ($w, $h); |
419 |
|
420 |
$h -= $tex[1]->{height}; |
421 |
$h -= $tex[4]->{height}; |
422 |
$w -= $tex[2]->{width}; |
423 |
$w -= $tex[3]->{width}; |
424 |
|
425 |
$h = $h < 0 ? 0 : $h; |
426 |
$w = $w < 0 ? 0 : $w; |
427 |
|
428 |
$self->child->size_allocate ($w, $h); |
429 |
$self->child->move ($tex[3]->{width}, $tex[1]->{height}); |
430 |
} |
431 |
|
432 |
sub _draw { |
433 |
my ($self) = @_; |
434 |
|
435 |
my ($w, $h) = ($self->{w}, $self->{h}); |
436 |
my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
437 |
|
438 |
glEnable GL_BLEND; |
439 |
glEnable GL_TEXTURE_2D; |
440 |
glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
441 |
glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
442 |
|
443 |
my $top = $tex[1]; |
444 |
$top->draw_quad (0, 0, $w, $top->{height}); |
445 |
|
446 |
my $left = $tex[3]; |
447 |
$left->draw_quad (0, $top->{height}, $left->{width}, $ch); |
448 |
|
449 |
my $right = $tex[2]; |
450 |
$right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); |
451 |
|
452 |
my $bottom = $tex[4]; |
453 |
$bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); |
454 |
|
455 |
my $bg = $tex[0]; |
456 |
glBindTexture GL_TEXTURE_2D, $bg->{name}; |
457 |
glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
458 |
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
459 |
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
460 |
|
461 |
my $rep_x = $cw / $bg->{width}; |
462 |
my $rep_y = $ch / $bg->{height}; |
463 |
|
464 |
$bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); |
465 |
|
466 |
glDisable GL_BLEND; |
467 |
glDisable GL_TEXTURE_2D; |
468 |
|
469 |
$self->child->draw; |
470 |
|
471 |
} |
472 |
|
473 |
############################################################################# |
474 |
|
475 |
package Crossfire::Client::Widget::Table; |
476 |
|
477 |
our @ISA = Crossfire::Client::Widget::Bin::; |
478 |
|
479 |
use SDL::OpenGL; |
480 |
|
481 |
sub add { |
482 |
my ($self, $x, $y, $chld) = @_; |
483 |
my $old_chld = $self->{children}[$y][$x]; |
484 |
|
485 |
$self->{children}[$y][$x] = $chld; |
486 |
$chld->set_parent ($self); |
487 |
$self->update; |
488 |
} |
489 |
|
490 |
sub max_row_height { |
491 |
my ($self, $row) = @_; |
492 |
|
493 |
my $hs = 0; |
494 |
for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { |
495 |
my $c = $self->{children}->[$row]->[$xi]; |
496 |
if ($c) { |
497 |
my ($w, $h) = $c->size_request; |
498 |
if ($hs < $h) { $hs = $h } |
499 |
} |
500 |
} |
501 |
return $hs; |
502 |
} |
503 |
|
504 |
sub max_col_width { |
505 |
my ($self, $col) = @_; |
506 |
|
507 |
my $ws = 0; |
508 |
for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { |
509 |
my $c = ($self->{children}->[$yi] || [])->[$col]; |
510 |
if ($c) { |
511 |
my ($w, $h) = $c->size_request; |
512 |
if ($ws < $w) { $ws = $w } |
513 |
} |
514 |
} |
515 |
return $ws; |
516 |
} |
517 |
|
518 |
sub size_request { |
519 |
my ($self) = @_; |
520 |
|
521 |
my ($hs, $ws) = (0, 0); |
522 |
|
523 |
for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
524 |
$hs += $self->max_row_height ($yi); |
525 |
} |
526 |
|
527 |
for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
528 |
my $wm = 0; |
529 |
for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
530 |
$wm += $self->max_col_width ($xi) |
531 |
} |
532 |
if ($ws < $wm) { $ws = $wm } |
533 |
} |
534 |
|
535 |
return ($ws, $hs); |
536 |
} |
537 |
|
538 |
sub _draw { |
539 |
my ($self) = @_; |
540 |
|
541 |
my $y = 0; |
542 |
for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
543 |
my $x = 0; |
544 |
|
545 |
for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
546 |
|
547 |
my $c = $self->{children}->[$yi]->[$xi]; |
548 |
if ($c) { |
549 |
$c->move ($x, $y, 0); #TODO: Move to size_request |
550 |
$c->draw if $c; |
551 |
} |
552 |
|
553 |
$x += $self->max_col_width ($xi); |
554 |
} |
555 |
|
556 |
$y += $self->max_row_height ($yi); |
557 |
} |
558 |
} |
559 |
|
560 |
############################################################################# |
561 |
|
562 |
package Crossfire::Client::Widget::VBox; |
563 |
|
564 |
our @ISA = Crossfire::Client::Widget::Container::; |
565 |
|
566 |
use SDL::OpenGL; |
567 |
|
568 |
sub size_request { |
569 |
my ($self) = @_; |
570 |
|
571 |
my @alloc = map [$_->size_request], @{$self->{children}}; |
572 |
|
573 |
( |
574 |
(List::Util::max map $_->[0], @alloc), |
575 |
(List::Util::sum map $_->[1], @alloc), |
576 |
) |
577 |
} |
578 |
|
579 |
sub size_allocate { |
580 |
my ($self, $w, $h) = @_; |
581 |
|
582 |
$self->w ($w); |
583 |
$self->h ($h); |
584 |
|
585 |
my $exp; |
586 |
my @oth; |
587 |
# find expand widget |
588 |
for (@{$self->{children}}) { |
589 |
if ($_->{expand}) { |
590 |
$exp = $_; |
591 |
last; |
592 |
} |
593 |
push @oth, $_; |
594 |
} |
595 |
|
596 |
my ($ow, $oh); |
597 |
|
598 |
# get sizes of other widgets |
599 |
for (@oth) { |
600 |
my ($w, $h) = $_->size_request; |
601 |
$oh += $h; |
602 |
if ($ow < $w) { $ow = $w } |
603 |
} |
604 |
|
605 |
my $y = 0; |
606 |
for (@{$self->{children}}) { |
607 |
$_->move (0, $y); |
608 |
|
609 |
if ($_ == $exp) { |
610 |
$_->size_allocate ($w, $h - $oh); |
611 |
$y += $h - $oh; |
612 |
} else { |
613 |
my ($cw, $h) = $_->size_request; |
614 |
$_->size_allocate ($w, $h); |
615 |
$y += $h; |
616 |
} |
617 |
} |
618 |
} |
619 |
|
620 |
############################################################################# |
621 |
|
622 |
package Crossfire::Client::Widget::Label; |
623 |
|
624 |
our @ISA = Crossfire::Client::Widget::; |
625 |
|
626 |
use SDL::OpenGL; |
627 |
|
628 |
sub new { |
629 |
my ($class, $x, $y, $z, $height, $text) = @_; |
630 |
|
631 |
$height ||= $::FONTSIZE; |
632 |
|
633 |
# TODO: color, and make height, xyz etc. optional |
634 |
my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
635 |
|
636 |
$self->set_text ($text); |
637 |
|
638 |
$self |
639 |
} |
640 |
|
641 |
sub set_text { |
642 |
my ($self, $text) = @_; |
643 |
|
644 |
$self->{text} = $text; |
645 |
$self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; |
646 |
|
647 |
$self->update; |
648 |
} |
649 |
|
650 |
sub get_text { |
651 |
my ($self, $text) = @_; |
652 |
|
653 |
$self->{text} |
654 |
} |
655 |
|
656 |
sub size_request { |
657 |
my ($self) = @_; |
658 |
|
659 |
if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
660 |
( |
661 |
$self->{texture}{width}, |
662 |
$self->{texture}{height}, |
663 |
) |
664 |
} else { |
665 |
my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; |
666 |
|
667 |
($w, $h) |
668 |
} |
669 |
|
670 |
} |
671 |
|
672 |
sub _draw { |
673 |
my ($self) = @_; |
674 |
|
675 |
my $tex = $self->{texture}; |
676 |
|
677 |
glEnable GL_BLEND; |
678 |
glEnable GL_TEXTURE_2D; |
679 |
glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
680 |
glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
681 |
|
682 |
glColor 1, 0, 0, 1; # TODO color |
683 |
|
684 |
$tex->draw_quad (0, 0); |
685 |
|
686 |
glDisable GL_BLEND; |
687 |
glDisable GL_TEXTURE_2D; |
688 |
} |
689 |
|
690 |
############################################################################# |
691 |
|
692 |
package Crossfire::Client::Widget::Entry; |
693 |
|
694 |
our @ISA = Crossfire::Client::Widget::Label::; |
695 |
|
696 |
use SDL; |
697 |
use SDL::OpenGL; |
698 |
|
699 |
sub key_down { |
700 |
my ($self, $ev) = @_; |
701 |
|
702 |
my $mod = $ev->key_mod; |
703 |
my $sym = $ev->key_sym; |
704 |
|
705 |
my $uni = $ev->key_unicode; |
706 |
|
707 |
my $text = $self->get_text; |
708 |
|
709 |
if ($sym == SDLK_BACKSPACE) { |
710 |
substr $text, -1, 1, ''; |
711 |
} elsif ($uni) { |
712 |
$text .= chr $uni; |
713 |
print "$uni <$text>\n";#d# |
714 |
} |
715 |
|
716 |
$self->set_text ($text); |
717 |
} |
718 |
|
719 |
sub button_down { |
720 |
my ($self, $ev) = @_; |
721 |
|
722 |
$self->focus_in; |
723 |
} |
724 |
|
725 |
sub _draw { |
726 |
my ($self) = @_; |
727 |
|
728 |
if ($FOCUS == $self) { |
729 |
glColor 1, 1, 1; |
730 |
} else { |
731 |
glColor 0.7, 0.7, 0.7; |
732 |
} |
733 |
|
734 |
glBegin GL_QUADS; |
735 |
glVertex 0 , 0; |
736 |
glVertex 0 , $self->{h} - 1; |
737 |
glVertex $self->{w} - 1, $self->{h} - 1; |
738 |
glVertex $self->{w} - 1, 0; |
739 |
glEnd; |
740 |
|
741 |
$self->SUPER::_draw; |
742 |
} |
743 |
|
744 |
############################################################################# |
745 |
|
746 |
package Crossfire::Client::Widget::MapWidget; |
747 |
|
748 |
use strict; |
749 |
|
750 |
use List::Util qw(min max); |
751 |
|
752 |
use SDL; |
753 |
use SDL::OpenGL; |
754 |
use SDL::OpenGL::Constants; |
755 |
|
756 |
our @ISA = Crossfire::Client::Widget::; |
757 |
|
758 |
sub key_down { |
759 |
print "MAPKEYDOWN\n"; |
760 |
} |
761 |
|
762 |
sub key_up { |
763 |
} |
764 |
|
765 |
sub size_request { |
766 |
( |
767 |
1 + int $::WIDTH / 32, |
768 |
1 + int $::HEIGHT / 32, |
769 |
) |
770 |
} |
771 |
|
772 |
sub _draw { |
773 |
my ($self) = @_; |
774 |
|
775 |
my $mx = $::CONN->{mapx}; |
776 |
my $my = $::CONN->{mapy}; |
777 |
|
778 |
my $map = $::CONN->{map}; |
779 |
|
780 |
my ($xofs, $yofs); |
781 |
|
782 |
my $sw = 1 + int $::WIDTH / 32; |
783 |
my $sh = 1 + int $::HEIGHT / 32; |
784 |
|
785 |
if ($::CONN->{mapw} > $sw) { |
786 |
$xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
787 |
} else { |
788 |
$xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
789 |
} |
790 |
|
791 |
if ($::CONN->{maph} > $sh) { |
792 |
$yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
793 |
} else { |
794 |
$yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
795 |
} |
796 |
|
797 |
glEnable GL_TEXTURE_2D; |
798 |
glEnable GL_BLEND; |
799 |
glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
800 |
|
801 |
my $sw4 = ($sw + 3) & ~3; |
802 |
my $lighting = "\x00" x ($sw4 * $sh); |
803 |
|
804 |
for my $x (0 .. $sw - 1) { |
805 |
for my $y (0 .. $sh - 1) { |
806 |
|
807 |
my $cell = $map->[$x + $xofs][$y + $yofs] |
808 |
or next; |
809 |
|
810 |
my $darkness = $cell->[0] * (1 / 255); |
811 |
if ($darkness < 0) { |
812 |
$darkness = $cell->[1] ? 0.1 : 0; |
813 |
} |
814 |
substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; |
815 |
|
816 |
for my $num (grep $_, @$cell[1,2,3]) { |
817 |
my $tex = $::CONN->{face}[$num]{texture} || next; |
818 |
|
819 |
my $w = $tex->{width}; |
820 |
my $h = $tex->{height}; |
821 |
|
822 |
my $px = ($x + 1) * 32 - $w; |
823 |
my $py = ($y + 1) * 32 - $h; |
824 |
|
825 |
$tex->draw_quad ($px, $py, $w, $h); |
826 |
} |
827 |
} |
828 |
} |
829 |
|
830 |
# if (1) { # higher quality darkness |
831 |
# $lighting =~ s/(.)/$1$1$1/gs; |
832 |
# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
833 |
# |
834 |
# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear"); |
835 |
# |
836 |
# $lighting = $pb->get_pixels; |
837 |
# $lighting =~ s/(.)../$1/gs; |
838 |
# } |
839 |
|
840 |
$lighting = new Crossfire::Client::Texture |
841 |
width => $sw4, |
842 |
height => $sh, |
843 |
data => $lighting, |
844 |
internalformat => GL_ALPHA, |
845 |
format => GL_ALPHA; |
846 |
|
847 |
glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
848 |
glColor 0.45, 0.45, 0.45, 1; |
849 |
glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
850 |
glBindTexture GL_TEXTURE_2D, $lighting->{name}; |
851 |
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
852 |
|
853 |
$lighting->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
854 |
|
855 |
glDisable GL_TEXTURE_2D; |
856 |
glDisable GL_BLEND; |
857 |
} |
858 |
|
859 |
my %DIR = ( |
860 |
SDLK_KP8, [1, "north"], |
861 |
SDLK_KP9, [2, "northeast"], |
862 |
SDLK_KP6, [3, "east"], |
863 |
SDLK_KP3, [4, "southeast"], |
864 |
SDLK_KP2, [5, "south"], |
865 |
SDLK_KP1, [6, "southwest"], |
866 |
SDLK_KP4, [7, "west"], |
867 |
SDLK_KP7, [8, "northwest"], |
868 |
|
869 |
SDLK_UP, [1, "north"], |
870 |
SDLK_RIGHT, [3, "east"], |
871 |
SDLK_DOWN, [5, "south"], |
872 |
SDLK_LEFT, [7, "west"], |
873 |
); |
874 |
|
875 |
sub key_down { |
876 |
my ($self, $ev) = @_; |
877 |
|
878 |
my $mod = $ev->key_mod; |
879 |
my $sym = $ev->key_sym; |
880 |
|
881 |
if ($sym == SDLK_KP5) { |
882 |
$::CONN->send ("command stay fire"); |
883 |
} elsif (exists $DIR{$sym}) { |
884 |
if ($mod & KMOD_SHIFT) { |
885 |
$self->{shft}++; |
886 |
$::CONN->send ("command fire $DIR{$sym}[0]"); |
887 |
} elsif ($mod & KMOD_CTRL) { |
888 |
$self->{ctrl}++; |
889 |
$::CONN->send ("command run $DIR{$sym}[0]"); |
890 |
} else { |
891 |
$::CONN->send ("command $DIR{$sym}[1]"); |
892 |
} |
893 |
} |
894 |
} |
895 |
|
896 |
sub key_up { |
897 |
my ($self, $ev) = @_; |
898 |
|
899 |
my $mod = $ev->key_mod; |
900 |
my $sym = $ev->key_sym; |
901 |
|
902 |
if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
903 |
$::CONN->send ("command fire_stop"); |
904 |
} |
905 |
if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
906 |
$::CONN->send ("command run_stop"); |
907 |
} |
908 |
} |
909 |
|
910 |
############################################################################# |
911 |
|
912 |
package Crossfire::Client::Widget::Animator; |
913 |
|
914 |
use SDL::OpenGL; |
915 |
|
916 |
our @ISA = Crossfire::Client::Widget::Bin::; |
917 |
|
918 |
sub moveto { |
919 |
my ($self, $x, $y) = @_; |
920 |
|
921 |
$self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
922 |
$self->{speed} = 0.001; |
923 |
$self->{time} = 1; |
924 |
|
925 |
::animation_start $self; |
926 |
} |
927 |
|
928 |
sub animate { |
929 |
my ($self, $interval) = @_; |
930 |
|
931 |
printf "%5.2f\n", 1 / $interval;#d# |
932 |
|
933 |
$self->{time} -= $interval * $self->{speed}; |
934 |
if ($self->{time} <= 0) { |
935 |
$self->{time} = 0; |
936 |
::animation_stop $self; |
937 |
} |
938 |
|
939 |
my ($x0, $y0, $x1, $y1) = @{$self->{moveto}}; |
940 |
|
941 |
$self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time}); |
942 |
$self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time}); |
943 |
} |
944 |
|
945 |
sub _draw { |
946 |
my ($self) = @_; |
947 |
|
948 |
glPushMatrix; |
949 |
glRotate $self->{time} * 1000, 0, 1, 0; |
950 |
$self->{children}[0]->draw; |
951 |
glPopMatrix; |
952 |
} |
953 |
|
954 |
############################################################################# |
955 |
|
956 |
package Crossfire::Client::Widget::Toplevel; |
957 |
|
958 |
our @ISA = Crossfire::Client::Widget::Container::; |
959 |
|
960 |
sub size_request { |
961 |
($::WIDTH, $::HEIGHT) |
962 |
} |
963 |
|
964 |
sub size_allocate { |
965 |
my ($self, $w, $h) = @_; |
966 |
|
967 |
$self->SUPER::size_allocate ($w, $h); |
968 |
|
969 |
$_->size_allocate ($_->size_request) |
970 |
for @{$self->{children}}; |
971 |
} |
972 |
|
973 |
sub update { |
974 |
my ($self) = @_; |
975 |
|
976 |
$self->size_allocate ($self->size_request); |
977 |
::refresh (); |
978 |
} |
979 |
|
980 |
sub add { |
981 |
my ($self, $widget) = @_; |
982 |
|
983 |
$self->SUPER::add ($widget); |
984 |
|
985 |
$widget->size_allocate ($widget->size_request); |
986 |
} |
987 |
|
988 |
sub draw { |
989 |
my ($self) = @_; |
990 |
|
991 |
$self->_draw; |
992 |
} |
993 |
|
994 |
############################################################################# |
995 |
|
996 |
package Crossfire::Client::Widget; |
997 |
|
998 |
$TOPLEVEL = new Crossfire::Client::Widget::Toplevel; |
999 |
|
1000 |
1 |
1001 |
|