… | |
… | |
909 | } |
909 | } |
910 | |
910 | |
911 | sub _draw { |
911 | sub _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 | |
1133 | package CFPlus::UI::FancyFrame; |
1131 | package CFPlus::UI::FancyFrame; |
|
|
1132 | |
|
|
1133 | our @ISA = CFPlus::UI::Bin::; |
|
|
1134 | |
|
|
1135 | use CFPlus::OpenGL; |
|
|
1136 | |
|
|
1137 | sub 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 | |
|
|
1159 | sub add { |
|
|
1160 | my ($self, @widgets) = @_; |
|
|
1161 | |
|
|
1162 | $self->SUPER::add (@widgets); |
|
|
1163 | $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; |
|
|
1164 | } |
|
|
1165 | |
|
|
1166 | sub border { |
|
|
1167 | int $_[0]{border} * $::FONTSIZE |
|
|
1168 | } |
|
|
1169 | |
|
|
1170 | sub 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 | |
|
|
1184 | sub 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 | |
|
|
1203 | sub _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 | |
|
|
1231 | package CFPlus::UI::Toplevel; |
1134 | |
1232 | |
1135 | our @ISA = CFPlus::UI::Bin::; |
1233 | our @ISA = CFPlus::UI::Bin::; |
1136 | |
1234 | |
1137 | use CFPlus::OpenGL; |
1235 | use CFPlus::OpenGL; |
1138 | |
1236 | |
… | |
… | |
1950 | utf8::encode $text; |
2048 | utf8::encode $text; |
1951 | |
2049 | |
1952 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) |
2050 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) |
1953 | } |
2051 | } |
1954 | |
2052 | |
1955 | glColor @{$self->{fg}}; |
|
|
1956 | glBegin GL_LINES; |
2053 | glBegin GL_LINES; |
1957 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; |
2054 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; |
1958 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
2055 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
1959 | glEnd; |
2056 | glEnd; |
1960 | } |
2057 | } |
1961 | } |
2058 | } |
1962 | |
2059 | |
1963 | package CFPlus::UI::Entry; |
2060 | package CFPlus::UI::Entry; |
… | |
… | |
2753 | |
2850 | |
2754 | # todo: base offset on lines or so, not on pixels |
2851 | # todo: base offset on lines or so, not on pixels |
2755 | $self->{children}[1]->set_value ($offset); |
2852 | $self->{children}[1]->set_value ($offset); |
2756 | } |
2853 | } |
2757 | |
2854 | |
|
|
2855 | sub current_paragraph { |
|
|
2856 | my ($self) = @_; |
|
|
2857 | |
|
|
2858 | $self->{top_paragraph} - 1 |
|
|
2859 | } |
|
|
2860 | |
|
|
2861 | sub scroll_to { |
|
|
2862 | my ($self, $para) = @_; |
|
|
2863 | |
|
|
2864 | $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para; |
|
|
2865 | |
|
|
2866 | $self->{scroll_to} = $para; |
|
|
2867 | $self->update; |
|
|
2868 | } |
|
|
2869 | |
2758 | sub clear { |
2870 | sub clear { |
2759 | my ($self) = @_; |
2871 | my ($self) = @_; |
2760 | |
2872 | |
2761 | my (undef, undef, @other) = @{ $self->{children} }; |
2873 | my (undef, undef, @other) = @{ $self->{children} }; |
2762 | $self->remove ($_) for @other; |
2874 | $self->remove ($_) for @other; |
… | |
… | |
2789 | } |
2901 | } |
2790 | |
2902 | |
2791 | sub scroll_to_bottom { |
2903 | sub scroll_to_bottom { |
2792 | my ($self) = @_; |
2904 | my ($self) = @_; |
2793 | |
2905 | |
2794 | $self->{scroll_to_bottom} = 1; |
2906 | $self->{scroll_to} = $#{$self->{par}}; |
2795 | $self->update; |
2907 | $self->update; |
2796 | } |
2908 | } |
2797 | |
2909 | |
|
|
2910 | sub force_uptodate { |
|
|
2911 | my ($self) = @_; |
|
|
2912 | |
|
|
2913 | if (delete $self->{need_reflow}) { |
|
|
2914 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
|
|
2915 | |
|
|
2916 | my $height = 0; |
|
|
2917 | |
|
|
2918 | for my $para (@{$self->{par}}) { |
|
|
2919 | if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) { |
|
|
2920 | my $layout = $self->get_layout ($para); |
|
|
2921 | my ($w, $h) = $layout->size; |
|
|
2922 | |
|
|
2923 | $para->{w} = $w + $para->{indent}; |
|
|
2924 | $para->{h} = $h; |
|
|
2925 | $para->{wrapped} = $layout->has_wrapped; |
|
|
2926 | } |
|
|
2927 | |
|
|
2928 | $para->{y} = $height; |
|
|
2929 | $height += $para->{h}; |
|
|
2930 | } |
|
|
2931 | |
|
|
2932 | $self->{height} = $height; |
|
|
2933 | $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]); |
|
|
2934 | |
|
|
2935 | delete $self->{texture}; |
|
|
2936 | } |
|
|
2937 | |
|
|
2938 | if (my $paridx = delete $self->{scroll_to}) { |
|
|
2939 | $self->{children}[1]->set_value ($self->{par}[$paridx]{y}); |
|
|
2940 | } |
|
|
2941 | } |
|
|
2942 | |
2798 | sub update { |
2943 | sub update { |
2799 | my ($self) = @_; |
2944 | my ($self) = @_; |
2800 | |
2945 | |
2801 | $self->SUPER::update; |
2946 | $self->SUPER::update; |
2802 | |
2947 | |
2803 | return unless $self->{h} > 0; |
2948 | return unless $self->{h} > 0; |
2804 | |
2949 | |
2805 | delete $self->{texture}; |
2950 | delete $self->{texture}; |
2806 | |
2951 | |
2807 | $ROOT->on_post_alloc ($self => sub { |
2952 | $ROOT->on_post_alloc ($self => sub { |
|
|
2953 | $self->force_uptodate; |
|
|
2954 | |
2808 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
2955 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
2809 | |
|
|
2810 | if (delete $self->{need_reflow}) { |
|
|
2811 | my $height = 0; |
|
|
2812 | |
|
|
2813 | for my $para (@{$self->{par}}) { |
|
|
2814 | if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) { |
|
|
2815 | my $layout = $self->get_layout ($para); |
|
|
2816 | my ($w, $h) = $layout->size; |
|
|
2817 | |
|
|
2818 | $para->{w} = $w + $para->{indent}; |
|
|
2819 | $para->{h} = $h; |
|
|
2820 | $para->{wrapped} = $layout->has_wrapped; |
|
|
2821 | } |
|
|
2822 | |
|
|
2823 | $height += $para->{h}; |
|
|
2824 | } |
|
|
2825 | |
|
|
2826 | $self->{height} = $height; |
|
|
2827 | |
|
|
2828 | $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]); |
|
|
2829 | |
|
|
2830 | delete $self->{texture}; |
|
|
2831 | } |
|
|
2832 | |
|
|
2833 | if (delete $self->{scroll_to_bottom}) { |
|
|
2834 | $self->{children}[1]->set_value (1e10); |
|
|
2835 | } |
|
|
2836 | |
2956 | |
2837 | $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { |
2957 | $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { |
2838 | glClearColor 0, 0, 0, 0; |
2958 | glClearColor 0, 0, 0, 0; |
2839 | glClear GL_COLOR_BUFFER_BIT; |
2959 | glClear GL_COLOR_BUFFER_BIT; |
2840 | |
2960 | |
2841 | my $top = int $self->{children}[1]{range}[0]; |
2961 | my $top = int $self->{children}[1]{range}[0]; |
2842 | |
2962 | |
|
|
2963 | my $paridx = 0; |
|
|
2964 | my $top_paragraph; |
|
|
2965 | my $top = int $self->{children}[1]{range}[0]; |
|
|
2966 | |
2843 | my $y0 = $top; |
2967 | my $y0 = $top; |
2844 | my $y1 = $top + $H; |
2968 | my $y1 = $top + $H; |
2845 | |
2969 | |
2846 | my $y = 0; |
|
|
2847 | |
|
|
2848 | for my $para (@{$self->{par}}) { |
2970 | for my $para (@{$self->{par}}) { |
2849 | my $h = $para->{h}; |
2971 | my $h = $para->{h}; |
|
|
2972 | my $y = $para->{y}; |
2850 | |
2973 | |
2851 | if ($y0 < $y + $h && $y < $y1) { |
2974 | if ($y0 < $y + $h && $y < $y1) { |
2852 | |
|
|
2853 | my $layout = $self->get_layout ($para); |
2975 | my $layout = $self->get_layout ($para); |
2854 | |
2976 | |
2855 | $layout->render ($para->{indent}, $y - $y0); |
2977 | $layout->render ($para->{indent}, $y - $y0); |
2856 | |
2978 | |
2857 | if (my @w = @{ $para->{widget} }) { |
2979 | if (my @w = @{ $para->{widget} }) { |
… | |
… | |
2866 | $_->draw; |
2988 | $_->draw; |
2867 | } |
2989 | } |
2868 | } |
2990 | } |
2869 | } |
2991 | } |
2870 | |
2992 | |
2871 | $y += $h; |
2993 | $paridx++; |
|
|
2994 | $top_paragraph ||= $paridx if $y >= $top; |
2872 | } |
2995 | } |
|
|
2996 | |
|
|
2997 | $self->{top_paragraph} = $top_paragraph; |
2873 | }; |
2998 | }; |
2874 | }); |
2999 | }); |
2875 | } |
3000 | } |
2876 | |
3001 | |
2877 | sub reconfigure { |
3002 | sub reconfigure { |
… | |
… | |
3162 | |
3287 | |
3163 | ############################################################################# |
3288 | ############################################################################# |
3164 | |
3289 | |
3165 | package CFPlus::UI::Menu; |
3290 | package CFPlus::UI::Menu; |
3166 | |
3291 | |
3167 | our @ISA = CFPlus::UI::FancyFrame::; |
3292 | our @ISA = CFPlus::UI::Toplevel::; |
3168 | |
3293 | |
3169 | use CFPlus::OpenGL; |
3294 | use CFPlus::OpenGL; |
3170 | |
3295 | |
3171 | sub new { |
3296 | sub new { |
3172 | my $class = shift; |
3297 | my $class = shift; |