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.102 by elmex, Fri Apr 14 12:17:06 2006 UTC vs.
Revision 1.105 by root, Fri Apr 14 14:55:28 2006 UTC

61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
62 62
63 if ($widget != $HOVER) { 63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 64 my $hover = $HOVER; $HOVER = $widget;
65 65
66 $hover->update if $hover; 66 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER; 67 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 } 68 }
69 69
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
71} 71}
72 72
439} 439}
440 440
441sub render_chld { 441sub render_chld {
442 my ($self) = @_; 442 my ($self) = @_;
443 443
444 $self->{texture} = 444 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
445 CFClient::Texture->new_from_opengl ( 445 glClearColor 0, 0, 0, 1;
446 $self->{w}, $self->{h}, sub { $self->child->draw } 446 glClear GL_COLOR_BUFFER_BIT;
447 ); 447 $self->child->draw;
448 };
448} 449}
449 450
450sub size_allocate { 451sub size_allocate {
451 my ($self, $x, $y, $w, $h) = @_; 452 my ($self, $x, $y, $w, $h) = @_;
452 453
831 } 832 }
832 833
833 my $y = 0; 834 my $y = 0;
834 for (0 .. $#$children) { 835 for (0 .. $#$children) {
835 my $child = $children->[$_]; 836 my $child = $children->[$_];
836 my $h = $h[$_]; 837 my $h = int $h[$_];
837 $child->size_allocate ($y, 0, $h, $w); 838 $child->size_allocate ($y, 0, $h, $w);
838 839
839 $y += $h; 840 $y += $h;
840 } 841 }
841} 842}
885 } 886 }
886 887
887 my $y = 0; 888 my $y = 0;
888 for (0 .. $#$children) { 889 for (0 .. $#$children) {
889 my $child = $children->[$_]; 890 my $child = $children->[$_];
890 my $h = $h[$_]; 891 my $h = int $h[$_];
891 $child->size_allocate (0, $y, $w, $h); 892 $child->size_allocate (0, $y, $w, $h);
892 893
893 $y += $h; 894 $y += $h;
894 } 895 }
895} 896}
904 905
905sub new { 906sub new {
906 my ($class, %arg) = @_; 907 my ($class, %arg) = @_;
907 908
908 my $self = $class->SUPER::new ( 909 my $self = $class->SUPER::new (
909 fg => [1, 1, 1], 910 fg => [1, 1, 1],
910 height => $::FONTSIZE, 911 fontsize => $::FONTSIZE,
911 text => "", 912 text => "",
912 align => -1, 913 align => -1,
913 padding => 2, 914 padding => 2,
914 layout => new CFClient::Layout, 915 layout => new CFClient::Layout,
915 %arg 916 %arg
916 ); 917 );
917 918
918 $self->set_text ($self->{text}); 919 $self->set_text ($self->{text});
919 920
949 950
950sub size_request { 951sub size_request {
951 my ($self) = @_; 952 my ($self) = @_;
952 953
953 $self->{layout}->set_width; 954 $self->{layout}->set_width;
954 $self->{layout}->set_height ($self->{height}); 955 $self->{layout}->set_height ($self->{fontsize});
955 my ($w, $h) = $self->{layout}->size; 956 my ($w, $h) = $self->{layout}->size;
956 957
957 ( 958 (
958 $w + $self->{padding} * 2, 959 $w + $self->{padding} * 2,
959 $h + $self->{padding} * 2, 960 $h + $self->{padding} * 2,
978sub _draw { 979sub _draw {
979 my ($self) = @_; 980 my ($self) = @_;
980 981
981 my $tex = $self->{texture} ||= do { 982 my $tex = $self->{texture} ||= do {
982 $self->{layout}->set_width ($self->{w}); 983 $self->{layout}->set_width ($self->{w});
983 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); 984 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
984 new_from_layout CFClient::Texture $self->{layout} 985 new_from_layout CFClient::Texture $self->{layout}
985 }; 986 };
986 987
987 glEnable GL_BLEND; 988 glEnable GL_BLEND;
988 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 989 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
989 glEnable GL_TEXTURE_2D; 990 glEnable GL_TEXTURE_2D;
990 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 991 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
991 992
992 glColor @{$self->{fg}}; 993 glColor @{$self->{fg}};
993 994
994 my $x = 995 my $x =
995 $self->{align} < 0 ? $self->{padding} 996 $self->{align} < 0 ? $self->{padding}
996 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 997 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
997 : ($self->{w} - $tex->{w}) * 0.5; 998 : ($self->{w} - $tex->{w}) * 0.5;
998 999
999 glTranslate int $x, int +($self->{h} - $tex->{h}) * 0.5, 0; 1000 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5);
1000 $tex->draw_quad (0, 0);
1001 1001
1002 glDisable GL_TEXTURE_2D; 1002 glDisable GL_TEXTURE_2D;
1003 glDisable GL_BLEND; 1003 glDisable GL_BLEND;
1004} 1004}
1005 1005
1033 1033
1034 $self->{last_activity} = $::NOW; 1034 $self->{last_activity} = $::NOW;
1035 1035
1036 $self->{text} = $text; 1036 $self->{text} = $text;
1037 $self->{layout}->set_width ($self->{w}); 1037 $self->{layout}->set_width ($self->{w});
1038 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); 1038 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1039 1039
1040 $text =~ s/./*/g if $self->{hidden}; 1040 $text =~ s/./*/g if $self->{hidden};
1041 1041
1042 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1042 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
1043 1043
1350 range => [0, 0, 100, 10], 1350 range => [0, 0, 100, 10],
1351 req_w => 40, 1351 req_w => 40,
1352 req_h => 13, 1352 req_h => 13,
1353 vertical => 0, 1353 vertical => 0,
1354 can_hover => 1, 1354 can_hover => 1,
1355 inner_pad => 5,
1355 @_ 1356 @_
1356 ); 1357 );
1357 1358
1358 $self 1359 $self
1359} 1360}
1380 if ($GRAB == $self) { 1381 if ($GRAB == $self) {
1381 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1382 my ($value, $lo, $hi, $page) = @{$self->{range}};
1382 1383
1383 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 1384 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1384 1385
1386 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1387 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1388
1389 $x -= $inner_pad_px; # substract the padding
1385 $x = $x * ($hi - $lo) / $w + $lo; 1390 $x = $x * ($hi - $lo) / $inner_w + $lo;
1386 $x = $lo if $x < $lo; 1391 $x = $lo if $x < $lo;
1387 $x = $hi - $page if $x > $hi - $page; 1392 $x = $hi - $page if $x > $hi - $page;
1388 $self->{range}[0] = $x; 1393 $self->{range}[0] = $x;
1389 1394
1390 $self->emit (changed => $x); 1395 $self->emit (changed => $x);
1391 $self->update; 1396 $self->update;
1392 } 1397 }
1398}
1399
1400# the inner_* stuff is for generating a padding for the slider handle,
1401# so that the handle doesn't leave the texture. This calculation isn't 100%
1402# correct propably, but it does the job for now
1403sub _calc_inner_pad_px {
1404 my ($self, $w) = @_;
1405 ($w / 100) * $self->{inner_pad} # % to pixels
1393} 1406}
1394 1407
1395sub _draw { 1408sub _draw {
1396 my ($self) = @_; 1409 my ($self) = @_;
1397 1410
1411 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1424 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1412 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1425 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1413 1426
1414 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1427 my ($value, $lo, $hi, $page) = @{$self->{range}};
1415 1428
1416 # the inner_* stuff is for generating a padding for the slider handle, 1429 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1417 # so that the handle doesn't leave the texture. This calculation isn't 100%
1418 # correct propably, but it does the job for now
1419 my $inner_pad = 5; # 5% of width for slider bg texture border
1420 my $inner_pad_px = ($w / 100) * $inner_pad; # % to pixels
1421 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right 1430 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1422 1431
1423 $page = int $page * $inner_w / ($hi - $lo); 1432 $page = int $page * $inner_w / ($hi - $lo);
1424 $value = int +($value - $lo) * $inner_w / ($hi - $lo); 1433 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1425 1434
1426 $w -= $page; 1435 $w -= $page;
1456 1465
1457sub new { 1466sub new {
1458 my $class = shift; 1467 my $class = shift;
1459 1468
1460 my $self = $class->SUPER::new ( 1469 my $self = $class->SUPER::new (
1470 req_w => $::WIDTH / 6,
1471 req_h => $::HEIGHT / 6,
1472 fontsize => $::FONTSIZE,
1473 @_,
1474
1475 layout => (new CFClient::Layout),
1461 par => [], 1476 par => [],
1462 @_, 1477 height => 0,
1463 children => [ 1478 children => [
1464 (new CFClient::UI::Empty expand => 1), 1479 (new CFClient::UI::Empty expand => 1),
1465 (new CFClient::UI::Slider vertical => 1), 1480 (new CFClient::UI::Slider vertical => 1),
1466 ], 1481 ],
1467 ); 1482 );
1468 1483
1469 $self 1484 $self
1470} 1485}
1471 1486
1487sub text_height {
1488 my ($self, $text) = @_;
1489
1490 my $layout = $self->{layout};
1491
1492 $layout->set_height ($self->{fontsize});
1493 $layout->set_width ($self->{w});
1494 $layout->set_text ($text);
1495
1496 ($layout->size)[1]
1497}
1498
1499sub reflow {
1500 my ($self) = @_;
1501
1502 my $height = 0;
1503
1504 for (@{$self->{par}}) {
1505 $height += $_->[0] = $self->text_height ($_->[2]);
1506 }
1507
1508 $self->{height} = $height;
1509
1510 $self->{range} = [$height - $self->{h}, 0, $height, $self->{h}];
1511}
1512
1513sub size_request {
1514 my ($self) = @_;
1515
1516 ($self->{req_w}, $self->{req_h})
1517}
1518
1519sub size_allocate {
1520 my ($self, $x, $y, $w, $h) = @_;
1521
1522 $self->SUPER::size_allocate ($x, $y, $w, $h);
1523
1524 $self->{layout}->set_height ($self->{fontsize});
1525 $self->{layout}->set_width ($self->{w});
1526
1527 $self->reflow;
1528 $self->update;
1529}
1530
1472sub add_paragraph { 1531sub add_paragraph {
1473 my ($self, $color, $text) = @_; 1532 my ($self, $color, $text) = @_;
1474 1533
1534 #TODO: intelligently "reformat" paragraph
1535
1536 my $height = $self->text_height ($text);
1537
1538 $self->{height} += $height;
1539
1475 push @{$self->{par}}, [$color, $text]; 1540 push @{$self->{par}}, [$height, $color, $text];
1476 my $count = scalar @{$self->{par}}; 1541
1477 $self->{children}[1]{range} = [$count - 1, 0, $count, 1]; 1542 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
1478 $self->{children}[1]->update; 1543 $self->{children}[1]->update;
1479} 1544}
1480 1545
1546sub update {
1547 my ($self) = @_;
1548
1549 $self->SUPER::update;
1550
1551 return unless $self->{h} > 0;
1552
1553 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
1554 glClearColor 0, 0, 0, 1;
1555 glClear GL_COLOR_BUFFER_BIT;
1556
1557 my $y0 = $self->{height} - $self->{h};
1558 my $y1 = $self->{height};
1559
1560 glEnable GL_BLEND;
1561 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1562 glEnable GL_TEXTURE_2D;
1563 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1564
1565 my $y = 0;
1566
1567 my $layout = $self->{layout};
1568
1569 for my $par (@{$self->{par}}) {
1570 my $h = $par->[0];
1571
1572 if ($y0 < $y + $h && $y < $y1) {
1573 $layout->set_text ($par->[2]);
1574
1575 glColor @{ $par->[1] };
1576 my ($W, $H) = $layout->size;
1577 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0);
1578 }
1579
1580 $y += $h;
1581 }
1582
1583 glDisable GL_TEXTURE_2D;
1584 glDisable GL_BLEND;
1585 };
1586}
1587
1481sub _draw { 1588sub _draw {
1482 my ($self) = @_; 1589 my ($self) = @_;
1483 1590
1484 my $par = $self->{par}; 1591 if ($self->{texture}) {
1485
1486 my $bottom = (scalar @$par) - 1;
1487
1488 my $w = $self->{children}[0]{w};
1489 my $y = $self->{h};
1490
1491 glEnable GL_BLEND;
1492 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1493 glEnable GL_TEXTURE_2D; 1592 glEnable GL_TEXTURE_2D;
1494 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1593 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1495 1594 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1496 # TODO: everything!
1497 while ($y > 0 && $bottom >= 0) {
1498 my $par = $par->[$bottom--];
1499
1500 my $layout = new CFClient::Layout;
1501
1502 $layout->set_height ($::FONTSIZE);
1503 $layout->set_width ($w);
1504 $layout->set_text ($par->[1]);
1505 my $tex = new_from_layout CFClient::Texture $layout;
1506 $y -= $tex->{h};
1507
1508 glColor @{ $par->[0] };
1509 $tex->draw_quad (0, $y);
1510 }
1511
1512 glDisable GL_TEXTURE_2D; 1595 glDisable GL_TEXTURE_2D;
1513 glDisable GL_BLEND; 1596 }
1597
1514} 1598}
1515 1599
1516############################################################################# 1600#############################################################################
1517 1601
1518package CFClient::UI::MapWidget; 1602package CFClient::UI::MapWidget;
1653 $::CONN->user_send ("command stay fire"); 1737 $::CONN->user_send ("command stay fire");
1654 } elsif ($sym == SDLK_a) { 1738 } elsif ($sym == SDLK_a) {
1655 $::CONN->user_send ("command apply"); 1739 $::CONN->user_send ("command apply");
1656 } elsif ($sym == SDLK_QUOTE) { 1740 } elsif ($sym == SDLK_QUOTE) {
1657 $self->emit ('activate_console'); 1741 $self->emit ('activate_console');
1742 } elsif ($sym == SDLK_SLASH) {
1743 $self->emit ('activate_console' => '/');
1658 } elsif (exists $DIR{$sym}) { 1744 } elsif (exists $DIR{$sym}) {
1659 if ($mod & KMOD_SHIFT) { 1745 if ($mod & KMOD_SHIFT) {
1660 $self->{shft}++; 1746 $self->{shft}++;
1661 $::CONN->user_send ("command fire $DIR{$sym}[0]"); 1747 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1662 } elsif ($mod & KMOD_CTRL) { 1748 } elsif ($mod & KMOD_CTRL) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines