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.59 by root, Tue Apr 11 13:05:12 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 ( 688 my $self = $class->SUPER::new (
644 x => $x, 689 color => [1, 1, 1],
645 y => $y, 690 height => $::FONTSIZE,
646 z => $z, 691 text => "",
647 height => $height,
648 layout => new Crossfire::Client::Layout, 692 layout => new CFClient::Layout,
693 %arg
649 ); 694 );
650 695
651 $self->set_text ($text); 696 $self->set_text ($self->{text});
652 697
653 $self 698 $self
654} 699}
655 700
656sub set_text { 701sub set_text {
670 715
671sub size_request { 716sub size_request {
672 my ($self) = @_; 717 my ($self) = @_;
673 718
674 $self->{layout}->set_width; 719 $self->{layout}->set_width;
720 $self->{layout}->set_height ($self->{height});
675 $self->{layout}->size 721 $self->{layout}->size
676# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 722# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
677# ( 723# (
678# $self->{texture}{width}, 724# $self->{texture}{width},
679# $self->{texture}{height}, 725# $self->{texture}{height},
680# ) 726# )
681# } else { 727# } else {
682# my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; 728# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
683# 729#
684# ($w, $h) 730# ($w, $h)
685# } 731# }
686} 732}
687 733
695sub _draw { 741sub _draw {
696 my ($self) = @_; 742 my ($self) = @_;
697 743
698 my $tex = $self->{texture} ||= do { 744 my $tex = $self->{texture} ||= do {
699 $self->{layout}->set_width ($self->{w}); 745 $self->{layout}->set_width ($self->{w});
700 new_from_layout Crossfire::Client::Texture $self->{layout}; 746 new_from_layout CFClient::Texture $self->{layout};
701 }; 747 };
702 748
703 glEnable GL_BLEND; 749 glEnable GL_BLEND;
704 glEnable GL_TEXTURE_2D; 750 glEnable GL_TEXTURE_2D;
705 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 751 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
706 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 752 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
707 753
708 glColor 1, 0, 0, 1; # TODO color 754 glColor @{$self->{color}};
709 755
710 $tex->draw_quad (0, 0); 756 $tex->draw_quad (0, 0);
711 757
712 glDisable GL_BLEND; 758 glDisable GL_BLEND;
713 glDisable GL_TEXTURE_2D; 759 glDisable GL_TEXTURE_2D;
714} 760}
715 761
716############################################################################# 762#############################################################################
717 763
718package Crossfire::Client::Widget::Entry; 764package CFClient::Widget::Entry;
719 765
720our @ISA = Crossfire::Client::Widget::Label::; 766our @ISA = CFClient::Widget::Label::;
721 767
722use SDL; 768use SDL;
723use SDL::OpenGL; 769use SDL::OpenGL;
724 770
725sub key_down { 771sub key_down {
771 $self->SUPER::_draw; 817 $self->SUPER::_draw;
772} 818}
773 819
774############################################################################# 820#############################################################################
775 821
776package Crossfire::Client::Widget::MapWidget; 822package CFClient::Widget::MapWidget;
777 823
778use strict; 824use strict;
779 825
780use List::Util qw(min max); 826use List::Util qw(min max);
781 827
782use SDL; 828use SDL;
783use SDL::OpenGL; 829use SDL::OpenGL;
784use SDL::OpenGL::Constants; 830use SDL::OpenGL::Constants;
785 831
786our @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}
787 843
788sub key_down { 844sub key_down {
789 print "MAPKEYDOWN\n"; 845 print "MAPKEYDOWN\n";
790} 846}
791 847
797 1 + int $::WIDTH / 32, 853 1 + int $::WIDTH / 32,
798 1 + int $::HEIGHT / 32, 854 1 + int $::HEIGHT / 32,
799 ) 855 )
800} 856}
801 857
858sub update {
859 my ($self) = @_;
860
861 $self->{need_update} = 1;
862}
863
802sub _draw { 864sub _draw {
803 my ($self) = @_; 865 my ($self) = @_;
804 866
867 if (delete $self->{need_update}) {
868 glNewList $self->{list}, GL_COMPILE;
869
805 my $mx = $::CONN->{mapx}; 870 my $mx = $::CONN->{mapx};
806 my $my = $::CONN->{mapy}; 871 my $my = $::CONN->{mapy};
807 872
808 my $map = $::CONN->{map}; 873 my $map = $::CONN->{map};
809 874
810 my ($xofs, $yofs); 875 my ($xofs, $yofs);
811 876
812 my $sw = 1 + int $::WIDTH / 32; 877 my $sw = 1 + int $::WIDTH / 32;
813 my $sh = 1 + int $::HEIGHT / 32; 878 my $sh = 1 + int $::HEIGHT / 32;
814 879
815 if ($::CONN->{mapw} > $sw) { 880 if ($::CONN->{mapw} > $sw) {
816 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 881 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
817 } else { 882 } else {
818 $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};
819 } 884 }
820 885
821 if ($::CONN->{maph} > $sh) { 886 if ($::CONN->{maph} > $sh) {
822 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 887 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
823 } else { 888 } else {
824 $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};
825 } 890 }
826 891
827 glEnable GL_TEXTURE_2D; 892 glEnable GL_TEXTURE_2D;
828 glEnable GL_BLEND; 893 glEnable GL_BLEND;
829 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 894 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
830 895
831 my $sw4 = ($sw + 3) & ~3; 896 my $sw4 = ($sw + 3) & ~3;
832 my $darkness = "\x00" x ($sw4 * $sh); 897 my $darkness = "\x00" x ($sw4 * $sh);
833 898
834 for my $x (0 .. $sw - 1) { 899 for my $x (0 .. $sw - 1) {
835 my $row = $map->[$x + $xofs]; 900 my $row = $map->[$x + $xofs];
836 for my $y (0 .. $sh - 1) { 901 for my $y (0 .. $sh - 1) {
837 902
838 my $cell = $row->[$y + $yofs] 903 my $cell = $row->[$y + $yofs]
839 or next; 904 or next;
840 905
841 my $dark = $cell->[0]; 906 my $dark = $cell->[0];
842 if ($dark < 0) { 907 if ($dark < 0) {
843 substr $darkness, $y * $sw4 + $x, 1, chr 224; 908 substr $darkness, $y * $sw4 + $x, 1, chr 224;
844 } else { 909 } else {
845 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; 910 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
846 } 911 }
847 912
848 for my $num (grep $_, @$cell[1,2,3]) { 913 for my $num (grep $_, @$cell[1,2,3]) {
849 my $tex = $::CONN->{face}[$num]{texture} || next; 914 my $tex = $::CONN->{face}[$num]{texture} || next;
850 915
851 my $w = $tex->{width}; 916 my $w = $tex->{width};
852 my $h = $tex->{height}; 917 my $h = $tex->{height};
853 918
854 $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 }
855 } 921 }
856 } 922 }
857 }
858 923
859# if (1) { # higher quality darkness 924# if (1) { # higher quality darkness
860# $lighting =~ s/(.)/$1$1$1/gs; 925# $lighting =~ s/(.)/$1$1$1/gs;
861# 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;
862# 927#
864# 929#
865# $lighting = $pb->get_pixels; 930# $lighting = $pb->get_pixels;
866# $lighting =~ s/(.)../$1/gs; 931# $lighting =~ s/(.)../$1/gs;
867# } 932# }
868 933
869 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 934 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
870 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 935 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
871 936
872 $darkness = new Crossfire::Client::Texture 937 $darkness = new CFClient::Texture
873 width => $sw4, 938 width => $sw4,
874 height => $sh, 939 height => $sh,
875 data => $darkness, 940 data => $darkness,
876 internalformat => GL_ALPHA, 941 internalformat => GL_ALPHA,
877 format => GL_ALPHA; 942 format => GL_ALPHA;
878 943
879 glColor 0.45, 0.45, 0.45, 1; 944 glColor 0.45, 0.45, 0.45, 1;
880 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); 945 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
881 946
882 glDisable GL_TEXTURE_2D; 947 glDisable GL_TEXTURE_2D;
883 glDisable GL_BLEND; 948 glDisable GL_BLEND;
949
950 glEndList;
951 }
952
953 glCallList $self->{list};
884} 954}
885 955
886my %DIR = ( 956my %DIR = (
887 SDLK_KP8, [1, "north"], 957 SDLK_KP8, [1, "north"],
888 SDLK_KP9, [2, "northeast"], 958 SDLK_KP9, [2, "northeast"],
934 } 1004 }
935} 1005}
936 1006
937############################################################################# 1007#############################################################################
938 1008
939package Crossfire::Client::Widget::Animator; 1009package CFClient::Widget::Animator;
940 1010
941use SDL::OpenGL; 1011use SDL::OpenGL;
942 1012
943our @ISA = Crossfire::Client::Widget::Bin::; 1013our @ISA = CFClient::Widget::Bin::;
944 1014
945sub moveto { 1015sub moveto {
946 my ($self, $x, $y) = @_; 1016 my ($self, $x, $y) = @_;
947 1017
948 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 1018 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
976 glPopMatrix; 1046 glPopMatrix;
977} 1047}
978 1048
979############################################################################# 1049#############################################################################
980 1050
981package Crossfire::Client::Widget::Toplevel; 1051package CFClient::Widget::Toplevel;
982 1052
983our @ISA = Crossfire::Client::Widget::Container::; 1053our @ISA = CFClient::Widget::Container::;
984 1054
985sub size_request { 1055sub size_request {
986 ($::WIDTH, $::HEIGHT) 1056 ($::WIDTH, $::HEIGHT)
987} 1057}
988 1058
1022 $self->_draw; 1092 $self->_draw;
1023} 1093}
1024 1094
1025############################################################################# 1095#############################################################################
1026 1096
1027package Crossfire::Client::Widget; 1097package CFClient::Widget;
1028 1098
1029$TOPLEVEL = new Crossfire::Client::Widget::Toplevel; 1099$TOPLEVEL = new CFClient::Widget::Toplevel;
1030 1100
10311 11011
1032 1102

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines