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

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