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.227 by root, Wed May 24 03:29:20 2006 UTC vs.
Revision 1.230 by root, Wed May 24 21:59:54 2006 UTC

144 144
145 $_->update 145 $_->update
146 for @widgets; 146 for @widgets;
147} 147}
148 148
149sub reconfigure_widgets {
150 # make a copy, otherwise C<for> complains about freed values.
151 my @widgets = values %WIDGET;
152
153 $_->reconfigure
154 for @widgets;
155}
156
149# call when resolution changes etc. 157# call when resolution changes etc.
150sub rescale_widgets { 158sub rescale_widgets {
151 my ($sx, $sy) = @_; 159 my ($sx, $sy) = @_;
152 160
153 # make a copy, otherwise for complains about freed values.
154 my @widgets = values %WIDGET; 161 for my $widget (values %WIDGET) {
155
156 for my $widget (@widgets) {
157 if ($widget->{toplevel}) { 162 if ($widget->{is_toplevel}) {
158 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 163 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
159 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 164 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
160 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; 165 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
161 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w}; 166 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
162 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; 167 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
163 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 168 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
164 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; 169 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
165 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h}; 170 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
166 } 171 }
167
168 $widget->reconfigure;
169 } 172 }
173
174 reconfigure_widgets;
170} 175}
171 176
172############################################################################# 177#############################################################################
173 178
174package CFClient::UI::Base; 179package CFClient::UI::Base;
816 821
817 my $slider = new CFClient::UI::Slider 822 my $slider = new CFClient::UI::Slider
818 vertical => 1, 823 vertical => 1,
819 range => [0, 0, 1, 0.01], # HACK fix 824 range => [0, 0, 1, 0.01], # HACK fix
820 connect_changed => sub { 825 connect_changed => sub {
821 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); 826 $self->{vp}->set_offset (0, $_[1]);
822 }, 827 },
823 ; 828 ;
824 829
825 $self = $class->SUPER::new ( 830 $self = $class->SUPER::new (
826 vp => (new CFClient::UI::ViewPort expand => 1), 831 vp => (new CFClient::UI::ViewPort expand => 1),
831 $self->{vp}->add ($self->{scrolled}); 836 $self->{vp}->add ($self->{scrolled});
832 $self->add ($self->{vp}); 837 $self->add ($self->{vp});
833 $self->add ($self->{slider}); 838 $self->add ($self->{slider});
834 839
835 $self 840 $self
841}
842
843sub size_allocate {
844 my ($self, $w, $h) = @_;
845
846 $self->SUPER::size_allocate ($w, $h);
847
848 my $child = $self->{vp}->child;
849 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
836} 850}
837 851
838#TODO# update range on size_allocate depending on child 852#TODO# update range on size_allocate depending on child
839# update viewport offset on scroll 853# update viewport offset on scroll
840 854
894 my $class = shift; 908 my $class = shift;
895 909
896 # TODO: user_x, user_y, overwrite moveto? 910 # TODO: user_x, user_y, overwrite moveto?
897 911
898 my $self = $class->SUPER::new ( 912 my $self = $class->SUPER::new (
899 bg => [1, 1, 1, 1], 913 bg => [1, 1, 1, 1],
900 border_bg => [1, 1, 1, 1], 914 border_bg => [1, 1, 1, 1],
901 border => 0.6, 915 border => 0.6,
902 toplevel => 1, 916 is_toplevel => 1,
903 can_events => 1, 917 can_events => 1,
904 @_ 918 @_
905 ); 919 );
906 920
907 $self->{title} &&= new CFClient::UI::Label 921 $self->{title} &&= new CFClient::UI::Label
908 align => 0, 922 align => 0,
2293 $self->{layout}->set_width ($self->{children}[0]{w}); 2307 $self->{layout}->set_width ($self->{children}[0]{w});
2294 2308
2295 $self->reflow; 2309 $self->reflow;
2296} 2310}
2297 2311
2298sub text_height { 2312sub text_size {
2299 my ($self, $text, $indent) = @_; 2313 my ($self, $text, $indent) = @_;
2300 2314
2301 my $layout = $self->{layout}; 2315 my $layout = $self->{layout};
2302 2316
2303 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2317 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2304 $layout->set_width ($self->{children}[0]{w} - $indent); 2318 $layout->set_width ($self->{children}[0]{w} - $indent);
2305 $layout->set_markup ($text); 2319 $layout->set_markup ($text);
2306 2320
2307 ($layout->size)[1] 2321 $layout->size
2308} 2322}
2309 2323
2310sub reflow { 2324sub reflow {
2311 my ($self) = @_; 2325 my ($self) = @_;
2312 2326
2331 2345
2332sub add_paragraph { 2346sub add_paragraph {
2333 my ($self, $color, $text, $indent) = @_; 2347 my ($self, $color, $text, $indent) = @_;
2334 2348
2335 for my $line (split /\n/, $text) { 2349 for my $line (split /\n/, $text) {
2336 my $height = $self->text_height ($line); 2350 my ($w, $h) = $self->text_size ($line);
2337 $self->{height} += $height; 2351 $self->{height} += $h;
2338 push @{$self->{par}}, [$height, $color, $indent, $line]; 2352 push @{$self->{par}}, [$w + $indent, $h, $color, $indent, $line];
2339 } 2353 }
2340 2354
2341 $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]); 2355 $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]);
2342} 2356}
2343 2357
2349 return unless $self->{h} > 0; 2363 return unless $self->{h} > 0;
2350 2364
2351 delete $self->{texture}; 2365 delete $self->{texture};
2352 2366
2353 $ROOT->on_post_alloc ($self, sub { 2367 $ROOT->on_post_alloc ($self, sub {
2368 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2369
2354 if (delete $self->{need_reflow}) { 2370 if (delete $self->{need_reflow}) {
2355 my $height = 0; 2371 my $height = 0;
2356 2372
2357 $height += $_->[0] = $self->text_height ($_->[3], $_->[2]) 2373 my $layout = $self->{layout};
2374
2375 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2376
2358 for @{$self->{par}}; 2377 for (@{$self->{par}}) {
2378 if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support
2379 $layout->set_width ($W - $_->[3]);
2380 $layout->set_markup ($_->[4]);
2381 my ($w, $h) = $layout->size;
2382 $_->[0] = $w + $_->[3];
2383 $_->[1] = $h;
2384 }
2385
2386 $height += $_->[1];
2387 }
2359 2388
2360 $self->{height} = $height; 2389 $self->{height} = $height;
2361 2390
2362 $self->{children}[1]->set_range ([$height, 0, $height, $self->{h}, 1]); 2391 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]);
2363 2392
2364 delete $self->{texture}; 2393 delete $self->{texture};
2365 } 2394 }
2366 2395
2367 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2396 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub {
2368 glClearColor 0.5, 0.5, 0.5, 0; 2397 glClearColor 0.5, 0.5, 0.5, 0;
2369 glClear GL_COLOR_BUFFER_BIT; 2398 glClear GL_COLOR_BUFFER_BIT;
2370 2399
2371 my $top = int $self->{children}[1]{range}[0]; 2400 my $top = int $self->{children}[1]{range}[0];
2372 2401
2373 my $y0 = $top; 2402 my $y0 = $top;
2374 my $y1 = $top + $self->{h}; 2403 my $y1 = $top + $H;
2375 2404
2376 my $y = 0; 2405 my $y = 0;
2377 2406
2378 my $layout = $self->{layout}; 2407 my $layout = $self->{layout};
2379 2408
2380 $layout->set_font ($self->{font}) if $self->{font}; 2409 $layout->set_font ($self->{font}) if $self->{font};
2381 2410
2382 glEnable GL_BLEND; 2411 glEnable GL_BLEND;
2412 #TODO# not correct in windows where rgba is forced off
2383 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2413 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2384 2414
2385 for my $par (@{$self->{par}}) { 2415 for my $par (@{$self->{par}}) {
2386 my $h = $par->[0]; 2416 my $h = $par->[1];
2387 2417
2388 if ($y0 < $y + $h && $y < $y1) { 2418 if ($y0 < $y + $h && $y < $y1) {
2389 $layout->set_foreground (@{ $par->[1] }); 2419 $layout->set_foreground (@{ $par->[2] });
2390 $layout->set_width ($self->{w} - $par->[2]); 2420 $layout->set_width ($W - $par->[3]);
2391 $layout->set_markup ($par->[3]); 2421 $layout->set_markup ($par->[4]);
2392 2422
2393 my ($w, $h, $data, $format, $internalformat) = $layout->render; 2423 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2394 2424
2395 glRasterPos $par->[2], $y - $y0; 2425 glRasterPos $par->[3], $y - $y0;
2396 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data; 2426 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2397 } 2427 }
2398 2428
2399 $y += $h; 2429 $y += $h;
2400 } 2430 }
3028} 3058}
3029 3059
3030sub size_allocate { 3060sub size_allocate {
3031 my ($self, $w, $h) = @_; 3061 my ($self, $w, $h) = @_;
3032 3062
3033 my $old_w = $self->{old_w}; $self->{old_w} = $w;
3034 my $old_h = $self->{old_h}; $self->{old_h} = $h;
3035
3036 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
3037 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
3038
3039 for my $child ($self->children) { 3063 for my $child ($self->children) {
3040 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3064 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3041 3065
3042 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3066 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
3043 if exists $child->{req_x}; 3067 if exists $child->{req_x};
3072} 3096}
3073 3097
3074sub add { 3098sub add {
3075 my ($self, @children) = @_; 3099 my ($self, @children) = @_;
3076 3100
3101 for (my @widgets = @children; my $w = pop @widgets; ) {
3102 push @widgets, $w->children;
3103 $w->{root} = $self;
3104 $w->{visible} = 1;
3105 }
3106
3077 for my $child (@children) { 3107 for my $child (@children) {
3078 $child->{toplevel} = 1; 3108 $child->{is_toplevel} = 1;
3079 3109
3080 # integerise window positions 3110 # integerise window positions
3081 $child->{x} = int $child->{x}; 3111 $child->{x} = int $child->{x};
3082 $child->{y} = int $child->{y}; 3112 $child->{y} = int $child->{y};
3083 } 3113 }
3084 3114
3085 $self->SUPER::add (@children); 3115 $self->SUPER::add (@children);
3086
3087 while (@children) {
3088 my $w = pop @children;
3089 push @children, $w->children;
3090 $w->{visible} = 1;
3091 }
3092} 3116}
3093 3117
3094sub remove { 3118sub remove {
3095 my ($self, @children) = @_; 3119 my ($self, @children) = @_;
3096 3120

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines