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.65 by root, Tue Apr 11 14:04:27 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
215package Crossfire::Client::Widget::Container; 220package CFClient::Widget::Container;
216 221
217our @ISA = Crossfire::Client::Widget::; 222our @ISA = CFClient::Widget::;
218 223
219sub new { 224sub new {
220 my ($class, @widgets) = @_; 225 my ($class, %arg) = @_;
221 226
227 my $children = delete $arg{children} || [];
228
222 my $self = $class->SUPER::new (children => []); 229 my $self = $class->SUPER::new (children => [], %arg);
223 $self->add ($_) for @widgets; 230 $self->add ($_) for @$children;
224 231
225 $self 232 $self
226} 233}
227 234
228sub add { 235sub add {
269 $_->draw for @{$self->{children}}; 276 $_->draw for @{$self->{children}};
270} 277}
271 278
272############################################################################# 279#############################################################################
273 280
274package Crossfire::Client::Widget::Bin; 281package CFClient::Widget::Bin;
275 282
276our @ISA = Crossfire::Client::Widget::Container::; 283our @ISA = CFClient::Widget::Container::;
277 284
278sub child { $_[0]->{children}[0] } 285sub child { $_[0]->{children}[0] }
279 286
280sub size_request { 287sub size_request {
281 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 288 $_[0]{children}[0]->size_request if $_[0]{children}[0];
289 if $self->{children}[0] 296 if $self->{children}[0]
290} 297}
291 298
292############################################################################# 299#############################################################################
293 300
294package Crossfire::Client::Widget::Window; 301package CFClient::Widget::Window;
295 302
296our @ISA = Crossfire::Client::Widget::Bin::; 303our @ISA = CFClient::Widget::Bin::;
297 304
298use SDL::OpenGL; 305use SDL::OpenGL;
299 306
300sub new { 307sub new {
301 my ($class, $x, $y, $z, $w, $h) = @_; 308 my ($class, %arg) = @_;
302 309
303 my $self = $class->SUPER::new; 310 my $self = $class->SUPER::new (%arg);
304
305 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
306} 311}
307 312
308sub update { 313sub update {
309 my ($self) = @_; 314 my ($self) = @_;
310 315
316 # we want to do this delayed...
311 $self->render_chld; 317 $self->render_chld;
312 $self->SUPER::update; 318 $self->SUPER::update;
313} 319}
314 320
315sub render_chld { 321sub render_chld {
316 my ($self) = @_; 322 my ($self) = @_;
317 323
318 $self->{texture} = 324 $self->{texture} =
319 Crossfire::Client::Texture->new_from_opengl ( 325 CFClient::Texture->new_from_opengl (
320 $self->{w}, $self->{h}, sub { $self->child->draw } 326 $self->{w}, $self->{h}, sub { $self->child->draw }
321 ); 327 );
322} 328}
323 329
324sub size_allocate { 330sub size_allocate {
350 glDisable GL_TEXTURE_2D; 356 glDisable GL_TEXTURE_2D;
351} 357}
352 358
353############################################################################# 359#############################################################################
354 360
355package Crossfire::Client::Widget::Frame; 361package CFClient::Widget::Frame;
356 362
357our @ISA = Crossfire::Client::Widget::Bin::; 363our @ISA = CFClient::Widget::Bin::;
358 364
359use SDL::OpenGL; 365use SDL::OpenGL;
360 366
361sub size_request { 367sub size_request {
362 my ($self) = @_; 368 my ($self) = @_;
396 $chld->draw; 402 $chld->draw;
397} 403}
398 404
399############################################################################# 405#############################################################################
400 406
401package Crossfire::Client::Widget::FancyFrame; 407package CFClient::Widget::FancyFrame;
402 408
403our @ISA = Crossfire::Client::Widget::Bin::; 409our @ISA = CFClient::Widget::Bin::;
404 410
405use SDL::OpenGL; 411use SDL::OpenGL;
406 412
407my @tex = 413my @tex =
408 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 414 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); 415 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
410 416
411sub size_request { 417sub size_request {
412 my ($self) = @_; 418 my ($self) = @_;
413 419
479 485
480} 486}
481 487
482############################################################################# 488#############################################################################
483 489
484package Crossfire::Client::Widget::Table; 490package CFClient::Widget::Table;
485 491
486our @ISA = Crossfire::Client::Widget::Bin::; 492our @ISA = CFClient::Widget::Bin::;
487 493
488use SDL::OpenGL; 494use SDL::OpenGL;
489 495
490sub add { 496sub add {
491 my ($self, $x, $y, $chld) = @_; 497 my ($self, $x, $y, $chld) = @_;
566 } 572 }
567} 573}
568 574
569############################################################################# 575#############################################################################
570 576
571package Crossfire::Client::Widget::VBox; 577package CFClient::Widget::VBox;
572 578
573our @ISA = Crossfire::Client::Widget::Container::; 579our @ISA = CFClient::Widget::Container::;
574 580
575use SDL::OpenGL; 581use SDL::OpenGL;
576 582
577sub size_request { 583sub size_request {
578 my ($self) = @_; 584 my ($self) = @_;
626 } 632 }
627} 633}
628 634
629############################################################################# 635#############################################################################
630 636
631package Crossfire::Client::Widget::Label; 637package CFClient::Widget::Label;
632 638
633our @ISA = Crossfire::Client::Widget::; 639our @ISA = CFClient::Widget::;
634 640
635use SDL::OpenGL; 641use SDL::OpenGL;
636 642
637sub new { 643sub new {
638 my ($class, $x, $y, $z, $height, $text) = @_; 644 my ($class, %arg) = @_;
639
640 $height ||= $::FONTSIZE;
641 645
642 # TODO: color, and make height, xyz etc. optional 646 # TODO: color, and make height, xyz etc. optional
643 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 647 my $self = $class->SUPER::new (
648 color => [1, 1, 1],
649 height => $::FONTSIZE,
650 text => "",
651 layout => new CFClient::Layout,
652 %arg
653 );
644 654
645 $self->set_text ($text); 655 $self->set_text ($self->{text});
646 656
647 $self 657 $self
648} 658}
649 659
650sub set_text { 660sub set_text {
651 my ($self, $text) = @_; 661 my ($self, $text) = @_;
652 662
653 $self->{text} = $text; 663 $self->{text} = $text;
654 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 664 $self->{layout}->set_markup ($text);
655 665
656 $self->update; 666 delete $self->{texture};
657} 667}
658 668
659sub get_text { 669sub get_text {
660 my ($self, $text) = @_; 670 my ($self, $text) = @_;
661 671
663} 673}
664 674
665sub size_request { 675sub size_request {
666 my ($self) = @_; 676 my ($self) = @_;
667 677
678 $self->{layout}->set_width;
679 $self->{layout}->set_height ($self->{height});
680 $self->{layout}->size
668 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 681# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
669 ( 682# (
670 $self->{texture}{width}, 683# $self->{texture}{width},
671 $self->{texture}{height}, 684# $self->{texture}{height},
672 ) 685# )
673 } else { 686# } else {
674 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; 687# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
675 688#
676 ($w, $h) 689# ($w, $h)
677 } 690# }
691}
678 692
693sub size_allocate {
694 my ($self, $w, $h) = @_;
695
696 $self->SUPER::size_allocate ($w, $h);
697 delete $self->{texture};
679} 698}
680 699
681sub _draw { 700sub _draw {
682 my ($self) = @_; 701 my ($self) = @_;
683 702
684 my $tex = $self->{texture}; 703 my $tex = $self->{texture} ||= do {
704 $self->{layout}->set_width ($self->{w});
705 new_from_layout CFClient::Texture $self->{layout};
706 };
685 707
686 glEnable GL_BLEND; 708 glEnable GL_BLEND;
687 glEnable GL_TEXTURE_2D; 709 glEnable GL_TEXTURE_2D;
688 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 710 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
689 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 711 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
690 712
691 glColor 1, 0, 0, 1; # TODO color 713 glColor @{$self->{color}};
692 714
693 $tex->draw_quad (0, 0); 715 $tex->draw_quad (0, 0);
694 716
695 glDisable GL_BLEND; 717 glDisable GL_BLEND;
696 glDisable GL_TEXTURE_2D; 718 glDisable GL_TEXTURE_2D;
697} 719}
698 720
699############################################################################# 721#############################################################################
700 722
701package Crossfire::Client::Widget::Entry; 723package CFClient::Widget::Entry;
702 724
703our @ISA = Crossfire::Client::Widget::Label::; 725our @ISA = CFClient::Widget::Label::;
704 726
705use SDL; 727use SDL;
706use SDL::OpenGL; 728use SDL::OpenGL;
707 729
708sub key_down { 730sub key_down {
754 $self->SUPER::_draw; 776 $self->SUPER::_draw;
755} 777}
756 778
757############################################################################# 779#############################################################################
758 780
759package Crossfire::Client::Widget::MapWidget; 781package CFClient::Widget::MapWidget;
760 782
761use strict; 783use strict;
762 784
763use List::Util qw(min max); 785use List::Util qw(min max);
764 786
765use SDL; 787use SDL;
766use SDL::OpenGL; 788use SDL::OpenGL;
767use SDL::OpenGL::Constants; 789use SDL::OpenGL::Constants;
768 790
769our @ISA = Crossfire::Client::Widget::; 791our @ISA = CFClient::Widget::;
792
793sub new {
794 my $class = shift;
795
796 $class->SUPER::new (
797 z => -1,
798 list => (glGenLists 1),
799 @_
800 )
801}
770 802
771sub key_down { 803sub key_down {
772 print "MAPKEYDOWN\n"; 804 print "MAPKEYDOWN\n";
773} 805}
774 806
780 1 + int $::WIDTH / 32, 812 1 + int $::WIDTH / 32,
781 1 + int $::HEIGHT / 32, 813 1 + int $::HEIGHT / 32,
782 ) 814 )
783} 815}
784 816
817sub update {
818 my ($self) = @_;
819
820 $self->{need_update} = 1;
821}
822
785sub _draw { 823sub _draw {
786 my ($self) = @_; 824 my ($self) = @_;
787 825
826 if (delete $self->{need_update}) {
827 glNewList $self->{list}, GL_COMPILE;
828
788 my $mx = $::CONN->{mapx}; 829 my $mx = $::CONN->{mapx};
789 my $my = $::CONN->{mapy}; 830 my $my = $::CONN->{mapy};
790 831
791 my $map = $::CONN->{map}; 832 my $map = $::CONN->{map};
792 833
793 my ($xofs, $yofs); 834 my ($xofs, $yofs);
794 835
795 my $sw = 1 + int $::WIDTH / 32; 836 my $sw = 1 + int $::WIDTH / 32;
796 my $sh = 1 + int $::HEIGHT / 32; 837 my $sh = 1 + int $::HEIGHT / 32;
797 838
798 if ($::CONN->{mapw} > $sw) { 839 if ($::CONN->{mapw} > $sw) {
799 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 840 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
800 } else { 841 } else {
801 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 842 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
802 } 843 }
803 844
804 if ($::CONN->{maph} > $sh) { 845 if ($::CONN->{maph} > $sh) {
805 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 846 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
806 } else { 847 } else {
807 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 848 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
808 } 849 }
809 850
810 glEnable GL_TEXTURE_2D; 851 glEnable GL_TEXTURE_2D;
811 glEnable GL_BLEND; 852 glEnable GL_BLEND;
812 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 853 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
813 854
814 my $sw4 = ($sw + 3) & ~3; 855 my $sw4 = ($sw + 3) & ~3;
815 my $darkness = "\x00" x ($sw4 * $sh); 856 my $darkness = "\x00" x ($sw4 * $sh);
816 857
817 for my $x (0 .. $sw - 1) { 858 for my $x (0 .. $sw - 1) {
818 my $row = $map->[$x + $xofs]; 859 my $row = $map->[$x + $xofs];
819 for my $y (0 .. $sh - 1) { 860 for my $y (0 .. $sh - 1) {
820 861
821 my $cell = $row->[$y + $yofs] 862 my $cell = $row->[$y + $yofs]
822 or next; 863 or next;
823 864
824 my $dark = $cell->[0]; 865 my $dark = $cell->[0];
825 if ($dark < 0) { 866 if ($dark < 0) {
826 substr $darkness, $y * $sw4 + $x, 1, chr 224; 867 substr $darkness, $y * $sw4 + $x, 1, chr 224;
827 } else { 868 } else {
828 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; 869 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
829 } 870 }
830 871
831 for my $num (grep $_, @$cell[1,2,3]) { 872 for my $num (grep $_, @$cell[1,2,3]) {
832 my $tex = $::CONN->{face}[$num]{texture} || next; 873 my $tex = $::CONN->{face}[$num]{texture} || next;
833 874
834 my $w = $tex->{width}; 875 my $w = $tex->{width};
835 my $h = $tex->{height}; 876 my $h = $tex->{height};
836 877
837 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); 878 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
879 }
838 } 880 }
839 } 881 }
840 }
841 882
842# if (1) { # higher quality darkness 883# if (1) { # higher quality darkness
843# $lighting =~ s/(.)/$1$1$1/gs; 884# $lighting =~ s/(.)/$1$1$1/gs;
844# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; 885# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
845# 886#
847# 888#
848# $lighting = $pb->get_pixels; 889# $lighting = $pb->get_pixels;
849# $lighting =~ s/(.)../$1/gs; 890# $lighting =~ s/(.)../$1/gs;
850# } 891# }
851 892
852 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 893 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
853 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 894 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
854 895
855 $darkness = new Crossfire::Client::Texture 896 $darkness = new CFClient::Texture
856 width => $sw4, 897 width => $sw4,
857 height => $sh, 898 height => $sh,
858 data => $darkness, 899 data => $darkness,
859 internalformat => GL_ALPHA, 900 internalformat => GL_ALPHA,
860 format => GL_ALPHA; 901 format => GL_ALPHA;
861 902
862 glColor 0.45, 0.45, 0.45, 1; 903 glColor 0.45, 0.45, 0.45, 1;
863 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); 904 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
864 905
865 glDisable GL_TEXTURE_2D; 906 glDisable GL_TEXTURE_2D;
866 glDisable GL_BLEND; 907 glDisable GL_BLEND;
908
909 glEndList;
910 }
911
912 glCallList $self->{list};
867} 913}
868 914
869my %DIR = ( 915my %DIR = (
870 SDLK_KP8, [1, "north"], 916 SDLK_KP8, [1, "north"],
871 SDLK_KP9, [2, "northeast"], 917 SDLK_KP9, [2, "northeast"],
917 } 963 }
918} 964}
919 965
920############################################################################# 966#############################################################################
921 967
922package Crossfire::Client::Widget::Animator; 968package CFClient::Widget::Animator;
923 969
924use SDL::OpenGL; 970use SDL::OpenGL;
925 971
926our @ISA = Crossfire::Client::Widget::Bin::; 972our @ISA = CFClient::Widget::Bin::;
927 973
928sub moveto { 974sub moveto {
929 my ($self, $x, $y) = @_; 975 my ($self, $x, $y) = @_;
930 976
931 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 977 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
959 glPopMatrix; 1005 glPopMatrix;
960} 1006}
961 1007
962############################################################################# 1008#############################################################################
963 1009
964package Crossfire::Client::Widget::Toplevel; 1010package CFClient::Widget::Toplevel;
965 1011
966our @ISA = Crossfire::Client::Widget::Container::; 1012our @ISA = CFClient::Widget::Container::;
967 1013
968sub size_request { 1014sub size_request {
969 ($::WIDTH, $::HEIGHT) 1015 ($::WIDTH, $::HEIGHT)
970} 1016}
971 1017
1005 $self->_draw; 1051 $self->_draw;
1006} 1052}
1007 1053
1008############################################################################# 1054#############################################################################
1009 1055
1010package Crossfire::Client::Widget; 1056package CFClient::Widget;
1011 1057
1012$TOPLEVEL = new Crossfire::Client::Widget::Toplevel; 1058$TOPLEVEL = new CFClient::Widget::Toplevel;
1013 1059
10141 10601
1015 1061

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines