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.47 by root, Sun Apr 9 22:24:43 2006 UTC vs.
Revision 1.65 by root, Tue Apr 11 14:04:27 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 {
79 x => 0,
80 y => 0,
81 z => 0,
82 @_
83 }, $class
22} 84}
23 85
24sub move { 86sub move {
25 my ($self, $x, $y, $z) = @_; 87 my ($self, $x, $y, $z) = @_;
26 $self->{x} = $x; 88 $self->{x} = $x;
42 104
43 $self->{w} = $w; 105 $self->{w} = $w;
44 $self->{h} = $h; 106 $self->{h} = $h;
45} 107}
46 108
109# translate global koordinates to local coordinate system
110sub translate {
111 my ($self, $x, $y) = @_;
112
113 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
114}
115
47sub focus_in { 116sub focus_in {
48 my ($widget) = @_; 117 my ($self) = @_;
49 $::FOCUS = $widget; 118
119 my $focus = $FOCUS; $FOCUS = $self;
120 $focus->update if $focus;
121 $FOCUS->update;
50} 122}
51 123
52sub focus_out { 124sub focus_out {
53 my ($widget) = @_; 125 my ($self) = @_;
54}
55 126
56sub key_down { 127 return unless $FOCUS == $self;
57 my ($widget, $sdlev) = @_;
58}
59 128
60sub key_up { 129 my $focus = $FOCUS; undef $FOCUS;
61 my ($widget, $sdlev) = @_; 130 $focus->update if $focus; #?
62} 131}
63 132
133sub mouse_motion { }
134sub button_up { }
64sub button_down { 135sub button_down { }
65 my ($widget, $sdlev) = @_; 136sub key_down { }
66} 137sub key_up { }
67 138
68sub button_up {
69 my ($widget, $sdlev) = @_;
70}
71
72sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 139sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
73sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 140sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
74sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 141sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
75sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 142sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
76sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 143sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
77 144
78sub draw { 145sub draw {
79 my ($self) = @_; 146 my ($self) = @_;
80 147
81 glPushMatrix; 148 glPushMatrix;
82 glTranslate $self->{x}, $self->{y}, 0; 149 glTranslate $self->{x}, $self->{y}, 0;
83 $self->_draw; 150 $self->_draw;
84 if ($self == $::HOVER) { 151 if ($self == $HOVER) {
85 glColor 1, 1, 1, 1; 152 glColor 1, 1, 1, 0.4;
153 glEnable GL_BLEND;
86 glBegin GL_LINES; 154 glBegin GL_QUADS;
87 glVertex 0, 0; 155 glVertex 0 , 0;
88 glVertex $self->{w} - 1, 0; 156 glVertex $self->{w}, 0;
89 glVertex $self->{w} - 1, $self->{h} - 1; 157 glVertex $self->{w}, $self->{h};
90 glVertex 0, $self->{h} - 1; 158 glVertex 0 , $self->{h};
91 glVertex 0, 0;
92 glEnd; 159 glEnd;
160 glDisable GL_BLEND;
93 } 161 }
94 glPopMatrix; 162 glPopMatrix;
95} 163}
96 164
97sub _draw { 165sub _draw {
147 #$self->deactivate; 215 #$self->deactivate;
148} 216}
149 217
150############################################################################# 218#############################################################################
151 219
152package Crossfire::Client::Widget::Container; 220package CFClient::Widget::Container;
153 221
154our @ISA = Crossfire::Client::Widget::; 222our @ISA = CFClient::Widget::;
155 223
156sub new { 224sub new {
157 my ($class, @widgets) = @_; 225 my ($class, %arg) = @_;
158 226
227 my $children = delete $arg{children} || [];
228
159 my $self = $class->SUPER::new (children => []); 229 my $self = $class->SUPER::new (children => [], %arg);
160 $self->add ($_) for @widgets; 230 $self->add ($_) for @$children;
161 231
162 $self 232 $self
163} 233}
164 234
165sub add { 235sub add {
206 $_->draw for @{$self->{children}}; 276 $_->draw for @{$self->{children}};
207} 277}
208 278
209############################################################################# 279#############################################################################
210 280
211package Crossfire::Client::Widget::Bin; 281package CFClient::Widget::Bin;
212 282
213our @ISA = Crossfire::Client::Widget::Container::; 283our @ISA = CFClient::Widget::Container::;
214 284
215sub child { $_[0]->{children}[0] } 285sub child { $_[0]->{children}[0] }
216 286
217sub size_request { 287sub size_request {
218 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 288 $_[0]{children}[0]->size_request if $_[0]{children}[0];
226 if $self->{children}[0] 296 if $self->{children}[0]
227} 297}
228 298
229############################################################################# 299#############################################################################
230 300
231package Crossfire::Client::Widget::Toplevel; 301package CFClient::Widget::Window;
232 302
233our @ISA = Crossfire::Client::Widget::Container::; 303our @ISA = CFClient::Widget::Bin::;
304
305use SDL::OpenGL;
306
307sub new {
308 my ($class, %arg) = @_;
309
310 my $self = $class->SUPER::new (%arg);
311}
234 312
235sub update { 313sub update {
236 my ($self) = @_; 314 my ($self) = @_;
237 315
238 ::refresh (); 316 # we want to do this delayed...
239}
240
241sub add {
242 my ($self, $widget) = @_;
243
244 $self->SUPER::add ($widget);
245
246 $widget->size_allocate ($widget->size_request);
247}
248
249#############################################################################
250
251package Crossfire::Client::Widget::Window;
252
253our @ISA = Crossfire::Client::Widget::Bin::;
254
255use SDL::OpenGL;
256
257sub new {
258 my ($class, $x, $y, $z, $w, $h) = @_;
259
260 my $self = $class->SUPER::new;
261
262 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
263}
264
265sub update {
266 my ($self) = @_;
267
268 $self->render_chld; 317 $self->render_chld;
269 $self->SUPER::update; 318 $self->SUPER::update;
270} 319}
271 320
272sub render_chld { 321sub render_chld {
273 my ($self) = @_; 322 my ($self) = @_;
274 323
275 $self->{texture} = 324 $self->{texture} =
276 Crossfire::Client::Texture->new_from_opengl ( 325 CFClient::Texture->new_from_opengl (
277 $self->{w}, $self->{h}, sub { $self->child->draw } 326 $self->{w}, $self->{h}, sub { $self->child->draw }
278 ); 327 );
279} 328}
280 329
281sub size_allocate { 330sub size_allocate {
298 or return; 347 or return;
299 348
300 glEnable GL_BLEND; 349 glEnable GL_BLEND;
301 glEnable GL_TEXTURE_2D; 350 glEnable GL_TEXTURE_2D;
302 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 351 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
303 glBindTexture GL_TEXTURE_2D, $tex->{name};
304 352
305 glBegin GL_QUADS; 353 $tex->draw_quad (0, 0, $w, $h);
306 glTexCoord 0, 0; glVertex 0, 0;
307 glTexCoord 0, 1; glVertex 0, $h;
308 glTexCoord 1, 1; glVertex $w, $h;
309 glTexCoord 1, 0; glVertex $w, 0;
310 glEnd;
311 354
312 glDisable GL_BLEND; 355 glDisable GL_BLEND;
313 glDisable GL_TEXTURE_2D; 356 glDisable GL_TEXTURE_2D;
314} 357}
315 358
316############################################################################# 359#############################################################################
317 360
318package Crossfire::Client::Widget::Frame; 361package CFClient::Widget::Frame;
319 362
320our @ISA = Crossfire::Client::Widget::Bin::; 363our @ISA = CFClient::Widget::Bin::;
321 364
322use SDL::OpenGL; 365use SDL::OpenGL;
323 366
324sub size_request { 367sub size_request {
325 my ($self) = @_; 368 my ($self) = @_;
348 391
349 my ($w, $h) = $chld->size_request; 392 my ($w, $h) = $chld->size_request;
350 393
351 glBegin GL_QUADS; 394 glBegin GL_QUADS;
352 glColor 0, 0, 0; 395 glColor 0, 0, 0;
353 glTexCoord 0, 0; glVertex 0 , 0; 396 glVertex 0 , 0;
354 glTexCoord 0, 1; glVertex 0 , $h + 4; 397 glVertex 0 , $h + 4;
355 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 398 glVertex $w + 4 , $h + 4;
356 glTexCoord 1, 0; glVertex $w + 4 , 0; 399 glVertex $w + 4 , 0;
357 glEnd; 400 glEnd;
358 401
359 $chld->draw; 402 $chld->draw;
360} 403}
361 404
362############################################################################# 405#############################################################################
363 406
364package Crossfire::Client::Widget::FancyFrame; 407package CFClient::Widget::FancyFrame;
365 408
366our @ISA = Crossfire::Client::Widget::Bin::; 409our @ISA = CFClient::Widget::Bin::;
367 410
368use SDL::OpenGL; 411use SDL::OpenGL;
369 412
370my @tex = 413my @tex =
371 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 414 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
372 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 415 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
373 416
374sub size_request { 417sub size_request {
375 my ($self) = @_; 418 my ($self) = @_;
376 419
411 glEnable GL_TEXTURE_2D; 454 glEnable GL_TEXTURE_2D;
412 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 455 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
413 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 456 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
414 457
415 my $top = $tex[1]; 458 my $top = $tex[1];
416 glBindTexture GL_TEXTURE_2D, $top->{name}; 459 $top->draw_quad (0, 0, $w, $top->{height});
417
418 glBegin GL_QUADS;
419 glTexCoord 0, 0; glVertex 0 , 0;
420 glTexCoord 0, 1; glVertex 0 , $top->{height};
421 glTexCoord 1, 1; glVertex $w , $top->{height};
422 glTexCoord 1, 0; glVertex $w , 0;
423 glEnd;
424 460
425 my $left = $tex[3]; 461 my $left = $tex[3];
426 glBindTexture GL_TEXTURE_2D, $left->{name}; 462 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
427
428 glBegin GL_QUADS;
429 glTexCoord 0, 0; glVertex 0 , $top->{height};
430 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
431 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
432 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
433 glEnd;
434 463
435 my $right = $tex[2]; 464 my $right = $tex[2];
436 glBindTexture GL_TEXTURE_2D, $right->{name}; 465 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
437
438 glBegin GL_QUADS;
439 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
440 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
441 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
442 glTexCoord 1, 0; glVertex $w , $top->{height};
443 glEnd;
444 466
445 my $bottom = $tex[4]; 467 my $bottom = $tex[4];
446 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 468 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
447
448 glBegin GL_QUADS;
449 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
450 glTexCoord 0, 1; glVertex 0 , $h;
451 glTexCoord 1, 1; glVertex $w , $h;
452 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
453 glEnd;
454 469
455 my $bg = $tex[0]; 470 my $bg = $tex[0];
456 glBindTexture GL_TEXTURE_2D, $bg->{name}; 471 glBindTexture GL_TEXTURE_2D, $bg->{name};
457 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 472 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
458 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 473 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
459 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 474 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
460 475
461 my $rep_x = $cw / $bg->{width}; 476 my $rep_x = $cw / $bg->{width};
462 my $rep_y = $ch / $bg->{height}; 477 my $rep_y = $ch / $bg->{height};
463 478
464 glBegin GL_QUADS; 479 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
465 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
466 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
467 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
468 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
469 glEnd;
470 480
471 glDisable GL_BLEND; 481 glDisable GL_BLEND;
472 glDisable GL_TEXTURE_2D; 482 glDisable GL_TEXTURE_2D;
473 483
474 $self->child->draw; 484 $self->child->draw;
475 485
476} 486}
477 487
478############################################################################# 488#############################################################################
479 489
480package Crossfire::Client::Widget::Table; 490package CFClient::Widget::Table;
481 491
482our @ISA = Crossfire::Client::Widget::Bin::; 492our @ISA = CFClient::Widget::Bin::;
483 493
484use SDL::OpenGL; 494use SDL::OpenGL;
485 495
486sub add { 496sub add {
487 my ($self, $x, $y, $chld) = @_; 497 my ($self, $x, $y, $chld) = @_;
562 } 572 }
563} 573}
564 574
565############################################################################# 575#############################################################################
566 576
567package Crossfire::Client::Widget::VBox; 577package CFClient::Widget::VBox;
568 578
569our @ISA = Crossfire::Client::Widget::Container::; 579our @ISA = CFClient::Widget::Container::;
570 580
571use SDL::OpenGL; 581use SDL::OpenGL;
572 582
573sub size_request { 583sub size_request {
574 my ($self) = @_; 584 my ($self) = @_;
622 } 632 }
623} 633}
624 634
625############################################################################# 635#############################################################################
626 636
627package Crossfire::Client::Widget::Label; 637package CFClient::Widget::Label;
628 638
629our @ISA = Crossfire::Client::Widget::; 639our @ISA = CFClient::Widget::;
630 640
631use SDL::OpenGL; 641use SDL::OpenGL;
632 642
633sub new { 643sub new {
634 my ($class, $x, $y, $z, $height, $text) = @_; 644 my ($class, %arg) = @_;
635 645
636 # TODO: color, and make height, xyz etc. optional 646 # TODO: color, and make height, xyz etc. optional
637 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 647 my $self = $class->SUPER::new (
648 color => [1, 1, 1],
649 height => $::FONTSIZE,
650 text => "",
651 layout => new CFClient::Layout,
652 %arg
653 );
638 654
639 $self->set_text ($text); 655 $self->set_text ($self->{text});
640 656
641 $self 657 $self
642} 658}
643 659
644sub set_text { 660sub set_text {
645 my ($self, $text) = @_; 661 my ($self, $text) = @_;
646 662
647 $self->{text} = $text; 663 $self->{text} = $text;
648 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 664 $self->{layout}->set_markup ($text);
649 665
650 $self->update; 666 delete $self->{texture};
651} 667}
652 668
653sub get_text { 669sub get_text {
654 my ($self, $text) = @_; 670 my ($self, $text) = @_;
655 671
657} 673}
658 674
659sub size_request { 675sub size_request {
660 my ($self) = @_; 676 my ($self) = @_;
661 677
662 ( 678 $self->{layout}->set_width;
679 $self->{layout}->set_height ($self->{height});
680 $self->{layout}->size
681# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
682# (
663 $self->{texture}{width}, 683# $self->{texture}{width},
664 $self->{texture}{height}, 684# $self->{texture}{height},
665 ) 685# )
686# } else {
687# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
688#
689# ($w, $h)
690# }
691}
692
693sub size_allocate {
694 my ($self, $w, $h) = @_;
695
696 $self->SUPER::size_allocate ($w, $h);
697 delete $self->{texture};
666} 698}
667 699
668sub _draw { 700sub _draw {
669 my ($self) = @_; 701 my ($self) = @_;
670 702
671 my $tex = $self->{texture}; 703 my $tex = $self->{texture} ||= do {
704 $self->{layout}->set_width ($self->{w});
705 new_from_layout CFClient::Texture $self->{layout};
706 };
672 707
673 glEnable GL_BLEND; 708 glEnable GL_BLEND;
674 glEnable GL_TEXTURE_2D; 709 glEnable GL_TEXTURE_2D;
675 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 710 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
676 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 711 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
677 glBindTexture GL_TEXTURE_2D, $tex->{name};
678 712
679 glColor 1, 0, 0, 1; # TODO color 713 glColor @{$self->{color}};
680 714
681 glBegin GL_QUADS; 715 $tex->draw_quad (0, 0);
682 glTexCoord 0, 0; glVertex 0 , 0;
683 glTexCoord 0, 1; glVertex 0 , $tex->{height};
684 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
685 glTexCoord 1, 0; glVertex $tex->{width}, 0;
686 glEnd;
687 716
688 glDisable GL_BLEND; 717 glDisable GL_BLEND;
689 glDisable GL_TEXTURE_2D; 718 glDisable GL_TEXTURE_2D;
690} 719}
691 720
692############################################################################# 721#############################################################################
693 722
694package Crossfire::Client::Widget::TextEntry; 723package CFClient::Widget::Entry;
695 724
696our @ISA = Crossfire::Client::Widget::Label::; 725our @ISA = CFClient::Widget::Label::;
697 726
698use SDL; 727use SDL;
699use SDL::OpenGL; 728use SDL::OpenGL;
700 729
701sub key_down { 730sub key_down {
702 my ($self, $ev) = @_; 731 my ($self, $ev) = @_;
703 732
704 my $mod = $ev->key_mod; 733 my $mod = $ev->key_mod;
705 my $sym = $ev->key_sym; 734 my $sym = $ev->key_sym;
706 735
707 $ev->set_unicode (1);
708 my $uni = $ev->key_unicode; 736 my $uni = $ev->key_unicode;
709 737
710 my $text = $self->get_text; 738 my $text = $self->get_text;
711 739
712 if ($sym == SDLK_BACKSPACE) { 740 if ($sym == SDLK_BACKSPACE) {
713 substr $text, -1, 1, ''; 741 substr $text, -1, 1, '';
714
715 } elsif ($uni) { 742 } elsif ($uni) {
716 $text .= chr $uni; 743 $text .= chr $uni;
717 } 744 }
745
718 $self->set_text ($text); 746 $self->set_text ($text);
719} 747}
720 748
721############################################################################# 749sub button_down {
750 my ($self, $ev) = @_;
722 751
752 $self->focus_in;
753}
754
755sub mouse_motion {
756 my ($self, $ev, $x, $y) = @_;
757 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
758}
759
760sub _draw {
761 my ($self) = @_;
762
763 if ($FOCUS == $self) {
764 glColor 1, 1, 1;
765 } else {
766 glColor 0.7, 0.7, 0.7;
767 }
768
769 glBegin GL_QUADS;
770 glVertex 0 , 0;
771 glVertex 0 , $self->{h} - 1;
772 glVertex $self->{w} - 1, $self->{h} - 1;
773 glVertex $self->{w} - 1, 0;
774 glEnd;
775
776 $self->SUPER::_draw;
777}
778
779#############################################################################
780
723package Crossfire::Client::Widget::MapWidget; 781package CFClient::Widget::MapWidget;
724 782
725use strict; 783use strict;
726 784
727use List::Util qw(min max); 785use List::Util qw(min max);
728 786
729use SDL; 787use SDL;
730use SDL::OpenGL; 788use SDL::OpenGL;
731use SDL::OpenGL::Constants; 789use SDL::OpenGL::Constants;
732 790
733our @ISA = Crossfire::Client::Widget::; 791our @ISA = CFClient::Widget::;
792
793sub new {
794 my $class = shift;
795
796 $class->SUPER::new (
797 z => -1,
798 list => (glGenLists 1),
799 @_
800 )
801}
734 802
735sub key_down { 803sub key_down {
736 print "MAPKEYDOWN\n"; 804 print "MAPKEYDOWN\n";
737} 805}
738 806
739sub key_up { 807sub key_up {
740} 808}
741 809
742sub size_request { 810sub size_request {
743 811 (
812 1 + int $::WIDTH / 32,
813 1 + int $::HEIGHT / 32,
814 )
744} 815}
745 816
746sub size_allocate { 817sub update {
818 my ($self) = @_;
819
820 $self->{need_update} = 1;
747} 821}
748 822
749sub _draw { 823sub _draw {
750 my ($self) = @_; 824 my ($self) = @_;
751 825
826 if (delete $self->{need_update}) {
827 glNewList $self->{list}, GL_COMPILE;
828
752 my $mx = $::CONN->{mapx}; 829 my $mx = $::CONN->{mapx};
753 my $my = $::CONN->{mapy}; 830 my $my = $::CONN->{mapy};
754 831
755 my $map = $::CONN->{map}; 832 my $map = $::CONN->{map};
756 833
757 my ($xofs, $yofs); 834 my ($xofs, $yofs);
758 835
759 my $sw = 1 + int $::WIDTH / 32; 836 my $sw = 1 + int $::WIDTH / 32;
760 my $sh = 1 + int $::HEIGHT / 32; 837 my $sh = 1 + int $::HEIGHT / 32;
761 838
762 if ($::CONN->{mapw} > $sw) { 839 if ($::CONN->{mapw} > $sw) {
763 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 840 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
764 } else { 841 } else {
765 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 842 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
766 } 843 }
767 844
768 if ($::CONN->{maph} > $sh) { 845 if ($::CONN->{maph} > $sh) {
769 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 846 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
770 } else { 847 } else {
771 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 848 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
772 } 849 }
773 850
774 glEnable GL_TEXTURE_2D; 851 glEnable GL_TEXTURE_2D;
775 glEnable GL_BLEND; 852 glEnable GL_BLEND;
776 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 853 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
777 854
778 my $sw4 = ($sw + 3) & ~3; 855 my $sw4 = ($sw + 3) & ~3;
779 my $lighting = "\x00" x ($sw4 * $sh); 856 my $darkness = "\x00" x ($sw4 * $sh);
780 857
781 for my $x (0 .. $sw - 1) { 858 for my $x (0 .. $sw - 1) {
859 my $row = $map->[$x + $xofs];
782 for my $y (0 .. $sh - 1) { 860 for my $y (0 .. $sh - 1) {
783 861
784 my $cell = $map->[$x + $xofs][$y + $yofs] 862 my $cell = $row->[$y + $yofs]
785 or next; 863 or next;
786 864
787 my $darkness = $cell->[0] * (1 / 255); 865 my $dark = $cell->[0];
788 if ($darkness < 0) { 866 if ($dark < 0) {
789 $darkness = 0.15; 867 substr $darkness, $y * $sw4 + $x, 1, chr 224;
868 } else {
869 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
790 } 870 }
791 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
792 871
793 for my $num (grep $_, @$cell[1,2,3]) { 872 for my $num (grep $_, @$cell[1,2,3]) {
794 my $tex = $::CONN->{face}[$num]{texture} || next; 873 my $tex = $::CONN->{face}[$num]{texture} || next;
795 874
796 glBindTexture GL_TEXTURE_2D, $tex->{name};
797
798 my $w = $tex->{width}; 875 my $w = $tex->{width};
799 my $h = $tex->{height}; 876 my $h = $tex->{height};
800 877
801 my $px = ($x + 1) * 32 - $w; 878 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
802 my $py = ($y + 1) * 32 - $h;
803
804 glBegin GL_QUADS;
805 glTexCoord 0, 0; glVertex $px , $py;
806 glTexCoord 0, 1; glVertex $px , $py + $h;
807 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
808 glTexCoord 1, 0; glVertex $px + $w, $py;
809 glEnd; 879 }
810 } 880 }
811 } 881 }
812 }
813 882
814# if (1) { # higher quality darkness 883# if (1) { # higher quality darkness
815# $lighting =~ s/(.)/$1$1$1/gs; 884# $lighting =~ s/(.)/$1$1$1/gs;
816# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; 885# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
817# 886#
819# 888#
820# $lighting = $pb->get_pixels; 889# $lighting = $pb->get_pixels;
821# $lighting =~ s/(.)../$1/gs; 890# $lighting =~ s/(.)../$1/gs;
822# } 891# }
823 892
824 $lighting = new Crossfire::Client::Texture
825 width => $sw4,
826 height => $sh,
827 data => $lighting,
828 internalformat => GL_ALPHA4,
829 format => GL_ALPHA;
830
831 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 893 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
832 glColor 0, 0, 0, 0.75;
833 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 894 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
834 glBindTexture GL_TEXTURE_2D, $lighting->{name};
835 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
836 glBegin GL_QUADS;
837 glTexCoord 0, 0; glVertex 0 , 0;
838 glTexCoord 0, 1; glVertex 0 , $sh * 32;
839 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
840 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
841 glEnd;
842 895
896 $darkness = new CFClient::Texture
897 width => $sw4,
898 height => $sh,
899 data => $darkness,
900 internalformat => GL_ALPHA,
901 format => GL_ALPHA;
902
903 glColor 0.45, 0.45, 0.45, 1;
904 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
905
843 glDisable GL_TEXTURE_2D; 906 glDisable GL_TEXTURE_2D;
844 glDisable GL_BLEND; 907 glDisable GL_BLEND;
908
909 glEndList;
910 }
911
912 glCallList $self->{list};
845} 913}
846 914
847my %DIR = ( 915my %DIR = (
848 SDLK_KP8, [1, "north"], 916 SDLK_KP8, [1, "north"],
849 SDLK_KP9, [2, "northeast"], 917 SDLK_KP9, [2, "northeast"],
895 } 963 }
896} 964}
897 965
898############################################################################# 966#############################################################################
899 967
900package Crossfire::Client::Widget::Animator; 968package CFClient::Widget::Animator;
901 969
902use SDL::OpenGL; 970use SDL::OpenGL;
903 971
904our @ISA = Crossfire::Client::Widget::Bin::; 972our @ISA = CFClient::Widget::Bin::;
905 973
906sub moveto { 974sub moveto {
907 my ($self, $x, $y) = @_; 975 my ($self, $x, $y) = @_;
908 976
909 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 977 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
910 $self->{speed} = 0.2; 978 $self->{speed} = 0.001;
911 $self->{time} = 1; 979 $self->{time} = 1;
912 980
913 ::animation_start $self; 981 ::animation_start $self;
914} 982}
915 983
930 998
931sub _draw { 999sub _draw {
932 my ($self) = @_; 1000 my ($self) = @_;
933 1001
934 glPushMatrix; 1002 glPushMatrix;
935 glRotate $self->{time} * 10000, 0, 1, 0; 1003 glRotate $self->{time} * 1000, 0, 1, 0;
936 $self->{children}[0]->draw; 1004 $self->{children}[0]->draw;
937 glPopMatrix; 1005 glPopMatrix;
938} 1006}
939 1007
9401; 1008#############################################################################
941 1009
1010package CFClient::Widget::Toplevel;
1011
1012our @ISA = CFClient::Widget::Container::;
1013
1014sub size_request {
1015 ($::WIDTH, $::HEIGHT)
1016}
1017
1018sub size_allocate {
1019 my ($self, $w, $h) = @_;
1020
1021 $self->SUPER::size_allocate ($w, $h);
1022
1023 $_->size_allocate ($_->size_request)
1024 for @{$self->{children}};
1025}
1026
1027sub translate {
1028 my ($self, $x, $y) = @_;
1029
1030 ($x, $y)
1031}
1032
1033sub update {
1034 my ($self) = @_;
1035
1036 $self->size_allocate ($self->size_request);
1037 ::refresh ();
1038}
1039
1040sub add {
1041 my ($self, $widget) = @_;
1042
1043 $self->SUPER::add ($widget);
1044
1045 $widget->size_allocate ($widget->size_request);
1046}
1047
1048sub draw {
1049 my ($self) = @_;
1050
1051 $self->_draw;
1052}
1053
1054#############################################################################
1055
1056package CFClient::Widget;
1057
1058$TOPLEVEL = new CFClient::Widget::Toplevel;
1059
10601
1061

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines