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 | |
… | |
… | |
144 | my ($self, $row) = @_; |
221 | my ($self, $row) = @_; |
145 | |
222 | |
146 | my $hs = 0; |
223 | my $hs = 0; |
147 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { |
224 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { |
148 | my $c = $self->{childs}->[$row]->[$xi]; |
225 | my $c = $self->{childs}->[$row]->[$xi]; |
|
|
226 | if ($c) { |
149 | my ($w, $h) = $c->size_request if $c; |
227 | my ($w, $h) = $c->size_request; |
150 | if ($hs < $h) { $hs = $h } |
228 | if ($hs < $h) { $hs = $h } |
|
|
229 | } |
151 | } |
230 | } |
152 | return $hs; |
231 | return $hs; |
153 | } |
232 | } |
154 | |
233 | |
155 | sub max_col_width { |
234 | sub max_col_width { |
156 | my ($self, $col) = @_; |
235 | my ($self, $col) = @_; |
157 | |
236 | |
158 | my $ws = 0; |
237 | my $ws = 0; |
159 | for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { |
238 | for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { |
160 | my $c = ($self->{childs}->[$yi] || [])->[$col]; |
239 | my $c = ($self->{childs}->[$yi] || [])->[$col]; |
|
|
240 | if ($c) { |
161 | my ($w, $h) = $c->size_request if $c; |
241 | my ($w, $h) = $c->size_request; |
162 | if ($ws < $w) { $ws = $w } |
242 | if ($ws < $w) { $ws = $w } |
|
|
243 | } |
163 | } |
244 | } |
164 | return $ws; |
245 | return $ws; |
165 | } |
246 | } |
166 | |
247 | |
167 | sub size_request { |
248 | sub size_request { |
… | |
… | |
192 | my $x = 0; |
273 | my $x = 0; |
193 | |
274 | |
194 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { |
275 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { |
195 | |
276 | |
196 | glPushMatrix; |
277 | glPushMatrix; |
197 | glTranslate ($x, $y, 0); |
278 | glTranslate ($x, $y, 0);#TODO#there must be no translate here, instead the widget must be moved |
198 | my $c = $self->{childs}->[$yi]->[$xi]; |
279 | my $c = $self->{childs}->[$yi]->[$xi]; |
199 | $c->_draw if $c; |
280 | $c->draw if $c; |
200 | glPopMatrix; |
281 | glPopMatrix; |
201 | |
282 | |
202 | $x += $self->max_col_width ($xi); |
283 | $x += $self->max_col_width ($xi); |
203 | } |
284 | } |
204 | |
285 | |
… | |
… | |
234 | my ($self) = @_; |
315 | my ($self) = @_; |
235 | |
316 | |
236 | my ($x, $y); |
317 | my ($x, $y); |
237 | for (@{$self->{childs} || []}) { |
318 | for (@{$self->{childs} || []}) { |
238 | glPushMatrix; |
319 | glPushMatrix; |
239 | glTranslate (0, $y, 0); |
320 | glTranslate (0, $y, 0);# see above TODO |
240 | $_->_draw; |
321 | $_->draw; |
241 | glPopMatrix; |
322 | glPopMatrix; |
242 | my ($w, $h) = $_->size_request; |
323 | my ($w, $h) = $_->size_request; |
243 | $y += $h; |
324 | $y += $h; |
244 | } |
325 | } |
245 | } |
326 | } |
… | |
… | |
281 | |
362 | |
282 | sub _draw { |
363 | sub _draw { |
283 | my ($self) = @_; |
364 | my ($self) = @_; |
284 | |
365 | |
285 | my $tex = $self->{texture}; |
366 | my $tex = $self->{texture}; |
286 | |
|
|
287 | $self->{x}--; |
|
|
288 | |
367 | |
289 | glEnable GL_BLEND; |
368 | glEnable GL_BLEND; |
290 | glEnable GL_TEXTURE_2D; |
369 | glEnable GL_TEXTURE_2D; |
291 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
370 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
292 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
371 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
… | |
… | |
339 | |
418 | |
340 | sub key_up { |
419 | sub key_up { |
341 | } |
420 | } |
342 | |
421 | |
343 | sub _draw { |
422 | sub _draw { |
|
|
423 | my ($self) = @_; |
|
|
424 | |
344 | glEnable GL_TEXTURE_2D; |
425 | glEnable GL_TEXTURE_2D; |
345 | glEnable GL_BLEND; |
426 | glEnable GL_BLEND; |
346 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
427 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
347 | |
428 | |
348 | my $map = $::CONN->{map}; |
429 | my $map = $::CONN->{map}; |
349 | |
430 | |
350 | for my $x (0 .. $::CONN->{mapw} - 1) { |
431 | for my $x (0 .. int $::WIDTH / 32) { |
351 | for my $y (0 .. $::CONN->{maph} - 1) { |
432 | for my $y (0 .. int $::HEIGHT / 32) { |
352 | |
433 | |
353 | my $cell = $map->[$x][$y] |
434 | my $cell = $map->[$x + $::CONN->{mapx}] |
|
|
435 | [$y + $::CONN->{mapy}] |
354 | or next; |
436 | or next; |
355 | |
437 | |
356 | my $darkness = $cell->[3] * (1 / 255); |
438 | my $darkness = $cell->[0] * (1 / 255); |
|
|
439 | if ($darkness < 0) { |
|
|
440 | glColor 0.3, 0.3, 0.3; |
|
|
441 | } else { |
357 | glColor $darkness, $darkness, $darkness; |
442 | glColor $darkness, $darkness, $darkness; |
|
|
443 | } |
358 | |
444 | |
359 | for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { |
445 | for my $num (grep $_, @$cell[1,2,3]) { |
360 | my $tex = $::CONN->{face}[$num]{texture} || next; |
446 | my $tex = $::CONN->{face}[$num]{texture} || next; |
361 | |
447 | |
362 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
448 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
363 | |
449 | |
|
|
450 | my $w = $tex->{width}; |
|
|
451 | my $h = $tex->{height}; |
|
|
452 | |
|
|
453 | my $px = ($x + 1) * 32 - $w; |
|
|
454 | my $py = ($y + 1) * 32 - $h; |
|
|
455 | |
364 | glBegin GL_QUADS; |
456 | glBegin GL_QUADS; |
365 | glTexCoord 0, 0; glVertex $x * 32 , $y * 32; |
457 | glTexCoord 0, 0; glVertex $px , $py; |
366 | glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32; |
458 | glTexCoord 0, 1; glVertex $px , $py + $h; |
367 | glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32; |
459 | glTexCoord 1, 1; glVertex $px + $w, $py + $h; |
368 | glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32; |
460 | glTexCoord 1, 0; glVertex $px + $w, $py; |
369 | glEnd; |
461 | glEnd; |
370 | } |
462 | } |
371 | } |
463 | } |
372 | } |
464 | } |
373 | |
465 | |
… | |
… | |
375 | glDisable GL_BLEND; |
467 | glDisable GL_BLEND; |
376 | } |
468 | } |
377 | |
469 | |
378 | my %DIR = ( |
470 | my %DIR = ( |
379 | SDLK_KP8, [1, "north"], |
471 | SDLK_KP8, [1, "north"], |
380 | SDLK_KP9, [2, "northest"], |
472 | SDLK_KP9, [2, "northeast"], |
381 | SDLK_KP6, [3, "east"], |
473 | SDLK_KP6, [3, "east"], |
382 | SDLK_KP3, [4, "southeast"], |
474 | SDLK_KP3, [4, "southeast"], |
383 | SDLK_KP2, [5, "south"], |
475 | SDLK_KP2, [5, "south"], |
384 | SDLK_KP1, [6, "southwest"], |
476 | SDLK_KP1, [6, "southwest"], |
385 | SDLK_KP4, [7, "west"], |
477 | SDLK_KP4, [7, "west"], |
386 | SDLK_KP7, [8, "northwest"], |
478 | SDLK_KP7, [8, "northwest"], |
|
|
479 | |
|
|
480 | SDLK_UP, [1, "north"], |
|
|
481 | SDLK_RIGHT, [3, "east"], |
|
|
482 | SDLK_DOWN, [5, "south"], |
|
|
483 | SDLK_LEFT, [7, "west"], |
387 | ); |
484 | ); |
388 | |
485 | |
389 | sub key_down { |
486 | sub key_down { |
390 | my ($self, $ev) = @_; |
487 | my ($self, $ev) = @_; |
391 | |
488 | |
… | |
… | |
394 | |
491 | |
395 | if ($sym == SDLK_KP5) { |
492 | if ($sym == SDLK_KP5) { |
396 | $::CONN->send ("command stay fire"); |
493 | $::CONN->send ("command stay fire"); |
397 | } elsif (exists $DIR{$sym}) { |
494 | } elsif (exists $DIR{$sym}) { |
398 | if ($mod & KMOD_SHIFT) { |
495 | if ($mod & KMOD_SHIFT) { |
|
|
496 | $self->{shft}++; |
399 | $::CONN->send ("command fire $DIR{$sym}[0]"); |
497 | $::CONN->send ("command fire $DIR{$sym}[0]"); |
400 | } elsif ($mod & KMOD_CTRL) { |
498 | } elsif ($mod & KMOD_CTRL) { |
|
|
499 | $self->{ctrl}++; |
401 | $::CONN->send ("command run $DIR{$sym}[0]"); |
500 | $::CONN->send ("command run $DIR{$sym}[0]"); |
402 | } else { |
501 | } else { |
|
|
502 | $::CONN->send ("command $DIR{$sym}[1]"); |
403 | } |
503 | } |
404 | } |
504 | } |
405 | } |
505 | } |
406 | |
506 | |
407 | sub key_up { |
507 | sub key_up { |
408 | my ($self, $ev) = @_; |
508 | my ($self, $ev) = @_; |
409 | |
509 | |
410 | my $mod = $ev->key_mod; |
510 | my $mod = $ev->key_mod; |
411 | my $sym = $ev->key_sym; |
511 | my $sym = $ev->key_sym; |
412 | |
512 | |
413 | if (exists $DIR{$sym}) { |
513 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
414 | if ($mod & KMOD_SHIFT) { |
|
|
415 | $::CONN->send ("command fire_stop"); |
514 | $::CONN->send ("command fire_stop"); |
416 | } else { |
515 | } |
|
|
516 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
417 | $::CONN->send ("command run_stop"); |
517 | $::CONN->send ("command run_stop"); |
418 | } |
|
|
419 | } |
518 | } |
420 | } |
519 | } |
421 | |
520 | |
422 | 1; |
521 | 1; |
423 | |
522 | |