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.3 by elmex, Fri Apr 7 18:16:51 2006 UTC vs.
Revision 1.26 by elmex, Sat Apr 8 22:23:57 2006 UTC

1package 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}
29
23sub deactivate { 30sub deactivate {
24 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 needs_redraw {
45 0
46}
47
48sub size_request {
49 die "size_request is abtract";
25} 50}
26 51
27sub focus_in { 52sub focus_in {
28 my ($widget) = @_; 53 my ($widget) = @_;
29 $FOCUS = $widget; 54 $FOCUS = $widget;
30} 55}
56
31sub focus_out { 57sub focus_out {
32 my ($widget) = @_; 58 my ($widget) = @_;
33} 59}
60
34sub key_down { 61sub key_down {
35 my ($widget, $sdlev) = @_; 62 my ($widget, $sdlev) = @_;
36} 63}
64
37sub key_up { 65sub key_up {
38 my ($widget, $sdlev) = @_; 66 my ($widget, $sdlev) = @_;
39} 67}
68
40sub button_down { 69sub button_down {
41 my ($widget, $sdlev) = @_; 70 my ($widget, $sdlev) = @_;
42} 71}
72
43sub button_up { 73sub button_up {
44 my ($widget, $sdlev) = @_; 74 my ($widget, $sdlev) = @_;
45} 75}
76
77sub w { $_[0]->{w} }
78sub h { $_[0]->{h} }
79sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} }
80sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} }
81sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} }
82
46sub draw { 83sub draw {
84 my ($self) = @_;
85
86 glPushMatrix;
87 glTranslate $self->{x}, $self->{y}, 0;
88 $self->_draw;
89 glPopMatrix;
90}
91
92sub _draw {
47 my ($widget) = @_; 93 my ($widget) = @_;
48} 94}
95
49sub bbox { 96sub bbox {
50 my ($widget) = @_; 97 my ($widget) = @_;
51} 98}
52 99
53package Client::TextView; 100sub DESTROY {
101 my ($self) = @_;
102
103 $self->deactivate;
104}
105
106package Crossfire::Client::Widget::Container;
107
108our @ISA = Crossfire::Client::Widget::;
109
110use SDL::OpenGL;
111
112sub add { $_[0]->{child} = $_[1] }
113sub get { $_[0]->{child} }
114
115sub size_request { $_[0]->{child}->size_request if $_[0]->{child} }
116
117sub _draw { die "Containers can't be drawn!" }
118
119package Crossfire::Client::Widget::Window;
120
121our @ISA = Crossfire::Client::Widget::Container::;
122
123use SDL::OpenGL;
124
125sub add {
126 my ($self, $chld) = @_;
127 $self->SUPER::add ($chld);
128 $self->render_chld; #TODO: Move this to the size_request event propably?
129}
130
131sub 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
143sub size_request {
144 my ($self) = @_;
145 my $chld = $self->get
146 or return (0, 0);
147 $chld->size_request
148}
149
150sub _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}
174
175package Crossfire::Client::Widget::Frame;
176
177our @ISA = Crossfire::Client::Widget::Container::;
178
179use SDL::OpenGL;
180
181sub size_request {
182 my ($self) = @_;
183 my $chld = $self->get
184 or return (0, 0);
185 map { $_ + 4 } $chld->size_request;
186}
187
188sub _draw {
189 my ($self) = @_;
190
191 my $chld = $self->get;
192
193 $chld->move (2, 2, 0); #TODO:move to size_request
194
195 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 $chld->draw;
206}
207
208package Crossfire::Client::Widget::Table;
209
210our @ISA = Crossfire::Client::Widget::Container::;
211
212use SDL::OpenGL;
213
214sub add {
215 my ($self, $x, $y, $chld) = @_;
216 $self->{childs}[$y][$x] = $chld;
217}
218
219sub 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 if ($c) {
226 my ($w, $h) = $c->size_request;
227 if ($hs < $h) { $hs = $h }
228 }
229 }
230 return $hs;
231}
232
233sub 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 if ($c) {
240 my ($w, $h) = $c->size_request;
241 if ($ws < $w) { $ws = $w }
242 }
243 }
244 return $ws;
245}
246
247sub 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
267sub _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 if ($c) {
278 $c->move ($x, $y, 0); #TODO: Move to size_request
279 $c->draw if $c;
280 }
281
282 $x += $self->max_col_width ($xi);
283 }
284
285 $y += $self->max_row_height ($yi);
286 }
287}
288
289package Crossfire::Client::Widget::VBox;
290
291our @ISA = Crossfire::Client::Widget::Container::;
292
293use SDL::OpenGL;
294
295sub add {
296 my ($self, $chld) = @_;
297 push @{$self->{childs}}, $chld;
298}
299
300sub 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
313sub _draw {
314 my ($self) = @_;
315
316 my ($x, $y);
317 for (@{$self->{childs} || []}) {
318 $_->move (0, $y, 0); #TODO: move to size_request
319 $_->draw;
320 my ($w, $h) = $_->size_request;
321 $y += $h;
322 }
323}
324
325package Crossfire::Client::Widget::Label;
326
327our @ISA = Crossfire::Client::Widget::;
328
329use SDL::OpenGL;
330
331sub new {
332 my ($class, $x, $y, $z, $ttf, $text) = @_;
333
334 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf);
335
336 $self->set_text ($text);
337
338 $self
339}
340
341sub set_text {
342 my ($self, $text) = @_;
343 $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text;
344}
345
346sub get_text {
347 my ($self, $text) = @_;
348 $self->{text}
349}
350
351sub size_request {
352 my ($self) = @_;
353
354 (
355 $self->{texture}{width},
356 $self->{texture}{height},
357 )
358}
359
360sub _draw {
361 my ($self) = @_;
362
363 my $tex = $self->{texture};
364
365 glEnable GL_BLEND;
366 glEnable GL_TEXTURE_2D;
367 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;#DECAL;
368 glBindTexture GL_TEXTURE_2D, $tex->{name};
369
370 glColor 1, 0, 1;
371
372 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 glDisable GL_BLEND;
380 glDisable GL_TEXTURE_2D;
381}
382
383package Crossfire::Client::Widget::TextView;
384
54use strict; 385use strict;
386
55our @ISA = qw/Client::Widget/; 387our @ISA = qw/Crossfire::Client::Widget/;
56 388
57use SDL::OpenGL; 389use SDL::OpenGL;
58use SDL::OpenGL::Constants; 390use SDL::OpenGL::Constants;
59 391
60sub add_line { 392sub add_line {
61 my ($self, $line) = @_; 393 my ($self, $line) = @_;
62 push @{$self->{lines}}, $line; 394 push @{$self->{lines}}, $line;
63} 395}
64 396
65sub draw { 397sub _draw {
66 my ($self) = @_; 398 my ($self) = @_;
67 399
68} 400}
69 401
70package Client::MapWidget; 402package Crossfire::Client::Widget::MapWidget;
403
71use strict; 404use strict;
72our @ISA = qw/Client::Widget/;
73 405
406use List::Util qw(min max);
407
408use SDL;
74use SDL::OpenGL; 409use SDL::OpenGL;
75use SDL::OpenGL::Constants; 410use SDL::OpenGL::Constants;
411
412our @ISA = Crossfire::Client::Widget::;
76 413
77sub key_down { 414sub key_down {
78 print "MAPKEYDOWN\n"; 415 print "MAPKEYDOWN\n";
79} 416}
80 417
81sub key_up { 418sub key_up {
82} 419}
83 420
84sub draw { 421sub _draw {
422 my ($self) = @_;
423
424 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 $xofs = ($sw - $::CONN->{mapw}) * 0.5;
436 } else {
437 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
438 }
439
440 if ($::CONN->{maph} > $sh) {
441 $yofs = ($sh - $::CONN->{maph}) * 0.5;
442 } else {
443 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
444 }
445
85 glEnable GL_TEXTURE_2D; 446 glEnable GL_TEXTURE_2D;
86 glEnable GL_BLEND; 447 glEnable GL_BLEND;
448 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
87 449
88 my $map = $::CONN->{map}; 450 warn "<$mx,$my> <$xofs,$yofs>\n";#d#
89 451
90 for my $x (0 .. $::CONN->{mapw} - 1) { 452 for my $x (0 .. $sw - 1) {
91 for my $y (0 .. $::CONN->{maph} - 1) { 453 for my $y (0 .. $sh - 1) {
92 454
93 my $cell = $map->[$x][$y] 455 my $cell = $map->[$x + $xofs][$y + $yofs]
94 or next; 456 or next;
95 457
96 my $darkness = $cell->[3] * (1 / 255); 458 my $darkness = $cell->[0] * (1 / 255);
459 if ($darkness < 0) {
460 glColor 0.3, 0.3, 0.3;
461 } else {
97 glColor $darkness, $darkness, $darkness; 462 glColor $darkness, $darkness, $darkness;
463 }
98 464
99 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 465 for my $num (grep $_, @$cell[1,2,3]) {
100 my $tex = $::CONN->{face}[$num]{texture} || 0; 466 my $tex = $::CONN->{face}[$num]{texture} || next;
101 467
102 glBindTexture GL_TEXTURE_2D, $tex; 468 glBindTexture GL_TEXTURE_2D, $tex->{name};
469
470 my $w = $tex->{width};
471 my $h = $tex->{height};
472
473 my $px = ($x + 1) * 32 - $w;
474 my $py = ($y + 1) * 32 - $h;
103 475
104 glBegin GL_QUADS; 476 glBegin GL_QUADS;
105 glTexCoord 0, 0; glVertex $x, $y; 477 glTexCoord 0, 0; glVertex $px , $py;
106 glTexCoord 0, 1; glVertex $x, $y + 1; 478 glTexCoord 0, 1; glVertex $px , $py + $h;
107 glTexCoord 1, 1; glVertex $x + 1, $y + 1; 479 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
108 glTexCoord 1, 0; glVertex $x + 1, $y; 480 glTexCoord 1, 0; glVertex $px + $w, $py;
109 glEnd; 481 glEnd;
110 } 482 }
111 } 483 }
112 } 484 }
113 485
114 glDisable GL_TEXTURE_2D; 486 glDisable GL_TEXTURE_2D;
115 glDisable GL_BLEND; 487 glDisable GL_BLEND;
116} 488}
117 489
490my %DIR = (
491 SDLK_KP8, [1, "north"],
492 SDLK_KP9, [2, "northeast"],
493 SDLK_KP6, [3, "east"],
494 SDLK_KP3, [4, "southeast"],
495 SDLK_KP2, [5, "south"],
496 SDLK_KP1, [6, "southwest"],
497 SDLK_KP4, [7, "west"],
498 SDLK_KP7, [8, "northwest"],
499
500 SDLK_UP, [1, "north"],
501 SDLK_RIGHT, [3, "east"],
502 SDLK_DOWN, [5, "south"],
503 SDLK_LEFT, [7, "west"],
504);
505
506sub key_down {
507 my ($self, $ev) = @_;
508
509 my $mod = $ev->key_mod;
510 my $sym = $ev->key_sym;
511
512 if ($sym == SDLK_KP5) {
513 $::CONN->send ("command stay fire");
514 } elsif (exists $DIR{$sym}) {
515 if ($mod & KMOD_SHIFT) {
516 $self->{shft}++;
517 $::CONN->send ("command fire $DIR{$sym}[0]");
518 } elsif ($mod & KMOD_CTRL) {
519 $self->{ctrl}++;
520 $::CONN->send ("command run $DIR{$sym}[0]");
521 } else {
522 $::CONN->send ("command $DIR{$sym}[1]");
523 }
524 }
525}
526
527sub key_up {
528 my ($self, $ev) = @_;
529
530 my $mod = $ev->key_mod;
531 my $sym = $ev->key_sym;
532
533 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
534 $::CONN->send ("command fire_stop");
535 }
536 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
537 $::CONN->send ("command run_stop");
538 }
539}
540
1181; 5411;
542

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines