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.342 by root, Sun Aug 13 14:38:35 2006 UTC vs.
Revision 1.348 by root, Thu Aug 17 01:27:28 2006 UTC

909} 909}
910 910
911sub _draw { 911sub _draw {
912 my ($self) = @_; 912 my ($self) = @_;
913 913
914 my ($w, $h) = @$self{qw(w h)};
915
916 my $tex = $self->{texture} 914 my $tex = $self->{texture}
917 or return; 915 or return;
918 916
919 glEnable GL_TEXTURE_2D; 917 glEnable GL_TEXTURE_2D;
920 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 918 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
921 glColor 0, 0, 0, 1; 919 glColor 0, 0, 0, 1;
922 920
923 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 921 $tex->draw_quad_alpha_premultiplied (0, 0);
924 922
925 glDisable GL_TEXTURE_2D; 923 glDisable GL_TEXTURE_2D;
926} 924}
927 925
928############################################################################# 926#############################################################################
1129} 1127}
1130 1128
1131############################################################################# 1129#############################################################################
1132 1130
1133package CFPlus::UI::FancyFrame; 1131package CFPlus::UI::FancyFrame;
1132
1133our @ISA = CFPlus::UI::Bin::;
1134
1135use CFPlus::OpenGL;
1136
1137sub new {
1138 my ($class, %arg) = @_;
1139
1140 if ((exists $arg{label}) && !ref $arg{label}) {
1141 $arg{label} = new CFPlus::UI::Label
1142 align => 1,
1143 valign => 0,
1144 text => $arg{label},
1145 fontsize => ($arg{border} || 0.8) * 0.75;
1146 }
1147
1148 my $self = $class->SUPER::new (
1149 # label => "",
1150 fg => [0.6, 0.3, 0.1],
1151 border => 0.8,
1152 style => 'single',
1153 %arg,
1154 );
1155
1156 $self
1157}
1158
1159sub add {
1160 my ($self, @widgets) = @_;
1161
1162 $self->SUPER::add (@widgets);
1163 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label};
1164}
1165
1166sub border {
1167 int $_[0]{border} * $::FONTSIZE
1168}
1169
1170sub size_request {
1171 my ($self) = @_;
1172
1173 ($self->{label_w}, undef) = $self->{label}->size_request
1174 if $self->{label};
1175
1176 my ($w, $h) = $self->SUPER::size_request;
1177
1178 (
1179 $w + $self->border * 2,
1180 $h + $self->border * 2,
1181 )
1182}
1183
1184sub invoke_size_allocate {
1185 my ($self, $w, $h) = @_;
1186
1187 my $border = $self->border;
1188
1189 $w -= List::Util::max 0, $border * 2;
1190 $h -= List::Util::max 0, $border * 2;
1191
1192 if (my $label = $self->{label}) {
1193 $label->{w} = List::Util::max 0, List::Util::min $self->{label_w}, $w - $border * 2;
1194 $label->{h} = List::Util::min $h, $border;
1195 $label->invoke_size_allocate ($label->{w}, $label->{h});
1196 }
1197
1198 $self->child->configure ($border, $border, $w, $h);
1199
1200 1
1201}
1202
1203sub _draw {
1204 my ($self) = @_;
1205
1206 my $child = $self->{children}[0];
1207
1208 my $border = $self->border;
1209 my ($w, $h) = ($self->{w}, $self->{h});
1210
1211 $child->draw;
1212
1213 glColor @{$self->{fg}};
1214 glBegin GL_LINE_STRIP;
1215 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1216 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1217 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1218 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1219 glVertex $w - $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1220 glVertex $self->{label} ? $border * 2 + $self->{label}{w} : $border * 1.5, $border * 0.5 + 0.5;
1221 glEnd;
1222
1223 if ($self->{label}) {
1224 glTranslate $border * 2, 0;
1225 $self->{label}->_draw;
1226 }
1227}
1228
1229#############################################################################
1230
1231package CFPlus::UI::Toplevel;
1134 1232
1135our @ISA = CFPlus::UI::Bin::; 1233our @ISA = CFPlus::UI::Bin::;
1136 1234
1137use CFPlus::OpenGL; 1235use CFPlus::OpenGL;
1138 1236
1618 #active_bg => none 1716 #active_bg => none
1619 #font => default_font 1717 #font => default_font
1620 #text => initial text 1718 #text => initial text
1621 #markup => initial narkup 1719 #markup => initial narkup
1622 #max_w => maximum pixel width 1720 #max_w => maximum pixel width
1721 #style => 0, # render flags
1623 ellipsise => 3, # end 1722 ellipsise => 3, # end
1624 layout => (new CFPlus::Layout), 1723 layout => (new CFPlus::Layout),
1625 fontsize => 1, 1724 fontsize => 1,
1626 align => -1, 1725 align => -1,
1627 valign => -1, 1726 valign => -1,
1778 }; 1877 };
1779 1878
1780 my $w = List::Util::min $self->{w} + 4, $size->[0]; 1879 my $w = List::Util::min $self->{w} + 4, $size->[0];
1781 my $h = List::Util::min $self->{h} + 2, $size->[1]; 1880 my $h = List::Util::min $self->{h} + 2, $size->[1];
1782 1881
1783 $self->{layout}->render ($self->{ox}, $self->{oy}); 1882 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1784} 1883}
1785 1884
1786############################################################################# 1885#############################################################################
1787 1886
1788package CFPlus::UI::EntryBase; 1887package CFPlus::UI::EntryBase;
1867 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1966 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1868 } elsif ($sym == CFPlus::SDLK_HOME) { 1967 } elsif ($sym == CFPlus::SDLK_HOME) {
1869 $self->{cursor} = 0; 1968 $self->{cursor} = 0;
1870 } elsif ($sym == CFPlus::SDLK_END) { 1969 } elsif ($sym == CFPlus::SDLK_END) {
1871 $self->{cursor} = length $text; 1970 $self->{cursor} = length $text;
1971 } elsif ($uni == 21) { # ctrl-u
1972 $text = "";
1872 } elsif ($uni == 27) { 1973 } elsif ($uni == 27) {
1873 $self->emit ('escape'); 1974 $self->emit ('escape');
1874 } elsif ($uni) { 1975 } elsif ($uni) {
1875 substr $text, $self->{cursor}++, 0, chr $uni; 1976 substr $text, $self->{cursor}++, 0, chr $uni;
1876 } else { 1977 } else {
1949 utf8::encode $text; 2050 utf8::encode $text;
1950 2051
1951 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2052 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1952 } 2053 }
1953 2054
1954 glColor @{$self->{fg}};
1955 glBegin GL_LINES; 2055 glBegin GL_LINES;
1956 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2056 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1957 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2057 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1958 glEnd; 2058 glEnd;
1959 } 2059 }
1960} 2060}
1961 2061
1962package CFPlus::UI::Entry; 2062package CFPlus::UI::Entry;
2752 2852
2753 # todo: base offset on lines or so, not on pixels 2853 # todo: base offset on lines or so, not on pixels
2754 $self->{children}[1]->set_value ($offset); 2854 $self->{children}[1]->set_value ($offset);
2755} 2855}
2756 2856
2857sub current_paragraph {
2858 my ($self) = @_;
2859
2860 $self->{top_paragraph} - 1
2861}
2862
2863sub scroll_to {
2864 my ($self, $para) = @_;
2865
2866 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para;
2867
2868 $self->{scroll_to} = $para;
2869 $self->update;
2870}
2871
2757sub clear { 2872sub clear {
2758 my ($self) = @_; 2873 my ($self) = @_;
2759 2874
2760 my (undef, undef, @other) = @{ $self->{children} }; 2875 my (undef, undef, @other) = @{ $self->{children} };
2761 $self->remove ($_) for @other; 2876 $self->remove ($_) for @other;
2788} 2903}
2789 2904
2790sub scroll_to_bottom { 2905sub scroll_to_bottom {
2791 my ($self) = @_; 2906 my ($self) = @_;
2792 2907
2793 $self->{scroll_to_bottom} = 1; 2908 $self->{scroll_to} = $#{$self->{par}};
2794 $self->update; 2909 $self->update;
2795} 2910}
2796 2911
2912sub force_uptodate {
2913 my ($self) = @_;
2914
2915 if (delete $self->{need_reflow}) {
2916 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2917
2918 my $height = 0;
2919
2920 for my $para (@{$self->{par}}) {
2921 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2922 my $layout = $self->get_layout ($para);
2923 my ($w, $h) = $layout->size;
2924
2925 $para->{w} = $w + $para->{indent};
2926 $para->{h} = $h;
2927 $para->{wrapped} = $layout->has_wrapped;
2928 }
2929
2930 $para->{y} = $height;
2931 $height += $para->{h};
2932 }
2933
2934 $self->{height} = $height;
2935 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2936
2937 delete $self->{texture};
2938 }
2939
2940 if (my $paridx = delete $self->{scroll_to}) {
2941 $self->{children}[1]->set_value ($self->{par}[$paridx]{y});
2942 }
2943}
2944
2797sub update { 2945sub update {
2798 my ($self) = @_; 2946 my ($self) = @_;
2799 2947
2800 $self->SUPER::update; 2948 $self->SUPER::update;
2801 2949
2802 return unless $self->{h} > 0; 2950 return unless $self->{h} > 0;
2803 2951
2804 delete $self->{texture}; 2952 delete $self->{texture};
2805 2953
2806 $ROOT->on_post_alloc ($self => sub { 2954 $ROOT->on_post_alloc ($self => sub {
2955 $self->force_uptodate;
2956
2807 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 2957 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2808
2809 if (delete $self->{need_reflow}) {
2810 my $height = 0;
2811
2812 for my $para (@{$self->{par}}) {
2813 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2814 my $layout = $self->get_layout ($para);
2815 my ($w, $h) = $layout->size;
2816
2817 $para->{w} = $w + $para->{indent};
2818 $para->{h} = $h;
2819 $para->{wrapped} = $layout->has_wrapped;
2820 }
2821
2822 $height += $para->{h};
2823 }
2824
2825 $self->{height} = $height;
2826
2827 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2828
2829 delete $self->{texture};
2830 }
2831
2832 if (delete $self->{scroll_to_bottom}) {
2833 $self->{children}[1]->set_value (1e10);
2834 }
2835 2958
2836 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 2959 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub {
2837 glClearColor 0, 0, 0, 0; 2960 glClearColor 0, 0, 0, 0;
2838 glClear GL_COLOR_BUFFER_BIT; 2961 glClear GL_COLOR_BUFFER_BIT;
2839 2962
2840 my $top = int $self->{children}[1]{range}[0]; 2963 my $top = int $self->{children}[1]{range}[0];
2841 2964
2965 my $paridx = 0;
2966 my $top_paragraph;
2967 my $top = int $self->{children}[1]{range}[0];
2968
2842 my $y0 = $top; 2969 my $y0 = $top;
2843 my $y1 = $top + $H; 2970 my $y1 = $top + $H;
2844 2971
2845 my $y = 0;
2846
2847 for my $para (@{$self->{par}}) { 2972 for my $para (@{$self->{par}}) {
2848 my $h = $para->{h}; 2973 my $h = $para->{h};
2974 my $y = $para->{y};
2849 2975
2850 if ($y0 < $y + $h && $y < $y1) { 2976 if ($y0 < $y + $h && $y < $y1) {
2851
2852 my $layout = $self->get_layout ($para); 2977 my $layout = $self->get_layout ($para);
2853 2978
2854 $layout->render ($para->{indent}, $y - $y0); 2979 $layout->render ($para->{indent}, $y - $y0);
2855 2980
2856 if (my @w = @{ $para->{widget} }) { 2981 if (my @w = @{ $para->{widget} }) {
2865 $_->draw; 2990 $_->draw;
2866 } 2991 }
2867 } 2992 }
2868 } 2993 }
2869 2994
2870 $y += $h; 2995 $paridx++;
2996 $top_paragraph ||= $paridx if $y >= $top;
2871 } 2997 }
2998
2999 $self->{top_paragraph} = $top_paragraph;
2872 }; 3000 };
2873 }); 3001 });
2874} 3002}
2875 3003
2876sub reconfigure { 3004sub reconfigure {
2997 3125
2998 $self->add (new CFPlus::UI::Label 3126 $self->add (new CFPlus::UI::Label
2999 markup => $tooltip, 3127 markup => $tooltip,
3000 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3128 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3001 fontsize => 0.8, 3129 fontsize => 0.8,
3002 fg => [0, 0, 0, 1], 3130 style => 1, # FLAG_INVERSE
3003 ellipsise => 0, 3131 ellipsise => 0,
3004 font => ($widget->{tooltip_font} || $::FONT_PROP), 3132 font => ($widget->{tooltip_font} || $::FONT_PROP),
3005 ); 3133 );
3006} 3134}
3007 3135
3161 3289
3162############################################################################# 3290#############################################################################
3163 3291
3164package CFPlus::UI::Menu; 3292package CFPlus::UI::Menu;
3165 3293
3166our @ISA = CFPlus::UI::FancyFrame::; 3294our @ISA = CFPlus::UI::Toplevel::;
3167 3295
3168use CFPlus::OpenGL; 3296use CFPlus::OpenGL;
3169 3297
3170sub new { 3298sub new {
3171 my $class = shift; 3299 my $class = shift;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines