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