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.186 by root, Mon May 8 20:39:34 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
530 496
531 $self 497 $self
532} 498}
533 499
534sub add { 500sub add {
535 my ($self, $child) = @_; 501 my ($self, @widgets) = @_;
536 502
537 $child->set_parent ($self); 503 $_->set_parent ($self)
504 for @widgets;
538 505
539 use sort 'stable'; 506 use sort 'stable';
540 507
541 $self->{children} = [ 508 $self->{children} = [
542 sort { $a->{z} <=> $b->{z} } 509 sort { $a->{z} <=> $b->{z} }
543 @{$self->{children}}, $child 510 @{$self->{children}}, @widgets
544 ]; 511 ];
545 512
546 $child->check_size; 513 $self->check_size;
547 $self->update; 514 $self->update;
548} 515}
549 516
550sub children { 517sub children {
551 @{ $_[0]{children} } 518 @{ $_[0]{children} }
699 my ($w, $h) = ($self->w, $self->h); 666 my ($w, $h) = ($self->w, $self->h);
700 667
701 my $tex = $self->{texture} 668 my $tex = $self->{texture}
702 or return; 669 or return;
703 670
704 glEnable GL_BLEND;
705 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
706 glEnable GL_TEXTURE_2D; 671 glEnable GL_TEXTURE_2D;
707 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 672 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
708 glColor 0, 0, 0, 1; 673 glColor 0, 0, 0, 1;
709 674
710 $tex->draw_quad (0, 0, $w, $h); 675 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
711 676
712 glDisable GL_BLEND;
713 glDisable GL_TEXTURE_2D; 677 glDisable GL_TEXTURE_2D;
714} 678}
715 679
716############################################################################# 680#############################################################################
717 681
741 $self->{view_y} = int $y; 705 $self->{view_y} = int $y;
742 706
743 $self->update; 707 $self->update;
744} 708}
745 709
746# 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
747sub _topleft { 711sub coord2local {
748 my ($self, $x, $y) = @_; 712 my ($self, $x, $y) = @_;
749 713
750 $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)
751} 724}
752 725
753sub find_widget { 726sub find_widget {
754 my ($self, $x, $y) = @_; 727 my ($self, $x, $y) = @_;
755 728
1237} 1210}
1238 1211
1239sub size_allocate { 1212sub size_allocate {
1240 my ($self, $w, $h) = @_; 1213 my ($self, $w, $h) = @_;
1241 1214
1215 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1216
1242 my $children = $self->{children}; 1217 my $children = $self->{children};
1243 1218
1244 my @h = map $_->{req_h}, @$children; 1219 my @h = map $_->{req_h}, @$children;
1245 1220
1246 my $req_h = List::Util::sum @h; 1221 my $req_h = List::Util::sum @h;
1285 my ($class, %arg) = @_; 1260 my ($class, %arg) = @_;
1286 1261
1287 my $self = $class->SUPER::new ( 1262 my $self = $class->SUPER::new (
1288 fg => [1, 1, 1], 1263 fg => [1, 1, 1],
1289 #font => default_font 1264 #font => default_font
1265 #text => initial text
1266 #markup => initial narkup
1267 layout => (new CFClient::Layout),
1290 fontsize => 1, 1268 fontsize => 1,
1291 text => "",
1292 align => -1, 1269 align => -1,
1293 valign => -1, 1270 valign => -1,
1294 padding => 2, 1271 padding => 2,
1295 layout => new CFClient::Layout,
1296 can_events => 0, 1272 can_events => 0,
1297 %arg 1273 %arg
1298 ); 1274 );
1299 1275
1300 if (exists $self->{template}) { 1276 if (exists $self->{template}) {
1301 my $layout = new CFClient::Layout; 1277 my $layout = new CFClient::Layout;
1302 $layout->set_text (delete $self->{template}); 1278 $layout->set_text (delete $self->{template});
1303 $self->{template} = $layout; 1279 $self->{template} = $layout;
1304 } 1280 }
1305 1281
1306 $self->set_text (delete $self->{text}) if exists $self->{text}; 1282 if (exists $self->{markup}) {
1307 $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 }
1308 1287
1309 $self 1288 $self
1310} 1289}
1311 1290
1312sub escape { 1291sub escape {
1330 my ($self, $text) = @_; 1309 my ($self, $text) = @_;
1331 1310
1332 return if $self->{text} eq "T$text"; 1311 return if $self->{text} eq "T$text";
1333 $self->{text} = "T$text"; 1312 $self->{text} = "T$text";
1334 1313
1314 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1335 $self->{layout}->set_text ($text); 1315 $self->{layout}->set_text ($text);
1336 1316
1337 delete $self->{texture};
1338 $self->update; 1317 $self->update;
1339 $self->check_size; 1318 $self->check_size;
1340} 1319}
1341 1320
1342sub set_markup { 1321sub set_markup {
1343 my ($self, $markup) = @_; 1322 my ($self, $markup) = @_;
1344 1323
1345 return if $self->{text} eq "M$markup"; 1324 return if $self->{text} eq "M$markup";
1346 $self->{text} = "M$markup"; 1325 $self->{text} = "M$markup";
1347 1326
1327 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1328
1329 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1348 $self->{layout}->set_markup ($markup); 1330 $self->{layout}->set_markup ($markup);
1349 1331
1350 delete $self->{texture};
1351 $self->update; 1332 $self->update;
1352 $self->check_size; 1333 $self->check_size;
1353} 1334}
1354 1335
1355sub size_request { 1336sub size_request {
1395 1376
1396sub _draw { 1377sub _draw {
1397 my ($self) = @_; 1378 my ($self) = @_;
1398 1379
1399 my $tex = $self->{texture} ||= do { 1380 my $tex = $self->{texture} ||= do {
1381 $self->{layout}->set_foreground (@{$self->{fg}});
1400 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1382 $self->{layout}->set_font ($self->{font}) if $self->{font};
1401 $self->{layout}->set_width ($self->{w}); 1383 $self->{layout}->set_width ($self->{w});
1402 $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
1403 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
1404 }; 1397 };
1405 1398
1406 glEnable GL_BLEND;
1407 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1408 glEnable GL_TEXTURE_2D; 1399 glEnable GL_TEXTURE_2D;
1409 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1400 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1410 1401
1402 if ($tex->{format} == GL_ALPHA) {
1411 glColor @{$self->{fg}}; 1403 glColor @{$self->{fg}};
1412
1413 $self->{ox} = int (
1414 $self->{align} < 0 ? $self->{padding}
1415 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1416 : ($self->{w} - $tex->{w}) * 0.5
1417 );
1418
1419 $self->{oy} = int (
1420 $self->{valign} < 0 ? $self->{padding}
1421 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1422 : ($self->{h} - $tex->{h}) * 0.5
1423 );
1424
1425 $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 }
1426 1408
1427 glDisable GL_TEXTURE_2D; 1409 glDisable GL_TEXTURE_2D;
1428 glDisable GL_BLEND;
1429} 1410}
1430 1411
1431############################################################################# 1412#############################################################################
1432 1413
1433package CFClient::UI::EntryBase; 1414package CFClient::UI::EntryBase;
1468 $self->{layout}->set_text ("$text "); 1449 $self->{layout}->set_text ("$text ");
1469 1450
1470 $self->emit (changed => $self->{text}); 1451 $self->emit (changed => $self->{text});
1471} 1452}
1472 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
1473sub get_text { 1463sub get_text {
1474 $_[0]{text} 1464 $_[0]{text}
1475} 1465}
1476 1466
1477sub size_request { 1467sub size_request {
1484 1474
1485sub size_allocate { 1475sub size_allocate {
1486 my ($self, $w, $h) = @_; 1476 my ($self, $w, $h) = @_;
1487 1477
1488 $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
1489}
1490
1491sub set_text {
1492 my ($self, $text) = @_;
1493
1494 $self->{cursor} = length $text;
1495 $self->_set_text ($text);
1496 $self->update;
1497} 1479}
1498 1480
1499sub key_down { 1481sub key_down {
1500 my ($self, $ev) = @_; 1482 my ($self, $ev) = @_;
1501 1483
1689 1671
1690 if ($GRAB == $self) { 1672 if ($GRAB == $self) {
1691 $self->{fg} = $self->{active_fg}; 1673 $self->{fg} = $self->{active_fg};
1692 } 1674 }
1693 1675
1694 glEnable GL_BLEND;
1695 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1696 glEnable GL_TEXTURE_2D; 1676 glEnable GL_TEXTURE_2D;
1697 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1677 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1698 glColor 0, 0, 0, 1; 1678 glColor 0, 0, 0, 1;
1699 1679
1700 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1680 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1701 1681
1702 glDisable GL_TEXTURE_2D; 1682 glDisable GL_TEXTURE_2D;
1703 glDisable GL_BLEND;
1704 1683
1705 $self->SUPER::_draw; 1684 $self->SUPER::_draw;
1706} 1685}
1707 1686
1708############################################################################# 1687#############################################################################
1755 1734
1756 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;
1757 1736
1758 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1737 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1759 1738
1760 glEnable GL_BLEND;
1761 glEnable GL_TEXTURE_2D; 1739 glEnable GL_TEXTURE_2D;
1762 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1763 1740
1764 my $tex = $self->{state} ? $tex[1] : $tex[0]; 1741 my $tex = $self->{state} ? $tex[1] : $tex[0];
1765 1742
1766 $tex->draw_quad (0, 0, $s, $s); 1743 $tex->draw_quad_alpha (0, 0, $s, $s);
1767 1744
1768 glDisable GL_TEXTURE_2D; 1745 glDisable GL_TEXTURE_2D;
1769 glDisable GL_BLEND;
1770} 1746}
1771 1747
1772############################################################################# 1748#############################################################################
1773 1749
1774package CFClient::UI::Image; 1750package CFClient::UI::Image;
1817 glTranslate 0, -$self->{w}, 0; 1793 glTranslate 0, -$self->{w}, 0;
1818 1794
1819 ($w, $h) = ($h, $w); 1795 ($w, $h) = ($h, $w);
1820 } 1796 }
1821 1797
1822 glEnable GL_BLEND;
1823 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1824 glEnable GL_TEXTURE_2D; 1798 glEnable GL_TEXTURE_2D;
1825 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1799 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1826 1800
1827 $tex->draw_quad (0, 0, $w, $h); 1801 $tex->draw_quad_alpha (0, 0, $w, $h);
1828 1802
1829 glDisable GL_BLEND;
1830 glDisable GL_TEXTURE_2D; 1803 glDisable GL_TEXTURE_2D;
1831} 1804}
1832 1805
1833############################################################################# 1806#############################################################################
1834 1807
2126 $page ||= 2; 2099 $page ||= 2;
2127 2100
2128 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2101 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2129 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2102 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2130 2103
2131 glEnable GL_BLEND;
2132 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2133 glEnable GL_TEXTURE_2D; 2104 glEnable GL_TEXTURE_2D;
2134 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2105 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2135 2106
2136 # draw background 2107 # draw background
2137 $tex[1]->draw_quad (0, 0, $w, $h); 2108 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2138 2109
2139 # draw handle 2110 # draw handle
2140 $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);
2141 2112
2142 glDisable GL_BLEND;
2143 glDisable GL_TEXTURE_2D; 2113 glDisable GL_TEXTURE_2D;
2144} 2114}
2145 2115
2146############################################################################# 2116#############################################################################
2147 2117
2158 fontsize => 1, 2128 fontsize => 1,
2159 can_events => 0, 2129 can_events => 0,
2160 #font => default_font 2130 #font => default_font
2161 @_, 2131 @_,
2162 2132
2163 layout => (new CFClient::Layout), 2133 layout => (new CFClient::Layout 1),
2164 par => [], 2134 par => [],
2165 height => 0, 2135 height => 0,
2166 children => [ 2136 children => [
2167 (new CFClient::UI::Empty expand => 1), 2137 (new CFClient::UI::Empty expand => 1),
2168 (new CFClient::UI::Slider vertical => 1), 2138 (new CFClient::UI::Slider vertical => 1),
2186 2156
2187 my $layout = $self->{layout}; 2157 my $layout = $self->{layout};
2188 2158
2189 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2159 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2190 $layout->set_width ($self->{children}[0]{w}); 2160 $layout->set_width ($self->{children}[0]{w});
2191 $layout->set_text ($text); 2161 $layout->set_markup ($text);
2192 2162
2193 ($layout->size)[1] 2163 ($layout->size)[1]
2194} 2164}
2195 2165
2196sub reflow { 2166sub reflow {
2252 2222
2253 $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 {
2254 glClearColor 0, 0, 0, 0; 2224 glClearColor 0, 0, 0, 0;
2255 glClear GL_COLOR_BUFFER_BIT; 2225 glClear GL_COLOR_BUFFER_BIT;
2256 2226
2257 glEnable GL_BLEND;
2258 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2259 glEnable GL_TEXTURE_2D; 2227 glEnable GL_TEXTURE_2D;
2260 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2228 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2261 2229
2262 my $top = int $self->{children}[1]{range}[0]; 2230 my $top = int $self->{children}[1]{range}[0];
2263 2231
2272 2240
2273 for my $par (@{$self->{par}}) { 2241 for my $par (@{$self->{par}}) {
2274 my $h = $par->[0]; 2242 my $h = $par->[0];
2275 2243
2276 if ($y0 < $y + $h && $y < $y1) { 2244 if ($y0 < $y + $h && $y < $y1) {
2245 $layout->set_foreground (@{ $par->[1] });
2277 $layout->set_text ($par->[2]); 2246 $layout->set_markup ($par->[2]);
2278 2247
2279 glColor @{ $par->[1] };
2280 my ($W, $H) = $layout->size; 2248 my ($W, $H) = $layout->size;
2281 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);
2282 } 2250 }
2283 2251
2284 $y += $h; 2252 $y += $h;
2285 } 2253 }
2286 2254
2287 glDisable GL_TEXTURE_2D; 2255 glDisable GL_TEXTURE_2D;
2288 glDisable GL_BLEND;
2289 }; 2256 };
2290 }); 2257 });
2291} 2258}
2292 2259
2293sub _draw { 2260sub _draw {
2294 my ($self) = @_; 2261 my ($self) = @_;
2295 2262
2296 glEnable GL_BLEND;
2297 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2298 glEnable GL_TEXTURE_2D; 2263 glEnable GL_TEXTURE_2D;
2299 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2264 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2300 glColor 1, 1, 1, 1; 2265 glColor 1, 1, 1, 1;
2301 $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});
2302 glDisable GL_TEXTURE_2D; 2267 glDisable GL_TEXTURE_2D;
2303 glDisable GL_BLEND;
2304 2268
2305 $self->{children}[1]->draw; 2269 $self->{children}[1]->draw;
2306 2270
2307} 2271}
2308 2272
2402 @_, 2366 @_,
2403 can_events => 0, 2367 can_events => 0,
2404 ) 2368 )
2405} 2369}
2406 2370
2407sub set_markup { 2371sub set_tooltip_from {
2408 my ($self, $text) = @_; 2372 my ($self, $widget) = @_;
2409 2373
2410 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2374 $self->{label} = new CFClient::UI::Label
2411 $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
2412 $self->add ($self->{label}); 2381 $self->add ($self->{label});
2413} 2382}
2414 2383
2415sub size_request { 2384sub size_request {
2416 my ($self) = @_; 2385 my ($self) = @_;
2417
2418 $self->child->set_max_size ($::WIDTH * 0.3);
2419 2386
2420 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2387 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2421 2388
2422 ($w + 4, $h + 4) 2389 ($w + 4, $h + 4)
2423} 2390}
2485 return unless $::CONN;#d# manage and cache textures differently 2452 return unless $::CONN;#d# manage and cache textures differently
2486 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2453 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2487 2454
2488 # TODO animation 2455 # TODO animation
2489 if ($tex) { 2456 if ($tex) {
2490 glEnable GL_BLEND;
2491 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2492 glEnable GL_TEXTURE_2D; 2457 glEnable GL_TEXTURE_2D;
2493 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2458 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2494 glColor 1, 1, 1, 1; 2459 glColor 1, 1, 1, 1;
2495 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2460 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2496 glDisable GL_TEXTURE_2D; 2461 glDisable GL_TEXTURE_2D;
2497 glDisable GL_BLEND;
2498 } 2462 }
2499} 2463}
2500 2464
2501############################################################################# 2465#############################################################################
2502 2466
2507sub new { 2471sub new {
2508 my $class = shift; 2472 my $class = shift;
2509 2473
2510 my %args = @_; 2474 my %args = @_;
2511 2475
2512 my $item = $args{item}; 2476 my $item = delete $args{item};
2513 2477
2514 my $desc = $item->{nrof} < 2 2478 my $desc = $item->{nrof} < 2
2515 ? $item->{name} 2479 ? $item->{name}
2516 : "$item->{nrof} $item->{name_pl}"; 2480 : "$item->{nrof} $item->{name_pl}";
2517 2481
2546 2510
2547 1 2511 1
2548 }, 2512 },
2549 %args 2513 %args
2550 ); 2514 );
2515
2551 $self->add(new CFClient::UI::Face 2516 $self->add (new CFClient::UI::Face
2552 can_events => 0, 2517 can_events => 0,
2553 face => $item->{face}, 2518 face => $item->{face},
2554 anim => $item->{anim}, 2519 anim => $item->{anim},
2555 animspeed => $item->{animspeed}); 2520 animspeed => $item->{animspeed},
2521 );
2522
2556 $self->add(new CFClient::UI::Label 2523 $self->add (new CFClient::UI::Label
2557 can_events => 0, 2524 can_events => 0,
2558 text => $desc); 2525 text => $desc,
2526 );
2559 2527
2560 $self 2528 $self
2561} 2529}
2562 2530
2563############################################################################# 2531#############################################################################
2590 for my $item (@items) { 2558 for my $item (@items) {
2591 my $desc = $item->{nrof} < 2 2559 my $desc = $item->{nrof} < 2
2592 ? $item->{name} 2560 ? $item->{name}
2593 : "$item->{nrof} $item->{name_pl}"; 2561 : "$item->{nrof} $item->{name_pl}";
2594 2562
2595 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); 2563 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item);
2596 } 2564 }
2597 2565
2598# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2566# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2599} 2567}
2600 2568
2675 } 2643 }
2676} 2644}
2677 2645
2678############################################################################# 2646#############################################################################
2679 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
2680package CFClient::UI::Root; 2715package CFClient::UI::Root;
2681 2716
2682our @ISA = CFClient::UI::Container::; 2717our @ISA = CFClient::UI::Container::;
2683 2718
2684use CFClient::OpenGL; 2719use CFClient::OpenGL;
2685 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
2686sub check_size { 2736sub check_size {
2687 my ($self) = @_; 2737 my ($self) = @_;
2688 2738
2689 $self->configure (0, 0, $self->{w}, $self->{h}); 2739 $self->size_allocate ($self->{w}, $self->{h})
2740 if $self->{w};
2690} 2741}
2691 2742
2692sub size_request { 2743sub size_request {
2693 my ($self) = @_; 2744 my ($self) = @_;
2694 2745
2702 my $old_h = $self->{old_h}; 2753 my $old_h = $self->{old_h};
2703 2754
2704 if ($old_w && $old_h) { 2755 if ($old_w && $old_h) {
2705 for my $child ($self->children) { 2756 for my $child ($self->children) {
2706 $child->{x} = int 0.5 + $child->{x} * $w / $old_w; 2757 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2707 $child->{w} = int 0.5 + $child->{req_w} * $w / $old_w; 2758 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2708 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; 2759 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2709 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; 2760 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2710 $child->{y} = int 0.5 + $child->{y} * $h / $old_h; 2761 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2711 $child->{h} = int 0.5 + $child->{h} * $h / $old_h; 2762 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2712 $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};
2713 $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};
2714 } 2765 }
2715 } 2766 }
2716 2767
2717 $self->{old_w} = $w;
2718 $self->{old_h} = $h;
2719}
2720
2721sub configure {
2722 my ($self, $x, $y, $w, $h) = @_;
2723
2724 $self->SUPER::configure ($x, $y, $w, $h);
2725
2726 for my $child ($self->children) { 2768 for my $child ($self->children) {
2727 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)};
2728 2770
2729 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2771 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2730 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2772 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2731 $child->configure ($X, $Y, $W, $H); 2773 $child->configure ($X, $Y, $W, $H);
2732 } 2774 }
2733}
2734 2775
2735sub _topleft { 2776 $self->{old_w} = $w;
2777 $self->{old_h} = $h;
2778}
2779
2780sub coord2local {
2736 my ($self, $x, $y) = @_; 2781 my ($self, $x, $y) = @_;
2737 2782
2738 ($x, $y) 2783 ($x, $y)
2739} 2784}
2740 2785
2786sub coord2global {
2787 my ($self, $x, $y) = @_;
2788
2789 ($x, $y)
2790}
2791
2741sub update { 2792sub update {
2742 my ($self) = @_; 2793 my ($self) = @_;
2743 2794
2744 $self->check_size; 2795 $self->check_size;
2745 ::refresh (); 2796 $::WANT_REFRESH++;
2746} 2797}
2747 2798
2748sub add { 2799sub add {
2749 my ($self, $child) = @_; 2800 my ($self, $child) = @_;
2750 2801
2751 # integerize window positions 2802 # integerise window positions
2752 $child->{x} = int $child->{x}; 2803 $child->{x} = int $child->{x};
2753 $child->{y} = int $child->{y}; 2804 $child->{y} = int $child->{y};
2754 2805
2755 $self->SUPER::add ($child); 2806 $self->SUPER::add ($child);
2756} 2807}
2759 my ($self, $id, $cb) = @_; 2810 my ($self, $id, $cb) = @_;
2760 2811
2761 $self->{refresh_hook}{$id} = $cb; 2812 $self->{refresh_hook}{$id} = $cb;
2762} 2813}
2763 2814
2764sub draw { 2815sub draw {
2765 my ($self) = @_; 2816 my ($self) = @_;
2766 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
2767 while (my $rcb = delete $self->{refresh_hook}) { 2862 while ($self->{refresh_hook}) {
2768 $_->() for values %$rcb; 2863 $_->()
2864 for values %{delete $self->{refresh_hook}};
2769 } 2865 }
2770 2866
2771 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2867 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2772 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2868 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2773 glClear GL_COLOR_BUFFER_BIT; 2869 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines