… | |
… | |
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; |
… | |
… | |
134 | my ($self) = @_; |
137 | my ($self) = @_; |
135 | |
138 | |
136 | #$self->deactivate; |
139 | #$self->deactivate; |
137 | } |
140 | } |
138 | |
141 | |
|
|
142 | ############################################################################# |
|
|
143 | |
139 | package Crossfire::Client::Widget::Container; |
144 | package Crossfire::Client::Widget::Container; |
140 | |
145 | |
141 | our @ISA = Crossfire::Client::Widget::; |
146 | our @ISA = Crossfire::Client::Widget::; |
142 | |
147 | |
143 | sub new { |
148 | sub new { |
… | |
… | |
200 | my ($self) = @_; |
205 | my ($self) = @_; |
201 | |
206 | |
202 | $_->draw for @{$self->{children}}; |
207 | $_->draw for @{$self->{children}}; |
203 | } |
208 | } |
204 | |
209 | |
|
|
210 | ############################################################################# |
|
|
211 | |
205 | package Crossfire::Client::Widget::Bin; |
212 | package Crossfire::Client::Widget::Bin; |
206 | |
213 | |
207 | our @ISA = Crossfire::Client::Widget::Container::; |
214 | our @ISA = Crossfire::Client::Widget::Container::; |
208 | |
215 | |
209 | sub get { $_[0]->{children}[0] } |
216 | sub child { $_[0]->{children}[0] } |
210 | |
217 | |
211 | sub size_request { |
218 | sub size_request { |
212 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
219 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
213 | } |
220 | } |
214 | |
221 | |
215 | sub size_allocate { |
222 | sub size_allocate { |
216 | my ($self, $w, $h) = @_; |
223 | my ($self, $w, $h) = @_; |
|
|
224 | |
217 | $self->SUPER::size_allocate ($w, $h); |
225 | $self->SUPER::size_allocate ($w, $h); |
218 | $self->{children}[0]->size_allocate ($w, $h) |
226 | $self->{children}[0]->size_allocate ($w, $h) |
219 | if $self->{children}[0] |
227 | if $self->{children}[0] |
220 | } |
228 | } |
221 | |
229 | |
|
|
230 | ############################################################################# |
|
|
231 | |
222 | package Crossfire::Client::Widget::Toplevel; |
232 | package Crossfire::Client::Widget::Toplevel; |
223 | |
233 | |
224 | our @ISA = Crossfire::Client::Widget::Container::; |
234 | our @ISA = Crossfire::Client::Widget::Container::; |
225 | |
235 | |
226 | sub update { |
236 | sub update { |
227 | my ($self) = @_; |
237 | my ($self) = @_; |
228 | |
238 | |
229 | ::refresh (); |
239 | ::refresh (); |
230 | } |
240 | } |
231 | |
241 | |
|
|
242 | sub add { |
|
|
243 | my ($self, $widget) = @_; |
|
|
244 | |
|
|
245 | $self->SUPER::add ($widget); |
|
|
246 | |
|
|
247 | $widget->size_allocate ($widget->size_request); |
|
|
248 | } |
|
|
249 | |
|
|
250 | ############################################################################# |
|
|
251 | |
232 | package Crossfire::Client::Widget::Window; |
252 | package Crossfire::Client::Widget::Window; |
233 | |
253 | |
234 | our @ISA = Crossfire::Client::Widget::Bin::; |
254 | our @ISA = Crossfire::Client::Widget::Bin::; |
235 | |
255 | |
236 | use SDL::OpenGL; |
256 | use SDL::OpenGL; |
237 | |
257 | |
238 | sub add { |
258 | sub new { |
239 | my ($self, $chld) = @_; |
259 | my ($class, $x, $y, $z, $w, $h) = @_; |
240 | warn "ADD $chld\n"; |
|
|
241 | $self->SUPER::add ($chld); |
|
|
242 | $chld->set_parent ($self); |
|
|
243 | } |
|
|
244 | |
260 | |
245 | sub remove { |
261 | my $self = $class->SUPER::new; |
246 | my ($self) = @_; |
262 | |
247 | # 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); |
248 | # $self->update; |
|
|
249 | } |
264 | } |
250 | |
265 | |
251 | sub update { |
266 | sub update { |
252 | my ($self) = @_; |
267 | my ($self) = @_; |
|
|
268 | |
253 | $self->render_chld; |
269 | $self->render_chld; |
|
|
270 | $self->SUPER::update; |
254 | } |
271 | } |
255 | |
272 | |
256 | sub render_chld { |
273 | sub render_chld { |
257 | my ($self) = @_; |
274 | my ($self) = @_; |
258 | my $chld = $self->get; |
|
|
259 | my ($w, $h) = $self->size_request; |
|
|
260 | |
275 | |
261 | require Carp; |
|
|
262 | Carp::cluck "RENDERCHI $w $h"; |
|
|
263 | warn "RENDERCHI $w $h\n"; |
|
|
264 | $self->{texture} = |
276 | $self->{texture} = |
265 | Crossfire::Client::Texture->new_from_opengl ( |
277 | Crossfire::Client::Texture->new_from_opengl ( |
266 | $w, $h, sub { $chld->draw } |
278 | $self->{w}, $self->{h}, sub { $self->child->draw } |
267 | ); |
279 | ); |
268 | $self->{texture}->upload; |
|
|
269 | } |
|
|
270 | |
|
|
271 | sub size_request { |
|
|
272 | my ($self) = @_; |
|
|
273 | ($self->w, $self->h) |
|
|
274 | } |
280 | } |
275 | |
281 | |
276 | sub size_allocate { |
282 | sub size_allocate { |
277 | my ($self, $w, $h) = @_; |
283 | my ($self, $w, $h) = @_; |
278 | |
284 | |
279 | $self->w ($w); |
285 | $self->{w} = $w; |
280 | $self->h ($h); |
286 | $self->{h} = $h; |
|
|
287 | |
281 | $self->get->size_allocate ($w, $h); |
288 | $self->child->size_allocate ($w, $h); |
282 | |
289 | |
283 | $self->update; #TODO: Move this to the size_request event propably? |
290 | $self->render_chld; |
284 | } |
291 | } |
285 | |
292 | |
286 | sub _draw { |
293 | sub _draw { |
287 | my ($self) = @_; |
294 | my ($self) = @_; |
288 | |
295 | |
… | |
… | |
305 | |
312 | |
306 | glDisable GL_BLEND; |
313 | glDisable GL_BLEND; |
307 | glDisable GL_TEXTURE_2D; |
314 | glDisable GL_TEXTURE_2D; |
308 | } |
315 | } |
309 | |
316 | |
|
|
317 | ############################################################################# |
|
|
318 | |
310 | package Crossfire::Client::Widget::Frame; |
319 | package Crossfire::Client::Widget::Frame; |
311 | |
320 | |
312 | our @ISA = Crossfire::Client::Widget::Bin::; |
321 | our @ISA = Crossfire::Client::Widget::Bin::; |
313 | |
322 | |
314 | use SDL::OpenGL; |
323 | use SDL::OpenGL; |
315 | |
324 | |
316 | sub size_request { |
325 | sub size_request { |
317 | my ($self) = @_; |
326 | my ($self) = @_; |
318 | my $chld = $self->get |
327 | my $chld = $self->child |
319 | or return (0, 0); |
328 | or return (0, 0); |
320 | |
329 | |
321 | $chld->move (2, 2); |
330 | $chld->move (2, 2); |
322 | |
331 | |
323 | map { $_ + 4 } $chld->size_request; |
332 | map { $_ + 4 } $chld->size_request; |
324 | } |
333 | } |
325 | |
334 | |
326 | sub size_allocate { |
335 | sub size_allocate { |
327 | my ($self, $w, $h) = @_; |
336 | my ($self, $w, $h) = @_; |
328 | |
337 | |
329 | $self->w ($w); |
338 | $self->{w} = $w; |
330 | $self->h ($h); |
339 | $self->{h} = $h; |
331 | |
340 | |
332 | $self->get->size_allocate ($w - 4, $h - 4); |
341 | $self->child->size_allocate ($w - 4, $h - 4); |
333 | $self->get->move (2, 2); |
342 | $self->child->move (2, 2); |
334 | } |
343 | } |
335 | |
344 | |
336 | sub _draw { |
345 | sub _draw { |
337 | my ($self) = @_; |
346 | my ($self) = @_; |
338 | |
347 | |
339 | my $chld = $self->get; |
348 | my $chld = $self->child; |
340 | |
349 | |
341 | my ($w, $h) = $chld->size_request; |
350 | my ($w, $h) = $chld->size_request; |
342 | |
351 | |
343 | glBegin GL_QUADS; |
352 | glBegin GL_QUADS; |
344 | glColor 0, 0, 0; |
353 | glColor 0, 0, 0; |
… | |
… | |
349 | glEnd; |
358 | glEnd; |
350 | |
359 | |
351 | $chld->draw; |
360 | $chld->draw; |
352 | } |
361 | } |
353 | |
362 | |
|
|
363 | ############################################################################# |
|
|
364 | |
354 | package Crossfire::Client::Widget::FancyFrame; |
365 | package Crossfire::Client::Widget::FancyFrame; |
355 | |
366 | |
356 | our @ISA = Crossfire::Client::Widget::Frame::; |
367 | our @ISA = Crossfire::Client::Widget::Bin::; |
357 | |
368 | |
358 | use SDL::OpenGL; |
369 | use SDL::OpenGL; |
359 | |
370 | |
360 | sub new { |
371 | my @tex = |
361 | my ($self, $theme) = @_; |
|
|
362 | $self = $self->SUPER::new; |
|
|
363 | |
|
|
364 | $self->{txts} = [ |
|
|
365 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
372 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
366 | 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); |
367 | ]; |
|
|
368 | $self |
|
|
369 | } |
|
|
370 | |
374 | |
371 | sub size_request { |
375 | sub size_request { |
372 | my ($self) = @_; |
376 | my ($self) = @_; |
|
|
377 | |
373 | my ($w, $h) = $self->get->size_request; |
378 | my ($w, $h) = $self->SUPER::size_request; |
374 | |
379 | |
375 | $h += $self->{txts}->[1]->{height}; |
380 | $h += $tex[1]->{height}; |
376 | $h += $self->{txts}->[4]->{height}; |
381 | $h += $tex[4]->{height}; |
377 | $w += $self->{txts}->[2]->{width}; |
382 | $w += $tex[2]->{width}; |
378 | $w += $self->{txts}->[3]->{width}; |
383 | $w += $tex[3]->{width}; |
379 | |
384 | |
380 | ($w, $h) |
385 | ($w, $h) |
381 | } |
386 | } |
382 | |
387 | |
383 | sub size_allocate { |
388 | sub size_allocate { |
384 | my ($self, $w, $h) = @_; |
389 | my ($self, $w, $h) = @_; |
385 | |
390 | |
386 | $self->w ($w); |
391 | $self->SUPER::size_allocate ($w, $h); |
387 | $self->h ($h); |
392 | |
388 | $h -= $self->{txts}->[1]->{height}; |
393 | $h -= $tex[1]->{height}; |
389 | $h -= $self->{txts}->[4]->{height}; |
394 | $h -= $tex[4]->{height}; |
390 | $w -= $self->{txts}->[2]->{width}; |
395 | $w -= $tex[2]->{width}; |
391 | $w -= $self->{txts}->[3]->{width}; |
396 | $w -= $tex[3]->{width}; |
392 | |
397 | |
393 | $h = $h < 0 ? 0 : $h; |
398 | $h = $h < 0 ? 0 : $h; |
394 | $w = $w < 0 ? 0 : $w; |
399 | $w = $w < 0 ? 0 : $w; |
395 | warn "CHILD:$w $h\n"; |
400 | warn "CHILD:$w $h\n"; |
396 | $self->get->size_allocate ($w, $h); |
401 | $self->child->size_allocate ($w, $h); |
397 | $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); |
402 | $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); |
398 | } |
403 | } |
399 | |
404 | |
400 | sub _draw { |
405 | sub _draw { |
401 | my ($self) = @_; |
406 | my ($self) = @_; |
402 | |
407 | |
403 | my ($w, $h) = ($self->w, $self->h); |
408 | my ($w, $h) = ($self->w, $self->h); |
404 | my ($cw, $ch) = ($self->get->w, $self->get->h); |
409 | my ($cw, $ch) = ($self->child->w, $self->child->h); |
405 | |
410 | |
406 | glEnable GL_BLEND; |
411 | glEnable GL_BLEND; |
407 | glEnable GL_TEXTURE_2D; |
412 | glEnable GL_TEXTURE_2D; |
408 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
413 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
409 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
414 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
410 | |
415 | |
411 | my $top = $self->{txts}->[1]; |
416 | my $top = $tex[1]; |
412 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
417 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
413 | |
418 | |
414 | glBegin GL_QUADS; |
419 | glBegin GL_QUADS; |
415 | glTexCoord 0, 0; glVertex 0 , 0; |
420 | glTexCoord 0, 0; glVertex 0 , 0; |
416 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
421 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
417 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
422 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
418 | glTexCoord 1, 0; glVertex $w , 0; |
423 | glTexCoord 1, 0; glVertex $w , 0; |
419 | glEnd; |
424 | glEnd; |
420 | |
425 | |
421 | my $left = $self->{txts}->[3]; |
426 | my $left = $tex[3]; |
422 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
427 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
423 | |
428 | |
424 | glBegin GL_QUADS; |
429 | glBegin GL_QUADS; |
425 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
430 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
426 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
431 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
427 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
432 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
428 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
433 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
429 | glEnd; |
434 | glEnd; |
430 | |
435 | |
431 | my $right = $self->{txts}->[2]; |
436 | my $right = $tex[2]; |
432 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
437 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
433 | |
438 | |
434 | glBegin GL_QUADS; |
439 | glBegin GL_QUADS; |
435 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
440 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
436 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
441 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
437 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
442 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
438 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
443 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
439 | glEnd; |
444 | glEnd; |
440 | |
445 | |
441 | my $bottom = $self->{txts}->[4]; |
446 | my $bottom = $tex[4]; |
442 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
447 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
443 | |
448 | |
444 | glBegin GL_QUADS; |
449 | glBegin GL_QUADS; |
445 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
450 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
446 | glTexCoord 0, 1; glVertex 0 , $h; |
451 | glTexCoord 0, 1; glVertex 0 , $h; |
447 | glTexCoord 1, 1; glVertex $w , $h; |
452 | glTexCoord 1, 1; glVertex $w , $h; |
448 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
453 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
449 | glEnd; |
454 | glEnd; |
450 | |
455 | |
451 | my $bg = $self->{txts}->[0]; |
456 | my $bg = $tex[0]; |
452 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
457 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
453 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
458 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
454 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
459 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
455 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
460 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
456 | |
461 | |
… | |
… | |
465 | glEnd; |
470 | glEnd; |
466 | |
471 | |
467 | glDisable GL_BLEND; |
472 | glDisable GL_BLEND; |
468 | glDisable GL_TEXTURE_2D; |
473 | glDisable GL_TEXTURE_2D; |
469 | |
474 | |
470 | $self->get->draw; |
475 | $self->child->draw; |
471 | |
476 | |
472 | } |
477 | } |
|
|
478 | |
|
|
479 | ############################################################################# |
473 | |
480 | |
474 | package Crossfire::Client::Widget::Table; |
481 | package Crossfire::Client::Widget::Table; |
475 | |
482 | |
476 | our @ISA = Crossfire::Client::Widget::Bin::; |
483 | our @ISA = Crossfire::Client::Widget::Bin::; |
477 | |
484 | |
… | |
… | |
553 | } |
560 | } |
554 | |
561 | |
555 | $y += $self->max_row_height ($yi); |
562 | $y += $self->max_row_height ($yi); |
556 | } |
563 | } |
557 | } |
564 | } |
|
|
565 | |
|
|
566 | ############################################################################# |
558 | |
567 | |
559 | package Crossfire::Client::Widget::VBox; |
568 | package Crossfire::Client::Widget::VBox; |
560 | |
569 | |
561 | our @ISA = Crossfire::Client::Widget::Container::; |
570 | our @ISA = Crossfire::Client::Widget::Container::; |
562 | |
571 | |
… | |
… | |
611 | $_->draw; |
620 | $_->draw; |
612 | $y += $_->h; |
621 | $y += $_->h; |
613 | } |
622 | } |
614 | } |
623 | } |
615 | |
624 | |
|
|
625 | ############################################################################# |
|
|
626 | |
616 | package Crossfire::Client::Widget::Label; |
627 | package Crossfire::Client::Widget::Label; |
617 | |
628 | |
618 | our @ISA = Crossfire::Client::Widget::; |
629 | our @ISA = Crossfire::Client::Widget::; |
619 | |
630 | |
620 | use SDL::OpenGL; |
631 | use SDL::OpenGL; |
… | |
… | |
676 | |
687 | |
677 | glDisable GL_BLEND; |
688 | glDisable GL_BLEND; |
678 | glDisable GL_TEXTURE_2D; |
689 | glDisable GL_TEXTURE_2D; |
679 | } |
690 | } |
680 | |
691 | |
|
|
692 | ############################################################################# |
|
|
693 | |
681 | package Crossfire::Client::Widget::TextEntry; |
694 | package Crossfire::Client::Widget::TextEntry; |
682 | |
695 | |
683 | our @ISA = Crossfire::Client::Widget::Label::; |
696 | our @ISA = Crossfire::Client::Widget::Label::; |
684 | |
697 | |
685 | use SDL; |
698 | use SDL; |
… | |
… | |
702 | } elsif ($uni) { |
715 | } elsif ($uni) { |
703 | $text .= chr $uni; |
716 | $text .= chr $uni; |
704 | } |
717 | } |
705 | $self->set_text ($text); |
718 | $self->set_text ($text); |
706 | } |
719 | } |
|
|
720 | |
|
|
721 | ############################################################################# |
707 | |
722 | |
708 | package Crossfire::Client::Widget::MapWidget; |
723 | package Crossfire::Client::Widget::MapWidget; |
709 | |
724 | |
710 | use strict; |
725 | use strict; |
711 | |
726 | |
… | |
… | |
878 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
893 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
879 | $::CONN->send ("command run_stop"); |
894 | $::CONN->send ("command run_stop"); |
880 | } |
895 | } |
881 | } |
896 | } |
882 | |
897 | |
|
|
898 | ############################################################################# |
|
|
899 | |
883 | package Crossfire::Client::Widget::Animator; |
900 | package Crossfire::Client::Widget::Animator; |
884 | |
901 | |
885 | use SDL::OpenGL; |
902 | use SDL::OpenGL; |
886 | |
903 | |
887 | our @ISA = Crossfire::Client::Widget::Bin::; |
904 | our @ISA = Crossfire::Client::Widget::Bin::; |