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.204 by root, Sun May 14 22:24:16 2006 UTC vs.
Revision 1.212 by elmex, Wed May 17 14:55:14 2006 UTC

282 282
283 delete $self->{max_w}; $self->{max_w} = $w if $w; 283 delete $self->{max_w}; $self->{max_w} = $w if $w;
284 delete $self->{max_h}; $self->{max_h} = $h if $h; 284 delete $self->{max_h}; $self->{max_h} = $h if $h;
285} 285}
286 286
287sub set_tooltip {
288 my ($self, $tooltip) = @_;
289
290 $self->{tooltip} = $tooltip;
291
292 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
293 delete $CFClient::UI::TOOLTIP->{owner};
294 CFClient::UI::check_tooltip;
295 }
296}
297
287# translate global coordinates to local coordinate system 298# translate global coordinates to local coordinate system
288sub coord2local { 299sub coord2local {
289 my ($self, $x, $y) = @_; 300 my ($self, $x, $y) = @_;
290 301
291 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 302 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
375 glVertex $self->{w}, 0; 386 glVertex $self->{w}, 0;
376 glVertex $self->{w}, $self->{h}; 387 glVertex $self->{w}, $self->{h};
377 glVertex 0 , $self->{h}; 388 glVertex 0 , $self->{h};
378 glEnd; 389 glEnd;
379 glPopMatrix; 390 glPopMatrix;
380 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 391 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
381 } 392 }
382} 393}
383 394
384sub _draw { 395sub _draw {
385 my ($self) = @_; 396 my ($self) = @_;
455 my $class = shift; 466 my $class = shift;
456 467
457 # range [value, low, high, page] 468 # range [value, low, high, page]
458 469
459 $class->SUPER::new ( 470 $class->SUPER::new (
460 bg => [0, 0, 0, 0.2], 471 #bg => [0, 0, 0, 0.2],
461 active_bg => [1, 1, 1, 0.5], 472 #active_bg => [1, 1, 1, 0.5],
462 @_ 473 @_
463 ) 474 )
464} 475}
465 476
466sub _draw { 477sub _draw {
467 my ($self) = @_; 478 my ($self) = @_;
468 479
480 my $color = $FOCUS == $self && $self->{active_bg}
481 ? $self->{active_bg}
482 : $self->{bg};
483
484 if ($color && (@$color < 4 || $color->[3])) {
469 my ($w, $h) = @$self{qw(w h)}; 485 my ($w, $h) = @$self{qw(w h)};
470 486
471 glEnable GL_BLEND; 487 glEnable GL_BLEND;
472 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 488 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
473 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 489 glColor @$color;
474 490
475 glBegin GL_QUADS; 491 glBegin GL_QUADS;
476 glVertex 0 , 0; 492 glVertex 0 , 0;
477 glVertex 0 , $h; 493 glVertex 0 , $h;
478 glVertex $w, $h; 494 glVertex $w, $h;
479 glVertex $w, 0; 495 glVertex $w, 0;
480 glEnd; 496 glEnd;
481 497
482 glDisable GL_BLEND; 498 glDisable GL_BLEND;
499 }
483} 500}
484 501
485############################################################################# 502#############################################################################
486 503
487package CFClient::UI::Empty; 504package CFClient::UI::Empty;
895 my ($self, $w, $h) = @_; 912 my ($self, $w, $h) = @_;
896 913
897 $h -= List::Util::max 0, $self->border * 2; 914 $h -= List::Util::max 0, $self->border * 2;
898 $w -= List::Util::max 0, $self->border * 2; 915 $w -= List::Util::max 0, $self->border * 2;
899 916
900 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) 917 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
901 if $self->{title}; 918 if $self->{title};
902 919
903 $self->child->configure ($self->border, $self->border, $w, $h); 920 $self->child->configure ($self->border, $self->border, $w, $h);
904} 921}
905 922
1266 1283
1267############################################################################# 1284#############################################################################
1268 1285
1269package CFClient::UI::Label; 1286package CFClient::UI::Label;
1270 1287
1271our @ISA = CFClient::UI::Base::; 1288our @ISA = CFClient::UI::DrawBG::;
1272 1289
1273use CFClient::OpenGL; 1290use CFClient::OpenGL;
1274 1291
1275sub new { 1292sub new {
1276 my ($class, %arg) = @_; 1293 my ($class, %arg) = @_;
1277 1294
1278 my $self = $class->SUPER::new ( 1295 my $self = $class->SUPER::new (
1279 fg => [1, 1, 1], 1296 fg => [1, 1, 1],
1297 #bg => none
1298 #active_bg => none
1280 #font => default_font 1299 #font => default_font
1281 #text => initial text 1300 #text => initial text
1282 #markup => initial narkup 1301 #markup => initial narkup
1283 layout => (new CFClient::Layout), 1302 layout => (new CFClient::Layout),
1284 fontsize => 1, 1303 fontsize => 1,
1302 } 1321 }
1303 1322
1304 $self 1323 $self
1305} 1324}
1306 1325
1307sub escape { 1326sub escape($) {
1308 local $_ = $_[1]; 1327 local $_ = $_[0];
1309 1328
1310 s/&/&amp;/g; 1329 s/&/&amp;/g;
1311 s/>/&gt;/g; 1330 s/>/&gt;/g;
1312 s/</&lt;/g; 1331 s/</&lt;/g;
1313 1332
1314 $_[1] 1333 $_
1315} 1334}
1316 1335
1317sub update { 1336sub update {
1318 my ($self) = @_; 1337 my ($self) = @_;
1319 1338
1390 $self->check_size; 1409 $self->check_size;
1391} 1410}
1392 1411
1393sub _draw { 1412sub _draw {
1394 my ($self) = @_; 1413 my ($self) = @_;
1414
1415 $self->SUPER::_draw; # draw background, if applicable
1395 1416
1396 my $tex = $self->{texture} ||= do { 1417 my $tex = $self->{texture} ||= do {
1397 $self->{layout}->set_foreground (@{$self->{fg}}); 1418 $self->{layout}->set_foreground (@{$self->{fg}});
1398 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1419 $self->{layout}->set_font ($self->{font}) if $self->{font};
1399 $self->{layout}->set_width ($self->{w}); 1420 $self->{layout}->set_width ($self->{w});
1400 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1421 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1401 1422
1402 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1423 my $tex = new_from_layout CFClient::Texture $self->{layout};
1403 1424
1404 $self->{ox} = int $self->{align} < 0 ? $self->{padding} 1425 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1405 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1426 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1406 : ($self->{w} - $tex->{w}) * 0.5; 1427 : ($self->{w} - $tex->{w}) * 0.5);
1407 1428
1408 $self->{oy} = int $self->{valign} < 0 ? $self->{padding} 1429 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1409 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1430 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1410 : ($self->{h} - $tex->{h}) * 0.5; 1431 : ($self->{h} - $tex->{h}) * 0.5);
1411 1432
1412 $tex 1433 $tex
1413 }; 1434 };
1414 1435
1415 glEnable GL_TEXTURE_2D; 1436 glEnable GL_TEXTURE_2D;
1470sub set_text { 1491sub set_text {
1471 my ($self, $text) = @_; 1492 my ($self, $text) = @_;
1472 1493
1473 $self->{cursor} = length $text; 1494 $self->{cursor} = length $text;
1474 $self->_set_text ($text); 1495 $self->_set_text ($text);
1496 $self->update;
1475 $self->check_size; 1497 $self->check_size;
1476 $self->update;
1477} 1498}
1478 1499
1479sub get_text { 1500sub get_text {
1480 $_[0]{text} 1501 $_[0]{text}
1481} 1502}
1521 substr $text, $self->{cursor}++, 0, chr $uni; 1542 substr $text, $self->{cursor}++, 0, chr $uni;
1522 } 1543 }
1523 1544
1524 $self->_set_text ($text); 1545 $self->_set_text ($text);
1525 $self->update; 1546 $self->update;
1547 $self->check_size;
1526} 1548}
1527 1549
1528sub focus_in { 1550sub focus_in {
1529 my ($self) = @_; 1551 my ($self) = @_;
1530 1552
1659 my $class = shift; 1681 my $class = shift;
1660 1682
1661 $class->SUPER::new ( 1683 $class->SUPER::new (
1662 padding => 4, 1684 padding => 4,
1663 fg => [1, 1, 1], 1685 fg => [1, 1, 1],
1664 bg => [1, 1, 1, 0.2],
1665 active_fg => [0, 0, 1], 1686 active_fg => [0, 0, 1],
1666 can_hover => 1, 1687 can_hover => 1,
1667 align => 0, 1688 align => 0,
1668 valign => 0, 1689 valign => 0,
1669 can_events => 1, 1690 can_events => 1,
1717 1738
1718 $class->SUPER::new ( 1739 $class->SUPER::new (
1719 padding => 2, 1740 padding => 2,
1720 fg => [1, 1, 1], 1741 fg => [1, 1, 1],
1721 active_fg => [1, 1, 0], 1742 active_fg => [1, 1, 0],
1743 bg => [0, 0, 0, 0.2],
1744 active_bg => [1, 1, 1, 0.5],
1722 state => 0, 1745 state => 0,
1723 can_hover => 1, 1746 can_hover => 1,
1724 @_ 1747 @_
1725 ) 1748 )
1726} 1749}
2010 qw(s1_slider.png s1_slider_bg.png); 2033 qw(s1_slider.png s1_slider_bg.png);
2011 2034
2012sub new { 2035sub new {
2013 my $class = shift; 2036 my $class = shift;
2014 2037
2015 # range [value, low, high, page] 2038 # range [value, low, high, page, unit]
2016 2039
2017 # TODO: 0-width page 2040 # TODO: 0-width page
2018 # TODO: req_w/h are wrong with vertical 2041 # TODO: req_w/h are wrong with vertical
2019 # TODO: calculations are off 2042 # TODO: calculations are off
2020 my $self = $class->SUPER::new ( 2043 my $self = $class->SUPER::new (
2021 fg => [1, 1, 1], 2044 fg => [1, 1, 1],
2022 active_fg => [0, 0, 0], 2045 active_fg => [0, 0, 0],
2046 bg => [0, 0, 0, 0.2],
2047 active_bg => [1, 1, 1, 0.5],
2023 range => [0, 0, 100, 10], 2048 range => [0, 0, 100, 10],
2024 req_w => $::WIDTH / 80, 2049 req_w => $::WIDTH / 80,
2025 req_h => $::WIDTH / 80, 2050 req_h => $::WIDTH / 80,
2026 vertical => 0, 2051 vertical => 0,
2027 can_hover => 1, 2052 can_hover => 1,
2028 inner_pad => 5, 2053 inner_pad => .05,
2029 @_ 2054 @_
2030 ); 2055 );
2031 2056
2057 $self->set_value ($self->{range}[0]);
2058 $self->update;
2059
2032 $self 2060 $self
2061}
2062
2063sub set_value {
2064 my ($self, $value) = @_;
2065
2066 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2067
2068 $hi = $lo + 1 if $hi <= $lo;
2069
2070 $value = $lo if $value < $lo;
2071 $value = $hi if $value > $hi;
2072
2073 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2074 if $unit;
2075
2076 $page = $hi - $lo if $page > $hi - $lo;
2077
2078 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2079
2080 if ($value != $old_value) {
2081 $self->emit (changed => $value);
2082 $self->update;
2083 }
2033} 2084}
2034 2085
2035sub size_request { 2086sub size_request {
2036 my ($self) = @_; 2087 my ($self) = @_;
2037 2088
2050 2101
2051sub mouse_motion { 2102sub mouse_motion {
2052 my ($self, $ev, $x, $y) = @_; 2103 my ($self, $ev, $x, $y) = @_;
2053 2104
2054 if ($GRAB == $self) { 2105 if ($GRAB == $self) {
2106 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2107
2108 my (undef, $lo, $hi, $page) = @{$self->{range}};
2109
2110 $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad};
2111
2112 $self->set_value ($x * ($hi - $lo) + $lo);
2113 }
2114}
2115
2116sub update {
2117 my ($self) = @_;
2118
2119 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2120 $self->set_value ($self->{range}[0]);
2121
2055 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2122 my ($value, $lo, $hi, $page) = @{$self->{range}};
2056 2123
2057 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2124 my $inner_w = 1 - 2 * $self->{inner_pad};
2058 2125
2059 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2126 $self->{scale} = ($inner_w / ($hi - $lo)) || 1;
2060 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2061 2127
2062 $x -= $inner_pad_px; # substract the padding 2128 $page = $self->{scale} * $page || 10 / ($self->{w} || 1);
2063 $x = $x * ($hi - $lo) / $inner_w + $lo; 2129 $value = $self->{scale} * ($value - $lo);
2064 $x = $lo if $x < $lo;
2065 $x = $hi - $page if $x > $hi - $page;
2066 $self->{range}[0] = $x;
2067 2130
2068 $self->emit (changed => $x); 2131 $value = $self->{inner_pad} + ($value - $page * 0.5);
2069 $self->update; 2132
2133 $value = 0 if $value < 0;
2134 $page = 1 - $value if $value + $page > 1;
2135
2136 $self->{knob_x} = $value;
2137 $self->{knob_w} = $page;
2070 } 2138 });
2071}
2072 2139
2073# the inner_* stuff is for generating a padding for the slider handle, 2140 $self->SUPER::update;
2074# so that the handle doesn't leave the texture. This calculation isn't 100%
2075# correct propably, but it does the job for now
2076sub _calc_inner_pad_px {
2077 my ($self, $w) = @_;
2078 ($w / 100) * $self->{inner_pad} # % to pixels
2079} 2141}
2080 2142
2081sub _draw { 2143sub _draw {
2082 my ($self) = @_; 2144 my ($self) = @_;
2083 2145
2084 $self->SUPER::_draw (); 2146 $self->SUPER::_draw ();
2085 2147
2086 my ($w, $h) = @$self{qw(w h)}; 2148 glScale $self->{w}, $self->{h};
2087 2149
2088 if ($self->{vertical}) { 2150 if ($self->{vertical}) {
2089 # draw a vertical slider like a rotated horizontal slider 2151 # draw a vertical slider like a rotated horizontal slider
2090 2152
2091 glRotate 90, 0, 0, 1; 2153 glRotate 90, 0, 0, 1;
2092 glTranslate 0, -$self->{w}, 0; 2154 glTranslate 0, 1, 0;
2093
2094 ($w, $h) = ($h, $w);
2095 } 2155 }
2096 2156
2097 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2157 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2098 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2158 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2099 2159
2100 my ($value, $lo, $hi, $page) = @{$self->{range}};
2101
2102 $hi = $value + 1 if $lo == $hi;
2103
2104 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
2105 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2106
2107 $page = int $page * $inner_w / ($hi - $lo);
2108 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
2109
2110 $w -= $page;
2111 $page &= ~1;
2112 glTranslate $page * 0.5, 0, 0;
2113 $page ||= 2;
2114
2115 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2116 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2117
2118 glEnable GL_TEXTURE_2D; 2160 glEnable GL_TEXTURE_2D;
2119 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2161 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2120 2162
2121 # draw background 2163 # draw background
2122 $tex[1]->draw_quad_alpha (0, 0, $w, $h); 2164 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2123 2165
2124 # draw handle 2166 # draw handle
2125 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h); 2167 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
2126 2168
2127 glDisable GL_TEXTURE_2D; 2169 glDisable GL_TEXTURE_2D;
2128} 2170}
2129 2171
2130############################################################################# 2172#############################################################################
2476 2518
2477package CFClient::UI::InventoryItem; 2519package CFClient::UI::InventoryItem;
2478 2520
2479our @ISA = CFClient::UI::HBox::; 2521our @ISA = CFClient::UI::HBox::;
2480 2522
2523sub _item_to_desc {
2524 my ($item) = @_;
2525
2526 my $desc =
2527 $item->{nrof} < 2
2528 ? $item->{name}
2529 : "$item->{nrof} $item->{name_pl}";
2530
2531 $item->{flags} & Crossfire::Protocol::F_OPEN
2532 and $desc .= " (open)";
2533 $item->{flags} & Crossfire::Protocol::F_APPLIED
2534 and $desc .= " (applied)";
2535 $item->{flags} & Crossfire::Protocol::F_UNPAID
2536 and $desc .= " (unpaid)";
2537 $item->{flags} & Crossfire::Protocol::F_MAGIC
2538 and $desc .= " (magic)";
2539 $item->{flags} & Crossfire::Protocol::F_CURSED
2540 and $desc .= " (cursed)";
2541 $item->{flags} & Crossfire::Protocol::F_DAMNED
2542 and $desc .= " (damned)";
2543 $item->{flags} & Crossfire::Protocol::F_LOCKED
2544 and $desc .= " *";
2545
2546 $desc
2547}
2548
2481sub new { 2549sub new {
2482 my $class = shift; 2550 my $class = shift;
2483 2551
2484 my %args = @_; 2552 my %args = @_;
2485 2553
2486 my $item = delete $args{item}; 2554 my $item = delete $args{item};
2487 2555
2488 my $desc = $item->{nrof} < 2 2556 my $desc = _item_to_desc ($item);
2489 ? $item->{name}
2490 : "$item->{nrof} $item->{name_pl}";
2491
2492 2557
2493 my $self = $class->SUPER::new ( 2558 my $self = $class->SUPER::new (
2494 can_hover => 1, 2559 can_hover => 1,
2495 can_events => 1, 2560 can_events => 1,
2496 tooltip => (CFClient::UI::Label->escape ($desc) 2561 tooltip => ((CFClient::UI::Label::escape $desc)
2497 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), 2562 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2498 connect_button_down => sub { 2563 connect_button_down => sub {
2499 my ($self, $ev, $x, $y) = @_; 2564 my ($self, $ev, $x, $y) = @_;
2500 2565
2501 # todo: maybe put examine on 1? but should just be a tooltip :( 2566 # todo: maybe put examine on 1? but should just be a tooltip :(
2502 if ($ev->{button} == 1) { 2567 if ($ev->{button} == 1) {
2568 my $targ = $::CONN->{player}{tag};
2569
2570 if ($item->{container} == $::CONN->{player}{tag}) {
2571 $targ = $main::OPENCONT;
2572 }
2573
2503 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); 2574 $::CONN->send ("move $targ $item->{tag} 0");
2504 } elsif ($ev->{button} == 2) { 2575 } elsif ($ev->{button} == 2) {
2505 $::CONN->send ("apply $item->{tag}"); 2576 $::CONN->send ("apply $item->{tag}");
2506 } elsif ($ev->{button} == 3) { 2577 } elsif ($ev->{button} == 3) {
2507 CFClient::UI::Menu->new ( 2578 my @menu_items = (
2508 items => [
2509 ["examine", sub { $::CONN->send ("examine $item->{tag}") }], 2579 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2580 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2581 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2582 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2510 [ 2583 [
2511 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", 2584 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2512 sub { $::CONN->send ("lock $item->{tag}") }, 2585 sub { $::CONN->send ("lock $item->{tag}") },
2513 ],
2514 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2515 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2516 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2517 ], 2586 ],
2518 )->popup ($ev); 2587 );
2588
2589 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2519 } 2590 }
2520 2591
2521 1 2592 1
2522 }, 2593 },
2523 %args 2594 %args
2524 ); 2595 );
2596
2525 2597
2526 $self->add (new CFClient::UI::Face 2598 $self->add (new CFClient::UI::Face
2527 can_events => 0, 2599 can_events => 0,
2528 face => $item->{face}, 2600 face => $item->{face},
2529 anim => $item->{anim}, 2601 anim => $item->{anim},
2530 animspeed => $item->{animspeed}, 2602 animspeed => $item->{animspeed},
2531 ); 2603 );
2532 2604
2533 $self->add (new CFClient::UI::Label 2605 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2534 can_events => 0, 2606
2535 text => $desc, 2607 $self->{item} = $item;
2536 ); 2608
2609 $self->update_item;
2537 2610
2538 $self 2611 $self
2612}
2613
2614sub update_item {
2615 my ($self) = @_;
2616
2617 my $desc = _item_to_desc ($self->{item});
2618
2619 $self->{name_lbl}->set_text ($desc);
2539} 2620}
2540 2621
2541############################################################################# 2622#############################################################################
2542 2623
2543package CFClient::UI::Inventory; 2624package CFClient::UI::Inventory;
2567 } @$items; 2648 } @$items;
2568 2649
2569 $self->{real_items} = \@items; 2650 $self->{real_items} = \@items;
2570 2651
2571 for my $item (@items) { 2652 for my $item (@items) {
2572 my $desc = $item->{nrof} < 2
2573 ? $item->{name}
2574 : "$item->{nrof} $item->{name_pl}";
2575
2576 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; 2653 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2654 $item->update_item ();
2577 } 2655 }
2578 2656
2579 $self->{scrolled}->add (@items); 2657 $self->{scrolled}->add (@items);
2580 2658
2581# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2659# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2662 2740
2663package CFClient::UI::Statusbox; 2741package CFClient::UI::Statusbox;
2664 2742
2665our @ISA = CFClient::UI::VBox::; 2743our @ISA = CFClient::UI::VBox::;
2666 2744
2745sub new {
2746 my $class = shift;
2747
2748 $class->SUPER::new (
2749 fontsize => 0.8,
2750 @_,
2751 )
2752}
2753
2667sub reorder { 2754sub reorder {
2668 my ($self) = @_; 2755 my ($self) = @_;
2669 my $NOW = time; 2756 my $NOW = time;
2670 2757
2671 while (my ($k, $v) = each %{ $self->{item} }) { 2758 while (my ($k, $v) = each %{ $self->{item} }) {
2687 # TODO: doesn't handle markup well (read as: at all) 2774 # TODO: doesn't handle markup well (read as: at all)
2688 my $short = $item->{count} > 1 2775 my $short = $item->{count} > 1
2689 ? "<b>$item->{count} ×</b> $item->{text}" 2776 ? "<b>$item->{count} ×</b> $item->{text}"
2690 : $item->{text}; 2777 : $item->{text};
2691 2778
2779 my $fontsize = $item->{fontsize} || $self->{fontsize};
2780
2692 for ($short) { 2781 for ($short) {
2693 s/^\s+//; 2782 s/^\s+//;
2694 s/\012.*/…/s; 2783 s/\s+/ /g;
2695 my $len = int 40 / $item->{fontsize}; 2784 my $len = int 40 / $fontsize;
2696 substr $_, $len, length, "…" if $len < length; 2785 substr $_, $len, length, "…" if $len < length;
2697 } 2786 }
2698 2787
2699 new CFClient::UI::Label 2788 new CFClient::UI::Label
2700 markup => $short, 2789 markup => $short,
2701 tooltip => $item->{tooltip}, 2790 tooltip => $item->{tooltip},
2702 tooltip_font => $::FONT_PROP, 2791 tooltip_font => $::FONT_PROP,
2703 tooltip_width => 0.67, 2792 tooltip_width => 0.67,
2704 fontsize => $item->{fontsize}, 2793 fontsize => $fontsize,
2705 color => $item->{color}, 2794 fg => $item->{fg},
2706 can_events => 1, 2795 can_events => 1,
2707 can_hover => 1 2796 can_hover => 1
2708 }; 2797 };
2709 } 2798 }
2710 2799
2736 $self->{item}{$group} = { 2825 $self->{item}{$group} = {
2737 id => ++$self->{id}, 2826 id => ++$self->{id},
2738 text => $text, 2827 text => $text,
2739 timeout => $timeout, 2828 timeout => $timeout,
2740 tooltip => $text, 2829 tooltip => $text,
2741 fontsize => 0.8,
2742 color => [0.8, 0.8, 0.8, 0.8], 2830 fg => [0.8, 0.8, 0.8, 0.8],
2743 pri => 0, 2831 pri => 0,
2744 count => 1, 2832 count => 1,
2745 %arg, 2833 %arg,
2746 }; 2834 };
2747 } 2835 }
2795 if $old_w && $old_h && ($old_w != $w || $old_h != $h); 2883 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2796 2884
2797 for my $child ($self->children) { 2885 for my $child ($self->children) {
2798 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2886 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2799 2887
2888 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
2889 if exists $child->{req_x};
2890
2891 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
2892 if exists $child->{req_y};
2893
2800 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2894 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2801 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2895 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
2896
2802 $child->configure ($X, $Y, $W, $H); 2897 $child->configure ($X, $Y, $W, $H);
2803 } 2898 }
2804} 2899}
2805 2900
2806sub coord2local { 2901sub coord2local {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines