ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.31
Committed: Sun Apr 9 00:43:11 2006 UTC (18 years, 1 month ago) by elmex
Branch: MAIN
Changes since 1.30: +106 -1 lines
Log Message:
added basic TextEntry

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.29 my ($w, $h) = $self->size_request;#TODO# use width/height of texture
154    
155 elmex 1.20 my $tex = $self->{texture}
156     or return;
157    
158     glEnable GL_BLEND;
159     glEnable GL_TEXTURE_2D;
160     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
161     glBindTexture GL_TEXTURE_2D, $tex->{name};
162    
163 elmex 1.31 glColor 1, 0, 1;
164 elmex 1.20
165     glBegin GL_QUADS;
166 root 1.23 glTexCoord 0, 0; glVertex 0, 0;
167     glTexCoord 0, 1; glVertex 0, $h;
168     glTexCoord 1, 1; glVertex $w, $h;
169     glTexCoord 1, 0; glVertex $w, 0;
170 elmex 1.20 glEnd;
171    
172     glDisable GL_BLEND;
173     glDisable GL_TEXTURE_2D;
174     }
175    
176 elmex 1.15 package Crossfire::Client::Widget::Frame;
177    
178     our @ISA = Crossfire::Client::Widget::Container::;
179    
180     use SDL::OpenGL;
181    
182     sub size_request {
183     my ($self) = @_;
184     my $chld = $self->get
185     or return (0, 0);
186 root 1.30
187     $chld->move (2, 2);
188    
189 elmex 1.15 map { $_ + 4 } $chld->size_request;
190     }
191    
192     sub _draw {
193     my ($self) = @_;
194    
195     my $chld = $self->get;
196    
197     my ($w, $h) = $chld->size_request;
198    
199     glBegin GL_QUADS;
200 root 1.30 glColor 0, 0, 0;
201 elmex 1.15 glTexCoord 0, 0; glVertex 0 , 0;
202     glTexCoord 0, 1; glVertex 0 , $h + 4;
203     glTexCoord 1, 1; glVertex $w + 4 , $h + 4;
204     glTexCoord 1, 0; glVertex $w + 4 , 0;
205     glEnd;
206    
207 root 1.23 $chld->draw;
208 elmex 1.15 }
209    
210 elmex 1.31 package Crossfire::Client::Widget::FancyFrame;
211    
212     our @ISA = Crossfire::Client::Widget::Frame::;
213    
214     use SDL::OpenGL;
215    
216     #TODO: implement themed frame
217    
218 elmex 1.15 package Crossfire::Client::Widget::Table;
219    
220     our @ISA = Crossfire::Client::Widget::Container::;
221    
222     use SDL::OpenGL;
223    
224     sub add {
225     my ($self, $x, $y, $chld) = @_;
226     $self->{childs}[$y][$x] = $chld;
227     }
228    
229     sub max_row_height {
230     my ($self, $row) = @_;
231    
232     my $hs = 0;
233     for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) {
234     my $c = $self->{childs}->[$row]->[$xi];
235 elmex 1.17 if ($c) {
236     my ($w, $h) = $c->size_request;
237     if ($hs < $h) { $hs = $h }
238     }
239 elmex 1.15 }
240     return $hs;
241     }
242    
243     sub max_col_width {
244     my ($self, $col) = @_;
245    
246     my $ws = 0;
247     for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) {
248     my $c = ($self->{childs}->[$yi] || [])->[$col];
249 elmex 1.17 if ($c) {
250     my ($w, $h) = $c->size_request;
251     if ($ws < $w) { $ws = $w }
252     }
253 elmex 1.15 }
254     return $ws;
255     }
256    
257     sub size_request {
258     my ($self) = @_;
259    
260     my ($hs, $ws) = (0, 0);
261    
262     for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
263     $hs += $self->max_row_height ($yi);
264     }
265    
266     for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
267     my $wm = 0;
268     for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
269     $wm += $self->max_col_width ($xi)
270     }
271     if ($ws < $wm) { $ws = $wm }
272     }
273    
274     return ($ws, $hs);
275     }
276    
277     sub _draw {
278     my ($self) = @_;
279    
280     my $y = 0;
281     for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
282     my $x = 0;
283    
284     for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
285    
286     my $c = $self->{childs}->[$yi]->[$xi];
287 elmex 1.26 if ($c) {
288     $c->move ($x, $y, 0); #TODO: Move to size_request
289     $c->draw if $c;
290     }
291 elmex 1.15
292     $x += $self->max_col_width ($xi);
293     }
294    
295     $y += $self->max_row_height ($yi);
296     }
297     }
298    
299     package Crossfire::Client::Widget::VBox;
300    
301     our @ISA = Crossfire::Client::Widget::Container::;
302    
303     use SDL::OpenGL;
304    
305     sub add {
306     my ($self, $chld) = @_;
307     push @{$self->{childs}}, $chld;
308     }
309    
310     sub size_request {
311     my ($self) = @_;
312    
313     my ($hs, $ws) = (0, 0);
314     for (@{$self->{childs} || []}) {
315     my ($w, $h) = $_->size_request;
316     $hs += $h;
317     if ($ws < $w) { $ws = $w }
318     }
319    
320     return ($ws, $hs);
321     }
322    
323     sub _draw {
324     my ($self) = @_;
325    
326     my ($x, $y);
327     for (@{$self->{childs} || []}) {
328 elmex 1.26 $_->move (0, $y, 0); #TODO: move to size_request
329 root 1.23 $_->draw;
330 elmex 1.15 my ($w, $h) = $_->size_request;
331     $y += $h;
332     }
333     }
334    
335 root 1.10 package Crossfire::Client::Widget::Label;
336    
337 root 1.12 our @ISA = Crossfire::Client::Widget::;
338    
339 root 1.10 use SDL::OpenGL;
340    
341     sub new {
342 root 1.28 my ($class, $x, $y, $z, $height, $text) = @_;
343 root 1.10
344 root 1.28 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
345 root 1.10
346 elmex 1.15 $self->set_text ($text);
347 root 1.10
348     $self
349     }
350    
351 elmex 1.15 sub set_text {
352     my ($self, $text) = @_;
353 root 1.28
354     $self->{text} = $text;
355    
356     $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height};
357 elmex 1.15 }
358    
359     sub get_text {
360     my ($self, $text) = @_;
361 root 1.28
362 elmex 1.15 $self->{text}
363     }
364    
365 root 1.14 sub size_request {
366     my ($self) = @_;
367    
368     (
369     $self->{texture}{width},
370     $self->{texture}{height},
371     )
372     }
373    
374 elmex 1.11 sub _draw {
375 root 1.10 my ($self) = @_;
376    
377     my $tex = $self->{texture};
378    
379 root 1.12 glEnable GL_BLEND;
380 root 1.10 glEnable GL_TEXTURE_2D;
381 root 1.30 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
382 root 1.28 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
383 root 1.10 glBindTexture GL_TEXTURE_2D, $tex->{name};
384    
385 root 1.29 glColor 1, 1, 1, 0.8;
386 root 1.12
387 root 1.10 glBegin GL_QUADS;
388     glTexCoord 0, 0; glVertex 0 , 0;
389     glTexCoord 0, 1; glVertex 0 , $tex->{height};
390     glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
391     glTexCoord 1, 0; glVertex $tex->{width}, 0;
392     glEnd;
393    
394 root 1.12 glDisable GL_BLEND;
395 root 1.10 glDisable GL_TEXTURE_2D;
396     }
397    
398 elmex 1.31 package Crossfire::Client::Widget::TextEntry;
399    
400     our @ISA = Crossfire::Client::Widget::Label::;
401    
402     use SDL;
403     use SDL::OpenGL;
404    
405     sub key_down {
406     my ($self, $ev) = @_;
407    
408     my $mod = $ev->key_mod;
409     my $sym = $ev->key_sym;
410    
411     $ev->set_unicode (1);
412     my $uni = $ev->key_unicode;
413    
414     my $text = $self->get_text;
415    
416     if ($sym == SDLK_BACKSPACE) {
417     substr $text, -1, 1, '';
418    
419     } elsif ($uni) {
420     $text .= chr $uni;
421     }
422     $self->set_text ($text);
423     }
424    
425    
426     # XXX: TextView isn't neccessary with pango multiline text rendering
427 elmex 1.9 package Crossfire::Client::Widget::TextView;
428 root 1.4
429 elmex 1.3 use strict;
430 root 1.10
431 elmex 1.9 our @ISA = qw/Crossfire::Client::Widget/;
432 elmex 1.3
433     use SDL::OpenGL;
434     use SDL::OpenGL::Constants;
435    
436 elmex 1.31 sub new {
437     my ($class, $text, $h) = @_;
438     my $self = $class->SUPER::new ();
439    
440     $self->{txt_height} = $h;
441     @{$self->{lines}} = split /\r?\n/, $text;
442    
443     for (split /\r?\n/, $text) {
444     $self->add_line ($_);
445     }
446     $self
447     }
448    
449     #sub render_lines {
450     # my ($self) = @_;
451     #
452     # $self->{txt_lines} = [];
453     #
454     # for (@{$self->{lines}}) {
455     # push @{$self->{txt_lines}},
456     # new_from_ttf Crossfire::Client::Texture $self->{ttf}, $_;
457     # }
458     #}
459    
460 elmex 1.3 sub add_line {
461     my ($self, $line) = @_;
462     push @{$self->{lines}}, $line;
463 elmex 1.31
464     push @{$self->{txt_lines}},
465     new_from_text Crossfire::Client::Texture $line, $self->{txt_height};
466     }
467    
468     sub size_request {
469     my ($self) = @_;
470    
471     my $w = 0;
472     my $h = 0;
473    
474     for (@{$self->{txt_lines}}) {
475     if ($w < $_->{width}) { $w = $_->{width} }
476     $h += $_->{height};
477     }
478    
479     return ($w, $h);
480     }
481    
482     sub draw_line {
483     my ($self, $tex, $y) = @_;
484    
485     glBindTexture GL_TEXTURE_2D, $tex->{name};
486    
487     glColor 1, 0, 1;
488    
489     glBegin GL_QUADS;
490     glTexCoord 0, 0; glVertex 0 , $y;
491     glTexCoord 0, 1; glVertex 0 , $y + $tex->{height};
492     glTexCoord 1, 1; glVertex $tex->{width}, $y + $tex->{height};
493     glTexCoord 1, 0; glVertex $tex->{width}, $y;
494     glEnd;
495 elmex 1.3 }
496    
497 elmex 1.11 sub _draw {
498 elmex 1.3 my ($self) = @_;
499    
500 elmex 1.31 glEnable GL_BLEND;
501     glEnable GL_TEXTURE_2D;
502     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;#DECAL;
503    
504     my $l = 0;
505     for (@{$self->{txt_lines}}) {
506     $self->draw_line ($_, $l);
507     $l += $_->{height};
508     }
509    
510     glDisable GL_BLEND;
511     glDisable GL_TEXTURE_2D;
512 elmex 1.3 }
513    
514 elmex 1.9 package Crossfire::Client::Widget::MapWidget;
515 root 1.4
516 elmex 1.2 use strict;
517 elmex 1.7
518 root 1.25 use List::Util qw(min max);
519 elmex 1.2
520 root 1.16 use SDL;
521 elmex 1.2 use SDL::OpenGL;
522     use SDL::OpenGL::Constants;
523    
524 root 1.25 our @ISA = Crossfire::Client::Widget::;
525    
526 elmex 1.2 sub key_down {
527     print "MAPKEYDOWN\n";
528     }
529    
530     sub key_up {
531     }
532    
533 elmex 1.11 sub _draw {
534 root 1.21 my ($self) = @_;
535    
536 root 1.25 my $mx = $::CONN->{mapx};
537     my $my = $::CONN->{mapy};
538    
539     my $map = $::CONN->{map};
540    
541     my ($xofs, $yofs);
542    
543     my $sw = 1 + int $::WIDTH / 32;
544     my $sh = 1 + int $::HEIGHT / 32;
545    
546     if ($::CONN->{mapw} > $sw) {
547 root 1.27 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
548 root 1.25 } else {
549     $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
550     }
551    
552     if ($::CONN->{maph} > $sh) {
553 root 1.27 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
554 root 1.25 } else {
555     $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
556     }
557    
558 elmex 1.2 glEnable GL_TEXTURE_2D;
559     glEnable GL_BLEND;
560 root 1.12 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
561 elmex 1.2
562 root 1.25 for my $x (0 .. $sw - 1) {
563     for my $y (0 .. $sh - 1) {
564 elmex 1.2
565 root 1.25 my $cell = $map->[$x + $xofs][$y + $yofs]
566 elmex 1.2 or next;
567    
568 root 1.21 my $darkness = $cell->[0] * (1 / 255);
569     if ($darkness < 0) {
570 root 1.24 glColor 0.3, 0.3, 0.3;
571     } else {
572     glColor $darkness, $darkness, $darkness;
573 root 1.21 }
574 elmex 1.2
575 root 1.21 for my $num (grep $_, @$cell[1,2,3]) {
576 root 1.4 my $tex = $::CONN->{face}[$num]{texture} || next;
577 elmex 1.2
578 root 1.4 glBindTexture GL_TEXTURE_2D, $tex->{name};
579 elmex 1.2
580 root 1.19 my $w = $tex->{width};
581     my $h = $tex->{height};
582    
583     my $px = ($x + 1) * 32 - $w;
584     my $py = ($y + 1) * 32 - $h;
585    
586 elmex 1.2 glBegin GL_QUADS;
587 root 1.19 glTexCoord 0, 0; glVertex $px , $py;
588     glTexCoord 0, 1; glVertex $px , $py + $h;
589     glTexCoord 1, 1; glVertex $px + $w, $py + $h;
590     glTexCoord 1, 0; glVertex $px + $w, $py;
591 elmex 1.2 glEnd;
592     }
593     }
594     }
595    
596     glDisable GL_TEXTURE_2D;
597     glDisable GL_BLEND;
598     }
599    
600 root 1.16 my %DIR = (
601     SDLK_KP8, [1, "north"],
602 root 1.18 SDLK_KP9, [2, "northeast"],
603 root 1.16 SDLK_KP6, [3, "east"],
604     SDLK_KP3, [4, "southeast"],
605     SDLK_KP2, [5, "south"],
606     SDLK_KP1, [6, "southwest"],
607     SDLK_KP4, [7, "west"],
608     SDLK_KP7, [8, "northwest"],
609 root 1.18
610     SDLK_UP, [1, "north"],
611     SDLK_RIGHT, [3, "east"],
612     SDLK_DOWN, [5, "south"],
613     SDLK_LEFT, [7, "west"],
614 root 1.16 );
615    
616     sub key_down {
617     my ($self, $ev) = @_;
618    
619     my $mod = $ev->key_mod;
620     my $sym = $ev->key_sym;
621    
622     if ($sym == SDLK_KP5) {
623     $::CONN->send ("command stay fire");
624     } elsif (exists $DIR{$sym}) {
625     if ($mod & KMOD_SHIFT) {
626 root 1.18 $self->{shft}++;
627 root 1.16 $::CONN->send ("command fire $DIR{$sym}[0]");
628     } elsif ($mod & KMOD_CTRL) {
629 root 1.18 $self->{ctrl}++;
630 root 1.16 $::CONN->send ("command run $DIR{$sym}[0]");
631     } else {
632 root 1.18 $::CONN->send ("command $DIR{$sym}[1]");
633 root 1.16 }
634     }
635     }
636    
637     sub key_up {
638     my ($self, $ev) = @_;
639    
640     my $mod = $ev->key_mod;
641     my $sym = $ev->key_sym;
642    
643 root 1.18 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
644     $::CONN->send ("command fire_stop");
645     }
646     if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
647     $::CONN->send ("command run_stop");
648 root 1.16 }
649     }
650    
651 elmex 1.1 1;
652 root 1.5