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