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 | |
… | |
… | |
144 | my ($self, $row) = @_; |
224 | my ($self, $row) = @_; |
145 | |
225 | |
146 | my $hs = 0; |
226 | my $hs = 0; |
147 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { |
227 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { |
148 | my $c = $self->{childs}->[$row]->[$xi]; |
228 | my $c = $self->{childs}->[$row]->[$xi]; |
|
|
229 | if ($c) { |
149 | my ($w, $h) = $c->size_request if $c; |
230 | my ($w, $h) = $c->size_request; |
150 | if ($hs < $h) { $hs = $h } |
231 | if ($hs < $h) { $hs = $h } |
|
|
232 | } |
151 | } |
233 | } |
152 | return $hs; |
234 | return $hs; |
153 | } |
235 | } |
154 | |
236 | |
155 | sub max_col_width { |
237 | sub max_col_width { |
156 | my ($self, $col) = @_; |
238 | my ($self, $col) = @_; |
157 | |
239 | |
158 | my $ws = 0; |
240 | my $ws = 0; |
159 | for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { |
241 | for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { |
160 | my $c = ($self->{childs}->[$yi] || [])->[$col]; |
242 | my $c = ($self->{childs}->[$yi] || [])->[$col]; |
|
|
243 | if ($c) { |
161 | my ($w, $h) = $c->size_request if $c; |
244 | my ($w, $h) = $c->size_request; |
162 | if ($ws < $w) { $ws = $w } |
245 | if ($ws < $w) { $ws = $w } |
|
|
246 | } |
163 | } |
247 | } |
164 | return $ws; |
248 | return $ws; |
165 | } |
249 | } |
166 | |
250 | |
167 | sub size_request { |
251 | sub size_request { |
… | |
… | |
282 | sub _draw { |
366 | sub _draw { |
283 | my ($self) = @_; |
367 | my ($self) = @_; |
284 | |
368 | |
285 | my $tex = $self->{texture}; |
369 | my $tex = $self->{texture}; |
286 | |
370 | |
287 | $self->{x}--; |
|
|
288 | |
|
|
289 | glEnable GL_BLEND; |
371 | glEnable GL_BLEND; |
290 | glEnable GL_TEXTURE_2D; |
372 | glEnable GL_TEXTURE_2D; |
291 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
373 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
292 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
374 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
293 | |
375 | |
… | |
… | |
359 | for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { |
441 | for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { |
360 | my $tex = $::CONN->{face}[$num]{texture} || next; |
442 | my $tex = $::CONN->{face}[$num]{texture} || next; |
361 | |
443 | |
362 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
444 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
363 | |
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 | |
364 | glBegin GL_QUADS; |
452 | glBegin GL_QUADS; |
365 | glTexCoord 0, 0; glVertex $x * 32 , $y * 32; |
453 | glTexCoord 0, 0; glVertex $px , $py; |
366 | glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32; |
454 | glTexCoord 0, 1; glVertex $px , $py + $h; |
367 | glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32; |
455 | glTexCoord 1, 1; glVertex $px + $w, $py + $h; |
368 | glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32; |
456 | glTexCoord 1, 0; glVertex $px + $w, $py; |
369 | glEnd; |
457 | glEnd; |
370 | } |
458 | } |
371 | } |
459 | } |
372 | } |
460 | } |
373 | |
461 | |
… | |
… | |
375 | glDisable GL_BLEND; |
463 | glDisable GL_BLEND; |
376 | } |
464 | } |
377 | |
465 | |
378 | my %DIR = ( |
466 | my %DIR = ( |
379 | SDLK_KP8, [1, "north"], |
467 | SDLK_KP8, [1, "north"], |
380 | SDLK_KP9, [2, "northest"], |
468 | SDLK_KP9, [2, "northeast"], |
381 | SDLK_KP6, [3, "east"], |
469 | SDLK_KP6, [3, "east"], |
382 | SDLK_KP3, [4, "southeast"], |
470 | SDLK_KP3, [4, "southeast"], |
383 | SDLK_KP2, [5, "south"], |
471 | SDLK_KP2, [5, "south"], |
384 | SDLK_KP1, [6, "southwest"], |
472 | SDLK_KP1, [6, "southwest"], |
385 | SDLK_KP4, [7, "west"], |
473 | SDLK_KP4, [7, "west"], |
386 | 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"], |
387 | ); |
480 | ); |
388 | |
481 | |
389 | sub key_down { |
482 | sub key_down { |
390 | my ($self, $ev) = @_; |
483 | my ($self, $ev) = @_; |
391 | |
484 | |
… | |
… | |
394 | |
487 | |
395 | if ($sym == SDLK_KP5) { |
488 | if ($sym == SDLK_KP5) { |
396 | $::CONN->send ("command stay fire"); |
489 | $::CONN->send ("command stay fire"); |
397 | } elsif (exists $DIR{$sym}) { |
490 | } elsif (exists $DIR{$sym}) { |
398 | if ($mod & KMOD_SHIFT) { |
491 | if ($mod & KMOD_SHIFT) { |
|
|
492 | $self->{shft}++; |
399 | $::CONN->send ("command fire $DIR{$sym}[0]"); |
493 | $::CONN->send ("command fire $DIR{$sym}[0]"); |
400 | } elsif ($mod & KMOD_CTRL) { |
494 | } elsif ($mod & KMOD_CTRL) { |
|
|
495 | $self->{ctrl}++; |
401 | $::CONN->send ("command run $DIR{$sym}[0]"); |
496 | $::CONN->send ("command run $DIR{$sym}[0]"); |
402 | } else { |
497 | } else { |
|
|
498 | $::CONN->send ("command $DIR{$sym}[1]"); |
403 | } |
499 | } |
404 | } |
500 | } |
405 | } |
501 | } |
406 | |
502 | |
407 | sub key_up { |
503 | sub key_up { |
408 | my ($self, $ev) = @_; |
504 | my ($self, $ev) = @_; |
409 | |
505 | |
410 | my $mod = $ev->key_mod; |
506 | my $mod = $ev->key_mod; |
411 | my $sym = $ev->key_sym; |
507 | my $sym = $ev->key_sym; |
412 | |
508 | |
413 | if (exists $DIR{$sym}) { |
509 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
414 | if ($mod & KMOD_SHIFT) { |
|
|
415 | $::CONN->send ("command fire_stop"); |
510 | $::CONN->send ("command fire_stop"); |
416 | } else { |
511 | } |
|
|
512 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
417 | $::CONN->send ("command run_stop"); |
513 | $::CONN->send ("command run_stop"); |
418 | } |
|
|
419 | } |
514 | } |
420 | } |
515 | } |
421 | |
516 | |
422 | 1; |
517 | 1; |
423 | |
518 | |