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.343 by root, Sun Aug 13 15:14:17 2006 UTC vs.
Revision 1.360 by root, Fri Dec 8 22:25:32 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
898 glClear GL_COLOR_BUFFER_BIT; 898 glClear GL_COLOR_BUFFER_BIT;
899 899
900 { 900 {
901 package CFPlus::UI::Base; 901 package CFPlus::UI::Base;
902 902
903 ($draw_x, $draw_y, $draw_w, $draw_h) = 903 local ($draw_x, $draw_y, $draw_w, $draw_h) =
904 (0, 0, $self->{w}, $self->{h}); 904 (0, 0, $self->{w}, $self->{h});
905
906 $self->_render;
905 } 907 }
906
907 $self->_render;
908 }; 908 };
909} 909}
910 910
911sub _draw { 911sub _draw {
912 my ($self) = @_; 912 my ($self) = @_;
913
914 my ($w, $h) = @$self{qw(w h)};
915 913
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
1802 active_fg => [0, 0, 0], 1900 active_fg => [0, 0, 0],
1803 can_hover => 1, 1901 can_hover => 1,
1804 can_focus => 1, 1902 can_focus => 1,
1805 valign => 0, 1903 valign => 0,
1806 can_events => 1, 1904 can_events => 1,
1905 ellipsise => 0,
1807 #text => ... 1906 #text => ...
1808 #hidden => "*", 1907 #hidden => "*",
1809 @_ 1908 @_
1810 ) 1909 )
1811} 1910}
1868 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1967 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1869 } elsif ($sym == CFPlus::SDLK_HOME) { 1968 } elsif ($sym == CFPlus::SDLK_HOME) {
1870 $self->{cursor} = 0; 1969 $self->{cursor} = 0;
1871 } elsif ($sym == CFPlus::SDLK_END) { 1970 } elsif ($sym == CFPlus::SDLK_END) {
1872 $self->{cursor} = length $text; 1971 $self->{cursor} = length $text;
1972 } elsif ($uni == 21) { # ctrl-u
1973 $text = "";
1974 $self->{cursor} = 0;
1873 } elsif ($uni == 27) { 1975 } elsif ($uni == 27) {
1874 $self->emit ('escape'); 1976 $self->emit ('escape');
1875 } elsif ($uni) { 1977 } elsif ($uni >= 0x20 || $uni == 0x0d) {
1876 substr $text, $self->{cursor}++, 0, chr $uni; 1978 substr $text, $self->{cursor}++, 0, chr $uni;
1877 } else { 1979 } else {
1878 return 0; 1980 return 0;
1879 } 1981 }
1880 1982
1881 $self->_set_text ($text); 1983 $self->_set_text ($text);
1882 1984
1883 $self->realloc; 1985 $self->realloc;
1986 $self->update;
1884 1987
1885 1 1988 1
1886} 1989}
1887 1990
1888sub invoke_focus_in { 1991sub invoke_focus_in {
1950 utf8::encode $text; 2053 utf8::encode $text;
1951 2054
1952 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2055 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1953 } 2056 }
1954 2057
1955 glColor @{$self->{fg}};
1956 glBegin GL_LINES; 2058 glBegin GL_LINES;
1957 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2059 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}; 2060 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1959 glEnd; 2061 glEnd;
1960 } 2062 }
1961} 2063}
2064
2065#############################################################################
1962 2066
1963package CFPlus::UI::Entry; 2067package CFPlus::UI::Entry;
1964 2068
1965our @ISA = CFPlus::UI::EntryBase::; 2069our @ISA = CFPlus::UI::EntryBase::;
1966 2070
1969sub invoke_key_down { 2073sub invoke_key_down {
1970 my ($self, $ev) = @_; 2074 my ($self, $ev) = @_;
1971 2075
1972 my $sym = $ev->{sym}; 2076 my $sym = $ev->{sym};
1973 2077
1974 if ($sym == 13) { 2078 if ($ev->{uni} == 0x0d || $sym == 13) {
1975 unshift @{$self->{history}}, 2079 unshift @{$self->{history}},
1976 my $txt = $self->get_text; 2080 my $txt = $self->get_text;
1977 2081
1978 $self->{history_pointer} = -1; 2082 $self->{history_pointer} = -1;
1979 $self->{history_saveback} = ''; 2083 $self->{history_saveback} = '';
2000 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2104 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2001 } else { 2105 } else {
2002 $self->set_text ($self->{history_saveback}); 2106 $self->set_text ($self->{history_saveback});
2003 } 2107 }
2004 2108
2109 } else {
2110 return $self->SUPER::invoke_key_down ($ev)
2111 }
2112
2113 1
2114}
2115
2116#############################################################################
2117
2118package CFPlus::UI::TextEdit;
2119
2120our @ISA = CFPlus::UI::EntryBase::;
2121
2122use CFPlus::OpenGL;
2123
2124sub move_cursor_ver {
2125 my ($self, $dy) = @_;
2126
2127 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2128
2129 $y += $dy;
2130
2131 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) {
2132 $self->{cursor} = $index;
2133 delete $self->{cur_h};
2134 $self->update;
2135 return;
2136 }
2137}
2138
2139sub invoke_key_down {
2140 my ($self, $ev) = @_;
2141
2142 my $sym = $ev->{sym};
2143
2144 if ($sym == CFPlus::SDLK_UP) {
2145 $self->move_cursor_ver (-1);
2146 } elsif ($sym == CFPlus::SDLK_DOWN) {
2147 $self->move_cursor_ver (+1);
2005 } else { 2148 } else {
2006 return $self->SUPER::invoke_key_down ($ev) 2149 return $self->SUPER::invoke_key_down ($ev)
2007 } 2150 }
2008 2151
2009 1 2152 1
2753 2896
2754 # todo: base offset on lines or so, not on pixels 2897 # todo: base offset on lines or so, not on pixels
2755 $self->{children}[1]->set_value ($offset); 2898 $self->{children}[1]->set_value ($offset);
2756} 2899}
2757 2900
2901sub current_paragraph {
2902 my ($self) = @_;
2903
2904 $self->{top_paragraph} - 1
2905}
2906
2907sub scroll_to {
2908 my ($self, $para) = @_;
2909
2910 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para;
2911
2912 $self->{scroll_to} = $para;
2913 $self->update;
2914}
2915
2758sub clear { 2916sub clear {
2759 my ($self) = @_; 2917 my ($self) = @_;
2760 2918
2761 my (undef, undef, @other) = @{ $self->{children} }; 2919 my (undef, undef, @other) = @{ $self->{children} };
2762 $self->remove ($_) for @other; 2920 $self->remove ($_) for @other;
2789} 2947}
2790 2948
2791sub scroll_to_bottom { 2949sub scroll_to_bottom {
2792 my ($self) = @_; 2950 my ($self) = @_;
2793 2951
2794 $self->{scroll_to_bottom} = 1; 2952 $self->{scroll_to} = $#{$self->{par}};
2795 $self->update; 2953 $self->update;
2796} 2954}
2797 2955
2956sub force_uptodate {
2957 my ($self) = @_;
2958
2959 if (delete $self->{need_reflow}) {
2960 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2961
2962 my $height = 0;
2963
2964 for my $para (@{$self->{par}}) {
2965 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2966 my $layout = $self->get_layout ($para);
2967 my ($w, $h) = $layout->size;
2968
2969 $para->{w} = $w + $para->{indent};
2970 $para->{h} = $h;
2971 $para->{wrapped} = $layout->has_wrapped;
2972 }
2973
2974 $para->{y} = $height;
2975 $height += $para->{h};
2976 }
2977
2978 $self->{height} = $height;
2979 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2980
2981 delete $self->{texture};
2982 }
2983
2984 if (my $paridx = delete $self->{scroll_to}) {
2985 $self->{children}[1]->set_value ($self->{par}[$paridx]{y});
2986 }
2987}
2988
2798sub update { 2989sub update {
2799 my ($self) = @_; 2990 my ($self) = @_;
2800 2991
2801 $self->SUPER::update; 2992 $self->SUPER::update;
2802 2993
2803 return unless $self->{h} > 0; 2994 return unless $self->{h} > 0;
2804 2995
2805 delete $self->{texture}; 2996 delete $self->{texture};
2806 2997
2807 $ROOT->on_post_alloc ($self => sub { 2998 $ROOT->on_post_alloc ($self => sub {
2999 $self->force_uptodate;
3000
2808 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3001 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 3002
2837 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3003 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub {
2838 glClearColor 0, 0, 0, 0; 3004 glClearColor 0, 0, 0, 0;
2839 glClear GL_COLOR_BUFFER_BIT; 3005 glClear GL_COLOR_BUFFER_BIT;
2840 3006
3007 package CFPlus::UI::Base;
3008 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3009 (0, 0, $self->{w}, $self->{h});
3010
3011 my $top = int $self->{children}[1]{range}[0];
3012
3013 my $paridx = 0;
3014 my $top_paragraph;
2841 my $top = int $self->{children}[1]{range}[0]; 3015 my $top = int $self->{children}[1]{range}[0];
2842 3016
2843 my $y0 = $top; 3017 my $y0 = $top;
2844 my $y1 = $top + $H; 3018 my $y1 = $top + $H;
2845 3019
2846 my $y = 0;
2847
2848 for my $para (@{$self->{par}}) { 3020 for my $para (@{$self->{par}}) {
2849 my $h = $para->{h}; 3021 my $h = $para->{h};
3022 my $y = $para->{y};
2850 3023
2851 if ($y0 < $y + $h && $y < $y1) { 3024 if ($y0 < $y + $h && $y < $y1) {
2852
2853 my $layout = $self->get_layout ($para); 3025 my $layout = $self->get_layout ($para);
2854 3026
2855 $layout->render ($para->{indent}, $y - $y0); 3027 $layout->render ($para->{indent}, $y - $y0);
2856 3028
2857 if (my @w = @{ $para->{widget} }) { 3029 if (my @w = @{ $para->{widget} }) {
2866 $_->draw; 3038 $_->draw;
2867 } 3039 }
2868 } 3040 }
2869 } 3041 }
2870 3042
2871 $y += $h; 3043 $paridx++;
3044 $top_paragraph ||= $paridx if $y >= $top;
2872 } 3045 }
3046
3047 $self->{top_paragraph} = $top_paragraph;
2873 }; 3048 };
2874 }); 3049 });
2875} 3050}
2876 3051
2877sub reconfigure { 3052sub reconfigure {
3156 3331
3157package CFPlus::UI::Buttonbar; 3332package CFPlus::UI::Buttonbar;
3158 3333
3159our @ISA = CFPlus::UI::HBox::; 3334our @ISA = CFPlus::UI::HBox::;
3160 3335
3161# TODO: should actualyl wrap buttons and other goodies. 3336# TODO: should actually wrap buttons and other goodies.
3162 3337
3163############################################################################# 3338#############################################################################
3164 3339
3165package CFPlus::UI::Menu; 3340package CFPlus::UI::Menu;
3166 3341
3167our @ISA = CFPlus::UI::FancyFrame::; 3342our @ISA = CFPlus::UI::Toplevel::;
3168 3343
3169use CFPlus::OpenGL; 3344use CFPlus::OpenGL;
3170 3345
3171sub new { 3346sub new {
3172 my $class = shift; 3347 my $class = shift;
3695 if ($ev->{button} == 1) { 3870 if ($ev->{button} == 1) {
3696 $::CONN->user_send ("cast $spell->{name}"); 3871 $::CONN->user_send ("cast $spell->{name}");
3697 } elsif ($ev->{button} == 2) { 3872 } elsif ($ev->{button} == 2) {
3698 $::CONN->user_send ("invoke $spell->{name}"); 3873 $::CONN->user_send ("invoke $spell->{name}");
3699 } elsif ($ev->{button} == 3) { 3874 } elsif ($ev->{button} == 3) {
3875 my $shortname = CFPlus::shorten $spell->{name}, 14;
3700 (new CFPlus::UI::Menu 3876 (new CFPlus::UI::Menu
3701 items => [ 3877 items => [
3702 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }], 3878 ["bind <i>cast $shortname</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }],
3703 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }], 3879 ["bind <i>invoke $shortname</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }],
3704 ], 3880 ],
3705 )->popup ($ev); 3881 )->popup ($ev);
3706 } else { 3882 } else {
3707 return 0; 3883 return 0;
3708 } 3884 }
3709 3885
3710 1 3886 1
3711 }; 3887 };
3712 3888
3713 my $tooltip = "$spell->{message}$TOOLTIP_ALL"; 3889 my $tooltip = (CFPlus::asxml $spell->{message}) . $TOOLTIP_ALL;
3714 3890
3715 #TODO: add path info to tooltip 3891 #TODO: add path info to tooltip
3716 #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path}); 3892 #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path});
3717 3893
3718 $self->add (0, $row, new CFPlus::UI::Face 3894 $self->add (0, $row, new CFPlus::UI::Face
3996 glLoadIdentity; 4172 glLoadIdentity;
3997 4173
3998 { 4174 {
3999 package CFPlus::UI::Base; 4175 package CFPlus::UI::Base;
4000 4176
4001 ($draw_x, $draw_y, $draw_w, $draw_h) = 4177 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4002 (0, 0, $self->{w}, $self->{h}); 4178 (0, 0, $self->{w}, $self->{h});
4003 }
4004 4179
4005 $self->_draw; 4180 $self->_draw;
4181 }
4006} 4182}
4007 4183
4008############################################################################# 4184#############################################################################
4009 4185
4010package CFPlus::UI; 4186package CFPlus::UI;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines