ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.37
Committed: Sun Apr 9 21:05:50 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.36: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

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