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.63 by root, Tue Apr 11 13:29:24 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, @widgets) = @_;
212 221
213 my $self = $class->SUPER::new (children => []); 222 my $self = $class->SUPER::new (children => []);
260 $_->draw for @{$self->{children}}; 269 $_->draw for @{$self->{children}};
261} 270}
262 271
263############################################################################# 272#############################################################################
264 273
265package Crossfire::Client::Widget::Bin; 274package CFClient::Widget::Bin;
266 275
267our @ISA = Crossfire::Client::Widget::Container::; 276our @ISA = CFClient::Widget::Container::;
268 277
269sub child { $_[0]->{children}[0] } 278sub child { $_[0]->{children}[0] }
270 279
271sub size_request { 280sub size_request {
272 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 281 $_[0]{children}[0]->size_request if $_[0]{children}[0];
280 if $self->{children}[0] 289 if $self->{children}[0]
281} 290}
282 291
283############################################################################# 292#############################################################################
284 293
285package Crossfire::Client::Widget::Window; 294package CFClient::Widget::Window;
286 295
287our @ISA = Crossfire::Client::Widget::Bin::; 296our @ISA = CFClient::Widget::Bin::;
288 297
289use SDL::OpenGL; 298use SDL::OpenGL;
290 299
291sub new { 300sub new {
292 my ($class, $x, $y, $z, $w, $h) = @_; 301 my ($class, $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, $x, $y, $z, $height, $text) = @_;
630 640
631 $height ||= $::FONTSIZE; 641 $height ||= $::FONTSIZE;
632 642
633 # TODO: color, and make height, xyz etc. optional 643 # TODO: color, and make height, xyz etc. optional
634 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 644 my $self = $class->SUPER::new (
645 color => [1, 1, 1],
646 x => $x,
647 y => $y,
648 z => $z,
649 height => $height,
650 layout => new CFClient::Layout $height,
651 );
635 652
636 $self->set_text ($text); 653 $self->set_text ($text);
637 654
638 $self 655 $self
639} 656}
640 657
641sub set_text { 658sub set_text {
642 my ($self, $text) = @_; 659 my ($self, $text) = @_;
643 660
644 $self->{text} = $text; 661 $self->{text} = $text;
645 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 662 $self->{layout}->set_markup ($text);
646 663
647 $self->update; 664 delete $self->{texture};
648} 665}
649 666
650sub get_text { 667sub get_text {
651 my ($self, $text) = @_; 668 my ($self, $text) = @_;
652 669
654} 671}
655 672
656sub size_request { 673sub size_request {
657 my ($self) = @_; 674 my ($self) = @_;
658 675
676 $self->{layout}->set_width;
677 $self->{layout}->size
659 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 678# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
660 ( 679# (
661 $self->{texture}{width}, 680# $self->{texture}{width},
662 $self->{texture}{height}, 681# $self->{texture}{height},
663 ) 682# )
664 } else { 683# } else {
665 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; 684# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
666 685#
667 ($w, $h) 686# ($w, $h)
668 } 687# }
688}
669 689
690sub size_allocate {
691 my ($self, $w, $h) = @_;
692
693 $self->SUPER::size_allocate ($w, $h);
694 delete $self->{texture};
670} 695}
671 696
672sub _draw { 697sub _draw {
673 my ($self) = @_; 698 my ($self) = @_;
674 699
675 my $tex = $self->{texture}; 700 my $tex = $self->{texture} ||= do {
701 $self->{layout}->set_width ($self->{w});
702 new_from_layout CFClient::Texture $self->{layout};
703 };
676 704
677 glEnable GL_BLEND; 705 glEnable GL_BLEND;
678 glEnable GL_TEXTURE_2D; 706 glEnable GL_TEXTURE_2D;
679 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 707 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
680 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 708 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
681 709
682 glColor 1, 0, 0, 1; # TODO color 710 glColor @{$self->{color}};
683 711
684 $tex->draw_quad (0, 0); 712 $tex->draw_quad (0, 0);
685 713
686 glDisable GL_BLEND; 714 glDisable GL_BLEND;
687 glDisable GL_TEXTURE_2D; 715 glDisable GL_TEXTURE_2D;
688} 716}
689 717
690############################################################################# 718#############################################################################
691 719
692package Crossfire::Client::Widget::Entry; 720package CFClient::Widget::Entry;
693 721
694our @ISA = Crossfire::Client::Widget::Label::; 722our @ISA = CFClient::Widget::Label::;
695 723
696use SDL; 724use SDL;
697use SDL::OpenGL; 725use SDL::OpenGL;
698 726
699sub key_down { 727sub key_down {
708 736
709 if ($sym == SDLK_BACKSPACE) { 737 if ($sym == SDLK_BACKSPACE) {
710 substr $text, -1, 1, ''; 738 substr $text, -1, 1, '';
711 } elsif ($uni) { 739 } elsif ($uni) {
712 $text .= chr $uni; 740 $text .= chr $uni;
713 print "$uni <$text>\n";#d#
714 } 741 }
715 742
716 $self->set_text ($text); 743 $self->set_text ($text);
717} 744}
718 745
719sub button_down { 746sub button_down {
720 my ($self, $ev) = @_; 747 my ($self, $ev) = @_;
721 748
722 $self->focus_in; 749 $self->focus_in;
750}
751
752sub mouse_motion {
753 my ($self, $ev, $x, $y) = @_;
754 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
723} 755}
724 756
725sub _draw { 757sub _draw {
726 my ($self) = @_; 758 my ($self) = @_;
727 759
741 $self->SUPER::_draw; 773 $self->SUPER::_draw;
742} 774}
743 775
744############################################################################# 776#############################################################################
745 777
746package Crossfire::Client::Widget::MapWidget; 778package CFClient::Widget::MapWidget;
747 779
748use strict; 780use strict;
749 781
750use List::Util qw(min max); 782use List::Util qw(min max);
751 783
752use SDL; 784use SDL;
753use SDL::OpenGL; 785use SDL::OpenGL;
754use SDL::OpenGL::Constants; 786use SDL::OpenGL::Constants;
755 787
756our @ISA = Crossfire::Client::Widget::; 788our @ISA = CFClient::Widget::;
757 789
758sub key_down { 790sub key_down {
759 print "MAPKEYDOWN\n"; 791 print "MAPKEYDOWN\n";
760} 792}
761 793
797 glEnable GL_TEXTURE_2D; 829 glEnable GL_TEXTURE_2D;
798 glEnable GL_BLEND; 830 glEnable GL_BLEND;
799 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 831 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
800 832
801 my $sw4 = ($sw + 3) & ~3; 833 my $sw4 = ($sw + 3) & ~3;
802 my $lighting = "\x00" x ($sw4 * $sh); 834 my $darkness = "\x00" x ($sw4 * $sh);
803 835
804 for my $x (0 .. $sw - 1) { 836 for my $x (0 .. $sw - 1) {
837 my $row = $map->[$x + $xofs];
805 for my $y (0 .. $sh - 1) { 838 for my $y (0 .. $sh - 1) {
806 839
807 my $cell = $map->[$x + $xofs][$y + $yofs] 840 my $cell = $row->[$y + $yofs]
808 or next; 841 or next;
809 842
810 my $darkness = $cell->[0] * (1 / 255); 843 my $dark = $cell->[0];
811 if ($darkness < 0) { 844 if ($dark < 0) {
812 $darkness = $cell->[1] ? 0.1 : 0; 845 substr $darkness, $y * $sw4 + $x, 1, chr 224;
846 } else {
847 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
813 } 848 }
814 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
815 849
816 for my $num (grep $_, @$cell[1,2,3]) { 850 for my $num (grep $_, @$cell[1,2,3]) {
817 my $tex = $::CONN->{face}[$num]{texture} || next; 851 my $tex = $::CONN->{face}[$num]{texture} || next;
818 852
819 my $w = $tex->{width}; 853 my $w = $tex->{width};
820 my $h = $tex->{height}; 854 my $h = $tex->{height};
821 855
822 my $px = ($x + 1) * 32 - $w; 856 $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 } 857 }
827 } 858 }
828 } 859 }
829 860
830# if (1) { # higher quality darkness 861# if (1) { # higher quality darkness
835# 866#
836# $lighting = $pb->get_pixels; 867# $lighting = $pb->get_pixels;
837# $lighting =~ s/(.)../$1/gs; 868# $lighting =~ s/(.)../$1/gs;
838# } 869# }
839 870
840 $lighting = new Crossfire::Client::Texture 871 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
872 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
873
874 $darkness = new CFClient::Texture
841 width => $sw4, 875 width => $sw4,
842 height => $sh, 876 height => $sh,
843 data => $lighting, 877 data => $darkness,
844 internalformat => GL_ALPHA, 878 internalformat => GL_ALPHA,
845 format => GL_ALPHA; 879 format => GL_ALPHA;
846 880
847 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
848 glColor 0.45, 0.45, 0.45, 1; 881 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); 882 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
854 883
855 glDisable GL_TEXTURE_2D; 884 glDisable GL_TEXTURE_2D;
856 glDisable GL_BLEND; 885 glDisable GL_BLEND;
857} 886}
858 887
907 } 936 }
908} 937}
909 938
910############################################################################# 939#############################################################################
911 940
912package Crossfire::Client::Widget::Animator; 941package CFClient::Widget::Animator;
913 942
914use SDL::OpenGL; 943use SDL::OpenGL;
915 944
916our @ISA = Crossfire::Client::Widget::Bin::; 945our @ISA = CFClient::Widget::Bin::;
917 946
918sub moveto { 947sub moveto {
919 my ($self, $x, $y) = @_; 948 my ($self, $x, $y) = @_;
920 949
921 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 950 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
926} 955}
927 956
928sub animate { 957sub animate {
929 my ($self, $interval) = @_; 958 my ($self, $interval) = @_;
930 959
931 printf "%5.2f\n", 1 / $interval;#d#
932
933 $self->{time} -= $interval * $self->{speed}; 960 $self->{time} -= $interval * $self->{speed};
934 if ($self->{time} <= 0) { 961 if ($self->{time} <= 0) {
935 $self->{time} = 0; 962 $self->{time} = 0;
936 ::animation_stop $self; 963 ::animation_stop $self;
937 } 964 }
951 glPopMatrix; 978 glPopMatrix;
952} 979}
953 980
954############################################################################# 981#############################################################################
955 982
956package Crossfire::Client::Widget::Toplevel; 983package CFClient::Widget::Toplevel;
957 984
958our @ISA = Crossfire::Client::Widget::Container::; 985our @ISA = CFClient::Widget::Container::;
959 986
960sub size_request { 987sub size_request {
961 ($::WIDTH, $::HEIGHT) 988 ($::WIDTH, $::HEIGHT)
962} 989}
963 990
968 995
969 $_->size_allocate ($_->size_request) 996 $_->size_allocate ($_->size_request)
970 for @{$self->{children}}; 997 for @{$self->{children}};
971} 998}
972 999
1000sub translate {
1001 my ($self, $x, $y) = @_;
1002
1003 ($x, $y)
1004}
1005
973sub update { 1006sub update {
974 my ($self) = @_; 1007 my ($self) = @_;
975 1008
976 $self->size_allocate ($self->size_request); 1009 $self->size_allocate ($self->size_request);
977 ::refresh (); 1010 ::refresh ();
991 $self->_draw; 1024 $self->_draw;
992} 1025}
993 1026
994############################################################################# 1027#############################################################################
995 1028
996package Crossfire::Client::Widget; 1029package CFClient::Widget;
997 1030
998$TOPLEVEL = new Crossfire::Client::Widget::Toplevel; 1031$TOPLEVEL = new CFClient::Widget::Toplevel;
999 1032
10001 10331
1001 1034

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines