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.62 by root, Tue Apr 11 13:27:48 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) = @_;
305 314
306sub render_chld { 315sub render_chld {
307 my ($self) = @_; 316 my ($self) = @_;
308 317
309 $self->{texture} = 318 $self->{texture} =
310 Crossfire::Client::Texture->new_from_opengl ( 319 CFClient::Texture->new_from_opengl (
311 $self->{w}, $self->{h}, sub { $self->child->draw } 320 $self->{w}, $self->{h}, sub { $self->child->draw }
312 ); 321 );
313} 322}
314 323
315sub size_allocate { 324sub size_allocate {
341 glDisable GL_TEXTURE_2D; 350 glDisable GL_TEXTURE_2D;
342} 351}
343 352
344############################################################################# 353#############################################################################
345 354
346package Crossfire::Client::Widget::Frame; 355package CFClient::Widget::Frame;
347 356
348our @ISA = Crossfire::Client::Widget::Bin::; 357our @ISA = CFClient::Widget::Bin::;
349 358
350use SDL::OpenGL; 359use SDL::OpenGL;
351 360
352sub size_request { 361sub size_request {
353 my ($self) = @_; 362 my ($self) = @_;
387 $chld->draw; 396 $chld->draw;
388} 397}
389 398
390############################################################################# 399#############################################################################
391 400
392package Crossfire::Client::Widget::FancyFrame; 401package CFClient::Widget::FancyFrame;
393 402
394our @ISA = Crossfire::Client::Widget::Bin::; 403our @ISA = CFClient::Widget::Bin::;
395 404
396use SDL::OpenGL; 405use SDL::OpenGL;
397 406
398my @tex = 407my @tex =
399 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 408 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); 409 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
401 410
402sub size_request { 411sub size_request {
403 my ($self) = @_; 412 my ($self) = @_;
404 413
470 479
471} 480}
472 481
473############################################################################# 482#############################################################################
474 483
475package Crossfire::Client::Widget::Table; 484package CFClient::Widget::Table;
476 485
477our @ISA = Crossfire::Client::Widget::Bin::; 486our @ISA = CFClient::Widget::Bin::;
478 487
479use SDL::OpenGL; 488use SDL::OpenGL;
480 489
481sub add { 490sub add {
482 my ($self, $x, $y, $chld) = @_; 491 my ($self, $x, $y, $chld) = @_;
557 } 566 }
558} 567}
559 568
560############################################################################# 569#############################################################################
561 570
562package Crossfire::Client::Widget::VBox; 571package CFClient::Widget::VBox;
563 572
564our @ISA = Crossfire::Client::Widget::Container::; 573our @ISA = CFClient::Widget::Container::;
565 574
566use SDL::OpenGL; 575use SDL::OpenGL;
567 576
568sub size_request { 577sub size_request {
569 my ($self) = @_; 578 my ($self) = @_;
617 } 626 }
618} 627}
619 628
620############################################################################# 629#############################################################################
621 630
622package Crossfire::Client::Widget::Label; 631package CFClient::Widget::Label;
623 632
624our @ISA = Crossfire::Client::Widget::; 633our @ISA = CFClient::Widget::;
625 634
626use SDL::OpenGL; 635use SDL::OpenGL;
627 636
628sub new { 637sub new {
629 my ($class, $x, $y, $z, $height, $text) = @_; 638 my ($class, $x, $y, $z, $height, $text) = @_;
630 639
631 $height ||= $::FONTSIZE; 640 $height ||= $::FONTSIZE;
632 641
633 # TODO: color, and make height, xyz etc. optional 642 # TODO: color, and make height, xyz etc. optional
634 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 643 my $self = $class->SUPER::new (
644 color => [1, 1, 1],
645 x => $x,
646 y => $y,
647 z => $z,
648 height => $height,
649 layout => new CFClient::Layout $height,
650 );
635 651
636 $self->set_text ($text); 652 $self->set_text ($text);
637 653
638 $self 654 $self
639} 655}
640 656
641sub set_text { 657sub set_text {
642 my ($self, $text) = @_; 658 my ($self, $text) = @_;
643 659
644 $self->{text} = $text; 660 $self->{text} = $text;
645 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 661 $self->{layout}->set_markup ($text);
646 662
647 $self->update; 663 delete $self->{texture};
648} 664}
649 665
650sub get_text { 666sub get_text {
651 my ($self, $text) = @_; 667 my ($self, $text) = @_;
652 668
654} 670}
655 671
656sub size_request { 672sub size_request {
657 my ($self) = @_; 673 my ($self) = @_;
658 674
675 $self->{layout}->set_width;
676 $self->{layout}->size
659 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 677# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
660 ( 678# (
661 $self->{texture}{width}, 679# $self->{texture}{width},
662 $self->{texture}{height}, 680# $self->{texture}{height},
663 ) 681# )
664 } else { 682# } else {
665 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; 683# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
666 684#
667 ($w, $h) 685# ($w, $h)
668 } 686# }
687}
669 688
689sub size_allocate {
690 my ($self, $w, $h) = @_;
691
692 $self->SUPER::size_allocate ($w, $h);
693 delete $self->{texture};
670} 694}
671 695
672sub _draw { 696sub _draw {
673 my ($self) = @_; 697 my ($self) = @_;
674 698
675 my $tex = $self->{texture}; 699 my $tex = $self->{texture} ||= do {
700 $self->{layout}->set_width ($self->{w});
701 new_from_layout CFClient::Texture $self->{layout};
702 };
676 703
677 glEnable GL_BLEND; 704 glEnable GL_BLEND;
678 glEnable GL_TEXTURE_2D; 705 glEnable GL_TEXTURE_2D;
679 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 706 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
680 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 707 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
681 708
682 glColor 1, 0, 0, 1; # TODO color 709 glColor @{$self->{color}};
683 710
684 $tex->draw_quad (0, 0); 711 $tex->draw_quad (0, 0);
685 712
686 glDisable GL_BLEND; 713 glDisable GL_BLEND;
687 glDisable GL_TEXTURE_2D; 714 glDisable GL_TEXTURE_2D;
688} 715}
689 716
690############################################################################# 717#############################################################################
691 718
692package Crossfire::Client::Widget::Entry; 719package CFClient::Widget::Entry;
693 720
694our @ISA = Crossfire::Client::Widget::Label::; 721our @ISA = CFClient::Widget::Label::;
695 722
696use SDL; 723use SDL;
697use SDL::OpenGL; 724use SDL::OpenGL;
698 725
699sub key_down { 726sub key_down {
708 735
709 if ($sym == SDLK_BACKSPACE) { 736 if ($sym == SDLK_BACKSPACE) {
710 substr $text, -1, 1, ''; 737 substr $text, -1, 1, '';
711 } elsif ($uni) { 738 } elsif ($uni) {
712 $text .= chr $uni; 739 $text .= chr $uni;
713 print "$uni <$text>\n";#d#
714 } 740 }
715 741
716 $self->set_text ($text); 742 $self->set_text ($text);
717} 743}
718 744
719sub button_down { 745sub button_down {
720 my ($self, $ev) = @_; 746 my ($self, $ev) = @_;
721 747
722 $self->focus_in; 748 $self->focus_in;
749}
750
751sub mouse_motion {
752 my ($self, $ev, $x, $y) = @_;
753 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
723} 754}
724 755
725sub _draw { 756sub _draw {
726 my ($self) = @_; 757 my ($self) = @_;
727 758
741 $self->SUPER::_draw; 772 $self->SUPER::_draw;
742} 773}
743 774
744############################################################################# 775#############################################################################
745 776
746package Crossfire::Client::Widget::MapWidget; 777package CFClient::Widget::MapWidget;
747 778
748use strict; 779use strict;
749 780
750use List::Util qw(min max); 781use List::Util qw(min max);
751 782
752use SDL; 783use SDL;
753use SDL::OpenGL; 784use SDL::OpenGL;
754use SDL::OpenGL::Constants; 785use SDL::OpenGL::Constants;
755 786
756our @ISA = Crossfire::Client::Widget::; 787our @ISA = CFClient::Widget::;
757 788
758sub key_down { 789sub key_down {
759 print "MAPKEYDOWN\n"; 790 print "MAPKEYDOWN\n";
760} 791}
761 792
797 glEnable GL_TEXTURE_2D; 828 glEnable GL_TEXTURE_2D;
798 glEnable GL_BLEND; 829 glEnable GL_BLEND;
799 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 830 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
800 831
801 my $sw4 = ($sw + 3) & ~3; 832 my $sw4 = ($sw + 3) & ~3;
802 my $lighting = "\x00" x ($sw4 * $sh); 833 my $darkness = "\x00" x ($sw4 * $sh);
803 834
804 for my $x (0 .. $sw - 1) { 835 for my $x (0 .. $sw - 1) {
836 my $row = $map->[$x + $xofs];
805 for my $y (0 .. $sh - 1) { 837 for my $y (0 .. $sh - 1) {
806 838
807 my $cell = $map->[$x + $xofs][$y + $yofs] 839 my $cell = $row->[$y + $yofs]
808 or next; 840 or next;
809 841
810 my $darkness = $cell->[0] * (1 / 255); 842 my $dark = $cell->[0];
811 if ($darkness < 0) { 843 if ($dark < 0) {
812 $darkness = $cell->[1] ? 0.1 : 0; 844 substr $darkness, $y * $sw4 + $x, 1, chr 224;
845 } else {
846 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
813 } 847 }
814 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
815 848
816 for my $num (grep $_, @$cell[1,2,3]) { 849 for my $num (grep $_, @$cell[1,2,3]) {
817 my $tex = $::CONN->{face}[$num]{texture} || next; 850 my $tex = $::CONN->{face}[$num]{texture} || next;
818 851
819 my $w = $tex->{width}; 852 my $w = $tex->{width};
820 my $h = $tex->{height}; 853 my $h = $tex->{height};
821 854
822 my $px = ($x + 1) * 32 - $w; 855 $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 } 856 }
827 } 857 }
828 } 858 }
829 859
830# if (1) { # higher quality darkness 860# if (1) { # higher quality darkness
835# 865#
836# $lighting = $pb->get_pixels; 866# $lighting = $pb->get_pixels;
837# $lighting =~ s/(.)../$1/gs; 867# $lighting =~ s/(.)../$1/gs;
838# } 868# }
839 869
840 $lighting = new Crossfire::Client::Texture 870 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
871 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
872
873 $darkness = new CFClient::Texture
841 width => $sw4, 874 width => $sw4,
842 height => $sh, 875 height => $sh,
843 data => $lighting, 876 data => $darkness,
844 internalformat => GL_ALPHA, 877 internalformat => GL_ALPHA,
845 format => GL_ALPHA; 878 format => GL_ALPHA;
846 879
847 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
848 glColor 0.45, 0.45, 0.45, 1; 880 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); 881 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
854 882
855 glDisable GL_TEXTURE_2D; 883 glDisable GL_TEXTURE_2D;
856 glDisable GL_BLEND; 884 glDisable GL_BLEND;
857} 885}
858 886
907 } 935 }
908} 936}
909 937
910############################################################################# 938#############################################################################
911 939
912package Crossfire::Client::Widget::Animator; 940package CFClient::Widget::Animator;
913 941
914use SDL::OpenGL; 942use SDL::OpenGL;
915 943
916our @ISA = Crossfire::Client::Widget::Bin::; 944our @ISA = CFClient::Widget::Bin::;
917 945
918sub moveto { 946sub moveto {
919 my ($self, $x, $y) = @_; 947 my ($self, $x, $y) = @_;
920 948
921 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 949 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
926} 954}
927 955
928sub animate { 956sub animate {
929 my ($self, $interval) = @_; 957 my ($self, $interval) = @_;
930 958
931 printf "%5.2f\n", 1 / $interval;#d#
932
933 $self->{time} -= $interval * $self->{speed}; 959 $self->{time} -= $interval * $self->{speed};
934 if ($self->{time} <= 0) { 960 if ($self->{time} <= 0) {
935 $self->{time} = 0; 961 $self->{time} = 0;
936 ::animation_stop $self; 962 ::animation_stop $self;
937 } 963 }
951 glPopMatrix; 977 glPopMatrix;
952} 978}
953 979
954############################################################################# 980#############################################################################
955 981
956package Crossfire::Client::Widget::Toplevel; 982package CFClient::Widget::Toplevel;
957 983
958our @ISA = Crossfire::Client::Widget::Container::; 984our @ISA = CFClient::Widget::Container::;
959 985
960sub size_request { 986sub size_request {
961 ($::WIDTH, $::HEIGHT) 987 ($::WIDTH, $::HEIGHT)
962} 988}
963 989
968 994
969 $_->size_allocate ($_->size_request) 995 $_->size_allocate ($_->size_request)
970 for @{$self->{children}}; 996 for @{$self->{children}};
971} 997}
972 998
999sub translate {
1000 my ($self, $x, $y) = @_;
1001
1002 ($x, $y)
1003}
1004
973sub update { 1005sub update {
974 my ($self) = @_; 1006 my ($self) = @_;
975 1007
976 $self->size_allocate ($self->size_request); 1008 $self->size_allocate ($self->size_request);
977 ::refresh (); 1009 ::refresh ();
991 $self->_draw; 1023 $self->_draw;
992} 1024}
993 1025
994############################################################################# 1026#############################################################################
995 1027
996package Crossfire::Client::Widget; 1028package CFClient::Widget;
997 1029
998$TOPLEVEL = new Crossfire::Client::Widget::Toplevel; 1030$TOPLEVEL = new CFClient::Widget::Toplevel;
999 1031
10001 10321
1001 1033

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines