ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.27
Committed: Sat Apr 8 22:25:37 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.26: +2 -4 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 elmex 1.6 package Crossfire::Client::Widget;
2 root 1.8
3 elmex 1.1 use strict;
4 root 1.18
5     use Scalar::Util;
6    
7 elmex 1.11 use SDL::OpenGL;
8     use SDL::OpenGL::Constants;
9 elmex 1.1
10     our $FOCUS; # the widget with current focus
11 elmex 1.13 our @ACTIVE_WIDGETS;
12 elmex 1.1
13     # class methods for events
14     sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
15     sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS }
16     sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS }
17     sub feed_sdl_button_up_event { $FOCUS->button_up ($_[0]) if $FOCUS }
18    
19     sub new {
20     my $class = shift;
21 root 1.10
22     bless { @_ }, $class
23 elmex 1.1 }
24    
25 elmex 1.2 sub activate {
26 elmex 1.13 push @ACTIVE_WIDGETS, $_[0];
27 root 1.18 Scalar::Util::weaken $ACTIVE_WIDGETS[-1];
28 elmex 1.2 }
29 root 1.4
30 elmex 1.2 sub deactivate {
31 elmex 1.13 @ACTIVE_WIDGETS =
32     sort { $a->{z} <=> $b->{z} }
33 root 1.18 grep { $_ && $_ != $_[0] }
34 elmex 1.13 @ACTIVE_WIDGETS;
35 elmex 1.2 }
36    
37 root 1.18 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 elmex 1.20 sub needs_redraw {
45     0
46     }
47    
48 root 1.14 sub size_request {
49     die "size_request is abtract";
50     }
51    
52 elmex 1.1 sub focus_in {
53     my ($widget) = @_;
54     $FOCUS = $widget;
55     }
56 root 1.4
57 elmex 1.1 sub focus_out {
58     my ($widget) = @_;
59     }
60 root 1.4
61 elmex 1.1 sub key_down {
62     my ($widget, $sdlev) = @_;
63     }
64 root 1.4
65 elmex 1.1 sub key_up {
66     my ($widget, $sdlev) = @_;
67     }
68 root 1.4
69 elmex 1.1 sub button_down {
70     my ($widget, $sdlev) = @_;
71     }
72 root 1.4
73 elmex 1.1 sub button_up {
74     my ($widget, $sdlev) = @_;
75     }
76 root 1.4
77 elmex 1.15 sub w { $_[0]->{w} }
78     sub h { $_[0]->{h} }
79 elmex 1.11 sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} }
80     sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} }
81 elmex 1.13 sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} }
82 elmex 1.11
83 elmex 1.1 sub draw {
84 elmex 1.11 my ($self) = @_;
85    
86     glPushMatrix;
87 root 1.12 glTranslate $self->{x}, $self->{y}, 0;
88 elmex 1.11 $self->_draw;
89     glPopMatrix;
90     }
91    
92     sub _draw {
93 elmex 1.1 my ($widget) = @_;
94     }
95 root 1.4
96 elmex 1.1 sub bbox {
97     my ($widget) = @_;
98     }
99 elmex 1.2
100 root 1.18 sub DESTROY {
101     my ($self) = @_;
102    
103     $self->deactivate;
104     }
105    
106 elmex 1.15 package Crossfire::Client::Widget::Container;
107    
108     our @ISA = Crossfire::Client::Widget::;
109    
110     use SDL::OpenGL;
111    
112     sub add { $_[0]->{child} = $_[1] }
113     sub get { $_[0]->{child} }
114    
115     sub size_request { $_[0]->{child}->size_request if $_[0]->{child} }
116    
117     sub _draw { die "Containers can't be drawn!" }
118    
119 elmex 1.20 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 elmex 1.26 $self->render_chld; #TODO: Move this to the size_request event propably?
129 elmex 1.20 }
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 root 1.23 $w, $h, sub { $chld->draw }
139 elmex 1.20 );
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 root 1.23 my ($w, $h) = $self->size_request;#TODO# use widht/height of texture
154 elmex 1.20 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 root 1.23 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 elmex 1.20 glEnd;
170    
171     glDisable GL_BLEND;
172     glDisable GL_TEXTURE_2D;
173     }
174    
175 elmex 1.15 package Crossfire::Client::Widget::Frame;
176    
177     our @ISA = Crossfire::Client::Widget::Container::;
178    
179     use SDL::OpenGL;
180    
181     sub size_request {
182     my ($self) = @_;
183     my $chld = $self->get
184     or return (0, 0);
185     map { $_ + 4 } $chld->size_request;
186     }
187    
188     sub _draw {
189     my ($self) = @_;
190    
191     my $chld = $self->get;
192    
193 elmex 1.26 $chld->move (2, 2, 0); #TODO:move to size_request
194    
195 elmex 1.15 my ($w, $h) = $chld->size_request;
196    
197     glColor 1, 0, 0;
198     glBegin GL_QUADS;
199     glTexCoord 0, 0; glVertex 0 , 0;
200     glTexCoord 0, 1; glVertex 0 , $h + 4;
201     glTexCoord 1, 1; glVertex $w + 4 , $h + 4;
202     glTexCoord 1, 0; glVertex $w + 4 , 0;
203     glEnd;
204    
205 root 1.23 $chld->draw;
206 elmex 1.15 }
207    
208     package Crossfire::Client::Widget::Table;
209    
210     our @ISA = Crossfire::Client::Widget::Container::;
211    
212     use SDL::OpenGL;
213    
214     sub add {
215     my ($self, $x, $y, $chld) = @_;
216     $self->{childs}[$y][$x] = $chld;
217     }
218    
219     sub max_row_height {
220     my ($self, $row) = @_;
221    
222     my $hs = 0;
223     for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) {
224     my $c = $self->{childs}->[$row]->[$xi];
225 elmex 1.17 if ($c) {
226     my ($w, $h) = $c->size_request;
227     if ($hs < $h) { $hs = $h }
228     }
229 elmex 1.15 }
230     return $hs;
231     }
232    
233     sub max_col_width {
234     my ($self, $col) = @_;
235    
236     my $ws = 0;
237     for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) {
238     my $c = ($self->{childs}->[$yi] || [])->[$col];
239 elmex 1.17 if ($c) {
240     my ($w, $h) = $c->size_request;
241     if ($ws < $w) { $ws = $w }
242     }
243 elmex 1.15 }
244     return $ws;
245     }
246    
247     sub size_request {
248     my ($self) = @_;
249    
250     my ($hs, $ws) = (0, 0);
251    
252     for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
253     $hs += $self->max_row_height ($yi);
254     }
255    
256     for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
257     my $wm = 0;
258     for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
259     $wm += $self->max_col_width ($xi)
260     }
261     if ($ws < $wm) { $ws = $wm }
262     }
263    
264     return ($ws, $hs);
265     }
266    
267     sub _draw {
268     my ($self) = @_;
269    
270     my $y = 0;
271     for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
272     my $x = 0;
273    
274     for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
275    
276     my $c = $self->{childs}->[$yi]->[$xi];
277 elmex 1.26 if ($c) {
278     $c->move ($x, $y, 0); #TODO: Move to size_request
279     $c->draw if $c;
280     }
281 elmex 1.15
282     $x += $self->max_col_width ($xi);
283     }
284    
285     $y += $self->max_row_height ($yi);
286     }
287     }
288    
289     package Crossfire::Client::Widget::VBox;
290    
291     our @ISA = Crossfire::Client::Widget::Container::;
292    
293     use SDL::OpenGL;
294    
295     sub add {
296     my ($self, $chld) = @_;
297     push @{$self->{childs}}, $chld;
298     }
299    
300     sub size_request {
301     my ($self) = @_;
302    
303     my ($hs, $ws) = (0, 0);
304     for (@{$self->{childs} || []}) {
305     my ($w, $h) = $_->size_request;
306     $hs += $h;
307     if ($ws < $w) { $ws = $w }
308     }
309    
310     return ($ws, $hs);
311     }
312    
313     sub _draw {
314     my ($self) = @_;
315    
316     my ($x, $y);
317     for (@{$self->{childs} || []}) {
318 elmex 1.26 $_->move (0, $y, 0); #TODO: move to size_request
319 root 1.23 $_->draw;
320 elmex 1.15 my ($w, $h) = $_->size_request;
321     $y += $h;
322     }
323     }
324    
325 root 1.10 package Crossfire::Client::Widget::Label;
326    
327 root 1.12 our @ISA = Crossfire::Client::Widget::;
328    
329 root 1.10 use SDL::OpenGL;
330    
331     sub new {
332 root 1.12 my ($class, $x, $y, $z, $ttf, $text) = @_;
333 root 1.10
334 elmex 1.15 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf);
335 root 1.10
336 elmex 1.15 $self->set_text ($text);
337 root 1.10
338     $self
339     }
340    
341 elmex 1.15 sub set_text {
342     my ($self, $text) = @_;
343     $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text;
344     }
345    
346     sub get_text {
347     my ($self, $text) = @_;
348     $self->{text}
349     }
350    
351 root 1.14 sub size_request {
352     my ($self) = @_;
353    
354     (
355     $self->{texture}{width},
356     $self->{texture}{height},
357     )
358     }
359    
360 elmex 1.11 sub _draw {
361 root 1.10 my ($self) = @_;
362    
363     my $tex = $self->{texture};
364    
365 root 1.12 glEnable GL_BLEND;
366 root 1.10 glEnable GL_TEXTURE_2D;
367 elmex 1.26 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;#DECAL;
368 root 1.10 glBindTexture GL_TEXTURE_2D, $tex->{name};
369    
370 elmex 1.26 glColor 1, 0, 1;
371 root 1.12
372 root 1.10 glBegin GL_QUADS;
373     glTexCoord 0, 0; glVertex 0 , 0;
374     glTexCoord 0, 1; glVertex 0 , $tex->{height};
375     glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
376     glTexCoord 1, 0; glVertex $tex->{width}, 0;
377     glEnd;
378    
379 root 1.12 glDisable GL_BLEND;
380 root 1.10 glDisable GL_TEXTURE_2D;
381     }
382    
383 elmex 1.9 package Crossfire::Client::Widget::TextView;
384 root 1.4
385 elmex 1.3 use strict;
386 root 1.10
387 elmex 1.9 our @ISA = qw/Crossfire::Client::Widget/;
388 elmex 1.3
389     use SDL::OpenGL;
390     use SDL::OpenGL::Constants;
391    
392     sub add_line {
393     my ($self, $line) = @_;
394     push @{$self->{lines}}, $line;
395     }
396    
397 elmex 1.11 sub _draw {
398 elmex 1.3 my ($self) = @_;
399    
400     }
401    
402 elmex 1.9 package Crossfire::Client::Widget::MapWidget;
403 root 1.4
404 elmex 1.2 use strict;
405 elmex 1.7
406 root 1.25 use List::Util qw(min max);
407 elmex 1.2
408 root 1.16 use SDL;
409 elmex 1.2 use SDL::OpenGL;
410     use SDL::OpenGL::Constants;
411    
412 root 1.25 our @ISA = Crossfire::Client::Widget::;
413    
414 elmex 1.2 sub key_down {
415     print "MAPKEYDOWN\n";
416     }
417    
418     sub key_up {
419     }
420    
421 elmex 1.11 sub _draw {
422 root 1.21 my ($self) = @_;
423    
424 root 1.25 my $mx = $::CONN->{mapx};
425     my $my = $::CONN->{mapy};
426    
427     my $map = $::CONN->{map};
428    
429     my ($xofs, $yofs);
430    
431     my $sw = 1 + int $::WIDTH / 32;
432     my $sh = 1 + int $::HEIGHT / 32;
433    
434     if ($::CONN->{mapw} > $sw) {
435 root 1.27 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
436 root 1.25 } else {
437     $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
438     }
439    
440     if ($::CONN->{maph} > $sh) {
441 root 1.27 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
442 root 1.25 } else {
443     $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
444     }
445    
446 elmex 1.2 glEnable GL_TEXTURE_2D;
447     glEnable GL_BLEND;
448 root 1.12 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
449 elmex 1.2
450 root 1.25 for my $x (0 .. $sw - 1) {
451     for my $y (0 .. $sh - 1) {
452 elmex 1.2
453 root 1.25 my $cell = $map->[$x + $xofs][$y + $yofs]
454 elmex 1.2 or next;
455    
456 root 1.21 my $darkness = $cell->[0] * (1 / 255);
457     if ($darkness < 0) {
458 root 1.24 glColor 0.3, 0.3, 0.3;
459     } else {
460     glColor $darkness, $darkness, $darkness;
461 root 1.21 }
462 elmex 1.2
463 root 1.21 for my $num (grep $_, @$cell[1,2,3]) {
464 root 1.4 my $tex = $::CONN->{face}[$num]{texture} || next;
465 elmex 1.2
466 root 1.4 glBindTexture GL_TEXTURE_2D, $tex->{name};
467 elmex 1.2
468 root 1.19 my $w = $tex->{width};
469     my $h = $tex->{height};
470    
471     my $px = ($x + 1) * 32 - $w;
472     my $py = ($y + 1) * 32 - $h;
473    
474 elmex 1.2 glBegin GL_QUADS;
475 root 1.19 glTexCoord 0, 0; glVertex $px , $py;
476     glTexCoord 0, 1; glVertex $px , $py + $h;
477     glTexCoord 1, 1; glVertex $px + $w, $py + $h;
478     glTexCoord 1, 0; glVertex $px + $w, $py;
479 elmex 1.2 glEnd;
480     }
481     }
482     }
483    
484     glDisable GL_TEXTURE_2D;
485     glDisable GL_BLEND;
486     }
487    
488 root 1.16 my %DIR = (
489     SDLK_KP8, [1, "north"],
490 root 1.18 SDLK_KP9, [2, "northeast"],
491 root 1.16 SDLK_KP6, [3, "east"],
492     SDLK_KP3, [4, "southeast"],
493     SDLK_KP2, [5, "south"],
494     SDLK_KP1, [6, "southwest"],
495     SDLK_KP4, [7, "west"],
496     SDLK_KP7, [8, "northwest"],
497 root 1.18
498     SDLK_UP, [1, "north"],
499     SDLK_RIGHT, [3, "east"],
500     SDLK_DOWN, [5, "south"],
501     SDLK_LEFT, [7, "west"],
502 root 1.16 );
503    
504     sub key_down {
505     my ($self, $ev) = @_;
506    
507     my $mod = $ev->key_mod;
508     my $sym = $ev->key_sym;
509    
510     if ($sym == SDLK_KP5) {
511     $::CONN->send ("command stay fire");
512     } elsif (exists $DIR{$sym}) {
513     if ($mod & KMOD_SHIFT) {
514 root 1.18 $self->{shft}++;
515 root 1.16 $::CONN->send ("command fire $DIR{$sym}[0]");
516     } elsif ($mod & KMOD_CTRL) {
517 root 1.18 $self->{ctrl}++;
518 root 1.16 $::CONN->send ("command run $DIR{$sym}[0]");
519     } else {
520 root 1.18 $::CONN->send ("command $DIR{$sym}[1]");
521 root 1.16 }
522     }
523     }
524    
525     sub key_up {
526     my ($self, $ev) = @_;
527    
528     my $mod = $ev->key_mod;
529     my $sym = $ev->key_sym;
530    
531 root 1.18 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
532     $::CONN->send ("command fire_stop");
533     }
534     if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
535     $::CONN->send ("command run_stop");
536 root 1.16 }
537     }
538    
539 elmex 1.1 1;
540 root 1.5