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.6 by elmex, Fri Apr 7 19:58:55 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
2use strict; 3use strict;
3 4
5use Scalar::Util;
6
7use SDL::OpenGL;
8use SDL::OpenGL::Constants;
9
4our $FOCUS; # the widget with current focus 10our $FOCUS; # the widget with current focus
5our %ACTIVE_WIDGETS; 11our @ACTIVE_WIDGETS;
6 12
7# class methods for events 13# class methods for events
8sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 14sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
9sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 15sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS }
10sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS } 16sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS }
11sub feed_sdl_button_up_event { $FOCUS->button_up ($_[0]) if $FOCUS } 17sub feed_sdl_button_up_event { $FOCUS->button_up ($_[0]) if $FOCUS }
12 18
13sub new { 19sub new {
14 my $class = shift; 20 my $class = shift;
15 my $self = { @_ }; 21
16 bless $self, $class; 22 bless { @_ }, $class
17 return $self;
18} 23}
19 24
20sub activate { 25sub activate {
21 $ACTIVE_WIDGETS{$_[0]} = $_[0]; 26 push @ACTIVE_WIDGETS, $_[0];
27 Scalar::Util::weaken $ACTIVE_WIDGETS[-1];
22} 28}
23 29
24sub deactivate { 30sub deactivate {
25 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";
26} 46}
27 47
28sub focus_in { 48sub focus_in {
29 my ($widget) = @_; 49 my ($widget) = @_;
30 $FOCUS = $widget; 50 $FOCUS = $widget;
48 68
49sub button_up { 69sub button_up {
50 my ($widget, $sdlev) = @_; 70 my ($widget, $sdlev) = @_;
51} 71}
52 72
73sub w { $_[0]->{w} }
74sub h { $_[0]->{h} }
75sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} }
76sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} }
77sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} }
78
53sub draw { 79sub draw {
80 my ($self) = @_;
81
82 glPushMatrix;
83 glTranslate $self->{x}, $self->{y}, 0;
84 $self->_draw;
85 glPopMatrix;
86}
87
88sub _draw {
54 my ($widget) = @_; 89 my ($widget) = @_;
55} 90}
56 91
57sub bbox { 92sub bbox {
58 my ($widget) = @_; 93 my ($widget) = @_;
59} 94}
60 95
61package Client::TextView; 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
268package Crossfire::Client::Widget::Label;
269
270our @ISA = Crossfire::Client::Widget::;
271
272use SDL::OpenGL;
273
274sub new {
275 my ($class, $x, $y, $z, $ttf, $text) = @_;
276
277 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf);
278
279 $self->set_text ($text);
280
281 $self
282}
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
303sub _draw {
304 my ($self) = @_;
305
306 my $tex = $self->{texture};
307
308 glEnable GL_BLEND;
309 glEnable GL_TEXTURE_2D;
310 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
311 glBindTexture GL_TEXTURE_2D, $tex->{name};
312
313 glColor 1, 1, 1;
314
315 glBegin GL_QUADS;
316 glTexCoord 0, 0; glVertex 0 , 0;
317 glTexCoord 0, 1; glVertex 0 , $tex->{height};
318 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
319 glTexCoord 1, 0; glVertex $tex->{width}, 0;
320 glEnd;
321
322 glDisable GL_BLEND;
323 glDisable GL_TEXTURE_2D;
324}
325
326package Crossfire::Client::Widget::TextView;
62 327
63use strict; 328use strict;
329
64our @ISA = qw/Client::Widget/; 330our @ISA = qw/Crossfire::Client::Widget/;
65 331
66use SDL::OpenGL; 332use SDL::OpenGL;
67use SDL::OpenGL::Constants; 333use SDL::OpenGL::Constants;
68 334
69sub add_line { 335sub add_line {
70 my ($self, $line) = @_; 336 my ($self, $line) = @_;
71 push @{$self->{lines}}, $line; 337 push @{$self->{lines}}, $line;
72} 338}
73 339
74sub draw { 340sub _draw {
75 my ($self) = @_; 341 my ($self) = @_;
76 342
77} 343}
78 344
79package Client::MapWidget; 345package Crossfire::Client::Widget::MapWidget;
80 346
81use strict; 347use strict;
348
82our @ISA = qw/Client::Widget/; 349our @ISA = qw/Crossfire::Client::Widget/;
83 350
351use SDL;
84use SDL::OpenGL; 352use SDL::OpenGL;
85use SDL::OpenGL::Constants; 353use SDL::OpenGL::Constants;
86 354
87sub key_down { 355sub key_down {
88 print "MAPKEYDOWN\n"; 356 print "MAPKEYDOWN\n";
89} 357}
90 358
91sub key_up { 359sub key_up {
92} 360}
93 361
94my $x;
95
96sub draw { 362sub _draw {
97 glEnable GL_TEXTURE_2D; 363 glEnable GL_TEXTURE_2D;
98 glEnable GL_BLEND; 364 glEnable GL_BLEND;
99 365 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
100 glPushMatrix;
101 366
102 my $map = $::CONN->{map}; 367 my $map = $::CONN->{map};
103 368
104 for my $x (0 .. $::CONN->{mapw} - 1) { 369 for my $x (0 .. $::CONN->{mapw} - 1) {
105 for my $y (0 .. $::CONN->{maph} - 1) { 370 for my $y (0 .. $::CONN->{maph} - 1) {
114 my $tex = $::CONN->{face}[$num]{texture} || next; 379 my $tex = $::CONN->{face}[$num]{texture} || next;
115 380
116 glBindTexture GL_TEXTURE_2D, $tex->{name}; 381 glBindTexture GL_TEXTURE_2D, $tex->{name};
117 382
118 glBegin GL_QUADS; 383 glBegin GL_QUADS;
119 glTexCoord 0, 0; glVertex $x, $y; 384 glTexCoord 0, 0; glVertex $x * 32 , $y * 32;
120 glTexCoord 0, 1; glVertex $x, $y + 1; 385 glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32;
121 glTexCoord 1, 1; glVertex $x + 1, $y + 1; 386 glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32;
122 glTexCoord 1, 0; glVertex $x + 1, $y; 387 glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32;
123 glEnd; 388 glEnd;
124 } 389 }
125 } 390 }
126 } 391 }
127 392
128 glPopMatrix;
129
130 glDisable GL_TEXTURE_2D; 393 glDisable GL_TEXTURE_2D;
131 glDisable GL_BLEND; 394 glDisable GL_BLEND;
132} 395}
133 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
1341; 4481;
135 449

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines