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.15 by elmex, Fri Apr 7 23:05:21 2006 UTC vs.
Revision 1.30 by root, Sun Apr 9 00:18:45 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;
19 bless { @_ }, $class 22 bless { @_ }, $class
20} 23}
21 24
22sub activate { 25sub activate {
23 push @ACTIVE_WIDGETS, $_[0]; 26 push @ACTIVE_WIDGETS, $_[0];
27 Scalar::Util::weaken $ACTIVE_WIDGETS[-1];
24} 28}
25 29
26sub deactivate { 30sub deactivate {
27 @ACTIVE_WIDGETS = 31 @ACTIVE_WIDGETS =
28 sort { $a->{z} <=> $b->{z} } 32 sort { $a->{z} <=> $b->{z} }
29 grep { $_ != $_[0] } 33 grep { $_ && $_ != $_[0] }
30 @ACTIVE_WIDGETS; 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
31} 46}
32 47
33sub size_request { 48sub size_request {
34 die "size_request is abtract"; 49 die "size_request is abtract";
35} 50}
80 95
81sub bbox { 96sub bbox {
82 my ($widget) = @_; 97 my ($widget) = @_;
83} 98}
84 99
100sub DESTROY {
101 my ($self) = @_;
102
103 $self->deactivate;
104}
105
85package Crossfire::Client::Widget::Container; 106package Crossfire::Client::Widget::Container;
86 107
87our @ISA = Crossfire::Client::Widget::; 108our @ISA = Crossfire::Client::Widget::;
88 109
89use SDL::OpenGL; 110use SDL::OpenGL;
93 114
94sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } 115sub size_request { $_[0]->{child}->size_request if $_[0]->{child} }
95 116
96sub _draw { die "Containers can't be drawn!" } 117sub _draw { die "Containers can't be drawn!" }
97 118
98package Crossfire::Client::Widget::Frame; 119package Crossfire::Client::Widget::Window;
99 120
100our @ISA = Crossfire::Client::Widget::Container::; 121our @ISA = Crossfire::Client::Widget::Container::;
101 122
102use SDL::OpenGL; 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}
103 142
104sub size_request { 143sub size_request {
105 my ($self) = @_; 144 my ($self) = @_;
106 my $chld = $self->get 145 my $chld = $self->get
107 or return (0, 0); 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
108 map { $_ + 4 } $chld->size_request; 189 map { $_ + 4 } $chld->size_request;
109} 190}
110 191
111sub _draw { 192sub _draw {
112 my ($self) = @_; 193 my ($self) = @_;
113 194
114 my $chld = $self->get; 195 my $chld = $self->get;
115 196
116 my ($w, $h) = $chld->size_request; 197 my ($w, $h) = $chld->size_request;
117 198
118 glColor 1, 0, 0;
119 glBegin GL_QUADS; 199 glBegin GL_QUADS;
200 glColor 0, 0, 0;
120 glTexCoord 0, 0; glVertex 0 , 0; 201 glTexCoord 0, 0; glVertex 0 , 0;
121 glTexCoord 0, 1; glVertex 0 , $h + 4; 202 glTexCoord 0, 1; glVertex 0 , $h + 4;
122 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 203 glTexCoord 1, 1; glVertex $w + 4 , $h + 4;
123 glTexCoord 1, 0; glVertex $w + 4 , 0; 204 glTexCoord 1, 0; glVertex $w + 4 , 0;
124 glEnd; 205 glEnd;
125 206
126 glPushMatrix;
127 glTranslate (2, 2, 0);
128 $chld->_draw; 207 $chld->draw;
129 glPopMatrix;
130} 208}
131 209
132package Crossfire::Client::Widget::Table; 210package Crossfire::Client::Widget::Table;
133 211
134our @ISA = Crossfire::Client::Widget::Container::; 212our @ISA = Crossfire::Client::Widget::Container::;
144 my ($self, $row) = @_; 222 my ($self, $row) = @_;
145 223
146 my $hs = 0; 224 my $hs = 0;
147 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { 225 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) {
148 my $c = $self->{childs}->[$row]->[$xi]; 226 my $c = $self->{childs}->[$row]->[$xi];
227 if ($c) {
149 my ($w, $h) = $c->size_request if $c; 228 my ($w, $h) = $c->size_request;
150 if ($hs < $h) { $hs = $h } 229 if ($hs < $h) { $hs = $h }
230 }
151 } 231 }
152 return $hs; 232 return $hs;
153} 233}
154 234
155sub max_col_width { 235sub max_col_width {
156 my ($self, $col) = @_; 236 my ($self, $col) = @_;
157 237
158 my $ws = 0; 238 my $ws = 0;
159 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { 239 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) {
160 my $c = ($self->{childs}->[$yi] || [])->[$col]; 240 my $c = ($self->{childs}->[$yi] || [])->[$col];
241 if ($c) {
161 my ($w, $h) = $c->size_request if $c; 242 my ($w, $h) = $c->size_request;
162 if ($ws < $w) { $ws = $w } 243 if ($ws < $w) { $ws = $w }
244 }
163 } 245 }
164 return $ws; 246 return $ws;
165} 247}
166 248
167sub size_request { 249sub size_request {
191 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 273 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
192 my $x = 0; 274 my $x = 0;
193 275
194 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 276 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
195 277
196 glPushMatrix;
197 glTranslate ($x, $y, 0);
198 my $c = $self->{childs}->[$yi]->[$xi]; 278 my $c = $self->{childs}->[$yi]->[$xi];
279 if ($c) {
280 $c->move ($x, $y, 0); #TODO: Move to size_request
199 $c->_draw if $c; 281 $c->draw if $c;
200 glPopMatrix; 282 }
201 283
202 $x += $self->max_col_width ($xi); 284 $x += $self->max_col_width ($xi);
203 } 285 }
204 286
205 $y += $self->max_row_height ($yi); 287 $y += $self->max_row_height ($yi);
233sub _draw { 315sub _draw {
234 my ($self) = @_; 316 my ($self) = @_;
235 317
236 my ($x, $y); 318 my ($x, $y);
237 for (@{$self->{childs} || []}) { 319 for (@{$self->{childs} || []}) {
238 glPushMatrix; 320 $_->move (0, $y, 0); #TODO: move to size_request
239 glTranslate (0, $y, 0);
240 $_->_draw; 321 $_->draw;
241 glPopMatrix;
242 my ($w, $h) = $_->size_request; 322 my ($w, $h) = $_->size_request;
243 $y += $h; 323 $y += $h;
244 } 324 }
245} 325}
246 326
249our @ISA = Crossfire::Client::Widget::; 329our @ISA = Crossfire::Client::Widget::;
250 330
251use SDL::OpenGL; 331use SDL::OpenGL;
252 332
253sub new { 333sub new {
254 my ($class, $x, $y, $z, $ttf, $text) = @_; 334 my ($class, $x, $y, $z, $height, $text) = @_;
255 335
256 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf); 336 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
257 337
258 $self->set_text ($text); 338 $self->set_text ($text);
259 339
260 $self 340 $self
261} 341}
262 342
263sub set_text { 343sub set_text {
264 my ($self, $text) = @_; 344 my ($self, $text) = @_;
345
346 $self->{text} = $text;
347
265 $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text; 348 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height};
266} 349}
267 350
268sub get_text { 351sub get_text {
269 my ($self, $text) = @_; 352 my ($self, $text) = @_;
353
270 $self->{text} 354 $self->{text}
271} 355}
272 356
273sub size_request { 357sub size_request {
274 my ($self) = @_; 358 my ($self) = @_;
282sub _draw { 366sub _draw {
283 my ($self) = @_; 367 my ($self) = @_;
284 368
285 my $tex = $self->{texture}; 369 my $tex = $self->{texture};
286 370
287 $self->{x}--;
288
289 glEnable GL_BLEND; 371 glEnable GL_BLEND;
290 glEnable GL_TEXTURE_2D; 372 glEnable GL_TEXTURE_2D;
373 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
291 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 374 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
292 glBindTexture GL_TEXTURE_2D, $tex->{name}; 375 glBindTexture GL_TEXTURE_2D, $tex->{name};
293 376
294 glColor 1, 1, 1; 377 glColor 1, 1, 1, 0.8;
295 378
296 glBegin GL_QUADS; 379 glBegin GL_QUADS;
297 glTexCoord 0, 0; glVertex 0 , 0; 380 glTexCoord 0, 0; glVertex 0 , 0;
298 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 381 glTexCoord 0, 1; glVertex 0 , $tex->{height};
299 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 382 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
325 408
326package Crossfire::Client::Widget::MapWidget; 409package Crossfire::Client::Widget::MapWidget;
327 410
328use strict; 411use strict;
329 412
330our @ISA = qw/Crossfire::Client::Widget/; 413use List::Util qw(min max);
331 414
415use SDL;
332use SDL::OpenGL; 416use SDL::OpenGL;
333use SDL::OpenGL::Constants; 417use SDL::OpenGL::Constants;
418
419our @ISA = Crossfire::Client::Widget::;
334 420
335sub key_down { 421sub key_down {
336 print "MAPKEYDOWN\n"; 422 print "MAPKEYDOWN\n";
337} 423}
338 424
339sub key_up { 425sub key_up {
340} 426}
341 427
342sub _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
343 glEnable GL_TEXTURE_2D; 453 glEnable GL_TEXTURE_2D;
344 glEnable GL_BLEND; 454 glEnable GL_BLEND;
345 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 455 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
346 456
347 my $map = $::CONN->{map}; 457 for my $x (0 .. $sw - 1) {
348
349 for my $x (0 .. $::CONN->{mapw} - 1) {
350 for my $y (0 .. $::CONN->{maph} - 1) { 458 for my $y (0 .. $sh - 1) {
351 459
352 my $cell = $map->[$x][$y] 460 my $cell = $map->[$x + $xofs][$y + $yofs]
353 or next; 461 or next;
354 462
355 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 {
356 glColor $darkness, $darkness, $darkness; 467 glColor $darkness, $darkness, $darkness;
468 }
357 469
358 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 470 for my $num (grep $_, @$cell[1,2,3]) {
359 my $tex = $::CONN->{face}[$num]{texture} || next; 471 my $tex = $::CONN->{face}[$num]{texture} || next;
360 472
361 glBindTexture GL_TEXTURE_2D, $tex->{name}; 473 glBindTexture GL_TEXTURE_2D, $tex->{name};
362 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;
480
363 glBegin GL_QUADS; 481 glBegin GL_QUADS;
364 glTexCoord 0, 0; glVertex $x * 32 , $y * 32; 482 glTexCoord 0, 0; glVertex $px , $py;
365 glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32; 483 glTexCoord 0, 1; glVertex $px , $py + $h;
366 glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32; 484 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
367 glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32; 485 glTexCoord 1, 0; glVertex $px + $w, $py;
368 glEnd; 486 glEnd;
369 } 487 }
370 } 488 }
371 } 489 }
372 490
373 glDisable GL_TEXTURE_2D; 491 glDisable GL_TEXTURE_2D;
374 glDisable GL_BLEND; 492 glDisable GL_BLEND;
375} 493}
376 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
3771; 5461;
378 547

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines