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.48 by root, Sun Apr 9 22:26:09 2006 UTC vs.
Revision 1.62 by root, Tue Apr 11 13:27:48 2006 UTC

1package Crossfire::Client::Widget; 1package CFClient::Widget;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util;
6 6
7use SDL::OpenGL; 7use SDL::OpenGL;
8use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
9 9
10use Crossfire::Client; 10use CFClient;
11
12our ($FOCUS, $HOVER, $GRAB); # various widgets
13
14our $TOPLEVEL;
15our $BUTTON_STATE;
11 16
12# class methods for events 17# class methods for events
13sub feed_sdl_key_down_event { $::FOCUS->key_down ($_[0]) if $::FOCUS } 18sub feed_sdl_key_down_event {
14sub feed_sdl_key_up_event { $::FOCUS->key_up ($_[0]) if $::FOCUS } 19 $FOCUS->key_down ($_[0]) if $FOCUS;
20}
21
22sub feed_sdl_key_up_event {
23 $FOCUS->key_up ($_[0]) if $FOCUS;
24}
25
15sub feed_sdl_button_down_event { } 26sub feed_sdl_button_down_event {
27 my ($ev) = @_;
28 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
29
30 if (!$BUTTON_STATE) {
31 my $widget = $TOPLEVEL->find_widget ($x, $y);
32
33 $GRAB = $widget;
34 $GRAB->update if $GRAB;
35 }
36
37 $BUTTON_STATE |= 1 << ($ev->button - 1);
38
39 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
40}
41
16sub feed_sdl_button_up_event { } 42sub feed_sdl_button_up_event {
43 my ($ev) = @_;
44 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
45
46 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
47
48 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
49
50 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
51
52 if (!$BUTTON_STATE) {
53 my $grab = $GRAB; undef $GRAB;
54 $grab->update if $grab;
55 $GRAB->update if $GRAB;
56 }
57}
58
59sub feed_sdl_motion_event {
60 my ($ev) = @_;
61 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
62
63 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
64
65 if ($widget != $HOVER) {
66 my $hover = $HOVER; $HOVER = $widget;
67
68 $hover->update if $hover;
69 $HOVER->update if $HOVER;
70 }
71
72 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
73}
17 74
18sub new { 75sub new {
19 my $class = shift; 76 my $class = shift;
20 77
21 bless { @_ }, $class 78 bless { @_ }, $class
42 99
43 $self->{w} = $w; 100 $self->{w} = $w;
44 $self->{h} = $h; 101 $self->{h} = $h;
45} 102}
46 103
104# translate global koordinates to local coordinate system
105sub translate {
106 my ($self, $x, $y) = @_;
107
108 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
109}
110
47sub focus_in { 111sub focus_in {
48 my ($widget) = @_; 112 my ($self) = @_;
49 $::FOCUS = $widget; 113
114 my $focus = $FOCUS; $FOCUS = $self;
115 $focus->update if $focus;
116 $FOCUS->update;
50} 117}
51 118
52sub focus_out { 119sub focus_out {
53 my ($widget) = @_; 120 my ($self) = @_;
54}
55 121
56sub key_down { 122 return unless $FOCUS == $self;
57 my ($widget, $sdlev) = @_;
58}
59 123
60sub key_up { 124 my $focus = $FOCUS; undef $FOCUS;
61 my ($widget, $sdlev) = @_; 125 $focus->update if $focus; #?
62} 126}
63 127
128sub mouse_motion { }
129sub button_up { }
64sub button_down { 130sub button_down { }
65 my ($widget, $sdlev) = @_; 131sub key_down { }
66} 132sub key_up { }
67 133
68sub button_up {
69 my ($widget, $sdlev) = @_;
70}
71
72sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 134sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
73sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 135sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
74sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 136sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
75sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 137sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
76sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 138sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
77 139
78sub draw { 140sub draw {
79 my ($self) = @_; 141 my ($self) = @_;
80 142
81 glPushMatrix; 143 glPushMatrix;
82 glTranslate $self->{x}, $self->{y}, 0; 144 glTranslate $self->{x}, $self->{y}, 0;
83 $self->_draw; 145 $self->_draw;
84 if ($self == $::HOVER) { 146 if ($self == $HOVER) {
85 glColor 1, 1, 1, 0.4; 147 glColor 1, 1, 1, 0.4;
86 glEnable GL_BLEND; 148 glEnable GL_BLEND;
87 glBegin GL_QUADS; 149 glBegin GL_QUADS;
88 glVertex 0, 0; 150 glVertex 0 , 0;
89 glVertex $self->{w} - 1, 0; 151 glVertex $self->{w}, 0;
90 glVertex $self->{w} - 1, $self->{h} - 1; 152 glVertex $self->{w}, $self->{h};
91 glVertex 0, $self->{h} - 1; 153 glVertex 0 , $self->{h};
92 glEnd; 154 glEnd;
93 glDisable GL_BLEND; 155 glDisable GL_BLEND;
94 } 156 }
95 glPopMatrix; 157 glPopMatrix;
96} 158}
148 #$self->deactivate; 210 #$self->deactivate;
149} 211}
150 212
151############################################################################# 213#############################################################################
152 214
153package Crossfire::Client::Widget::Container; 215package CFClient::Widget::Container;
154 216
155our @ISA = Crossfire::Client::Widget::; 217our @ISA = CFClient::Widget::;
156 218
157sub new { 219sub new {
158 my ($class, @widgets) = @_; 220 my ($class, @widgets) = @_;
159 221
160 my $self = $class->SUPER::new (children => []); 222 my $self = $class->SUPER::new (children => []);
207 $_->draw for @{$self->{children}}; 269 $_->draw for @{$self->{children}};
208} 270}
209 271
210############################################################################# 272#############################################################################
211 273
212package Crossfire::Client::Widget::Bin; 274package CFClient::Widget::Bin;
213 275
214our @ISA = Crossfire::Client::Widget::Container::; 276our @ISA = CFClient::Widget::Container::;
215 277
216sub child { $_[0]->{children}[0] } 278sub child { $_[0]->{children}[0] }
217 279
218sub size_request { 280sub size_request {
219 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 281 $_[0]{children}[0]->size_request if $_[0]{children}[0];
227 if $self->{children}[0] 289 if $self->{children}[0]
228} 290}
229 291
230############################################################################# 292#############################################################################
231 293
232package Crossfire::Client::Widget::Toplevel;
233
234our @ISA = Crossfire::Client::Widget::Container::;
235
236sub update {
237 my ($self) = @_;
238
239 ::refresh ();
240}
241
242sub add {
243 my ($self, $widget) = @_;
244
245 $self->SUPER::add ($widget);
246
247 $widget->size_allocate ($widget->size_request);
248}
249
250#############################################################################
251
252package Crossfire::Client::Widget::Window; 294package CFClient::Widget::Window;
253 295
254our @ISA = Crossfire::Client::Widget::Bin::; 296our @ISA = CFClient::Widget::Bin::;
255 297
256use SDL::OpenGL; 298use SDL::OpenGL;
257 299
258sub new { 300sub new {
259 my ($class, $x, $y, $z, $w, $h) = @_; 301 my ($class, $x, $y, $z, $w, $h) = @_;
272 314
273sub render_chld { 315sub render_chld {
274 my ($self) = @_; 316 my ($self) = @_;
275 317
276 $self->{texture} = 318 $self->{texture} =
277 Crossfire::Client::Texture->new_from_opengl ( 319 CFClient::Texture->new_from_opengl (
278 $self->{w}, $self->{h}, sub { $self->child->draw } 320 $self->{w}, $self->{h}, sub { $self->child->draw }
279 ); 321 );
280} 322}
281 323
282sub size_allocate { 324sub size_allocate {
299 or return; 341 or return;
300 342
301 glEnable GL_BLEND; 343 glEnable GL_BLEND;
302 glEnable GL_TEXTURE_2D; 344 glEnable GL_TEXTURE_2D;
303 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 345 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
304 glBindTexture GL_TEXTURE_2D, $tex->{name};
305 346
306 glBegin GL_QUADS; 347 $tex->draw_quad (0, 0, $w, $h);
307 glTexCoord 0, 0; glVertex 0, 0;
308 glTexCoord 0, 1; glVertex 0, $h;
309 glTexCoord 1, 1; glVertex $w, $h;
310 glTexCoord 1, 0; glVertex $w, 0;
311 glEnd;
312 348
313 glDisable GL_BLEND; 349 glDisable GL_BLEND;
314 glDisable GL_TEXTURE_2D; 350 glDisable GL_TEXTURE_2D;
315} 351}
316 352
317############################################################################# 353#############################################################################
318 354
319package Crossfire::Client::Widget::Frame; 355package CFClient::Widget::Frame;
320 356
321our @ISA = Crossfire::Client::Widget::Bin::; 357our @ISA = CFClient::Widget::Bin::;
322 358
323use SDL::OpenGL; 359use SDL::OpenGL;
324 360
325sub size_request { 361sub size_request {
326 my ($self) = @_; 362 my ($self) = @_;
349 385
350 my ($w, $h) = $chld->size_request; 386 my ($w, $h) = $chld->size_request;
351 387
352 glBegin GL_QUADS; 388 glBegin GL_QUADS;
353 glColor 0, 0, 0; 389 glColor 0, 0, 0;
354 glTexCoord 0, 0; glVertex 0 , 0; 390 glVertex 0 , 0;
355 glTexCoord 0, 1; glVertex 0 , $h + 4; 391 glVertex 0 , $h + 4;
356 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 392 glVertex $w + 4 , $h + 4;
357 glTexCoord 1, 0; glVertex $w + 4 , 0; 393 glVertex $w + 4 , 0;
358 glEnd; 394 glEnd;
359 395
360 $chld->draw; 396 $chld->draw;
361} 397}
362 398
363############################################################################# 399#############################################################################
364 400
365package Crossfire::Client::Widget::FancyFrame; 401package CFClient::Widget::FancyFrame;
366 402
367our @ISA = Crossfire::Client::Widget::Bin::; 403our @ISA = CFClient::Widget::Bin::;
368 404
369use SDL::OpenGL; 405use SDL::OpenGL;
370 406
371my @tex = 407my @tex =
372 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 408 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
373 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 409 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
374 410
375sub size_request { 411sub size_request {
376 my ($self) = @_; 412 my ($self) = @_;
377 413
412 glEnable GL_TEXTURE_2D; 448 glEnable GL_TEXTURE_2D;
413 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 449 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
414 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 450 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
415 451
416 my $top = $tex[1]; 452 my $top = $tex[1];
417 glBindTexture GL_TEXTURE_2D, $top->{name}; 453 $top->draw_quad (0, 0, $w, $top->{height});
418
419 glBegin GL_QUADS;
420 glTexCoord 0, 0; glVertex 0 , 0;
421 glTexCoord 0, 1; glVertex 0 , $top->{height};
422 glTexCoord 1, 1; glVertex $w , $top->{height};
423 glTexCoord 1, 0; glVertex $w , 0;
424 glEnd;
425 454
426 my $left = $tex[3]; 455 my $left = $tex[3];
427 glBindTexture GL_TEXTURE_2D, $left->{name}; 456 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
428
429 glBegin GL_QUADS;
430 glTexCoord 0, 0; glVertex 0 , $top->{height};
431 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
432 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
433 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
434 glEnd;
435 457
436 my $right = $tex[2]; 458 my $right = $tex[2];
437 glBindTexture GL_TEXTURE_2D, $right->{name}; 459 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
438
439 glBegin GL_QUADS;
440 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
441 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
442 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
443 glTexCoord 1, 0; glVertex $w , $top->{height};
444 glEnd;
445 460
446 my $bottom = $tex[4]; 461 my $bottom = $tex[4];
447 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 462 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
448
449 glBegin GL_QUADS;
450 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
451 glTexCoord 0, 1; glVertex 0 , $h;
452 glTexCoord 1, 1; glVertex $w , $h;
453 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
454 glEnd;
455 463
456 my $bg = $tex[0]; 464 my $bg = $tex[0];
457 glBindTexture GL_TEXTURE_2D, $bg->{name}; 465 glBindTexture GL_TEXTURE_2D, $bg->{name};
458 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 466 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
459 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 467 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
460 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
461 469
462 my $rep_x = $cw / $bg->{width}; 470 my $rep_x = $cw / $bg->{width};
463 my $rep_y = $ch / $bg->{height}; 471 my $rep_y = $ch / $bg->{height};
464 472
465 glBegin GL_QUADS; 473 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
466 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
467 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
468 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
469 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
470 glEnd;
471 474
472 glDisable GL_BLEND; 475 glDisable GL_BLEND;
473 glDisable GL_TEXTURE_2D; 476 glDisable GL_TEXTURE_2D;
474 477
475 $self->child->draw; 478 $self->child->draw;
476 479
477} 480}
478 481
479############################################################################# 482#############################################################################
480 483
481package Crossfire::Client::Widget::Table; 484package CFClient::Widget::Table;
482 485
483our @ISA = Crossfire::Client::Widget::Bin::; 486our @ISA = CFClient::Widget::Bin::;
484 487
485use SDL::OpenGL; 488use SDL::OpenGL;
486 489
487sub add { 490sub add {
488 my ($self, $x, $y, $chld) = @_; 491 my ($self, $x, $y, $chld) = @_;
563 } 566 }
564} 567}
565 568
566############################################################################# 569#############################################################################
567 570
568package Crossfire::Client::Widget::VBox; 571package CFClient::Widget::VBox;
569 572
570our @ISA = Crossfire::Client::Widget::Container::; 573our @ISA = CFClient::Widget::Container::;
571 574
572use SDL::OpenGL; 575use SDL::OpenGL;
573 576
574sub size_request { 577sub size_request {
575 my ($self) = @_; 578 my ($self) = @_;
623 } 626 }
624} 627}
625 628
626############################################################################# 629#############################################################################
627 630
628package Crossfire::Client::Widget::Label; 631package CFClient::Widget::Label;
629 632
630our @ISA = Crossfire::Client::Widget::; 633our @ISA = CFClient::Widget::;
631 634
632use SDL::OpenGL; 635use SDL::OpenGL;
633 636
634sub new { 637sub new {
635 my ($class, $x, $y, $z, $height, $text) = @_; 638 my ($class, $x, $y, $z, $height, $text) = @_;
636 639
640 $height ||= $::FONTSIZE;
641
637 # TODO: color, and make height, xyz etc. optional 642 # TODO: color, and make height, xyz etc. optional
638 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 643 my $self = $class->SUPER::new (
644 color => [1, 1, 1],
645 x => $x,
646 y => $y,
647 z => $z,
648 height => $height,
649 layout => new CFClient::Layout $height,
650 );
639 651
640 $self->set_text ($text); 652 $self->set_text ($text);
641 653
642 $self 654 $self
643} 655}
644 656
645sub set_text { 657sub set_text {
646 my ($self, $text) = @_; 658 my ($self, $text) = @_;
647 659
648 $self->{text} = $text; 660 $self->{text} = $text;
649 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 661 $self->{layout}->set_markup ($text);
650 662
651 $self->update; 663 delete $self->{texture};
652} 664}
653 665
654sub get_text { 666sub get_text {
655 my ($self, $text) = @_; 667 my ($self, $text) = @_;
656 668
658} 670}
659 671
660sub size_request { 672sub size_request {
661 my ($self) = @_; 673 my ($self) = @_;
662 674
663 ( 675 $self->{layout}->set_width;
676 $self->{layout}->size
677# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
678# (
664 $self->{texture}{width}, 679# $self->{texture}{width},
665 $self->{texture}{height}, 680# $self->{texture}{height},
666 ) 681# )
682# } else {
683# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
684#
685# ($w, $h)
686# }
687}
688
689sub size_allocate {
690 my ($self, $w, $h) = @_;
691
692 $self->SUPER::size_allocate ($w, $h);
693 delete $self->{texture};
667} 694}
668 695
669sub _draw { 696sub _draw {
670 my ($self) = @_; 697 my ($self) = @_;
671 698
672 my $tex = $self->{texture}; 699 my $tex = $self->{texture} ||= do {
700 $self->{layout}->set_width ($self->{w});
701 new_from_layout CFClient::Texture $self->{layout};
702 };
673 703
674 glEnable GL_BLEND; 704 glEnable GL_BLEND;
675 glEnable GL_TEXTURE_2D; 705 glEnable GL_TEXTURE_2D;
676 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 706 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
677 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 707 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
678 glBindTexture GL_TEXTURE_2D, $tex->{name};
679 708
680 glColor 1, 0, 0, 1; # TODO color 709 glColor @{$self->{color}};
681 710
682 glBegin GL_QUADS; 711 $tex->draw_quad (0, 0);
683 glTexCoord 0, 0; glVertex 0 , 0;
684 glTexCoord 0, 1; glVertex 0 , $tex->{height};
685 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
686 glTexCoord 1, 0; glVertex $tex->{width}, 0;
687 glEnd;
688 712
689 glDisable GL_BLEND; 713 glDisable GL_BLEND;
690 glDisable GL_TEXTURE_2D; 714 glDisable GL_TEXTURE_2D;
691} 715}
692 716
693############################################################################# 717#############################################################################
694 718
695package Crossfire::Client::Widget::TextEntry; 719package CFClient::Widget::Entry;
696 720
697our @ISA = Crossfire::Client::Widget::Label::; 721our @ISA = CFClient::Widget::Label::;
698 722
699use SDL; 723use SDL;
700use SDL::OpenGL; 724use SDL::OpenGL;
701 725
702sub key_down { 726sub key_down {
703 my ($self, $ev) = @_; 727 my ($self, $ev) = @_;
704 728
705 my $mod = $ev->key_mod; 729 my $mod = $ev->key_mod;
706 my $sym = $ev->key_sym; 730 my $sym = $ev->key_sym;
707 731
708 $ev->set_unicode (1);
709 my $uni = $ev->key_unicode; 732 my $uni = $ev->key_unicode;
710 733
711 my $text = $self->get_text; 734 my $text = $self->get_text;
712 735
713 if ($sym == SDLK_BACKSPACE) { 736 if ($sym == SDLK_BACKSPACE) {
714 substr $text, -1, 1, ''; 737 substr $text, -1, 1, '';
715
716 } elsif ($uni) { 738 } elsif ($uni) {
717 $text .= chr $uni; 739 $text .= chr $uni;
718 } 740 }
741
719 $self->set_text ($text); 742 $self->set_text ($text);
720} 743}
721 744
722############################################################################# 745sub button_down {
746 my ($self, $ev) = @_;
723 747
748 $self->focus_in;
749}
750
751sub mouse_motion {
752 my ($self, $ev, $x, $y) = @_;
753 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
754}
755
756sub _draw {
757 my ($self) = @_;
758
759 if ($FOCUS == $self) {
760 glColor 1, 1, 1;
761 } else {
762 glColor 0.7, 0.7, 0.7;
763 }
764
765 glBegin GL_QUADS;
766 glVertex 0 , 0;
767 glVertex 0 , $self->{h} - 1;
768 glVertex $self->{w} - 1, $self->{h} - 1;
769 glVertex $self->{w} - 1, 0;
770 glEnd;
771
772 $self->SUPER::_draw;
773}
774
775#############################################################################
776
724package Crossfire::Client::Widget::MapWidget; 777package CFClient::Widget::MapWidget;
725 778
726use strict; 779use strict;
727 780
728use List::Util qw(min max); 781use List::Util qw(min max);
729 782
730use SDL; 783use SDL;
731use SDL::OpenGL; 784use SDL::OpenGL;
732use SDL::OpenGL::Constants; 785use SDL::OpenGL::Constants;
733 786
734our @ISA = Crossfire::Client::Widget::; 787our @ISA = CFClient::Widget::;
735 788
736sub key_down { 789sub key_down {
737 print "MAPKEYDOWN\n"; 790 print "MAPKEYDOWN\n";
738} 791}
739 792
740sub key_up { 793sub key_up {
741} 794}
742 795
743sub size_request { 796sub size_request {
744 797 (
745} 798 1 + int $::WIDTH / 32,
746 799 1 + int $::HEIGHT / 32,
747sub size_allocate { 800 )
748} 801}
749 802
750sub _draw { 803sub _draw {
751 my ($self) = @_; 804 my ($self) = @_;
752 805
775 glEnable GL_TEXTURE_2D; 828 glEnable GL_TEXTURE_2D;
776 glEnable GL_BLEND; 829 glEnable GL_BLEND;
777 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 830 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
778 831
779 my $sw4 = ($sw + 3) & ~3; 832 my $sw4 = ($sw + 3) & ~3;
780 my $lighting = "\x00" x ($sw4 * $sh); 833 my $darkness = "\x00" x ($sw4 * $sh);
781 834
782 for my $x (0 .. $sw - 1) { 835 for my $x (0 .. $sw - 1) {
836 my $row = $map->[$x + $xofs];
783 for my $y (0 .. $sh - 1) { 837 for my $y (0 .. $sh - 1) {
784 838
785 my $cell = $map->[$x + $xofs][$y + $yofs] 839 my $cell = $row->[$y + $yofs]
786 or next; 840 or next;
787 841
788 my $darkness = $cell->[0] * (1 / 255); 842 my $dark = $cell->[0];
789 if ($darkness < 0) { 843 if ($dark < 0) {
790 $darkness = 0.15; 844 substr $darkness, $y * $sw4 + $x, 1, chr 224;
845 } else {
846 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
791 } 847 }
792 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
793 848
794 for my $num (grep $_, @$cell[1,2,3]) { 849 for my $num (grep $_, @$cell[1,2,3]) {
795 my $tex = $::CONN->{face}[$num]{texture} || next; 850 my $tex = $::CONN->{face}[$num]{texture} || next;
796 851
797 glBindTexture GL_TEXTURE_2D, $tex->{name};
798
799 my $w = $tex->{width}; 852 my $w = $tex->{width};
800 my $h = $tex->{height}; 853 my $h = $tex->{height};
801 854
802 my $px = ($x + 1) * 32 - $w; 855 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
803 my $py = ($y + 1) * 32 - $h;
804
805 glBegin GL_QUADS;
806 glTexCoord 0, 0; glVertex $px , $py;
807 glTexCoord 0, 1; glVertex $px , $py + $h;
808 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
809 glTexCoord 1, 0; glVertex $px + $w, $py;
810 glEnd;
811 } 856 }
812 } 857 }
813 } 858 }
814 859
815# if (1) { # higher quality darkness 860# if (1) { # higher quality darkness
820# 865#
821# $lighting = $pb->get_pixels; 866# $lighting = $pb->get_pixels;
822# $lighting =~ s/(.)../$1/gs; 867# $lighting =~ s/(.)../$1/gs;
823# } 868# }
824 869
825 $lighting = new Crossfire::Client::Texture 870 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
871 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
872
873 $darkness = new CFClient::Texture
826 width => $sw4, 874 width => $sw4,
827 height => $sh, 875 height => $sh,
828 data => $lighting, 876 data => $darkness,
829 internalformat => GL_ALPHA4, 877 internalformat => GL_ALPHA,
830 format => GL_ALPHA; 878 format => GL_ALPHA;
831 879
832 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 880 glColor 0.45, 0.45, 0.45, 1;
833 glColor 0, 0, 0, 0.75; 881 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
834 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
835 glBindTexture GL_TEXTURE_2D, $lighting->{name};
836 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
837 glBegin GL_QUADS;
838 glTexCoord 0, 0; glVertex 0 , 0;
839 glTexCoord 0, 1; glVertex 0 , $sh * 32;
840 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
841 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
842 glEnd;
843 882
844 glDisable GL_TEXTURE_2D; 883 glDisable GL_TEXTURE_2D;
845 glDisable GL_BLEND; 884 glDisable GL_BLEND;
846} 885}
847 886
896 } 935 }
897} 936}
898 937
899############################################################################# 938#############################################################################
900 939
901package Crossfire::Client::Widget::Animator; 940package CFClient::Widget::Animator;
902 941
903use SDL::OpenGL; 942use SDL::OpenGL;
904 943
905our @ISA = Crossfire::Client::Widget::Bin::; 944our @ISA = CFClient::Widget::Bin::;
906 945
907sub moveto { 946sub moveto {
908 my ($self, $x, $y) = @_; 947 my ($self, $x, $y) = @_;
909 948
910 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 949 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
911 $self->{speed} = 0.2; 950 $self->{speed} = 0.001;
912 $self->{time} = 1; 951 $self->{time} = 1;
913 952
914 ::animation_start $self; 953 ::animation_start $self;
915} 954}
916 955
931 970
932sub _draw { 971sub _draw {
933 my ($self) = @_; 972 my ($self) = @_;
934 973
935 glPushMatrix; 974 glPushMatrix;
936 glRotate $self->{time} * 10000, 0, 1, 0; 975 glRotate $self->{time} * 1000, 0, 1, 0;
937 $self->{children}[0]->draw; 976 $self->{children}[0]->draw;
938 glPopMatrix; 977 glPopMatrix;
939} 978}
940 979
9411; 980#############################################################################
942 981
982package CFClient::Widget::Toplevel;
983
984our @ISA = CFClient::Widget::Container::;
985
986sub size_request {
987 ($::WIDTH, $::HEIGHT)
988}
989
990sub size_allocate {
991 my ($self, $w, $h) = @_;
992
993 $self->SUPER::size_allocate ($w, $h);
994
995 $_->size_allocate ($_->size_request)
996 for @{$self->{children}};
997}
998
999sub translate {
1000 my ($self, $x, $y) = @_;
1001
1002 ($x, $y)
1003}
1004
1005sub update {
1006 my ($self) = @_;
1007
1008 $self->size_allocate ($self->size_request);
1009 ::refresh ();
1010}
1011
1012sub add {
1013 my ($self, $widget) = @_;
1014
1015 $self->SUPER::add ($widget);
1016
1017 $widget->size_allocate ($widget->size_request);
1018}
1019
1020sub draw {
1021 my ($self) = @_;
1022
1023 $self->_draw;
1024}
1025
1026#############################################################################
1027
1028package CFClient::Widget;
1029
1030$TOPLEVEL = new CFClient::Widget::Toplevel;
1031
10321
1033

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines