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.49 by root, Sun Apr 9 22:26:51 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
13
14# class methods for events 12# class methods for events
15sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 13sub feed_sdl_key_down_event { $::FOCUS->key_down ($_[0]) if $::FOCUS }
16sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 14sub feed_sdl_key_up_event { $::FOCUS->key_up ($_[0]) if $::FOCUS }
17sub feed_sdl_button_down_event { } 15sub feed_sdl_button_down_event { }
18sub feed_sdl_button_up_event { } 16sub feed_sdl_button_up_event { }
19 17
20sub new { 18sub new {
21 my $class = shift; 19 my $class = shift;
46 $self->{h} = $h; 44 $self->{h} = $h;
47} 45}
48 46
49sub focus_in { 47sub focus_in {
50 my ($widget) = @_; 48 my ($widget) = @_;
51 $FOCUS = $widget; 49 $::FOCUS = $widget;
52} 50}
53 51
54sub focus_out { 52sub focus_out {
55 my ($widget) = @_; 53 my ($widget) = @_;
56} 54}
81 my ($self) = @_; 79 my ($self) = @_;
82 80
83 glPushMatrix; 81 glPushMatrix;
84 glTranslate $self->{x}, $self->{y}, 0; 82 glTranslate $self->{x}, $self->{y}, 0;
85 $self->_draw; 83 $self->_draw;
84 if ($self == $::HOVER) {
85 glColor 1, 1, 1, 0.4;
86 glBegin GL_QUADS;
87 glVertex 0, 0;
88 glVertex $self->{w} - 1, 0;
89 glVertex $self->{w} - 1, $self->{h} - 1;
90 glVertex 0, $self->{h} - 1;
91 glEnd;
92 }
86 glPopMatrix; 93 glPopMatrix;
87} 94}
88 95
89sub _draw { 96sub _draw {
90 my ($self) = @_; 97 my ($self) = @_;
162 169
163 @{$self->{children}} = 170 @{$self->{children}} =
164 sort { $a->{z} <=> $b->{z} } 171 sort { $a->{z} <=> $b->{z} }
165 @{$self->{children}}, $chld; 172 @{$self->{children}}, $chld;
166 173
167 $self->size_allocate ($self->{w}, $self->{h}); 174 $self->size_allocate ($self->{w}, $self->{h})
175 if $self->{w}; #TODO: check for "realised state"
168} 176}
169 177
170sub remove { 178sub remove {
171 my ($self, $widget) = @_; 179 my ($self, $widget) = @_;
172 180
176} 184}
177 185
178sub find_widget { 186sub find_widget {
179 my ($self, $x, $y) = @_; 187 my ($self, $x, $y) = @_;
180 188
189 $x -= $self->{x};
190 $y -= $self->{y};
191
181 my $res; 192 my $res;
182 193
183 for (@{ $self->{children} }) { 194 for (reverse @{ $self->{children} }) {
184 $res = $_->find_widget ($x, $y) 195 $res = $_->find_widget ($x, $y)
185 and return $res; 196 and return $res;
186 } 197 }
187 198
188 () 199 $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} 200}
203 201
204sub _draw { 202sub _draw {
205 my ($self) = @_; 203 my ($self) = @_;
206 204
219 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 217 $_[0]{children}[0]->size_request if $_[0]{children}[0];
220} 218}
221 219
222sub size_allocate { 220sub size_allocate {
223 my ($self, $w, $h) = @_; 221 my ($self, $w, $h) = @_;
222
224 $self->SUPER::size_allocate ($w, $h); 223 $self->SUPER::size_allocate ($w, $h);
225 $self->{children}[0]->size_allocate ($w, $h) 224 $self->{children}[0]->size_allocate ($w, $h)
226 if $self->{children}[0] 225 if $self->{children}[0]
227} 226}
228 227
236 my ($self) = @_; 235 my ($self) = @_;
237 236
238 ::refresh (); 237 ::refresh ();
239} 238}
240 239
240sub add {
241 my ($self, $widget) = @_;
242
243 $self->SUPER::add ($widget);
244
245 $widget->size_allocate ($widget->size_request);
246}
247
241############################################################################# 248#############################################################################
242 249
243package Crossfire::Client::Widget::Window; 250package Crossfire::Client::Widget::Window;
244 251
245our @ISA = Crossfire::Client::Widget::Bin::; 252our @ISA = Crossfire::Client::Widget::Bin::;
246 253
247use SDL::OpenGL; 254use SDL::OpenGL;
248 255
249sub add { 256sub new {
250 my ($self, $chld) = @_; 257 my ($class, $x, $y, $z, $w, $h) = @_;
251 warn "ADD $chld\n";
252 $self->SUPER::add ($chld);
253 $chld->set_parent ($self);
254}
255 258
256sub remove { 259 my $self = $class->SUPER::new;
257 my ($self) = @_; 260
258 # TODO FIXME: removing a child from a window will crash, see render_chld 261 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
259 # $self->update;
260} 262}
261 263
262sub update { 264sub update {
263 my ($self) = @_; 265 my ($self) = @_;
266
264 $self->render_chld; 267 $self->render_chld;
268 $self->SUPER::update;
265} 269}
266 270
267sub render_chld { 271sub render_chld {
268 my ($self) = @_; 272 my ($self) = @_;
269 my $chld = $self->get;
270 my ($w, $h) = $self->size_request;
271 273
272 require Carp;
273 Carp::cluck "RENDERCHI $w $h";
274 warn "RENDERCHI $w $h\n";
275 $self->{texture} = 274 $self->{texture} =
276 Crossfire::Client::Texture->new_from_opengl ( 275 Crossfire::Client::Texture->new_from_opengl (
277 $w, $h, sub { $chld->draw } 276 $self->{w}, $self->{h}, sub { $self->child->draw }
278 ); 277 );
279 $self->{texture}->upload;
280}
281
282sub size_request {
283 my ($self) = @_;
284 ($self->w, $self->h)
285} 278}
286 279
287sub size_allocate { 280sub size_allocate {
288 my ($self, $w, $h) = @_; 281 my ($self, $w, $h) = @_;
289 282
290 $self->w ($w); 283 $self->{w} = $w;
291 $self->h ($h); 284 $self->{h} = $h;
285
292 $self->get->size_allocate ($w, $h); 286 $self->child->size_allocate ($w, $h);
293 287
294 $self->update; #TODO: Move this to the size_request event propably? 288 $self->render_chld;
295} 289}
296 290
297sub _draw { 291sub _draw {
298 my ($self) = @_; 292 my ($self) = @_;
299 293
336 map { $_ + 4 } $chld->size_request; 330 map { $_ + 4 } $chld->size_request;
337} 331}
338 332
339sub size_allocate { 333sub size_allocate {
340 my ($self, $w, $h) = @_; 334 my ($self, $w, $h) = @_;
341 335
342 $self->SUPER::size_allocate ($w, $h); 336 $self->{w} = $w;
337 $self->{h} = $h;
343 338
344 $self->child->size_allocate ($w - 4, $h - 4); 339 $self->child->size_allocate ($w - 4, $h - 4);
345 $self->child->move (2, 2); 340 $self->child->move (2, 2);
346} 341}
347 342
398 $w -= $tex[2]->{width}; 393 $w -= $tex[2]->{width};
399 $w -= $tex[3]->{width}; 394 $w -= $tex[3]->{width};
400 395
401 $h = $h < 0 ? 0 : $h; 396 $h = $h < 0 ? 0 : $h;
402 $w = $w < 0 ? 0 : $w; 397 $w = $w < 0 ? 0 : $w;
403 warn "CHILD:$w $h\n"; 398
404 $self->child->size_allocate ($w, $h); 399 $self->child->size_allocate ($w, $h);
405 $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 400 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
406} 401}
407 402
408sub _draw { 403sub _draw {
409 my ($self) = @_; 404 my ($self) = @_;
410 405
411 my ($w, $h) = ($self->w, $self->h); 406 my ($w, $h) = ($self->{w}, $self->{h});
412 my ($cw, $ch) = ($self->child->w, $self->child->h); 407 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
413 408
414 glEnable GL_BLEND; 409 glEnable GL_BLEND;
415 glEnable GL_TEXTURE_2D; 410 glEnable GL_TEXTURE_2D;
416 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 411 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
417 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 412 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
571package Crossfire::Client::Widget::VBox; 566package Crossfire::Client::Widget::VBox;
572 567
573our @ISA = Crossfire::Client::Widget::Container::; 568our @ISA = Crossfire::Client::Widget::Container::;
574 569
575use SDL::OpenGL; 570use SDL::OpenGL;
571
572sub size_request {
573 my ($self) = @_;
574
575 my @alloc = map [$_->size_request], @{$self->{children}};
576
577 (
578 (List::Util::max map $_->[0], @alloc),
579 (List::Util::sum map $_->[1], @alloc),
580 )
581}
576 582
577sub size_allocate { 583sub size_allocate {
578 my ($self, $w, $h) = @_; 584 my ($self, $w, $h) = @_;
579 585
580 $self->w ($w); 586 $self->w ($w);
610 } else { 616 } else {
611 my ($cw, $h) = $_->size_request; 617 my ($cw, $h) = $_->size_request;
612 $_->size_allocate ($w, $h); 618 $_->size_allocate ($w, $h);
613 $y += $h; 619 $y += $h;
614 } 620 }
615 }
616}
617
618sub _draw {
619 my ($self) = @_;
620
621 my ($x, $y);
622 for (@{$self->{children} || []}) {
623 $_->draw;
624 $y += $_->h;
625 } 621 }
626} 622}
627 623
628############################################################################# 624#############################################################################
629 625

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines