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

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.6 by elmex, Fri Apr 7 19:58:55 2006 UTC vs.
Revision 1.34 by elmex, Sun Apr 9 02:44:51 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines