ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.46 by root, Sun Apr 9 22:21:02 2006 UTC vs.
Revision 1.66 by root, Tue Apr 11 14:36:02 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
220package CFClient::Widget::Empty;
221
222our @ISA = CFClient::Widget::;
223
224sub size_request {
225 (0, 0)
226}
227
228sub draw {
229}
230
231#############################################################################
232
144package Crossfire::Client::Widget::Container; 233package CFClient::Widget::Container;
145 234
146our @ISA = Crossfire::Client::Widget::; 235our @ISA = CFClient::Widget::;
147 236
148sub new { 237sub new {
149 my ($class, @widgets) = @_; 238 my ($class, %arg) = @_;
150 239
240 my $children = delete $arg{children} || [];
241
151 my $self = $class->SUPER::new (children => []); 242 my $self = $class->SUPER::new (children => [], %arg);
152 $self->add ($_) for @widgets; 243 $self->add ($_) for @$children;
153 244
154 $self 245 $self
155} 246}
156 247
157sub add { 248sub add {
158 my ($self, $chld, $expand) = @_; 249 my ($self, $chld, $expand) = @_;
159 250
160 $chld->{expand} = $expand; 251 $chld->{expand} = $expand;
161 $chld->set_parent ($self); 252 $chld->set_parent ($self);
162 253
163 @{$self->{children}} = 254 $self->{children} = [
164 sort { $a->{z} <=> $b->{z} } 255 sort { $a->{z} <=> $b->{z} }
165 @{$self->{children}}, $chld; 256 @{$self->{children}}, $chld
257 ];
166 258
167 $self->size_allocate ($self->{w}, $self->{h}) 259 $self->size_allocate ($self->{w}, $self->{h})
168 if $self->{w}; #TODO: check for "realised state" 260 if $self->{w}; #TODO: check for "realised state"
169} 261}
170 262
198 $_->draw for @{$self->{children}}; 290 $_->draw for @{$self->{children}};
199} 291}
200 292
201############################################################################# 293#############################################################################
202 294
203package Crossfire::Client::Widget::Bin; 295package CFClient::Widget::Bin;
204 296
205our @ISA = Crossfire::Client::Widget::Container::; 297our @ISA = CFClient::Widget::Container::;
298
299sub new {
300 my ($class, %arg) = @_;
301
302 my $child = (delete $arg{child}) || new CFClient::Widget::Empty::;
303
304 $class->SUPER::new (children => [$child], %arg)
305}
306
307sub add {
308 my ($self, $widget) = @_;
309
310 $self->{children} = [];
311
312 $self->SUPER::add ($widget);
313}
314
315sub remove {
316 my ($self, $widget) = @_;
317
318 $self->SUPER::remove ($widget);
319
320 $self->{children} = [new CFClient::Widget::Empty]
321 unless @{$self->{children}};
322}
206 323
207sub child { $_[0]->{children}[0] } 324sub child { $_[0]->{children}[0] }
208 325
209sub size_request { 326sub size_request {
210 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 327 $_[0]{children}[0]->size_request if $_[0]{children}[0];
218 if $self->{children}[0] 335 if $self->{children}[0]
219} 336}
220 337
221############################################################################# 338#############################################################################
222 339
223package Crossfire::Client::Widget::Toplevel; 340package CFClient::Widget::Window;
224 341
225our @ISA = Crossfire::Client::Widget::Container::; 342our @ISA = CFClient::Widget::Bin::;
343
344use SDL::OpenGL;
345
346sub new {
347 my ($class, %arg) = @_;
348
349 my $self = $class->SUPER::new (%arg);
350}
226 351
227sub update { 352sub update {
228 my ($self) = @_; 353 my ($self) = @_;
229 354
230 ::refresh (); 355 # we want to do this delayed...
231}
232
233sub add {
234 my ($self, $widget) = @_;
235
236 $self->SUPER::add ($widget);
237
238 $widget->size_allocate ($widget->size_request);
239}
240
241#############################################################################
242
243package Crossfire::Client::Widget::Window;
244
245our @ISA = Crossfire::Client::Widget::Bin::;
246
247use SDL::OpenGL;
248
249sub new {
250 my ($class, $x, $y, $z, $w, $h) = @_;
251
252 my $self = $class->SUPER::new;
253
254 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
255}
256
257sub update {
258 my ($self) = @_;
259
260 $self->render_chld; 356 $self->render_chld;
261 $self->SUPER::update; 357 $self->SUPER::update;
262} 358}
263 359
264sub render_chld { 360sub render_chld {
265 my ($self) = @_; 361 my ($self) = @_;
266 362
267 $self->{texture} = 363 $self->{texture} =
268 Crossfire::Client::Texture->new_from_opengl ( 364 CFClient::Texture->new_from_opengl (
269 $self->{w}, $self->{h}, sub { $self->child->draw } 365 $self->{w}, $self->{h}, sub { $self->child->draw }
270 ); 366 );
271} 367}
272 368
273sub size_allocate { 369sub size_allocate {
290 or return; 386 or return;
291 387
292 glEnable GL_BLEND; 388 glEnable GL_BLEND;
293 glEnable GL_TEXTURE_2D; 389 glEnable GL_TEXTURE_2D;
294 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 390 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
295 glBindTexture GL_TEXTURE_2D, $tex->{name};
296 391
297 glBegin GL_QUADS; 392 $tex->draw_quad (0, 0, $w, $h);
298 glTexCoord 0, 0; glVertex 0, 0;
299 glTexCoord 0, 1; glVertex 0, $h;
300 glTexCoord 1, 1; glVertex $w, $h;
301 glTexCoord 1, 0; glVertex $w, 0;
302 glEnd;
303 393
304 glDisable GL_BLEND; 394 glDisable GL_BLEND;
305 glDisable GL_TEXTURE_2D; 395 glDisable GL_TEXTURE_2D;
306} 396}
307 397
308############################################################################# 398#############################################################################
309 399
310package Crossfire::Client::Widget::Frame; 400package CFClient::Widget::Frame;
311 401
312our @ISA = Crossfire::Client::Widget::Bin::; 402our @ISA = CFClient::Widget::Bin::;
313 403
314use SDL::OpenGL; 404use SDL::OpenGL;
315 405
316sub size_request { 406sub size_request {
317 my ($self) = @_; 407 my ($self) = @_;
340 430
341 my ($w, $h) = $chld->size_request; 431 my ($w, $h) = $chld->size_request;
342 432
343 glBegin GL_QUADS; 433 glBegin GL_QUADS;
344 glColor 0, 0, 0; 434 glColor 0, 0, 0;
345 glTexCoord 0, 0; glVertex 0 , 0; 435 glVertex 0 , 0;
346 glTexCoord 0, 1; glVertex 0 , $h + 4; 436 glVertex 0 , $h + 4;
347 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 437 glVertex $w + 4 , $h + 4;
348 glTexCoord 1, 0; glVertex $w + 4 , 0; 438 glVertex $w + 4 , 0;
349 glEnd; 439 glEnd;
350 440
351 $chld->draw; 441 $chld->draw;
352} 442}
353 443
354############################################################################# 444#############################################################################
355 445
356package Crossfire::Client::Widget::FancyFrame; 446package CFClient::Widget::FancyFrame;
357 447
358our @ISA = Crossfire::Client::Widget::Bin::; 448our @ISA = CFClient::Widget::Bin::;
359 449
360use SDL::OpenGL; 450use SDL::OpenGL;
361 451
362my @tex = 452my @tex =
363 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 453 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
364 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 454 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
365 455
366sub size_request { 456sub size_request {
367 my ($self) = @_; 457 my ($self) = @_;
368 458
387 $w -= $tex[3]->{width}; 477 $w -= $tex[3]->{width};
388 478
389 $h = $h < 0 ? 0 : $h; 479 $h = $h < 0 ? 0 : $h;
390 $w = $w < 0 ? 0 : $w; 480 $w = $w < 0 ? 0 : $w;
391 481
482 my $child = $self->child;
483
392 $self->child->size_allocate ($w, $h); 484 $child->size_allocate ($w, $h);
393 $self->child->move ($tex[3]->{width}, $tex[1]->{height}); 485 $child->move ($tex[3]->{width}, $tex[1]->{height});
394} 486}
395 487
396sub _draw { 488sub _draw {
397 my ($self) = @_; 489 my ($self) = @_;
398 490
403 glEnable GL_TEXTURE_2D; 495 glEnable GL_TEXTURE_2D;
404 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 496 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
405 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 497 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
406 498
407 my $top = $tex[1]; 499 my $top = $tex[1];
408 glBindTexture GL_TEXTURE_2D, $top->{name}; 500 $top->draw_quad (0, 0, $w, $top->{height});
409
410 glBegin GL_QUADS;
411 glTexCoord 0, 0; glVertex 0 , 0;
412 glTexCoord 0, 1; glVertex 0 , $top->{height};
413 glTexCoord 1, 1; glVertex $w , $top->{height};
414 glTexCoord 1, 0; glVertex $w , 0;
415 glEnd;
416 501
417 my $left = $tex[3]; 502 my $left = $tex[3];
418 glBindTexture GL_TEXTURE_2D, $left->{name}; 503 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
419
420 glBegin GL_QUADS;
421 glTexCoord 0, 0; glVertex 0 , $top->{height};
422 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
423 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
424 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
425 glEnd;
426 504
427 my $right = $tex[2]; 505 my $right = $tex[2];
428 glBindTexture GL_TEXTURE_2D, $right->{name}; 506 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
429
430 glBegin GL_QUADS;
431 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
432 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
433 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
434 glTexCoord 1, 0; glVertex $w , $top->{height};
435 glEnd;
436 507
437 my $bottom = $tex[4]; 508 my $bottom = $tex[4];
438 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 509 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
439
440 glBegin GL_QUADS;
441 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
442 glTexCoord 0, 1; glVertex 0 , $h;
443 glTexCoord 1, 1; glVertex $w , $h;
444 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
445 glEnd;
446 510
447 my $bg = $tex[0]; 511 my $bg = $tex[0];
448 glBindTexture GL_TEXTURE_2D, $bg->{name}; 512 glBindTexture GL_TEXTURE_2D, $bg->{name};
449 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 513 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
450 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 514 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
451 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 515 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
452 516
453 my $rep_x = $cw / $bg->{width}; 517 my $rep_x = $cw / $bg->{width};
454 my $rep_y = $ch / $bg->{height}; 518 my $rep_y = $ch / $bg->{height};
455 519
456 glBegin GL_QUADS; 520 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
457 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
458 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
459 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
460 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
461 glEnd;
462 521
463 glDisable GL_BLEND; 522 glDisable GL_BLEND;
464 glDisable GL_TEXTURE_2D; 523 glDisable GL_TEXTURE_2D;
465 524
466 $self->child->draw; 525 $self->child->draw;
467 526
468} 527}
469 528
470############################################################################# 529#############################################################################
471 530
472package Crossfire::Client::Widget::Table; 531package CFClient::Widget::Table;
473 532
474our @ISA = Crossfire::Client::Widget::Bin::; 533our @ISA = CFClient::Widget::Bin::;
475 534
476use SDL::OpenGL; 535use SDL::OpenGL;
477 536
478sub add { 537sub add {
479 my ($self, $x, $y, $chld) = @_; 538 my ($self, $x, $y, $chld) = @_;
554 } 613 }
555} 614}
556 615
557############################################################################# 616#############################################################################
558 617
559package Crossfire::Client::Widget::VBox; 618package CFClient::Widget::VBox;
560 619
561our @ISA = Crossfire::Client::Widget::Container::; 620our @ISA = CFClient::Widget::Container::;
562 621
563use SDL::OpenGL; 622use SDL::OpenGL;
564 623
565sub size_request { 624sub size_request {
566 my ($self) = @_; 625 my ($self) = @_;
614 } 673 }
615} 674}
616 675
617############################################################################# 676#############################################################################
618 677
619package Crossfire::Client::Widget::Label; 678package CFClient::Widget::Label;
620 679
621our @ISA = Crossfire::Client::Widget::; 680our @ISA = CFClient::Widget::;
622 681
623use SDL::OpenGL; 682use SDL::OpenGL;
624 683
625sub new { 684sub new {
626 my ($class, $x, $y, $z, $height, $text) = @_; 685 my ($class, %arg) = @_;
627 686
628 # TODO: color, and make height, xyz etc. optional 687 # TODO: color, and make height, xyz etc. optional
629 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 688 my $self = $class->SUPER::new (
689 color => [1, 1, 1],
690 height => $::FONTSIZE,
691 text => "",
692 layout => new CFClient::Layout,
693 %arg
694 );
630 695
631 $self->set_text ($text); 696 $self->set_text ($self->{text});
632 697
633 $self 698 $self
634} 699}
635 700
636sub set_text { 701sub set_text {
637 my ($self, $text) = @_; 702 my ($self, $text) = @_;
638 703
639 $self->{text} = $text; 704 $self->{text} = $text;
640 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 705 $self->{layout}->set_markup ($text);
641 706
642 $self->update; 707 delete $self->{texture};
643} 708}
644 709
645sub get_text { 710sub get_text {
646 my ($self, $text) = @_; 711 my ($self, $text) = @_;
647 712
649} 714}
650 715
651sub size_request { 716sub size_request {
652 my ($self) = @_; 717 my ($self) = @_;
653 718
654 ( 719 $self->{layout}->set_width;
720 $self->{layout}->set_height ($self->{height});
721 $self->{layout}->size
722# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
723# (
655 $self->{texture}{width}, 724# $self->{texture}{width},
656 $self->{texture}{height}, 725# $self->{texture}{height},
657 ) 726# )
727# } else {
728# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
729#
730# ($w, $h)
731# }
732}
733
734sub size_allocate {
735 my ($self, $w, $h) = @_;
736
737 $self->SUPER::size_allocate ($w, $h);
738 delete $self->{texture};
658} 739}
659 740
660sub _draw { 741sub _draw {
661 my ($self) = @_; 742 my ($self) = @_;
662 743
663 my $tex = $self->{texture}; 744 my $tex = $self->{texture} ||= do {
745 $self->{layout}->set_width ($self->{w});
746 new_from_layout CFClient::Texture $self->{layout};
747 };
664 748
665 glEnable GL_BLEND; 749 glEnable GL_BLEND;
666 glEnable GL_TEXTURE_2D; 750 glEnable GL_TEXTURE_2D;
667 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 751 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
668 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 752 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
669 glBindTexture GL_TEXTURE_2D, $tex->{name};
670 753
671 glColor 1, 0, 0, 1; # TODO color 754 glColor @{$self->{color}};
672 755
673 glBegin GL_QUADS; 756 $tex->draw_quad (0, 0);
674 glTexCoord 0, 0; glVertex 0 , 0;
675 glTexCoord 0, 1; glVertex 0 , $tex->{height};
676 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
677 glTexCoord 1, 0; glVertex $tex->{width}, 0;
678 glEnd;
679 757
680 glDisable GL_BLEND; 758 glDisable GL_BLEND;
681 glDisable GL_TEXTURE_2D; 759 glDisable GL_TEXTURE_2D;
682} 760}
683 761
684############################################################################# 762#############################################################################
685 763
686package Crossfire::Client::Widget::TextEntry; 764package CFClient::Widget::Entry;
687 765
688our @ISA = Crossfire::Client::Widget::Label::; 766our @ISA = CFClient::Widget::Label::;
689 767
690use SDL; 768use SDL;
691use SDL::OpenGL; 769use SDL::OpenGL;
692 770
693sub key_down { 771sub key_down {
694 my ($self, $ev) = @_; 772 my ($self, $ev) = @_;
695 773
696 my $mod = $ev->key_mod; 774 my $mod = $ev->key_mod;
697 my $sym = $ev->key_sym; 775 my $sym = $ev->key_sym;
698 776
699 $ev->set_unicode (1);
700 my $uni = $ev->key_unicode; 777 my $uni = $ev->key_unicode;
701 778
702 my $text = $self->get_text; 779 my $text = $self->get_text;
703 780
704 if ($sym == SDLK_BACKSPACE) { 781 if ($sym == SDLK_BACKSPACE) {
705 substr $text, -1, 1, ''; 782 substr $text, -1, 1, '';
706
707 } elsif ($uni) { 783 } elsif ($uni) {
708 $text .= chr $uni; 784 $text .= chr $uni;
709 } 785 }
786
710 $self->set_text ($text); 787 $self->set_text ($text);
711} 788}
712 789
713############################################################################# 790sub button_down {
791 my ($self, $ev) = @_;
714 792
793 $self->focus_in;
794}
795
796sub mouse_motion {
797 my ($self, $ev, $x, $y) = @_;
798 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
799}
800
801sub _draw {
802 my ($self) = @_;
803
804 if ($FOCUS == $self) {
805 glColor 1, 1, 1;
806 } else {
807 glColor 0.7, 0.7, 0.7;
808 }
809
810 glBegin GL_QUADS;
811 glVertex 0 , 0;
812 glVertex 0 , $self->{h} - 1;
813 glVertex $self->{w} - 1, $self->{h} - 1;
814 glVertex $self->{w} - 1, 0;
815 glEnd;
816
817 $self->SUPER::_draw;
818}
819
820#############################################################################
821
715package Crossfire::Client::Widget::MapWidget; 822package CFClient::Widget::MapWidget;
716 823
717use strict; 824use strict;
718 825
719use List::Util qw(min max); 826use List::Util qw(min max);
720 827
721use SDL; 828use SDL;
722use SDL::OpenGL; 829use SDL::OpenGL;
723use SDL::OpenGL::Constants; 830use SDL::OpenGL::Constants;
724 831
725our @ISA = Crossfire::Client::Widget::; 832our @ISA = CFClient::Widget::;
833
834sub new {
835 my $class = shift;
836
837 $class->SUPER::new (
838 z => -1,
839 list => (glGenLists 1),
840 @_
841 )
842}
726 843
727sub key_down { 844sub key_down {
728 print "MAPKEYDOWN\n"; 845 print "MAPKEYDOWN\n";
729} 846}
730 847
731sub key_up { 848sub key_up {
732} 849}
733 850
734sub size_request { 851sub size_request {
735 852 (
853 1 + int $::WIDTH / 32,
854 1 + int $::HEIGHT / 32,
855 )
736} 856}
737 857
738sub size_allocate { 858sub update {
859 my ($self) = @_;
860
861 $self->{need_update} = 1;
739} 862}
740 863
741sub _draw { 864sub _draw {
742 my ($self) = @_; 865 my ($self) = @_;
743 866
867 if (delete $self->{need_update}) {
868 glNewList $self->{list}, GL_COMPILE;
869
744 my $mx = $::CONN->{mapx}; 870 my $mx = $::CONN->{mapx};
745 my $my = $::CONN->{mapy}; 871 my $my = $::CONN->{mapy};
746 872
747 my $map = $::CONN->{map}; 873 my $map = $::CONN->{map};
748 874
749 my ($xofs, $yofs); 875 my ($xofs, $yofs);
750 876
751 my $sw = 1 + int $::WIDTH / 32; 877 my $sw = 1 + int $::WIDTH / 32;
752 my $sh = 1 + int $::HEIGHT / 32; 878 my $sh = 1 + int $::HEIGHT / 32;
753 879
754 if ($::CONN->{mapw} > $sw) { 880 if ($::CONN->{mapw} > $sw) {
755 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 881 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
756 } else { 882 } else {
757 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 883 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
758 } 884 }
759 885
760 if ($::CONN->{maph} > $sh) { 886 if ($::CONN->{maph} > $sh) {
761 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 887 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
762 } else { 888 } else {
763 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 889 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
764 } 890 }
765 891
766 glEnable GL_TEXTURE_2D; 892 glEnable GL_TEXTURE_2D;
767 glEnable GL_BLEND; 893 glEnable GL_BLEND;
768 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 894 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
769 895
770 my $sw4 = ($sw + 3) & ~3; 896 my $sw4 = ($sw + 3) & ~3;
771 my $lighting = "\x00" x ($sw4 * $sh); 897 my $darkness = "\x00" x ($sw4 * $sh);
772 898
773 for my $x (0 .. $sw - 1) { 899 for my $x (0 .. $sw - 1) {
900 my $row = $map->[$x + $xofs];
774 for my $y (0 .. $sh - 1) { 901 for my $y (0 .. $sh - 1) {
775 902
776 my $cell = $map->[$x + $xofs][$y + $yofs] 903 my $cell = $row->[$y + $yofs]
777 or next; 904 or next;
778 905
779 my $darkness = $cell->[0] * (1 / 255); 906 my $dark = $cell->[0];
780 if ($darkness < 0) { 907 if ($dark < 0) {
781 $darkness = 0.15; 908 substr $darkness, $y * $sw4 + $x, 1, chr 224;
909 } else {
910 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
782 } 911 }
783 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
784 912
785 for my $num (grep $_, @$cell[1,2,3]) { 913 for my $num (grep $_, @$cell[1,2,3]) {
786 my $tex = $::CONN->{face}[$num]{texture} || next; 914 my $tex = $::CONN->{face}[$num]{texture} || next;
787 915
788 glBindTexture GL_TEXTURE_2D, $tex->{name};
789
790 my $w = $tex->{width}; 916 my $w = $tex->{width};
791 my $h = $tex->{height}; 917 my $h = $tex->{height};
792 918
793 my $px = ($x + 1) * 32 - $w; 919 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
794 my $py = ($y + 1) * 32 - $h;
795
796 glBegin GL_QUADS;
797 glTexCoord 0, 0; glVertex $px , $py;
798 glTexCoord 0, 1; glVertex $px , $py + $h;
799 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
800 glTexCoord 1, 0; glVertex $px + $w, $py;
801 glEnd; 920 }
802 } 921 }
803 } 922 }
804 }
805 923
806# if (1) { # higher quality darkness 924# if (1) { # higher quality darkness
807# $lighting =~ s/(.)/$1$1$1/gs; 925# $lighting =~ s/(.)/$1$1$1/gs;
808# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; 926# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
809# 927#
811# 929#
812# $lighting = $pb->get_pixels; 930# $lighting = $pb->get_pixels;
813# $lighting =~ s/(.)../$1/gs; 931# $lighting =~ s/(.)../$1/gs;
814# } 932# }
815 933
816 $lighting = new Crossfire::Client::Texture
817 width => $sw4,
818 height => $sh,
819 data => $lighting,
820 internalformat => GL_ALPHA4,
821 format => GL_ALPHA;
822
823 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 934 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
824 glColor 0, 0, 0, 0.75;
825 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 935 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
826 glBindTexture GL_TEXTURE_2D, $lighting->{name};
827 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
828 glBegin GL_QUADS;
829 glTexCoord 0, 0; glVertex 0 , 0;
830 glTexCoord 0, 1; glVertex 0 , $sh * 32;
831 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
832 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
833 glEnd;
834 936
937 $darkness = new CFClient::Texture
938 width => $sw4,
939 height => $sh,
940 data => $darkness,
941 internalformat => GL_ALPHA,
942 format => GL_ALPHA;
943
944 glColor 0.45, 0.45, 0.45, 1;
945 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
946
835 glDisable GL_TEXTURE_2D; 947 glDisable GL_TEXTURE_2D;
836 glDisable GL_BLEND; 948 glDisable GL_BLEND;
949
950 glEndList;
951 }
952
953 glCallList $self->{list};
837} 954}
838 955
839my %DIR = ( 956my %DIR = (
840 SDLK_KP8, [1, "north"], 957 SDLK_KP8, [1, "north"],
841 SDLK_KP9, [2, "northeast"], 958 SDLK_KP9, [2, "northeast"],
887 } 1004 }
888} 1005}
889 1006
890############################################################################# 1007#############################################################################
891 1008
892package Crossfire::Client::Widget::Animator; 1009package CFClient::Widget::Animator;
893 1010
894use SDL::OpenGL; 1011use SDL::OpenGL;
895 1012
896our @ISA = Crossfire::Client::Widget::Bin::; 1013our @ISA = CFClient::Widget::Bin::;
897 1014
898sub moveto { 1015sub moveto {
899 my ($self, $x, $y) = @_; 1016 my ($self, $x, $y) = @_;
900 1017
901 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 1018 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
902 $self->{speed} = 0.2; 1019 $self->{speed} = 0.001;
903 $self->{time} = 1; 1020 $self->{time} = 1;
904 1021
905 ::animation_start $self; 1022 ::animation_start $self;
906} 1023}
907 1024
922 1039
923sub _draw { 1040sub _draw {
924 my ($self) = @_; 1041 my ($self) = @_;
925 1042
926 glPushMatrix; 1043 glPushMatrix;
927 glRotate $self->{time} * 10000, 0, 1, 0; 1044 glRotate $self->{time} * 1000, 0, 1, 0;
928 $self->{children}[0]->draw; 1045 $self->{children}[0]->draw;
929 glPopMatrix; 1046 glPopMatrix;
930} 1047}
931 1048
9321; 1049#############################################################################
933 1050
1051package CFClient::Widget::Toplevel;
1052
1053our @ISA = CFClient::Widget::Container::;
1054
1055sub size_request {
1056 ($::WIDTH, $::HEIGHT)
1057}
1058
1059sub size_allocate {
1060 my ($self, $w, $h) = @_;
1061
1062 $self->SUPER::size_allocate ($w, $h);
1063
1064 $_->size_allocate ($_->size_request)
1065 for @{$self->{children}};
1066}
1067
1068sub translate {
1069 my ($self, $x, $y) = @_;
1070
1071 ($x, $y)
1072}
1073
1074sub update {
1075 my ($self) = @_;
1076
1077 $self->size_allocate ($self->size_request);
1078 ::refresh ();
1079}
1080
1081sub add {
1082 my ($self, $widget) = @_;
1083
1084 $self->SUPER::add ($widget);
1085
1086 $widget->size_allocate ($widget->size_request);
1087}
1088
1089sub draw {
1090 my ($self) = @_;
1091
1092 $self->_draw;
1093}
1094
1095#############################################################################
1096
1097package CFClient::Widget;
1098
1099$TOPLEVEL = new CFClient::Widget::Toplevel;
1100
11011
1102

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines