… | |
… | |
73 | } |
73 | } |
74 | |
74 | |
75 | sub new { |
75 | sub new { |
76 | my $class = shift; |
76 | my $class = shift; |
77 | |
77 | |
78 | bless { @_ }, $class |
78 | bless { |
|
|
79 | x => 0, |
|
|
80 | y => 0, |
|
|
81 | z => 0, |
|
|
82 | @_ |
|
|
83 | }, $class |
79 | } |
84 | } |
80 | |
85 | |
81 | sub move { |
86 | sub move { |
82 | my ($self, $x, $y, $z) = @_; |
87 | my ($self, $x, $y, $z) = @_; |
83 | $self->{x} = $x; |
88 | $self->{x} = $x; |
… | |
… | |
210 | #$self->deactivate; |
215 | #$self->deactivate; |
211 | } |
216 | } |
212 | |
217 | |
213 | ############################################################################# |
218 | ############################################################################# |
214 | |
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 | |
215 | package CFClient::Widget::Container; |
233 | package CFClient::Widget::Container; |
216 | |
234 | |
217 | our @ISA = CFClient::Widget::; |
235 | our @ISA = CFClient::Widget::; |
218 | |
236 | |
219 | sub new { |
237 | sub new { |
220 | my ($class, @widgets) = @_; |
238 | my ($class, %arg) = @_; |
221 | |
239 | |
|
|
240 | my $children = delete $arg{children} || []; |
|
|
241 | |
222 | my $self = $class->SUPER::new (children => []); |
242 | my $self = $class->SUPER::new (children => [], %arg); |
223 | $self->add ($_) for @widgets; |
243 | $self->add ($_) for @$children; |
224 | |
244 | |
225 | $self |
245 | $self |
226 | } |
246 | } |
227 | |
247 | |
228 | sub add { |
248 | sub add { |
229 | my ($self, $chld, $expand) = @_; |
249 | my ($self, $chld, $expand) = @_; |
230 | |
250 | |
231 | $chld->{expand} = $expand; |
251 | $chld->{expand} = $expand; |
232 | $chld->set_parent ($self); |
252 | $chld->set_parent ($self); |
233 | |
253 | |
234 | @{$self->{children}} = |
254 | $self->{children} = [ |
235 | sort { $a->{z} <=> $b->{z} } |
255 | sort { $a->{z} <=> $b->{z} } |
236 | @{$self->{children}}, $chld; |
256 | @{$self->{children}}, $chld |
|
|
257 | ]; |
237 | |
258 | |
238 | $self->size_allocate ($self->{w}, $self->{h}) |
259 | $self->size_allocate ($self->{w}, $self->{h}) |
239 | if $self->{w}; #TODO: check for "realised state" |
260 | if $self->{w}; #TODO: check for "realised state" |
240 | } |
261 | } |
241 | |
262 | |
… | |
… | |
273 | |
294 | |
274 | package CFClient::Widget::Bin; |
295 | package CFClient::Widget::Bin; |
275 | |
296 | |
276 | our @ISA = CFClient::Widget::Container::; |
297 | our @ISA = CFClient::Widget::Container::; |
277 | |
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 | } |
|
|
323 | |
278 | sub child { $_[0]->{children}[0] } |
324 | sub child { $_[0]->{children}[0] } |
279 | |
325 | |
280 | sub size_request { |
326 | sub size_request { |
281 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
327 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
282 | } |
328 | } |
… | |
… | |
296 | our @ISA = CFClient::Widget::Bin::; |
342 | our @ISA = CFClient::Widget::Bin::; |
297 | |
343 | |
298 | use SDL::OpenGL; |
344 | use SDL::OpenGL; |
299 | |
345 | |
300 | sub new { |
346 | sub new { |
301 | my ($class, $x, $y, $z, $w, $h) = @_; |
347 | my ($class, %arg) = @_; |
302 | |
348 | |
303 | my $self = $class->SUPER::new; |
349 | my $self = $class->SUPER::new (%arg); |
304 | |
|
|
305 | @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); |
|
|
306 | } |
350 | } |
307 | |
351 | |
308 | sub update { |
352 | sub update { |
309 | my ($self) = @_; |
353 | my ($self) = @_; |
310 | |
354 | |
… | |
… | |
433 | $w -= $tex[3]->{width}; |
477 | $w -= $tex[3]->{width}; |
434 | |
478 | |
435 | $h = $h < 0 ? 0 : $h; |
479 | $h = $h < 0 ? 0 : $h; |
436 | $w = $w < 0 ? 0 : $w; |
480 | $w = $w < 0 ? 0 : $w; |
437 | |
481 | |
|
|
482 | my $child = $self->child; |
|
|
483 | |
438 | $self->child->size_allocate ($w, $h); |
484 | $child->size_allocate ($w, $h); |
439 | $self->child->move ($tex[3]->{width}, $tex[1]->{height}); |
485 | $child->move ($tex[3]->{width}, $tex[1]->{height}); |
440 | } |
486 | } |
441 | |
487 | |
442 | sub _draw { |
488 | sub _draw { |
443 | my ($self) = @_; |
489 | my ($self) = @_; |
444 | |
490 | |
… | |
… | |
634 | our @ISA = CFClient::Widget::; |
680 | our @ISA = CFClient::Widget::; |
635 | |
681 | |
636 | use SDL::OpenGL; |
682 | use SDL::OpenGL; |
637 | |
683 | |
638 | sub new { |
684 | sub new { |
639 | my ($class, $x, $y, $z, $height, $text) = @_; |
685 | my ($class, %arg) = @_; |
640 | |
|
|
641 | $height ||= $::FONTSIZE; |
|
|
642 | |
686 | |
643 | # TODO: color, and make height, xyz etc. optional |
687 | # TODO: color, and make height, xyz etc. optional |
644 | my $self = $class->SUPER::new ( |
688 | my $self = $class->SUPER::new ( |
645 | color => [1, 1, 1], |
689 | color => [1, 1, 1], |
646 | x => $x, |
690 | height => $::FONTSIZE, |
647 | y => $y, |
691 | text => "", |
648 | z => $z, |
|
|
649 | height => $height, |
|
|
650 | layout => new CFClient::Layout $height, |
692 | layout => new CFClient::Layout, |
|
|
693 | %arg |
651 | ); |
694 | ); |
652 | |
695 | |
653 | $self->set_text ($text); |
696 | $self->set_text ($self->{text}); |
654 | |
697 | |
655 | $self |
698 | $self |
656 | } |
699 | } |
657 | |
700 | |
658 | sub set_text { |
701 | sub set_text { |
… | |
… | |
672 | |
715 | |
673 | sub size_request { |
716 | sub size_request { |
674 | my ($self) = @_; |
717 | my ($self) = @_; |
675 | |
718 | |
676 | $self->{layout}->set_width; |
719 | $self->{layout}->set_width; |
|
|
720 | $self->{layout}->set_height ($self->{height}); |
677 | $self->{layout}->size |
721 | $self->{layout}->size |
678 | # if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
722 | # if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
679 | # ( |
723 | # ( |
680 | # $self->{texture}{width}, |
724 | # $self->{texture}{width}, |
681 | # $self->{texture}{height}, |
725 | # $self->{texture}{height}, |
… | |
… | |
785 | use SDL::OpenGL; |
829 | use SDL::OpenGL; |
786 | use SDL::OpenGL::Constants; |
830 | use SDL::OpenGL::Constants; |
787 | |
831 | |
788 | our @ISA = CFClient::Widget::; |
832 | our @ISA = CFClient::Widget::; |
789 | |
833 | |
|
|
834 | sub new { |
|
|
835 | my $class = shift; |
|
|
836 | |
|
|
837 | $class->SUPER::new ( |
|
|
838 | z => -1, |
|
|
839 | list => (glGenLists 1), |
|
|
840 | @_ |
|
|
841 | ) |
|
|
842 | } |
|
|
843 | |
790 | sub key_down { |
844 | sub key_down { |
791 | print "MAPKEYDOWN\n"; |
845 | print "MAPKEYDOWN\n"; |
792 | } |
846 | } |
793 | |
847 | |
794 | sub key_up { |
848 | sub key_up { |
… | |
… | |
799 | 1 + int $::WIDTH / 32, |
853 | 1 + int $::WIDTH / 32, |
800 | 1 + int $::HEIGHT / 32, |
854 | 1 + int $::HEIGHT / 32, |
801 | ) |
855 | ) |
802 | } |
856 | } |
803 | |
857 | |
|
|
858 | sub update { |
|
|
859 | my ($self) = @_; |
|
|
860 | |
|
|
861 | $self->{need_update} = 1; |
|
|
862 | } |
|
|
863 | |
804 | sub _draw { |
864 | sub _draw { |
805 | my ($self) = @_; |
865 | my ($self) = @_; |
806 | |
866 | |
|
|
867 | if (delete $self->{need_update}) { |
|
|
868 | glNewList $self->{list}, GL_COMPILE; |
|
|
869 | |
807 | my $mx = $::CONN->{mapx}; |
870 | my $mx = $::CONN->{mapx}; |
808 | my $my = $::CONN->{mapy}; |
871 | my $my = $::CONN->{mapy}; |
809 | |
872 | |
810 | my $map = $::CONN->{map}; |
873 | my $map = $::CONN->{map}; |
811 | |
874 | |
812 | my ($xofs, $yofs); |
875 | my ($xofs, $yofs); |
813 | |
876 | |
814 | my $sw = 1 + int $::WIDTH / 32; |
877 | my $sw = 1 + int $::WIDTH / 32; |
815 | my $sh = 1 + int $::HEIGHT / 32; |
878 | my $sh = 1 + int $::HEIGHT / 32; |
816 | |
879 | |
817 | if ($::CONN->{mapw} > $sw) { |
880 | if ($::CONN->{mapw} > $sw) { |
818 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
881 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
819 | } else { |
882 | } else { |
820 | $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}; |
821 | } |
884 | } |
822 | |
885 | |
823 | if ($::CONN->{maph} > $sh) { |
886 | if ($::CONN->{maph} > $sh) { |
824 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
887 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
825 | } else { |
888 | } else { |
826 | $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}; |
827 | } |
890 | } |
828 | |
891 | |
829 | glEnable GL_TEXTURE_2D; |
892 | glEnable GL_TEXTURE_2D; |
830 | glEnable GL_BLEND; |
893 | glEnable GL_BLEND; |
831 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
894 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
832 | |
895 | |
833 | my $sw4 = ($sw + 3) & ~3; |
896 | my $sw4 = ($sw + 3) & ~3; |
834 | my $darkness = "\x00" x ($sw4 * $sh); |
897 | my $darkness = "\x00" x ($sw4 * $sh); |
835 | |
898 | |
836 | for my $x (0 .. $sw - 1) { |
899 | for my $x (0 .. $sw - 1) { |
837 | my $row = $map->[$x + $xofs]; |
900 | my $row = $map->[$x + $xofs]; |
838 | for my $y (0 .. $sh - 1) { |
901 | for my $y (0 .. $sh - 1) { |
839 | |
902 | |
840 | my $cell = $row->[$y + $yofs] |
903 | my $cell = $row->[$y + $yofs] |
841 | or next; |
904 | or next; |
842 | |
905 | |
843 | my $dark = $cell->[0]; |
906 | my $dark = $cell->[0]; |
844 | if ($dark < 0) { |
907 | if ($dark < 0) { |
845 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
908 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
846 | } else { |
909 | } else { |
847 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
910 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
848 | } |
911 | } |
849 | |
912 | |
850 | for my $num (grep $_, @$cell[1,2,3]) { |
913 | for my $num (grep $_, @$cell[1,2,3]) { |
851 | my $tex = $::CONN->{face}[$num]{texture} || next; |
914 | my $tex = $::CONN->{face}[$num]{texture} || next; |
852 | |
915 | |
853 | my $w = $tex->{width}; |
916 | my $w = $tex->{width}; |
854 | my $h = $tex->{height}; |
917 | my $h = $tex->{height}; |
855 | |
918 | |
856 | $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 | } |
857 | } |
921 | } |
858 | } |
922 | } |
859 | } |
|
|
860 | |
923 | |
861 | # if (1) { # higher quality darkness |
924 | # if (1) { # higher quality darkness |
862 | # $lighting =~ s/(.)/$1$1$1/gs; |
925 | # $lighting =~ s/(.)/$1$1$1/gs; |
863 | # 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; |
864 | # |
927 | # |
… | |
… | |
866 | # |
929 | # |
867 | # $lighting = $pb->get_pixels; |
930 | # $lighting = $pb->get_pixels; |
868 | # $lighting =~ s/(.)../$1/gs; |
931 | # $lighting =~ s/(.)../$1/gs; |
869 | # } |
932 | # } |
870 | |
933 | |
871 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
934 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
872 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
935 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
873 | |
936 | |
874 | $darkness = new CFClient::Texture |
937 | $darkness = new CFClient::Texture |
875 | width => $sw4, |
938 | width => $sw4, |
876 | height => $sh, |
939 | height => $sh, |
877 | data => $darkness, |
940 | data => $darkness, |
878 | internalformat => GL_ALPHA, |
941 | internalformat => GL_ALPHA, |
879 | format => GL_ALPHA; |
942 | format => GL_ALPHA; |
880 | |
943 | |
881 | glColor 0.45, 0.45, 0.45, 1; |
944 | glColor 0.45, 0.45, 0.45, 1; |
882 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
945 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
883 | |
946 | |
884 | glDisable GL_TEXTURE_2D; |
947 | glDisable GL_TEXTURE_2D; |
885 | glDisable GL_BLEND; |
948 | glDisable GL_BLEND; |
|
|
949 | |
|
|
950 | glEndList; |
|
|
951 | } |
|
|
952 | |
|
|
953 | glCallList $self->{list}; |
886 | } |
954 | } |
887 | |
955 | |
888 | my %DIR = ( |
956 | my %DIR = ( |
889 | SDLK_KP8, [1, "north"], |
957 | SDLK_KP8, [1, "north"], |
890 | SDLK_KP9, [2, "northeast"], |
958 | SDLK_KP9, [2, "northeast"], |