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.75 by root, Tue Apr 11 20:44:49 2006 UTC vs.
Revision 1.81 by root, Wed Apr 12 00:40:45 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;
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;
181 glPopMatrix; 189 glPopMatrix;
182 190
183 if ($self == $HOVER) { 191 if ($self == $HOVER) {
184 my ($x, $y) = @$self{qw(x y)}; 192 my ($x, $y) = @$self{qw(x y)};
185 193
186 glColor 1, 1, 1, 0.1; 194 glColor 0, 0, 1, 0.2;
187 glEnable GL_BLEND; 195 glEnable GL_BLEND;
188 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 196 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
189 glBegin GL_QUADS; 197 glBegin GL_QUADS;
190 glVertex $x , $y; 198 glVertex $x , $y;
191 glVertex $x + $self->{w}, $y; 199 glVertex $x + $self->{w}, $y;
200 my ($self) = @_; 208 my ($self) = @_;
201 209
202 warn "no draw defined for $self\n"; 210 warn "no draw defined for $self\n";
203} 211}
204 212
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 { 213sub find_widget {
217 my ($self, $x, $y) = @_; 214 my ($self, $x, $y) = @_;
218 215
219 return $self 216 return $self
220 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 217 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
221 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 218 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
222 219
223 () 220 ()
224} 221}
225 222
226sub del_parent { $_[0]->{parent} = undef }
227
228sub set_parent { 223sub set_parent {
229 my ($self, $par) = @_; 224 my ($self, $par) = @_;
230 225
231 $self->{parent} = $par; 226 $self->{parent} = $par;
232 Scalar::Util::weaken $self->{parent}; 227 Scalar::Util::weaken $self->{parent};
275 my $class = shift; 270 my $class = shift;
276 271
277 # range [value, low, high, page] 272 # range [value, low, high, page]
278 273
279 $class->SUPER::new ( 274 $class->SUPER::new (
280 bg => [0, 0, 0, 0.4], 275 bg => [0, 0, 0, 0.2],
281 active_bg => [1, 1, 1], 276 active_bg => [1, 1, 1, 0.5],
282 @_ 277 @_
283 ) 278 )
284} 279}
285 280
286sub _draw { 281sub _draw {
287 my ($self) = @_; 282 my ($self) = @_;
288 283
289 my ($w, $h) = @$self{qw(w h)}; 284 my ($w, $h) = @$self{qw(w h)};
290 285
286 glEnable GL_BLEND;
287 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
291 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 288 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
289
292 glBegin GL_QUADS; 290 glBegin GL_QUADS;
293 glVertex 0 , 0; 291 glVertex 0 , 0;
294 glVertex 0 , $h; 292 glVertex 0 , $h;
295 glVertex $w, $h; 293 glVertex $w, $h;
296 glVertex $w, 0; 294 glVertex $w, 0;
297 glEnd; 295 glEnd;
296
297 glDisable GL_BLEND;
298} 298}
299 299
300############################################################################# 300#############################################################################
301 301
302package CFClient::UI::Empty; 302package CFClient::UI::Empty;
325 325
326 $self 326 $self
327} 327}
328 328
329sub add { 329sub add {
330 my ($self, $chld, $expand) = @_; 330 my ($self, $chld) = @_;
331 331
332 $chld->{expand} = $expand;
333 $chld->set_parent ($self); 332 $chld->set_parent ($self);
334 333
335 $self->{children} = [ 334 $self->{children} = [
336 sort { $a->{z} <=> $b->{z} } 335 sort { $a->{z} <=> $b->{z} }
337 @{$self->{children}}, $chld 336 @{$self->{children}}, $chld
533 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 532 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
534 533
535sub size_request { 534sub size_request {
536 my ($self) = @_; 535 my ($self) = @_;
537 536
537 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
538
538 my ($w, $h) = $self->SUPER::size_request; 539 my ($w, $h) = $self->SUPER::size_request;
539 540
540 $h += $tex[1]->{h}; 541 $h += $tex[1]->{h};
541 $h += $tex[4]->{h}; 542 $h += $tex[4]->{h};
542 $w += $tex[2]->{w}; 543 $w += $tex[2]->{w};
548sub size_allocate { 549sub size_allocate {
549 my ($self, $x, $y, $w, $h) = @_; 550 my ($self, $x, $y, $w, $h) = @_;
550 551
551 $self->_size_allocate ($x, $y, $w, $h) or return; 552 $self->_size_allocate ($x, $y, $w, $h) or return;
552 553
553 $h -= $tex[1]->{h}; 554 $h -= $tex[1]{h};
554 $h -= $tex[4]->{h}; 555 $h -= $tex[4]{h};
555 $w -= $tex[2]->{w}; 556 $w -= $tex[2]{w};
556 $w -= $tex[3]->{w}; 557 $w -= $tex[3]{w};
557 558
558 $h = $h < 0 ? 0 : $h; 559 $h = $h < 0 ? 0 : $h;
559 $w = $w < 0 ? 0 : $w; 560 $w = $w < 0 ? 0 : $w;
560 561
561 my $child = $self->child; 562 my $child = $self->child;
562 563
563 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h); 564 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
565}
566
567sub button_down {
568 my ($self, $ev, $x, $y) = @_;
569
570 if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w}
571 && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) {
572
573 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
574 my ($bw, $bh) = ($self->{w}, $self->{h});
575
576 $self->{motion} = sub {
577 my ($ev, $x, $y) = @_;
578
579 ($x, $y) = ($ev->motion_x, $ev->motion_y);
580
581 $self->{user_w} = $bw + $x - $ox;
582 $self->{user_h} = $bh + $y - $oy;
583 $self->update;
584 };
585
586 } elsif ($x >= 0 && $x < $self->{w}
587 && $y >= 0 && $y < $tex[1]{h}) {
588
589 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
590 my ($bx, $by) = ($self->{x}, $self->{y});
591
592 $self->{motion} = sub {
593 my ($ev, $x, $y) = @_;
594
595 ($x, $y) = ($ev->motion_x, $ev->motion_y);
596
597 $self->move ($bx + $x - $ox, $by + $y - $oy);
598 $self->update;
599 };
600 }
601}
602
603sub button_up {
604 my ($self, $ev, $x, $y) = @_;
605
606 delete $self->{motion};
607}
608
609sub mouse_motion {
610 my ($self, $ev, $x, $y) = @_;
611
612 $self->{motion}->($ev, $x, $y) if $self->{motion};
564} 613}
565 614
566sub _draw { 615sub _draw {
567 my ($self) = @_; 616 my ($self) = @_;
568 617
585 634
586 my $bottom = $tex[4]; 635 my $bottom = $tex[4];
587 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); 636 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
588 637
589 my $bg = $tex[0]; 638 my $bg = $tex[0];
639
590 glBindTexture GL_TEXTURE_2D, $bg->{name}; 640 glBindTexture GL_TEXTURE_2D, $bg->{name};
591 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 641 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
592 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 642 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
593 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 643 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
594 644
595 my $rep_x = $cw / $bg->{w}; 645 my $rep_x = $cw / $bg->{w};
596 my $rep_y = $ch / $bg->{h}; 646 my $rep_y = $ch / $bg->{h};
597 647
598 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); 648 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
599 649
650 glDisable GL_TEXTURE_2D;
600 glDisable GL_BLEND; 651 glDisable GL_BLEND;
601 glDisable GL_TEXTURE_2D;
602 652
603 $self->child->draw; 653 $self->child->draw;
604 654
605} 655}
606 656
611our @ISA = CFClient::UI::Base::; 661our @ISA = CFClient::UI::Base::;
612 662
613use List::Util qw(max sum); 663use List::Util qw(max sum);
614 664
615use SDL::OpenGL; 665use SDL::OpenGL;
666
667sub new {
668 my $class = shift;
669
670 $class->SUPER::new (
671 col_expand => [],
672 @_
673 )
674}
616 675
617sub add { 676sub add {
618 my ($self, $x, $y, $chld) = @_; 677 my ($self, $x, $y, $chld) = @_;
619 678
620 $self->{children}[$y][$x] = $chld; 679 $self->{children}[$y][$x] = $chld;
650 my ($self) = @_; 709 my ($self) = @_;
651 710
652 my ($ws, $hs) = $self->get_wh; 711 my ($ws, $hs) = $self->get_wh;
653 712
654 ( 713 (
655 (List::Util::sum @$ws), 714 (sum @$ws),
656 (List::Util::sum @$hs), 715 (sum @$hs),
657 ) 716 )
658} 717}
659 718
660sub size_allocate { 719sub size_allocate {
661 my ($self, $x, $y, $w, $h) = @_; 720 my ($self, $x, $y, $w, $h) = @_;
662 721
663 $self->_size_allocate ($x, $y, $w, $h) or return; 722 $self->_size_allocate ($x, $y, $w, $h) or return;
664 723
665 my ($ws, $hs) = $self->get_wh; 724 my ($ws, $hs) = $self->get_wh;
666 725
667 my $req_w = List::Util::sum @$ws; 726 my $req_w = sum @$ws;
668 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;
669 733
670 # linearly scale sizes 734 # linearly scale sizes
671 $_ *= $req_w / $w for @$ws; 735 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
672 $_ *= $req_h / $h for @$hs; 736 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
673 737
674 my $y; 738 my $y;
675 739
676 for my $r (0 .. $#{$self->{children}}) { 740 for my $r (0 .. $#{$self->{children}}) {
677 my $row = $self->{children}[$r] 741 my $row = $self->{children}[$r]
694 $y += $row_h; 758 $y += $row_h;
695 } 759 }
696 760
697} 761}
698 762
763sub find_widget {
764 my ($self, $x, $y) = @_;
765
766 $x -= $self->{x};
767 $y -= $self->{y};
768
769 my $res;
770
771 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
772 $res = $_->find_widget ($x, $y)
773 and return $res;
774 }
775
776 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
777}
778
699sub _draw { 779sub _draw {
700 my ($self) = @_; 780 my ($self) = @_;
701 781
702 for (grep $_, @{$self->{children}}) { 782 for (grep $_, @{$self->{children}}) {
703 $_->draw for grep $_, @$_; 783 $_->draw for grep $_, @$_;
704 } 784 }
705} 785}
706 786
707############################################################################# 787#############################################################################
708 788
789package CFClient::UI::HBox;
790
791# TODO: wrap into common Box base class
792
793our @ISA = CFClient::UI::Container::;
794
795sub size_request {
796 my ($self) = @_;
797
798 my @alloc = map [$_->size_request], @{$self->{children}};
799
800 (
801 (List::Util::sum map $_->[0], @alloc),
802 (List::Util::max map $_->[1], @alloc),
803 )
804}
805
806sub size_allocate {
807 my ($self, $x, $y, $w, $h) = @_;
808
809 $self->_size_allocate ($x, $y, $w, $h) or return;
810
811 return unless $self->{w};
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 = $h[$_];
841 $child->size_allocate ($y, 0, $h, $w);
842
843 $y += $h;
844 }
845}
846
847#############################################################################
848
709package CFClient::UI::VBox; 849package CFClient::UI::VBox;
710 850
851# TODO: wrap into common Box base class
852
711our @ISA = CFClient::UI::Container::; 853our @ISA = CFClient::UI::Container::;
712
713use SDL::OpenGL;
714 854
715sub size_request { 855sub size_request {
716 my ($self) = @_; 856 my ($self) = @_;
717 857
718 my @alloc = map [$_->size_request], @{$self->{children}}; 858 my @alloc = map [$_->size_request], @{$self->{children}};
736 876
737 my $req_h = List::Util::sum @h; 877 my $req_h = List::Util::sum @h;
738 878
739 if ($req_h > $h) { 879 if ($req_h > $h) {
740 # ah well, not enough space 880 # ah well, not enough space
741 $_ = $h[$_] * $h / $req_h for @h; 881 $_ *= $h / $req_h for @h;
742 } else { 882 } else {
743 my @exp = grep $_->{expand}, @$children; 883 my $exp = List::Util::sum map $_->{expand}, @$children;
744 @exp = @$children unless @exp; 884 $exp ||= 1;
745 885
746 my %exp = map +($_ => 1), @exp;
747
748 for (0 .. $#$children) { 886 for (0 .. $#$children) {
749 my $child = $children->[$_]; 887 my $child = $children->[$_];
750 888
751 my $alloc_h = $h[$_]; 889 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
752 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
753 $h[$_] = $alloc_h;
754 } 890 }
755 } 891 }
756 892
757 my $y = 0; 893 my $y = 0;
758 for (0 .. $#$children) { 894 for (0 .. $#$children) {
774 910
775sub new { 911sub new {
776 my ($class, %arg) = @_; 912 my ($class, %arg) = @_;
777 913
778 my $self = $class->SUPER::new ( 914 my $self = $class->SUPER::new (
779 fg => [1, 1, 1], 915 fg => [1, 1, 1],
780 height => $::FONTSIZE, 916 height => $::FONTSIZE,
781 text => "", 917 text => "",
782 align => -1, 918 align => -1,
919 padding => 2,
783 layout => new CFClient::Layout, 920 layout => new CFClient::Layout,
784 %arg 921 %arg
785 ); 922 );
786 923
787 $self->set_text ($self->{text}); 924 $self->set_text ($self->{text});
788 925
804 941
805 $self->{text} = $text; 942 $self->{text} = $text;
806 $self->{layout}->set_markup ($text); 943 $self->{layout}->set_markup ($text);
807 944
808 delete $self->{texture}; 945 delete $self->{texture};
946# $self->{w} = $self->{h} = -1;
809 $self->update; 947 $self->update;
810} 948}
811 949
812sub get_text { 950sub get_text {
813 my ($self, $text) = @_; 951 my ($self, $text) = @_;
818sub size_request { 956sub size_request {
819 my ($self) = @_; 957 my ($self) = @_;
820 958
821 $self->{layout}->set_width; 959 $self->{layout}->set_width;
822 $self->{layout}->set_height ($self->{height}); 960 $self->{layout}->set_height ($self->{height});
823 $self->{layout}->size 961 my ($w, $h) = $self->{layout}->size;
824# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack 962
825# ( 963 (
826# $self->{texture}{w}, 964 $w + $self->{padding} * 2,
827# $self->{texture}{h}, 965 $h + $self->{padding} * 2,
828# ) 966 )
829# } else {
830# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
831#
832# ($w, $h)
833# }
834} 967}
835 968
836sub size_allocate { 969sub size_allocate {
837 my ($self, $x, $y, $w, $h) = @_; 970 my ($self, $x, $y, $w, $h) = @_;
838 971
851sub _draw { 984sub _draw {
852 my ($self) = @_; 985 my ($self) = @_;
853 986
854 my $tex = $self->{texture} ||= do { 987 my $tex = $self->{texture} ||= do {
855 $self->{layout}->set_width ($self->{w}); 988 $self->{layout}->set_width ($self->{w});
989 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height});
856 new_from_layout CFClient::Texture $self->{layout} 990 new_from_layout CFClient::Texture $self->{layout}
857 }; 991 };
858 992
859 glEnable GL_BLEND; 993 glEnable GL_BLEND;
860 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 994 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
862 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 996 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
863 997
864 glColor @{$self->{fg}}; 998 glColor @{$self->{fg}};
865 999
866 my $x = 1000 my $x =
867 $self->{align} < 0 ? 0 1001 $self->{align} < 0 ? $self->{padding}
868 : $self->{align} > 0 ? $self->{w} - $tex->{w} 1002 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
869 : ($self->{w} - $tex->{w}) * 0.5; 1003 : ($self->{w} - $tex->{w}) * 0.5;
870 1004
871 $tex->draw_quad ($x, 0); 1005 $tex->draw_quad ($x, $self->{padding});
872 1006
873 glDisable GL_TEXTURE_2D; 1007 glDisable GL_TEXTURE_2D;
874 glDisable GL_BLEND; 1008 glDisable GL_BLEND;
875} 1009}
876 1010
886sub new { 1020sub new {
887 my $class = shift; 1021 my $class = shift;
888 1022
889 $class->SUPER::new ( 1023 $class->SUPER::new (
890 fg => [1, 1, 1], 1024 fg => [1, 1, 1],
891 bg => [0, 0, 0, 0.4], 1025 bg => [0, 0, 0, 0.2],
892 active_bg => [1, 1, 1], 1026 active_bg => [1, 1, 1, 0.5],
893 active_fg => [0, 0, 0], 1027 active_fg => [0, 0, 0],
894 @_ 1028 @_
895 ) 1029 )
896} 1030}
897 1031
903 $self->{text} = $text; 1037 $self->{text} = $text;
904 $self->{layout}->set_width ($self->{w}); 1038 $self->{layout}->set_width ($self->{w});
905 1039
906 $text =~ s/./*/g if $self->{hidden}; 1040 $text =~ s/./*/g if $self->{hidden};
907 1041
908
909 $self->{layout}->set_markup ($self->escape_text ($text)); 1042 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
910 1043
911 $text = substr $text, 0, $self->{cursor}; 1044 $text = substr $text, 0, $self->{cursor};
912 utf8::encode $text; 1045 utf8::encode $text;
913 1046
914 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 1047 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
954 substr $text, $self->{cursor}, 1, ""; 1087 substr $text, $self->{cursor}, 1, "";
955 } elsif ($sym == SDLK_LEFT) { 1088 } elsif ($sym == SDLK_LEFT) {
956 --$self->{cursor} if $self->{cursor}; 1089 --$self->{cursor} if $self->{cursor};
957 } elsif ($sym == SDLK_RIGHT) { 1090 } elsif ($sym == SDLK_RIGHT) {
958 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1091 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1092 } elsif ($sym == SDLK_HOME) {
1093 $self->{cursor} = 0;
1094 } elsif ($sym == SDLK_END) {
1095 $self->{cursor} = length $text;
959 } elsif ($uni) { 1096 } elsif ($uni) {
960 substr $text, $self->{cursor}++, 0, chr $uni; 1097 substr $text, $self->{cursor}++, 0, chr $uni;
961 } 1098 }
962 1099
963 $self->_set_text ($text); 1100 $self->_set_text ($text);
978 $self->SUPER::button_down ($ev, $x, $y); 1115 $self->SUPER::button_down ($ev, $x, $y);
979 1116
980 my $idx = $self->{layout}->xy_to_index ($x, $y); 1117 my $idx = $self->{layout}->xy_to_index ($x, $y);
981 1118
982 # byte-index to char-index 1119 # byte-index to char-index
983 my $text = $self->{layout}; 1120 my $text = $self->{text};
984 utf8::encode $text; 1121 utf8::encode $text;
985 $self->{cursor} = length substr $text, 0, $idx; 1122 $self->{cursor} = length substr $text, 0, $idx;
986 1123
987 $self->_set_text ($self->{text}); 1124 $self->_set_text ($self->{text});
988 $self->update; 1125 $self->update;
1003 $self->{fg} = $self->{active_fg}; 1140 $self->{fg} = $self->{active_fg};
1004 } else { 1141 } else {
1005 glColor @{$self->{bg}}; 1142 glColor @{$self->{bg}};
1006 } 1143 }
1007 1144
1145 glEnable GL_BLEND;
1146 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1008 glBegin GL_QUADS; 1147 glBegin GL_QUADS;
1009 glVertex 0 , 0; 1148 glVertex 0 , 0;
1010 glVertex 0 , $self->{h}; 1149 glVertex 0 , $self->{h};
1011 glVertex $self->{w}, $self->{h}; 1150 glVertex $self->{w}, $self->{h};
1012 glVertex $self->{w}, 0; 1151 glVertex $self->{w}, 0;
1013 glEnd; 1152 glEnd;
1153 glDisable GL_BLEND;
1014 1154
1015 $self->SUPER::_draw; 1155 $self->SUPER::_draw;
1016 1156
1017 #TODO: force update every cursor change :( 1157 #TODO: force update every cursor change :(
1018 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1158 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1024 } 1164 }
1025} 1165}
1026 1166
1027############################################################################# 1167#############################################################################
1028 1168
1029package CFClient::UI::Slider; 1169package CFClient::UI::Button;
1030 1170
1031use strict; 1171our @ISA = CFClient::UI::Label::;
1032 1172
1173use SDL;
1033use SDL::OpenGL; 1174use SDL::OpenGL;
1034
1035our @ISA = CFClient::UI::DrawBG::;
1036
1037sub size_request {
1038 my ($self) = @_;
1039
1040 my $w = 50;
1041 my $h = 10;
1042
1043 $self->{vertical} ? ($h, $w) : ($w, $h)
1044}
1045 1175
1046sub new { 1176sub new {
1047 my $class = shift; 1177 my $class = shift;
1048 1178
1179 $class->SUPER::new (
1180 padding => 4,
1181 fg => [1, 1, 1],
1182 bg => [1, 1, 1, 0.2],
1183 active_bg => [0, 0, 0, 0.5],
1184 active_fg => [1, 1, 0],
1185 border_fg => [1, 1, 0],
1186 @_
1187 )
1188}
1189
1190sub button_up {
1191 my ($self, $ev, $x, $y) = @_;
1192
1193 if ($x >= 0 && $x < $self->{w}
1194 && $y >= 0 && $y < $self->{h}) {
1195 $self->emit ("activate");
1196 }
1197}
1198
1199sub _draw {
1200 my ($self) = @_;
1201
1202 local $self->{fg} = $self->{fg};
1203
1204 glEnable GL_BLEND;
1205 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1206
1207 glTranslate 0.375, 0.375, 0; # make line and polyogon coordinates behave similarly
1208
1209 glBegin GL_LINE_LOOP;
1210 glColor @{$self->{border_fg}};
1211 glVertex 1 , 1;
1212 glVertex 1 , $self->{h} - 2;
1213 glVertex $self->{w} - 2, $self->{h} - 2;
1214 glVertex $self->{w} - 2, 1;
1215 glEnd;
1216
1217 if ($GRAB == $self) {
1218 glColor @{$self->{active_bg}};
1219 $self->{fg} = $self->{active_fg};
1220 } else {
1221 glColor @{$self->{bg}};
1222 }
1223
1224 glBegin GL_QUADS;
1225 glVertex 2 , 2;
1226 glVertex 2 , $self->{h} - 2;
1227 glVertex $self->{w} - 2, $self->{h} - 2;
1228 glVertex $self->{w} - 2, 2;
1229 glEnd;
1230 glDisable GL_BLEND;
1231
1232 $self->SUPER::_draw;
1233}
1234
1235#############################################################################
1236
1237package CFClient::UI::Slider;
1238
1239use strict;
1240
1241use SDL::OpenGL;
1242
1243our @ISA = CFClient::UI::DrawBG::;
1244
1245sub new {
1246 my $class = shift;
1247
1049 # range [value, low, high, page] 1248 # range [value, low, high, page]
1050 1249
1051 $class->SUPER::new ( 1250 my $self = $class->SUPER::new (
1052 fg => [1, 1, 1], 1251 fg => [1, 1, 1],
1053 active_fg => [0, 0, 0], 1252 active_fg => [0, 0, 0],
1054 range => [0, 0, 100, 10], 1253 range => [0, 0, 100, 10],
1254 req_w => 40,
1255 req_h => 10,
1055 vertical => 1, 1256 vertical => 0,
1056 @_ 1257 @_
1057 ) 1258 );
1259
1260 $self
1261}
1262
1263sub size_request {
1264 my ($self) = @_;
1265
1266 my $w = $self->{req_w};
1267 my $h = $self->{req_h};
1268
1269 $self->{vertical} ? ($h, $w) : ($w, $h)
1058} 1270}
1059 1271
1060sub button_down { 1272sub button_down {
1061 my ($self, $ev, $x, $y) = @_; 1273 my ($self, $ev, $x, $y) = @_;
1062 1274
1107 $value = int +($value - $lo) * $w / ($hi - $lo); 1319 $value = int +($value - $lo) * $w / ($hi - $lo);
1108 1320
1109 $w -= $page; 1321 $w -= $page;
1110 $page &= ~1; 1322 $page &= ~1;
1111 glTranslate $page * 0.5, 0, 0; 1323 glTranslate $page * 0.5, 0, 0;
1324 $page ||= 2;
1112 1325
1113 glColor @$fg; 1326 glColor @$fg;
1114 glBegin GL_LINES; 1327 glBegin GL_LINES;
1115 glVertex 0, 0; glVertex 0, $h; 1328 glVertex 0, 0; glVertex 0, $h;
1116 glVertex $w - 1, 0; glVertex $w - 1, $h; 1329 glVertex $w - 1, 0; glVertex $w - 1, $h;
1167sub key_up { 1380sub key_up {
1168} 1381}
1169 1382
1170sub size_request { 1383sub size_request {
1171 ( 1384 (
1172 1 + int $::WIDTH / 32, 1385 1 + 32 * int $::WIDTH / 32,
1173 1 + int $::HEIGHT / 32, 1386 1 + 32 * int $::HEIGHT / 32,
1174 ) 1387 )
1175} 1388}
1176 1389
1177sub update { 1390sub update {
1178 my ($self) = @_; 1391 my ($self) = @_;
1179 1392
1180 $self->{need_update} = 1; 1393 $self->{need_update} = 1;
1181 $self->SUPER::update; 1394 $self->SUPER::update;
1182} 1395}
1183 1396
1184sub _draw { 1397sub draw {
1185 my ($self) = @_; 1398 my ($self) = @_;
1186 1399
1187 if (delete $self->{need_update}) { 1400 if (delete $self->{need_update}) {
1188 glNewList $self->{list}, GL_COMPILE; 1401 glNewList $self->{list}, GL_COMPILE;
1189 1402

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines