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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines