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.188 by root, Mon May 8 21:16:09 2006 UTC vs.
Revision 1.196 by root, Fri May 12 00:16:34 2006 UTC

1package CFClient::UI; 1package CFClient::UI;
2 2
3use utf8;
3use strict; 4use strict;
4 5
5use Scalar::Util (); 6use Scalar::Util ();
6use List::Util (); 7use List::Util ();
7 8
23 24
24 my $tip = $widget->{tooltip}; 25 my $tip = $widget->{tooltip};
25 26
26 $tip = $tip->($widget) if CODE:: eq ref $tip; 27 $tip = $tip->($widget) if CODE:: eq ref $tip;
27 28
28 $TOOLTIP->set_markup ($widget->{tooltip}); 29 $TOOLTIP->set_tooltip_from ($widget);
29
30 $TOOLTIP->show; 30 $TOOLTIP->show;
31 31
32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
33 33
34 if ($x + $TOOLTIP->{w} > $::WIDTH) { 34 if ($x + $TOOLTIP->{w} > $::WIDTH) {
232 $self->{y} = $y; 232 $self->{y} = $y;
233 $self->update; 233 $self->update;
234 } 234 }
235 235
236 if ($self->{w} != $w || $self->{h} != $h) { 236 if ($self->{w} != $w || $self->{h} != $h) {
237 $self->{w} = $w; 237 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h];
238 $self->{h} = $h;
239
240 $self->size_allocate ($w, $h);
241 $self->update;
242 $self->emit (size_allocate => $w, $h);
243 } 238 }
244} 239}
245 240
246sub size_allocate { 241sub size_allocate {
247 # nothing to be done 242 # nothing to be done
256 251
257 $_->reconfigure 252 $_->reconfigure
258 for $self->children; 253 for $self->children;
259 254
260 $self->check_size; 255 $self->check_size;
261 $self->size_allocate ($self->{w}, $self->{h}); 256 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $self->{w}, $self->{h}];
262 $self->update; 257 $self->update;
263} 258}
264 259
265sub set_max_size { 260sub set_max_size {
266 my ($self, $w, $h) = @_; 261 my ($self, $w, $h) = @_;
267 262
268 delete $self->{max_w}; $self->{max_w} = $w if $w; 263 delete $self->{max_w}; $self->{max_w} = $w if $w;
269 delete $self->{max_h}; $self->{max_h} = $h if $h; 264 delete $self->{max_h}; $self->{max_h} = $h if $h;
270}
271
272# return top left coordinates
273sub _topleft {
274 my ($self, $x, $y) = @_;
275
276 $self->{parent}
277 or Carp::confess "no parent widget in _topleft\n";#d#
278
279 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
280} 265}
281 266
282# translate global coordinates to local coordinate system 267# translate global coordinates to local coordinate system
283sub coord2local { 268sub coord2local {
284 my ($self, $x, $y) = @_; 269 my ($self, $x, $y) = @_;
285 270
286 my ($X, $Y) = $self->_topleft; 271 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
287 ($x - $X, $y - $Y)
288} 272}
289 273
290# translate local coordinates to global coordinate system 274# translate local coordinates to global coordinate system
291sub coord2global { 275sub coord2global {
292 my ($self, $x, $y) = @_; 276 my ($self, $x, $y) = @_;
293 277
294 my ($X, $Y) = $self->_topleft; 278 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
295 ($x + $X, $y + $Y)
296} 279}
297 280
298sub focus_in { 281sub focus_in {
299 my ($self) = @_; 282 my ($self) = @_;
300 283
398 381
399sub set_parent { 382sub set_parent {
400 my ($self, $parent) = @_; 383 my ($self, $parent) = @_;
401 384
402 Scalar::Util::weaken ($self->{parent} = $parent); 385 Scalar::Util::weaken ($self->{parent} = $parent);
386
387 $self->check_size
388 unless exists $self->{req_w};
403} 389}
404 390
405sub check_size { 391sub check_size {
406 my ($self) = @_; 392 my ($self) = @_;
407 393
408 $self->{parent} 394 $CFClient::UI::ROOT->{check_size}{$self} = $self;
409 or return 1;
410
411 my ($w, $h) = $self->{user_w} && $self->{user_h}
412 ? @$self{qw(user_w user_h)}
413 : $self->size_request;
414
415 if ($w != $self->{req_w} || $h != $self->{req_h}) {
416 $self->{req_w} = $w;
417 $self->{req_h} = $h;
418
419 $self->{parent}->check_size
420 or $self->size_allocate (
421 (List::Util::max $self->{w}, $w),
422 (List::Util::max $self->{h}, $h),
423 );
424
425 1
426 } else {
427 0
428 }
429} 395}
430 396
431sub update { 397sub update {
432 my ($self) = @_; 398 my ($self) = @_;
433 399
542 $self->{children} = [ 508 $self->{children} = [
543 sort { $a->{z} <=> $b->{z} } 509 sort { $a->{z} <=> $b->{z} }
544 @{$self->{children}}, @widgets 510 @{$self->{children}}, @widgets
545 ]; 511 ];
546 512
547 $_->check_size 513 $self->check_size;
548 for @widgets;
549
550 $self->update; 514 $self->update;
551} 515}
552 516
553sub children { 517sub children {
554 @{ $_[0]{children} } 518 @{ $_[0]{children} }
702 my ($w, $h) = ($self->w, $self->h); 666 my ($w, $h) = ($self->w, $self->h);
703 667
704 my $tex = $self->{texture} 668 my $tex = $self->{texture}
705 or return; 669 or return;
706 670
707 glEnable GL_BLEND;
708 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
709 glEnable GL_TEXTURE_2D; 671 glEnable GL_TEXTURE_2D;
710 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 672 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
711 glColor 0, 0, 0, 1; 673 glColor 0, 0, 0, 1;
712 674
713 $tex->draw_quad (0, 0, $w, $h); 675 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
714 676
715 glDisable GL_BLEND;
716 glDisable GL_TEXTURE_2D; 677 glDisable GL_TEXTURE_2D;
717} 678}
718 679
719############################################################################# 680#############################################################################
720 681
744 $self->{view_y} = int $y; 705 $self->{view_y} = int $y;
745 706
746 $self->update; 707 $self->update;
747} 708}
748 709
749# hmm, this does not work for topleft of $self... but we should not aks for that 710# hmm, this does not work for topleft of $self... but we should not ask for that
750sub _topleft { 711sub coord2local {
751 my ($self, $x, $y) = @_; 712 my ($self, $x, $y) = @_;
752 713
753 $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y}) 714 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
715}
716
717sub coord2global {
718 my ($self, $x, $y) = @_;
719
720 $x = List::Util::min $self->{w}, $x - $self->{view_x};
721 $y = List::Util::min $self->{h}, $y - $self->{view_y};
722
723 $self->SUPER::coord2global ($x, $y)
754} 724}
755 725
756sub find_widget { 726sub find_widget {
757 my ($self, $x, $y) = @_; 727 my ($self, $x, $y) = @_;
758 728
1240} 1210}
1241 1211
1242sub size_allocate { 1212sub size_allocate {
1243 my ($self, $w, $h) = @_; 1213 my ($self, $w, $h) = @_;
1244 1214
1215 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1216
1245 my $children = $self->{children}; 1217 my $children = $self->{children};
1246 1218
1247 my @h = map $_->{req_h}, @$children; 1219 my @h = map $_->{req_h}, @$children;
1248 1220
1249 my $req_h = List::Util::sum @h; 1221 my $req_h = List::Util::sum @h;
1288 my ($class, %arg) = @_; 1260 my ($class, %arg) = @_;
1289 1261
1290 my $self = $class->SUPER::new ( 1262 my $self = $class->SUPER::new (
1291 fg => [1, 1, 1], 1263 fg => [1, 1, 1],
1292 #font => default_font 1264 #font => default_font
1265 #text => initial text
1266 #markup => initial narkup
1267 layout => (new CFClient::Layout),
1293 fontsize => 1, 1268 fontsize => 1,
1294 text => "",
1295 align => -1, 1269 align => -1,
1296 valign => -1, 1270 valign => -1,
1297 padding => 2, 1271 padding => 2,
1298 layout => new CFClient::Layout,
1299 can_events => 0, 1272 can_events => 0,
1300 %arg 1273 %arg
1301 ); 1274 );
1302 1275
1303 if (exists $self->{template}) { 1276 if (exists $self->{template}) {
1304 my $layout = new CFClient::Layout; 1277 my $layout = new CFClient::Layout;
1305 $layout->set_text (delete $self->{template}); 1278 $layout->set_text (delete $self->{template});
1306 $self->{template} = $layout; 1279 $self->{template} = $layout;
1307 } 1280 }
1308 1281
1309 $self->set_text (delete $self->{text}) if exists $self->{text}; 1282 if (exists $self->{markup}) {
1310 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1283 $self->set_markup (delete $self->{markup});
1284 } else {
1285 $self->set_text (delete $self->{text});
1286 }
1311 1287
1312 $self 1288 $self
1313} 1289}
1314 1290
1315sub escape { 1291sub escape {
1333 my ($self, $text) = @_; 1309 my ($self, $text) = @_;
1334 1310
1335 return if $self->{text} eq "T$text"; 1311 return if $self->{text} eq "T$text";
1336 $self->{text} = "T$text"; 1312 $self->{text} = "T$text";
1337 1313
1314 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1338 $self->{layout}->set_text ($text); 1315 $self->{layout}->set_text ($text);
1339 1316
1340 delete $self->{texture};
1341 $self->update; 1317 $self->update;
1342 $self->check_size; 1318 $self->check_size;
1343} 1319}
1344 1320
1345sub set_markup { 1321sub set_markup {
1346 my ($self, $markup) = @_; 1322 my ($self, $markup) = @_;
1347 1323
1348 return if $self->{text} eq "M$markup"; 1324 return if $self->{text} eq "M$markup";
1349 $self->{text} = "M$markup"; 1325 $self->{text} = "M$markup";
1350 1326
1327 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1328
1329 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1351 $self->{layout}->set_markup ($markup); 1330 $self->{layout}->set_markup ($markup);
1352 1331
1353 delete $self->{texture};
1354 $self->update; 1332 $self->update;
1355 $self->check_size; 1333 $self->check_size;
1356} 1334}
1357 1335
1358sub size_request { 1336sub size_request {
1398 1376
1399sub _draw { 1377sub _draw {
1400 my ($self) = @_; 1378 my ($self) = @_;
1401 1379
1402 my $tex = $self->{texture} ||= do { 1380 my $tex = $self->{texture} ||= do {
1381 $self->{layout}->set_foreground (@{$self->{fg}});
1403 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1382 $self->{layout}->set_font ($self->{font}) if $self->{font};
1404 $self->{layout}->set_width ($self->{w}); 1383 $self->{layout}->set_width ($self->{w});
1405 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1384 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1385
1406 new_from_layout CFClient::Texture $self->{layout} 1386 my $tex = new_from_layout CFClient::Texture $self->{layout};
1387
1388 $self->{ox} = int $self->{align} < 0 ? $self->{padding}
1389 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1390 : ($self->{w} - $tex->{w}) * 0.5;
1391
1392 $self->{oy} = int $self->{valign} < 0 ? $self->{padding}
1393 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1394 : ($self->{h} - $tex->{h}) * 0.5;
1395
1396 $tex
1407 }; 1397 };
1408 1398
1409 glEnable GL_BLEND;
1410 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1411 glEnable GL_TEXTURE_2D; 1399 glEnable GL_TEXTURE_2D;
1412 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1400 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1413 1401
1402 if ($tex->{format} == GL_ALPHA) {
1414 glColor @{$self->{fg}}; 1403 glColor @{$self->{fg}};
1415
1416 $self->{ox} = int (
1417 $self->{align} < 0 ? $self->{padding}
1418 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1419 : ($self->{w} - $tex->{w}) * 0.5
1420 );
1421
1422 $self->{oy} = int (
1423 $self->{valign} < 0 ? $self->{padding}
1424 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1425 : ($self->{h} - $tex->{h}) * 0.5
1426 );
1427
1428 $tex->draw_quad ($self->{ox}, $self->{oy}); 1404 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1405 } else {
1406 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1407 }
1429 1408
1430 glDisable GL_TEXTURE_2D; 1409 glDisable GL_TEXTURE_2D;
1431 glDisable GL_BLEND;
1432} 1410}
1433 1411
1434############################################################################# 1412#############################################################################
1435 1413
1436package CFClient::UI::EntryBase; 1414package CFClient::UI::EntryBase;
1471 $self->{layout}->set_text ("$text "); 1449 $self->{layout}->set_text ("$text ");
1472 1450
1473 $self->emit (changed => $self->{text}); 1451 $self->emit (changed => $self->{text});
1474} 1452}
1475 1453
1454sub set_text {
1455 my ($self, $text) = @_;
1456
1457 $self->{cursor} = length $text;
1458 $self->_set_text ($text);
1459 $self->check_size;
1460 $self->update;
1461}
1462
1476sub get_text { 1463sub get_text {
1477 $_[0]{text} 1464 $_[0]{text}
1478} 1465}
1479 1466
1480sub size_request { 1467sub size_request {
1487 1474
1488sub size_allocate { 1475sub size_allocate {
1489 my ($self, $w, $h) = @_; 1476 my ($self, $w, $h) = @_;
1490 1477
1491 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text 1478 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1492}
1493
1494sub set_text {
1495 my ($self, $text) = @_;
1496
1497 $self->{cursor} = length $text;
1498 $self->_set_text ($text);
1499 $self->update;
1500} 1479}
1501 1480
1502sub key_down { 1481sub key_down {
1503 my ($self, $ev) = @_; 1482 my ($self, $ev) = @_;
1504 1483
1692 1671
1693 if ($GRAB == $self) { 1672 if ($GRAB == $self) {
1694 $self->{fg} = $self->{active_fg}; 1673 $self->{fg} = $self->{active_fg};
1695 } 1674 }
1696 1675
1697 glEnable GL_BLEND;
1698 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1699 glEnable GL_TEXTURE_2D; 1676 glEnable GL_TEXTURE_2D;
1700 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1677 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1701 glColor 0, 0, 0, 1; 1678 glColor 0, 0, 0, 1;
1702 1679
1703 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1680 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1704 1681
1705 glDisable GL_TEXTURE_2D; 1682 glDisable GL_TEXTURE_2D;
1706 glDisable GL_BLEND;
1707 1683
1708 $self->SUPER::_draw; 1684 $self->SUPER::_draw;
1709} 1685}
1710 1686
1711############################################################################# 1687#############################################################################
1758 1734
1759 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1735 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1760 1736
1761 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1737 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1762 1738
1763 glEnable GL_BLEND;
1764 glEnable GL_TEXTURE_2D; 1739 glEnable GL_TEXTURE_2D;
1765 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1766 1740
1767 my $tex = $self->{state} ? $tex[1] : $tex[0]; 1741 my $tex = $self->{state} ? $tex[1] : $tex[0];
1768 1742
1769 $tex->draw_quad (0, 0, $s, $s); 1743 $tex->draw_quad_alpha (0, 0, $s, $s);
1770 1744
1771 glDisable GL_TEXTURE_2D; 1745 glDisable GL_TEXTURE_2D;
1772 glDisable GL_BLEND;
1773} 1746}
1774 1747
1775############################################################################# 1748#############################################################################
1776 1749
1777package CFClient::UI::Image; 1750package CFClient::UI::Image;
1820 glTranslate 0, -$self->{w}, 0; 1793 glTranslate 0, -$self->{w}, 0;
1821 1794
1822 ($w, $h) = ($h, $w); 1795 ($w, $h) = ($h, $w);
1823 } 1796 }
1824 1797
1825 glEnable GL_BLEND;
1826 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1827 glEnable GL_TEXTURE_2D; 1798 glEnable GL_TEXTURE_2D;
1828 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1799 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1829 1800
1830 $tex->draw_quad (0, 0, $w, $h); 1801 $tex->draw_quad_alpha (0, 0, $w, $h);
1831 1802
1832 glDisable GL_BLEND;
1833 glDisable GL_TEXTURE_2D; 1803 glDisable GL_TEXTURE_2D;
1834} 1804}
1835 1805
1836############################################################################# 1806#############################################################################
1837 1807
2129 $page ||= 2; 2099 $page ||= 2;
2130 2100
2131 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2101 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2132 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2102 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2133 2103
2134 glEnable GL_BLEND;
2135 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2136 glEnable GL_TEXTURE_2D; 2104 glEnable GL_TEXTURE_2D;
2137 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2105 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2138 2106
2139 # draw background 2107 # draw background
2140 $tex[1]->draw_quad (0, 0, $w, $h); 2108 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2141 2109
2142 # draw handle 2110 # draw handle
2143 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2111 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h);
2144 2112
2145 glDisable GL_BLEND;
2146 glDisable GL_TEXTURE_2D; 2113 glDisable GL_TEXTURE_2D;
2147} 2114}
2148 2115
2149############################################################################# 2116#############################################################################
2150 2117
2161 fontsize => 1, 2128 fontsize => 1,
2162 can_events => 0, 2129 can_events => 0,
2163 #font => default_font 2130 #font => default_font
2164 @_, 2131 @_,
2165 2132
2166 layout => (new CFClient::Layout), 2133 layout => (new CFClient::Layout 1),
2167 par => [], 2134 par => [],
2168 height => 0, 2135 height => 0,
2169 children => [ 2136 children => [
2170 (new CFClient::UI::Empty expand => 1), 2137 (new CFClient::UI::Empty expand => 1),
2171 (new CFClient::UI::Slider vertical => 1), 2138 (new CFClient::UI::Slider vertical => 1),
2189 2156
2190 my $layout = $self->{layout}; 2157 my $layout = $self->{layout};
2191 2158
2192 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2159 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2193 $layout->set_width ($self->{children}[0]{w}); 2160 $layout->set_width ($self->{children}[0]{w});
2194 $layout->set_text ($text); 2161 $layout->set_markup ($text);
2195 2162
2196 ($layout->size)[1] 2163 ($layout->size)[1]
2197} 2164}
2198 2165
2199sub reflow { 2166sub reflow {
2255 2222
2256 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2223 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2257 glClearColor 0, 0, 0, 0; 2224 glClearColor 0, 0, 0, 0;
2258 glClear GL_COLOR_BUFFER_BIT; 2225 glClear GL_COLOR_BUFFER_BIT;
2259 2226
2260 glEnable GL_BLEND;
2261 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2262 glEnable GL_TEXTURE_2D; 2227 glEnable GL_TEXTURE_2D;
2263 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2228 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2264 2229
2265 my $top = int $self->{children}[1]{range}[0]; 2230 my $top = int $self->{children}[1]{range}[0];
2266 2231
2275 2240
2276 for my $par (@{$self->{par}}) { 2241 for my $par (@{$self->{par}}) {
2277 my $h = $par->[0]; 2242 my $h = $par->[0];
2278 2243
2279 if ($y0 < $y + $h && $y < $y1) { 2244 if ($y0 < $y + $h && $y < $y1) {
2245 $layout->set_foreground (@{ $par->[1] });
2280 $layout->set_text ($par->[2]); 2246 $layout->set_markup ($par->[2]);
2281 2247
2282 glColor @{ $par->[1] };
2283 my ($W, $H) = $layout->size; 2248 my ($W, $H) = $layout->size;
2284 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2249 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0);
2285 } 2250 }
2286 2251
2287 $y += $h; 2252 $y += $h;
2288 } 2253 }
2289 2254
2290 glDisable GL_TEXTURE_2D; 2255 glDisable GL_TEXTURE_2D;
2291 glDisable GL_BLEND;
2292 }; 2256 };
2293 }); 2257 });
2294} 2258}
2295 2259
2296sub _draw { 2260sub _draw {
2297 my ($self) = @_; 2261 my ($self) = @_;
2298 2262
2299 glEnable GL_BLEND;
2300 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2301 glEnable GL_TEXTURE_2D; 2263 glEnable GL_TEXTURE_2D;
2302 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2264 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2303 glColor 1, 1, 1, 1; 2265 glColor 1, 1, 1, 1;
2304 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2266 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2305 glDisable GL_TEXTURE_2D; 2267 glDisable GL_TEXTURE_2D;
2306 glDisable GL_BLEND;
2307 2268
2308 $self->{children}[1]->draw; 2269 $self->{children}[1]->draw;
2309 2270
2310} 2271}
2311 2272
2405 @_, 2366 @_,
2406 can_events => 0, 2367 can_events => 0,
2407 ) 2368 )
2408} 2369}
2409 2370
2410sub set_markup { 2371sub set_tooltip_from {
2411 my ($self, $text) = @_; 2372 my ($self, $widget) = @_;
2412 2373
2413 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2374 $self->{label} = new CFClient::UI::Label
2414 $self->{label}->set_markup ($text); 2375 markup => $widget->{tooltip},
2376 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2377 fontsize => 0.8,
2378 fg => [0, 0, 0, 1],
2379 font => ($widget->{tooltip_font} || $::FONT_PROP);
2380
2415 $self->add ($self->{label}); 2381 $self->add ($self->{label});
2416} 2382}
2417 2383
2418sub size_request { 2384sub size_request {
2419 my ($self) = @_; 2385 my ($self) = @_;
2420
2421 $self->child->set_max_size ($::WIDTH * 0.3);
2422 2386
2423 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2387 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2424 2388
2425 ($w + 4, $h + 4) 2389 ($w + 4, $h + 4)
2426} 2390}
2488 return unless $::CONN;#d# manage and cache textures differently 2452 return unless $::CONN;#d# manage and cache textures differently
2489 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2453 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2490 2454
2491 # TODO animation 2455 # TODO animation
2492 if ($tex) { 2456 if ($tex) {
2493 glEnable GL_BLEND;
2494 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2495 glEnable GL_TEXTURE_2D; 2457 glEnable GL_TEXTURE_2D;
2496 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2458 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2497 glColor 1, 1, 1, 1; 2459 glColor 1, 1, 1, 1;
2498 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2460 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2499 glDisable GL_TEXTURE_2D; 2461 glDisable GL_TEXTURE_2D;
2500 glDisable GL_BLEND;
2501 } 2462 }
2502} 2463}
2503 2464
2504############################################################################# 2465#############################################################################
2505 2466
2510sub new { 2471sub new {
2511 my $class = shift; 2472 my $class = shift;
2512 2473
2513 my %args = @_; 2474 my %args = @_;
2514 2475
2515 my $item = $args{item}; 2476 my $item = delete $args{item};
2516 2477
2517 my $desc = $item->{nrof} < 2 2478 my $desc = $item->{nrof} < 2
2518 ? $item->{name} 2479 ? $item->{name}
2519 : "$item->{nrof} $item->{name_pl}"; 2480 : "$item->{nrof} $item->{name_pl}";
2520 2481
2549 2510
2550 1 2511 1
2551 }, 2512 },
2552 %args 2513 %args
2553 ); 2514 );
2515
2554 $self->add(new CFClient::UI::Face 2516 $self->add (new CFClient::UI::Face
2555 can_events => 0, 2517 can_events => 0,
2556 face => $item->{face}, 2518 face => $item->{face},
2557 anim => $item->{anim}, 2519 anim => $item->{anim},
2558 animspeed => $item->{animspeed}); 2520 animspeed => $item->{animspeed},
2521 );
2522
2559 $self->add(new CFClient::UI::Label 2523 $self->add (new CFClient::UI::Label
2560 can_events => 0, 2524 can_events => 0,
2561 text => $desc); 2525 text => $desc,
2526 );
2562 2527
2563 $self 2528 $self
2564} 2529}
2565 2530
2566############################################################################# 2531#############################################################################
2593 for my $item (@items) { 2558 for my $item (@items) {
2594 my $desc = $item->{nrof} < 2 2559 my $desc = $item->{nrof} < 2
2595 ? $item->{name} 2560 ? $item->{name}
2596 : "$item->{nrof} $item->{name_pl}"; 2561 : "$item->{nrof} $item->{name_pl}";
2597 2562
2598 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); 2563 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item);
2599 } 2564 }
2600 2565
2601# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2566# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2602} 2567}
2603 2568
2678 } 2643 }
2679} 2644}
2680 2645
2681############################################################################# 2646#############################################################################
2682 2647
2648package CFClient::UI::Statusbox;
2649
2650our @ISA = CFClient::UI::VBox::;
2651
2652sub reorder {
2653 my ($self) = @_;
2654 my $NOW = time;
2655
2656 while (my ($k, $v) = each %{ $self->{item} }) {
2657 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2658 }
2659
2660 my @widgets;
2661 my @items = sort { $a->{time} <=> $b->{time} } values %{ $self->{item} };
2662 my $count = 10 + 1;
2663 for my $item (@items) {
2664 last unless --$count;
2665
2666 push @widgets, $item->{label} ||= do {
2667 # TODO: doesn't handle markup well (read as: at all)
2668 my $short = delete $item->{text};
2669 for ($short) {
2670 s/^\s+//;
2671 s/\012.*//s;
2672 my $len = int 30 / $item->{fontsize};
2673 substr $_, $len, length, "…" if $len < length;
2674 }
2675
2676 new CFClient::UI::Label
2677 markup => $short,
2678 tooltip => delete $item->{tooltip},
2679 tooltip_font => $::FONT_PROP,
2680 tooltip_width => 0.75,
2681 fontsize => delete $item->{fontsize},
2682 color => delete $item->{color},
2683 can_events => 1,
2684 can_hover => 1,
2685 };
2686 }
2687
2688 $self->clear;
2689 $self->SUPER::add (@widgets);
2690}
2691
2692sub add {
2693 my ($self, $text, %arg) = @_;
2694
2695 my $item = {
2696 time => time,
2697 text => $text,
2698 timeout => 60,
2699 tooltip => $text,
2700 fontsize => 0.8,
2701 color => [0.8, 0.8, 0.8, 0.8],
2702 %arg,
2703 };
2704
2705 $item->{timeout} += time;
2706 $item->{group} ||= $item+0;
2707
2708 $item = $self->{item}{$item->{group}} ||= $item;
2709
2710 $self->reorder;
2711}
2712
2713#############################################################################
2714
2683package CFClient::UI::Root; 2715package CFClient::UI::Root;
2684 2716
2685our @ISA = CFClient::UI::Container::; 2717our @ISA = CFClient::UI::Container::;
2686 2718
2687use CFClient::OpenGL; 2719use CFClient::OpenGL;
2688 2720
2721sub new {
2722 my $class = shift;
2723
2724 $class->SUPER::new (
2725 @_,
2726 )
2727}
2728
2729sub configure {
2730 my ($self, $x, $y, $w, $h) = @_;
2731
2732 $self->{w} = $w;
2733 $self->{h} = $h;
2734}
2735
2689sub check_size { 2736sub check_size {
2690 my ($self) = @_; 2737 my ($self) = @_;
2691 2738
2692 $self->configure (0, 0, $self->{w}, $self->{h}); 2739 $self->size_allocate ($self->{w}, $self->{h})
2740 if $self->{w};
2693} 2741}
2694 2742
2695sub size_request { 2743sub size_request {
2696 my ($self) = @_; 2744 my ($self) = @_;
2697 2745
2715 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; 2763 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2716 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; 2764 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2717 } 2765 }
2718 } 2766 }
2719 2767
2720 $self->{old_w} = $w;
2721 $self->{old_h} = $h;
2722}
2723
2724sub configure {
2725 my ($self, $x, $y, $w, $h) = @_;
2726
2727 $self->SUPER::configure ($x, $y, $w, $h);
2728
2729 for my $child ($self->children) { 2768 for my $child ($self->children) {
2730 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2769 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2731 2770
2732 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2771 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2733 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2772 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2734 $child->configure ($X, $Y, $W, $H); 2773 $child->configure ($X, $Y, $W, $H);
2735 } 2774 }
2736}
2737 2775
2738sub _topleft { 2776 $self->{old_w} = $w;
2777 $self->{old_h} = $h;
2778}
2779
2780sub coord2local {
2739 my ($self, $x, $y) = @_; 2781 my ($self, $x, $y) = @_;
2740 2782
2741 ($x, $y) 2783 ($x, $y)
2742} 2784}
2743 2785
2786sub coord2global {
2787 my ($self, $x, $y) = @_;
2788
2789 ($x, $y)
2790}
2791
2744sub update { 2792sub update {
2745 my ($self) = @_; 2793 my ($self) = @_;
2746 2794
2747 $self->check_size; 2795 $self->check_size;
2748 ::refresh (); 2796 $::WANT_REFRESH++;
2749} 2797}
2750 2798
2751sub add { 2799sub add {
2752 my ($self, $child) = @_; 2800 my ($self, $child) = @_;
2753 2801
2754 # integerize window positions 2802 # integerise window positions
2755 $child->{x} = int $child->{x}; 2803 $child->{x} = int $child->{x};
2756 $child->{y} = int $child->{y}; 2804 $child->{y} = int $child->{y};
2757 2805
2758 $self->SUPER::add ($child); 2806 $self->SUPER::add ($child);
2759} 2807}
2762 my ($self, $id, $cb) = @_; 2810 my ($self, $id, $cb) = @_;
2763 2811
2764 $self->{refresh_hook}{$id} = $cb; 2812 $self->{refresh_hook}{$id} = $cb;
2765} 2813}
2766 2814
2767sub draw { 2815sub draw {
2768 my ($self) = @_; 2816 my ($self) = @_;
2769 2817
2818 if ($self->{check_size}) {
2819 my @queue = ([], []);
2820
2821 for (;;) {
2822 if ($self->{check_size}) {
2823 # heuristic: check containers last
2824 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_
2825 for values %{delete $self->{check_size}}
2826 }
2827
2828 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last;
2829
2830 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2831 ? @$widget{qw(user_w user_h)}
2832 : $widget->size_request;
2833
2834 if ($w != $widget->{req_w} || $h != $widget->{req_h}) {
2835 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2836
2837 $widget->{req_w} = $w;
2838 $widget->{req_h} = $h;
2839
2840 $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}];
2841
2842 $widget->{parent}->check_size
2843 if $widget->{parent};
2844 }
2845 }
2846 }
2847
2848 while ($self->{size_alloc}) {
2849 for (values %{delete $self->{size_alloc}}) {
2850 my ($widget, $w, $h) = @$_;
2851
2852 $w = 0 if $w < 0;
2853 $h = 0 if $h < 0;
2854
2855 $widget->{w} = $w;
2856 $widget->{h} = $h;
2857 $widget->size_allocate ($w, $h);
2858 $widget->emit (size_allocate => $w, $h);
2859 }
2860 }
2861
2770 while (my $rcb = delete $self->{refresh_hook}) { 2862 while ($self->{refresh_hook}) {
2771 $_->() for values %$rcb; 2863 $_->()
2864 for values %{delete $self->{refresh_hook}};
2772 } 2865 }
2773 2866
2774 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2867 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2775 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2868 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2776 glClear GL_COLOR_BUFFER_BIT; 2869 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines