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