… | |
… | |
4 | |
4 | |
5 | use Scalar::Util; |
5 | use Scalar::Util; |
6 | |
6 | |
7 | use SDL::OpenGL; |
7 | use SDL::OpenGL; |
8 | use SDL::OpenGL::Constants; |
8 | use SDL::OpenGL::Constants; |
|
|
9 | |
|
|
10 | use Crossfire::Client; |
9 | |
11 | |
10 | our $FOCUS; # the widget with current focus |
12 | our $FOCUS; # the widget with current focus |
11 | |
13 | |
12 | # class methods for events |
14 | # class methods for events |
13 | sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } |
15 | sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } |
… | |
… | |
217 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
219 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
218 | } |
220 | } |
219 | |
221 | |
220 | sub size_allocate { |
222 | sub size_allocate { |
221 | my ($self, $w, $h) = @_; |
223 | my ($self, $w, $h) = @_; |
|
|
224 | |
222 | $self->SUPER::size_allocate ($w, $h); |
225 | $self->SUPER::size_allocate ($w, $h); |
223 | $self->{children}[0]->size_allocate ($w, $h) |
226 | $self->{children}[0]->size_allocate ($w, $h) |
224 | if $self->{children}[0] |
227 | if $self->{children}[0] |
225 | } |
228 | } |
226 | |
229 | |
… | |
… | |
234 | my ($self) = @_; |
237 | my ($self) = @_; |
235 | |
238 | |
236 | ::refresh (); |
239 | ::refresh (); |
237 | } |
240 | } |
238 | |
241 | |
|
|
242 | sub add { |
|
|
243 | my ($self, $widget) = @_; |
|
|
244 | |
|
|
245 | $self->SUPER::add ($widget); |
|
|
246 | |
|
|
247 | $widget->size_allocate ($widget->size_request); |
|
|
248 | } |
|
|
249 | |
239 | ############################################################################# |
250 | ############################################################################# |
240 | |
251 | |
241 | package Crossfire::Client::Widget::Window; |
252 | package Crossfire::Client::Widget::Window; |
242 | |
253 | |
243 | our @ISA = Crossfire::Client::Widget::Bin::; |
254 | our @ISA = Crossfire::Client::Widget::Bin::; |
244 | |
255 | |
245 | use SDL::OpenGL; |
256 | use SDL::OpenGL; |
246 | |
257 | |
247 | sub add { |
258 | sub new { |
248 | my ($self, $chld) = @_; |
259 | my ($class, $x, $y, $z, $w, $h) = @_; |
249 | warn "ADD $chld\n"; |
|
|
250 | $self->SUPER::add ($chld); |
|
|
251 | $chld->set_parent ($self); |
|
|
252 | } |
|
|
253 | |
260 | |
254 | sub remove { |
261 | my $self = $class->SUPER::new; |
255 | my ($self) = @_; |
262 | |
256 | # TODO FIXME: removing a child from a window will crash, see render_chld |
263 | @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); |
257 | # $self->update; |
|
|
258 | } |
264 | } |
259 | |
265 | |
260 | sub update { |
266 | sub update { |
261 | my ($self) = @_; |
267 | my ($self) = @_; |
|
|
268 | |
262 | $self->render_chld; |
269 | $self->render_chld; |
|
|
270 | $self->SUPER::update; |
263 | } |
271 | } |
264 | |
272 | |
265 | sub render_chld { |
273 | sub render_chld { |
266 | my ($self) = @_; |
274 | my ($self) = @_; |
267 | my $chld = $self->get; |
|
|
268 | my ($w, $h) = $self->size_request; |
|
|
269 | |
275 | |
270 | require Carp; |
|
|
271 | Carp::cluck "RENDERCHI $w $h"; |
|
|
272 | warn "RENDERCHI $w $h\n"; |
|
|
273 | $self->{texture} = |
276 | $self->{texture} = |
274 | Crossfire::Client::Texture->new_from_opengl ( |
277 | Crossfire::Client::Texture->new_from_opengl ( |
275 | $w, $h, sub { $chld->draw } |
278 | $self->{w}, $self->{h}, sub { $self->child->draw } |
276 | ); |
279 | ); |
277 | $self->{texture}->upload; |
|
|
278 | } |
|
|
279 | |
|
|
280 | sub size_request { |
|
|
281 | my ($self) = @_; |
|
|
282 | ($self->w, $self->h) |
|
|
283 | } |
280 | } |
284 | |
281 | |
285 | sub size_allocate { |
282 | sub size_allocate { |
286 | my ($self, $w, $h) = @_; |
283 | my ($self, $w, $h) = @_; |
287 | |
284 | |
288 | $self->w ($w); |
285 | $self->{w} = $w; |
289 | $self->h ($h); |
286 | $self->{h} = $h; |
|
|
287 | |
290 | $self->get->size_allocate ($w, $h); |
288 | $self->child->size_allocate ($w, $h); |
291 | |
289 | |
292 | $self->update; #TODO: Move this to the size_request event propably? |
290 | $self->render_chld; |
293 | } |
291 | } |
294 | |
292 | |
295 | sub _draw { |
293 | sub _draw { |
296 | my ($self) = @_; |
294 | my ($self) = @_; |
297 | |
295 | |
… | |
… | |
334 | map { $_ + 4 } $chld->size_request; |
332 | map { $_ + 4 } $chld->size_request; |
335 | } |
333 | } |
336 | |
334 | |
337 | sub size_allocate { |
335 | sub size_allocate { |
338 | my ($self, $w, $h) = @_; |
336 | my ($self, $w, $h) = @_; |
339 | |
337 | |
340 | $self->w ($w); |
338 | $self->{w} = $w; |
341 | $self->h ($h); |
339 | $self->{h} = $h; |
342 | |
340 | |
343 | $self->child->size_allocate ($w - 4, $h - 4); |
341 | $self->child->size_allocate ($w - 4, $h - 4); |
344 | $self->child->move (2, 2); |
342 | $self->child->move (2, 2); |
345 | } |
343 | } |
346 | |
344 | |
… | |
… | |
364 | |
362 | |
365 | ############################################################################# |
363 | ############################################################################# |
366 | |
364 | |
367 | package Crossfire::Client::Widget::FancyFrame; |
365 | package Crossfire::Client::Widget::FancyFrame; |
368 | |
366 | |
369 | our @ISA = Crossfire::Client::Widget::Frame::; |
367 | our @ISA = Crossfire::Client::Widget::Bin::; |
370 | |
368 | |
371 | use SDL::OpenGL; |
369 | use SDL::OpenGL; |
372 | |
370 | |
373 | sub new { |
371 | my @tex = |
374 | my ($self, $theme) = @_; |
|
|
375 | $self = $self->SUPER::new; |
|
|
376 | |
|
|
377 | $self->{txts} = [ |
|
|
378 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
372 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
379 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png) |
373 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
380 | ]; |
|
|
381 | $self |
|
|
382 | } |
|
|
383 | |
374 | |
384 | sub size_request { |
375 | sub size_request { |
385 | my ($self) = @_; |
376 | my ($self) = @_; |
386 | |
377 | |
387 | my ($w, $h) = $self->SUPER::size_request; |
378 | my ($w, $h) = $self->SUPER::size_request; |
388 | |
379 | |
389 | $h += $self->{txts}->[1]->{height}; |
380 | $h += $tex[1]->{height}; |
390 | $h += $self->{txts}->[4]->{height}; |
381 | $h += $tex[4]->{height}; |
391 | $w += $self->{txts}->[2]->{width}; |
382 | $w += $tex[2]->{width}; |
392 | $w += $self->{txts}->[3]->{width}; |
383 | $w += $tex[3]->{width}; |
393 | |
384 | |
394 | ($w, $h) |
385 | ($w, $h) |
395 | } |
386 | } |
396 | |
387 | |
397 | sub size_allocate { |
388 | sub size_allocate { |
398 | my ($self, $w, $h) = @_; |
389 | my ($self, $w, $h) = @_; |
399 | |
390 | |
400 | $self->SUPER::size_allocate ($w, $h); |
391 | $self->SUPER::size_allocate ($w, $h); |
401 | |
392 | |
402 | $h -= $self->{txts}->[1]->{height}; |
393 | $h -= $tex[1]->{height}; |
403 | $h -= $self->{txts}->[4]->{height}; |
394 | $h -= $tex[4]->{height}; |
404 | $w -= $self->{txts}->[2]->{width}; |
395 | $w -= $tex[2]->{width}; |
405 | $w -= $self->{txts}->[3]->{width}; |
396 | $w -= $tex[3]->{width}; |
406 | |
397 | |
407 | $h = $h < 0 ? 0 : $h; |
398 | $h = $h < 0 ? 0 : $h; |
408 | $w = $w < 0 ? 0 : $w; |
399 | $w = $w < 0 ? 0 : $w; |
409 | warn "CHILD:$w $h\n"; |
400 | warn "CHILD:$w $h\n"; |
410 | $self->child->size_allocate ($w, $h); |
401 | $self->child->size_allocate ($w, $h); |
… | |
… | |
420 | glEnable GL_BLEND; |
411 | glEnable GL_BLEND; |
421 | glEnable GL_TEXTURE_2D; |
412 | glEnable GL_TEXTURE_2D; |
422 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
413 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
423 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
414 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
424 | |
415 | |
425 | my $top = $self->{txts}->[1]; |
416 | my $top = $tex[1]; |
426 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
417 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
427 | |
418 | |
428 | glBegin GL_QUADS; |
419 | glBegin GL_QUADS; |
429 | glTexCoord 0, 0; glVertex 0 , 0; |
420 | glTexCoord 0, 0; glVertex 0 , 0; |
430 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
421 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
431 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
422 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
432 | glTexCoord 1, 0; glVertex $w , 0; |
423 | glTexCoord 1, 0; glVertex $w , 0; |
433 | glEnd; |
424 | glEnd; |
434 | |
425 | |
435 | my $left = $self->{txts}->[3]; |
426 | my $left = $tex[3]; |
436 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
427 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
437 | |
428 | |
438 | glBegin GL_QUADS; |
429 | glBegin GL_QUADS; |
439 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
430 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
440 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
431 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
441 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
432 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
442 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
433 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
443 | glEnd; |
434 | glEnd; |
444 | |
435 | |
445 | my $right = $self->{txts}->[2]; |
436 | my $right = $tex[2]; |
446 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
437 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
447 | |
438 | |
448 | glBegin GL_QUADS; |
439 | glBegin GL_QUADS; |
449 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
440 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
450 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
441 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
451 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
442 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
452 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
443 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
453 | glEnd; |
444 | glEnd; |
454 | |
445 | |
455 | my $bottom = $self->{txts}->[4]; |
446 | my $bottom = $tex[4]; |
456 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
447 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
457 | |
448 | |
458 | glBegin GL_QUADS; |
449 | glBegin GL_QUADS; |
459 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
450 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
460 | glTexCoord 0, 1; glVertex 0 , $h; |
451 | glTexCoord 0, 1; glVertex 0 , $h; |
461 | glTexCoord 1, 1; glVertex $w , $h; |
452 | glTexCoord 1, 1; glVertex $w , $h; |
462 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
453 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
463 | glEnd; |
454 | glEnd; |
464 | |
455 | |
465 | my $bg = $self->{txts}->[0]; |
456 | my $bg = $tex[0]; |
466 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
457 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
467 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
458 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
468 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
459 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
469 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
460 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
470 | |
461 | |