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 | |
76 | bless { @_ }, $class |
78 | bless { |
|
|
79 | x => 0, |
|
|
80 | y => 0, |
|
|
81 | z => 0, |
|
|
82 | @_ |
|
|
83 | }, $class |
77 | } |
84 | } |
78 | |
85 | |
79 | sub move { |
86 | sub move { |
80 | my ($self, $x, $y, $z) = @_; |
87 | my ($self, $x, $y, $z) = @_; |
81 | $self->{x} = $x; |
88 | $self->{x} = $x; |
… | |
… | |
95 | sub size_allocate { |
102 | sub size_allocate { |
96 | my ($self, $w, $h) = @_; |
103 | my ($self, $w, $h) = @_; |
97 | |
104 | |
98 | $self->{w} = $w; |
105 | $self->{w} = $w; |
99 | $self->{h} = $h; |
106 | $self->{h} = $h; |
|
|
107 | } |
|
|
108 | |
|
|
109 | # translate global koordinates to local coordinate system |
|
|
110 | sub translate { |
|
|
111 | my ($self, $x, $y) = @_; |
|
|
112 | |
|
|
113 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
100 | } |
114 | } |
101 | |
115 | |
102 | sub focus_in { |
116 | sub focus_in { |
103 | my ($self) = @_; |
117 | my ($self) = @_; |
104 | |
118 | |
… | |
… | |
201 | #$self->deactivate; |
215 | #$self->deactivate; |
202 | } |
216 | } |
203 | |
217 | |
204 | ############################################################################# |
218 | ############################################################################# |
205 | |
219 | |
|
|
220 | package CFClient::Widget::Empty; |
|
|
221 | |
|
|
222 | our @ISA = CFClient::Widget::; |
|
|
223 | |
|
|
224 | sub size_request { |
|
|
225 | (0, 0) |
|
|
226 | } |
|
|
227 | |
|
|
228 | sub draw { |
|
|
229 | } |
|
|
230 | |
|
|
231 | ############################################################################# |
|
|
232 | |
206 | package Crossfire::Client::Widget::Container; |
233 | package CFClient::Widget::Container; |
207 | |
234 | |
208 | our @ISA = Crossfire::Client::Widget::; |
235 | our @ISA = CFClient::Widget::; |
209 | |
236 | |
210 | sub new { |
237 | sub new { |
211 | my ($class, @widgets) = @_; |
238 | my ($class, %arg) = @_; |
212 | |
239 | |
|
|
240 | my $children = delete $arg{children} || []; |
|
|
241 | |
213 | my $self = $class->SUPER::new (children => []); |
242 | my $self = $class->SUPER::new (children => [], %arg); |
214 | $self->add ($_) for @widgets; |
243 | $self->add ($_) for @$children; |
215 | |
244 | |
216 | $self |
245 | $self |
217 | } |
246 | } |
218 | |
247 | |
219 | sub add { |
248 | sub add { |
220 | my ($self, $chld, $expand) = @_; |
249 | my ($self, $chld, $expand) = @_; |
221 | |
250 | |
222 | $chld->{expand} = $expand; |
251 | $chld->{expand} = $expand; |
223 | $chld->set_parent ($self); |
252 | $chld->set_parent ($self); |
224 | |
253 | |
225 | @{$self->{children}} = |
254 | $self->{children} = [ |
226 | sort { $a->{z} <=> $b->{z} } |
255 | sort { $a->{z} <=> $b->{z} } |
227 | @{$self->{children}}, $chld; |
256 | @{$self->{children}}, $chld |
|
|
257 | ]; |
228 | |
258 | |
229 | $self->size_allocate ($self->{w}, $self->{h}) |
259 | $self->size_allocate ($self->{w}, $self->{h}) |
230 | if $self->{w}; #TODO: check for "realised state" |
260 | if $self->{w}; #TODO: check for "realised state" |
231 | } |
261 | } |
232 | |
262 | |
… | |
… | |
260 | $_->draw for @{$self->{children}}; |
290 | $_->draw for @{$self->{children}}; |
261 | } |
291 | } |
262 | |
292 | |
263 | ############################################################################# |
293 | ############################################################################# |
264 | |
294 | |
265 | package Crossfire::Client::Widget::Bin; |
295 | package CFClient::Widget::Bin; |
266 | |
296 | |
267 | our @ISA = Crossfire::Client::Widget::Container::; |
297 | our @ISA = CFClient::Widget::Container::; |
|
|
298 | |
|
|
299 | sub new { |
|
|
300 | my ($class, %arg) = @_; |
|
|
301 | |
|
|
302 | my $child = (delete $arg{child}) || new CFClient::Widget::Empty::; |
|
|
303 | |
|
|
304 | $class->SUPER::new (children => [$child], %arg) |
|
|
305 | } |
|
|
306 | |
|
|
307 | sub add { |
|
|
308 | my ($self, $widget) = @_; |
|
|
309 | |
|
|
310 | $self->{children} = []; |
|
|
311 | |
|
|
312 | $self->SUPER::add ($widget); |
|
|
313 | } |
|
|
314 | |
|
|
315 | sub remove { |
|
|
316 | my ($self, $widget) = @_; |
|
|
317 | |
|
|
318 | $self->SUPER::remove ($widget); |
|
|
319 | |
|
|
320 | $self->{children} = [new CFClient::Widget::Empty] |
|
|
321 | unless @{$self->{children}}; |
|
|
322 | } |
268 | |
323 | |
269 | sub child { $_[0]->{children}[0] } |
324 | sub child { $_[0]->{children}[0] } |
270 | |
325 | |
271 | sub size_request { |
326 | sub size_request { |
272 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
327 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
… | |
… | |
280 | if $self->{children}[0] |
335 | if $self->{children}[0] |
281 | } |
336 | } |
282 | |
337 | |
283 | ############################################################################# |
338 | ############################################################################# |
284 | |
339 | |
285 | package Crossfire::Client::Widget::Window; |
340 | package CFClient::Widget::Window; |
286 | |
341 | |
287 | our @ISA = Crossfire::Client::Widget::Bin::; |
342 | our @ISA = CFClient::Widget::Bin::; |
288 | |
343 | |
289 | use SDL::OpenGL; |
344 | use SDL::OpenGL; |
290 | |
345 | |
291 | sub new { |
346 | sub new { |
292 | my ($class, $x, $y, $z, $w, $h) = @_; |
347 | my ($class, %arg) = @_; |
293 | |
348 | |
294 | my $self = $class->SUPER::new; |
349 | my $self = $class->SUPER::new (%arg); |
295 | |
|
|
296 | @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); |
|
|
297 | } |
350 | } |
298 | |
351 | |
299 | sub update { |
352 | sub update { |
300 | my ($self) = @_; |
353 | my ($self) = @_; |
301 | |
354 | |
|
|
355 | # we want to do this delayed... |
302 | $self->render_chld; |
356 | $self->render_chld; |
303 | $self->SUPER::update; |
357 | $self->SUPER::update; |
304 | } |
358 | } |
305 | |
359 | |
306 | sub render_chld { |
360 | sub render_chld { |
307 | my ($self) = @_; |
361 | my ($self) = @_; |
308 | |
362 | |
309 | $self->{texture} = |
363 | $self->{texture} = |
310 | Crossfire::Client::Texture->new_from_opengl ( |
364 | CFClient::Texture->new_from_opengl ( |
311 | $self->{w}, $self->{h}, sub { $self->child->draw } |
365 | $self->{w}, $self->{h}, sub { $self->child->draw } |
312 | ); |
366 | ); |
313 | } |
367 | } |
314 | |
368 | |
315 | sub size_allocate { |
369 | sub size_allocate { |
… | |
… | |
341 | glDisable GL_TEXTURE_2D; |
395 | glDisable GL_TEXTURE_2D; |
342 | } |
396 | } |
343 | |
397 | |
344 | ############################################################################# |
398 | ############################################################################# |
345 | |
399 | |
346 | package Crossfire::Client::Widget::Frame; |
400 | package CFClient::Widget::Frame; |
347 | |
401 | |
348 | our @ISA = Crossfire::Client::Widget::Bin::; |
402 | our @ISA = CFClient::Widget::Bin::; |
349 | |
403 | |
350 | use SDL::OpenGL; |
404 | use SDL::OpenGL; |
351 | |
405 | |
352 | sub size_request { |
406 | sub size_request { |
353 | my ($self) = @_; |
407 | my ($self) = @_; |
… | |
… | |
387 | $chld->draw; |
441 | $chld->draw; |
388 | } |
442 | } |
389 | |
443 | |
390 | ############################################################################# |
444 | ############################################################################# |
391 | |
445 | |
392 | package Crossfire::Client::Widget::FancyFrame; |
446 | package CFClient::Widget::FancyFrame; |
393 | |
447 | |
394 | our @ISA = Crossfire::Client::Widget::Bin::; |
448 | our @ISA = CFClient::Widget::Bin::; |
395 | |
449 | |
396 | use SDL::OpenGL; |
450 | use SDL::OpenGL; |
397 | |
451 | |
398 | my @tex = |
452 | my @tex = |
399 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
453 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
400 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
454 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
401 | |
455 | |
402 | sub size_request { |
456 | sub size_request { |
403 | my ($self) = @_; |
457 | my ($self) = @_; |
404 | |
458 | |
… | |
… | |
423 | $w -= $tex[3]->{width}; |
477 | $w -= $tex[3]->{width}; |
424 | |
478 | |
425 | $h = $h < 0 ? 0 : $h; |
479 | $h = $h < 0 ? 0 : $h; |
426 | $w = $w < 0 ? 0 : $w; |
480 | $w = $w < 0 ? 0 : $w; |
427 | |
481 | |
|
|
482 | my $child = $self->child; |
|
|
483 | |
428 | $self->child->size_allocate ($w, $h); |
484 | $child->size_allocate ($w, $h); |
429 | $self->child->move ($tex[3]->{width}, $tex[1]->{height}); |
485 | $child->move ($tex[3]->{width}, $tex[1]->{height}); |
430 | } |
486 | } |
431 | |
487 | |
432 | sub _draw { |
488 | sub _draw { |
433 | my ($self) = @_; |
489 | my ($self) = @_; |
434 | |
490 | |
… | |
… | |
470 | |
526 | |
471 | } |
527 | } |
472 | |
528 | |
473 | ############################################################################# |
529 | ############################################################################# |
474 | |
530 | |
475 | package Crossfire::Client::Widget::Table; |
531 | package CFClient::Widget::Table; |
476 | |
532 | |
477 | our @ISA = Crossfire::Client::Widget::Bin::; |
533 | our @ISA = CFClient::Widget::Bin::; |
478 | |
534 | |
479 | use SDL::OpenGL; |
535 | use SDL::OpenGL; |
480 | |
536 | |
481 | sub add { |
537 | sub add { |
482 | my ($self, $x, $y, $chld) = @_; |
538 | my ($self, $x, $y, $chld) = @_; |
… | |
… | |
557 | } |
613 | } |
558 | } |
614 | } |
559 | |
615 | |
560 | ############################################################################# |
616 | ############################################################################# |
561 | |
617 | |
562 | package Crossfire::Client::Widget::VBox; |
618 | package CFClient::Widget::VBox; |
563 | |
619 | |
564 | our @ISA = Crossfire::Client::Widget::Container::; |
620 | our @ISA = CFClient::Widget::Container::; |
565 | |
621 | |
566 | use SDL::OpenGL; |
622 | use SDL::OpenGL; |
567 | |
623 | |
568 | sub size_request { |
624 | sub size_request { |
569 | my ($self) = @_; |
625 | my ($self) = @_; |
… | |
… | |
617 | } |
673 | } |
618 | } |
674 | } |
619 | |
675 | |
620 | ############################################################################# |
676 | ############################################################################# |
621 | |
677 | |
622 | package Crossfire::Client::Widget::Label; |
678 | package CFClient::Widget::Label; |
623 | |
679 | |
624 | our @ISA = Crossfire::Client::Widget::; |
680 | our @ISA = CFClient::Widget::; |
625 | |
681 | |
626 | use SDL::OpenGL; |
682 | use SDL::OpenGL; |
627 | |
683 | |
628 | sub new { |
684 | sub new { |
629 | my ($class, $x, $y, $z, $height, $text) = @_; |
685 | my ($class, %arg) = @_; |
630 | |
|
|
631 | $height ||= $::FONTSIZE; |
|
|
632 | |
686 | |
633 | # TODO: color, and make height, xyz etc. optional |
687 | # TODO: color, and make height, xyz etc. optional |
634 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
688 | my $self = $class->SUPER::new ( |
|
|
689 | color => [1, 1, 1], |
|
|
690 | height => $::FONTSIZE, |
|
|
691 | text => "", |
|
|
692 | layout => new CFClient::Layout, |
|
|
693 | %arg |
|
|
694 | ); |
635 | |
695 | |
636 | $self->set_text ($text); |
696 | $self->set_text ($self->{text}); |
637 | |
697 | |
638 | $self |
698 | $self |
639 | } |
699 | } |
640 | |
700 | |
641 | sub set_text { |
701 | sub set_text { |
642 | my ($self, $text) = @_; |
702 | my ($self, $text) = @_; |
643 | |
703 | |
644 | $self->{text} = $text; |
704 | $self->{text} = $text; |
645 | $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; |
705 | $self->{layout}->set_markup ($text); |
646 | |
706 | |
647 | $self->update; |
707 | delete $self->{texture}; |
648 | } |
708 | } |
649 | |
709 | |
650 | sub get_text { |
710 | sub get_text { |
651 | my ($self, $text) = @_; |
711 | my ($self, $text) = @_; |
652 | |
712 | |
… | |
… | |
654 | } |
714 | } |
655 | |
715 | |
656 | sub size_request { |
716 | sub size_request { |
657 | my ($self) = @_; |
717 | my ($self) = @_; |
658 | |
718 | |
|
|
719 | $self->{layout}->set_width; |
|
|
720 | $self->{layout}->set_height ($self->{height}); |
|
|
721 | $self->{layout}->size |
659 | if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
722 | # if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
660 | ( |
723 | # ( |
661 | $self->{texture}{width}, |
724 | # $self->{texture}{width}, |
662 | $self->{texture}{height}, |
725 | # $self->{texture}{height}, |
663 | ) |
726 | # ) |
664 | } else { |
727 | # } else { |
665 | my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; |
728 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height}; |
666 | |
729 | # |
667 | ($w, $h) |
730 | # ($w, $h) |
668 | } |
731 | # } |
|
|
732 | } |
669 | |
733 | |
|
|
734 | sub size_allocate { |
|
|
735 | my ($self, $w, $h) = @_; |
|
|
736 | |
|
|
737 | $self->SUPER::size_allocate ($w, $h); |
|
|
738 | delete $self->{texture}; |
670 | } |
739 | } |
671 | |
740 | |
672 | sub _draw { |
741 | sub _draw { |
673 | my ($self) = @_; |
742 | my ($self) = @_; |
674 | |
743 | |
675 | my $tex = $self->{texture}; |
744 | my $tex = $self->{texture} ||= do { |
|
|
745 | $self->{layout}->set_width ($self->{w}); |
|
|
746 | new_from_layout CFClient::Texture $self->{layout}; |
|
|
747 | }; |
676 | |
748 | |
677 | glEnable GL_BLEND; |
749 | glEnable GL_BLEND; |
678 | glEnable GL_TEXTURE_2D; |
750 | glEnable GL_TEXTURE_2D; |
679 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
751 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
680 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
752 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
681 | |
753 | |
682 | glColor 1, 0, 0, 1; # TODO color |
754 | glColor @{$self->{color}}; |
683 | |
755 | |
684 | $tex->draw_quad (0, 0); |
756 | $tex->draw_quad (0, 0); |
685 | |
757 | |
686 | glDisable GL_BLEND; |
758 | glDisable GL_BLEND; |
687 | glDisable GL_TEXTURE_2D; |
759 | glDisable GL_TEXTURE_2D; |
688 | } |
760 | } |
689 | |
761 | |
690 | ############################################################################# |
762 | ############################################################################# |
691 | |
763 | |
692 | package Crossfire::Client::Widget::Entry; |
764 | package CFClient::Widget::Entry; |
693 | |
765 | |
694 | our @ISA = Crossfire::Client::Widget::Label::; |
766 | our @ISA = CFClient::Widget::Label::; |
695 | |
767 | |
696 | use SDL; |
768 | use SDL; |
697 | use SDL::OpenGL; |
769 | use SDL::OpenGL; |
698 | |
770 | |
699 | sub key_down { |
771 | sub key_down { |
… | |
… | |
708 | |
780 | |
709 | if ($sym == SDLK_BACKSPACE) { |
781 | if ($sym == SDLK_BACKSPACE) { |
710 | substr $text, -1, 1, ''; |
782 | substr $text, -1, 1, ''; |
711 | } elsif ($uni) { |
783 | } elsif ($uni) { |
712 | $text .= chr $uni; |
784 | $text .= chr $uni; |
713 | print "$uni <$text>\n";#d# |
|
|
714 | } |
785 | } |
715 | |
786 | |
716 | $self->set_text ($text); |
787 | $self->set_text ($text); |
717 | } |
788 | } |
718 | |
789 | |
719 | sub button_down { |
790 | sub button_down { |
720 | my ($self, $ev) = @_; |
791 | my ($self, $ev) = @_; |
721 | |
792 | |
722 | $self->focus_in; |
793 | $self->focus_in; |
|
|
794 | } |
|
|
795 | |
|
|
796 | sub mouse_motion { |
|
|
797 | my ($self, $ev, $x, $y) = @_; |
|
|
798 | printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
723 | } |
799 | } |
724 | |
800 | |
725 | sub _draw { |
801 | sub _draw { |
726 | my ($self) = @_; |
802 | my ($self) = @_; |
727 | |
803 | |
… | |
… | |
741 | $self->SUPER::_draw; |
817 | $self->SUPER::_draw; |
742 | } |
818 | } |
743 | |
819 | |
744 | ############################################################################# |
820 | ############################################################################# |
745 | |
821 | |
746 | package Crossfire::Client::Widget::MapWidget; |
822 | package CFClient::Widget::MapWidget; |
747 | |
823 | |
748 | use strict; |
824 | use strict; |
749 | |
825 | |
750 | use List::Util qw(min max); |
826 | use List::Util qw(min max); |
751 | |
827 | |
752 | use SDL; |
828 | use SDL; |
753 | use SDL::OpenGL; |
829 | use SDL::OpenGL; |
754 | use SDL::OpenGL::Constants; |
830 | use SDL::OpenGL::Constants; |
755 | |
831 | |
756 | our @ISA = Crossfire::Client::Widget::; |
832 | our @ISA = CFClient::Widget::; |
|
|
833 | |
|
|
834 | sub new { |
|
|
835 | my $class = shift; |
|
|
836 | |
|
|
837 | $class->SUPER::new ( |
|
|
838 | z => -1, |
|
|
839 | list => (glGenLists 1), |
|
|
840 | @_ |
|
|
841 | ) |
|
|
842 | } |
757 | |
843 | |
758 | sub key_down { |
844 | sub key_down { |
759 | print "MAPKEYDOWN\n"; |
845 | print "MAPKEYDOWN\n"; |
760 | } |
846 | } |
761 | |
847 | |
… | |
… | |
767 | 1 + int $::WIDTH / 32, |
853 | 1 + int $::WIDTH / 32, |
768 | 1 + int $::HEIGHT / 32, |
854 | 1 + int $::HEIGHT / 32, |
769 | ) |
855 | ) |
770 | } |
856 | } |
771 | |
857 | |
|
|
858 | sub update { |
|
|
859 | my ($self) = @_; |
|
|
860 | |
|
|
861 | $self->{need_update} = 1; |
|
|
862 | } |
|
|
863 | |
772 | sub _draw { |
864 | sub _draw { |
773 | my ($self) = @_; |
865 | my ($self) = @_; |
774 | |
866 | |
|
|
867 | if (delete $self->{need_update}) { |
|
|
868 | glNewList $self->{list}, GL_COMPILE; |
|
|
869 | |
775 | my $mx = $::CONN->{mapx}; |
870 | my $mx = $::CONN->{mapx}; |
776 | my $my = $::CONN->{mapy}; |
871 | my $my = $::CONN->{mapy}; |
777 | |
872 | |
778 | my $map = $::CONN->{map}; |
873 | my $map = $::CONN->{map}; |
779 | |
874 | |
780 | my ($xofs, $yofs); |
875 | my ($xofs, $yofs); |
781 | |
876 | |
782 | my $sw = 1 + int $::WIDTH / 32; |
877 | my $sw = 1 + int $::WIDTH / 32; |
783 | my $sh = 1 + int $::HEIGHT / 32; |
878 | my $sh = 1 + int $::HEIGHT / 32; |
784 | |
879 | |
785 | if ($::CONN->{mapw} > $sw) { |
880 | if ($::CONN->{mapw} > $sw) { |
786 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
881 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
787 | } else { |
882 | } else { |
788 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
883 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
789 | } |
884 | } |
790 | |
885 | |
791 | if ($::CONN->{maph} > $sh) { |
886 | if ($::CONN->{maph} > $sh) { |
792 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
887 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
793 | } else { |
888 | } else { |
794 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
889 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
795 | } |
890 | } |
796 | |
891 | |
797 | glEnable GL_TEXTURE_2D; |
892 | glEnable GL_TEXTURE_2D; |
798 | glEnable GL_BLEND; |
893 | glEnable GL_BLEND; |
799 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
894 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
800 | |
895 | |
801 | my $sw4 = ($sw + 3) & ~3; |
896 | my $sw4 = ($sw + 3) & ~3; |
802 | my $darkness = "\x00" x ($sw4 * $sh); |
897 | my $darkness = "\x00" x ($sw4 * $sh); |
803 | |
898 | |
804 | for my $x (0 .. $sw - 1) { |
899 | for my $x (0 .. $sw - 1) { |
805 | my $row = $map->[$x + $xofs]; |
900 | my $row = $map->[$x + $xofs]; |
806 | for my $y (0 .. $sh - 1) { |
901 | for my $y (0 .. $sh - 1) { |
807 | |
902 | |
808 | my $cell = $row->[$y + $yofs] |
903 | my $cell = $row->[$y + $yofs] |
809 | or next; |
904 | or next; |
810 | |
905 | |
811 | my $dark = $cell->[0]; |
906 | my $dark = $cell->[0]; |
812 | if ($dark < 0) { |
907 | if ($dark < 0) { |
813 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
908 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
814 | } else { |
909 | } else { |
815 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
910 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
816 | } |
911 | } |
817 | |
912 | |
818 | for my $num (grep $_, @$cell[1,2,3]) { |
913 | for my $num (grep $_, @$cell[1,2,3]) { |
819 | my $tex = $::CONN->{face}[$num]{texture} || next; |
914 | my $tex = $::CONN->{face}[$num]{texture} || next; |
820 | |
915 | |
821 | my $w = $tex->{width}; |
916 | my $w = $tex->{width}; |
822 | my $h = $tex->{height}; |
917 | my $h = $tex->{height}; |
823 | |
918 | |
824 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
919 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
|
|
920 | } |
825 | } |
921 | } |
826 | } |
922 | } |
827 | } |
|
|
828 | |
923 | |
829 | # if (1) { # higher quality darkness |
924 | # if (1) { # higher quality darkness |
830 | # $lighting =~ s/(.)/$1$1$1/gs; |
925 | # $lighting =~ s/(.)/$1$1$1/gs; |
831 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
926 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
832 | # |
927 | # |
… | |
… | |
834 | # |
929 | # |
835 | # $lighting = $pb->get_pixels; |
930 | # $lighting = $pb->get_pixels; |
836 | # $lighting =~ s/(.)../$1/gs; |
931 | # $lighting =~ s/(.)../$1/gs; |
837 | # } |
932 | # } |
838 | |
933 | |
839 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
934 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
840 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
935 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
841 | |
936 | |
842 | $darkness = new Crossfire::Client::Texture |
937 | $darkness = new CFClient::Texture |
843 | width => $sw4, |
938 | width => $sw4, |
844 | height => $sh, |
939 | height => $sh, |
845 | data => $darkness, |
940 | data => $darkness, |
846 | internalformat => GL_ALPHA, |
941 | internalformat => GL_ALPHA, |
847 | format => GL_ALPHA; |
942 | format => GL_ALPHA; |
848 | |
943 | |
849 | glColor 0.45, 0.45, 0.45, 1; |
944 | glColor 0.45, 0.45, 0.45, 1; |
850 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
945 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
851 | |
946 | |
852 | glDisable GL_TEXTURE_2D; |
947 | glDisable GL_TEXTURE_2D; |
853 | glDisable GL_BLEND; |
948 | glDisable GL_BLEND; |
|
|
949 | |
|
|
950 | glEndList; |
|
|
951 | } |
|
|
952 | |
|
|
953 | glCallList $self->{list}; |
854 | } |
954 | } |
855 | |
955 | |
856 | my %DIR = ( |
956 | my %DIR = ( |
857 | SDLK_KP8, [1, "north"], |
957 | SDLK_KP8, [1, "north"], |
858 | SDLK_KP9, [2, "northeast"], |
958 | SDLK_KP9, [2, "northeast"], |
… | |
… | |
904 | } |
1004 | } |
905 | } |
1005 | } |
906 | |
1006 | |
907 | ############################################################################# |
1007 | ############################################################################# |
908 | |
1008 | |
909 | package Crossfire::Client::Widget::Animator; |
1009 | package CFClient::Widget::Animator; |
910 | |
1010 | |
911 | use SDL::OpenGL; |
1011 | use SDL::OpenGL; |
912 | |
1012 | |
913 | our @ISA = Crossfire::Client::Widget::Bin::; |
1013 | our @ISA = CFClient::Widget::Bin::; |
914 | |
1014 | |
915 | sub moveto { |
1015 | sub moveto { |
916 | my ($self, $x, $y) = @_; |
1016 | my ($self, $x, $y) = @_; |
917 | |
1017 | |
918 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
1018 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
… | |
… | |
923 | } |
1023 | } |
924 | |
1024 | |
925 | sub animate { |
1025 | sub animate { |
926 | my ($self, $interval) = @_; |
1026 | my ($self, $interval) = @_; |
927 | |
1027 | |
928 | printf "%5.2f\n", 1 / $interval if $interval;#d# |
|
|
929 | |
|
|
930 | $self->{time} -= $interval * $self->{speed}; |
1028 | $self->{time} -= $interval * $self->{speed}; |
931 | if ($self->{time} <= 0) { |
1029 | if ($self->{time} <= 0) { |
932 | $self->{time} = 0; |
1030 | $self->{time} = 0; |
933 | ::animation_stop $self; |
1031 | ::animation_stop $self; |
934 | } |
1032 | } |
… | |
… | |
948 | glPopMatrix; |
1046 | glPopMatrix; |
949 | } |
1047 | } |
950 | |
1048 | |
951 | ############################################################################# |
1049 | ############################################################################# |
952 | |
1050 | |
953 | package Crossfire::Client::Widget::Toplevel; |
1051 | package CFClient::Widget::Toplevel; |
954 | |
1052 | |
955 | our @ISA = Crossfire::Client::Widget::Container::; |
1053 | our @ISA = CFClient::Widget::Container::; |
956 | |
1054 | |
957 | sub size_request { |
1055 | sub size_request { |
958 | ($::WIDTH, $::HEIGHT) |
1056 | ($::WIDTH, $::HEIGHT) |
959 | } |
1057 | } |
960 | |
1058 | |
… | |
… | |
965 | |
1063 | |
966 | $_->size_allocate ($_->size_request) |
1064 | $_->size_allocate ($_->size_request) |
967 | for @{$self->{children}}; |
1065 | for @{$self->{children}}; |
968 | } |
1066 | } |
969 | |
1067 | |
|
|
1068 | sub translate { |
|
|
1069 | my ($self, $x, $y) = @_; |
|
|
1070 | |
|
|
1071 | ($x, $y) |
|
|
1072 | } |
|
|
1073 | |
970 | sub update { |
1074 | sub update { |
971 | my ($self) = @_; |
1075 | my ($self) = @_; |
972 | |
1076 | |
973 | $self->size_allocate ($self->size_request); |
1077 | $self->size_allocate ($self->size_request); |
974 | ::refresh (); |
1078 | ::refresh (); |
… | |
… | |
988 | $self->_draw; |
1092 | $self->_draw; |
989 | } |
1093 | } |
990 | |
1094 | |
991 | ############################################################################# |
1095 | ############################################################################# |
992 | |
1096 | |
993 | package Crossfire::Client::Widget; |
1097 | package CFClient::Widget; |
994 | |
1098 | |
995 | $TOPLEVEL = new Crossfire::Client::Widget::Toplevel; |
1099 | $TOPLEVEL = new CFClient::Widget::Toplevel; |
996 | |
1100 | |
997 | 1 |
1101 | 1 |
998 | |
1102 | |