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