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.216 by root, Fri May 19 16:43:57 2006 UTC vs.
Revision 1.223 by elmex, Tue May 23 18:10:51 2006 UTC

134 for (@$vals) { 134 for (@$vals) {
135 my $i = int $_ + $rem; 135 my $i = int $_ + $rem;
136 $rem += $_ - $i; 136 $rem += $_ - $i;
137 $_ = $i; 137 $_ = $i;
138 } 138 }
139}
140
141sub full_refresh {
142 # make a copy, otherwise for complains about freed values.
143 my @widgets = values %WIDGET;
144
145 $_->update
146 for @widgets;
139} 147}
140 148
141# call when resolution changes etc. 149# call when resolution changes etc.
142sub rescale_widgets { 150sub rescale_widgets {
143 my ($sx, $sy) = @_; 151 my ($sx, $sy) = @_;
198 %$self = (); 206 %$self = ();
199} 207}
200 208
201sub show { 209sub show {
202 my ($self) = @_; 210 my ($self) = @_;
203
204 return if $self->{parent}; 211 return if $self->{parent};
205 212
206 $CFClient::UI::ROOT->add ($self); 213 $CFClient::UI::ROOT->add ($self);
214}
215
216sub show_centered {
217 my ($self) = @_;
218 return if $self->{parent};
219
220 $self->show;
221
222 $CFClient::UI::ROOT->on_post_alloc (
223 "centered $self" => sub {
224 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5);
225 },
226 );
207} 227}
208 228
209sub hide { 229sub hide {
210 my ($self) = @_; 230 my ($self) = @_;
211 231
419 Scalar::Util::weaken ($self->{parent} = $parent); 439 Scalar::Util::weaken ($self->{parent} = $parent);
420 440
421 # TODO: req_w _does_change after ->reconfigure 441 # TODO: req_w _does_change after ->reconfigure
422 $self->check_size 442 $self->check_size
423 unless exists $self->{req_w}; 443 unless exists $self->{req_w};
444
445 $self->show;
424} 446}
425 447
426sub check_size { 448sub check_size {
427 my ($self, $forced) = @_; 449 my ($self, $forced) = @_;
428 450
797 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); 819 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
798 }, 820 },
799 ; 821 ;
800 822
801 $self = $class->SUPER::new ( 823 $self = $class->SUPER::new (
802 vp => (new CFClient::UI::ViewPort), 824 vp => (new CFClient::UI::ViewPort expand => 1),
803 slider => $slider, 825 slider => $slider,
804 @_, 826 @_,
805 ); 827 );
806 828
807 $self->{vp}->add ($self->{scrolled}); 829 $self->{vp}->add ($self->{scrolled});
2050 range => [0, 0, 100, 10], 2072 range => [0, 0, 100, 10],
2051 req_w => $::WIDTH / 80, 2073 req_w => $::WIDTH / 80,
2052 req_h => $::WIDTH / 80, 2074 req_h => $::WIDTH / 80,
2053 vertical => 0, 2075 vertical => 0,
2054 can_hover => 1, 2076 can_hover => 1,
2055 inner_pad => .05, 2077 inner_pad => 0.02,
2056 @_ 2078 @_
2057 ); 2079 );
2058 2080
2059 $self->set_value ($self->{range}[0]); 2081 $self->set_value ($self->{range}[0]);
2060 $self->update; 2082 $self->update;
2207 2229
2208 $self->{fontsize} = $fontsize; 2230 $self->{fontsize} = $fontsize;
2209 $self->reflow; 2231 $self->reflow;
2210} 2232}
2211 2233
2234sub size_allocate {
2235 my ($self, $w, $h) = @_;
2236
2237 $self->SUPER::size_allocate ($w, $h);
2238
2239 $self->{layout}->set_font ($self->{font}) if $self->{font};
2240 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2241 $self->{layout}->set_width ($self->{children}[0]{w});
2242
2243 $self->reflow;
2244}
2245
2212sub text_height { 2246sub text_height {
2213 my ($self, $text) = @_; 2247 my ($self, $text, $indent) = @_;
2214 2248
2215 my $layout = $self->{layout}; 2249 my $layout = $self->{layout};
2216 2250
2217 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2251 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2218 $layout->set_width ($self->{children}[0]{w}); 2252 $layout->set_width ($self->{children}[0]{w} - $indent);
2219 $layout->set_markup ($text); 2253 $layout->set_markup ($text);
2220 2254
2221 ($layout->size)[1] 2255 ($layout->size)[1]
2222} 2256}
2223 2257
2226 2260
2227 $self->{need_reflow}++; 2261 $self->{need_reflow}++;
2228 $self->update; 2262 $self->update;
2229} 2263}
2230 2264
2231sub size_allocate {
2232 my ($self, $w, $h) = @_;
2233
2234 $self->SUPER::size_allocate ($w, $h);
2235
2236 $self->{layout}->set_font ($self->{font}) if $self->{font};
2237 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2238 $self->{layout}->set_width ($self->{children}[0]{w});
2239
2240 $self->reflow;
2241}
2242
2243sub add_paragraph { 2265sub add_paragraph {
2244 my ($self, $color, $text) = @_; 2266 my ($self, $color, $text, $indent) = @_;
2245 2267
2246 #TODO: intelligently "reformat" paragraph 2268 #TODO: intelligently "reformat" paragraph
2247 2269
2270 for my $line (split /\n/, $text) {
2248 my $height = $self->text_height ($text); 2271 my $height = $self->text_height ($line);
2249
2250 $self->{height} += $height; 2272 $self->{height} += $height;
2251
2252 push @{$self->{par}}, [$height, $color, $text]; 2273 push @{$self->{par}}, [$height, $color, $indent, $line];
2274 }
2253 2275
2254 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; 2276 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
2255 $self->{children}[1]->update; 2277 $self->{children}[1]->update;
2256} 2278}
2257 2279
2266 2288
2267 $ROOT->on_post_alloc ($self, sub { 2289 $ROOT->on_post_alloc ($self, sub {
2268 if (delete $self->{need_reflow}) { 2290 if (delete $self->{need_reflow}) {
2269 my $height = 0; 2291 my $height = 0;
2270 2292
2271 $height += $_->[0] = $self->text_height ($_->[2]) 2293 $height += $_->[0] = $self->text_height ($_->[3], $_->[2])
2272 for @{$self->{par}}; 2294 for @{$self->{par}};
2273 2295
2274 $self->{height} = $height; 2296 $self->{height} = $height;
2275 2297
2276 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2298 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2299 $self->{children}[1]->update;
2277 2300
2278 delete $self->{texture}; 2301 delete $self->{texture};
2279 } 2302 }
2280 2303
2281 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2304 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2282 glClearColor 0.5, 0.5, 0.5, 0; 2305 glClearColor 0.5, 0.5, 0.5, 0;
2283 glClear GL_COLOR_BUFFER_BIT; 2306 glClear GL_COLOR_BUFFER_BIT;
2284 2307
2285 glEnable GL_TEXTURE_2D;
2286 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2287
2288 my $top = int $self->{children}[1]{range}[0]; 2308 my $top = int $self->{children}[1]{range}[0];
2289 2309
2290 my $y0 = $top; 2310 my $y0 = $top;
2291 my $y1 = $top + $self->{h}; 2311 my $y1 = $top + $self->{h};
2292 2312
2294 2314
2295 my $layout = $self->{layout}; 2315 my $layout = $self->{layout};
2296 2316
2297 $layout->set_font ($self->{font}) if $self->{font}; 2317 $layout->set_font ($self->{font}) if $self->{font};
2298 2318
2319 glEnable GL_BLEND;
2320 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2321
2299 for my $par (@{$self->{par}}) { 2322 for my $par (@{$self->{par}}) {
2300 my $h = $par->[0]; 2323 my $h = $par->[0];
2301 2324
2302 if ($y0 < $y + $h && $y < $y1) { 2325 if ($y0 < $y + $h && $y < $y1) {
2303 $layout->set_foreground (@{ $par->[1] }); 2326 $layout->set_foreground (@{ $par->[1] });
2327 $layout->set_width ($self->{w} - $par->[2]);
2304 $layout->set_markup ($par->[2]); 2328 $layout->set_markup ($par->[3]);
2305 2329
2306 my ($W, $H) = $layout->size; 2330 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2307 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0); 2331
2332 glRasterPos $par->[2], $y - $y0;
2333 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2308 } 2334 }
2309 2335
2310 $y += $h; 2336 $y += $h;
2311 } 2337 }
2312 2338
2313 glDisable GL_TEXTURE_2D; 2339 glDisable GL_BLEND;
2314 }; 2340 };
2315 }); 2341 });
2316} 2342}
2317 2343
2318sub _draw { 2344sub _draw {
2489use CFClient::OpenGL; 2515use CFClient::OpenGL;
2490 2516
2491sub new { 2517sub new {
2492 my $class = shift; 2518 my $class = shift;
2493 2519
2494 $class->SUPER::new ( 2520 my $self = $class->SUPER::new (
2495 aspect => 1, 2521 aspect => 1,
2496 @_, 2522 @_,
2497 ) 2523 );
2524
2525 if ($self->{anim} && $self->{animspeed}) {
2526 Scalar::Util::weaken (my $widget = $self);
2527
2528 $self->{timer} = Event->timer (
2529 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
2530 hard => 1,
2531 interval => $self->{animspeed},
2532 cb => sub {
2533 ++$widget->{frame};
2534 $widget->update;
2535 },
2536 );
2537 }
2538
2539 $self
2498} 2540}
2499 2541
2500sub size_request { 2542sub size_request {
2501 (32, 8) 2543 (32, 8)
2502} 2544}
2503 2545
2546sub update {
2547 my ($self) = @_;
2548
2549 return unless $self->{visible};
2550
2551 $self->SUPER::update;
2552}
2553
2504sub _draw { 2554sub _draw {
2505 my ($self) = @_; 2555 my ($self) = @_;
2506 2556
2507 return unless $::CONN;#d# manage and cache textures differently 2557 return unless $::CONN;#d# manage and cache textures differently
2558
2559 my $face;
2560
2561 if ($self->{frame}) {
2562 my $anim = $::CONN->{anim}[$self->{anim}];
2563
2564 $face = $anim->[ $self->{frame} % @$anim ]
2565 if $anim && @$anim;
2566 }
2567
2508 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2568 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2509 2569
2510 # TODO animation
2511 if ($tex) { 2570 if ($tex) {
2512 glEnable GL_TEXTURE_2D; 2571 glEnable GL_TEXTURE_2D;
2513 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2572 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2514 glColor 1, 1, 1, 1; 2573 glColor 1, 1, 1, 1;
2515 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2574 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2516 glDisable GL_TEXTURE_2D; 2575 glDisable GL_TEXTURE_2D;
2517 } 2576 }
2518} 2577}
2519 2578
2579sub DESTROY {
2580 my ($self) = @_;
2581
2582 $self->{timer}->cancel
2583 if $self->{timer};
2584
2585 $self->SUPER::DESTROY;
2586}
2587
2520############################################################################# 2588#############################################################################
2521 2589
2522package CFClient::UI::InventoryItem; 2590package CFClient::UI::InventoryItem;
2523 2591
2524our @ISA = CFClient::UI::HBox::; 2592our @ISA = CFClient::UI::HBox::;
2527 my ($item) = @_; 2595 my ($item) = @_;
2528 2596
2529 my $desc = 2597 my $desc =
2530 $item->{nrof} < 2 2598 $item->{nrof} < 2
2531 ? $item->{name} 2599 ? $item->{name}
2532 : "$item->{nrof} $item->{name_pl}"; 2600 : "$item->{nrof} × $item->{name_pl}";
2533 2601
2534 $item->{flags} & Crossfire::Protocol::F_OPEN 2602 $item->{flags} & Crossfire::Protocol::F_OPEN
2535 and $desc .= " (open)"; 2603 and $desc .= " (open)";
2536 $item->{flags} & Crossfire::Protocol::F_APPLIED 2604 $item->{flags} & Crossfire::Protocol::F_APPLIED
2537 and $desc .= " (applied)"; 2605 and $desc .= " (applied)";
2557 my $item = delete $args{item}; 2625 my $item = delete $args{item};
2558 2626
2559 my $desc = _item_to_desc ($item); 2627 my $desc = _item_to_desc ($item);
2560 2628
2561 my $self = $class->SUPER::new ( 2629 my $self = $class->SUPER::new (
2562 can_hover => 1, 2630 can_hover => 1,
2563 can_events => 1, 2631 can_events => 1,
2564 tooltip => ((CFClient::UI::Label::escape $desc) 2632 tooltip => ((CFClient::UI::Label::escape $desc)
2565 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), 2633 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2566 connect_button_down => sub { 2634 connect_button_down => sub {
2567 my ($self, $ev, $x, $y) = @_; 2635 my ($self, $ev, $x, $y) = @_;
2568 2636
2569 # todo: maybe put examine on 1? but should just be a tooltip :( 2637 # todo: maybe put examine on 1? but should just be a tooltip :(
2578 } elsif ($ev->{button} == 2) { 2646 } elsif ($ev->{button} == 2) {
2579 $::CONN->send ("apply $item->{tag}"); 2647 $::CONN->send ("apply $item->{tag}");
2580 } elsif ($ev->{button} == 3) { 2648 } elsif ($ev->{button} == 3) {
2581 my @menu_items = ( 2649 my @menu_items = (
2582 ["examine", sub { $::CONN->send ("examine $item->{tag}") }], 2650 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2583 ["mark", sub { $::CONN->send ("mark $item->{tag}") }], 2651 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2584 ["apply", sub { $::CONN->send ("apply $item->{tag}") }], 2652 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2653 (
2654 $item->{flags} & Crossfire::Protocol::F_LOCKED
2655 ? (
2656 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2657 )
2658 : (
2659 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2585 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }], 2660 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2586 [ 2661 )
2587 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2588 sub { $::CONN->send ("lock $item->{tag}") },
2589 ], 2662 ),
2590 ); 2663 );
2591 2664
2592 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); 2665 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2593 } 2666 }
2594 2667
2937 $child->{x} = int $child->{x}; 3010 $child->{x} = int $child->{x};
2938 $child->{y} = int $child->{y}; 3011 $child->{y} = int $child->{y};
2939 } 3012 }
2940 3013
2941 $self->SUPER::add (@children); 3014 $self->SUPER::add (@children);
3015
3016 while (@children) {
3017 my $w = pop @children;
3018 push @children, $w->children;
3019 $w->{visible} = 1;
3020 }
3021}
3022
3023sub remove {
3024 my ($self, @children) = @_;
3025
3026 $self->SUPER::remove (@children);
3027
3028 while (@children) {
3029 my $w = pop @children;
3030 push @children, $w->children;
3031 delete $w->{visible};
3032 }
2942} 3033}
2943 3034
2944sub on_refresh { 3035sub on_refresh {
2945 my ($self, $id, $cb) = @_; 3036 my ($self, $id, $cb) = @_;
2946 3037
3015 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 3106 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3016 glClear GL_COLOR_BUFFER_BIT; 3107 glClear GL_COLOR_BUFFER_BIT;
3017 3108
3018 glMatrixMode GL_PROJECTION; 3109 glMatrixMode GL_PROJECTION;
3019 glLoadIdentity; 3110 glLoadIdentity;
3020 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; 3111 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3021 glMatrixMode GL_MODELVIEW; 3112 glMatrixMode GL_MODELVIEW;
3022 glLoadIdentity; 3113 glLoadIdentity;
3023 3114
3024 $self->_draw; 3115 $self->_draw;
3025} 3116}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines