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