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.43 by root, Sun Apr 9 22:12:11 2006 UTC vs.
Revision 1.55 by root, Mon Apr 10 19:34:04 2006 UTC

7use SDL::OpenGL; 7use SDL::OpenGL;
8use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
9 9
10use Crossfire::Client; 10use Crossfire::Client;
11 11
12our $FOCUS; # the widget with current focus 12our ($FOCUS, $HOVER, $GRAB); # various widgets
13
14our $TOPLEVEL;
15our $BUTTON_STATE;
13 16
14# class methods for events 17# class methods for events
15sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 18sub feed_sdl_key_down_event {
16sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 19 $FOCUS->key_down ($_[0]) if $FOCUS;
20}
21
22sub feed_sdl_key_up_event {
23 $FOCUS->key_up ($_[0]) if $FOCUS;
24}
25
17sub feed_sdl_button_down_event { } 26sub feed_sdl_button_down_event {
27 my ($ev) = @_;
28 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
29
30 if (!$BUTTON_STATE) {
31 my $widget = $TOPLEVEL->find_widget ($x, $y);
32
33 $GRAB = $widget;
34 $GRAB->update if $GRAB;
35 }
36
37 $BUTTON_STATE |= 1 << ($ev->button - 1);
38
39 $GRAB->button_down ($ev) if $GRAB;
40}
41
18sub feed_sdl_button_up_event { } 42sub feed_sdl_button_up_event {
43 my ($ev) = @_;
44 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
45
46 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
47
48 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
49
50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab;
53 $GRAB->update if $GRAB;
54 }
55}
56
57sub feed_sdl_motion_event {
58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
60
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
62
63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget;
65
66 $hover->update if $hover;
67 $HOVER->update if $HOVER;
68 }
69
70 $HOVER->mouse_motion ($ev) if $HOVER;
71}
19 72
20sub new { 73sub new {
21 my $class = shift; 74 my $class = shift;
22 75
23 bless { @_ }, $class 76 bless { @_ }, $class
45 $self->{w} = $w; 98 $self->{w} = $w;
46 $self->{h} = $h; 99 $self->{h} = $h;
47} 100}
48 101
49sub focus_in { 102sub focus_in {
50 my ($widget) = @_; 103 my ($self) = @_;
51 $FOCUS = $widget; 104
105 my $focus = $FOCUS; $FOCUS = $self;
106 $focus->update if $focus;
107 $FOCUS->update;
52} 108}
53 109
54sub focus_out { 110sub focus_out {
55 my ($widget) = @_; 111 my ($self) = @_;
56}
57 112
58sub key_down { 113 return unless $FOCUS == $self;
59 my ($widget, $sdlev) = @_;
60}
61 114
62sub key_up { 115 my $focus = $FOCUS; undef $FOCUS;
63 my ($widget, $sdlev) = @_; 116 $focus->update if $focus; #?
64} 117}
65 118
119sub mouse_motion { }
120sub button_up { }
66sub button_down { 121sub button_down { }
67 my ($widget, $sdlev) = @_; 122sub key_down { }
68} 123sub key_up { }
69 124
70sub button_up {
71 my ($widget, $sdlev) = @_;
72}
73
74sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 125sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
75sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 126sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
76sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 127sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
77sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 128sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
78sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 129sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
79 130
80sub draw { 131sub draw {
81 my ($self) = @_; 132 my ($self) = @_;
82 133
83 glPushMatrix; 134 glPushMatrix;
84 glTranslate $self->{x}, $self->{y}, 0; 135 glTranslate $self->{x}, $self->{y}, 0;
85 $self->_draw; 136 $self->_draw;
137 if ($self == $HOVER) {
138 glColor 1, 1, 1, 0.4;
139 glEnable GL_BLEND;
140 glBegin GL_QUADS;
141 glVertex 0 , 0;
142 glVertex $self->{w}, 0;
143 glVertex $self->{w}, $self->{h};
144 glVertex 0 , $self->{h};
145 glEnd;
146 glDisable GL_BLEND;
147 }
86 glPopMatrix; 148 glPopMatrix;
87} 149}
88 150
89sub _draw { 151sub _draw {
90 my ($self) = @_; 152 my ($self) = @_;
177} 239}
178 240
179sub find_widget { 241sub find_widget {
180 my ($self, $x, $y) = @_; 242 my ($self, $x, $y) = @_;
181 243
244 $x -= $self->{x};
245 $y -= $self->{y};
246
182 my $res; 247 my $res;
183 248
184 for (@{ $self->{children} }) { 249 for (reverse @{ $self->{children} }) {
185 $res = $_->find_widget ($x, $y) 250 $res = $_->find_widget ($x, $y)
186 and return $res; 251 and return $res;
187 } 252 }
188 253
189 () 254 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
190} 255}
191 256
192sub _draw { 257sub _draw {
193 my ($self) = @_; 258 my ($self) = @_;
194 259
211 my ($self, $w, $h) = @_; 276 my ($self, $w, $h) = @_;
212 277
213 $self->SUPER::size_allocate ($w, $h); 278 $self->SUPER::size_allocate ($w, $h);
214 $self->{children}[0]->size_allocate ($w, $h) 279 $self->{children}[0]->size_allocate ($w, $h)
215 if $self->{children}[0] 280 if $self->{children}[0]
216}
217
218#############################################################################
219
220package Crossfire::Client::Widget::Toplevel;
221
222our @ISA = Crossfire::Client::Widget::Container::;
223
224sub update {
225 my ($self) = @_;
226
227 ::refresh ();
228}
229
230sub add {
231 my ($self, $widget) = @_;
232
233 $self->SUPER::add ($widget);
234
235 $widget->size_allocate ($widget->size_request);
236} 281}
237 282
238############################################################################# 283#############################################################################
239 284
240package Crossfire::Client::Widget::Window; 285package Crossfire::Client::Widget::Window;
620use SDL::OpenGL; 665use SDL::OpenGL;
621 666
622sub new { 667sub new {
623 my ($class, $x, $y, $z, $height, $text) = @_; 668 my ($class, $x, $y, $z, $height, $text) = @_;
624 669
670 $height ||= $::FONTSIZE;
671
625 # TODO: color, and make height, xyz etc. optional 672 # TODO: color, and make height, xyz etc. optional
626 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 673 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
627 674
628 $self->set_text ($text); 675 $self->set_text ($text);
629 676
646} 693}
647 694
648sub size_request { 695sub size_request {
649 my ($self) = @_; 696 my ($self) = @_;
650 697
651 ( 698 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
699 (
652 $self->{texture}{width}, 700 $self->{texture}{width},
653 $self->{texture}{height}, 701 $self->{texture}{height},
702 )
703 } else {
704 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height};
705
706 ($w, $h)
654 ) 707 }
708
655} 709}
656 710
657sub _draw { 711sub _draw {
658 my ($self) = @_; 712 my ($self) = @_;
659 713
678 glDisable GL_TEXTURE_2D; 732 glDisable GL_TEXTURE_2D;
679} 733}
680 734
681############################################################################# 735#############################################################################
682 736
683package Crossfire::Client::Widget::TextEntry; 737package Crossfire::Client::Widget::Entry;
684 738
685our @ISA = Crossfire::Client::Widget::Label::; 739our @ISA = Crossfire::Client::Widget::Label::;
686 740
687use SDL; 741use SDL;
688use SDL::OpenGL; 742use SDL::OpenGL;
691 my ($self, $ev) = @_; 745 my ($self, $ev) = @_;
692 746
693 my $mod = $ev->key_mod; 747 my $mod = $ev->key_mod;
694 my $sym = $ev->key_sym; 748 my $sym = $ev->key_sym;
695 749
696 $ev->set_unicode (1);
697 my $uni = $ev->key_unicode; 750 my $uni = $ev->key_unicode;
698 751
699 my $text = $self->get_text; 752 my $text = $self->get_text;
700 753
701 if ($sym == SDLK_BACKSPACE) { 754 if ($sym == SDLK_BACKSPACE) {
702 substr $text, -1, 1, ''; 755 substr $text, -1, 1, '';
703
704 } elsif ($uni) { 756 } elsif ($uni) {
705 $text .= chr $uni; 757 $text .= chr $uni;
758 print "$uni <$text>\n";#d#
706 } 759 }
760
707 $self->set_text ($text); 761 $self->set_text ($text);
762}
763
764sub button_down {
765 my ($self, $ev) = @_;
766
767 $self->focus_in;
768}
769
770sub _draw {
771 my ($self) = @_;
772
773 if ($FOCUS == $self) {
774 glColor 1, 1, 1;
775 } else {
776 glColor 0.7, 0.7, 0.7;
777 }
778
779 glBegin GL_QUADS;
780 glVertex 0 , 0;
781 glVertex 0 , $self->{h} - 1;
782 glVertex $self->{w} - 1, $self->{h} - 1;
783 glVertex $self->{w} - 1, 0;
784 glEnd;
785
786 $self->SUPER::_draw;
708} 787}
709 788
710############################################################################# 789#############################################################################
711 790
712package Crossfire::Client::Widget::MapWidget; 791package Crossfire::Client::Widget::MapWidget;
727 806
728sub key_up { 807sub key_up {
729} 808}
730 809
731sub size_request { 810sub size_request {
732 811 (
733} 812 1 + int $::WIDTH / 32,
734 813 1 + int $::HEIGHT / 32,
735sub size_allocate { 814 )
736} 815}
737 816
738sub _draw { 817sub _draw {
739 my ($self) = @_; 818 my ($self) = @_;
740 819
773 my $cell = $map->[$x + $xofs][$y + $yofs] 852 my $cell = $map->[$x + $xofs][$y + $yofs]
774 or next; 853 or next;
775 854
776 my $darkness = $cell->[0] * (1 / 255); 855 my $darkness = $cell->[0] * (1 / 255);
777 if ($darkness < 0) { 856 if ($darkness < 0) {
778 $darkness = 0.15; 857 $darkness = $cell->[1] ? 0.1 : 0;
779 } 858 }
780 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; 859 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
781 860
782 for my $num (grep $_, @$cell[1,2,3]) { 861 for my $num (grep $_, @$cell[1,2,3]) {
783 my $tex = $::CONN->{face}[$num]{texture} || next; 862 my $tex = $::CONN->{face}[$num]{texture} || next;
812 891
813 $lighting = new Crossfire::Client::Texture 892 $lighting = new Crossfire::Client::Texture
814 width => $sw4, 893 width => $sw4,
815 height => $sh, 894 height => $sh,
816 data => $lighting, 895 data => $lighting,
817 internalformat => GL_ALPHA4, 896 internalformat => GL_ALPHA,
818 format => GL_ALPHA; 897 format => GL_ALPHA;
819 898
820 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 899 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
821 glColor 0, 0, 0, 0.75; 900 glColor 0.45, 0.45, 0.45, 1;
822 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 901 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
823 glBindTexture GL_TEXTURE_2D, $lighting->{name}; 902 glBindTexture GL_TEXTURE_2D, $lighting->{name};
824 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 903 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
825 glBegin GL_QUADS; 904 glBegin GL_QUADS;
826 glTexCoord 0, 0; glVertex 0 , 0; 905 glTexCoord 0, 0; glVertex 0 , 0;
894 973
895sub moveto { 974sub moveto {
896 my ($self, $x, $y) = @_; 975 my ($self, $x, $y) = @_;
897 976
898 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 977 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
899 $self->{speed} = 0.2; 978 $self->{speed} = 2;
900 $self->{time} = 1; 979 $self->{time} = 1;
901 980
902 ::animation_start $self; 981 ::animation_start $self;
903} 982}
904 983
919 998
920sub _draw { 999sub _draw {
921 my ($self) = @_; 1000 my ($self) = @_;
922 1001
923 glPushMatrix; 1002 glPushMatrix;
924 glRotate $self->{time} * 10000, 0, 1, 0; 1003 glRotate $self->{time} * 1000, 0, 1, 0;
925 $self->{children}[0]->draw; 1004 $self->{children}[0]->draw;
926 glPopMatrix; 1005 glPopMatrix;
927} 1006}
928 1007
9291; 1008#############################################################################
930 1009
1010package Crossfire::Client::Widget::Toplevel;
1011
1012our @ISA = Crossfire::Client::Widget::Container::;
1013
1014sub size_request {
1015 ($::WIDTH, $::HEIGHT)
1016}
1017
1018sub size_allocate {
1019 my ($self, $w, $h) = @_;
1020
1021 $self->SUPER::size_allocate ($w, $h);
1022
1023 $_->size_allocate ($_->size_request)
1024 for @{$self->{children}};
1025}
1026
1027sub update {
1028 my ($self) = @_;
1029
1030 $self->size_allocate ($self->size_request);
1031 ::refresh ();
1032}
1033
1034sub add {
1035 my ($self, $widget) = @_;
1036
1037 $self->SUPER::add ($widget);
1038
1039 $widget->size_allocate ($widget->size_request);
1040}
1041
1042sub draw {
1043 my ($self) = @_;
1044
1045 $self->_draw;
1046}
1047
1048#############################################################################
1049
1050package Crossfire::Client::Widget;
1051
1052$TOPLEVEL = new Crossfire::Client::Widget::Toplevel;
1053
10541
1055

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines