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.41 by root, Sun Apr 9 21:50:21 2006 UTC vs.
Revision 1.53 by root, Mon Apr 10 11:56:28 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) = @_;
162 224
163 @{$self->{children}} = 225 @{$self->{children}} =
164 sort { $a->{z} <=> $b->{z} } 226 sort { $a->{z} <=> $b->{z} }
165 @{$self->{children}}, $chld; 227 @{$self->{children}}, $chld;
166 228
167 $self->size_allocate ($self->{w}, $self->{h}); 229 $self->size_allocate ($self->{w}, $self->{h})
230 if $self->{w}; #TODO: check for "realised state"
168} 231}
169 232
170sub remove { 233sub remove {
171 my ($self, $widget) = @_; 234 my ($self, $widget) = @_;
172 235
176} 239}
177 240
178sub find_widget { 241sub find_widget {
179 my ($self, $x, $y) = @_; 242 my ($self, $x, $y) = @_;
180 243
244 $x -= $self->{x};
245 $y -= $self->{y};
246
181 my $res; 247 my $res;
182 248
183 for (@{ $self->{children} }) { 249 for (reverse @{ $self->{children} }) {
184 $res = $_->find_widget ($x, $y) 250 $res = $_->find_widget ($x, $y)
185 and return $res; 251 and return $res;
186 } 252 }
187 253
188 () 254 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
189}
190
191sub size_request {
192 my ($self) = @_;
193
194 my ($hs, $ws) = (0, 0);
195 for (@{$self->{children} || []}) {
196 my ($w, $h) = $_->size_request;
197 $hs += $h;
198 if ($ws < $w) { $ws = $w }
199 }
200
201 return ($ws, $hs);
202} 255}
203 256
204sub _draw { 257sub _draw {
205 my ($self) = @_; 258 my ($self) = @_;
206 259
219 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 272 $_[0]{children}[0]->size_request if $_[0]{children}[0];
220} 273}
221 274
222sub size_allocate { 275sub size_allocate {
223 my ($self, $w, $h) = @_; 276 my ($self, $w, $h) = @_;
277
224 $self->SUPER::size_allocate ($w, $h); 278 $self->SUPER::size_allocate ($w, $h);
225 $self->{children}[0]->size_allocate ($w, $h) 279 $self->{children}[0]->size_allocate ($w, $h)
226 if $self->{children}[0] 280 if $self->{children}[0]
227} 281}
228 282
229############################################################################# 283#############################################################################
230 284
231package Crossfire::Client::Widget::Toplevel; 285package Crossfire::Client::Widget::Window;
232 286
233our @ISA = Crossfire::Client::Widget::Container::; 287our @ISA = Crossfire::Client::Widget::Bin::;
288
289use SDL::OpenGL;
290
291sub new {
292 my ($class, $x, $y, $z, $w, $h) = @_;
293
294 my $self = $class->SUPER::new;
295
296 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
297}
234 298
235sub update { 299sub update {
236 my ($self) = @_; 300 my ($self) = @_;
237 301
238 ::refresh ();
239}
240
241#############################################################################
242
243package Crossfire::Client::Widget::Window;
244
245our @ISA = Crossfire::Client::Widget::Bin::;
246
247use SDL::OpenGL;
248
249sub add {
250 my ($self, $chld) = @_;
251 warn "ADD $chld\n";
252 $self->SUPER::add ($chld);
253 $chld->set_parent ($self);
254}
255
256sub remove {
257 my ($self) = @_;
258 # TODO FIXME: removing a child from a window will crash, see render_chld
259 # $self->update;
260}
261
262sub update {
263 my ($self) = @_;
264 $self->render_chld; 302 $self->render_chld;
303 $self->SUPER::update;
265} 304}
266 305
267sub render_chld { 306sub render_chld {
268 my ($self) = @_; 307 my ($self) = @_;
269 my $chld = $self->get;
270 my ($w, $h) = $self->size_request;
271 308
272 require Carp;
273 Carp::cluck "RENDERCHI $w $h";
274 warn "RENDERCHI $w $h\n";
275 $self->{texture} = 309 $self->{texture} =
276 Crossfire::Client::Texture->new_from_opengl ( 310 Crossfire::Client::Texture->new_from_opengl (
277 $w, $h, sub { $chld->draw } 311 $self->{w}, $self->{h}, sub { $self->child->draw }
278 ); 312 );
279 $self->{texture}->upload;
280}
281
282sub size_request {
283 my ($self) = @_;
284 ($self->w, $self->h)
285} 313}
286 314
287sub size_allocate { 315sub size_allocate {
288 my ($self, $w, $h) = @_; 316 my ($self, $w, $h) = @_;
289 317
290 $self->w ($w); 318 $self->{w} = $w;
291 $self->h ($h); 319 $self->{h} = $h;
320
292 $self->get->size_allocate ($w, $h); 321 $self->child->size_allocate ($w, $h);
293 322
294 $self->update; #TODO: Move this to the size_request event propably? 323 $self->render_chld;
295} 324}
296 325
297sub _draw { 326sub _draw {
298 my ($self) = @_; 327 my ($self) = @_;
299 328
336 map { $_ + 4 } $chld->size_request; 365 map { $_ + 4 } $chld->size_request;
337} 366}
338 367
339sub size_allocate { 368sub size_allocate {
340 my ($self, $w, $h) = @_; 369 my ($self, $w, $h) = @_;
341 370
342 $self->SUPER::size_allocate ($w, $h); 371 $self->{w} = $w;
372 $self->{h} = $h;
343 373
344 $self->child->size_allocate ($w - 4, $h - 4); 374 $self->child->size_allocate ($w - 4, $h - 4);
345 $self->child->move (2, 2); 375 $self->child->move (2, 2);
346} 376}
347 377
398 $w -= $tex[2]->{width}; 428 $w -= $tex[2]->{width};
399 $w -= $tex[3]->{width}; 429 $w -= $tex[3]->{width};
400 430
401 $h = $h < 0 ? 0 : $h; 431 $h = $h < 0 ? 0 : $h;
402 $w = $w < 0 ? 0 : $w; 432 $w = $w < 0 ? 0 : $w;
403 warn "CHILD:$w $h\n"; 433
404 $self->child->size_allocate ($w, $h); 434 $self->child->size_allocate ($w, $h);
405 $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 435 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
406} 436}
407 437
408sub _draw { 438sub _draw {
409 my ($self) = @_; 439 my ($self) = @_;
410 440
411 my ($w, $h) = ($self->w, $self->h); 441 my ($w, $h) = ($self->{w}, $self->{h});
412 my ($cw, $ch) = ($self->child->w, $self->child->h); 442 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
413 443
414 glEnable GL_BLEND; 444 glEnable GL_BLEND;
415 glEnable GL_TEXTURE_2D; 445 glEnable GL_TEXTURE_2D;
416 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 446 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
417 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 447 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
571package Crossfire::Client::Widget::VBox; 601package Crossfire::Client::Widget::VBox;
572 602
573our @ISA = Crossfire::Client::Widget::Container::; 603our @ISA = Crossfire::Client::Widget::Container::;
574 604
575use SDL::OpenGL; 605use SDL::OpenGL;
606
607sub size_request {
608 my ($self) = @_;
609
610 my @alloc = map [$_->size_request], @{$self->{children}};
611
612 (
613 (List::Util::max map $_->[0], @alloc),
614 (List::Util::sum map $_->[1], @alloc),
615 )
616}
576 617
577sub size_allocate { 618sub size_allocate {
578 my ($self, $w, $h) = @_; 619 my ($self, $w, $h) = @_;
579 620
580 $self->w ($w); 621 $self->w ($w);
613 $y += $h; 654 $y += $h;
614 } 655 }
615 } 656 }
616} 657}
617 658
618sub _draw {
619 my ($self) = @_;
620
621 my ($x, $y);
622 for (@{$self->{children} || []}) {
623 $_->draw;
624 $y += $_->h;
625 }
626}
627
628############################################################################# 659#############################################################################
629 660
630package Crossfire::Client::Widget::Label; 661package Crossfire::Client::Widget::Label;
631 662
632our @ISA = Crossfire::Client::Widget::; 663our @ISA = Crossfire::Client::Widget::;
634use SDL::OpenGL; 665use SDL::OpenGL;
635 666
636sub new { 667sub new {
637 my ($class, $x, $y, $z, $height, $text) = @_; 668 my ($class, $x, $y, $z, $height, $text) = @_;
638 669
670 $height ||= $::FONTSIZE;
671
639 # TODO: color, and make height, xyz etc. optional 672 # TODO: color, and make height, xyz etc. optional
640 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);
641 674
642 $self->set_text ($text); 675 $self->set_text ($text);
643 676
660} 693}
661 694
662sub size_request { 695sub size_request {
663 my ($self) = @_; 696 my ($self) = @_;
664 697
665 ( 698 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
699 (
666 $self->{texture}{width}, 700 $self->{texture}{width},
667 $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)
668 ) 707 }
708
669} 709}
670 710
671sub _draw { 711sub _draw {
672 my ($self) = @_; 712 my ($self) = @_;
673 713
692 glDisable GL_TEXTURE_2D; 732 glDisable GL_TEXTURE_2D;
693} 733}
694 734
695############################################################################# 735#############################################################################
696 736
697package Crossfire::Client::Widget::TextEntry; 737package Crossfire::Client::Widget::Entry;
698 738
699our @ISA = Crossfire::Client::Widget::Label::; 739our @ISA = Crossfire::Client::Widget::Label::;
700 740
701use SDL; 741use SDL;
702use SDL::OpenGL; 742use SDL::OpenGL;
705 my ($self, $ev) = @_; 745 my ($self, $ev) = @_;
706 746
707 my $mod = $ev->key_mod; 747 my $mod = $ev->key_mod;
708 my $sym = $ev->key_sym; 748 my $sym = $ev->key_sym;
709 749
710 $ev->set_unicode (1);
711 my $uni = $ev->key_unicode; 750 my $uni = $ev->key_unicode;
712 751
713 my $text = $self->get_text; 752 my $text = $self->get_text;
714 753
715 if ($sym == SDLK_BACKSPACE) { 754 if ($sym == SDLK_BACKSPACE) {
716 substr $text, -1, 1, ''; 755 substr $text, -1, 1, '';
717
718 } elsif ($uni) { 756 } elsif ($uni) {
719 $text .= chr $uni; 757 $text .= chr $uni;
758 print "$uni <$text>\n";#d#
720 } 759 }
760
721 $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;
722} 787}
723 788
724############################################################################# 789#############################################################################
725 790
726package Crossfire::Client::Widget::MapWidget; 791package Crossfire::Client::Widget::MapWidget;
741 806
742sub key_up { 807sub key_up {
743} 808}
744 809
745sub size_request { 810sub size_request {
746 811 (
747} 812 1 + int $::WIDTH / 32,
748 813 1 + int $::HEIGHT / 32,
749sub size_allocate { 814 )
750} 815}
751 816
752sub _draw { 817sub _draw {
753 my ($self) = @_; 818 my ($self) = @_;
754 819
787 my $cell = $map->[$x + $xofs][$y + $yofs] 852 my $cell = $map->[$x + $xofs][$y + $yofs]
788 or next; 853 or next;
789 854
790 my $darkness = $cell->[0] * (1 / 255); 855 my $darkness = $cell->[0] * (1 / 255);
791 if ($darkness < 0) { 856 if ($darkness < 0) {
792 $darkness = 0.15; 857 $darkness = $cell->[1] ? 0.2 : 0;
793 } 858 }
794 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; 859 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
795 860
796 for my $num (grep $_, @$cell[1,2,3]) { 861 for my $num (grep $_, @$cell[1,2,3]) {
797 my $tex = $::CONN->{face}[$num]{texture} || next; 862 my $tex = $::CONN->{face}[$num]{texture} || next;
826 891
827 $lighting = new Crossfire::Client::Texture 892 $lighting = new Crossfire::Client::Texture
828 width => $sw4, 893 width => $sw4,
829 height => $sh, 894 height => $sh,
830 data => $lighting, 895 data => $lighting,
831 internalformat => GL_ALPHA4, 896 internalformat => GL_ALPHA,
832 format => GL_ALPHA; 897 format => GL_ALPHA;
833 898
834 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 899 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
835 glColor 0, 0, 0, 0.75; 900 glColor 0.5, 0.5, 0.5, 1;
836 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 901 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
837 glBindTexture GL_TEXTURE_2D, $lighting->{name}; 902 glBindTexture GL_TEXTURE_2D, $lighting->{name};
838 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 903 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
839 glBegin GL_QUADS; 904 glBegin GL_QUADS;
840 glTexCoord 0, 0; glVertex 0 , 0; 905 glTexCoord 0, 0; glVertex 0 , 0;
908 973
909sub moveto { 974sub moveto {
910 my ($self, $x, $y) = @_; 975 my ($self, $x, $y) = @_;
911 976
912 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 977 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
913 $self->{speed} = 0.2; 978 $self->{speed} = 2;
914 $self->{time} = 1; 979 $self->{time} = 1;
915 980
916 ::animation_start $self; 981 ::animation_start $self;
917} 982}
918 983
933 998
934sub _draw { 999sub _draw {
935 my ($self) = @_; 1000 my ($self) = @_;
936 1001
937 glPushMatrix; 1002 glPushMatrix;
938 glRotate $self->{time} * 10000, 0, 1, 0; 1003 glRotate $self->{time} * 1000, 0, 1, 0;
939 $self->{children}[0]->draw; 1004 $self->{children}[0]->draw;
940 glPopMatrix; 1005 glPopMatrix;
941} 1006}
942 1007
9431; 1008#############################################################################
944 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