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.39 by root, Sun Apr 9 21:39:08 2006 UTC vs.
Revision 1.45 by root, Sun Apr 9 22:19:03 2006 UTC

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
10use Crossfire::Client;
9 11
10our $FOCUS; # the widget with current focus 12our $FOCUS; # the widget with current focus
11 13
12# class methods for events 14# class methods for events
13sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 15sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
37 Carp::confess "size_request is abtract"; 39 Carp::confess "size_request is abtract";
38} 40}
39 41
40sub size_allocate { 42sub size_allocate {
41 my ($self, $w, $h) = @_; 43 my ($self, $w, $h) = @_;
44
42 $self->w ($w); 45 $self->{w} = $w;
43 $self->h ($h); 46 $self->{h} = $h;
44} 47}
45 48
46sub focus_in { 49sub focus_in {
47 my ($widget) = @_; 50 my ($widget) = @_;
48 $FOCUS = $widget; 51 $FOCUS = $widget;
159 162
160 @{$self->{children}} = 163 @{$self->{children}} =
161 sort { $a->{z} <=> $b->{z} } 164 sort { $a->{z} <=> $b->{z} }
162 @{$self->{children}}, $chld; 165 @{$self->{children}}, $chld;
163 166
164 $self->size_allocate ($self->{w}, $self->{h}); 167 $self->size_allocate ($self->{w}, $self->{h})
168 if $self->{w}; #TODO: check for "realised state"
165} 169}
166 170
167sub remove { 171sub remove {
168 my ($self, $widget) = @_; 172 my ($self, $widget) = @_;
169 173
172 $self->size_allocate ($self->{w}, $self->{h}); 176 $self->size_allocate ($self->{w}, $self->{h});
173} 177}
174 178
175sub find_widget { 179sub find_widget {
176 my ($self, $x, $y) = @_; 180 my ($self, $x, $y) = @_;
181
182 $x -= $self->{x};
183 $y -= $self->{y};
177 184
178 my $res; 185 my $res;
179 186
180 for (@{ $self->{children} }) { 187 for (@{ $self->{children} }) {
181 $res = $_->find_widget ($x, $y) 188 $res = $_->find_widget ($x, $y)
183 } 190 }
184 191
185 () 192 ()
186} 193}
187 194
188sub size_request {
189 my ($self) = @_;
190
191 my ($hs, $ws) = (0, 0);
192 for (@{$self->{children} || []}) {
193 my ($w, $h) = $_->size_request;
194 $hs += $h;
195 if ($ws < $w) { $ws = $w }
196 }
197
198 return ($ws, $hs);
199}
200
201sub _draw { 195sub _draw {
202 my ($self) = @_; 196 my ($self) = @_;
203 197
204 $_->draw for @{$self->{children}}; 198 $_->draw for @{$self->{children}};
205} 199}
216 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 210 $_[0]{children}[0]->size_request if $_[0]{children}[0];
217} 211}
218 212
219sub size_allocate { 213sub size_allocate {
220 my ($self, $w, $h) = @_; 214 my ($self, $w, $h) = @_;
215
221 $self->SUPER::size_allocate ($w, $h); 216 $self->SUPER::size_allocate ($w, $h);
222 $self->{children}[0]->size_allocate ($w, $h) 217 $self->{children}[0]->size_allocate ($w, $h)
223 if $self->{children}[0] 218 if $self->{children}[0]
224} 219}
225 220
233 my ($self) = @_; 228 my ($self) = @_;
234 229
235 ::refresh (); 230 ::refresh ();
236} 231}
237 232
233sub add {
234 my ($self, $widget) = @_;
235
236 $self->SUPER::add ($widget);
237
238 $widget->size_allocate ($widget->size_request);
239}
240
238############################################################################# 241#############################################################################
239 242
240package Crossfire::Client::Widget::Window; 243package Crossfire::Client::Widget::Window;
241 244
242our @ISA = Crossfire::Client::Widget::Bin::; 245our @ISA = Crossfire::Client::Widget::Bin::;
243 246
244use SDL::OpenGL; 247use SDL::OpenGL;
245 248
246sub add { 249sub new {
247 my ($self, $chld) = @_; 250 my ($class, $x, $y, $z, $w, $h) = @_;
248 warn "ADD $chld\n";
249 $self->SUPER::add ($chld);
250 $chld->set_parent ($self);
251}
252 251
253sub remove { 252 my $self = $class->SUPER::new;
254 my ($self) = @_; 253
255 # TODO FIXME: removing a child from a window will crash, see render_chld 254 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
256 # $self->update;
257} 255}
258 256
259sub update { 257sub update {
260 my ($self) = @_; 258 my ($self) = @_;
259
261 $self->render_chld; 260 $self->render_chld;
261 $self->SUPER::update;
262} 262}
263 263
264sub render_chld { 264sub render_chld {
265 my ($self) = @_; 265 my ($self) = @_;
266 my $chld = $self->get;
267 my ($w, $h) = $self->size_request;
268 266
269 require Carp;
270 Carp::cluck "RENDERCHI $w $h";
271 warn "RENDERCHI $w $h\n";
272 $self->{texture} = 267 $self->{texture} =
273 Crossfire::Client::Texture->new_from_opengl ( 268 Crossfire::Client::Texture->new_from_opengl (
274 $w, $h, sub { $chld->draw } 269 $self->{w}, $self->{h}, sub { $self->child->draw }
275 ); 270 );
276 $self->{texture}->upload;
277}
278
279sub size_request {
280 my ($self) = @_;
281 ($self->w, $self->h)
282} 271}
283 272
284sub size_allocate { 273sub size_allocate {
285 my ($self, $w, $h) = @_; 274 my ($self, $w, $h) = @_;
286 275
287 $self->w ($w); 276 $self->{w} = $w;
288 $self->h ($h); 277 $self->{h} = $h;
278
289 $self->get->size_allocate ($w, $h); 279 $self->child->size_allocate ($w, $h);
290 280
291 $self->update; #TODO: Move this to the size_request event propably? 281 $self->render_chld;
292} 282}
293 283
294sub _draw { 284sub _draw {
295 my ($self) = @_; 285 my ($self) = @_;
296 286
333 map { $_ + 4 } $chld->size_request; 323 map { $_ + 4 } $chld->size_request;
334} 324}
335 325
336sub size_allocate { 326sub size_allocate {
337 my ($self, $w, $h) = @_; 327 my ($self, $w, $h) = @_;
338 328
339 $self->w ($w); 329 $self->{w} = $w;
340 $self->h ($h); 330 $self->{h} = $h;
341 331
342 $self->child->size_allocate ($w - 4, $h - 4); 332 $self->child->size_allocate ($w - 4, $h - 4);
343 $self->child->move (2, 2); 333 $self->child->move (2, 2);
344} 334}
345 335
363 353
364############################################################################# 354#############################################################################
365 355
366package Crossfire::Client::Widget::FancyFrame; 356package Crossfire::Client::Widget::FancyFrame;
367 357
368our @ISA = Crossfire::Client::Widget::Frame::; 358our @ISA = Crossfire::Client::Widget::Bin::;
369 359
370use SDL::OpenGL; 360use SDL::OpenGL;
371 361
372sub new { 362my @tex =
373 my ($self, $theme) = @_;
374 $self = $self->SUPER::new;
375
376 $self->{txts} = [
377 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 363 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
378 qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ 364 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
379 ];
380 $self
381}
382 365
383sub size_request { 366sub size_request {
384 my ($self) = @_; 367 my ($self) = @_;
385 368
386 my ($w, $h) = $self->SUPER::size_request; 369 my ($w, $h) = $self->SUPER::size_request;
387 370
388 $h += $self->{txts}->[1]->{height}; 371 $h += $tex[1]->{height};
389 $h += $self->{txts}->[4]->{height}; 372 $h += $tex[4]->{height};
390 $w += $self->{txts}->[2]->{width}; 373 $w += $tex[2]->{width};
391 $w += $self->{txts}->[3]->{width}; 374 $w += $tex[3]->{width};
392 375
393 ($w, $h) 376 ($w, $h)
394} 377}
395 378
396sub size_allocate { 379sub size_allocate {
397 my ($self, $w, $h) = @_; 380 my ($self, $w, $h) = @_;
398 381
399 $self->w ($w); 382 $self->SUPER::size_allocate ($w, $h);
400 $self->h ($h); 383
401 $h -= $self->{txts}->[1]->{height}; 384 $h -= $tex[1]->{height};
402 $h -= $self->{txts}->[4]->{height}; 385 $h -= $tex[4]->{height};
403 $w -= $self->{txts}->[2]->{width}; 386 $w -= $tex[2]->{width};
404 $w -= $self->{txts}->[3]->{width}; 387 $w -= $tex[3]->{width};
405 388
406 $h = $h < 0 ? 0 : $h; 389 $h = $h < 0 ? 0 : $h;
407 $w = $w < 0 ? 0 : $w; 390 $w = $w < 0 ? 0 : $w;
408 warn "CHILD:$w $h\n"; 391
409 $self->child->size_allocate ($w, $h); 392 $self->child->size_allocate ($w, $h);
410 $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 393 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
411} 394}
412 395
413sub _draw { 396sub _draw {
414 my ($self) = @_; 397 my ($self) = @_;
415 398
416 my ($w, $h) = ($self->w, $self->h); 399 my ($w, $h) = ($self->{w}, $self->{h});
417 my ($cw, $ch) = ($self->child->w, $self->child->h); 400 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
418 401
419 glEnable GL_BLEND; 402 glEnable GL_BLEND;
420 glEnable GL_TEXTURE_2D; 403 glEnable GL_TEXTURE_2D;
421 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 404 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
422 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 405 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
423 406
424 my $top = $self->{txts}->[1]; 407 my $top = $tex[1];
425 glBindTexture GL_TEXTURE_2D, $top->{name}; 408 glBindTexture GL_TEXTURE_2D, $top->{name};
426 409
427 glBegin GL_QUADS; 410 glBegin GL_QUADS;
428 glTexCoord 0, 0; glVertex 0 , 0; 411 glTexCoord 0, 0; glVertex 0 , 0;
429 glTexCoord 0, 1; glVertex 0 , $top->{height}; 412 glTexCoord 0, 1; glVertex 0 , $top->{height};
430 glTexCoord 1, 1; glVertex $w , $top->{height}; 413 glTexCoord 1, 1; glVertex $w , $top->{height};
431 glTexCoord 1, 0; glVertex $w , 0; 414 glTexCoord 1, 0; glVertex $w , 0;
432 glEnd; 415 glEnd;
433 416
434 my $left = $self->{txts}->[3]; 417 my $left = $tex[3];
435 glBindTexture GL_TEXTURE_2D, $left->{name}; 418 glBindTexture GL_TEXTURE_2D, $left->{name};
436 419
437 glBegin GL_QUADS; 420 glBegin GL_QUADS;
438 glTexCoord 0, 0; glVertex 0 , $top->{height}; 421 glTexCoord 0, 0; glVertex 0 , $top->{height};
439 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; 422 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
440 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; 423 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
441 glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; 424 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
442 glEnd; 425 glEnd;
443 426
444 my $right = $self->{txts}->[2]; 427 my $right = $tex[2];
445 glBindTexture GL_TEXTURE_2D, $right->{name}; 428 glBindTexture GL_TEXTURE_2D, $right->{name};
446 429
447 glBegin GL_QUADS; 430 glBegin GL_QUADS;
448 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; 431 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
449 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; 432 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
450 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; 433 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
451 glTexCoord 1, 0; glVertex $w , $top->{height}; 434 glTexCoord 1, 0; glVertex $w , $top->{height};
452 glEnd; 435 glEnd;
453 436
454 my $bottom = $self->{txts}->[4]; 437 my $bottom = $tex[4];
455 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 438 glBindTexture GL_TEXTURE_2D, $bottom->{name};
456 439
457 glBegin GL_QUADS; 440 glBegin GL_QUADS;
458 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; 441 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
459 glTexCoord 0, 1; glVertex 0 , $h; 442 glTexCoord 0, 1; glVertex 0 , $h;
460 glTexCoord 1, 1; glVertex $w , $h; 443 glTexCoord 1, 1; glVertex $w , $h;
461 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; 444 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
462 glEnd; 445 glEnd;
463 446
464 my $bg = $self->{txts}->[0]; 447 my $bg = $tex[0];
465 glBindTexture GL_TEXTURE_2D, $bg->{name}; 448 glBindTexture GL_TEXTURE_2D, $bg->{name};
466 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 449 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
467 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 450 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 451 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
469 452
576package Crossfire::Client::Widget::VBox; 559package Crossfire::Client::Widget::VBox;
577 560
578our @ISA = Crossfire::Client::Widget::Container::; 561our @ISA = Crossfire::Client::Widget::Container::;
579 562
580use SDL::OpenGL; 563use SDL::OpenGL;
564
565sub size_request {
566 my ($self) = @_;
567
568 my @alloc = map [$_->size_request], @{$self->{children}};
569
570 (
571 (List::Util::max map $_->[0], @alloc),
572 (List::Util::sum map $_->[1], @alloc),
573 )
574}
581 575
582sub size_allocate { 576sub size_allocate {
583 my ($self, $w, $h) = @_; 577 my ($self, $w, $h) = @_;
584 578
585 $self->w ($w); 579 $self->w ($w);
615 } else { 609 } else {
616 my ($cw, $h) = $_->size_request; 610 my ($cw, $h) = $_->size_request;
617 $_->size_allocate ($w, $h); 611 $_->size_allocate ($w, $h);
618 $y += $h; 612 $y += $h;
619 } 613 }
620 }
621}
622
623sub _draw {
624 my ($self) = @_;
625
626 my ($x, $y);
627 for (@{$self->{children} || []}) {
628 $_->draw;
629 $y += $_->h;
630 } 614 }
631} 615}
632 616
633############################################################################# 617#############################################################################
634 618

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines