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.346 by root, Mon Aug 14 01:21:02 2006 UTC vs.
Revision 1.358 by root, Fri Dec 8 14:59:54 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) = @_;
1138 my ($class, %arg) = @_; 1138 my ($class, %arg) = @_;
1139 1139
1140 if ((exists $arg{label}) && !ref $arg{label}) { 1140 if ((exists $arg{label}) && !ref $arg{label}) {
1141 $arg{label} = new CFPlus::UI::Label 1141 $arg{label} = new CFPlus::UI::Label
1142 align => 1, 1142 align => 1,
1143 valign => 1, 1143 valign => 0,
1144 text => $arg{label}, 1144 text => $arg{label},
1145 fontsize => ($arg{border} || 0.8) * 0.75; 1145 fontsize => ($arg{border} || 0.8) * 0.75;
1146 } 1146 }
1147 1147
1148 my $self = $class->SUPER::new ( 1148 my $self = $class->SUPER::new (
1900 active_fg => [0, 0, 0], 1900 active_fg => [0, 0, 0],
1901 can_hover => 1, 1901 can_hover => 1,
1902 can_focus => 1, 1902 can_focus => 1,
1903 valign => 0, 1903 valign => 0,
1904 can_events => 1, 1904 can_events => 1,
1905 ellipsise => 0,
1905 #text => ... 1906 #text => ...
1906 #hidden => "*", 1907 #hidden => "*",
1907 @_ 1908 @_
1908 ) 1909 )
1909} 1910}
1966 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1967 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1967 } elsif ($sym == CFPlus::SDLK_HOME) { 1968 } elsif ($sym == CFPlus::SDLK_HOME) {
1968 $self->{cursor} = 0; 1969 $self->{cursor} = 0;
1969 } elsif ($sym == CFPlus::SDLK_END) { 1970 } elsif ($sym == CFPlus::SDLK_END) {
1970 $self->{cursor} = length $text; 1971 $self->{cursor} = length $text;
1972 } elsif ($uni == 21) { # ctrl-u
1973 $text = "";
1974 $self->{cursor} = 0;
1971 } elsif ($uni == 27) { 1975 } elsif ($uni == 27) {
1972 $self->emit ('escape'); 1976 $self->emit ('escape');
1973 } elsif ($uni) { 1977 } elsif ($uni >= 0x20) {
1974 substr $text, $self->{cursor}++, 0, chr $uni; 1978 substr $text, $self->{cursor}++, 0, chr $uni;
1975 } else { 1979 } else {
1976 return 0; 1980 return 0;
1977 } 1981 }
1978 1982
1979 $self->_set_text ($text); 1983 $self->_set_text ($text);
1980 1984
1981 $self->realloc; 1985 $self->realloc;
1986 $self->update;
1982 1987
1983 1 1988 1
1984} 1989}
1985 1990
1986sub invoke_focus_in { 1991sub invoke_focus_in {
2054 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2059 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
2055 glVertex 0.5 + $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};
2056 glEnd; 2061 glEnd;
2057 } 2062 }
2058} 2063}
2064
2065#############################################################################
2059 2066
2060package CFPlus::UI::Entry; 2067package CFPlus::UI::Entry;
2061 2068
2062our @ISA = CFPlus::UI::EntryBase::; 2069our @ISA = CFPlus::UI::EntryBase::;
2063 2070
2097 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2104 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2098 } else { 2105 } else {
2099 $self->set_text ($self->{history_saveback}); 2106 $self->set_text ($self->{history_saveback});
2100 } 2107 }
2101 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 do {
2130 if (defined (my $index = $self->{layout}->line_x_to_index ($y + $dy, $x))) {
2131 $self->{cursor} = $index;
2132 delete $self->{cur_h};
2133 $self->update;
2134 return;
2135 }
2136 } while --$x >= 0;
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);
2102 } else { 2148 } else {
2103 return $self->SUPER::invoke_key_down ($ev) 2149 return $self->SUPER::invoke_key_down ($ev)
2104 } 2150 }
2105 2151
2106 1 2152 1
2853} 2899}
2854 2900
2855sub current_paragraph { 2901sub current_paragraph {
2856 my ($self) = @_; 2902 my ($self) = @_;
2857 2903
2858 $self->force_uptodate;
2859 $self->{top_paragraph} - 1 2904 $self->{top_paragraph} - 1
2860} 2905}
2861 2906
2862sub scroll_to { 2907sub scroll_to {
2863 my ($self, $para) = @_; 2908 my ($self, $para) = @_;
2864 2909
2865 $self->force_uptodate;
2866
2867 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para; 2910 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para;
2868 2911
2869 $self->{children}[1]->set_value ($self->{par}[$para]{y}); 2912 $self->{scroll_to} = $para;
2913 $self->update;
2870} 2914}
2871 2915
2872sub clear { 2916sub clear {
2873 my ($self) = @_; 2917 my ($self) = @_;
2874 2918
2903} 2947}
2904 2948
2905sub scroll_to_bottom { 2949sub scroll_to_bottom {
2906 my ($self) = @_; 2950 my ($self) = @_;
2907 2951
2908 $self->{scroll_to_bottom} = 1; 2952 $self->{scroll_to} = $#{$self->{par}};
2909 $self->update; 2953 $self->update;
2910} 2954}
2911 2955
2912sub force_uptodate { 2956sub force_uptodate {
2913 my ($self) = @_; 2957 my ($self) = @_;
2914 2958
2915 if (delete $self->{need_reflow}) { 2959 if (delete $self->{need_reflow}) {
2916 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 2960 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2917 2961
2918 my $height = 0; 2962 my $height = 0;
2919 my $paridx;
2920 my $top_paragraph;
2921 my $top = int $self->{children}[1]{range}[0];
2922 2963
2923 for my $para (@{$self->{par}}) { 2964 for my $para (@{$self->{par}}) {
2924 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) { 2965 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2925 my $layout = $self->get_layout ($para); 2966 my $layout = $self->get_layout ($para);
2926 my ($w, $h) = $layout->size; 2967 my ($w, $h) = $layout->size;
2929 $para->{h} = $h; 2970 $para->{h} = $h;
2930 $para->{wrapped} = $layout->has_wrapped; 2971 $para->{wrapped} = $layout->has_wrapped;
2931 } 2972 }
2932 2973
2933 $para->{y} = $height; 2974 $para->{y} = $height;
2934
2935 $paridx++;
2936 $top_paragraph ||= $paridx if $height >= $top;
2937
2938 $height += $para->{h}; 2975 $height += $para->{h};
2939 } 2976 }
2940 2977
2941 $self->{top_paragraph} = $top_paragraph;
2942 $self->{height} = $height; 2978 $self->{height} = $height;
2943
2944 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]); 2979 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2945 2980
2946 delete $self->{texture}; 2981 delete $self->{texture};
2947 } 2982 }
2948 2983
2949 if (delete $self->{scroll_to_bottom}) { 2984 if (my $paridx = delete $self->{scroll_to}) {
2950 $self->{children}[1]->set_value (1e10); 2985 $self->{children}[1]->set_value ($self->{par}[$paridx]{y});
2951 } 2986 }
2952} 2987}
2953 2988
2954sub update { 2989sub update {
2955 my ($self) = @_; 2990 my ($self) = @_;
2967 3002
2968 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3003 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub {
2969 glClearColor 0, 0, 0, 0; 3004 glClearColor 0, 0, 0, 0;
2970 glClear GL_COLOR_BUFFER_BIT; 3005 glClear GL_COLOR_BUFFER_BIT;
2971 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;
2972 my $top = int $self->{children}[1]{range}[0]; 3015 my $top = int $self->{children}[1]{range}[0];
2973 3016
2974 my $y0 = $top; 3017 my $y0 = $top;
2975 my $y1 = $top + $H; 3018 my $y1 = $top + $H;
2976 3019
2994 3037
2995 $_->draw; 3038 $_->draw;
2996 } 3039 }
2997 } 3040 }
2998 } 3041 }
3042
3043 $paridx++;
3044 $top_paragraph ||= $paridx if $y >= $top;
2999 } 3045 }
3046
3047 $self->{top_paragraph} = $top_paragraph;
3000 }; 3048 };
3001 }); 3049 });
3002} 3050}
3003 3051
3004sub reconfigure { 3052sub reconfigure {
3283 3331
3284package CFPlus::UI::Buttonbar; 3332package CFPlus::UI::Buttonbar;
3285 3333
3286our @ISA = CFPlus::UI::HBox::; 3334our @ISA = CFPlus::UI::HBox::;
3287 3335
3288# TODO: should actualyl wrap buttons and other goodies. 3336# TODO: should actually wrap buttons and other goodies.
3289 3337
3290############################################################################# 3338#############################################################################
3291 3339
3292package CFPlus::UI::Menu; 3340package CFPlus::UI::Menu;
3293 3341
3822 if ($ev->{button} == 1) { 3870 if ($ev->{button} == 1) {
3823 $::CONN->user_send ("cast $spell->{name}"); 3871 $::CONN->user_send ("cast $spell->{name}");
3824 } elsif ($ev->{button} == 2) { 3872 } elsif ($ev->{button} == 2) {
3825 $::CONN->user_send ("invoke $spell->{name}"); 3873 $::CONN->user_send ("invoke $spell->{name}");
3826 } elsif ($ev->{button} == 3) { 3874 } elsif ($ev->{button} == 3) {
3875 my $shortname = CFPlus::shorten $spell->{name}, 14;
3827 (new CFPlus::UI::Menu 3876 (new CFPlus::UI::Menu
3828 items => [ 3877 items => [
3829 ["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}"]) }],
3830 ["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}"]) }],
3831 ], 3880 ],
3832 )->popup ($ev); 3881 )->popup ($ev);
3833 } else { 3882 } else {
3834 return 0; 3883 return 0;
3835 } 3884 }
3836 3885
3837 1 3886 1
3838 }; 3887 };
3839 3888
3840 my $tooltip = "$spell->{message}$TOOLTIP_ALL"; 3889 my $tooltip = (CFPlus::asxml $spell->{message}) . $TOOLTIP_ALL;
3841 3890
3842 #TODO: add path info to tooltip 3891 #TODO: add path info to tooltip
3843 #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path}); 3892 #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path});
3844 3893
3845 $self->add (0, $row, new CFPlus::UI::Face 3894 $self->add (0, $row, new CFPlus::UI::Face
4123 glLoadIdentity; 4172 glLoadIdentity;
4124 4173
4125 { 4174 {
4126 package CFPlus::UI::Base; 4175 package CFPlus::UI::Base;
4127 4176
4128 ($draw_x, $draw_y, $draw_w, $draw_h) = 4177 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4129 (0, 0, $self->{w}, $self->{h}); 4178 (0, 0, $self->{w}, $self->{h});
4130 }
4131 4179
4132 $self->_draw; 4180 $self->_draw;
4181 }
4133} 4182}
4134 4183
4135############################################################################# 4184#############################################################################
4136 4185
4137package CFPlus::UI; 4186package CFPlus::UI;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines