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.12 by root, Fri Apr 7 20:55:32 2006 UTC vs.
Revision 1.16 by root, Fri Apr 7 23:16:29 2006 UTC

3use strict; 3use strict;
4use SDL::OpenGL; 4use SDL::OpenGL;
5use SDL::OpenGL::Constants; 5use SDL::OpenGL::Constants;
6 6
7our $FOCUS; # the widget with current focus 7our $FOCUS; # the widget with current focus
8our %ACTIVE_WIDGETS; 8our @ACTIVE_WIDGETS;
9 9
10# class methods for events 10# class methods for events
11sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 11sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
12sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 12sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS }
13sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS } 13sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS }
18 18
19 bless { @_ }, $class 19 bless { @_ }, $class
20} 20}
21 21
22sub activate { 22sub activate {
23 $ACTIVE_WIDGETS{$_[0]} = $_[0]; 23 push @ACTIVE_WIDGETS, $_[0];
24} 24}
25 25
26sub deactivate { 26sub deactivate {
27 delete $ACTIVE_WIDGETS{$_[0]}; 27 @ACTIVE_WIDGETS =
28 sort { $a->{z} <=> $b->{z} }
29 grep { $_ != $_[0] }
30 @ACTIVE_WIDGETS;
31}
32
33sub size_request {
34 die "size_request is abtract";
28} 35}
29 36
30sub focus_in { 37sub focus_in {
31 my ($widget) = @_; 38 my ($widget) = @_;
32 $FOCUS = $widget; 39 $FOCUS = $widget;
50 57
51sub button_up { 58sub button_up {
52 my ($widget, $sdlev) = @_; 59 my ($widget, $sdlev) = @_;
53} 60}
54 61
62sub w { $_[0]->{w} }
63sub h { $_[0]->{h} }
55sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 64sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} }
56sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 65sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} }
66sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} }
57 67
58sub draw { 68sub draw {
59 my ($self) = @_; 69 my ($self) = @_;
60 70
61 glPushMatrix; 71 glPushMatrix;
70 80
71sub bbox { 81sub bbox {
72 my ($widget) = @_; 82 my ($widget) = @_;
73} 83}
74 84
85package Crossfire::Client::Widget::Container;
86
87our @ISA = Crossfire::Client::Widget::;
88
89use SDL::OpenGL;
90
91sub add { $_[0]->{child} = $_[1] }
92sub get { $_[0]->{child} }
93
94sub size_request { $_[0]->{child}->size_request if $_[0]->{child} }
95
96sub _draw { die "Containers can't be drawn!" }
97
98package Crossfire::Client::Widget::Frame;
99
100our @ISA = Crossfire::Client::Widget::Container::;
101
102use SDL::OpenGL;
103
104sub size_request {
105 my ($self) = @_;
106 my $chld = $self->get
107 or return (0, 0);
108 map { $_ + 4 } $chld->size_request;
109}
110
111sub _draw {
112 my ($self) = @_;
113
114 my $chld = $self->get;
115
116 my ($w, $h) = $chld->size_request;
117
118 glColor 1, 0, 0;
119 glBegin GL_QUADS;
120 glTexCoord 0, 0; glVertex 0 , 0;
121 glTexCoord 0, 1; glVertex 0 , $h + 4;
122 glTexCoord 1, 1; glVertex $w + 4 , $h + 4;
123 glTexCoord 1, 0; glVertex $w + 4 , 0;
124 glEnd;
125
126 glPushMatrix;
127 glTranslate (2, 2, 0);
128 $chld->_draw;
129 glPopMatrix;
130}
131
132package Crossfire::Client::Widget::Table;
133
134our @ISA = Crossfire::Client::Widget::Container::;
135
136use SDL::OpenGL;
137
138sub add {
139 my ($self, $x, $y, $chld) = @_;
140 $self->{childs}[$y][$x] = $chld;
141}
142
143sub max_row_height {
144 my ($self, $row) = @_;
145
146 my $hs = 0;
147 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) {
148 my $c = $self->{childs}->[$row]->[$xi];
149 my ($w, $h) = $c->size_request if $c;
150 if ($hs < $h) { $hs = $h }
151 }
152 return $hs;
153}
154
155sub max_col_width {
156 my ($self, $col) = @_;
157
158 my $ws = 0;
159 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) {
160 my $c = ($self->{childs}->[$yi] || [])->[$col];
161 my ($w, $h) = $c->size_request if $c;
162 if ($ws < $w) { $ws = $w }
163 }
164 return $ws;
165}
166
167sub size_request {
168 my ($self) = @_;
169
170 my ($hs, $ws) = (0, 0);
171
172 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
173 $hs += $self->max_row_height ($yi);
174 }
175
176 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
177 my $wm = 0;
178 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
179 $wm += $self->max_col_width ($xi)
180 }
181 if ($ws < $wm) { $ws = $wm }
182 }
183
184 return ($ws, $hs);
185}
186
187sub _draw {
188 my ($self) = @_;
189
190 my $y = 0;
191 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
192 my $x = 0;
193
194 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
195
196 glPushMatrix;
197 glTranslate ($x, $y, 0);
198 my $c = $self->{childs}->[$yi]->[$xi];
199 $c->_draw if $c;
200 glPopMatrix;
201
202 $x += $self->max_col_width ($xi);
203 }
204
205 $y += $self->max_row_height ($yi);
206 }
207}
208
209package Crossfire::Client::Widget::VBox;
210
211our @ISA = Crossfire::Client::Widget::Container::;
212
213use SDL::OpenGL;
214
215sub add {
216 my ($self, $chld) = @_;
217 push @{$self->{childs}}, $chld;
218}
219
220sub size_request {
221 my ($self) = @_;
222
223 my ($hs, $ws) = (0, 0);
224 for (@{$self->{childs} || []}) {
225 my ($w, $h) = $_->size_request;
226 $hs += $h;
227 if ($ws < $w) { $ws = $w }
228 }
229
230 return ($ws, $hs);
231}
232
233sub _draw {
234 my ($self) = @_;
235
236 my ($x, $y);
237 for (@{$self->{childs} || []}) {
238 glPushMatrix;
239 glTranslate (0, $y, 0);
240 $_->_draw;
241 glPopMatrix;
242 my ($w, $h) = $_->size_request;
243 $y += $h;
244 }
245}
246
75package Crossfire::Client::Widget::Label; 247package Crossfire::Client::Widget::Label;
76 248
77our @ISA = Crossfire::Client::Widget::; 249our @ISA = Crossfire::Client::Widget::;
78 250
79use SDL::OpenGL; 251use SDL::OpenGL;
80 252
81sub new { 253sub new {
82 my ($class, $x, $y, $z, $ttf, $text) = @_; 254 my ($class, $x, $y, $z, $ttf, $text) = @_;
83 255
84 my $self = $class->SUPER::new (x => $x, y => $y, z => $z); 256 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf);
85 257
86 $self->{texture} = new_from_ttf Crossfire::Client::Texture $ttf, $text; 258 $self->set_text ($text);
87 259
88 $self 260 $self
261}
262
263sub set_text {
264 my ($self, $text) = @_;
265 $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text;
266}
267
268sub get_text {
269 my ($self, $text) = @_;
270 $self->{text}
271}
272
273sub size_request {
274 my ($self) = @_;
275
276 (
277 $self->{texture}{width},
278 $self->{texture}{height},
279 )
89} 280}
90 281
91sub _draw { 282sub _draw {
92 my ($self) = @_; 283 my ($self) = @_;
93 284
136 327
137use strict; 328use strict;
138 329
139our @ISA = qw/Crossfire::Client::Widget/; 330our @ISA = qw/Crossfire::Client::Widget/;
140 331
332use SDL;
141use SDL::OpenGL; 333use SDL::OpenGL;
142use SDL::OpenGL::Constants; 334use SDL::OpenGL::Constants;
143 335
144sub key_down { 336sub key_down {
145 print "MAPKEYDOWN\n"; 337 print "MAPKEYDOWN\n";
181 373
182 glDisable GL_TEXTURE_2D; 374 glDisable GL_TEXTURE_2D;
183 glDisable GL_BLEND; 375 glDisable GL_BLEND;
184} 376}
185 377
378my %DIR = (
379 SDLK_KP8, [1, "north"],
380 SDLK_KP9, [2, "northest"],
381 SDLK_KP6, [3, "east"],
382 SDLK_KP3, [4, "southeast"],
383 SDLK_KP2, [5, "south"],
384 SDLK_KP1, [6, "southwest"],
385 SDLK_KP4, [7, "west"],
386 SDLK_KP7, [8, "northwest"],
387);
388
389sub key_down {
390 my ($self, $ev) = @_;
391
392 my $mod = $ev->key_mod;
393 my $sym = $ev->key_sym;
394
395 if ($sym == SDLK_KP5) {
396 $::CONN->send ("command stay fire");
397 } elsif (exists $DIR{$sym}) {
398 if ($mod & KMOD_SHIFT) {
399 $::CONN->send ("command fire $DIR{$sym}[0]");
400 } elsif ($mod & KMOD_CTRL) {
401 $::CONN->send ("command run $DIR{$sym}[0]");
402 } else {
403 }
404 }
405}
406
407sub key_up {
408 my ($self, $ev) = @_;
409
410 my $mod = $ev->key_mod;
411 my $sym = $ev->key_sym;
412
413 if (exists $DIR{$sym}) {
414 if ($mod & KMOD_SHIFT) {
415 $::CONN->send ("command fire_stop");
416 } else {
417 $::CONN->send ("command run_stop");
418 }
419 }
420}
421
1861; 4221;
187 423

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines