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; |
31 | } |
42 | } |
32 | |
43 | |
33 | sub size_request { |
44 | sub size_request { |
34 | die "size_request is abtract"; |
45 | die "size_request is abtract"; |
35 | } |
46 | } |
… | |
… | |
78 | my ($widget) = @_; |
89 | my ($widget) = @_; |
79 | } |
90 | } |
80 | |
91 | |
81 | sub bbox { |
92 | sub bbox { |
82 | my ($widget) = @_; |
93 | my ($widget) = @_; |
|
|
94 | } |
|
|
95 | |
|
|
96 | sub DESTROY { |
|
|
97 | my ($self) = @_; |
|
|
98 | |
|
|
99 | $self->deactivate; |
83 | } |
100 | } |
84 | |
101 | |
85 | package Crossfire::Client::Widget::Container; |
102 | package Crossfire::Client::Widget::Container; |
86 | |
103 | |
87 | our @ISA = Crossfire::Client::Widget::; |
104 | our @ISA = Crossfire::Client::Widget::; |
… | |
… | |
286 | sub _draw { |
303 | sub _draw { |
287 | my ($self) = @_; |
304 | my ($self) = @_; |
288 | |
305 | |
289 | my $tex = $self->{texture}; |
306 | my $tex = $self->{texture}; |
290 | |
307 | |
291 | $self->{x}--; |
|
|
292 | |
|
|
293 | glEnable GL_BLEND; |
308 | glEnable GL_BLEND; |
294 | glEnable GL_TEXTURE_2D; |
309 | glEnable GL_TEXTURE_2D; |
295 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
310 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
296 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
311 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
297 | |
312 | |
… | |
… | |
363 | for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { |
378 | for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { |
364 | my $tex = $::CONN->{face}[$num]{texture} || next; |
379 | my $tex = $::CONN->{face}[$num]{texture} || next; |
365 | |
380 | |
366 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
381 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
367 | |
382 | |
|
|
383 | my $w = $tex->{width}; |
|
|
384 | my $h = $tex->{height}; |
|
|
385 | |
|
|
386 | my $px = ($x + 1) * 32 - $w; |
|
|
387 | my $py = ($y + 1) * 32 - $h; |
|
|
388 | |
368 | glBegin GL_QUADS; |
389 | glBegin GL_QUADS; |
369 | glTexCoord 0, 0; glVertex $x * 32 , $y * 32; |
390 | glTexCoord 0, 0; glVertex $px , $py; |
370 | glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32; |
391 | glTexCoord 0, 1; glVertex $px , $py + $h; |
371 | glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32; |
392 | glTexCoord 1, 1; glVertex $px + $w, $py + $h; |
372 | glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32; |
393 | glTexCoord 1, 0; glVertex $px + $w, $py; |
373 | glEnd; |
394 | glEnd; |
374 | } |
395 | } |
375 | } |
396 | } |
376 | } |
397 | } |
377 | |
398 | |
… | |
… | |
379 | glDisable GL_BLEND; |
400 | glDisable GL_BLEND; |
380 | } |
401 | } |
381 | |
402 | |
382 | my %DIR = ( |
403 | my %DIR = ( |
383 | SDLK_KP8, [1, "north"], |
404 | SDLK_KP8, [1, "north"], |
384 | SDLK_KP9, [2, "northest"], |
405 | SDLK_KP9, [2, "northeast"], |
385 | SDLK_KP6, [3, "east"], |
406 | SDLK_KP6, [3, "east"], |
386 | SDLK_KP3, [4, "southeast"], |
407 | SDLK_KP3, [4, "southeast"], |
387 | SDLK_KP2, [5, "south"], |
408 | SDLK_KP2, [5, "south"], |
388 | SDLK_KP1, [6, "southwest"], |
409 | SDLK_KP1, [6, "southwest"], |
389 | SDLK_KP4, [7, "west"], |
410 | SDLK_KP4, [7, "west"], |
390 | SDLK_KP7, [8, "northwest"], |
411 | SDLK_KP7, [8, "northwest"], |
|
|
412 | |
|
|
413 | SDLK_UP, [1, "north"], |
|
|
414 | SDLK_RIGHT, [3, "east"], |
|
|
415 | SDLK_DOWN, [5, "south"], |
|
|
416 | SDLK_LEFT, [7, "west"], |
391 | ); |
417 | ); |
392 | |
418 | |
393 | sub key_down { |
419 | sub key_down { |
394 | my ($self, $ev) = @_; |
420 | my ($self, $ev) = @_; |
395 | |
421 | |
… | |
… | |
398 | |
424 | |
399 | if ($sym == SDLK_KP5) { |
425 | if ($sym == SDLK_KP5) { |
400 | $::CONN->send ("command stay fire"); |
426 | $::CONN->send ("command stay fire"); |
401 | } elsif (exists $DIR{$sym}) { |
427 | } elsif (exists $DIR{$sym}) { |
402 | if ($mod & KMOD_SHIFT) { |
428 | if ($mod & KMOD_SHIFT) { |
|
|
429 | $self->{shft}++; |
403 | $::CONN->send ("command fire $DIR{$sym}[0]"); |
430 | $::CONN->send ("command fire $DIR{$sym}[0]"); |
404 | } elsif ($mod & KMOD_CTRL) { |
431 | } elsif ($mod & KMOD_CTRL) { |
|
|
432 | $self->{ctrl}++; |
405 | $::CONN->send ("command run $DIR{$sym}[0]"); |
433 | $::CONN->send ("command run $DIR{$sym}[0]"); |
406 | } else { |
434 | } else { |
|
|
435 | $::CONN->send ("command $DIR{$sym}[1]"); |
407 | } |
436 | } |
408 | } |
437 | } |
409 | } |
438 | } |
410 | |
439 | |
411 | sub key_up { |
440 | sub key_up { |
412 | my ($self, $ev) = @_; |
441 | my ($self, $ev) = @_; |
413 | |
442 | |
414 | my $mod = $ev->key_mod; |
443 | my $mod = $ev->key_mod; |
415 | my $sym = $ev->key_sym; |
444 | my $sym = $ev->key_sym; |
416 | |
445 | |
417 | if (exists $DIR{$sym}) { |
446 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
418 | if ($mod & KMOD_SHIFT) { |
|
|
419 | $::CONN->send ("command fire_stop"); |
447 | $::CONN->send ("command fire_stop"); |
420 | } else { |
448 | } |
|
|
449 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
421 | $::CONN->send ("command run_stop"); |
450 | $::CONN->send ("command run_stop"); |
422 | } |
|
|
423 | } |
451 | } |
424 | } |
452 | } |
425 | |
453 | |
426 | 1; |
454 | 1; |
427 | |
455 | |