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.186 by root, Mon May 8 20:39:34 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->{parent} 396 $self->{force_alloc} = 1 if $forced;
409 or return 1; 397 $CFClient::UI::ROOT->{check_size}{$self} = $self;
410
411 my ($w, $h) = $self->{user_w} && $self->{user_h}
412 ? @$self{qw(user_w user_h)}
413 : $self->size_request;
414
415 if ($w != $self->{req_w} || $h != $self->{req_h}) {
416 $self->{req_w} = $w;
417 $self->{req_h} = $h;
418
419 $self->{parent}->check_size
420 or $self->size_allocate (
421 (List::Util::max $self->{w}, $w),
422 (List::Util::max $self->{h}, $h),
423 );
424
425 1
426 } else {
427 0
428 }
429} 398}
430 399
431sub update { 400sub update {
432 my ($self) = @_; 401 my ($self) = @_;
433 402
530 499
531 $self 500 $self
532} 501}
533 502
534sub add { 503sub add {
535 my ($self, $child) = @_; 504 my ($self, @widgets) = @_;
536 505
537 $child->set_parent ($self); 506 $_->set_parent ($self)
507 for @widgets;
538 508
539 use sort 'stable'; 509 use sort 'stable';
540 510
541 $self->{children} = [ 511 $self->{children} = [
542 sort { $a->{z} <=> $b->{z} } 512 sort { $a->{z} <=> $b->{z} }
543 @{$self->{children}}, $child 513 @{$self->{children}}, @widgets
544 ]; 514 ];
545 515
546 $child->check_size; 516 $self->check_size (1);
547 $self->update; 517 $self->update;
548} 518}
549 519
550sub children { 520sub children {
551 @{ $_[0]{children} } 521 @{ $_[0]{children} }
658} 628}
659 629
660sub update { 630sub update {
661 my ($self) = @_; 631 my ($self) = @_;
662 632
663 $ROOT->on_refresh ($self => sub { $self->render_child }); 633 $ROOT->on_post_alloc ($self => sub { $self->render_child });
664 $self->SUPER::update; 634 $self->SUPER::update;
665} 635}
666 636
667sub size_allocate { 637sub size_allocate {
668 my ($self, $w, $h) = @_; 638 my ($self, $w, $h) = @_;
699 my ($w, $h) = ($self->w, $self->h); 669 my ($w, $h) = ($self->w, $self->h);
700 670
701 my $tex = $self->{texture} 671 my $tex = $self->{texture}
702 or return; 672 or return;
703 673
704 glEnable GL_BLEND;
705 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
706 glEnable GL_TEXTURE_2D; 674 glEnable GL_TEXTURE_2D;
707 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 675 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
708 glColor 0, 0, 0, 1; 676 glColor 0, 0, 0, 1;
709 677
710 $tex->draw_quad (0, 0, $w, $h); 678 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
711 679
712 glDisable GL_BLEND;
713 glDisable GL_TEXTURE_2D; 680 glDisable GL_TEXTURE_2D;
714} 681}
715 682
716############################################################################# 683#############################################################################
717 684
741 $self->{view_y} = int $y; 708 $self->{view_y} = int $y;
742 709
743 $self->update; 710 $self->update;
744} 711}
745 712
746# 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
747sub _topleft { 714sub coord2local {
748 my ($self, $x, $y) = @_; 715 my ($self, $x, $y) = @_;
749 716
750 $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)
751} 727}
752 728
753sub find_widget { 729sub find_widget {
754 my ($self, $x, $y) = @_; 730 my ($self, $x, $y) = @_;
755 731
800 $self->add ($self->{slider}); 776 $self->add ($self->{slider});
801 777
802 $self 778 $self
803} 779}
804 780
805#TODO# update range on size_allocate depeneing on child 781#TODO# update range on size_allocate depending on child
806# update viewport offset on scroll 782# update viewport offset on scroll
807 783
808############################################################################# 784#############################################################################
809 785
810package CFClient::UI::Frame; 786package CFClient::UI::Frame;
814use CFClient::OpenGL; 790use CFClient::OpenGL;
815 791
816sub new { 792sub new {
817 my $class = shift; 793 my $class = shift;
818 794
819 my $self = $class->SUPER::new ( 795 $class->SUPER::new (
820 bg => [1, 1, 1, 1], 796 bg => undef,
821 border_bg => [1, 1, 1, 1],
822 border => 0.8,
823 @_ 797 @_,
824 ); 798 )
825
826 $self
827} 799}
828 800
829sub _draw { 801sub _draw {
830 my ($self) = @_; 802 my ($self) = @_;
831 803
832 my ($w, $h) = ($self->{w}, $self->{h}); 804 if ($self->{bg}) {
805 my ($w, $h) = @$self{qw(w h)};
833 806
834 glEnable GL_BLEND; 807 glEnable GL_BLEND;
835 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 808 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
836 glEnable GL_TEXTURE_2D; 809 glColor @{ $self->{bg} };
837 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
838 810
839# glBegin GL_QUADS; 811 glBegin GL_QUADS;
840# glColor 0, 0, 0, 0;
841# glVertex 0 , 0; 812 glVertex 0 , 0;
842# glVertex 0 , $h; 813 glVertex 0 , $h;
843# glVertex $w, $h; 814 glVertex $w, $h;
844# glVertex $w, 0; 815 glVertex $w, 0;
845# glEnd; 816 glEnd;
846 817
847
848 $self->child->draw;
849 glDisable GL_BLEND; 818 glDisable GL_BLEND;
850 glDisable GL_TEXTURE_2D; 819 }
820
821 $self->SUPER::_draw;
851} 822}
852 823
853############################################################################# 824#############################################################################
854 825
855package CFClient::UI::FancyFrame; 826package CFClient::UI::FancyFrame;
971 my ($self) = @_; 942 my ($self) = @_;
972 943
973 my ($w, $h ) = ($self->{w}, $self->{h}); 944 my ($w, $h ) = ($self->{w}, $self->{h});
974 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 945 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
975 946
976 glEnable GL_BLEND;
977 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
978 glEnable GL_TEXTURE_2D; 947 glEnable GL_TEXTURE_2D;
979 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 948 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
980 949
981 my $border = $self->border; 950 my $border = $self->border;
982 951
983 glColor @{ $self->{border_bg} }; 952 glColor @{ $self->{border_bg} };
984 $tex[1]->draw_quad (0, 0, $w, $border); 953 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
985 $tex[3]->draw_quad (0, $border, $border, $ch); 954 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
986 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 955 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
987 $tex[4]->draw_quad (0, $h - $border, $w, $border); 956 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
988 957
989 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 958 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
990 my $bg = $tex[0]; 959 my $bg = $tex[0];
991 960
992 # TODO: repeat texture not scale 961 # TODO: repeat texture not scale
996 glColor @{ $self->{bg} }; 965 glColor @{ $self->{bg} };
997 966
998 $bg->{s} = $rep_x; 967 $bg->{s} = $rep_x;
999 $bg->{t} = $rep_y; 968 $bg->{t} = $rep_y;
1000 $bg->{wrap_mode} = 1; 969 $bg->{wrap_mode} = 1;
1001 $bg->draw_quad ($border, $border, $cw, $ch); 970 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
971 }
1002 972
1003 glDisable GL_TEXTURE_2D; 973 glDisable GL_TEXTURE_2D;
1004 glDisable GL_BLEND;
1005 }
1006 974
1007 $self->{title}->draw if $self->{title}; 975 $self->{title}->draw if $self->{title};
1008 976
1009 $self->child->draw; 977 $self->child->draw;
1010} 978}
1237} 1205}
1238 1206
1239sub size_allocate { 1207sub size_allocate {
1240 my ($self, $w, $h) = @_; 1208 my ($self, $w, $h) = @_;
1241 1209
1210 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1211
1242 my $children = $self->{children}; 1212 my $children = $self->{children};
1243 1213
1244 my @h = map $_->{req_h}, @$children; 1214 my @h = map $_->{req_h}, @$children;
1245 1215
1246 my $req_h = List::Util::sum @h; 1216 my $req_h = List::Util::sum @h;
1285 my ($class, %arg) = @_; 1255 my ($class, %arg) = @_;
1286 1256
1287 my $self = $class->SUPER::new ( 1257 my $self = $class->SUPER::new (
1288 fg => [1, 1, 1], 1258 fg => [1, 1, 1],
1289 #font => default_font 1259 #font => default_font
1260 #text => initial text
1261 #markup => initial narkup
1262 layout => (new CFClient::Layout),
1290 fontsize => 1, 1263 fontsize => 1,
1291 text => "",
1292 align => -1, 1264 align => -1,
1293 valign => -1, 1265 valign => -1,
1294 padding => 2, 1266 padding => 2,
1295 layout => new CFClient::Layout,
1296 can_events => 0, 1267 can_events => 0,
1297 %arg 1268 %arg
1298 ); 1269 );
1299 1270
1300 if (exists $self->{template}) { 1271 if (exists $self->{template}) {
1301 my $layout = new CFClient::Layout; 1272 my $layout = new CFClient::Layout;
1302 $layout->set_text (delete $self->{template}); 1273 $layout->set_text (delete $self->{template});
1303 $self->{template} = $layout; 1274 $self->{template} = $layout;
1304 } 1275 }
1305 1276
1306 $self->set_text (delete $self->{text}) if exists $self->{text}; 1277 if (exists $self->{markup}) {
1307 $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 }
1308 1282
1309 $self 1283 $self
1310} 1284}
1311 1285
1312sub escape { 1286sub escape {
1330 my ($self, $text) = @_; 1304 my ($self, $text) = @_;
1331 1305
1332 return if $self->{text} eq "T$text"; 1306 return if $self->{text} eq "T$text";
1333 $self->{text} = "T$text"; 1307 $self->{text} = "T$text";
1334 1308
1309 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1335 $self->{layout}->set_text ($text); 1310 $self->{layout}->set_text ($text);
1336 1311
1337 delete $self->{texture};
1338 $self->update; 1312 $self->update;
1339 $self->check_size; 1313 $self->check_size;
1340} 1314}
1341 1315
1342sub set_markup { 1316sub set_markup {
1343 my ($self, $markup) = @_; 1317 my ($self, $markup) = @_;
1344 1318
1345 return if $self->{text} eq "M$markup"; 1319 return if $self->{text} eq "M$markup";
1346 $self->{text} = "M$markup"; 1320 $self->{text} = "M$markup";
1347 1321
1322 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1323
1324 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1348 $self->{layout}->set_markup ($markup); 1325 $self->{layout}->set_markup ($markup);
1349 1326
1350 delete $self->{texture};
1351 $self->update; 1327 $self->update;
1352 $self->check_size; 1328 $self->check_size;
1353} 1329}
1354 1330
1355sub size_request { 1331sub size_request {
1395 1371
1396sub _draw { 1372sub _draw {
1397 my ($self) = @_; 1373 my ($self) = @_;
1398 1374
1399 my $tex = $self->{texture} ||= do { 1375 my $tex = $self->{texture} ||= do {
1376 $self->{layout}->set_foreground (@{$self->{fg}});
1400 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1377 $self->{layout}->set_font ($self->{font}) if $self->{font};
1401 $self->{layout}->set_width ($self->{w}); 1378 $self->{layout}->set_width ($self->{w});
1402 $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
1403 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
1404 }; 1392 };
1405 1393
1406 glEnable GL_BLEND;
1407 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1408 glEnable GL_TEXTURE_2D; 1394 glEnable GL_TEXTURE_2D;
1409 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1395 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1410 1396
1397 if ($tex->{format} == GL_ALPHA) {
1411 glColor @{$self->{fg}}; 1398 glColor @{$self->{fg}};
1412
1413 $self->{ox} = int (
1414 $self->{align} < 0 ? $self->{padding}
1415 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1416 : ($self->{w} - $tex->{w}) * 0.5
1417 );
1418
1419 $self->{oy} = int (
1420 $self->{valign} < 0 ? $self->{padding}
1421 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1422 : ($self->{h} - $tex->{h}) * 0.5
1423 );
1424
1425 $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 }
1426 1403
1427 glDisable GL_TEXTURE_2D; 1404 glDisable GL_TEXTURE_2D;
1428 glDisable GL_BLEND;
1429} 1405}
1430 1406
1431############################################################################# 1407#############################################################################
1432 1408
1433package CFClient::UI::EntryBase; 1409package CFClient::UI::EntryBase;
1468 $self->{layout}->set_text ("$text "); 1444 $self->{layout}->set_text ("$text ");
1469 1445
1470 $self->emit (changed => $self->{text}); 1446 $self->emit (changed => $self->{text});
1471} 1447}
1472 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
1473sub get_text { 1458sub get_text {
1474 $_[0]{text} 1459 $_[0]{text}
1475} 1460}
1476 1461
1477sub size_request { 1462sub size_request {
1484 1469
1485sub size_allocate { 1470sub size_allocate {
1486 my ($self, $w, $h) = @_; 1471 my ($self, $w, $h) = @_;
1487 1472
1488 $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
1489}
1490
1491sub set_text {
1492 my ($self, $text) = @_;
1493
1494 $self->{cursor} = length $text;
1495 $self->_set_text ($text);
1496 $self->update;
1497} 1474}
1498 1475
1499sub key_down { 1476sub key_down {
1500 my ($self, $ev) = @_; 1477 my ($self, $ev) = @_;
1501 1478
1503 my $sym = $ev->{sym}; 1480 my $sym = $ev->{sym};
1504 my $uni = $ev->{unicode}; 1481 my $uni = $ev->{unicode};
1505 1482
1506 my $text = $self->get_text; 1483 my $text = $self->get_text;
1507 1484
1508 if ($sym == 8) { 1485 if ($uni == 8) {
1509 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1486 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1510 } elsif ($sym == 127) { 1487 } elsif ($uni == 127) {
1511 substr $text, $self->{cursor}, 1, ""; 1488 substr $text, $self->{cursor}, 1, "";
1512 } elsif ($sym == CFClient::SDLK_LEFT) { 1489 } elsif ($sym == CFClient::SDLK_LEFT) {
1513 --$self->{cursor} if $self->{cursor}; 1490 --$self->{cursor} if $self->{cursor};
1514 } elsif ($sym == CFClient::SDLK_RIGHT) { 1491 } elsif ($sym == CFClient::SDLK_RIGHT) {
1515 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1492 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1516 } elsif ($sym == CFClient::SDLK_HOME) { 1493 } elsif ($sym == CFClient::SDLK_HOME) {
1517 $self->{cursor} = 0; 1494 $self->{cursor} = 0;
1518 } elsif ($sym == CFClient::SDLK_END) { 1495 } elsif ($sym == CFClient::SDLK_END) {
1519 $self->{cursor} = length $text; 1496 $self->{cursor} = length $text;
1520 } elsif ($sym == 27) { 1497 } elsif ($uni == 27) {
1521 $self->emit ('escape'); 1498 $self->emit ('escape');
1522 } elsif ($uni) { 1499 } elsif ($uni) {
1523 substr $text, $self->{cursor}++, 0, chr $uni; 1500 substr $text, $self->{cursor}++, 0, chr $uni;
1524 } 1501 }
1525 1502
1689 1666
1690 if ($GRAB == $self) { 1667 if ($GRAB == $self) {
1691 $self->{fg} = $self->{active_fg}; 1668 $self->{fg} = $self->{active_fg};
1692 } 1669 }
1693 1670
1694 glEnable GL_BLEND;
1695 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1696 glEnable GL_TEXTURE_2D; 1671 glEnable GL_TEXTURE_2D;
1697 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1672 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1698 glColor 0, 0, 0, 1; 1673 glColor 0, 0, 0, 1;
1699 1674
1700 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1675 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1701 1676
1702 glDisable GL_TEXTURE_2D; 1677 glDisable GL_TEXTURE_2D;
1703 glDisable GL_BLEND;
1704 1678
1705 $self->SUPER::_draw; 1679 $self->SUPER::_draw;
1706} 1680}
1707 1681
1708############################################################################# 1682#############################################################################
1755 1729
1756 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;
1757 1731
1758 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1732 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1759 1733
1760 glEnable GL_BLEND; 1734 my $tex = $self->{state} ? $tex[1] : $tex[0];
1735
1761 glEnable GL_TEXTURE_2D; 1736 glEnable GL_TEXTURE_2D;
1762 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1763
1764 my $tex = $self->{state} ? $tex[1] : $tex[0];
1765
1766 $tex->draw_quad (0, 0, $s, $s); 1737 $tex->draw_quad_alpha (0, 0, $s, $s);
1767
1768 glDisable GL_TEXTURE_2D; 1738 glDisable GL_TEXTURE_2D;
1769 glDisable GL_BLEND;
1770} 1739}
1771 1740
1772############################################################################# 1741#############################################################################
1773 1742
1774package CFClient::UI::Image; 1743package CFClient::UI::Image;
1817 glTranslate 0, -$self->{w}, 0; 1786 glTranslate 0, -$self->{w}, 0;
1818 1787
1819 ($w, $h) = ($h, $w); 1788 ($w, $h) = ($h, $w);
1820 } 1789 }
1821 1790
1822 glEnable GL_BLEND;
1823 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1824 glEnable GL_TEXTURE_2D; 1791 glEnable GL_TEXTURE_2D;
1825 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1792 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1826 1793
1827 $tex->draw_quad (0, 0, $w, $h); 1794 $tex->draw_quad_alpha (0, 0, $w, $h);
1828 1795
1829 glDisable GL_BLEND;
1830 glDisable GL_TEXTURE_2D; 1796 glDisable GL_TEXTURE_2D;
1831} 1797}
1832 1798
1833############################################################################# 1799#############################################################################
1834 1800
2126 $page ||= 2; 2092 $page ||= 2;
2127 2093
2128 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2094 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2129 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2095 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2130 2096
2131 glEnable GL_BLEND;
2132 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2133 glEnable GL_TEXTURE_2D; 2097 glEnable GL_TEXTURE_2D;
2134 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2098 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2135 2099
2136 # draw background 2100 # draw background
2137 $tex[1]->draw_quad (0, 0, $w, $h); 2101 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2138 2102
2139 # draw handle 2103 # draw handle
2140 $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);
2141 2105
2142 glDisable GL_BLEND;
2143 glDisable GL_TEXTURE_2D; 2106 glDisable GL_TEXTURE_2D;
2144} 2107}
2145 2108
2146############################################################################# 2109#############################################################################
2147 2110
2158 fontsize => 1, 2121 fontsize => 1,
2159 can_events => 0, 2122 can_events => 0,
2160 #font => default_font 2123 #font => default_font
2161 @_, 2124 @_,
2162 2125
2163 layout => (new CFClient::Layout), 2126 layout => (new CFClient::Layout 1),
2164 par => [], 2127 par => [],
2165 height => 0, 2128 height => 0,
2166 children => [ 2129 children => [
2167 (new CFClient::UI::Empty expand => 1), 2130 (new CFClient::UI::Empty expand => 1),
2168 (new CFClient::UI::Slider vertical => 1), 2131 (new CFClient::UI::Slider vertical => 1),
2186 2149
2187 my $layout = $self->{layout}; 2150 my $layout = $self->{layout};
2188 2151
2189 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2152 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2190 $layout->set_width ($self->{children}[0]{w}); 2153 $layout->set_width ($self->{children}[0]{w});
2191 $layout->set_text ($text); 2154 $layout->set_markup ($text);
2192 2155
2193 ($layout->size)[1] 2156 ($layout->size)[1]
2194} 2157}
2195 2158
2196sub reflow { 2159sub reflow {
2234 2197
2235 return unless $self->{h} > 0; 2198 return unless $self->{h} > 0;
2236 2199
2237 delete $self->{texture}; 2200 delete $self->{texture};
2238 2201
2239 $ROOT->on_refresh ($self, sub { 2202 $ROOT->on_post_alloc ($self, sub {
2240 if (delete $self->{need_reflow}) { 2203 if (delete $self->{need_reflow}) {
2241 my $height = 0; 2204 my $height = 0;
2242 2205
2243 $height += $_->[0] = $self->text_height ($_->[2]) 2206 $height += $_->[0] = $self->text_height ($_->[2])
2244 for @{$self->{par}}; 2207 for @{$self->{par}};
2252 2215
2253 $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 {
2254 glClearColor 0, 0, 0, 0; 2217 glClearColor 0, 0, 0, 0;
2255 glClear GL_COLOR_BUFFER_BIT; 2218 glClear GL_COLOR_BUFFER_BIT;
2256 2219
2257 glEnable GL_BLEND;
2258 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2259 glEnable GL_TEXTURE_2D; 2220 glEnable GL_TEXTURE_2D;
2260 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2221 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2261 2222
2262 my $top = int $self->{children}[1]{range}[0]; 2223 my $top = int $self->{children}[1]{range}[0];
2263 2224
2272 2233
2273 for my $par (@{$self->{par}}) { 2234 for my $par (@{$self->{par}}) {
2274 my $h = $par->[0]; 2235 my $h = $par->[0];
2275 2236
2276 if ($y0 < $y + $h && $y < $y1) { 2237 if ($y0 < $y + $h && $y < $y1) {
2238 $layout->set_foreground (@{ $par->[1] });
2277 $layout->set_text ($par->[2]); 2239 $layout->set_markup ($par->[2]);
2278 2240
2279 glColor @{ $par->[1] };
2280 my ($W, $H) = $layout->size; 2241 my ($W, $H) = $layout->size;
2281 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);
2282 } 2243 }
2283 2244
2284 $y += $h; 2245 $y += $h;
2285 } 2246 }
2286 2247
2287 glDisable GL_TEXTURE_2D; 2248 glDisable GL_TEXTURE_2D;
2288 glDisable GL_BLEND;
2289 }; 2249 };
2290 }); 2250 });
2291} 2251}
2292 2252
2293sub _draw { 2253sub _draw {
2294 my ($self) = @_; 2254 my ($self) = @_;
2295 2255
2296 glEnable GL_BLEND;
2297 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2298 glEnable GL_TEXTURE_2D; 2256 glEnable GL_TEXTURE_2D;
2299 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2257 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2300 glColor 1, 1, 1, 1; 2258 glColor 1, 1, 1, 1;
2301 $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});
2302 glDisable GL_TEXTURE_2D; 2260 glDisable GL_TEXTURE_2D;
2303 glDisable GL_BLEND;
2304 2261
2305 $self->{children}[1]->draw; 2262 $self->{children}[1]->draw;
2306 2263
2307} 2264}
2308 2265
2402 @_, 2359 @_,
2403 can_events => 0, 2360 can_events => 0,
2404 ) 2361 )
2405} 2362}
2406 2363
2407sub set_markup { 2364sub set_tooltip_from {
2408 my ($self, $text) = @_; 2365 my ($self, $widget) = @_;
2409 2366
2410 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2367 $self->add (new CFClient::UI::Label
2411 $self->{label}->set_markup ($text); 2368 markup => $widget->{tooltip},
2412 $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 );
2413} 2374}
2414 2375
2415sub size_request { 2376sub size_request {
2416 my ($self) = @_; 2377 my ($self) = @_;
2417
2418 $self->child->set_max_size ($::WIDTH * 0.3);
2419 2378
2420 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2379 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2421 2380
2422 ($w + 4, $h + 4) 2381 ($w + 4, $h + 4)
2423} 2382}
2429} 2388}
2430 2389
2431sub _draw { 2390sub _draw {
2432 my ($self) = @_; 2391 my ($self) = @_;
2433 2392
2434 glPushMatrix;
2435 glTranslate 0.375, 0.375; 2393 glTranslate 0.375, 0.375;
2436 2394
2437 my ($w, $h) = @$self{qw(w h)}; 2395 my ($w, $h) = @$self{qw(w h)};
2438 2396
2439 glColor 1, 0.8, 0.4; 2397 glColor 1, 0.8, 0.4;
2450 glVertex 0 , $h; 2408 glVertex 0 , $h;
2451 glVertex $w, $h; 2409 glVertex $w, $h;
2452 glVertex $w, 0; 2410 glVertex $w, 0;
2453 glEnd; 2411 glEnd;
2454 2412
2455 glPopMatrix; 2413 glTranslate 2 - 0.375, 2 - 0.375;
2456
2457 glTranslate 2, 2;
2458 $self->SUPER::_draw; 2414 $self->SUPER::_draw;
2459} 2415}
2460 2416
2461############################################################################# 2417#############################################################################
2462 2418
2485 return unless $::CONN;#d# manage and cache textures differently 2441 return unless $::CONN;#d# manage and cache textures differently
2486 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2442 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2487 2443
2488 # TODO animation 2444 # TODO animation
2489 if ($tex) { 2445 if ($tex) {
2490 glEnable GL_BLEND;
2491 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2492 glEnable GL_TEXTURE_2D; 2446 glEnable GL_TEXTURE_2D;
2493 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2447 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2494 glColor 1, 1, 1, 1; 2448 glColor 1, 1, 1, 1;
2495 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2449 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2496 glDisable GL_TEXTURE_2D; 2450 glDisable GL_TEXTURE_2D;
2497 glDisable GL_BLEND;
2498 } 2451 }
2499} 2452}
2500 2453
2501############################################################################# 2454#############################################################################
2502 2455
2507sub new { 2460sub new {
2508 my $class = shift; 2461 my $class = shift;
2509 2462
2510 my %args = @_; 2463 my %args = @_;
2511 2464
2512 my $item = $args{item}; 2465 my $item = delete $args{item};
2513 2466
2514 my $desc = $item->{nrof} < 2 2467 my $desc = $item->{nrof} < 2
2515 ? $item->{name} 2468 ? $item->{name}
2516 : "$item->{nrof} $item->{name_pl}"; 2469 : "$item->{nrof} $item->{name_pl}";
2517 2470
2546 2499
2547 1 2500 1
2548 }, 2501 },
2549 %args 2502 %args
2550 ); 2503 );
2504
2551 $self->add(new CFClient::UI::Face 2505 $self->add (new CFClient::UI::Face
2552 can_events => 0, 2506 can_events => 0,
2553 face => $item->{face}, 2507 face => $item->{face},
2554 anim => $item->{anim}, 2508 anim => $item->{anim},
2555 animspeed => $item->{animspeed}); 2509 animspeed => $item->{animspeed},
2510 );
2511
2556 $self->add(new CFClient::UI::Label 2512 $self->add (new CFClient::UI::Label
2557 can_events => 0, 2513 can_events => 0,
2558 text => $desc); 2514 text => $desc,
2515 );
2559 2516
2560 $self 2517 $self
2561} 2518}
2562 2519
2563############################################################################# 2520#############################################################################
2581 my ($self, $items) = @_; 2538 my ($self, $items) = @_;
2582 2539
2583 $self->{scrolled}->clear; 2540 $self->{scrolled}->clear;
2584 return unless $items; 2541 return unless $items;
2585 2542
2586 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;
2587 2547
2588 $self->{real_items} = \@items; 2548 $self->{real_items} = \@items;
2589 2549
2590 for my $item (@items) { 2550 for my $item (@items) {
2591 my $desc = $item->{nrof} < 2 2551 my $desc = $item->{nrof} < 2
2592 ? $item->{name} 2552 ? $item->{name}
2593 : "$item->{nrof} $item->{name_pl}"; 2553 : "$item->{nrof} $item->{name_pl}";
2594 2554
2595 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); 2555 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2596 } 2556 }
2557
2558 $self->{scrolled}->add (@items);
2597 2559
2598# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2560# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2599} 2561}
2600 2562
2601sub size_request { 2563sub size_request {
2675 } 2637 }
2676} 2638}
2677 2639
2678############################################################################# 2640#############################################################################
2679 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
2680package CFClient::UI::Root; 2733package CFClient::UI::Root;
2681 2734
2682our @ISA = CFClient::UI::Container::; 2735our @ISA = CFClient::UI::Container::;
2683 2736
2684use CFClient::OpenGL; 2737use CFClient::OpenGL;
2685 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
2686sub check_size { 2754sub check_size {
2687 my ($self) = @_; 2755 my ($self) = @_;
2688 2756
2689 $self->configure (0, 0, $self->{w}, $self->{h}); 2757 $self->size_allocate ($self->{w}, $self->{h})
2758 if $self->{w};
2690} 2759}
2691 2760
2692sub size_request { 2761sub size_request {
2693 my ($self) = @_; 2762 my ($self) = @_;
2694 2763
2702 my $old_h = $self->{old_h}; 2771 my $old_h = $self->{old_h};
2703 2772
2704 if ($old_w && $old_h) { 2773 if ($old_w && $old_h) {
2705 for my $child ($self->children) { 2774 for my $child ($self->children) {
2706 $child->{x} = int 0.5 + $child->{x} * $w / $old_w; 2775 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2707 $child->{w} = int 0.5 + $child->{req_w} * $w / $old_w; 2776 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2708 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; 2777 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2709 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; 2778 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2710 $child->{y} = int 0.5 + $child->{y} * $h / $old_h; 2779 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2711 $child->{h} = int 0.5 + $child->{h} * $h / $old_h; 2780 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2712 $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};
2713 $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};
2714 } 2783 }
2715 } 2784 }
2716 2785
2717 $self->{old_w} = $w;
2718 $self->{old_h} = $h;
2719}
2720
2721sub configure {
2722 my ($self, $x, $y, $w, $h) = @_;
2723
2724 $self->SUPER::configure ($x, $y, $w, $h);
2725
2726 for my $child ($self->children) { 2786 for my $child ($self->children) {
2727 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)};
2728 2788
2729 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2789 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2730 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2790 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2731 $child->configure ($X, $Y, $W, $H); 2791 $child->configure ($X, $Y, $W, $H);
2732 } 2792 }
2733}
2734 2793
2735sub _topleft { 2794 $self->{old_w} = $w;
2795 $self->{old_h} = $h;
2796}
2797
2798sub coord2local {
2736 my ($self, $x, $y) = @_; 2799 my ($self, $x, $y) = @_;
2737 2800
2738 ($x, $y) 2801 ($x, $y)
2739} 2802}
2740 2803
2804sub coord2global {
2805 my ($self, $x, $y) = @_;
2806
2807 ($x, $y)
2808}
2809
2741sub update { 2810sub update {
2742 my ($self) = @_; 2811 my ($self) = @_;
2743 2812
2744 $self->check_size; 2813 $self->check_size;
2745 ::refresh (); 2814 $::WANT_REFRESH++;
2746} 2815}
2747 2816
2748sub add { 2817sub add {
2749 my ($self, $child) = @_; 2818 my ($self, $child) = @_;
2750 2819
2751 # integerize window positions 2820 # integerise window positions
2752 $child->{x} = int $child->{x}; 2821 $child->{x} = int $child->{x};
2753 $child->{y} = int $child->{y}; 2822 $child->{y} = int $child->{y};
2754 2823
2755 $self->SUPER::add ($child); 2824 $self->SUPER::add ($child);
2756} 2825}
2759 my ($self, $id, $cb) = @_; 2828 my ($self, $id, $cb) = @_;
2760 2829
2761 $self->{refresh_hook}{$id} = $cb; 2830 $self->{refresh_hook}{$id} = $cb;
2762} 2831}
2763 2832
2833sub on_post_alloc {
2834 my ($self, $id, $cb) = @_;
2835
2836 $self->{post_alloc_hook}{$id} = $cb;
2837}
2838
2764sub draw { 2839sub draw {
2765 my ($self) = @_; 2840 my ($self) = @_;
2766 2841
2767 while (my $rcb = delete $self->{refresh_hook}) { 2842 while ($self->{refresh_hook}) {
2768 $_->() 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}};
2769 } 2895 }
2770 2896
2771 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2897 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2772 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2898 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2773 glClear GL_COLOR_BUFFER_BIT; 2899 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines