ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.12 by root, Fri Apr 7 20:55:32 2006 UTC vs.
Revision 1.18 by root, Sat Apr 8 13:36:26 2006 UTC

1package Crossfire::Client::Widget; 1package Crossfire::Client::Widget;
2 2
3use strict; 3use strict;
4
5use Scalar::Util;
6
4use SDL::OpenGL; 7use SDL::OpenGL;
5use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
6 9
7our $FOCUS; # the widget with current focus 10our $FOCUS; # the widget with current focus
8our %ACTIVE_WIDGETS; 11our @ACTIVE_WIDGETS;
9 12
10# class methods for events 13# class methods for events
11sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 14sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
12sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 15sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS }
13sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS } 16sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS }
18 21
19 bless { @_ }, $class 22 bless { @_ }, $class
20} 23}
21 24
22sub activate { 25sub activate {
23 $ACTIVE_WIDGETS{$_[0]} = $_[0]; 26 push @ACTIVE_WIDGETS, $_[0];
27 Scalar::Util::weaken $ACTIVE_WIDGETS[-1];
24} 28}
25 29
26sub deactivate { 30sub deactivate {
27 delete $ACTIVE_WIDGETS{$_[0]}; 31 @ACTIVE_WIDGETS =
32 sort { $a->{z} <=> $b->{z} }
33 grep { $_ && $_ != $_[0] }
34 @ACTIVE_WIDGETS;
35}
36
37sub move {
38 my ($self, $x, $y, $z) = @_;
39 $self->{x} = $x;
40 $self->{y} = $y;
41 $self->{z} = $z if defined $z;
42}
43
44sub size_request {
45 die "size_request is abtract";
28} 46}
29 47
30sub focus_in { 48sub focus_in {
31 my ($widget) = @_; 49 my ($widget) = @_;
32 $FOCUS = $widget; 50 $FOCUS = $widget;
50 68
51sub button_up { 69sub button_up {
52 my ($widget, $sdlev) = @_; 70 my ($widget, $sdlev) = @_;
53} 71}
54 72
73sub w { $_[0]->{w} }
74sub h { $_[0]->{h} }
55sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 75sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} }
56sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 76sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} }
77sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} }
57 78
58sub draw { 79sub draw {
59 my ($self) = @_; 80 my ($self) = @_;
60 81
61 glPushMatrix; 82 glPushMatrix;
70 91
71sub bbox { 92sub bbox {
72 my ($widget) = @_; 93 my ($widget) = @_;
73} 94}
74 95
96sub DESTROY {
97 my ($self) = @_;
98
99 $self->deactivate;
100}
101
102package Crossfire::Client::Widget::Container;
103
104our @ISA = Crossfire::Client::Widget::;
105
106use SDL::OpenGL;
107
108sub add { $_[0]->{child} = $_[1] }
109sub get { $_[0]->{child} }
110
111sub size_request { $_[0]->{child}->size_request if $_[0]->{child} }
112
113sub _draw { die "Containers can't be drawn!" }
114
115package Crossfire::Client::Widget::Frame;
116
117our @ISA = Crossfire::Client::Widget::Container::;
118
119use SDL::OpenGL;
120
121sub size_request {
122 my ($self) = @_;
123 my $chld = $self->get
124 or return (0, 0);
125 map { $_ + 4 } $chld->size_request;
126}
127
128sub _draw {
129 my ($self) = @_;
130
131 my $chld = $self->get;
132
133 my ($w, $h) = $chld->size_request;
134
135 glColor 1, 0, 0;
136 glBegin GL_QUADS;
137 glTexCoord 0, 0; glVertex 0 , 0;
138 glTexCoord 0, 1; glVertex 0 , $h + 4;
139 glTexCoord 1, 1; glVertex $w + 4 , $h + 4;
140 glTexCoord 1, 0; glVertex $w + 4 , 0;
141 glEnd;
142
143 glPushMatrix;
144 glTranslate (2, 2, 0);
145 $chld->_draw;
146 glPopMatrix;
147}
148
149package Crossfire::Client::Widget::Table;
150
151our @ISA = Crossfire::Client::Widget::Container::;
152
153use SDL::OpenGL;
154
155sub add {
156 my ($self, $x, $y, $chld) = @_;
157 $self->{childs}[$y][$x] = $chld;
158}
159
160sub max_row_height {
161 my ($self, $row) = @_;
162
163 my $hs = 0;
164 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) {
165 my $c = $self->{childs}->[$row]->[$xi];
166 if ($c) {
167 my ($w, $h) = $c->size_request;
168 if ($hs < $h) { $hs = $h }
169 }
170 }
171 return $hs;
172}
173
174sub max_col_width {
175 my ($self, $col) = @_;
176
177 my $ws = 0;
178 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) {
179 my $c = ($self->{childs}->[$yi] || [])->[$col];
180 if ($c) {
181 my ($w, $h) = $c->size_request;
182 if ($ws < $w) { $ws = $w }
183 }
184 }
185 return $ws;
186}
187
188sub size_request {
189 my ($self) = @_;
190
191 my ($hs, $ws) = (0, 0);
192
193 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
194 $hs += $self->max_row_height ($yi);
195 }
196
197 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
198 my $wm = 0;
199 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
200 $wm += $self->max_col_width ($xi)
201 }
202 if ($ws < $wm) { $ws = $wm }
203 }
204
205 return ($ws, $hs);
206}
207
208sub _draw {
209 my ($self) = @_;
210
211 my $y = 0;
212 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
213 my $x = 0;
214
215 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
216
217 glPushMatrix;
218 glTranslate ($x, $y, 0);
219 my $c = $self->{childs}->[$yi]->[$xi];
220 $c->_draw if $c;
221 glPopMatrix;
222
223 $x += $self->max_col_width ($xi);
224 }
225
226 $y += $self->max_row_height ($yi);
227 }
228}
229
230package Crossfire::Client::Widget::VBox;
231
232our @ISA = Crossfire::Client::Widget::Container::;
233
234use SDL::OpenGL;
235
236sub add {
237 my ($self, $chld) = @_;
238 push @{$self->{childs}}, $chld;
239}
240
241sub size_request {
242 my ($self) = @_;
243
244 my ($hs, $ws) = (0, 0);
245 for (@{$self->{childs} || []}) {
246 my ($w, $h) = $_->size_request;
247 $hs += $h;
248 if ($ws < $w) { $ws = $w }
249 }
250
251 return ($ws, $hs);
252}
253
254sub _draw {
255 my ($self) = @_;
256
257 my ($x, $y);
258 for (@{$self->{childs} || []}) {
259 glPushMatrix;
260 glTranslate (0, $y, 0);
261 $_->_draw;
262 glPopMatrix;
263 my ($w, $h) = $_->size_request;
264 $y += $h;
265 }
266}
267
75package Crossfire::Client::Widget::Label; 268package Crossfire::Client::Widget::Label;
76 269
77our @ISA = Crossfire::Client::Widget::; 270our @ISA = Crossfire::Client::Widget::;
78 271
79use SDL::OpenGL; 272use SDL::OpenGL;
80 273
81sub new { 274sub new {
82 my ($class, $x, $y, $z, $ttf, $text) = @_; 275 my ($class, $x, $y, $z, $ttf, $text) = @_;
83 276
84 my $self = $class->SUPER::new (x => $x, y => $y, z => $z); 277 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf);
85 278
86 $self->{texture} = new_from_ttf Crossfire::Client::Texture $ttf, $text; 279 $self->set_text ($text);
87 280
88 $self 281 $self
89} 282}
90 283
284sub set_text {
285 my ($self, $text) = @_;
286 $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text;
287}
288
289sub get_text {
290 my ($self, $text) = @_;
291 $self->{text}
292}
293
294sub size_request {
295 my ($self) = @_;
296
297 (
298 $self->{texture}{width},
299 $self->{texture}{height},
300 )
301}
302
91sub _draw { 303sub _draw {
92 my ($self) = @_; 304 my ($self) = @_;
93 305
94 my $tex = $self->{texture}; 306 my $tex = $self->{texture};
95
96 $self->{x}--;
97 307
98 glEnable GL_BLEND; 308 glEnable GL_BLEND;
99 glEnable GL_TEXTURE_2D; 309 glEnable GL_TEXTURE_2D;
100 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 310 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
101 glBindTexture GL_TEXTURE_2D, $tex->{name}; 311 glBindTexture GL_TEXTURE_2D, $tex->{name};
136 346
137use strict; 347use strict;
138 348
139our @ISA = qw/Crossfire::Client::Widget/; 349our @ISA = qw/Crossfire::Client::Widget/;
140 350
351use SDL;
141use SDL::OpenGL; 352use SDL::OpenGL;
142use SDL::OpenGL::Constants; 353use SDL::OpenGL::Constants;
143 354
144sub key_down { 355sub key_down {
145 print "MAPKEYDOWN\n"; 356 print "MAPKEYDOWN\n";
181 392
182 glDisable GL_TEXTURE_2D; 393 glDisable GL_TEXTURE_2D;
183 glDisable GL_BLEND; 394 glDisable GL_BLEND;
184} 395}
185 396
397my %DIR = (
398 SDLK_KP8, [1, "north"],
399 SDLK_KP9, [2, "northeast"],
400 SDLK_KP6, [3, "east"],
401 SDLK_KP3, [4, "southeast"],
402 SDLK_KP2, [5, "south"],
403 SDLK_KP1, [6, "southwest"],
404 SDLK_KP4, [7, "west"],
405 SDLK_KP7, [8, "northwest"],
406
407 SDLK_UP, [1, "north"],
408 SDLK_RIGHT, [3, "east"],
409 SDLK_DOWN, [5, "south"],
410 SDLK_LEFT, [7, "west"],
411);
412
413sub key_down {
414 my ($self, $ev) = @_;
415
416 my $mod = $ev->key_mod;
417 my $sym = $ev->key_sym;
418
419 if ($sym == SDLK_KP5) {
420 $::CONN->send ("command stay fire");
421 } elsif (exists $DIR{$sym}) {
422 if ($mod & KMOD_SHIFT) {
423 $self->{shft}++;
424 $::CONN->send ("command fire $DIR{$sym}[0]");
425 } elsif ($mod & KMOD_CTRL) {
426 $self->{ctrl}++;
427 $::CONN->send ("command run $DIR{$sym}[0]");
428 } else {
429 $::CONN->send ("command $DIR{$sym}[1]");
430 }
431 }
432}
433
434sub key_up {
435 my ($self, $ev) = @_;
436
437 my $mod = $ev->key_mod;
438 my $sym = $ev->key_sym;
439
440 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
441 $::CONN->send ("command fire_stop");
442 }
443 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
444 $::CONN->send ("command run_stop");
445 }
446}
447
1861; 4481;
187 449

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines