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.22 by elmex, Sat Apr 8 18:18:09 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;
129}
130
131sub render_chld {
132 my ($self) = @_;
133 my $chld = $self->get;
134 my ($w, $h) = $self->size_request;
135
136 $w = $h = 256;
137
138 $self->{texture} =
139 Crossfire::Client::Texture->new_from_opengl (
140 $w, $h, sub {
141 glPushMatrix;
142 glLoadIdentity;
143 $chld->_draw;
144 glPopMatrix;
145 }
146 );
147 $self->{texture}->upload;
148}
149
150sub size_request {
151 my ($self) = @_;
152 my $chld = $self->get
153 or return (0, 0);
154 $chld->size_request
155}
156
157sub _draw {
158 my ($self) = @_;
159
160 my ($w, $h) = $self->size_request;
161 my $tex = $self->{texture}
162 or return;
163
164 glEnable GL_BLEND;
165 glEnable GL_TEXTURE_2D;
166 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
167 glBindTexture GL_TEXTURE_2D, $tex->{name};
168
169 glColor 1, 1, 1;
170
171 glBegin GL_QUADS;
172 glTexCoord 0, 0; glVertex 0 , 0;
173 glTexCoord 0, 1; glVertex 0 , $h;
174 glTexCoord 1, 1; glVertex $w , $h;
175 glTexCoord 1, 0; glVertex $w , 0;
176 glEnd;
177
178 glDisable GL_BLEND;
179 glDisable GL_TEXTURE_2D;
180}
97 181
98package Crossfire::Client::Widget::Frame; 182package Crossfire::Client::Widget::Frame;
99 183
100our @ISA = Crossfire::Client::Widget::Container::; 184our @ISA = Crossfire::Client::Widget::Container::;
101 185
286sub _draw { 370sub _draw {
287 my ($self) = @_; 371 my ($self) = @_;
288 372
289 my $tex = $self->{texture}; 373 my $tex = $self->{texture};
290 374
291 $self->{x}--;
292
293 glEnable GL_BLEND; 375 glEnable GL_BLEND;
294 glEnable GL_TEXTURE_2D; 376 glEnable GL_TEXTURE_2D;
295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 377 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
296 glBindTexture GL_TEXTURE_2D, $tex->{name}; 378 glBindTexture GL_TEXTURE_2D, $tex->{name};
297 379
343 425
344sub key_up { 426sub key_up {
345} 427}
346 428
347sub _draw { 429sub _draw {
430 my ($self) = @_;
431
348 glEnable GL_TEXTURE_2D; 432 glEnable GL_TEXTURE_2D;
349 glEnable GL_BLEND; 433 glEnable GL_BLEND;
350 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 434 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
351 435
352 my $map = $::CONN->{map}; 436 my $map = $::CONN->{map};
353 437
354 for my $x (0 .. $::CONN->{mapw} - 1) { 438 for my $x (0 .. int $::WIDTH / 32) {
355 for my $y (0 .. $::CONN->{maph} - 1) { 439 for my $y (0 .. int $::HEIGHT / 32) {
356 440
357 my $cell = $map->[$x][$y] 441 my $cell = $map->[$x + $::CONN->{mapx}]
442 [$y + $::CONN->{mapy}]
358 or next; 443 or next;
359 444
360 my $darkness = $cell->[3] * (1 / 255); 445 my $darkness = $cell->[0] * (1 / 255);
446 if ($darkness < 0) {
447 $darkness = 0.5;
448 }
361 glColor $darkness, $darkness, $darkness; 449 glColor $darkness, $darkness, $darkness;
362 450
363 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 451 for my $num (grep $_, @$cell[1,2,3]) {
364 my $tex = $::CONN->{face}[$num]{texture} || next; 452 my $tex = $::CONN->{face}[$num]{texture} || next;
365 453
366 glBindTexture GL_TEXTURE_2D, $tex->{name}; 454 glBindTexture GL_TEXTURE_2D, $tex->{name};
367 455
456 my $w = $tex->{width};
457 my $h = $tex->{height};
458
459 my $px = ($x + 1) * 32 - $w;
460 my $py = ($y + 1) * 32 - $h;
461
368 glBegin GL_QUADS; 462 glBegin GL_QUADS;
369 glTexCoord 0, 0; glVertex $x * 32 , $y * 32; 463 glTexCoord 0, 0; glVertex $px , $py;
370 glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32; 464 glTexCoord 0, 1; glVertex $px , $py + $h;
371 glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32; 465 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
372 glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32; 466 glTexCoord 1, 0; glVertex $px + $w, $py;
373 glEnd; 467 glEnd;
374 } 468 }
375 } 469 }
376 } 470 }
377 471
379 glDisable GL_BLEND; 473 glDisable GL_BLEND;
380} 474}
381 475
382my %DIR = ( 476my %DIR = (
383 SDLK_KP8, [1, "north"], 477 SDLK_KP8, [1, "north"],
384 SDLK_KP9, [2, "northest"], 478 SDLK_KP9, [2, "northeast"],
385 SDLK_KP6, [3, "east"], 479 SDLK_KP6, [3, "east"],
386 SDLK_KP3, [4, "southeast"], 480 SDLK_KP3, [4, "southeast"],
387 SDLK_KP2, [5, "south"], 481 SDLK_KP2, [5, "south"],
388 SDLK_KP1, [6, "southwest"], 482 SDLK_KP1, [6, "southwest"],
389 SDLK_KP4, [7, "west"], 483 SDLK_KP4, [7, "west"],
390 SDLK_KP7, [8, "northwest"], 484 SDLK_KP7, [8, "northwest"],
485
486 SDLK_UP, [1, "north"],
487 SDLK_RIGHT, [3, "east"],
488 SDLK_DOWN, [5, "south"],
489 SDLK_LEFT, [7, "west"],
391); 490);
392 491
393sub key_down { 492sub key_down {
394 my ($self, $ev) = @_; 493 my ($self, $ev) = @_;
395 494
398 497
399 if ($sym == SDLK_KP5) { 498 if ($sym == SDLK_KP5) {
400 $::CONN->send ("command stay fire"); 499 $::CONN->send ("command stay fire");
401 } elsif (exists $DIR{$sym}) { 500 } elsif (exists $DIR{$sym}) {
402 if ($mod & KMOD_SHIFT) { 501 if ($mod & KMOD_SHIFT) {
502 $self->{shft}++;
403 $::CONN->send ("command fire $DIR{$sym}[0]"); 503 $::CONN->send ("command fire $DIR{$sym}[0]");
404 } elsif ($mod & KMOD_CTRL) { 504 } elsif ($mod & KMOD_CTRL) {
505 $self->{ctrl}++;
405 $::CONN->send ("command run $DIR{$sym}[0]"); 506 $::CONN->send ("command run $DIR{$sym}[0]");
406 } else { 507 } else {
508 $::CONN->send ("command $DIR{$sym}[1]");
407 } 509 }
408 } 510 }
409} 511}
410 512
411sub key_up { 513sub key_up {
412 my ($self, $ev) = @_; 514 my ($self, $ev) = @_;
413 515
414 my $mod = $ev->key_mod; 516 my $mod = $ev->key_mod;
415 my $sym = $ev->key_sym; 517 my $sym = $ev->key_sym;
416 518
417 if (exists $DIR{$sym}) { 519 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
418 if ($mod & KMOD_SHIFT) {
419 $::CONN->send ("command fire_stop"); 520 $::CONN->send ("command fire_stop");
420 } else { 521 }
522 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
421 $::CONN->send ("command run_stop"); 523 $::CONN->send ("command run_stop");
422 }
423 } 524 }
424} 525}
425 526
4261; 5271;
427 528

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines