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.200 by root, Sat May 13 00:27:09 2006 UTC vs.
Revision 1.212 by elmex, Wed May 17 14:55:14 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;
259} 275}
260 276
277sub children {
278}
279
261sub set_max_size { 280sub set_max_size {
262 my ($self, $w, $h) = @_; 281 my ($self, $w, $h) = @_;
263 282
264 delete $self->{max_w}; $self->{max_w} = $w if $w; 283 delete $self->{max_w}; $self->{max_w} = $w if $w;
265 delete $self->{max_h}; $self->{max_h} = $h if $h; 284 delete $self->{max_h}; $self->{max_h} = $h if $h;
285}
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 }
266} 296}
267 297
268# translate global coordinates to local coordinate system 298# translate global coordinates to local coordinate system
269sub coord2local { 299sub coord2local {
270 my ($self, $x, $y) = @_; 300 my ($self, $x, $y) = @_;
356 glVertex $self->{w}, 0; 386 glVertex $self->{w}, 0;
357 glVertex $self->{w}, $self->{h}; 387 glVertex $self->{w}, $self->{h};
358 glVertex 0 , $self->{h}; 388 glVertex 0 , $self->{h};
359 glEnd; 389 glEnd;
360 glPopMatrix; 390 glPopMatrix;
361 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;
362 } 392 }
363} 393}
364 394
365sub _draw { 395sub _draw {
366 my ($self) = @_; 396 my ($self) = @_;
417} 447}
418 448
419sub DESTROY { 449sub DESTROY {
420 my ($self) = @_; 450 my ($self) = @_;
421 451
452 delete $WIDGET{$self+0};
422 #$self->deactivate; 453 #$self->deactivate;
423} 454}
424 455
425############################################################################# 456#############################################################################
426 457
435 my $class = shift; 466 my $class = shift;
436 467
437 # range [value, low, high, page] 468 # range [value, low, high, page]
438 469
439 $class->SUPER::new ( 470 $class->SUPER::new (
440 bg => [0, 0, 0, 0.2], 471 #bg => [0, 0, 0, 0.2],
441 active_bg => [1, 1, 1, 0.5], 472 #active_bg => [1, 1, 1, 0.5],
442 @_ 473 @_
443 ) 474 )
444} 475}
445 476
446sub _draw { 477sub _draw {
447 my ($self) = @_; 478 my ($self) = @_;
448 479
480 my $color = $FOCUS == $self && $self->{active_bg}
481 ? $self->{active_bg}
482 : $self->{bg};
483
484 if ($color && (@$color < 4 || $color->[3])) {
449 my ($w, $h) = @$self{qw(w h)}; 485 my ($w, $h) = @$self{qw(w h)};
450 486
451 glEnable GL_BLEND; 487 glEnable GL_BLEND;
452 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 488 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
453 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 489 glColor @$color;
454 490
455 glBegin GL_QUADS; 491 glBegin GL_QUADS;
456 glVertex 0 , 0; 492 glVertex 0 , 0;
457 glVertex 0 , $h; 493 glVertex 0 , $h;
458 glVertex $w, $h; 494 glVertex $w, $h;
459 glVertex $w, 0; 495 glVertex $w, 0;
460 glEnd; 496 glEnd;
461 497
462 glDisable GL_BLEND; 498 glDisable GL_BLEND;
499 }
463} 500}
464 501
465############################################################################# 502#############################################################################
466 503
467package CFClient::UI::Empty; 504package CFClient::UI::Empty;
840 877
841 my $self = $class->SUPER::new ( 878 my $self = $class->SUPER::new (
842 bg => [1, 1, 1, 1], 879 bg => [1, 1, 1, 1],
843 border_bg => [1, 1, 1, 1], 880 border_bg => [1, 1, 1, 1],
844 border => 0.6, 881 border => 0.6,
882 toplevel => 1,
845 can_events => 1, 883 can_events => 1,
846 @_ 884 @_
847 ); 885 );
848 886
849 $self->{title} &&= new CFClient::UI::Label 887 $self->{title} &&= new CFClient::UI::Label
874 my ($self, $w, $h) = @_; 912 my ($self, $w, $h) = @_;
875 913
876 $h -= List::Util::max 0, $self->border * 2; 914 $h -= List::Util::max 0, $self->border * 2;
877 $w -= List::Util::max 0, $self->border * 2; 915 $w -= List::Util::max 0, $self->border * 2;
878 916
879 $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)
880 if $self->{title}; 918 if $self->{title};
881 919
882 $self->child->configure ($self->border, $self->border, $w, $h); 920 $self->child->configure ($self->border, $self->border, $w, $h);
883} 921}
884 922
1245 1283
1246############################################################################# 1284#############################################################################
1247 1285
1248package CFClient::UI::Label; 1286package CFClient::UI::Label;
1249 1287
1250our @ISA = CFClient::UI::Base::; 1288our @ISA = CFClient::UI::DrawBG::;
1251 1289
1252use CFClient::OpenGL; 1290use CFClient::OpenGL;
1253 1291
1254sub new { 1292sub new {
1255 my ($class, %arg) = @_; 1293 my ($class, %arg) = @_;
1256 1294
1257 my $self = $class->SUPER::new ( 1295 my $self = $class->SUPER::new (
1258 fg => [1, 1, 1], 1296 fg => [1, 1, 1],
1297 #bg => none
1298 #active_bg => none
1259 #font => default_font 1299 #font => default_font
1260 #text => initial text 1300 #text => initial text
1261 #markup => initial narkup 1301 #markup => initial narkup
1262 layout => (new CFClient::Layout), 1302 layout => (new CFClient::Layout),
1263 fontsize => 1, 1303 fontsize => 1,
1281 } 1321 }
1282 1322
1283 $self 1323 $self
1284} 1324}
1285 1325
1286sub escape { 1326sub escape($) {
1287 local $_ = $_[1]; 1327 local $_ = $_[0];
1288 1328
1289 s/&/&amp;/g; 1329 s/&/&amp;/g;
1290 s/>/&gt;/g; 1330 s/>/&gt;/g;
1291 s/</&lt;/g; 1331 s/</&lt;/g;
1292 1332
1293 $_[1] 1333 $_
1294} 1334}
1295 1335
1296sub update { 1336sub update {
1297 my ($self) = @_; 1337 my ($self) = @_;
1298 1338
1369 $self->check_size; 1409 $self->check_size;
1370} 1410}
1371 1411
1372sub _draw { 1412sub _draw {
1373 my ($self) = @_; 1413 my ($self) = @_;
1414
1415 $self->SUPER::_draw; # draw background, if applicable
1374 1416
1375 my $tex = $self->{texture} ||= do { 1417 my $tex = $self->{texture} ||= do {
1376 $self->{layout}->set_foreground (@{$self->{fg}}); 1418 $self->{layout}->set_foreground (@{$self->{fg}});
1377 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1419 $self->{layout}->set_font ($self->{font}) if $self->{font};
1378 $self->{layout}->set_width ($self->{w}); 1420 $self->{layout}->set_width ($self->{w});
1379 $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);
1380 1422
1381 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1423 my $tex = new_from_layout CFClient::Texture $self->{layout};
1382 1424
1383 $self->{ox} = int $self->{align} < 0 ? $self->{padding} 1425 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1384 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1426 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1385 : ($self->{w} - $tex->{w}) * 0.5; 1427 : ($self->{w} - $tex->{w}) * 0.5);
1386 1428
1387 $self->{oy} = int $self->{valign} < 0 ? $self->{padding} 1429 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1388 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1430 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1389 : ($self->{h} - $tex->{h}) * 0.5; 1431 : ($self->{h} - $tex->{h}) * 0.5);
1390 1432
1391 $tex 1433 $tex
1392 }; 1434 };
1393 1435
1394 glEnable GL_TEXTURE_2D; 1436 glEnable GL_TEXTURE_2D;
1449sub set_text { 1491sub set_text {
1450 my ($self, $text) = @_; 1492 my ($self, $text) = @_;
1451 1493
1452 $self->{cursor} = length $text; 1494 $self->{cursor} = length $text;
1453 $self->_set_text ($text); 1495 $self->_set_text ($text);
1496 $self->update;
1454 $self->check_size; 1497 $self->check_size;
1455 $self->update;
1456} 1498}
1457 1499
1458sub get_text { 1500sub get_text {
1459 $_[0]{text} 1501 $_[0]{text}
1460} 1502}
1500 substr $text, $self->{cursor}++, 0, chr $uni; 1542 substr $text, $self->{cursor}++, 0, chr $uni;
1501 } 1543 }
1502 1544
1503 $self->_set_text ($text); 1545 $self->_set_text ($text);
1504 $self->update; 1546 $self->update;
1547 $self->check_size;
1505} 1548}
1506 1549
1507sub focus_in { 1550sub focus_in {
1508 my ($self) = @_; 1551 my ($self) = @_;
1509 1552
1638 my $class = shift; 1681 my $class = shift;
1639 1682
1640 $class->SUPER::new ( 1683 $class->SUPER::new (
1641 padding => 4, 1684 padding => 4,
1642 fg => [1, 1, 1], 1685 fg => [1, 1, 1],
1643 bg => [1, 1, 1, 0.2],
1644 active_fg => [0, 0, 1], 1686 active_fg => [0, 0, 1],
1645 can_hover => 1, 1687 can_hover => 1,
1646 align => 0, 1688 align => 0,
1647 valign => 0, 1689 valign => 0,
1648 can_events => 1, 1690 can_events => 1,
1696 1738
1697 $class->SUPER::new ( 1739 $class->SUPER::new (
1698 padding => 2, 1740 padding => 2,
1699 fg => [1, 1, 1], 1741 fg => [1, 1, 1],
1700 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],
1701 state => 0, 1745 state => 0,
1702 can_hover => 1, 1746 can_hover => 1,
1703 @_ 1747 @_
1704 ) 1748 )
1705} 1749}
1989 qw(s1_slider.png s1_slider_bg.png); 2033 qw(s1_slider.png s1_slider_bg.png);
1990 2034
1991sub new { 2035sub new {
1992 my $class = shift; 2036 my $class = shift;
1993 2037
1994 # range [value, low, high, page] 2038 # range [value, low, high, page, unit]
1995 2039
1996 # TODO: 0-width page 2040 # TODO: 0-width page
1997 # TODO: req_w/h are wrong with vertical 2041 # TODO: req_w/h are wrong with vertical
1998 # TODO: calculations are off 2042 # TODO: calculations are off
1999 my $self = $class->SUPER::new ( 2043 my $self = $class->SUPER::new (
2000 fg => [1, 1, 1], 2044 fg => [1, 1, 1],
2001 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],
2002 range => [0, 0, 100, 10], 2048 range => [0, 0, 100, 10],
2003 req_w => $::WIDTH / 80, 2049 req_w => $::WIDTH / 80,
2004 req_h => $::WIDTH / 80, 2050 req_h => $::WIDTH / 80,
2005 vertical => 0, 2051 vertical => 0,
2006 can_hover => 1, 2052 can_hover => 1,
2007 inner_pad => 5, 2053 inner_pad => .05,
2008 @_ 2054 @_
2009 ); 2055 );
2010 2056
2057 $self->set_value ($self->{range}[0]);
2058 $self->update;
2059
2011 $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 }
2012} 2084}
2013 2085
2014sub size_request { 2086sub size_request {
2015 my ($self) = @_; 2087 my ($self) = @_;
2016 2088
2029 2101
2030sub mouse_motion { 2102sub mouse_motion {
2031 my ($self, $ev, $x, $y) = @_; 2103 my ($self, $ev, $x, $y) = @_;
2032 2104
2033 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
2034 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2122 my ($value, $lo, $hi, $page) = @{$self->{range}};
2035 2123
2036 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2124 my $inner_w = 1 - 2 * $self->{inner_pad};
2037 2125
2038 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2126 $self->{scale} = ($inner_w / ($hi - $lo)) || 1;
2039 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2040 2127
2041 $x -= $inner_pad_px; # substract the padding 2128 $page = $self->{scale} * $page || 10 / ($self->{w} || 1);
2042 $x = $x * ($hi - $lo) / $inner_w + $lo; 2129 $value = $self->{scale} * ($value - $lo);
2043 $x = $lo if $x < $lo;
2044 $x = $hi - $page if $x > $hi - $page;
2045 $self->{range}[0] = $x;
2046 2130
2047 $self->emit (changed => $x); 2131 $value = $self->{inner_pad} + ($value - $page * 0.5);
2048 $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;
2049 } 2138 });
2050}
2051 2139
2052# the inner_* stuff is for generating a padding for the slider handle, 2140 $self->SUPER::update;
2053# so that the handle doesn't leave the texture. This calculation isn't 100%
2054# correct propably, but it does the job for now
2055sub _calc_inner_pad_px {
2056 my ($self, $w) = @_;
2057 ($w / 100) * $self->{inner_pad} # % to pixels
2058} 2141}
2059 2142
2060sub _draw { 2143sub _draw {
2061 my ($self) = @_; 2144 my ($self) = @_;
2062 2145
2063 $self->SUPER::_draw (); 2146 $self->SUPER::_draw ();
2064 2147
2065 my ($w, $h) = @$self{qw(w h)}; 2148 glScale $self->{w}, $self->{h};
2066 2149
2067 if ($self->{vertical}) { 2150 if ($self->{vertical}) {
2068 # draw a vertical slider like a rotated horizontal slider 2151 # draw a vertical slider like a rotated horizontal slider
2069 2152
2070 glRotate 90, 0, 0, 1; 2153 glRotate 90, 0, 0, 1;
2071 glTranslate 0, -$self->{w}, 0; 2154 glTranslate 0, 1, 0;
2072
2073 ($w, $h) = ($h, $w);
2074 } 2155 }
2075 2156
2076 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2157 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2077 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2158 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2078 2159
2079 my ($value, $lo, $hi, $page) = @{$self->{range}};
2080
2081 $hi = $value + 1 if $lo == $hi;
2082
2083 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
2084 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2085
2086 $page = int $page * $inner_w / ($hi - $lo);
2087 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
2088
2089 $w -= $page;
2090 $page &= ~1;
2091 glTranslate $page * 0.5, 0, 0;
2092 $page ||= 2;
2093
2094 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2095 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2096
2097 glEnable GL_TEXTURE_2D; 2160 glEnable GL_TEXTURE_2D;
2098 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2161 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2099 2162
2100 # draw background 2163 # draw background
2101 $tex[1]->draw_quad_alpha (0, 0, $w, $h); 2164 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2102 2165
2103 # draw handle 2166 # draw handle
2104 $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);
2105 2168
2106 glDisable GL_TEXTURE_2D; 2169 glDisable GL_TEXTURE_2D;
2107} 2170}
2108 2171
2109############################################################################# 2172#############################################################################
2455 2518
2456package CFClient::UI::InventoryItem; 2519package CFClient::UI::InventoryItem;
2457 2520
2458our @ISA = CFClient::UI::HBox::; 2521our @ISA = CFClient::UI::HBox::;
2459 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
2460sub new { 2549sub new {
2461 my $class = shift; 2550 my $class = shift;
2462 2551
2463 my %args = @_; 2552 my %args = @_;
2464 2553
2465 my $item = delete $args{item}; 2554 my $item = delete $args{item};
2466 2555
2467 my $desc = $item->{nrof} < 2 2556 my $desc = _item_to_desc ($item);
2468 ? $item->{name}
2469 : "$item->{nrof} $item->{name_pl}";
2470
2471 2557
2472 my $self = $class->SUPER::new ( 2558 my $self = $class->SUPER::new (
2473 can_hover => 1, 2559 can_hover => 1,
2474 can_events => 1, 2560 can_events => 1,
2475 tooltip => (CFClient::UI::Label->escape ($desc) 2561 tooltip => ((CFClient::UI::Label::escape $desc)
2476 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), 2562 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2477 connect_button_down => sub { 2563 connect_button_down => sub {
2478 my ($self, $ev, $x, $y) = @_; 2564 my ($self, $ev, $x, $y) = @_;
2479 2565
2480 # 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 :(
2481 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
2482 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); 2574 $::CONN->send ("move $targ $item->{tag} 0");
2483 } elsif ($ev->{button} == 2) { 2575 } elsif ($ev->{button} == 2) {
2484 $::CONN->send ("apply $item->{tag}"); 2576 $::CONN->send ("apply $item->{tag}");
2485 } elsif ($ev->{button} == 3) { 2577 } elsif ($ev->{button} == 3) {
2486 CFClient::UI::Menu->new ( 2578 my @menu_items = (
2487 items => [
2488 ["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") }],
2489 [ 2583 [
2490 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", 2584 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2491 sub { $::CONN->send ("lock $item->{tag}") }, 2585 sub { $::CONN->send ("lock $item->{tag}") },
2492 ],
2493 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2494 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2495 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2496 ], 2586 ],
2497 )->popup ($ev); 2587 );
2588
2589 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2498 } 2590 }
2499 2591
2500 1 2592 1
2501 }, 2593 },
2502 %args 2594 %args
2503 ); 2595 );
2596
2504 2597
2505 $self->add (new CFClient::UI::Face 2598 $self->add (new CFClient::UI::Face
2506 can_events => 0, 2599 can_events => 0,
2507 face => $item->{face}, 2600 face => $item->{face},
2508 anim => $item->{anim}, 2601 anim => $item->{anim},
2509 animspeed => $item->{animspeed}, 2602 animspeed => $item->{animspeed},
2510 ); 2603 );
2511 2604
2512 $self->add (new CFClient::UI::Label 2605 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2513 can_events => 0, 2606
2514 text => $desc, 2607 $self->{item} = $item;
2515 ); 2608
2609 $self->update_item;
2516 2610
2517 $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);
2518} 2620}
2519 2621
2520############################################################################# 2622#############################################################################
2521 2623
2522package CFClient::UI::Inventory; 2624package CFClient::UI::Inventory;
2538 my ($self, $items) = @_; 2640 my ($self, $items) = @_;
2539 2641
2540 $self->{scrolled}->clear; 2642 $self->{scrolled}->clear;
2541 return unless $items; 2643 return unless $items;
2542 2644
2543 my @items = sort { $a->{type} <=> $b->{type} } @$items; 2645 my @items = sort {
2646 ($a->{type} <=> $b->{type})
2647 or ($a->{name} cmp $b->{name})
2648 } @$items;
2544 2649
2545 $self->{real_items} = \@items; 2650 $self->{real_items} = \@items;
2546 2651
2547 for my $item (@items) { 2652 for my $item (@items) {
2548 my $desc = $item->{nrof} < 2
2549 ? $item->{name}
2550 : "$item->{nrof} $item->{name_pl}";
2551
2552 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); 2653 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2654 $item->update_item ();
2553 } 2655 }
2656
2657 $self->{scrolled}->add (@items);
2554 2658
2555# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2659# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2556} 2660}
2557 2661
2558sub size_request { 2662sub size_request {
2636 2740
2637package CFClient::UI::Statusbox; 2741package CFClient::UI::Statusbox;
2638 2742
2639our @ISA = CFClient::UI::VBox::; 2743our @ISA = CFClient::UI::VBox::;
2640 2744
2745sub new {
2746 my $class = shift;
2747
2748 $class->SUPER::new (
2749 fontsize => 0.8,
2750 @_,
2751 )
2752}
2753
2641sub reorder { 2754sub reorder {
2642 my ($self) = @_; 2755 my ($self) = @_;
2643 my $NOW = time; 2756 my $NOW = time;
2644 2757
2645 while (my ($k, $v) = each %{ $self->{item} }) { 2758 while (my ($k, $v) = each %{ $self->{item} }) {
2661 # TODO: doesn't handle markup well (read as: at all) 2774 # TODO: doesn't handle markup well (read as: at all)
2662 my $short = $item->{count} > 1 2775 my $short = $item->{count} > 1
2663 ? "<b>$item->{count} ×</b> $item->{text}" 2776 ? "<b>$item->{count} ×</b> $item->{text}"
2664 : $item->{text}; 2777 : $item->{text};
2665 2778
2779 my $fontsize = $item->{fontsize} || $self->{fontsize};
2780
2666 for ($short) { 2781 for ($short) {
2667 s/^\s+//; 2782 s/^\s+//;
2668 s/\012.*/…/s; 2783 s/\s+/ /g;
2669 my $len = int 40 / $item->{fontsize}; 2784 my $len = int 40 / $fontsize;
2670 substr $_, $len, length, "…" if $len < length; 2785 substr $_, $len, length, "…" if $len < length;
2671 } 2786 }
2672 2787
2673 new CFClient::UI::Label 2788 new CFClient::UI::Label
2674 markup => $short, 2789 markup => $short,
2675 tooltip => $item->{tooltip}, 2790 tooltip => $item->{tooltip},
2676 tooltip_font => $::FONT_PROP, 2791 tooltip_font => $::FONT_PROP,
2677 tooltip_width => 0.67, 2792 tooltip_width => 0.67,
2678 fontsize => $item->{fontsize}, 2793 fontsize => $fontsize,
2679 color => $item->{color}, 2794 fg => $item->{fg},
2680 can_events => 1, 2795 can_events => 1,
2681 can_hover => 1 2796 can_hover => 1
2682 }; 2797 };
2683 } 2798 }
2684 2799
2710 $self->{item}{$group} = { 2825 $self->{item}{$group} = {
2711 id => ++$self->{id}, 2826 id => ++$self->{id},
2712 text => $text, 2827 text => $text,
2713 timeout => $timeout, 2828 timeout => $timeout,
2714 tooltip => $text, 2829 tooltip => $text,
2715 fontsize => 0.8,
2716 color => [0.8, 0.8, 0.8, 0.8], 2830 fg => [0.8, 0.8, 0.8, 0.8],
2717 pri => 0, 2831 pri => 0,
2718 count => 1, 2832 count => 1,
2719 %arg, 2833 %arg,
2720 }; 2834 };
2721 } 2835 }
2760} 2874}
2761 2875
2762sub size_allocate { 2876sub size_allocate {
2763 my ($self, $w, $h) = @_; 2877 my ($self, $w, $h) = @_;
2764 2878
2765 my $old_w = $self->{old_w}; 2879 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2766 my $old_h = $self->{old_h}; 2880 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2767 2881
2768 if ($old_w && $old_h) { 2882 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2769 for my $child ($self->children) { 2883 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2770 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2771 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2772 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2773 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2774 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2775 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2776 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2777 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2778 }
2779 }
2780 2884
2781 for my $child ($self->children) { 2885 for my $child ($self->children) {
2782 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)};
2783 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
2784 $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;
2785 $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
2786 $child->configure ($X, $Y, $W, $H); 2897 $child->configure ($X, $Y, $W, $H);
2787 } 2898 }
2788
2789 $self->{old_w} = $w;
2790 $self->{old_h} = $h;
2791} 2899}
2792 2900
2793sub coord2local { 2901sub coord2local {
2794 my ($self, $x, $y) = @_; 2902 my ($self, $x, $y) = @_;
2795 2903
2808 $self->check_size; 2916 $self->check_size;
2809 $::WANT_REFRESH++; 2917 $::WANT_REFRESH++;
2810} 2918}
2811 2919
2812sub add { 2920sub add {
2813 my ($self, $child) = @_; 2921 my ($self, @children) = @_;
2814 2922
2923 for my $child (@children) {
2924 $child->{toplevel} = 1;
2925
2815 # integerise window positions 2926 # integerise window positions
2816 $child->{x} = int $child->{x}; 2927 $child->{x} = int $child->{x};
2817 $child->{y} = int $child->{y}; 2928 $child->{y} = int $child->{y};
2929 }
2818 2930
2819 $self->SUPER::add ($child); 2931 $self->SUPER::add (@children);
2820} 2932}
2821 2933
2822sub on_refresh { 2934sub on_refresh {
2823 my ($self, $id, $cb) = @_; 2935 my ($self, $id, $cb) = @_;
2824 2936

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines