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.222 by root, Mon May 22 03:59: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);
207} 214}
208 215
419 Scalar::Util::weaken ($self->{parent} = $parent); 426 Scalar::Util::weaken ($self->{parent} = $parent);
420 427
421 # TODO: req_w _does_change after ->reconfigure 428 # TODO: req_w _does_change after ->reconfigure
422 $self->check_size 429 $self->check_size
423 unless exists $self->{req_w}; 430 unless exists $self->{req_w};
431
432 $self->show;
424} 433}
425 434
426sub check_size { 435sub check_size {
427 my ($self, $forced) = @_; 436 my ($self, $forced) = @_;
428 437
797 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); 806 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
798 }, 807 },
799 ; 808 ;
800 809
801 $self = $class->SUPER::new ( 810 $self = $class->SUPER::new (
802 vp => (new CFClient::UI::ViewPort), 811 vp => (new CFClient::UI::ViewPort expand => 1),
803 slider => $slider, 812 slider => $slider,
804 @_, 813 @_,
805 ); 814 );
806 815
807 $self->{vp}->add ($self->{scrolled}); 816 $self->{vp}->add ($self->{scrolled});
2050 range => [0, 0, 100, 10], 2059 range => [0, 0, 100, 10],
2051 req_w => $::WIDTH / 80, 2060 req_w => $::WIDTH / 80,
2052 req_h => $::WIDTH / 80, 2061 req_h => $::WIDTH / 80,
2053 vertical => 0, 2062 vertical => 0,
2054 can_hover => 1, 2063 can_hover => 1,
2055 inner_pad => .05, 2064 inner_pad => 0.02,
2056 @_ 2065 @_
2057 ); 2066 );
2058 2067
2059 $self->set_value ($self->{range}[0]); 2068 $self->set_value ($self->{range}[0]);
2060 $self->update; 2069 $self->update;
2207 2216
2208 $self->{fontsize} = $fontsize; 2217 $self->{fontsize} = $fontsize;
2209 $self->reflow; 2218 $self->reflow;
2210} 2219}
2211 2220
2221sub size_allocate {
2222 my ($self, $w, $h) = @_;
2223
2224 $self->SUPER::size_allocate ($w, $h);
2225
2226 $self->{layout}->set_font ($self->{font}) if $self->{font};
2227 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2228 $self->{layout}->set_width ($self->{children}[0]{w});
2229
2230 $self->reflow;
2231}
2232
2212sub text_height { 2233sub text_height {
2213 my ($self, $text) = @_; 2234 my ($self, $text, $indent) = @_;
2214 2235
2215 my $layout = $self->{layout}; 2236 my $layout = $self->{layout};
2216 2237
2217 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2238 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2218 $layout->set_width ($self->{children}[0]{w}); 2239 $layout->set_width ($self->{children}[0]{w} - $indent);
2219 $layout->set_markup ($text); 2240 $layout->set_markup ($text);
2220 2241
2221 ($layout->size)[1] 2242 ($layout->size)[1]
2222} 2243}
2223 2244
2226 2247
2227 $self->{need_reflow}++; 2248 $self->{need_reflow}++;
2228 $self->update; 2249 $self->update;
2229} 2250}
2230 2251
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 { 2252sub add_paragraph {
2244 my ($self, $color, $text) = @_; 2253 my ($self, $color, $text, $indent) = @_;
2245 2254
2246 #TODO: intelligently "reformat" paragraph 2255 #TODO: intelligently "reformat" paragraph
2247 2256
2257 for my $line (split /\n/, $text) {
2248 my $height = $self->text_height ($text); 2258 my $height = $self->text_height ($line);
2249
2250 $self->{height} += $height; 2259 $self->{height} += $height;
2251
2252 push @{$self->{par}}, [$height, $color, $text]; 2260 push @{$self->{par}}, [$height, $color, $indent, $line];
2261 }
2253 2262
2254 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; 2263 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
2255 $self->{children}[1]->update; 2264 $self->{children}[1]->update;
2256} 2265}
2257 2266
2266 2275
2267 $ROOT->on_post_alloc ($self, sub { 2276 $ROOT->on_post_alloc ($self, sub {
2268 if (delete $self->{need_reflow}) { 2277 if (delete $self->{need_reflow}) {
2269 my $height = 0; 2278 my $height = 0;
2270 2279
2271 $height += $_->[0] = $self->text_height ($_->[2]) 2280 $height += $_->[0] = $self->text_height ($_->[3], $_->[2])
2272 for @{$self->{par}}; 2281 for @{$self->{par}};
2273 2282
2274 $self->{height} = $height; 2283 $self->{height} = $height;
2275 2284
2276 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2285 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2286 $self->{children}[1]->update;
2277 2287
2278 delete $self->{texture}; 2288 delete $self->{texture};
2279 } 2289 }
2280 2290
2281 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2291 $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; 2292 glClearColor 0.5, 0.5, 0.5, 0;
2283 glClear GL_COLOR_BUFFER_BIT; 2293 glClear GL_COLOR_BUFFER_BIT;
2284 2294
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]; 2295 my $top = int $self->{children}[1]{range}[0];
2289 2296
2290 my $y0 = $top; 2297 my $y0 = $top;
2291 my $y1 = $top + $self->{h}; 2298 my $y1 = $top + $self->{h};
2292 2299
2294 2301
2295 my $layout = $self->{layout}; 2302 my $layout = $self->{layout};
2296 2303
2297 $layout->set_font ($self->{font}) if $self->{font}; 2304 $layout->set_font ($self->{font}) if $self->{font};
2298 2305
2306 glEnable GL_BLEND;
2307 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2308
2299 for my $par (@{$self->{par}}) { 2309 for my $par (@{$self->{par}}) {
2300 my $h = $par->[0]; 2310 my $h = $par->[0];
2301 2311
2302 if ($y0 < $y + $h && $y < $y1) { 2312 if ($y0 < $y + $h && $y < $y1) {
2303 $layout->set_foreground (@{ $par->[1] }); 2313 $layout->set_foreground (@{ $par->[1] });
2314 $layout->set_width ($self->{w} - $par->[2]);
2304 $layout->set_markup ($par->[2]); 2315 $layout->set_markup ($par->[3]);
2305 2316
2306 my ($W, $H) = $layout->size; 2317 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2307 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0); 2318
2319 glRasterPos $par->[2], $y - $y0;
2320 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2308 } 2321 }
2309 2322
2310 $y += $h; 2323 $y += $h;
2311 } 2324 }
2312 2325
2313 glDisable GL_TEXTURE_2D; 2326 glDisable GL_BLEND;
2314 }; 2327 };
2315 }); 2328 });
2316} 2329}
2317 2330
2318sub _draw { 2331sub _draw {
2489use CFClient::OpenGL; 2502use CFClient::OpenGL;
2490 2503
2491sub new { 2504sub new {
2492 my $class = shift; 2505 my $class = shift;
2493 2506
2494 $class->SUPER::new ( 2507 my $self = $class->SUPER::new (
2495 aspect => 1, 2508 aspect => 1,
2496 @_, 2509 @_,
2497 ) 2510 );
2511
2512 if ($self->{anim} && $self->{animspeed}) {
2513 Scalar::Util::weaken (my $widget = $self);
2514
2515 $self->{timer} = Event->timer (
2516 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
2517 hard => 1,
2518 interval => $self->{animspeed},
2519 cb => sub {
2520 ++$widget->{frame};
2521 $widget->update;
2522 },
2523 );
2524 }
2525
2526 $self
2498} 2527}
2499 2528
2500sub size_request { 2529sub size_request {
2501 (32, 8) 2530 (32, 8)
2502} 2531}
2503 2532
2533sub update {
2534 my ($self) = @_;
2535
2536 return unless $self->{visible};
2537
2538 $self->SUPER::update;
2539}
2540
2504sub _draw { 2541sub _draw {
2505 my ($self) = @_; 2542 my ($self) = @_;
2506 2543
2507 return unless $::CONN;#d# manage and cache textures differently 2544 return unless $::CONN;#d# manage and cache textures differently
2545
2546 my $face;
2547
2548 if ($self->{frame}) {
2549 my $anim = $::CONN->{anim}[$self->{anim}];
2550
2551 $face = $anim->[ $self->{frame} % @$anim ]
2552 if $anim && @$anim;
2553 }
2554
2508 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2555 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2509 2556
2510 # TODO animation
2511 if ($tex) { 2557 if ($tex) {
2512 glEnable GL_TEXTURE_2D; 2558 glEnable GL_TEXTURE_2D;
2513 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2559 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2514 glColor 1, 1, 1, 1; 2560 glColor 1, 1, 1, 1;
2515 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2561 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2516 glDisable GL_TEXTURE_2D; 2562 glDisable GL_TEXTURE_2D;
2517 } 2563 }
2518} 2564}
2519 2565
2566sub DESTROY {
2567 my ($self) = @_;
2568
2569 $self->{timer}->cancel
2570 if $self->{timer};
2571
2572 $self->SUPER::DESTROY;
2573}
2574
2520############################################################################# 2575#############################################################################
2521 2576
2522package CFClient::UI::InventoryItem; 2577package CFClient::UI::InventoryItem;
2523 2578
2524our @ISA = CFClient::UI::HBox::; 2579our @ISA = CFClient::UI::HBox::;
2527 my ($item) = @_; 2582 my ($item) = @_;
2528 2583
2529 my $desc = 2584 my $desc =
2530 $item->{nrof} < 2 2585 $item->{nrof} < 2
2531 ? $item->{name} 2586 ? $item->{name}
2532 : "$item->{nrof} $item->{name_pl}"; 2587 : "$item->{nrof} × $item->{name_pl}";
2533 2588
2534 $item->{flags} & Crossfire::Protocol::F_OPEN 2589 $item->{flags} & Crossfire::Protocol::F_OPEN
2535 and $desc .= " (open)"; 2590 and $desc .= " (open)";
2536 $item->{flags} & Crossfire::Protocol::F_APPLIED 2591 $item->{flags} & Crossfire::Protocol::F_APPLIED
2537 and $desc .= " (applied)"; 2592 and $desc .= " (applied)";
2557 my $item = delete $args{item}; 2612 my $item = delete $args{item};
2558 2613
2559 my $desc = _item_to_desc ($item); 2614 my $desc = _item_to_desc ($item);
2560 2615
2561 my $self = $class->SUPER::new ( 2616 my $self = $class->SUPER::new (
2562 can_hover => 1, 2617 can_hover => 1,
2563 can_events => 1, 2618 can_events => 1,
2564 tooltip => ((CFClient::UI::Label::escape $desc) 2619 tooltip => ((CFClient::UI::Label::escape $desc)
2565 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), 2620 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2566 connect_button_down => sub { 2621 connect_button_down => sub {
2567 my ($self, $ev, $x, $y) = @_; 2622 my ($self, $ev, $x, $y) = @_;
2568 2623
2569 # todo: maybe put examine on 1? but should just be a tooltip :( 2624 # todo: maybe put examine on 1? but should just be a tooltip :(
2578 } elsif ($ev->{button} == 2) { 2633 } elsif ($ev->{button} == 2) {
2579 $::CONN->send ("apply $item->{tag}"); 2634 $::CONN->send ("apply $item->{tag}");
2580 } elsif ($ev->{button} == 3) { 2635 } elsif ($ev->{button} == 3) {
2581 my @menu_items = ( 2636 my @menu_items = (
2582 ["examine", sub { $::CONN->send ("examine $item->{tag}") }], 2637 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2583 ["mark", sub { $::CONN->send ("mark $item->{tag}") }], 2638 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2584 ["apply", sub { $::CONN->send ("apply $item->{tag}") }], 2639 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2640 (
2641 $item->{flags} & Crossfire::Protocol::F_LOCKED
2642 ? (
2643 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2644 )
2645 : (
2646 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2585 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }], 2647 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2586 [ 2648 )
2587 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2588 sub { $::CONN->send ("lock $item->{tag}") },
2589 ], 2649 ),
2590 ); 2650 );
2591 2651
2592 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); 2652 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2593 } 2653 }
2594 2654
2937 $child->{x} = int $child->{x}; 2997 $child->{x} = int $child->{x};
2938 $child->{y} = int $child->{y}; 2998 $child->{y} = int $child->{y};
2939 } 2999 }
2940 3000
2941 $self->SUPER::add (@children); 3001 $self->SUPER::add (@children);
3002
3003 while (@children) {
3004 my $w = pop @children;
3005 push @children, $w->children;
3006 $w->{visible} = 1;
3007 }
3008}
3009
3010sub remove {
3011 my ($self, @children) = @_;
3012
3013 $self->SUPER::remove (@children);
3014
3015 while (@children) {
3016 my $w = pop @children;
3017 push @children, $w->children;
3018 delete $w->{visible};
3019 }
2942} 3020}
2943 3021
2944sub on_refresh { 3022sub on_refresh {
2945 my ($self, $id, $cb) = @_; 3023 my ($self, $id, $cb) = @_;
2946 3024
3015 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 3093 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3016 glClear GL_COLOR_BUFFER_BIT; 3094 glClear GL_COLOR_BUFFER_BIT;
3017 3095
3018 glMatrixMode GL_PROJECTION; 3096 glMatrixMode GL_PROJECTION;
3019 glLoadIdentity; 3097 glLoadIdentity;
3020 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; 3098 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3021 glMatrixMode GL_MODELVIEW; 3099 glMatrixMode GL_MODELVIEW;
3022 glLoadIdentity; 3100 glLoadIdentity;
3023 3101
3024 $self->_draw; 3102 $self->_draw;
3025} 3103}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines