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.191 by root, Tue May 9 22:27:53 2006 UTC vs.
Revision 1.198 by root, Fri May 12 02:08:52 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) {
35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); 35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0);
36 } 36 }
37 37
38 $TOOLTIP->move ($x, $y); 38 $TOOLTIP->move ($x, $y);
39 $TOOLTIP->check_size;
40 $TOOLTIP->update;
39 } 41 }
40 42
41 return; 43 return;
42 } 44 }
43 } 45 }
250 my ($self) = @_; 252 my ($self) = @_;
251 253
252 $_->reconfigure 254 $_->reconfigure
253 for $self->children; 255 for $self->children;
254 256
255 $self->check_size; 257 $self->check_size (1);
256 $self->size_allocate ($self->{w}, $self->{h});
257 $self->update; 258 $self->update;
258} 259}
259 260
260sub set_max_size { 261sub set_max_size {
261 my ($self, $w, $h) = @_; 262 my ($self, $w, $h) = @_;
380} 381}
381 382
382sub set_parent { 383sub set_parent {
383 my ($self, $parent) = @_; 384 my ($self, $parent) = @_;
384 385
385 $self->{_tree_depth} = $parent->{_tree_depth} + 1;
386 Scalar::Util::weaken ($self->{parent} = $parent); 386 Scalar::Util::weaken ($self->{parent} = $parent);
387
388
389 # TODO: req_w _does_change after ->reconfigure
390 $self->check_size
391 unless exists $self->{req_w};
387} 392}
388 393
389sub check_size { 394sub check_size {
390 my ($self) = @_; 395 my ($self, $forced) = @_;
391 396
397 $self->{force_alloc} = 1 if $forced;
392 $CFClient::UI::ROOT->{check_size}{$self} = $self; 398 $CFClient::UI::ROOT->{check_size}{$self} = $self;
393} 399}
394 400
395sub update { 401sub update {
396 my ($self) = @_; 402 my ($self) = @_;
506 $self->{children} = [ 512 $self->{children} = [
507 sort { $a->{z} <=> $b->{z} } 513 sort { $a->{z} <=> $b->{z} }
508 @{$self->{children}}, @widgets 514 @{$self->{children}}, @widgets
509 ]; 515 ];
510 516
511 $_->check_size 517 $self->check_size (1);
512 for @widgets;
513
514 $self->update; 518 $self->update;
515} 519}
516 520
517sub children { 521sub children {
518 @{ $_[0]{children} } 522 @{ $_[0]{children} }
625} 629}
626 630
627sub update { 631sub update {
628 my ($self) = @_; 632 my ($self) = @_;
629 633
630 $ROOT->on_refresh ($self => sub { $self->render_child }); 634 $ROOT->on_post_alloc ($self => sub { $self->render_child });
631 $self->SUPER::update; 635 $self->SUPER::update;
632} 636}
633 637
634sub size_allocate { 638sub size_allocate {
635 my ($self, $w, $h) = @_; 639 my ($self, $w, $h) = @_;
666 my ($w, $h) = ($self->w, $self->h); 670 my ($w, $h) = ($self->w, $self->h);
667 671
668 my $tex = $self->{texture} 672 my $tex = $self->{texture}
669 or return; 673 or return;
670 674
671 glEnable GL_BLEND;
672 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
673 glEnable GL_TEXTURE_2D; 675 glEnable GL_TEXTURE_2D;
674 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 676 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
675 glColor 0, 0, 0, 1; 677 glColor 0, 0, 0, 1;
676 678
677 $tex->draw_quad (0, 0, $w, $h); 679 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
678 680
679 glDisable GL_BLEND;
680 glDisable GL_TEXTURE_2D; 681 glDisable GL_TEXTURE_2D;
681} 682}
682 683
683############################################################################# 684#############################################################################
684 685
787 788
788our @ISA = CFClient::UI::Bin::; 789our @ISA = CFClient::UI::Bin::;
789 790
790use CFClient::OpenGL; 791use CFClient::OpenGL;
791 792
792sub new {
793 my $class = shift;
794
795 my $self = $class->SUPER::new (
796 bg => [1, 1, 1, 1],
797 border_bg => [1, 1, 1, 1],
798 border => 0.8,
799 @_
800 );
801
802 $self
803}
804
805sub _draw {
806 my ($self) = @_;
807
808 my ($w, $h) = ($self->{w}, $self->{h});
809
810 glEnable GL_BLEND;
811 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
812 glEnable GL_TEXTURE_2D;
813 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
814
815# glBegin GL_QUADS;
816# glColor 0, 0, 0, 0;
817# glVertex 0 , 0;
818# glVertex 0 , $h;
819# glVertex $w, $h;
820# glVertex $w, 0;
821# glEnd;
822
823
824 $self->child->draw;
825 glDisable GL_BLEND;
826 glDisable GL_TEXTURE_2D;
827}
828
829############################################################################# 793#############################################################################
830 794
831package CFClient::UI::FancyFrame; 795package CFClient::UI::FancyFrame;
832 796
833our @ISA = CFClient::UI::Bin::; 797our @ISA = CFClient::UI::Bin::;
947 my ($self) = @_; 911 my ($self) = @_;
948 912
949 my ($w, $h ) = ($self->{w}, $self->{h}); 913 my ($w, $h ) = ($self->{w}, $self->{h});
950 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 914 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
951 915
952 glEnable GL_BLEND;
953 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
954 glEnable GL_TEXTURE_2D; 916 glEnable GL_TEXTURE_2D;
955 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 917 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
956 918
957 my $border = $self->border; 919 my $border = $self->border;
958 920
959 glColor @{ $self->{border_bg} }; 921 glColor @{ $self->{border_bg} };
960 $tex[1]->draw_quad (0, 0, $w, $border); 922 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
961 $tex[3]->draw_quad (0, $border, $border, $ch); 923 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
962 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 924 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
963 $tex[4]->draw_quad (0, $h - $border, $w, $border); 925 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
964 926
965 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 927 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
966 my $bg = $tex[0]; 928 my $bg = $tex[0];
967 929
968 # TODO: repeat texture not scale 930 # TODO: repeat texture not scale
972 glColor @{ $self->{bg} }; 934 glColor @{ $self->{bg} };
973 935
974 $bg->{s} = $rep_x; 936 $bg->{s} = $rep_x;
975 $bg->{t} = $rep_y; 937 $bg->{t} = $rep_y;
976 $bg->{wrap_mode} = 1; 938 $bg->{wrap_mode} = 1;
977 $bg->draw_quad ($border, $border, $cw, $ch); 939 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
940 }
978 941
979 glDisable GL_TEXTURE_2D; 942 glDisable GL_TEXTURE_2D;
980 glDisable GL_BLEND;
981 }
982 943
983 $self->{title}->draw if $self->{title}; 944 $self->{title}->draw if $self->{title};
984 945
985 $self->child->draw; 946 $self->child->draw;
986} 947}
1213} 1174}
1214 1175
1215sub size_allocate { 1176sub size_allocate {
1216 my ($self, $w, $h) = @_; 1177 my ($self, $w, $h) = @_;
1217 1178
1179 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1180
1218 my $children = $self->{children}; 1181 my $children = $self->{children};
1219 1182
1220 my @h = map $_->{req_h}, @$children; 1183 my @h = map $_->{req_h}, @$children;
1221 1184
1222 my $req_h = List::Util::sum @h; 1185 my $req_h = List::Util::sum @h;
1261 my ($class, %arg) = @_; 1224 my ($class, %arg) = @_;
1262 1225
1263 my $self = $class->SUPER::new ( 1226 my $self = $class->SUPER::new (
1264 fg => [1, 1, 1], 1227 fg => [1, 1, 1],
1265 #font => default_font 1228 #font => default_font
1229 #text => initial text
1230 #markup => initial narkup
1231 layout => (new CFClient::Layout),
1266 fontsize => 1, 1232 fontsize => 1,
1267 text => "",
1268 align => -1, 1233 align => -1,
1269 valign => -1, 1234 valign => -1,
1270 padding => 2, 1235 padding => 2,
1271 layout => new CFClient::Layout,
1272 can_events => 0, 1236 can_events => 0,
1273 %arg 1237 %arg
1274 ); 1238 );
1275 1239
1276 if (exists $self->{template}) { 1240 if (exists $self->{template}) {
1277 my $layout = new CFClient::Layout; 1241 my $layout = new CFClient::Layout;
1278 $layout->set_text (delete $self->{template}); 1242 $layout->set_text (delete $self->{template});
1279 $self->{template} = $layout; 1243 $self->{template} = $layout;
1280 } 1244 }
1281 1245
1282 $self->set_text (delete $self->{text}) if exists $self->{text}; 1246 if (exists $self->{markup}) {
1283 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1247 $self->set_markup (delete $self->{markup});
1248 } else {
1249 $self->set_text (delete $self->{text});
1250 }
1284 1251
1285 $self 1252 $self
1286} 1253}
1287 1254
1288sub escape { 1255sub escape {
1306 my ($self, $text) = @_; 1273 my ($self, $text) = @_;
1307 1274
1308 return if $self->{text} eq "T$text"; 1275 return if $self->{text} eq "T$text";
1309 $self->{text} = "T$text"; 1276 $self->{text} = "T$text";
1310 1277
1278 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1311 $self->{layout}->set_text ($text); 1279 $self->{layout}->set_text ($text);
1312 1280
1313 $self->update; 1281 $self->update;
1314 $self->check_size; 1282 $self->check_size;
1315} 1283}
1318 my ($self, $markup) = @_; 1286 my ($self, $markup) = @_;
1319 1287
1320 return if $self->{text} eq "M$markup"; 1288 return if $self->{text} eq "M$markup";
1321 $self->{text} = "M$markup"; 1289 $self->{text} = "M$markup";
1322 1290
1291 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1292
1293 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1323 $self->{layout}->set_markup ($markup); 1294 $self->{layout}->set_markup ($markup);
1324 1295
1325 $self->update; 1296 $self->update;
1326 $self->check_size; 1297 $self->check_size;
1327} 1298}
1369 1340
1370sub _draw { 1341sub _draw {
1371 my ($self) = @_; 1342 my ($self) = @_;
1372 1343
1373 my $tex = $self->{texture} ||= do { 1344 my $tex = $self->{texture} ||= do {
1345 $self->{layout}->set_foreground (@{$self->{fg}});
1374 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1346 $self->{layout}->set_font ($self->{font}) if $self->{font};
1375 $self->{layout}->set_width ($self->{w}); 1347 $self->{layout}->set_width ($self->{w});
1376 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1348 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1349
1377 new_from_layout CFClient::Texture $self->{layout} 1350 my $tex = new_from_layout CFClient::Texture $self->{layout};
1351
1352 $self->{ox} = int $self->{align} < 0 ? $self->{padding}
1353 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1354 : ($self->{w} - $tex->{w}) * 0.5;
1355
1356 $self->{oy} = int $self->{valign} < 0 ? $self->{padding}
1357 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1358 : ($self->{h} - $tex->{h}) * 0.5;
1359
1360 $tex
1378 }; 1361 };
1379 1362
1380 glEnable GL_BLEND;
1381 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1382 glEnable GL_TEXTURE_2D; 1363 glEnable GL_TEXTURE_2D;
1383 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1364 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1384 1365
1366 if ($tex->{format} == GL_ALPHA) {
1385 glColor @{$self->{fg}}; 1367 glColor @{$self->{fg}};
1386
1387 $self->{ox} = int (
1388 $self->{align} < 0 ? $self->{padding}
1389 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1390 : ($self->{w} - $tex->{w}) * 0.5
1391 );
1392
1393 $self->{oy} = int (
1394 $self->{valign} < 0 ? $self->{padding}
1395 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1396 : ($self->{h} - $tex->{h}) * 0.5
1397 );
1398
1399 $tex->draw_quad ($self->{ox}, $self->{oy}); 1368 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1369 } else {
1370 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1371 }
1400 1372
1401 glDisable GL_TEXTURE_2D; 1373 glDisable GL_TEXTURE_2D;
1402 glDisable GL_BLEND;
1403} 1374}
1404 1375
1405############################################################################# 1376#############################################################################
1406 1377
1407package CFClient::UI::EntryBase; 1378package CFClient::UI::EntryBase;
1442 $self->{layout}->set_text ("$text "); 1413 $self->{layout}->set_text ("$text ");
1443 1414
1444 $self->emit (changed => $self->{text}); 1415 $self->emit (changed => $self->{text});
1445} 1416}
1446 1417
1418sub set_text {
1419 my ($self, $text) = @_;
1420
1421 $self->{cursor} = length $text;
1422 $self->_set_text ($text);
1423 $self->check_size;
1424 $self->update;
1425}
1426
1447sub get_text { 1427sub get_text {
1448 $_[0]{text} 1428 $_[0]{text}
1449} 1429}
1450 1430
1451sub size_request { 1431sub size_request {
1458 1438
1459sub size_allocate { 1439sub size_allocate {
1460 my ($self, $w, $h) = @_; 1440 my ($self, $w, $h) = @_;
1461 1441
1462 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text 1442 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1463}
1464
1465sub set_text {
1466 my ($self, $text) = @_;
1467
1468 $self->{cursor} = length $text;
1469 $self->_set_text ($text);
1470 $self->update;
1471} 1443}
1472 1444
1473sub key_down { 1445sub key_down {
1474 my ($self, $ev) = @_; 1446 my ($self, $ev) = @_;
1475 1447
1663 1635
1664 if ($GRAB == $self) { 1636 if ($GRAB == $self) {
1665 $self->{fg} = $self->{active_fg}; 1637 $self->{fg} = $self->{active_fg};
1666 } 1638 }
1667 1639
1668 glEnable GL_BLEND;
1669 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1670 glEnable GL_TEXTURE_2D; 1640 glEnable GL_TEXTURE_2D;
1671 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1641 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1672 glColor 0, 0, 0, 1; 1642 glColor 0, 0, 0, 1;
1673 1643
1674 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1644 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1675 1645
1676 glDisable GL_TEXTURE_2D; 1646 glDisable GL_TEXTURE_2D;
1677 glDisable GL_BLEND;
1678 1647
1679 $self->SUPER::_draw; 1648 $self->SUPER::_draw;
1680} 1649}
1681 1650
1682############################################################################# 1651#############################################################################
1729 1698
1730 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1699 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1731 1700
1732 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1701 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1733 1702
1734 glEnable GL_BLEND; 1703 my $tex = $self->{state} ? $tex[1] : $tex[0];
1704
1735 glEnable GL_TEXTURE_2D; 1705 glEnable GL_TEXTURE_2D;
1736 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1737
1738 my $tex = $self->{state} ? $tex[1] : $tex[0];
1739
1740 $tex->draw_quad (0, 0, $s, $s); 1706 $tex->draw_quad_alpha (0, 0, $s, $s);
1741
1742 glDisable GL_TEXTURE_2D; 1707 glDisable GL_TEXTURE_2D;
1743 glDisable GL_BLEND;
1744} 1708}
1745 1709
1746############################################################################# 1710#############################################################################
1747 1711
1748package CFClient::UI::Image; 1712package CFClient::UI::Image;
1791 glTranslate 0, -$self->{w}, 0; 1755 glTranslate 0, -$self->{w}, 0;
1792 1756
1793 ($w, $h) = ($h, $w); 1757 ($w, $h) = ($h, $w);
1794 } 1758 }
1795 1759
1796 glEnable GL_BLEND;
1797 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1798 glEnable GL_TEXTURE_2D; 1760 glEnable GL_TEXTURE_2D;
1799 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1761 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1800 1762
1801 $tex->draw_quad (0, 0, $w, $h); 1763 $tex->draw_quad_alpha (0, 0, $w, $h);
1802 1764
1803 glDisable GL_BLEND;
1804 glDisable GL_TEXTURE_2D; 1765 glDisable GL_TEXTURE_2D;
1805} 1766}
1806 1767
1807############################################################################# 1768#############################################################################
1808 1769
2100 $page ||= 2; 2061 $page ||= 2;
2101 2062
2102 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2063 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2103 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2064 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2104 2065
2105 glEnable GL_BLEND;
2106 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2107 glEnable GL_TEXTURE_2D; 2066 glEnable GL_TEXTURE_2D;
2108 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2067 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2109 2068
2110 # draw background 2069 # draw background
2111 $tex[1]->draw_quad (0, 0, $w, $h); 2070 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2112 2071
2113 # draw handle 2072 # draw handle
2114 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2073 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h);
2115 2074
2116 glDisable GL_BLEND;
2117 glDisable GL_TEXTURE_2D; 2075 glDisable GL_TEXTURE_2D;
2118} 2076}
2119 2077
2120############################################################################# 2078#############################################################################
2121 2079
2132 fontsize => 1, 2090 fontsize => 1,
2133 can_events => 0, 2091 can_events => 0,
2134 #font => default_font 2092 #font => default_font
2135 @_, 2093 @_,
2136 2094
2137 layout => (new CFClient::Layout), 2095 layout => (new CFClient::Layout 1),
2138 par => [], 2096 par => [],
2139 height => 0, 2097 height => 0,
2140 children => [ 2098 children => [
2141 (new CFClient::UI::Empty expand => 1), 2099 (new CFClient::UI::Empty expand => 1),
2142 (new CFClient::UI::Slider vertical => 1), 2100 (new CFClient::UI::Slider vertical => 1),
2160 2118
2161 my $layout = $self->{layout}; 2119 my $layout = $self->{layout};
2162 2120
2163 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2121 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2164 $layout->set_width ($self->{children}[0]{w}); 2122 $layout->set_width ($self->{children}[0]{w});
2165 $layout->set_text ($text); 2123 $layout->set_markup ($text);
2166 2124
2167 ($layout->size)[1] 2125 ($layout->size)[1]
2168} 2126}
2169 2127
2170sub reflow { 2128sub reflow {
2208 2166
2209 return unless $self->{h} > 0; 2167 return unless $self->{h} > 0;
2210 2168
2211 delete $self->{texture}; 2169 delete $self->{texture};
2212 2170
2213 $ROOT->on_refresh ($self, sub { 2171 $ROOT->on_post_alloc ($self, sub {
2214 if (delete $self->{need_reflow}) { 2172 if (delete $self->{need_reflow}) {
2215 my $height = 0; 2173 my $height = 0;
2216 2174
2217 $height += $_->[0] = $self->text_height ($_->[2]) 2175 $height += $_->[0] = $self->text_height ($_->[2])
2218 for @{$self->{par}}; 2176 for @{$self->{par}};
2226 2184
2227 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2185 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2228 glClearColor 0, 0, 0, 0; 2186 glClearColor 0, 0, 0, 0;
2229 glClear GL_COLOR_BUFFER_BIT; 2187 glClear GL_COLOR_BUFFER_BIT;
2230 2188
2231 glEnable GL_BLEND;
2232 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2233 glEnable GL_TEXTURE_2D; 2189 glEnable GL_TEXTURE_2D;
2234 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2190 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2235 2191
2236 my $top = int $self->{children}[1]{range}[0]; 2192 my $top = int $self->{children}[1]{range}[0];
2237 2193
2246 2202
2247 for my $par (@{$self->{par}}) { 2203 for my $par (@{$self->{par}}) {
2248 my $h = $par->[0]; 2204 my $h = $par->[0];
2249 2205
2250 if ($y0 < $y + $h && $y < $y1) { 2206 if ($y0 < $y + $h && $y < $y1) {
2207 $layout->set_foreground (@{ $par->[1] });
2251 $layout->set_text ($par->[2]); 2208 $layout->set_markup ($par->[2]);
2252 2209
2253 glColor @{ $par->[1] };
2254 my ($W, $H) = $layout->size; 2210 my ($W, $H) = $layout->size;
2255 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2211 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0);
2256 } 2212 }
2257 2213
2258 $y += $h; 2214 $y += $h;
2259 } 2215 }
2260 2216
2261 glDisable GL_TEXTURE_2D; 2217 glDisable GL_TEXTURE_2D;
2262 glDisable GL_BLEND;
2263 }; 2218 };
2264 }); 2219 });
2265} 2220}
2266 2221
2267sub _draw { 2222sub _draw {
2268 my ($self) = @_; 2223 my ($self) = @_;
2269 2224
2270 glEnable GL_BLEND;
2271 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2272 glEnable GL_TEXTURE_2D; 2225 glEnable GL_TEXTURE_2D;
2273 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2226 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2274 glColor 1, 1, 1, 1; 2227 glColor 1, 1, 1, 1;
2275 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2228 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2276 glDisable GL_TEXTURE_2D; 2229 glDisable GL_TEXTURE_2D;
2277 glDisable GL_BLEND;
2278 2230
2279 $self->{children}[1]->draw; 2231 $self->{children}[1]->draw;
2280 2232
2281} 2233}
2282 2234
2376 @_, 2328 @_,
2377 can_events => 0, 2329 can_events => 0,
2378 ) 2330 )
2379} 2331}
2380 2332
2381sub set_markup { 2333sub set_tooltip_from {
2382 my ($self, $text) = @_; 2334 my ($self, $widget) = @_;
2383 2335
2384 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2336 $self->add (new CFClient::UI::Label
2385 $self->{label}->set_markup ($text); 2337 markup => $widget->{tooltip},
2386 $self->add ($self->{label}); 2338 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2339 fontsize => 0.8,
2340 fg => [0, 0, 0, 1],
2341 font => ($widget->{tooltip_font} || $::FONT_PROP),
2342 );
2387} 2343}
2388 2344
2389sub size_request { 2345sub size_request {
2390 my ($self) = @_; 2346 my ($self) = @_;
2391
2392 $self->child->set_max_size ($::WIDTH * 0.3);
2393 2347
2394 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2348 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2395 2349
2396 ($w + 4, $h + 4) 2350 ($w + 4, $h + 4)
2397} 2351}
2403} 2357}
2404 2358
2405sub _draw { 2359sub _draw {
2406 my ($self) = @_; 2360 my ($self) = @_;
2407 2361
2408 glPushMatrix;
2409 glTranslate 0.375, 0.375; 2362 glTranslate 0.375, 0.375;
2410 2363
2411 my ($w, $h) = @$self{qw(w h)}; 2364 my ($w, $h) = @$self{qw(w h)};
2412 2365
2413 glColor 1, 0.8, 0.4; 2366 glColor 1, 0.8, 0.4;
2424 glVertex 0 , $h; 2377 glVertex 0 , $h;
2425 glVertex $w, $h; 2378 glVertex $w, $h;
2426 glVertex $w, 0; 2379 glVertex $w, 0;
2427 glEnd; 2380 glEnd;
2428 2381
2429 glPopMatrix; 2382 glTranslate 2 - 0.375, 2 - 0.375;
2430
2431 glTranslate 2, 2;
2432 $self->SUPER::_draw; 2383 $self->SUPER::_draw;
2433} 2384}
2434 2385
2435############################################################################# 2386#############################################################################
2436 2387
2459 return unless $::CONN;#d# manage and cache textures differently 2410 return unless $::CONN;#d# manage and cache textures differently
2460 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2411 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2461 2412
2462 # TODO animation 2413 # TODO animation
2463 if ($tex) { 2414 if ($tex) {
2464 glEnable GL_BLEND;
2465 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2466 glEnable GL_TEXTURE_2D; 2415 glEnable GL_TEXTURE_2D;
2467 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2416 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2468 glColor 1, 1, 1, 1; 2417 glColor 1, 1, 1, 1;
2469 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2418 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2470 glDisable GL_TEXTURE_2D; 2419 glDisable GL_TEXTURE_2D;
2471 glDisable GL_BLEND;
2472 } 2420 }
2473} 2421}
2474 2422
2475############################################################################# 2423#############################################################################
2476 2424
2481sub new { 2429sub new {
2482 my $class = shift; 2430 my $class = shift;
2483 2431
2484 my %args = @_; 2432 my %args = @_;
2485 2433
2486 my $item = $args{item}; 2434 my $item = delete $args{item};
2487 2435
2488 my $desc = $item->{nrof} < 2 2436 my $desc = $item->{nrof} < 2
2489 ? $item->{name} 2437 ? $item->{name}
2490 : "$item->{nrof} $item->{name_pl}"; 2438 : "$item->{nrof} $item->{name_pl}";
2491 2439
2520 2468
2521 1 2469 1
2522 }, 2470 },
2523 %args 2471 %args
2524 ); 2472 );
2473
2525 $self->add(new CFClient::UI::Face 2474 $self->add (new CFClient::UI::Face
2526 can_events => 0, 2475 can_events => 0,
2527 face => $item->{face}, 2476 face => $item->{face},
2528 anim => $item->{anim}, 2477 anim => $item->{anim},
2529 animspeed => $item->{animspeed}); 2478 animspeed => $item->{animspeed},
2479 );
2480
2530 $self->add(new CFClient::UI::Label 2481 $self->add (new CFClient::UI::Label
2531 can_events => 0, 2482 can_events => 0,
2532 text => $desc); 2483 text => $desc,
2484 );
2533 2485
2534 $self 2486 $self
2535} 2487}
2536 2488
2537############################################################################# 2489#############################################################################
2564 for my $item (@items) { 2516 for my $item (@items) {
2565 my $desc = $item->{nrof} < 2 2517 my $desc = $item->{nrof} < 2
2566 ? $item->{name} 2518 ? $item->{name}
2567 : "$item->{nrof} $item->{name_pl}"; 2519 : "$item->{nrof} $item->{name_pl}";
2568 2520
2569 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); 2521 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item);
2570 } 2522 }
2571 2523
2572# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2524# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2573} 2525}
2574 2526
2645 $self->hide; 2597 $self->hide;
2646 2598
2647 $self->emit ("popdown"); 2599 $self->emit ("popdown");
2648 $self->{hover}[1]->() if $self->{hover}; 2600 $self->{hover}[1]->() if $self->{hover};
2649 } 2601 }
2602}
2603
2604#############################################################################
2605
2606package CFClient::UI::Statusbox;
2607
2608our @ISA = CFClient::UI::VBox::;
2609
2610sub reorder {
2611 my ($self) = @_;
2612 my $NOW = time;
2613
2614 while (my ($k, $v) = each %{ $self->{item} }) {
2615 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2616 }
2617
2618 my @widgets;
2619
2620 my @items = sort {
2621 $a->{pri} <=> $b->{pri}
2622 or $b->{id} <=> $a->{id}
2623 } values %{ $self->{item} };
2624
2625 my $count = 10 + 1;
2626 for my $item (@items) {
2627 last unless --$count;
2628
2629 push @widgets, $item->{label} ||= do {
2630 # TODO: doesn't handle markup well (read as: at all)
2631 my $short = $item->{count} > 1
2632 ? "<b>$item->{count} ×</b> $item->{text}"
2633 : $item->{text};
2634
2635 for ($short) {
2636 s/^\s+//;
2637 s/\012.*/…/s;
2638 my $len = int 40 / $item->{fontsize};
2639 substr $_, $len, length, "…" if $len < length;
2640 }
2641
2642 new CFClient::UI::Label
2643 markup => $short,
2644 tooltip => $item->{tooltip},
2645 tooltip_font => $::FONT_PROP,
2646 tooltip_width => 0.67,
2647 fontsize => $item->{fontsize},
2648 color => $item->{color},
2649 can_events => 1,
2650 can_hover => 1
2651 };
2652 }
2653
2654 $self->clear;
2655 $self->SUPER::add (reverse @widgets);
2656}
2657
2658sub add {
2659 my ($self, $text, %arg) = @_;
2660
2661 $text =~ s/^\s+//;
2662 $text =~ s/\s+$//;
2663
2664 my $timeout = time + ((delete $arg{timeout}) || 60);
2665
2666 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2667
2668 if (my $item = $self->{item}{$group}) {
2669 if ($item->{text} eq $text) {
2670 $item->{count}++;
2671 } else {
2672 $item->{count} = 1;
2673 $item->{text} = $item->{tooltip} = $text;
2674 }
2675 $item->{id} = ++$self->{id};
2676 $item->{timeout} = $timeout;
2677 delete $item->{label};
2678 } else {
2679 $self->{item}{$group} = {
2680 id => ++$self->{id},
2681 text => $text,
2682 timeout => $timeout,
2683 tooltip => $text,
2684 fontsize => 0.8,
2685 color => [0.8, 0.8, 0.8, 0.8],
2686 pri => 0,
2687 count => 1,
2688 %arg,
2689 };
2690 }
2691
2692 $self->reorder;
2650} 2693}
2651 2694
2652############################################################################# 2695#############################################################################
2653 2696
2654package CFClient::UI::Root; 2697package CFClient::UI::Root;
2749 my ($self, $id, $cb) = @_; 2792 my ($self, $id, $cb) = @_;
2750 2793
2751 $self->{refresh_hook}{$id} = $cb; 2794 $self->{refresh_hook}{$id} = $cb;
2752} 2795}
2753 2796
2797sub on_post_alloc {
2798 my ($self, $id, $cb) = @_;
2799
2800 $self->{post_alloc_hook}{$id} = $cb;
2801}
2802
2754sub draw { 2803sub draw {
2755 my ($self) = @_; 2804 my ($self) = @_;
2805
2806 while ($self->{refresh_hook}) {
2807 $_->()
2808 for values %{delete $self->{refresh_hook}};
2809 }
2756 2810
2757 if ($self->{check_size}) { 2811 if ($self->{check_size}) {
2758 my @queue = ([], []); 2812 my @queue = ([], []);
2759 2813
2760 for (;;) { 2814 for (;;) {
2768 2822
2769 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 2823 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2770 ? @$widget{qw(user_w user_h)} 2824 ? @$widget{qw(user_w user_h)}
2771 : $widget->size_request; 2825 : $widget->size_request;
2772 2826
2827 if (delete $widget->{force_alloc}
2773 if ($w != $widget->{req_w} || $h != $widget->{req_h}) { 2828 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2829 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2830
2774 $widget->{req_w} = $w; 2831 $widget->{req_w} = $w;
2775 $widget->{req_h} = $h; 2832 $widget->{req_h} = $h;
2776 2833
2777 $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}]; 2834 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2778 2835
2779 $widget->{parent}->check_size 2836 $widget->{parent}->check_size
2780 if $widget->{parent}; 2837 if $widget->{parent};
2781 } 2838 }
2782 } 2839 }
2783 } 2840 }
2784 2841
2785 while ($self->{size_alloc}) { 2842 while ($self->{size_alloc}) {
2786 for (values %{delete $self->{size_alloc}}) { 2843 for (values %{delete $self->{size_alloc}}) {
2787 my ($widget, $w, $h) = @$_; 2844 my ($widget, $w, $h) = @$_;
2845
2846 $w = 0 if $w < 0;
2847 $h = 0 if $h < 0;
2788 2848
2789 $widget->{w} = $w; 2849 $widget->{w} = $w;
2790 $widget->{h} = $h; 2850 $widget->{h} = $h;
2791 $widget->size_allocate ($w, $h); 2851 $widget->size_allocate ($w, $h);
2792 $widget->emit (size_allocate => $w, $h); 2852 $widget->emit (size_allocate => $w, $h);
2793 } 2853 }
2794 } 2854 }
2795 2855
2796 while ($self->{refresh_hook}) { 2856 while ($self->{post_alloc_hook}) {
2797 $_->() 2857 $_->()
2798 for values %{delete $self->{refresh_hook}}; 2858 for values %{delete $self->{post_alloc_hook}};
2799 } 2859 }
2800 2860
2801 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2861 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2802 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2862 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2803 glClear GL_COLOR_BUFFER_BIT; 2863 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines