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.43 by root, Sun Apr 9 22:12:11 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 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 => []);
177} 248}
178 249
179sub find_widget { 250sub find_widget {
180 my ($self, $x, $y) = @_; 251 my ($self, $x, $y) = @_;
181 252
253 $x -= $self->{x};
254 $y -= $self->{y};
255
182 my $res; 256 my $res;
183 257
184 for (@{ $self->{children} }) { 258 for (reverse @{ $self->{children} }) {
185 $res = $_->find_widget ($x, $y) 259 $res = $_->find_widget ($x, $y)
186 and return $res; 260 and return $res;
187 } 261 }
188 262
189 () 263 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
190} 264}
191 265
192sub _draw { 266sub _draw {
193 my ($self) = @_; 267 my ($self) = @_;
194 268
195 $_->draw for @{$self->{children}}; 269 $_->draw for @{$self->{children}};
196} 270}
197 271
198############################################################################# 272#############################################################################
199 273
200package Crossfire::Client::Widget::Bin; 274package CFClient::Widget::Bin;
201 275
202our @ISA = Crossfire::Client::Widget::Container::; 276our @ISA = CFClient::Widget::Container::;
203 277
204sub child { $_[0]->{children}[0] } 278sub child { $_[0]->{children}[0] }
205 279
206sub size_request { 280sub size_request {
207 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 281 $_[0]{children}[0]->size_request if $_[0]{children}[0];
215 if $self->{children}[0] 289 if $self->{children}[0]
216} 290}
217 291
218############################################################################# 292#############################################################################
219 293
220package Crossfire::Client::Widget::Toplevel;
221
222our @ISA = Crossfire::Client::Widget::Container::;
223
224sub update {
225 my ($self) = @_;
226
227 ::refresh ();
228}
229
230sub add {
231 my ($self, $widget) = @_;
232
233 $self->SUPER::add ($widget);
234
235 $widget->size_allocate ($widget->size_request);
236}
237
238#############################################################################
239
240package Crossfire::Client::Widget::Window; 294package CFClient::Widget::Window;
241 295
242our @ISA = Crossfire::Client::Widget::Bin::; 296our @ISA = CFClient::Widget::Bin::;
243 297
244use SDL::OpenGL; 298use SDL::OpenGL;
245 299
246sub new { 300sub new {
247 my ($class, $x, $y, $z, $w, $h) = @_; 301 my ($class, $x, $y, $z, $w, $h) = @_;
260 314
261sub render_chld { 315sub render_chld {
262 my ($self) = @_; 316 my ($self) = @_;
263 317
264 $self->{texture} = 318 $self->{texture} =
265 Crossfire::Client::Texture->new_from_opengl ( 319 CFClient::Texture->new_from_opengl (
266 $self->{w}, $self->{h}, sub { $self->child->draw } 320 $self->{w}, $self->{h}, sub { $self->child->draw }
267 ); 321 );
268} 322}
269 323
270sub size_allocate { 324sub size_allocate {
287 or return; 341 or return;
288 342
289 glEnable GL_BLEND; 343 glEnable GL_BLEND;
290 glEnable GL_TEXTURE_2D; 344 glEnable GL_TEXTURE_2D;
291 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 345 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
292 glBindTexture GL_TEXTURE_2D, $tex->{name};
293 346
294 glBegin GL_QUADS; 347 $tex->draw_quad (0, 0, $w, $h);
295 glTexCoord 0, 0; glVertex 0, 0;
296 glTexCoord 0, 1; glVertex 0, $h;
297 glTexCoord 1, 1; glVertex $w, $h;
298 glTexCoord 1, 0; glVertex $w, 0;
299 glEnd;
300 348
301 glDisable GL_BLEND; 349 glDisable GL_BLEND;
302 glDisable GL_TEXTURE_2D; 350 glDisable GL_TEXTURE_2D;
303} 351}
304 352
305############################################################################# 353#############################################################################
306 354
307package Crossfire::Client::Widget::Frame; 355package CFClient::Widget::Frame;
308 356
309our @ISA = Crossfire::Client::Widget::Bin::; 357our @ISA = CFClient::Widget::Bin::;
310 358
311use SDL::OpenGL; 359use SDL::OpenGL;
312 360
313sub size_request { 361sub size_request {
314 my ($self) = @_; 362 my ($self) = @_;
337 385
338 my ($w, $h) = $chld->size_request; 386 my ($w, $h) = $chld->size_request;
339 387
340 glBegin GL_QUADS; 388 glBegin GL_QUADS;
341 glColor 0, 0, 0; 389 glColor 0, 0, 0;
342 glTexCoord 0, 0; glVertex 0 , 0; 390 glVertex 0 , 0;
343 glTexCoord 0, 1; glVertex 0 , $h + 4; 391 glVertex 0 , $h + 4;
344 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 392 glVertex $w + 4 , $h + 4;
345 glTexCoord 1, 0; glVertex $w + 4 , 0; 393 glVertex $w + 4 , 0;
346 glEnd; 394 glEnd;
347 395
348 $chld->draw; 396 $chld->draw;
349} 397}
350 398
351############################################################################# 399#############################################################################
352 400
353package Crossfire::Client::Widget::FancyFrame; 401package CFClient::Widget::FancyFrame;
354 402
355our @ISA = Crossfire::Client::Widget::Bin::; 403our @ISA = CFClient::Widget::Bin::;
356 404
357use SDL::OpenGL; 405use SDL::OpenGL;
358 406
359my @tex = 407my @tex =
360 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 408 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
361 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);
362 410
363sub size_request { 411sub size_request {
364 my ($self) = @_; 412 my ($self) = @_;
365 413
400 glEnable GL_TEXTURE_2D; 448 glEnable GL_TEXTURE_2D;
401 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 449 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
402 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 450 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
403 451
404 my $top = $tex[1]; 452 my $top = $tex[1];
405 glBindTexture GL_TEXTURE_2D, $top->{name}; 453 $top->draw_quad (0, 0, $w, $top->{height});
406
407 glBegin GL_QUADS;
408 glTexCoord 0, 0; glVertex 0 , 0;
409 glTexCoord 0, 1; glVertex 0 , $top->{height};
410 glTexCoord 1, 1; glVertex $w , $top->{height};
411 glTexCoord 1, 0; glVertex $w , 0;
412 glEnd;
413 454
414 my $left = $tex[3]; 455 my $left = $tex[3];
415 glBindTexture GL_TEXTURE_2D, $left->{name}; 456 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
416
417 glBegin GL_QUADS;
418 glTexCoord 0, 0; glVertex 0 , $top->{height};
419 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
420 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
421 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
422 glEnd;
423 457
424 my $right = $tex[2]; 458 my $right = $tex[2];
425 glBindTexture GL_TEXTURE_2D, $right->{name}; 459 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
426
427 glBegin GL_QUADS;
428 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
429 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
430 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
431 glTexCoord 1, 0; glVertex $w , $top->{height};
432 glEnd;
433 460
434 my $bottom = $tex[4]; 461 my $bottom = $tex[4];
435 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 462 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
436
437 glBegin GL_QUADS;
438 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
439 glTexCoord 0, 1; glVertex 0 , $h;
440 glTexCoord 1, 1; glVertex $w , $h;
441 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
442 glEnd;
443 463
444 my $bg = $tex[0]; 464 my $bg = $tex[0];
445 glBindTexture GL_TEXTURE_2D, $bg->{name}; 465 glBindTexture GL_TEXTURE_2D, $bg->{name};
446 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 466 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
447 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 467 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
448 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
449 469
450 my $rep_x = $cw / $bg->{width}; 470 my $rep_x = $cw / $bg->{width};
451 my $rep_y = $ch / $bg->{height}; 471 my $rep_y = $ch / $bg->{height};
452 472
453 glBegin GL_QUADS; 473 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
454 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
455 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
456 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
457 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
458 glEnd;
459 474
460 glDisable GL_BLEND; 475 glDisable GL_BLEND;
461 glDisable GL_TEXTURE_2D; 476 glDisable GL_TEXTURE_2D;
462 477
463 $self->child->draw; 478 $self->child->draw;
464 479
465} 480}
466 481
467############################################################################# 482#############################################################################
468 483
469package Crossfire::Client::Widget::Table; 484package CFClient::Widget::Table;
470 485
471our @ISA = Crossfire::Client::Widget::Bin::; 486our @ISA = CFClient::Widget::Bin::;
472 487
473use SDL::OpenGL; 488use SDL::OpenGL;
474 489
475sub add { 490sub add {
476 my ($self, $x, $y, $chld) = @_; 491 my ($self, $x, $y, $chld) = @_;
551 } 566 }
552} 567}
553 568
554############################################################################# 569#############################################################################
555 570
556package Crossfire::Client::Widget::VBox; 571package CFClient::Widget::VBox;
557 572
558our @ISA = Crossfire::Client::Widget::Container::; 573our @ISA = CFClient::Widget::Container::;
559 574
560use SDL::OpenGL; 575use SDL::OpenGL;
561 576
562sub size_request { 577sub size_request {
563 my ($self) = @_; 578 my ($self) = @_;
611 } 626 }
612} 627}
613 628
614############################################################################# 629#############################################################################
615 630
616package Crossfire::Client::Widget::Label; 631package CFClient::Widget::Label;
617 632
618our @ISA = Crossfire::Client::Widget::; 633our @ISA = CFClient::Widget::;
619 634
620use SDL::OpenGL; 635use SDL::OpenGL;
621 636
622sub new { 637sub new {
623 my ($class, $x, $y, $z, $height, $text) = @_; 638 my ($class, $x, $y, $z, $height, $text) = @_;
624 639
640 $height ||= $::FONTSIZE;
641
625 # TODO: color, and make height, xyz etc. optional 642 # TODO: color, and make height, xyz etc. optional
626 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 );
627 651
628 $self->set_text ($text); 652 $self->set_text ($text);
629 653
630 $self 654 $self
631} 655}
632 656
633sub set_text { 657sub set_text {
634 my ($self, $text) = @_; 658 my ($self, $text) = @_;
635 659
636 $self->{text} = $text; 660 $self->{text} = $text;
637 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 661 $self->{layout}->set_markup ($text);
638 662
639 $self->update; 663 delete $self->{texture};
640} 664}
641 665
642sub get_text { 666sub get_text {
643 my ($self, $text) = @_; 667 my ($self, $text) = @_;
644 668
646} 670}
647 671
648sub size_request { 672sub size_request {
649 my ($self) = @_; 673 my ($self) = @_;
650 674
651 ( 675 $self->{layout}->set_width;
676 $self->{layout}->size
677# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
678# (
652 $self->{texture}{width}, 679# $self->{texture}{width},
653 $self->{texture}{height}, 680# $self->{texture}{height},
654 ) 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};
655} 694}
656 695
657sub _draw { 696sub _draw {
658 my ($self) = @_; 697 my ($self) = @_;
659 698
660 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 };
661 703
662 glEnable GL_BLEND; 704 glEnable GL_BLEND;
663 glEnable GL_TEXTURE_2D; 705 glEnable GL_TEXTURE_2D;
664 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 706 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
665 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 707 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
666 glBindTexture GL_TEXTURE_2D, $tex->{name};
667 708
668 glColor 1, 0, 0, 1; # TODO color 709 glColor @{$self->{color}};
669 710
670 glBegin GL_QUADS; 711 $tex->draw_quad (0, 0);
671 glTexCoord 0, 0; glVertex 0 , 0;
672 glTexCoord 0, 1; glVertex 0 , $tex->{height};
673 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
674 glTexCoord 1, 0; glVertex $tex->{width}, 0;
675 glEnd;
676 712
677 glDisable GL_BLEND; 713 glDisable GL_BLEND;
678 glDisable GL_TEXTURE_2D; 714 glDisable GL_TEXTURE_2D;
679} 715}
680 716
681############################################################################# 717#############################################################################
682 718
683package Crossfire::Client::Widget::TextEntry; 719package CFClient::Widget::Entry;
684 720
685our @ISA = Crossfire::Client::Widget::Label::; 721our @ISA = CFClient::Widget::Label::;
686 722
687use SDL; 723use SDL;
688use SDL::OpenGL; 724use SDL::OpenGL;
689 725
690sub key_down { 726sub key_down {
691 my ($self, $ev) = @_; 727 my ($self, $ev) = @_;
692 728
693 my $mod = $ev->key_mod; 729 my $mod = $ev->key_mod;
694 my $sym = $ev->key_sym; 730 my $sym = $ev->key_sym;
695 731
696 $ev->set_unicode (1);
697 my $uni = $ev->key_unicode; 732 my $uni = $ev->key_unicode;
698 733
699 my $text = $self->get_text; 734 my $text = $self->get_text;
700 735
701 if ($sym == SDLK_BACKSPACE) { 736 if ($sym == SDLK_BACKSPACE) {
702 substr $text, -1, 1, ''; 737 substr $text, -1, 1, '';
703
704 } elsif ($uni) { 738 } elsif ($uni) {
705 $text .= chr $uni; 739 $text .= chr $uni;
706 } 740 }
741
707 $self->set_text ($text); 742 $self->set_text ($text);
708} 743}
709 744
710############################################################################# 745sub button_down {
746 my ($self, $ev) = @_;
711 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
712package Crossfire::Client::Widget::MapWidget; 777package CFClient::Widget::MapWidget;
713 778
714use strict; 779use strict;
715 780
716use List::Util qw(min max); 781use List::Util qw(min max);
717 782
718use SDL; 783use SDL;
719use SDL::OpenGL; 784use SDL::OpenGL;
720use SDL::OpenGL::Constants; 785use SDL::OpenGL::Constants;
721 786
722our @ISA = Crossfire::Client::Widget::; 787our @ISA = CFClient::Widget::;
723 788
724sub key_down { 789sub key_down {
725 print "MAPKEYDOWN\n"; 790 print "MAPKEYDOWN\n";
726} 791}
727 792
728sub key_up { 793sub key_up {
729} 794}
730 795
731sub size_request { 796sub size_request {
732 797 (
733} 798 1 + int $::WIDTH / 32,
734 799 1 + int $::HEIGHT / 32,
735sub size_allocate { 800 )
736} 801}
737 802
738sub _draw { 803sub _draw {
739 my ($self) = @_; 804 my ($self) = @_;
740 805
763 glEnable GL_TEXTURE_2D; 828 glEnable GL_TEXTURE_2D;
764 glEnable GL_BLEND; 829 glEnable GL_BLEND;
765 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 830 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
766 831
767 my $sw4 = ($sw + 3) & ~3; 832 my $sw4 = ($sw + 3) & ~3;
768 my $lighting = "\x00" x ($sw4 * $sh); 833 my $darkness = "\x00" x ($sw4 * $sh);
769 834
770 for my $x (0 .. $sw - 1) { 835 for my $x (0 .. $sw - 1) {
836 my $row = $map->[$x + $xofs];
771 for my $y (0 .. $sh - 1) { 837 for my $y (0 .. $sh - 1) {
772 838
773 my $cell = $map->[$x + $xofs][$y + $yofs] 839 my $cell = $row->[$y + $yofs]
774 or next; 840 or next;
775 841
776 my $darkness = $cell->[0] * (1 / 255); 842 my $dark = $cell->[0];
777 if ($darkness < 0) { 843 if ($dark < 0) {
778 $darkness = 0.15; 844 substr $darkness, $y * $sw4 + $x, 1, chr 224;
845 } else {
846 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
779 } 847 }
780 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
781 848
782 for my $num (grep $_, @$cell[1,2,3]) { 849 for my $num (grep $_, @$cell[1,2,3]) {
783 my $tex = $::CONN->{face}[$num]{texture} || next; 850 my $tex = $::CONN->{face}[$num]{texture} || next;
784 851
785 glBindTexture GL_TEXTURE_2D, $tex->{name};
786
787 my $w = $tex->{width}; 852 my $w = $tex->{width};
788 my $h = $tex->{height}; 853 my $h = $tex->{height};
789 854
790 my $px = ($x + 1) * 32 - $w; 855 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
791 my $py = ($y + 1) * 32 - $h;
792
793 glBegin GL_QUADS;
794 glTexCoord 0, 0; glVertex $px , $py;
795 glTexCoord 0, 1; glVertex $px , $py + $h;
796 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
797 glTexCoord 1, 0; glVertex $px + $w, $py;
798 glEnd;
799 } 856 }
800 } 857 }
801 } 858 }
802 859
803# if (1) { # higher quality darkness 860# if (1) { # higher quality darkness
808# 865#
809# $lighting = $pb->get_pixels; 866# $lighting = $pb->get_pixels;
810# $lighting =~ s/(.)../$1/gs; 867# $lighting =~ s/(.)../$1/gs;
811# } 868# }
812 869
813 $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
814 width => $sw4, 874 width => $sw4,
815 height => $sh, 875 height => $sh,
816 data => $lighting, 876 data => $darkness,
817 internalformat => GL_ALPHA4, 877 internalformat => GL_ALPHA,
818 format => GL_ALPHA; 878 format => GL_ALPHA;
819 879
820 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 880 glColor 0.45, 0.45, 0.45, 1;
821 glColor 0, 0, 0, 0.75; 881 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
822 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
823 glBindTexture GL_TEXTURE_2D, $lighting->{name};
824 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
825 glBegin GL_QUADS;
826 glTexCoord 0, 0; glVertex 0 , 0;
827 glTexCoord 0, 1; glVertex 0 , $sh * 32;
828 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
829 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
830 glEnd;
831 882
832 glDisable GL_TEXTURE_2D; 883 glDisable GL_TEXTURE_2D;
833 glDisable GL_BLEND; 884 glDisable GL_BLEND;
834} 885}
835 886
884 } 935 }
885} 936}
886 937
887############################################################################# 938#############################################################################
888 939
889package Crossfire::Client::Widget::Animator; 940package CFClient::Widget::Animator;
890 941
891use SDL::OpenGL; 942use SDL::OpenGL;
892 943
893our @ISA = Crossfire::Client::Widget::Bin::; 944our @ISA = CFClient::Widget::Bin::;
894 945
895sub moveto { 946sub moveto {
896 my ($self, $x, $y) = @_; 947 my ($self, $x, $y) = @_;
897 948
898 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 949 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
899 $self->{speed} = 0.2; 950 $self->{speed} = 0.001;
900 $self->{time} = 1; 951 $self->{time} = 1;
901 952
902 ::animation_start $self; 953 ::animation_start $self;
903} 954}
904 955
919 970
920sub _draw { 971sub _draw {
921 my ($self) = @_; 972 my ($self) = @_;
922 973
923 glPushMatrix; 974 glPushMatrix;
924 glRotate $self->{time} * 10000, 0, 1, 0; 975 glRotate $self->{time} * 1000, 0, 1, 0;
925 $self->{children}[0]->draw; 976 $self->{children}[0]->draw;
926 glPopMatrix; 977 glPopMatrix;
927} 978}
928 979
9291; 980#############################################################################
930 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