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.197 by root, Fri May 12 01:47:04 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
270sub reconfigure {
271 my ($self) = @_;
272
273 $self->check_size (1);
274 $self->update;
275}
276
247sub children { 277sub children {
248}
249
250# call when resolution changes etc.
251sub reconfigure {
252 my ($self) = @_;
253
254 $_->reconfigure
255 for $self->children;
256
257 $self->check_size;
258 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $self->{w}, $self->{h}];
259 $self->update;
260} 278}
261 279
262sub set_max_size { 280sub set_max_size {
263 my ($self, $w, $h) = @_; 281 my ($self, $w, $h) = @_;
264 282
357 glVertex $self->{w}, 0; 375 glVertex $self->{w}, 0;
358 glVertex $self->{w}, $self->{h}; 376 glVertex $self->{w}, $self->{h};
359 glVertex 0 , $self->{h}; 377 glVertex 0 , $self->{h};
360 glEnd; 378 glEnd;
361 glPopMatrix; 379 glPopMatrix;
362 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;
363 } 381 }
364} 382}
365 383
366sub _draw { 384sub _draw {
367 my ($self) = @_; 385 my ($self) = @_;
384sub set_parent { 402sub set_parent {
385 my ($self, $parent) = @_; 403 my ($self, $parent) = @_;
386 404
387 Scalar::Util::weaken ($self->{parent} = $parent); 405 Scalar::Util::weaken ($self->{parent} = $parent);
388 406
407 # TODO: req_w _does_change after ->reconfigure
389 $self->check_size 408 $self->check_size
390 unless exists $self->{req_w}; 409 unless exists $self->{req_w};
391} 410}
392 411
393sub check_size { 412sub check_size {
417} 436}
418 437
419sub DESTROY { 438sub DESTROY {
420 my ($self) = @_; 439 my ($self) = @_;
421 440
441 delete $WIDGET{$self+0};
422 #$self->deactivate; 442 #$self->deactivate;
423} 443}
424 444
425############################################################################# 445#############################################################################
426 446
628} 648}
629 649
630sub update { 650sub update {
631 my ($self) = @_; 651 my ($self) = @_;
632 652
633 $ROOT->on_refresh ($self => sub { $self->render_child }); 653 $ROOT->on_post_alloc ($self => sub { $self->render_child });
634 $self->SUPER::update; 654 $self->SUPER::update;
635} 655}
636 656
637sub size_allocate { 657sub size_allocate {
638 my ($self, $w, $h) = @_; 658 my ($self, $w, $h) = @_;
776 $self->add ($self->{slider}); 796 $self->add ($self->{slider});
777 797
778 $self 798 $self
779} 799}
780 800
781#TODO# update range on size_allocate depeneing on child 801#TODO# update range on size_allocate depending on child
782# update viewport offset on scroll 802# update viewport offset on scroll
783 803
784############################################################################# 804#############################################################################
785 805
786package CFClient::UI::Frame; 806package CFClient::UI::Frame;
787 807
788our @ISA = CFClient::UI::Bin::; 808our @ISA = CFClient::UI::Bin::;
789 809
790use 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}
791 843
792############################################################################# 844#############################################################################
793 845
794package CFClient::UI::FancyFrame; 846package CFClient::UI::FancyFrame;
795 847
808 860
809 my $self = $class->SUPER::new ( 861 my $self = $class->SUPER::new (
810 bg => [1, 1, 1, 1], 862 bg => [1, 1, 1, 1],
811 border_bg => [1, 1, 1, 1], 863 border_bg => [1, 1, 1, 1],
812 border => 0.6, 864 border => 0.6,
865 toplevel => 1,
813 can_events => 1, 866 can_events => 1,
814 @_ 867 @_
815 ); 868 );
816 869
817 $self->{title} &&= new CFClient::UI::Label 870 $self->{title} &&= new CFClient::UI::Label
842 my ($self, $w, $h) = @_; 895 my ($self, $w, $h) = @_;
843 896
844 $h -= List::Util::max 0, $self->border * 2; 897 $h -= List::Util::max 0, $self->border * 2;
845 $w -= List::Util::max 0, $self->border * 2; 898 $w -= List::Util::max 0, $self->border * 2;
846 899
847 $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)
848 if $self->{title}; 901 if $self->{title};
849 902
850 $self->child->configure ($self->border, $self->border, $w, $h); 903 $self->child->configure ($self->border, $self->border, $w, $h);
851} 904}
852 905
1346 $self->{layout}->set_width ($self->{w}); 1399 $self->{layout}->set_width ($self->{w});
1347 $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);
1348 1401
1349 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1402 my $tex = new_from_layout CFClient::Texture $self->{layout};
1350 1403
1351 $self->{ox} = int $self->{align} < 0 ? $self->{padding} 1404 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1352 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1405 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1353 : ($self->{w} - $tex->{w}) * 0.5; 1406 : ($self->{w} - $tex->{w}) * 0.5);
1354 1407
1355 $self->{oy} = int $self->{valign} < 0 ? $self->{padding} 1408 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1356 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1409 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1357 : ($self->{h} - $tex->{h}) * 0.5; 1410 : ($self->{h} - $tex->{h}) * 0.5);
1358 1411
1359 $tex 1412 $tex
1360 }; 1413 };
1361 1414
1362 glEnable GL_TEXTURE_2D; 1415 glEnable GL_TEXTURE_2D;
1417sub set_text { 1470sub set_text {
1418 my ($self, $text) = @_; 1471 my ($self, $text) = @_;
1419 1472
1420 $self->{cursor} = length $text; 1473 $self->{cursor} = length $text;
1421 $self->_set_text ($text); 1474 $self->_set_text ($text);
1475 $self->update;
1422 $self->check_size; 1476 $self->check_size;
1423 $self->update;
1424} 1477}
1425 1478
1426sub get_text { 1479sub get_text {
1427 $_[0]{text} 1480 $_[0]{text}
1428} 1481}
1448 my $sym = $ev->{sym}; 1501 my $sym = $ev->{sym};
1449 my $uni = $ev->{unicode}; 1502 my $uni = $ev->{unicode};
1450 1503
1451 my $text = $self->get_text; 1504 my $text = $self->get_text;
1452 1505
1453 if ($sym == 8) { 1506 if ($uni == 8) {
1454 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1507 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1455 } elsif ($sym == 127) { 1508 } elsif ($uni == 127) {
1456 substr $text, $self->{cursor}, 1, ""; 1509 substr $text, $self->{cursor}, 1, "";
1457 } elsif ($sym == CFClient::SDLK_LEFT) { 1510 } elsif ($sym == CFClient::SDLK_LEFT) {
1458 --$self->{cursor} if $self->{cursor}; 1511 --$self->{cursor} if $self->{cursor};
1459 } elsif ($sym == CFClient::SDLK_RIGHT) { 1512 } elsif ($sym == CFClient::SDLK_RIGHT) {
1460 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1513 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1461 } elsif ($sym == CFClient::SDLK_HOME) { 1514 } elsif ($sym == CFClient::SDLK_HOME) {
1462 $self->{cursor} = 0; 1515 $self->{cursor} = 0;
1463 } elsif ($sym == CFClient::SDLK_END) { 1516 } elsif ($sym == CFClient::SDLK_END) {
1464 $self->{cursor} = length $text; 1517 $self->{cursor} = length $text;
1465 } elsif ($sym == 27) { 1518 } elsif ($uni == 27) {
1466 $self->emit ('escape'); 1519 $self->emit ('escape');
1467 } elsif ($uni) { 1520 } elsif ($uni) {
1468 substr $text, $self->{cursor}++, 0, chr $uni; 1521 substr $text, $self->{cursor}++, 0, chr $uni;
1469 } 1522 }
1470 1523
1471 $self->_set_text ($text); 1524 $self->_set_text ($text);
1472 $self->update; 1525 $self->update;
1526 $self->check_size;
1473} 1527}
1474 1528
1475sub focus_in { 1529sub focus_in {
1476 my ($self) = @_; 1530 my ($self) = @_;
1477 1531
1957 qw(s1_slider.png s1_slider_bg.png); 2011 qw(s1_slider.png s1_slider_bg.png);
1958 2012
1959sub new { 2013sub new {
1960 my $class = shift; 2014 my $class = shift;
1961 2015
1962 # range [value, low, high, page] 2016 # range [value, low, high, page, unit]
1963 2017
1964 # TODO: 0-width page 2018 # TODO: 0-width page
1965 # TODO: req_w/h are wrong with vertical 2019 # TODO: req_w/h are wrong with vertical
1966 # TODO: calculations are off 2020 # TODO: calculations are off
1967 my $self = $class->SUPER::new ( 2021 my $self = $class->SUPER::new (
1970 range => [0, 0, 100, 10], 2024 range => [0, 0, 100, 10],
1971 req_w => $::WIDTH / 80, 2025 req_w => $::WIDTH / 80,
1972 req_h => $::WIDTH / 80, 2026 req_h => $::WIDTH / 80,
1973 vertical => 0, 2027 vertical => 0,
1974 can_hover => 1, 2028 can_hover => 1,
1975 inner_pad => 5, 2029 inner_pad => .05,
1976 @_ 2030 @_
1977 ); 2031 );
1978 2032
2033 $self->set_value ($self->{range}[0]);
2034 $self->update;
2035
1979 $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 }
1980} 2060}
1981 2061
1982sub size_request { 2062sub size_request {
1983 my ($self) = @_; 2063 my ($self) = @_;
1984 2064
1997 2077
1998sub mouse_motion { 2078sub mouse_motion {
1999 my ($self, $ev, $x, $y) = @_; 2079 my ($self, $ev, $x, $y) = @_;
2000 2080
2001 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
2002 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2098 my ($value, $lo, $hi, $page) = @{$self->{range}};
2003 2099
2004 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2100 my $inner_w = 1 - 2 * $self->{inner_pad};
2005 2101
2006 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2102 $self->{scale} = ($inner_w / ($hi - $lo)) || 1;
2007 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2008 2103
2009 $x -= $inner_pad_px; # substract the padding 2104 $page = $self->{scale} * $page || 10 / ($self->{w} || 1);
2010 $x = $x * ($hi - $lo) / $inner_w + $lo; 2105 $value = $self->{scale} * ($value - $lo);
2011 $x = $lo if $x < $lo;
2012 $x = $hi - $page if $x > $hi - $page;
2013 $self->{range}[0] = $x;
2014 2106
2015 $self->emit (changed => $x); 2107 $value = $self->{inner_pad} + ($value - $page * 0.5);
2016 $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;
2017 } 2114 });
2018}
2019 2115
2020# the inner_* stuff is for generating a padding for the slider handle, 2116 $self->SUPER::update;
2021# so that the handle doesn't leave the texture. This calculation isn't 100%
2022# correct propably, but it does the job for now
2023sub _calc_inner_pad_px {
2024 my ($self, $w) = @_;
2025 ($w / 100) * $self->{inner_pad} # % to pixels
2026} 2117}
2027 2118
2028sub _draw { 2119sub _draw {
2029 my ($self) = @_; 2120 my ($self) = @_;
2030 2121
2031 $self->SUPER::_draw (); 2122 $self->SUPER::_draw ();
2032 2123
2033 my ($w, $h) = @$self{qw(w h)}; 2124 glScale $self->{w}, $self->{h};
2034 2125
2035 if ($self->{vertical}) { 2126 if ($self->{vertical}) {
2036 # draw a vertical slider like a rotated horizontal slider 2127 # draw a vertical slider like a rotated horizontal slider
2037 2128
2038 glRotate 90, 0, 0, 1; 2129 glRotate 90, 0, 0, 1;
2039 glTranslate 0, -$self->{w}, 0; 2130 glTranslate 0, 1, 0;
2040
2041 ($w, $h) = ($h, $w);
2042 } 2131 }
2043 2132
2044 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2133 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2045 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2134 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2046 2135
2047 my ($value, $lo, $hi, $page) = @{$self->{range}};
2048
2049 $hi = $value + 1 if $lo == $hi;
2050
2051 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
2052 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2053
2054 $page = int $page * $inner_w / ($hi - $lo);
2055 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
2056
2057 $w -= $page;
2058 $page &= ~1;
2059 glTranslate $page * 0.5, 0, 0;
2060 $page ||= 2;
2061
2062 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2063 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2064
2065 glEnable GL_TEXTURE_2D; 2136 glEnable GL_TEXTURE_2D;
2066 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2137 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2067 2138
2068 # draw background 2139 # draw background
2069 $tex[1]->draw_quad_alpha (0, 0, $w, $h); 2140 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2070 2141
2071 # draw handle 2142 # draw handle
2072 $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);
2073 2144
2074 glDisable GL_TEXTURE_2D; 2145 glDisable GL_TEXTURE_2D;
2075} 2146}
2076 2147
2077############################################################################# 2148#############################################################################
2165 2236
2166 return unless $self->{h} > 0; 2237 return unless $self->{h} > 0;
2167 2238
2168 delete $self->{texture}; 2239 delete $self->{texture};
2169 2240
2170 $ROOT->on_refresh ($self, sub { 2241 $ROOT->on_post_alloc ($self, sub {
2171 if (delete $self->{need_reflow}) { 2242 if (delete $self->{need_reflow}) {
2172 my $height = 0; 2243 my $height = 0;
2173 2244
2174 $height += $_->[0] = $self->text_height ($_->[2]) 2245 $height += $_->[0] = $self->text_height ($_->[2])
2175 for @{$self->{par}}; 2246 for @{$self->{par}};
2434 2505
2435 my $desc = $item->{nrof} < 2 2506 my $desc = $item->{nrof} < 2
2436 ? $item->{name} 2507 ? $item->{name}
2437 : "$item->{nrof} $item->{name_pl}"; 2508 : "$item->{nrof} $item->{name_pl}";
2438 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 .= " *";
2439 2524
2440 my $self = $class->SUPER::new ( 2525 my $self = $class->SUPER::new (
2441 can_hover => 1, 2526 can_hover => 1,
2442 can_events => 1, 2527 can_events => 1,
2443 tooltip => (CFClient::UI::Label->escape ($desc) 2528 tooltip => (CFClient::UI::Label->escape ($desc)
2449 if ($ev->{button} == 1) { 2534 if ($ev->{button} == 1) {
2450 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); 2535 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2451 } elsif ($ev->{button} == 2) { 2536 } elsif ($ev->{button} == 2) {
2452 $::CONN->send ("apply $item->{tag}"); 2537 $::CONN->send ("apply $item->{tag}");
2453 } elsif ($ev->{button} == 3) { 2538 } elsif ($ev->{button} == 3) {
2454 CFClient::UI::Menu->new ( 2539 my @menu_items = (
2455 items => [
2456 ["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") }],
2457 [ 2544 [
2458 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", 2545 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2459 sub { $::CONN->send ("lock $item->{tag}") }, 2546 sub { $::CONN->send ("lock ". () ."$item->{tag}") },
2460 ],
2461 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2462 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2463 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2464 ], 2547 ],
2465 )->popup ($ev); 2548 );
2549
2550 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2466 } 2551 }
2467 2552
2468 1 2553 1
2469 }, 2554 },
2470 %args 2555 %args
2506 my ($self, $items) = @_; 2591 my ($self, $items) = @_;
2507 2592
2508 $self->{scrolled}->clear; 2593 $self->{scrolled}->clear;
2509 return unless $items; 2594 return unless $items;
2510 2595
2511 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;
2512 2600
2513 $self->{real_items} = \@items; 2601 $self->{real_items} = \@items;
2514 2602
2515 for my $item (@items) { 2603 for my $item (@items) {
2516 my $desc = $item->{nrof} < 2 2604 my $desc = $item->{nrof} < 2
2517 ? $item->{name} 2605 ? $item->{name}
2518 : "$item->{nrof} $item->{name_pl}"; 2606 : "$item->{nrof} $item->{name_pl}";
2519 2607
2520 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); 2608 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2521 } 2609 }
2610
2611 $self->{scrolled}->add (@items);
2522 2612
2523# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2613# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2524} 2614}
2525 2615
2526sub size_request { 2616sub size_request {
2631 ? "<b>$item->{count} ×</b> $item->{text}" 2721 ? "<b>$item->{count} ×</b> $item->{text}"
2632 : $item->{text}; 2722 : $item->{text};
2633 2723
2634 for ($short) { 2724 for ($short) {
2635 s/^\s+//; 2725 s/^\s+//;
2636 s/\012.*/…/s; 2726 s/\s+/ /g;
2637 my $len = int 30 / $item->{fontsize}; 2727 my $len = int 40 / $item->{fontsize};
2638 substr $_, $len, length, "…" if $len < length; 2728 substr $_, $len, length, "…" if $len < length;
2639 } 2729 }
2640 2730
2641 new CFClient::UI::Label 2731 new CFClient::UI::Label
2642 markup => $short, 2732 markup => $short,
2643 tooltip => $item->{tooltip}, 2733 tooltip => $item->{tooltip},
2644 tooltip_font => $::FONT_PROP, 2734 tooltip_font => $::FONT_PROP,
2645 tooltip_width => 0.67, 2735 tooltip_width => 0.67,
2646 fontsize => $item->{fontsize}, 2736 fontsize => $item->{fontsize},
2647 color => $item->{color}, 2737 fg => $item->{fg},
2648 can_events => 1, 2738 can_events => 1,
2649 can_hover => 1 2739 can_hover => 1
2650 }; 2740 };
2651 } 2741 }
2652 2742
2654 $self->SUPER::add (reverse @widgets); 2744 $self->SUPER::add (reverse @widgets);
2655} 2745}
2656 2746
2657sub add { 2747sub add {
2658 my ($self, $text, %arg) = @_; 2748 my ($self, $text, %arg) = @_;
2749
2750 $text =~ s/^\s+//;
2751 $text =~ s/\s+$//;
2659 2752
2660 my $timeout = time + ((delete $arg{timeout}) || 60); 2753 my $timeout = time + ((delete $arg{timeout}) || 60);
2661 2754
2662 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 2755 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2663 2756
2666 $item->{count}++; 2759 $item->{count}++;
2667 } else { 2760 } else {
2668 $item->{count} = 1; 2761 $item->{count} = 1;
2669 $item->{text} = $item->{tooltip} = $text; 2762 $item->{text} = $item->{tooltip} = $text;
2670 } 2763 }
2764 $item->{id} = ++$self->{id};
2671 $item->{timeout} = $timeout; 2765 $item->{timeout} = $timeout;
2672 delete $item->{label}; 2766 delete $item->{label};
2673 } else { 2767 } else {
2674 $self->{item}{$group} = { 2768 $self->{item}{$group} = {
2675 id => ++$self->{id}, 2769 id => ++$self->{id},
2676 text => $text, 2770 text => $text,
2677 timeout => $timeout, 2771 timeout => $timeout,
2678 tooltip => $text, 2772 tooltip => $text,
2679 fontsize => 0.8, 2773 fontsize => 0.8,
2680 color => [0.8, 0.8, 0.8, 0.8], 2774 fg => [0.8, 0.8, 0.8, 0.8],
2681 pri => 0, 2775 pri => 0,
2682 count => 1, 2776 count => 1,
2683 %arg, 2777 %arg,
2684 }; 2778 };
2685 } 2779 }
2724} 2818}
2725 2819
2726sub size_allocate { 2820sub size_allocate {
2727 my ($self, $w, $h) = @_; 2821 my ($self, $w, $h) = @_;
2728 2822
2729 my $old_w = $self->{old_w}; 2823 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2730 my $old_h = $self->{old_h}; 2824 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2731 2825
2732 if ($old_w && $old_h) { 2826 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2733 for my $child ($self->children) { 2827 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2734 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2735 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2736 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2737 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2738 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2739 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2740 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2741 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2742 }
2743 }
2744 2828
2745 for my $child ($self->children) { 2829 for my $child ($self->children) {
2746 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)};
2747 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
2748 $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;
2749 $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
2750 $child->configure ($X, $Y, $W, $H); 2841 $child->configure ($X, $Y, $W, $H);
2751 } 2842 }
2752
2753 $self->{old_w} = $w;
2754 $self->{old_h} = $h;
2755} 2843}
2756 2844
2757sub coord2local { 2845sub coord2local {
2758 my ($self, $x, $y) = @_; 2846 my ($self, $x, $y) = @_;
2759 2847
2772 $self->check_size; 2860 $self->check_size;
2773 $::WANT_REFRESH++; 2861 $::WANT_REFRESH++;
2774} 2862}
2775 2863
2776sub add { 2864sub add {
2777 my ($self, $child) = @_; 2865 my ($self, @children) = @_;
2778 2866
2867 for my $child (@children) {
2868 $child->{toplevel} = 1;
2869
2779 # integerise window positions 2870 # integerise window positions
2780 $child->{x} = int $child->{x}; 2871 $child->{x} = int $child->{x};
2781 $child->{y} = int $child->{y}; 2872 $child->{y} = int $child->{y};
2873 }
2782 2874
2783 $self->SUPER::add ($child); 2875 $self->SUPER::add (@children);
2784} 2876}
2785 2877
2786sub on_refresh { 2878sub on_refresh {
2787 my ($self, $id, $cb) = @_; 2879 my ($self, $id, $cb) = @_;
2788 2880
2789 $self->{refresh_hook}{$id} = $cb; 2881 $self->{refresh_hook}{$id} = $cb;
2790} 2882}
2791 2883
2884sub on_post_alloc {
2885 my ($self, $id, $cb) = @_;
2886
2887 $self->{post_alloc_hook}{$id} = $cb;
2888}
2889
2792sub draw { 2890sub draw {
2793 my ($self) = @_; 2891 my ($self) = @_;
2892
2893 while ($self->{refresh_hook}) {
2894 $_->()
2895 for values %{delete $self->{refresh_hook}};
2896 }
2794 2897
2795 if ($self->{check_size}) { 2898 if ($self->{check_size}) {
2796 my @queue = ([], []); 2899 my @queue = ([], []);
2797 2900
2798 for (;;) { 2901 for (;;) {
2835 $widget->size_allocate ($w, $h); 2938 $widget->size_allocate ($w, $h);
2836 $widget->emit (size_allocate => $w, $h); 2939 $widget->emit (size_allocate => $w, $h);
2837 } 2940 }
2838 } 2941 }
2839 2942
2840 while ($self->{refresh_hook}) { 2943 while ($self->{post_alloc_hook}) {
2841 $_->() 2944 $_->()
2842 for values %{delete $self->{refresh_hook}}; 2945 for values %{delete $self->{post_alloc_hook}};
2843 } 2946 }
2844 2947
2845 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2948 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2846 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2949 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2847 glClear GL_COLOR_BUFFER_BIT; 2950 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines