… | |
… | |
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 | |
9 | |
10 | our $FOCUS; # the widget with current focus |
10 | use Crossfire::Client; |
11 | |
11 | |
12 | # class methods for events |
12 | # class methods for events |
13 | sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } |
13 | sub feed_sdl_key_down_event { $::FOCUS->key_down ($_[0]) if $::FOCUS } |
14 | sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } |
14 | sub feed_sdl_key_up_event { $::FOCUS->key_up ($_[0]) if $::FOCUS } |
15 | sub feed_sdl_button_down_event { } |
15 | sub feed_sdl_button_down_event { } |
16 | sub feed_sdl_button_up_event { } |
16 | sub feed_sdl_button_up_event { } |
17 | |
17 | |
18 | sub new { |
18 | sub new { |
19 | my $class = shift; |
19 | my $class = shift; |
… | |
… | |
44 | $self->{h} = $h; |
44 | $self->{h} = $h; |
45 | } |
45 | } |
46 | |
46 | |
47 | sub focus_in { |
47 | sub focus_in { |
48 | my ($widget) = @_; |
48 | my ($widget) = @_; |
49 | $FOCUS = $widget; |
49 | $::FOCUS = $widget; |
50 | } |
50 | } |
51 | |
51 | |
52 | sub focus_out { |
52 | sub focus_out { |
53 | my ($widget) = @_; |
53 | my ($widget) = @_; |
54 | } |
54 | } |
… | |
… | |
79 | my ($self) = @_; |
79 | my ($self) = @_; |
80 | |
80 | |
81 | glPushMatrix; |
81 | glPushMatrix; |
82 | glTranslate $self->{x}, $self->{y}, 0; |
82 | glTranslate $self->{x}, $self->{y}, 0; |
83 | $self->_draw; |
83 | $self->_draw; |
|
|
84 | if ($self == $::HOVER) { |
|
|
85 | glColor 1, 1, 1, 1; |
|
|
86 | glBegin GL_LINES; |
|
|
87 | glVertex 0, 0; |
|
|
88 | glVertex $self->{w} - 1, 0; |
|
|
89 | glVertex $self->{w} - 1, $self->{h} - 1; |
|
|
90 | glVertex 0, $self->{h} - 1; |
|
|
91 | glVertex 0, 0; |
|
|
92 | glEnd; |
|
|
93 | } |
84 | glPopMatrix; |
94 | glPopMatrix; |
85 | } |
95 | } |
86 | |
96 | |
87 | sub _draw { |
97 | sub _draw { |
88 | my ($self) = @_; |
98 | my ($self) = @_; |
… | |
… | |
160 | |
170 | |
161 | @{$self->{children}} = |
171 | @{$self->{children}} = |
162 | sort { $a->{z} <=> $b->{z} } |
172 | sort { $a->{z} <=> $b->{z} } |
163 | @{$self->{children}}, $chld; |
173 | @{$self->{children}}, $chld; |
164 | |
174 | |
165 | $self->size_allocate ($self->{w}, $self->{h}); |
175 | $self->size_allocate ($self->{w}, $self->{h}) |
|
|
176 | if $self->{w}; #TODO: check for "realised state" |
166 | } |
177 | } |
167 | |
178 | |
168 | sub remove { |
179 | sub remove { |
169 | my ($self, $widget) = @_; |
180 | my ($self, $widget) = @_; |
170 | |
181 | |
… | |
… | |
174 | } |
185 | } |
175 | |
186 | |
176 | sub find_widget { |
187 | sub find_widget { |
177 | my ($self, $x, $y) = @_; |
188 | my ($self, $x, $y) = @_; |
178 | |
189 | |
|
|
190 | $x -= $self->{x}; |
|
|
191 | $y -= $self->{y}; |
|
|
192 | |
179 | my $res; |
193 | my $res; |
180 | |
194 | |
181 | for (@{ $self->{children} }) { |
195 | for (reverse @{ $self->{children} }) { |
182 | $res = $_->find_widget ($x, $y) |
196 | $res = $_->find_widget ($x, $y) |
183 | and return $res; |
197 | and return $res; |
184 | } |
198 | } |
185 | |
199 | |
186 | () |
200 | $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 | } |
201 | } |
201 | |
202 | |
202 | sub _draw { |
203 | sub _draw { |
203 | my ($self) = @_; |
204 | my ($self) = @_; |
204 | |
205 | |
… | |
… | |
217 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
218 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
218 | } |
219 | } |
219 | |
220 | |
220 | sub size_allocate { |
221 | sub size_allocate { |
221 | my ($self, $w, $h) = @_; |
222 | my ($self, $w, $h) = @_; |
|
|
223 | |
222 | $self->SUPER::size_allocate ($w, $h); |
224 | $self->SUPER::size_allocate ($w, $h); |
223 | $self->{children}[0]->size_allocate ($w, $h) |
225 | $self->{children}[0]->size_allocate ($w, $h) |
224 | if $self->{children}[0] |
226 | if $self->{children}[0] |
225 | } |
227 | } |
226 | |
228 | |
… | |
… | |
234 | my ($self) = @_; |
236 | my ($self) = @_; |
235 | |
237 | |
236 | ::refresh (); |
238 | ::refresh (); |
237 | } |
239 | } |
238 | |
240 | |
|
|
241 | sub add { |
|
|
242 | my ($self, $widget) = @_; |
|
|
243 | |
|
|
244 | $self->SUPER::add ($widget); |
|
|
245 | |
|
|
246 | $widget->size_allocate ($widget->size_request); |
|
|
247 | } |
|
|
248 | |
239 | ############################################################################# |
249 | ############################################################################# |
240 | |
250 | |
241 | package Crossfire::Client::Widget::Window; |
251 | package Crossfire::Client::Widget::Window; |
242 | |
252 | |
243 | our @ISA = Crossfire::Client::Widget::Bin::; |
253 | our @ISA = Crossfire::Client::Widget::Bin::; |
244 | |
254 | |
245 | use SDL::OpenGL; |
255 | use SDL::OpenGL; |
246 | |
256 | |
247 | sub add { |
257 | sub new { |
248 | my ($self, $chld) = @_; |
258 | my ($class, $x, $y, $z, $w, $h) = @_; |
249 | warn "ADD $chld\n"; |
|
|
250 | $self->SUPER::add ($chld); |
|
|
251 | $chld->set_parent ($self); |
|
|
252 | } |
|
|
253 | |
259 | |
254 | sub remove { |
260 | my $self = $class->SUPER::new; |
255 | my ($self) = @_; |
261 | |
256 | # TODO FIXME: removing a child from a window will crash, see render_chld |
262 | @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); |
257 | # $self->update; |
|
|
258 | } |
263 | } |
259 | |
264 | |
260 | sub update { |
265 | sub update { |
261 | my ($self) = @_; |
266 | my ($self) = @_; |
|
|
267 | |
262 | $self->render_chld; |
268 | $self->render_chld; |
|
|
269 | $self->SUPER::update; |
263 | } |
270 | } |
264 | |
271 | |
265 | sub render_chld { |
272 | sub render_chld { |
266 | my ($self) = @_; |
273 | my ($self) = @_; |
267 | my $chld = $self->get; |
|
|
268 | my ($w, $h) = $self->size_request; |
|
|
269 | |
274 | |
270 | require Carp; |
|
|
271 | Carp::cluck "RENDERCHI $w $h"; |
|
|
272 | warn "RENDERCHI $w $h\n"; |
|
|
273 | $self->{texture} = |
275 | $self->{texture} = |
274 | Crossfire::Client::Texture->new_from_opengl ( |
276 | Crossfire::Client::Texture->new_from_opengl ( |
275 | $w, $h, sub { $chld->draw } |
277 | $self->{w}, $self->{h}, sub { $self->child->draw } |
276 | ); |
278 | ); |
277 | $self->{texture}->upload; |
|
|
278 | } |
|
|
279 | |
|
|
280 | sub size_request { |
|
|
281 | my ($self) = @_; |
|
|
282 | ($self->w, $self->h) |
|
|
283 | } |
279 | } |
284 | |
280 | |
285 | sub size_allocate { |
281 | sub size_allocate { |
286 | my ($self, $w, $h) = @_; |
282 | my ($self, $w, $h) = @_; |
287 | |
283 | |
288 | $self->w ($w); |
284 | $self->{w} = $w; |
289 | $self->h ($h); |
285 | $self->{h} = $h; |
|
|
286 | |
290 | $self->get->size_allocate ($w, $h); |
287 | $self->child->size_allocate ($w, $h); |
291 | |
288 | |
292 | $self->update; #TODO: Move this to the size_request event propably? |
289 | $self->render_chld; |
293 | } |
290 | } |
294 | |
291 | |
295 | sub _draw { |
292 | sub _draw { |
296 | my ($self) = @_; |
293 | my ($self) = @_; |
297 | |
294 | |
… | |
… | |
334 | map { $_ + 4 } $chld->size_request; |
331 | map { $_ + 4 } $chld->size_request; |
335 | } |
332 | } |
336 | |
333 | |
337 | sub size_allocate { |
334 | sub size_allocate { |
338 | my ($self, $w, $h) = @_; |
335 | my ($self, $w, $h) = @_; |
339 | |
336 | |
340 | $self->w ($w); |
337 | $self->{w} = $w; |
341 | $self->h ($h); |
338 | $self->{h} = $h; |
342 | |
339 | |
343 | $self->child->size_allocate ($w - 4, $h - 4); |
340 | $self->child->size_allocate ($w - 4, $h - 4); |
344 | $self->child->move (2, 2); |
341 | $self->child->move (2, 2); |
345 | } |
342 | } |
346 | |
343 | |
… | |
… | |
364 | |
361 | |
365 | ############################################################################# |
362 | ############################################################################# |
366 | |
363 | |
367 | package Crossfire::Client::Widget::FancyFrame; |
364 | package Crossfire::Client::Widget::FancyFrame; |
368 | |
365 | |
369 | our @ISA = Crossfire::Client::Widget::Frame::; |
366 | our @ISA = Crossfire::Client::Widget::Bin::; |
370 | |
367 | |
371 | use SDL::OpenGL; |
368 | use SDL::OpenGL; |
372 | |
369 | |
373 | sub new { |
370 | 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 $_ } |
371 | 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) |
372 | 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 | |
373 | |
384 | sub size_request { |
374 | sub size_request { |
385 | my ($self) = @_; |
375 | my ($self) = @_; |
386 | |
376 | |
387 | my ($w, $h) = $self->SUPER::size_request; |
377 | my ($w, $h) = $self->SUPER::size_request; |
388 | |
378 | |
389 | $h += $self->{txts}->[1]->{height}; |
379 | $h += $tex[1]->{height}; |
390 | $h += $self->{txts}->[4]->{height}; |
380 | $h += $tex[4]->{height}; |
391 | $w += $self->{txts}->[2]->{width}; |
381 | $w += $tex[2]->{width}; |
392 | $w += $self->{txts}->[3]->{width}; |
382 | $w += $tex[3]->{width}; |
393 | |
383 | |
394 | ($w, $h) |
384 | ($w, $h) |
395 | } |
385 | } |
396 | |
386 | |
397 | sub size_allocate { |
387 | sub size_allocate { |
398 | my ($self, $w, $h) = @_; |
388 | my ($self, $w, $h) = @_; |
399 | |
389 | |
400 | $self->SUPER::size_allocate ($w, $h); |
390 | $self->SUPER::size_allocate ($w, $h); |
401 | |
391 | |
402 | $h -= $self->{txts}->[1]->{height}; |
392 | $h -= $tex[1]->{height}; |
403 | $h -= $self->{txts}->[4]->{height}; |
393 | $h -= $tex[4]->{height}; |
404 | $w -= $self->{txts}->[2]->{width}; |
394 | $w -= $tex[2]->{width}; |
405 | $w -= $self->{txts}->[3]->{width}; |
395 | $w -= $tex[3]->{width}; |
406 | |
396 | |
407 | $h = $h < 0 ? 0 : $h; |
397 | $h = $h < 0 ? 0 : $h; |
408 | $w = $w < 0 ? 0 : $w; |
398 | $w = $w < 0 ? 0 : $w; |
409 | warn "CHILD:$w $h\n"; |
399 | |
410 | $self->child->size_allocate ($w, $h); |
400 | $self->child->size_allocate ($w, $h); |
411 | $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); |
401 | $self->child->move ($tex[3]->{width}, $tex[1]->{height}); |
412 | } |
402 | } |
413 | |
403 | |
414 | sub _draw { |
404 | sub _draw { |
415 | my ($self) = @_; |
405 | my ($self) = @_; |
416 | |
406 | |
417 | my ($w, $h) = ($self->w, $self->h); |
407 | my ($w, $h) = ($self->{w}, $self->{h}); |
418 | my ($cw, $ch) = ($self->child->w, $self->child->h); |
408 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
419 | |
409 | |
420 | glEnable GL_BLEND; |
410 | glEnable GL_BLEND; |
421 | glEnable GL_TEXTURE_2D; |
411 | glEnable GL_TEXTURE_2D; |
422 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
412 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
423 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
413 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
424 | |
414 | |
425 | my $top = $self->{txts}->[1]; |
415 | my $top = $tex[1]; |
426 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
416 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
427 | |
417 | |
428 | glBegin GL_QUADS; |
418 | glBegin GL_QUADS; |
429 | glTexCoord 0, 0; glVertex 0 , 0; |
419 | glTexCoord 0, 0; glVertex 0 , 0; |
430 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
420 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
431 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
421 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
432 | glTexCoord 1, 0; glVertex $w , 0; |
422 | glTexCoord 1, 0; glVertex $w , 0; |
433 | glEnd; |
423 | glEnd; |
434 | |
424 | |
435 | my $left = $self->{txts}->[3]; |
425 | my $left = $tex[3]; |
436 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
426 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
437 | |
427 | |
438 | glBegin GL_QUADS; |
428 | glBegin GL_QUADS; |
439 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
429 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
440 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
430 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
441 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
431 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
442 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
432 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
443 | glEnd; |
433 | glEnd; |
444 | |
434 | |
445 | my $right = $self->{txts}->[2]; |
435 | my $right = $tex[2]; |
446 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
436 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
447 | |
437 | |
448 | glBegin GL_QUADS; |
438 | glBegin GL_QUADS; |
449 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
439 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
450 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
440 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
451 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
441 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
452 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
442 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
453 | glEnd; |
443 | glEnd; |
454 | |
444 | |
455 | my $bottom = $self->{txts}->[4]; |
445 | my $bottom = $tex[4]; |
456 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
446 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
457 | |
447 | |
458 | glBegin GL_QUADS; |
448 | glBegin GL_QUADS; |
459 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
449 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
460 | glTexCoord 0, 1; glVertex 0 , $h; |
450 | glTexCoord 0, 1; glVertex 0 , $h; |
461 | glTexCoord 1, 1; glVertex $w , $h; |
451 | glTexCoord 1, 1; glVertex $w , $h; |
462 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
452 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
463 | glEnd; |
453 | glEnd; |
464 | |
454 | |
465 | my $bg = $self->{txts}->[0]; |
455 | my $bg = $tex[0]; |
466 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
456 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
467 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
457 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
468 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
458 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
469 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
459 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
470 | |
460 | |
… | |
… | |
577 | package Crossfire::Client::Widget::VBox; |
567 | package Crossfire::Client::Widget::VBox; |
578 | |
568 | |
579 | our @ISA = Crossfire::Client::Widget::Container::; |
569 | our @ISA = Crossfire::Client::Widget::Container::; |
580 | |
570 | |
581 | use SDL::OpenGL; |
571 | use SDL::OpenGL; |
|
|
572 | |
|
|
573 | sub size_request { |
|
|
574 | my ($self) = @_; |
|
|
575 | |
|
|
576 | my @alloc = map [$_->size_request], @{$self->{children}}; |
|
|
577 | |
|
|
578 | ( |
|
|
579 | (List::Util::max map $_->[0], @alloc), |
|
|
580 | (List::Util::sum map $_->[1], @alloc), |
|
|
581 | ) |
|
|
582 | } |
582 | |
583 | |
583 | sub size_allocate { |
584 | sub size_allocate { |
584 | my ($self, $w, $h) = @_; |
585 | my ($self, $w, $h) = @_; |
585 | |
586 | |
586 | $self->w ($w); |
587 | $self->w ($w); |
… | |
… | |
616 | } else { |
617 | } else { |
617 | my ($cw, $h) = $_->size_request; |
618 | my ($cw, $h) = $_->size_request; |
618 | $_->size_allocate ($w, $h); |
619 | $_->size_allocate ($w, $h); |
619 | $y += $h; |
620 | $y += $h; |
620 | } |
621 | } |
621 | } |
|
|
622 | } |
|
|
623 | |
|
|
624 | sub _draw { |
|
|
625 | my ($self) = @_; |
|
|
626 | |
|
|
627 | my ($x, $y); |
|
|
628 | for (@{$self->{children} || []}) { |
|
|
629 | $_->draw; |
|
|
630 | $y += $_->h; |
|
|
631 | } |
622 | } |
632 | } |
623 | } |
633 | |
624 | |
634 | ############################################################################# |
625 | ############################################################################# |
635 | |
626 | |