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.42 by root, Sun Apr 9 22:00:58 2006 UTC vs.
Revision 1.63 by root, Tue Apr 11 13:29:24 2006 UTC

1package Crossfire::Client::Widget; 1package CFClient::Widget;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util;
6 6
7use SDL::OpenGL; 7use SDL::OpenGL;
8use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
9 9
10use Crossfire::Client; 10use CFClient;
11 11
12our $FOCUS; # the widget with current focus 12our ($FOCUS, $HOVER, $GRAB); # various widgets
13
14our $TOPLEVEL;
15our $BUTTON_STATE;
13 16
14# class methods for events 17# class methods for events
15sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 18sub feed_sdl_key_down_event {
16sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 19 $FOCUS->key_down ($_[0]) if $FOCUS;
20}
21
22sub feed_sdl_key_up_event {
23 $FOCUS->key_up ($_[0]) if $FOCUS;
24}
25
17sub feed_sdl_button_down_event { } 26sub feed_sdl_button_down_event {
27 my ($ev) = @_;
28 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
29
30 if (!$BUTTON_STATE) {
31 my $widget = $TOPLEVEL->find_widget ($x, $y);
32
33 $GRAB = $widget;
34 $GRAB->update if $GRAB;
35 }
36
37 $BUTTON_STATE |= 1 << ($ev->button - 1);
38
39 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
40}
41
18sub feed_sdl_button_up_event { } 42sub feed_sdl_button_up_event {
43 my ($ev) = @_;
44 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
45
46 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
47
48 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
49
50 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
51
52 if (!$BUTTON_STATE) {
53 my $grab = $GRAB; undef $GRAB;
54 $grab->update if $grab;
55 $GRAB->update if $GRAB;
56 }
57}
58
59sub feed_sdl_motion_event {
60 my ($ev) = @_;
61 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
62
63 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
64
65 if ($widget != $HOVER) {
66 my $hover = $HOVER; $HOVER = $widget;
67
68 $hover->update if $hover;
69 $HOVER->update if $HOVER;
70 }
71
72 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
73}
19 74
20sub new { 75sub new {
21 my $class = shift; 76 my $class = shift;
22 77
23 bless { @_ }, $class 78 bless { @_ }, $class
44 99
45 $self->{w} = $w; 100 $self->{w} = $w;
46 $self->{h} = $h; 101 $self->{h} = $h;
47} 102}
48 103
104# translate global koordinates to local coordinate system
105sub translate {
106 my ($self, $x, $y) = @_;
107
108 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
109}
110
49sub focus_in { 111sub focus_in {
50 my ($widget) = @_; 112 my ($self) = @_;
51 $FOCUS = $widget; 113
114 my $focus = $FOCUS; $FOCUS = $self;
115 $focus->update if $focus;
116 $FOCUS->update;
52} 117}
53 118
54sub focus_out { 119sub focus_out {
55 my ($widget) = @_; 120 my ($self) = @_;
56}
57 121
58sub key_down { 122 return unless $FOCUS == $self;
59 my ($widget, $sdlev) = @_;
60}
61 123
62sub key_up { 124 my $focus = $FOCUS; undef $FOCUS;
63 my ($widget, $sdlev) = @_; 125 $focus->update if $focus; #?
64} 126}
65 127
128sub mouse_motion { }
129sub button_up { }
66sub button_down { 130sub button_down { }
67 my ($widget, $sdlev) = @_; 131sub key_down { }
68} 132sub key_up { }
69 133
70sub button_up {
71 my ($widget, $sdlev) = @_;
72}
73
74sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 134sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
75sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 135sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
76sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 136sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
77sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 137sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
78sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 138sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
79 139
80sub draw { 140sub draw {
81 my ($self) = @_; 141 my ($self) = @_;
82 142
83 glPushMatrix; 143 glPushMatrix;
84 glTranslate $self->{x}, $self->{y}, 0; 144 glTranslate $self->{x}, $self->{y}, 0;
85 $self->_draw; 145 $self->_draw;
146 if ($self == $HOVER) {
147 glColor 1, 1, 1, 0.4;
148 glEnable GL_BLEND;
149 glBegin GL_QUADS;
150 glVertex 0 , 0;
151 glVertex $self->{w}, 0;
152 glVertex $self->{w}, $self->{h};
153 glVertex 0 , $self->{h};
154 glEnd;
155 glDisable GL_BLEND;
156 }
86 glPopMatrix; 157 glPopMatrix;
87} 158}
88 159
89sub _draw { 160sub _draw {
90 my ($self) = @_; 161 my ($self) = @_;
139 #$self->deactivate; 210 #$self->deactivate;
140} 211}
141 212
142############################################################################# 213#############################################################################
143 214
144package Crossfire::Client::Widget::Container; 215package CFClient::Widget::Container;
145 216
146our @ISA = Crossfire::Client::Widget::; 217our @ISA = CFClient::Widget::;
147 218
148sub new { 219sub new {
149 my ($class, @widgets) = @_; 220 my ($class, @widgets) = @_;
150 221
151 my $self = $class->SUPER::new (children => []); 222 my $self = $class->SUPER::new (children => []);
162 233
163 @{$self->{children}} = 234 @{$self->{children}} =
164 sort { $a->{z} <=> $b->{z} } 235 sort { $a->{z} <=> $b->{z} }
165 @{$self->{children}}, $chld; 236 @{$self->{children}}, $chld;
166 237
167 $self->size_allocate ($self->{w}, $self->{h}); 238 $self->size_allocate ($self->{w}, $self->{h})
239 if $self->{w}; #TODO: check for "realised state"
168} 240}
169 241
170sub remove { 242sub remove {
171 my ($self, $widget) = @_; 243 my ($self, $widget) = @_;
172 244
176} 248}
177 249
178sub find_widget { 250sub find_widget {
179 my ($self, $x, $y) = @_; 251 my ($self, $x, $y) = @_;
180 252
253 $x -= $self->{x};
254 $y -= $self->{y};
255
181 my $res; 256 my $res;
182 257
183 for (@{ $self->{children} }) { 258 for (reverse @{ $self->{children} }) {
184 $res = $_->find_widget ($x, $y) 259 $res = $_->find_widget ($x, $y)
185 and return $res; 260 and return $res;
186 } 261 }
187 262
188 () 263 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
189}
190
191sub size_request {
192 my ($self) = @_;
193
194 my ($hs, $ws) = (0, 0);
195 for (@{$self->{children} || []}) {
196 my ($w, $h) = $_->size_request;
197 $hs += $h;
198 if ($ws < $w) { $ws = $w }
199 }
200
201 return ($ws, $hs);
202} 264}
203 265
204sub _draw { 266sub _draw {
205 my ($self) = @_; 267 my ($self) = @_;
206 268
207 $_->draw for @{$self->{children}}; 269 $_->draw for @{$self->{children}};
208} 270}
209 271
210############################################################################# 272#############################################################################
211 273
212package Crossfire::Client::Widget::Bin; 274package CFClient::Widget::Bin;
213 275
214our @ISA = Crossfire::Client::Widget::Container::; 276our @ISA = CFClient::Widget::Container::;
215 277
216sub child { $_[0]->{children}[0] } 278sub child { $_[0]->{children}[0] }
217 279
218sub size_request { 280sub size_request {
219 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 281 $_[0]{children}[0]->size_request if $_[0]{children}[0];
227 if $self->{children}[0] 289 if $self->{children}[0]
228} 290}
229 291
230############################################################################# 292#############################################################################
231 293
232package Crossfire::Client::Widget::Toplevel;
233
234our @ISA = Crossfire::Client::Widget::Container::;
235
236sub update {
237 my ($self) = @_;
238
239 ::refresh ();
240}
241
242sub add {
243 my ($self, $widget) = @_;
244
245 $self->SUPER::add ($widget);
246
247 $widget->size_allocate ($widget->size_request);
248}
249
250#############################################################################
251
252package Crossfire::Client::Widget::Window; 294package CFClient::Widget::Window;
253 295
254our @ISA = Crossfire::Client::Widget::Bin::; 296our @ISA = CFClient::Widget::Bin::;
255 297
256use SDL::OpenGL; 298use SDL::OpenGL;
257 299
258sub new { 300sub new {
259 my ($class, $x, $y, $z, $w, $h) = @_; 301 my ($class, $x, $y, $z, $w, $h) = @_;
264} 306}
265 307
266sub update { 308sub update {
267 my ($self) = @_; 309 my ($self) = @_;
268 310
311 # we want to do this delayed...
269 $self->render_chld; 312 $self->render_chld;
270 $self->SUPER::update; 313 $self->SUPER::update;
271} 314}
272 315
273sub render_chld { 316sub render_chld {
274 my ($self) = @_; 317 my ($self) = @_;
275 318
276 $self->{texture} = 319 $self->{texture} =
277 Crossfire::Client::Texture->new_from_opengl ( 320 CFClient::Texture->new_from_opengl (
278 $self->{w}, $self->{h}, sub { $self->child->draw } 321 $self->{w}, $self->{h}, sub { $self->child->draw }
279 ); 322 );
280} 323}
281 324
282sub size_allocate { 325sub size_allocate {
299 or return; 342 or return;
300 343
301 glEnable GL_BLEND; 344 glEnable GL_BLEND;
302 glEnable GL_TEXTURE_2D; 345 glEnable GL_TEXTURE_2D;
303 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 346 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
304 glBindTexture GL_TEXTURE_2D, $tex->{name};
305 347
306 glBegin GL_QUADS; 348 $tex->draw_quad (0, 0, $w, $h);
307 glTexCoord 0, 0; glVertex 0, 0;
308 glTexCoord 0, 1; glVertex 0, $h;
309 glTexCoord 1, 1; glVertex $w, $h;
310 glTexCoord 1, 0; glVertex $w, 0;
311 glEnd;
312 349
313 glDisable GL_BLEND; 350 glDisable GL_BLEND;
314 glDisable GL_TEXTURE_2D; 351 glDisable GL_TEXTURE_2D;
315} 352}
316 353
317############################################################################# 354#############################################################################
318 355
319package Crossfire::Client::Widget::Frame; 356package CFClient::Widget::Frame;
320 357
321our @ISA = Crossfire::Client::Widget::Bin::; 358our @ISA = CFClient::Widget::Bin::;
322 359
323use SDL::OpenGL; 360use SDL::OpenGL;
324 361
325sub size_request { 362sub size_request {
326 my ($self) = @_; 363 my ($self) = @_;
349 386
350 my ($w, $h) = $chld->size_request; 387 my ($w, $h) = $chld->size_request;
351 388
352 glBegin GL_QUADS; 389 glBegin GL_QUADS;
353 glColor 0, 0, 0; 390 glColor 0, 0, 0;
354 glTexCoord 0, 0; glVertex 0 , 0; 391 glVertex 0 , 0;
355 glTexCoord 0, 1; glVertex 0 , $h + 4; 392 glVertex 0 , $h + 4;
356 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 393 glVertex $w + 4 , $h + 4;
357 glTexCoord 1, 0; glVertex $w + 4 , 0; 394 glVertex $w + 4 , 0;
358 glEnd; 395 glEnd;
359 396
360 $chld->draw; 397 $chld->draw;
361} 398}
362 399
363############################################################################# 400#############################################################################
364 401
365package Crossfire::Client::Widget::FancyFrame; 402package CFClient::Widget::FancyFrame;
366 403
367our @ISA = Crossfire::Client::Widget::Bin::; 404our @ISA = CFClient::Widget::Bin::;
368 405
369use SDL::OpenGL; 406use SDL::OpenGL;
370 407
371my @tex = 408my @tex =
372 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 409 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
373 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 410 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
374 411
375sub size_request { 412sub size_request {
376 my ($self) = @_; 413 my ($self) = @_;
377 414
395 $w -= $tex[2]->{width}; 432 $w -= $tex[2]->{width};
396 $w -= $tex[3]->{width}; 433 $w -= $tex[3]->{width};
397 434
398 $h = $h < 0 ? 0 : $h; 435 $h = $h < 0 ? 0 : $h;
399 $w = $w < 0 ? 0 : $w; 436 $w = $w < 0 ? 0 : $w;
400 warn "CHILD:$w $h\n"; 437
401 $self->child->size_allocate ($w, $h); 438 $self->child->size_allocate ($w, $h);
402 $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 439 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
403} 440}
404 441
405sub _draw { 442sub _draw {
406 my ($self) = @_; 443 my ($self) = @_;
407 444
408 my ($w, $h) = ($self->w, $self->h); 445 my ($w, $h) = ($self->{w}, $self->{h});
409 my ($cw, $ch) = ($self->child->w, $self->child->h); 446 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
410 447
411 glEnable GL_BLEND; 448 glEnable GL_BLEND;
412 glEnable GL_TEXTURE_2D; 449 glEnable GL_TEXTURE_2D;
413 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 450 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
414 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 451 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
415 452
416 my $top = $tex[1]; 453 my $top = $tex[1];
417 glBindTexture GL_TEXTURE_2D, $top->{name}; 454 $top->draw_quad (0, 0, $w, $top->{height});
418
419 glBegin GL_QUADS;
420 glTexCoord 0, 0; glVertex 0 , 0;
421 glTexCoord 0, 1; glVertex 0 , $top->{height};
422 glTexCoord 1, 1; glVertex $w , $top->{height};
423 glTexCoord 1, 0; glVertex $w , 0;
424 glEnd;
425 455
426 my $left = $tex[3]; 456 my $left = $tex[3];
427 glBindTexture GL_TEXTURE_2D, $left->{name}; 457 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
428
429 glBegin GL_QUADS;
430 glTexCoord 0, 0; glVertex 0 , $top->{height};
431 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
432 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
433 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
434 glEnd;
435 458
436 my $right = $tex[2]; 459 my $right = $tex[2];
437 glBindTexture GL_TEXTURE_2D, $right->{name}; 460 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
438
439 glBegin GL_QUADS;
440 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
441 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
442 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
443 glTexCoord 1, 0; glVertex $w , $top->{height};
444 glEnd;
445 461
446 my $bottom = $tex[4]; 462 my $bottom = $tex[4];
447 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 463 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
448
449 glBegin GL_QUADS;
450 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
451 glTexCoord 0, 1; glVertex 0 , $h;
452 glTexCoord 1, 1; glVertex $w , $h;
453 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
454 glEnd;
455 464
456 my $bg = $tex[0]; 465 my $bg = $tex[0];
457 glBindTexture GL_TEXTURE_2D, $bg->{name}; 466 glBindTexture GL_TEXTURE_2D, $bg->{name};
458 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 467 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
459 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
460 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 469 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
461 470
462 my $rep_x = $cw / $bg->{width}; 471 my $rep_x = $cw / $bg->{width};
463 my $rep_y = $ch / $bg->{height}; 472 my $rep_y = $ch / $bg->{height};
464 473
465 glBegin GL_QUADS; 474 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
466 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
467 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
468 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
469 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
470 glEnd;
471 475
472 glDisable GL_BLEND; 476 glDisable GL_BLEND;
473 glDisable GL_TEXTURE_2D; 477 glDisable GL_TEXTURE_2D;
474 478
475 $self->child->draw; 479 $self->child->draw;
476 480
477} 481}
478 482
479############################################################################# 483#############################################################################
480 484
481package Crossfire::Client::Widget::Table; 485package CFClient::Widget::Table;
482 486
483our @ISA = Crossfire::Client::Widget::Bin::; 487our @ISA = CFClient::Widget::Bin::;
484 488
485use SDL::OpenGL; 489use SDL::OpenGL;
486 490
487sub add { 491sub add {
488 my ($self, $x, $y, $chld) = @_; 492 my ($self, $x, $y, $chld) = @_;
563 } 567 }
564} 568}
565 569
566############################################################################# 570#############################################################################
567 571
568package Crossfire::Client::Widget::VBox; 572package CFClient::Widget::VBox;
569 573
570our @ISA = Crossfire::Client::Widget::Container::; 574our @ISA = CFClient::Widget::Container::;
571 575
572use SDL::OpenGL; 576use SDL::OpenGL;
577
578sub size_request {
579 my ($self) = @_;
580
581 my @alloc = map [$_->size_request], @{$self->{children}};
582
583 (
584 (List::Util::max map $_->[0], @alloc),
585 (List::Util::sum map $_->[1], @alloc),
586 )
587}
573 588
574sub size_allocate { 589sub size_allocate {
575 my ($self, $w, $h) = @_; 590 my ($self, $w, $h) = @_;
576 591
577 $self->w ($w); 592 $self->w ($w);
610 $y += $h; 625 $y += $h;
611 } 626 }
612 } 627 }
613} 628}
614 629
615sub _draw {
616 my ($self) = @_;
617
618 my ($x, $y);
619 for (@{$self->{children} || []}) {
620 $_->draw;
621 $y += $_->h;
622 }
623}
624
625############################################################################# 630#############################################################################
626 631
627package Crossfire::Client::Widget::Label; 632package CFClient::Widget::Label;
628 633
629our @ISA = Crossfire::Client::Widget::; 634our @ISA = CFClient::Widget::;
630 635
631use SDL::OpenGL; 636use SDL::OpenGL;
632 637
633sub new { 638sub new {
634 my ($class, $x, $y, $z, $height, $text) = @_; 639 my ($class, $x, $y, $z, $height, $text) = @_;
635 640
641 $height ||= $::FONTSIZE;
642
636 # TODO: color, and make height, xyz etc. optional 643 # TODO: color, and make height, xyz etc. optional
637 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 644 my $self = $class->SUPER::new (
645 color => [1, 1, 1],
646 x => $x,
647 y => $y,
648 z => $z,
649 height => $height,
650 layout => new CFClient::Layout $height,
651 );
638 652
639 $self->set_text ($text); 653 $self->set_text ($text);
640 654
641 $self 655 $self
642} 656}
643 657
644sub set_text { 658sub set_text {
645 my ($self, $text) = @_; 659 my ($self, $text) = @_;
646 660
647 $self->{text} = $text; 661 $self->{text} = $text;
648 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 662 $self->{layout}->set_markup ($text);
649 663
650 $self->update; 664 delete $self->{texture};
651} 665}
652 666
653sub get_text { 667sub get_text {
654 my ($self, $text) = @_; 668 my ($self, $text) = @_;
655 669
657} 671}
658 672
659sub size_request { 673sub size_request {
660 my ($self) = @_; 674 my ($self) = @_;
661 675
662 ( 676 $self->{layout}->set_width;
677 $self->{layout}->size
678# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
679# (
663 $self->{texture}{width}, 680# $self->{texture}{width},
664 $self->{texture}{height}, 681# $self->{texture}{height},
665 ) 682# )
683# } else {
684# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
685#
686# ($w, $h)
687# }
688}
689
690sub size_allocate {
691 my ($self, $w, $h) = @_;
692
693 $self->SUPER::size_allocate ($w, $h);
694 delete $self->{texture};
666} 695}
667 696
668sub _draw { 697sub _draw {
669 my ($self) = @_; 698 my ($self) = @_;
670 699
671 my $tex = $self->{texture}; 700 my $tex = $self->{texture} ||= do {
701 $self->{layout}->set_width ($self->{w});
702 new_from_layout CFClient::Texture $self->{layout};
703 };
672 704
673 glEnable GL_BLEND; 705 glEnable GL_BLEND;
674 glEnable GL_TEXTURE_2D; 706 glEnable GL_TEXTURE_2D;
675 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 707 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
676 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 708 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
677 glBindTexture GL_TEXTURE_2D, $tex->{name};
678 709
679 glColor 1, 0, 0, 1; # TODO color 710 glColor @{$self->{color}};
680 711
681 glBegin GL_QUADS; 712 $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 713
688 glDisable GL_BLEND; 714 glDisable GL_BLEND;
689 glDisable GL_TEXTURE_2D; 715 glDisable GL_TEXTURE_2D;
690} 716}
691 717
692############################################################################# 718#############################################################################
693 719
694package Crossfire::Client::Widget::TextEntry; 720package CFClient::Widget::Entry;
695 721
696our @ISA = Crossfire::Client::Widget::Label::; 722our @ISA = CFClient::Widget::Label::;
697 723
698use SDL; 724use SDL;
699use SDL::OpenGL; 725use SDL::OpenGL;
700 726
701sub key_down { 727sub key_down {
702 my ($self, $ev) = @_; 728 my ($self, $ev) = @_;
703 729
704 my $mod = $ev->key_mod; 730 my $mod = $ev->key_mod;
705 my $sym = $ev->key_sym; 731 my $sym = $ev->key_sym;
706 732
707 $ev->set_unicode (1);
708 my $uni = $ev->key_unicode; 733 my $uni = $ev->key_unicode;
709 734
710 my $text = $self->get_text; 735 my $text = $self->get_text;
711 736
712 if ($sym == SDLK_BACKSPACE) { 737 if ($sym == SDLK_BACKSPACE) {
713 substr $text, -1, 1, ''; 738 substr $text, -1, 1, '';
714
715 } elsif ($uni) { 739 } elsif ($uni) {
716 $text .= chr $uni; 740 $text .= chr $uni;
717 } 741 }
742
718 $self->set_text ($text); 743 $self->set_text ($text);
719} 744}
720 745
721############################################################################# 746sub button_down {
747 my ($self, $ev) = @_;
722 748
749 $self->focus_in;
750}
751
752sub mouse_motion {
753 my ($self, $ev, $x, $y) = @_;
754 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
755}
756
757sub _draw {
758 my ($self) = @_;
759
760 if ($FOCUS == $self) {
761 glColor 1, 1, 1;
762 } else {
763 glColor 0.7, 0.7, 0.7;
764 }
765
766 glBegin GL_QUADS;
767 glVertex 0 , 0;
768 glVertex 0 , $self->{h} - 1;
769 glVertex $self->{w} - 1, $self->{h} - 1;
770 glVertex $self->{w} - 1, 0;
771 glEnd;
772
773 $self->SUPER::_draw;
774}
775
776#############################################################################
777
723package Crossfire::Client::Widget::MapWidget; 778package CFClient::Widget::MapWidget;
724 779
725use strict; 780use strict;
726 781
727use List::Util qw(min max); 782use List::Util qw(min max);
728 783
729use SDL; 784use SDL;
730use SDL::OpenGL; 785use SDL::OpenGL;
731use SDL::OpenGL::Constants; 786use SDL::OpenGL::Constants;
732 787
733our @ISA = Crossfire::Client::Widget::; 788our @ISA = CFClient::Widget::;
734 789
735sub key_down { 790sub key_down {
736 print "MAPKEYDOWN\n"; 791 print "MAPKEYDOWN\n";
737} 792}
738 793
739sub key_up { 794sub key_up {
740} 795}
741 796
742sub size_request { 797sub size_request {
743 798 (
744} 799 1 + int $::WIDTH / 32,
745 800 1 + int $::HEIGHT / 32,
746sub size_allocate { 801 )
747} 802}
748 803
749sub _draw { 804sub _draw {
750 my ($self) = @_; 805 my ($self) = @_;
751 806
774 glEnable GL_TEXTURE_2D; 829 glEnable GL_TEXTURE_2D;
775 glEnable GL_BLEND; 830 glEnable GL_BLEND;
776 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 831 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
777 832
778 my $sw4 = ($sw + 3) & ~3; 833 my $sw4 = ($sw + 3) & ~3;
779 my $lighting = "\x00" x ($sw4 * $sh); 834 my $darkness = "\x00" x ($sw4 * $sh);
780 835
781 for my $x (0 .. $sw - 1) { 836 for my $x (0 .. $sw - 1) {
837 my $row = $map->[$x + $xofs];
782 for my $y (0 .. $sh - 1) { 838 for my $y (0 .. $sh - 1) {
783 839
784 my $cell = $map->[$x + $xofs][$y + $yofs] 840 my $cell = $row->[$y + $yofs]
785 or next; 841 or next;
786 842
787 my $darkness = $cell->[0] * (1 / 255); 843 my $dark = $cell->[0];
788 if ($darkness < 0) { 844 if ($dark < 0) {
789 $darkness = 0.15; 845 substr $darkness, $y * $sw4 + $x, 1, chr 224;
846 } else {
847 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
790 } 848 }
791 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
792 849
793 for my $num (grep $_, @$cell[1,2,3]) { 850 for my $num (grep $_, @$cell[1,2,3]) {
794 my $tex = $::CONN->{face}[$num]{texture} || next; 851 my $tex = $::CONN->{face}[$num]{texture} || next;
795 852
796 glBindTexture GL_TEXTURE_2D, $tex->{name};
797
798 my $w = $tex->{width}; 853 my $w = $tex->{width};
799 my $h = $tex->{height}; 854 my $h = $tex->{height};
800 855
801 my $px = ($x + 1) * 32 - $w; 856 $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;
810 } 857 }
811 } 858 }
812 } 859 }
813 860
814# if (1) { # higher quality darkness 861# if (1) { # higher quality darkness
819# 866#
820# $lighting = $pb->get_pixels; 867# $lighting = $pb->get_pixels;
821# $lighting =~ s/(.)../$1/gs; 868# $lighting =~ s/(.)../$1/gs;
822# } 869# }
823 870
824 $lighting = new Crossfire::Client::Texture 871 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
872 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
873
874 $darkness = new CFClient::Texture
825 width => $sw4, 875 width => $sw4,
826 height => $sh, 876 height => $sh,
827 data => $lighting, 877 data => $darkness,
828 internalformat => GL_ALPHA4, 878 internalformat => GL_ALPHA,
829 format => GL_ALPHA; 879 format => GL_ALPHA;
830 880
831 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 881 glColor 0.45, 0.45, 0.45, 1;
832 glColor 0, 0, 0, 0.75; 882 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
833 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 883
843 glDisable GL_TEXTURE_2D; 884 glDisable GL_TEXTURE_2D;
844 glDisable GL_BLEND; 885 glDisable GL_BLEND;
845} 886}
846 887
895 } 936 }
896} 937}
897 938
898############################################################################# 939#############################################################################
899 940
900package Crossfire::Client::Widget::Animator; 941package CFClient::Widget::Animator;
901 942
902use SDL::OpenGL; 943use SDL::OpenGL;
903 944
904our @ISA = Crossfire::Client::Widget::Bin::; 945our @ISA = CFClient::Widget::Bin::;
905 946
906sub moveto { 947sub moveto {
907 my ($self, $x, $y) = @_; 948 my ($self, $x, $y) = @_;
908 949
909 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 950 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
910 $self->{speed} = 0.2; 951 $self->{speed} = 0.001;
911 $self->{time} = 1; 952 $self->{time} = 1;
912 953
913 ::animation_start $self; 954 ::animation_start $self;
914} 955}
915 956
930 971
931sub _draw { 972sub _draw {
932 my ($self) = @_; 973 my ($self) = @_;
933 974
934 glPushMatrix; 975 glPushMatrix;
935 glRotate $self->{time} * 10000, 0, 1, 0; 976 glRotate $self->{time} * 1000, 0, 1, 0;
936 $self->{children}[0]->draw; 977 $self->{children}[0]->draw;
937 glPopMatrix; 978 glPopMatrix;
938} 979}
939 980
9401; 981#############################################################################
941 982
983package CFClient::Widget::Toplevel;
984
985our @ISA = CFClient::Widget::Container::;
986
987sub size_request {
988 ($::WIDTH, $::HEIGHT)
989}
990
991sub size_allocate {
992 my ($self, $w, $h) = @_;
993
994 $self->SUPER::size_allocate ($w, $h);
995
996 $_->size_allocate ($_->size_request)
997 for @{$self->{children}};
998}
999
1000sub translate {
1001 my ($self, $x, $y) = @_;
1002
1003 ($x, $y)
1004}
1005
1006sub update {
1007 my ($self) = @_;
1008
1009 $self->size_allocate ($self->size_request);
1010 ::refresh ();
1011}
1012
1013sub add {
1014 my ($self, $widget) = @_;
1015
1016 $self->SUPER::add ($widget);
1017
1018 $widget->size_allocate ($widget->size_request);
1019}
1020
1021sub draw {
1022 my ($self) = @_;
1023
1024 $self->_draw;
1025}
1026
1027#############################################################################
1028
1029package CFClient::Widget;
1030
1031$TOPLEVEL = new CFClient::Widget::Toplevel;
1032
10331
1034

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines