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.199 by root, Fri May 12 15:45:42 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 # TODO: req_w _does_change after ->reconfigure
389 $self->check_size
390 unless exists $self->{req_w};
387} 391}
388 392
389sub check_size { 393sub check_size {
390 my ($self) = @_; 394 my ($self, $forced) = @_;
391 395
396 $self->{force_alloc} = 1 if $forced;
392 $CFClient::UI::ROOT->{check_size}{$self} = $self; 397 $CFClient::UI::ROOT->{check_size}{$self} = $self;
393} 398}
394 399
395sub update { 400sub update {
396 my ($self) = @_; 401 my ($self) = @_;
506 $self->{children} = [ 511 $self->{children} = [
507 sort { $a->{z} <=> $b->{z} } 512 sort { $a->{z} <=> $b->{z} }
508 @{$self->{children}}, @widgets 513 @{$self->{children}}, @widgets
509 ]; 514 ];
510 515
511 $_->check_size 516 $self->check_size (1);
512 for @widgets;
513
514 $self->update; 517 $self->update;
515} 518}
516 519
517sub children { 520sub children {
518 @{ $_[0]{children} } 521 @{ $_[0]{children} }
625} 628}
626 629
627sub update { 630sub update {
628 my ($self) = @_; 631 my ($self) = @_;
629 632
630 $ROOT->on_refresh ($self => sub { $self->render_child }); 633 $ROOT->on_post_alloc ($self => sub { $self->render_child });
631 $self->SUPER::update; 634 $self->SUPER::update;
632} 635}
633 636
634sub size_allocate { 637sub size_allocate {
635 my ($self, $w, $h) = @_; 638 my ($self, $w, $h) = @_;
666 my ($w, $h) = ($self->w, $self->h); 669 my ($w, $h) = ($self->w, $self->h);
667 670
668 my $tex = $self->{texture} 671 my $tex = $self->{texture}
669 or return; 672 or return;
670 673
671 glEnable GL_BLEND;
672 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
673 glEnable GL_TEXTURE_2D; 674 glEnable GL_TEXTURE_2D;
674 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 675 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
675 glColor 0, 0, 0, 1; 676 glColor 0, 0, 0, 1;
676 677
677 $tex->draw_quad (0, 0, $w, $h); 678 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
678 679
679 glDisable GL_BLEND;
680 glDisable GL_TEXTURE_2D; 680 glDisable GL_TEXTURE_2D;
681} 681}
682 682
683############################################################################# 683#############################################################################
684 684
776 $self->add ($self->{slider}); 776 $self->add ($self->{slider});
777 777
778 $self 778 $self
779} 779}
780 780
781#TODO# update range on size_allocate depeneing on child 781#TODO# update range on size_allocate depending on child
782# update viewport offset on scroll 782# update viewport offset on scroll
783 783
784############################################################################# 784#############################################################################
785 785
786package CFClient::UI::Frame; 786package CFClient::UI::Frame;
790use CFClient::OpenGL; 790use CFClient::OpenGL;
791 791
792sub new { 792sub new {
793 my $class = shift; 793 my $class = shift;
794 794
795 my $self = $class->SUPER::new ( 795 $class->SUPER::new (
796 bg => [1, 1, 1, 1], 796 bg => undef,
797 border_bg => [1, 1, 1, 1],
798 border => 0.8,
799 @_ 797 @_,
800 ); 798 )
801
802 $self
803} 799}
804 800
805sub _draw { 801sub _draw {
806 my ($self) = @_; 802 my ($self) = @_;
807 803
808 my ($w, $h) = ($self->{w}, $self->{h}); 804 if ($self->{bg}) {
805 my ($w, $h) = @$self{qw(w h)};
809 806
810 glEnable GL_BLEND; 807 glEnable GL_BLEND;
811 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 808 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
812 glEnable GL_TEXTURE_2D; 809 glColor @{ $self->{bg} };
813 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
814 810
815# glBegin GL_QUADS; 811 glBegin GL_QUADS;
816# glColor 0, 0, 0, 0;
817# glVertex 0 , 0; 812 glVertex 0 , 0;
818# glVertex 0 , $h; 813 glVertex 0 , $h;
819# glVertex $w, $h; 814 glVertex $w, $h;
820# glVertex $w, 0; 815 glVertex $w, 0;
821# glEnd; 816 glEnd;
822 817
823
824 $self->child->draw;
825 glDisable GL_BLEND; 818 glDisable GL_BLEND;
826 glDisable GL_TEXTURE_2D; 819 }
820
821 $self->SUPER::_draw;
827} 822}
828 823
829############################################################################# 824#############################################################################
830 825
831package CFClient::UI::FancyFrame; 826package CFClient::UI::FancyFrame;
947 my ($self) = @_; 942 my ($self) = @_;
948 943
949 my ($w, $h ) = ($self->{w}, $self->{h}); 944 my ($w, $h ) = ($self->{w}, $self->{h});
950 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 945 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
951 946
952 glEnable GL_BLEND;
953 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
954 glEnable GL_TEXTURE_2D; 947 glEnable GL_TEXTURE_2D;
955 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 948 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
956 949
957 my $border = $self->border; 950 my $border = $self->border;
958 951
959 glColor @{ $self->{border_bg} }; 952 glColor @{ $self->{border_bg} };
960 $tex[1]->draw_quad (0, 0, $w, $border); 953 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
961 $tex[3]->draw_quad (0, $border, $border, $ch); 954 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
962 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 955 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
963 $tex[4]->draw_quad (0, $h - $border, $w, $border); 956 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
964 957
965 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 958 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
966 my $bg = $tex[0]; 959 my $bg = $tex[0];
967 960
968 # TODO: repeat texture not scale 961 # TODO: repeat texture not scale
972 glColor @{ $self->{bg} }; 965 glColor @{ $self->{bg} };
973 966
974 $bg->{s} = $rep_x; 967 $bg->{s} = $rep_x;
975 $bg->{t} = $rep_y; 968 $bg->{t} = $rep_y;
976 $bg->{wrap_mode} = 1; 969 $bg->{wrap_mode} = 1;
977 $bg->draw_quad ($border, $border, $cw, $ch); 970 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
971 }
978 972
979 glDisable GL_TEXTURE_2D; 973 glDisable GL_TEXTURE_2D;
980 glDisable GL_BLEND;
981 }
982 974
983 $self->{title}->draw if $self->{title}; 975 $self->{title}->draw if $self->{title};
984 976
985 $self->child->draw; 977 $self->child->draw;
986} 978}
1213} 1205}
1214 1206
1215sub size_allocate { 1207sub size_allocate {
1216 my ($self, $w, $h) = @_; 1208 my ($self, $w, $h) = @_;
1217 1209
1210 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1211
1218 my $children = $self->{children}; 1212 my $children = $self->{children};
1219 1213
1220 my @h = map $_->{req_h}, @$children; 1214 my @h = map $_->{req_h}, @$children;
1221 1215
1222 my $req_h = List::Util::sum @h; 1216 my $req_h = List::Util::sum @h;
1261 my ($class, %arg) = @_; 1255 my ($class, %arg) = @_;
1262 1256
1263 my $self = $class->SUPER::new ( 1257 my $self = $class->SUPER::new (
1264 fg => [1, 1, 1], 1258 fg => [1, 1, 1],
1265 #font => default_font 1259 #font => default_font
1260 #text => initial text
1261 #markup => initial narkup
1262 layout => (new CFClient::Layout),
1266 fontsize => 1, 1263 fontsize => 1,
1267 text => "",
1268 align => -1, 1264 align => -1,
1269 valign => -1, 1265 valign => -1,
1270 padding => 2, 1266 padding => 2,
1271 layout => new CFClient::Layout,
1272 can_events => 0, 1267 can_events => 0,
1273 %arg 1268 %arg
1274 ); 1269 );
1275 1270
1276 if (exists $self->{template}) { 1271 if (exists $self->{template}) {
1277 my $layout = new CFClient::Layout; 1272 my $layout = new CFClient::Layout;
1278 $layout->set_text (delete $self->{template}); 1273 $layout->set_text (delete $self->{template});
1279 $self->{template} = $layout; 1274 $self->{template} = $layout;
1280 } 1275 }
1281 1276
1282 $self->set_text (delete $self->{text}) if exists $self->{text}; 1277 if (exists $self->{markup}) {
1283 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1278 $self->set_markup (delete $self->{markup});
1279 } else {
1280 $self->set_text (delete $self->{text});
1281 }
1284 1282
1285 $self 1283 $self
1286} 1284}
1287 1285
1288sub escape { 1286sub escape {
1306 my ($self, $text) = @_; 1304 my ($self, $text) = @_;
1307 1305
1308 return if $self->{text} eq "T$text"; 1306 return if $self->{text} eq "T$text";
1309 $self->{text} = "T$text"; 1307 $self->{text} = "T$text";
1310 1308
1309 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1311 $self->{layout}->set_text ($text); 1310 $self->{layout}->set_text ($text);
1312 1311
1313 $self->update; 1312 $self->update;
1314 $self->check_size; 1313 $self->check_size;
1315} 1314}
1318 my ($self, $markup) = @_; 1317 my ($self, $markup) = @_;
1319 1318
1320 return if $self->{text} eq "M$markup"; 1319 return if $self->{text} eq "M$markup";
1321 $self->{text} = "M$markup"; 1320 $self->{text} = "M$markup";
1322 1321
1322 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1323
1324 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1323 $self->{layout}->set_markup ($markup); 1325 $self->{layout}->set_markup ($markup);
1324 1326
1325 $self->update; 1327 $self->update;
1326 $self->check_size; 1328 $self->check_size;
1327} 1329}
1369 1371
1370sub _draw { 1372sub _draw {
1371 my ($self) = @_; 1373 my ($self) = @_;
1372 1374
1373 my $tex = $self->{texture} ||= do { 1375 my $tex = $self->{texture} ||= do {
1376 $self->{layout}->set_foreground (@{$self->{fg}});
1374 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1377 $self->{layout}->set_font ($self->{font}) if $self->{font};
1375 $self->{layout}->set_width ($self->{w}); 1378 $self->{layout}->set_width ($self->{w});
1376 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1379 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1380
1377 new_from_layout CFClient::Texture $self->{layout} 1381 my $tex = new_from_layout CFClient::Texture $self->{layout};
1382
1383 $self->{ox} = int $self->{align} < 0 ? $self->{padding}
1384 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1385 : ($self->{w} - $tex->{w}) * 0.5;
1386
1387 $self->{oy} = int $self->{valign} < 0 ? $self->{padding}
1388 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1389 : ($self->{h} - $tex->{h}) * 0.5;
1390
1391 $tex
1378 }; 1392 };
1379 1393
1380 glEnable GL_BLEND;
1381 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1382 glEnable GL_TEXTURE_2D; 1394 glEnable GL_TEXTURE_2D;
1383 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1395 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1384 1396
1397 if ($tex->{format} == GL_ALPHA) {
1385 glColor @{$self->{fg}}; 1398 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}); 1399 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1400 } else {
1401 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1402 }
1400 1403
1401 glDisable GL_TEXTURE_2D; 1404 glDisable GL_TEXTURE_2D;
1402 glDisable GL_BLEND;
1403} 1405}
1404 1406
1405############################################################################# 1407#############################################################################
1406 1408
1407package CFClient::UI::EntryBase; 1409package CFClient::UI::EntryBase;
1442 $self->{layout}->set_text ("$text "); 1444 $self->{layout}->set_text ("$text ");
1443 1445
1444 $self->emit (changed => $self->{text}); 1446 $self->emit (changed => $self->{text});
1445} 1447}
1446 1448
1449sub set_text {
1450 my ($self, $text) = @_;
1451
1452 $self->{cursor} = length $text;
1453 $self->_set_text ($text);
1454 $self->check_size;
1455 $self->update;
1456}
1457
1447sub get_text { 1458sub get_text {
1448 $_[0]{text} 1459 $_[0]{text}
1449} 1460}
1450 1461
1451sub size_request { 1462sub size_request {
1458 1469
1459sub size_allocate { 1470sub size_allocate {
1460 my ($self, $w, $h) = @_; 1471 my ($self, $w, $h) = @_;
1461 1472
1462 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text 1473 $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} 1474}
1472 1475
1473sub key_down { 1476sub key_down {
1474 my ($self, $ev) = @_; 1477 my ($self, $ev) = @_;
1475 1478
1663 1666
1664 if ($GRAB == $self) { 1667 if ($GRAB == $self) {
1665 $self->{fg} = $self->{active_fg}; 1668 $self->{fg} = $self->{active_fg};
1666 } 1669 }
1667 1670
1668 glEnable GL_BLEND;
1669 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1670 glEnable GL_TEXTURE_2D; 1671 glEnable GL_TEXTURE_2D;
1671 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1672 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1672 glColor 0, 0, 0, 1; 1673 glColor 0, 0, 0, 1;
1673 1674
1674 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1675 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1675 1676
1676 glDisable GL_TEXTURE_2D; 1677 glDisable GL_TEXTURE_2D;
1677 glDisable GL_BLEND;
1678 1678
1679 $self->SUPER::_draw; 1679 $self->SUPER::_draw;
1680} 1680}
1681 1681
1682############################################################################# 1682#############################################################################
1729 1729
1730 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1730 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1731 1731
1732 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1732 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1733 1733
1734 glEnable GL_BLEND; 1734 my $tex = $self->{state} ? $tex[1] : $tex[0];
1735
1735 glEnable GL_TEXTURE_2D; 1736 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); 1737 $tex->draw_quad_alpha (0, 0, $s, $s);
1741
1742 glDisable GL_TEXTURE_2D; 1738 glDisable GL_TEXTURE_2D;
1743 glDisable GL_BLEND;
1744} 1739}
1745 1740
1746############################################################################# 1741#############################################################################
1747 1742
1748package CFClient::UI::Image; 1743package CFClient::UI::Image;
1791 glTranslate 0, -$self->{w}, 0; 1786 glTranslate 0, -$self->{w}, 0;
1792 1787
1793 ($w, $h) = ($h, $w); 1788 ($w, $h) = ($h, $w);
1794 } 1789 }
1795 1790
1796 glEnable GL_BLEND;
1797 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1798 glEnable GL_TEXTURE_2D; 1791 glEnable GL_TEXTURE_2D;
1799 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1792 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1800 1793
1801 $tex->draw_quad (0, 0, $w, $h); 1794 $tex->draw_quad_alpha (0, 0, $w, $h);
1802 1795
1803 glDisable GL_BLEND;
1804 glDisable GL_TEXTURE_2D; 1796 glDisable GL_TEXTURE_2D;
1805} 1797}
1806 1798
1807############################################################################# 1799#############################################################################
1808 1800
2100 $page ||= 2; 2092 $page ||= 2;
2101 2093
2102 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2094 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2103 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2095 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2104 2096
2105 glEnable GL_BLEND;
2106 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2107 glEnable GL_TEXTURE_2D; 2097 glEnable GL_TEXTURE_2D;
2108 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2098 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2109 2099
2110 # draw background 2100 # draw background
2111 $tex[1]->draw_quad (0, 0, $w, $h); 2101 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2112 2102
2113 # draw handle 2103 # draw handle
2114 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2104 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h);
2115 2105
2116 glDisable GL_BLEND;
2117 glDisable GL_TEXTURE_2D; 2106 glDisable GL_TEXTURE_2D;
2118} 2107}
2119 2108
2120############################################################################# 2109#############################################################################
2121 2110
2132 fontsize => 1, 2121 fontsize => 1,
2133 can_events => 0, 2122 can_events => 0,
2134 #font => default_font 2123 #font => default_font
2135 @_, 2124 @_,
2136 2125
2137 layout => (new CFClient::Layout), 2126 layout => (new CFClient::Layout 1),
2138 par => [], 2127 par => [],
2139 height => 0, 2128 height => 0,
2140 children => [ 2129 children => [
2141 (new CFClient::UI::Empty expand => 1), 2130 (new CFClient::UI::Empty expand => 1),
2142 (new CFClient::UI::Slider vertical => 1), 2131 (new CFClient::UI::Slider vertical => 1),
2160 2149
2161 my $layout = $self->{layout}; 2150 my $layout = $self->{layout};
2162 2151
2163 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2152 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2164 $layout->set_width ($self->{children}[0]{w}); 2153 $layout->set_width ($self->{children}[0]{w});
2165 $layout->set_text ($text); 2154 $layout->set_markup ($text);
2166 2155
2167 ($layout->size)[1] 2156 ($layout->size)[1]
2168} 2157}
2169 2158
2170sub reflow { 2159sub reflow {
2208 2197
2209 return unless $self->{h} > 0; 2198 return unless $self->{h} > 0;
2210 2199
2211 delete $self->{texture}; 2200 delete $self->{texture};
2212 2201
2213 $ROOT->on_refresh ($self, sub { 2202 $ROOT->on_post_alloc ($self, sub {
2214 if (delete $self->{need_reflow}) { 2203 if (delete $self->{need_reflow}) {
2215 my $height = 0; 2204 my $height = 0;
2216 2205
2217 $height += $_->[0] = $self->text_height ($_->[2]) 2206 $height += $_->[0] = $self->text_height ($_->[2])
2218 for @{$self->{par}}; 2207 for @{$self->{par}};
2226 2215
2227 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2216 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2228 glClearColor 0, 0, 0, 0; 2217 glClearColor 0, 0, 0, 0;
2229 glClear GL_COLOR_BUFFER_BIT; 2218 glClear GL_COLOR_BUFFER_BIT;
2230 2219
2231 glEnable GL_BLEND;
2232 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2233 glEnable GL_TEXTURE_2D; 2220 glEnable GL_TEXTURE_2D;
2234 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2221 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2235 2222
2236 my $top = int $self->{children}[1]{range}[0]; 2223 my $top = int $self->{children}[1]{range}[0];
2237 2224
2246 2233
2247 for my $par (@{$self->{par}}) { 2234 for my $par (@{$self->{par}}) {
2248 my $h = $par->[0]; 2235 my $h = $par->[0];
2249 2236
2250 if ($y0 < $y + $h && $y < $y1) { 2237 if ($y0 < $y + $h && $y < $y1) {
2238 $layout->set_foreground (@{ $par->[1] });
2251 $layout->set_text ($par->[2]); 2239 $layout->set_markup ($par->[2]);
2252 2240
2253 glColor @{ $par->[1] };
2254 my ($W, $H) = $layout->size; 2241 my ($W, $H) = $layout->size;
2255 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2242 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0);
2256 } 2243 }
2257 2244
2258 $y += $h; 2245 $y += $h;
2259 } 2246 }
2260 2247
2261 glDisable GL_TEXTURE_2D; 2248 glDisable GL_TEXTURE_2D;
2262 glDisable GL_BLEND;
2263 }; 2249 };
2264 }); 2250 });
2265} 2251}
2266 2252
2267sub _draw { 2253sub _draw {
2268 my ($self) = @_; 2254 my ($self) = @_;
2269 2255
2270 glEnable GL_BLEND;
2271 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2272 glEnable GL_TEXTURE_2D; 2256 glEnable GL_TEXTURE_2D;
2273 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2257 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2274 glColor 1, 1, 1, 1; 2258 glColor 1, 1, 1, 1;
2275 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2259 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2276 glDisable GL_TEXTURE_2D; 2260 glDisable GL_TEXTURE_2D;
2277 glDisable GL_BLEND;
2278 2261
2279 $self->{children}[1]->draw; 2262 $self->{children}[1]->draw;
2280 2263
2281} 2264}
2282 2265
2376 @_, 2359 @_,
2377 can_events => 0, 2360 can_events => 0,
2378 ) 2361 )
2379} 2362}
2380 2363
2381sub set_markup { 2364sub set_tooltip_from {
2382 my ($self, $text) = @_; 2365 my ($self, $widget) = @_;
2383 2366
2384 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2367 $self->add (new CFClient::UI::Label
2385 $self->{label}->set_markup ($text); 2368 markup => $widget->{tooltip},
2386 $self->add ($self->{label}); 2369 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2370 fontsize => 0.8,
2371 fg => [0, 0, 0, 1],
2372 font => ($widget->{tooltip_font} || $::FONT_PROP),
2373 );
2387} 2374}
2388 2375
2389sub size_request { 2376sub size_request {
2390 my ($self) = @_; 2377 my ($self) = @_;
2391
2392 $self->child->set_max_size ($::WIDTH * 0.3);
2393 2378
2394 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2379 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2395 2380
2396 ($w + 4, $h + 4) 2381 ($w + 4, $h + 4)
2397} 2382}
2403} 2388}
2404 2389
2405sub _draw { 2390sub _draw {
2406 my ($self) = @_; 2391 my ($self) = @_;
2407 2392
2408 glPushMatrix;
2409 glTranslate 0.375, 0.375; 2393 glTranslate 0.375, 0.375;
2410 2394
2411 my ($w, $h) = @$self{qw(w h)}; 2395 my ($w, $h) = @$self{qw(w h)};
2412 2396
2413 glColor 1, 0.8, 0.4; 2397 glColor 1, 0.8, 0.4;
2424 glVertex 0 , $h; 2408 glVertex 0 , $h;
2425 glVertex $w, $h; 2409 glVertex $w, $h;
2426 glVertex $w, 0; 2410 glVertex $w, 0;
2427 glEnd; 2411 glEnd;
2428 2412
2429 glPopMatrix; 2413 glTranslate 2 - 0.375, 2 - 0.375;
2430
2431 glTranslate 2, 2;
2432 $self->SUPER::_draw; 2414 $self->SUPER::_draw;
2433} 2415}
2434 2416
2435############################################################################# 2417#############################################################################
2436 2418
2459 return unless $::CONN;#d# manage and cache textures differently 2441 return unless $::CONN;#d# manage and cache textures differently
2460 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2442 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2461 2443
2462 # TODO animation 2444 # TODO animation
2463 if ($tex) { 2445 if ($tex) {
2464 glEnable GL_BLEND;
2465 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2466 glEnable GL_TEXTURE_2D; 2446 glEnable GL_TEXTURE_2D;
2467 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2447 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2468 glColor 1, 1, 1, 1; 2448 glColor 1, 1, 1, 1;
2469 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2449 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2470 glDisable GL_TEXTURE_2D; 2450 glDisable GL_TEXTURE_2D;
2471 glDisable GL_BLEND;
2472 } 2451 }
2473} 2452}
2474 2453
2475############################################################################# 2454#############################################################################
2476 2455
2481sub new { 2460sub new {
2482 my $class = shift; 2461 my $class = shift;
2483 2462
2484 my %args = @_; 2463 my %args = @_;
2485 2464
2486 my $item = $args{item}; 2465 my $item = delete $args{item};
2487 2466
2488 my $desc = $item->{nrof} < 2 2467 my $desc = $item->{nrof} < 2
2489 ? $item->{name} 2468 ? $item->{name}
2490 : "$item->{nrof} $item->{name_pl}"; 2469 : "$item->{nrof} $item->{name_pl}";
2491 2470
2520 2499
2521 1 2500 1
2522 }, 2501 },
2523 %args 2502 %args
2524 ); 2503 );
2504
2525 $self->add(new CFClient::UI::Face 2505 $self->add (new CFClient::UI::Face
2526 can_events => 0, 2506 can_events => 0,
2527 face => $item->{face}, 2507 face => $item->{face},
2528 anim => $item->{anim}, 2508 anim => $item->{anim},
2529 animspeed => $item->{animspeed}); 2509 animspeed => $item->{animspeed},
2510 );
2511
2530 $self->add(new CFClient::UI::Label 2512 $self->add (new CFClient::UI::Label
2531 can_events => 0, 2513 can_events => 0,
2532 text => $desc); 2514 text => $desc,
2515 );
2533 2516
2534 $self 2517 $self
2535} 2518}
2536 2519
2537############################################################################# 2520#############################################################################
2564 for my $item (@items) { 2547 for my $item (@items) {
2565 my $desc = $item->{nrof} < 2 2548 my $desc = $item->{nrof} < 2
2566 ? $item->{name} 2549 ? $item->{name}
2567 : "$item->{nrof} $item->{name_pl}"; 2550 : "$item->{nrof} $item->{name_pl}";
2568 2551
2569 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); 2552 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item);
2570 } 2553 }
2571 2554
2572# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2555# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2573} 2556}
2574 2557
2645 $self->hide; 2628 $self->hide;
2646 2629
2647 $self->emit ("popdown"); 2630 $self->emit ("popdown");
2648 $self->{hover}[1]->() if $self->{hover}; 2631 $self->{hover}[1]->() if $self->{hover};
2649 } 2632 }
2633}
2634
2635#############################################################################
2636
2637package CFClient::UI::Statusbox;
2638
2639our @ISA = CFClient::UI::VBox::;
2640
2641sub reorder {
2642 my ($self) = @_;
2643 my $NOW = time;
2644
2645 while (my ($k, $v) = each %{ $self->{item} }) {
2646 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2647 }
2648
2649 my @widgets;
2650
2651 my @items = sort {
2652 $a->{pri} <=> $b->{pri}
2653 or $b->{id} <=> $a->{id}
2654 } values %{ $self->{item} };
2655
2656 my $count = 10 + 1;
2657 for my $item (@items) {
2658 last unless --$count;
2659
2660 push @widgets, $item->{label} ||= do {
2661 # TODO: doesn't handle markup well (read as: at all)
2662 my $short = $item->{count} > 1
2663 ? "<b>$item->{count} ×</b> $item->{text}"
2664 : $item->{text};
2665
2666 for ($short) {
2667 s/^\s+//;
2668 s/\012.*/…/s;
2669 my $len = int 40 / $item->{fontsize};
2670 substr $_, $len, length, "…" if $len < length;
2671 }
2672
2673 new CFClient::UI::Label
2674 markup => $short,
2675 tooltip => $item->{tooltip},
2676 tooltip_font => $::FONT_PROP,
2677 tooltip_width => 0.67,
2678 fontsize => $item->{fontsize},
2679 color => $item->{color},
2680 can_events => 1,
2681 can_hover => 1
2682 };
2683 }
2684
2685 $self->clear;
2686 $self->SUPER::add (reverse @widgets);
2687}
2688
2689sub add {
2690 my ($self, $text, %arg) = @_;
2691
2692 $text =~ s/^\s+//;
2693 $text =~ s/\s+$//;
2694
2695 my $timeout = time + ((delete $arg{timeout}) || 60);
2696
2697 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2698
2699 if (my $item = $self->{item}{$group}) {
2700 if ($item->{text} eq $text) {
2701 $item->{count}++;
2702 } else {
2703 $item->{count} = 1;
2704 $item->{text} = $item->{tooltip} = $text;
2705 }
2706 $item->{id} = ++$self->{id};
2707 $item->{timeout} = $timeout;
2708 delete $item->{label};
2709 } else {
2710 $self->{item}{$group} = {
2711 id => ++$self->{id},
2712 text => $text,
2713 timeout => $timeout,
2714 tooltip => $text,
2715 fontsize => 0.8,
2716 color => [0.8, 0.8, 0.8, 0.8],
2717 pri => 0,
2718 count => 1,
2719 %arg,
2720 };
2721 }
2722
2723 $self->reorder;
2650} 2724}
2651 2725
2652############################################################################# 2726#############################################################################
2653 2727
2654package CFClient::UI::Root; 2728package CFClient::UI::Root;
2749 my ($self, $id, $cb) = @_; 2823 my ($self, $id, $cb) = @_;
2750 2824
2751 $self->{refresh_hook}{$id} = $cb; 2825 $self->{refresh_hook}{$id} = $cb;
2752} 2826}
2753 2827
2828sub on_post_alloc {
2829 my ($self, $id, $cb) = @_;
2830
2831 $self->{post_alloc_hook}{$id} = $cb;
2832}
2833
2754sub draw { 2834sub draw {
2755 my ($self) = @_; 2835 my ($self) = @_;
2836
2837 while ($self->{refresh_hook}) {
2838 $_->()
2839 for values %{delete $self->{refresh_hook}};
2840 }
2756 2841
2757 if ($self->{check_size}) { 2842 if ($self->{check_size}) {
2758 my @queue = ([], []); 2843 my @queue = ([], []);
2759 2844
2760 for (;;) { 2845 for (;;) {
2768 2853
2769 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 2854 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2770 ? @$widget{qw(user_w user_h)} 2855 ? @$widget{qw(user_w user_h)}
2771 : $widget->size_request; 2856 : $widget->size_request;
2772 2857
2858 if (delete $widget->{force_alloc}
2773 if ($w != $widget->{req_w} || $h != $widget->{req_h}) { 2859 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2860 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2861
2774 $widget->{req_w} = $w; 2862 $widget->{req_w} = $w;
2775 $widget->{req_h} = $h; 2863 $widget->{req_h} = $h;
2776 2864
2777 $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}]; 2865 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2778 2866
2779 $widget->{parent}->check_size 2867 $widget->{parent}->check_size
2780 if $widget->{parent}; 2868 if $widget->{parent};
2781 } 2869 }
2782 } 2870 }
2783 } 2871 }
2784 2872
2785 while ($self->{size_alloc}) { 2873 while ($self->{size_alloc}) {
2786 for (values %{delete $self->{size_alloc}}) { 2874 for (values %{delete $self->{size_alloc}}) {
2787 my ($widget, $w, $h) = @$_; 2875 my ($widget, $w, $h) = @$_;
2876
2877 $w = 0 if $w < 0;
2878 $h = 0 if $h < 0;
2788 2879
2789 $widget->{w} = $w; 2880 $widget->{w} = $w;
2790 $widget->{h} = $h; 2881 $widget->{h} = $h;
2791 $widget->size_allocate ($w, $h); 2882 $widget->size_allocate ($w, $h);
2792 $widget->emit (size_allocate => $w, $h); 2883 $widget->emit (size_allocate => $w, $h);
2793 } 2884 }
2794 } 2885 }
2795 2886
2796 while ($self->{refresh_hook}) { 2887 while ($self->{post_alloc_hook}) {
2797 $_->() 2888 $_->()
2798 for values %{delete $self->{refresh_hook}}; 2889 for values %{delete $self->{post_alloc_hook}};
2799 } 2890 }
2800 2891
2801 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2892 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2802 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2893 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2803 glClear GL_COLOR_BUFFER_BIT; 2894 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines