ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.190 by root, Mon May 8 22:17:38 2006 UTC vs.
Revision 1.201 by root, Sat May 13 23:17:49 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 }
232 $self->{y} = $y; 234 $self->{y} = $y;
233 $self->update; 235 $self->update;
234 } 236 }
235 237
236 if ($self->{w} != $w || $self->{h} != $h) { 238 if ($self->{w} != $w || $self->{h} != $h) {
237 $self->{w} = $w; 239 $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 } 240 }
244} 241}
245 242
246sub size_allocate { 243sub size_allocate {
247 # nothing to be done 244 # nothing to be done
255 my ($self) = @_; 252 my ($self) = @_;
256 253
257 $_->reconfigure 254 $_->reconfigure
258 for $self->children; 255 for $self->children;
259 256
260 $self->check_size; 257 $self->check_size (1);
261 $self->size_allocate ($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 # TODO: req_w _does_change after ->reconfigure
389 $self->check_size
390 unless exists $self->{req_w};
403} 391}
404 392
405sub check_size { 393sub check_size {
406 my ($self) = @_; 394 my ($self, $forced) = @_;
407 395
408 $self->_check_size; 396 $self->{force_alloc} = 1 if $forced;
409 return;
410 #TODO: but needs a vastly more difficult algorithm
411 my $check_size = $CFClient::UI::ROOT->{_check_size} ||= {};
412
413 $check_size->{$self} = $self;
414
415 $CFClient::UI::ROOT->on_refresh (_check_size => sub {
416 while (%$check_size) {
417 my @widgets = values %$check_size;
418 $_->_check_size
419 for @widgets;
420 }
421 });
422}
423
424sub _check_size {
425 my ($self) = @_;
426
427 delete $CFClient::UI::ROOT->{_check_size}{$self}; 397 $CFClient::UI::ROOT->{check_size}{$self} = $self;
428
429 $self->{parent}
430 or return 1;
431
432 my ($w, $h) = $self->{user_w} && $self->{user_h}
433 ? @$self{qw(user_w user_h)}
434 : $self->size_request;
435
436 if ($w != $self->{req_w} || $h != $self->{req_h}) {
437 $self->{req_w} = $w;
438 $self->{req_h} = $h;
439
440 $self->{parent}->_check_size
441 or $self->size_allocate (
442 (List::Util::max $self->{w}, $w),
443 (List::Util::max $self->{h}, $h),
444 );
445
446 1
447 } else {
448 0
449 }
450} 398}
451 399
452sub update { 400sub update {
453 my ($self) = @_; 401 my ($self) = @_;
454 402
455 my $update = $CFClient::UI::ROOT->{_update} ||= {}; 403 $self->{parent}->update
456 404 if $self->{parent};
457 $update->{$self} = $self;
458
459 $CFClient::UI::ROOT->on_refresh (_update => sub {
460 while (%$update) {
461 my @widgets = values %$update;
462 %$update = ();
463
464 $_->{parent} && $_->{parent}->update
465 for @widgets;
466 }
467 });
468} 405}
469 406
470sub connect { 407sub connect {
471 my ($self, $signal, $cb) = @_; 408 my ($self, $signal, $cb) = @_;
472 409
574 $self->{children} = [ 511 $self->{children} = [
575 sort { $a->{z} <=> $b->{z} } 512 sort { $a->{z} <=> $b->{z} }
576 @{$self->{children}}, @widgets 513 @{$self->{children}}, @widgets
577 ]; 514 ];
578 515
579 $_->check_size 516 $self->check_size (1);
580 for @widgets;
581
582 $self->update; 517 $self->update;
583} 518}
584 519
585sub children { 520sub children {
586 @{ $_[0]{children} } 521 @{ $_[0]{children} }
693} 628}
694 629
695sub update { 630sub update {
696 my ($self) = @_; 631 my ($self) = @_;
697 632
698 $ROOT->on_refresh ($self => sub { $self->render_child }); 633 $ROOT->on_post_alloc ($self => sub { $self->render_child });
699 $self->SUPER::update; 634 $self->SUPER::update;
700} 635}
701 636
702sub size_allocate { 637sub size_allocate {
703 my ($self, $w, $h) = @_; 638 my ($self, $w, $h) = @_;
734 my ($w, $h) = ($self->w, $self->h); 669 my ($w, $h) = ($self->w, $self->h);
735 670
736 my $tex = $self->{texture} 671 my $tex = $self->{texture}
737 or return; 672 or return;
738 673
739 glEnable GL_BLEND;
740 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
741 glEnable GL_TEXTURE_2D; 674 glEnable GL_TEXTURE_2D;
742 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 675 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
743 glColor 0, 0, 0, 1; 676 glColor 0, 0, 0, 1;
744 677
745 $tex->draw_quad (0, 0, $w, $h); 678 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
746 679
747 glDisable GL_BLEND;
748 glDisable GL_TEXTURE_2D; 680 glDisable GL_TEXTURE_2D;
749} 681}
750 682
751############################################################################# 683#############################################################################
752 684
776 $self->{view_y} = int $y; 708 $self->{view_y} = int $y;
777 709
778 $self->update; 710 $self->update;
779} 711}
780 712
781# hmm, this does not work for topleft of $self... but we should not aks for that 713# hmm, this does not work for topleft of $self... but we should not ask for that
782sub _topleft { 714sub coord2local {
783 my ($self, $x, $y) = @_; 715 my ($self, $x, $y) = @_;
784 716
785 $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y}) 717 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
718}
719
720sub coord2global {
721 my ($self, $x, $y) = @_;
722
723 $x = List::Util::min $self->{w}, $x - $self->{view_x};
724 $y = List::Util::min $self->{h}, $y - $self->{view_y};
725
726 $self->SUPER::coord2global ($x, $y)
786} 727}
787 728
788sub find_widget { 729sub find_widget {
789 my ($self, $x, $y) = @_; 730 my ($self, $x, $y) = @_;
790 731
835 $self->add ($self->{slider}); 776 $self->add ($self->{slider});
836 777
837 $self 778 $self
838} 779}
839 780
840#TODO# update range on size_allocate depeneing on child 781#TODO# update range on size_allocate depending on child
841# update viewport offset on scroll 782# update viewport offset on scroll
842 783
843############################################################################# 784#############################################################################
844 785
845package CFClient::UI::Frame; 786package CFClient::UI::Frame;
849use CFClient::OpenGL; 790use CFClient::OpenGL;
850 791
851sub new { 792sub new {
852 my $class = shift; 793 my $class = shift;
853 794
854 my $self = $class->SUPER::new ( 795 $class->SUPER::new (
855 bg => [1, 1, 1, 1], 796 bg => undef,
856 border_bg => [1, 1, 1, 1],
857 border => 0.8,
858 @_ 797 @_,
859 ); 798 )
860
861 $self
862} 799}
863 800
864sub _draw { 801sub _draw {
865 my ($self) = @_; 802 my ($self) = @_;
866 803
867 my ($w, $h) = ($self->{w}, $self->{h}); 804 if ($self->{bg}) {
805 my ($w, $h) = @$self{qw(w h)};
868 806
869 glEnable GL_BLEND; 807 glEnable GL_BLEND;
870 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 808 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
871 glEnable GL_TEXTURE_2D; 809 glColor @{ $self->{bg} };
872 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
873 810
874# glBegin GL_QUADS; 811 glBegin GL_QUADS;
875# glColor 0, 0, 0, 0;
876# glVertex 0 , 0; 812 glVertex 0 , 0;
877# glVertex 0 , $h; 813 glVertex 0 , $h;
878# glVertex $w, $h; 814 glVertex $w, $h;
879# glVertex $w, 0; 815 glVertex $w, 0;
880# glEnd; 816 glEnd;
881 817
882
883 $self->child->draw;
884 glDisable GL_BLEND; 818 glDisable GL_BLEND;
885 glDisable GL_TEXTURE_2D; 819 }
820
821 $self->SUPER::_draw;
886} 822}
887 823
888############################################################################# 824#############################################################################
889 825
890package CFClient::UI::FancyFrame; 826package CFClient::UI::FancyFrame;
1006 my ($self) = @_; 942 my ($self) = @_;
1007 943
1008 my ($w, $h ) = ($self->{w}, $self->{h}); 944 my ($w, $h ) = ($self->{w}, $self->{h});
1009 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 945 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
1010 946
1011 glEnable GL_BLEND;
1012 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1013 glEnable GL_TEXTURE_2D; 947 glEnable GL_TEXTURE_2D;
1014 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 948 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1015 949
1016 my $border = $self->border; 950 my $border = $self->border;
1017 951
1018 glColor @{ $self->{border_bg} }; 952 glColor @{ $self->{border_bg} };
1019 $tex[1]->draw_quad (0, 0, $w, $border); 953 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
1020 $tex[3]->draw_quad (0, $border, $border, $ch); 954 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
1021 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 955 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1022 $tex[4]->draw_quad (0, $h - $border, $w, $border); 956 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
1023 957
1024 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 958 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1025 my $bg = $tex[0]; 959 my $bg = $tex[0];
1026 960
1027 # TODO: repeat texture not scale 961 # TODO: repeat texture not scale
1031 glColor @{ $self->{bg} }; 965 glColor @{ $self->{bg} };
1032 966
1033 $bg->{s} = $rep_x; 967 $bg->{s} = $rep_x;
1034 $bg->{t} = $rep_y; 968 $bg->{t} = $rep_y;
1035 $bg->{wrap_mode} = 1; 969 $bg->{wrap_mode} = 1;
1036 $bg->draw_quad ($border, $border, $cw, $ch); 970 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
971 }
1037 972
1038 glDisable GL_TEXTURE_2D; 973 glDisable GL_TEXTURE_2D;
1039 glDisable GL_BLEND;
1040 }
1041 974
1042 $self->{title}->draw if $self->{title}; 975 $self->{title}->draw if $self->{title};
1043 976
1044 $self->child->draw; 977 $self->child->draw;
1045} 978}
1272} 1205}
1273 1206
1274sub size_allocate { 1207sub size_allocate {
1275 my ($self, $w, $h) = @_; 1208 my ($self, $w, $h) = @_;
1276 1209
1210 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1211
1277 my $children = $self->{children}; 1212 my $children = $self->{children};
1278 1213
1279 my @h = map $_->{req_h}, @$children; 1214 my @h = map $_->{req_h}, @$children;
1280 1215
1281 my $req_h = List::Util::sum @h; 1216 my $req_h = List::Util::sum @h;
1320 my ($class, %arg) = @_; 1255 my ($class, %arg) = @_;
1321 1256
1322 my $self = $class->SUPER::new ( 1257 my $self = $class->SUPER::new (
1323 fg => [1, 1, 1], 1258 fg => [1, 1, 1],
1324 #font => default_font 1259 #font => default_font
1260 #text => initial text
1261 #markup => initial narkup
1262 layout => (new CFClient::Layout),
1325 fontsize => 1, 1263 fontsize => 1,
1326 text => "",
1327 align => -1, 1264 align => -1,
1328 valign => -1, 1265 valign => -1,
1329 padding => 2, 1266 padding => 2,
1330 layout => new CFClient::Layout,
1331 can_events => 0, 1267 can_events => 0,
1332 %arg 1268 %arg
1333 ); 1269 );
1334 1270
1335 if (exists $self->{template}) { 1271 if (exists $self->{template}) {
1336 my $layout = new CFClient::Layout; 1272 my $layout = new CFClient::Layout;
1337 $layout->set_text (delete $self->{template}); 1273 $layout->set_text (delete $self->{template});
1338 $self->{template} = $layout; 1274 $self->{template} = $layout;
1339 } 1275 }
1340 1276
1341 $self->set_text (delete $self->{text}) if exists $self->{text}; 1277 if (exists $self->{markup}) {
1342 $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 }
1343 1282
1344 $self 1283 $self
1345} 1284}
1346 1285
1347sub escape { 1286sub escape {
1365 my ($self, $text) = @_; 1304 my ($self, $text) = @_;
1366 1305
1367 return if $self->{text} eq "T$text"; 1306 return if $self->{text} eq "T$text";
1368 $self->{text} = "T$text"; 1307 $self->{text} = "T$text";
1369 1308
1309 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1370 $self->{layout}->set_text ($text); 1310 $self->{layout}->set_text ($text);
1371 1311
1372 delete $self->{texture};
1373 $self->update; 1312 $self->update;
1374 $self->check_size; 1313 $self->check_size;
1375} 1314}
1376 1315
1377sub set_markup { 1316sub set_markup {
1378 my ($self, $markup) = @_; 1317 my ($self, $markup) = @_;
1379 1318
1380 return if $self->{text} eq "M$markup"; 1319 return if $self->{text} eq "M$markup";
1381 $self->{text} = "M$markup"; 1320 $self->{text} = "M$markup";
1382 1321
1322 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1323
1324 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1383 $self->{layout}->set_markup ($markup); 1325 $self->{layout}->set_markup ($markup);
1384 1326
1385 delete $self->{texture};
1386 $self->update; 1327 $self->update;
1387 $self->check_size; 1328 $self->check_size;
1388} 1329}
1389 1330
1390sub size_request { 1331sub size_request {
1430 1371
1431sub _draw { 1372sub _draw {
1432 my ($self) = @_; 1373 my ($self) = @_;
1433 1374
1434 my $tex = $self->{texture} ||= do { 1375 my $tex = $self->{texture} ||= do {
1376 $self->{layout}->set_foreground (@{$self->{fg}});
1435 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1377 $self->{layout}->set_font ($self->{font}) if $self->{font};
1436 $self->{layout}->set_width ($self->{w}); 1378 $self->{layout}->set_width ($self->{w});
1437 $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
1438 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
1439 }; 1392 };
1440 1393
1441 glEnable GL_BLEND;
1442 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1443 glEnable GL_TEXTURE_2D; 1394 glEnable GL_TEXTURE_2D;
1444 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1395 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1445 1396
1397 if ($tex->{format} == GL_ALPHA) {
1446 glColor @{$self->{fg}}; 1398 glColor @{$self->{fg}};
1447
1448 $self->{ox} = int (
1449 $self->{align} < 0 ? $self->{padding}
1450 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1451 : ($self->{w} - $tex->{w}) * 0.5
1452 );
1453
1454 $self->{oy} = int (
1455 $self->{valign} < 0 ? $self->{padding}
1456 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1457 : ($self->{h} - $tex->{h}) * 0.5
1458 );
1459
1460 $tex->draw_quad ($self->{ox}, $self->{oy}); 1399 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1400 } else {
1401 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1402 }
1461 1403
1462 glDisable GL_TEXTURE_2D; 1404 glDisable GL_TEXTURE_2D;
1463 glDisable GL_BLEND;
1464} 1405}
1465 1406
1466############################################################################# 1407#############################################################################
1467 1408
1468package CFClient::UI::EntryBase; 1409package CFClient::UI::EntryBase;
1503 $self->{layout}->set_text ("$text "); 1444 $self->{layout}->set_text ("$text ");
1504 1445
1505 $self->emit (changed => $self->{text}); 1446 $self->emit (changed => $self->{text});
1506} 1447}
1507 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
1508sub get_text { 1458sub get_text {
1509 $_[0]{text} 1459 $_[0]{text}
1510} 1460}
1511 1461
1512sub size_request { 1462sub size_request {
1519 1469
1520sub size_allocate { 1470sub size_allocate {
1521 my ($self, $w, $h) = @_; 1471 my ($self, $w, $h) = @_;
1522 1472
1523 $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
1524}
1525
1526sub set_text {
1527 my ($self, $text) = @_;
1528
1529 $self->{cursor} = length $text;
1530 $self->_set_text ($text);
1531 $self->update;
1532} 1474}
1533 1475
1534sub key_down { 1476sub key_down {
1535 my ($self, $ev) = @_; 1477 my ($self, $ev) = @_;
1536 1478
1538 my $sym = $ev->{sym}; 1480 my $sym = $ev->{sym};
1539 my $uni = $ev->{unicode}; 1481 my $uni = $ev->{unicode};
1540 1482
1541 my $text = $self->get_text; 1483 my $text = $self->get_text;
1542 1484
1543 if ($sym == 8) { 1485 if ($uni == 8) {
1544 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1486 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1545 } elsif ($sym == 127) { 1487 } elsif ($uni == 127) {
1546 substr $text, $self->{cursor}, 1, ""; 1488 substr $text, $self->{cursor}, 1, "";
1547 } elsif ($sym == CFClient::SDLK_LEFT) { 1489 } elsif ($sym == CFClient::SDLK_LEFT) {
1548 --$self->{cursor} if $self->{cursor}; 1490 --$self->{cursor} if $self->{cursor};
1549 } elsif ($sym == CFClient::SDLK_RIGHT) { 1491 } elsif ($sym == CFClient::SDLK_RIGHT) {
1550 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1492 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1551 } elsif ($sym == CFClient::SDLK_HOME) { 1493 } elsif ($sym == CFClient::SDLK_HOME) {
1552 $self->{cursor} = 0; 1494 $self->{cursor} = 0;
1553 } elsif ($sym == CFClient::SDLK_END) { 1495 } elsif ($sym == CFClient::SDLK_END) {
1554 $self->{cursor} = length $text; 1496 $self->{cursor} = length $text;
1555 } elsif ($sym == 27) { 1497 } elsif ($uni == 27) {
1556 $self->emit ('escape'); 1498 $self->emit ('escape');
1557 } elsif ($uni) { 1499 } elsif ($uni) {
1558 substr $text, $self->{cursor}++, 0, chr $uni; 1500 substr $text, $self->{cursor}++, 0, chr $uni;
1559 } 1501 }
1560 1502
1724 1666
1725 if ($GRAB == $self) { 1667 if ($GRAB == $self) {
1726 $self->{fg} = $self->{active_fg}; 1668 $self->{fg} = $self->{active_fg};
1727 } 1669 }
1728 1670
1729 glEnable GL_BLEND;
1730 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1731 glEnable GL_TEXTURE_2D; 1671 glEnable GL_TEXTURE_2D;
1732 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1672 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1733 glColor 0, 0, 0, 1; 1673 glColor 0, 0, 0, 1;
1734 1674
1735 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1675 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1736 1676
1737 glDisable GL_TEXTURE_2D; 1677 glDisable GL_TEXTURE_2D;
1738 glDisable GL_BLEND;
1739 1678
1740 $self->SUPER::_draw; 1679 $self->SUPER::_draw;
1741} 1680}
1742 1681
1743############################################################################# 1682#############################################################################
1790 1729
1791 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;
1792 1731
1793 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1732 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1794 1733
1795 glEnable GL_BLEND; 1734 my $tex = $self->{state} ? $tex[1] : $tex[0];
1735
1796 glEnable GL_TEXTURE_2D; 1736 glEnable GL_TEXTURE_2D;
1797 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1798
1799 my $tex = $self->{state} ? $tex[1] : $tex[0];
1800
1801 $tex->draw_quad (0, 0, $s, $s); 1737 $tex->draw_quad_alpha (0, 0, $s, $s);
1802
1803 glDisable GL_TEXTURE_2D; 1738 glDisable GL_TEXTURE_2D;
1804 glDisable GL_BLEND;
1805} 1739}
1806 1740
1807############################################################################# 1741#############################################################################
1808 1742
1809package CFClient::UI::Image; 1743package CFClient::UI::Image;
1852 glTranslate 0, -$self->{w}, 0; 1786 glTranslate 0, -$self->{w}, 0;
1853 1787
1854 ($w, $h) = ($h, $w); 1788 ($w, $h) = ($h, $w);
1855 } 1789 }
1856 1790
1857 glEnable GL_BLEND;
1858 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1859 glEnable GL_TEXTURE_2D; 1791 glEnable GL_TEXTURE_2D;
1860 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1792 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1861 1793
1862 $tex->draw_quad (0, 0, $w, $h); 1794 $tex->draw_quad_alpha (0, 0, $w, $h);
1863 1795
1864 glDisable GL_BLEND;
1865 glDisable GL_TEXTURE_2D; 1796 glDisable GL_TEXTURE_2D;
1866} 1797}
1867 1798
1868############################################################################# 1799#############################################################################
1869 1800
2161 $page ||= 2; 2092 $page ||= 2;
2162 2093
2163 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2094 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2164 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2095 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2165 2096
2166 glEnable GL_BLEND;
2167 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2168 glEnable GL_TEXTURE_2D; 2097 glEnable GL_TEXTURE_2D;
2169 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2098 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2170 2099
2171 # draw background 2100 # draw background
2172 $tex[1]->draw_quad (0, 0, $w, $h); 2101 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2173 2102
2174 # draw handle 2103 # draw handle
2175 $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);
2176 2105
2177 glDisable GL_BLEND;
2178 glDisable GL_TEXTURE_2D; 2106 glDisable GL_TEXTURE_2D;
2179} 2107}
2180 2108
2181############################################################################# 2109#############################################################################
2182 2110
2193 fontsize => 1, 2121 fontsize => 1,
2194 can_events => 0, 2122 can_events => 0,
2195 #font => default_font 2123 #font => default_font
2196 @_, 2124 @_,
2197 2125
2198 layout => (new CFClient::Layout), 2126 layout => (new CFClient::Layout 1),
2199 par => [], 2127 par => [],
2200 height => 0, 2128 height => 0,
2201 children => [ 2129 children => [
2202 (new CFClient::UI::Empty expand => 1), 2130 (new CFClient::UI::Empty expand => 1),
2203 (new CFClient::UI::Slider vertical => 1), 2131 (new CFClient::UI::Slider vertical => 1),
2221 2149
2222 my $layout = $self->{layout}; 2150 my $layout = $self->{layout};
2223 2151
2224 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2152 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2225 $layout->set_width ($self->{children}[0]{w}); 2153 $layout->set_width ($self->{children}[0]{w});
2226 $layout->set_text ($text); 2154 $layout->set_markup ($text);
2227 2155
2228 ($layout->size)[1] 2156 ($layout->size)[1]
2229} 2157}
2230 2158
2231sub reflow { 2159sub reflow {
2269 2197
2270 return unless $self->{h} > 0; 2198 return unless $self->{h} > 0;
2271 2199
2272 delete $self->{texture}; 2200 delete $self->{texture};
2273 2201
2274 $ROOT->on_refresh ($self, sub { 2202 $ROOT->on_post_alloc ($self, sub {
2275 if (delete $self->{need_reflow}) { 2203 if (delete $self->{need_reflow}) {
2276 my $height = 0; 2204 my $height = 0;
2277 2205
2278 $height += $_->[0] = $self->text_height ($_->[2]) 2206 $height += $_->[0] = $self->text_height ($_->[2])
2279 for @{$self->{par}}; 2207 for @{$self->{par}};
2287 2215
2288 $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 {
2289 glClearColor 0, 0, 0, 0; 2217 glClearColor 0, 0, 0, 0;
2290 glClear GL_COLOR_BUFFER_BIT; 2218 glClear GL_COLOR_BUFFER_BIT;
2291 2219
2292 glEnable GL_BLEND;
2293 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2294 glEnable GL_TEXTURE_2D; 2220 glEnable GL_TEXTURE_2D;
2295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2221 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2296 2222
2297 my $top = int $self->{children}[1]{range}[0]; 2223 my $top = int $self->{children}[1]{range}[0];
2298 2224
2307 2233
2308 for my $par (@{$self->{par}}) { 2234 for my $par (@{$self->{par}}) {
2309 my $h = $par->[0]; 2235 my $h = $par->[0];
2310 2236
2311 if ($y0 < $y + $h && $y < $y1) { 2237 if ($y0 < $y + $h && $y < $y1) {
2238 $layout->set_foreground (@{ $par->[1] });
2312 $layout->set_text ($par->[2]); 2239 $layout->set_markup ($par->[2]);
2313 2240
2314 glColor @{ $par->[1] };
2315 my ($W, $H) = $layout->size; 2241 my ($W, $H) = $layout->size;
2316 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);
2317 } 2243 }
2318 2244
2319 $y += $h; 2245 $y += $h;
2320 } 2246 }
2321 2247
2322 glDisable GL_TEXTURE_2D; 2248 glDisable GL_TEXTURE_2D;
2323 glDisable GL_BLEND;
2324 }; 2249 };
2325 }); 2250 });
2326} 2251}
2327 2252
2328sub _draw { 2253sub _draw {
2329 my ($self) = @_; 2254 my ($self) = @_;
2330 2255
2331 glEnable GL_BLEND;
2332 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2333 glEnable GL_TEXTURE_2D; 2256 glEnable GL_TEXTURE_2D;
2334 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2257 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2335 glColor 1, 1, 1, 1; 2258 glColor 1, 1, 1, 1;
2336 $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});
2337 glDisable GL_TEXTURE_2D; 2260 glDisable GL_TEXTURE_2D;
2338 glDisable GL_BLEND;
2339 2261
2340 $self->{children}[1]->draw; 2262 $self->{children}[1]->draw;
2341 2263
2342} 2264}
2343 2265
2437 @_, 2359 @_,
2438 can_events => 0, 2360 can_events => 0,
2439 ) 2361 )
2440} 2362}
2441 2363
2442sub set_markup { 2364sub set_tooltip_from {
2443 my ($self, $text) = @_; 2365 my ($self, $widget) = @_;
2444 2366
2445 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2367 $self->add (new CFClient::UI::Label
2446 $self->{label}->set_markup ($text); 2368 markup => $widget->{tooltip},
2447 $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 );
2448} 2374}
2449 2375
2450sub size_request { 2376sub size_request {
2451 my ($self) = @_; 2377 my ($self) = @_;
2452
2453 $self->child->set_max_size ($::WIDTH * 0.3);
2454 2378
2455 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2379 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2456 2380
2457 ($w + 4, $h + 4) 2381 ($w + 4, $h + 4)
2458} 2382}
2464} 2388}
2465 2389
2466sub _draw { 2390sub _draw {
2467 my ($self) = @_; 2391 my ($self) = @_;
2468 2392
2469 glPushMatrix;
2470 glTranslate 0.375, 0.375; 2393 glTranslate 0.375, 0.375;
2471 2394
2472 my ($w, $h) = @$self{qw(w h)}; 2395 my ($w, $h) = @$self{qw(w h)};
2473 2396
2474 glColor 1, 0.8, 0.4; 2397 glColor 1, 0.8, 0.4;
2485 glVertex 0 , $h; 2408 glVertex 0 , $h;
2486 glVertex $w, $h; 2409 glVertex $w, $h;
2487 glVertex $w, 0; 2410 glVertex $w, 0;
2488 glEnd; 2411 glEnd;
2489 2412
2490 glPopMatrix; 2413 glTranslate 2 - 0.375, 2 - 0.375;
2491
2492 glTranslate 2, 2;
2493 $self->SUPER::_draw; 2414 $self->SUPER::_draw;
2494} 2415}
2495 2416
2496############################################################################# 2417#############################################################################
2497 2418
2520 return unless $::CONN;#d# manage and cache textures differently 2441 return unless $::CONN;#d# manage and cache textures differently
2521 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2442 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2522 2443
2523 # TODO animation 2444 # TODO animation
2524 if ($tex) { 2445 if ($tex) {
2525 glEnable GL_BLEND;
2526 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2527 glEnable GL_TEXTURE_2D; 2446 glEnable GL_TEXTURE_2D;
2528 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2447 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2529 glColor 1, 1, 1, 1; 2448 glColor 1, 1, 1, 1;
2530 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2449 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2531 glDisable GL_TEXTURE_2D; 2450 glDisable GL_TEXTURE_2D;
2532 glDisable GL_BLEND;
2533 } 2451 }
2534} 2452}
2535 2453
2536############################################################################# 2454#############################################################################
2537 2455
2542sub new { 2460sub new {
2543 my $class = shift; 2461 my $class = shift;
2544 2462
2545 my %args = @_; 2463 my %args = @_;
2546 2464
2547 my $item = $args{item}; 2465 my $item = delete $args{item};
2548 2466
2549 my $desc = $item->{nrof} < 2 2467 my $desc = $item->{nrof} < 2
2550 ? $item->{name} 2468 ? $item->{name}
2551 : "$item->{nrof} $item->{name_pl}"; 2469 : "$item->{nrof} $item->{name_pl}";
2552 2470
2581 2499
2582 1 2500 1
2583 }, 2501 },
2584 %args 2502 %args
2585 ); 2503 );
2504
2586 $self->add(new CFClient::UI::Face 2505 $self->add (new CFClient::UI::Face
2587 can_events => 0, 2506 can_events => 0,
2588 face => $item->{face}, 2507 face => $item->{face},
2589 anim => $item->{anim}, 2508 anim => $item->{anim},
2590 animspeed => $item->{animspeed}); 2509 animspeed => $item->{animspeed},
2510 );
2511
2591 $self->add(new CFClient::UI::Label 2512 $self->add (new CFClient::UI::Label
2592 can_events => 0, 2513 can_events => 0,
2593 text => $desc); 2514 text => $desc,
2515 );
2594 2516
2595 $self 2517 $self
2596} 2518}
2597 2519
2598############################################################################# 2520#############################################################################
2616 my ($self, $items) = @_; 2538 my ($self, $items) = @_;
2617 2539
2618 $self->{scrolled}->clear; 2540 $self->{scrolled}->clear;
2619 return unless $items; 2541 return unless $items;
2620 2542
2621 my @items = sort { $a->{type} <=> $b->{type} } @$items; 2543 my @items = sort {
2544 ($a->{type} <=> $b->{type})
2545 or ($a->{name} cmp $b->{name})
2546 } @$items;
2622 2547
2623 $self->{real_items} = \@items; 2548 $self->{real_items} = \@items;
2624 2549
2625 for my $item (@items) { 2550 for my $item (@items) {
2626 my $desc = $item->{nrof} < 2 2551 my $desc = $item->{nrof} < 2
2627 ? $item->{name} 2552 ? $item->{name}
2628 : "$item->{nrof} $item->{name_pl}"; 2553 : "$item->{nrof} $item->{name_pl}";
2629 2554
2630 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); 2555 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2631 } 2556 }
2557
2558 $self->{scrolled}->add (@items);
2632 2559
2633# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2560# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2634} 2561}
2635 2562
2636sub size_request { 2563sub size_request {
2710 } 2637 }
2711} 2638}
2712 2639
2713############################################################################# 2640#############################################################################
2714 2641
2642package CFClient::UI::Statusbox;
2643
2644our @ISA = CFClient::UI::VBox::;
2645
2646sub reorder {
2647 my ($self) = @_;
2648 my $NOW = time;
2649
2650 while (my ($k, $v) = each %{ $self->{item} }) {
2651 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2652 }
2653
2654 my @widgets;
2655
2656 my @items = sort {
2657 $a->{pri} <=> $b->{pri}
2658 or $b->{id} <=> $a->{id}
2659 } values %{ $self->{item} };
2660
2661 my $count = 10 + 1;
2662 for my $item (@items) {
2663 last unless --$count;
2664
2665 push @widgets, $item->{label} ||= do {
2666 # TODO: doesn't handle markup well (read as: at all)
2667 my $short = $item->{count} > 1
2668 ? "<b>$item->{count} ×</b> $item->{text}"
2669 : $item->{text};
2670
2671 for ($short) {
2672 s/^\s+//;
2673 s/\012.*/…/s;
2674 my $len = int 40 / $item->{fontsize};
2675 substr $_, $len, length, "…" if $len < length;
2676 }
2677
2678 new CFClient::UI::Label
2679 markup => $short,
2680 tooltip => $item->{tooltip},
2681 tooltip_font => $::FONT_PROP,
2682 tooltip_width => 0.67,
2683 fontsize => $item->{fontsize},
2684 color => $item->{color},
2685 can_events => 1,
2686 can_hover => 1
2687 };
2688 }
2689
2690 $self->clear;
2691 $self->SUPER::add (reverse @widgets);
2692}
2693
2694sub add {
2695 my ($self, $text, %arg) = @_;
2696
2697 $text =~ s/^\s+//;
2698 $text =~ s/\s+$//;
2699
2700 my $timeout = time + ((delete $arg{timeout}) || 60);
2701
2702 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2703
2704 if (my $item = $self->{item}{$group}) {
2705 if ($item->{text} eq $text) {
2706 $item->{count}++;
2707 } else {
2708 $item->{count} = 1;
2709 $item->{text} = $item->{tooltip} = $text;
2710 }
2711 $item->{id} = ++$self->{id};
2712 $item->{timeout} = $timeout;
2713 delete $item->{label};
2714 } else {
2715 $self->{item}{$group} = {
2716 id => ++$self->{id},
2717 text => $text,
2718 timeout => $timeout,
2719 tooltip => $text,
2720 fontsize => 0.8,
2721 color => [0.8, 0.8, 0.8, 0.8],
2722 pri => 0,
2723 count => 1,
2724 %arg,
2725 };
2726 }
2727
2728 $self->reorder;
2729}
2730
2731#############################################################################
2732
2715package CFClient::UI::Root; 2733package CFClient::UI::Root;
2716 2734
2717our @ISA = CFClient::UI::Container::; 2735our @ISA = CFClient::UI::Container::;
2718 2736
2719use CFClient::OpenGL; 2737use CFClient::OpenGL;
2720 2738
2739sub new {
2740 my $class = shift;
2741
2742 $class->SUPER::new (
2743 @_,
2744 )
2745}
2746
2747sub configure {
2748 my ($self, $x, $y, $w, $h) = @_;
2749
2750 $self->{w} = $w;
2751 $self->{h} = $h;
2752}
2753
2721sub check_size { 2754sub check_size {
2722 my ($self) = @_; 2755 my ($self) = @_;
2723 2756
2724 $self->configure (0, 0, $self->{w}, $self->{h}); 2757 $self->size_allocate ($self->{w}, $self->{h})
2758 if $self->{w};
2725} 2759}
2726 2760
2727sub size_request { 2761sub size_request {
2728 my ($self) = @_; 2762 my ($self) = @_;
2729 2763
2747 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; 2781 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2748 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; 2782 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2749 } 2783 }
2750 } 2784 }
2751 2785
2752 $self->{old_w} = $w;
2753 $self->{old_h} = $h;
2754}
2755
2756sub configure {
2757 my ($self, $x, $y, $w, $h) = @_;
2758
2759 $self->SUPER::configure ($x, $y, $w, $h);
2760
2761 for my $child ($self->children) { 2786 for my $child ($self->children) {
2762 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2787 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2763 2788
2764 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2789 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2765 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2790 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2766 $child->configure ($X, $Y, $W, $H); 2791 $child->configure ($X, $Y, $W, $H);
2767 } 2792 }
2768}
2769 2793
2770sub _topleft { 2794 $self->{old_w} = $w;
2795 $self->{old_h} = $h;
2796}
2797
2798sub coord2local {
2771 my ($self, $x, $y) = @_; 2799 my ($self, $x, $y) = @_;
2772 2800
2773 ($x, $y) 2801 ($x, $y)
2774} 2802}
2775 2803
2804sub coord2global {
2805 my ($self, $x, $y) = @_;
2806
2807 ($x, $y)
2808}
2809
2776sub update { 2810sub update {
2777 my ($self) = @_; 2811 my ($self) = @_;
2778 2812
2779 $self->check_size; 2813 $self->check_size;
2780 ::refresh (); 2814 $::WANT_REFRESH++;
2781} 2815}
2782 2816
2783sub add { 2817sub add {
2784 my ($self, $child) = @_; 2818 my ($self, $child) = @_;
2785 2819
2786 # integerize window positions 2820 # integerise window positions
2787 $child->{x} = int $child->{x}; 2821 $child->{x} = int $child->{x};
2788 $child->{y} = int $child->{y}; 2822 $child->{y} = int $child->{y};
2789 2823
2790 $self->SUPER::add ($child); 2824 $self->SUPER::add ($child);
2791} 2825}
2794 my ($self, $id, $cb) = @_; 2828 my ($self, $id, $cb) = @_;
2795 2829
2796 $self->{refresh_hook}{$id} = $cb; 2830 $self->{refresh_hook}{$id} = $cb;
2797} 2831}
2798 2832
2833sub on_post_alloc {
2834 my ($self, $id, $cb) = @_;
2835
2836 $self->{post_alloc_hook}{$id} = $cb;
2837}
2838
2799sub draw { 2839sub draw {
2800 my ($self) = @_; 2840 my ($self) = @_;
2801 2841
2802 while (my $rcb = delete $self->{refresh_hook}) { 2842 while ($self->{refresh_hook}) {
2803 $_->() for values %$rcb; 2843 $_->()
2844 for values %{delete $self->{refresh_hook}};
2845 }
2846
2847 if ($self->{check_size}) {
2848 my @queue = ([], []);
2849
2850 for (;;) {
2851 if ($self->{check_size}) {
2852 # heuristic: check containers last
2853 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_
2854 for values %{delete $self->{check_size}}
2855 }
2856
2857 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last;
2858
2859 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2860 ? @$widget{qw(user_w user_h)}
2861 : $widget->size_request;
2862
2863 if (delete $widget->{force_alloc}
2864 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2865 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2866
2867 $widget->{req_w} = $w;
2868 $widget->{req_h} = $h;
2869
2870 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2871
2872 $widget->{parent}->check_size
2873 if $widget->{parent};
2874 }
2875 }
2876 }
2877
2878 while ($self->{size_alloc}) {
2879 for (values %{delete $self->{size_alloc}}) {
2880 my ($widget, $w, $h) = @$_;
2881
2882 $w = 0 if $w < 0;
2883 $h = 0 if $h < 0;
2884
2885 $widget->{w} = $w;
2886 $widget->{h} = $h;
2887 $widget->size_allocate ($w, $h);
2888 $widget->emit (size_allocate => $w, $h);
2889 }
2890 }
2891
2892 while ($self->{post_alloc_hook}) {
2893 $_->()
2894 for values %{delete $self->{post_alloc_hook}};
2804 } 2895 }
2805 2896
2806 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2897 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2807 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2898 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2808 glClear GL_COLOR_BUFFER_BIT; 2899 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines