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.208 by root, Mon May 15 22:52:17 2006 UTC vs.
Revision 1.220 by root, Mon May 22 03:28:55 2006 UTC

136 $rem += $_ - $i; 136 $rem += $_ - $i;
137 $_ = $i; 137 $_ = $i;
138 } 138 }
139} 139}
140 140
141sub full_refresh {
142 # make a copy, otherwise for complains about freed values.
143 my @widgets = values %WIDGET;
144
145 $_->update
146 for @widgets;
147}
148
141# call when resolution changes etc. 149# call when resolution changes etc.
142sub rescale_widgets { 150sub rescale_widgets {
143 my ($sx, $sy) = @_; 151 my ($sx, $sy) = @_;
144 152
153 # make a copy, otherwise for complains about freed values.
145 for my $widget (values %WIDGET) { 154 my @widgets = values %WIDGET;
155
156 for my $widget (@widgets) {
146 if ($widget->{toplevel}) { 157 if ($widget->{toplevel}) {
147 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 158 $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}; 159 $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}; 160 $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}; 161 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
280sub set_max_size { 291sub set_max_size {
281 my ($self, $w, $h) = @_; 292 my ($self, $w, $h) = @_;
282 293
283 delete $self->{max_w}; $self->{max_w} = $w if $w; 294 delete $self->{max_w}; $self->{max_w} = $w if $w;
284 delete $self->{max_h}; $self->{max_h} = $h if $h; 295 delete $self->{max_h}; $self->{max_h} = $h if $h;
296}
297
298sub set_tooltip {
299 my ($self, $tooltip) = @_;
300
301 $self->{tooltip} = $tooltip;
302
303 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
304 delete $CFClient::UI::TOOLTIP->{owner};
305 CFClient::UI::check_tooltip;
306 }
285} 307}
286 308
287# translate global coordinates to local coordinate system 309# translate global coordinates to local coordinate system
288sub coord2local { 310sub coord2local {
289 my ($self, $x, $y) = @_; 311 my ($self, $x, $y) = @_;
455 my $class = shift; 477 my $class = shift;
456 478
457 # range [value, low, high, page] 479 # range [value, low, high, page]
458 480
459 $class->SUPER::new ( 481 $class->SUPER::new (
460 bg => [0, 0, 0, 0.2], 482 #bg => [0, 0, 0, 0.2],
461 active_bg => [1, 1, 1, 0.5], 483 #active_bg => [1, 1, 1, 0.5],
462 @_ 484 @_
463 ) 485 )
464} 486}
465 487
466sub _draw { 488sub _draw {
467 my ($self) = @_; 489 my ($self) = @_;
468 490
491 my $color = $FOCUS == $self && $self->{active_bg}
492 ? $self->{active_bg}
493 : $self->{bg};
494
495 if ($color && (@$color < 4 || $color->[3])) {
469 my ($w, $h) = @$self{qw(w h)}; 496 my ($w, $h) = @$self{qw(w h)};
470 497
471 glEnable GL_BLEND; 498 glEnable GL_BLEND;
472 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 499 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
473 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 500 glColor @$color;
474 501
475 glBegin GL_QUADS; 502 glBegin GL_QUADS;
476 glVertex 0 , 0; 503 glVertex 0 , 0;
477 glVertex 0 , $h; 504 glVertex 0 , $h;
478 glVertex $w, $h; 505 glVertex $w, $h;
479 glVertex $w, 0; 506 glVertex $w, 0;
480 glEnd; 507 glEnd;
481 508
482 glDisable GL_BLEND; 509 glDisable GL_BLEND;
510 }
483} 511}
484 512
485############################################################################# 513#############################################################################
486 514
487package CFClient::UI::Empty; 515package CFClient::UI::Empty;
671 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 699 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
672 glClearColor 0, 0, 0, 0; 700 glClearColor 0, 0, 0, 0;
673 glClear GL_COLOR_BUFFER_BIT; 701 glClear GL_COLOR_BUFFER_BIT;
674 702
675 $self->_render; 703 $self->_render;
676# glColorMask 1, 1, 1, 0;
677# glEnable GL_BLEND;
678# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
679# glRasterPos 0, 0;
680# glCopyPixels 0, 0, $self->{w}, $self->{h};
681# glDisable GL_BLEND;
682# glColorMask 1, 1, 1, 1;
683 }; 704 };
684} 705}
685 706
686sub _draw { 707sub _draw {
687 my ($self) = @_; 708 my ($self) = @_;
691 my $tex = $self->{texture} 712 my $tex = $self->{texture}
692 or return; 713 or return;
693 714
694 glEnable GL_TEXTURE_2D; 715 glEnable GL_TEXTURE_2D;
695 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 716 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
696 glColor 0, 0, 0, 1; 717 glColor 1, 1, 1, 1;
697 718
698 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 719 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
699 720
700 glDisable GL_TEXTURE_2D; 721 glDisable GL_TEXTURE_2D;
701} 722}
784 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); 805 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
785 }, 806 },
786 ; 807 ;
787 808
788 $self = $class->SUPER::new ( 809 $self = $class->SUPER::new (
789 vp => (new CFClient::UI::ViewPort), 810 vp => (new CFClient::UI::ViewPort expand => 1),
790 slider => $slider, 811 slider => $slider,
791 @_, 812 @_,
792 ); 813 );
793 814
794 $self->{vp}->add ($self->{scrolled}); 815 $self->{vp}->add ($self->{scrolled});
1266 1287
1267############################################################################# 1288#############################################################################
1268 1289
1269package CFClient::UI::Label; 1290package CFClient::UI::Label;
1270 1291
1271our @ISA = CFClient::UI::Base::; 1292our @ISA = CFClient::UI::DrawBG::;
1272 1293
1273use CFClient::OpenGL; 1294use CFClient::OpenGL;
1274 1295
1275sub new { 1296sub new {
1276 my ($class, %arg) = @_; 1297 my ($class, %arg) = @_;
1277 1298
1278 my $self = $class->SUPER::new ( 1299 my $self = $class->SUPER::new (
1279 fg => [1, 1, 1], 1300 fg => [1, 1, 1],
1301 #bg => none
1302 #active_bg => none
1280 #font => default_font 1303 #font => default_font
1281 #text => initial text 1304 #text => initial text
1282 #markup => initial narkup 1305 #markup => initial narkup
1306 #max_w => maximum pixel width
1307 ellipsise => 3, # end
1283 layout => (new CFClient::Layout), 1308 layout => (new CFClient::Layout),
1284 fontsize => 1, 1309 fontsize => 1,
1285 align => -1, 1310 align => -1,
1286 valign => -1, 1311 valign => -1,
1287 padding => 2, 1312 padding => 2,
1302 } 1327 }
1303 1328
1304 $self 1329 $self
1305} 1330}
1306 1331
1307sub escape { 1332sub escape($) {
1308 local $_ = $_[1]; 1333 local $_ = $_[0];
1309 1334
1310 s/&/&amp;/g; 1335 s/&/&amp;/g;
1311 s/>/&gt;/g; 1336 s/>/&gt;/g;
1312 s/</&lt;/g; 1337 s/</&lt;/g;
1313 1338
1314 $_[1] 1339 $_
1315} 1340}
1316 1341
1317sub update { 1342sub update {
1318 my ($self) = @_; 1343 my ($self) = @_;
1319 1344
1352sub size_request { 1377sub size_request {
1353 my ($self) = @_; 1378 my ($self) = @_;
1354 1379
1355 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1380 $self->{layout}->set_font ($self->{font}) if $self->{font};
1356 $self->{layout}->set_width ($self->{max_w} || -1); 1381 $self->{layout}->set_width ($self->{max_w} || -1);
1382 $self->{layout}->set_ellipsise ($self->{ellipsise});
1383 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1357 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1384 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1358 1385
1359 my ($w, $h) = $self->{layout}->size; 1386 my ($w, $h) = $self->{layout}->size;
1360 1387
1361 if (exists $self->{template}) { 1388 if (exists $self->{template}) {
1391} 1418}
1392 1419
1393sub _draw { 1420sub _draw {
1394 my ($self) = @_; 1421 my ($self) = @_;
1395 1422
1423 $self->SUPER::_draw; # draw background, if applicable
1424
1396 my $tex = $self->{texture} ||= do { 1425 my $tex = $self->{texture} ||= do {
1397 $self->{layout}->set_foreground (@{$self->{fg}}); 1426 $self->{layout}->set_foreground (@{$self->{fg}});
1398 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1427 $self->{layout}->set_font ($self->{font}) if $self->{font};
1399 $self->{layout}->set_width ($self->{w}); 1428 $self->{layout}->set_width ($self->{w});
1429 $self->{layout}->set_ellipsise ($self->{ellipsise});
1430 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1400 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1431 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1401 1432
1402 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1433 my $tex = new_from_layout CFClient::Texture $self->{layout};
1403 1434
1404 $self->{ox} = int ($self->{align} < 0 ? $self->{padding} 1435 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1405 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1436 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1660 my $class = shift; 1691 my $class = shift;
1661 1692
1662 $class->SUPER::new ( 1693 $class->SUPER::new (
1663 padding => 4, 1694 padding => 4,
1664 fg => [1, 1, 1], 1695 fg => [1, 1, 1],
1665 bg => [1, 1, 1, 0.2],
1666 active_fg => [0, 0, 1], 1696 active_fg => [0, 0, 1],
1667 can_hover => 1, 1697 can_hover => 1,
1668 align => 0, 1698 align => 0,
1669 valign => 0, 1699 valign => 0,
1670 can_events => 1, 1700 can_events => 1,
1718 1748
1719 $class->SUPER::new ( 1749 $class->SUPER::new (
1720 padding => 2, 1750 padding => 2,
1721 fg => [1, 1, 1], 1751 fg => [1, 1, 1],
1722 active_fg => [1, 1, 0], 1752 active_fg => [1, 1, 0],
1753 bg => [0, 0, 0, 0.2],
1754 active_bg => [1, 1, 1, 0.5],
1723 state => 0, 1755 state => 0,
1724 can_hover => 1, 1756 can_hover => 1,
1725 @_ 1757 @_
1726 ) 1758 )
1727} 1759}
2019 # TODO: req_w/h are wrong with vertical 2051 # TODO: req_w/h are wrong with vertical
2020 # TODO: calculations are off 2052 # TODO: calculations are off
2021 my $self = $class->SUPER::new ( 2053 my $self = $class->SUPER::new (
2022 fg => [1, 1, 1], 2054 fg => [1, 1, 1],
2023 active_fg => [0, 0, 0], 2055 active_fg => [0, 0, 0],
2056 bg => [0, 0, 0, 0.2],
2057 active_bg => [1, 1, 1, 0.5],
2024 range => [0, 0, 100, 10], 2058 range => [0, 0, 100, 10],
2025 req_w => $::WIDTH / 80, 2059 req_w => $::WIDTH / 80,
2026 req_h => $::WIDTH / 80, 2060 req_h => $::WIDTH / 80,
2027 vertical => 0, 2061 vertical => 0,
2028 can_hover => 1, 2062 can_hover => 1,
2029 inner_pad => .05, 2063 inner_pad => 0.02,
2030 @_ 2064 @_
2031 ); 2065 );
2032 2066
2033 $self->set_value ($self->{range}[0]); 2067 $self->set_value ($self->{range}[0]);
2034 $self->update; 2068 $self->update;
2124 glScale $self->{w}, $self->{h}; 2158 glScale $self->{w}, $self->{h};
2125 2159
2126 if ($self->{vertical}) { 2160 if ($self->{vertical}) {
2127 # draw a vertical slider like a rotated horizontal slider 2161 # draw a vertical slider like a rotated horizontal slider
2128 2162
2163 glTranslate 1, 0, 0;
2129 glRotate 90, 0, 0, 1; 2164 glRotate 90, 0, 0, 1;
2130 glTranslate 0, 1, 0;
2131 } 2165 }
2132 2166
2133 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2167 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2134 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2168 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2135 2169
2181 2215
2182 $self->{fontsize} = $fontsize; 2216 $self->{fontsize} = $fontsize;
2183 $self->reflow; 2217 $self->reflow;
2184} 2218}
2185 2219
2220sub size_allocate {
2221 my ($self, $w, $h) = @_;
2222
2223 $self->SUPER::size_allocate ($w, $h);
2224
2225 $self->{layout}->set_font ($self->{font}) if $self->{font};
2226 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2227 $self->{layout}->set_width ($self->{children}[0]{w});
2228
2229 $self->reflow;
2230}
2231
2186sub text_height { 2232sub text_height {
2187 my ($self, $text) = @_; 2233 my ($self, $text, $indent) = @_;
2188 2234
2189 my $layout = $self->{layout}; 2235 my $layout = $self->{layout};
2190 2236
2191 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2237 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2192 $layout->set_width ($self->{children}[0]{w}); 2238 $layout->set_width ($self->{children}[0]{w} - $indent);
2193 $layout->set_markup ($text); 2239 $layout->set_markup ($text);
2194 2240
2195 ($layout->size)[1] 2241 ($layout->size)[1]
2196} 2242}
2197 2243
2200 2246
2201 $self->{need_reflow}++; 2247 $self->{need_reflow}++;
2202 $self->update; 2248 $self->update;
2203} 2249}
2204 2250
2205sub size_allocate {
2206 my ($self, $w, $h) = @_;
2207
2208 $self->SUPER::size_allocate ($w, $h);
2209
2210 $self->{layout}->set_font ($self->{font}) if $self->{font};
2211 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2212 $self->{layout}->set_width ($self->{children}[0]{w});
2213
2214 $self->reflow;
2215}
2216
2217sub add_paragraph { 2251sub add_paragraph {
2218 my ($self, $color, $text) = @_; 2252 my ($self, $color, $text, $indent) = @_;
2219 2253
2220 #TODO: intelligently "reformat" paragraph 2254 #TODO: intelligently "reformat" paragraph
2221 2255
2256 for my $line (split /\n/, $text) {
2222 my $height = $self->text_height ($text); 2257 my $height = $self->text_height ($line);
2223
2224 $self->{height} += $height; 2258 $self->{height} += $height;
2225
2226 push @{$self->{par}}, [$height, $color, $text]; 2259 push @{$self->{par}}, [$height, $color, $indent, $line];
2260 }
2227 2261
2228 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; 2262 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
2229 $self->{children}[1]->update; 2263 $self->{children}[1]->update;
2230} 2264}
2231 2265
2240 2274
2241 $ROOT->on_post_alloc ($self, sub { 2275 $ROOT->on_post_alloc ($self, sub {
2242 if (delete $self->{need_reflow}) { 2276 if (delete $self->{need_reflow}) {
2243 my $height = 0; 2277 my $height = 0;
2244 2278
2245 $height += $_->[0] = $self->text_height ($_->[2]) 2279 $height += $_->[0] = $self->text_height ($_->[3], $_->[2])
2246 for @{$self->{par}}; 2280 for @{$self->{par}};
2247 2281
2248 $self->{height} = $height; 2282 $self->{height} = $height;
2249 2283
2250 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2284 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2285 $self->{children}[1]->update;
2251 2286
2252 delete $self->{texture}; 2287 delete $self->{texture};
2253 } 2288 }
2254 2289
2255 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2290 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2256 glClearColor 0, 0, 0, 0; 2291 glClearColor 0.5, 0.5, 0.5, 0;
2257 glClear GL_COLOR_BUFFER_BIT; 2292 glClear GL_COLOR_BUFFER_BIT;
2258
2259 glEnable GL_TEXTURE_2D;
2260 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2261 2293
2262 my $top = int $self->{children}[1]{range}[0]; 2294 my $top = int $self->{children}[1]{range}[0];
2263 2295
2264 my $y0 = $top; 2296 my $y0 = $top;
2265 my $y1 = $top + $self->{h}; 2297 my $y1 = $top + $self->{h};
2268 2300
2269 my $layout = $self->{layout}; 2301 my $layout = $self->{layout};
2270 2302
2271 $layout->set_font ($self->{font}) if $self->{font}; 2303 $layout->set_font ($self->{font}) if $self->{font};
2272 2304
2305 glEnable GL_BLEND;
2306 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2307
2273 for my $par (@{$self->{par}}) { 2308 for my $par (@{$self->{par}}) {
2274 my $h = $par->[0]; 2309 my $h = $par->[0];
2275 2310
2276 if ($y0 < $y + $h && $y < $y1) { 2311 if ($y0 < $y + $h && $y < $y1) {
2277 $layout->set_foreground (@{ $par->[1] }); 2312 $layout->set_foreground (@{ $par->[1] });
2313 $layout->set_width ($self->{w} - $par->[2]);
2278 $layout->set_markup ($par->[2]); 2314 $layout->set_markup ($par->[3]);
2279 2315
2280 my ($W, $H) = $layout->size; 2316 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2281 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0); 2317
2318 glRasterPos $par->[2], $y - $y0;
2319 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2282 } 2320 }
2283 2321
2284 $y += $h; 2322 $y += $h;
2285 } 2323 }
2286 2324
2287 glDisable GL_TEXTURE_2D; 2325 glDisable GL_BLEND;
2288 }; 2326 };
2289 }); 2327 });
2290} 2328}
2291 2329
2292sub _draw { 2330sub _draw {
2293 my ($self) = @_; 2331 my ($self) = @_;
2294 2332
2295 glEnable GL_TEXTURE_2D; 2333 glEnable GL_TEXTURE_2D;
2296 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2334 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2297 glColor 1, 1, 1, 1; 2335 glColor 1, 1, 1, 1;
2298 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2336 $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2299 glDisable GL_TEXTURE_2D; 2337 glDisable GL_TEXTURE_2D;
2300 2338
2301 $self->{children}[1]->draw; 2339 $self->{children}[1]->draw;
2302 2340
2303} 2341}
2402 2440
2403sub set_tooltip_from { 2441sub set_tooltip_from {
2404 my ($self, $widget) = @_; 2442 my ($self, $widget) = @_;
2405 2443
2406 $self->add (new CFClient::UI::Label 2444 $self->add (new CFClient::UI::Label
2407 markup => $widget->{tooltip}, 2445 markup => $widget->{tooltip},
2408 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 2446 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2409 fontsize => 0.8, 2447 fontsize => 0.8,
2410 fg => [0, 0, 0, 1], 2448 fg => [0, 0, 0, 1],
2449 ellipsise => 0,
2411 font => ($widget->{tooltip_font} || $::FONT_PROP), 2450 font => ($widget->{tooltip_font} || $::FONT_PROP),
2412 ); 2451 );
2413} 2452}
2414 2453
2415sub size_request { 2454sub size_request {
2416 my ($self) = @_; 2455 my ($self) = @_;
2462use CFClient::OpenGL; 2501use CFClient::OpenGL;
2463 2502
2464sub new { 2503sub new {
2465 my $class = shift; 2504 my $class = shift;
2466 2505
2467 $class->SUPER::new ( 2506 my $self = $class->SUPER::new (
2468 aspect => 1, 2507 aspect => 1,
2469 @_, 2508 @_,
2470 ) 2509 );
2510
2511 if ($self->{anim} && $self->{animspeed}) {
2512 Scalar::Util::weaken (my $widget = $self);
2513
2514 $self->{timer} = Event->timer (
2515 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
2516 hard => 1,
2517 interval => $self->{animspeed},
2518 cb => sub {
2519 ++$widget->{frame};
2520 $widget->update;
2521 },
2522 );
2523 }
2524
2525 $self
2471} 2526}
2472 2527
2473sub size_request { 2528sub size_request {
2474 (32, 8) 2529 (32, 8)
2475} 2530}
2476 2531
2477sub _draw { 2532sub _draw {
2478 my ($self) = @_; 2533 my ($self) = @_;
2479 2534
2480 return unless $::CONN;#d# manage and cache textures differently 2535 return unless $::CONN;#d# manage and cache textures differently
2536
2537 my $face;
2538
2539 if ($self->{frame}) {
2540 my $anim = $::CONN->{anim}[$self->{anim}];
2541
2542 $face = $anim->[ $self->{frame} % @$anim ]
2543 if $anim && @$anim;
2544 }
2545
2481 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2546 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2482 2547
2483 # TODO animation
2484 if ($tex) { 2548 if ($tex) {
2485 glEnable GL_TEXTURE_2D; 2549 glEnable GL_TEXTURE_2D;
2486 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2550 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2487 glColor 1, 1, 1, 1; 2551 glColor 1, 1, 1, 1;
2488 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2552 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2489 glDisable GL_TEXTURE_2D; 2553 glDisable GL_TEXTURE_2D;
2490 } 2554 }
2491} 2555}
2492 2556
2557sub DESTROY {
2558 my ($self) = @_;
2559
2560 $self->{timer}->cancel
2561 if $self->{timer};
2562
2563 $self->SUPER::DESTROY;
2564}
2565
2493############################################################################# 2566#############################################################################
2494 2567
2495package CFClient::UI::InventoryItem; 2568package CFClient::UI::InventoryItem;
2496 2569
2497our @ISA = CFClient::UI::HBox::; 2570our @ISA = CFClient::UI::HBox::;
2498 2571
2499sub new { 2572sub _item_to_desc {
2500 my $class = shift; 2573 my ($item) = @_;
2501 2574
2502 my %args = @_; 2575 my $desc =
2503
2504 my $item = delete $args{item};
2505
2506 my $desc = $item->{nrof} < 2 2576 $item->{nrof} < 2
2507 ? $item->{name} 2577 ? $item->{name}
2508 : "$item->{nrof} $item->{name_pl}"; 2578 : "$item->{nrof} × $item->{name_pl}";
2509 2579
2510 $item->{flags} & Crossfire::Protocol::F_OPEN 2580 $item->{flags} & Crossfire::Protocol::F_OPEN
2511 and $desc .= " (open)"; 2581 and $desc .= " (open)";
2512 $item->{flags} & Crossfire::Protocol::F_APPLIED 2582 $item->{flags} & Crossfire::Protocol::F_APPLIED
2513 and $desc .= " (applied)"; 2583 and $desc .= " (applied)";
2520 $item->{flags} & Crossfire::Protocol::F_DAMNED 2590 $item->{flags} & Crossfire::Protocol::F_DAMNED
2521 and $desc .= " (damned)"; 2591 and $desc .= " (damned)";
2522 $item->{flags} & Crossfire::Protocol::F_LOCKED 2592 $item->{flags} & Crossfire::Protocol::F_LOCKED
2523 and $desc .= " *"; 2593 and $desc .= " *";
2524 2594
2595 $desc
2596}
2597
2598sub new {
2599 my $class = shift;
2600
2601 my %args = @_;
2602
2603 my $item = delete $args{item};
2604
2605 my $desc = _item_to_desc ($item);
2606
2525 my $self = $class->SUPER::new ( 2607 my $self = $class->SUPER::new (
2526 can_hover => 1, 2608 can_hover => 1,
2527 can_events => 1, 2609 can_events => 1,
2528 tooltip => (CFClient::UI::Label->escape ($desc) 2610 tooltip => ((CFClient::UI::Label::escape $desc)
2529 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), 2611 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2530 connect_button_down => sub { 2612 connect_button_down => sub {
2531 my ($self, $ev, $x, $y) = @_; 2613 my ($self, $ev, $x, $y) = @_;
2532 2614
2533 # todo: maybe put examine on 1? but should just be a tooltip :( 2615 # todo: maybe put examine on 1? but should just be a tooltip :(
2534 if ($ev->{button} == 1) { 2616 if ($ev->{button} == 1) {
2617 my $targ = $::CONN->{player}{tag};
2618
2619 if ($item->{container} == $::CONN->{player}{tag}) {
2620 $targ = $main::OPENCONT;
2621 }
2622
2535 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); 2623 $::CONN->send ("move $targ $item->{tag} 0");
2536 } elsif ($ev->{button} == 2) { 2624 } elsif ($ev->{button} == 2) {
2537 $::CONN->send ("apply $item->{tag}"); 2625 $::CONN->send ("apply $item->{tag}");
2538 } elsif ($ev->{button} == 3) { 2626 } elsif ($ev->{button} == 3) {
2539 my @menu_items = ( 2627 my @menu_items = (
2540 ["examine", sub { $::CONN->send ("examine $item->{tag}") }], 2628 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2541 ["mark", sub { $::CONN->send ("mark $item->{tag}") }], 2629 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2542 ["apply", sub { $::CONN->send ("apply $item->{tag}") }], 2630 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2631 (
2632 $item->{flags} & Crossfire::Protocol::F_LOCKED
2633 ? (
2634 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2635 )
2636 : (
2637 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2543 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }], 2638 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2544 [ 2639 )
2545 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2546 sub { $::CONN->send ("lock ". () ."$item->{tag}") },
2547 ], 2640 ),
2548 ); 2641 );
2549 2642
2550 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); 2643 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2551 } 2644 }
2552 2645
2553 1 2646 1
2554 }, 2647 },
2555 %args 2648 %args
2556 ); 2649 );
2650
2557 2651
2558 $self->add (new CFClient::UI::Face 2652 $self->add (new CFClient::UI::Face
2559 can_events => 0, 2653 can_events => 0,
2560 face => $item->{face}, 2654 face => $item->{face},
2561 anim => $item->{anim}, 2655 anim => $item->{anim},
2562 animspeed => $item->{animspeed}, 2656 animspeed => $item->{animspeed},
2563 ); 2657 );
2564 2658
2565 $self->add (new CFClient::UI::Label 2659 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2566 can_events => 0, 2660
2567 text => $desc, 2661 $self->{item} = $item;
2568 ); 2662
2663 $self->update_item;
2569 2664
2570 $self 2665 $self
2666}
2667
2668sub update_item {
2669 my ($self) = @_;
2670
2671 my $desc = _item_to_desc ($self->{item});
2672
2673 $self->{name_lbl}->set_text ($desc);
2571} 2674}
2572 2675
2573############################################################################# 2676#############################################################################
2574 2677
2575package CFClient::UI::Inventory; 2678package CFClient::UI::Inventory;
2599 } @$items; 2702 } @$items;
2600 2703
2601 $self->{real_items} = \@items; 2704 $self->{real_items} = \@items;
2602 2705
2603 for my $item (@items) { 2706 for my $item (@items) {
2604 my $desc = $item->{nrof} < 2
2605 ? $item->{name}
2606 : "$item->{nrof} $item->{name_pl}";
2607
2608 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; 2707 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2708 $item->update_item ();
2609 } 2709 }
2610 2710
2611 $self->{scrolled}->add (@items); 2711 $self->{scrolled}->add (@items);
2612 2712
2613# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2713# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2694 2794
2695package CFClient::UI::Statusbox; 2795package CFClient::UI::Statusbox;
2696 2796
2697our @ISA = CFClient::UI::VBox::; 2797our @ISA = CFClient::UI::VBox::;
2698 2798
2799sub new {
2800 my $class = shift;
2801
2802 $class->SUPER::new (
2803 fontsize => 0.8,
2804 @_,
2805 )
2806}
2807
2699sub reorder { 2808sub reorder {
2700 my ($self) = @_; 2809 my ($self) = @_;
2701 my $NOW = time; 2810 my $NOW = time;
2702 2811
2703 while (my ($k, $v) = each %{ $self->{item} }) { 2812 while (my ($k, $v) = each %{ $self->{item} }) {
2722 : $item->{text}; 2831 : $item->{text};
2723 2832
2724 for ($short) { 2833 for ($short) {
2725 s/^\s+//; 2834 s/^\s+//;
2726 s/\s+/ /g; 2835 s/\s+/ /g;
2727 my $len = int 40 / $item->{fontsize};
2728 substr $_, $len, length, "…" if $len < length;
2729 } 2836 }
2730 2837
2731 new CFClient::UI::Label 2838 new CFClient::UI::Label
2732 markup => $short, 2839 markup => $short,
2733 tooltip => $item->{tooltip}, 2840 tooltip => $item->{tooltip},
2734 tooltip_font => $::FONT_PROP, 2841 tooltip_font => $::FONT_PROP,
2735 tooltip_width => 0.67, 2842 tooltip_width => 0.67,
2736 fontsize => $item->{fontsize}, 2843 fontsize => $item->{fontsize} || $self->{fontsize},
2844 max_w => $::WIDTH * 0.44,
2737 fg => $item->{fg}, 2845 fg => $item->{fg},
2738 can_events => 1, 2846 can_events => 1,
2739 can_hover => 1 2847 can_hover => 1
2740 }; 2848 };
2741 } 2849 }
2768 $self->{item}{$group} = { 2876 $self->{item}{$group} = {
2769 id => ++$self->{id}, 2877 id => ++$self->{id},
2770 text => $text, 2878 text => $text,
2771 timeout => $timeout, 2879 timeout => $timeout,
2772 tooltip => $text, 2880 tooltip => $text,
2773 fontsize => 0.8,
2774 fg => [0.8, 0.8, 0.8, 0.8], 2881 fg => [0.8, 0.8, 0.8, 0.8],
2775 pri => 0, 2882 pri => 0,
2776 count => 1, 2883 count => 1,
2777 %arg, 2884 %arg,
2778 }; 2885 };
2779 } 2886 }
2780 2887
2781 $self->reorder; 2888 $self->reorder;
2889}
2890
2891sub reconfigure {
2892 my ($self) = @_;
2893
2894 delete $_->{label}
2895 for values %{ $self->{item} || {} };
2896
2897 $self->reorder;
2898 $self->SUPER::reconfigure;
2782} 2899}
2783 2900
2784############################################################################# 2901#############################################################################
2785 2902
2786package CFClient::UI::Root; 2903package CFClient::UI::Root;
2949 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 3066 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2950 glClear GL_COLOR_BUFFER_BIT; 3067 glClear GL_COLOR_BUFFER_BIT;
2951 3068
2952 glMatrixMode GL_PROJECTION; 3069 glMatrixMode GL_PROJECTION;
2953 glLoadIdentity; 3070 glLoadIdentity;
2954 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; 3071 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
2955 glMatrixMode GL_MODELVIEW; 3072 glMatrixMode GL_MODELVIEW;
2956 glLoadIdentity; 3073 glLoadIdentity;
2957 3074
2958 $self->_draw; 3075 $self->_draw;
2959} 3076}
2961############################################################################# 3078#############################################################################
2962 3079
2963package CFClient::UI; 3080package CFClient::UI;
2964 3081
2965$ROOT = new CFClient::UI::Root; 3082$ROOT = new CFClient::UI::Root;
2966$TOOLTIP = new CFClient::UI::Tooltip; 3083$TOOLTIP = new CFClient::UI::Tooltip z => 900;
2967 3084
29681 30851
2969 3086

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines