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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines