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