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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines