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.40 by root, Sun Apr 9 21:41:11 2006 UTC vs.
Revision 1.57 by root, Mon Apr 10 22:53:49 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines