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.12 by root, Fri Apr 7 20:55:32 2006 UTC vs.
Revision 1.24 by root, Sat Apr 8 20:25:26 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;
9 12
10# class methods for events 13# class methods for events
11sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 14sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
12sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 15sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS }
13sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS } 16sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS }
18 21
19 bless { @_ }, $class 22 bless { @_ }, $class
20} 23}
21 24
22sub activate { 25sub activate {
23 $ACTIVE_WIDGETS{$_[0]} = $_[0]; 26 push @ACTIVE_WIDGETS, $_[0];
27 Scalar::Util::weaken $ACTIVE_WIDGETS[-1];
24} 28}
25 29
26sub deactivate { 30sub deactivate {
27 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";
28} 50}
29 51
30sub focus_in { 52sub focus_in {
31 my ($widget) = @_; 53 my ($widget) = @_;
32 $FOCUS = $widget; 54 $FOCUS = $widget;
50 72
51sub button_up { 73sub button_up {
52 my ($widget, $sdlev) = @_; 74 my ($widget, $sdlev) = @_;
53} 75}
54 76
77sub w { $_[0]->{w} }
78sub h { $_[0]->{h} }
55sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 79sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} }
56sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 80sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} }
81sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} }
57 82
58sub draw { 83sub draw {
59 my ($self) = @_; 84 my ($self) = @_;
60 85
61 glPushMatrix; 86 glPushMatrix;
70 95
71sub bbox { 96sub bbox {
72 my ($widget) = @_; 97 my ($widget) = @_;
73} 98}
74 99
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;
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}
174
175package Crossfire::Client::Widget::Frame;
176
177our @ISA = Crossfire::Client::Widget::Container::;
178
179use SDL::OpenGL;
180
181sub size_request {
182 my ($self) = @_;
183 my $chld = $self->get
184 or return (0, 0);
185 map { $_ + 4 } $chld->size_request;
186}
187
188sub _draw {
189 my ($self) = @_;
190
191 my $chld = $self->get;
192
193 my ($w, $h) = $chld->size_request;
194
195 glColor 1, 0, 0;
196 glBegin GL_QUADS;
197 glTexCoord 0, 0; glVertex 0 , 0;
198 glTexCoord 0, 1; glVertex 0 , $h + 4;
199 glTexCoord 1, 1; glVertex $w + 4 , $h + 4;
200 glTexCoord 1, 0; glVertex $w + 4 , 0;
201 glEnd;
202
203 glPushMatrix;
204 glTranslate (2, 2, 0);
205 $chld->draw;
206 glPopMatrix;
207}
208
209package Crossfire::Client::Widget::Table;
210
211our @ISA = Crossfire::Client::Widget::Container::;
212
213use SDL::OpenGL;
214
215sub add {
216 my ($self, $x, $y, $chld) = @_;
217 $self->{childs}[$y][$x] = $chld;
218}
219
220sub max_row_height {
221 my ($self, $row) = @_;
222
223 my $hs = 0;
224 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) {
225 my $c = $self->{childs}->[$row]->[$xi];
226 if ($c) {
227 my ($w, $h) = $c->size_request;
228 if ($hs < $h) { $hs = $h }
229 }
230 }
231 return $hs;
232}
233
234sub max_col_width {
235 my ($self, $col) = @_;
236
237 my $ws = 0;
238 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) {
239 my $c = ($self->{childs}->[$yi] || [])->[$col];
240 if ($c) {
241 my ($w, $h) = $c->size_request;
242 if ($ws < $w) { $ws = $w }
243 }
244 }
245 return $ws;
246}
247
248sub size_request {
249 my ($self) = @_;
250
251 my ($hs, $ws) = (0, 0);
252
253 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
254 $hs += $self->max_row_height ($yi);
255 }
256
257 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
258 my $wm = 0;
259 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
260 $wm += $self->max_col_width ($xi)
261 }
262 if ($ws < $wm) { $ws = $wm }
263 }
264
265 return ($ws, $hs);
266}
267
268sub _draw {
269 my ($self) = @_;
270
271 my $y = 0;
272 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
273 my $x = 0;
274
275 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
276
277 glPushMatrix;
278 glTranslate ($x, $y, 0);#TODO#there must be no translate here, instead the widget must be moved
279 my $c = $self->{childs}->[$yi]->[$xi];
280 $c->draw if $c;
281 glPopMatrix;
282
283 $x += $self->max_col_width ($xi);
284 }
285
286 $y += $self->max_row_height ($yi);
287 }
288}
289
290package Crossfire::Client::Widget::VBox;
291
292our @ISA = Crossfire::Client::Widget::Container::;
293
294use SDL::OpenGL;
295
296sub add {
297 my ($self, $chld) = @_;
298 push @{$self->{childs}}, $chld;
299}
300
301sub size_request {
302 my ($self) = @_;
303
304 my ($hs, $ws) = (0, 0);
305 for (@{$self->{childs} || []}) {
306 my ($w, $h) = $_->size_request;
307 $hs += $h;
308 if ($ws < $w) { $ws = $w }
309 }
310
311 return ($ws, $hs);
312}
313
314sub _draw {
315 my ($self) = @_;
316
317 my ($x, $y);
318 for (@{$self->{childs} || []}) {
319 glPushMatrix;
320 glTranslate (0, $y, 0);# see above TODO
321 $_->draw;
322 glPopMatrix;
323 my ($w, $h) = $_->size_request;
324 $y += $h;
325 }
326}
327
75package Crossfire::Client::Widget::Label; 328package Crossfire::Client::Widget::Label;
76 329
77our @ISA = Crossfire::Client::Widget::; 330our @ISA = Crossfire::Client::Widget::;
78 331
79use SDL::OpenGL; 332use SDL::OpenGL;
80 333
81sub new { 334sub new {
82 my ($class, $x, $y, $z, $ttf, $text) = @_; 335 my ($class, $x, $y, $z, $ttf, $text) = @_;
83 336
84 my $self = $class->SUPER::new (x => $x, y => $y, z => $z); 337 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf);
85 338
86 $self->{texture} = new_from_ttf Crossfire::Client::Texture $ttf, $text; 339 $self->set_text ($text);
87 340
88 $self 341 $self
89} 342}
90 343
344sub set_text {
345 my ($self, $text) = @_;
346 $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text;
347}
348
349sub get_text {
350 my ($self, $text) = @_;
351 $self->{text}
352}
353
354sub size_request {
355 my ($self) = @_;
356
357 (
358 $self->{texture}{width},
359 $self->{texture}{height},
360 )
361}
362
91sub _draw { 363sub _draw {
92 my ($self) = @_; 364 my ($self) = @_;
93 365
94 my $tex = $self->{texture}; 366 my $tex = $self->{texture};
95
96 $self->{x}--;
97 367
98 glEnable GL_BLEND; 368 glEnable GL_BLEND;
99 glEnable GL_TEXTURE_2D; 369 glEnable GL_TEXTURE_2D;
100 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 370 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
101 glBindTexture GL_TEXTURE_2D, $tex->{name}; 371 glBindTexture GL_TEXTURE_2D, $tex->{name};
136 406
137use strict; 407use strict;
138 408
139our @ISA = qw/Crossfire::Client::Widget/; 409our @ISA = qw/Crossfire::Client::Widget/;
140 410
411use SDL;
141use SDL::OpenGL; 412use SDL::OpenGL;
142use SDL::OpenGL::Constants; 413use SDL::OpenGL::Constants;
143 414
144sub key_down { 415sub key_down {
145 print "MAPKEYDOWN\n"; 416 print "MAPKEYDOWN\n";
147 418
148sub key_up { 419sub key_up {
149} 420}
150 421
151sub _draw { 422sub _draw {
423 my ($self) = @_;
424
152 glEnable GL_TEXTURE_2D; 425 glEnable GL_TEXTURE_2D;
153 glEnable GL_BLEND; 426 glEnable GL_BLEND;
154 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 427 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
155 428
156 my $map = $::CONN->{map}; 429 my $map = $::CONN->{map};
157 430
158 for my $x (0 .. $::CONN->{mapw} - 1) { 431 for my $x (0 .. int $::WIDTH / 32) {
159 for my $y (0 .. $::CONN->{maph} - 1) { 432 for my $y (0 .. int $::HEIGHT / 32) {
160 433
161 my $cell = $map->[$x][$y] 434 my $cell = $map->[$x + $::CONN->{mapx}]
435 [$y + $::CONN->{mapy}]
162 or next; 436 or next;
163 437
164 my $darkness = $cell->[3] * (1 / 255); 438 my $darkness = $cell->[0] * (1 / 255);
439 if ($darkness < 0) {
440 glColor 0.3, 0.3, 0.3;
441 } else {
165 glColor $darkness, $darkness, $darkness; 442 glColor $darkness, $darkness, $darkness;
443 }
166 444
167 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 445 for my $num (grep $_, @$cell[1,2,3]) {
168 my $tex = $::CONN->{face}[$num]{texture} || next; 446 my $tex = $::CONN->{face}[$num]{texture} || next;
169 447
170 glBindTexture GL_TEXTURE_2D, $tex->{name}; 448 glBindTexture GL_TEXTURE_2D, $tex->{name};
171 449
450 my $w = $tex->{width};
451 my $h = $tex->{height};
452
453 my $px = ($x + 1) * 32 - $w;
454 my $py = ($y + 1) * 32 - $h;
455
172 glBegin GL_QUADS; 456 glBegin GL_QUADS;
173 glTexCoord 0, 0; glVertex $x * 32 , $y * 32; 457 glTexCoord 0, 0; glVertex $px , $py;
174 glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32; 458 glTexCoord 0, 1; glVertex $px , $py + $h;
175 glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32; 459 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
176 glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32; 460 glTexCoord 1, 0; glVertex $px + $w, $py;
177 glEnd; 461 glEnd;
178 } 462 }
179 } 463 }
180 } 464 }
181 465
182 glDisable GL_TEXTURE_2D; 466 glDisable GL_TEXTURE_2D;
183 glDisable GL_BLEND; 467 glDisable GL_BLEND;
184} 468}
185 469
470my %DIR = (
471 SDLK_KP8, [1, "north"],
472 SDLK_KP9, [2, "northeast"],
473 SDLK_KP6, [3, "east"],
474 SDLK_KP3, [4, "southeast"],
475 SDLK_KP2, [5, "south"],
476 SDLK_KP1, [6, "southwest"],
477 SDLK_KP4, [7, "west"],
478 SDLK_KP7, [8, "northwest"],
479
480 SDLK_UP, [1, "north"],
481 SDLK_RIGHT, [3, "east"],
482 SDLK_DOWN, [5, "south"],
483 SDLK_LEFT, [7, "west"],
484);
485
486sub key_down {
487 my ($self, $ev) = @_;
488
489 my $mod = $ev->key_mod;
490 my $sym = $ev->key_sym;
491
492 if ($sym == SDLK_KP5) {
493 $::CONN->send ("command stay fire");
494 } elsif (exists $DIR{$sym}) {
495 if ($mod & KMOD_SHIFT) {
496 $self->{shft}++;
497 $::CONN->send ("command fire $DIR{$sym}[0]");
498 } elsif ($mod & KMOD_CTRL) {
499 $self->{ctrl}++;
500 $::CONN->send ("command run $DIR{$sym}[0]");
501 } else {
502 $::CONN->send ("command $DIR{$sym}[1]");
503 }
504 }
505}
506
507sub key_up {
508 my ($self, $ev) = @_;
509
510 my $mod = $ev->key_mod;
511 my $sym = $ev->key_sym;
512
513 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
514 $::CONN->send ("command fire_stop");
515 }
516 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
517 $::CONN->send ("command run_stop");
518 }
519}
520
1861; 5211;
187 522

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines