ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.76 by root, Tue Apr 11 21:24:09 2006 UTC vs.
Revision 1.107 by root, Fri Apr 14 20:27:35 2006 UTC

43 43
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
45 45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
47 47
48 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB;
49 49
50 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 52 $grab->update if $grab;
53 $GRAB->update if $GRAB; 53 $GRAB->update if $GRAB;
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
62 62
63 if ($widget != $HOVER) { 63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 64 my $hover = $HOVER; $HOVER = $widget;
65 65
66 $hover->update if $hover; 66 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER; 67 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 } 68 }
69 69
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
71} 71}
72 72
79use SDL::OpenGL; 79use SDL::OpenGL;
80 80
81sub new { 81sub new {
82 my $class = shift; 82 my $class = shift;
83 83
84 bless { 84 my $self = bless {
85 x => 0, 85 x => 0,
86 y => 0, 86 y => 0,
87 z => 0, 87 z => 0,
88 w => -1, 88 w => -1,
89 h => -1, 89 h => -1,
90 @_ 90 @_
91 }, $class 91 }, $class;
92
93 for (keys %$self) {
94 if (/^connect_(.*)$/) {
95 $self->connect ($1 => delete $self->{$_});
96 }
97 }
98
99 $self
92} 100}
93 101
94sub move { 102sub move {
95 my ($self, $x, $y, $z) = @_; 103 my ($self, $x, $y, $z) = @_;
96 $self->{x} = $x; 104 $self->{x} = $x;
125 my ($self, $x, $y, $w, $h) = @_; 133 my ($self, $x, $y, $w, $h) = @_;
126 134
127 $self->_size_allocate ($x, $y, $w, $h); 135 $self->_size_allocate ($x, $y, $w, $h);
128} 136}
129 137
130# translate global koordinates to local coordinate system 138# translate global coordinates to local coordinate system
131sub translate { 139sub translate {
132 my ($self, $x, $y) = @_; 140 my ($self, $x, $y) = @_;
133 141
134 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 142 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
135} 143}
136 144
137sub focus_in { 145sub focus_in {
138 my ($self) = @_; 146 my ($self) = @_;
139 147
140 return if $FOCUS == $self; 148 return if $FOCUS == $self;
149 return unless $self->{can_focus};
141 150
142 my $focus = $FOCUS; $FOCUS = $self; 151 my $focus = $FOCUS; $FOCUS = $self;
143 $focus->update if $focus; 152 $focus->update if $focus;
144 $FOCUS->update; 153 $FOCUS->update;
145} 154}
178 glPushMatrix; 187 glPushMatrix;
179 glTranslate $self->{x}, $self->{y}, 0; 188 glTranslate $self->{x}, $self->{y}, 0;
180 $self->_draw; 189 $self->_draw;
181 glPopMatrix; 190 glPopMatrix;
182 191
183 if ($self == $HOVER) { 192 if ($self == $HOVER && $self->{can_hover}) {
184 my ($x, $y) = @$self{qw(x y)}; 193 my ($x, $y) = @$self{qw(x y)};
185 194
186 glColor 0, 0, 1, 0.2; 195 glColor 0, 0, 1, 0.2;
187 glEnable GL_BLEND; 196 glEnable GL_BLEND;
188 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 197 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
200 my ($self) = @_; 209 my ($self) = @_;
201 210
202 warn "no draw defined for $self\n"; 211 warn "no draw defined for $self\n";
203} 212}
204 213
205sub bbox {
206 my ($self) = @_;
207 my ($w, $h) = $self->size_request;
208 (
209 $self->{x},
210 $self->{y},
211 $self->{x} = $w,
212 $self->{y} = $h
213 )
214}
215
216sub find_widget { 214sub find_widget {
217 my ($self, $x, $y) = @_; 215 my ($self, $x, $y) = @_;
218 216
219 return $self 217 return $self
220 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 218 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
221 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 219 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
222 220
223 () 221 ()
224} 222}
225 223
226sub del_parent { $_[0]->{parent} = undef }
227
228sub set_parent { 224sub set_parent {
229 my ($self, $par) = @_; 225 my ($self, $par) = @_;
230 226
231 $self->{parent} = $par; 227 $self->{parent} = $par;
232 Scalar::Util::weaken $self->{parent}; 228 Scalar::Util::weaken $self->{parent};
276 272
277 # range [value, low, high, page] 273 # range [value, low, high, page]
278 274
279 $class->SUPER::new ( 275 $class->SUPER::new (
280 bg => [0, 0, 0, 0.2], 276 bg => [0, 0, 0, 0.2],
281 active_bg => [1, 1, 1], 277 active_bg => [1, 1, 1, 0.5],
282 @_ 278 @_
283 ) 279 )
284} 280}
285 281
286sub _draw { 282sub _draw {
330 326
331 $self 327 $self
332} 328}
333 329
334sub add { 330sub add {
335 my ($self, $chld, $expand) = @_; 331 my ($self, $chld) = @_;
336 332
337 $chld->{expand} = $expand;
338 $chld->set_parent ($self); 333 $chld->set_parent ($self);
339 334
340 $self->{children} = [ 335 $self->{children} = [
341 sort { $a->{z} <=> $b->{z} } 336 sort { $a->{z} <=> $b->{z} }
342 @{$self->{children}}, $chld 337 @{$self->{children}}, $chld
444} 439}
445 440
446sub render_chld { 441sub render_chld {
447 my ($self) = @_; 442 my ($self) = @_;
448 443
449 $self->{texture} = 444 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
450 CFClient::Texture->new_from_opengl ( 445 glClearColor 0, 0, 0, 1;
451 $self->{w}, $self->{h}, sub { $self->child->draw } 446 glClear GL_COLOR_BUFFER_BIT;
452 ); 447 $self->child->draw;
448 };
453} 449}
454 450
455sub size_allocate { 451sub size_allocate {
456 my ($self, $x, $y, $w, $h) = @_; 452 my ($self, $x, $y, $w, $h) = @_;
457 453
535 531
536my @tex = 532my @tex =
537 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 533 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
538 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 534 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
539 535
536sub new {
537 my $class = shift;
538
539 # TODO: user_x, user_y, overwrite moveto?
540
541 $class->SUPER::new (
542 bg => [1, 1, 1, 1],
543 border_bg => [1, 1, 1, 1],
544 border => $::FONTSIZE * 0.8,
545 @_
546 )
547}
548
540sub size_request { 549sub size_request {
541 my ($self) = @_; 550 my ($self) = @_;
542 551
552 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
553
543 my ($w, $h) = $self->SUPER::size_request; 554 my ($w, $h) = $self->SUPER::size_request;
544 555
545 $h += $tex[1]->{h}; 556 (
546 $h += $tex[4]->{h}; 557 $w + $self->{border} * 2,
547 $w += $tex[2]->{w}; 558 $h + $self->{border} * 2,
548 $w += $tex[3]->{w}; 559 )
549
550 ($w, $h)
551} 560}
552 561
553sub size_allocate { 562sub size_allocate {
554 my ($self, $x, $y, $w, $h) = @_; 563 my ($self, $x, $y, $w, $h) = @_;
555 564
556 $self->_size_allocate ($x, $y, $w, $h) or return; 565 $self->_size_allocate ($x, $y, $w, $h) or return;
557 566
558 $h -= $tex[1]->{h}; 567 $h -= List::Util::max 0, $self->{border} * 2;
559 $h -= $tex[4]->{h}; 568 $w -= List::Util::max 0, $self->{border} * 2;
560 $w -= $tex[2]->{w};
561 $w -= $tex[3]->{w};
562 569
563 $h = $h < 0 ? 0 : $h; 570 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h);
564 $w = $w < 0 ? 0 : $w; 571}
565 572
566 my $child = $self->child; 573sub button_down {
574 my ($self, $ev, $x, $y) = @_;
567 575
568 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h); 576 if ($x < $self->{w} && $x >= $self->{w} - $self->{border}
577 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) {
578
579 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
580 my ($bw, $bh) = ($self->{w}, $self->{h});
581
582 $self->{motion} = sub {
583 my ($ev, $x, $y) = @_;
584
585 ($x, $y) = ($ev->motion_x, $ev->motion_y);
586
587 $self->{user_w} = $bw + $x - $ox;
588 $self->{user_h} = $bh + $y - $oy;
589 $self->update;
590 };
591
592 } elsif ($x >= 0 && $x < $self->{w}
593 && $y >= 0 && $y < $self->{border}) {
594
595 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
596 my ($bx, $by) = ($self->{x}, $self->{y});
597
598 $self->{motion} = sub {
599 my ($ev, $x, $y) = @_;
600
601 ($x, $y) = ($ev->motion_x, $ev->motion_y);
602
603 $self->move ($bx + $x - $ox, $by + $y - $oy);
604 $self->update;
605 };
606 }
607}
608
609sub button_up {
610 my ($self, $ev, $x, $y) = @_;
611
612 delete $self->{motion};
613}
614
615sub mouse_motion {
616 my ($self, $ev, $x, $y) = @_;
617
618 $self->{motion}->($ev, $x, $y) if $self->{motion};
569} 619}
570 620
571sub _draw { 621sub _draw {
572 my ($self) = @_; 622 my ($self) = @_;
573 623
574 my ($w, $h) = ($self->{w}, $self->{h}); 624 my ($w, $h ) = ($self->{w}, $self->{h});
575 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 625 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
626
627 glEnable GL_BLEND;
628 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
629 glEnable GL_TEXTURE_2D;
630 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
631
632 glColor @{ $self->{border_bg} };
633 $tex[1]->draw_quad (0, 0, $w, $self->{border});
634 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch);
635 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch);
636 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border});
637
638 my $bg = $tex[0];
639
640 # TODO: repeat texture not scale
641 my $rep_x = $cw / $bg->{w};
642 my $rep_y = $ch / $bg->{h};
643
644 glColor @{ $self->{bg} };
645
646 $bg->{s} = $rep_x;
647 $bg->{t} = $rep_y;
648 $bg->{wrap_mode} = 1;
649 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch);
650
651 glDisable GL_TEXTURE_2D;
652 glDisable GL_BLEND;
653
654 $self->child->draw;
655}
656
657#############################################################################
658
659package CFClient::UI::Table;
660
661our @ISA = CFClient::UI::Base::;
662
663use List::Util qw(max sum);
664
665use SDL::OpenGL;
666
667sub new {
668 my $class = shift;
669
670 $class->SUPER::new (
671 col_expand => [],
672 @_
673 )
674}
675
676sub add {
677 my ($self, $x, $y, $chld) = @_;
678
679 $self->{children}[$y][$x] = $chld;
680 $chld->set_parent ($self);
681
682 $self->{w} = $self->{h} = -1;
683 $self->update;
684}
685
686sub get_wh {
687 my ($self) = @_;
688
689 my (@w, @h);
690
691 for my $y (0 .. $#{$self->{children}}) {
692 my $row = $self->{children}[$y]
693 or next;
694
695 for my $x (0 .. $#$row) {
696 my $widget = $row->[$x]
697 or next;
698 my ($w, $h) = $widget->size_request;
699
700 $w[$x] = max $w[$x], $w;
701 $h[$y] = max $h[$y], $h;
702 }
703 }
704
705 (\@w, \@h)
706}
707
708sub size_request {
709 my ($self) = @_;
710
711 my ($ws, $hs) = $self->get_wh;
712
713 (
714 (sum @$ws),
715 (sum @$hs),
716 )
717}
718
719sub size_allocate {
720 my ($self, $x, $y, $w, $h) = @_;
721
722 $self->_size_allocate ($x, $y, $w, $h) or return;
723
724 my ($ws, $hs) = $self->get_wh;
725
726 my $req_w = sum @$ws;
727 my $req_h = sum @$hs;
728
729 # TODO: nicer code && do row_expand
730 my @col_expand = @{$self->{col_expand}};
731 @col_expand = (1) x @$ws unless @col_expand;
732 my $col_expand = (sum @col_expand) || 1;
733
734 # linearly scale sizes
735 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
736 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
737
738 $_ = int $_ for @$ws; #TODO: avoid rounding problems
739 $_ = int $_ for @$hs; #TODO: avoid rounding problems
740
741 my $y;
742
743 for my $r (0 .. $#{$self->{children}}) {
744 my $row = $self->{children}[$r]
745 or next;
746
747 my $x = 0;
748 my $row_h = $hs->[$r];
749
750 for my $c (0 .. $#$row) {
751 my $col_w = $ws->[$c];
752
753 if (my $widget = $row->[$c]) {
754 $widget->size_allocate ($x, $y, $col_w, $row_h);
755 }
756
757 $x += $col_w;
758 }
759
760 $y += $row_h;
761 }
762
763}
764
765sub find_widget {
766 my ($self, $x, $y) = @_;
767
768 $x -= $self->{x};
769 $y -= $self->{y};
770
771 my $res;
772
773 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
774 $res = $_->find_widget ($x, $y)
775 and return $res;
776 }
777
778 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
779}
780
781sub _draw {
782 my ($self) = @_;
783
784 for (grep $_, @{$self->{children}}) {
785 $_->draw for grep $_, @$_;
786 }
787}
788
789#############################################################################
790
791package CFClient::UI::HBox;
792
793# TODO: wrap into common Box base class
794
795our @ISA = CFClient::UI::Container::;
796
797sub size_request {
798 my ($self) = @_;
799
800 my @alloc = map [$_->size_request], @{$self->{children}};
801
802 (
803 (List::Util::sum map $_->[0], @alloc),
804 (List::Util::max map $_->[1], @alloc),
805 )
806}
807
808sub size_allocate {
809 my ($self, $x, $y, $w, $h) = @_;
810
811 $self->_size_allocate ($x, $y, $w, $h);
812
813 ($h, $w) = ($w, $h);
814
815 my $children = $self->{children};
816
817 my @h = map +($_->size_request)[0], @$children;
818
819 my $req_h = List::Util::sum @h;
820
821 if ($req_h > $h) {
822 # ah well, not enough space
823 $_ *= $h / $req_h for @h;
824 } else {
825 my $exp = List::Util::sum map $_->{expand}, @$children;
826 $exp ||= 1;
827
828 for (0 .. $#$children) {
829 my $child = $children->[$_];
830
831 my $alloc_h = $h[$_];
832 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
833 $h[$_] = $alloc_h;
834 }
835 }
836
837 my $y = 0;
838 for (0 .. $#$children) {
839 my $child = $children->[$_];
840 my $h = int $h[$_];
841 $child->size_allocate ($y, 0, $h, $w);
842
843 $y += $h;
844 }
845}
846
847#############################################################################
848
849package CFClient::UI::VBox;
850
851# TODO: wrap into common Box base class
852
853our @ISA = CFClient::UI::Container::;
854
855sub size_request {
856 my ($self) = @_;
857
858 my @alloc = map [$_->size_request], @{$self->{children}};
859
860 (
861 (List::Util::max map $_->[0], @alloc),
862 (List::Util::sum map $_->[1], @alloc),
863 )
864}
865
866sub size_allocate {
867 my ($self, $x, $y, $w, $h) = @_;
868
869 $self->_size_allocate ($x, $y, $w, $h);
870
871 my $children = $self->{children};
872
873 my @h = map +($_->size_request)[1], @$children;
874
875 my $req_h = List::Util::sum @h;
876
877 if ($req_h > $h) {
878 # ah well, not enough space
879 $_ *= $h / $req_h for @h;
880 } else {
881 my $exp = List::Util::sum map $_->{expand}, @$children;
882 $exp ||= 1;
883
884 for (0 .. $#$children) {
885 my $child = $children->[$_];
886
887 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
888 }
889 }
890
891 my $y = 0;
892 for (0 .. $#$children) {
893 my $child = $children->[$_];
894 my $h = int $h[$_];
895 $child->size_allocate (0, $y, $w, $h);
896
897 $y += $h;
898 }
899}
900
901#############################################################################
902
903package CFClient::UI::Label;
904
905our @ISA = CFClient::UI::Base::;
906
907use SDL::OpenGL;
908
909sub new {
910 my ($class, %arg) = @_;
911
912 my $self = $class->SUPER::new (
913 fg => [1, 1, 1],
914 fontsize => $::FONTSIZE,
915 text => "",
916 align => -1,
917 padding => 2,
918 layout => new CFClient::Layout,
919 %arg
920 );
921
922 $self->set_text ($self->{text});
923
924 $self
925}
926
927sub escape_text {
928 local $_ = $_[1];
929
930 s/&/&amp;/g;
931 s/>/&gt;/g;
932 s/</&lt;/g;
933
934 $_[1]
935}
936
937sub set_text {
938 my ($self, $text) = @_;
939
940 $self->{text} = $text;
941 $self->{layout}->set_markup ($text);
942
943 delete $self->{texture};
944# $self->{w} = $self->{h} = -1;
945 $self->update;
946}
947
948sub get_text {
949 my ($self, $text) = @_;
950
951 $self->{text}
952}
953
954sub size_request {
955 my ($self) = @_;
956
957 $self->{layout}->set_width;
958 $self->{layout}->set_height ($self->{fontsize});
959 my ($w, $h) = $self->{layout}->size;
960
961 (
962 $w + $self->{padding} * 2,
963 $h + $self->{padding} * 2,
964 )
965}
966
967sub size_allocate {
968 my ($self, $x, $y, $w, $h) = @_;
969
970 $self->_size_allocate ($x, $y, $w, $h) or return;
971
972 delete $self->{texture};
973}
974
975sub update {
976 my ($self) = @_;
977
978 delete $self->{texture};
979 $self->SUPER::update;
980}
981
982sub _draw {
983 my ($self) = @_;
984
985 my $tex = $self->{texture} ||= do {
986 $self->{layout}->set_width ($self->{w});
987 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
988 new_from_layout CFClient::Texture $self->{layout}
989 };
576 990
577 glEnable GL_BLEND; 991 glEnable GL_BLEND;
578 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 992 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
579 glEnable GL_TEXTURE_2D; 993 glEnable GL_TEXTURE_2D;
580 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 994 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
581 995
582 my $top = $tex[1];
583 $top->draw_quad (0, 0, $w, $top->{h});
584
585 my $left = $tex[3];
586 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
587
588 my $right = $tex[2];
589 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
590
591 my $bottom = $tex[4];
592 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
593
594 my $bg = $tex[0];
595
596 glBindTexture GL_TEXTURE_2D, $bg->{name};
597 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
598 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
599 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
600
601 my $rep_x = $cw / $bg->{w};
602 my $rep_y = $ch / $bg->{h};
603
604 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
605
606 glDisable GL_TEXTURE_2D;
607 glDisable GL_BLEND;
608
609 $self->child->draw;
610
611}
612
613#############################################################################
614
615package CFClient::UI::Table;
616
617our @ISA = CFClient::UI::Base::;
618
619use List::Util qw(max sum);
620
621use SDL::OpenGL;
622
623sub add {
624 my ($self, $x, $y, $chld) = @_;
625
626 $self->{children}[$y][$x] = $chld;
627 $chld->set_parent ($self);
628
629 $self->{w} = $self->{h} = -1;
630 $self->update;
631}
632
633sub get_wh {
634 my ($self) = @_;
635
636 my (@w, @h);
637
638 for my $y (0 .. $#{$self->{children}}) {
639 my $row = $self->{children}[$y]
640 or next;
641
642 for my $x (0 .. $#$row) {
643 my $widget = $row->[$x]
644 or next;
645 my ($w, $h) = $widget->size_request;
646
647 $w[$x] = max $w[$x], $w;
648 $h[$y] = max $h[$y], $h;
649 }
650 }
651
652 (\@w, \@h)
653}
654
655sub size_request {
656 my ($self) = @_;
657
658 my ($ws, $hs) = $self->get_wh;
659
660 (
661 (List::Util::sum @$ws),
662 (List::Util::sum @$hs),
663 )
664}
665
666sub size_allocate {
667 my ($self, $x, $y, $w, $h) = @_;
668
669 $self->_size_allocate ($x, $y, $w, $h) or return;
670
671 my ($ws, $hs) = $self->get_wh;
672
673 my $req_w = List::Util::sum @$ws;
674 my $req_h = List::Util::sum @$hs;
675
676 # linearly scale sizes
677 $_ *= $req_w / $w for @$ws;
678 $_ *= $req_h / $h for @$hs;
679
680 my $y;
681
682 for my $r (0 .. $#{$self->{children}}) {
683 my $row = $self->{children}[$r]
684 or next;
685
686 my $x = 0;
687 my $row_h = $hs->[$r];
688
689 for my $c (0 .. $#$row) {
690 my $widget = $row->[$c]
691 or next;
692
693 my $col_w = $ws->[$c];
694
695 $widget->size_allocate ($x, $y, $col_w, $row_h);
696
697 $x += $col_w;
698 }
699
700 $y += $row_h;
701 }
702
703}
704
705sub find_widget {
706 my ($self, $x, $y) = @_;
707
708 $x -= $self->{x};
709 $y -= $self->{y};
710
711 my $res;
712
713 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
714 $res = $_->find_widget ($x, $y)
715 and return $res;
716 }
717
718 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
719}
720
721sub _draw {
722 my ($self) = @_;
723
724 for (grep $_, @{$self->{children}}) {
725 $_->draw for grep $_, @$_;
726 }
727}
728
729#############################################################################
730
731package CFClient::UI::HBox;
732
733# TODO: wrap into common Box base class
734
735our @ISA = CFClient::UI::Container::;
736
737sub size_request {
738 my ($self) = @_;
739
740 my @alloc = map [$_->size_request], @{$self->{children}};
741
742 (
743 (List::Util::sum map $_->[0], @alloc),
744 (List::Util::max map $_->[1], @alloc),
745 )
746}
747
748sub size_allocate {
749 my ($self, $x, $y, $w, $h) = @_;
750
751 $self->_size_allocate ($x, $y, $w, $h) or return;
752
753 return unless $self->{w};
754
755 ($h, $w) = ($w, $h);
756
757 my $children = $self->{children};
758
759 my @h = map +($_->size_request)[0], @$children;
760
761 my $req_h = List::Util::sum @h;
762
763 if ($req_h > $h) {
764 # ah well, not enough space
765 $_ = $h[$_] * $h / $req_h for @h;
766 } else {
767 my @exp = grep $_->{expand}, @$children;
768 @exp = @$children unless @exp;
769
770 my %exp = map +($_ => 1), @exp;
771
772 for (0 .. $#$children) {
773 my $child = $children->[$_];
774
775 my $alloc_h = $h[$_];
776 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
777 $h[$_] = $alloc_h;
778 }
779 }
780
781 my $y = 0;
782 for (0 .. $#$children) {
783 my $child = $children->[$_];
784 my $h = $h[$_];
785 $child->size_allocate ($y, 0, $h, $w);
786
787 $y += $h;
788 }
789}
790
791#############################################################################
792
793package CFClient::UI::VBox;
794
795# TODO: wrap into common Box base class
796
797our @ISA = CFClient::UI::Container::;
798
799sub size_request {
800 my ($self) = @_;
801
802 my @alloc = map [$_->size_request], @{$self->{children}};
803
804 (
805 (List::Util::max map $_->[0], @alloc),
806 (List::Util::sum map $_->[1], @alloc),
807 )
808}
809
810sub size_allocate {
811 my ($self, $x, $y, $w, $h) = @_;
812
813 $self->_size_allocate ($x, $y, $w, $h) or return;
814
815 return unless $self->{h};
816
817 my $children = $self->{children};
818
819 my @h = map +($_->size_request)[1], @$children;
820
821 my $req_h = List::Util::sum @h;
822
823 if ($req_h > $h) {
824 # ah well, not enough space
825 $_ = $h[$_] * $h / $req_h for @h;
826 } else {
827 my @exp = grep $_->{expand}, @$children;
828 @exp = @$children unless @exp;
829
830 my %exp = map +($_ => 1), @exp;
831
832 for (0 .. $#$children) {
833 my $child = $children->[$_];
834
835 my $alloc_h = $h[$_];
836 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
837 $h[$_] = $alloc_h;
838 }
839 }
840
841 my $y = 0;
842 for (0 .. $#$children) {
843 my $child = $children->[$_];
844 my $h = $h[$_];
845 $child->size_allocate (0, $y, $w, $h);
846
847 $y += $h;
848 }
849}
850
851#############################################################################
852
853package CFClient::UI::Label;
854
855our @ISA = CFClient::UI::Base::;
856
857use SDL::OpenGL;
858
859sub new {
860 my ($class, %arg) = @_;
861
862 my $self = $class->SUPER::new (
863 fg => [1, 1, 1],
864 height => $::FONTSIZE,
865 text => "",
866 align => -1,
867 padding => 2,
868 layout => new CFClient::Layout,
869 %arg
870 );
871
872 $self->set_text ($self->{text});
873
874 $self
875}
876
877sub escape_text {
878 local $_ = $_[1];
879
880 s/&/&amp;/g;
881 s/>/&gt;/g;
882 s/</&lt;/g;
883
884 $_[1]
885}
886
887sub set_text {
888 my ($self, $text) = @_;
889
890 $self->{text} = $text;
891 $self->{layout}->set_markup ($text);
892
893 delete $self->{texture};
894 $self->update;
895}
896
897sub get_text {
898 my ($self, $text) = @_;
899
900 $self->{text}
901}
902
903sub size_request {
904 my ($self) = @_;
905
906 $self->{layout}->set_width;
907 $self->{layout}->set_height ($self->{height});
908 my ($w, $h) = $self->{layout}->size;
909
910 (
911 $w + $self->{padding} * 2,
912 $h + $self->{padding} * 2,
913 )
914}
915
916sub size_allocate {
917 my ($self, $x, $y, $w, $h) = @_;
918
919 $self->_size_allocate ($x, $y, $w, $h) or return;
920
921 delete $self->{texture};
922}
923
924sub update {
925 my ($self) = @_;
926
927 delete $self->{texture};
928 $self->SUPER::update;
929}
930
931sub _draw {
932 my ($self) = @_;
933
934 my $tex = $self->{texture} ||= do {
935 $self->{layout}->set_width ($self->{w});
936 new_from_layout CFClient::Texture $self->{layout}
937 };
938
939 glEnable GL_BLEND;
940 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
941 glEnable GL_TEXTURE_2D;
942 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
943
944 glColor @{$self->{fg}}; 996 glColor @{$self->{fg}};
945 997
946 my $x = 998 my $x =
947 $self->{align} < 0 ? $self->{padding} 999 $self->{align} < 0 ? $self->{padding}
948 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1000 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
949 : ($self->{w} - $tex->{w}) * 0.5; 1001 : ($self->{w} - $tex->{w}) * 0.5;
950 1002
951 $tex->draw_quad ($x, 0); 1003 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5);
952 1004
953 glDisable GL_TEXTURE_2D; 1005 glDisable GL_TEXTURE_2D;
954 glDisable GL_BLEND; 1006 glDisable GL_BLEND;
955} 1007}
956 1008
967 my $class = shift; 1019 my $class = shift;
968 1020
969 $class->SUPER::new ( 1021 $class->SUPER::new (
970 fg => [1, 1, 1], 1022 fg => [1, 1, 1],
971 bg => [0, 0, 0, 0.2], 1023 bg => [0, 0, 0, 0.2],
972 active_bg => [1, 1, 1], 1024 active_bg => [1, 1, 1, 0.5],
973 active_fg => [0, 0, 0], 1025 active_fg => [0, 0, 0],
1026 can_hover => 1,
1027 can_focus => 1,
974 @_ 1028 @_
975 ) 1029 )
976} 1030}
977 1031
978sub _set_text { 1032sub _set_text {
979 my ($self, $text) = @_; 1033 my ($self, $text) = @_;
980 1034
1035 my $old_text = $self->{text};
1036
981 $self->{last_activity} = $::NOW; 1037 $self->{last_activity} = $::NOW;
982 1038
983 $self->{text} = $text; 1039 $self->{text} = $text;
984 $self->{layout}->set_width ($self->{w}); 1040 $self->{layout}->set_width ($self->{w});
1041 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
985 1042
986 $text =~ s/./*/g if $self->{hidden}; 1043 $text =~ s/./*/g if $self->{hidden};
987 1044
988 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1045 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
989 1046
990 $text = substr $text, 0, $self->{cursor}; 1047 $text = substr $text, 0, $self->{cursor};
991 utf8::encode $text; 1048 utf8::encode $text;
992 1049
993 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 1050 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1051
1052 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1053 if $old_text ne $self->{text};
994} 1054}
995 1055
996sub size_request { 1056sub size_request {
997 my ($self) = @_; 1057 my ($self) = @_;
998 1058
1037 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1097 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1038 } elsif ($sym == SDLK_HOME) { 1098 } elsif ($sym == SDLK_HOME) {
1039 $self->{cursor} = 0; 1099 $self->{cursor} = 0;
1040 } elsif ($sym == SDLK_END) { 1100 } elsif ($sym == SDLK_END) {
1041 $self->{cursor} = length $text; 1101 $self->{cursor} = length $text;
1102 } elsif ($sym == SDLK_ESCAPE) {
1103 $self->emit ('escape');
1042 } elsif ($uni) { 1104 } elsif ($uni) {
1043 substr $text, $self->{cursor}++, 0, chr $uni; 1105 substr $text, $self->{cursor}++, 0, chr $uni;
1044 } 1106 }
1045 1107
1046 $self->_set_text ($text); 1108 $self->_set_text ($text);
1108 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1170 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h};
1109 glEnd; 1171 glEnd;
1110 } 1172 }
1111} 1173}
1112 1174
1113#############################################################################
1114
1115package CFClient::UI::Slider; 1175package CFClient::UI::LineEntry;
1116 1176
1117use strict; 1177our @ISA = CFClient::UI::Entry::;
1118 1178
1179use SDL;
1119use SDL::OpenGL; 1180use SDL::OpenGL;
1120 1181
1182sub key_down {
1183 my ($self, $ev) = @_;
1184
1185 my $sym = $ev->key_sym;
1186
1187 if ($sym == SDLK_RETURN) {
1188 $self->emit (activate => $self->get_text);
1189 $self->update;
1190
1191 } else {
1192 $self->SUPER::key_down ($ev);
1193 }
1194
1195}
1196
1197#############################################################################
1198
1199package CFClient::UI::Button;
1200
1121our @ISA = CFClient::UI::DrawBG::; 1201our @ISA = CFClient::UI::Label::;
1202
1203use SDL;
1204use SDL::OpenGL;
1205
1206my @tex =
1207 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1208 qw(b1_button_active.png);
1122 1209
1123sub new { 1210sub new {
1124 my $class = shift; 1211 my $class = shift;
1125 1212
1213 $class->SUPER::new (
1214 padding => 4,
1215 fg => [1, 1, 1],
1216 bg => [1, 1, 1, 0.2],
1217 active_fg => [1, 1, 0],
1218 can_hover => 1,
1219 @_
1220 )
1221}
1222
1223sub button_up {
1224 my ($self, $ev, $x, $y) = @_;
1225
1226 if ($x >= 0 && $x < $self->{w}
1227 && $y >= 0 && $y < $self->{h}) {
1228 $self->emit ("activate");
1229 }
1230}
1231
1232sub _draw {
1233 my ($self) = @_;
1234
1235 local $self->{fg} = $self->{fg};
1236 my $tex = $tex[0];
1237
1238 glEnable GL_BLEND;
1239 glEnable GL_TEXTURE_2D;
1240 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1241
1242 if ($GRAB == $self) {
1243 $self->{fg} = $self->{active_fg};
1244 }
1245
1246 glBindTexture GL_TEXTURE_2D, $tex->{name};
1247 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1248
1249 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
1250
1251 glDisable GL_TEXTURE_2D;
1252 glDisable GL_BLEND;
1253
1254 $self->SUPER::_draw;
1255}
1256
1257#############################################################################
1258
1259package CFClient::UI::CheckBox;
1260
1261our @ISA = CFClient::UI::DrawBG::;
1262
1263my @tex =
1264 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1265 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1266
1267use SDL;
1268use SDL::OpenGL;
1269
1270sub new {
1271 my $class = shift;
1272
1273 $class->SUPER::new (
1274 padding => 2,
1275 fg => [1, 1, 1],
1276 active_fg => [1, 1, 0],
1277 state => 0,
1278 can_hover => 1,
1279 @_
1280 )
1281}
1282
1283sub size_request {
1284 my ($self) = @_;
1285
1286 ($self->{padding} * 2 + 6) x 2
1287}
1288
1289sub size_allocate {
1290 my ($self, $x, $y, $w, $h) = @_;
1291
1292 $self->_size_allocate ($x, $y, $w, $h);
1293}
1294
1295sub button_down {
1296 my ($self, $ev, $x, $y) = @_;
1297
1298 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1299 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1300 $self->{state} = !$self->{state};
1301 $self->emit (changed => $self->{state});
1302 }
1303}
1304
1305sub _draw {
1306 my ($self) = @_;
1307
1308 $self->SUPER::_draw;
1309
1310 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1311
1312 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1313
1314 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1315
1316 glEnable GL_BLEND;
1317 glEnable GL_TEXTURE_2D;
1318 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1319
1320 my $tex = $self->{state} ? $tex[1] : $tex[0];
1321
1322 $tex->draw_quad (0, 0, $s, $s);
1323
1324 glDisable GL_TEXTURE_2D;
1325 glDisable GL_BLEND;
1326}
1327
1328#############################################################################
1329
1330package CFClient::UI::Slider;
1331
1332use strict;
1333
1334use SDL::OpenGL;
1335
1336our @ISA = CFClient::UI::DrawBG::;
1337
1338my @tex =
1339 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1340 qw(s1_slider.png s1_slider_bg.png);
1341
1342sub new {
1343 my $class = shift;
1344
1126 # range [value, low, high, page] 1345 # range [value, low, high, page]
1127 1346
1347 # TODO: 0-width page
1348 # TODO: req_w/h are wrong with vertical
1349 # TODO: calculations are off
1128 my $self = $class->SUPER::new ( 1350 my $self = $class->SUPER::new (
1129 fg => [1, 1, 1], 1351 fg => [1, 1, 1],
1130 active_fg => [0, 0, 0], 1352 active_fg => [0, 0, 0],
1131 range => [0, 0, 100, 10], 1353 range => [0, 0, 100, 10],
1132 req_w => 40, 1354 req_w => 40,
1133 req_h => 10, 1355 req_h => 13,
1134 vertical => 0, 1356 vertical => 0,
1357 can_hover => 1,
1358 inner_pad => 5,
1135 @_ 1359 @_
1136 ); 1360 );
1137 1361
1138 $self 1362 $self
1139} 1363}
1160 if ($GRAB == $self) { 1384 if ($GRAB == $self) {
1161 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1385 my ($value, $lo, $hi, $page) = @{$self->{range}};
1162 1386
1163 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 1387 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1164 1388
1389 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1390 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1391
1392 $x -= $inner_pad_px; # substract the padding
1165 $x = $x * ($hi - $lo) / $w + $lo; 1393 $x = $x * ($hi - $lo) / $inner_w + $lo;
1166 $x = $lo if $x < $lo; 1394 $x = $lo if $x < $lo;
1167 $x = $hi - $page if $x > $hi - $page; 1395 $x = $hi - $page if $x > $hi - $page;
1168 $self->{range}[0] = $x; 1396 $self->{range}[0] = $x;
1169 1397
1170 $self->emit (changed => $x); 1398 $self->emit (changed => $x);
1171 $self->update; 1399 $self->update;
1172 } 1400 }
1401}
1402
1403# the inner_* stuff is for generating a padding for the slider handle,
1404# so that the handle doesn't leave the texture. This calculation isn't 100%
1405# correct propably, but it does the job for now
1406sub _calc_inner_pad_px {
1407 my ($self, $w) = @_;
1408 ($w / 100) * $self->{inner_pad} # % to pixels
1173} 1409}
1174 1410
1175sub _draw { 1411sub _draw {
1176 my ($self) = @_; 1412 my ($self) = @_;
1177 1413
1191 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1427 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1192 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1428 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1193 1429
1194 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1430 my ($value, $lo, $hi, $page) = @{$self->{range}};
1195 1431
1432 $hi = $value + 1 if $lo == $hi;
1433
1434 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1435 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1436
1196 $page = int $page * $w / ($hi - $lo); 1437 $page = int $page * $inner_w / ($hi - $lo);
1197 $value = int +($value - $lo) * $w / ($hi - $lo); 1438 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1198 1439
1199 $w -= $page; 1440 $w -= $page;
1200 $page &= ~1; 1441 $page &= ~1;
1201 glTranslate $page * 0.5, 0, 0; 1442 glTranslate $page * 0.5, 0, 0;
1443 $page ||= 2;
1202 1444
1203 glColor @$fg;
1204 glBegin GL_LINES;
1205 glVertex 0, 0; glVertex 0, $h;
1206 glVertex $w - 1, 0; glVertex $w - 1, $h;
1207 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1208 glEnd;
1209
1210 my $knob_a = $value - $page * 0.5; 1445 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1211 my $knob_b = $value + $page * 0.5; 1446 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1212 1447
1213 glBegin GL_QUADS; 1448 glEnable GL_BLEND;
1214 glColor @$fg; 1449 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1215 glVertex $knob_a, 0; 1450 glEnable GL_TEXTURE_2D;
1216 glVertex $knob_a, $h; 1451 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1217 glVertex $knob_b, $h;
1218 glVertex $knob_b, 0;
1219 1452
1220 if ($knob_a < $knob_b - 2) { 1453 # draw background
1221 glColor @$bg; 1454 $tex[1]->draw_quad (0, 0, $w, $h);
1222 glVertex $knob_a + 1, 1; 1455
1223 glVertex $knob_a + 1, $h - 1; 1456 # draw handle
1224 glVertex $knob_b - 1, $h - 1; 1457 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h);
1225 glVertex $knob_b - 1, 1; 1458
1459 glDisable GL_BLEND;
1460 glDisable GL_TEXTURE_2D;
1461}
1462
1463#############################################################################
1464
1465package CFClient::UI::TextView;
1466
1467our @ISA = CFClient::UI::HBox::;
1468
1469use SDL::OpenGL;
1470
1471sub new {
1472 my $class = shift;
1473
1474 my $self = $class->SUPER::new (
1475 req_w => $::WIDTH / 6,
1476 req_h => $::HEIGHT / 6,
1477 fontsize => $::FONTSIZE,
1478 @_,
1479
1480 layout => (new CFClient::Layout),
1481 par => [],
1482 height => 0,
1483 children => [
1484 (new CFClient::UI::Empty expand => 1),
1485 (new CFClient::UI::Slider vertical => 1),
1486 ],
1487 );
1488
1489 $self->{children}[1]->connect (changed => sub {
1490 $self->update;
1491 });
1492
1493 $self
1494}
1495
1496sub set_fontsize {
1497 my ($self, $fontsize) = @_;
1498
1499 $self->{fontsize} = $fontsize;
1500 $self->reflow;
1501}
1502
1503sub text_height {
1504 my ($self, $text) = @_;
1505
1506 my $layout = $self->{layout};
1507
1508 $layout->set_height ($self->{fontsize});
1509 $layout->set_width ($self->{w});
1510 $layout->set_text ($text);
1226 } 1511
1227 glEnd; 1512 ($layout->size)[1]
1513}
1514
1515sub reflow {
1516 my ($self) = @_;
1517
1518 $self->{need_reflow}++;
1519 $self->update;
1520}
1521
1522sub size_request {
1523 my ($self) = @_;
1524
1525 ($self->{req_w}, $self->{req_h})
1526}
1527
1528sub size_allocate {
1529 my ($self, $x, $y, $w, $h) = @_;
1530
1531 $self->SUPER::size_allocate ($x, $y, $w, $h);
1532
1533 $self->{layout}->set_height ($self->{fontsize});
1534 $self->{layout}->set_width ($self->{w});
1535
1536 $self->reflow;
1537}
1538
1539sub add_paragraph {
1540 my ($self, $color, $text) = @_;
1541
1542 #TODO: intelligently "reformat" paragraph
1543
1544 my $height = $self->text_height ($text);
1545
1546 $self->{height} += $height;
1547
1548 push @{$self->{par}}, [$height, $color, $text];
1549
1550 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
1551 $self->{children}[1]->update;
1552}
1553
1554sub update {
1555 my ($self) = @_;
1556
1557 $self->SUPER::update;
1558
1559 return unless $self->{h} > 0;
1560
1561 delete $self->{texture};
1562
1563 $TOPLEVEL->on_refresh ($self, sub {
1564 if (delete $self->{need_reflow}) {
1565 my $height = 0;
1566
1567 $height += $_->[0] = $self->text_height ($_->[2])
1568 for @{$self->{par}};
1569
1570 $self->{height} = $height;
1571
1572 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
1573
1574 delete $self->{texture};
1575 }
1576
1577 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
1578 glClearColor 0, 0, 0, 1;
1579 glClear GL_COLOR_BUFFER_BIT;
1580
1581 glEnable GL_BLEND;
1582 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1583 glEnable GL_TEXTURE_2D;
1584 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1585
1586 my $top = int $self->{children}[1]{range}[0];
1587
1588 my $y0 = $top;
1589 my $y1 = $top + $self->{h};
1590
1591 my $y = 0;
1592
1593 my $layout = $self->{layout};
1594
1595 for my $par (@{$self->{par}}) {
1596 my $h = $par->[0];
1597
1598 if ($y0 < $y + $h && $y < $y1) {
1599 $layout->set_text ($par->[2]);
1600
1601 glColor @{ $par->[1] };
1602 my ($W, $H) = $layout->size;
1603 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0);
1604 }
1605
1606 $y += $h;
1607 }
1608
1609 glDisable GL_TEXTURE_2D;
1610 glDisable GL_BLEND;
1611 };
1612 });
1613}
1614
1615sub _draw {
1616 my ($self) = @_;
1617
1618 if ($self->{texture}) {
1619 glEnable GL_TEXTURE_2D;
1620 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1621 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1622 glDisable GL_TEXTURE_2D;
1623 }
1624
1625 $self->{children}[1]->draw;
1626
1228} 1627}
1229 1628
1230############################################################################# 1629#############################################################################
1231 1630
1232package CFClient::UI::MapWidget; 1631package CFClient::UI::MapWidget;
1242 1641
1243sub new { 1642sub new {
1244 my $class = shift; 1643 my $class = shift;
1245 1644
1246 $class->SUPER::new ( 1645 $class->SUPER::new (
1247 z => -1, 1646 z => -1,
1647 can_focus => 1,
1248 list => (glGenLists 1), 1648 list => (glGenLists 1),
1249 @_ 1649 @_
1250 ) 1650 )
1251} 1651}
1252 1652
1253sub key_down { 1653sub key_down {
1257sub key_up { 1657sub key_up {
1258} 1658}
1259 1659
1260sub size_request { 1660sub size_request {
1261 ( 1661 (
1262 1 + int $::WIDTH / 32, 1662 1 + 32 * int $::WIDTH / 32,
1263 1 + int $::HEIGHT / 32, 1663 1 + 32 * int $::HEIGHT / 32,
1264 ) 1664 )
1265} 1665}
1266 1666
1267sub update { 1667sub update {
1268 my ($self) = @_; 1668 my ($self) = @_;
1269 1669
1270 $self->{need_update} = 1; 1670 $self->{need_update} = 1;
1271 $self->SUPER::update; 1671 $self->SUPER::update;
1272} 1672}
1273 1673
1274sub _draw { 1674sub draw {
1275 my ($self) = @_; 1675 my ($self) = @_;
1276 1676
1677 $self->{need_update}++;#d#
1277 if (delete $self->{need_update}) { 1678 if (delete $self->{need_update}) {
1278 glNewList $self->{list}, GL_COMPILE; 1679 glNewList $self->{list}, GL_COMPILE;
1279 1680
1280 my $mx = $::CONN->{mapx};
1281 my $my = $::CONN->{mapy};
1282
1283 my $map = $::CONN->{map};
1284
1285 my ($xofs, $yofs);
1286
1287 my $sw = 1 + int $::WIDTH / 32; 1681 my $sw = int $::WIDTH / 32;
1288 my $sh = 1 + int $::HEIGHT / 32; 1682 my $sh = int $::HEIGHT / 32;
1289 1683
1290 if ($::CONN->{mapw} > $sw) { 1684 if ($::MAP) {
1291 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 1685 my ($w, $h, $data) = $::MAP->draw (0, 0, $sw, $sh);
1292 } else {
1293 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
1294 }
1295 1686
1296 if ($::CONN->{maph} > $sh) { 1687 if ($::CFG->{fow_enable}) {
1297 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 1688 if ($::CFG->{fow_smooth}) { # smooth fog of war
1298 } else { 1689 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1299 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 1690 glConvolutionFilter2D
1300 } 1691 GL_CONVOLUTION_2D,
1301 1692 GL_ALPHA,
1302 glEnable GL_TEXTURE_2D; 1693 3, 3,
1303 glEnable GL_BLEND; 1694 GL_ALPHA, GL_FLOAT,
1304 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1695 pack "f*",
1305 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1696 0.1, 0.1, 0.1,
1306 1697 0.1, 0.2, 0.1,
1307 my $sw4 = ($sw + 3) & ~3; 1698 0.1, 0.1, 0.1,
1308 my $darkness = "\x00" x ($sw4 * $sh);
1309
1310 for my $x (0 .. $sw - 1) {
1311 my $row = $map->[$x + $xofs];
1312 for my $y (0 .. $sh - 1) {
1313
1314 my $cell = $row->[$y + $yofs]
1315 or next; 1699 ;
1316 1700 glEnable GL_CONVOLUTION_2D;
1317 my $dark = $cell->[0];
1318 if ($dark < 0) {
1319 substr $darkness, $y * $sw4 + $x, 1, chr 224;
1320 } else {
1321 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
1322 } 1701 }
1323 1702
1324 for my $num (grep $_, @$cell[1,2,3]) { 1703 my $tex = new CFClient::Texture
1325 my $tex = $::CONN->{face}[$num]{texture} || next; 1704 w => $w,
1326 1705 h => $h,
1327 my ($w, $h) = @$tex{qw(w h)}; 1706 data => $data,
1707 internalformat => GL_ALPHA,
1708 format => GL_ALPHA;
1328 1709
1329 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); 1710 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1330 } 1711
1712 glEnable GL_BLEND;
1713 glEnable GL_TEXTURE_2D;
1714 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1715
1716 glColor +($::CFG->{fow_intensity}) x 3, 1;
1717 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1718
1719 glDisable GL_TEXTURE_2D;
1720 glDisable GL_BLEND;
1331 } 1721 }
1332 } 1722 }
1333 1723
1334# if (1) { # higher quality darkness
1335# $lighting =~ s/(.)/$1$1$1/gs;
1336# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
1337#
1338# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
1339#
1340# $lighting = $pb->get_pixels;
1341# $lighting =~ s/(.)../$1/gs;
1342# }
1343
1344 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1345
1346 $darkness = new CFClient::Texture
1347 w => $sw4,
1348 h => $sh,
1349 data => $darkness,
1350 internalformat => GL_ALPHA,
1351 format => GL_ALPHA;
1352
1353 glColor 0.45, 0.45, 0.45, 1;
1354 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
1355
1356 glDisable GL_TEXTURE_2D;
1357 glDisable GL_BLEND;
1358
1359 glEndList; 1724 glEndList;
1360 } 1725 }
1361 1726
1362 glCallList $self->{list}; 1727 glCallList $self->{list};
1728
1729 if ($FOCUS != $self) {
1730 glColor 64/255, 64/255, 64/255;
1731 glLogicOp GL_AND;
1732 glEnable GL_COLOR_LOGIC_OP;
1733 glBegin GL_QUADS;
1734 glVertex 0, 0;
1735 glVertex 0, $::HEIGHT;
1736 glVertex $::WIDTH, $::HEIGHT;
1737 glVertex $::WIDTH, 0;
1738 glEnd;
1739 glDisable GL_COLOR_LOGIC_OP;
1740 }
1363} 1741}
1364 1742
1365my %DIR = ( 1743my %DIR = (
1366 SDLK_KP8, [1, "north"], 1744 SDLK_KP8, [1, "north"],
1367 SDLK_KP9, [2, "northeast"], 1745 SDLK_KP9, [2, "northeast"],
1383 1761
1384 my $mod = $ev->key_mod; 1762 my $mod = $ev->key_mod;
1385 my $sym = $ev->key_sym; 1763 my $sym = $ev->key_sym;
1386 1764
1387 if ($sym == SDLK_KP5) { 1765 if ($sym == SDLK_KP5) {
1388 $::CONN->send ("command stay fire"); 1766 $::CONN->user_send ("command stay fire");
1767 } elsif ($sym == SDLK_a) {
1768 $::CONN->user_send ("command apply");
1769 } elsif ($sym == SDLK_QUOTE) {
1770 $self->emit ('activate_console');
1771 } elsif ($sym == SDLK_SLASH) {
1772 $self->emit ('activate_console' => '/');
1389 } elsif (exists $DIR{$sym}) { 1773 } elsif (exists $DIR{$sym}) {
1390 if ($mod & KMOD_SHIFT) { 1774 if ($mod & KMOD_SHIFT) {
1391 $self->{shft}++; 1775 $self->{shft}++;
1392 $::CONN->send ("command fire $DIR{$sym}[0]"); 1776 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1393 } elsif ($mod & KMOD_CTRL) { 1777 } elsif ($mod & KMOD_CTRL) {
1394 $self->{ctrl}++; 1778 $self->{ctrl}++;
1395 $::CONN->send ("command run $DIR{$sym}[0]"); 1779 $::CONN->user_send ("command run $DIR{$sym}[0]");
1396 } else { 1780 } else {
1397 $::CONN->send ("command $DIR{$sym}[1]"); 1781 $::CONN->user_send ("command $DIR{$sym}[1]");
1398 } 1782 }
1399 } 1783 }
1400} 1784}
1401 1785
1402sub key_up { 1786sub key_up {
1404 1788
1405 my $mod = $ev->key_mod; 1789 my $mod = $ev->key_mod;
1406 my $sym = $ev->key_sym; 1790 my $sym = $ev->key_sym;
1407 1791
1408 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { 1792 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1409 $::CONN->send ("command fire_stop"); 1793 $::CONN->user_send ("command fire_stop");
1410 } 1794 }
1411 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 1795 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1412 $::CONN->send ("command run_stop"); 1796 $::CONN->user_send ("command run_stop");
1413 } 1797 }
1414} 1798}
1415 1799
1416############################################################################# 1800#############################################################################
1417 1801
1455 glPopMatrix; 1839 glPopMatrix;
1456} 1840}
1457 1841
1458############################################################################# 1842#############################################################################
1459 1843
1844package CFClient::UI::Flopper;
1845
1846our @ISA = CFClient::UI::Button::;
1847
1848sub new {
1849 my $class = shift;
1850
1851 my $self = $class->SUPER::new (
1852 state => 0,
1853 connect_activate => \&toggle_flopper,
1854 @_
1855 );
1856
1857 if ($self->{state}) {
1858 $self->{state} = 0;
1859 $self->toggle_flopper;
1860 }
1861
1862 $self
1863}
1864
1865sub toggle_flopper {
1866 my ($self) = @_;
1867
1868 # TODO: use animation
1869 if ($self->{state} = !$self->{state}) {
1870 $CFClient::UI::TOPLEVEL->add ($self->{other});
1871 $self->{other}->move (
1872 ($::WIDTH - $self->{other}{w}) * 0.5,
1873 ($::HEIGHT - $self->{other}{h}) * 0.5,
1874 );
1875 } else {
1876 $CFClient::UI::TOPLEVEL->remove ($self->{other});
1877 }
1878}
1879
1880#############################################################################
1881
1460package CFClient::UI::Toplevel; 1882package CFClient::UI::Toplevel;
1461 1883
1462our @ISA = CFClient::UI::Container::; 1884our @ISA = CFClient::UI::Container::;
1885
1886use SDL::OpenGL;
1463 1887
1464sub size_request { 1888sub size_request {
1465 ($::WIDTH, $::HEIGHT) 1889 ($::WIDTH, $::HEIGHT)
1466} 1890}
1467 1891
1493 $self->SUPER::add ($widget); 1917 $self->SUPER::add ($widget);
1494 1918
1495 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); 1919 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1496} 1920}
1497 1921
1922sub on_refresh {
1923 my ($self, $id, $cb) = @_;
1924
1925 $self->{refresh_hook}{$id} = $cb;
1926}
1927
1498sub draw { 1928sub draw {
1499 my ($self) = @_; 1929 my ($self) = @_;
1500 1930
1931 while (my $rcb = delete $self->{refresh_hook}) {
1932 $_->() for values %$rcb;
1933 }
1934
1935 glViewport 0, 0, $::WIDTH, $::HEIGHT;
1936 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
1937 glClear GL_COLOR_BUFFER_BIT;
1938
1939 glMatrixMode GL_PROJECTION;
1940 glLoadIdentity;
1941 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
1942 glMatrixMode GL_MODELVIEW;
1943 glLoadIdentity;
1944
1501 $self->_draw; 1945 $self->_draw;
1502} 1946}
1503 1947
1504############################################################################# 1948#############################################################################
1505 1949

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines