1 | package Crossfire::Client::Widget; |
1 | package CFClient::Widget; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
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 | |
9 | |
10 | use Crossfire::Client; |
10 | use CFClient; |
11 | |
11 | |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
13 | |
13 | |
14 | our $TOPLEVEL; |
14 | our $TOPLEVEL; |
15 | our $BUTTON_STATE; |
15 | our $BUTTON_STATE; |
… | |
… | |
34 | $GRAB->update if $GRAB; |
34 | $GRAB->update if $GRAB; |
35 | } |
35 | } |
36 | |
36 | |
37 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
37 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
38 | |
38 | |
39 | $GRAB->button_down ($ev) if $GRAB; |
39 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
40 | } |
40 | } |
41 | |
41 | |
42 | sub feed_sdl_button_up_event { |
42 | sub feed_sdl_button_up_event { |
43 | my ($ev) = @_; |
43 | my ($ev) = @_; |
44 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
44 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
45 | |
45 | |
46 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
46 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
47 | |
47 | |
48 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
48 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
|
|
49 | |
|
|
50 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
49 | |
51 | |
50 | if (!$BUTTON_STATE) { |
52 | if (!$BUTTON_STATE) { |
51 | my $grab = $GRAB; undef $GRAB; |
53 | my $grab = $GRAB; undef $GRAB; |
52 | $grab->update if $grab; |
54 | $grab->update if $grab; |
53 | $GRAB->update if $GRAB; |
55 | $GRAB->update if $GRAB; |
… | |
… | |
65 | |
67 | |
66 | $hover->update if $hover; |
68 | $hover->update if $hover; |
67 | $HOVER->update if $HOVER; |
69 | $HOVER->update if $HOVER; |
68 | } |
70 | } |
69 | |
71 | |
70 | $HOVER->mouse_motion ($ev) if $HOVER; |
72 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
71 | } |
73 | } |
72 | |
74 | |
73 | sub new { |
75 | sub new { |
74 | my $class = shift; |
76 | my $class = shift; |
75 | |
77 | |
76 | bless { @_ }, $class |
78 | bless { |
|
|
79 | x => 0, |
|
|
80 | y => 0, |
|
|
81 | z => 0, |
|
|
82 | @_ |
|
|
83 | }, $class |
77 | } |
84 | } |
78 | |
85 | |
79 | sub move { |
86 | sub move { |
80 | my ($self, $x, $y, $z) = @_; |
87 | my ($self, $x, $y, $z) = @_; |
81 | $self->{x} = $x; |
88 | $self->{x} = $x; |
… | |
… | |
95 | sub size_allocate { |
102 | sub size_allocate { |
96 | my ($self, $w, $h) = @_; |
103 | my ($self, $w, $h) = @_; |
97 | |
104 | |
98 | $self->{w} = $w; |
105 | $self->{w} = $w; |
99 | $self->{h} = $h; |
106 | $self->{h} = $h; |
|
|
107 | } |
|
|
108 | |
|
|
109 | # translate global koordinates to local coordinate system |
|
|
110 | sub translate { |
|
|
111 | my ($self, $x, $y) = @_; |
|
|
112 | |
|
|
113 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
100 | } |
114 | } |
101 | |
115 | |
102 | sub focus_in { |
116 | sub focus_in { |
103 | my ($self) = @_; |
117 | my ($self) = @_; |
104 | |
118 | |
… | |
… | |
201 | #$self->deactivate; |
215 | #$self->deactivate; |
202 | } |
216 | } |
203 | |
217 | |
204 | ############################################################################# |
218 | ############################################################################# |
205 | |
219 | |
206 | package Crossfire::Client::Widget::Container; |
220 | package CFClient::Widget::Container; |
207 | |
221 | |
208 | our @ISA = Crossfire::Client::Widget::; |
222 | our @ISA = CFClient::Widget::; |
209 | |
223 | |
210 | sub new { |
224 | sub new { |
211 | my ($class, @widgets) = @_; |
225 | my ($class, %arg) = @_; |
212 | |
226 | |
|
|
227 | my $children = delete $arg{children} || []; |
|
|
228 | |
213 | my $self = $class->SUPER::new (children => []); |
229 | my $self = $class->SUPER::new (children => [], %arg); |
214 | $self->add ($_) for @widgets; |
230 | $self->add ($_) for @$children; |
215 | |
231 | |
216 | $self |
232 | $self |
217 | } |
233 | } |
218 | |
234 | |
219 | sub add { |
235 | sub add { |
… | |
… | |
260 | $_->draw for @{$self->{children}}; |
276 | $_->draw for @{$self->{children}}; |
261 | } |
277 | } |
262 | |
278 | |
263 | ############################################################################# |
279 | ############################################################################# |
264 | |
280 | |
265 | package Crossfire::Client::Widget::Bin; |
281 | package CFClient::Widget::Bin; |
266 | |
282 | |
267 | our @ISA = Crossfire::Client::Widget::Container::; |
283 | our @ISA = CFClient::Widget::Container::; |
268 | |
284 | |
269 | sub child { $_[0]->{children}[0] } |
285 | sub child { $_[0]->{children}[0] } |
270 | |
286 | |
271 | sub size_request { |
287 | sub size_request { |
272 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
288 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
… | |
… | |
280 | if $self->{children}[0] |
296 | if $self->{children}[0] |
281 | } |
297 | } |
282 | |
298 | |
283 | ############################################################################# |
299 | ############################################################################# |
284 | |
300 | |
285 | package Crossfire::Client::Widget::Window; |
301 | package CFClient::Widget::Window; |
286 | |
302 | |
287 | our @ISA = Crossfire::Client::Widget::Bin::; |
303 | our @ISA = CFClient::Widget::Bin::; |
288 | |
304 | |
289 | use SDL::OpenGL; |
305 | use SDL::OpenGL; |
290 | |
306 | |
291 | sub new { |
307 | sub new { |
292 | my ($class, $x, $y, $z, $w, $h) = @_; |
308 | my ($class, %arg) = @_; |
293 | |
309 | |
294 | my $self = $class->SUPER::new; |
310 | my $self = $class->SUPER::new (%arg); |
295 | |
|
|
296 | @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); |
|
|
297 | } |
311 | } |
298 | |
312 | |
299 | sub update { |
313 | sub update { |
300 | my ($self) = @_; |
314 | my ($self) = @_; |
301 | |
315 | |
|
|
316 | # we want to do this delayed... |
302 | $self->render_chld; |
317 | $self->render_chld; |
303 | $self->SUPER::update; |
318 | $self->SUPER::update; |
304 | } |
319 | } |
305 | |
320 | |
306 | sub render_chld { |
321 | sub render_chld { |
307 | my ($self) = @_; |
322 | my ($self) = @_; |
308 | |
323 | |
309 | $self->{texture} = |
324 | $self->{texture} = |
310 | Crossfire::Client::Texture->new_from_opengl ( |
325 | CFClient::Texture->new_from_opengl ( |
311 | $self->{w}, $self->{h}, sub { $self->child->draw } |
326 | $self->{w}, $self->{h}, sub { $self->child->draw } |
312 | ); |
327 | ); |
313 | } |
328 | } |
314 | |
329 | |
315 | sub size_allocate { |
330 | sub size_allocate { |
… | |
… | |
332 | or return; |
347 | or return; |
333 | |
348 | |
334 | glEnable GL_BLEND; |
349 | glEnable GL_BLEND; |
335 | glEnable GL_TEXTURE_2D; |
350 | glEnable GL_TEXTURE_2D; |
336 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
351 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
337 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
338 | |
352 | |
339 | glBegin GL_QUADS; |
353 | $tex->draw_quad (0, 0, $w, $h); |
340 | glTexCoord 0, 0; glVertex 0, 0; |
|
|
341 | glTexCoord 0, 1; glVertex 0, $h; |
|
|
342 | glTexCoord 1, 1; glVertex $w, $h; |
|
|
343 | glTexCoord 1, 0; glVertex $w, 0; |
|
|
344 | glEnd; |
|
|
345 | |
354 | |
346 | glDisable GL_BLEND; |
355 | glDisable GL_BLEND; |
347 | glDisable GL_TEXTURE_2D; |
356 | glDisable GL_TEXTURE_2D; |
348 | } |
357 | } |
349 | |
358 | |
350 | ############################################################################# |
359 | ############################################################################# |
351 | |
360 | |
352 | package Crossfire::Client::Widget::Frame; |
361 | package CFClient::Widget::Frame; |
353 | |
362 | |
354 | our @ISA = Crossfire::Client::Widget::Bin::; |
363 | our @ISA = CFClient::Widget::Bin::; |
355 | |
364 | |
356 | use SDL::OpenGL; |
365 | use SDL::OpenGL; |
357 | |
366 | |
358 | sub size_request { |
367 | sub size_request { |
359 | my ($self) = @_; |
368 | my ($self) = @_; |
… | |
… | |
382 | |
391 | |
383 | my ($w, $h) = $chld->size_request; |
392 | my ($w, $h) = $chld->size_request; |
384 | |
393 | |
385 | glBegin GL_QUADS; |
394 | glBegin GL_QUADS; |
386 | glColor 0, 0, 0; |
395 | glColor 0, 0, 0; |
387 | glTexCoord 0, 0; glVertex 0 , 0; |
396 | glVertex 0 , 0; |
388 | glTexCoord 0, 1; glVertex 0 , $h + 4; |
397 | glVertex 0 , $h + 4; |
389 | glTexCoord 1, 1; glVertex $w + 4 , $h + 4; |
398 | glVertex $w + 4 , $h + 4; |
390 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
399 | glVertex $w + 4 , 0; |
391 | glEnd; |
400 | glEnd; |
392 | |
401 | |
393 | $chld->draw; |
402 | $chld->draw; |
394 | } |
403 | } |
395 | |
404 | |
396 | ############################################################################# |
405 | ############################################################################# |
397 | |
406 | |
398 | package Crossfire::Client::Widget::FancyFrame; |
407 | package CFClient::Widget::FancyFrame; |
399 | |
408 | |
400 | our @ISA = Crossfire::Client::Widget::Bin::; |
409 | our @ISA = CFClient::Widget::Bin::; |
401 | |
410 | |
402 | use SDL::OpenGL; |
411 | use SDL::OpenGL; |
403 | |
412 | |
404 | my @tex = |
413 | my @tex = |
405 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
414 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
406 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
415 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
407 | |
416 | |
408 | sub size_request { |
417 | sub size_request { |
409 | my ($self) = @_; |
418 | my ($self) = @_; |
410 | |
419 | |
… | |
… | |
445 | glEnable GL_TEXTURE_2D; |
454 | glEnable GL_TEXTURE_2D; |
446 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
455 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
447 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
456 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
448 | |
457 | |
449 | my $top = $tex[1]; |
458 | my $top = $tex[1]; |
450 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
459 | $top->draw_quad (0, 0, $w, $top->{height}); |
451 | |
|
|
452 | glBegin GL_QUADS; |
|
|
453 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
454 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
|
|
455 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
|
|
456 | glTexCoord 1, 0; glVertex $w , 0; |
|
|
457 | glEnd; |
|
|
458 | |
460 | |
459 | my $left = $tex[3]; |
461 | my $left = $tex[3]; |
460 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
462 | $left->draw_quad (0, $top->{height}, $left->{width}, $ch); |
461 | |
|
|
462 | glBegin GL_QUADS; |
|
|
463 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
|
|
464 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
|
|
465 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
|
|
466 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
|
|
467 | glEnd; |
|
|
468 | |
463 | |
469 | my $right = $tex[2]; |
464 | my $right = $tex[2]; |
470 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
465 | $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); |
471 | |
|
|
472 | glBegin GL_QUADS; |
|
|
473 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
|
|
474 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
|
|
475 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
|
|
476 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
|
|
477 | glEnd; |
|
|
478 | |
466 | |
479 | my $bottom = $tex[4]; |
467 | my $bottom = $tex[4]; |
480 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
468 | $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); |
481 | |
|
|
482 | glBegin GL_QUADS; |
|
|
483 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
|
|
484 | glTexCoord 0, 1; glVertex 0 , $h; |
|
|
485 | glTexCoord 1, 1; glVertex $w , $h; |
|
|
486 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
|
|
487 | glEnd; |
|
|
488 | |
469 | |
489 | my $bg = $tex[0]; |
470 | my $bg = $tex[0]; |
490 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
471 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
491 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
472 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
492 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
473 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
493 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
474 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
494 | |
475 | |
495 | my $rep_x = $cw / $bg->{width}; |
476 | my $rep_x = $cw / $bg->{width}; |
496 | my $rep_y = $ch / $bg->{height}; |
477 | my $rep_y = $ch / $bg->{height}; |
497 | |
478 | |
498 | glBegin GL_QUADS; |
479 | $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); |
499 | glTexCoord 0, 0; glVertex $left->{width}, $top->{height}; |
|
|
500 | glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch; |
|
|
501 | glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch; |
|
|
502 | glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height}; |
|
|
503 | glEnd; |
|
|
504 | |
480 | |
505 | glDisable GL_BLEND; |
481 | glDisable GL_BLEND; |
506 | glDisable GL_TEXTURE_2D; |
482 | glDisable GL_TEXTURE_2D; |
507 | |
483 | |
508 | $self->child->draw; |
484 | $self->child->draw; |
509 | |
485 | |
510 | } |
486 | } |
511 | |
487 | |
512 | ############################################################################# |
488 | ############################################################################# |
513 | |
489 | |
514 | package Crossfire::Client::Widget::Table; |
490 | package CFClient::Widget::Table; |
515 | |
491 | |
516 | our @ISA = Crossfire::Client::Widget::Bin::; |
492 | our @ISA = CFClient::Widget::Bin::; |
517 | |
493 | |
518 | use SDL::OpenGL; |
494 | use SDL::OpenGL; |
519 | |
495 | |
520 | sub add { |
496 | sub add { |
521 | my ($self, $x, $y, $chld) = @_; |
497 | my ($self, $x, $y, $chld) = @_; |
… | |
… | |
596 | } |
572 | } |
597 | } |
573 | } |
598 | |
574 | |
599 | ############################################################################# |
575 | ############################################################################# |
600 | |
576 | |
601 | package Crossfire::Client::Widget::VBox; |
577 | package CFClient::Widget::VBox; |
602 | |
578 | |
603 | our @ISA = Crossfire::Client::Widget::Container::; |
579 | our @ISA = CFClient::Widget::Container::; |
604 | |
580 | |
605 | use SDL::OpenGL; |
581 | use SDL::OpenGL; |
606 | |
582 | |
607 | sub size_request { |
583 | sub size_request { |
608 | my ($self) = @_; |
584 | my ($self) = @_; |
… | |
… | |
656 | } |
632 | } |
657 | } |
633 | } |
658 | |
634 | |
659 | ############################################################################# |
635 | ############################################################################# |
660 | |
636 | |
661 | package Crossfire::Client::Widget::Label; |
637 | package CFClient::Widget::Label; |
662 | |
638 | |
663 | our @ISA = Crossfire::Client::Widget::; |
639 | our @ISA = CFClient::Widget::; |
664 | |
640 | |
665 | use SDL::OpenGL; |
641 | use SDL::OpenGL; |
666 | |
642 | |
667 | sub new { |
643 | sub new { |
668 | my ($class, $x, $y, $z, $height, $text) = @_; |
644 | my ($class, %arg) = @_; |
669 | |
|
|
670 | $height ||= $::FONTSIZE; |
|
|
671 | |
645 | |
672 | # TODO: color, and make height, xyz etc. optional |
646 | # TODO: color, and make height, xyz etc. optional |
673 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
647 | my $self = $class->SUPER::new ( |
|
|
648 | color => [1, 1, 1], |
|
|
649 | height => $::FONTSIZE, |
|
|
650 | text => "", |
|
|
651 | layout => new CFClient::Layout, |
|
|
652 | %arg |
|
|
653 | ); |
674 | |
654 | |
675 | $self->set_text ($text); |
655 | $self->set_text ($self->{text}); |
676 | |
656 | |
677 | $self |
657 | $self |
678 | } |
658 | } |
679 | |
659 | |
680 | sub set_text { |
660 | sub set_text { |
681 | my ($self, $text) = @_; |
661 | my ($self, $text) = @_; |
682 | |
662 | |
683 | $self->{text} = $text; |
663 | $self->{text} = $text; |
684 | $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; |
664 | $self->{layout}->set_markup ($text); |
685 | |
665 | |
686 | $self->update; |
666 | delete $self->{texture}; |
687 | } |
667 | } |
688 | |
668 | |
689 | sub get_text { |
669 | sub get_text { |
690 | my ($self, $text) = @_; |
670 | my ($self, $text) = @_; |
691 | |
671 | |
… | |
… | |
693 | } |
673 | } |
694 | |
674 | |
695 | sub size_request { |
675 | sub size_request { |
696 | my ($self) = @_; |
676 | my ($self) = @_; |
697 | |
677 | |
|
|
678 | $self->{layout}->set_width; |
|
|
679 | $self->{layout}->set_height ($self->{height}); |
|
|
680 | $self->{layout}->size |
698 | if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
681 | # if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
699 | ( |
682 | # ( |
700 | $self->{texture}{width}, |
683 | # $self->{texture}{width}, |
701 | $self->{texture}{height}, |
684 | # $self->{texture}{height}, |
702 | ) |
685 | # ) |
703 | } else { |
686 | # } else { |
704 | my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; |
687 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height}; |
705 | |
688 | # |
706 | ($w, $h) |
689 | # ($w, $h) |
707 | } |
690 | # } |
|
|
691 | } |
708 | |
692 | |
|
|
693 | sub size_allocate { |
|
|
694 | my ($self, $w, $h) = @_; |
|
|
695 | |
|
|
696 | $self->SUPER::size_allocate ($w, $h); |
|
|
697 | delete $self->{texture}; |
709 | } |
698 | } |
710 | |
699 | |
711 | sub _draw { |
700 | sub _draw { |
712 | my ($self) = @_; |
701 | my ($self) = @_; |
713 | |
702 | |
714 | my $tex = $self->{texture}; |
703 | my $tex = $self->{texture} ||= do { |
|
|
704 | $self->{layout}->set_width ($self->{w}); |
|
|
705 | new_from_layout CFClient::Texture $self->{layout}; |
|
|
706 | }; |
715 | |
707 | |
716 | glEnable GL_BLEND; |
708 | glEnable GL_BLEND; |
717 | glEnable GL_TEXTURE_2D; |
709 | glEnable GL_TEXTURE_2D; |
718 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
710 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
719 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
711 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
720 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
721 | |
712 | |
722 | glColor 1, 0, 0, 1; # TODO color |
713 | glColor @{$self->{color}}; |
723 | |
714 | |
724 | glBegin GL_QUADS; |
715 | $tex->draw_quad (0, 0); |
725 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
726 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
|
|
727 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
|
|
728 | glTexCoord 1, 0; glVertex $tex->{width}, 0; |
|
|
729 | glEnd; |
|
|
730 | |
716 | |
731 | glDisable GL_BLEND; |
717 | glDisable GL_BLEND; |
732 | glDisable GL_TEXTURE_2D; |
718 | glDisable GL_TEXTURE_2D; |
733 | } |
719 | } |
734 | |
720 | |
735 | ############################################################################# |
721 | ############################################################################# |
736 | |
722 | |
737 | package Crossfire::Client::Widget::Entry; |
723 | package CFClient::Widget::Entry; |
738 | |
724 | |
739 | our @ISA = Crossfire::Client::Widget::Label::; |
725 | our @ISA = CFClient::Widget::Label::; |
740 | |
726 | |
741 | use SDL; |
727 | use SDL; |
742 | use SDL::OpenGL; |
728 | use SDL::OpenGL; |
743 | |
729 | |
744 | sub key_down { |
730 | sub key_down { |
… | |
… | |
753 | |
739 | |
754 | if ($sym == SDLK_BACKSPACE) { |
740 | if ($sym == SDLK_BACKSPACE) { |
755 | substr $text, -1, 1, ''; |
741 | substr $text, -1, 1, ''; |
756 | } elsif ($uni) { |
742 | } elsif ($uni) { |
757 | $text .= chr $uni; |
743 | $text .= chr $uni; |
758 | print "$uni <$text>\n";#d# |
|
|
759 | } |
744 | } |
760 | |
745 | |
761 | $self->set_text ($text); |
746 | $self->set_text ($text); |
762 | } |
747 | } |
763 | |
748 | |
764 | sub button_down { |
749 | sub button_down { |
765 | my ($self, $ev) = @_; |
750 | my ($self, $ev) = @_; |
766 | |
751 | |
767 | $self->focus_in; |
752 | $self->focus_in; |
|
|
753 | } |
|
|
754 | |
|
|
755 | sub mouse_motion { |
|
|
756 | my ($self, $ev, $x, $y) = @_; |
|
|
757 | printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
768 | } |
758 | } |
769 | |
759 | |
770 | sub _draw { |
760 | sub _draw { |
771 | my ($self) = @_; |
761 | my ($self) = @_; |
772 | |
762 | |
… | |
… | |
786 | $self->SUPER::_draw; |
776 | $self->SUPER::_draw; |
787 | } |
777 | } |
788 | |
778 | |
789 | ############################################################################# |
779 | ############################################################################# |
790 | |
780 | |
791 | package Crossfire::Client::Widget::MapWidget; |
781 | package CFClient::Widget::MapWidget; |
792 | |
782 | |
793 | use strict; |
783 | use strict; |
794 | |
784 | |
795 | use List::Util qw(min max); |
785 | use List::Util qw(min max); |
796 | |
786 | |
797 | use SDL; |
787 | use SDL; |
798 | use SDL::OpenGL; |
788 | use SDL::OpenGL; |
799 | use SDL::OpenGL::Constants; |
789 | use SDL::OpenGL::Constants; |
800 | |
790 | |
801 | our @ISA = Crossfire::Client::Widget::; |
791 | our @ISA = CFClient::Widget::; |
|
|
792 | |
|
|
793 | sub new { |
|
|
794 | my $class = shift; |
|
|
795 | |
|
|
796 | $class->SUPER::new ( |
|
|
797 | z => -1, |
|
|
798 | list => (glGenLists 1), |
|
|
799 | @_ |
|
|
800 | ) |
|
|
801 | } |
802 | |
802 | |
803 | sub key_down { |
803 | sub key_down { |
804 | print "MAPKEYDOWN\n"; |
804 | print "MAPKEYDOWN\n"; |
805 | } |
805 | } |
806 | |
806 | |
… | |
… | |
812 | 1 + int $::WIDTH / 32, |
812 | 1 + int $::WIDTH / 32, |
813 | 1 + int $::HEIGHT / 32, |
813 | 1 + int $::HEIGHT / 32, |
814 | ) |
814 | ) |
815 | } |
815 | } |
816 | |
816 | |
|
|
817 | sub update { |
|
|
818 | my ($self) = @_; |
|
|
819 | |
|
|
820 | $self->{need_update} = 1; |
|
|
821 | } |
|
|
822 | |
817 | sub _draw { |
823 | sub _draw { |
818 | my ($self) = @_; |
824 | my ($self) = @_; |
819 | |
825 | |
|
|
826 | if (delete $self->{need_update}) { |
|
|
827 | glNewList $self->{list}, GL_COMPILE; |
|
|
828 | |
820 | my $mx = $::CONN->{mapx}; |
829 | my $mx = $::CONN->{mapx}; |
821 | my $my = $::CONN->{mapy}; |
830 | my $my = $::CONN->{mapy}; |
822 | |
831 | |
823 | my $map = $::CONN->{map}; |
832 | my $map = $::CONN->{map}; |
824 | |
833 | |
825 | my ($xofs, $yofs); |
834 | my ($xofs, $yofs); |
826 | |
835 | |
827 | my $sw = 1 + int $::WIDTH / 32; |
836 | my $sw = 1 + int $::WIDTH / 32; |
828 | my $sh = 1 + int $::HEIGHT / 32; |
837 | my $sh = 1 + int $::HEIGHT / 32; |
829 | |
838 | |
830 | if ($::CONN->{mapw} > $sw) { |
839 | if ($::CONN->{mapw} > $sw) { |
831 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
840 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
832 | } else { |
841 | } else { |
833 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
842 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
834 | } |
843 | } |
835 | |
844 | |
836 | if ($::CONN->{maph} > $sh) { |
845 | if ($::CONN->{maph} > $sh) { |
837 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
846 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
838 | } else { |
847 | } else { |
839 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
848 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
840 | } |
849 | } |
841 | |
850 | |
842 | glEnable GL_TEXTURE_2D; |
851 | glEnable GL_TEXTURE_2D; |
843 | glEnable GL_BLEND; |
852 | glEnable GL_BLEND; |
844 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
853 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
845 | |
854 | |
846 | my $sw4 = ($sw + 3) & ~3; |
855 | my $sw4 = ($sw + 3) & ~3; |
847 | my $lighting = "\x00" x ($sw4 * $sh); |
856 | my $darkness = "\x00" x ($sw4 * $sh); |
848 | |
857 | |
849 | for my $x (0 .. $sw - 1) { |
858 | for my $x (0 .. $sw - 1) { |
|
|
859 | my $row = $map->[$x + $xofs]; |
850 | for my $y (0 .. $sh - 1) { |
860 | for my $y (0 .. $sh - 1) { |
851 | |
861 | |
852 | my $cell = $map->[$x + $xofs][$y + $yofs] |
862 | my $cell = $row->[$y + $yofs] |
853 | or next; |
863 | or next; |
854 | |
864 | |
855 | my $darkness = $cell->[0] * (1 / 255); |
865 | my $dark = $cell->[0]; |
856 | if ($darkness < 0) { |
866 | if ($dark < 0) { |
857 | $darkness = $cell->[1] ? 0.1 : 0; |
867 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
|
|
868 | } else { |
|
|
869 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
858 | } |
870 | } |
859 | substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; |
|
|
860 | |
871 | |
861 | for my $num (grep $_, @$cell[1,2,3]) { |
872 | for my $num (grep $_, @$cell[1,2,3]) { |
862 | my $tex = $::CONN->{face}[$num]{texture} || next; |
873 | my $tex = $::CONN->{face}[$num]{texture} || next; |
863 | |
874 | |
864 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
865 | |
|
|
866 | my $w = $tex->{width}; |
875 | my $w = $tex->{width}; |
867 | my $h = $tex->{height}; |
876 | my $h = $tex->{height}; |
868 | |
877 | |
869 | my $px = ($x + 1) * 32 - $w; |
878 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
870 | my $py = ($y + 1) * 32 - $h; |
|
|
871 | |
|
|
872 | glBegin GL_QUADS; |
|
|
873 | glTexCoord 0, 0; glVertex $px , $py; |
|
|
874 | glTexCoord 0, 1; glVertex $px , $py + $h; |
|
|
875 | glTexCoord 1, 1; glVertex $px + $w, $py + $h; |
|
|
876 | glTexCoord 1, 0; glVertex $px + $w, $py; |
|
|
877 | glEnd; |
879 | } |
878 | } |
880 | } |
879 | } |
881 | } |
880 | } |
|
|
881 | |
882 | |
882 | # if (1) { # higher quality darkness |
883 | # if (1) { # higher quality darkness |
883 | # $lighting =~ s/(.)/$1$1$1/gs; |
884 | # $lighting =~ s/(.)/$1$1$1/gs; |
884 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
885 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
885 | # |
886 | # |
… | |
… | |
887 | # |
888 | # |
888 | # $lighting = $pb->get_pixels; |
889 | # $lighting = $pb->get_pixels; |
889 | # $lighting =~ s/(.)../$1/gs; |
890 | # $lighting =~ s/(.)../$1/gs; |
890 | # } |
891 | # } |
891 | |
892 | |
892 | $lighting = new Crossfire::Client::Texture |
|
|
893 | width => $sw4, |
|
|
894 | height => $sh, |
|
|
895 | data => $lighting, |
|
|
896 | internalformat => GL_ALPHA, |
|
|
897 | format => GL_ALPHA; |
|
|
898 | |
|
|
899 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
893 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
900 | glColor 0.45, 0.45, 0.45, 1; |
|
|
901 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
894 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
902 | glBindTexture GL_TEXTURE_2D, $lighting->{name}; |
|
|
903 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
904 | glBegin GL_QUADS; |
|
|
905 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
906 | glTexCoord 0, 1; glVertex 0 , $sh * 32; |
|
|
907 | glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32; |
|
|
908 | glTexCoord 1, 0; glVertex $sw4 * 32, 0; |
|
|
909 | glEnd; |
|
|
910 | |
895 | |
|
|
896 | $darkness = new CFClient::Texture |
|
|
897 | width => $sw4, |
|
|
898 | height => $sh, |
|
|
899 | data => $darkness, |
|
|
900 | internalformat => GL_ALPHA, |
|
|
901 | format => GL_ALPHA; |
|
|
902 | |
|
|
903 | glColor 0.45, 0.45, 0.45, 1; |
|
|
904 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
|
|
905 | |
911 | glDisable GL_TEXTURE_2D; |
906 | glDisable GL_TEXTURE_2D; |
912 | glDisable GL_BLEND; |
907 | glDisable GL_BLEND; |
|
|
908 | |
|
|
909 | glEndList; |
|
|
910 | } |
|
|
911 | |
|
|
912 | glCallList $self->{list}; |
913 | } |
913 | } |
914 | |
914 | |
915 | my %DIR = ( |
915 | my %DIR = ( |
916 | SDLK_KP8, [1, "north"], |
916 | SDLK_KP8, [1, "north"], |
917 | SDLK_KP9, [2, "northeast"], |
917 | SDLK_KP9, [2, "northeast"], |
… | |
… | |
963 | } |
963 | } |
964 | } |
964 | } |
965 | |
965 | |
966 | ############################################################################# |
966 | ############################################################################# |
967 | |
967 | |
968 | package Crossfire::Client::Widget::Animator; |
968 | package CFClient::Widget::Animator; |
969 | |
969 | |
970 | use SDL::OpenGL; |
970 | use SDL::OpenGL; |
971 | |
971 | |
972 | our @ISA = Crossfire::Client::Widget::Bin::; |
972 | our @ISA = CFClient::Widget::Bin::; |
973 | |
973 | |
974 | sub moveto { |
974 | sub moveto { |
975 | my ($self, $x, $y) = @_; |
975 | my ($self, $x, $y) = @_; |
976 | |
976 | |
977 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
977 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
978 | $self->{speed} = 2; |
978 | $self->{speed} = 0.001; |
979 | $self->{time} = 1; |
979 | $self->{time} = 1; |
980 | |
980 | |
981 | ::animation_start $self; |
981 | ::animation_start $self; |
982 | } |
982 | } |
983 | |
983 | |
… | |
… | |
1005 | glPopMatrix; |
1005 | glPopMatrix; |
1006 | } |
1006 | } |
1007 | |
1007 | |
1008 | ############################################################################# |
1008 | ############################################################################# |
1009 | |
1009 | |
1010 | package Crossfire::Client::Widget::Toplevel; |
1010 | package CFClient::Widget::Toplevel; |
1011 | |
1011 | |
1012 | our @ISA = Crossfire::Client::Widget::Container::; |
1012 | our @ISA = CFClient::Widget::Container::; |
1013 | |
1013 | |
1014 | sub size_request { |
1014 | sub size_request { |
1015 | ($::WIDTH, $::HEIGHT) |
1015 | ($::WIDTH, $::HEIGHT) |
1016 | } |
1016 | } |
1017 | |
1017 | |
… | |
… | |
1022 | |
1022 | |
1023 | $_->size_allocate ($_->size_request) |
1023 | $_->size_allocate ($_->size_request) |
1024 | for @{$self->{children}}; |
1024 | for @{$self->{children}}; |
1025 | } |
1025 | } |
1026 | |
1026 | |
|
|
1027 | sub translate { |
|
|
1028 | my ($self, $x, $y) = @_; |
|
|
1029 | |
|
|
1030 | ($x, $y) |
|
|
1031 | } |
|
|
1032 | |
1027 | sub update { |
1033 | sub update { |
1028 | my ($self) = @_; |
1034 | my ($self) = @_; |
1029 | |
1035 | |
1030 | $self->size_allocate ($self->size_request); |
1036 | $self->size_allocate ($self->size_request); |
1031 | ::refresh (); |
1037 | ::refresh (); |
… | |
… | |
1045 | $self->_draw; |
1051 | $self->_draw; |
1046 | } |
1052 | } |
1047 | |
1053 | |
1048 | ############################################################################# |
1054 | ############################################################################# |
1049 | |
1055 | |
1050 | package Crossfire::Client::Widget; |
1056 | package CFClient::Widget; |
1051 | |
1057 | |
1052 | $TOPLEVEL = new Crossfire::Client::Widget::Toplevel; |
1058 | $TOPLEVEL = new CFClient::Widget::Toplevel; |
1053 | |
1059 | |
1054 | 1 |
1060 | 1 |
1055 | |
1061 | |