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.195 by root, Thu May 11 23:54:31 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_markup ($widget->{tooltip}, $widget->{tooltip_font});
29 30
30 $TOOLTIP->show; 31 $TOOLTIP->show;
31 32
32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 33 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
33 34
232 $self->{y} = $y; 233 $self->{y} = $y;
233 $self->update; 234 $self->update;
234 } 235 }
235 236
236 if ($self->{w} != $w || $self->{h} != $h) { 237 if ($self->{w} != $w || $self->{h} != $h) {
237 $self->{w} = $w; 238 $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 } 239 }
244} 240}
245 241
246sub size_allocate { 242sub size_allocate {
247 # nothing to be done 243 # nothing to be done
256 252
257 $_->reconfigure 253 $_->reconfigure
258 for $self->children; 254 for $self->children;
259 255
260 $self->check_size; 256 $self->check_size;
261 $self->size_allocate ($self->{w}, $self->{h}); 257 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $self->{w}, $self->{h}];
262 $self->update; 258 $self->update;
263} 259}
264 260
265sub set_max_size { 261sub set_max_size {
266 my ($self, $w, $h) = @_; 262 my ($self, $w, $h) = @_;
267 263
268 delete $self->{max_w}; $self->{max_w} = $w if $w; 264 delete $self->{max_w}; $self->{max_w} = $w if $w;
269 delete $self->{max_h}; $self->{max_h} = $h if $h; 265 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} 266}
281 267
282# translate global coordinates to local coordinate system 268# translate global coordinates to local coordinate system
283sub coord2local { 269sub coord2local {
284 my ($self, $x, $y) = @_; 270 my ($self, $x, $y) = @_;
285 271
286 my ($X, $Y) = $self->_topleft; 272 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
287 ($x - $X, $y - $Y)
288} 273}
289 274
290# translate local coordinates to global coordinate system 275# translate local coordinates to global coordinate system
291sub coord2global { 276sub coord2global {
292 my ($self, $x, $y) = @_; 277 my ($self, $x, $y) = @_;
293 278
294 my ($X, $Y) = $self->_topleft; 279 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
295 ($x + $X, $y + $Y)
296} 280}
297 281
298sub focus_in { 282sub focus_in {
299 my ($self) = @_; 283 my ($self) = @_;
300 284
398 382
399sub set_parent { 383sub set_parent {
400 my ($self, $parent) = @_; 384 my ($self, $parent) = @_;
401 385
402 Scalar::Util::weaken ($self->{parent} = $parent); 386 Scalar::Util::weaken ($self->{parent} = $parent);
387
388 $self->check_size
389 unless exists $self->{req_w};
403} 390}
404 391
405sub check_size { 392sub check_size {
406 my ($self) = @_; 393 my ($self) = @_;
407 394
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}; 395 $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} 396}
450 397
451sub update { 398sub update {
452 my ($self) = @_; 399 my ($self) = @_;
453 400
454 my $update = $CFClient::UI::ROOT->{_update} ||= {}; 401 $self->{parent}->update
455 402 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} 403}
468 404
469sub connect { 405sub connect {
470 my ($self, $signal, $cb) = @_; 406 my ($self, $signal, $cb) = @_;
471 407
573 $self->{children} = [ 509 $self->{children} = [
574 sort { $a->{z} <=> $b->{z} } 510 sort { $a->{z} <=> $b->{z} }
575 @{$self->{children}}, @widgets 511 @{$self->{children}}, @widgets
576 ]; 512 ];
577 513
578 $_->check_size 514 $self->check_size;
579 for @widgets;
580
581 $self->update; 515 $self->update;
582} 516}
583 517
584sub children { 518sub children {
585 @{ $_[0]{children} } 519 @{ $_[0]{children} }
733 my ($w, $h) = ($self->w, $self->h); 667 my ($w, $h) = ($self->w, $self->h);
734 668
735 my $tex = $self->{texture} 669 my $tex = $self->{texture}
736 or return; 670 or return;
737 671
738 glEnable GL_BLEND;
739 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
740 glEnable GL_TEXTURE_2D; 672 glEnable GL_TEXTURE_2D;
741 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 673 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
742 glColor 0, 0, 0, 1; 674 glColor 0, 0, 0, 1;
743 675
744 $tex->draw_quad (0, 0, $w, $h); 676 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
745 677
746 glDisable GL_BLEND;
747 glDisable GL_TEXTURE_2D; 678 glDisable GL_TEXTURE_2D;
748} 679}
749 680
750############################################################################# 681#############################################################################
751 682
775 $self->{view_y} = int $y; 706 $self->{view_y} = int $y;
776 707
777 $self->update; 708 $self->update;
778} 709}
779 710
780# hmm, this does not work for topleft of $self... but we should not aks for that 711# hmm, this does not work for topleft of $self... but we should not ask for that
781sub _topleft { 712sub coord2local {
782 my ($self, $x, $y) = @_; 713 my ($self, $x, $y) = @_;
783 714
784 $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y}) 715 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
716}
717
718sub coord2global {
719 my ($self, $x, $y) = @_;
720
721 $x = List::Util::min $self->{w}, $x - $self->{view_x};
722 $y = List::Util::min $self->{h}, $y - $self->{view_y};
723
724 $self->SUPER::coord2global ($x, $y)
785} 725}
786 726
787sub find_widget { 727sub find_widget {
788 my ($self, $x, $y) = @_; 728 my ($self, $x, $y) = @_;
789 729
1271} 1211}
1272 1212
1273sub size_allocate { 1213sub size_allocate {
1274 my ($self, $w, $h) = @_; 1214 my ($self, $w, $h) = @_;
1275 1215
1216 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1217
1276 my $children = $self->{children}; 1218 my $children = $self->{children};
1277 1219
1278 my @h = map $_->{req_h}, @$children; 1220 my @h = map $_->{req_h}, @$children;
1279 1221
1280 my $req_h = List::Util::sum @h; 1222 my $req_h = List::Util::sum @h;
1319 my ($class, %arg) = @_; 1261 my ($class, %arg) = @_;
1320 1262
1321 my $self = $class->SUPER::new ( 1263 my $self = $class->SUPER::new (
1322 fg => [1, 1, 1], 1264 fg => [1, 1, 1],
1323 #font => default_font 1265 #font => default_font
1266 #text => initial text
1267 #markup => initial narkup
1268 layout => (new CFClient::Layout),
1324 fontsize => 1, 1269 fontsize => 1,
1325 text => "",
1326 align => -1, 1270 align => -1,
1327 valign => -1, 1271 valign => -1,
1328 padding => 2, 1272 padding => 2,
1329 layout => new CFClient::Layout,
1330 can_events => 0, 1273 can_events => 0,
1331 %arg 1274 %arg
1332 ); 1275 );
1333 1276
1334 if (exists $self->{template}) { 1277 if (exists $self->{template}) {
1335 my $layout = new CFClient::Layout; 1278 my $layout = new CFClient::Layout;
1336 $layout->set_text (delete $self->{template}); 1279 $layout->set_text (delete $self->{template});
1337 $self->{template} = $layout; 1280 $self->{template} = $layout;
1338 } 1281 }
1339 1282
1340 $self->set_text (delete $self->{text}) if exists $self->{text}; 1283 if (exists $self->{markup}) {
1341 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1284 $self->set_markup (delete $self->{markup});
1285 } else {
1286 $self->set_text (delete $self->{text});
1287 }
1342 1288
1343 $self 1289 $self
1344} 1290}
1345 1291
1346sub escape { 1292sub escape {
1364 my ($self, $text) = @_; 1310 my ($self, $text) = @_;
1365 1311
1366 return if $self->{text} eq "T$text"; 1312 return if $self->{text} eq "T$text";
1367 $self->{text} = "T$text"; 1313 $self->{text} = "T$text";
1368 1314
1315 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1369 $self->{layout}->set_text ($text); 1316 $self->{layout}->set_text ($text);
1370 1317
1371 delete $self->{texture};
1372 $self->update; 1318 $self->update;
1373 $self->check_size; 1319 $self->check_size;
1374} 1320}
1375 1321
1376sub set_markup { 1322sub set_markup {
1377 my ($self, $markup) = @_; 1323 my ($self, $markup) = @_;
1378 1324
1379 return if $self->{text} eq "M$markup"; 1325 return if $self->{text} eq "M$markup";
1380 $self->{text} = "M$markup"; 1326 $self->{text} = "M$markup";
1381 1327
1328 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1329
1330 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1382 $self->{layout}->set_markup ($markup); 1331 $self->{layout}->set_markup ($markup);
1383 1332
1384 delete $self->{texture};
1385 $self->update; 1333 $self->update;
1386 $self->check_size; 1334 $self->check_size;
1387} 1335}
1388 1336
1389sub size_request { 1337sub size_request {
1429 1377
1430sub _draw { 1378sub _draw {
1431 my ($self) = @_; 1379 my ($self) = @_;
1432 1380
1433 my $tex = $self->{texture} ||= do { 1381 my $tex = $self->{texture} ||= do {
1382 $self->{layout}->set_foreground (@{$self->{fg}});
1434 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1383 $self->{layout}->set_font ($self->{font}) if $self->{font};
1435 $self->{layout}->set_width ($self->{w}); 1384 $self->{layout}->set_width ($self->{w});
1436 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1385 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1386
1437 new_from_layout CFClient::Texture $self->{layout} 1387 my $tex = new_from_layout CFClient::Texture $self->{layout};
1388
1389 $self->{ox} = int $self->{align} < 0 ? $self->{padding}
1390 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1391 : ($self->{w} - $tex->{w}) * 0.5;
1392
1393 $self->{oy} = int $self->{valign} < 0 ? $self->{padding}
1394 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1395 : ($self->{h} - $tex->{h}) * 0.5;
1396
1397 $tex
1438 }; 1398 };
1439 1399
1440 glEnable GL_BLEND;
1441 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1442 glEnable GL_TEXTURE_2D; 1400 glEnable GL_TEXTURE_2D;
1443 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1401 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1444 1402
1403 if ($tex->{format} == GL_ALPHA) {
1445 glColor @{$self->{fg}}; 1404 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}); 1405 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1406 } else {
1407 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1408 }
1460 1409
1461 glDisable GL_TEXTURE_2D; 1410 glDisable GL_TEXTURE_2D;
1462 glDisable GL_BLEND;
1463} 1411}
1464 1412
1465############################################################################# 1413#############################################################################
1466 1414
1467package CFClient::UI::EntryBase; 1415package CFClient::UI::EntryBase;
1502 $self->{layout}->set_text ("$text "); 1450 $self->{layout}->set_text ("$text ");
1503 1451
1504 $self->emit (changed => $self->{text}); 1452 $self->emit (changed => $self->{text});
1505} 1453}
1506 1454
1455sub set_text {
1456 my ($self, $text) = @_;
1457
1458 $self->{cursor} = length $text;
1459 $self->_set_text ($text);
1460 $self->check_size;
1461 $self->update;
1462}
1463
1507sub get_text { 1464sub get_text {
1508 $_[0]{text} 1465 $_[0]{text}
1509} 1466}
1510 1467
1511sub size_request { 1468sub size_request {
1518 1475
1519sub size_allocate { 1476sub size_allocate {
1520 my ($self, $w, $h) = @_; 1477 my ($self, $w, $h) = @_;
1521 1478
1522 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text 1479 $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} 1480}
1532 1481
1533sub key_down { 1482sub key_down {
1534 my ($self, $ev) = @_; 1483 my ($self, $ev) = @_;
1535 1484
1723 1672
1724 if ($GRAB == $self) { 1673 if ($GRAB == $self) {
1725 $self->{fg} = $self->{active_fg}; 1674 $self->{fg} = $self->{active_fg};
1726 } 1675 }
1727 1676
1728 glEnable GL_BLEND;
1729 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1730 glEnable GL_TEXTURE_2D; 1677 glEnable GL_TEXTURE_2D;
1731 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1678 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1732 glColor 0, 0, 0, 1; 1679 glColor 0, 0, 0, 1;
1733 1680
1734 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1681 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1735 1682
1736 glDisable GL_TEXTURE_2D; 1683 glDisable GL_TEXTURE_2D;
1737 glDisable GL_BLEND;
1738 1684
1739 $self->SUPER::_draw; 1685 $self->SUPER::_draw;
1740} 1686}
1741 1687
1742############################################################################# 1688#############################################################################
1789 1735
1790 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1736 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1791 1737
1792 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1738 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1793 1739
1794 glEnable GL_BLEND;
1795 glEnable GL_TEXTURE_2D; 1740 glEnable GL_TEXTURE_2D;
1796 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1797 1741
1798 my $tex = $self->{state} ? $tex[1] : $tex[0]; 1742 my $tex = $self->{state} ? $tex[1] : $tex[0];
1799 1743
1800 $tex->draw_quad (0, 0, $s, $s); 1744 $tex->draw_quad_alpha (0, 0, $s, $s);
1801 1745
1802 glDisable GL_TEXTURE_2D; 1746 glDisable GL_TEXTURE_2D;
1803 glDisable GL_BLEND;
1804} 1747}
1805 1748
1806############################################################################# 1749#############################################################################
1807 1750
1808package CFClient::UI::Image; 1751package CFClient::UI::Image;
1851 glTranslate 0, -$self->{w}, 0; 1794 glTranslate 0, -$self->{w}, 0;
1852 1795
1853 ($w, $h) = ($h, $w); 1796 ($w, $h) = ($h, $w);
1854 } 1797 }
1855 1798
1856 glEnable GL_BLEND;
1857 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1858 glEnable GL_TEXTURE_2D; 1799 glEnable GL_TEXTURE_2D;
1859 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1800 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1860 1801
1861 $tex->draw_quad (0, 0, $w, $h); 1802 $tex->draw_quad_alpha (0, 0, $w, $h);
1862 1803
1863 glDisable GL_BLEND;
1864 glDisable GL_TEXTURE_2D; 1804 glDisable GL_TEXTURE_2D;
1865} 1805}
1866 1806
1867############################################################################# 1807#############################################################################
1868 1808
2160 $page ||= 2; 2100 $page ||= 2;
2161 2101
2162 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2102 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2163 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2103 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2164 2104
2165 glEnable GL_BLEND;
2166 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2167 glEnable GL_TEXTURE_2D; 2105 glEnable GL_TEXTURE_2D;
2168 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2106 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2169 2107
2170 # draw background 2108 # draw background
2171 $tex[1]->draw_quad (0, 0, $w, $h); 2109 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2172 2110
2173 # draw handle 2111 # draw handle
2174 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2112 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h);
2175 2113
2176 glDisable GL_BLEND;
2177 glDisable GL_TEXTURE_2D; 2114 glDisable GL_TEXTURE_2D;
2178} 2115}
2179 2116
2180############################################################################# 2117#############################################################################
2181 2118
2192 fontsize => 1, 2129 fontsize => 1,
2193 can_events => 0, 2130 can_events => 0,
2194 #font => default_font 2131 #font => default_font
2195 @_, 2132 @_,
2196 2133
2197 layout => (new CFClient::Layout), 2134 layout => (new CFClient::Layout 1),
2198 par => [], 2135 par => [],
2199 height => 0, 2136 height => 0,
2200 children => [ 2137 children => [
2201 (new CFClient::UI::Empty expand => 1), 2138 (new CFClient::UI::Empty expand => 1),
2202 (new CFClient::UI::Slider vertical => 1), 2139 (new CFClient::UI::Slider vertical => 1),
2220 2157
2221 my $layout = $self->{layout}; 2158 my $layout = $self->{layout};
2222 2159
2223 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2160 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2224 $layout->set_width ($self->{children}[0]{w}); 2161 $layout->set_width ($self->{children}[0]{w});
2225 $layout->set_text ($text); 2162 $layout->set_markup ($text);
2226 2163
2227 ($layout->size)[1] 2164 ($layout->size)[1]
2228} 2165}
2229 2166
2230sub reflow { 2167sub reflow {
2286 2223
2287 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2224 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2288 glClearColor 0, 0, 0, 0; 2225 glClearColor 0, 0, 0, 0;
2289 glClear GL_COLOR_BUFFER_BIT; 2226 glClear GL_COLOR_BUFFER_BIT;
2290 2227
2291 glEnable GL_BLEND;
2292 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2293 glEnable GL_TEXTURE_2D; 2228 glEnable GL_TEXTURE_2D;
2294 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2229 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2295 2230
2296 my $top = int $self->{children}[1]{range}[0]; 2231 my $top = int $self->{children}[1]{range}[0];
2297 2232
2306 2241
2307 for my $par (@{$self->{par}}) { 2242 for my $par (@{$self->{par}}) {
2308 my $h = $par->[0]; 2243 my $h = $par->[0];
2309 2244
2310 if ($y0 < $y + $h && $y < $y1) { 2245 if ($y0 < $y + $h && $y < $y1) {
2246 $layout->set_foreground (@{ $par->[1] });
2311 $layout->set_text ($par->[2]); 2247 $layout->set_markup ($par->[2]);
2312 2248
2313 glColor @{ $par->[1] };
2314 my ($W, $H) = $layout->size; 2249 my ($W, $H) = $layout->size;
2315 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2250 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0);
2316 } 2251 }
2317 2252
2318 $y += $h; 2253 $y += $h;
2319 } 2254 }
2320 2255
2321 glDisable GL_TEXTURE_2D; 2256 glDisable GL_TEXTURE_2D;
2322 glDisable GL_BLEND;
2323 }; 2257 };
2324 }); 2258 });
2325} 2259}
2326 2260
2327sub _draw { 2261sub _draw {
2328 my ($self) = @_; 2262 my ($self) = @_;
2329 2263
2330 glEnable GL_BLEND;
2331 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2332 glEnable GL_TEXTURE_2D; 2264 glEnable GL_TEXTURE_2D;
2333 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2265 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2334 glColor 1, 1, 1, 1; 2266 glColor 1, 1, 1, 1;
2335 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2267 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2336 glDisable GL_TEXTURE_2D; 2268 glDisable GL_TEXTURE_2D;
2337 glDisable GL_BLEND;
2338 2269
2339 $self->{children}[1]->draw; 2270 $self->{children}[1]->draw;
2340 2271
2341} 2272}
2342 2273
2437 can_events => 0, 2368 can_events => 0,
2438 ) 2369 )
2439} 2370}
2440 2371
2441sub set_markup { 2372sub set_markup {
2442 my ($self, $text) = @_; 2373 my ($self, $text, $font) = @_;
2443 2374
2444 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2375 $self->{label} = new CFClient::UI::Label
2376 fontsize => 0.8,
2377 fg => [0, 0, 0],
2378 font => ($font || $::FONT_PROP);
2379
2380 $self->{label}->set_max_size ($::WIDTH * 0.3);
2445 $self->{label}->set_markup ($text); 2381 $self->{label}->set_markup ($text);
2446 $self->add ($self->{label}); 2382 $self->add ($self->{label});
2447} 2383}
2448 2384
2449sub size_request { 2385sub size_request {
2450 my ($self) = @_; 2386 my ($self) = @_;
2451
2452 $self->child->set_max_size ($::WIDTH * 0.3);
2453 2387
2454 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2388 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2455 2389
2456 ($w + 4, $h + 4) 2390 ($w + 4, $h + 4)
2457} 2391}
2519 return unless $::CONN;#d# manage and cache textures differently 2453 return unless $::CONN;#d# manage and cache textures differently
2520 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2454 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2521 2455
2522 # TODO animation 2456 # TODO animation
2523 if ($tex) { 2457 if ($tex) {
2524 glEnable GL_BLEND;
2525 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2526 glEnable GL_TEXTURE_2D; 2458 glEnable GL_TEXTURE_2D;
2527 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2459 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2528 glColor 1, 1, 1, 1; 2460 glColor 1, 1, 1, 1;
2529 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2461 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2530 glDisable GL_TEXTURE_2D; 2462 glDisable GL_TEXTURE_2D;
2531 glDisable GL_BLEND;
2532 } 2463 }
2533} 2464}
2534 2465
2535############################################################################# 2466#############################################################################
2536 2467
2541sub new { 2472sub new {
2542 my $class = shift; 2473 my $class = shift;
2543 2474
2544 my %args = @_; 2475 my %args = @_;
2545 2476
2546 my $item = $args{item}; 2477 my $item = delete $args{item};
2547 2478
2548 my $desc = $item->{nrof} < 2 2479 my $desc = $item->{nrof} < 2
2549 ? $item->{name} 2480 ? $item->{name}
2550 : "$item->{nrof} $item->{name_pl}"; 2481 : "$item->{nrof} $item->{name_pl}";
2551 2482
2580 2511
2581 1 2512 1
2582 }, 2513 },
2583 %args 2514 %args
2584 ); 2515 );
2516
2585 $self->add(new CFClient::UI::Face 2517 $self->add (new CFClient::UI::Face
2586 can_events => 0, 2518 can_events => 0,
2587 face => $item->{face}, 2519 face => $item->{face},
2588 anim => $item->{anim}, 2520 anim => $item->{anim},
2589 animspeed => $item->{animspeed}); 2521 animspeed => $item->{animspeed},
2522 );
2523
2590 $self->add(new CFClient::UI::Label 2524 $self->add (new CFClient::UI::Label
2591 can_events => 0, 2525 can_events => 0,
2592 text => $desc); 2526 text => $desc,
2527 );
2593 2528
2594 $self 2529 $self
2595} 2530}
2596 2531
2597############################################################################# 2532#############################################################################
2624 for my $item (@items) { 2559 for my $item (@items) {
2625 my $desc = $item->{nrof} < 2 2560 my $desc = $item->{nrof} < 2
2626 ? $item->{name} 2561 ? $item->{name}
2627 : "$item->{nrof} $item->{name_pl}"; 2562 : "$item->{nrof} $item->{name_pl}";
2628 2563
2629 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); 2564 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item);
2630 } 2565 }
2631 2566
2632# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2567# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2633} 2568}
2634 2569
2709 } 2644 }
2710} 2645}
2711 2646
2712############################################################################# 2647#############################################################################
2713 2648
2649package CFClient::UI::Statusbox;
2650
2651our @ISA = CFClient::UI::VBox::;
2652
2653sub reorder {
2654 my ($self) = @_;
2655 my $NOW = time;
2656
2657 while (my ($k, $v) = each %{ $self->{item} }) {
2658 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2659 }
2660
2661 my @widgets;
2662 my @items = sort { $a->{time} <=> $b->{time} } values %{ $self->{item} };
2663 my $count = 10 + 1;
2664 for my $item (@items) {
2665 last unless --$count;
2666
2667 push @widgets, $item->{label} ||= do {
2668 # TODO: doesn't handle markup well (read as: at all)
2669 my $short = delete $item->{text};
2670 for ($short) {
2671 s/^\s+//;
2672 s/\012.*//s;
2673 my $len = int 30 / $item->{fontsize};
2674 substr $_, $len, length, "…" if $len < length;
2675 }
2676
2677 new CFClient::UI::Label
2678 markup => $short,
2679 tooltip => delete $item->{tooltip},
2680 fontsize => delete $item->{fontsize},
2681 color => delete $item->{color},
2682 can_events => 1,
2683 can_hover => 1,
2684 };
2685 }
2686
2687 $self->clear;
2688 $self->SUPER::add (@widgets);
2689}
2690
2691sub add {
2692 my ($self, $text, %arg) = @_;
2693
2694 my $item = {
2695 time => time,
2696 text => $text,
2697 timeout => 60,
2698 tooltip => $text,
2699 fontsize => 0.8,
2700 color => [0.8, 0.8, 0.8, 0.8],
2701 %arg,
2702 };
2703
2704 $item->{timeout} += time;
2705 $item->{group} ||= $item+0;
2706
2707 $item = $self->{item}{$item->{group}} ||= $item;
2708
2709 $self->reorder;
2710}
2711
2712#############################################################################
2713
2714package CFClient::UI::Root; 2714package CFClient::UI::Root;
2715 2715
2716our @ISA = CFClient::UI::Container::; 2716our @ISA = CFClient::UI::Container::;
2717 2717
2718use CFClient::OpenGL; 2718use CFClient::OpenGL;
2719 2719
2720sub new {
2721 my $class = shift;
2722
2723 $class->SUPER::new (
2724 @_,
2725 )
2726}
2727
2728sub configure {
2729 my ($self, $x, $y, $w, $h) = @_;
2730
2731 $self->{w} = $w;
2732 $self->{h} = $h;
2733}
2734
2720sub check_size { 2735sub check_size {
2721 my ($self) = @_; 2736 my ($self) = @_;
2722 2737
2723 $self->configure (0, 0, $self->{w}, $self->{h}); 2738 $self->size_allocate ($self->{w}, $self->{h})
2739 if $self->{w};
2724} 2740}
2725 2741
2726sub size_request { 2742sub size_request {
2727 my ($self) = @_; 2743 my ($self) = @_;
2728 2744
2746 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; 2762 $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}; 2763 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2748 } 2764 }
2749 } 2765 }
2750 2766
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) { 2767 for my $child ($self->children) {
2761 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2768 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2762 2769
2763 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2770 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2764 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2771 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2765 $child->configure ($X, $Y, $W, $H); 2772 $child->configure ($X, $Y, $W, $H);
2766 } 2773 }
2767}
2768 2774
2769sub _topleft { 2775 $self->{old_w} = $w;
2776 $self->{old_h} = $h;
2777}
2778
2779sub coord2local {
2770 my ($self, $x, $y) = @_; 2780 my ($self, $x, $y) = @_;
2771 2781
2772 ($x, $y) 2782 ($x, $y)
2773} 2783}
2774 2784
2785sub coord2global {
2786 my ($self, $x, $y) = @_;
2787
2788 ($x, $y)
2789}
2790
2775sub update { 2791sub update {
2776 my ($self) = @_; 2792 my ($self) = @_;
2777 2793
2778 $self->check_size; 2794 $self->check_size;
2779 ::refresh (); 2795 $::WANT_REFRESH++;
2780} 2796}
2781 2797
2782sub add { 2798sub add {
2783 my ($self, $child) = @_; 2799 my ($self, $child) = @_;
2784 2800
2785 # integerize window positions 2801 # integerise window positions
2786 $child->{x} = int $child->{x}; 2802 $child->{x} = int $child->{x};
2787 $child->{y} = int $child->{y}; 2803 $child->{y} = int $child->{y};
2788 2804
2789 $self->SUPER::add ($child); 2805 $self->SUPER::add ($child);
2790} 2806}
2793 my ($self, $id, $cb) = @_; 2809 my ($self, $id, $cb) = @_;
2794 2810
2795 $self->{refresh_hook}{$id} = $cb; 2811 $self->{refresh_hook}{$id} = $cb;
2796} 2812}
2797 2813
2798sub draw { 2814sub draw {
2799 my ($self) = @_; 2815 my ($self) = @_;
2800 2816
2817 if ($self->{check_size}) {
2818 my @queue = ([], []);
2819
2820 for (;;) {
2821 if ($self->{check_size}) {
2822 # heuristic: check containers last
2823 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_
2824 for values %{delete $self->{check_size}}
2825 }
2826
2827 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last;
2828
2829 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2830 ? @$widget{qw(user_w user_h)}
2831 : $widget->size_request;
2832
2833 if ($w != $widget->{req_w} || $h != $widget->{req_h}) {
2834 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2835
2836 $widget->{req_w} = $w;
2837 $widget->{req_h} = $h;
2838
2839 $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}];
2840
2841 $widget->{parent}->check_size
2842 if $widget->{parent};
2843 }
2844 }
2845 }
2846
2847 while ($self->{size_alloc}) {
2848 for (values %{delete $self->{size_alloc}}) {
2849 my ($widget, $w, $h) = @$_;
2850
2851 $w = 0 if $w < 0;
2852 $h = 0 if $h < 0;
2853
2854 $widget->{w} = $w;
2855 $widget->{h} = $h;
2856 $widget->size_allocate ($w, $h);
2857 $widget->emit (size_allocate => $w, $h);
2858 }
2859 }
2860
2801 while (my $rcb = delete $self->{refresh_hook}) { 2861 while ($self->{refresh_hook}) {
2802 $_->() for values %$rcb; 2862 $_->()
2863 for values %{delete $self->{refresh_hook}};
2803 } 2864 }
2804 2865
2805 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2866 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2806 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2867 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2807 glClear GL_COLOR_BUFFER_BIT; 2868 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines