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.76 by root, Tue Apr 11 21:24:09 2006 UTC

181 glPopMatrix; 181 glPopMatrix;
182 182
183 if ($self == $HOVER) { 183 if ($self == $HOVER) {
184 my ($x, $y) = @$self{qw(x y)}; 184 my ($x, $y) = @$self{qw(x y)};
185 185
186 glColor 1, 1, 1, 0.1; 186 glColor 0, 0, 1, 0.2;
187 glEnable GL_BLEND; 187 glEnable GL_BLEND;
188 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 188 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
189 glBegin GL_QUADS; 189 glBegin GL_QUADS;
190 glVertex $x , $y; 190 glVertex $x , $y;
191 glVertex $x + $self->{w}, $y; 191 glVertex $x + $self->{w}, $y;
275 my $class = shift; 275 my $class = shift;
276 276
277 # range [value, low, high, page] 277 # range [value, low, high, page]
278 278
279 $class->SUPER::new ( 279 $class->SUPER::new (
280 bg => [0, 0, 0, 0.4], 280 bg => [0, 0, 0, 0.2],
281 active_bg => [1, 1, 1], 281 active_bg => [1, 1, 1],
282 @_ 282 @_
283 ) 283 )
284} 284}
285 285
286sub _draw { 286sub _draw {
287 my ($self) = @_; 287 my ($self) = @_;
288 288
289 my ($w, $h) = @$self{qw(w h)}; 289 my ($w, $h) = @$self{qw(w h)};
290 290
291 glEnable GL_BLEND;
292 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
291 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 293 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
294
292 glBegin GL_QUADS; 295 glBegin GL_QUADS;
293 glVertex 0 , 0; 296 glVertex 0 , 0;
294 glVertex 0 , $h; 297 glVertex 0 , $h;
295 glVertex $w, $h; 298 glVertex $w, $h;
296 glVertex $w, 0; 299 glVertex $w, 0;
297 glEnd; 300 glEnd;
301
302 glDisable GL_BLEND;
298} 303}
299 304
300############################################################################# 305#############################################################################
301 306
302package CFClient::UI::Empty; 307package CFClient::UI::Empty;
585 590
586 my $bottom = $tex[4]; 591 my $bottom = $tex[4];
587 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); 592 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
588 593
589 my $bg = $tex[0]; 594 my $bg = $tex[0];
595
590 glBindTexture GL_TEXTURE_2D, $bg->{name}; 596 glBindTexture GL_TEXTURE_2D, $bg->{name};
591 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 597 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
592 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 598 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
593 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 599 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
594 600
595 my $rep_x = $cw / $bg->{w}; 601 my $rep_x = $cw / $bg->{w};
596 my $rep_y = $ch / $bg->{h}; 602 my $rep_y = $ch / $bg->{h};
597 603
598 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); 604 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
599 605
606 glDisable GL_TEXTURE_2D;
600 glDisable GL_BLEND; 607 glDisable GL_BLEND;
601 glDisable GL_TEXTURE_2D;
602 608
603 $self->child->draw; 609 $self->child->draw;
604 610
605} 611}
606 612
694 $y += $row_h; 700 $y += $row_h;
695 } 701 }
696 702
697} 703}
698 704
705sub find_widget {
706 my ($self, $x, $y) = @_;
707
708 $x -= $self->{x};
709 $y -= $self->{y};
710
711 my $res;
712
713 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
714 $res = $_->find_widget ($x, $y)
715 and return $res;
716 }
717
718 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
719}
720
699sub _draw { 721sub _draw {
700 my ($self) = @_; 722 my ($self) = @_;
701 723
702 for (grep $_, @{$self->{children}}) { 724 for (grep $_, @{$self->{children}}) {
703 $_->draw for grep $_, @$_; 725 $_->draw for grep $_, @$_;
704 } 726 }
705} 727}
706 728
707############################################################################# 729#############################################################################
708 730
709package CFClient::UI::VBox; 731package CFClient::UI::HBox;
732
733# TODO: wrap into common Box base class
710 734
711our @ISA = CFClient::UI::Container::; 735our @ISA = CFClient::UI::Container::;
712 736
713use SDL::OpenGL;
714
715sub size_request { 737sub size_request {
716 my ($self) = @_; 738 my ($self) = @_;
717 739
718 my @alloc = map [$_->size_request], @{$self->{children}}; 740 my @alloc = map [$_->size_request], @{$self->{children}};
719 741
720 ( 742 (
721 (List::Util::max map $_->[0], @alloc),
722 (List::Util::sum map $_->[1], @alloc), 743 (List::Util::sum map $_->[0], @alloc),
744 (List::Util::max map $_->[1], @alloc),
723 ) 745 )
724} 746}
725 747
726sub size_allocate { 748sub size_allocate {
727 my ($self, $x, $y, $w, $h) = @_; 749 my ($self, $x, $y, $w, $h) = @_;
728 750
729 $self->_size_allocate ($x, $y, $w, $h) or return; 751 $self->_size_allocate ($x, $y, $w, $h) or return;
730 752
731 return unless $self->{h}; 753 return unless $self->{w};
754
755 ($h, $w) = ($w, $h);
732 756
733 my $children = $self->{children}; 757 my $children = $self->{children};
734 758
735 my @h = map +($_->size_request)[1], @$children; 759 my @h = map +($_->size_request)[0], @$children;
736 760
737 my $req_h = List::Util::sum @h; 761 my $req_h = List::Util::sum @h;
738 762
739 if ($req_h > $h) { 763 if ($req_h > $h) {
740 # ah well, not enough space 764 # ah well, not enough space
756 780
757 my $y = 0; 781 my $y = 0;
758 for (0 .. $#$children) { 782 for (0 .. $#$children) {
759 my $child = $children->[$_]; 783 my $child = $children->[$_];
760 my $h = $h[$_]; 784 my $h = $h[$_];
785 $child->size_allocate ($y, 0, $h, $w);
786
787 $y += $h;
788 }
789}
790
791#############################################################################
792
793package CFClient::UI::VBox;
794
795# TODO: wrap into common Box base class
796
797our @ISA = CFClient::UI::Container::;
798
799sub size_request {
800 my ($self) = @_;
801
802 my @alloc = map [$_->size_request], @{$self->{children}};
803
804 (
805 (List::Util::max map $_->[0], @alloc),
806 (List::Util::sum map $_->[1], @alloc),
807 )
808}
809
810sub size_allocate {
811 my ($self, $x, $y, $w, $h) = @_;
812
813 $self->_size_allocate ($x, $y, $w, $h) or return;
814
815 return unless $self->{h};
816
817 my $children = $self->{children};
818
819 my @h = map +($_->size_request)[1], @$children;
820
821 my $req_h = List::Util::sum @h;
822
823 if ($req_h > $h) {
824 # ah well, not enough space
825 $_ = $h[$_] * $h / $req_h for @h;
826 } else {
827 my @exp = grep $_->{expand}, @$children;
828 @exp = @$children unless @exp;
829
830 my %exp = map +($_ => 1), @exp;
831
832 for (0 .. $#$children) {
833 my $child = $children->[$_];
834
835 my $alloc_h = $h[$_];
836 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
837 $h[$_] = $alloc_h;
838 }
839 }
840
841 my $y = 0;
842 for (0 .. $#$children) {
843 my $child = $children->[$_];
844 my $h = $h[$_];
761 $child->size_allocate (0, $y, $w, $h); 845 $child->size_allocate (0, $y, $w, $h);
762 846
763 $y += $h; 847 $y += $h;
764 } 848 }
765} 849}
774 858
775sub new { 859sub new {
776 my ($class, %arg) = @_; 860 my ($class, %arg) = @_;
777 861
778 my $self = $class->SUPER::new ( 862 my $self = $class->SUPER::new (
779 fg => [1, 1, 1], 863 fg => [1, 1, 1],
780 height => $::FONTSIZE, 864 height => $::FONTSIZE,
781 text => "", 865 text => "",
782 align => -1, 866 align => -1,
867 padding => 2,
783 layout => new CFClient::Layout, 868 layout => new CFClient::Layout,
784 %arg 869 %arg
785 ); 870 );
786 871
787 $self->set_text ($self->{text}); 872 $self->set_text ($self->{text});
788 873
818sub size_request { 903sub size_request {
819 my ($self) = @_; 904 my ($self) = @_;
820 905
821 $self->{layout}->set_width; 906 $self->{layout}->set_width;
822 $self->{layout}->set_height ($self->{height}); 907 $self->{layout}->set_height ($self->{height});
823 $self->{layout}->size 908 my ($w, $h) = $self->{layout}->size;
824# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack 909
825# ( 910 (
826# $self->{texture}{w}, 911 $w + $self->{padding} * 2,
827# $self->{texture}{h}, 912 $h + $self->{padding} * 2,
828# ) 913 )
829# } else {
830# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
831#
832# ($w, $h)
833# }
834} 914}
835 915
836sub size_allocate { 916sub size_allocate {
837 my ($self, $x, $y, $w, $h) = @_; 917 my ($self, $x, $y, $w, $h) = @_;
838 918
862 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 942 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
863 943
864 glColor @{$self->{fg}}; 944 glColor @{$self->{fg}};
865 945
866 my $x = 946 my $x =
867 $self->{align} < 0 ? 0 947 $self->{align} < 0 ? $self->{padding}
868 : $self->{align} > 0 ? $self->{w} - $tex->{w} 948 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
869 : ($self->{w} - $tex->{w}) * 0.5; 949 : ($self->{w} - $tex->{w}) * 0.5;
870 950
871 $tex->draw_quad ($x, 0); 951 $tex->draw_quad ($x, 0);
872 952
873 glDisable GL_TEXTURE_2D; 953 glDisable GL_TEXTURE_2D;
886sub new { 966sub new {
887 my $class = shift; 967 my $class = shift;
888 968
889 $class->SUPER::new ( 969 $class->SUPER::new (
890 fg => [1, 1, 1], 970 fg => [1, 1, 1],
891 bg => [0, 0, 0, 0.4], 971 bg => [0, 0, 0, 0.2],
892 active_bg => [1, 1, 1], 972 active_bg => [1, 1, 1],
893 active_fg => [0, 0, 0], 973 active_fg => [0, 0, 0],
894 @_ 974 @_
895 ) 975 )
896} 976}
903 $self->{text} = $text; 983 $self->{text} = $text;
904 $self->{layout}->set_width ($self->{w}); 984 $self->{layout}->set_width ($self->{w});
905 985
906 $text =~ s/./*/g if $self->{hidden}; 986 $text =~ s/./*/g if $self->{hidden};
907 987
908
909 $self->{layout}->set_markup ($self->escape_text ($text)); 988 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
910 989
911 $text = substr $text, 0, $self->{cursor}; 990 $text = substr $text, 0, $self->{cursor};
912 utf8::encode $text; 991 utf8::encode $text;
913 992
914 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 993 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
954 substr $text, $self->{cursor}, 1, ""; 1033 substr $text, $self->{cursor}, 1, "";
955 } elsif ($sym == SDLK_LEFT) { 1034 } elsif ($sym == SDLK_LEFT) {
956 --$self->{cursor} if $self->{cursor}; 1035 --$self->{cursor} if $self->{cursor};
957 } elsif ($sym == SDLK_RIGHT) { 1036 } elsif ($sym == SDLK_RIGHT) {
958 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1037 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1038 } elsif ($sym == SDLK_HOME) {
1039 $self->{cursor} = 0;
1040 } elsif ($sym == SDLK_END) {
1041 $self->{cursor} = length $text;
959 } elsif ($uni) { 1042 } elsif ($uni) {
960 substr $text, $self->{cursor}++, 0, chr $uni; 1043 substr $text, $self->{cursor}++, 0, chr $uni;
961 } 1044 }
962 1045
963 $self->_set_text ($text); 1046 $self->_set_text ($text);
978 $self->SUPER::button_down ($ev, $x, $y); 1061 $self->SUPER::button_down ($ev, $x, $y);
979 1062
980 my $idx = $self->{layout}->xy_to_index ($x, $y); 1063 my $idx = $self->{layout}->xy_to_index ($x, $y);
981 1064
982 # byte-index to char-index 1065 # byte-index to char-index
983 my $text = $self->{layout}; 1066 my $text = $self->{text};
984 utf8::encode $text; 1067 utf8::encode $text;
985 $self->{cursor} = length substr $text, 0, $idx; 1068 $self->{cursor} = length substr $text, 0, $idx;
986 1069
987 $self->_set_text ($self->{text}); 1070 $self->_set_text ($self->{text});
988 $self->update; 1071 $self->update;
1003 $self->{fg} = $self->{active_fg}; 1086 $self->{fg} = $self->{active_fg};
1004 } else { 1087 } else {
1005 glColor @{$self->{bg}}; 1088 glColor @{$self->{bg}};
1006 } 1089 }
1007 1090
1091 glEnable GL_BLEND;
1092 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1008 glBegin GL_QUADS; 1093 glBegin GL_QUADS;
1009 glVertex 0 , 0; 1094 glVertex 0 , 0;
1010 glVertex 0 , $self->{h}; 1095 glVertex 0 , $self->{h};
1011 glVertex $self->{w}, $self->{h}; 1096 glVertex $self->{w}, $self->{h};
1012 glVertex $self->{w}, 0; 1097 glVertex $self->{w}, 0;
1013 glEnd; 1098 glEnd;
1099 glDisable GL_BLEND;
1014 1100
1015 $self->SUPER::_draw; 1101 $self->SUPER::_draw;
1016 1102
1017 #TODO: force update every cursor change :( 1103 #TODO: force update every cursor change :(
1018 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1104 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1032 1118
1033use SDL::OpenGL; 1119use SDL::OpenGL;
1034 1120
1035our @ISA = CFClient::UI::DrawBG::; 1121our @ISA = CFClient::UI::DrawBG::;
1036 1122
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
1046sub new { 1123sub new {
1047 my $class = shift; 1124 my $class = shift;
1048 1125
1049 # range [value, low, high, page] 1126 # range [value, low, high, page]
1050 1127
1051 $class->SUPER::new ( 1128 my $self = $class->SUPER::new (
1052 fg => [1, 1, 1], 1129 fg => [1, 1, 1],
1053 active_fg => [0, 0, 0], 1130 active_fg => [0, 0, 0],
1054 range => [0, 0, 100, 10], 1131 range => [0, 0, 100, 10],
1132 req_w => 40,
1133 req_h => 10,
1055 vertical => 1, 1134 vertical => 0,
1056 @_ 1135 @_
1057 ) 1136 );
1137
1138 $self
1139}
1140
1141sub size_request {
1142 my ($self) = @_;
1143
1144 my $w = $self->{req_w};
1145 my $h = $self->{req_h};
1146
1147 $self->{vertical} ? ($h, $w) : ($w, $h)
1058} 1148}
1059 1149
1060sub button_down { 1150sub button_down {
1061 my ($self, $ev, $x, $y) = @_; 1151 my ($self, $ev, $x, $y) = @_;
1062 1152

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines