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 { |
|
|
139 | my ($txt, $w, $h) = @_; |
|
|
140 | $chld->_draw; |
|
|
141 | } |
|
|
142 | ); |
|
|
143 | $self->{texture}->upload; |
|
|
144 | } |
|
|
145 | |
|
|
146 | sub size_request { |
|
|
147 | my ($self) = @_; |
|
|
148 | my $chld = $self->get |
|
|
149 | or return (0, 0); |
|
|
150 | $chld->size_request |
|
|
151 | } |
|
|
152 | |
|
|
153 | sub _draw { |
|
|
154 | my ($self) = @_; |
|
|
155 | |
|
|
156 | my $tex = $self->{texture} |
|
|
157 | or return; |
|
|
158 | |
|
|
159 | warn "DRAW TEX: $tex->{width} $tex->{height}\n"; |
|
|
160 | glEnable GL_BLEND; |
|
|
161 | glEnable GL_TEXTURE_2D; |
|
|
162 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
163 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
164 | |
|
|
165 | glColor 1, 1, 1; |
|
|
166 | |
|
|
167 | glBegin GL_QUADS; |
|
|
168 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
169 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
|
|
170 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
|
|
171 | glTexCoord 1, 0; glVertex $tex->{width}, 0; |
|
|
172 | glEnd; |
|
|
173 | |
|
|
174 | glDisable GL_BLEND; |
|
|
175 | glDisable GL_TEXTURE_2D; |
|
|
176 | } |
97 | |
177 | |
98 | package Crossfire::Client::Widget::Frame; |
178 | package Crossfire::Client::Widget::Frame; |
99 | |
179 | |
100 | our @ISA = Crossfire::Client::Widget::Container::; |
180 | our @ISA = Crossfire::Client::Widget::Container::; |
101 | |
181 | |
… | |
… | |
286 | sub _draw { |
366 | sub _draw { |
287 | my ($self) = @_; |
367 | my ($self) = @_; |
288 | |
368 | |
289 | my $tex = $self->{texture}; |
369 | my $tex = $self->{texture}; |
290 | |
370 | |
291 | $self->{x}--; |
|
|
292 | |
|
|
293 | glEnable GL_BLEND; |
371 | glEnable GL_BLEND; |
294 | glEnable GL_TEXTURE_2D; |
372 | glEnable GL_TEXTURE_2D; |
295 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
373 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
296 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
374 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
297 | |
375 | |
… | |
… | |
363 | for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { |
441 | for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { |
364 | my $tex = $::CONN->{face}[$num]{texture} || next; |
442 | my $tex = $::CONN->{face}[$num]{texture} || next; |
365 | |
443 | |
366 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
444 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
367 | |
445 | |
|
|
446 | my $w = $tex->{width}; |
|
|
447 | my $h = $tex->{height}; |
|
|
448 | |
|
|
449 | my $px = ($x + 1) * 32 - $w; |
|
|
450 | my $py = ($y + 1) * 32 - $h; |
|
|
451 | |
368 | glBegin GL_QUADS; |
452 | glBegin GL_QUADS; |
369 | glTexCoord 0, 0; glVertex $x * 32 , $y * 32; |
453 | glTexCoord 0, 0; glVertex $px , $py; |
370 | glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32; |
454 | glTexCoord 0, 1; glVertex $px , $py + $h; |
371 | glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32; |
455 | glTexCoord 1, 1; glVertex $px + $w, $py + $h; |
372 | glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32; |
456 | glTexCoord 1, 0; glVertex $px + $w, $py; |
373 | glEnd; |
457 | glEnd; |
374 | } |
458 | } |
375 | } |
459 | } |
376 | } |
460 | } |
377 | |
461 | |
… | |
… | |
379 | glDisable GL_BLEND; |
463 | glDisable GL_BLEND; |
380 | } |
464 | } |
381 | |
465 | |
382 | my %DIR = ( |
466 | my %DIR = ( |
383 | SDLK_KP8, [1, "north"], |
467 | SDLK_KP8, [1, "north"], |
384 | SDLK_KP9, [2, "northest"], |
468 | SDLK_KP9, [2, "northeast"], |
385 | SDLK_KP6, [3, "east"], |
469 | SDLK_KP6, [3, "east"], |
386 | SDLK_KP3, [4, "southeast"], |
470 | SDLK_KP3, [4, "southeast"], |
387 | SDLK_KP2, [5, "south"], |
471 | SDLK_KP2, [5, "south"], |
388 | SDLK_KP1, [6, "southwest"], |
472 | SDLK_KP1, [6, "southwest"], |
389 | SDLK_KP4, [7, "west"], |
473 | SDLK_KP4, [7, "west"], |
390 | SDLK_KP7, [8, "northwest"], |
474 | SDLK_KP7, [8, "northwest"], |
|
|
475 | |
|
|
476 | SDLK_UP, [1, "north"], |
|
|
477 | SDLK_RIGHT, [3, "east"], |
|
|
478 | SDLK_DOWN, [5, "south"], |
|
|
479 | SDLK_LEFT, [7, "west"], |
391 | ); |
480 | ); |
392 | |
481 | |
393 | sub key_down { |
482 | sub key_down { |
394 | my ($self, $ev) = @_; |
483 | my ($self, $ev) = @_; |
395 | |
484 | |
… | |
… | |
398 | |
487 | |
399 | if ($sym == SDLK_KP5) { |
488 | if ($sym == SDLK_KP5) { |
400 | $::CONN->send ("command stay fire"); |
489 | $::CONN->send ("command stay fire"); |
401 | } elsif (exists $DIR{$sym}) { |
490 | } elsif (exists $DIR{$sym}) { |
402 | if ($mod & KMOD_SHIFT) { |
491 | if ($mod & KMOD_SHIFT) { |
|
|
492 | $self->{shft}++; |
403 | $::CONN->send ("command fire $DIR{$sym}[0]"); |
493 | $::CONN->send ("command fire $DIR{$sym}[0]"); |
404 | } elsif ($mod & KMOD_CTRL) { |
494 | } elsif ($mod & KMOD_CTRL) { |
|
|
495 | $self->{ctrl}++; |
405 | $::CONN->send ("command run $DIR{$sym}[0]"); |
496 | $::CONN->send ("command run $DIR{$sym}[0]"); |
406 | } else { |
497 | } else { |
|
|
498 | $::CONN->send ("command $DIR{$sym}[1]"); |
407 | } |
499 | } |
408 | } |
500 | } |
409 | } |
501 | } |
410 | |
502 | |
411 | sub key_up { |
503 | sub key_up { |
412 | my ($self, $ev) = @_; |
504 | my ($self, $ev) = @_; |
413 | |
505 | |
414 | my $mod = $ev->key_mod; |
506 | my $mod = $ev->key_mod; |
415 | my $sym = $ev->key_sym; |
507 | my $sym = $ev->key_sym; |
416 | |
508 | |
417 | if (exists $DIR{$sym}) { |
509 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
418 | if ($mod & KMOD_SHIFT) { |
|
|
419 | $::CONN->send ("command fire_stop"); |
510 | $::CONN->send ("command fire_stop"); |
420 | } else { |
511 | } |
|
|
512 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
421 | $::CONN->send ("command run_stop"); |
513 | $::CONN->send ("command run_stop"); |
422 | } |
|
|
423 | } |
514 | } |
424 | } |
515 | } |
425 | |
516 | |
426 | 1; |
517 | 1; |
427 | |
518 | |