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.17 by elmex, Sat Apr 8 13:34:19 2006 UTC vs.
Revision 1.28 by root, Sun Apr 9 00:06:10 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;
92sub get { $_[0]->{child} } 113sub get { $_[0]->{child} }
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!" }
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}
97 174
98package Crossfire::Client::Widget::Frame; 175package Crossfire::Client::Widget::Frame;
99 176
100our @ISA = Crossfire::Client::Widget::Container::; 177our @ISA = Crossfire::Client::Widget::Container::;
101 178
110 187
111sub _draw { 188sub _draw {
112 my ($self) = @_; 189 my ($self) = @_;
113 190
114 my $chld = $self->get; 191 my $chld = $self->get;
192
193 $chld->move (2, 2, 0); #TODO:move to size_request
115 194
116 my ($w, $h) = $chld->size_request; 195 my ($w, $h) = $chld->size_request;
117 196
118 glColor 1, 0, 0; 197 glColor 1, 0, 0;
119 glBegin GL_QUADS; 198 glBegin GL_QUADS;
121 glTexCoord 0, 1; glVertex 0 , $h + 4; 200 glTexCoord 0, 1; glVertex 0 , $h + 4;
122 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 201 glTexCoord 1, 1; glVertex $w + 4 , $h + 4;
123 glTexCoord 1, 0; glVertex $w + 4 , 0; 202 glTexCoord 1, 0; glVertex $w + 4 , 0;
124 glEnd; 203 glEnd;
125 204
126 glPushMatrix;
127 glTranslate (2, 2, 0);
128 $chld->_draw; 205 $chld->draw;
129 glPopMatrix;
130} 206}
131 207
132package Crossfire::Client::Widget::Table; 208package Crossfire::Client::Widget::Table;
133 209
134our @ISA = Crossfire::Client::Widget::Container::; 210our @ISA = Crossfire::Client::Widget::Container::;
195 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 271 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
196 my $x = 0; 272 my $x = 0;
197 273
198 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 274 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
199 275
200 glPushMatrix;
201 glTranslate ($x, $y, 0);
202 my $c = $self->{childs}->[$yi]->[$xi]; 276 my $c = $self->{childs}->[$yi]->[$xi];
277 if ($c) {
278 $c->move ($x, $y, 0); #TODO: Move to size_request
203 $c->_draw if $c; 279 $c->draw if $c;
204 glPopMatrix; 280 }
205 281
206 $x += $self->max_col_width ($xi); 282 $x += $self->max_col_width ($xi);
207 } 283 }
208 284
209 $y += $self->max_row_height ($yi); 285 $y += $self->max_row_height ($yi);
237sub _draw { 313sub _draw {
238 my ($self) = @_; 314 my ($self) = @_;
239 315
240 my ($x, $y); 316 my ($x, $y);
241 for (@{$self->{childs} || []}) { 317 for (@{$self->{childs} || []}) {
242 glPushMatrix; 318 $_->move (0, $y, 0); #TODO: move to size_request
243 glTranslate (0, $y, 0);
244 $_->_draw; 319 $_->draw;
245 glPopMatrix;
246 my ($w, $h) = $_->size_request; 320 my ($w, $h) = $_->size_request;
247 $y += $h; 321 $y += $h;
248 } 322 }
249} 323}
250 324
253our @ISA = Crossfire::Client::Widget::; 327our @ISA = Crossfire::Client::Widget::;
254 328
255use SDL::OpenGL; 329use SDL::OpenGL;
256 330
257sub new { 331sub new {
258 my ($class, $x, $y, $z, $ttf, $text) = @_; 332 my ($class, $x, $y, $z, $height, $text) = @_;
259 333
260 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf); 334 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
261 335
262 $self->set_text ($text); 336 $self->set_text ($text);
263 337
264 $self 338 $self
265} 339}
266 340
267sub set_text { 341sub set_text {
268 my ($self, $text) = @_; 342 my ($self, $text) = @_;
343
344 $self->{text} = $text;
345
269 $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text; 346 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height};
270} 347}
271 348
272sub get_text { 349sub get_text {
273 my ($self, $text) = @_; 350 my ($self, $text) = @_;
351
274 $self->{text} 352 $self->{text}
275} 353}
276 354
277sub size_request { 355sub size_request {
278 my ($self) = @_; 356 my ($self) = @_;
286sub _draw { 364sub _draw {
287 my ($self) = @_; 365 my ($self) = @_;
288 366
289 my $tex = $self->{texture}; 367 my $tex = $self->{texture};
290 368
291 $self->{x}--;
292
293 glEnable GL_BLEND; 369 glEnable GL_BLEND;
294 glEnable GL_TEXTURE_2D; 370 glEnable GL_TEXTURE_2D;
295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 371 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
296 glBindTexture GL_TEXTURE_2D, $tex->{name}; 372 glBindTexture GL_TEXTURE_2D, $tex->{name};
297 373
298 glColor 1, 1, 1; 374 glColor 1, 0, 1;
299 375
300 glBegin GL_QUADS; 376 glBegin GL_QUADS;
301 glTexCoord 0, 0; glVertex 0 , 0; 377 glTexCoord 0, 0; glVertex 0 , 0;
302 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 378 glTexCoord 0, 1; glVertex 0 , $tex->{height};
303 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 379 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
329 405
330package Crossfire::Client::Widget::MapWidget; 406package Crossfire::Client::Widget::MapWidget;
331 407
332use strict; 408use strict;
333 409
334our @ISA = qw/Crossfire::Client::Widget/; 410use List::Util qw(min max);
335 411
336use SDL; 412use SDL;
337use SDL::OpenGL; 413use SDL::OpenGL;
338use SDL::OpenGL::Constants; 414use SDL::OpenGL::Constants;
415
416our @ISA = Crossfire::Client::Widget::;
339 417
340sub key_down { 418sub key_down {
341 print "MAPKEYDOWN\n"; 419 print "MAPKEYDOWN\n";
342} 420}
343 421
344sub key_up { 422sub key_up {
345} 423}
346 424
347sub _draw { 425sub _draw {
426 my ($self) = @_;
427
428 my $mx = $::CONN->{mapx};
429 my $my = $::CONN->{mapy};
430
431 my $map = $::CONN->{map};
432
433 my ($xofs, $yofs);
434
435 my $sw = 1 + int $::WIDTH / 32;
436 my $sh = 1 + int $::HEIGHT / 32;
437
438 if ($::CONN->{mapw} > $sw) {
439 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
440 } else {
441 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
442 }
443
444 if ($::CONN->{maph} > $sh) {
445 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
446 } else {
447 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
448 }
449
348 glEnable GL_TEXTURE_2D; 450 glEnable GL_TEXTURE_2D;
349 glEnable GL_BLEND; 451 glEnable GL_BLEND;
350 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 452 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
351 453
352 my $map = $::CONN->{map}; 454 for my $x (0 .. $sw - 1) {
353
354 for my $x (0 .. $::CONN->{mapw} - 1) {
355 for my $y (0 .. $::CONN->{maph} - 1) { 455 for my $y (0 .. $sh - 1) {
356 456
357 my $cell = $map->[$x][$y] 457 my $cell = $map->[$x + $xofs][$y + $yofs]
358 or next; 458 or next;
359 459
360 my $darkness = $cell->[3] * (1 / 255); 460 my $darkness = $cell->[0] * (1 / 255);
461 if ($darkness < 0) {
462 glColor 0.3, 0.3, 0.3;
463 } else {
361 glColor $darkness, $darkness, $darkness; 464 glColor $darkness, $darkness, $darkness;
465 }
362 466
363 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 467 for my $num (grep $_, @$cell[1,2,3]) {
364 my $tex = $::CONN->{face}[$num]{texture} || next; 468 my $tex = $::CONN->{face}[$num]{texture} || next;
365 469
366 glBindTexture GL_TEXTURE_2D, $tex->{name}; 470 glBindTexture GL_TEXTURE_2D, $tex->{name};
367 471
472 my $w = $tex->{width};
473 my $h = $tex->{height};
474
475 my $px = ($x + 1) * 32 - $w;
476 my $py = ($y + 1) * 32 - $h;
477
368 glBegin GL_QUADS; 478 glBegin GL_QUADS;
369 glTexCoord 0, 0; glVertex $x * 32 , $y * 32; 479 glTexCoord 0, 0; glVertex $px , $py;
370 glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32; 480 glTexCoord 0, 1; glVertex $px , $py + $h;
371 glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32; 481 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
372 glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32; 482 glTexCoord 1, 0; glVertex $px + $w, $py;
373 glEnd; 483 glEnd;
374 } 484 }
375 } 485 }
376 } 486 }
377 487
379 glDisable GL_BLEND; 489 glDisable GL_BLEND;
380} 490}
381 491
382my %DIR = ( 492my %DIR = (
383 SDLK_KP8, [1, "north"], 493 SDLK_KP8, [1, "north"],
384 SDLK_KP9, [2, "northest"], 494 SDLK_KP9, [2, "northeast"],
385 SDLK_KP6, [3, "east"], 495 SDLK_KP6, [3, "east"],
386 SDLK_KP3, [4, "southeast"], 496 SDLK_KP3, [4, "southeast"],
387 SDLK_KP2, [5, "south"], 497 SDLK_KP2, [5, "south"],
388 SDLK_KP1, [6, "southwest"], 498 SDLK_KP1, [6, "southwest"],
389 SDLK_KP4, [7, "west"], 499 SDLK_KP4, [7, "west"],
390 SDLK_KP7, [8, "northwest"], 500 SDLK_KP7, [8, "northwest"],
501
502 SDLK_UP, [1, "north"],
503 SDLK_RIGHT, [3, "east"],
504 SDLK_DOWN, [5, "south"],
505 SDLK_LEFT, [7, "west"],
391); 506);
392 507
393sub key_down { 508sub key_down {
394 my ($self, $ev) = @_; 509 my ($self, $ev) = @_;
395 510
398 513
399 if ($sym == SDLK_KP5) { 514 if ($sym == SDLK_KP5) {
400 $::CONN->send ("command stay fire"); 515 $::CONN->send ("command stay fire");
401 } elsif (exists $DIR{$sym}) { 516 } elsif (exists $DIR{$sym}) {
402 if ($mod & KMOD_SHIFT) { 517 if ($mod & KMOD_SHIFT) {
518 $self->{shft}++;
403 $::CONN->send ("command fire $DIR{$sym}[0]"); 519 $::CONN->send ("command fire $DIR{$sym}[0]");
404 } elsif ($mod & KMOD_CTRL) { 520 } elsif ($mod & KMOD_CTRL) {
521 $self->{ctrl}++;
405 $::CONN->send ("command run $DIR{$sym}[0]"); 522 $::CONN->send ("command run $DIR{$sym}[0]");
406 } else { 523 } else {
524 $::CONN->send ("command $DIR{$sym}[1]");
407 } 525 }
408 } 526 }
409} 527}
410 528
411sub key_up { 529sub key_up {
412 my ($self, $ev) = @_; 530 my ($self, $ev) = @_;
413 531
414 my $mod = $ev->key_mod; 532 my $mod = $ev->key_mod;
415 my $sym = $ev->key_sym; 533 my $sym = $ev->key_sym;
416 534
417 if (exists $DIR{$sym}) { 535 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
418 if ($mod & KMOD_SHIFT) {
419 $::CONN->send ("command fire_stop"); 536 $::CONN->send ("command fire_stop");
420 } else { 537 }
538 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
421 $::CONN->send ("command run_stop"); 539 $::CONN->send ("command run_stop");
422 }
423 } 540 }
424} 541}
425 542
4261; 5431;
427 544

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines