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.340 by root, Sun Jul 30 13:16:44 2006 UTC vs.
Revision 1.353 by root, Thu Nov 16 19:42:45 2006 UTC

6use Scalar::Util (); 6use Scalar::Util ();
7use List::Util (); 7use List::Util ();
8use Event; 8use Event;
9 9
10use CFPlus; 10use CFPlus;
11use CFPlus::Pod;
11use CFPlus::Texture; 12use CFPlus::Texture;
12 13
13our ($FOCUS, $HOVER, $GRAB); # various widgets 14our ($FOCUS, $HOVER, $GRAB); # various widgets
14 15
15our $LAYOUT; 16our $LAYOUT;
897 glClear GL_COLOR_BUFFER_BIT; 898 glClear GL_COLOR_BUFFER_BIT;
898 899
899 { 900 {
900 package CFPlus::UI::Base; 901 package CFPlus::UI::Base;
901 902
902 ($draw_x, $draw_y, $draw_w, $draw_h) = 903 local ($draw_x, $draw_y, $draw_w, $draw_h) =
903 (0, 0, $self->{w}, $self->{h}); 904 (0, 0, $self->{w}, $self->{h});
905
906 $self->_render;
904 } 907 }
905
906 $self->_render;
907 }; 908 };
908} 909}
909 910
910sub _draw { 911sub _draw {
911 my ($self) = @_; 912 my ($self) = @_;
912
913 my ($w, $h) = @$self{qw(w h)};
914 913
915 my $tex = $self->{texture} 914 my $tex = $self->{texture}
916 or return; 915 or return;
917 916
918 glEnable GL_TEXTURE_2D; 917 glEnable GL_TEXTURE_2D;
919 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 918 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
920 glColor 0, 0, 0, 1; 919 glColor 0, 0, 0, 1;
921 920
922 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 921 $tex->draw_quad_alpha_premultiplied (0, 0);
923 922
924 glDisable GL_TEXTURE_2D; 923 glDisable GL_TEXTURE_2D;
925} 924}
926 925
927############################################################################# 926#############################################################################
1128} 1127}
1129 1128
1130############################################################################# 1129#############################################################################
1131 1130
1132package 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;
1133 1232
1134our @ISA = CFPlus::UI::Bin::; 1233our @ISA = CFPlus::UI::Bin::;
1135 1234
1136use CFPlus::OpenGL; 1235use CFPlus::OpenGL;
1137 1236
1617 #active_bg => none 1716 #active_bg => none
1618 #font => default_font 1717 #font => default_font
1619 #text => initial text 1718 #text => initial text
1620 #markup => initial narkup 1719 #markup => initial narkup
1621 #max_w => maximum pixel width 1720 #max_w => maximum pixel width
1721 #style => 0, # render flags
1622 ellipsise => 3, # end 1722 ellipsise => 3, # end
1623 layout => (new CFPlus::Layout), 1723 layout => (new CFPlus::Layout),
1624 fontsize => 1, 1724 fontsize => 1,
1625 align => -1, 1725 align => -1,
1626 valign => -1, 1726 valign => -1,
1777 }; 1877 };
1778 1878
1779 my $w = List::Util::min $self->{w} + 4, $size->[0]; 1879 my $w = List::Util::min $self->{w} + 4, $size->[0];
1780 my $h = List::Util::min $self->{h} + 2, $size->[1]; 1880 my $h = List::Util::min $self->{h} + 2, $size->[1];
1781 1881
1782 $self->{layout}->render ($self->{ox}, $self->{oy}); 1882 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1783} 1883}
1784 1884
1785############################################################################# 1885#############################################################################
1786 1886
1787package CFPlus::UI::EntryBase; 1887package CFPlus::UI::EntryBase;
1866 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1966 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1867 } elsif ($sym == CFPlus::SDLK_HOME) { 1967 } elsif ($sym == CFPlus::SDLK_HOME) {
1868 $self->{cursor} = 0; 1968 $self->{cursor} = 0;
1869 } elsif ($sym == CFPlus::SDLK_END) { 1969 } elsif ($sym == CFPlus::SDLK_END) {
1870 $self->{cursor} = length $text; 1970 $self->{cursor} = length $text;
1971 } elsif ($uni == 21) { # ctrl-u
1972 $text = "";
1973 $self->{cursor} = 0;
1871 } elsif ($uni == 27) { 1974 } elsif ($uni == 27) {
1872 $self->emit ('escape'); 1975 $self->emit ('escape');
1873 } elsif ($uni) { 1976 } elsif ($uni) {
1874 substr $text, $self->{cursor}++, 0, chr $uni; 1977 substr $text, $self->{cursor}++, 0, chr $uni;
1875 } else { 1978 } else {
1948 utf8::encode $text; 2051 utf8::encode $text;
1949 2052
1950 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2053 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1951 } 2054 }
1952 2055
1953 glColor @{$self->{fg}};
1954 glBegin GL_LINES; 2056 glBegin GL_LINES;
1955 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2057 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1956 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2058 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1957 glEnd; 2059 glEnd;
1958 } 2060 }
1959} 2061}
1960 2062
1961package CFPlus::UI::Entry; 2063package CFPlus::UI::Entry;
2751 2853
2752 # todo: base offset on lines or so, not on pixels 2854 # todo: base offset on lines or so, not on pixels
2753 $self->{children}[1]->set_value ($offset); 2855 $self->{children}[1]->set_value ($offset);
2754} 2856}
2755 2857
2858sub current_paragraph {
2859 my ($self) = @_;
2860
2861 $self->{top_paragraph} - 1
2862}
2863
2864sub scroll_to {
2865 my ($self, $para) = @_;
2866
2867 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para;
2868
2869 $self->{scroll_to} = $para;
2870 $self->update;
2871}
2872
2756sub clear { 2873sub clear {
2757 my ($self) = @_; 2874 my ($self) = @_;
2758 2875
2759 my (undef, undef, @other) = @{ $self->{children} }; 2876 my (undef, undef, @other) = @{ $self->{children} };
2760 $self->remove ($_) for @other; 2877 $self->remove ($_) for @other;
2787} 2904}
2788 2905
2789sub scroll_to_bottom { 2906sub scroll_to_bottom {
2790 my ($self) = @_; 2907 my ($self) = @_;
2791 2908
2792 $self->{scroll_to_bottom} = 1; 2909 $self->{scroll_to} = $#{$self->{par}};
2793 $self->update; 2910 $self->update;
2794} 2911}
2795 2912
2913sub force_uptodate {
2914 my ($self) = @_;
2915
2916 if (delete $self->{need_reflow}) {
2917 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2918
2919 my $height = 0;
2920
2921 for my $para (@{$self->{par}}) {
2922 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2923 my $layout = $self->get_layout ($para);
2924 my ($w, $h) = $layout->size;
2925
2926 $para->{w} = $w + $para->{indent};
2927 $para->{h} = $h;
2928 $para->{wrapped} = $layout->has_wrapped;
2929 }
2930
2931 $para->{y} = $height;
2932 $height += $para->{h};
2933 }
2934
2935 $self->{height} = $height;
2936 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2937
2938 delete $self->{texture};
2939 }
2940
2941 if (my $paridx = delete $self->{scroll_to}) {
2942 $self->{children}[1]->set_value ($self->{par}[$paridx]{y});
2943 }
2944}
2945
2796sub update { 2946sub update {
2797 my ($self) = @_; 2947 my ($self) = @_;
2798 2948
2799 $self->SUPER::update; 2949 $self->SUPER::update;
2800 2950
2801 return unless $self->{h} > 0; 2951 return unless $self->{h} > 0;
2802 2952
2803 delete $self->{texture}; 2953 delete $self->{texture};
2804 2954
2805 $ROOT->on_post_alloc ($self => sub { 2955 $ROOT->on_post_alloc ($self => sub {
2956 $self->force_uptodate;
2957
2806 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 2958 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2807
2808 if (delete $self->{need_reflow}) {
2809 my $height = 0;
2810
2811 for my $para (@{$self->{par}}) {
2812 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2813 my $layout = $self->get_layout ($para);
2814 my ($w, $h) = $layout->size;
2815
2816 $para->{w} = $w + $para->{indent};
2817 $para->{h} = $h;
2818 $para->{wrapped} = $layout->has_wrapped;
2819 }
2820
2821 $height += $para->{h};
2822 }
2823
2824 $self->{height} = $height;
2825
2826 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2827
2828 delete $self->{texture};
2829 }
2830
2831 if (delete $self->{scroll_to_bottom}) {
2832 $self->{children}[1]->set_value (1e10);
2833 }
2834 2959
2835 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 2960 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub {
2836 glClearColor 0, 0, 0, 0; 2961 glClearColor 0, 0, 0, 0;
2837 glClear GL_COLOR_BUFFER_BIT; 2962 glClear GL_COLOR_BUFFER_BIT;
2838 2963
2964 package CFPlus::UI::Base;
2965 local ($draw_x, $draw_y, $draw_w, $draw_h) =
2966 (0, 0, $self->{w}, $self->{h});
2967
2968 my $top = int $self->{children}[1]{range}[0];
2969
2970 my $paridx = 0;
2971 my $top_paragraph;
2839 my $top = int $self->{children}[1]{range}[0]; 2972 my $top = int $self->{children}[1]{range}[0];
2840 2973
2841 my $y0 = $top; 2974 my $y0 = $top;
2842 my $y1 = $top + $H; 2975 my $y1 = $top + $H;
2843 2976
2844 my $y = 0;
2845
2846 for my $para (@{$self->{par}}) { 2977 for my $para (@{$self->{par}}) {
2847 my $h = $para->{h}; 2978 my $h = $para->{h};
2979 my $y = $para->{y};
2848 2980
2849 if ($y0 < $y + $h && $y < $y1) { 2981 if ($y0 < $y + $h && $y < $y1) {
2850
2851 my $layout = $self->get_layout ($para); 2982 my $layout = $self->get_layout ($para);
2852 2983
2853 $layout->render ($para->{indent}, $y - $y0); 2984 $layout->render ($para->{indent}, $y - $y0);
2854 2985
2855 if (my @w = @{ $para->{widget} }) { 2986 if (my @w = @{ $para->{widget} }) {
2864 $_->draw; 2995 $_->draw;
2865 } 2996 }
2866 } 2997 }
2867 } 2998 }
2868 2999
2869 $y += $h; 3000 $paridx++;
3001 $top_paragraph ||= $paridx if $y >= $top;
2870 } 3002 }
3003
3004 $self->{top_paragraph} = $top_paragraph;
2871 }; 3005 };
2872 }); 3006 });
2873} 3007}
2874 3008
2875sub reconfigure { 3009sub reconfigure {
2977} 3111}
2978 3112
2979sub set_tooltip_from { 3113sub set_tooltip_from {
2980 my ($self, $widget) = @_; 3114 my ($self, $widget) = @_;
2981 3115
3116 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3117 if $widget->{tooltip} =~ /^#(.*)$/;
3118
2982 my $tooltip = $widget->{tooltip}; 3119 my $tooltip = $widget->{tooltip};
2983 3120
2984 if ($ENV{CFPLUS_DEBUG} & 2) { 3121 if ($ENV{CFPLUS_DEBUG} & 2) {
2985 $tooltip .= "\n\n" . (ref $widget) . "\n" 3122 $tooltip .= "\n\n" . (ref $widget) . "\n"
2986 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3123 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
2993 3130
2994 $self->add (new CFPlus::UI::Label 3131 $self->add (new CFPlus::UI::Label
2995 markup => $tooltip, 3132 markup => $tooltip,
2996 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3133 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2997 fontsize => 0.8, 3134 fontsize => 0.8,
2998 fg => [0, 0, 0, 1], 3135 style => 1, # FLAG_INVERSE
2999 ellipsise => 0, 3136 ellipsise => 0,
3000 font => ($widget->{tooltip_font} || $::FONT_PROP), 3137 font => ($widget->{tooltip_font} || $::FONT_PROP),
3001 ); 3138 );
3002} 3139}
3003 3140
3151 3288
3152package CFPlus::UI::Buttonbar; 3289package CFPlus::UI::Buttonbar;
3153 3290
3154our @ISA = CFPlus::UI::HBox::; 3291our @ISA = CFPlus::UI::HBox::;
3155 3292
3156# TODO: should actualyl wrap buttons and other goodies. 3293# TODO: should actually wrap buttons and other goodies.
3157 3294
3158############################################################################# 3295#############################################################################
3159 3296
3160package CFPlus::UI::Menu; 3297package CFPlus::UI::Menu;
3161 3298
3162our @ISA = CFPlus::UI::FancyFrame::; 3299our @ISA = CFPlus::UI::Toplevel::;
3163 3300
3164use CFPlus::OpenGL; 3301use CFPlus::OpenGL;
3165 3302
3166sub new { 3303sub new {
3167 my $class = shift; 3304 my $class = shift;
3703 } 3840 }
3704 3841
3705 1 3842 1
3706 }; 3843 };
3707 3844
3708 my $tooltip = "$spell->{message}$TOOLTIP_ALL"; 3845 my $tooltip = (CFPlus::asxml $spell->{message}) . $TOOLTIP_ALL;
3709 3846
3710 #TODO: add path info to tooltip 3847 #TODO: add path info to tooltip
3711 #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path}); 3848 #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path});
3712 3849
3713 $self->add (0, $row, new CFPlus::UI::Face 3850 $self->add (0, $row, new CFPlus::UI::Face
3991 glLoadIdentity; 4128 glLoadIdentity;
3992 4129
3993 { 4130 {
3994 package CFPlus::UI::Base; 4131 package CFPlus::UI::Base;
3995 4132
3996 ($draw_x, $draw_y, $draw_w, $draw_h) = 4133 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3997 (0, 0, $self->{w}, $self->{h}); 4134 (0, 0, $self->{w}, $self->{h});
3998 }
3999 4135
4000 $self->_draw; 4136 $self->_draw;
4137 }
4001} 4138}
4002 4139
4003############################################################################# 4140#############################################################################
4004 4141
4005package CFPlus::UI; 4142package CFPlus::UI;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines