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; |
… | |
… | |
210 | #$self->deactivate; |
210 | #$self->deactivate; |
211 | } |
211 | } |
212 | |
212 | |
213 | ############################################################################# |
213 | ############################################################################# |
214 | |
214 | |
215 | package Crossfire::Client::Widget::Container; |
215 | package CFClient::Widget::Container; |
216 | |
216 | |
217 | our @ISA = Crossfire::Client::Widget::; |
217 | our @ISA = CFClient::Widget::; |
218 | |
218 | |
219 | sub new { |
219 | sub new { |
220 | my ($class, @widgets) = @_; |
220 | my ($class, %arg) = @_; |
|
|
221 | |
|
|
222 | my $children = delete $arg{children} || []; |
221 | |
223 | |
222 | my $self = $class->SUPER::new (children => []); |
224 | my $self = $class->SUPER::new (children => []); |
223 | $self->add ($_) for @widgets; |
225 | $self->add ($_) for @$children; |
224 | |
226 | |
225 | $self |
227 | $self |
226 | } |
228 | } |
227 | |
229 | |
228 | sub add { |
230 | sub add { |
… | |
… | |
269 | $_->draw for @{$self->{children}}; |
271 | $_->draw for @{$self->{children}}; |
270 | } |
272 | } |
271 | |
273 | |
272 | ############################################################################# |
274 | ############################################################################# |
273 | |
275 | |
274 | package Crossfire::Client::Widget::Bin; |
276 | package CFClient::Widget::Bin; |
275 | |
277 | |
276 | our @ISA = Crossfire::Client::Widget::Container::; |
278 | our @ISA = CFClient::Widget::Container::; |
277 | |
279 | |
278 | sub child { $_[0]->{children}[0] } |
280 | sub child { $_[0]->{children}[0] } |
279 | |
281 | |
280 | sub size_request { |
282 | sub size_request { |
281 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
283 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
… | |
… | |
289 | if $self->{children}[0] |
291 | if $self->{children}[0] |
290 | } |
292 | } |
291 | |
293 | |
292 | ############################################################################# |
294 | ############################################################################# |
293 | |
295 | |
294 | package Crossfire::Client::Widget::Window; |
296 | package CFClient::Widget::Window; |
295 | |
297 | |
296 | our @ISA = Crossfire::Client::Widget::Bin::; |
298 | our @ISA = CFClient::Widget::Bin::; |
297 | |
299 | |
298 | use SDL::OpenGL; |
300 | use SDL::OpenGL; |
299 | |
301 | |
300 | sub new { |
302 | sub new { |
301 | my ($class, $x, $y, $z, $w, $h) = @_; |
303 | my ($class, %arg) = @_; |
302 | |
304 | |
303 | my $self = $class->SUPER::new; |
305 | my $self = $class->SUPER::new (%arg); |
304 | |
|
|
305 | @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); |
|
|
306 | } |
306 | } |
307 | |
307 | |
308 | sub update { |
308 | sub update { |
309 | my ($self) = @_; |
309 | my ($self) = @_; |
310 | |
310 | |
|
|
311 | # we want to do this delayed... |
311 | $self->render_chld; |
312 | $self->render_chld; |
312 | $self->SUPER::update; |
313 | $self->SUPER::update; |
313 | } |
314 | } |
314 | |
315 | |
315 | sub render_chld { |
316 | sub render_chld { |
316 | my ($self) = @_; |
317 | my ($self) = @_; |
317 | |
318 | |
318 | $self->{texture} = |
319 | $self->{texture} = |
319 | Crossfire::Client::Texture->new_from_opengl ( |
320 | CFClient::Texture->new_from_opengl ( |
320 | $self->{w}, $self->{h}, sub { $self->child->draw } |
321 | $self->{w}, $self->{h}, sub { $self->child->draw } |
321 | ); |
322 | ); |
322 | } |
323 | } |
323 | |
324 | |
324 | sub size_allocate { |
325 | sub size_allocate { |
… | |
… | |
350 | glDisable GL_TEXTURE_2D; |
351 | glDisable GL_TEXTURE_2D; |
351 | } |
352 | } |
352 | |
353 | |
353 | ############################################################################# |
354 | ############################################################################# |
354 | |
355 | |
355 | package Crossfire::Client::Widget::Frame; |
356 | package CFClient::Widget::Frame; |
356 | |
357 | |
357 | our @ISA = Crossfire::Client::Widget::Bin::; |
358 | our @ISA = CFClient::Widget::Bin::; |
358 | |
359 | |
359 | use SDL::OpenGL; |
360 | use SDL::OpenGL; |
360 | |
361 | |
361 | sub size_request { |
362 | sub size_request { |
362 | my ($self) = @_; |
363 | my ($self) = @_; |
… | |
… | |
396 | $chld->draw; |
397 | $chld->draw; |
397 | } |
398 | } |
398 | |
399 | |
399 | ############################################################################# |
400 | ############################################################################# |
400 | |
401 | |
401 | package Crossfire::Client::Widget::FancyFrame; |
402 | package CFClient::Widget::FancyFrame; |
402 | |
403 | |
403 | our @ISA = Crossfire::Client::Widget::Bin::; |
404 | our @ISA = CFClient::Widget::Bin::; |
404 | |
405 | |
405 | use SDL::OpenGL; |
406 | use SDL::OpenGL; |
406 | |
407 | |
407 | my @tex = |
408 | my @tex = |
408 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
409 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
409 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
410 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
410 | |
411 | |
411 | sub size_request { |
412 | sub size_request { |
412 | my ($self) = @_; |
413 | my ($self) = @_; |
413 | |
414 | |
… | |
… | |
479 | |
480 | |
480 | } |
481 | } |
481 | |
482 | |
482 | ############################################################################# |
483 | ############################################################################# |
483 | |
484 | |
484 | package Crossfire::Client::Widget::Table; |
485 | package CFClient::Widget::Table; |
485 | |
486 | |
486 | our @ISA = Crossfire::Client::Widget::Bin::; |
487 | our @ISA = CFClient::Widget::Bin::; |
487 | |
488 | |
488 | use SDL::OpenGL; |
489 | use SDL::OpenGL; |
489 | |
490 | |
490 | sub add { |
491 | sub add { |
491 | my ($self, $x, $y, $chld) = @_; |
492 | my ($self, $x, $y, $chld) = @_; |
… | |
… | |
566 | } |
567 | } |
567 | } |
568 | } |
568 | |
569 | |
569 | ############################################################################# |
570 | ############################################################################# |
570 | |
571 | |
571 | package Crossfire::Client::Widget::VBox; |
572 | package CFClient::Widget::VBox; |
572 | |
573 | |
573 | our @ISA = Crossfire::Client::Widget::Container::; |
574 | our @ISA = CFClient::Widget::Container::; |
574 | |
575 | |
575 | use SDL::OpenGL; |
576 | use SDL::OpenGL; |
576 | |
577 | |
577 | sub size_request { |
578 | sub size_request { |
578 | my ($self) = @_; |
579 | my ($self) = @_; |
… | |
… | |
626 | } |
627 | } |
627 | } |
628 | } |
628 | |
629 | |
629 | ############################################################################# |
630 | ############################################################################# |
630 | |
631 | |
631 | package Crossfire::Client::Widget::Label; |
632 | package CFClient::Widget::Label; |
632 | |
633 | |
633 | our @ISA = Crossfire::Client::Widget::; |
634 | our @ISA = CFClient::Widget::; |
634 | |
635 | |
635 | use SDL::OpenGL; |
636 | use SDL::OpenGL; |
636 | |
637 | |
637 | sub new { |
638 | sub new { |
638 | my ($class, $x, $y, $z, $height, $text) = @_; |
639 | my ($class, %arg) = @_; |
639 | |
|
|
640 | $height ||= $::FONTSIZE; |
|
|
641 | |
640 | |
642 | # TODO: color, and make height, xyz etc. optional |
641 | # TODO: color, and make height, xyz etc. optional |
643 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
642 | my $self = $class->SUPER::new ( |
|
|
643 | color => [1, 1, 1], |
|
|
644 | height => $::FONTSIZE, |
|
|
645 | text => "", |
|
|
646 | layout => new CFClient::Layout, |
|
|
647 | %arg |
|
|
648 | ); |
644 | |
649 | |
645 | $self->set_text ($text); |
650 | $self->set_text ($self->{text}); |
646 | |
651 | |
647 | $self |
652 | $self |
648 | } |
653 | } |
649 | |
654 | |
650 | sub set_text { |
655 | sub set_text { |
651 | my ($self, $text) = @_; |
656 | my ($self, $text) = @_; |
652 | |
657 | |
653 | $self->{text} = $text; |
658 | $self->{text} = $text; |
654 | $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; |
659 | $self->{layout}->set_markup ($text); |
655 | |
660 | |
656 | $self->update; |
661 | delete $self->{texture}; |
657 | } |
662 | } |
658 | |
663 | |
659 | sub get_text { |
664 | sub get_text { |
660 | my ($self, $text) = @_; |
665 | my ($self, $text) = @_; |
661 | |
666 | |
… | |
… | |
663 | } |
668 | } |
664 | |
669 | |
665 | sub size_request { |
670 | sub size_request { |
666 | my ($self) = @_; |
671 | my ($self) = @_; |
667 | |
672 | |
|
|
673 | $self->{layout}->set_width; |
|
|
674 | $self->{layout}->set_height ($self->{height}); |
|
|
675 | $self->{layout}->size |
668 | if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
676 | # if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
669 | ( |
677 | # ( |
670 | $self->{texture}{width}, |
678 | # $self->{texture}{width}, |
671 | $self->{texture}{height}, |
679 | # $self->{texture}{height}, |
672 | ) |
680 | # ) |
673 | } else { |
681 | # } else { |
674 | my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; |
682 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height}; |
675 | |
683 | # |
676 | ($w, $h) |
684 | # ($w, $h) |
677 | } |
685 | # } |
|
|
686 | } |
678 | |
687 | |
|
|
688 | sub size_allocate { |
|
|
689 | my ($self, $w, $h) = @_; |
|
|
690 | |
|
|
691 | $self->SUPER::size_allocate ($w, $h); |
|
|
692 | delete $self->{texture}; |
679 | } |
693 | } |
680 | |
694 | |
681 | sub _draw { |
695 | sub _draw { |
682 | my ($self) = @_; |
696 | my ($self) = @_; |
683 | |
697 | |
684 | my $tex = $self->{texture}; |
698 | my $tex = $self->{texture} ||= do { |
|
|
699 | $self->{layout}->set_width ($self->{w}); |
|
|
700 | new_from_layout CFClient::Texture $self->{layout}; |
|
|
701 | }; |
685 | |
702 | |
686 | glEnable GL_BLEND; |
703 | glEnable GL_BLEND; |
687 | glEnable GL_TEXTURE_2D; |
704 | glEnable GL_TEXTURE_2D; |
688 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
705 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
689 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
706 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
690 | |
707 | |
691 | glColor 1, 0, 0, 1; # TODO color |
708 | glColor @{$self->{color}}; |
692 | |
709 | |
693 | $tex->draw_quad (0, 0); |
710 | $tex->draw_quad (0, 0); |
694 | |
711 | |
695 | glDisable GL_BLEND; |
712 | glDisable GL_BLEND; |
696 | glDisable GL_TEXTURE_2D; |
713 | glDisable GL_TEXTURE_2D; |
697 | } |
714 | } |
698 | |
715 | |
699 | ############################################################################# |
716 | ############################################################################# |
700 | |
717 | |
701 | package Crossfire::Client::Widget::Entry; |
718 | package CFClient::Widget::Entry; |
702 | |
719 | |
703 | our @ISA = Crossfire::Client::Widget::Label::; |
720 | our @ISA = CFClient::Widget::Label::; |
704 | |
721 | |
705 | use SDL; |
722 | use SDL; |
706 | use SDL::OpenGL; |
723 | use SDL::OpenGL; |
707 | |
724 | |
708 | sub key_down { |
725 | sub key_down { |
… | |
… | |
754 | $self->SUPER::_draw; |
771 | $self->SUPER::_draw; |
755 | } |
772 | } |
756 | |
773 | |
757 | ############################################################################# |
774 | ############################################################################# |
758 | |
775 | |
759 | package Crossfire::Client::Widget::MapWidget; |
776 | package CFClient::Widget::MapWidget; |
760 | |
777 | |
761 | use strict; |
778 | use strict; |
762 | |
779 | |
763 | use List::Util qw(min max); |
780 | use List::Util qw(min max); |
764 | |
781 | |
765 | use SDL; |
782 | use SDL; |
766 | use SDL::OpenGL; |
783 | use SDL::OpenGL; |
767 | use SDL::OpenGL::Constants; |
784 | use SDL::OpenGL::Constants; |
768 | |
785 | |
769 | our @ISA = Crossfire::Client::Widget::; |
786 | our @ISA = CFClient::Widget::; |
|
|
787 | |
|
|
788 | sub new { |
|
|
789 | my $class = shift; |
|
|
790 | |
|
|
791 | $class->SUPER::new (z => -1, @_) |
|
|
792 | } |
770 | |
793 | |
771 | sub key_down { |
794 | sub key_down { |
772 | print "MAPKEYDOWN\n"; |
795 | print "MAPKEYDOWN\n"; |
773 | } |
796 | } |
774 | |
797 | |
… | |
… | |
850 | # } |
873 | # } |
851 | |
874 | |
852 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
875 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
853 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
876 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
854 | |
877 | |
855 | $darkness = new Crossfire::Client::Texture |
878 | $darkness = new CFClient::Texture |
856 | width => $sw4, |
879 | width => $sw4, |
857 | height => $sh, |
880 | height => $sh, |
858 | data => $darkness, |
881 | data => $darkness, |
859 | internalformat => GL_ALPHA, |
882 | internalformat => GL_ALPHA, |
860 | format => GL_ALPHA; |
883 | format => GL_ALPHA; |
… | |
… | |
917 | } |
940 | } |
918 | } |
941 | } |
919 | |
942 | |
920 | ############################################################################# |
943 | ############################################################################# |
921 | |
944 | |
922 | package Crossfire::Client::Widget::Animator; |
945 | package CFClient::Widget::Animator; |
923 | |
946 | |
924 | use SDL::OpenGL; |
947 | use SDL::OpenGL; |
925 | |
948 | |
926 | our @ISA = Crossfire::Client::Widget::Bin::; |
949 | our @ISA = CFClient::Widget::Bin::; |
927 | |
950 | |
928 | sub moveto { |
951 | sub moveto { |
929 | my ($self, $x, $y) = @_; |
952 | my ($self, $x, $y) = @_; |
930 | |
953 | |
931 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
954 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
… | |
… | |
959 | glPopMatrix; |
982 | glPopMatrix; |
960 | } |
983 | } |
961 | |
984 | |
962 | ############################################################################# |
985 | ############################################################################# |
963 | |
986 | |
964 | package Crossfire::Client::Widget::Toplevel; |
987 | package CFClient::Widget::Toplevel; |
965 | |
988 | |
966 | our @ISA = Crossfire::Client::Widget::Container::; |
989 | our @ISA = CFClient::Widget::Container::; |
967 | |
990 | |
968 | sub size_request { |
991 | sub size_request { |
969 | ($::WIDTH, $::HEIGHT) |
992 | ($::WIDTH, $::HEIGHT) |
970 | } |
993 | } |
971 | |
994 | |
… | |
… | |
1005 | $self->_draw; |
1028 | $self->_draw; |
1006 | } |
1029 | } |
1007 | |
1030 | |
1008 | ############################################################################# |
1031 | ############################################################################# |
1009 | |
1032 | |
1010 | package Crossfire::Client::Widget; |
1033 | package CFClient::Widget; |
1011 | |
1034 | |
1012 | $TOPLEVEL = new Crossfire::Client::Widget::Toplevel; |
1035 | $TOPLEVEL = new CFClient::Widget::Toplevel; |
1013 | |
1036 | |
1014 | 1 |
1037 | 1 |
1015 | |
1038 | |