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.56 by root, Mon Apr 10 22:16:34 2006 UTC vs.
Revision 1.64 by root, Tue Apr 11 13:38:22 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;
34 $GRAB->update if $GRAB; 34 $GRAB->update if $GRAB;
35 } 35 }
36 36
37 $BUTTON_STATE |= 1 << ($ev->button - 1); 37 $BUTTON_STATE |= 1 << ($ev->button - 1);
38 38
39 $GRAB->button_down ($ev) if $GRAB; 39 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
40} 40}
41 41
42sub feed_sdl_button_up_event { 42sub feed_sdl_button_up_event {
43 my ($ev) = @_; 43 my ($ev) = @_;
44 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 44 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
45 45
46 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 46 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
47 47
48 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 48 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
49
50 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
49 51
50 if (!$BUTTON_STATE) { 52 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 53 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 54 $grab->update if $grab;
53 $GRAB->update if $GRAB; 55 $GRAB->update if $GRAB;
65 67
66 $hover->update if $hover; 68 $hover->update if $hover;
67 $HOVER->update if $HOVER; 69 $HOVER->update if $HOVER;
68 } 70 }
69 71
70 $HOVER->mouse_motion ($ev) if $HOVER; 72 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
71} 73}
72 74
73sub new { 75sub new {
74 my $class = shift; 76 my $class = shift;
75 77
95sub size_allocate { 97sub size_allocate {
96 my ($self, $w, $h) = @_; 98 my ($self, $w, $h) = @_;
97 99
98 $self->{w} = $w; 100 $self->{w} = $w;
99 $self->{h} = $h; 101 $self->{h} = $h;
102}
103
104# translate global koordinates to local coordinate system
105sub translate {
106 my ($self, $x, $y) = @_;
107
108 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
100} 109}
101 110
102sub focus_in { 111sub focus_in {
103 my ($self) = @_; 112 my ($self) = @_;
104 113
201 #$self->deactivate; 210 #$self->deactivate;
202} 211}
203 212
204############################################################################# 213#############################################################################
205 214
206package Crossfire::Client::Widget::Container; 215package CFClient::Widget::Container;
207 216
208our @ISA = Crossfire::Client::Widget::; 217our @ISA = CFClient::Widget::;
209 218
210sub new { 219sub new {
211 my ($class, @widgets) = @_; 220 my ($class, %arg) = @_;
221
222 my $children = delete $arg{children} || [];
212 223
213 my $self = $class->SUPER::new (children => []); 224 my $self = $class->SUPER::new (children => []);
214 $self->add ($_) for @widgets; 225 $self->add ($_) for @$children;
215 226
216 $self 227 $self
217} 228}
218 229
219sub add { 230sub add {
260 $_->draw for @{$self->{children}}; 271 $_->draw for @{$self->{children}};
261} 272}
262 273
263############################################################################# 274#############################################################################
264 275
265package Crossfire::Client::Widget::Bin; 276package CFClient::Widget::Bin;
266 277
267our @ISA = Crossfire::Client::Widget::Container::; 278our @ISA = CFClient::Widget::Container::;
268 279
269sub child { $_[0]->{children}[0] } 280sub child { $_[0]->{children}[0] }
270 281
271sub size_request { 282sub size_request {
272 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 283 $_[0]{children}[0]->size_request if $_[0]{children}[0];
280 if $self->{children}[0] 291 if $self->{children}[0]
281} 292}
282 293
283############################################################################# 294#############################################################################
284 295
285package Crossfire::Client::Widget::Window; 296package CFClient::Widget::Window;
286 297
287our @ISA = Crossfire::Client::Widget::Bin::; 298our @ISA = CFClient::Widget::Bin::;
288 299
289use SDL::OpenGL; 300use SDL::OpenGL;
290 301
291sub new { 302sub new {
292 my ($class, $x, $y, $z, $w, $h) = @_; 303 my ($class, %arg) = @_;
293 304
294 my $self = $class->SUPER::new; 305 my $self = $class->SUPER::new (%arg);
295
296 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
297} 306}
298 307
299sub update { 308sub update {
300 my ($self) = @_; 309 my ($self) = @_;
301 310
311 # we want to do this delayed...
302 $self->render_chld; 312 $self->render_chld;
303 $self->SUPER::update; 313 $self->SUPER::update;
304} 314}
305 315
306sub render_chld { 316sub render_chld {
307 my ($self) = @_; 317 my ($self) = @_;
308 318
309 $self->{texture} = 319 $self->{texture} =
310 Crossfire::Client::Texture->new_from_opengl ( 320 CFClient::Texture->new_from_opengl (
311 $self->{w}, $self->{h}, sub { $self->child->draw } 321 $self->{w}, $self->{h}, sub { $self->child->draw }
312 ); 322 );
313} 323}
314 324
315sub size_allocate { 325sub size_allocate {
341 glDisable GL_TEXTURE_2D; 351 glDisable GL_TEXTURE_2D;
342} 352}
343 353
344############################################################################# 354#############################################################################
345 355
346package Crossfire::Client::Widget::Frame; 356package CFClient::Widget::Frame;
347 357
348our @ISA = Crossfire::Client::Widget::Bin::; 358our @ISA = CFClient::Widget::Bin::;
349 359
350use SDL::OpenGL; 360use SDL::OpenGL;
351 361
352sub size_request { 362sub size_request {
353 my ($self) = @_; 363 my ($self) = @_;
387 $chld->draw; 397 $chld->draw;
388} 398}
389 399
390############################################################################# 400#############################################################################
391 401
392package Crossfire::Client::Widget::FancyFrame; 402package CFClient::Widget::FancyFrame;
393 403
394our @ISA = Crossfire::Client::Widget::Bin::; 404our @ISA = CFClient::Widget::Bin::;
395 405
396use SDL::OpenGL; 406use SDL::OpenGL;
397 407
398my @tex = 408my @tex =
399 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 409 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
400 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 410 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
401 411
402sub size_request { 412sub size_request {
403 my ($self) = @_; 413 my ($self) = @_;
404 414
470 480
471} 481}
472 482
473############################################################################# 483#############################################################################
474 484
475package Crossfire::Client::Widget::Table; 485package CFClient::Widget::Table;
476 486
477our @ISA = Crossfire::Client::Widget::Bin::; 487our @ISA = CFClient::Widget::Bin::;
478 488
479use SDL::OpenGL; 489use SDL::OpenGL;
480 490
481sub add { 491sub add {
482 my ($self, $x, $y, $chld) = @_; 492 my ($self, $x, $y, $chld) = @_;
557 } 567 }
558} 568}
559 569
560############################################################################# 570#############################################################################
561 571
562package Crossfire::Client::Widget::VBox; 572package CFClient::Widget::VBox;
563 573
564our @ISA = Crossfire::Client::Widget::Container::; 574our @ISA = CFClient::Widget::Container::;
565 575
566use SDL::OpenGL; 576use SDL::OpenGL;
567 577
568sub size_request { 578sub size_request {
569 my ($self) = @_; 579 my ($self) = @_;
617 } 627 }
618} 628}
619 629
620############################################################################# 630#############################################################################
621 631
622package Crossfire::Client::Widget::Label; 632package CFClient::Widget::Label;
623 633
624our @ISA = Crossfire::Client::Widget::; 634our @ISA = CFClient::Widget::;
625 635
626use SDL::OpenGL; 636use SDL::OpenGL;
627 637
628sub new { 638sub new {
629 my ($class, $x, $y, $z, $height, $text) = @_; 639 my ($class, %arg) = @_;
630
631 $height ||= $::FONTSIZE;
632 640
633 # TODO: color, and make height, xyz etc. optional 641 # TODO: color, and make height, xyz etc. optional
634 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 642 my $self = $class->SUPER::new (
643 color => [1, 1, 1],
644 height => $::FONTSIZE,
645 text => "",
646 layout => new CFClient::Layout,
647 %arg
648 );
635 649
636 $self->set_text ($text); 650 $self->set_text ($self->{text});
637 651
638 $self 652 $self
639} 653}
640 654
641sub set_text { 655sub set_text {
642 my ($self, $text) = @_; 656 my ($self, $text) = @_;
643 657
644 $self->{text} = $text; 658 $self->{text} = $text;
645 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 659 $self->{layout}->set_markup ($text);
646 660
647 $self->update; 661 delete $self->{texture};
648} 662}
649 663
650sub get_text { 664sub get_text {
651 my ($self, $text) = @_; 665 my ($self, $text) = @_;
652 666
654} 668}
655 669
656sub size_request { 670sub size_request {
657 my ($self) = @_; 671 my ($self) = @_;
658 672
673 $self->{layout}->set_width;
674 $self->{layout}->set_height ($self->{height});
675 $self->{layout}->size
659 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 676# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
660 ( 677# (
661 $self->{texture}{width}, 678# $self->{texture}{width},
662 $self->{texture}{height}, 679# $self->{texture}{height},
663 ) 680# )
664 } else { 681# } else {
665 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; 682# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
666 683#
667 ($w, $h) 684# ($w, $h)
668 } 685# }
686}
669 687
688sub size_allocate {
689 my ($self, $w, $h) = @_;
690
691 $self->SUPER::size_allocate ($w, $h);
692 delete $self->{texture};
670} 693}
671 694
672sub _draw { 695sub _draw {
673 my ($self) = @_; 696 my ($self) = @_;
674 697
675 my $tex = $self->{texture}; 698 my $tex = $self->{texture} ||= do {
699 $self->{layout}->set_width ($self->{w});
700 new_from_layout CFClient::Texture $self->{layout};
701 };
676 702
677 glEnable GL_BLEND; 703 glEnable GL_BLEND;
678 glEnable GL_TEXTURE_2D; 704 glEnable GL_TEXTURE_2D;
679 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 705 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
680 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 706 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
681 707
682 glColor 1, 0, 0, 1; # TODO color 708 glColor @{$self->{color}};
683 709
684 $tex->draw_quad (0, 0); 710 $tex->draw_quad (0, 0);
685 711
686 glDisable GL_BLEND; 712 glDisable GL_BLEND;
687 glDisable GL_TEXTURE_2D; 713 glDisable GL_TEXTURE_2D;
688} 714}
689 715
690############################################################################# 716#############################################################################
691 717
692package Crossfire::Client::Widget::Entry; 718package CFClient::Widget::Entry;
693 719
694our @ISA = Crossfire::Client::Widget::Label::; 720our @ISA = CFClient::Widget::Label::;
695 721
696use SDL; 722use SDL;
697use SDL::OpenGL; 723use SDL::OpenGL;
698 724
699sub key_down { 725sub key_down {
708 734
709 if ($sym == SDLK_BACKSPACE) { 735 if ($sym == SDLK_BACKSPACE) {
710 substr $text, -1, 1, ''; 736 substr $text, -1, 1, '';
711 } elsif ($uni) { 737 } elsif ($uni) {
712 $text .= chr $uni; 738 $text .= chr $uni;
713 print "$uni <$text>\n";#d#
714 } 739 }
715 740
716 $self->set_text ($text); 741 $self->set_text ($text);
717} 742}
718 743
719sub button_down { 744sub button_down {
720 my ($self, $ev) = @_; 745 my ($self, $ev) = @_;
721 746
722 $self->focus_in; 747 $self->focus_in;
748}
749
750sub mouse_motion {
751 my ($self, $ev, $x, $y) = @_;
752 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
723} 753}
724 754
725sub _draw { 755sub _draw {
726 my ($self) = @_; 756 my ($self) = @_;
727 757
741 $self->SUPER::_draw; 771 $self->SUPER::_draw;
742} 772}
743 773
744############################################################################# 774#############################################################################
745 775
746package Crossfire::Client::Widget::MapWidget; 776package CFClient::Widget::MapWidget;
747 777
748use strict; 778use strict;
749 779
750use List::Util qw(min max); 780use List::Util qw(min max);
751 781
752use SDL; 782use SDL;
753use SDL::OpenGL; 783use SDL::OpenGL;
754use SDL::OpenGL::Constants; 784use SDL::OpenGL::Constants;
755 785
756our @ISA = Crossfire::Client::Widget::; 786our @ISA = CFClient::Widget::;
787
788sub new {
789 my $class = shift;
790
791 $class->SUPER::new (z => -1, @_)
792}
757 793
758sub key_down { 794sub key_down {
759 print "MAPKEYDOWN\n"; 795 print "MAPKEYDOWN\n";
760} 796}
761 797
797 glEnable GL_TEXTURE_2D; 833 glEnable GL_TEXTURE_2D;
798 glEnable GL_BLEND; 834 glEnable GL_BLEND;
799 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 835 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
800 836
801 my $sw4 = ($sw + 3) & ~3; 837 my $sw4 = ($sw + 3) & ~3;
802 my $lighting = "\x00" x ($sw4 * $sh); 838 my $darkness = "\x00" x ($sw4 * $sh);
803 839
804 for my $x (0 .. $sw - 1) { 840 for my $x (0 .. $sw - 1) {
841 my $row = $map->[$x + $xofs];
805 for my $y (0 .. $sh - 1) { 842 for my $y (0 .. $sh - 1) {
806 843
807 my $cell = $map->[$x + $xofs][$y + $yofs] 844 my $cell = $row->[$y + $yofs]
808 or next; 845 or next;
809 846
810 my $darkness = $cell->[0] * (1 / 255); 847 my $dark = $cell->[0];
811 if ($darkness < 0) { 848 if ($dark < 0) {
812 $darkness = $cell->[1] ? 0.1 : 0; 849 substr $darkness, $y * $sw4 + $x, 1, chr 224;
850 } else {
851 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
813 } 852 }
814 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
815 853
816 for my $num (grep $_, @$cell[1,2,3]) { 854 for my $num (grep $_, @$cell[1,2,3]) {
817 my $tex = $::CONN->{face}[$num]{texture} || next; 855 my $tex = $::CONN->{face}[$num]{texture} || next;
818 856
819 my $w = $tex->{width}; 857 my $w = $tex->{width};
820 my $h = $tex->{height}; 858 my $h = $tex->{height};
821 859
822 my $px = ($x + 1) * 32 - $w; 860 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
823 my $py = ($y + 1) * 32 - $h;
824
825 $tex->draw_quad ($px, $py, $w, $h);
826 } 861 }
827 } 862 }
828 } 863 }
829 864
830# if (1) { # higher quality darkness 865# if (1) { # higher quality darkness
835# 870#
836# $lighting = $pb->get_pixels; 871# $lighting = $pb->get_pixels;
837# $lighting =~ s/(.)../$1/gs; 872# $lighting =~ s/(.)../$1/gs;
838# } 873# }
839 874
840 $lighting = new Crossfire::Client::Texture 875 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
876 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
877
878 $darkness = new CFClient::Texture
841 width => $sw4, 879 width => $sw4,
842 height => $sh, 880 height => $sh,
843 data => $lighting, 881 data => $darkness,
844 internalformat => GL_ALPHA, 882 internalformat => GL_ALPHA,
845 format => GL_ALPHA; 883 format => GL_ALPHA;
846 884
847 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
848 glColor 0.45, 0.45, 0.45, 1; 885 glColor 0.45, 0.45, 0.45, 1;
849 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
850 glBindTexture GL_TEXTURE_2D, $lighting->{name};
851 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
852
853 $lighting->draw_quad (0, 0, $sw4 * 32, $sh * 32); 886 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
854 887
855 glDisable GL_TEXTURE_2D; 888 glDisable GL_TEXTURE_2D;
856 glDisable GL_BLEND; 889 glDisable GL_BLEND;
857} 890}
858 891
907 } 940 }
908} 941}
909 942
910############################################################################# 943#############################################################################
911 944
912package Crossfire::Client::Widget::Animator; 945package CFClient::Widget::Animator;
913 946
914use SDL::OpenGL; 947use SDL::OpenGL;
915 948
916our @ISA = Crossfire::Client::Widget::Bin::; 949our @ISA = CFClient::Widget::Bin::;
917 950
918sub moveto { 951sub moveto {
919 my ($self, $x, $y) = @_; 952 my ($self, $x, $y) = @_;
920 953
921 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 954 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
926} 959}
927 960
928sub animate { 961sub animate {
929 my ($self, $interval) = @_; 962 my ($self, $interval) = @_;
930 963
931 printf "%5.2f\n", 1 / $interval;#d#
932
933 $self->{time} -= $interval * $self->{speed}; 964 $self->{time} -= $interval * $self->{speed};
934 if ($self->{time} <= 0) { 965 if ($self->{time} <= 0) {
935 $self->{time} = 0; 966 $self->{time} = 0;
936 ::animation_stop $self; 967 ::animation_stop $self;
937 } 968 }
951 glPopMatrix; 982 glPopMatrix;
952} 983}
953 984
954############################################################################# 985#############################################################################
955 986
956package Crossfire::Client::Widget::Toplevel; 987package CFClient::Widget::Toplevel;
957 988
958our @ISA = Crossfire::Client::Widget::Container::; 989our @ISA = CFClient::Widget::Container::;
959 990
960sub size_request { 991sub size_request {
961 ($::WIDTH, $::HEIGHT) 992 ($::WIDTH, $::HEIGHT)
962} 993}
963 994
968 999
969 $_->size_allocate ($_->size_request) 1000 $_->size_allocate ($_->size_request)
970 for @{$self->{children}}; 1001 for @{$self->{children}};
971} 1002}
972 1003
1004sub translate {
1005 my ($self, $x, $y) = @_;
1006
1007 ($x, $y)
1008}
1009
973sub update { 1010sub update {
974 my ($self) = @_; 1011 my ($self) = @_;
975 1012
976 $self->size_allocate ($self->size_request); 1013 $self->size_allocate ($self->size_request);
977 ::refresh (); 1014 ::refresh ();
991 $self->_draw; 1028 $self->_draw;
992} 1029}
993 1030
994############################################################################# 1031#############################################################################
995 1032
996package Crossfire::Client::Widget; 1033package CFClient::Widget;
997 1034
998$TOPLEVEL = new Crossfire::Client::Widget::Toplevel; 1035$TOPLEVEL = new CFClient::Widget::Toplevel;
999 1036
10001 10371
1001 1038

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines