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.31 by elmex, Sun Apr 9 00:43:11 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, 0, 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}
209
210package Crossfire::Client::Widget::FancyFrame;
211
212our @ISA = Crossfire::Client::Widget::Frame::;
213
214use SDL::OpenGL;
215
216#TODO: implement themed frame
131 217
132package Crossfire::Client::Widget::Table; 218package Crossfire::Client::Widget::Table;
133 219
134our @ISA = Crossfire::Client::Widget::Container::; 220our @ISA = Crossfire::Client::Widget::Container::;
135 221
195 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 281 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
196 my $x = 0; 282 my $x = 0;
197 283
198 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 284 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
199 285
200 glPushMatrix;
201 glTranslate ($x, $y, 0);
202 my $c = $self->{childs}->[$yi]->[$xi]; 286 my $c = $self->{childs}->[$yi]->[$xi];
287 if ($c) {
288 $c->move ($x, $y, 0); #TODO: Move to size_request
203 $c->_draw if $c; 289 $c->draw if $c;
204 glPopMatrix; 290 }
205 291
206 $x += $self->max_col_width ($xi); 292 $x += $self->max_col_width ($xi);
207 } 293 }
208 294
209 $y += $self->max_row_height ($yi); 295 $y += $self->max_row_height ($yi);
237sub _draw { 323sub _draw {
238 my ($self) = @_; 324 my ($self) = @_;
239 325
240 my ($x, $y); 326 my ($x, $y);
241 for (@{$self->{childs} || []}) { 327 for (@{$self->{childs} || []}) {
242 glPushMatrix; 328 $_->move (0, $y, 0); #TODO: move to size_request
243 glTranslate (0, $y, 0);
244 $_->_draw; 329 $_->draw;
245 glPopMatrix;
246 my ($w, $h) = $_->size_request; 330 my ($w, $h) = $_->size_request;
247 $y += $h; 331 $y += $h;
248 } 332 }
249} 333}
250 334
253our @ISA = Crossfire::Client::Widget::; 337our @ISA = Crossfire::Client::Widget::;
254 338
255use SDL::OpenGL; 339use SDL::OpenGL;
256 340
257sub new { 341sub new {
258 my ($class, $x, $y, $z, $ttf, $text) = @_; 342 my ($class, $x, $y, $z, $height, $text) = @_;
259 343
260 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf); 344 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
261 345
262 $self->set_text ($text); 346 $self->set_text ($text);
263 347
264 $self 348 $self
265} 349}
266 350
267sub set_text { 351sub set_text {
268 my ($self, $text) = @_; 352 my ($self, $text) = @_;
353
354 $self->{text} = $text;
355
269 $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text; 356 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height};
270} 357}
271 358
272sub get_text { 359sub get_text {
273 my ($self, $text) = @_; 360 my ($self, $text) = @_;
361
274 $self->{text} 362 $self->{text}
275} 363}
276 364
277sub size_request { 365sub size_request {
278 my ($self) = @_; 366 my ($self) = @_;
286sub _draw { 374sub _draw {
287 my ($self) = @_; 375 my ($self) = @_;
288 376
289 my $tex = $self->{texture}; 377 my $tex = $self->{texture};
290 378
291 $self->{x}--;
292
293 glEnable GL_BLEND; 379 glEnable GL_BLEND;
294 glEnable GL_TEXTURE_2D; 380 glEnable GL_TEXTURE_2D;
381 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 382 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
296 glBindTexture GL_TEXTURE_2D, $tex->{name}; 383 glBindTexture GL_TEXTURE_2D, $tex->{name};
297 384
298 glColor 1, 1, 1; 385 glColor 1, 1, 1, 0.8;
299 386
300 glBegin GL_QUADS; 387 glBegin GL_QUADS;
301 glTexCoord 0, 0; glVertex 0 , 0; 388 glTexCoord 0, 0; glVertex 0 , 0;
302 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 389 glTexCoord 0, 1; glVertex 0 , $tex->{height};
303 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 390 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
306 393
307 glDisable GL_BLEND; 394 glDisable GL_BLEND;
308 glDisable GL_TEXTURE_2D; 395 glDisable GL_TEXTURE_2D;
309} 396}
310 397
398package Crossfire::Client::Widget::TextEntry;
399
400our @ISA = Crossfire::Client::Widget::Label::;
401
402use SDL;
403use SDL::OpenGL;
404
405sub key_down {
406 my ($self, $ev) = @_;
407
408 my $mod = $ev->key_mod;
409 my $sym = $ev->key_sym;
410
411 $ev->set_unicode (1);
412 my $uni = $ev->key_unicode;
413
414 my $text = $self->get_text;
415
416 if ($sym == SDLK_BACKSPACE) {
417 substr $text, -1, 1, '';
418
419 } elsif ($uni) {
420 $text .= chr $uni;
421 }
422 $self->set_text ($text);
423}
424
425
426# XXX: TextView isn't neccessary with pango multiline text rendering
311package Crossfire::Client::Widget::TextView; 427package Crossfire::Client::Widget::TextView;
312 428
313use strict; 429use strict;
314 430
315our @ISA = qw/Crossfire::Client::Widget/; 431our @ISA = qw/Crossfire::Client::Widget/;
316 432
317use SDL::OpenGL; 433use SDL::OpenGL;
318use SDL::OpenGL::Constants; 434use SDL::OpenGL::Constants;
435
436sub new {
437 my ($class, $text, $h) = @_;
438 my $self = $class->SUPER::new ();
439
440 $self->{txt_height} = $h;
441 @{$self->{lines}} = split /\r?\n/, $text;
442
443 for (split /\r?\n/, $text) {
444 $self->add_line ($_);
445 }
446 $self
447}
448
449#sub render_lines {
450# my ($self) = @_;
451#
452# $self->{txt_lines} = [];
453#
454# for (@{$self->{lines}}) {
455# push @{$self->{txt_lines}},
456# new_from_ttf Crossfire::Client::Texture $self->{ttf}, $_;
457# }
458#}
319 459
320sub add_line { 460sub add_line {
321 my ($self, $line) = @_; 461 my ($self, $line) = @_;
322 push @{$self->{lines}}, $line; 462 push @{$self->{lines}}, $line;
323}
324 463
464 push @{$self->{txt_lines}},
465 new_from_text Crossfire::Client::Texture $line, $self->{txt_height};
466}
467
468sub size_request {
469 my ($self) = @_;
470
471 my $w = 0;
472 my $h = 0;
473
474 for (@{$self->{txt_lines}}) {
475 if ($w < $_->{width}) { $w = $_->{width} }
476 $h += $_->{height};
477 }
478
479 return ($w, $h);
480}
481
325sub _draw { 482sub draw_line {
326 my ($self) = @_; 483 my ($self, $tex, $y) = @_;
327 484
485 glBindTexture GL_TEXTURE_2D, $tex->{name};
486
487 glColor 1, 0, 1;
488
489 glBegin GL_QUADS;
490 glTexCoord 0, 0; glVertex 0 , $y;
491 glTexCoord 0, 1; glVertex 0 , $y + $tex->{height};
492 glTexCoord 1, 1; glVertex $tex->{width}, $y + $tex->{height};
493 glTexCoord 1, 0; glVertex $tex->{width}, $y;
494 glEnd;
495}
496
497sub _draw {
498 my ($self) = @_;
499
500 glEnable GL_BLEND;
501 glEnable GL_TEXTURE_2D;
502 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;#DECAL;
503
504 my $l = 0;
505 for (@{$self->{txt_lines}}) {
506 $self->draw_line ($_, $l);
507 $l += $_->{height};
508 }
509
510 glDisable GL_BLEND;
511 glDisable GL_TEXTURE_2D;
328} 512}
329 513
330package Crossfire::Client::Widget::MapWidget; 514package Crossfire::Client::Widget::MapWidget;
331 515
332use strict; 516use strict;
333 517
334our @ISA = qw/Crossfire::Client::Widget/; 518use List::Util qw(min max);
335 519
336use SDL; 520use SDL;
337use SDL::OpenGL; 521use SDL::OpenGL;
338use SDL::OpenGL::Constants; 522use SDL::OpenGL::Constants;
523
524our @ISA = Crossfire::Client::Widget::;
339 525
340sub key_down { 526sub key_down {
341 print "MAPKEYDOWN\n"; 527 print "MAPKEYDOWN\n";
342} 528}
343 529
344sub key_up { 530sub key_up {
345} 531}
346 532
347sub _draw { 533sub _draw {
534 my ($self) = @_;
535
536 my $mx = $::CONN->{mapx};
537 my $my = $::CONN->{mapy};
538
539 my $map = $::CONN->{map};
540
541 my ($xofs, $yofs);
542
543 my $sw = 1 + int $::WIDTH / 32;
544 my $sh = 1 + int $::HEIGHT / 32;
545
546 if ($::CONN->{mapw} > $sw) {
547 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
548 } else {
549 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
550 }
551
552 if ($::CONN->{maph} > $sh) {
553 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
554 } else {
555 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
556 }
557
348 glEnable GL_TEXTURE_2D; 558 glEnable GL_TEXTURE_2D;
349 glEnable GL_BLEND; 559 glEnable GL_BLEND;
350 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 560 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
351 561
352 my $map = $::CONN->{map}; 562 for my $x (0 .. $sw - 1) {
353
354 for my $x (0 .. $::CONN->{mapw} - 1) {
355 for my $y (0 .. $::CONN->{maph} - 1) { 563 for my $y (0 .. $sh - 1) {
356 564
357 my $cell = $map->[$x][$y] 565 my $cell = $map->[$x + $xofs][$y + $yofs]
358 or next; 566 or next;
359 567
360 my $darkness = $cell->[3] * (1 / 255); 568 my $darkness = $cell->[0] * (1 / 255);
569 if ($darkness < 0) {
570 glColor 0.3, 0.3, 0.3;
571 } else {
361 glColor $darkness, $darkness, $darkness; 572 glColor $darkness, $darkness, $darkness;
573 }
362 574
363 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 575 for my $num (grep $_, @$cell[1,2,3]) {
364 my $tex = $::CONN->{face}[$num]{texture} || next; 576 my $tex = $::CONN->{face}[$num]{texture} || next;
365 577
366 glBindTexture GL_TEXTURE_2D, $tex->{name}; 578 glBindTexture GL_TEXTURE_2D, $tex->{name};
367 579
580 my $w = $tex->{width};
581 my $h = $tex->{height};
582
583 my $px = ($x + 1) * 32 - $w;
584 my $py = ($y + 1) * 32 - $h;
585
368 glBegin GL_QUADS; 586 glBegin GL_QUADS;
369 glTexCoord 0, 0; glVertex $x * 32 , $y * 32; 587 glTexCoord 0, 0; glVertex $px , $py;
370 glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32; 588 glTexCoord 0, 1; glVertex $px , $py + $h;
371 glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32; 589 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
372 glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32; 590 glTexCoord 1, 0; glVertex $px + $w, $py;
373 glEnd; 591 glEnd;
374 } 592 }
375 } 593 }
376 } 594 }
377 595
379 glDisable GL_BLEND; 597 glDisable GL_BLEND;
380} 598}
381 599
382my %DIR = ( 600my %DIR = (
383 SDLK_KP8, [1, "north"], 601 SDLK_KP8, [1, "north"],
384 SDLK_KP9, [2, "northest"], 602 SDLK_KP9, [2, "northeast"],
385 SDLK_KP6, [3, "east"], 603 SDLK_KP6, [3, "east"],
386 SDLK_KP3, [4, "southeast"], 604 SDLK_KP3, [4, "southeast"],
387 SDLK_KP2, [5, "south"], 605 SDLK_KP2, [5, "south"],
388 SDLK_KP1, [6, "southwest"], 606 SDLK_KP1, [6, "southwest"],
389 SDLK_KP4, [7, "west"], 607 SDLK_KP4, [7, "west"],
390 SDLK_KP7, [8, "northwest"], 608 SDLK_KP7, [8, "northwest"],
609
610 SDLK_UP, [1, "north"],
611 SDLK_RIGHT, [3, "east"],
612 SDLK_DOWN, [5, "south"],
613 SDLK_LEFT, [7, "west"],
391); 614);
392 615
393sub key_down { 616sub key_down {
394 my ($self, $ev) = @_; 617 my ($self, $ev) = @_;
395 618
398 621
399 if ($sym == SDLK_KP5) { 622 if ($sym == SDLK_KP5) {
400 $::CONN->send ("command stay fire"); 623 $::CONN->send ("command stay fire");
401 } elsif (exists $DIR{$sym}) { 624 } elsif (exists $DIR{$sym}) {
402 if ($mod & KMOD_SHIFT) { 625 if ($mod & KMOD_SHIFT) {
626 $self->{shft}++;
403 $::CONN->send ("command fire $DIR{$sym}[0]"); 627 $::CONN->send ("command fire $DIR{$sym}[0]");
404 } elsif ($mod & KMOD_CTRL) { 628 } elsif ($mod & KMOD_CTRL) {
629 $self->{ctrl}++;
405 $::CONN->send ("command run $DIR{$sym}[0]"); 630 $::CONN->send ("command run $DIR{$sym}[0]");
406 } else { 631 } else {
632 $::CONN->send ("command $DIR{$sym}[1]");
407 } 633 }
408 } 634 }
409} 635}
410 636
411sub key_up { 637sub key_up {
412 my ($self, $ev) = @_; 638 my ($self, $ev) = @_;
413 639
414 my $mod = $ev->key_mod; 640 my $mod = $ev->key_mod;
415 my $sym = $ev->key_sym; 641 my $sym = $ev->key_sym;
416 642
417 if (exists $DIR{$sym}) { 643 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
418 if ($mod & KMOD_SHIFT) {
419 $::CONN->send ("command fire_stop"); 644 $::CONN->send ("command fire_stop");
420 } else { 645 }
646 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
421 $::CONN->send ("command run_stop"); 647 $::CONN->send ("command run_stop");
422 }
423 } 648 }
424} 649}
425 650
4261; 6511;
427 652

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines