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.49 by root, Sun Apr 9 22:26:51 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
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;
148 glEnable GL_BLEND;
86 glBegin GL_QUADS; 149 glBegin GL_QUADS;
87 glVertex 0, 0; 150 glVertex 0 , 0;
88 glVertex $self->{w} - 1, 0; 151 glVertex $self->{w}, 0;
89 glVertex $self->{w} - 1, $self->{h} - 1; 152 glVertex $self->{w}, $self->{h};
90 glVertex 0, $self->{h} - 1; 153 glVertex 0 , $self->{h};
91 glEnd; 154 glEnd;
155 glDisable GL_BLEND;
92 } 156 }
93 glPopMatrix; 157 glPopMatrix;
94} 158}
95 159
96sub _draw { 160sub _draw {
146 #$self->deactivate; 210 #$self->deactivate;
147} 211}
148 212
149############################################################################# 213#############################################################################
150 214
151package Crossfire::Client::Widget::Container; 215package CFClient::Widget::Container;
152 216
153our @ISA = Crossfire::Client::Widget::; 217our @ISA = CFClient::Widget::;
154 218
155sub new { 219sub new {
156 my ($class, @widgets) = @_; 220 my ($class, @widgets) = @_;
157 221
158 my $self = $class->SUPER::new (children => []); 222 my $self = $class->SUPER::new (children => []);
205 $_->draw for @{$self->{children}}; 269 $_->draw for @{$self->{children}};
206} 270}
207 271
208############################################################################# 272#############################################################################
209 273
210package Crossfire::Client::Widget::Bin; 274package CFClient::Widget::Bin;
211 275
212our @ISA = Crossfire::Client::Widget::Container::; 276our @ISA = CFClient::Widget::Container::;
213 277
214sub child { $_[0]->{children}[0] } 278sub child { $_[0]->{children}[0] }
215 279
216sub size_request { 280sub size_request {
217 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 281 $_[0]{children}[0]->size_request if $_[0]{children}[0];
225 if $self->{children}[0] 289 if $self->{children}[0]
226} 290}
227 291
228############################################################################# 292#############################################################################
229 293
230package Crossfire::Client::Widget::Toplevel;
231
232our @ISA = Crossfire::Client::Widget::Container::;
233
234sub update {
235 my ($self) = @_;
236
237 ::refresh ();
238}
239
240sub add {
241 my ($self, $widget) = @_;
242
243 $self->SUPER::add ($widget);
244
245 $widget->size_allocate ($widget->size_request);
246}
247
248#############################################################################
249
250package Crossfire::Client::Widget::Window; 294package CFClient::Widget::Window;
251 295
252our @ISA = Crossfire::Client::Widget::Bin::; 296our @ISA = CFClient::Widget::Bin::;
253 297
254use SDL::OpenGL; 298use SDL::OpenGL;
255 299
256sub new { 300sub new {
257 my ($class, $x, $y, $z, $w, $h) = @_; 301 my ($class, $x, $y, $z, $w, $h) = @_;
262} 306}
263 307
264sub update { 308sub update {
265 my ($self) = @_; 309 my ($self) = @_;
266 310
311 # we want to do this delayed...
267 $self->render_chld; 312 $self->render_chld;
268 $self->SUPER::update; 313 $self->SUPER::update;
269} 314}
270 315
271sub render_chld { 316sub render_chld {
272 my ($self) = @_; 317 my ($self) = @_;
273 318
274 $self->{texture} = 319 $self->{texture} =
275 Crossfire::Client::Texture->new_from_opengl ( 320 CFClient::Texture->new_from_opengl (
276 $self->{w}, $self->{h}, sub { $self->child->draw } 321 $self->{w}, $self->{h}, sub { $self->child->draw }
277 ); 322 );
278} 323}
279 324
280sub size_allocate { 325sub size_allocate {
297 or return; 342 or return;
298 343
299 glEnable GL_BLEND; 344 glEnable GL_BLEND;
300 glEnable GL_TEXTURE_2D; 345 glEnable GL_TEXTURE_2D;
301 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 346 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
302 glBindTexture GL_TEXTURE_2D, $tex->{name};
303 347
304 glBegin GL_QUADS; 348 $tex->draw_quad (0, 0, $w, $h);
305 glTexCoord 0, 0; glVertex 0, 0;
306 glTexCoord 0, 1; glVertex 0, $h;
307 glTexCoord 1, 1; glVertex $w, $h;
308 glTexCoord 1, 0; glVertex $w, 0;
309 glEnd;
310 349
311 glDisable GL_BLEND; 350 glDisable GL_BLEND;
312 glDisable GL_TEXTURE_2D; 351 glDisable GL_TEXTURE_2D;
313} 352}
314 353
315############################################################################# 354#############################################################################
316 355
317package Crossfire::Client::Widget::Frame; 356package CFClient::Widget::Frame;
318 357
319our @ISA = Crossfire::Client::Widget::Bin::; 358our @ISA = CFClient::Widget::Bin::;
320 359
321use SDL::OpenGL; 360use SDL::OpenGL;
322 361
323sub size_request { 362sub size_request {
324 my ($self) = @_; 363 my ($self) = @_;
347 386
348 my ($w, $h) = $chld->size_request; 387 my ($w, $h) = $chld->size_request;
349 388
350 glBegin GL_QUADS; 389 glBegin GL_QUADS;
351 glColor 0, 0, 0; 390 glColor 0, 0, 0;
352 glTexCoord 0, 0; glVertex 0 , 0; 391 glVertex 0 , 0;
353 glTexCoord 0, 1; glVertex 0 , $h + 4; 392 glVertex 0 , $h + 4;
354 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 393 glVertex $w + 4 , $h + 4;
355 glTexCoord 1, 0; glVertex $w + 4 , 0; 394 glVertex $w + 4 , 0;
356 glEnd; 395 glEnd;
357 396
358 $chld->draw; 397 $chld->draw;
359} 398}
360 399
361############################################################################# 400#############################################################################
362 401
363package Crossfire::Client::Widget::FancyFrame; 402package CFClient::Widget::FancyFrame;
364 403
365our @ISA = Crossfire::Client::Widget::Bin::; 404our @ISA = CFClient::Widget::Bin::;
366 405
367use SDL::OpenGL; 406use SDL::OpenGL;
368 407
369my @tex = 408my @tex =
370 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 409 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
371 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);
372 411
373sub size_request { 412sub size_request {
374 my ($self) = @_; 413 my ($self) = @_;
375 414
410 glEnable GL_TEXTURE_2D; 449 glEnable GL_TEXTURE_2D;
411 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 450 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
412 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 451 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
413 452
414 my $top = $tex[1]; 453 my $top = $tex[1];
415 glBindTexture GL_TEXTURE_2D, $top->{name}; 454 $top->draw_quad (0, 0, $w, $top->{height});
416
417 glBegin GL_QUADS;
418 glTexCoord 0, 0; glVertex 0 , 0;
419 glTexCoord 0, 1; glVertex 0 , $top->{height};
420 glTexCoord 1, 1; glVertex $w , $top->{height};
421 glTexCoord 1, 0; glVertex $w , 0;
422 glEnd;
423 455
424 my $left = $tex[3]; 456 my $left = $tex[3];
425 glBindTexture GL_TEXTURE_2D, $left->{name}; 457 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
426
427 glBegin GL_QUADS;
428 glTexCoord 0, 0; glVertex 0 , $top->{height};
429 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
430 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
431 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
432 glEnd;
433 458
434 my $right = $tex[2]; 459 my $right = $tex[2];
435 glBindTexture GL_TEXTURE_2D, $right->{name}; 460 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
436
437 glBegin GL_QUADS;
438 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
439 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
440 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
441 glTexCoord 1, 0; glVertex $w , $top->{height};
442 glEnd;
443 461
444 my $bottom = $tex[4]; 462 my $bottom = $tex[4];
445 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 463 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
446
447 glBegin GL_QUADS;
448 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
449 glTexCoord 0, 1; glVertex 0 , $h;
450 glTexCoord 1, 1; glVertex $w , $h;
451 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
452 glEnd;
453 464
454 my $bg = $tex[0]; 465 my $bg = $tex[0];
455 glBindTexture GL_TEXTURE_2D, $bg->{name}; 466 glBindTexture GL_TEXTURE_2D, $bg->{name};
456 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 467 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
457 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
458 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 469 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
459 470
460 my $rep_x = $cw / $bg->{width}; 471 my $rep_x = $cw / $bg->{width};
461 my $rep_y = $ch / $bg->{height}; 472 my $rep_y = $ch / $bg->{height};
462 473
463 glBegin GL_QUADS; 474 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
464 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
465 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
466 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
467 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
468 glEnd;
469 475
470 glDisable GL_BLEND; 476 glDisable GL_BLEND;
471 glDisable GL_TEXTURE_2D; 477 glDisable GL_TEXTURE_2D;
472 478
473 $self->child->draw; 479 $self->child->draw;
474 480
475} 481}
476 482
477############################################################################# 483#############################################################################
478 484
479package Crossfire::Client::Widget::Table; 485package CFClient::Widget::Table;
480 486
481our @ISA = Crossfire::Client::Widget::Bin::; 487our @ISA = CFClient::Widget::Bin::;
482 488
483use SDL::OpenGL; 489use SDL::OpenGL;
484 490
485sub add { 491sub add {
486 my ($self, $x, $y, $chld) = @_; 492 my ($self, $x, $y, $chld) = @_;
561 } 567 }
562} 568}
563 569
564############################################################################# 570#############################################################################
565 571
566package Crossfire::Client::Widget::VBox; 572package CFClient::Widget::VBox;
567 573
568our @ISA = Crossfire::Client::Widget::Container::; 574our @ISA = CFClient::Widget::Container::;
569 575
570use SDL::OpenGL; 576use SDL::OpenGL;
571 577
572sub size_request { 578sub size_request {
573 my ($self) = @_; 579 my ($self) = @_;
621 } 627 }
622} 628}
623 629
624############################################################################# 630#############################################################################
625 631
626package Crossfire::Client::Widget::Label; 632package CFClient::Widget::Label;
627 633
628our @ISA = Crossfire::Client::Widget::; 634our @ISA = CFClient::Widget::;
629 635
630use SDL::OpenGL; 636use SDL::OpenGL;
631 637
632sub new { 638sub new {
633 my ($class, $x, $y, $z, $height, $text) = @_; 639 my ($class, $x, $y, $z, $height, $text) = @_;
634 640
641 $height ||= $::FONTSIZE;
642
635 # TODO: color, and make height, xyz etc. optional 643 # TODO: color, and make height, xyz etc. optional
636 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 );
637 652
638 $self->set_text ($text); 653 $self->set_text ($text);
639 654
640 $self 655 $self
641} 656}
642 657
643sub set_text { 658sub set_text {
644 my ($self, $text) = @_; 659 my ($self, $text) = @_;
645 660
646 $self->{text} = $text; 661 $self->{text} = $text;
647 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 662 $self->{layout}->set_markup ($text);
648 663
649 $self->update; 664 delete $self->{texture};
650} 665}
651 666
652sub get_text { 667sub get_text {
653 my ($self, $text) = @_; 668 my ($self, $text) = @_;
654 669
656} 671}
657 672
658sub size_request { 673sub size_request {
659 my ($self) = @_; 674 my ($self) = @_;
660 675
661 ( 676 $self->{layout}->set_width;
677 $self->{layout}->size
678# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
679# (
662 $self->{texture}{width}, 680# $self->{texture}{width},
663 $self->{texture}{height}, 681# $self->{texture}{height},
664 ) 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};
665} 695}
666 696
667sub _draw { 697sub _draw {
668 my ($self) = @_; 698 my ($self) = @_;
669 699
670 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 };
671 704
672 glEnable GL_BLEND; 705 glEnable GL_BLEND;
673 glEnable GL_TEXTURE_2D; 706 glEnable GL_TEXTURE_2D;
674 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 707 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
675 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 708 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
676 glBindTexture GL_TEXTURE_2D, $tex->{name};
677 709
678 glColor 1, 0, 0, 1; # TODO color 710 glColor @{$self->{color}};
679 711
680 glBegin GL_QUADS; 712 $tex->draw_quad (0, 0);
681 glTexCoord 0, 0; glVertex 0 , 0;
682 glTexCoord 0, 1; glVertex 0 , $tex->{height};
683 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
684 glTexCoord 1, 0; glVertex $tex->{width}, 0;
685 glEnd;
686 713
687 glDisable GL_BLEND; 714 glDisable GL_BLEND;
688 glDisable GL_TEXTURE_2D; 715 glDisable GL_TEXTURE_2D;
689} 716}
690 717
691############################################################################# 718#############################################################################
692 719
693package Crossfire::Client::Widget::TextEntry; 720package CFClient::Widget::Entry;
694 721
695our @ISA = Crossfire::Client::Widget::Label::; 722our @ISA = CFClient::Widget::Label::;
696 723
697use SDL; 724use SDL;
698use SDL::OpenGL; 725use SDL::OpenGL;
699 726
700sub key_down { 727sub key_down {
701 my ($self, $ev) = @_; 728 my ($self, $ev) = @_;
702 729
703 my $mod = $ev->key_mod; 730 my $mod = $ev->key_mod;
704 my $sym = $ev->key_sym; 731 my $sym = $ev->key_sym;
705 732
706 $ev->set_unicode (1);
707 my $uni = $ev->key_unicode; 733 my $uni = $ev->key_unicode;
708 734
709 my $text = $self->get_text; 735 my $text = $self->get_text;
710 736
711 if ($sym == SDLK_BACKSPACE) { 737 if ($sym == SDLK_BACKSPACE) {
712 substr $text, -1, 1, ''; 738 substr $text, -1, 1, '';
713
714 } elsif ($uni) { 739 } elsif ($uni) {
715 $text .= chr $uni; 740 $text .= chr $uni;
716 } 741 }
742
717 $self->set_text ($text); 743 $self->set_text ($text);
718} 744}
719 745
720############################################################################# 746sub button_down {
747 my ($self, $ev) = @_;
721 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
722package Crossfire::Client::Widget::MapWidget; 778package CFClient::Widget::MapWidget;
723 779
724use strict; 780use strict;
725 781
726use List::Util qw(min max); 782use List::Util qw(min max);
727 783
728use SDL; 784use SDL;
729use SDL::OpenGL; 785use SDL::OpenGL;
730use SDL::OpenGL::Constants; 786use SDL::OpenGL::Constants;
731 787
732our @ISA = Crossfire::Client::Widget::; 788our @ISA = CFClient::Widget::;
733 789
734sub key_down { 790sub key_down {
735 print "MAPKEYDOWN\n"; 791 print "MAPKEYDOWN\n";
736} 792}
737 793
738sub key_up { 794sub key_up {
739} 795}
740 796
741sub size_request { 797sub size_request {
742 798 (
743} 799 1 + int $::WIDTH / 32,
744 800 1 + int $::HEIGHT / 32,
745sub size_allocate { 801 )
746} 802}
747 803
748sub _draw { 804sub _draw {
749 my ($self) = @_; 805 my ($self) = @_;
750 806
773 glEnable GL_TEXTURE_2D; 829 glEnable GL_TEXTURE_2D;
774 glEnable GL_BLEND; 830 glEnable GL_BLEND;
775 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 831 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
776 832
777 my $sw4 = ($sw + 3) & ~3; 833 my $sw4 = ($sw + 3) & ~3;
778 my $lighting = "\x00" x ($sw4 * $sh); 834 my $darkness = "\x00" x ($sw4 * $sh);
779 835
780 for my $x (0 .. $sw - 1) { 836 for my $x (0 .. $sw - 1) {
837 my $row = $map->[$x + $xofs];
781 for my $y (0 .. $sh - 1) { 838 for my $y (0 .. $sh - 1) {
782 839
783 my $cell = $map->[$x + $xofs][$y + $yofs] 840 my $cell = $row->[$y + $yofs]
784 or next; 841 or next;
785 842
786 my $darkness = $cell->[0] * (1 / 255); 843 my $dark = $cell->[0];
787 if ($darkness < 0) { 844 if ($dark < 0) {
788 $darkness = 0.15; 845 substr $darkness, $y * $sw4 + $x, 1, chr 224;
846 } else {
847 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
789 } 848 }
790 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
791 849
792 for my $num (grep $_, @$cell[1,2,3]) { 850 for my $num (grep $_, @$cell[1,2,3]) {
793 my $tex = $::CONN->{face}[$num]{texture} || next; 851 my $tex = $::CONN->{face}[$num]{texture} || next;
794 852
795 glBindTexture GL_TEXTURE_2D, $tex->{name};
796
797 my $w = $tex->{width}; 853 my $w = $tex->{width};
798 my $h = $tex->{height}; 854 my $h = $tex->{height};
799 855
800 my $px = ($x + 1) * 32 - $w; 856 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
801 my $py = ($y + 1) * 32 - $h;
802
803 glBegin GL_QUADS;
804 glTexCoord 0, 0; glVertex $px , $py;
805 glTexCoord 0, 1; glVertex $px , $py + $h;
806 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
807 glTexCoord 1, 0; glVertex $px + $w, $py;
808 glEnd;
809 } 857 }
810 } 858 }
811 } 859 }
812 860
813# if (1) { # higher quality darkness 861# if (1) { # higher quality darkness
818# 866#
819# $lighting = $pb->get_pixels; 867# $lighting = $pb->get_pixels;
820# $lighting =~ s/(.)../$1/gs; 868# $lighting =~ s/(.)../$1/gs;
821# } 869# }
822 870
823 $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
824 width => $sw4, 875 width => $sw4,
825 height => $sh, 876 height => $sh,
826 data => $lighting, 877 data => $darkness,
827 internalformat => GL_ALPHA4, 878 internalformat => GL_ALPHA,
828 format => GL_ALPHA; 879 format => GL_ALPHA;
829 880
830 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 881 glColor 0.45, 0.45, 0.45, 1;
831 glColor 0, 0, 0, 0.75; 882 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
832 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
833 glBindTexture GL_TEXTURE_2D, $lighting->{name};
834 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
835 glBegin GL_QUADS;
836 glTexCoord 0, 0; glVertex 0 , 0;
837 glTexCoord 0, 1; glVertex 0 , $sh * 32;
838 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
839 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
840 glEnd;
841 883
842 glDisable GL_TEXTURE_2D; 884 glDisable GL_TEXTURE_2D;
843 glDisable GL_BLEND; 885 glDisable GL_BLEND;
844} 886}
845 887
894 } 936 }
895} 937}
896 938
897############################################################################# 939#############################################################################
898 940
899package Crossfire::Client::Widget::Animator; 941package CFClient::Widget::Animator;
900 942
901use SDL::OpenGL; 943use SDL::OpenGL;
902 944
903our @ISA = Crossfire::Client::Widget::Bin::; 945our @ISA = CFClient::Widget::Bin::;
904 946
905sub moveto { 947sub moveto {
906 my ($self, $x, $y) = @_; 948 my ($self, $x, $y) = @_;
907 949
908 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 950 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
909 $self->{speed} = 0.2; 951 $self->{speed} = 0.001;
910 $self->{time} = 1; 952 $self->{time} = 1;
911 953
912 ::animation_start $self; 954 ::animation_start $self;
913} 955}
914 956
929 971
930sub _draw { 972sub _draw {
931 my ($self) = @_; 973 my ($self) = @_;
932 974
933 glPushMatrix; 975 glPushMatrix;
934 glRotate $self->{time} * 10000, 0, 1, 0; 976 glRotate $self->{time} * 1000, 0, 1, 0;
935 $self->{children}[0]->draw; 977 $self->{children}[0]->draw;
936 glPopMatrix; 978 glPopMatrix;
937} 979}
938 980
9391; 981#############################################################################
940 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