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.184 by root, Wed May 3 19:56:05 2006 UTC vs.
Revision 1.198 by root, Fri May 12 02:08:52 2006 UTC

1package CFClient::UI; 1package CFClient::UI;
2 2
3use utf8;
3use strict; 4use strict;
4 5
5use Scalar::Util (); 6use Scalar::Util ();
6use List::Util (); 7use List::Util ();
7 8
23 24
24 my $tip = $widget->{tooltip}; 25 my $tip = $widget->{tooltip};
25 26
26 $tip = $tip->($widget) if CODE:: eq ref $tip; 27 $tip = $tip->($widget) if CODE:: eq ref $tip;
27 28
28 $TOOLTIP->set_markup ($widget->{tooltip}); 29 $TOOLTIP->set_tooltip_from ($widget);
29
30 $TOOLTIP->show; 30 $TOOLTIP->show;
31 31
32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
33 33
34 if ($x + $TOOLTIP->{w} > $::WIDTH) { 34 if ($x + $TOOLTIP->{w} > $::WIDTH) {
35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); 35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0);
36 } 36 }
37 37
38 $TOOLTIP->move ($x, $y); 38 $TOOLTIP->move ($x, $y);
39 $TOOLTIP->check_size;
40 $TOOLTIP->update;
39 } 41 }
40 42
41 return; 43 return;
42 } 44 }
43 } 45 }
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 $_->check_size; 257 $self->check_size (1);
258 $self->update;
261} 259}
262 260
263sub set_max_size { 261sub set_max_size {
264 my ($self, $w, $h) = @_; 262 my ($self, $w, $h) = @_;
265 263
266 delete $self->{max_w}; $self->{max_w} = $w if $w; 264 delete $self->{max_w}; $self->{max_w} = $w if $w;
267 delete $self->{max_h}; $self->{max_h} = $h if $h; 265 delete $self->{max_h}; $self->{max_h} = $h if $h;
268}
269
270# return top left coordinates
271sub _topleft {
272 my ($self, $x, $y) = @_;
273
274 $self->{parent}
275 or Carp::confess "no parent widget in _topleft\n";#d#
276
277 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
278} 266}
279 267
280# translate global coordinates to local coordinate system 268# translate global coordinates to local coordinate system
281sub coord2local { 269sub coord2local {
282 my ($self, $x, $y) = @_; 270 my ($self, $x, $y) = @_;
283 271
284 my ($X, $Y) = $self->_topleft; 272 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
285 ($x - $X, $y - $Y)
286} 273}
287 274
288# translate local coordinates to global coordinate system 275# translate local coordinates to global coordinate system
289sub coord2global { 276sub coord2global {
290 my ($self, $x, $y) = @_; 277 my ($self, $x, $y) = @_;
291 278
292 my ($X, $Y) = $self->_topleft; 279 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
293 ($x + $X, $y + $Y)
294} 280}
295 281
296sub focus_in { 282sub focus_in {
297 my ($self) = @_; 283 my ($self) = @_;
298 284
396 382
397sub set_parent { 383sub set_parent {
398 my ($self, $parent) = @_; 384 my ($self, $parent) = @_;
399 385
400 Scalar::Util::weaken ($self->{parent} = $parent); 386 Scalar::Util::weaken ($self->{parent} = $parent);
387
388
389 # TODO: req_w _does_change after ->reconfigure
390 $self->check_size
391 unless exists $self->{req_w};
401} 392}
402 393
403sub check_size { 394sub check_size {
404 my ($self) = @_; 395 my ($self, $forced) = @_;
405 396
406 $self->{parent} 397 $self->{force_alloc} = 1 if $forced;
407 or return 1; 398 $CFClient::UI::ROOT->{check_size}{$self} = $self;
408
409 my ($w, $h) = $self->{user_w} && $self->{user_h}
410 ? @$self{qw(user_w user_h)}
411 : $self->size_request;
412
413 if ($w != $self->{req_w} || $h != $self->{req_h}) {
414 $self->{req_w} = $w;
415 $self->{req_h} = $h;
416
417 $self->{parent}->check_size
418 or $self->size_allocate (
419 (List::Util::max $self->{w}, $w),
420 (List::Util::max $self->{h}, $h),
421 );
422
423 1
424 } else {
425 0
426 }
427} 399}
428 400
429sub update { 401sub update {
430 my ($self) = @_; 402 my ($self) = @_;
431 403
528 500
529 $self 501 $self
530} 502}
531 503
532sub add { 504sub add {
533 my ($self, $child) = @_; 505 my ($self, @widgets) = @_;
534 506
535 $child->set_parent ($self); 507 $_->set_parent ($self)
508 for @widgets;
536 509
537 use sort 'stable'; 510 use sort 'stable';
538 511
539 $self->{children} = [ 512 $self->{children} = [
540 sort { $a->{z} <=> $b->{z} } 513 sort { $a->{z} <=> $b->{z} }
541 @{$self->{children}}, $child 514 @{$self->{children}}, @widgets
542 ]; 515 ];
543 516
544 $child->check_size; 517 $self->check_size (1);
545 $self->update; 518 $self->update;
546} 519}
547 520
548sub children { 521sub children {
549 @{ $_[0]{children} } 522 @{ $_[0]{children} }
656} 629}
657 630
658sub update { 631sub update {
659 my ($self) = @_; 632 my ($self) = @_;
660 633
661 $ROOT->on_refresh ($self => sub { $self->render_child }); 634 $ROOT->on_post_alloc ($self => sub { $self->render_child });
662 $self->SUPER::update; 635 $self->SUPER::update;
663} 636}
664 637
665sub size_allocate { 638sub size_allocate {
666 my ($self, $w, $h) = @_; 639 my ($self, $w, $h) = @_;
697 my ($w, $h) = ($self->w, $self->h); 670 my ($w, $h) = ($self->w, $self->h);
698 671
699 my $tex = $self->{texture} 672 my $tex = $self->{texture}
700 or return; 673 or return;
701 674
702 glEnable GL_BLEND;
703 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
704 glEnable GL_TEXTURE_2D; 675 glEnable GL_TEXTURE_2D;
705 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 676 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
706 glColor 0, 0, 0, 1; 677 glColor 0, 0, 0, 1;
707 678
708 $tex->draw_quad (0, 0, $w, $h); 679 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
709 680
710 glDisable GL_BLEND;
711 glDisable GL_TEXTURE_2D; 681 glDisable GL_TEXTURE_2D;
712} 682}
713 683
714############################################################################# 684#############################################################################
715 685
739 $self->{view_y} = int $y; 709 $self->{view_y} = int $y;
740 710
741 $self->update; 711 $self->update;
742} 712}
743 713
744# hmm, this does not work for topleft of $self... but we should not aks for that 714# hmm, this does not work for topleft of $self... but we should not ask for that
745sub _topleft { 715sub coord2local {
746 my ($self, $x, $y) = @_; 716 my ($self, $x, $y) = @_;
747 717
748 $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y}) 718 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
719}
720
721sub coord2global {
722 my ($self, $x, $y) = @_;
723
724 $x = List::Util::min $self->{w}, $x - $self->{view_x};
725 $y = List::Util::min $self->{h}, $y - $self->{view_y};
726
727 $self->SUPER::coord2global ($x, $y)
749} 728}
750 729
751sub find_widget { 730sub find_widget {
752 my ($self, $x, $y) = @_; 731 my ($self, $x, $y) = @_;
753 732
809 788
810our @ISA = CFClient::UI::Bin::; 789our @ISA = CFClient::UI::Bin::;
811 790
812use CFClient::OpenGL; 791use CFClient::OpenGL;
813 792
814sub new {
815 my $class = shift;
816
817 my $self = $class->SUPER::new (
818 bg => [1, 1, 1, 1],
819 border_bg => [1, 1, 1, 1],
820 border => 0.8,
821 @_
822 );
823
824 $self
825}
826
827sub _draw {
828 my ($self) = @_;
829
830 my ($w, $h) = ($self->{w}, $self->{h});
831
832 glEnable GL_BLEND;
833 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
834 glEnable GL_TEXTURE_2D;
835 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
836
837# glBegin GL_QUADS;
838# glColor 0, 0, 0, 0;
839# glVertex 0 , 0;
840# glVertex 0 , $h;
841# glVertex $w, $h;
842# glVertex $w, 0;
843# glEnd;
844
845
846 $self->child->draw;
847 glDisable GL_BLEND;
848 glDisable GL_TEXTURE_2D;
849}
850
851############################################################################# 793#############################################################################
852 794
853package CFClient::UI::FancyFrame; 795package CFClient::UI::FancyFrame;
854 796
855our @ISA = CFClient::UI::Bin::; 797our @ISA = CFClient::UI::Bin::;
969 my ($self) = @_; 911 my ($self) = @_;
970 912
971 my ($w, $h ) = ($self->{w}, $self->{h}); 913 my ($w, $h ) = ($self->{w}, $self->{h});
972 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 914 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
973 915
974 glEnable GL_BLEND;
975 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
976 glEnable GL_TEXTURE_2D; 916 glEnable GL_TEXTURE_2D;
977 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 917 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
978 918
979 my $border = $self->border; 919 my $border = $self->border;
980 920
981 glColor @{ $self->{border_bg} }; 921 glColor @{ $self->{border_bg} };
982 $tex[1]->draw_quad (0, 0, $w, $border); 922 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
983 $tex[3]->draw_quad (0, $border, $border, $ch); 923 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
984 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 924 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
985 $tex[4]->draw_quad (0, $h - $border, $w, $border); 925 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
986 926
987 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 927 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
988 my $bg = $tex[0]; 928 my $bg = $tex[0];
989 929
990 # TODO: repeat texture not scale 930 # TODO: repeat texture not scale
994 glColor @{ $self->{bg} }; 934 glColor @{ $self->{bg} };
995 935
996 $bg->{s} = $rep_x; 936 $bg->{s} = $rep_x;
997 $bg->{t} = $rep_y; 937 $bg->{t} = $rep_y;
998 $bg->{wrap_mode} = 1; 938 $bg->{wrap_mode} = 1;
999 $bg->draw_quad ($border, $border, $cw, $ch); 939 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
940 }
1000 941
1001 glDisable GL_TEXTURE_2D; 942 glDisable GL_TEXTURE_2D;
1002 glDisable GL_BLEND;
1003 }
1004 943
1005 $self->{title}->draw if $self->{title}; 944 $self->{title}->draw if $self->{title};
1006 945
1007 $self->child->draw; 946 $self->child->draw;
1008} 947}
1235} 1174}
1236 1175
1237sub size_allocate { 1176sub size_allocate {
1238 my ($self, $w, $h) = @_; 1177 my ($self, $w, $h) = @_;
1239 1178
1179 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1180
1240 my $children = $self->{children}; 1181 my $children = $self->{children};
1241 1182
1242 my @h = map $_->{req_h}, @$children; 1183 my @h = map $_->{req_h}, @$children;
1243 1184
1244 my $req_h = List::Util::sum @h; 1185 my $req_h = List::Util::sum @h;
1283 my ($class, %arg) = @_; 1224 my ($class, %arg) = @_;
1284 1225
1285 my $self = $class->SUPER::new ( 1226 my $self = $class->SUPER::new (
1286 fg => [1, 1, 1], 1227 fg => [1, 1, 1],
1287 #font => default_font 1228 #font => default_font
1229 #text => initial text
1230 #markup => initial narkup
1231 layout => (new CFClient::Layout),
1288 fontsize => 1, 1232 fontsize => 1,
1289 text => "",
1290 align => -1, 1233 align => -1,
1291 valign => -1, 1234 valign => -1,
1292 padding => 2, 1235 padding => 2,
1293 layout => new CFClient::Layout,
1294 can_events => 0, 1236 can_events => 0,
1295 %arg 1237 %arg
1296 ); 1238 );
1297 1239
1298 if (exists $self->{template}) { 1240 if (exists $self->{template}) {
1299 my $layout = new CFClient::Layout; 1241 my $layout = new CFClient::Layout;
1300 $layout->set_text (delete $self->{template}); 1242 $layout->set_text (delete $self->{template});
1301 $self->{template} = $layout; 1243 $self->{template} = $layout;
1302 } 1244 }
1303 1245
1304 $self->set_text (delete $self->{text}) if exists $self->{text}; 1246 if (exists $self->{markup}) {
1305 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1247 $self->set_markup (delete $self->{markup});
1248 } else {
1249 $self->set_text (delete $self->{text});
1250 }
1306 1251
1307 $self 1252 $self
1308} 1253}
1309 1254
1310sub escape { 1255sub escape {
1322 1267
1323 delete $self->{texture}; 1268 delete $self->{texture};
1324 $self->SUPER::update; 1269 $self->SUPER::update;
1325} 1270}
1326 1271
1327sub reconfigure {
1328 my ($self) = @_;
1329
1330 delete $self->{texture};
1331}
1332
1333sub set_text { 1272sub set_text {
1334 my ($self, $text) = @_; 1273 my ($self, $text) = @_;
1335 1274
1336 return if $self->{text} eq "T$text"; 1275 return if $self->{text} eq "T$text";
1337 $self->{text} = "T$text"; 1276 $self->{text} = "T$text";
1338 1277
1278 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1339 $self->{layout}->set_text ($text); 1279 $self->{layout}->set_text ($text);
1340 1280
1341 delete $self->{texture};
1342 $self->update; 1281 $self->update;
1343 $self->check_size; 1282 $self->check_size;
1344} 1283}
1345 1284
1346sub set_markup { 1285sub set_markup {
1347 my ($self, $markup) = @_; 1286 my ($self, $markup) = @_;
1348 1287
1349 return if $self->{text} eq "M$markup"; 1288 return if $self->{text} eq "M$markup";
1350 $self->{text} = "M$markup"; 1289 $self->{text} = "M$markup";
1351 1290
1291 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1292
1293 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1352 $self->{layout}->set_markup ($markup); 1294 $self->{layout}->set_markup ($markup);
1353 1295
1354 delete $self->{texture};
1355 $self->update; 1296 $self->update;
1356 $self->check_size; 1297 $self->check_size;
1357} 1298}
1358 1299
1359sub size_request { 1300sub size_request {
1390sub set_fontsize { 1331sub set_fontsize {
1391 my ($self, $fontsize) = @_; 1332 my ($self, $fontsize) = @_;
1392 1333
1393 $self->{fontsize} = $fontsize; 1334 $self->{fontsize} = $fontsize;
1394 delete $self->{texture}; 1335 delete $self->{texture};
1336
1337 $self->update;
1395 $self->check_size; 1338 $self->check_size;
1396 $self->update;
1397} 1339}
1398 1340
1399sub _draw { 1341sub _draw {
1400 my ($self) = @_; 1342 my ($self) = @_;
1401 1343
1402 my $tex = $self->{texture} ||= do { 1344 my $tex = $self->{texture} ||= do {
1345 $self->{layout}->set_foreground (@{$self->{fg}});
1403 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1346 $self->{layout}->set_font ($self->{font}) if $self->{font};
1404 $self->{layout}->set_width ($self->{w}); 1347 $self->{layout}->set_width ($self->{w});
1405 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1348 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1349
1406 new_from_layout CFClient::Texture $self->{layout} 1350 my $tex = new_from_layout CFClient::Texture $self->{layout};
1351
1352 $self->{ox} = int $self->{align} < 0 ? $self->{padding}
1353 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1354 : ($self->{w} - $tex->{w}) * 0.5;
1355
1356 $self->{oy} = int $self->{valign} < 0 ? $self->{padding}
1357 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1358 : ($self->{h} - $tex->{h}) * 0.5;
1359
1360 $tex
1407 }; 1361 };
1408 1362
1409 glEnable GL_BLEND;
1410 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1411 glEnable GL_TEXTURE_2D; 1363 glEnable GL_TEXTURE_2D;
1412 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1364 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1413 1365
1366 if ($tex->{format} == GL_ALPHA) {
1414 glColor @{$self->{fg}}; 1367 glColor @{$self->{fg}};
1415
1416 $self->{ox} = int (
1417 $self->{align} < 0 ? $self->{padding}
1418 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1419 : ($self->{w} - $tex->{w}) * 0.5
1420 );
1421
1422 $self->{oy} = int (
1423 $self->{valign} < 0 ? $self->{padding}
1424 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1425 : ($self->{h} - $tex->{h}) * 0.5
1426 );
1427
1428 $tex->draw_quad ($self->{ox}, $self->{oy}); 1368 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1369 } else {
1370 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1371 }
1429 1372
1430 glDisable GL_TEXTURE_2D; 1373 glDisable GL_TEXTURE_2D;
1431 glDisable GL_BLEND;
1432} 1374}
1433 1375
1434############################################################################# 1376#############################################################################
1435 1377
1436package CFClient::UI::EntryBase; 1378package CFClient::UI::EntryBase;
1471 $self->{layout}->set_text ("$text "); 1413 $self->{layout}->set_text ("$text ");
1472 1414
1473 $self->emit (changed => $self->{text}); 1415 $self->emit (changed => $self->{text});
1474} 1416}
1475 1417
1418sub set_text {
1419 my ($self, $text) = @_;
1420
1421 $self->{cursor} = length $text;
1422 $self->_set_text ($text);
1423 $self->check_size;
1424 $self->update;
1425}
1426
1476sub get_text { 1427sub get_text {
1477 $_[0]{text} 1428 $_[0]{text}
1478} 1429}
1479 1430
1480sub size_request { 1431sub size_request {
1487 1438
1488sub size_allocate { 1439sub size_allocate {
1489 my ($self, $w, $h) = @_; 1440 my ($self, $w, $h) = @_;
1490 1441
1491 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text 1442 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1492}
1493
1494sub set_text {
1495 my ($self, $text) = @_;
1496
1497 $self->{cursor} = length $text;
1498 $self->_set_text ($text);
1499 $self->update;
1500} 1443}
1501 1444
1502sub key_down { 1445sub key_down {
1503 my ($self, $ev) = @_; 1446 my ($self, $ev) = @_;
1504 1447
1692 1635
1693 if ($GRAB == $self) { 1636 if ($GRAB == $self) {
1694 $self->{fg} = $self->{active_fg}; 1637 $self->{fg} = $self->{active_fg};
1695 } 1638 }
1696 1639
1697 glEnable GL_BLEND;
1698 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1699 glEnable GL_TEXTURE_2D; 1640 glEnable GL_TEXTURE_2D;
1700 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1641 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1701 glColor 0, 0, 0, 1; 1642 glColor 0, 0, 0, 1;
1702 1643
1703 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1644 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1704 1645
1705 glDisable GL_TEXTURE_2D; 1646 glDisable GL_TEXTURE_2D;
1706 glDisable GL_BLEND;
1707 1647
1708 $self->SUPER::_draw; 1648 $self->SUPER::_draw;
1709} 1649}
1710 1650
1711############################################################################# 1651#############################################################################
1758 1698
1759 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1699 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1760 1700
1761 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1701 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1762 1702
1763 glEnable GL_BLEND; 1703 my $tex = $self->{state} ? $tex[1] : $tex[0];
1704
1764 glEnable GL_TEXTURE_2D; 1705 glEnable GL_TEXTURE_2D;
1765 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1766
1767 my $tex = $self->{state} ? $tex[1] : $tex[0];
1768
1769 $tex->draw_quad (0, 0, $s, $s); 1706 $tex->draw_quad_alpha (0, 0, $s, $s);
1770
1771 glDisable GL_TEXTURE_2D; 1707 glDisable GL_TEXTURE_2D;
1772 glDisable GL_BLEND;
1773} 1708}
1774 1709
1775############################################################################# 1710#############################################################################
1776 1711
1777package CFClient::UI::Image; 1712package CFClient::UI::Image;
1820 glTranslate 0, -$self->{w}, 0; 1755 glTranslate 0, -$self->{w}, 0;
1821 1756
1822 ($w, $h) = ($h, $w); 1757 ($w, $h) = ($h, $w);
1823 } 1758 }
1824 1759
1825 glEnable GL_BLEND;
1826 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1827 glEnable GL_TEXTURE_2D; 1760 glEnable GL_TEXTURE_2D;
1828 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1761 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1829 1762
1830 $tex->draw_quad (0, 0, $w, $h); 1763 $tex->draw_quad_alpha (0, 0, $w, $h);
1831 1764
1832 glDisable GL_BLEND;
1833 glDisable GL_TEXTURE_2D; 1765 glDisable GL_TEXTURE_2D;
1834} 1766}
1835 1767
1836############################################################################# 1768#############################################################################
1837 1769
2129 $page ||= 2; 2061 $page ||= 2;
2130 2062
2131 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2063 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2132 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2064 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2133 2065
2134 glEnable GL_BLEND;
2135 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2136 glEnable GL_TEXTURE_2D; 2066 glEnable GL_TEXTURE_2D;
2137 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2067 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2138 2068
2139 # draw background 2069 # draw background
2140 $tex[1]->draw_quad (0, 0, $w, $h); 2070 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2141 2071
2142 # draw handle 2072 # draw handle
2143 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2073 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h);
2144 2074
2145 glDisable GL_BLEND;
2146 glDisable GL_TEXTURE_2D; 2075 glDisable GL_TEXTURE_2D;
2147} 2076}
2148 2077
2149############################################################################# 2078#############################################################################
2150 2079
2161 fontsize => 1, 2090 fontsize => 1,
2162 can_events => 0, 2091 can_events => 0,
2163 #font => default_font 2092 #font => default_font
2164 @_, 2093 @_,
2165 2094
2166 layout => (new CFClient::Layout), 2095 layout => (new CFClient::Layout 1),
2167 par => [], 2096 par => [],
2168 height => 0, 2097 height => 0,
2169 children => [ 2098 children => [
2170 (new CFClient::UI::Empty expand => 1), 2099 (new CFClient::UI::Empty expand => 1),
2171 (new CFClient::UI::Slider vertical => 1), 2100 (new CFClient::UI::Slider vertical => 1),
2189 2118
2190 my $layout = $self->{layout}; 2119 my $layout = $self->{layout};
2191 2120
2192 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2121 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2193 $layout->set_width ($self->{children}[0]{w}); 2122 $layout->set_width ($self->{children}[0]{w});
2194 $layout->set_text ($text); 2123 $layout->set_markup ($text);
2195 2124
2196 ($layout->size)[1] 2125 ($layout->size)[1]
2197} 2126}
2198 2127
2199sub reflow { 2128sub reflow {
2237 2166
2238 return unless $self->{h} > 0; 2167 return unless $self->{h} > 0;
2239 2168
2240 delete $self->{texture}; 2169 delete $self->{texture};
2241 2170
2242 $ROOT->on_refresh ($self, sub { 2171 $ROOT->on_post_alloc ($self, sub {
2243 if (delete $self->{need_reflow}) { 2172 if (delete $self->{need_reflow}) {
2244 my $height = 0; 2173 my $height = 0;
2245 2174
2246 $height += $_->[0] = $self->text_height ($_->[2]) 2175 $height += $_->[0] = $self->text_height ($_->[2])
2247 for @{$self->{par}}; 2176 for @{$self->{par}};
2255 2184
2256 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2185 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2257 glClearColor 0, 0, 0, 0; 2186 glClearColor 0, 0, 0, 0;
2258 glClear GL_COLOR_BUFFER_BIT; 2187 glClear GL_COLOR_BUFFER_BIT;
2259 2188
2260 glEnable GL_BLEND;
2261 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2262 glEnable GL_TEXTURE_2D; 2189 glEnable GL_TEXTURE_2D;
2263 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2190 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2264 2191
2265 my $top = int $self->{children}[1]{range}[0]; 2192 my $top = int $self->{children}[1]{range}[0];
2266 2193
2275 2202
2276 for my $par (@{$self->{par}}) { 2203 for my $par (@{$self->{par}}) {
2277 my $h = $par->[0]; 2204 my $h = $par->[0];
2278 2205
2279 if ($y0 < $y + $h && $y < $y1) { 2206 if ($y0 < $y + $h && $y < $y1) {
2207 $layout->set_foreground (@{ $par->[1] });
2280 $layout->set_text ($par->[2]); 2208 $layout->set_markup ($par->[2]);
2281 2209
2282 glColor @{ $par->[1] };
2283 my ($W, $H) = $layout->size; 2210 my ($W, $H) = $layout->size;
2284 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2211 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0);
2285 } 2212 }
2286 2213
2287 $y += $h; 2214 $y += $h;
2288 } 2215 }
2289 2216
2290 glDisable GL_TEXTURE_2D; 2217 glDisable GL_TEXTURE_2D;
2291 glDisable GL_BLEND;
2292 }; 2218 };
2293 }); 2219 });
2294} 2220}
2295 2221
2296sub _draw { 2222sub _draw {
2297 my ($self) = @_; 2223 my ($self) = @_;
2298 2224
2299 glEnable GL_BLEND;
2300 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2301 glEnable GL_TEXTURE_2D; 2225 glEnable GL_TEXTURE_2D;
2302 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2226 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2303 glColor 1, 1, 1, 1; 2227 glColor 1, 1, 1, 1;
2304 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2228 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2305 glDisable GL_TEXTURE_2D; 2229 glDisable GL_TEXTURE_2D;
2306 glDisable GL_BLEND;
2307 2230
2308 $self->{children}[1]->draw; 2231 $self->{children}[1]->draw;
2309 2232
2310} 2233}
2311 2234
2405 @_, 2328 @_,
2406 can_events => 0, 2329 can_events => 0,
2407 ) 2330 )
2408} 2331}
2409 2332
2410sub set_markup { 2333sub set_tooltip_from {
2411 my ($self, $text) = @_; 2334 my ($self, $widget) = @_;
2412 2335
2413 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2336 $self->add (new CFClient::UI::Label
2414 $self->{label}->set_markup ($text); 2337 markup => $widget->{tooltip},
2415 $self->add ($self->{label}); 2338 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2339 fontsize => 0.8,
2340 fg => [0, 0, 0, 1],
2341 font => ($widget->{tooltip_font} || $::FONT_PROP),
2342 );
2416} 2343}
2417 2344
2418sub size_request { 2345sub size_request {
2419 my ($self) = @_; 2346 my ($self) = @_;
2420
2421 $self->child->set_max_size ($::WIDTH * 0.3);
2422 2347
2423 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2348 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2424 2349
2425 ($w + 4, $h + 4) 2350 ($w + 4, $h + 4)
2426} 2351}
2432} 2357}
2433 2358
2434sub _draw { 2359sub _draw {
2435 my ($self) = @_; 2360 my ($self) = @_;
2436 2361
2437 glPushMatrix;
2438 glTranslate 0.375, 0.375; 2362 glTranslate 0.375, 0.375;
2439 2363
2440 my ($w, $h) = @$self{qw(w h)}; 2364 my ($w, $h) = @$self{qw(w h)};
2441 2365
2442 glColor 1, 0.8, 0.4; 2366 glColor 1, 0.8, 0.4;
2453 glVertex 0 , $h; 2377 glVertex 0 , $h;
2454 glVertex $w, $h; 2378 glVertex $w, $h;
2455 glVertex $w, 0; 2379 glVertex $w, 0;
2456 glEnd; 2380 glEnd;
2457 2381
2458 glPopMatrix; 2382 glTranslate 2 - 0.375, 2 - 0.375;
2459
2460 glTranslate 2, 2;
2461 $self->SUPER::_draw; 2383 $self->SUPER::_draw;
2462} 2384}
2463 2385
2464############################################################################# 2386#############################################################################
2465 2387
2483} 2405}
2484 2406
2485sub _draw { 2407sub _draw {
2486 my ($self) = @_; 2408 my ($self) = @_;
2487 2409
2410 return unless $::CONN;#d# manage and cache textures differently
2488 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2411 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2489 2412
2490 # TODO animation 2413 # TODO animation
2491 if ($tex) { 2414 if ($tex) {
2492 glEnable GL_BLEND;
2493 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2494 glEnable GL_TEXTURE_2D; 2415 glEnable GL_TEXTURE_2D;
2495 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2416 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2496 glColor 1, 1, 1, 1; 2417 glColor 1, 1, 1, 1;
2497 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2418 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2498 glDisable GL_TEXTURE_2D; 2419 glDisable GL_TEXTURE_2D;
2499 glDisable GL_BLEND;
2500 } 2420 }
2501} 2421}
2502 2422
2503############################################################################# 2423#############################################################################
2504 2424
2505package CFClient::UI::Menu; 2425package CFClient::UI::InventoryItem;
2506 2426
2507our @ISA = CFClient::UI::FancyFrame::; 2427our @ISA = CFClient::UI::HBox::;
2508
2509use CFClient::OpenGL;
2510 2428
2511sub new { 2429sub new {
2512 my $class = shift; 2430 my $class = shift;
2513 2431
2514 my $self = $class->SUPER::new (
2515 items => [],
2516 z => 100,
2517 @_,
2518 );
2519
2520 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2521
2522 for my $item (@{ $self->{items} }) {
2523 my ($widget, $cb) = @$item;
2524
2525 # handle various types of items, only text for now
2526 if (!ref $widget) {
2527 $widget = new CFClient::UI::Label
2528 can_hover => 1,
2529 can_events => 1,
2530 text => $widget;
2531 }
2532
2533 $self->{item}{$widget} = $item;
2534
2535 $self->{vbox}->add ($widget);
2536 }
2537
2538 $self
2539}
2540
2541# popup given the event (must be a mouse button down event currently)
2542sub popup {
2543 my ($self, $ev) = @_;
2544
2545 $self->emit ("popdown");
2546
2547 # maybe save $GRAB? must be careful about events...
2548 $GRAB = $self;
2549 $self->{button} = $ev->{button};
2550
2551 $self->show;
2552 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2553}
2554
2555sub mouse_motion {
2556 my ($self, $ev, $x, $y) = @_;
2557
2558 # TODO: should use vbox->find_widget or so
2559 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2560 $self->{hover} = $self->{item}{$HOVER};
2561}
2562
2563sub button_up {
2564 my ($self, $ev, $x, $y) = @_;
2565
2566 if ($ev->{button} == $self->{button}) {
2567 undef $GRAB;
2568 $self->hide;
2569
2570 $self->emit ("popdown");
2571 $self->{hover}[1]->() if $self->{hover};
2572 }
2573}
2574
2575#############################################################################
2576
2577package CFClient::UI::Root;
2578
2579our @ISA = CFClient::UI::Container::;
2580
2581use CFClient::OpenGL;
2582
2583sub check_size {
2584 my ($self) = @_;
2585
2586 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2587}
2588
2589sub size_request {
2590 ($::WIDTH, $::HEIGHT)
2591}
2592
2593sub configure {
2594 my ($self, $x, $y, $w, $h) = @_;
2595
2596 $self->SUPER::configure ($x, $y, $w, $h);
2597
2598 for my $child (@{$self->{children}}) {
2599 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2600
2601 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2602 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2603 $child->configure ($X, $Y, $W,$H);
2604 }
2605}
2606
2607sub _topleft {
2608 my ($self, $x, $y) = @_;
2609
2610 ($x, $y)
2611}
2612
2613sub update {
2614 my ($self) = @_;
2615
2616 $self->check_size;
2617 ::refresh ();
2618}
2619
2620sub add {
2621 my ($self, $child) = @_;
2622
2623 # integerize window positions
2624 $child->{x} = int $child->{x};
2625 $child->{y} = int $child->{y};
2626
2627 $self->SUPER::add ($child);
2628}
2629
2630sub on_refresh {
2631 my ($self, $id, $cb) = @_;
2632
2633 $self->{refresh_hook}{$id} = $cb;
2634}
2635
2636sub draw {
2637 my ($self) = @_;
2638
2639 while (my $rcb = delete $self->{refresh_hook}) {
2640 $_->() for values %$rcb;
2641 }
2642
2643 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2644 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2645 glClear GL_COLOR_BUFFER_BIT;
2646
2647 glMatrixMode GL_PROJECTION;
2648 glLoadIdentity;
2649 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2650 glMatrixMode GL_MODELVIEW;
2651 glLoadIdentity;
2652
2653 $self->_draw;
2654}
2655
2656#############################################################################
2657
2658package CFClient::UI::InventoryItem;
2659
2660our @ISA = CFClient::UI::HBox::;
2661
2662sub new {
2663 my $class = shift;
2664
2665 my %args = @_; 2432 my %args = @_;
2666 2433
2667 my $item = $args{item}; 2434 my $item = delete $args{item};
2668 2435
2669 my $desc = $item->{nrof} < 2 2436 my $desc = $item->{nrof} < 2
2670 ? $item->{name} 2437 ? $item->{name}
2671 : "$item->{nrof} $item->{name_pl}"; 2438 : "$item->{nrof} $item->{name_pl}";
2672 2439
2701 2468
2702 1 2469 1
2703 }, 2470 },
2704 %args 2471 %args
2705 ); 2472 );
2473
2706 $self->add(new CFClient::UI::Face 2474 $self->add (new CFClient::UI::Face
2707 can_events => 0, 2475 can_events => 0,
2708 face => $item->{face}, 2476 face => $item->{face},
2709 anim => $item->{anim}, 2477 anim => $item->{anim},
2710 animspeed => $item->{animspeed}); 2478 animspeed => $item->{animspeed},
2479 );
2480
2711 $self->add(new CFClient::UI::Label 2481 $self->add (new CFClient::UI::Label
2712 can_events => 0, 2482 can_events => 0,
2713 text => $desc); 2483 text => $desc,
2484 );
2714 2485
2715 $self 2486 $self
2716} 2487}
2717 2488
2718############################################################################# 2489#############################################################################
2745 for my $item (@items) { 2516 for my $item (@items) {
2746 my $desc = $item->{nrof} < 2 2517 my $desc = $item->{nrof} < 2
2747 ? $item->{name} 2518 ? $item->{name}
2748 : "$item->{nrof} $item->{name_pl}"; 2519 : "$item->{nrof} $item->{name_pl}";
2749 2520
2750 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); 2521 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item);
2751 } 2522 }
2752 2523
2753# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2524# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2754} 2525}
2755 2526
2756sub size_request { 2527sub size_request {
2757 my ($self) = @_; 2528 my ($self) = @_;
2758 ($self->{req_w}, $self->{req_h}); 2529 ($self->{req_w}, $self->{req_h});
2530}
2531
2532#############################################################################
2533
2534package CFClient::UI::Menu;
2535
2536our @ISA = CFClient::UI::FancyFrame::;
2537
2538use CFClient::OpenGL;
2539
2540sub new {
2541 my $class = shift;
2542
2543 my $self = $class->SUPER::new (
2544 items => [],
2545 z => 100,
2546 @_,
2547 );
2548
2549 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2550
2551 for my $item (@{ $self->{items} }) {
2552 my ($widget, $cb) = @$item;
2553
2554 # handle various types of items, only text for now
2555 if (!ref $widget) {
2556 $widget = new CFClient::UI::Label
2557 can_hover => 1,
2558 can_events => 1,
2559 text => $widget;
2560 }
2561
2562 $self->{item}{$widget} = $item;
2563
2564 $self->{vbox}->add ($widget);
2565 }
2566
2567 $self
2568}
2569
2570# popup given the event (must be a mouse button down event currently)
2571sub popup {
2572 my ($self, $ev) = @_;
2573
2574 $self->emit ("popdown");
2575
2576 # maybe save $GRAB? must be careful about events...
2577 $GRAB = $self;
2578 $self->{button} = $ev->{button};
2579
2580 $self->show;
2581 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2582}
2583
2584sub mouse_motion {
2585 my ($self, $ev, $x, $y) = @_;
2586
2587 # TODO: should use vbox->find_widget or so
2588 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2589 $self->{hover} = $self->{item}{$HOVER};
2590}
2591
2592sub button_up {
2593 my ($self, $ev, $x, $y) = @_;
2594
2595 if ($ev->{button} == $self->{button}) {
2596 undef $GRAB;
2597 $self->hide;
2598
2599 $self->emit ("popdown");
2600 $self->{hover}[1]->() if $self->{hover};
2601 }
2602}
2603
2604#############################################################################
2605
2606package CFClient::UI::Statusbox;
2607
2608our @ISA = CFClient::UI::VBox::;
2609
2610sub reorder {
2611 my ($self) = @_;
2612 my $NOW = time;
2613
2614 while (my ($k, $v) = each %{ $self->{item} }) {
2615 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2616 }
2617
2618 my @widgets;
2619
2620 my @items = sort {
2621 $a->{pri} <=> $b->{pri}
2622 or $b->{id} <=> $a->{id}
2623 } values %{ $self->{item} };
2624
2625 my $count = 10 + 1;
2626 for my $item (@items) {
2627 last unless --$count;
2628
2629 push @widgets, $item->{label} ||= do {
2630 # TODO: doesn't handle markup well (read as: at all)
2631 my $short = $item->{count} > 1
2632 ? "<b>$item->{count} ×</b> $item->{text}"
2633 : $item->{text};
2634
2635 for ($short) {
2636 s/^\s+//;
2637 s/\012.*/…/s;
2638 my $len = int 40 / $item->{fontsize};
2639 substr $_, $len, length, "…" if $len < length;
2640 }
2641
2642 new CFClient::UI::Label
2643 markup => $short,
2644 tooltip => $item->{tooltip},
2645 tooltip_font => $::FONT_PROP,
2646 tooltip_width => 0.67,
2647 fontsize => $item->{fontsize},
2648 color => $item->{color},
2649 can_events => 1,
2650 can_hover => 1
2651 };
2652 }
2653
2654 $self->clear;
2655 $self->SUPER::add (reverse @widgets);
2656}
2657
2658sub add {
2659 my ($self, $text, %arg) = @_;
2660
2661 $text =~ s/^\s+//;
2662 $text =~ s/\s+$//;
2663
2664 my $timeout = time + ((delete $arg{timeout}) || 60);
2665
2666 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2667
2668 if (my $item = $self->{item}{$group}) {
2669 if ($item->{text} eq $text) {
2670 $item->{count}++;
2671 } else {
2672 $item->{count} = 1;
2673 $item->{text} = $item->{tooltip} = $text;
2674 }
2675 $item->{id} = ++$self->{id};
2676 $item->{timeout} = $timeout;
2677 delete $item->{label};
2678 } else {
2679 $self->{item}{$group} = {
2680 id => ++$self->{id},
2681 text => $text,
2682 timeout => $timeout,
2683 tooltip => $text,
2684 fontsize => 0.8,
2685 color => [0.8, 0.8, 0.8, 0.8],
2686 pri => 0,
2687 count => 1,
2688 %arg,
2689 };
2690 }
2691
2692 $self->reorder;
2693}
2694
2695#############################################################################
2696
2697package CFClient::UI::Root;
2698
2699our @ISA = CFClient::UI::Container::;
2700
2701use CFClient::OpenGL;
2702
2703sub new {
2704 my $class = shift;
2705
2706 $class->SUPER::new (
2707 @_,
2708 )
2709}
2710
2711sub configure {
2712 my ($self, $x, $y, $w, $h) = @_;
2713
2714 $self->{w} = $w;
2715 $self->{h} = $h;
2716}
2717
2718sub check_size {
2719 my ($self) = @_;
2720
2721 $self->size_allocate ($self->{w}, $self->{h})
2722 if $self->{w};
2723}
2724
2725sub size_request {
2726 my ($self) = @_;
2727
2728 ($self->{w}, $self->{h})
2729}
2730
2731sub size_allocate {
2732 my ($self, $w, $h) = @_;
2733
2734 my $old_w = $self->{old_w};
2735 my $old_h = $self->{old_h};
2736
2737 if ($old_w && $old_h) {
2738 for my $child ($self->children) {
2739 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2740 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2741 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2742 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2743 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2744 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2745 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2746 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2747 }
2748 }
2749
2750 for my $child ($self->children) {
2751 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2752
2753 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2754 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2755 $child->configure ($X, $Y, $W, $H);
2756 }
2757
2758 $self->{old_w} = $w;
2759 $self->{old_h} = $h;
2760}
2761
2762sub coord2local {
2763 my ($self, $x, $y) = @_;
2764
2765 ($x, $y)
2766}
2767
2768sub coord2global {
2769 my ($self, $x, $y) = @_;
2770
2771 ($x, $y)
2772}
2773
2774sub update {
2775 my ($self) = @_;
2776
2777 $self->check_size;
2778 $::WANT_REFRESH++;
2779}
2780
2781sub add {
2782 my ($self, $child) = @_;
2783
2784 # integerise window positions
2785 $child->{x} = int $child->{x};
2786 $child->{y} = int $child->{y};
2787
2788 $self->SUPER::add ($child);
2789}
2790
2791sub on_refresh {
2792 my ($self, $id, $cb) = @_;
2793
2794 $self->{refresh_hook}{$id} = $cb;
2795}
2796
2797sub on_post_alloc {
2798 my ($self, $id, $cb) = @_;
2799
2800 $self->{post_alloc_hook}{$id} = $cb;
2801}
2802
2803sub draw {
2804 my ($self) = @_;
2805
2806 while ($self->{refresh_hook}) {
2807 $_->()
2808 for values %{delete $self->{refresh_hook}};
2809 }
2810
2811 if ($self->{check_size}) {
2812 my @queue = ([], []);
2813
2814 for (;;) {
2815 if ($self->{check_size}) {
2816 # heuristic: check containers last
2817 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_
2818 for values %{delete $self->{check_size}}
2819 }
2820
2821 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last;
2822
2823 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2824 ? @$widget{qw(user_w user_h)}
2825 : $widget->size_request;
2826
2827 if (delete $widget->{force_alloc}
2828 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2829 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2830
2831 $widget->{req_w} = $w;
2832 $widget->{req_h} = $h;
2833
2834 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2835
2836 $widget->{parent}->check_size
2837 if $widget->{parent};
2838 }
2839 }
2840 }
2841
2842 while ($self->{size_alloc}) {
2843 for (values %{delete $self->{size_alloc}}) {
2844 my ($widget, $w, $h) = @$_;
2845
2846 $w = 0 if $w < 0;
2847 $h = 0 if $h < 0;
2848
2849 $widget->{w} = $w;
2850 $widget->{h} = $h;
2851 $widget->size_allocate ($w, $h);
2852 $widget->emit (size_allocate => $w, $h);
2853 }
2854 }
2855
2856 while ($self->{post_alloc_hook}) {
2857 $_->()
2858 for values %{delete $self->{post_alloc_hook}};
2859 }
2860
2861 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2862 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2863 glClear GL_COLOR_BUFFER_BIT;
2864
2865 glMatrixMode GL_PROJECTION;
2866 glLoadIdentity;
2867 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2868 glMatrixMode GL_MODELVIEW;
2869 glLoadIdentity;
2870
2871 $self->_draw;
2759} 2872}
2760 2873
2761############################################################################# 2874#############################################################################
2762 2875
2763package CFClient::UI; 2876package CFClient::UI;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines