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.41 by root, Sun Apr 9 21:50:21 2006 UTC vs.
Revision 1.56 by root, Mon Apr 10 22:16:34 2006 UTC

7use SDL::OpenGL; 7use SDL::OpenGL;
8use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
9 9
10use Crossfire::Client; 10use Crossfire::Client;
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) 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 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab;
53 $GRAB->update if $GRAB;
54 }
55}
56
57sub feed_sdl_motion_event {
58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
60
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
62
63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget;
65
66 $hover->update if $hover;
67 $HOVER->update if $HOVER;
68 }
69
70 $HOVER->mouse_motion ($ev) if $HOVER;
71}
19 72
20sub new { 73sub new {
21 my $class = shift; 74 my $class = shift;
22 75
23 bless { @_ }, $class 76 bless { @_ }, $class
45 $self->{w} = $w; 98 $self->{w} = $w;
46 $self->{h} = $h; 99 $self->{h} = $h;
47} 100}
48 101
49sub focus_in { 102sub focus_in {
50 my ($widget) = @_; 103 my ($self) = @_;
51 $FOCUS = $widget; 104
105 my $focus = $FOCUS; $FOCUS = $self;
106 $focus->update if $focus;
107 $FOCUS->update;
52} 108}
53 109
54sub focus_out { 110sub focus_out {
55 my ($widget) = @_; 111 my ($self) = @_;
56}
57 112
58sub key_down { 113 return unless $FOCUS == $self;
59 my ($widget, $sdlev) = @_;
60}
61 114
62sub key_up { 115 my $focus = $FOCUS; undef $FOCUS;
63 my ($widget, $sdlev) = @_; 116 $focus->update if $focus; #?
64} 117}
65 118
119sub mouse_motion { }
120sub button_up { }
66sub button_down { 121sub button_down { }
67 my ($widget, $sdlev) = @_; 122sub key_down { }
68} 123sub key_up { }
69 124
70sub button_up {
71 my ($widget, $sdlev) = @_;
72}
73
74sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 125sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
75sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 126sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
76sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 127sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
77sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 128sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
78sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 129sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
79 130
80sub draw { 131sub draw {
81 my ($self) = @_; 132 my ($self) = @_;
82 133
83 glPushMatrix; 134 glPushMatrix;
84 glTranslate $self->{x}, $self->{y}, 0; 135 glTranslate $self->{x}, $self->{y}, 0;
85 $self->_draw; 136 $self->_draw;
137 if ($self == $HOVER) {
138 glColor 1, 1, 1, 0.4;
139 glEnable GL_BLEND;
140 glBegin GL_QUADS;
141 glVertex 0 , 0;
142 glVertex $self->{w}, 0;
143 glVertex $self->{w}, $self->{h};
144 glVertex 0 , $self->{h};
145 glEnd;
146 glDisable GL_BLEND;
147 }
86 glPopMatrix; 148 glPopMatrix;
87} 149}
88 150
89sub _draw { 151sub _draw {
90 my ($self) = @_; 152 my ($self) = @_;
162 224
163 @{$self->{children}} = 225 @{$self->{children}} =
164 sort { $a->{z} <=> $b->{z} } 226 sort { $a->{z} <=> $b->{z} }
165 @{$self->{children}}, $chld; 227 @{$self->{children}}, $chld;
166 228
167 $self->size_allocate ($self->{w}, $self->{h}); 229 $self->size_allocate ($self->{w}, $self->{h})
230 if $self->{w}; #TODO: check for "realised state"
168} 231}
169 232
170sub remove { 233sub remove {
171 my ($self, $widget) = @_; 234 my ($self, $widget) = @_;
172 235
176} 239}
177 240
178sub find_widget { 241sub find_widget {
179 my ($self, $x, $y) = @_; 242 my ($self, $x, $y) = @_;
180 243
244 $x -= $self->{x};
245 $y -= $self->{y};
246
181 my $res; 247 my $res;
182 248
183 for (@{ $self->{children} }) { 249 for (reverse @{ $self->{children} }) {
184 $res = $_->find_widget ($x, $y) 250 $res = $_->find_widget ($x, $y)
185 and return $res; 251 and return $res;
186 } 252 }
187 253
188 () 254 $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} 255}
203 256
204sub _draw { 257sub _draw {
205 my ($self) = @_; 258 my ($self) = @_;
206 259
219 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 272 $_[0]{children}[0]->size_request if $_[0]{children}[0];
220} 273}
221 274
222sub size_allocate { 275sub size_allocate {
223 my ($self, $w, $h) = @_; 276 my ($self, $w, $h) = @_;
277
224 $self->SUPER::size_allocate ($w, $h); 278 $self->SUPER::size_allocate ($w, $h);
225 $self->{children}[0]->size_allocate ($w, $h) 279 $self->{children}[0]->size_allocate ($w, $h)
226 if $self->{children}[0] 280 if $self->{children}[0]
227} 281}
228 282
229############################################################################# 283#############################################################################
230 284
231package Crossfire::Client::Widget::Toplevel; 285package Crossfire::Client::Widget::Window;
232 286
233our @ISA = Crossfire::Client::Widget::Container::; 287our @ISA = Crossfire::Client::Widget::Bin::;
288
289use SDL::OpenGL;
290
291sub new {
292 my ($class, $x, $y, $z, $w, $h) = @_;
293
294 my $self = $class->SUPER::new;
295
296 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
297}
234 298
235sub update { 299sub update {
236 my ($self) = @_; 300 my ($self) = @_;
237 301
238 ::refresh ();
239}
240
241#############################################################################
242
243package Crossfire::Client::Widget::Window;
244
245our @ISA = Crossfire::Client::Widget::Bin::;
246
247use SDL::OpenGL;
248
249sub add {
250 my ($self, $chld) = @_;
251 warn "ADD $chld\n";
252 $self->SUPER::add ($chld);
253 $chld->set_parent ($self);
254}
255
256sub remove {
257 my ($self) = @_;
258 # TODO FIXME: removing a child from a window will crash, see render_chld
259 # $self->update;
260}
261
262sub update {
263 my ($self) = @_;
264 $self->render_chld; 302 $self->render_chld;
303 $self->SUPER::update;
265} 304}
266 305
267sub render_chld { 306sub render_chld {
268 my ($self) = @_; 307 my ($self) = @_;
269 my $chld = $self->get;
270 my ($w, $h) = $self->size_request;
271 308
272 require Carp;
273 Carp::cluck "RENDERCHI $w $h";
274 warn "RENDERCHI $w $h\n";
275 $self->{texture} = 309 $self->{texture} =
276 Crossfire::Client::Texture->new_from_opengl ( 310 Crossfire::Client::Texture->new_from_opengl (
277 $w, $h, sub { $chld->draw } 311 $self->{w}, $self->{h}, sub { $self->child->draw }
278 ); 312 );
279 $self->{texture}->upload;
280}
281
282sub size_request {
283 my ($self) = @_;
284 ($self->w, $self->h)
285} 313}
286 314
287sub size_allocate { 315sub size_allocate {
288 my ($self, $w, $h) = @_; 316 my ($self, $w, $h) = @_;
289 317
290 $self->w ($w); 318 $self->{w} = $w;
291 $self->h ($h); 319 $self->{h} = $h;
320
292 $self->get->size_allocate ($w, $h); 321 $self->child->size_allocate ($w, $h);
293 322
294 $self->update; #TODO: Move this to the size_request event propably? 323 $self->render_chld;
295} 324}
296 325
297sub _draw { 326sub _draw {
298 my ($self) = @_; 327 my ($self) = @_;
299 328
303 or return; 332 or return;
304 333
305 glEnable GL_BLEND; 334 glEnable GL_BLEND;
306 glEnable GL_TEXTURE_2D; 335 glEnable GL_TEXTURE_2D;
307 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 336 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
308 glBindTexture GL_TEXTURE_2D, $tex->{name};
309 337
310 glBegin GL_QUADS; 338 $tex->draw_quad (0, 0, $w, $h);
311 glTexCoord 0, 0; glVertex 0, 0;
312 glTexCoord 0, 1; glVertex 0, $h;
313 glTexCoord 1, 1; glVertex $w, $h;
314 glTexCoord 1, 0; glVertex $w, 0;
315 glEnd;
316 339
317 glDisable GL_BLEND; 340 glDisable GL_BLEND;
318 glDisable GL_TEXTURE_2D; 341 glDisable GL_TEXTURE_2D;
319} 342}
320 343
336 map { $_ + 4 } $chld->size_request; 359 map { $_ + 4 } $chld->size_request;
337} 360}
338 361
339sub size_allocate { 362sub size_allocate {
340 my ($self, $w, $h) = @_; 363 my ($self, $w, $h) = @_;
341 364
342 $self->SUPER::size_allocate ($w, $h); 365 $self->{w} = $w;
366 $self->{h} = $h;
343 367
344 $self->child->size_allocate ($w - 4, $h - 4); 368 $self->child->size_allocate ($w - 4, $h - 4);
345 $self->child->move (2, 2); 369 $self->child->move (2, 2);
346} 370}
347 371
352 376
353 my ($w, $h) = $chld->size_request; 377 my ($w, $h) = $chld->size_request;
354 378
355 glBegin GL_QUADS; 379 glBegin GL_QUADS;
356 glColor 0, 0, 0; 380 glColor 0, 0, 0;
357 glTexCoord 0, 0; glVertex 0 , 0; 381 glVertex 0 , 0;
358 glTexCoord 0, 1; glVertex 0 , $h + 4; 382 glVertex 0 , $h + 4;
359 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 383 glVertex $w + 4 , $h + 4;
360 glTexCoord 1, 0; glVertex $w + 4 , 0; 384 glVertex $w + 4 , 0;
361 glEnd; 385 glEnd;
362 386
363 $chld->draw; 387 $chld->draw;
364} 388}
365 389
398 $w -= $tex[2]->{width}; 422 $w -= $tex[2]->{width};
399 $w -= $tex[3]->{width}; 423 $w -= $tex[3]->{width};
400 424
401 $h = $h < 0 ? 0 : $h; 425 $h = $h < 0 ? 0 : $h;
402 $w = $w < 0 ? 0 : $w; 426 $w = $w < 0 ? 0 : $w;
403 warn "CHILD:$w $h\n"; 427
404 $self->child->size_allocate ($w, $h); 428 $self->child->size_allocate ($w, $h);
405 $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 429 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
406} 430}
407 431
408sub _draw { 432sub _draw {
409 my ($self) = @_; 433 my ($self) = @_;
410 434
411 my ($w, $h) = ($self->w, $self->h); 435 my ($w, $h) = ($self->{w}, $self->{h});
412 my ($cw, $ch) = ($self->child->w, $self->child->h); 436 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
413 437
414 glEnable GL_BLEND; 438 glEnable GL_BLEND;
415 glEnable GL_TEXTURE_2D; 439 glEnable GL_TEXTURE_2D;
416 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 440 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
417 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 441 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
418 442
419 my $top = $tex[1]; 443 my $top = $tex[1];
420 glBindTexture GL_TEXTURE_2D, $top->{name}; 444 $top->draw_quad (0, 0, $w, $top->{height});
421
422 glBegin GL_QUADS;
423 glTexCoord 0, 0; glVertex 0 , 0;
424 glTexCoord 0, 1; glVertex 0 , $top->{height};
425 glTexCoord 1, 1; glVertex $w , $top->{height};
426 glTexCoord 1, 0; glVertex $w , 0;
427 glEnd;
428 445
429 my $left = $tex[3]; 446 my $left = $tex[3];
430 glBindTexture GL_TEXTURE_2D, $left->{name}; 447 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
431
432 glBegin GL_QUADS;
433 glTexCoord 0, 0; glVertex 0 , $top->{height};
434 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
435 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
436 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
437 glEnd;
438 448
439 my $right = $tex[2]; 449 my $right = $tex[2];
440 glBindTexture GL_TEXTURE_2D, $right->{name}; 450 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
441
442 glBegin GL_QUADS;
443 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
444 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
445 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
446 glTexCoord 1, 0; glVertex $w , $top->{height};
447 glEnd;
448 451
449 my $bottom = $tex[4]; 452 my $bottom = $tex[4];
450 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 453 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
451
452 glBegin GL_QUADS;
453 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
454 glTexCoord 0, 1; glVertex 0 , $h;
455 glTexCoord 1, 1; glVertex $w , $h;
456 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
457 glEnd;
458 454
459 my $bg = $tex[0]; 455 my $bg = $tex[0];
460 glBindTexture GL_TEXTURE_2D, $bg->{name}; 456 glBindTexture GL_TEXTURE_2D, $bg->{name};
461 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 457 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
462 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 458 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
463 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 459 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
464 460
465 my $rep_x = $cw / $bg->{width}; 461 my $rep_x = $cw / $bg->{width};
466 my $rep_y = $ch / $bg->{height}; 462 my $rep_y = $ch / $bg->{height};
467 463
468 glBegin GL_QUADS; 464 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
469 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
470 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
471 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
472 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
473 glEnd;
474 465
475 glDisable GL_BLEND; 466 glDisable GL_BLEND;
476 glDisable GL_TEXTURE_2D; 467 glDisable GL_TEXTURE_2D;
477 468
478 $self->child->draw; 469 $self->child->draw;
571package Crossfire::Client::Widget::VBox; 562package Crossfire::Client::Widget::VBox;
572 563
573our @ISA = Crossfire::Client::Widget::Container::; 564our @ISA = Crossfire::Client::Widget::Container::;
574 565
575use SDL::OpenGL; 566use SDL::OpenGL;
567
568sub size_request {
569 my ($self) = @_;
570
571 my @alloc = map [$_->size_request], @{$self->{children}};
572
573 (
574 (List::Util::max map $_->[0], @alloc),
575 (List::Util::sum map $_->[1], @alloc),
576 )
577}
576 578
577sub size_allocate { 579sub size_allocate {
578 my ($self, $w, $h) = @_; 580 my ($self, $w, $h) = @_;
579 581
580 $self->w ($w); 582 $self->w ($w);
613 $y += $h; 615 $y += $h;
614 } 616 }
615 } 617 }
616} 618}
617 619
618sub _draw {
619 my ($self) = @_;
620
621 my ($x, $y);
622 for (@{$self->{children} || []}) {
623 $_->draw;
624 $y += $_->h;
625 }
626}
627
628############################################################################# 620#############################################################################
629 621
630package Crossfire::Client::Widget::Label; 622package Crossfire::Client::Widget::Label;
631 623
632our @ISA = Crossfire::Client::Widget::; 624our @ISA = Crossfire::Client::Widget::;
634use SDL::OpenGL; 626use SDL::OpenGL;
635 627
636sub new { 628sub new {
637 my ($class, $x, $y, $z, $height, $text) = @_; 629 my ($class, $x, $y, $z, $height, $text) = @_;
638 630
631 $height ||= $::FONTSIZE;
632
639 # TODO: color, and make height, xyz etc. optional 633 # TODO: color, and make height, xyz etc. optional
640 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 634 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
641 635
642 $self->set_text ($text); 636 $self->set_text ($text);
643 637
660} 654}
661 655
662sub size_request { 656sub size_request {
663 my ($self) = @_; 657 my ($self) = @_;
664 658
665 ( 659 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
660 (
666 $self->{texture}{width}, 661 $self->{texture}{width},
667 $self->{texture}{height}, 662 $self->{texture}{height},
663 )
664 } else {
665 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height};
666
667 ($w, $h)
668 ) 668 }
669
669} 670}
670 671
671sub _draw { 672sub _draw {
672 my ($self) = @_; 673 my ($self) = @_;
673 674
675 676
676 glEnable GL_BLEND; 677 glEnable GL_BLEND;
677 glEnable GL_TEXTURE_2D; 678 glEnable GL_TEXTURE_2D;
678 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 679 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
679 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 680 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
680 glBindTexture GL_TEXTURE_2D, $tex->{name};
681 681
682 glColor 1, 0, 0, 1; # TODO color 682 glColor 1, 0, 0, 1; # TODO color
683 683
684 glBegin GL_QUADS; 684 $tex->draw_quad (0, 0);
685 glTexCoord 0, 0; glVertex 0 , 0;
686 glTexCoord 0, 1; glVertex 0 , $tex->{height};
687 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
688 glTexCoord 1, 0; glVertex $tex->{width}, 0;
689 glEnd;
690 685
691 glDisable GL_BLEND; 686 glDisable GL_BLEND;
692 glDisable GL_TEXTURE_2D; 687 glDisable GL_TEXTURE_2D;
693} 688}
694 689
695############################################################################# 690#############################################################################
696 691
697package Crossfire::Client::Widget::TextEntry; 692package Crossfire::Client::Widget::Entry;
698 693
699our @ISA = Crossfire::Client::Widget::Label::; 694our @ISA = Crossfire::Client::Widget::Label::;
700 695
701use SDL; 696use SDL;
702use SDL::OpenGL; 697use SDL::OpenGL;
705 my ($self, $ev) = @_; 700 my ($self, $ev) = @_;
706 701
707 my $mod = $ev->key_mod; 702 my $mod = $ev->key_mod;
708 my $sym = $ev->key_sym; 703 my $sym = $ev->key_sym;
709 704
710 $ev->set_unicode (1);
711 my $uni = $ev->key_unicode; 705 my $uni = $ev->key_unicode;
712 706
713 my $text = $self->get_text; 707 my $text = $self->get_text;
714 708
715 if ($sym == SDLK_BACKSPACE) { 709 if ($sym == SDLK_BACKSPACE) {
716 substr $text, -1, 1, ''; 710 substr $text, -1, 1, '';
717
718 } elsif ($uni) { 711 } elsif ($uni) {
719 $text .= chr $uni; 712 $text .= chr $uni;
713 print "$uni <$text>\n";#d#
720 } 714 }
715
721 $self->set_text ($text); 716 $self->set_text ($text);
717}
718
719sub button_down {
720 my ($self, $ev) = @_;
721
722 $self->focus_in;
723}
724
725sub _draw {
726 my ($self) = @_;
727
728 if ($FOCUS == $self) {
729 glColor 1, 1, 1;
730 } else {
731 glColor 0.7, 0.7, 0.7;
732 }
733
734 glBegin GL_QUADS;
735 glVertex 0 , 0;
736 glVertex 0 , $self->{h} - 1;
737 glVertex $self->{w} - 1, $self->{h} - 1;
738 glVertex $self->{w} - 1, 0;
739 glEnd;
740
741 $self->SUPER::_draw;
722} 742}
723 743
724############################################################################# 744#############################################################################
725 745
726package Crossfire::Client::Widget::MapWidget; 746package Crossfire::Client::Widget::MapWidget;
741 761
742sub key_up { 762sub key_up {
743} 763}
744 764
745sub size_request { 765sub size_request {
746 766 (
747} 767 1 + int $::WIDTH / 32,
748 768 1 + int $::HEIGHT / 32,
749sub size_allocate { 769 )
750} 770}
751 771
752sub _draw { 772sub _draw {
753 my ($self) = @_; 773 my ($self) = @_;
754 774
787 my $cell = $map->[$x + $xofs][$y + $yofs] 807 my $cell = $map->[$x + $xofs][$y + $yofs]
788 or next; 808 or next;
789 809
790 my $darkness = $cell->[0] * (1 / 255); 810 my $darkness = $cell->[0] * (1 / 255);
791 if ($darkness < 0) { 811 if ($darkness < 0) {
792 $darkness = 0.15; 812 $darkness = $cell->[1] ? 0.1 : 0;
793 } 813 }
794 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; 814 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
795 815
796 for my $num (grep $_, @$cell[1,2,3]) { 816 for my $num (grep $_, @$cell[1,2,3]) {
797 my $tex = $::CONN->{face}[$num]{texture} || next; 817 my $tex = $::CONN->{face}[$num]{texture} || next;
798 818
799 glBindTexture GL_TEXTURE_2D, $tex->{name};
800
801 my $w = $tex->{width}; 819 my $w = $tex->{width};
802 my $h = $tex->{height}; 820 my $h = $tex->{height};
803 821
804 my $px = ($x + 1) * 32 - $w; 822 my $px = ($x + 1) * 32 - $w;
805 my $py = ($y + 1) * 32 - $h; 823 my $py = ($y + 1) * 32 - $h;
806 824
807 glBegin GL_QUADS; 825 $tex->draw_quad ($px, $py, $w, $h);
808 glTexCoord 0, 0; glVertex $px , $py;
809 glTexCoord 0, 1; glVertex $px , $py + $h;
810 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
811 glTexCoord 1, 0; glVertex $px + $w, $py;
812 glEnd;
813 } 826 }
814 } 827 }
815 } 828 }
816 829
817# if (1) { # higher quality darkness 830# if (1) { # higher quality darkness
826 839
827 $lighting = new Crossfire::Client::Texture 840 $lighting = new Crossfire::Client::Texture
828 width => $sw4, 841 width => $sw4,
829 height => $sh, 842 height => $sh,
830 data => $lighting, 843 data => $lighting,
831 internalformat => GL_ALPHA4, 844 internalformat => GL_ALPHA,
832 format => GL_ALPHA; 845 format => GL_ALPHA;
833 846
834 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 847 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
835 glColor 0, 0, 0, 0.75; 848 glColor 0.45, 0.45, 0.45, 1;
836 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 849 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
837 glBindTexture GL_TEXTURE_2D, $lighting->{name}; 850 glBindTexture GL_TEXTURE_2D, $lighting->{name};
838 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 851 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
839 glBegin GL_QUADS; 852
840 glTexCoord 0, 0; glVertex 0 , 0; 853 $lighting->draw_quad (0, 0, $sw4 * 32, $sh * 32);
841 glTexCoord 0, 1; glVertex 0 , $sh * 32;
842 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
843 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
844 glEnd;
845 854
846 glDisable GL_TEXTURE_2D; 855 glDisable GL_TEXTURE_2D;
847 glDisable GL_BLEND; 856 glDisable GL_BLEND;
848} 857}
849 858
908 917
909sub moveto { 918sub moveto {
910 my ($self, $x, $y) = @_; 919 my ($self, $x, $y) = @_;
911 920
912 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 921 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
913 $self->{speed} = 0.2; 922 $self->{speed} = 0.001;
914 $self->{time} = 1; 923 $self->{time} = 1;
915 924
916 ::animation_start $self; 925 ::animation_start $self;
917} 926}
918 927
919sub animate { 928sub animate {
920 my ($self, $interval) = @_; 929 my ($self, $interval) = @_;
930
931 printf "%5.2f\n", 1 / $interval;#d#
921 932
922 $self->{time} -= $interval * $self->{speed}; 933 $self->{time} -= $interval * $self->{speed};
923 if ($self->{time} <= 0) { 934 if ($self->{time} <= 0) {
924 $self->{time} = 0; 935 $self->{time} = 0;
925 ::animation_stop $self; 936 ::animation_stop $self;
933 944
934sub _draw { 945sub _draw {
935 my ($self) = @_; 946 my ($self) = @_;
936 947
937 glPushMatrix; 948 glPushMatrix;
938 glRotate $self->{time} * 10000, 0, 1, 0; 949 glRotate $self->{time} * 1000, 0, 1, 0;
939 $self->{children}[0]->draw; 950 $self->{children}[0]->draw;
940 glPopMatrix; 951 glPopMatrix;
941} 952}
942 953
9431; 954#############################################################################
944 955
956package Crossfire::Client::Widget::Toplevel;
957
958our @ISA = Crossfire::Client::Widget::Container::;
959
960sub size_request {
961 ($::WIDTH, $::HEIGHT)
962}
963
964sub size_allocate {
965 my ($self, $w, $h) = @_;
966
967 $self->SUPER::size_allocate ($w, $h);
968
969 $_->size_allocate ($_->size_request)
970 for @{$self->{children}};
971}
972
973sub update {
974 my ($self) = @_;
975
976 $self->size_allocate ($self->size_request);
977 ::refresh ();
978}
979
980sub add {
981 my ($self, $widget) = @_;
982
983 $self->SUPER::add ($widget);
984
985 $widget->size_allocate ($widget->size_request);
986}
987
988sub draw {
989 my ($self) = @_;
990
991 $self->_draw;
992}
993
994#############################################################################
995
996package Crossfire::Client::Widget;
997
998$TOPLEVEL = new Crossfire::Client::Widget::Toplevel;
999
10001
1001

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines