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.20 by elmex, Sat Apr 8 17:21:01 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 $self->{texture} =
137 Crossfire::Client::Texture->new_from_opengl (
138 $w, $h, sub {
139 my ($txt, $w, $h) = @_;
140 $chld->_draw;
141 }
142 );
143 $self->{texture}->upload;
144}
145
146sub size_request {
147 my ($self) = @_;
148 my $chld = $self->get
149 or return (0, 0);
150 $chld->size_request
151}
152
153sub _draw {
154 my ($self) = @_;
155
156 my $tex = $self->{texture}
157 or return;
158
159 warn "DRAW TEX: $tex->{width} $tex->{height}\n";
160 glEnable GL_BLEND;
161 glEnable GL_TEXTURE_2D;
162 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
163 glBindTexture GL_TEXTURE_2D, $tex->{name};
164
165 glColor 1, 1, 1;
166
167 glBegin GL_QUADS;
168 glTexCoord 0, 0; glVertex 0 , 0;
169 glTexCoord 0, 1; glVertex 0 , $tex->{height};
170 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
171 glTexCoord 1, 0; glVertex $tex->{width}, 0;
172 glEnd;
173
174 glDisable GL_BLEND;
175 glDisable GL_TEXTURE_2D;
176}
97 177
98package Crossfire::Client::Widget::Frame; 178package Crossfire::Client::Widget::Frame;
99 179
100our @ISA = Crossfire::Client::Widget::Container::; 180our @ISA = Crossfire::Client::Widget::Container::;
101 181
144 my ($self, $row) = @_; 224 my ($self, $row) = @_;
145 225
146 my $hs = 0; 226 my $hs = 0;
147 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { 227 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) {
148 my $c = $self->{childs}->[$row]->[$xi]; 228 my $c = $self->{childs}->[$row]->[$xi];
229 if ($c) {
149 my ($w, $h) = $c->size_request if $c; 230 my ($w, $h) = $c->size_request;
150 if ($hs < $h) { $hs = $h } 231 if ($hs < $h) { $hs = $h }
232 }
151 } 233 }
152 return $hs; 234 return $hs;
153} 235}
154 236
155sub max_col_width { 237sub max_col_width {
156 my ($self, $col) = @_; 238 my ($self, $col) = @_;
157 239
158 my $ws = 0; 240 my $ws = 0;
159 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { 241 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) {
160 my $c = ($self->{childs}->[$yi] || [])->[$col]; 242 my $c = ($self->{childs}->[$yi] || [])->[$col];
243 if ($c) {
161 my ($w, $h) = $c->size_request if $c; 244 my ($w, $h) = $c->size_request;
162 if ($ws < $w) { $ws = $w } 245 if ($ws < $w) { $ws = $w }
246 }
163 } 247 }
164 return $ws; 248 return $ws;
165} 249}
166 250
167sub size_request { 251sub size_request {
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;
291 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 373 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
292 glBindTexture GL_TEXTURE_2D, $tex->{name}; 374 glBindTexture GL_TEXTURE_2D, $tex->{name};
293 375
327 409
328use strict; 410use strict;
329 411
330our @ISA = qw/Crossfire::Client::Widget/; 412our @ISA = qw/Crossfire::Client::Widget/;
331 413
414use SDL;
332use SDL::OpenGL; 415use SDL::OpenGL;
333use SDL::OpenGL::Constants; 416use SDL::OpenGL::Constants;
334 417
335sub key_down { 418sub key_down {
336 print "MAPKEYDOWN\n"; 419 print "MAPKEYDOWN\n";
358 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 441 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) {
359 my $tex = $::CONN->{face}[$num]{texture} || next; 442 my $tex = $::CONN->{face}[$num]{texture} || next;
360 443
361 glBindTexture GL_TEXTURE_2D, $tex->{name}; 444 glBindTexture GL_TEXTURE_2D, $tex->{name};
362 445
446 my $w = $tex->{width};
447 my $h = $tex->{height};
448
449 my $px = ($x + 1) * 32 - $w;
450 my $py = ($y + 1) * 32 - $h;
451
363 glBegin GL_QUADS; 452 glBegin GL_QUADS;
364 glTexCoord 0, 0; glVertex $x * 32 , $y * 32; 453 glTexCoord 0, 0; glVertex $px , $py;
365 glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32; 454 glTexCoord 0, 1; glVertex $px , $py + $h;
366 glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32; 455 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
367 glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32; 456 glTexCoord 1, 0; glVertex $px + $w, $py;
368 glEnd; 457 glEnd;
369 } 458 }
370 } 459 }
371 } 460 }
372 461
373 glDisable GL_TEXTURE_2D; 462 glDisable GL_TEXTURE_2D;
374 glDisable GL_BLEND; 463 glDisable GL_BLEND;
375} 464}
376 465
466my %DIR = (
467 SDLK_KP8, [1, "north"],
468 SDLK_KP9, [2, "northeast"],
469 SDLK_KP6, [3, "east"],
470 SDLK_KP3, [4, "southeast"],
471 SDLK_KP2, [5, "south"],
472 SDLK_KP1, [6, "southwest"],
473 SDLK_KP4, [7, "west"],
474 SDLK_KP7, [8, "northwest"],
475
476 SDLK_UP, [1, "north"],
477 SDLK_RIGHT, [3, "east"],
478 SDLK_DOWN, [5, "south"],
479 SDLK_LEFT, [7, "west"],
480);
481
482sub key_down {
483 my ($self, $ev) = @_;
484
485 my $mod = $ev->key_mod;
486 my $sym = $ev->key_sym;
487
488 if ($sym == SDLK_KP5) {
489 $::CONN->send ("command stay fire");
490 } elsif (exists $DIR{$sym}) {
491 if ($mod & KMOD_SHIFT) {
492 $self->{shft}++;
493 $::CONN->send ("command fire $DIR{$sym}[0]");
494 } elsif ($mod & KMOD_CTRL) {
495 $self->{ctrl}++;
496 $::CONN->send ("command run $DIR{$sym}[0]");
497 } else {
498 $::CONN->send ("command $DIR{$sym}[1]");
499 }
500 }
501}
502
503sub key_up {
504 my ($self, $ev) = @_;
505
506 my $mod = $ev->key_mod;
507 my $sym = $ev->key_sym;
508
509 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
510 $::CONN->send ("command fire_stop");
511 }
512 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
513 $::CONN->send ("command run_stop");
514 }
515}
516
3771; 5171;
378 518

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines