ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.58 by root, Tue Apr 11 12:21:29 2006 UTC vs.
Revision 1.66 by root, Tue Apr 11 14:36:02 2006 UTC

1package Crossfire::Client::Widget; 1package CFClient::Widget;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util;
6 6
7use SDL::OpenGL; 7use SDL::OpenGL;
8use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
9 9
10use Crossfire::Client; 10use CFClient;
11 11
12our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
13 13
14our $TOPLEVEL; 14our $TOPLEVEL;
15our $BUTTON_STATE; 15our $BUTTON_STATE;
73} 73}
74 74
75sub new { 75sub 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
81sub move { 86sub 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
220package CFClient::Widget::Empty;
221
222our @ISA = CFClient::Widget::;
223
224sub size_request {
225 (0, 0)
226}
227
228sub draw {
229}
230
231#############################################################################
232
215package Crossfire::Client::Widget::Container; 233package CFClient::Widget::Container;
216 234
217our @ISA = Crossfire::Client::Widget::; 235our @ISA = CFClient::Widget::;
218 236
219sub new { 237sub 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
228sub add { 248sub 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
269 $_->draw for @{$self->{children}}; 290 $_->draw for @{$self->{children}};
270} 291}
271 292
272############################################################################# 293#############################################################################
273 294
274package Crossfire::Client::Widget::Bin; 295package CFClient::Widget::Bin;
275 296
276our @ISA = Crossfire::Client::Widget::Container::; 297our @ISA = CFClient::Widget::Container::;
298
299sub 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
307sub add {
308 my ($self, $widget) = @_;
309
310 $self->{children} = [];
311
312 $self->SUPER::add ($widget);
313}
314
315sub remove {
316 my ($self, $widget) = @_;
317
318 $self->SUPER::remove ($widget);
319
320 $self->{children} = [new CFClient::Widget::Empty]
321 unless @{$self->{children}};
322}
277 323
278sub child { $_[0]->{children}[0] } 324sub child { $_[0]->{children}[0] }
279 325
280sub size_request { 326sub size_request {
281 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 327 $_[0]{children}[0]->size_request if $_[0]{children}[0];
289 if $self->{children}[0] 335 if $self->{children}[0]
290} 336}
291 337
292############################################################################# 338#############################################################################
293 339
294package Crossfire::Client::Widget::Window; 340package CFClient::Widget::Window;
295 341
296our @ISA = Crossfire::Client::Widget::Bin::; 342our @ISA = CFClient::Widget::Bin::;
297 343
298use SDL::OpenGL; 344use SDL::OpenGL;
299 345
300sub new { 346sub 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
308sub update { 352sub update {
309 my ($self) = @_; 353 my ($self) = @_;
310 354
355 # we want to do this delayed...
311 $self->render_chld; 356 $self->render_chld;
312 $self->SUPER::update; 357 $self->SUPER::update;
313} 358}
314 359
315sub render_chld { 360sub render_chld {
316 my ($self) = @_; 361 my ($self) = @_;
317 362
318 $self->{texture} = 363 $self->{texture} =
319 Crossfire::Client::Texture->new_from_opengl ( 364 CFClient::Texture->new_from_opengl (
320 $self->{w}, $self->{h}, sub { $self->child->draw } 365 $self->{w}, $self->{h}, sub { $self->child->draw }
321 ); 366 );
322} 367}
323 368
324sub size_allocate { 369sub size_allocate {
350 glDisable GL_TEXTURE_2D; 395 glDisable GL_TEXTURE_2D;
351} 396}
352 397
353############################################################################# 398#############################################################################
354 399
355package Crossfire::Client::Widget::Frame; 400package CFClient::Widget::Frame;
356 401
357our @ISA = Crossfire::Client::Widget::Bin::; 402our @ISA = CFClient::Widget::Bin::;
358 403
359use SDL::OpenGL; 404use SDL::OpenGL;
360 405
361sub size_request { 406sub size_request {
362 my ($self) = @_; 407 my ($self) = @_;
396 $chld->draw; 441 $chld->draw;
397} 442}
398 443
399############################################################################# 444#############################################################################
400 445
401package Crossfire::Client::Widget::FancyFrame; 446package CFClient::Widget::FancyFrame;
402 447
403our @ISA = Crossfire::Client::Widget::Bin::; 448our @ISA = CFClient::Widget::Bin::;
404 449
405use SDL::OpenGL; 450use SDL::OpenGL;
406 451
407my @tex = 452my @tex =
408 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 453 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
409 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);
410 455
411sub size_request { 456sub size_request {
412 my ($self) = @_; 457 my ($self) = @_;
413 458
432 $w -= $tex[3]->{width}; 477 $w -= $tex[3]->{width};
433 478
434 $h = $h < 0 ? 0 : $h; 479 $h = $h < 0 ? 0 : $h;
435 $w = $w < 0 ? 0 : $w; 480 $w = $w < 0 ? 0 : $w;
436 481
482 my $child = $self->child;
483
437 $self->child->size_allocate ($w, $h); 484 $child->size_allocate ($w, $h);
438 $self->child->move ($tex[3]->{width}, $tex[1]->{height}); 485 $child->move ($tex[3]->{width}, $tex[1]->{height});
439} 486}
440 487
441sub _draw { 488sub _draw {
442 my ($self) = @_; 489 my ($self) = @_;
443 490
479 526
480} 527}
481 528
482############################################################################# 529#############################################################################
483 530
484package Crossfire::Client::Widget::Table; 531package CFClient::Widget::Table;
485 532
486our @ISA = Crossfire::Client::Widget::Bin::; 533our @ISA = CFClient::Widget::Bin::;
487 534
488use SDL::OpenGL; 535use SDL::OpenGL;
489 536
490sub add { 537sub add {
491 my ($self, $x, $y, $chld) = @_; 538 my ($self, $x, $y, $chld) = @_;
566 } 613 }
567} 614}
568 615
569############################################################################# 616#############################################################################
570 617
571package Crossfire::Client::Widget::VBox; 618package CFClient::Widget::VBox;
572 619
573our @ISA = Crossfire::Client::Widget::Container::; 620our @ISA = CFClient::Widget::Container::;
574 621
575use SDL::OpenGL; 622use SDL::OpenGL;
576 623
577sub size_request { 624sub size_request {
578 my ($self) = @_; 625 my ($self) = @_;
626 } 673 }
627} 674}
628 675
629############################################################################# 676#############################################################################
630 677
631package Crossfire::Client::Widget::Label; 678package CFClient::Widget::Label;
632 679
633our @ISA = Crossfire::Client::Widget::; 680our @ISA = CFClient::Widget::;
634 681
635use SDL::OpenGL; 682use SDL::OpenGL;
636 683
637sub new { 684sub new {
638 my ($class, $x, $y, $z, $height, $text) = @_; 685 my ($class, %arg) = @_;
639
640 $height ||= $::FONTSIZE;
641 686
642 # TODO: color, and make height, xyz etc. optional 687 # TODO: color, and make height, xyz etc. optional
643 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 );
644 695
645 $self->set_text ($text); 696 $self->set_text ($self->{text});
646 697
647 $self 698 $self
648} 699}
649 700
650sub set_text { 701sub set_text {
651 my ($self, $text) = @_; 702 my ($self, $text) = @_;
652 703
653 $self->{text} = $text; 704 $self->{text} = $text;
654 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 705 $self->{layout}->set_markup ($text);
655 706
656 $self->update; 707 delete $self->{texture};
657} 708}
658 709
659sub get_text { 710sub get_text {
660 my ($self, $text) = @_; 711 my ($self, $text) = @_;
661 712
663} 714}
664 715
665sub size_request { 716sub size_request {
666 my ($self) = @_; 717 my ($self) = @_;
667 718
719 $self->{layout}->set_width;
720 $self->{layout}->set_height ($self->{height});
721 $self->{layout}->size
668 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 722# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
669 ( 723# (
670 $self->{texture}{width}, 724# $self->{texture}{width},
671 $self->{texture}{height}, 725# $self->{texture}{height},
672 ) 726# )
673 } else { 727# } else {
674 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; 728# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
675 729#
676 ($w, $h) 730# ($w, $h)
677 } 731# }
732}
678 733
734sub size_allocate {
735 my ($self, $w, $h) = @_;
736
737 $self->SUPER::size_allocate ($w, $h);
738 delete $self->{texture};
679} 739}
680 740
681sub _draw { 741sub _draw {
682 my ($self) = @_; 742 my ($self) = @_;
683 743
684 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 };
685 748
686 glEnable GL_BLEND; 749 glEnable GL_BLEND;
687 glEnable GL_TEXTURE_2D; 750 glEnable GL_TEXTURE_2D;
688 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 751 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
689 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 752 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
690 753
691 glColor 1, 0, 0, 1; # TODO color 754 glColor @{$self->{color}};
692 755
693 $tex->draw_quad (0, 0); 756 $tex->draw_quad (0, 0);
694 757
695 glDisable GL_BLEND; 758 glDisable GL_BLEND;
696 glDisable GL_TEXTURE_2D; 759 glDisable GL_TEXTURE_2D;
697} 760}
698 761
699############################################################################# 762#############################################################################
700 763
701package Crossfire::Client::Widget::Entry; 764package CFClient::Widget::Entry;
702 765
703our @ISA = Crossfire::Client::Widget::Label::; 766our @ISA = CFClient::Widget::Label::;
704 767
705use SDL; 768use SDL;
706use SDL::OpenGL; 769use SDL::OpenGL;
707 770
708sub key_down { 771sub key_down {
754 $self->SUPER::_draw; 817 $self->SUPER::_draw;
755} 818}
756 819
757############################################################################# 820#############################################################################
758 821
759package Crossfire::Client::Widget::MapWidget; 822package CFClient::Widget::MapWidget;
760 823
761use strict; 824use strict;
762 825
763use List::Util qw(min max); 826use List::Util qw(min max);
764 827
765use SDL; 828use SDL;
766use SDL::OpenGL; 829use SDL::OpenGL;
767use SDL::OpenGL::Constants; 830use SDL::OpenGL::Constants;
768 831
769our @ISA = Crossfire::Client::Widget::; 832our @ISA = CFClient::Widget::;
833
834sub new {
835 my $class = shift;
836
837 $class->SUPER::new (
838 z => -1,
839 list => (glGenLists 1),
840 @_
841 )
842}
770 843
771sub key_down { 844sub key_down {
772 print "MAPKEYDOWN\n"; 845 print "MAPKEYDOWN\n";
773} 846}
774 847
780 1 + int $::WIDTH / 32, 853 1 + int $::WIDTH / 32,
781 1 + int $::HEIGHT / 32, 854 1 + int $::HEIGHT / 32,
782 ) 855 )
783} 856}
784 857
858sub update {
859 my ($self) = @_;
860
861 $self->{need_update} = 1;
862}
863
785sub _draw { 864sub _draw {
786 my ($self) = @_; 865 my ($self) = @_;
787 866
867 if (delete $self->{need_update}) {
868 glNewList $self->{list}, GL_COMPILE;
869
788 my $mx = $::CONN->{mapx}; 870 my $mx = $::CONN->{mapx};
789 my $my = $::CONN->{mapy}; 871 my $my = $::CONN->{mapy};
790 872
791 my $map = $::CONN->{map}; 873 my $map = $::CONN->{map};
792 874
793 my ($xofs, $yofs); 875 my ($xofs, $yofs);
794 876
795 my $sw = 1 + int $::WIDTH / 32; 877 my $sw = 1 + int $::WIDTH / 32;
796 my $sh = 1 + int $::HEIGHT / 32; 878 my $sh = 1 + int $::HEIGHT / 32;
797 879
798 if ($::CONN->{mapw} > $sw) { 880 if ($::CONN->{mapw} > $sw) {
799 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 881 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
800 } else { 882 } else {
801 $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};
802 } 884 }
803 885
804 if ($::CONN->{maph} > $sh) { 886 if ($::CONN->{maph} > $sh) {
805 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 887 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
806 } else { 888 } else {
807 $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};
808 } 890 }
809 891
810 glEnable GL_TEXTURE_2D; 892 glEnable GL_TEXTURE_2D;
811 glEnable GL_BLEND; 893 glEnable GL_BLEND;
812 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 894 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
813 895
814 my $sw4 = ($sw + 3) & ~3; 896 my $sw4 = ($sw + 3) & ~3;
815 my $darkness = "\x00" x ($sw4 * $sh); 897 my $darkness = "\x00" x ($sw4 * $sh);
816 898
817 for my $x (0 .. $sw - 1) { 899 for my $x (0 .. $sw - 1) {
818 my $row = $map->[$x + $xofs]; 900 my $row = $map->[$x + $xofs];
819 for my $y (0 .. $sh - 1) { 901 for my $y (0 .. $sh - 1) {
820 902
821 my $cell = $row->[$y + $yofs] 903 my $cell = $row->[$y + $yofs]
822 or next; 904 or next;
823 905
824 my $dark = $cell->[0]; 906 my $dark = $cell->[0];
825 if ($dark < 0) { 907 if ($dark < 0) {
826 substr $darkness, $y * $sw4 + $x, 1, chr 224; 908 substr $darkness, $y * $sw4 + $x, 1, chr 224;
827 } else { 909 } else {
828 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; 910 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
829 } 911 }
830 912
831 for my $num (grep $_, @$cell[1,2,3]) { 913 for my $num (grep $_, @$cell[1,2,3]) {
832 my $tex = $::CONN->{face}[$num]{texture} || next; 914 my $tex = $::CONN->{face}[$num]{texture} || next;
833 915
834 my $w = $tex->{width}; 916 my $w = $tex->{width};
835 my $h = $tex->{height}; 917 my $h = $tex->{height};
836 918
837 $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 }
838 } 921 }
839 } 922 }
840 }
841 923
842# if (1) { # higher quality darkness 924# if (1) { # higher quality darkness
843# $lighting =~ s/(.)/$1$1$1/gs; 925# $lighting =~ s/(.)/$1$1$1/gs;
844# 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;
845# 927#
847# 929#
848# $lighting = $pb->get_pixels; 930# $lighting = $pb->get_pixels;
849# $lighting =~ s/(.)../$1/gs; 931# $lighting =~ s/(.)../$1/gs;
850# } 932# }
851 933
852 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 934 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
853 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 935 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
854 936
855 $darkness = new Crossfire::Client::Texture 937 $darkness = new CFClient::Texture
856 width => $sw4, 938 width => $sw4,
857 height => $sh, 939 height => $sh,
858 data => $darkness, 940 data => $darkness,
859 internalformat => GL_ALPHA, 941 internalformat => GL_ALPHA,
860 format => GL_ALPHA; 942 format => GL_ALPHA;
861 943
862 glColor 0.45, 0.45, 0.45, 1; 944 glColor 0.45, 0.45, 0.45, 1;
863 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); 945 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
864 946
865 glDisable GL_TEXTURE_2D; 947 glDisable GL_TEXTURE_2D;
866 glDisable GL_BLEND; 948 glDisable GL_BLEND;
949
950 glEndList;
951 }
952
953 glCallList $self->{list};
867} 954}
868 955
869my %DIR = ( 956my %DIR = (
870 SDLK_KP8, [1, "north"], 957 SDLK_KP8, [1, "north"],
871 SDLK_KP9, [2, "northeast"], 958 SDLK_KP9, [2, "northeast"],
917 } 1004 }
918} 1005}
919 1006
920############################################################################# 1007#############################################################################
921 1008
922package Crossfire::Client::Widget::Animator; 1009package CFClient::Widget::Animator;
923 1010
924use SDL::OpenGL; 1011use SDL::OpenGL;
925 1012
926our @ISA = Crossfire::Client::Widget::Bin::; 1013our @ISA = CFClient::Widget::Bin::;
927 1014
928sub moveto { 1015sub moveto {
929 my ($self, $x, $y) = @_; 1016 my ($self, $x, $y) = @_;
930 1017
931 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 1018 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
959 glPopMatrix; 1046 glPopMatrix;
960} 1047}
961 1048
962############################################################################# 1049#############################################################################
963 1050
964package Crossfire::Client::Widget::Toplevel; 1051package CFClient::Widget::Toplevel;
965 1052
966our @ISA = Crossfire::Client::Widget::Container::; 1053our @ISA = CFClient::Widget::Container::;
967 1054
968sub size_request { 1055sub size_request {
969 ($::WIDTH, $::HEIGHT) 1056 ($::WIDTH, $::HEIGHT)
970} 1057}
971 1058
1005 $self->_draw; 1092 $self->_draw;
1006} 1093}
1007 1094
1008############################################################################# 1095#############################################################################
1009 1096
1010package Crossfire::Client::Widget; 1097package CFClient::Widget;
1011 1098
1012$TOPLEVEL = new Crossfire::Client::Widget::Toplevel; 1099$TOPLEVEL = new CFClient::Widget::Toplevel;
1013 1100
10141 11011
1015 1102

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines