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.77 by root, Tue Apr 11 22:14:13 2006 UTC vs.
Revision 1.109 by root, Sat Apr 15 01:13:47 2006 UTC

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 {
443} 439}
444 440
445sub render_chld { 441sub render_chld {
446 my ($self) = @_; 442 my ($self) = @_;
447 443
448 $self->{texture} = 444 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
449 CFClient::Texture->new_from_opengl ( 445 glClearColor 0, 0, 0, 1;
450 $self->{w}, $self->{h}, sub { $self->child->draw } 446 glClear GL_COLOR_BUFFER_BIT;
451 ); 447 $self->child->draw;
448 };
452} 449}
453 450
454sub size_allocate { 451sub size_allocate {
455 my ($self, $x, $y, $w, $h) = @_; 452 my ($self, $x, $y, $w, $h) = @_;
456 453
534 531
535my @tex = 532my @tex =
536 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 533 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
537 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);
538 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
539sub size_request { 549sub size_request {
540 my ($self) = @_; 550 my ($self) = @_;
541 551
542 my ($w, $h) = 552 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
543 $self->{user_w} && $self->{user_h} 553
544 ? ($self->{user_w}, $self->{user_h})
545 : ($self->SUPER::size_request); 554 my ($w, $h) = $self->SUPER::size_request;
546 555
547 $h += $tex[1]->{h}; 556 (
548 $h += $tex[4]->{h}; 557 $w + $self->{border} * 2,
549 $w += $tex[2]->{w}; 558 $h + $self->{border} * 2,
550 $w += $tex[3]->{w}; 559 )
551
552 ($w, $h)
553} 560}
554 561
555sub size_allocate { 562sub size_allocate {
556 my ($self, $x, $y, $w, $h) = @_; 563 my ($self, $x, $y, $w, $h) = @_;
557 564
558 $self->_size_allocate ($x, $y, $w, $h) or return; 565 $self->_size_allocate ($x, $y, $w, $h) or return;
559 566
560 $h -= $tex[1]{h}; 567 $h -= List::Util::max 0, $self->{border} * 2;
561 $h -= $tex[4]{h}; 568 $w -= List::Util::max 0, $self->{border} * 2;
562 $w -= $tex[2]{w};
563 $w -= $tex[3]{w};
564 569
565 $h = $h < 0 ? 0 : $h; 570 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h);
566 $w = $w < 0 ? 0 : $w;
567
568 my $child = $self->child;
569
570 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
571} 571}
572 572
573sub button_down { 573sub button_down {
574 my ($self, $ev, $x, $y) = @_; 574 my ($self, $ev, $x, $y) = @_;
575 575
576 if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w} 576 if ($x < $self->{w} && $x >= $self->{w} - $self->{border}
577 && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) { 577 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) {
578 578
579 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 579 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
580 my ($bw, $bh) = ($self->{w}, $self->{h}); 580 my ($bw, $bh) = ($self->{w}, $self->{h});
581 581
582 $self->{motion} = sub { 582 $self->{motion} = sub {
588 $self->{user_h} = $bh + $y - $oy; 588 $self->{user_h} = $bh + $y - $oy;
589 $self->update; 589 $self->update;
590 }; 590 };
591 591
592 } elsif ($x >= 0 && $x < $self->{w} 592 } elsif ($x >= 0 && $x < $self->{w}
593 && $y >= 0 && $y < $tex[1]{h}) { 593 && $y >= 0 && $y < $self->{border}) {
594 594
595 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 595 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
596 my ($bx, $by) = ($self->{x}, $self->{y}); 596 my ($bx, $by) = ($self->{x}, $self->{y});
597 597
598 $self->{motion} = sub { 598 $self->{motion} = sub {
619} 619}
620 620
621sub _draw { 621sub _draw {
622 my ($self) = @_; 622 my ($self) = @_;
623 623
624 my ($w, $h) = ($self->{w}, $self->{h}); 624 my ($w, $h ) = ($self->{w}, $self->{h});
625 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 625 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
626 626
627 glEnable GL_BLEND; 627 glEnable GL_BLEND;
628 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 628 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
629 glEnable GL_TEXTURE_2D; 629 glEnable GL_TEXTURE_2D;
630 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 630 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
631 631
632 my $top = $tex[1]; 632 glColor @{ $self->{border_bg} };
633 $top->draw_quad (0, 0, $w, $top->{h}); 633 $tex[1]->draw_quad (0, 0, $w, $self->{border});
634 634 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch);
635 my $left = $tex[3]; 635 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch);
636 $left->draw_quad (0, $top->{h}, $left->{w}, $ch); 636 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border});
637
638 my $right = $tex[2];
639 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
640
641 my $bottom = $tex[4];
642 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
643 637
644 my $bg = $tex[0]; 638 my $bg = $tex[0];
645 639
646 glBindTexture GL_TEXTURE_2D, $bg->{name}; 640 # TODO: repeat texture not scale
647 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
648 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
649 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
650
651 my $rep_x = $cw / $bg->{w}; 641 my $rep_x = $cw / $bg->{w};
652 my $rep_y = $ch / $bg->{h}; 642 my $rep_y = $ch / $bg->{h};
653 643
644 glColor @{ $self->{bg} };
645
646 $bg->{s} = $rep_x;
647 $bg->{t} = $rep_y;
648 $bg->{wrap_mode} = 1;
654 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); 649 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch);
655 650
656 glDisable GL_TEXTURE_2D; 651 glDisable GL_TEXTURE_2D;
657 glDisable GL_BLEND; 652 glDisable GL_BLEND;
658 653
659 $self->child->draw; 654 $self->child->draw;
660
661} 655}
662 656
663############################################################################# 657#############################################################################
664 658
665package CFClient::UI::Table; 659package CFClient::UI::Table;
667our @ISA = CFClient::UI::Base::; 661our @ISA = CFClient::UI::Base::;
668 662
669use List::Util qw(max sum); 663use List::Util qw(max sum);
670 664
671use SDL::OpenGL; 665use SDL::OpenGL;
666
667sub new {
668 my $class = shift;
669
670 $class->SUPER::new (
671 col_expand => [],
672 @_
673 )
674}
672 675
673sub add { 676sub add {
674 my ($self, $x, $y, $chld) = @_; 677 my ($self, $x, $y, $chld) = @_;
675 678
676 $self->{children}[$y][$x] = $chld; 679 $self->{children}[$y][$x] = $chld;
706 my ($self) = @_; 709 my ($self) = @_;
707 710
708 my ($ws, $hs) = $self->get_wh; 711 my ($ws, $hs) = $self->get_wh;
709 712
710 ( 713 (
711 (List::Util::sum @$ws), 714 (sum @$ws),
712 (List::Util::sum @$hs), 715 (sum @$hs),
713 ) 716 )
714} 717}
715 718
716sub size_allocate { 719sub size_allocate {
717 my ($self, $x, $y, $w, $h) = @_; 720 my ($self, $x, $y, $w, $h) = @_;
718 721
719 $self->_size_allocate ($x, $y, $w, $h) or return; 722 $self->_size_allocate ($x, $y, $w, $h) or return;
720 723
721 my ($ws, $hs) = $self->get_wh; 724 my ($ws, $hs) = $self->get_wh;
722 725
723 my $req_w = List::Util::sum @$ws; 726 my $req_w = sum @$ws;
724 my $req_h = List::Util::sum @$hs; 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;
725 733
726 # linearly scale sizes 734 # linearly scale sizes
727 $_ *= $w / $req_w for @$ws; 735 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
728 $_ *= $h / $req_h for @$hs; 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
729 740
730 my $y; 741 my $y;
731 742
732 for my $r (0 .. $#{$self->{children}}) { 743 for my $r (0 .. $#{$self->{children}}) {
733 my $row = $self->{children}[$r] 744 my $row = $self->{children}[$r]
735 746
736 my $x = 0; 747 my $x = 0;
737 my $row_h = $hs->[$r]; 748 my $row_h = $hs->[$r];
738 749
739 for my $c (0 .. $#$row) { 750 for my $c (0 .. $#$row) {
740 my $widget = $row->[$c]
741 or next;
742
743 my $col_w = $ws->[$c]; 751 my $col_w = $ws->[$c];
744 752
753 if (my $widget = $row->[$c]) {
745 $widget->size_allocate ($x, $y, $col_w, $row_h); 754 $widget->size_allocate ($x, $y, $col_w, $row_h);
755 }
746 756
747 $x += $col_w; 757 $x += $col_w;
748 } 758 }
749 759
750 $y += $row_h; 760 $y += $row_h;
796} 806}
797 807
798sub size_allocate { 808sub size_allocate {
799 my ($self, $x, $y, $w, $h) = @_; 809 my ($self, $x, $y, $w, $h) = @_;
800 810
801 $self->_size_allocate ($x, $y, $w, $h) or return; 811 $self->_size_allocate ($x, $y, $w, $h);
802
803 return unless $self->{w};
804 812
805 ($h, $w) = ($w, $h); 813 ($h, $w) = ($w, $h);
806 814
807 my $children = $self->{children}; 815 my $children = $self->{children};
808 816
810 818
811 my $req_h = List::Util::sum @h; 819 my $req_h = List::Util::sum @h;
812 820
813 if ($req_h > $h) { 821 if ($req_h > $h) {
814 # ah well, not enough space 822 # ah well, not enough space
815 $_ = $h[$_] * $h / $req_h for @h; 823 $_ *= $h / $req_h for @h;
816 } else { 824 } else {
817 my $exp = List::Util::sum map $_->{expand}, @$children; 825 my $exp = List::Util::sum map $_->{expand}, @$children;
818 $exp ||= 1; 826 $exp ||= 1;
819 827
820 for (0 .. $#$children) { 828 for (0 .. $#$children) {
827 } 835 }
828 836
829 my $y = 0; 837 my $y = 0;
830 for (0 .. $#$children) { 838 for (0 .. $#$children) {
831 my $child = $children->[$_]; 839 my $child = $children->[$_];
832 my $h = $h[$_]; 840 my $h = int $h[$_];
833 $child->size_allocate ($y, 0, $h, $w); 841 $child->size_allocate ($y, 0, $h, $w);
834 842
835 $y += $h; 843 $y += $h;
836 } 844 }
837} 845}
856} 864}
857 865
858sub size_allocate { 866sub size_allocate {
859 my ($self, $x, $y, $w, $h) = @_; 867 my ($self, $x, $y, $w, $h) = @_;
860 868
861 $self->_size_allocate ($x, $y, $w, $h) or return; 869 $self->_size_allocate ($x, $y, $w, $h);
862
863 return unless $self->{h};
864 870
865 my $children = $self->{children}; 871 my $children = $self->{children};
866 872
867 my @h = map +($_->size_request)[1], @$children; 873 my @h = map +($_->size_request)[1], @$children;
868 874
869 my $req_h = List::Util::sum @h; 875 my $req_h = List::Util::sum @h;
870 876
871 if ($req_h > $h) { 877 if ($req_h > $h) {
872 # ah well, not enough space 878 # ah well, not enough space
873 $_ = $h[$_] * $h / $req_h for @h; 879 $_ *= $h / $req_h for @h;
874 } else { 880 } else {
875 my $exp = List::Util::sum map $_->{expand}, @$children; 881 my $exp = List::Util::sum map $_->{expand}, @$children;
876 $exp ||= 1; 882 $exp ||= 1;
877 883
878 for (0 .. $#$children) { 884 for (0 .. $#$children) {
883 } 889 }
884 890
885 my $y = 0; 891 my $y = 0;
886 for (0 .. $#$children) { 892 for (0 .. $#$children) {
887 my $child = $children->[$_]; 893 my $child = $children->[$_];
888 my $h = $h[$_]; 894 my $h = int $h[$_];
889 $child->size_allocate (0, $y, $w, $h); 895 $child->size_allocate (0, $y, $w, $h);
890 896
891 $y += $h; 897 $y += $h;
892 } 898 }
893} 899}
902 908
903sub new { 909sub new {
904 my ($class, %arg) = @_; 910 my ($class, %arg) = @_;
905 911
906 my $self = $class->SUPER::new ( 912 my $self = $class->SUPER::new (
907 fg => [1, 1, 1], 913 fg => [1, 1, 1],
908 height => $::FONTSIZE, 914 fontsize => $::FONTSIZE,
909 text => "", 915 text => "",
910 align => -1, 916 align => -1,
911 padding => 2, 917 padding => 2,
912 layout => new CFClient::Layout, 918 layout => new CFClient::Layout,
913 %arg 919 %arg
914 ); 920 );
915 921
916 $self->set_text ($self->{text}); 922 $self->set_text ($self->{text});
917 923
933 939
934 $self->{text} = $text; 940 $self->{text} = $text;
935 $self->{layout}->set_markup ($text); 941 $self->{layout}->set_markup ($text);
936 942
937 delete $self->{texture}; 943 delete $self->{texture};
944# $self->{w} = $self->{h} = -1;
938 $self->update; 945 $self->update;
939} 946}
940 947
941sub get_text { 948sub get_text {
942 my ($self, $text) = @_; 949 my ($self, $text) = @_;
946 953
947sub size_request { 954sub size_request {
948 my ($self) = @_; 955 my ($self) = @_;
949 956
950 $self->{layout}->set_width; 957 $self->{layout}->set_width;
951 $self->{layout}->set_height ($self->{height}); 958 $self->{layout}->set_height ($self->{fontsize});
952 my ($w, $h) = $self->{layout}->size; 959 my ($w, $h) = $self->{layout}->size;
953 960
954 ( 961 (
955 $w + $self->{padding} * 2, 962 $w + $self->{padding} * 2,
956 $h + $self->{padding} * 2, 963 $h + $self->{padding} * 2,
975sub _draw { 982sub _draw {
976 my ($self) = @_; 983 my ($self) = @_;
977 984
978 my $tex = $self->{texture} ||= do { 985 my $tex = $self->{texture} ||= do {
979 $self->{layout}->set_width ($self->{w}); 986 $self->{layout}->set_width ($self->{w});
987 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
980 new_from_layout CFClient::Texture $self->{layout} 988 new_from_layout CFClient::Texture $self->{layout}
981 }; 989 };
982 990
983 glEnable GL_BLEND; 991 glEnable GL_BLEND;
984 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 992 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
985 glEnable GL_TEXTURE_2D; 993 glEnable GL_TEXTURE_2D;
986 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 994 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
987 995
988 glColor @{$self->{fg}}; 996 glColor @{$self->{fg}};
989 997
990 my $x = 998 my $x =
991 $self->{align} < 0 ? $self->{padding} 999 $self->{align} < 0 ? $self->{padding}
992 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1000 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
993 : ($self->{w} - $tex->{w}) * 0.5; 1001 : ($self->{w} - $tex->{w}) * 0.5;
994 1002
995 $tex->draw_quad ($x, $self->{padding}); 1003 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5);
996 1004
997 glDisable GL_TEXTURE_2D; 1005 glDisable GL_TEXTURE_2D;
998 glDisable GL_BLEND; 1006 glDisable GL_BLEND;
999} 1007}
1000 1008
1011 my $class = shift; 1019 my $class = shift;
1012 1020
1013 $class->SUPER::new ( 1021 $class->SUPER::new (
1014 fg => [1, 1, 1], 1022 fg => [1, 1, 1],
1015 bg => [0, 0, 0, 0.2], 1023 bg => [0, 0, 0, 0.2],
1016 active_bg => [1, 1, 1], 1024 active_bg => [1, 1, 1, 0.5],
1017 active_fg => [0, 0, 0], 1025 active_fg => [0, 0, 0],
1026 can_hover => 1,
1027 can_focus => 1,
1018 @_ 1028 @_
1019 ) 1029 )
1020} 1030}
1021 1031
1022sub _set_text { 1032sub _set_text {
1023 my ($self, $text) = @_; 1033 my ($self, $text) = @_;
1024 1034
1035 my $old_text = $self->{text};
1036
1025 $self->{last_activity} = $::NOW; 1037 $self->{last_activity} = $::NOW;
1026 1038
1027 $self->{text} = $text; 1039 $self->{text} = $text;
1028 $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});
1029 1042
1030 $text =~ s/./*/g if $self->{hidden}; 1043 $text =~ s/./*/g if $self->{hidden};
1031 1044
1032 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1045 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
1033 1046
1034 $text = substr $text, 0, $self->{cursor}; 1047 $text = substr $text, 0, $self->{cursor};
1035 utf8::encode $text; 1048 utf8::encode $text;
1036 1049
1037 @$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};
1038} 1054}
1039 1055
1040sub size_request { 1056sub size_request {
1041 my ($self) = @_; 1057 my ($self) = @_;
1042 1058
1081 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1097 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1082 } elsif ($sym == SDLK_HOME) { 1098 } elsif ($sym == SDLK_HOME) {
1083 $self->{cursor} = 0; 1099 $self->{cursor} = 0;
1084 } elsif ($sym == SDLK_END) { 1100 } elsif ($sym == SDLK_END) {
1085 $self->{cursor} = length $text; 1101 $self->{cursor} = length $text;
1102 } elsif ($sym == SDLK_ESCAPE) {
1103 $self->emit ('escape');
1086 } elsif ($uni) { 1104 } elsif ($uni) {
1087 substr $text, $self->{cursor}++, 0, chr $uni; 1105 substr $text, $self->{cursor}++, 0, chr $uni;
1088 } 1106 }
1089 1107
1090 $self->_set_text ($text); 1108 $self->_set_text ($text);
1152 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1170 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h};
1153 glEnd; 1171 glEnd;
1154 } 1172 }
1155} 1173}
1156 1174
1157#############################################################################
1158
1159package CFClient::UI::Slider; 1175package CFClient::UI::LineEntry;
1160 1176
1161use strict; 1177our @ISA = CFClient::UI::Entry::;
1162 1178
1179use SDL;
1163use SDL::OpenGL; 1180use SDL::OpenGL;
1164 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
1165our @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);
1166 1209
1167sub new { 1210sub new {
1168 my $class = shift; 1211 my $class = shift;
1169 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
1170 # range [value, low, high, page] 1345 # range [value, low, high, page]
1171 1346
1347 # TODO: 0-width page
1348 # TODO: req_w/h are wrong with vertical
1349 # TODO: calculations are off
1172 my $self = $class->SUPER::new ( 1350 my $self = $class->SUPER::new (
1173 fg => [1, 1, 1], 1351 fg => [1, 1, 1],
1174 active_fg => [0, 0, 0], 1352 active_fg => [0, 0, 0],
1175 range => [0, 0, 100, 10], 1353 range => [0, 0, 100, 10],
1176 req_w => 40, 1354 req_w => 40,
1177 req_h => 10, 1355 req_h => 13,
1178 vertical => 0, 1356 vertical => 0,
1357 can_hover => 1,
1358 inner_pad => 5,
1179 @_ 1359 @_
1180 ); 1360 );
1181 1361
1182 $self 1362 $self
1183} 1363}
1204 if ($GRAB == $self) { 1384 if ($GRAB == $self) {
1205 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1385 my ($value, $lo, $hi, $page) = @{$self->{range}};
1206 1386
1207 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 1387 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1208 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
1209 $x = $x * ($hi - $lo) / $w + $lo; 1393 $x = $x * ($hi - $lo) / $inner_w + $lo;
1210 $x = $lo if $x < $lo; 1394 $x = $lo if $x < $lo;
1211 $x = $hi - $page if $x > $hi - $page; 1395 $x = $hi - $page if $x > $hi - $page;
1212 $self->{range}[0] = $x; 1396 $self->{range}[0] = $x;
1213 1397
1214 $self->emit (changed => $x); 1398 $self->emit (changed => $x);
1215 $self->update; 1399 $self->update;
1216 } 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
1217} 1409}
1218 1410
1219sub _draw { 1411sub _draw {
1220 my ($self) = @_; 1412 my ($self) = @_;
1221 1413
1235 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1427 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1236 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1428 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1237 1429
1238 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1430 my ($value, $lo, $hi, $page) = @{$self->{range}};
1239 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
1240 $page = int $page * $w / ($hi - $lo); 1437 $page = int $page * $inner_w / ($hi - $lo);
1241 $value = int +($value - $lo) * $w / ($hi - $lo); 1438 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1242 1439
1243 $w -= $page; 1440 $w -= $page;
1244 $page &= ~1; 1441 $page &= ~1;
1245 glTranslate $page * 0.5, 0, 0; 1442 glTranslate $page * 0.5, 0, 0;
1443 $page ||= 2;
1246 1444
1247 glColor @$fg;
1248 glBegin GL_LINES;
1249 glVertex 0, 0; glVertex 0, $h;
1250 glVertex $w - 1, 0; glVertex $w - 1, $h;
1251 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1252 glEnd;
1253
1254 my $knob_a = $value - $page * 0.5; 1445 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1255 my $knob_b = $value + $page * 0.5; 1446 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1256 1447
1257 glBegin GL_QUADS; 1448 glEnable GL_BLEND;
1258 glColor @$fg; 1449 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1259 glVertex $knob_a, 0; 1450 glEnable GL_TEXTURE_2D;
1260 glVertex $knob_a, $h; 1451 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1261 glVertex $knob_b, $h;
1262 glVertex $knob_b, 0;
1263 1452
1264 if ($knob_a < $knob_b - 2) { 1453 # draw background
1265 glColor @$bg; 1454 $tex[1]->draw_quad (0, 0, $w, $h);
1266 glVertex $knob_a + 1, 1; 1455
1267 glVertex $knob_a + 1, $h - 1; 1456 # draw handle
1268 glVertex $knob_b - 1, $h - 1; 1457 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h);
1269 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);
1270 } 1511
1271 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
1272} 1627}
1273 1628
1274############################################################################# 1629#############################################################################
1275 1630
1276package CFClient::UI::MapWidget; 1631package CFClient::UI::MapWidget;
1286 1641
1287sub new { 1642sub new {
1288 my $class = shift; 1643 my $class = shift;
1289 1644
1290 $class->SUPER::new ( 1645 $class->SUPER::new (
1291 z => -1, 1646 z => -1,
1647 can_focus => 1,
1292 list => (glGenLists 1), 1648 list => (glGenLists 1),
1293 @_ 1649 @_
1294 ) 1650 )
1295} 1651}
1296 1652
1297sub key_down { 1653sub key_down {
1298 print "MAPKEYDOWN\n"; 1654 print "MAPKEYDOWN\n";
1299} 1655}
1300 1656
1301sub key_up { 1657sub key_up {
1658}
1659
1660sub button_down {
1661 my ($self, $ev, $x, $y) = @_;
1662
1663 $self->focus_in;
1664
1665 if ($ev->button == 2) {
1666 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1667 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1668
1669 $self->{motion} = sub {
1670 my ($ev, $x, $y) = @_;
1671
1672 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1673
1674 $::CFG->{map_shift_x} = $bw + $x - $ox;
1675 $::CFG->{map_shift_y} = $bh + $y - $oy;
1676
1677 $self->update;
1678 };
1679 }
1680}
1681
1682sub button_up {
1683 my ($self, $ev, $x, $y) = @_;
1684
1685 delete $self->{motion};
1686}
1687
1688sub mouse_motion {
1689 my ($self, $ev, $x, $y) = @_;
1690
1691 $self->{motion}->($ev, $x, $y) if $self->{motion};
1302} 1692}
1303 1693
1304sub size_request { 1694sub size_request {
1305 ( 1695 (
1306 1 + 32 * int $::WIDTH / 32, 1696 1 + 32 * int $::WIDTH / 32,
1319 my ($self) = @_; 1709 my ($self) = @_;
1320 1710
1321 if (delete $self->{need_update}) { 1711 if (delete $self->{need_update}) {
1322 glNewList $self->{list}, GL_COMPILE; 1712 glNewList $self->{list}, GL_COMPILE;
1323 1713
1324 my $mx = $::CONN->{mapx}; 1714 if ($::MAP) {
1325 my $my = $::CONN->{mapy};
1326
1327 my $map = $::CONN->{map};
1328
1329 my ($xofs, $yofs);
1330
1331 my $sw = 1 + int $::WIDTH / 32; 1715 my $sw = int $::WIDTH / 32;
1332 my $sh = 1 + int $::HEIGHT / 32; 1716 my $sh = int $::HEIGHT / 32;
1333 1717
1334 if ($::CONN->{mapw} > $sw) { 1718 my $sx = $::CFG->{map_shift_x};
1335 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 1719 my $sy = $::CFG->{map_shift_y};
1336 } else { 1720
1337 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 1721 glTranslate +($sx & 31) - 32, ($sy & 31) - 32, 0;
1722
1723 my ($w, $h, $data) = $::MAP->draw ($sx >> 5, $sy >> 5, 0, 0, $sw + 1, $sh + 1);
1724
1725 if ($::CFG->{fow_enable}) {
1726 if ($::CFG->{fow_smooth}) { # smooth fog of war
1727 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1728 glConvolutionFilter2D
1729 GL_CONVOLUTION_2D,
1730 GL_ALPHA,
1731 3, 3,
1732 GL_ALPHA, GL_FLOAT,
1733 pack "f*",
1734 0.1, 0.1, 0.1,
1735 0.1, 0.2, 0.1,
1736 0.1, 0.1, 0.1,
1737 ;
1738 glEnable GL_CONVOLUTION_2D;
1739 }
1740
1741 my $tex = new CFClient::Texture
1742 w => $w,
1743 h => $h,
1744 data => $data,
1745 internalformat => GL_ALPHA,
1746 format => GL_ALPHA;
1747
1748 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1749
1750 glEnable GL_BLEND;
1751 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1752 glEnable GL_TEXTURE_2D;
1753 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1754
1755 glColor +($::CFG->{fow_intensity}) x 3, 1;
1756 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1757
1758 glDisable GL_TEXTURE_2D;
1759 glDisable GL_BLEND;
1760 }
1761
1762 # HACK BEGIN
1763 {
1764 my ($w, $h) = (250, 250);
1765
1766 glEnable GL_BLEND;
1767 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1768 glEnable GL_TEXTURE_2D;
1769 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1770
1771 CFClient::Texture->new (
1772 w => $w,
1773 h => $h,
1774 data => $::MAP->mapmap ($w, $h),
1775 type => GL_UNSIGNED_INT_8_8_8_8_REV
1776 )->draw_quad (100, 100);
1777
1778 glDisable GL_TEXTURE_2D;
1779 glDisable GL_BLEND;
1780 }
1781 # HACK END
1338 } 1782 }
1339 1783
1340 if ($::CONN->{maph} > $sh) {
1341 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
1342 } else {
1343 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
1344 }
1345
1346 glEnable GL_TEXTURE_2D;
1347 glEnable GL_BLEND;
1348 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1349 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1350
1351 my $sw4 = ($sw + 3) & ~3;
1352 my $darkness = "\x00" x ($sw4 * $sh);
1353
1354 for my $x (0 .. $sw - 1) {
1355 my $row = $map->[$x + $xofs];
1356 for my $y (0 .. $sh - 1) {
1357
1358 my $cell = $row->[$y + $yofs]
1359 or next;
1360
1361 my $dark = $cell->[0];
1362 if ($dark < 0) {
1363 substr $darkness, $y * $sw4 + $x, 1, chr 224;
1364 } else {
1365 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
1366 }
1367
1368 for my $num (grep $_, @$cell[1,2,3]) {
1369 my $tex = $::CONN->{face}[$num]{texture} || next;
1370
1371 my ($w, $h) = @$tex{qw(w h)};
1372
1373 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
1374 }
1375 }
1376 }
1377
1378# if (1) { # higher quality darkness
1379# $lighting =~ s/(.)/$1$1$1/gs;
1380# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
1381#
1382# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
1383#
1384# $lighting = $pb->get_pixels;
1385# $lighting =~ s/(.)../$1/gs;
1386# }
1387
1388 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1389
1390 $darkness = new CFClient::Texture
1391 w => $sw4,
1392 h => $sh,
1393 data => $darkness,
1394 internalformat => GL_ALPHA,
1395 format => GL_ALPHA;
1396
1397 glColor 0.45, 0.45, 0.45, 1;
1398 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
1399
1400 glDisable GL_TEXTURE_2D;
1401 glDisable GL_BLEND;
1402
1403 glEndList; 1784 glEndList;
1404 } 1785 }
1405 1786
1787 glPushMatrix;
1406 glCallList $self->{list}; 1788 glCallList $self->{list};
1789 glPopMatrix;
1790
1791 if ($FOCUS != $self) {
1792 glColor 64/255, 64/255, 64/255;
1793 glLogicOp GL_AND;
1794 glEnable GL_COLOR_LOGIC_OP;
1795 glBegin GL_QUADS;
1796 glVertex 0, 0;
1797 glVertex 0, $::HEIGHT;
1798 glVertex $::WIDTH, $::HEIGHT;
1799 glVertex $::WIDTH, 0;
1800 glEnd;
1801 glDisable GL_COLOR_LOGIC_OP;
1802 }
1407} 1803}
1408 1804
1409my %DIR = ( 1805my %DIR = (
1410 SDLK_KP8, [1, "north"], 1806 SDLK_KP8, [1, "north"],
1411 SDLK_KP9, [2, "northeast"], 1807 SDLK_KP9, [2, "northeast"],
1427 1823
1428 my $mod = $ev->key_mod; 1824 my $mod = $ev->key_mod;
1429 my $sym = $ev->key_sym; 1825 my $sym = $ev->key_sym;
1430 1826
1431 if ($sym == SDLK_KP5) { 1827 if ($sym == SDLK_KP5) {
1432 $::CONN->send ("command stay fire"); 1828 $::CONN->user_send ("command stay fire");
1829 } elsif ($sym == SDLK_a) {
1830 $::CONN->user_send ("command apply");
1831 } elsif ($sym == SDLK_QUOTE) {
1832 $self->emit ('activate_console');
1833 } elsif ($sym == SDLK_SLASH) {
1834 $self->emit ('activate_console' => '/');
1433 } elsif (exists $DIR{$sym}) { 1835 } elsif (exists $DIR{$sym}) {
1434 if ($mod & KMOD_SHIFT) { 1836 if ($mod & KMOD_SHIFT) {
1435 $self->{shft}++; 1837 $self->{shft}++;
1436 $::CONN->send ("command fire $DIR{$sym}[0]"); 1838 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1437 } elsif ($mod & KMOD_CTRL) { 1839 } elsif ($mod & KMOD_CTRL) {
1438 $self->{ctrl}++; 1840 $self->{ctrl}++;
1439 $::CONN->send ("command run $DIR{$sym}[0]"); 1841 $::CONN->user_send ("command run $DIR{$sym}[0]");
1440 } else { 1842 } else {
1441 $::CONN->send ("command $DIR{$sym}[1]"); 1843 $::CONN->user_send ("command $DIR{$sym}[1]");
1442 } 1844 }
1443 } 1845 }
1444} 1846}
1445 1847
1446sub key_up { 1848sub key_up {
1448 1850
1449 my $mod = $ev->key_mod; 1851 my $mod = $ev->key_mod;
1450 my $sym = $ev->key_sym; 1852 my $sym = $ev->key_sym;
1451 1853
1452 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { 1854 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1453 $::CONN->send ("command fire_stop"); 1855 $::CONN->user_send ("command fire_stop");
1454 } 1856 }
1455 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 1857 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1456 $::CONN->send ("command run_stop"); 1858 $::CONN->user_send ("command run_stop");
1457 } 1859 }
1458} 1860}
1459 1861
1460############################################################################# 1862#############################################################################
1461 1863
1499 glPopMatrix; 1901 glPopMatrix;
1500} 1902}
1501 1903
1502############################################################################# 1904#############################################################################
1503 1905
1906package CFClient::UI::Flopper;
1907
1908our @ISA = CFClient::UI::Button::;
1909
1910sub new {
1911 my $class = shift;
1912
1913 my $self = $class->SUPER::new (
1914 state => 0,
1915 connect_activate => \&toggle_flopper,
1916 @_
1917 );
1918
1919 if ($self->{state}) {
1920 $self->{state} = 0;
1921 $self->toggle_flopper;
1922 }
1923
1924 $self
1925}
1926
1927sub toggle_flopper {
1928 my ($self) = @_;
1929
1930 # TODO: use animation
1931 if ($self->{state} = !$self->{state}) {
1932 $CFClient::UI::TOPLEVEL->add ($self->{other});
1933 $self->{other}->move (
1934 ($::WIDTH - $self->{other}{w}) * 0.5,
1935 ($::HEIGHT - $self->{other}{h}) * 0.5,
1936 );
1937 } else {
1938 $CFClient::UI::TOPLEVEL->remove ($self->{other});
1939 }
1940}
1941
1942#############################################################################
1943
1504package CFClient::UI::Toplevel; 1944package CFClient::UI::Toplevel;
1505 1945
1506our @ISA = CFClient::UI::Container::; 1946our @ISA = CFClient::UI::Container::;
1947
1948use SDL::OpenGL;
1507 1949
1508sub size_request { 1950sub size_request {
1509 ($::WIDTH, $::HEIGHT) 1951 ($::WIDTH, $::HEIGHT)
1510} 1952}
1511 1953
1537 $self->SUPER::add ($widget); 1979 $self->SUPER::add ($widget);
1538 1980
1539 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); 1981 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1540} 1982}
1541 1983
1984sub on_refresh {
1985 my ($self, $id, $cb) = @_;
1986
1987 $self->{refresh_hook}{$id} = $cb;
1988}
1989
1542sub draw { 1990sub draw {
1543 my ($self) = @_; 1991 my ($self) = @_;
1544 1992
1993 while (my $rcb = delete $self->{refresh_hook}) {
1994 $_->() for values %$rcb;
1995 }
1996
1997 glViewport 0, 0, $::WIDTH, $::HEIGHT;
1998 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
1999 glClear GL_COLOR_BUFFER_BIT;
2000
2001 glMatrixMode GL_PROJECTION;
2002 glLoadIdentity;
2003 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2004 glMatrixMode GL_MODELVIEW;
2005 glLoadIdentity;
2006
1545 $self->_draw; 2007 $self->_draw;
1546} 2008}
1547 2009
1548############################################################################# 2010#############################################################################
1549 2011

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines