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.354 by root, Sat Nov 18 23:31:23 2006 UTC

514); 514);
515 515
516sub emit { 516sub emit {
517 my ($self, $signal, @args) = @_; 517 my ($self, $signal, @args) = @_;
518 518
519 # I do not really like this solution, but I dislike duplication 519 # I do not really like this solution, but I do not like duplication
520 # and needlessly verbose code, too. 520 # and needlessly verbose code, either.
521 my @append 521 my @append
522 = $has_coords{$signal} 522 = $has_coords{$signal}
523 ? $args[0]->xy ($self) 523 ? $args[0]->xy ($self)
524 : (); 524 : ();
525 525
526 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# 526 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d#
527 527
528 if ($self->{tooltip} =~ /Apply.*audio/) {
529 warn "2 ", join ":", @{ $self->{signal_cb}{$signal} || []}, "\n";#d#
530 warn "3 $self->{parent}\n";#d#
531 }
532
528 #d##TODO# stop propagating at first true, do not use sum 533 #d##TODO# stop propagating at first true, do not use sum
534 my $res =
529 (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before 535 (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before
530 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure 536 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure
531 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent 537 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent
538 ;
539 if ($self->{tooltip} =~ /Apply.*audio/) {
540 warn "4 ", join ":", @{ $self->{signal_cb}{$signal} || []}, "\n";#d#
541 warn "5 $self->{parent}\n";#d#
542 }
543
544 $res
532} 545}
533 546
534sub find_widget { 547sub find_widget {
535 my ($self, $x, $y) = @_; 548 my ($self, $x, $y) = @_;
536 549
898 glClear GL_COLOR_BUFFER_BIT; 911 glClear GL_COLOR_BUFFER_BIT;
899 912
900 { 913 {
901 package CFPlus::UI::Base; 914 package CFPlus::UI::Base;
902 915
903 ($draw_x, $draw_y, $draw_w, $draw_h) = 916 local ($draw_x, $draw_y, $draw_w, $draw_h) =
904 (0, 0, $self->{w}, $self->{h}); 917 (0, 0, $self->{w}, $self->{h});
918
919 $self->_render;
905 } 920 }
906
907 $self->_render;
908 }; 921 };
909} 922}
910 923
911sub _draw { 924sub _draw {
912 my ($self) = @_; 925 my ($self) = @_;
913
914 my ($w, $h) = @$self{qw(w h)};
915 926
916 my $tex = $self->{texture} 927 my $tex = $self->{texture}
917 or return; 928 or return;
918 929
919 glEnable GL_TEXTURE_2D; 930 glEnable GL_TEXTURE_2D;
920 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 931 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
921 glColor 0, 0, 0, 1; 932 glColor 0, 0, 0, 1;
922 933
923 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 934 $tex->draw_quad_alpha_premultiplied (0, 0);
924 935
925 glDisable GL_TEXTURE_2D; 936 glDisable GL_TEXTURE_2D;
926} 937}
927 938
928############################################################################# 939#############################################################################
1129} 1140}
1130 1141
1131############################################################################# 1142#############################################################################
1132 1143
1133package CFPlus::UI::FancyFrame; 1144package CFPlus::UI::FancyFrame;
1145
1146our @ISA = CFPlus::UI::Bin::;
1147
1148use CFPlus::OpenGL;
1149
1150sub new {
1151 my ($class, %arg) = @_;
1152
1153 if ((exists $arg{label}) && !ref $arg{label}) {
1154 $arg{label} = new CFPlus::UI::Label
1155 align => 1,
1156 valign => 0,
1157 text => $arg{label},
1158 fontsize => ($arg{border} || 0.8) * 0.75;
1159 }
1160
1161 my $self = $class->SUPER::new (
1162 # label => "",
1163 fg => [0.6, 0.3, 0.1],
1164 border => 0.8,
1165 style => 'single',
1166 %arg,
1167 );
1168
1169 $self
1170}
1171
1172sub add {
1173 my ($self, @widgets) = @_;
1174
1175 $self->SUPER::add (@widgets);
1176 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label};
1177}
1178
1179sub border {
1180 int $_[0]{border} * $::FONTSIZE
1181}
1182
1183sub size_request {
1184 my ($self) = @_;
1185
1186 ($self->{label_w}, undef) = $self->{label}->size_request
1187 if $self->{label};
1188
1189 my ($w, $h) = $self->SUPER::size_request;
1190
1191 (
1192 $w + $self->border * 2,
1193 $h + $self->border * 2,
1194 )
1195}
1196
1197sub invoke_size_allocate {
1198 my ($self, $w, $h) = @_;
1199
1200 my $border = $self->border;
1201
1202 $w -= List::Util::max 0, $border * 2;
1203 $h -= List::Util::max 0, $border * 2;
1204
1205 if (my $label = $self->{label}) {
1206 $label->{w} = List::Util::max 0, List::Util::min $self->{label_w}, $w - $border * 2;
1207 $label->{h} = List::Util::min $h, $border;
1208 $label->invoke_size_allocate ($label->{w}, $label->{h});
1209 }
1210
1211 $self->child->configure ($border, $border, $w, $h);
1212
1213 1
1214}
1215
1216sub _draw {
1217 my ($self) = @_;
1218
1219 my $child = $self->{children}[0];
1220
1221 my $border = $self->border;
1222 my ($w, $h) = ($self->{w}, $self->{h});
1223
1224 $child->draw;
1225
1226 glColor @{$self->{fg}};
1227 glBegin GL_LINE_STRIP;
1228 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1229 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1230 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1231 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1232 glVertex $w - $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1233 glVertex $self->{label} ? $border * 2 + $self->{label}{w} : $border * 1.5, $border * 0.5 + 0.5;
1234 glEnd;
1235
1236 if ($self->{label}) {
1237 glTranslate $border * 2, 0;
1238 $self->{label}->_draw;
1239 }
1240}
1241
1242#############################################################################
1243
1244package CFPlus::UI::Toplevel;
1134 1245
1135our @ISA = CFPlus::UI::Bin::; 1246our @ISA = CFPlus::UI::Bin::;
1136 1247
1137use CFPlus::OpenGL; 1248use CFPlus::OpenGL;
1138 1249
1618 #active_bg => none 1729 #active_bg => none
1619 #font => default_font 1730 #font => default_font
1620 #text => initial text 1731 #text => initial text
1621 #markup => initial narkup 1732 #markup => initial narkup
1622 #max_w => maximum pixel width 1733 #max_w => maximum pixel width
1734 #style => 0, # render flags
1623 ellipsise => 3, # end 1735 ellipsise => 3, # end
1624 layout => (new CFPlus::Layout), 1736 layout => (new CFPlus::Layout),
1625 fontsize => 1, 1737 fontsize => 1,
1626 align => -1, 1738 align => -1,
1627 valign => -1, 1739 valign => -1,
1778 }; 1890 };
1779 1891
1780 my $w = List::Util::min $self->{w} + 4, $size->[0]; 1892 my $w = List::Util::min $self->{w} + 4, $size->[0];
1781 my $h = List::Util::min $self->{h} + 2, $size->[1]; 1893 my $h = List::Util::min $self->{h} + 2, $size->[1];
1782 1894
1783 $self->{layout}->render ($self->{ox}, $self->{oy}); 1895 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1784} 1896}
1785 1897
1786############################################################################# 1898#############################################################################
1787 1899
1788package CFPlus::UI::EntryBase; 1900package CFPlus::UI::EntryBase;
1867 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1979 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1868 } elsif ($sym == CFPlus::SDLK_HOME) { 1980 } elsif ($sym == CFPlus::SDLK_HOME) {
1869 $self->{cursor} = 0; 1981 $self->{cursor} = 0;
1870 } elsif ($sym == CFPlus::SDLK_END) { 1982 } elsif ($sym == CFPlus::SDLK_END) {
1871 $self->{cursor} = length $text; 1983 $self->{cursor} = length $text;
1984 } elsif ($uni == 21) { # ctrl-u
1985 $text = "";
1986 $self->{cursor} = 0;
1872 } elsif ($uni == 27) { 1987 } elsif ($uni == 27) {
1873 $self->emit ('escape'); 1988 $self->emit ('escape');
1874 } elsif ($uni) { 1989 } elsif ($uni) {
1875 substr $text, $self->{cursor}++, 0, chr $uni; 1990 substr $text, $self->{cursor}++, 0, chr $uni;
1876 } else { 1991 } else {
1949 utf8::encode $text; 2064 utf8::encode $text;
1950 2065
1951 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2066 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1952 } 2067 }
1953 2068
1954 glColor @{$self->{fg}};
1955 glBegin GL_LINES; 2069 glBegin GL_LINES;
1956 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2070 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}; 2071 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1958 glEnd; 2072 glEnd;
1959 } 2073 }
1960} 2074}
1961 2075
1962package CFPlus::UI::Entry; 2076package CFPlus::UI::Entry;
2752 2866
2753 # todo: base offset on lines or so, not on pixels 2867 # todo: base offset on lines or so, not on pixels
2754 $self->{children}[1]->set_value ($offset); 2868 $self->{children}[1]->set_value ($offset);
2755} 2869}
2756 2870
2871sub current_paragraph {
2872 my ($self) = @_;
2873
2874 $self->{top_paragraph} - 1
2875}
2876
2877sub scroll_to {
2878 my ($self, $para) = @_;
2879
2880 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para;
2881
2882 $self->{scroll_to} = $para;
2883 $self->update;
2884}
2885
2757sub clear { 2886sub clear {
2758 my ($self) = @_; 2887 my ($self) = @_;
2759 2888
2760 my (undef, undef, @other) = @{ $self->{children} }; 2889 my (undef, undef, @other) = @{ $self->{children} };
2761 $self->remove ($_) for @other; 2890 $self->remove ($_) for @other;
2788} 2917}
2789 2918
2790sub scroll_to_bottom { 2919sub scroll_to_bottom {
2791 my ($self) = @_; 2920 my ($self) = @_;
2792 2921
2793 $self->{scroll_to_bottom} = 1; 2922 $self->{scroll_to} = $#{$self->{par}};
2794 $self->update; 2923 $self->update;
2795} 2924}
2796 2925
2926sub force_uptodate {
2927 my ($self) = @_;
2928
2929 if (delete $self->{need_reflow}) {
2930 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2931
2932 my $height = 0;
2933
2934 for my $para (@{$self->{par}}) {
2935 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2936 my $layout = $self->get_layout ($para);
2937 my ($w, $h) = $layout->size;
2938
2939 $para->{w} = $w + $para->{indent};
2940 $para->{h} = $h;
2941 $para->{wrapped} = $layout->has_wrapped;
2942 }
2943
2944 $para->{y} = $height;
2945 $height += $para->{h};
2946 }
2947
2948 $self->{height} = $height;
2949 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2950
2951 delete $self->{texture};
2952 }
2953
2954 if (my $paridx = delete $self->{scroll_to}) {
2955 $self->{children}[1]->set_value ($self->{par}[$paridx]{y});
2956 }
2957}
2958
2797sub update { 2959sub update {
2798 my ($self) = @_; 2960 my ($self) = @_;
2799 2961
2800 $self->SUPER::update; 2962 $self->SUPER::update;
2801 2963
2802 return unless $self->{h} > 0; 2964 return unless $self->{h} > 0;
2803 2965
2804 delete $self->{texture}; 2966 delete $self->{texture};
2805 2967
2806 $ROOT->on_post_alloc ($self => sub { 2968 $ROOT->on_post_alloc ($self => sub {
2969 $self->force_uptodate;
2970
2807 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 2971 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 2972
2836 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 2973 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub {
2837 glClearColor 0, 0, 0, 0; 2974 glClearColor 0, 0, 0, 0;
2838 glClear GL_COLOR_BUFFER_BIT; 2975 glClear GL_COLOR_BUFFER_BIT;
2839 2976
2977 package CFPlus::UI::Base;
2978 local ($draw_x, $draw_y, $draw_w, $draw_h) =
2979 (0, 0, $self->{w}, $self->{h});
2980
2981 my $top = int $self->{children}[1]{range}[0];
2982
2983 my $paridx = 0;
2984 my $top_paragraph;
2840 my $top = int $self->{children}[1]{range}[0]; 2985 my $top = int $self->{children}[1]{range}[0];
2841 2986
2842 my $y0 = $top; 2987 my $y0 = $top;
2843 my $y1 = $top + $H; 2988 my $y1 = $top + $H;
2844 2989
2845 my $y = 0;
2846
2847 for my $para (@{$self->{par}}) { 2990 for my $para (@{$self->{par}}) {
2848 my $h = $para->{h}; 2991 my $h = $para->{h};
2992 my $y = $para->{y};
2849 2993
2850 if ($y0 < $y + $h && $y < $y1) { 2994 if ($y0 < $y + $h && $y < $y1) {
2851
2852 my $layout = $self->get_layout ($para); 2995 my $layout = $self->get_layout ($para);
2853 2996
2854 $layout->render ($para->{indent}, $y - $y0); 2997 $layout->render ($para->{indent}, $y - $y0);
2855 2998
2856 if (my @w = @{ $para->{widget} }) { 2999 if (my @w = @{ $para->{widget} }) {
2865 $_->draw; 3008 $_->draw;
2866 } 3009 }
2867 } 3010 }
2868 } 3011 }
2869 3012
2870 $y += $h; 3013 $paridx++;
3014 $top_paragraph ||= $paridx if $y >= $top;
2871 } 3015 }
3016
3017 $self->{top_paragraph} = $top_paragraph;
2872 }; 3018 };
2873 }); 3019 });
2874} 3020}
2875 3021
2876sub reconfigure { 3022sub reconfigure {
2997 3143
2998 $self->add (new CFPlus::UI::Label 3144 $self->add (new CFPlus::UI::Label
2999 markup => $tooltip, 3145 markup => $tooltip,
3000 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3146 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3001 fontsize => 0.8, 3147 fontsize => 0.8,
3002 fg => [0, 0, 0, 1], 3148 style => 1, # FLAG_INVERSE
3003 ellipsise => 0, 3149 ellipsise => 0,
3004 font => ($widget->{tooltip_font} || $::FONT_PROP), 3150 font => ($widget->{tooltip_font} || $::FONT_PROP),
3005 ); 3151 );
3006} 3152}
3007 3153
3155 3301
3156package CFPlus::UI::Buttonbar; 3302package CFPlus::UI::Buttonbar;
3157 3303
3158our @ISA = CFPlus::UI::HBox::; 3304our @ISA = CFPlus::UI::HBox::;
3159 3305
3160# TODO: should actualyl wrap buttons and other goodies. 3306# TODO: should actually wrap buttons and other goodies.
3161 3307
3162############################################################################# 3308#############################################################################
3163 3309
3164package CFPlus::UI::Menu; 3310package CFPlus::UI::Menu;
3165 3311
3166our @ISA = CFPlus::UI::FancyFrame::; 3312our @ISA = CFPlus::UI::Toplevel::;
3167 3313
3168use CFPlus::OpenGL; 3314use CFPlus::OpenGL;
3169 3315
3170sub new { 3316sub new {
3171 my $class = shift; 3317 my $class = shift;
3707 } 3853 }
3708 3854
3709 1 3855 1
3710 }; 3856 };
3711 3857
3712 my $tooltip = "$spell->{message}$TOOLTIP_ALL"; 3858 my $tooltip = (CFPlus::asxml $spell->{message}) . $TOOLTIP_ALL;
3713 3859
3714 #TODO: add path info to tooltip 3860 #TODO: add path info to tooltip
3715 #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path}); 3861 #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path});
3716 3862
3717 $self->add (0, $row, new CFPlus::UI::Face 3863 $self->add (0, $row, new CFPlus::UI::Face
3995 glLoadIdentity; 4141 glLoadIdentity;
3996 4142
3997 { 4143 {
3998 package CFPlus::UI::Base; 4144 package CFPlus::UI::Base;
3999 4145
4000 ($draw_x, $draw_y, $draw_w, $draw_h) = 4146 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4001 (0, 0, $self->{w}, $self->{h}); 4147 (0, 0, $self->{w}, $self->{h});
4002 }
4003 4148
4004 $self->_draw; 4149 $self->_draw;
4150 }
4005} 4151}
4006 4152
4007############################################################################# 4153#############################################################################
4008 4154
4009package CFPlus::UI; 4155package CFPlus::UI;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines