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.198 by root, Fri May 12 02:08:52 2006 UTC vs.
Revision 1.208 by root, Mon May 15 22:52:17 2006 UTC

11our ($FOCUS, $HOVER, $GRAB); # various widgets 11our ($FOCUS, $HOVER, $GRAB); # various widgets
12 12
13our $ROOT; 13our $ROOT;
14our $TOOLTIP; 14our $TOOLTIP;
15our $BUTTON_STATE; 15our $BUTTON_STATE;
16
17our %WIDGET; # all widgets, weak-referenced
16 18
17sub check_tooltip { 19sub check_tooltip {
18 if (!$GRAB) { 20 if (!$GRAB) {
19 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
20 if (length $widget->{tooltip}) { 22 if (length $widget->{tooltip}) {
29 $TOOLTIP->set_tooltip_from ($widget); 31 $TOOLTIP->set_tooltip_from ($widget);
30 $TOOLTIP->show; 32 $TOOLTIP->show;
31 33
32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 34 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
33 35
34 if ($x + $TOOLTIP->{w} > $::WIDTH) {
35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); 36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
36 } 37 if $x + $TOOLTIP->{w} > $::WIDTH;
37 38
38 $TOOLTIP->move ($x, $y); 39 $TOOLTIP->move ($x, $y);
39 $TOOLTIP->check_size; 40 $TOOLTIP->check_size;
40 $TOOLTIP->update; 41 $TOOLTIP->update;
41 } 42 }
132 133
133 for (@$vals) { 134 for (@$vals) {
134 my $i = int $_ + $rem; 135 my $i = int $_ + $rem;
135 $rem += $_ - $i; 136 $rem += $_ - $i;
136 $_ = $i; 137 $_ = $i;
138 }
139}
140
141# call when resolution changes etc.
142sub rescale_widgets {
143 my ($sx, $sy) = @_;
144
145 for my $widget (values %WIDGET) {
146 if ($widget->{toplevel}) {
147 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
148 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
149 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
150 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
151 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
152 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
153 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
154 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
155 }
156
157 $widget->reconfigure;
137 } 158 }
138} 159}
139 160
140############################################################################# 161#############################################################################
141 162
160 if (/^connect_(.*)$/) { 181 if (/^connect_(.*)$/) {
161 $self->connect ($1 => delete $self->{$_}); 182 $self->connect ($1 => delete $self->{$_});
162 } 183 }
163 } 184 }
164 185
186 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
187
165 $self 188 $self
166} 189}
167 190
168sub destroy { 191sub destroy {
169 my ($self) = @_; 192 my ($self) = @_;
242 265
243sub size_allocate { 266sub size_allocate {
244 # nothing to be done 267 # nothing to be done
245} 268}
246 269
247sub children {
248}
249
250# call when resolution changes etc.
251sub reconfigure { 270sub reconfigure {
252 my ($self) = @_; 271 my ($self) = @_;
253
254 $_->reconfigure
255 for $self->children;
256 272
257 $self->check_size (1); 273 $self->check_size (1);
258 $self->update; 274 $self->update;
275}
276
277sub children {
259} 278}
260 279
261sub set_max_size { 280sub set_max_size {
262 my ($self, $w, $h) = @_; 281 my ($self, $w, $h) = @_;
263 282
356 glVertex $self->{w}, 0; 375 glVertex $self->{w}, 0;
357 glVertex $self->{w}, $self->{h}; 376 glVertex $self->{w}, $self->{h};
358 glVertex 0 , $self->{h}; 377 glVertex 0 , $self->{h};
359 glEnd; 378 glEnd;
360 glPopMatrix; 379 glPopMatrix;
361 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 380 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
362 } 381 }
363} 382}
364 383
365sub _draw { 384sub _draw {
366 my ($self) = @_; 385 my ($self) = @_;
383sub set_parent { 402sub set_parent {
384 my ($self, $parent) = @_; 403 my ($self, $parent) = @_;
385 404
386 Scalar::Util::weaken ($self->{parent} = $parent); 405 Scalar::Util::weaken ($self->{parent} = $parent);
387 406
388
389 # TODO: req_w _does_change after ->reconfigure 407 # TODO: req_w _does_change after ->reconfigure
390 $self->check_size 408 $self->check_size
391 unless exists $self->{req_w}; 409 unless exists $self->{req_w};
392} 410}
393 411
418} 436}
419 437
420sub DESTROY { 438sub DESTROY {
421 my ($self) = @_; 439 my ($self) = @_;
422 440
441 delete $WIDGET{$self+0};
423 #$self->deactivate; 442 #$self->deactivate;
424} 443}
425 444
426############################################################################# 445#############################################################################
427 446
777 $self->add ($self->{slider}); 796 $self->add ($self->{slider});
778 797
779 $self 798 $self
780} 799}
781 800
782#TODO# update range on size_allocate depeneing on child 801#TODO# update range on size_allocate depending on child
783# update viewport offset on scroll 802# update viewport offset on scroll
784 803
785############################################################################# 804#############################################################################
786 805
787package CFClient::UI::Frame; 806package CFClient::UI::Frame;
788 807
789our @ISA = CFClient::UI::Bin::; 808our @ISA = CFClient::UI::Bin::;
790 809
791use CFClient::OpenGL; 810use CFClient::OpenGL;
811
812sub new {
813 my $class = shift;
814
815 $class->SUPER::new (
816 bg => undef,
817 @_,
818 )
819}
820
821sub _draw {
822 my ($self) = @_;
823
824 if ($self->{bg}) {
825 my ($w, $h) = @$self{qw(w h)};
826
827 glEnable GL_BLEND;
828 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
829 glColor @{ $self->{bg} };
830
831 glBegin GL_QUADS;
832 glVertex 0 , 0;
833 glVertex 0 , $h;
834 glVertex $w, $h;
835 glVertex $w, 0;
836 glEnd;
837
838 glDisable GL_BLEND;
839 }
840
841 $self->SUPER::_draw;
842}
792 843
793############################################################################# 844#############################################################################
794 845
795package CFClient::UI::FancyFrame; 846package CFClient::UI::FancyFrame;
796 847
809 860
810 my $self = $class->SUPER::new ( 861 my $self = $class->SUPER::new (
811 bg => [1, 1, 1, 1], 862 bg => [1, 1, 1, 1],
812 border_bg => [1, 1, 1, 1], 863 border_bg => [1, 1, 1, 1],
813 border => 0.6, 864 border => 0.6,
865 toplevel => 1,
814 can_events => 1, 866 can_events => 1,
815 @_ 867 @_
816 ); 868 );
817 869
818 $self->{title} &&= new CFClient::UI::Label 870 $self->{title} &&= new CFClient::UI::Label
843 my ($self, $w, $h) = @_; 895 my ($self, $w, $h) = @_;
844 896
845 $h -= List::Util::max 0, $self->border * 2; 897 $h -= List::Util::max 0, $self->border * 2;
846 $w -= List::Util::max 0, $self->border * 2; 898 $w -= List::Util::max 0, $self->border * 2;
847 899
848 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) 900 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
849 if $self->{title}; 901 if $self->{title};
850 902
851 $self->child->configure ($self->border, $self->border, $w, $h); 903 $self->child->configure ($self->border, $self->border, $w, $h);
852} 904}
853 905
1347 $self->{layout}->set_width ($self->{w}); 1399 $self->{layout}->set_width ($self->{w});
1348 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1400 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1349 1401
1350 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1402 my $tex = new_from_layout CFClient::Texture $self->{layout};
1351 1403
1352 $self->{ox} = int $self->{align} < 0 ? $self->{padding} 1404 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1353 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1405 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1354 : ($self->{w} - $tex->{w}) * 0.5; 1406 : ($self->{w} - $tex->{w}) * 0.5);
1355 1407
1356 $self->{oy} = int $self->{valign} < 0 ? $self->{padding} 1408 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1357 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1409 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1358 : ($self->{h} - $tex->{h}) * 0.5; 1410 : ($self->{h} - $tex->{h}) * 0.5);
1359 1411
1360 $tex 1412 $tex
1361 }; 1413 };
1362 1414
1363 glEnable GL_TEXTURE_2D; 1415 glEnable GL_TEXTURE_2D;
1418sub set_text { 1470sub set_text {
1419 my ($self, $text) = @_; 1471 my ($self, $text) = @_;
1420 1472
1421 $self->{cursor} = length $text; 1473 $self->{cursor} = length $text;
1422 $self->_set_text ($text); 1474 $self->_set_text ($text);
1475 $self->update;
1423 $self->check_size; 1476 $self->check_size;
1424 $self->update;
1425} 1477}
1426 1478
1427sub get_text { 1479sub get_text {
1428 $_[0]{text} 1480 $_[0]{text}
1429} 1481}
1449 my $sym = $ev->{sym}; 1501 my $sym = $ev->{sym};
1450 my $uni = $ev->{unicode}; 1502 my $uni = $ev->{unicode};
1451 1503
1452 my $text = $self->get_text; 1504 my $text = $self->get_text;
1453 1505
1454 if ($sym == 8) { 1506 if ($uni == 8) {
1455 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1507 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1456 } elsif ($sym == 127) { 1508 } elsif ($uni == 127) {
1457 substr $text, $self->{cursor}, 1, ""; 1509 substr $text, $self->{cursor}, 1, "";
1458 } elsif ($sym == CFClient::SDLK_LEFT) { 1510 } elsif ($sym == CFClient::SDLK_LEFT) {
1459 --$self->{cursor} if $self->{cursor}; 1511 --$self->{cursor} if $self->{cursor};
1460 } elsif ($sym == CFClient::SDLK_RIGHT) { 1512 } elsif ($sym == CFClient::SDLK_RIGHT) {
1461 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1513 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1462 } elsif ($sym == CFClient::SDLK_HOME) { 1514 } elsif ($sym == CFClient::SDLK_HOME) {
1463 $self->{cursor} = 0; 1515 $self->{cursor} = 0;
1464 } elsif ($sym == CFClient::SDLK_END) { 1516 } elsif ($sym == CFClient::SDLK_END) {
1465 $self->{cursor} = length $text; 1517 $self->{cursor} = length $text;
1466 } elsif ($sym == 27) { 1518 } elsif ($uni == 27) {
1467 $self->emit ('escape'); 1519 $self->emit ('escape');
1468 } elsif ($uni) { 1520 } elsif ($uni) {
1469 substr $text, $self->{cursor}++, 0, chr $uni; 1521 substr $text, $self->{cursor}++, 0, chr $uni;
1470 } 1522 }
1471 1523
1472 $self->_set_text ($text); 1524 $self->_set_text ($text);
1473 $self->update; 1525 $self->update;
1526 $self->check_size;
1474} 1527}
1475 1528
1476sub focus_in { 1529sub focus_in {
1477 my ($self) = @_; 1530 my ($self) = @_;
1478 1531
1958 qw(s1_slider.png s1_slider_bg.png); 2011 qw(s1_slider.png s1_slider_bg.png);
1959 2012
1960sub new { 2013sub new {
1961 my $class = shift; 2014 my $class = shift;
1962 2015
1963 # range [value, low, high, page] 2016 # range [value, low, high, page, unit]
1964 2017
1965 # TODO: 0-width page 2018 # TODO: 0-width page
1966 # TODO: req_w/h are wrong with vertical 2019 # TODO: req_w/h are wrong with vertical
1967 # TODO: calculations are off 2020 # TODO: calculations are off
1968 my $self = $class->SUPER::new ( 2021 my $self = $class->SUPER::new (
1971 range => [0, 0, 100, 10], 2024 range => [0, 0, 100, 10],
1972 req_w => $::WIDTH / 80, 2025 req_w => $::WIDTH / 80,
1973 req_h => $::WIDTH / 80, 2026 req_h => $::WIDTH / 80,
1974 vertical => 0, 2027 vertical => 0,
1975 can_hover => 1, 2028 can_hover => 1,
1976 inner_pad => 5, 2029 inner_pad => .05,
1977 @_ 2030 @_
1978 ); 2031 );
1979 2032
2033 $self->set_value ($self->{range}[0]);
2034 $self->update;
2035
1980 $self 2036 $self
2037}
2038
2039sub set_value {
2040 my ($self, $value) = @_;
2041
2042 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2043
2044 $hi = $lo + 1 if $hi <= $lo;
2045
2046 $value = $lo if $value < $lo;
2047 $value = $hi if $value > $hi;
2048
2049 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2050 if $unit;
2051
2052 $page = $hi - $lo if $page > $hi - $lo;
2053
2054 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2055
2056 if ($value != $old_value) {
2057 $self->emit (changed => $value);
2058 $self->update;
2059 }
1981} 2060}
1982 2061
1983sub size_request { 2062sub size_request {
1984 my ($self) = @_; 2063 my ($self) = @_;
1985 2064
1998 2077
1999sub mouse_motion { 2078sub mouse_motion {
2000 my ($self, $ev, $x, $y) = @_; 2079 my ($self, $ev, $x, $y) = @_;
2001 2080
2002 if ($GRAB == $self) { 2081 if ($GRAB == $self) {
2082 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2083
2084 my (undef, $lo, $hi, $page) = @{$self->{range}};
2085
2086 $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad};
2087
2088 $self->set_value ($x * ($hi - $lo) + $lo);
2089 }
2090}
2091
2092sub update {
2093 my ($self) = @_;
2094
2095 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2096 $self->set_value ($self->{range}[0]);
2097
2003 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2098 my ($value, $lo, $hi, $page) = @{$self->{range}};
2004 2099
2005 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2100 my $inner_w = 1 - 2 * $self->{inner_pad};
2006 2101
2007 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2102 $self->{scale} = ($inner_w / ($hi - $lo)) || 1;
2008 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2009 2103
2010 $x -= $inner_pad_px; # substract the padding 2104 $page = $self->{scale} * $page || 10 / ($self->{w} || 1);
2011 $x = $x * ($hi - $lo) / $inner_w + $lo; 2105 $value = $self->{scale} * ($value - $lo);
2012 $x = $lo if $x < $lo;
2013 $x = $hi - $page if $x > $hi - $page;
2014 $self->{range}[0] = $x;
2015 2106
2016 $self->emit (changed => $x); 2107 $value = $self->{inner_pad} + ($value - $page * 0.5);
2017 $self->update; 2108
2109 $value = 0 if $value < 0;
2110 $page = 1 - $value if $value + $page > 1;
2111
2112 $self->{knob_x} = $value;
2113 $self->{knob_w} = $page;
2018 } 2114 });
2019}
2020 2115
2021# the inner_* stuff is for generating a padding for the slider handle, 2116 $self->SUPER::update;
2022# so that the handle doesn't leave the texture. This calculation isn't 100%
2023# correct propably, but it does the job for now
2024sub _calc_inner_pad_px {
2025 my ($self, $w) = @_;
2026 ($w / 100) * $self->{inner_pad} # % to pixels
2027} 2117}
2028 2118
2029sub _draw { 2119sub _draw {
2030 my ($self) = @_; 2120 my ($self) = @_;
2031 2121
2032 $self->SUPER::_draw (); 2122 $self->SUPER::_draw ();
2033 2123
2034 my ($w, $h) = @$self{qw(w h)}; 2124 glScale $self->{w}, $self->{h};
2035 2125
2036 if ($self->{vertical}) { 2126 if ($self->{vertical}) {
2037 # draw a vertical slider like a rotated horizontal slider 2127 # draw a vertical slider like a rotated horizontal slider
2038 2128
2039 glRotate 90, 0, 0, 1; 2129 glRotate 90, 0, 0, 1;
2040 glTranslate 0, -$self->{w}, 0; 2130 glTranslate 0, 1, 0;
2041
2042 ($w, $h) = ($h, $w);
2043 } 2131 }
2044 2132
2045 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2133 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2046 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2134 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2047 2135
2048 my ($value, $lo, $hi, $page) = @{$self->{range}};
2049
2050 $hi = $value + 1 if $lo == $hi;
2051
2052 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
2053 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2054
2055 $page = int $page * $inner_w / ($hi - $lo);
2056 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
2057
2058 $w -= $page;
2059 $page &= ~1;
2060 glTranslate $page * 0.5, 0, 0;
2061 $page ||= 2;
2062
2063 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2064 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2065
2066 glEnable GL_TEXTURE_2D; 2136 glEnable GL_TEXTURE_2D;
2067 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2137 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2068 2138
2069 # draw background 2139 # draw background
2070 $tex[1]->draw_quad_alpha (0, 0, $w, $h); 2140 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2071 2141
2072 # draw handle 2142 # draw handle
2073 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h); 2143 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
2074 2144
2075 glDisable GL_TEXTURE_2D; 2145 glDisable GL_TEXTURE_2D;
2076} 2146}
2077 2147
2078############################################################################# 2148#############################################################################
2435 2505
2436 my $desc = $item->{nrof} < 2 2506 my $desc = $item->{nrof} < 2
2437 ? $item->{name} 2507 ? $item->{name}
2438 : "$item->{nrof} $item->{name_pl}"; 2508 : "$item->{nrof} $item->{name_pl}";
2439 2509
2510 $item->{flags} & Crossfire::Protocol::F_OPEN
2511 and $desc .= " (open)";
2512 $item->{flags} & Crossfire::Protocol::F_APPLIED
2513 and $desc .= " (applied)";
2514 $item->{flags} & Crossfire::Protocol::F_UNPAID
2515 and $desc .= " (unpaid)";
2516 $item->{flags} & Crossfire::Protocol::F_MAGIC
2517 and $desc .= " (magic)";
2518 $item->{flags} & Crossfire::Protocol::F_CURSED
2519 and $desc .= " (cursed)";
2520 $item->{flags} & Crossfire::Protocol::F_DAMNED
2521 and $desc .= " (damned)";
2522 $item->{flags} & Crossfire::Protocol::F_LOCKED
2523 and $desc .= " *";
2440 2524
2441 my $self = $class->SUPER::new ( 2525 my $self = $class->SUPER::new (
2442 can_hover => 1, 2526 can_hover => 1,
2443 can_events => 1, 2527 can_events => 1,
2444 tooltip => (CFClient::UI::Label->escape ($desc) 2528 tooltip => (CFClient::UI::Label->escape ($desc)
2450 if ($ev->{button} == 1) { 2534 if ($ev->{button} == 1) {
2451 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); 2535 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2452 } elsif ($ev->{button} == 2) { 2536 } elsif ($ev->{button} == 2) {
2453 $::CONN->send ("apply $item->{tag}"); 2537 $::CONN->send ("apply $item->{tag}");
2454 } elsif ($ev->{button} == 3) { 2538 } elsif ($ev->{button} == 3) {
2455 CFClient::UI::Menu->new ( 2539 my @menu_items = (
2456 items => [
2457 ["examine", sub { $::CONN->send ("examine $item->{tag}") }], 2540 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2541 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2542 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2543 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2458 [ 2544 [
2459 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", 2545 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2460 sub { $::CONN->send ("lock $item->{tag}") }, 2546 sub { $::CONN->send ("lock ". () ."$item->{tag}") },
2461 ],
2462 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2463 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2464 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2465 ], 2547 ],
2466 )->popup ($ev); 2548 );
2549
2550 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2467 } 2551 }
2468 2552
2469 1 2553 1
2470 }, 2554 },
2471 %args 2555 %args
2507 my ($self, $items) = @_; 2591 my ($self, $items) = @_;
2508 2592
2509 $self->{scrolled}->clear; 2593 $self->{scrolled}->clear;
2510 return unless $items; 2594 return unless $items;
2511 2595
2512 my @items = sort { $a->{type} <=> $b->{type} } @$items; 2596 my @items = sort {
2597 ($a->{type} <=> $b->{type})
2598 or ($a->{name} cmp $b->{name})
2599 } @$items;
2513 2600
2514 $self->{real_items} = \@items; 2601 $self->{real_items} = \@items;
2515 2602
2516 for my $item (@items) { 2603 for my $item (@items) {
2517 my $desc = $item->{nrof} < 2 2604 my $desc = $item->{nrof} < 2
2518 ? $item->{name} 2605 ? $item->{name}
2519 : "$item->{nrof} $item->{name_pl}"; 2606 : "$item->{nrof} $item->{name_pl}";
2520 2607
2521 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); 2608 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2522 } 2609 }
2610
2611 $self->{scrolled}->add (@items);
2523 2612
2524# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2613# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2525} 2614}
2526 2615
2527sub size_request { 2616sub size_request {
2632 ? "<b>$item->{count} ×</b> $item->{text}" 2721 ? "<b>$item->{count} ×</b> $item->{text}"
2633 : $item->{text}; 2722 : $item->{text};
2634 2723
2635 for ($short) { 2724 for ($short) {
2636 s/^\s+//; 2725 s/^\s+//;
2637 s/\012.*/…/s; 2726 s/\s+/ /g;
2638 my $len = int 40 / $item->{fontsize}; 2727 my $len = int 40 / $item->{fontsize};
2639 substr $_, $len, length, "…" if $len < length; 2728 substr $_, $len, length, "…" if $len < length;
2640 } 2729 }
2641 2730
2642 new CFClient::UI::Label 2731 new CFClient::UI::Label
2643 markup => $short, 2732 markup => $short,
2644 tooltip => $item->{tooltip}, 2733 tooltip => $item->{tooltip},
2645 tooltip_font => $::FONT_PROP, 2734 tooltip_font => $::FONT_PROP,
2646 tooltip_width => 0.67, 2735 tooltip_width => 0.67,
2647 fontsize => $item->{fontsize}, 2736 fontsize => $item->{fontsize},
2648 color => $item->{color}, 2737 fg => $item->{fg},
2649 can_events => 1, 2738 can_events => 1,
2650 can_hover => 1 2739 can_hover => 1
2651 }; 2740 };
2652 } 2741 }
2653 2742
2680 id => ++$self->{id}, 2769 id => ++$self->{id},
2681 text => $text, 2770 text => $text,
2682 timeout => $timeout, 2771 timeout => $timeout,
2683 tooltip => $text, 2772 tooltip => $text,
2684 fontsize => 0.8, 2773 fontsize => 0.8,
2685 color => [0.8, 0.8, 0.8, 0.8], 2774 fg => [0.8, 0.8, 0.8, 0.8],
2686 pri => 0, 2775 pri => 0,
2687 count => 1, 2776 count => 1,
2688 %arg, 2777 %arg,
2689 }; 2778 };
2690 } 2779 }
2729} 2818}
2730 2819
2731sub size_allocate { 2820sub size_allocate {
2732 my ($self, $w, $h) = @_; 2821 my ($self, $w, $h) = @_;
2733 2822
2734 my $old_w = $self->{old_w}; 2823 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2735 my $old_h = $self->{old_h}; 2824 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2736 2825
2737 if ($old_w && $old_h) { 2826 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2738 for my $child ($self->children) { 2827 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2739 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2740 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2741 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2742 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2743 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2744 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2745 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2746 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2747 }
2748 }
2749 2828
2750 for my $child ($self->children) { 2829 for my $child ($self->children) {
2751 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2830 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2752 2831
2832 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
2833 if exists $child->{req_x};
2834
2835 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
2836 if exists $child->{req_y};
2837
2753 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2838 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2754 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2839 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
2840
2755 $child->configure ($X, $Y, $W, $H); 2841 $child->configure ($X, $Y, $W, $H);
2756 } 2842 }
2757
2758 $self->{old_w} = $w;
2759 $self->{old_h} = $h;
2760} 2843}
2761 2844
2762sub coord2local { 2845sub coord2local {
2763 my ($self, $x, $y) = @_; 2846 my ($self, $x, $y) = @_;
2764 2847
2777 $self->check_size; 2860 $self->check_size;
2778 $::WANT_REFRESH++; 2861 $::WANT_REFRESH++;
2779} 2862}
2780 2863
2781sub add { 2864sub add {
2782 my ($self, $child) = @_; 2865 my ($self, @children) = @_;
2783 2866
2867 for my $child (@children) {
2868 $child->{toplevel} = 1;
2869
2784 # integerise window positions 2870 # integerise window positions
2785 $child->{x} = int $child->{x}; 2871 $child->{x} = int $child->{x};
2786 $child->{y} = int $child->{y}; 2872 $child->{y} = int $child->{y};
2873 }
2787 2874
2788 $self->SUPER::add ($child); 2875 $self->SUPER::add (@children);
2789} 2876}
2790 2877
2791sub on_refresh { 2878sub on_refresh {
2792 my ($self, $id, $cb) = @_; 2879 my ($self, $id, $cb) = @_;
2793 2880

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines