1 | package Crossfire::Client::Widget; |
1 | package CFClient::Widget; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | |
4 | |
5 | use Scalar::Util; |
5 | use Scalar::Util; |
6 | |
6 | |
7 | use SDL::OpenGL; |
7 | use SDL::OpenGL; |
8 | use SDL::OpenGL::Constants; |
8 | use SDL::OpenGL::Constants; |
9 | |
9 | |
10 | use Crossfire::Client; |
10 | use CFClient; |
11 | |
11 | |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
13 | |
13 | |
14 | our $TOPLEVEL; |
14 | our $TOPLEVEL; |
15 | our $BUTTON_STATE; |
15 | our $BUTTON_STATE; |
… | |
… | |
34 | $GRAB->update if $GRAB; |
34 | $GRAB->update if $GRAB; |
35 | } |
35 | } |
36 | |
36 | |
37 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
37 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
38 | |
38 | |
39 | $GRAB->button_down ($ev) if $GRAB; |
39 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
40 | } |
40 | } |
41 | |
41 | |
42 | sub feed_sdl_button_up_event { |
42 | sub feed_sdl_button_up_event { |
43 | my ($ev) = @_; |
43 | my ($ev) = @_; |
44 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
44 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
45 | |
45 | |
46 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
46 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
47 | |
47 | |
48 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
48 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
|
|
49 | |
|
|
50 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
49 | |
51 | |
50 | if (!$BUTTON_STATE) { |
52 | if (!$BUTTON_STATE) { |
51 | my $grab = $GRAB; undef $GRAB; |
53 | my $grab = $GRAB; undef $GRAB; |
52 | $grab->update if $grab; |
54 | $grab->update if $grab; |
53 | $GRAB->update if $GRAB; |
55 | $GRAB->update if $GRAB; |
… | |
… | |
65 | |
67 | |
66 | $hover->update if $hover; |
68 | $hover->update if $hover; |
67 | $HOVER->update if $HOVER; |
69 | $HOVER->update if $HOVER; |
68 | } |
70 | } |
69 | |
71 | |
70 | $HOVER->mouse_motion ($ev) if $HOVER; |
72 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
71 | } |
73 | } |
72 | |
74 | |
73 | sub new { |
75 | sub new { |
74 | my $class = shift; |
76 | my $class = shift; |
75 | |
77 | |
… | |
… | |
95 | sub size_allocate { |
97 | sub size_allocate { |
96 | my ($self, $w, $h) = @_; |
98 | my ($self, $w, $h) = @_; |
97 | |
99 | |
98 | $self->{w} = $w; |
100 | $self->{w} = $w; |
99 | $self->{h} = $h; |
101 | $self->{h} = $h; |
|
|
102 | } |
|
|
103 | |
|
|
104 | # translate global koordinates to local coordinate system |
|
|
105 | sub translate { |
|
|
106 | my ($self, $x, $y) = @_; |
|
|
107 | |
|
|
108 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
100 | } |
109 | } |
101 | |
110 | |
102 | sub focus_in { |
111 | sub focus_in { |
103 | my ($self) = @_; |
112 | my ($self) = @_; |
104 | |
113 | |
… | |
… | |
201 | #$self->deactivate; |
210 | #$self->deactivate; |
202 | } |
211 | } |
203 | |
212 | |
204 | ############################################################################# |
213 | ############################################################################# |
205 | |
214 | |
206 | package Crossfire::Client::Widget::Container; |
215 | package CFClient::Widget::Container; |
207 | |
216 | |
208 | our @ISA = Crossfire::Client::Widget::; |
217 | our @ISA = CFClient::Widget::; |
209 | |
218 | |
210 | sub new { |
219 | sub new { |
211 | my ($class, @widgets) = @_; |
220 | my ($class, @widgets) = @_; |
212 | |
221 | |
213 | my $self = $class->SUPER::new (children => []); |
222 | my $self = $class->SUPER::new (children => []); |
… | |
… | |
260 | $_->draw for @{$self->{children}}; |
269 | $_->draw for @{$self->{children}}; |
261 | } |
270 | } |
262 | |
271 | |
263 | ############################################################################# |
272 | ############################################################################# |
264 | |
273 | |
265 | package Crossfire::Client::Widget::Bin; |
274 | package CFClient::Widget::Bin; |
266 | |
275 | |
267 | our @ISA = Crossfire::Client::Widget::Container::; |
276 | our @ISA = CFClient::Widget::Container::; |
268 | |
277 | |
269 | sub child { $_[0]->{children}[0] } |
278 | sub child { $_[0]->{children}[0] } |
270 | |
279 | |
271 | sub size_request { |
280 | sub size_request { |
272 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
281 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
… | |
… | |
280 | if $self->{children}[0] |
289 | if $self->{children}[0] |
281 | } |
290 | } |
282 | |
291 | |
283 | ############################################################################# |
292 | ############################################################################# |
284 | |
293 | |
285 | package Crossfire::Client::Widget::Window; |
294 | package CFClient::Widget::Window; |
286 | |
295 | |
287 | our @ISA = Crossfire::Client::Widget::Bin::; |
296 | our @ISA = CFClient::Widget::Bin::; |
288 | |
297 | |
289 | use SDL::OpenGL; |
298 | use SDL::OpenGL; |
290 | |
299 | |
291 | sub new { |
300 | sub new { |
292 | my ($class, $x, $y, $z, $w, $h) = @_; |
301 | my ($class, $x, $y, $z, $w, $h) = @_; |
… | |
… | |
305 | |
314 | |
306 | sub render_chld { |
315 | sub render_chld { |
307 | my ($self) = @_; |
316 | my ($self) = @_; |
308 | |
317 | |
309 | $self->{texture} = |
318 | $self->{texture} = |
310 | Crossfire::Client::Texture->new_from_opengl ( |
319 | CFClient::Texture->new_from_opengl ( |
311 | $self->{w}, $self->{h}, sub { $self->child->draw } |
320 | $self->{w}, $self->{h}, sub { $self->child->draw } |
312 | ); |
321 | ); |
313 | } |
322 | } |
314 | |
323 | |
315 | sub size_allocate { |
324 | sub size_allocate { |
… | |
… | |
332 | or return; |
341 | or return; |
333 | |
342 | |
334 | glEnable GL_BLEND; |
343 | glEnable GL_BLEND; |
335 | glEnable GL_TEXTURE_2D; |
344 | glEnable GL_TEXTURE_2D; |
336 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
345 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
337 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
338 | |
346 | |
339 | glBegin GL_QUADS; |
347 | $tex->draw_quad (0, 0, $w, $h); |
340 | glTexCoord 0, 0; glVertex 0, 0; |
|
|
341 | glTexCoord 0, 1; glVertex 0, $h; |
|
|
342 | glTexCoord 1, 1; glVertex $w, $h; |
|
|
343 | glTexCoord 1, 0; glVertex $w, 0; |
|
|
344 | glEnd; |
|
|
345 | |
348 | |
346 | glDisable GL_BLEND; |
349 | glDisable GL_BLEND; |
347 | glDisable GL_TEXTURE_2D; |
350 | glDisable GL_TEXTURE_2D; |
348 | } |
351 | } |
349 | |
352 | |
350 | ############################################################################# |
353 | ############################################################################# |
351 | |
354 | |
352 | package Crossfire::Client::Widget::Frame; |
355 | package CFClient::Widget::Frame; |
353 | |
356 | |
354 | our @ISA = Crossfire::Client::Widget::Bin::; |
357 | our @ISA = CFClient::Widget::Bin::; |
355 | |
358 | |
356 | use SDL::OpenGL; |
359 | use SDL::OpenGL; |
357 | |
360 | |
358 | sub size_request { |
361 | sub size_request { |
359 | my ($self) = @_; |
362 | my ($self) = @_; |
… | |
… | |
382 | |
385 | |
383 | my ($w, $h) = $chld->size_request; |
386 | my ($w, $h) = $chld->size_request; |
384 | |
387 | |
385 | glBegin GL_QUADS; |
388 | glBegin GL_QUADS; |
386 | glColor 0, 0, 0; |
389 | glColor 0, 0, 0; |
387 | glTexCoord 0, 0; glVertex 0 , 0; |
390 | glVertex 0 , 0; |
388 | glTexCoord 0, 1; glVertex 0 , $h + 4; |
391 | glVertex 0 , $h + 4; |
389 | glTexCoord 1, 1; glVertex $w + 4 , $h + 4; |
392 | glVertex $w + 4 , $h + 4; |
390 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
393 | glVertex $w + 4 , 0; |
391 | glEnd; |
394 | glEnd; |
392 | |
395 | |
393 | $chld->draw; |
396 | $chld->draw; |
394 | } |
397 | } |
395 | |
398 | |
396 | ############################################################################# |
399 | ############################################################################# |
397 | |
400 | |
398 | package Crossfire::Client::Widget::FancyFrame; |
401 | package CFClient::Widget::FancyFrame; |
399 | |
402 | |
400 | our @ISA = Crossfire::Client::Widget::Bin::; |
403 | our @ISA = CFClient::Widget::Bin::; |
401 | |
404 | |
402 | use SDL::OpenGL; |
405 | use SDL::OpenGL; |
403 | |
406 | |
404 | my @tex = |
407 | my @tex = |
405 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
408 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
406 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
409 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
407 | |
410 | |
408 | sub size_request { |
411 | sub size_request { |
409 | my ($self) = @_; |
412 | my ($self) = @_; |
410 | |
413 | |
… | |
… | |
445 | glEnable GL_TEXTURE_2D; |
448 | glEnable GL_TEXTURE_2D; |
446 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
449 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
447 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
450 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
448 | |
451 | |
449 | my $top = $tex[1]; |
452 | my $top = $tex[1]; |
450 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
453 | $top->draw_quad (0, 0, $w, $top->{height}); |
451 | |
|
|
452 | glBegin GL_QUADS; |
|
|
453 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
454 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
|
|
455 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
|
|
456 | glTexCoord 1, 0; glVertex $w , 0; |
|
|
457 | glEnd; |
|
|
458 | |
454 | |
459 | my $left = $tex[3]; |
455 | my $left = $tex[3]; |
460 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
456 | $left->draw_quad (0, $top->{height}, $left->{width}, $ch); |
461 | |
|
|
462 | glBegin GL_QUADS; |
|
|
463 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
|
|
464 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
|
|
465 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
|
|
466 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
|
|
467 | glEnd; |
|
|
468 | |
457 | |
469 | my $right = $tex[2]; |
458 | my $right = $tex[2]; |
470 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
459 | $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); |
471 | |
|
|
472 | glBegin GL_QUADS; |
|
|
473 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
|
|
474 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
|
|
475 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
|
|
476 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
|
|
477 | glEnd; |
|
|
478 | |
460 | |
479 | my $bottom = $tex[4]; |
461 | my $bottom = $tex[4]; |
480 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
462 | $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); |
481 | |
|
|
482 | glBegin GL_QUADS; |
|
|
483 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
|
|
484 | glTexCoord 0, 1; glVertex 0 , $h; |
|
|
485 | glTexCoord 1, 1; glVertex $w , $h; |
|
|
486 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
|
|
487 | glEnd; |
|
|
488 | |
463 | |
489 | my $bg = $tex[0]; |
464 | my $bg = $tex[0]; |
490 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
465 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
491 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
466 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
492 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
467 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
493 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
468 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
494 | |
469 | |
495 | my $rep_x = $cw / $bg->{width}; |
470 | my $rep_x = $cw / $bg->{width}; |
496 | my $rep_y = $ch / $bg->{height}; |
471 | my $rep_y = $ch / $bg->{height}; |
497 | |
472 | |
498 | glBegin GL_QUADS; |
473 | $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); |
499 | glTexCoord 0, 0; glVertex $left->{width}, $top->{height}; |
|
|
500 | glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch; |
|
|
501 | glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch; |
|
|
502 | glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height}; |
|
|
503 | glEnd; |
|
|
504 | |
474 | |
505 | glDisable GL_BLEND; |
475 | glDisable GL_BLEND; |
506 | glDisable GL_TEXTURE_2D; |
476 | glDisable GL_TEXTURE_2D; |
507 | |
477 | |
508 | $self->child->draw; |
478 | $self->child->draw; |
509 | |
479 | |
510 | } |
480 | } |
511 | |
481 | |
512 | ############################################################################# |
482 | ############################################################################# |
513 | |
483 | |
514 | package Crossfire::Client::Widget::Table; |
484 | package CFClient::Widget::Table; |
515 | |
485 | |
516 | our @ISA = Crossfire::Client::Widget::Bin::; |
486 | our @ISA = CFClient::Widget::Bin::; |
517 | |
487 | |
518 | use SDL::OpenGL; |
488 | use SDL::OpenGL; |
519 | |
489 | |
520 | sub add { |
490 | sub add { |
521 | my ($self, $x, $y, $chld) = @_; |
491 | my ($self, $x, $y, $chld) = @_; |
… | |
… | |
596 | } |
566 | } |
597 | } |
567 | } |
598 | |
568 | |
599 | ############################################################################# |
569 | ############################################################################# |
600 | |
570 | |
601 | package Crossfire::Client::Widget::VBox; |
571 | package CFClient::Widget::VBox; |
602 | |
572 | |
603 | our @ISA = Crossfire::Client::Widget::Container::; |
573 | our @ISA = CFClient::Widget::Container::; |
604 | |
574 | |
605 | use SDL::OpenGL; |
575 | use SDL::OpenGL; |
606 | |
576 | |
607 | sub size_request { |
577 | sub size_request { |
608 | my ($self) = @_; |
578 | my ($self) = @_; |
… | |
… | |
656 | } |
626 | } |
657 | } |
627 | } |
658 | |
628 | |
659 | ############################################################################# |
629 | ############################################################################# |
660 | |
630 | |
661 | package Crossfire::Client::Widget::Label; |
631 | package CFClient::Widget::Label; |
662 | |
632 | |
663 | our @ISA = Crossfire::Client::Widget::; |
633 | our @ISA = CFClient::Widget::; |
664 | |
634 | |
665 | use SDL::OpenGL; |
635 | use SDL::OpenGL; |
666 | |
636 | |
667 | sub new { |
637 | sub new { |
668 | my ($class, $x, $y, $z, $height, $text) = @_; |
638 | my ($class, $x, $y, $z, $height, $text) = @_; |
669 | |
639 | |
670 | $height ||= $::FONTSIZE; |
640 | $height ||= $::FONTSIZE; |
671 | |
641 | |
672 | # TODO: color, and make height, xyz etc. optional |
642 | # TODO: color, and make height, xyz etc. optional |
673 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
643 | my $self = $class->SUPER::new ( |
|
|
644 | x => $x, |
|
|
645 | y => $y, |
|
|
646 | z => $z, |
|
|
647 | height => $height, |
|
|
648 | layout => new CFClient::Layout, |
|
|
649 | ); |
674 | |
650 | |
675 | $self->set_text ($text); |
651 | $self->set_text ($text); |
676 | |
652 | |
677 | $self |
653 | $self |
678 | } |
654 | } |
679 | |
655 | |
680 | sub set_text { |
656 | sub set_text { |
681 | my ($self, $text) = @_; |
657 | my ($self, $text) = @_; |
682 | |
658 | |
683 | $self->{text} = $text; |
659 | $self->{text} = $text; |
684 | $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; |
660 | $self->{layout}->set_markup ($text); |
685 | |
661 | |
686 | $self->update; |
662 | delete $self->{texture}; |
687 | } |
663 | } |
688 | |
664 | |
689 | sub get_text { |
665 | sub get_text { |
690 | my ($self, $text) = @_; |
666 | my ($self, $text) = @_; |
691 | |
667 | |
… | |
… | |
693 | } |
669 | } |
694 | |
670 | |
695 | sub size_request { |
671 | sub size_request { |
696 | my ($self) = @_; |
672 | my ($self) = @_; |
697 | |
673 | |
|
|
674 | $self->{layout}->set_width; |
|
|
675 | $self->{layout}->size |
698 | if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
676 | # if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
699 | ( |
677 | # ( |
700 | $self->{texture}{width}, |
678 | # $self->{texture}{width}, |
701 | $self->{texture}{height}, |
679 | # $self->{texture}{height}, |
702 | ) |
680 | # ) |
703 | } else { |
681 | # } else { |
704 | my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; |
682 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height}; |
705 | |
683 | # |
706 | ($w, $h) |
684 | # ($w, $h) |
707 | } |
685 | # } |
|
|
686 | } |
708 | |
687 | |
|
|
688 | sub size_allocate { |
|
|
689 | my ($self, $w, $h) = @_; |
|
|
690 | |
|
|
691 | $self->SUPER::size_allocate ($w, $h); |
|
|
692 | delete $self->{texture}; |
709 | } |
693 | } |
710 | |
694 | |
711 | sub _draw { |
695 | sub _draw { |
712 | my ($self) = @_; |
696 | my ($self) = @_; |
713 | |
697 | |
714 | my $tex = $self->{texture}; |
698 | my $tex = $self->{texture} ||= do { |
|
|
699 | $self->{layout}->set_width ($self->{w}); |
|
|
700 | new_from_layout CFClient::Texture $self->{layout}; |
|
|
701 | }; |
715 | |
702 | |
716 | glEnable GL_BLEND; |
703 | glEnable GL_BLEND; |
717 | glEnable GL_TEXTURE_2D; |
704 | glEnable GL_TEXTURE_2D; |
718 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
705 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
719 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
706 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
720 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
721 | |
707 | |
722 | glColor 1, 0, 0, 1; # TODO color |
708 | glColor 1, 0, 0, 1; # TODO color |
723 | |
709 | |
724 | glBegin GL_QUADS; |
710 | $tex->draw_quad (0, 0); |
725 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
726 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
|
|
727 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
|
|
728 | glTexCoord 1, 0; glVertex $tex->{width}, 0; |
|
|
729 | glEnd; |
|
|
730 | |
711 | |
731 | glDisable GL_BLEND; |
712 | glDisable GL_BLEND; |
732 | glDisable GL_TEXTURE_2D; |
713 | glDisable GL_TEXTURE_2D; |
733 | } |
714 | } |
734 | |
715 | |
735 | ############################################################################# |
716 | ############################################################################# |
736 | |
717 | |
737 | package Crossfire::Client::Widget::Entry; |
718 | package CFClient::Widget::Entry; |
738 | |
719 | |
739 | our @ISA = Crossfire::Client::Widget::Label::; |
720 | our @ISA = CFClient::Widget::Label::; |
740 | |
721 | |
741 | use SDL; |
722 | use SDL; |
742 | use SDL::OpenGL; |
723 | use SDL::OpenGL; |
743 | |
724 | |
744 | sub key_down { |
725 | sub key_down { |
… | |
… | |
753 | |
734 | |
754 | if ($sym == SDLK_BACKSPACE) { |
735 | if ($sym == SDLK_BACKSPACE) { |
755 | substr $text, -1, 1, ''; |
736 | substr $text, -1, 1, ''; |
756 | } elsif ($uni) { |
737 | } elsif ($uni) { |
757 | $text .= chr $uni; |
738 | $text .= chr $uni; |
758 | print "$uni <$text>\n";#d# |
|
|
759 | } |
739 | } |
760 | |
740 | |
761 | $self->set_text ($text); |
741 | $self->set_text ($text); |
762 | } |
742 | } |
763 | |
743 | |
764 | sub button_down { |
744 | sub button_down { |
765 | my ($self, $ev) = @_; |
745 | my ($self, $ev) = @_; |
766 | |
746 | |
767 | $self->focus_in; |
747 | $self->focus_in; |
|
|
748 | } |
|
|
749 | |
|
|
750 | sub mouse_motion { |
|
|
751 | my ($self, $ev, $x, $y) = @_; |
|
|
752 | printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
768 | } |
753 | } |
769 | |
754 | |
770 | sub _draw { |
755 | sub _draw { |
771 | my ($self) = @_; |
756 | my ($self) = @_; |
772 | |
757 | |
… | |
… | |
786 | $self->SUPER::_draw; |
771 | $self->SUPER::_draw; |
787 | } |
772 | } |
788 | |
773 | |
789 | ############################################################################# |
774 | ############################################################################# |
790 | |
775 | |
791 | package Crossfire::Client::Widget::MapWidget; |
776 | package CFClient::Widget::MapWidget; |
792 | |
777 | |
793 | use strict; |
778 | use strict; |
794 | |
779 | |
795 | use List::Util qw(min max); |
780 | use List::Util qw(min max); |
796 | |
781 | |
797 | use SDL; |
782 | use SDL; |
798 | use SDL::OpenGL; |
783 | use SDL::OpenGL; |
799 | use SDL::OpenGL::Constants; |
784 | use SDL::OpenGL::Constants; |
800 | |
785 | |
801 | our @ISA = Crossfire::Client::Widget::; |
786 | our @ISA = CFClient::Widget::; |
802 | |
787 | |
803 | sub key_down { |
788 | sub key_down { |
804 | print "MAPKEYDOWN\n"; |
789 | print "MAPKEYDOWN\n"; |
805 | } |
790 | } |
806 | |
791 | |
… | |
… | |
842 | glEnable GL_TEXTURE_2D; |
827 | glEnable GL_TEXTURE_2D; |
843 | glEnable GL_BLEND; |
828 | glEnable GL_BLEND; |
844 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
829 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
845 | |
830 | |
846 | my $sw4 = ($sw + 3) & ~3; |
831 | my $sw4 = ($sw + 3) & ~3; |
847 | my $lighting = "\x00" x ($sw4 * $sh); |
832 | my $darkness = "\x00" x ($sw4 * $sh); |
848 | |
833 | |
849 | for my $x (0 .. $sw - 1) { |
834 | for my $x (0 .. $sw - 1) { |
|
|
835 | my $row = $map->[$x + $xofs]; |
850 | for my $y (0 .. $sh - 1) { |
836 | for my $y (0 .. $sh - 1) { |
851 | |
837 | |
852 | my $cell = $map->[$x + $xofs][$y + $yofs] |
838 | my $cell = $row->[$y + $yofs] |
853 | or next; |
839 | or next; |
854 | |
840 | |
855 | my $darkness = $cell->[0] * (1 / 255); |
841 | my $dark = $cell->[0]; |
856 | if ($darkness < 0) { |
842 | if ($dark < 0) { |
857 | $darkness = $cell->[1] ? 0.1 : 0; |
843 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
|
|
844 | } else { |
|
|
845 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
858 | } |
846 | } |
859 | substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; |
|
|
860 | |
847 | |
861 | for my $num (grep $_, @$cell[1,2,3]) { |
848 | for my $num (grep $_, @$cell[1,2,3]) { |
862 | my $tex = $::CONN->{face}[$num]{texture} || next; |
849 | my $tex = $::CONN->{face}[$num]{texture} || next; |
863 | |
850 | |
864 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
865 | |
|
|
866 | my $w = $tex->{width}; |
851 | my $w = $tex->{width}; |
867 | my $h = $tex->{height}; |
852 | my $h = $tex->{height}; |
868 | |
853 | |
869 | my $px = ($x + 1) * 32 - $w; |
854 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
870 | my $py = ($y + 1) * 32 - $h; |
|
|
871 | |
|
|
872 | glBegin GL_QUADS; |
|
|
873 | glTexCoord 0, 0; glVertex $px , $py; |
|
|
874 | glTexCoord 0, 1; glVertex $px , $py + $h; |
|
|
875 | glTexCoord 1, 1; glVertex $px + $w, $py + $h; |
|
|
876 | glTexCoord 1, 0; glVertex $px + $w, $py; |
|
|
877 | glEnd; |
|
|
878 | } |
855 | } |
879 | } |
856 | } |
880 | } |
857 | } |
881 | |
858 | |
882 | # if (1) { # higher quality darkness |
859 | # if (1) { # higher quality darkness |
… | |
… | |
887 | # |
864 | # |
888 | # $lighting = $pb->get_pixels; |
865 | # $lighting = $pb->get_pixels; |
889 | # $lighting =~ s/(.)../$1/gs; |
866 | # $lighting =~ s/(.)../$1/gs; |
890 | # } |
867 | # } |
891 | |
868 | |
892 | $lighting = new Crossfire::Client::Texture |
869 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
870 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
871 | |
|
|
872 | $darkness = new CFClient::Texture |
893 | width => $sw4, |
873 | width => $sw4, |
894 | height => $sh, |
874 | height => $sh, |
895 | data => $lighting, |
875 | data => $darkness, |
896 | internalformat => GL_ALPHA, |
876 | internalformat => GL_ALPHA, |
897 | format => GL_ALPHA; |
877 | format => GL_ALPHA; |
898 | |
878 | |
899 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
900 | glColor 0.45, 0.45, 0.45, 1; |
879 | glColor 0.45, 0.45, 0.45, 1; |
901 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
880 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
902 | glBindTexture GL_TEXTURE_2D, $lighting->{name}; |
|
|
903 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
904 | glBegin GL_QUADS; |
|
|
905 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
906 | glTexCoord 0, 1; glVertex 0 , $sh * 32; |
|
|
907 | glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32; |
|
|
908 | glTexCoord 1, 0; glVertex $sw4 * 32, 0; |
|
|
909 | glEnd; |
|
|
910 | |
881 | |
911 | glDisable GL_TEXTURE_2D; |
882 | glDisable GL_TEXTURE_2D; |
912 | glDisable GL_BLEND; |
883 | glDisable GL_BLEND; |
913 | } |
884 | } |
914 | |
885 | |
… | |
… | |
963 | } |
934 | } |
964 | } |
935 | } |
965 | |
936 | |
966 | ############################################################################# |
937 | ############################################################################# |
967 | |
938 | |
968 | package Crossfire::Client::Widget::Animator; |
939 | package CFClient::Widget::Animator; |
969 | |
940 | |
970 | use SDL::OpenGL; |
941 | use SDL::OpenGL; |
971 | |
942 | |
972 | our @ISA = Crossfire::Client::Widget::Bin::; |
943 | our @ISA = CFClient::Widget::Bin::; |
973 | |
944 | |
974 | sub moveto { |
945 | sub moveto { |
975 | my ($self, $x, $y) = @_; |
946 | my ($self, $x, $y) = @_; |
976 | |
947 | |
977 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
948 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
978 | $self->{speed} = 2; |
949 | $self->{speed} = 0.001; |
979 | $self->{time} = 1; |
950 | $self->{time} = 1; |
980 | |
951 | |
981 | ::animation_start $self; |
952 | ::animation_start $self; |
982 | } |
953 | } |
983 | |
954 | |
… | |
… | |
1005 | glPopMatrix; |
976 | glPopMatrix; |
1006 | } |
977 | } |
1007 | |
978 | |
1008 | ############################################################################# |
979 | ############################################################################# |
1009 | |
980 | |
1010 | package Crossfire::Client::Widget::Toplevel; |
981 | package CFClient::Widget::Toplevel; |
1011 | |
982 | |
1012 | our @ISA = Crossfire::Client::Widget::Container::; |
983 | our @ISA = CFClient::Widget::Container::; |
1013 | |
984 | |
1014 | sub size_request { |
985 | sub size_request { |
1015 | ($::WIDTH, $::HEIGHT) |
986 | ($::WIDTH, $::HEIGHT) |
1016 | } |
987 | } |
1017 | |
988 | |
… | |
… | |
1022 | |
993 | |
1023 | $_->size_allocate ($_->size_request) |
994 | $_->size_allocate ($_->size_request) |
1024 | for @{$self->{children}}; |
995 | for @{$self->{children}}; |
1025 | } |
996 | } |
1026 | |
997 | |
|
|
998 | sub translate { |
|
|
999 | my ($self, $x, $y) = @_; |
|
|
1000 | |
|
|
1001 | ($x, $y) |
|
|
1002 | } |
|
|
1003 | |
1027 | sub update { |
1004 | sub update { |
1028 | my ($self) = @_; |
1005 | my ($self) = @_; |
1029 | |
1006 | |
1030 | $self->size_allocate ($self->size_request); |
1007 | $self->size_allocate ($self->size_request); |
1031 | ::refresh (); |
1008 | ::refresh (); |
… | |
… | |
1045 | $self->_draw; |
1022 | $self->_draw; |
1046 | } |
1023 | } |
1047 | |
1024 | |
1048 | ############################################################################# |
1025 | ############################################################################# |
1049 | |
1026 | |
1050 | package Crossfire::Client::Widget; |
1027 | package CFClient::Widget; |
1051 | |
1028 | |
1052 | $TOPLEVEL = new Crossfire::Client::Widget::Toplevel; |
1029 | $TOPLEVEL = new CFClient::Widget::Toplevel; |
1053 | |
1030 | |
1054 | 1 |
1031 | 1 |
1055 | |
1032 | |