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.49 by root, Sun Apr 9 22:26:51 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines