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.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 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 {
79 x => 0,
80 y => 0,
81 z => 0,
82 @_
83 }, $class
24} 84}
25 85
26sub move { 86sub move {
27 my ($self, $x, $y, $z) = @_; 87 my ($self, $x, $y, $z) = @_;
28 $self->{x} = $x; 88 $self->{x} = $x;
44 104
45 $self->{w} = $w; 105 $self->{w} = $w;
46 $self->{h} = $h; 106 $self->{h} = $h;
47} 107}
48 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
49sub focus_in { 116sub focus_in {
50 my ($widget) = @_; 117 my ($self) = @_;
51 $FOCUS = $widget; 118
119 my $focus = $FOCUS; $FOCUS = $self;
120 $focus->update if $focus;
121 $FOCUS->update;
52} 122}
53 123
54sub focus_out { 124sub focus_out {
55 my ($widget) = @_; 125 my ($self) = @_;
56}
57 126
58sub key_down { 127 return unless $FOCUS == $self;
59 my ($widget, $sdlev) = @_;
60}
61 128
62sub key_up { 129 my $focus = $FOCUS; undef $FOCUS;
63 my ($widget, $sdlev) = @_; 130 $focus->update if $focus; #?
64} 131}
65 132
133sub mouse_motion { }
134sub button_up { }
66sub button_down { 135sub button_down { }
67 my ($widget, $sdlev) = @_; 136sub key_down { }
68} 137sub key_up { }
69 138
70sub button_up {
71 my ($widget, $sdlev) = @_;
72}
73
74sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 139sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
75sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 140sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
76sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 141sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
77sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 142sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
78sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 143sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
79 144
80sub draw { 145sub draw {
81 my ($self) = @_; 146 my ($self) = @_;
82 147
83 glPushMatrix; 148 glPushMatrix;
84 glTranslate $self->{x}, $self->{y}, 0; 149 glTranslate $self->{x}, $self->{y}, 0;
85 $self->_draw; 150 $self->_draw;
151 if ($self == $HOVER) {
152 glColor 1, 1, 1, 0.4;
153 glEnable GL_BLEND;
154 glBegin GL_QUADS;
155 glVertex 0 , 0;
156 glVertex $self->{w}, 0;
157 glVertex $self->{w}, $self->{h};
158 glVertex 0 , $self->{h};
159 glEnd;
160 glDisable GL_BLEND;
161 }
86 glPopMatrix; 162 glPopMatrix;
87} 163}
88 164
89sub _draw { 165sub _draw {
90 my ($self) = @_; 166 my ($self) = @_;
139 #$self->deactivate; 215 #$self->deactivate;
140} 216}
141 217
142############################################################################# 218#############################################################################
143 219
144package Crossfire::Client::Widget::Container; 220package CFClient::Widget::Container;
145 221
146our @ISA = Crossfire::Client::Widget::; 222our @ISA = CFClient::Widget::;
147 223
148sub new { 224sub new {
149 my ($class, @widgets) = @_; 225 my ($class, %arg) = @_;
150 226
227 my $children = delete $arg{children} || [];
228
151 my $self = $class->SUPER::new (children => []); 229 my $self = $class->SUPER::new (children => [], %arg);
152 $self->add ($_) for @widgets; 230 $self->add ($_) for @$children;
153 231
154 $self 232 $self
155} 233}
156 234
157sub add { 235sub add {
177} 255}
178 256
179sub find_widget { 257sub find_widget {
180 my ($self, $x, $y) = @_; 258 my ($self, $x, $y) = @_;
181 259
260 $x -= $self->{x};
261 $y -= $self->{y};
262
182 my $res; 263 my $res;
183 264
184 for (@{ $self->{children} }) { 265 for (reverse @{ $self->{children} }) {
185 $res = $_->find_widget ($x, $y) 266 $res = $_->find_widget ($x, $y)
186 and return $res; 267 and return $res;
187 } 268 }
188 269
189 () 270 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
190} 271}
191 272
192sub _draw { 273sub _draw {
193 my ($self) = @_; 274 my ($self) = @_;
194 275
195 $_->draw for @{$self->{children}}; 276 $_->draw for @{$self->{children}};
196} 277}
197 278
198############################################################################# 279#############################################################################
199 280
200package Crossfire::Client::Widget::Bin; 281package CFClient::Widget::Bin;
201 282
202our @ISA = Crossfire::Client::Widget::Container::; 283our @ISA = CFClient::Widget::Container::;
203 284
204sub child { $_[0]->{children}[0] } 285sub child { $_[0]->{children}[0] }
205 286
206sub size_request { 287sub size_request {
207 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 288 $_[0]{children}[0]->size_request if $_[0]{children}[0];
215 if $self->{children}[0] 296 if $self->{children}[0]
216} 297}
217 298
218############################################################################# 299#############################################################################
219 300
220package Crossfire::Client::Widget::Toplevel; 301package CFClient::Widget::Window;
221 302
222our @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}
223 312
224sub update { 313sub update {
225 my ($self) = @_; 314 my ($self) = @_;
226 315
227 ::refresh (); 316 # we want to do this delayed...
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;
241
242our @ISA = Crossfire::Client::Widget::Bin::;
243
244use SDL::OpenGL;
245
246sub new {
247 my ($class, $x, $y, $z, $w, $h) = @_;
248
249 my $self = $class->SUPER::new;
250
251 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
252}
253
254sub update {
255 my ($self) = @_;
256
257 $self->render_chld; 317 $self->render_chld;
258 $self->SUPER::update; 318 $self->SUPER::update;
259} 319}
260 320
261sub render_chld { 321sub render_chld {
262 my ($self) = @_; 322 my ($self) = @_;
263 323
264 $self->{texture} = 324 $self->{texture} =
265 Crossfire::Client::Texture->new_from_opengl ( 325 CFClient::Texture->new_from_opengl (
266 $self->{w}, $self->{h}, sub { $self->child->draw } 326 $self->{w}, $self->{h}, sub { $self->child->draw }
267 ); 327 );
268} 328}
269 329
270sub size_allocate { 330sub size_allocate {
287 or return; 347 or return;
288 348
289 glEnable GL_BLEND; 349 glEnable GL_BLEND;
290 glEnable GL_TEXTURE_2D; 350 glEnable GL_TEXTURE_2D;
291 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 351 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
292 glBindTexture GL_TEXTURE_2D, $tex->{name};
293 352
294 glBegin GL_QUADS; 353 $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 354
301 glDisable GL_BLEND; 355 glDisable GL_BLEND;
302 glDisable GL_TEXTURE_2D; 356 glDisable GL_TEXTURE_2D;
303} 357}
304 358
305############################################################################# 359#############################################################################
306 360
307package Crossfire::Client::Widget::Frame; 361package CFClient::Widget::Frame;
308 362
309our @ISA = Crossfire::Client::Widget::Bin::; 363our @ISA = CFClient::Widget::Bin::;
310 364
311use SDL::OpenGL; 365use SDL::OpenGL;
312 366
313sub size_request { 367sub size_request {
314 my ($self) = @_; 368 my ($self) = @_;
337 391
338 my ($w, $h) = $chld->size_request; 392 my ($w, $h) = $chld->size_request;
339 393
340 glBegin GL_QUADS; 394 glBegin GL_QUADS;
341 glColor 0, 0, 0; 395 glColor 0, 0, 0;
342 glTexCoord 0, 0; glVertex 0 , 0; 396 glVertex 0 , 0;
343 glTexCoord 0, 1; glVertex 0 , $h + 4; 397 glVertex 0 , $h + 4;
344 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 398 glVertex $w + 4 , $h + 4;
345 glTexCoord 1, 0; glVertex $w + 4 , 0; 399 glVertex $w + 4 , 0;
346 glEnd; 400 glEnd;
347 401
348 $chld->draw; 402 $chld->draw;
349} 403}
350 404
351############################################################################# 405#############################################################################
352 406
353package Crossfire::Client::Widget::FancyFrame; 407package CFClient::Widget::FancyFrame;
354 408
355our @ISA = Crossfire::Client::Widget::Bin::; 409our @ISA = CFClient::Widget::Bin::;
356 410
357use SDL::OpenGL; 411use SDL::OpenGL;
358 412
359my @tex = 413my @tex =
360 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 414 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); 415 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
362 416
363sub size_request { 417sub size_request {
364 my ($self) = @_; 418 my ($self) = @_;
365 419
400 glEnable GL_TEXTURE_2D; 454 glEnable GL_TEXTURE_2D;
401 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 455 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
402 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 456 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
403 457
404 my $top = $tex[1]; 458 my $top = $tex[1];
405 glBindTexture GL_TEXTURE_2D, $top->{name}; 459 $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 460
414 my $left = $tex[3]; 461 my $left = $tex[3];
415 glBindTexture GL_TEXTURE_2D, $left->{name}; 462 $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 463
424 my $right = $tex[2]; 464 my $right = $tex[2];
425 glBindTexture GL_TEXTURE_2D, $right->{name}; 465 $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 466
434 my $bottom = $tex[4]; 467 my $bottom = $tex[4];
435 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 468 $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 469
444 my $bg = $tex[0]; 470 my $bg = $tex[0];
445 glBindTexture GL_TEXTURE_2D, $bg->{name}; 471 glBindTexture GL_TEXTURE_2D, $bg->{name};
446 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 472 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
447 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 473 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
448 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 474 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
449 475
450 my $rep_x = $cw / $bg->{width}; 476 my $rep_x = $cw / $bg->{width};
451 my $rep_y = $ch / $bg->{height}; 477 my $rep_y = $ch / $bg->{height};
452 478
453 glBegin GL_QUADS; 479 $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 480
460 glDisable GL_BLEND; 481 glDisable GL_BLEND;
461 glDisable GL_TEXTURE_2D; 482 glDisable GL_TEXTURE_2D;
462 483
463 $self->child->draw; 484 $self->child->draw;
464 485
465} 486}
466 487
467############################################################################# 488#############################################################################
468 489
469package Crossfire::Client::Widget::Table; 490package CFClient::Widget::Table;
470 491
471our @ISA = Crossfire::Client::Widget::Bin::; 492our @ISA = CFClient::Widget::Bin::;
472 493
473use SDL::OpenGL; 494use SDL::OpenGL;
474 495
475sub add { 496sub add {
476 my ($self, $x, $y, $chld) = @_; 497 my ($self, $x, $y, $chld) = @_;
551 } 572 }
552} 573}
553 574
554############################################################################# 575#############################################################################
555 576
556package Crossfire::Client::Widget::VBox; 577package CFClient::Widget::VBox;
557 578
558our @ISA = Crossfire::Client::Widget::Container::; 579our @ISA = CFClient::Widget::Container::;
559 580
560use SDL::OpenGL; 581use SDL::OpenGL;
561 582
562sub size_request { 583sub size_request {
563 my ($self) = @_; 584 my ($self) = @_;
611 } 632 }
612} 633}
613 634
614############################################################################# 635#############################################################################
615 636
616package Crossfire::Client::Widget::Label; 637package CFClient::Widget::Label;
617 638
618our @ISA = Crossfire::Client::Widget::; 639our @ISA = CFClient::Widget::;
619 640
620use SDL::OpenGL; 641use SDL::OpenGL;
621 642
622sub new { 643sub new {
623 my ($class, $x, $y, $z, $height, $text) = @_; 644 my ($class, %arg) = @_;
624 645
625 # TODO: color, and make height, xyz etc. optional 646 # TODO: color, and make height, xyz etc. optional
626 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 );
627 654
628 $self->set_text ($text); 655 $self->set_text ($self->{text});
629 656
630 $self 657 $self
631} 658}
632 659
633sub set_text { 660sub set_text {
634 my ($self, $text) = @_; 661 my ($self, $text) = @_;
635 662
636 $self->{text} = $text; 663 $self->{text} = $text;
637 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 664 $self->{layout}->set_markup ($text);
638 665
639 $self->update; 666 delete $self->{texture};
640} 667}
641 668
642sub get_text { 669sub get_text {
643 my ($self, $text) = @_; 670 my ($self, $text) = @_;
644 671
646} 673}
647 674
648sub size_request { 675sub size_request {
649 my ($self) = @_; 676 my ($self) = @_;
650 677
651 ( 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# (
652 $self->{texture}{width}, 683# $self->{texture}{width},
653 $self->{texture}{height}, 684# $self->{texture}{height},
654 ) 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};
655} 698}
656 699
657sub _draw { 700sub _draw {
658 my ($self) = @_; 701 my ($self) = @_;
659 702
660 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 };
661 707
662 glEnable GL_BLEND; 708 glEnable GL_BLEND;
663 glEnable GL_TEXTURE_2D; 709 glEnable GL_TEXTURE_2D;
664 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 710 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
665 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 711 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
666 glBindTexture GL_TEXTURE_2D, $tex->{name};
667 712
668 glColor 1, 0, 0, 1; # TODO color 713 glColor @{$self->{color}};
669 714
670 glBegin GL_QUADS; 715 $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 716
677 glDisable GL_BLEND; 717 glDisable GL_BLEND;
678 glDisable GL_TEXTURE_2D; 718 glDisable GL_TEXTURE_2D;
679} 719}
680 720
681############################################################################# 721#############################################################################
682 722
683package Crossfire::Client::Widget::TextEntry; 723package CFClient::Widget::Entry;
684 724
685our @ISA = Crossfire::Client::Widget::Label::; 725our @ISA = CFClient::Widget::Label::;
686 726
687use SDL; 727use SDL;
688use SDL::OpenGL; 728use SDL::OpenGL;
689 729
690sub key_down { 730sub key_down {
691 my ($self, $ev) = @_; 731 my ($self, $ev) = @_;
692 732
693 my $mod = $ev->key_mod; 733 my $mod = $ev->key_mod;
694 my $sym = $ev->key_sym; 734 my $sym = $ev->key_sym;
695 735
696 $ev->set_unicode (1);
697 my $uni = $ev->key_unicode; 736 my $uni = $ev->key_unicode;
698 737
699 my $text = $self->get_text; 738 my $text = $self->get_text;
700 739
701 if ($sym == SDLK_BACKSPACE) { 740 if ($sym == SDLK_BACKSPACE) {
702 substr $text, -1, 1, ''; 741 substr $text, -1, 1, '';
703
704 } elsif ($uni) { 742 } elsif ($uni) {
705 $text .= chr $uni; 743 $text .= chr $uni;
706 } 744 }
745
707 $self->set_text ($text); 746 $self->set_text ($text);
708} 747}
709 748
710############################################################################# 749sub button_down {
750 my ($self, $ev) = @_;
711 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
712package Crossfire::Client::Widget::MapWidget; 781package CFClient::Widget::MapWidget;
713 782
714use strict; 783use strict;
715 784
716use List::Util qw(min max); 785use List::Util qw(min max);
717 786
718use SDL; 787use SDL;
719use SDL::OpenGL; 788use SDL::OpenGL;
720use SDL::OpenGL::Constants; 789use SDL::OpenGL::Constants;
721 790
722our @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}
723 802
724sub key_down { 803sub key_down {
725 print "MAPKEYDOWN\n"; 804 print "MAPKEYDOWN\n";
726} 805}
727 806
728sub key_up { 807sub key_up {
729} 808}
730 809
731sub size_request { 810sub size_request {
732 811 (
812 1 + int $::WIDTH / 32,
813 1 + int $::HEIGHT / 32,
814 )
733} 815}
734 816
735sub size_allocate { 817sub update {
818 my ($self) = @_;
819
820 $self->{need_update} = 1;
736} 821}
737 822
738sub _draw { 823sub _draw {
739 my ($self) = @_; 824 my ($self) = @_;
740 825
826 if (delete $self->{need_update}) {
827 glNewList $self->{list}, GL_COMPILE;
828
741 my $mx = $::CONN->{mapx}; 829 my $mx = $::CONN->{mapx};
742 my $my = $::CONN->{mapy}; 830 my $my = $::CONN->{mapy};
743 831
744 my $map = $::CONN->{map}; 832 my $map = $::CONN->{map};
745 833
746 my ($xofs, $yofs); 834 my ($xofs, $yofs);
747 835
748 my $sw = 1 + int $::WIDTH / 32; 836 my $sw = 1 + int $::WIDTH / 32;
749 my $sh = 1 + int $::HEIGHT / 32; 837 my $sh = 1 + int $::HEIGHT / 32;
750 838
751 if ($::CONN->{mapw} > $sw) { 839 if ($::CONN->{mapw} > $sw) {
752 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 840 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
753 } else { 841 } else {
754 $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};
755 } 843 }
756 844
757 if ($::CONN->{maph} > $sh) { 845 if ($::CONN->{maph} > $sh) {
758 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 846 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
759 } else { 847 } else {
760 $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};
761 } 849 }
762 850
763 glEnable GL_TEXTURE_2D; 851 glEnable GL_TEXTURE_2D;
764 glEnable GL_BLEND; 852 glEnable GL_BLEND;
765 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 853 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
766 854
767 my $sw4 = ($sw + 3) & ~3; 855 my $sw4 = ($sw + 3) & ~3;
768 my $lighting = "\x00" x ($sw4 * $sh); 856 my $darkness = "\x00" x ($sw4 * $sh);
769 857
770 for my $x (0 .. $sw - 1) { 858 for my $x (0 .. $sw - 1) {
859 my $row = $map->[$x + $xofs];
771 for my $y (0 .. $sh - 1) { 860 for my $y (0 .. $sh - 1) {
772 861
773 my $cell = $map->[$x + $xofs][$y + $yofs] 862 my $cell = $row->[$y + $yofs]
774 or next; 863 or next;
775 864
776 my $darkness = $cell->[0] * (1 / 255); 865 my $dark = $cell->[0];
777 if ($darkness < 0) { 866 if ($dark < 0) {
778 $darkness = 0.15; 867 substr $darkness, $y * $sw4 + $x, 1, chr 224;
868 } else {
869 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
779 } 870 }
780 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
781 871
782 for my $num (grep $_, @$cell[1,2,3]) { 872 for my $num (grep $_, @$cell[1,2,3]) {
783 my $tex = $::CONN->{face}[$num]{texture} || next; 873 my $tex = $::CONN->{face}[$num]{texture} || next;
784 874
785 glBindTexture GL_TEXTURE_2D, $tex->{name};
786
787 my $w = $tex->{width}; 875 my $w = $tex->{width};
788 my $h = $tex->{height}; 876 my $h = $tex->{height};
789 877
790 my $px = ($x + 1) * 32 - $w; 878 $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; 879 }
799 } 880 }
800 } 881 }
801 }
802 882
803# if (1) { # higher quality darkness 883# if (1) { # higher quality darkness
804# $lighting =~ s/(.)/$1$1$1/gs; 884# $lighting =~ s/(.)/$1$1$1/gs;
805# 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;
806# 886#
808# 888#
809# $lighting = $pb->get_pixels; 889# $lighting = $pb->get_pixels;
810# $lighting =~ s/(.)../$1/gs; 890# $lighting =~ s/(.)../$1/gs;
811# } 891# }
812 892
813 $lighting = new Crossfire::Client::Texture
814 width => $sw4,
815 height => $sh,
816 data => $lighting,
817 internalformat => GL_ALPHA4,
818 format => GL_ALPHA;
819
820 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 893 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
821 glColor 0, 0, 0, 0.75;
822 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 894 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 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
832 glDisable GL_TEXTURE_2D; 906 glDisable GL_TEXTURE_2D;
833 glDisable GL_BLEND; 907 glDisable GL_BLEND;
908
909 glEndList;
910 }
911
912 glCallList $self->{list};
834} 913}
835 914
836my %DIR = ( 915my %DIR = (
837 SDLK_KP8, [1, "north"], 916 SDLK_KP8, [1, "north"],
838 SDLK_KP9, [2, "northeast"], 917 SDLK_KP9, [2, "northeast"],
884 } 963 }
885} 964}
886 965
887############################################################################# 966#############################################################################
888 967
889package Crossfire::Client::Widget::Animator; 968package CFClient::Widget::Animator;
890 969
891use SDL::OpenGL; 970use SDL::OpenGL;
892 971
893our @ISA = Crossfire::Client::Widget::Bin::; 972our @ISA = CFClient::Widget::Bin::;
894 973
895sub moveto { 974sub moveto {
896 my ($self, $x, $y) = @_; 975 my ($self, $x, $y) = @_;
897 976
898 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 977 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
899 $self->{speed} = 0.2; 978 $self->{speed} = 0.001;
900 $self->{time} = 1; 979 $self->{time} = 1;
901 980
902 ::animation_start $self; 981 ::animation_start $self;
903} 982}
904 983
919 998
920sub _draw { 999sub _draw {
921 my ($self) = @_; 1000 my ($self) = @_;
922 1001
923 glPushMatrix; 1002 glPushMatrix;
924 glRotate $self->{time} * 10000, 0, 1, 0; 1003 glRotate $self->{time} * 1000, 0, 1, 0;
925 $self->{children}[0]->draw; 1004 $self->{children}[0]->draw;
926 glPopMatrix; 1005 glPopMatrix;
927} 1006}
928 1007
9291; 1008#############################################################################
930 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