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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines