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.189 by root, Mon May 8 22:17:24 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->_check_size;
409 return;
410 my $check_size = $CFClient::UI::ROOT->{_check_size} ||= {};
411
412 $check_size->{$self} = $self;
413
414 $CFClient::UI::ROOT->on_refresh (_check_size => sub {
415 while (%$check_size) {
416 my @widgets = values %$check_size;
417 $_->_check_size
418 for @widgets;
419 }
420 });
421}
422
423sub _check_size {
424 my ($self) = @_;
425
426 delete $CFClient::UI::ROOT->{_check_size}{$self}; 394 $CFClient::UI::ROOT->{check_size}{$self} = $self;
427
428 $self->{parent}
429 or return 1;
430
431 my ($w, $h) = $self->{user_w} && $self->{user_h}
432 ? @$self{qw(user_w user_h)}
433 : $self->size_request;
434
435 if ($w != $self->{req_w} || $h != $self->{req_h}) {
436 $self->{req_w} = $w;
437 $self->{req_h} = $h;
438
439 $self->{parent}->_check_size
440 or $self->size_allocate (
441 (List::Util::max $self->{w}, $w),
442 (List::Util::max $self->{h}, $h),
443 );
444
445 1
446 } else {
447 0
448 }
449} 395}
450 396
451sub update { 397sub update {
452 my ($self) = @_; 398 my ($self) = @_;
453 399
454 my $update = $CFClient::UI::ROOT->{_update} ||= {}; 400 $self->{parent}->update
455 401 if $self->{parent};
456 $update->{$self} = $self;
457
458 $CFClient::UI::ROOT->on_refresh (_update => sub {
459 while (%$update) {
460 my @widgets = values %$update;
461 %$update = ();
462
463 $_->{parent} && $_->{parent}->update
464 for @widgets;
465 }
466 });
467} 402}
468 403
469sub connect { 404sub connect {
470 my ($self, $signal, $cb) = @_; 405 my ($self, $signal, $cb) = @_;
471 406
573 $self->{children} = [ 508 $self->{children} = [
574 sort { $a->{z} <=> $b->{z} } 509 sort { $a->{z} <=> $b->{z} }
575 @{$self->{children}}, @widgets 510 @{$self->{children}}, @widgets
576 ]; 511 ];
577 512
578 $_->check_size 513 $self->check_size;
579 for @widgets;
580
581 $self->update; 514 $self->update;
582} 515}
583 516
584sub children { 517sub children {
585 @{ $_[0]{children} } 518 @{ $_[0]{children} }
733 my ($w, $h) = ($self->w, $self->h); 666 my ($w, $h) = ($self->w, $self->h);
734 667
735 my $tex = $self->{texture} 668 my $tex = $self->{texture}
736 or return; 669 or return;
737 670
738 glEnable GL_BLEND;
739 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
740 glEnable GL_TEXTURE_2D; 671 glEnable GL_TEXTURE_2D;
741 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 672 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
742 glColor 0, 0, 0, 1; 673 glColor 0, 0, 0, 1;
743 674
744 $tex->draw_quad (0, 0, $w, $h); 675 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
745 676
746 glDisable GL_BLEND;
747 glDisable GL_TEXTURE_2D; 677 glDisable GL_TEXTURE_2D;
748} 678}
749 679
750############################################################################# 680#############################################################################
751 681
775 $self->{view_y} = int $y; 705 $self->{view_y} = int $y;
776 706
777 $self->update; 707 $self->update;
778} 708}
779 709
780# 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
781sub _topleft { 711sub coord2local {
782 my ($self, $x, $y) = @_; 712 my ($self, $x, $y) = @_;
783 713
784 $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)
785} 724}
786 725
787sub find_widget { 726sub find_widget {
788 my ($self, $x, $y) = @_; 727 my ($self, $x, $y) = @_;
789 728
1271} 1210}
1272 1211
1273sub size_allocate { 1212sub size_allocate {
1274 my ($self, $w, $h) = @_; 1213 my ($self, $w, $h) = @_;
1275 1214
1215 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1216
1276 my $children = $self->{children}; 1217 my $children = $self->{children};
1277 1218
1278 my @h = map $_->{req_h}, @$children; 1219 my @h = map $_->{req_h}, @$children;
1279 1220
1280 my $req_h = List::Util::sum @h; 1221 my $req_h = List::Util::sum @h;
1319 my ($class, %arg) = @_; 1260 my ($class, %arg) = @_;
1320 1261
1321 my $self = $class->SUPER::new ( 1262 my $self = $class->SUPER::new (
1322 fg => [1, 1, 1], 1263 fg => [1, 1, 1],
1323 #font => default_font 1264 #font => default_font
1265 #text => initial text
1266 #markup => initial narkup
1267 layout => (new CFClient::Layout),
1324 fontsize => 1, 1268 fontsize => 1,
1325 text => "",
1326 align => -1, 1269 align => -1,
1327 valign => -1, 1270 valign => -1,
1328 padding => 2, 1271 padding => 2,
1329 layout => new CFClient::Layout,
1330 can_events => 0, 1272 can_events => 0,
1331 %arg 1273 %arg
1332 ); 1274 );
1333 1275
1334 if (exists $self->{template}) { 1276 if (exists $self->{template}) {
1335 my $layout = new CFClient::Layout; 1277 my $layout = new CFClient::Layout;
1336 $layout->set_text (delete $self->{template}); 1278 $layout->set_text (delete $self->{template});
1337 $self->{template} = $layout; 1279 $self->{template} = $layout;
1338 } 1280 }
1339 1281
1340 $self->set_text (delete $self->{text}) if exists $self->{text}; 1282 if (exists $self->{markup}) {
1341 $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 }
1342 1287
1343 $self 1288 $self
1344} 1289}
1345 1290
1346sub escape { 1291sub escape {
1364 my ($self, $text) = @_; 1309 my ($self, $text) = @_;
1365 1310
1366 return if $self->{text} eq "T$text"; 1311 return if $self->{text} eq "T$text";
1367 $self->{text} = "T$text"; 1312 $self->{text} = "T$text";
1368 1313
1314 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1369 $self->{layout}->set_text ($text); 1315 $self->{layout}->set_text ($text);
1370 1316
1371 delete $self->{texture};
1372 $self->update; 1317 $self->update;
1373 $self->check_size; 1318 $self->check_size;
1374} 1319}
1375 1320
1376sub set_markup { 1321sub set_markup {
1377 my ($self, $markup) = @_; 1322 my ($self, $markup) = @_;
1378 1323
1379 return if $self->{text} eq "M$markup"; 1324 return if $self->{text} eq "M$markup";
1380 $self->{text} = "M$markup"; 1325 $self->{text} = "M$markup";
1381 1326
1327 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1328
1329 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1382 $self->{layout}->set_markup ($markup); 1330 $self->{layout}->set_markup ($markup);
1383 1331
1384 delete $self->{texture};
1385 $self->update; 1332 $self->update;
1386 $self->check_size; 1333 $self->check_size;
1387} 1334}
1388 1335
1389sub size_request { 1336sub size_request {
1429 1376
1430sub _draw { 1377sub _draw {
1431 my ($self) = @_; 1378 my ($self) = @_;
1432 1379
1433 my $tex = $self->{texture} ||= do { 1380 my $tex = $self->{texture} ||= do {
1381 $self->{layout}->set_foreground (@{$self->{fg}});
1434 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1382 $self->{layout}->set_font ($self->{font}) if $self->{font};
1435 $self->{layout}->set_width ($self->{w}); 1383 $self->{layout}->set_width ($self->{w});
1436 $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
1437 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
1438 }; 1397 };
1439 1398
1440 glEnable GL_BLEND;
1441 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1442 glEnable GL_TEXTURE_2D; 1399 glEnable GL_TEXTURE_2D;
1443 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1400 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1444 1401
1402 if ($tex->{format} == GL_ALPHA) {
1445 glColor @{$self->{fg}}; 1403 glColor @{$self->{fg}};
1446
1447 $self->{ox} = int (
1448 $self->{align} < 0 ? $self->{padding}
1449 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1450 : ($self->{w} - $tex->{w}) * 0.5
1451 );
1452
1453 $self->{oy} = int (
1454 $self->{valign} < 0 ? $self->{padding}
1455 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1456 : ($self->{h} - $tex->{h}) * 0.5
1457 );
1458
1459 $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 }
1460 1408
1461 glDisable GL_TEXTURE_2D; 1409 glDisable GL_TEXTURE_2D;
1462 glDisable GL_BLEND;
1463} 1410}
1464 1411
1465############################################################################# 1412#############################################################################
1466 1413
1467package CFClient::UI::EntryBase; 1414package CFClient::UI::EntryBase;
1502 $self->{layout}->set_text ("$text "); 1449 $self->{layout}->set_text ("$text ");
1503 1450
1504 $self->emit (changed => $self->{text}); 1451 $self->emit (changed => $self->{text});
1505} 1452}
1506 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
1507sub get_text { 1463sub get_text {
1508 $_[0]{text} 1464 $_[0]{text}
1509} 1465}
1510 1466
1511sub size_request { 1467sub size_request {
1518 1474
1519sub size_allocate { 1475sub size_allocate {
1520 my ($self, $w, $h) = @_; 1476 my ($self, $w, $h) = @_;
1521 1477
1522 $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
1523}
1524
1525sub set_text {
1526 my ($self, $text) = @_;
1527
1528 $self->{cursor} = length $text;
1529 $self->_set_text ($text);
1530 $self->update;
1531} 1479}
1532 1480
1533sub key_down { 1481sub key_down {
1534 my ($self, $ev) = @_; 1482 my ($self, $ev) = @_;
1535 1483
1723 1671
1724 if ($GRAB == $self) { 1672 if ($GRAB == $self) {
1725 $self->{fg} = $self->{active_fg}; 1673 $self->{fg} = $self->{active_fg};
1726 } 1674 }
1727 1675
1728 glEnable GL_BLEND;
1729 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1730 glEnable GL_TEXTURE_2D; 1676 glEnable GL_TEXTURE_2D;
1731 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1677 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1732 glColor 0, 0, 0, 1; 1678 glColor 0, 0, 0, 1;
1733 1679
1734 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1680 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1735 1681
1736 glDisable GL_TEXTURE_2D; 1682 glDisable GL_TEXTURE_2D;
1737 glDisable GL_BLEND;
1738 1683
1739 $self->SUPER::_draw; 1684 $self->SUPER::_draw;
1740} 1685}
1741 1686
1742############################################################################# 1687#############################################################################
1789 1734
1790 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;
1791 1736
1792 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1737 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1793 1738
1794 glEnable GL_BLEND;
1795 glEnable GL_TEXTURE_2D; 1739 glEnable GL_TEXTURE_2D;
1796 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1797 1740
1798 my $tex = $self->{state} ? $tex[1] : $tex[0]; 1741 my $tex = $self->{state} ? $tex[1] : $tex[0];
1799 1742
1800 $tex->draw_quad (0, 0, $s, $s); 1743 $tex->draw_quad_alpha (0, 0, $s, $s);
1801 1744
1802 glDisable GL_TEXTURE_2D; 1745 glDisable GL_TEXTURE_2D;
1803 glDisable GL_BLEND;
1804} 1746}
1805 1747
1806############################################################################# 1748#############################################################################
1807 1749
1808package CFClient::UI::Image; 1750package CFClient::UI::Image;
1851 glTranslate 0, -$self->{w}, 0; 1793 glTranslate 0, -$self->{w}, 0;
1852 1794
1853 ($w, $h) = ($h, $w); 1795 ($w, $h) = ($h, $w);
1854 } 1796 }
1855 1797
1856 glEnable GL_BLEND;
1857 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1858 glEnable GL_TEXTURE_2D; 1798 glEnable GL_TEXTURE_2D;
1859 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1799 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1860 1800
1861 $tex->draw_quad (0, 0, $w, $h); 1801 $tex->draw_quad_alpha (0, 0, $w, $h);
1862 1802
1863 glDisable GL_BLEND;
1864 glDisable GL_TEXTURE_2D; 1803 glDisable GL_TEXTURE_2D;
1865} 1804}
1866 1805
1867############################################################################# 1806#############################################################################
1868 1807
2160 $page ||= 2; 2099 $page ||= 2;
2161 2100
2162 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2101 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2163 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2102 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2164 2103
2165 glEnable GL_BLEND;
2166 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2167 glEnable GL_TEXTURE_2D; 2104 glEnable GL_TEXTURE_2D;
2168 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2105 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2169 2106
2170 # draw background 2107 # draw background
2171 $tex[1]->draw_quad (0, 0, $w, $h); 2108 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2172 2109
2173 # draw handle 2110 # draw handle
2174 $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);
2175 2112
2176 glDisable GL_BLEND;
2177 glDisable GL_TEXTURE_2D; 2113 glDisable GL_TEXTURE_2D;
2178} 2114}
2179 2115
2180############################################################################# 2116#############################################################################
2181 2117
2192 fontsize => 1, 2128 fontsize => 1,
2193 can_events => 0, 2129 can_events => 0,
2194 #font => default_font 2130 #font => default_font
2195 @_, 2131 @_,
2196 2132
2197 layout => (new CFClient::Layout), 2133 layout => (new CFClient::Layout 1),
2198 par => [], 2134 par => [],
2199 height => 0, 2135 height => 0,
2200 children => [ 2136 children => [
2201 (new CFClient::UI::Empty expand => 1), 2137 (new CFClient::UI::Empty expand => 1),
2202 (new CFClient::UI::Slider vertical => 1), 2138 (new CFClient::UI::Slider vertical => 1),
2220 2156
2221 my $layout = $self->{layout}; 2157 my $layout = $self->{layout};
2222 2158
2223 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2159 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2224 $layout->set_width ($self->{children}[0]{w}); 2160 $layout->set_width ($self->{children}[0]{w});
2225 $layout->set_text ($text); 2161 $layout->set_markup ($text);
2226 2162
2227 ($layout->size)[1] 2163 ($layout->size)[1]
2228} 2164}
2229 2165
2230sub reflow { 2166sub reflow {
2286 2222
2287 $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 {
2288 glClearColor 0, 0, 0, 0; 2224 glClearColor 0, 0, 0, 0;
2289 glClear GL_COLOR_BUFFER_BIT; 2225 glClear GL_COLOR_BUFFER_BIT;
2290 2226
2291 glEnable GL_BLEND;
2292 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2293 glEnable GL_TEXTURE_2D; 2227 glEnable GL_TEXTURE_2D;
2294 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2228 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2295 2229
2296 my $top = int $self->{children}[1]{range}[0]; 2230 my $top = int $self->{children}[1]{range}[0];
2297 2231
2306 2240
2307 for my $par (@{$self->{par}}) { 2241 for my $par (@{$self->{par}}) {
2308 my $h = $par->[0]; 2242 my $h = $par->[0];
2309 2243
2310 if ($y0 < $y + $h && $y < $y1) { 2244 if ($y0 < $y + $h && $y < $y1) {
2245 $layout->set_foreground (@{ $par->[1] });
2311 $layout->set_text ($par->[2]); 2246 $layout->set_markup ($par->[2]);
2312 2247
2313 glColor @{ $par->[1] };
2314 my ($W, $H) = $layout->size; 2248 my ($W, $H) = $layout->size;
2315 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);
2316 } 2250 }
2317 2251
2318 $y += $h; 2252 $y += $h;
2319 } 2253 }
2320 2254
2321 glDisable GL_TEXTURE_2D; 2255 glDisable GL_TEXTURE_2D;
2322 glDisable GL_BLEND;
2323 }; 2256 };
2324 }); 2257 });
2325} 2258}
2326 2259
2327sub _draw { 2260sub _draw {
2328 my ($self) = @_; 2261 my ($self) = @_;
2329 2262
2330 glEnable GL_BLEND;
2331 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2332 glEnable GL_TEXTURE_2D; 2263 glEnable GL_TEXTURE_2D;
2333 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2264 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2334 glColor 1, 1, 1, 1; 2265 glColor 1, 1, 1, 1;
2335 $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});
2336 glDisable GL_TEXTURE_2D; 2267 glDisable GL_TEXTURE_2D;
2337 glDisable GL_BLEND;
2338 2268
2339 $self->{children}[1]->draw; 2269 $self->{children}[1]->draw;
2340 2270
2341} 2271}
2342 2272
2436 @_, 2366 @_,
2437 can_events => 0, 2367 can_events => 0,
2438 ) 2368 )
2439} 2369}
2440 2370
2441sub set_markup { 2371sub set_tooltip_from {
2442 my ($self, $text) = @_; 2372 my ($self, $widget) = @_;
2443 2373
2444 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2374 $self->{label} = new CFClient::UI::Label
2445 $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
2446 $self->add ($self->{label}); 2381 $self->add ($self->{label});
2447} 2382}
2448 2383
2449sub size_request { 2384sub size_request {
2450 my ($self) = @_; 2385 my ($self) = @_;
2451
2452 $self->child->set_max_size ($::WIDTH * 0.3);
2453 2386
2454 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2387 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2455 2388
2456 ($w + 4, $h + 4) 2389 ($w + 4, $h + 4)
2457} 2390}
2519 return unless $::CONN;#d# manage and cache textures differently 2452 return unless $::CONN;#d# manage and cache textures differently
2520 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2453 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2521 2454
2522 # TODO animation 2455 # TODO animation
2523 if ($tex) { 2456 if ($tex) {
2524 glEnable GL_BLEND;
2525 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2526 glEnable GL_TEXTURE_2D; 2457 glEnable GL_TEXTURE_2D;
2527 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2458 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2528 glColor 1, 1, 1, 1; 2459 glColor 1, 1, 1, 1;
2529 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2460 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2530 glDisable GL_TEXTURE_2D; 2461 glDisable GL_TEXTURE_2D;
2531 glDisable GL_BLEND;
2532 } 2462 }
2533} 2463}
2534 2464
2535############################################################################# 2465#############################################################################
2536 2466
2541sub new { 2471sub new {
2542 my $class = shift; 2472 my $class = shift;
2543 2473
2544 my %args = @_; 2474 my %args = @_;
2545 2475
2546 my $item = $args{item}; 2476 my $item = delete $args{item};
2547 2477
2548 my $desc = $item->{nrof} < 2 2478 my $desc = $item->{nrof} < 2
2549 ? $item->{name} 2479 ? $item->{name}
2550 : "$item->{nrof} $item->{name_pl}"; 2480 : "$item->{nrof} $item->{name_pl}";
2551 2481
2580 2510
2581 1 2511 1
2582 }, 2512 },
2583 %args 2513 %args
2584 ); 2514 );
2515
2585 $self->add(new CFClient::UI::Face 2516 $self->add (new CFClient::UI::Face
2586 can_events => 0, 2517 can_events => 0,
2587 face => $item->{face}, 2518 face => $item->{face},
2588 anim => $item->{anim}, 2519 anim => $item->{anim},
2589 animspeed => $item->{animspeed}); 2520 animspeed => $item->{animspeed},
2521 );
2522
2590 $self->add(new CFClient::UI::Label 2523 $self->add (new CFClient::UI::Label
2591 can_events => 0, 2524 can_events => 0,
2592 text => $desc); 2525 text => $desc,
2526 );
2593 2527
2594 $self 2528 $self
2595} 2529}
2596 2530
2597############################################################################# 2531#############################################################################
2624 for my $item (@items) { 2558 for my $item (@items) {
2625 my $desc = $item->{nrof} < 2 2559 my $desc = $item->{nrof} < 2
2626 ? $item->{name} 2560 ? $item->{name}
2627 : "$item->{nrof} $item->{name_pl}"; 2561 : "$item->{nrof} $item->{name_pl}";
2628 2562
2629 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); 2563 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item);
2630 } 2564 }
2631 2565
2632# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2566# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2633} 2567}
2634 2568
2709 } 2643 }
2710} 2644}
2711 2645
2712############################################################################# 2646#############################################################################
2713 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
2714package CFClient::UI::Root; 2715package CFClient::UI::Root;
2715 2716
2716our @ISA = CFClient::UI::Container::; 2717our @ISA = CFClient::UI::Container::;
2717 2718
2718use CFClient::OpenGL; 2719use CFClient::OpenGL;
2719 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
2720sub check_size { 2736sub check_size {
2721 my ($self) = @_; 2737 my ($self) = @_;
2722 2738
2723 $self->configure (0, 0, $self->{w}, $self->{h}); 2739 $self->size_allocate ($self->{w}, $self->{h})
2740 if $self->{w};
2724} 2741}
2725 2742
2726sub size_request { 2743sub size_request {
2727 my ($self) = @_; 2744 my ($self) = @_;
2728 2745
2746 $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};
2747 $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};
2748 } 2765 }
2749 } 2766 }
2750 2767
2751 $self->{old_w} = $w;
2752 $self->{old_h} = $h;
2753}
2754
2755sub configure {
2756 my ($self, $x, $y, $w, $h) = @_;
2757
2758 $self->SUPER::configure ($x, $y, $w, $h);
2759
2760 for my $child ($self->children) { 2768 for my $child ($self->children) {
2761 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)};
2762 2770
2763 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2771 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2764 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2772 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2765 $child->configure ($X, $Y, $W, $H); 2773 $child->configure ($X, $Y, $W, $H);
2766 } 2774 }
2767}
2768 2775
2769sub _topleft { 2776 $self->{old_w} = $w;
2777 $self->{old_h} = $h;
2778}
2779
2780sub coord2local {
2770 my ($self, $x, $y) = @_; 2781 my ($self, $x, $y) = @_;
2771 2782
2772 ($x, $y) 2783 ($x, $y)
2773} 2784}
2774 2785
2786sub coord2global {
2787 my ($self, $x, $y) = @_;
2788
2789 ($x, $y)
2790}
2791
2775sub update { 2792sub update {
2776 my ($self) = @_; 2793 my ($self) = @_;
2777 2794
2778 $self->check_size; 2795 $self->check_size;
2779 ::refresh (); 2796 $::WANT_REFRESH++;
2780} 2797}
2781 2798
2782sub add { 2799sub add {
2783 my ($self, $child) = @_; 2800 my ($self, $child) = @_;
2784 2801
2785 # integerize window positions 2802 # integerise window positions
2786 $child->{x} = int $child->{x}; 2803 $child->{x} = int $child->{x};
2787 $child->{y} = int $child->{y}; 2804 $child->{y} = int $child->{y};
2788 2805
2789 $self->SUPER::add ($child); 2806 $self->SUPER::add ($child);
2790} 2807}
2793 my ($self, $id, $cb) = @_; 2810 my ($self, $id, $cb) = @_;
2794 2811
2795 $self->{refresh_hook}{$id} = $cb; 2812 $self->{refresh_hook}{$id} = $cb;
2796} 2813}
2797 2814
2798sub draw { 2815sub draw {
2799 my ($self) = @_; 2816 my ($self) = @_;
2800 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
2801 while (my $rcb = delete $self->{refresh_hook}) { 2862 while ($self->{refresh_hook}) {
2802 $_->() for values %$rcb; 2863 $_->()
2864 for values %{delete $self->{refresh_hook}};
2803 } 2865 }
2804 2866
2805 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2867 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2806 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2868 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2807 glClear GL_COLOR_BUFFER_BIT; 2869 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines