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.84 by root, Wed Apr 12 12:20:00 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;
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}
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;
593 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
594 642
595 my $rep_x = $cw / $bg->{w}; 643 my $rep_x = $cw / $bg->{w};
596 my $rep_y = $ch / $bg->{h}; 644 my $rep_y = $ch / $bg->{h};
597 645
598 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); 646 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
599 647
648 glDisable GL_TEXTURE_2D;
600 glDisable GL_BLEND; 649 glDisable GL_BLEND;
601 glDisable GL_TEXTURE_2D;
602 650
603 $self->child->draw; 651 $self->child->draw;
604 652
605} 653}
606 654
611our @ISA = CFClient::UI::Base::; 659our @ISA = CFClient::UI::Base::;
612 660
613use List::Util qw(max sum); 661use List::Util qw(max sum);
614 662
615use SDL::OpenGL; 663use SDL::OpenGL;
664
665sub new {
666 my $class = shift;
667
668 $class->SUPER::new (
669 col_expand => [],
670 @_
671 )
672}
616 673
617sub add { 674sub add {
618 my ($self, $x, $y, $chld) = @_; 675 my ($self, $x, $y, $chld) = @_;
619 676
620 $self->{children}[$y][$x] = $chld; 677 $self->{children}[$y][$x] = $chld;
650 my ($self) = @_; 707 my ($self) = @_;
651 708
652 my ($ws, $hs) = $self->get_wh; 709 my ($ws, $hs) = $self->get_wh;
653 710
654 ( 711 (
655 (List::Util::sum @$ws), 712 (sum @$ws),
656 (List::Util::sum @$hs), 713 (sum @$hs),
657 ) 714 )
658} 715}
659 716
660sub size_allocate { 717sub size_allocate {
661 my ($self, $x, $y, $w, $h) = @_; 718 my ($self, $x, $y, $w, $h) = @_;
662 719
663 $self->_size_allocate ($x, $y, $w, $h) or return; 720 $self->_size_allocate ($x, $y, $w, $h) or return;
664 721
665 my ($ws, $hs) = $self->get_wh; 722 my ($ws, $hs) = $self->get_wh;
666 723
667 my $req_w = List::Util::sum @$ws; 724 my $req_w = sum @$ws;
668 my $req_h = List::Util::sum @$hs; 725 my $req_h = sum @$hs;
726
727 # TODO: nicer code && do row_expand
728 my @col_expand = @{$self->{col_expand}};
729 @col_expand = (1) x @$ws unless @col_expand;
730 my $col_expand = (sum @col_expand) || 1;
669 731
670 # linearly scale sizes 732 # linearly scale sizes
671 $_ *= $req_w / $w for @$ws; 733 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
672 $_ *= $req_h / $h for @$hs; 734 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
673 735
674 my $y; 736 my $y;
675 737
676 for my $r (0 .. $#{$self->{children}}) { 738 for my $r (0 .. $#{$self->{children}}) {
677 my $row = $self->{children}[$r] 739 my $row = $self->{children}[$r]
679 741
680 my $x = 0; 742 my $x = 0;
681 my $row_h = $hs->[$r]; 743 my $row_h = $hs->[$r];
682 744
683 for my $c (0 .. $#$row) { 745 for my $c (0 .. $#$row) {
684 my $widget = $row->[$c]
685 or next;
686
687 my $col_w = $ws->[$c]; 746 my $col_w = $ws->[$c];
688 747
748 if (my $widget = $row->[$c]) {
689 $widget->size_allocate ($x, $y, $col_w, $row_h); 749 $widget->size_allocate ($x, $y, $col_w, $row_h);
750 }
690 751
691 $x += $col_w; 752 $x += $col_w;
692 } 753 }
693 754
694 $y += $row_h; 755 $y += $row_h;
695 } 756 }
696 757
697} 758}
698 759
760sub find_widget {
761 my ($self, $x, $y) = @_;
762
763 $x -= $self->{x};
764 $y -= $self->{y};
765
766 my $res;
767
768 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
769 $res = $_->find_widget ($x, $y)
770 and return $res;
771 }
772
773 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
774}
775
699sub _draw { 776sub _draw {
700 my ($self) = @_; 777 my ($self) = @_;
701 778
702 for (grep $_, @{$self->{children}}) { 779 for (grep $_, @{$self->{children}}) {
703 $_->draw for grep $_, @$_; 780 $_->draw for grep $_, @$_;
704 } 781 }
705} 782}
706 783
707############################################################################# 784#############################################################################
708 785
786package CFClient::UI::HBox;
787
788# TODO: wrap into common Box base class
789
790our @ISA = CFClient::UI::Container::;
791
792sub size_request {
793 my ($self) = @_;
794
795 my @alloc = map [$_->size_request], @{$self->{children}};
796
797 (
798 (List::Util::sum map $_->[0], @alloc),
799 (List::Util::max map $_->[1], @alloc),
800 )
801}
802
803sub size_allocate {
804 my ($self, $x, $y, $w, $h) = @_;
805
806 $self->_size_allocate ($x, $y, $w, $h) or return;
807
808 return unless $self->{w};
809
810 ($h, $w) = ($w, $h);
811
812 my $children = $self->{children};
813
814 my @h = map +($_->size_request)[0], @$children;
815
816 my $req_h = List::Util::sum @h;
817
818 if ($req_h > $h) {
819 # ah well, not enough space
820 $_ *= $h / $req_h for @h;
821 } else {
822 my $exp = List::Util::sum map $_->{expand}, @$children;
823 $exp ||= 1;
824
825 for (0 .. $#$children) {
826 my $child = $children->[$_];
827
828 my $alloc_h = $h[$_];
829 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
830 $h[$_] = $alloc_h;
831 }
832 }
833
834 my $y = 0;
835 for (0 .. $#$children) {
836 my $child = $children->[$_];
837 my $h = $h[$_];
838 $child->size_allocate ($y, 0, $h, $w);
839
840 $y += $h;
841 }
842}
843
844#############################################################################
845
709package CFClient::UI::VBox; 846package CFClient::UI::VBox;
710 847
848# TODO: wrap into common Box base class
849
711our @ISA = CFClient::UI::Container::; 850our @ISA = CFClient::UI::Container::;
712
713use SDL::OpenGL;
714 851
715sub size_request { 852sub size_request {
716 my ($self) = @_; 853 my ($self) = @_;
717 854
718 my @alloc = map [$_->size_request], @{$self->{children}}; 855 my @alloc = map [$_->size_request], @{$self->{children}};
736 873
737 my $req_h = List::Util::sum @h; 874 my $req_h = List::Util::sum @h;
738 875
739 if ($req_h > $h) { 876 if ($req_h > $h) {
740 # ah well, not enough space 877 # ah well, not enough space
741 $_ = $h[$_] * $h / $req_h for @h; 878 $_ *= $h / $req_h for @h;
742 } else { 879 } else {
743 my @exp = grep $_->{expand}, @$children; 880 my $exp = List::Util::sum map $_->{expand}, @$children;
744 @exp = @$children unless @exp; 881 $exp ||= 1;
745 882
746 my %exp = map +($_ => 1), @exp;
747
748 for (0 .. $#$children) { 883 for (0 .. $#$children) {
749 my $child = $children->[$_]; 884 my $child = $children->[$_];
750 885
751 my $alloc_h = $h[$_]; 886 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
752 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
753 $h[$_] = $alloc_h;
754 } 887 }
755 } 888 }
756 889
757 my $y = 0; 890 my $y = 0;
758 for (0 .. $#$children) { 891 for (0 .. $#$children) {
774 907
775sub new { 908sub new {
776 my ($class, %arg) = @_; 909 my ($class, %arg) = @_;
777 910
778 my $self = $class->SUPER::new ( 911 my $self = $class->SUPER::new (
779 fg => [1, 1, 1], 912 fg => [1, 1, 1],
780 height => $::FONTSIZE, 913 height => $::FONTSIZE,
781 text => "", 914 text => "",
782 align => -1, 915 align => -1,
916 padding => 2,
783 layout => new CFClient::Layout, 917 layout => new CFClient::Layout,
784 %arg 918 %arg
785 ); 919 );
786 920
787 $self->set_text ($self->{text}); 921 $self->set_text ($self->{text});
788 922
804 938
805 $self->{text} = $text; 939 $self->{text} = $text;
806 $self->{layout}->set_markup ($text); 940 $self->{layout}->set_markup ($text);
807 941
808 delete $self->{texture}; 942 delete $self->{texture};
943# $self->{w} = $self->{h} = -1;
809 $self->update; 944 $self->update;
810} 945}
811 946
812sub get_text { 947sub get_text {
813 my ($self, $text) = @_; 948 my ($self, $text) = @_;
818sub size_request { 953sub size_request {
819 my ($self) = @_; 954 my ($self) = @_;
820 955
821 $self->{layout}->set_width; 956 $self->{layout}->set_width;
822 $self->{layout}->set_height ($self->{height}); 957 $self->{layout}->set_height ($self->{height});
823 $self->{layout}->size 958 my ($w, $h) = $self->{layout}->size;
824# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack 959
825# ( 960 (
826# $self->{texture}{w}, 961 $w + $self->{padding} * 2,
827# $self->{texture}{h}, 962 $h + $self->{padding} * 2,
828# ) 963 )
829# } else {
830# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
831#
832# ($w, $h)
833# }
834} 964}
835 965
836sub size_allocate { 966sub size_allocate {
837 my ($self, $x, $y, $w, $h) = @_; 967 my ($self, $x, $y, $w, $h) = @_;
838 968
851sub _draw { 981sub _draw {
852 my ($self) = @_; 982 my ($self) = @_;
853 983
854 my $tex = $self->{texture} ||= do { 984 my $tex = $self->{texture} ||= do {
855 $self->{layout}->set_width ($self->{w}); 985 $self->{layout}->set_width ($self->{w});
986 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height});
856 new_from_layout CFClient::Texture $self->{layout} 987 new_from_layout CFClient::Texture $self->{layout}
857 }; 988 };
858 989
859 glEnable GL_BLEND; 990 glEnable GL_BLEND;
860 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 991 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
862 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 993 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
863 994
864 glColor @{$self->{fg}}; 995 glColor @{$self->{fg}};
865 996
866 my $x = 997 my $x =
867 $self->{align} < 0 ? 0 998 $self->{align} < 0 ? $self->{padding}
868 : $self->{align} > 0 ? $self->{w} - $tex->{w} 999 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
869 : ($self->{w} - $tex->{w}) * 0.5; 1000 : ($self->{w} - $tex->{w}) * 0.5;
870 1001
1002 glTranslate $x, ($self->{h} - $tex->{h}) * 0.5, 0;
871 $tex->draw_quad ($x, 0); 1003 $tex->draw_quad (0, 0);
872 1004
873 glDisable GL_TEXTURE_2D; 1005 glDisable GL_TEXTURE_2D;
874 glDisable GL_BLEND; 1006 glDisable GL_BLEND;
875} 1007}
876 1008
886sub new { 1018sub new {
887 my $class = shift; 1019 my $class = shift;
888 1020
889 $class->SUPER::new ( 1021 $class->SUPER::new (
890 fg => [1, 1, 1], 1022 fg => [1, 1, 1],
891 bg => [0, 0, 0, 0.4], 1023 bg => [0, 0, 0, 0.2],
892 active_bg => [1, 1, 1], 1024 active_bg => [1, 1, 1, 0.5],
893 active_fg => [0, 0, 0], 1025 active_fg => [0, 0, 0],
894 @_ 1026 @_
895 ) 1027 )
896} 1028}
897 1029
900 1032
901 $self->{last_activity} = $::NOW; 1033 $self->{last_activity} = $::NOW;
902 1034
903 $self->{text} = $text; 1035 $self->{text} = $text;
904 $self->{layout}->set_width ($self->{w}); 1036 $self->{layout}->set_width ($self->{w});
1037 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height});
905 1038
906 $text =~ s/./*/g if $self->{hidden}; 1039 $text =~ s/./*/g if $self->{hidden};
907 1040
908
909 $self->{layout}->set_markup ($self->escape_text ($text)); 1041 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
910 1042
911 $text = substr $text, 0, $self->{cursor}; 1043 $text = substr $text, 0, $self->{cursor};
912 utf8::encode $text; 1044 utf8::encode $text;
913 1045
914 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 1046 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
954 substr $text, $self->{cursor}, 1, ""; 1086 substr $text, $self->{cursor}, 1, "";
955 } elsif ($sym == SDLK_LEFT) { 1087 } elsif ($sym == SDLK_LEFT) {
956 --$self->{cursor} if $self->{cursor}; 1088 --$self->{cursor} if $self->{cursor};
957 } elsif ($sym == SDLK_RIGHT) { 1089 } elsif ($sym == SDLK_RIGHT) {
958 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1090 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1091 } elsif ($sym == SDLK_HOME) {
1092 $self->{cursor} = 0;
1093 } elsif ($sym == SDLK_END) {
1094 $self->{cursor} = length $text;
959 } elsif ($uni) { 1095 } elsif ($uni) {
960 substr $text, $self->{cursor}++, 0, chr $uni; 1096 substr $text, $self->{cursor}++, 0, chr $uni;
961 } 1097 }
962 1098
963 $self->_set_text ($text); 1099 $self->_set_text ($text);
978 $self->SUPER::button_down ($ev, $x, $y); 1114 $self->SUPER::button_down ($ev, $x, $y);
979 1115
980 my $idx = $self->{layout}->xy_to_index ($x, $y); 1116 my $idx = $self->{layout}->xy_to_index ($x, $y);
981 1117
982 # byte-index to char-index 1118 # byte-index to char-index
983 my $text = $self->{layout}; 1119 my $text = $self->{text};
984 utf8::encode $text; 1120 utf8::encode $text;
985 $self->{cursor} = length substr $text, 0, $idx; 1121 $self->{cursor} = length substr $text, 0, $idx;
986 1122
987 $self->_set_text ($self->{text}); 1123 $self->_set_text ($self->{text});
988 $self->update; 1124 $self->update;
1003 $self->{fg} = $self->{active_fg}; 1139 $self->{fg} = $self->{active_fg};
1004 } else { 1140 } else {
1005 glColor @{$self->{bg}}; 1141 glColor @{$self->{bg}};
1006 } 1142 }
1007 1143
1144 glEnable GL_BLEND;
1145 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1008 glBegin GL_QUADS; 1146 glBegin GL_QUADS;
1009 glVertex 0 , 0; 1147 glVertex 0 , 0;
1010 glVertex 0 , $self->{h}; 1148 glVertex 0 , $self->{h};
1011 glVertex $self->{w}, $self->{h}; 1149 glVertex $self->{w}, $self->{h};
1012 glVertex $self->{w}, 0; 1150 glVertex $self->{w}, 0;
1013 glEnd; 1151 glEnd;
1152 glDisable GL_BLEND;
1014 1153
1015 $self->SUPER::_draw; 1154 $self->SUPER::_draw;
1016 1155
1017 #TODO: force update every cursor change :( 1156 #TODO: force update every cursor change :(
1018 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1157 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1024 } 1163 }
1025} 1164}
1026 1165
1027############################################################################# 1166#############################################################################
1028 1167
1029package CFClient::UI::Slider; 1168package CFClient::UI::Button;
1030 1169
1031use strict; 1170our @ISA = CFClient::UI::Label::;
1032 1171
1172use SDL;
1033use SDL::OpenGL; 1173use 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 1174
1046sub new { 1175sub new {
1047 my $class = shift; 1176 my $class = shift;
1048 1177
1178 $class->SUPER::new (
1179 padding => 4,
1180 fg => [1, 1, 1],
1181 bg => [1, 1, 1, 0.2],
1182 active_bg => [0, 0, 0, 0.5],
1183 active_fg => [1, 1, 0],
1184 border_fg => [1, 1, 0],
1185 @_
1186 )
1187}
1188
1189sub button_up {
1190 my ($self, $ev, $x, $y) = @_;
1191
1192 if ($x >= 0 && $x < $self->{w}
1193 && $y >= 0 && $y < $self->{h}) {
1194 $self->emit ("activate");
1195 }
1196}
1197
1198sub _draw {
1199 my ($self) = @_;
1200
1201 local $self->{fg} = $self->{fg};
1202
1203 glEnable GL_BLEND;
1204 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1205
1206 glTranslate 0.375, 0.375, 0; # make line and polyogon coordinates behave similarly
1207
1208 glBegin GL_LINE_LOOP;
1209 glColor @{$self->{border_fg}};
1210 glVertex 1 , 1;
1211 glVertex 1 , $self->{h} - 2;
1212 glVertex $self->{w} - 2, $self->{h} - 2;
1213 glVertex $self->{w} - 2, 1;
1214 glEnd;
1215
1216 if ($GRAB == $self) {
1217 glColor @{$self->{active_bg}};
1218 $self->{fg} = $self->{active_fg};
1219 } else {
1220 glColor @{$self->{bg}};
1221 }
1222
1223 glBegin GL_QUADS;
1224 glVertex 2 , 2;
1225 glVertex 2 , $self->{h} - 2;
1226 glVertex $self->{w} - 2, $self->{h} - 2;
1227 glVertex $self->{w} - 2, 2;
1228 glEnd;
1229 glDisable GL_BLEND;
1230
1231 $self->SUPER::_draw;
1232}
1233
1234#############################################################################
1235
1236package CFClient::UI::Slider;
1237
1238use strict;
1239
1240use SDL::OpenGL;
1241
1242our @ISA = CFClient::UI::DrawBG::;
1243
1244sub new {
1245 my $class = shift;
1246
1049 # range [value, low, high, page] 1247 # range [value, low, high, page]
1050 1248
1051 $class->SUPER::new ( 1249 my $self = $class->SUPER::new (
1052 fg => [1, 1, 1], 1250 fg => [1, 1, 1],
1053 active_fg => [0, 0, 0], 1251 active_fg => [0, 0, 0],
1054 range => [0, 0, 100, 10], 1252 range => [0, 0, 100, 10],
1253 req_w => 40,
1254 req_h => 10,
1055 vertical => 1, 1255 vertical => 0,
1056 @_ 1256 @_
1057 ) 1257 );
1258
1259 $self
1260}
1261
1262sub size_request {
1263 my ($self) = @_;
1264
1265 my $w = $self->{req_w};
1266 my $h = $self->{req_h};
1267
1268 $self->{vertical} ? ($h, $w) : ($w, $h)
1058} 1269}
1059 1270
1060sub button_down { 1271sub button_down {
1061 my ($self, $ev, $x, $y) = @_; 1272 my ($self, $ev, $x, $y) = @_;
1062 1273
1107 $value = int +($value - $lo) * $w / ($hi - $lo); 1318 $value = int +($value - $lo) * $w / ($hi - $lo);
1108 1319
1109 $w -= $page; 1320 $w -= $page;
1110 $page &= ~1; 1321 $page &= ~1;
1111 glTranslate $page * 0.5, 0, 0; 1322 glTranslate $page * 0.5, 0, 0;
1323 $page ||= 2;
1112 1324
1113 glColor @$fg; 1325 glColor @$fg;
1114 glBegin GL_LINES; 1326 glBegin GL_LINES;
1115 glVertex 0, 0; glVertex 0, $h; 1327 glVertex 0, 0; glVertex 0, $h;
1116 glVertex $w - 1, 0; glVertex $w - 1, $h; 1328 glVertex $w - 1, 0; glVertex $w - 1, $h;
1167sub key_up { 1379sub key_up {
1168} 1380}
1169 1381
1170sub size_request { 1382sub size_request {
1171 ( 1383 (
1172 1 + int $::WIDTH / 32, 1384 1 + 32 * int $::WIDTH / 32,
1173 1 + int $::HEIGHT / 32, 1385 1 + 32 * int $::HEIGHT / 32,
1174 ) 1386 )
1175} 1387}
1176 1388
1177sub update { 1389sub update {
1178 my ($self) = @_; 1390 my ($self) = @_;
1179 1391
1180 $self->{need_update} = 1; 1392 $self->{need_update} = 1;
1181 $self->SUPER::update; 1393 $self->SUPER::update;
1182} 1394}
1183 1395
1184sub _draw { 1396sub draw {
1185 my ($self) = @_; 1397 my ($self) = @_;
1186 1398
1187 if (delete $self->{need_update}) { 1399 if (delete $self->{need_update}) {
1188 glNewList $self->{list}, GL_COMPILE; 1400 glNewList $self->{list}, GL_COMPILE;
1189 1401

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines