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.192 by root, Tue May 9 22:49:32 2006 UTC vs.
Revision 1.194 by root, Thu May 11 23:41:47 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
380} 381}
381 382
382sub set_parent { 383sub set_parent {
383 my ($self, $parent) = @_; 384 my ($self, $parent) = @_;
384 385
385 $self->{_tree_depth} = $parent->{_tree_depth} + 1;
386 Scalar::Util::weaken ($self->{parent} = $parent); 386 Scalar::Util::weaken ($self->{parent} = $parent);
387
388 $self->check_size
389 unless exists $self->{req_w};
387} 390}
388 391
389sub check_size { 392sub check_size {
390 my ($self) = @_; 393 my ($self) = @_;
391 394
506 $self->{children} = [ 509 $self->{children} = [
507 sort { $a->{z} <=> $b->{z} } 510 sort { $a->{z} <=> $b->{z} }
508 @{$self->{children}}, @widgets 511 @{$self->{children}}, @widgets
509 ]; 512 ];
510 513
511 $_->check_size 514 $self->check_size;
512 for @widgets;
513
514 $self->update; 515 $self->update;
515} 516}
516 517
517sub children { 518sub children {
518 @{ $_[0]{children} } 519 @{ $_[0]{children} }
666 my ($w, $h) = ($self->w, $self->h); 667 my ($w, $h) = ($self->w, $self->h);
667 668
668 my $tex = $self->{texture} 669 my $tex = $self->{texture}
669 or return; 670 or return;
670 671
671 glEnable GL_BLEND;
672 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
673 glEnable GL_TEXTURE_2D; 672 glEnable GL_TEXTURE_2D;
674 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 673 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
675 glColor 0, 0, 0, 1; 674 glColor 0, 0, 0, 1;
676 675
677 $tex->draw_quad (0, 0, $w, $h); 676 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
678 677
679 glDisable GL_BLEND;
680 glDisable GL_TEXTURE_2D; 678 glDisable GL_TEXTURE_2D;
681} 679}
682 680
683############################################################################# 681#############################################################################
684 682
1213} 1211}
1214 1212
1215sub size_allocate { 1213sub size_allocate {
1216 my ($self, $w, $h) = @_; 1214 my ($self, $w, $h) = @_;
1217 1215
1216 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1217
1218 my $children = $self->{children}; 1218 my $children = $self->{children};
1219 1219
1220 my @h = map $_->{req_h}, @$children; 1220 my @h = map $_->{req_h}, @$children;
1221 1221
1222 my $req_h = List::Util::sum @h; 1222 my $req_h = List::Util::sum @h;
1261 my ($class, %arg) = @_; 1261 my ($class, %arg) = @_;
1262 1262
1263 my $self = $class->SUPER::new ( 1263 my $self = $class->SUPER::new (
1264 fg => [1, 1, 1], 1264 fg => [1, 1, 1],
1265 #font => default_font 1265 #font => default_font
1266 #text => initial text
1267 #markup => initial narkup
1268 layout => (new CFClient::Layout),
1266 fontsize => 1, 1269 fontsize => 1,
1267 text => "",
1268 align => -1, 1270 align => -1,
1269 valign => -1, 1271 valign => -1,
1270 padding => 2, 1272 padding => 2,
1271 layout => new CFClient::Layout,
1272 can_events => 0, 1273 can_events => 0,
1273 %arg 1274 %arg
1274 ); 1275 );
1275 1276
1276 if (exists $self->{template}) { 1277 if (exists $self->{template}) {
1277 my $layout = new CFClient::Layout; 1278 my $layout = new CFClient::Layout;
1278 $layout->set_text (delete $self->{template}); 1279 $layout->set_text (delete $self->{template});
1279 $self->{template} = $layout; 1280 $self->{template} = $layout;
1280 } 1281 }
1281 1282
1282 $self->set_text (delete $self->{text}) if exists $self->{text}; 1283 if (exists $self->{markup}) {
1283 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1284 $self->set_markup (delete $self->{markup});
1285 } else {
1286 $self->set_text (delete $self->{text});
1287 }
1284 1288
1285 $self 1289 $self
1286} 1290}
1287 1291
1288sub escape { 1292sub escape {
1306 my ($self, $text) = @_; 1310 my ($self, $text) = @_;
1307 1311
1308 return if $self->{text} eq "T$text"; 1312 return if $self->{text} eq "T$text";
1309 $self->{text} = "T$text"; 1313 $self->{text} = "T$text";
1310 1314
1315 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1311 $self->{layout}->set_text ($text); 1316 $self->{layout}->set_text ($text);
1312 1317
1313 $self->update; 1318 $self->update;
1314 $self->check_size; 1319 $self->check_size;
1315} 1320}
1318 my ($self, $markup) = @_; 1323 my ($self, $markup) = @_;
1319 1324
1320 return if $self->{text} eq "M$markup"; 1325 return if $self->{text} eq "M$markup";
1321 $self->{text} = "M$markup"; 1326 $self->{text} = "M$markup";
1322 1327
1328 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1329
1330 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1323 $self->{layout}->set_markup ($markup); 1331 $self->{layout}->set_markup ($markup);
1324 1332
1325 $self->update; 1333 $self->update;
1326 $self->check_size; 1334 $self->check_size;
1327} 1335}
1369 1377
1370sub _draw { 1378sub _draw {
1371 my ($self) = @_; 1379 my ($self) = @_;
1372 1380
1373 my $tex = $self->{texture} ||= do { 1381 my $tex = $self->{texture} ||= do {
1382 $self->{layout}->set_foreground (@{$self->{fg}});
1374 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1383 $self->{layout}->set_font ($self->{font}) if $self->{font};
1375 $self->{layout}->set_width ($self->{w}); 1384 $self->{layout}->set_width ($self->{w});
1376 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1385 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1386
1377 new_from_layout CFClient::Texture $self->{layout} 1387 my $tex = new_from_layout CFClient::Texture $self->{layout};
1388
1389 $self->{ox} = int $self->{align} < 0 ? $self->{padding}
1390 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1391 : ($self->{w} - $tex->{w}) * 0.5;
1392
1393 $self->{oy} = int $self->{valign} < 0 ? $self->{padding}
1394 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1395 : ($self->{h} - $tex->{h}) * 0.5;
1396
1397 $tex
1378 }; 1398 };
1379 1399
1380 glEnable GL_BLEND;
1381 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1382 glEnable GL_TEXTURE_2D; 1400 glEnable GL_TEXTURE_2D;
1383 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1401 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1384 1402
1403 if ($tex->{format} == GL_ALPHA) {
1385 glColor @{$self->{fg}}; 1404 glColor @{$self->{fg}};
1386
1387 $self->{ox} = int (
1388 $self->{align} < 0 ? $self->{padding}
1389 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1390 : ($self->{w} - $tex->{w}) * 0.5
1391 );
1392
1393 $self->{oy} = int (
1394 $self->{valign} < 0 ? $self->{padding}
1395 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1396 : ($self->{h} - $tex->{h}) * 0.5
1397 );
1398
1399 $tex->draw_quad ($self->{ox}, $self->{oy}); 1405 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1406 } else {
1407 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1408 }
1400 1409
1401 glDisable GL_TEXTURE_2D; 1410 glDisable GL_TEXTURE_2D;
1402 glDisable GL_BLEND;
1403} 1411}
1404 1412
1405############################################################################# 1413#############################################################################
1406 1414
1407package CFClient::UI::EntryBase; 1415package CFClient::UI::EntryBase;
1442 $self->{layout}->set_text ("$text "); 1450 $self->{layout}->set_text ("$text ");
1443 1451
1444 $self->emit (changed => $self->{text}); 1452 $self->emit (changed => $self->{text});
1445} 1453}
1446 1454
1455sub set_text {
1456 my ($self, $text) = @_;
1457
1458 $self->{cursor} = length $text;
1459 $self->_set_text ($text);
1460 $self->check_size;
1461 $self->update;
1462}
1463
1447sub get_text { 1464sub get_text {
1448 $_[0]{text} 1465 $_[0]{text}
1449} 1466}
1450 1467
1451sub size_request { 1468sub size_request {
1458 1475
1459sub size_allocate { 1476sub size_allocate {
1460 my ($self, $w, $h) = @_; 1477 my ($self, $w, $h) = @_;
1461 1478
1462 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text 1479 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1463}
1464
1465sub set_text {
1466 my ($self, $text) = @_;
1467
1468 $self->{cursor} = length $text;
1469 $self->_set_text ($text);
1470 $self->update;
1471} 1480}
1472 1481
1473sub key_down { 1482sub key_down {
1474 my ($self, $ev) = @_; 1483 my ($self, $ev) = @_;
1475 1484
2653 } 2662 }
2654} 2663}
2655 2664
2656############################################################################# 2665#############################################################################
2657 2666
2667package CFClient::UI::Statusbox;
2668
2669our @ISA = CFClient::UI::VBox::;
2670
2671sub reorder {
2672 my ($self) = @_;
2673 my $NOW = time;
2674
2675 while (my ($k, $v) = each %{ $self->{item} }) {
2676 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2677 }
2678
2679 my @widgets;
2680 my @items = sort { $a->{time} <=> $b->{time} } values %{ $self->{item} };
2681 my $count = 10 + 1;
2682 for my $item (@items) {
2683 last unless --$count;
2684
2685 push @widgets, $item->{label} ||= do {
2686 # TODO: doesn't handle markup well (read as: at all)
2687 my $short = delete $item->{text};
2688 for ($short) {
2689 s/^\s+//;
2690 s/\012.*//s;
2691 my $len = int 30 / $item->{fontsize};
2692 substr $_, $len, length, "…" if $len < length;
2693 }
2694
2695 new CFClient::UI::Label
2696 markup => $short,
2697 tooltip => delete $item->{tooltip},
2698 fontsize => delete $item->{fontsize},
2699 color => delete $item->{color},
2700 can_events => 1,
2701 can_hover => 1,
2702 };
2703 }
2704
2705 $self->clear;
2706 $self->SUPER::add (@widgets);
2707}
2708
2709sub add {
2710 my ($self, $text, %arg) = @_;
2711
2712 my $item = {
2713 time => time,
2714 text => $text,
2715 timeout => 60,
2716 tooltip => $text,
2717 fontsize => 0.8,
2718 color => [0.8, 0.8, 0.8, 0.8],
2719 %arg,
2720 };
2721
2722 $item->{timeout} += time;
2723 $item->{group} ||= $item+0;
2724
2725 $item = $self->{item}{$item->{group}} ||= $item;
2726
2727 $self->reorder;
2728}
2729
2730#############################################################################
2731
2658package CFClient::UI::Root; 2732package CFClient::UI::Root;
2659 2733
2660our @ISA = CFClient::UI::Container::; 2734our @ISA = CFClient::UI::Container::;
2661 2735
2662use CFClient::OpenGL; 2736use CFClient::OpenGL;
2773 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 2847 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2774 ? @$widget{qw(user_w user_h)} 2848 ? @$widget{qw(user_w user_h)}
2775 : $widget->size_request; 2849 : $widget->size_request;
2776 2850
2777 if ($w != $widget->{req_w} || $h != $widget->{req_h}) { 2851 if ($w != $widget->{req_w} || $h != $widget->{req_h}) {
2852 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2853
2778 $widget->{req_w} = $w; 2854 $widget->{req_w} = $w;
2779 $widget->{req_h} = $h; 2855 $widget->{req_h} = $h;
2780 2856
2781 $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}]; 2857 $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}];
2782 2858
2787 } 2863 }
2788 2864
2789 while ($self->{size_alloc}) { 2865 while ($self->{size_alloc}) {
2790 for (values %{delete $self->{size_alloc}}) { 2866 for (values %{delete $self->{size_alloc}}) {
2791 my ($widget, $w, $h) = @$_; 2867 my ($widget, $w, $h) = @$_;
2868
2869 $w = 0 if $w < 0;
2870 $h = 0 if $h < 0;
2792 2871
2793 $widget->{w} = $w; 2872 $widget->{w} = $w;
2794 $widget->{h} = $h; 2873 $widget->{h} = $h;
2795 $widget->size_allocate ($w, $h); 2874 $widget->size_allocate ($w, $h);
2796 $widget->emit (size_allocate => $w, $h); 2875 $widget->emit (size_allocate => $w, $h);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines