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.193 by root, Wed May 10 21:12:26 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
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
1263 my ($class, %arg) = @_; 1261 my ($class, %arg) = @_;
1264 1262
1265 my $self = $class->SUPER::new ( 1263 my $self = $class->SUPER::new (
1266 fg => [1, 1, 1], 1264 fg => [1, 1, 1],
1267 #font => default_font 1265 #font => default_font
1266 #text => initial text
1267 #markup => initial narkup
1268 layout => (new CFClient::Layout),
1268 fontsize => 1, 1269 fontsize => 1,
1269 text => "",
1270 align => -1, 1270 align => -1,
1271 valign => -1, 1271 valign => -1,
1272 padding => 2, 1272 padding => 2,
1273 layout => new CFClient::Layout,
1274 can_events => 0, 1273 can_events => 0,
1275 %arg 1274 %arg
1276 ); 1275 );
1277 1276
1278 if (exists $self->{template}) { 1277 if (exists $self->{template}) {
1279 my $layout = new CFClient::Layout; 1278 my $layout = new CFClient::Layout;
1280 $layout->set_text (delete $self->{template}); 1279 $layout->set_text (delete $self->{template});
1281 $self->{template} = $layout; 1280 $self->{template} = $layout;
1282 } 1281 }
1283 1282
1284 $self->set_text (delete $self->{text}) if exists $self->{text}; 1283 if (exists $self->{markup}) {
1285 $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 }
1286 1288
1287 $self 1289 $self
1288} 1290}
1289 1291
1290sub escape { 1292sub escape {
1308 my ($self, $text) = @_; 1310 my ($self, $text) = @_;
1309 1311
1310 return if $self->{text} eq "T$text"; 1312 return if $self->{text} eq "T$text";
1311 $self->{text} = "T$text"; 1313 $self->{text} = "T$text";
1312 1314
1315 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1313 $self->{layout}->set_text ($text); 1316 $self->{layout}->set_text ($text);
1314 1317
1315 $self->update; 1318 $self->update;
1316 $self->check_size; 1319 $self->check_size;
1317} 1320}
1320 my ($self, $markup) = @_; 1323 my ($self, $markup) = @_;
1321 1324
1322 return if $self->{text} eq "M$markup"; 1325 return if $self->{text} eq "M$markup";
1323 $self->{text} = "M$markup"; 1326 $self->{text} = "M$markup";
1324 1327
1328 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1329
1330 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1325 $self->{layout}->set_markup ($markup); 1331 $self->{layout}->set_markup ($markup);
1326 1332
1327 $self->update; 1333 $self->update;
1328 $self->check_size; 1334 $self->check_size;
1329} 1335}
1371 1377
1372sub _draw { 1378sub _draw {
1373 my ($self) = @_; 1379 my ($self) = @_;
1374 1380
1375 my $tex = $self->{texture} ||= do { 1381 my $tex = $self->{texture} ||= do {
1382 $self->{layout}->set_foreground (@{$self->{fg}});
1376 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1383 $self->{layout}->set_font ($self->{font}) if $self->{font};
1377 $self->{layout}->set_width ($self->{w}); 1384 $self->{layout}->set_width ($self->{w});
1378 $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
1379 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
1380 }; 1398 };
1381 1399
1382 glEnable GL_BLEND;
1383 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1384 glEnable GL_TEXTURE_2D; 1400 glEnable GL_TEXTURE_2D;
1385 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1401 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1386 1402
1403 if ($tex->{format} == GL_ALPHA) {
1387 glColor @{$self->{fg}}; 1404 glColor @{$self->{fg}};
1388
1389 $self->{ox} = int (
1390 $self->{align} < 0 ? $self->{padding}
1391 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1392 : ($self->{w} - $tex->{w}) * 0.5
1393 );
1394
1395 $self->{oy} = int (
1396 $self->{valign} < 0 ? $self->{padding}
1397 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1398 : ($self->{h} - $tex->{h}) * 0.5
1399 );
1400
1401 $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 }
1402 1409
1403 glDisable GL_TEXTURE_2D; 1410 glDisable GL_TEXTURE_2D;
1404 glDisable GL_BLEND;
1405} 1411}
1406 1412
1407############################################################################# 1413#############################################################################
1408 1414
1409package CFClient::UI::EntryBase; 1415package CFClient::UI::EntryBase;
1444 $self->{layout}->set_text ("$text "); 1450 $self->{layout}->set_text ("$text ");
1445 1451
1446 $self->emit (changed => $self->{text}); 1452 $self->emit (changed => $self->{text});
1447} 1453}
1448 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
1449sub get_text { 1464sub get_text {
1450 $_[0]{text} 1465 $_[0]{text}
1451} 1466}
1452 1467
1453sub size_request { 1468sub size_request {
1460 1475
1461sub size_allocate { 1476sub size_allocate {
1462 my ($self, $w, $h) = @_; 1477 my ($self, $w, $h) = @_;
1463 1478
1464 $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
1465}
1466
1467sub set_text {
1468 my ($self, $text) = @_;
1469
1470 $self->{cursor} = length $text;
1471 $self->_set_text ($text);
1472 $self->update;
1473} 1480}
1474 1481
1475sub key_down { 1482sub key_down {
1476 my ($self, $ev) = @_; 1483 my ($self, $ev) = @_;
1477 1484
2655 } 2662 }
2656} 2663}
2657 2664
2658############################################################################# 2665#############################################################################
2659 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
2660package CFClient::UI::Root; 2732package CFClient::UI::Root;
2661 2733
2662our @ISA = CFClient::UI::Container::; 2734our @ISA = CFClient::UI::Container::;
2663 2735
2664use CFClient::OpenGL; 2736use CFClient::OpenGL;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines