1 | package Crossfire::Client::Widget; |
1 | package Crossfire::Client::Widget; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
|
|
4 | |
|
|
5 | use Scalar::Util; |
|
|
6 | |
4 | use SDL::OpenGL; |
7 | use SDL::OpenGL; |
5 | use SDL::OpenGL::Constants; |
8 | use SDL::OpenGL::Constants; |
6 | |
9 | |
7 | our $FOCUS; # the widget with current focus |
10 | our $FOCUS; # the widget with current focus |
8 | our @ACTIVE_WIDGETS; |
11 | our @ACTIVE_WIDGETS; |
… | |
… | |
19 | bless { @_ }, $class |
22 | bless { @_ }, $class |
20 | } |
23 | } |
21 | |
24 | |
22 | sub activate { |
25 | sub activate { |
23 | push @ACTIVE_WIDGETS, $_[0]; |
26 | push @ACTIVE_WIDGETS, $_[0]; |
|
|
27 | Scalar::Util::weaken $ACTIVE_WIDGETS[-1]; |
24 | } |
28 | } |
25 | |
29 | |
26 | sub deactivate { |
30 | sub deactivate { |
27 | @ACTIVE_WIDGETS = |
31 | @ACTIVE_WIDGETS = |
28 | sort { $a->{z} <=> $b->{z} } |
32 | sort { $a->{z} <=> $b->{z} } |
29 | grep { $_ != $_[0] } |
33 | grep { $_ && $_ != $_[0] } |
30 | @ACTIVE_WIDGETS; |
34 | @ACTIVE_WIDGETS; |
|
|
35 | } |
|
|
36 | |
|
|
37 | sub move { |
|
|
38 | my ($self, $x, $y, $z) = @_; |
|
|
39 | $self->{x} = $x; |
|
|
40 | $self->{y} = $y; |
|
|
41 | $self->{z} = $z if defined $z; |
|
|
42 | } |
|
|
43 | |
|
|
44 | sub needs_redraw { |
|
|
45 | 0 |
31 | } |
46 | } |
32 | |
47 | |
33 | sub size_request { |
48 | sub size_request { |
34 | die "size_request is abtract"; |
49 | die "size_request is abtract"; |
35 | } |
50 | } |
… | |
… | |
80 | |
95 | |
81 | sub bbox { |
96 | sub bbox { |
82 | my ($widget) = @_; |
97 | my ($widget) = @_; |
83 | } |
98 | } |
84 | |
99 | |
|
|
100 | sub DESTROY { |
|
|
101 | my ($self) = @_; |
|
|
102 | |
|
|
103 | $self->deactivate; |
|
|
104 | } |
|
|
105 | |
85 | package Crossfire::Client::Widget::Container; |
106 | package Crossfire::Client::Widget::Container; |
86 | |
107 | |
87 | our @ISA = Crossfire::Client::Widget::; |
108 | our @ISA = Crossfire::Client::Widget::; |
88 | |
109 | |
89 | use SDL::OpenGL; |
110 | use SDL::OpenGL; |
… | |
… | |
92 | sub get { $_[0]->{child} } |
113 | sub get { $_[0]->{child} } |
93 | |
114 | |
94 | sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } |
115 | sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } |
95 | |
116 | |
96 | sub _draw { die "Containers can't be drawn!" } |
117 | sub _draw { die "Containers can't be drawn!" } |
|
|
118 | |
|
|
119 | package Crossfire::Client::Widget::Window; |
|
|
120 | |
|
|
121 | our @ISA = Crossfire::Client::Widget::Container::; |
|
|
122 | |
|
|
123 | use SDL::OpenGL; |
|
|
124 | |
|
|
125 | sub add { |
|
|
126 | my ($self, $chld) = @_; |
|
|
127 | $self->SUPER::add ($chld); |
|
|
128 | $self->render_chld; |
|
|
129 | } |
|
|
130 | |
|
|
131 | sub render_chld { |
|
|
132 | my ($self) = @_; |
|
|
133 | my $chld = $self->get; |
|
|
134 | my ($w, $h) = $self->size_request; |
|
|
135 | |
|
|
136 | $self->{texture} = |
|
|
137 | Crossfire::Client::Texture->new_from_opengl ( |
|
|
138 | $w, $h, sub { $chld->draw } |
|
|
139 | ); |
|
|
140 | $self->{texture}->upload; |
|
|
141 | } |
|
|
142 | |
|
|
143 | sub size_request { |
|
|
144 | my ($self) = @_; |
|
|
145 | my $chld = $self->get |
|
|
146 | or return (0, 0); |
|
|
147 | $chld->size_request |
|
|
148 | } |
|
|
149 | |
|
|
150 | sub _draw { |
|
|
151 | my ($self) = @_; |
|
|
152 | |
|
|
153 | my ($w, $h) = $self->size_request;#TODO# use widht/height of texture |
|
|
154 | my $tex = $self->{texture} |
|
|
155 | or return; |
|
|
156 | |
|
|
157 | glEnable GL_BLEND; |
|
|
158 | glEnable GL_TEXTURE_2D; |
|
|
159 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
160 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
161 | |
|
|
162 | glColor 1, 1, 1; |
|
|
163 | |
|
|
164 | glBegin GL_QUADS; |
|
|
165 | glTexCoord 0, 0; glVertex 0, 0; |
|
|
166 | glTexCoord 0, 1; glVertex 0, $h; |
|
|
167 | glTexCoord 1, 1; glVertex $w, $h; |
|
|
168 | glTexCoord 1, 0; glVertex $w, 0; |
|
|
169 | glEnd; |
|
|
170 | |
|
|
171 | glDisable GL_BLEND; |
|
|
172 | glDisable GL_TEXTURE_2D; |
|
|
173 | } |
97 | |
174 | |
98 | package Crossfire::Client::Widget::Frame; |
175 | package Crossfire::Client::Widget::Frame; |
99 | |
176 | |
100 | our @ISA = Crossfire::Client::Widget::Container::; |
177 | our @ISA = Crossfire::Client::Widget::Container::; |
101 | |
178 | |
… | |
… | |
123 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
200 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
124 | glEnd; |
201 | glEnd; |
125 | |
202 | |
126 | glPushMatrix; |
203 | glPushMatrix; |
127 | glTranslate (2, 2, 0); |
204 | glTranslate (2, 2, 0); |
128 | $chld->_draw; |
205 | $chld->draw; |
129 | glPopMatrix; |
206 | glPopMatrix; |
130 | } |
207 | } |
131 | |
208 | |
132 | package Crossfire::Client::Widget::Table; |
209 | package Crossfire::Client::Widget::Table; |
133 | |
210 | |
… | |
… | |
196 | my $x = 0; |
273 | my $x = 0; |
197 | |
274 | |
198 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { |
275 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { |
199 | |
276 | |
200 | glPushMatrix; |
277 | glPushMatrix; |
201 | glTranslate ($x, $y, 0); |
278 | glTranslate ($x, $y, 0);#TODO#there must be no translate here, instead the widget must be moved |
202 | my $c = $self->{childs}->[$yi]->[$xi]; |
279 | my $c = $self->{childs}->[$yi]->[$xi]; |
203 | $c->_draw if $c; |
280 | $c->draw if $c; |
204 | glPopMatrix; |
281 | glPopMatrix; |
205 | |
282 | |
206 | $x += $self->max_col_width ($xi); |
283 | $x += $self->max_col_width ($xi); |
207 | } |
284 | } |
208 | |
285 | |
… | |
… | |
238 | my ($self) = @_; |
315 | my ($self) = @_; |
239 | |
316 | |
240 | my ($x, $y); |
317 | my ($x, $y); |
241 | for (@{$self->{childs} || []}) { |
318 | for (@{$self->{childs} || []}) { |
242 | glPushMatrix; |
319 | glPushMatrix; |
243 | glTranslate (0, $y, 0); |
320 | glTranslate (0, $y, 0);# see above TODO |
244 | $_->_draw; |
321 | $_->draw; |
245 | glPopMatrix; |
322 | glPopMatrix; |
246 | my ($w, $h) = $_->size_request; |
323 | my ($w, $h) = $_->size_request; |
247 | $y += $h; |
324 | $y += $h; |
248 | } |
325 | } |
249 | } |
326 | } |
… | |
… | |
285 | |
362 | |
286 | sub _draw { |
363 | sub _draw { |
287 | my ($self) = @_; |
364 | my ($self) = @_; |
288 | |
365 | |
289 | my $tex = $self->{texture}; |
366 | my $tex = $self->{texture}; |
290 | |
|
|
291 | $self->{x}--; |
|
|
292 | |
367 | |
293 | glEnable GL_BLEND; |
368 | glEnable GL_BLEND; |
294 | glEnable GL_TEXTURE_2D; |
369 | glEnable GL_TEXTURE_2D; |
295 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
370 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
296 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
371 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
… | |
… | |
329 | |
404 | |
330 | package Crossfire::Client::Widget::MapWidget; |
405 | package Crossfire::Client::Widget::MapWidget; |
331 | |
406 | |
332 | use strict; |
407 | use strict; |
333 | |
408 | |
334 | our @ISA = qw/Crossfire::Client::Widget/; |
409 | use List::Util qw(min max); |
335 | |
410 | |
336 | use SDL; |
411 | use SDL; |
337 | use SDL::OpenGL; |
412 | use SDL::OpenGL; |
338 | use SDL::OpenGL::Constants; |
413 | use SDL::OpenGL::Constants; |
|
|
414 | |
|
|
415 | our @ISA = Crossfire::Client::Widget::; |
339 | |
416 | |
340 | sub key_down { |
417 | sub key_down { |
341 | print "MAPKEYDOWN\n"; |
418 | print "MAPKEYDOWN\n"; |
342 | } |
419 | } |
343 | |
420 | |
344 | sub key_up { |
421 | sub key_up { |
345 | } |
422 | } |
346 | |
423 | |
347 | sub _draw { |
424 | sub _draw { |
|
|
425 | my ($self) = @_; |
|
|
426 | |
|
|
427 | my $mx = $::CONN->{mapx}; |
|
|
428 | my $my = $::CONN->{mapy}; |
|
|
429 | |
|
|
430 | my $map = $::CONN->{map}; |
|
|
431 | |
|
|
432 | my ($xofs, $yofs); |
|
|
433 | |
|
|
434 | my $sw = 1 + int $::WIDTH / 32; |
|
|
435 | my $sh = 1 + int $::HEIGHT / 32; |
|
|
436 | |
|
|
437 | if ($::CONN->{mapw} > $sw) { |
|
|
438 | $xofs = ($sw - $::CONN->{mapw}) * 0.5; |
|
|
439 | } else { |
|
|
440 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
|
|
441 | } |
|
|
442 | |
|
|
443 | if ($::CONN->{maph} > $sh) { |
|
|
444 | $yofs = ($sh - $::CONN->{maph}) * 0.5; |
|
|
445 | } else { |
|
|
446 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
|
|
447 | } |
|
|
448 | |
348 | glEnable GL_TEXTURE_2D; |
449 | glEnable GL_TEXTURE_2D; |
349 | glEnable GL_BLEND; |
450 | glEnable GL_BLEND; |
350 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
451 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
351 | |
452 | |
352 | my $map = $::CONN->{map}; |
453 | warn "<$mx,$my> <$xofs,$yofs>\n";#d# |
353 | |
454 | |
354 | for my $x (0 .. $::CONN->{mapw} - 1) { |
455 | for my $x (0 .. $sw - 1) { |
355 | for my $y (0 .. $::CONN->{maph} - 1) { |
456 | for my $y (0 .. $sh - 1) { |
356 | |
457 | |
357 | my $cell = $map->[$x][$y] |
458 | my $cell = $map->[$x + $xofs][$y + $yofs] |
358 | or next; |
459 | or next; |
359 | |
460 | |
360 | my $darkness = $cell->[3] * (1 / 255); |
461 | my $darkness = $cell->[0] * (1 / 255); |
|
|
462 | if ($darkness < 0) { |
|
|
463 | glColor 0.3, 0.3, 0.3; |
|
|
464 | } else { |
361 | glColor $darkness, $darkness, $darkness; |
465 | glColor $darkness, $darkness, $darkness; |
|
|
466 | } |
362 | |
467 | |
363 | for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { |
468 | for my $num (grep $_, @$cell[1,2,3]) { |
364 | my $tex = $::CONN->{face}[$num]{texture} || next; |
469 | my $tex = $::CONN->{face}[$num]{texture} || next; |
365 | |
470 | |
366 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
471 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
367 | |
472 | |
|
|
473 | my $w = $tex->{width}; |
|
|
474 | my $h = $tex->{height}; |
|
|
475 | |
|
|
476 | my $px = ($x + 1) * 32 - $w; |
|
|
477 | my $py = ($y + 1) * 32 - $h; |
|
|
478 | |
368 | glBegin GL_QUADS; |
479 | glBegin GL_QUADS; |
369 | glTexCoord 0, 0; glVertex $x * 32 , $y * 32; |
480 | glTexCoord 0, 0; glVertex $px , $py; |
370 | glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32; |
481 | glTexCoord 0, 1; glVertex $px , $py + $h; |
371 | glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32; |
482 | glTexCoord 1, 1; glVertex $px + $w, $py + $h; |
372 | glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32; |
483 | glTexCoord 1, 0; glVertex $px + $w, $py; |
373 | glEnd; |
484 | glEnd; |
374 | } |
485 | } |
375 | } |
486 | } |
376 | } |
487 | } |
377 | |
488 | |
… | |
… | |
379 | glDisable GL_BLEND; |
490 | glDisable GL_BLEND; |
380 | } |
491 | } |
381 | |
492 | |
382 | my %DIR = ( |
493 | my %DIR = ( |
383 | SDLK_KP8, [1, "north"], |
494 | SDLK_KP8, [1, "north"], |
384 | SDLK_KP9, [2, "northest"], |
495 | SDLK_KP9, [2, "northeast"], |
385 | SDLK_KP6, [3, "east"], |
496 | SDLK_KP6, [3, "east"], |
386 | SDLK_KP3, [4, "southeast"], |
497 | SDLK_KP3, [4, "southeast"], |
387 | SDLK_KP2, [5, "south"], |
498 | SDLK_KP2, [5, "south"], |
388 | SDLK_KP1, [6, "southwest"], |
499 | SDLK_KP1, [6, "southwest"], |
389 | SDLK_KP4, [7, "west"], |
500 | SDLK_KP4, [7, "west"], |
390 | SDLK_KP7, [8, "northwest"], |
501 | SDLK_KP7, [8, "northwest"], |
|
|
502 | |
|
|
503 | SDLK_UP, [1, "north"], |
|
|
504 | SDLK_RIGHT, [3, "east"], |
|
|
505 | SDLK_DOWN, [5, "south"], |
|
|
506 | SDLK_LEFT, [7, "west"], |
391 | ); |
507 | ); |
392 | |
508 | |
393 | sub key_down { |
509 | sub key_down { |
394 | my ($self, $ev) = @_; |
510 | my ($self, $ev) = @_; |
395 | |
511 | |
… | |
… | |
398 | |
514 | |
399 | if ($sym == SDLK_KP5) { |
515 | if ($sym == SDLK_KP5) { |
400 | $::CONN->send ("command stay fire"); |
516 | $::CONN->send ("command stay fire"); |
401 | } elsif (exists $DIR{$sym}) { |
517 | } elsif (exists $DIR{$sym}) { |
402 | if ($mod & KMOD_SHIFT) { |
518 | if ($mod & KMOD_SHIFT) { |
|
|
519 | $self->{shft}++; |
403 | $::CONN->send ("command fire $DIR{$sym}[0]"); |
520 | $::CONN->send ("command fire $DIR{$sym}[0]"); |
404 | } elsif ($mod & KMOD_CTRL) { |
521 | } elsif ($mod & KMOD_CTRL) { |
|
|
522 | $self->{ctrl}++; |
405 | $::CONN->send ("command run $DIR{$sym}[0]"); |
523 | $::CONN->send ("command run $DIR{$sym}[0]"); |
406 | } else { |
524 | } else { |
|
|
525 | $::CONN->send ("command $DIR{$sym}[1]"); |
407 | } |
526 | } |
408 | } |
527 | } |
409 | } |
528 | } |
410 | |
529 | |
411 | sub key_up { |
530 | sub key_up { |
412 | my ($self, $ev) = @_; |
531 | my ($self, $ev) = @_; |
413 | |
532 | |
414 | my $mod = $ev->key_mod; |
533 | my $mod = $ev->key_mod; |
415 | my $sym = $ev->key_sym; |
534 | my $sym = $ev->key_sym; |
416 | |
535 | |
417 | if (exists $DIR{$sym}) { |
536 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
418 | if ($mod & KMOD_SHIFT) { |
|
|
419 | $::CONN->send ("command fire_stop"); |
537 | $::CONN->send ("command fire_stop"); |
420 | } else { |
538 | } |
|
|
539 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
421 | $::CONN->send ("command run_stop"); |
540 | $::CONN->send ("command run_stop"); |
422 | } |
|
|
423 | } |
541 | } |
424 | } |
542 | } |
425 | |
543 | |
426 | 1; |
544 | 1; |
427 | |
545 | |