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.197 by root, Fri May 12 01:47:04 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
256 253
257 $_->reconfigure 254 $_->reconfigure
258 for $self->children; 255 for $self->children;
259 256
260 $self->check_size; 257 $self->check_size;
261 $self->size_allocate ($self->{w}, $self->{h}); 258 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $self->{w}, $self->{h}];
262 $self->update; 259 $self->update;
263} 260}
264 261
265sub set_max_size { 262sub set_max_size {
266 my ($self, $w, $h) = @_; 263 my ($self, $w, $h) = @_;
267 264
268 delete $self->{max_w}; $self->{max_w} = $w if $w; 265 delete $self->{max_w}; $self->{max_w} = $w if $w;
269 delete $self->{max_h}; $self->{max_h} = $h if $h; 266 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} 267}
281 268
282# translate global coordinates to local coordinate system 269# translate global coordinates to local coordinate system
283sub coord2local { 270sub coord2local {
284 my ($self, $x, $y) = @_; 271 my ($self, $x, $y) = @_;
285 272
286 my ($X, $Y) = $self->_topleft; 273 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
287 ($x - $X, $y - $Y)
288} 274}
289 275
290# translate local coordinates to global coordinate system 276# translate local coordinates to global coordinate system
291sub coord2global { 277sub coord2global {
292 my ($self, $x, $y) = @_; 278 my ($self, $x, $y) = @_;
293 279
294 my ($X, $Y) = $self->_topleft; 280 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
295 ($x + $X, $y + $Y)
296} 281}
297 282
298sub focus_in { 283sub focus_in {
299 my ($self) = @_; 284 my ($self) = @_;
300 285
398 383
399sub set_parent { 384sub set_parent {
400 my ($self, $parent) = @_; 385 my ($self, $parent) = @_;
401 386
402 Scalar::Util::weaken ($self->{parent} = $parent); 387 Scalar::Util::weaken ($self->{parent} = $parent);
388
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} }
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
846 787
847our @ISA = CFClient::UI::Bin::; 788our @ISA = CFClient::UI::Bin::;
848 789
849use CFClient::OpenGL; 790use CFClient::OpenGL;
850 791
851sub new {
852 my $class = shift;
853
854 my $self = $class->SUPER::new (
855 bg => [1, 1, 1, 1],
856 border_bg => [1, 1, 1, 1],
857 border => 0.8,
858 @_
859 );
860
861 $self
862}
863
864sub _draw {
865 my ($self) = @_;
866
867 my ($w, $h) = ($self->{w}, $self->{h});
868
869 glEnable GL_BLEND;
870 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
871 glEnable GL_TEXTURE_2D;
872 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
873
874# glBegin GL_QUADS;
875# glColor 0, 0, 0, 0;
876# glVertex 0 , 0;
877# glVertex 0 , $h;
878# glVertex $w, $h;
879# glVertex $w, 0;
880# glEnd;
881
882
883 $self->child->draw;
884 glDisable GL_BLEND;
885 glDisable GL_TEXTURE_2D;
886}
887
888############################################################################# 792#############################################################################
889 793
890package CFClient::UI::FancyFrame; 794package CFClient::UI::FancyFrame;
891 795
892our @ISA = CFClient::UI::Bin::; 796our @ISA = CFClient::UI::Bin::;
1006 my ($self) = @_; 910 my ($self) = @_;
1007 911
1008 my ($w, $h ) = ($self->{w}, $self->{h}); 912 my ($w, $h ) = ($self->{w}, $self->{h});
1009 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 913 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
1010 914
1011 glEnable GL_BLEND;
1012 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1013 glEnable GL_TEXTURE_2D; 915 glEnable GL_TEXTURE_2D;
1014 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 916 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1015 917
1016 my $border = $self->border; 918 my $border = $self->border;
1017 919
1018 glColor @{ $self->{border_bg} }; 920 glColor @{ $self->{border_bg} };
1019 $tex[1]->draw_quad (0, 0, $w, $border); 921 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
1020 $tex[3]->draw_quad (0, $border, $border, $ch); 922 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
1021 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 923 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1022 $tex[4]->draw_quad (0, $h - $border, $w, $border); 924 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
1023 925
1024 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 926 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1025 my $bg = $tex[0]; 927 my $bg = $tex[0];
1026 928
1027 # TODO: repeat texture not scale 929 # TODO: repeat texture not scale
1031 glColor @{ $self->{bg} }; 933 glColor @{ $self->{bg} };
1032 934
1033 $bg->{s} = $rep_x; 935 $bg->{s} = $rep_x;
1034 $bg->{t} = $rep_y; 936 $bg->{t} = $rep_y;
1035 $bg->{wrap_mode} = 1; 937 $bg->{wrap_mode} = 1;
1036 $bg->draw_quad ($border, $border, $cw, $ch); 938 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
939 }
1037 940
1038 glDisable GL_TEXTURE_2D; 941 glDisable GL_TEXTURE_2D;
1039 glDisable GL_BLEND;
1040 }
1041 942
1042 $self->{title}->draw if $self->{title}; 943 $self->{title}->draw if $self->{title};
1043 944
1044 $self->child->draw; 945 $self->child->draw;
1045} 946}
1272} 1173}
1273 1174
1274sub size_allocate { 1175sub size_allocate {
1275 my ($self, $w, $h) = @_; 1176 my ($self, $w, $h) = @_;
1276 1177
1178 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1179
1277 my $children = $self->{children}; 1180 my $children = $self->{children};
1278 1181
1279 my @h = map $_->{req_h}, @$children; 1182 my @h = map $_->{req_h}, @$children;
1280 1183
1281 my $req_h = List::Util::sum @h; 1184 my $req_h = List::Util::sum @h;
1320 my ($class, %arg) = @_; 1223 my ($class, %arg) = @_;
1321 1224
1322 my $self = $class->SUPER::new ( 1225 my $self = $class->SUPER::new (
1323 fg => [1, 1, 1], 1226 fg => [1, 1, 1],
1324 #font => default_font 1227 #font => default_font
1228 #text => initial text
1229 #markup => initial narkup
1230 layout => (new CFClient::Layout),
1325 fontsize => 1, 1231 fontsize => 1,
1326 text => "",
1327 align => -1, 1232 align => -1,
1328 valign => -1, 1233 valign => -1,
1329 padding => 2, 1234 padding => 2,
1330 layout => new CFClient::Layout,
1331 can_events => 0, 1235 can_events => 0,
1332 %arg 1236 %arg
1333 ); 1237 );
1334 1238
1335 if (exists $self->{template}) { 1239 if (exists $self->{template}) {
1336 my $layout = new CFClient::Layout; 1240 my $layout = new CFClient::Layout;
1337 $layout->set_text (delete $self->{template}); 1241 $layout->set_text (delete $self->{template});
1338 $self->{template} = $layout; 1242 $self->{template} = $layout;
1339 } 1243 }
1340 1244
1341 $self->set_text (delete $self->{text}) if exists $self->{text}; 1245 if (exists $self->{markup}) {
1342 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1246 $self->set_markup (delete $self->{markup});
1247 } else {
1248 $self->set_text (delete $self->{text});
1249 }
1343 1250
1344 $self 1251 $self
1345} 1252}
1346 1253
1347sub escape { 1254sub escape {
1365 my ($self, $text) = @_; 1272 my ($self, $text) = @_;
1366 1273
1367 return if $self->{text} eq "T$text"; 1274 return if $self->{text} eq "T$text";
1368 $self->{text} = "T$text"; 1275 $self->{text} = "T$text";
1369 1276
1277 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1370 $self->{layout}->set_text ($text); 1278 $self->{layout}->set_text ($text);
1371 1279
1372 delete $self->{texture};
1373 $self->update; 1280 $self->update;
1374 $self->check_size; 1281 $self->check_size;
1375} 1282}
1376 1283
1377sub set_markup { 1284sub set_markup {
1378 my ($self, $markup) = @_; 1285 my ($self, $markup) = @_;
1379 1286
1380 return if $self->{text} eq "M$markup"; 1287 return if $self->{text} eq "M$markup";
1381 $self->{text} = "M$markup"; 1288 $self->{text} = "M$markup";
1382 1289
1290 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1291
1292 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1383 $self->{layout}->set_markup ($markup); 1293 $self->{layout}->set_markup ($markup);
1384 1294
1385 delete $self->{texture};
1386 $self->update; 1295 $self->update;
1387 $self->check_size; 1296 $self->check_size;
1388} 1297}
1389 1298
1390sub size_request { 1299sub size_request {
1430 1339
1431sub _draw { 1340sub _draw {
1432 my ($self) = @_; 1341 my ($self) = @_;
1433 1342
1434 my $tex = $self->{texture} ||= do { 1343 my $tex = $self->{texture} ||= do {
1344 $self->{layout}->set_foreground (@{$self->{fg}});
1435 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1345 $self->{layout}->set_font ($self->{font}) if $self->{font};
1436 $self->{layout}->set_width ($self->{w}); 1346 $self->{layout}->set_width ($self->{w});
1437 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1347 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1348
1438 new_from_layout CFClient::Texture $self->{layout} 1349 my $tex = new_from_layout CFClient::Texture $self->{layout};
1350
1351 $self->{ox} = int $self->{align} < 0 ? $self->{padding}
1352 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1353 : ($self->{w} - $tex->{w}) * 0.5;
1354
1355 $self->{oy} = int $self->{valign} < 0 ? $self->{padding}
1356 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1357 : ($self->{h} - $tex->{h}) * 0.5;
1358
1359 $tex
1439 }; 1360 };
1440 1361
1441 glEnable GL_BLEND;
1442 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1443 glEnable GL_TEXTURE_2D; 1362 glEnable GL_TEXTURE_2D;
1444 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1363 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1445 1364
1365 if ($tex->{format} == GL_ALPHA) {
1446 glColor @{$self->{fg}}; 1366 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}); 1367 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1368 } else {
1369 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1370 }
1461 1371
1462 glDisable GL_TEXTURE_2D; 1372 glDisable GL_TEXTURE_2D;
1463 glDisable GL_BLEND;
1464} 1373}
1465 1374
1466############################################################################# 1375#############################################################################
1467 1376
1468package CFClient::UI::EntryBase; 1377package CFClient::UI::EntryBase;
1503 $self->{layout}->set_text ("$text "); 1412 $self->{layout}->set_text ("$text ");
1504 1413
1505 $self->emit (changed => $self->{text}); 1414 $self->emit (changed => $self->{text});
1506} 1415}
1507 1416
1417sub set_text {
1418 my ($self, $text) = @_;
1419
1420 $self->{cursor} = length $text;
1421 $self->_set_text ($text);
1422 $self->check_size;
1423 $self->update;
1424}
1425
1508sub get_text { 1426sub get_text {
1509 $_[0]{text} 1427 $_[0]{text}
1510} 1428}
1511 1429
1512sub size_request { 1430sub size_request {
1519 1437
1520sub size_allocate { 1438sub size_allocate {
1521 my ($self, $w, $h) = @_; 1439 my ($self, $w, $h) = @_;
1522 1440
1523 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text 1441 $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} 1442}
1533 1443
1534sub key_down { 1444sub key_down {
1535 my ($self, $ev) = @_; 1445 my ($self, $ev) = @_;
1536 1446
1724 1634
1725 if ($GRAB == $self) { 1635 if ($GRAB == $self) {
1726 $self->{fg} = $self->{active_fg}; 1636 $self->{fg} = $self->{active_fg};
1727 } 1637 }
1728 1638
1729 glEnable GL_BLEND;
1730 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1731 glEnable GL_TEXTURE_2D; 1639 glEnable GL_TEXTURE_2D;
1732 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1640 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1733 glColor 0, 0, 0, 1; 1641 glColor 0, 0, 0, 1;
1734 1642
1735 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1643 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1736 1644
1737 glDisable GL_TEXTURE_2D; 1645 glDisable GL_TEXTURE_2D;
1738 glDisable GL_BLEND;
1739 1646
1740 $self->SUPER::_draw; 1647 $self->SUPER::_draw;
1741} 1648}
1742 1649
1743############################################################################# 1650#############################################################################
1790 1697
1791 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1698 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1792 1699
1793 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1700 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1794 1701
1795 glEnable GL_BLEND; 1702 my $tex = $self->{state} ? $tex[1] : $tex[0];
1703
1796 glEnable GL_TEXTURE_2D; 1704 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); 1705 $tex->draw_quad_alpha (0, 0, $s, $s);
1802
1803 glDisable GL_TEXTURE_2D; 1706 glDisable GL_TEXTURE_2D;
1804 glDisable GL_BLEND;
1805} 1707}
1806 1708
1807############################################################################# 1709#############################################################################
1808 1710
1809package CFClient::UI::Image; 1711package CFClient::UI::Image;
1852 glTranslate 0, -$self->{w}, 0; 1754 glTranslate 0, -$self->{w}, 0;
1853 1755
1854 ($w, $h) = ($h, $w); 1756 ($w, $h) = ($h, $w);
1855 } 1757 }
1856 1758
1857 glEnable GL_BLEND;
1858 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1859 glEnable GL_TEXTURE_2D; 1759 glEnable GL_TEXTURE_2D;
1860 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1760 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1861 1761
1862 $tex->draw_quad (0, 0, $w, $h); 1762 $tex->draw_quad_alpha (0, 0, $w, $h);
1863 1763
1864 glDisable GL_BLEND;
1865 glDisable GL_TEXTURE_2D; 1764 glDisable GL_TEXTURE_2D;
1866} 1765}
1867 1766
1868############################################################################# 1767#############################################################################
1869 1768
2161 $page ||= 2; 2060 $page ||= 2;
2162 2061
2163 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2062 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2164 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2063 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2165 2064
2166 glEnable GL_BLEND;
2167 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2168 glEnable GL_TEXTURE_2D; 2065 glEnable GL_TEXTURE_2D;
2169 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2066 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2170 2067
2171 # draw background 2068 # draw background
2172 $tex[1]->draw_quad (0, 0, $w, $h); 2069 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2173 2070
2174 # draw handle 2071 # draw handle
2175 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2072 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h);
2176 2073
2177 glDisable GL_BLEND;
2178 glDisable GL_TEXTURE_2D; 2074 glDisable GL_TEXTURE_2D;
2179} 2075}
2180 2076
2181############################################################################# 2077#############################################################################
2182 2078
2193 fontsize => 1, 2089 fontsize => 1,
2194 can_events => 0, 2090 can_events => 0,
2195 #font => default_font 2091 #font => default_font
2196 @_, 2092 @_,
2197 2093
2198 layout => (new CFClient::Layout), 2094 layout => (new CFClient::Layout 1),
2199 par => [], 2095 par => [],
2200 height => 0, 2096 height => 0,
2201 children => [ 2097 children => [
2202 (new CFClient::UI::Empty expand => 1), 2098 (new CFClient::UI::Empty expand => 1),
2203 (new CFClient::UI::Slider vertical => 1), 2099 (new CFClient::UI::Slider vertical => 1),
2221 2117
2222 my $layout = $self->{layout}; 2118 my $layout = $self->{layout};
2223 2119
2224 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2120 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2225 $layout->set_width ($self->{children}[0]{w}); 2121 $layout->set_width ($self->{children}[0]{w});
2226 $layout->set_text ($text); 2122 $layout->set_markup ($text);
2227 2123
2228 ($layout->size)[1] 2124 ($layout->size)[1]
2229} 2125}
2230 2126
2231sub reflow { 2127sub reflow {
2287 2183
2288 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2184 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2289 glClearColor 0, 0, 0, 0; 2185 glClearColor 0, 0, 0, 0;
2290 glClear GL_COLOR_BUFFER_BIT; 2186 glClear GL_COLOR_BUFFER_BIT;
2291 2187
2292 glEnable GL_BLEND;
2293 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2294 glEnable GL_TEXTURE_2D; 2188 glEnable GL_TEXTURE_2D;
2295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2189 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2296 2190
2297 my $top = int $self->{children}[1]{range}[0]; 2191 my $top = int $self->{children}[1]{range}[0];
2298 2192
2307 2201
2308 for my $par (@{$self->{par}}) { 2202 for my $par (@{$self->{par}}) {
2309 my $h = $par->[0]; 2203 my $h = $par->[0];
2310 2204
2311 if ($y0 < $y + $h && $y < $y1) { 2205 if ($y0 < $y + $h && $y < $y1) {
2206 $layout->set_foreground (@{ $par->[1] });
2312 $layout->set_text ($par->[2]); 2207 $layout->set_markup ($par->[2]);
2313 2208
2314 glColor @{ $par->[1] };
2315 my ($W, $H) = $layout->size; 2209 my ($W, $H) = $layout->size;
2316 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2210 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0);
2317 } 2211 }
2318 2212
2319 $y += $h; 2213 $y += $h;
2320 } 2214 }
2321 2215
2322 glDisable GL_TEXTURE_2D; 2216 glDisable GL_TEXTURE_2D;
2323 glDisable GL_BLEND;
2324 }; 2217 };
2325 }); 2218 });
2326} 2219}
2327 2220
2328sub _draw { 2221sub _draw {
2329 my ($self) = @_; 2222 my ($self) = @_;
2330 2223
2331 glEnable GL_BLEND;
2332 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2333 glEnable GL_TEXTURE_2D; 2224 glEnable GL_TEXTURE_2D;
2334 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2225 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2335 glColor 1, 1, 1, 1; 2226 glColor 1, 1, 1, 1;
2336 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2227 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2337 glDisable GL_TEXTURE_2D; 2228 glDisable GL_TEXTURE_2D;
2338 glDisable GL_BLEND;
2339 2229
2340 $self->{children}[1]->draw; 2230 $self->{children}[1]->draw;
2341 2231
2342} 2232}
2343 2233
2437 @_, 2327 @_,
2438 can_events => 0, 2328 can_events => 0,
2439 ) 2329 )
2440} 2330}
2441 2331
2442sub set_markup { 2332sub set_tooltip_from {
2443 my ($self, $text) = @_; 2333 my ($self, $widget) = @_;
2444 2334
2445 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2335 $self->add (new CFClient::UI::Label
2446 $self->{label}->set_markup ($text); 2336 markup => $widget->{tooltip},
2447 $self->add ($self->{label}); 2337 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2338 fontsize => 0.8,
2339 fg => [0, 0, 0, 1],
2340 font => ($widget->{tooltip_font} || $::FONT_PROP),
2341 );
2448} 2342}
2449 2343
2450sub size_request { 2344sub size_request {
2451 my ($self) = @_; 2345 my ($self) = @_;
2452
2453 $self->child->set_max_size ($::WIDTH * 0.3);
2454 2346
2455 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2347 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2456 2348
2457 ($w + 4, $h + 4) 2349 ($w + 4, $h + 4)
2458} 2350}
2464} 2356}
2465 2357
2466sub _draw { 2358sub _draw {
2467 my ($self) = @_; 2359 my ($self) = @_;
2468 2360
2469 glPushMatrix;
2470 glTranslate 0.375, 0.375; 2361 glTranslate 0.375, 0.375;
2471 2362
2472 my ($w, $h) = @$self{qw(w h)}; 2363 my ($w, $h) = @$self{qw(w h)};
2473 2364
2474 glColor 1, 0.8, 0.4; 2365 glColor 1, 0.8, 0.4;
2485 glVertex 0 , $h; 2376 glVertex 0 , $h;
2486 glVertex $w, $h; 2377 glVertex $w, $h;
2487 glVertex $w, 0; 2378 glVertex $w, 0;
2488 glEnd; 2379 glEnd;
2489 2380
2490 glPopMatrix; 2381 glTranslate 2 - 0.375, 2 - 0.375;
2491
2492 glTranslate 2, 2;
2493 $self->SUPER::_draw; 2382 $self->SUPER::_draw;
2494} 2383}
2495 2384
2496############################################################################# 2385#############################################################################
2497 2386
2520 return unless $::CONN;#d# manage and cache textures differently 2409 return unless $::CONN;#d# manage and cache textures differently
2521 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2410 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2522 2411
2523 # TODO animation 2412 # TODO animation
2524 if ($tex) { 2413 if ($tex) {
2525 glEnable GL_BLEND;
2526 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2527 glEnable GL_TEXTURE_2D; 2414 glEnable GL_TEXTURE_2D;
2528 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2415 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2529 glColor 1, 1, 1, 1; 2416 glColor 1, 1, 1, 1;
2530 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2417 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2531 glDisable GL_TEXTURE_2D; 2418 glDisable GL_TEXTURE_2D;
2532 glDisable GL_BLEND;
2533 } 2419 }
2534} 2420}
2535 2421
2536############################################################################# 2422#############################################################################
2537 2423
2542sub new { 2428sub new {
2543 my $class = shift; 2429 my $class = shift;
2544 2430
2545 my %args = @_; 2431 my %args = @_;
2546 2432
2547 my $item = $args{item}; 2433 my $item = delete $args{item};
2548 2434
2549 my $desc = $item->{nrof} < 2 2435 my $desc = $item->{nrof} < 2
2550 ? $item->{name} 2436 ? $item->{name}
2551 : "$item->{nrof} $item->{name_pl}"; 2437 : "$item->{nrof} $item->{name_pl}";
2552 2438
2581 2467
2582 1 2468 1
2583 }, 2469 },
2584 %args 2470 %args
2585 ); 2471 );
2472
2586 $self->add(new CFClient::UI::Face 2473 $self->add (new CFClient::UI::Face
2587 can_events => 0, 2474 can_events => 0,
2588 face => $item->{face}, 2475 face => $item->{face},
2589 anim => $item->{anim}, 2476 anim => $item->{anim},
2590 animspeed => $item->{animspeed}); 2477 animspeed => $item->{animspeed},
2478 );
2479
2591 $self->add(new CFClient::UI::Label 2480 $self->add (new CFClient::UI::Label
2592 can_events => 0, 2481 can_events => 0,
2593 text => $desc); 2482 text => $desc,
2483 );
2594 2484
2595 $self 2485 $self
2596} 2486}
2597 2487
2598############################################################################# 2488#############################################################################
2625 for my $item (@items) { 2515 for my $item (@items) {
2626 my $desc = $item->{nrof} < 2 2516 my $desc = $item->{nrof} < 2
2627 ? $item->{name} 2517 ? $item->{name}
2628 : "$item->{nrof} $item->{name_pl}"; 2518 : "$item->{nrof} $item->{name_pl}";
2629 2519
2630 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); 2520 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item);
2631 } 2521 }
2632 2522
2633# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2523# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2634} 2524}
2635 2525
2710 } 2600 }
2711} 2601}
2712 2602
2713############################################################################# 2603#############################################################################
2714 2604
2605package CFClient::UI::Statusbox;
2606
2607our @ISA = CFClient::UI::VBox::;
2608
2609sub reorder {
2610 my ($self) = @_;
2611 my $NOW = time;
2612
2613 while (my ($k, $v) = each %{ $self->{item} }) {
2614 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2615 }
2616
2617 my @widgets;
2618
2619 my @items = sort {
2620 $a->{pri} <=> $b->{pri}
2621 or $b->{id} <=> $a->{id}
2622 } values %{ $self->{item} };
2623
2624 my $count = 10 + 1;
2625 for my $item (@items) {
2626 last unless --$count;
2627
2628 push @widgets, $item->{label} ||= do {
2629 # TODO: doesn't handle markup well (read as: at all)
2630 my $short = $item->{count} > 1
2631 ? "<b>$item->{count} ×</b> $item->{text}"
2632 : $item->{text};
2633
2634 for ($short) {
2635 s/^\s+//;
2636 s/\012.*/…/s;
2637 my $len = int 30 / $item->{fontsize};
2638 substr $_, $len, length, "…" if $len < length;
2639 }
2640
2641 new CFClient::UI::Label
2642 markup => $short,
2643 tooltip => $item->{tooltip},
2644 tooltip_font => $::FONT_PROP,
2645 tooltip_width => 0.67,
2646 fontsize => $item->{fontsize},
2647 color => $item->{color},
2648 can_events => 1,
2649 can_hover => 1
2650 };
2651 }
2652
2653 $self->clear;
2654 $self->SUPER::add (reverse @widgets);
2655}
2656
2657sub add {
2658 my ($self, $text, %arg) = @_;
2659
2660 my $timeout = time + ((delete $arg{timeout}) || 60);
2661
2662 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2663
2664 if (my $item = $self->{item}{$group}) {
2665 if ($item->{text} eq $text) {
2666 $item->{count}++;
2667 } else {
2668 $item->{count} = 1;
2669 $item->{text} = $item->{tooltip} = $text;
2670 }
2671 $item->{timeout} = $timeout;
2672 delete $item->{label};
2673 } else {
2674 $self->{item}{$group} = {
2675 id => ++$self->{id},
2676 text => $text,
2677 timeout => $timeout,
2678 tooltip => $text,
2679 fontsize => 0.8,
2680 color => [0.8, 0.8, 0.8, 0.8],
2681 pri => 0,
2682 count => 1,
2683 %arg,
2684 };
2685 }
2686
2687 $self->reorder;
2688}
2689
2690#############################################################################
2691
2715package CFClient::UI::Root; 2692package CFClient::UI::Root;
2716 2693
2717our @ISA = CFClient::UI::Container::; 2694our @ISA = CFClient::UI::Container::;
2718 2695
2719use CFClient::OpenGL; 2696use CFClient::OpenGL;
2720 2697
2698sub new {
2699 my $class = shift;
2700
2701 $class->SUPER::new (
2702 @_,
2703 )
2704}
2705
2706sub configure {
2707 my ($self, $x, $y, $w, $h) = @_;
2708
2709 $self->{w} = $w;
2710 $self->{h} = $h;
2711}
2712
2721sub check_size { 2713sub check_size {
2722 my ($self) = @_; 2714 my ($self) = @_;
2723 2715
2724 $self->configure (0, 0, $self->{w}, $self->{h}); 2716 $self->size_allocate ($self->{w}, $self->{h})
2717 if $self->{w};
2725} 2718}
2726 2719
2727sub size_request { 2720sub size_request {
2728 my ($self) = @_; 2721 my ($self) = @_;
2729 2722
2747 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; 2740 $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}; 2741 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2749 } 2742 }
2750 } 2743 }
2751 2744
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) { 2745 for my $child ($self->children) {
2762 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2746 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2763 2747
2764 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2748 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2765 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2749 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2766 $child->configure ($X, $Y, $W, $H); 2750 $child->configure ($X, $Y, $W, $H);
2767 } 2751 }
2768}
2769 2752
2770sub _topleft { 2753 $self->{old_w} = $w;
2754 $self->{old_h} = $h;
2755}
2756
2757sub coord2local {
2771 my ($self, $x, $y) = @_; 2758 my ($self, $x, $y) = @_;
2772 2759
2773 ($x, $y) 2760 ($x, $y)
2774} 2761}
2775 2762
2763sub coord2global {
2764 my ($self, $x, $y) = @_;
2765
2766 ($x, $y)
2767}
2768
2776sub update { 2769sub update {
2777 my ($self) = @_; 2770 my ($self) = @_;
2778 2771
2779 $self->check_size; 2772 $self->check_size;
2780 ::refresh (); 2773 $::WANT_REFRESH++;
2781} 2774}
2782 2775
2783sub add { 2776sub add {
2784 my ($self, $child) = @_; 2777 my ($self, $child) = @_;
2785 2778
2786 # integerize window positions 2779 # integerise window positions
2787 $child->{x} = int $child->{x}; 2780 $child->{x} = int $child->{x};
2788 $child->{y} = int $child->{y}; 2781 $child->{y} = int $child->{y};
2789 2782
2790 $self->SUPER::add ($child); 2783 $self->SUPER::add ($child);
2791} 2784}
2794 my ($self, $id, $cb) = @_; 2787 my ($self, $id, $cb) = @_;
2795 2788
2796 $self->{refresh_hook}{$id} = $cb; 2789 $self->{refresh_hook}{$id} = $cb;
2797} 2790}
2798 2791
2799sub draw { 2792sub draw {
2800 my ($self) = @_; 2793 my ($self) = @_;
2801 2794
2795 if ($self->{check_size}) {
2796 my @queue = ([], []);
2797
2798 for (;;) {
2799 if ($self->{check_size}) {
2800 # heuristic: check containers last
2801 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_
2802 for values %{delete $self->{check_size}}
2803 }
2804
2805 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last;
2806
2807 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2808 ? @$widget{qw(user_w user_h)}
2809 : $widget->size_request;
2810
2811 if (delete $widget->{force_alloc}
2812 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2813 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2814
2815 $widget->{req_w} = $w;
2816 $widget->{req_h} = $h;
2817
2818 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2819
2820 $widget->{parent}->check_size
2821 if $widget->{parent};
2822 }
2823 }
2824 }
2825
2826 while ($self->{size_alloc}) {
2827 for (values %{delete $self->{size_alloc}}) {
2828 my ($widget, $w, $h) = @$_;
2829
2830 $w = 0 if $w < 0;
2831 $h = 0 if $h < 0;
2832
2833 $widget->{w} = $w;
2834 $widget->{h} = $h;
2835 $widget->size_allocate ($w, $h);
2836 $widget->emit (size_allocate => $w, $h);
2837 }
2838 }
2839
2802 while (my $rcb = delete $self->{refresh_hook}) { 2840 while ($self->{refresh_hook}) {
2803 $_->() for values %$rcb; 2841 $_->()
2842 for values %{delete $self->{refresh_hook}};
2804 } 2843 }
2805 2844
2806 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2845 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2807 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2846 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2808 glClear GL_COLOR_BUFFER_BIT; 2847 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines