… | |
… | |
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 | |
441 | sub render_chld { |
441 | sub 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 | |
450 | sub size_allocate { |
451 | sub size_allocate { |
451 | my ($self, $x, $y, $w, $h) = @_; |
452 | my ($self, $x, $y, $w, $h) = @_; |
452 | |
453 | |
… | |
… | |
731 | my $col_expand = (sum @col_expand) || 1; |
732 | my $col_expand = (sum @col_expand) || 1; |
732 | |
733 | |
733 | # linearly scale sizes |
734 | # linearly scale sizes |
734 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
735 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
735 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
736 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
|
|
737 | |
|
|
738 | $_ = int $_ for @$ws; #TODO: avoid rounding problems |
|
|
739 | $_ = int $_ for @$hs; #TODO: avoid rounding problems |
736 | |
740 | |
737 | my $y; |
741 | my $y; |
738 | |
742 | |
739 | for my $r (0 .. $#{$self->{children}}) { |
743 | for my $r (0 .. $#{$self->{children}}) { |
740 | my $row = $self->{children}[$r] |
744 | my $row = $self->{children}[$r] |
… | |
… | |
831 | } |
835 | } |
832 | |
836 | |
833 | my $y = 0; |
837 | my $y = 0; |
834 | for (0 .. $#$children) { |
838 | for (0 .. $#$children) { |
835 | my $child = $children->[$_]; |
839 | my $child = $children->[$_]; |
836 | my $h = $h[$_]; |
840 | my $h = int $h[$_]; |
837 | $child->size_allocate ($y, 0, $h, $w); |
841 | $child->size_allocate ($y, 0, $h, $w); |
838 | |
842 | |
839 | $y += $h; |
843 | $y += $h; |
840 | } |
844 | } |
841 | } |
845 | } |
… | |
… | |
885 | } |
889 | } |
886 | |
890 | |
887 | my $y = 0; |
891 | my $y = 0; |
888 | for (0 .. $#$children) { |
892 | for (0 .. $#$children) { |
889 | my $child = $children->[$_]; |
893 | my $child = $children->[$_]; |
890 | my $h = $h[$_]; |
894 | my $h = int $h[$_]; |
891 | $child->size_allocate (0, $y, $w, $h); |
895 | $child->size_allocate (0, $y, $w, $h); |
892 | |
896 | |
893 | $y += $h; |
897 | $y += $h; |
894 | } |
898 | } |
895 | } |
899 | } |
… | |
… | |
904 | |
908 | |
905 | sub new { |
909 | sub new { |
906 | my ($class, %arg) = @_; |
910 | my ($class, %arg) = @_; |
907 | |
911 | |
908 | my $self = $class->SUPER::new ( |
912 | my $self = $class->SUPER::new ( |
909 | fg => [1, 1, 1], |
913 | fg => [1, 1, 1], |
910 | height => $::FONTSIZE, |
914 | fontsize => $::FONTSIZE, |
911 | text => "", |
915 | text => "", |
912 | align => -1, |
916 | align => -1, |
913 | padding => 2, |
917 | padding => 2, |
914 | layout => new CFClient::Layout, |
918 | layout => new CFClient::Layout, |
915 | %arg |
919 | %arg |
916 | ); |
920 | ); |
917 | |
921 | |
918 | $self->set_text ($self->{text}); |
922 | $self->set_text ($self->{text}); |
919 | |
923 | |
… | |
… | |
949 | |
953 | |
950 | sub size_request { |
954 | sub size_request { |
951 | my ($self) = @_; |
955 | my ($self) = @_; |
952 | |
956 | |
953 | $self->{layout}->set_width; |
957 | $self->{layout}->set_width; |
954 | $self->{layout}->set_height ($self->{height}); |
958 | $self->{layout}->set_height ($self->{fontsize}); |
955 | my ($w, $h) = $self->{layout}->size; |
959 | my ($w, $h) = $self->{layout}->size; |
956 | |
960 | |
957 | ( |
961 | ( |
958 | $w + $self->{padding} * 2, |
962 | $w + $self->{padding} * 2, |
959 | $h + $self->{padding} * 2, |
963 | $h + $self->{padding} * 2, |
… | |
… | |
978 | sub _draw { |
982 | sub _draw { |
979 | my ($self) = @_; |
983 | my ($self) = @_; |
980 | |
984 | |
981 | my $tex = $self->{texture} ||= do { |
985 | my $tex = $self->{texture} ||= do { |
982 | $self->{layout}->set_width ($self->{w}); |
986 | $self->{layout}->set_width ($self->{w}); |
983 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); |
987 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
984 | new_from_layout CFClient::Texture $self->{layout} |
988 | new_from_layout CFClient::Texture $self->{layout} |
985 | }; |
989 | }; |
986 | |
990 | |
987 | glEnable GL_BLEND; |
991 | glEnable GL_BLEND; |
988 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
992 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
989 | glEnable GL_TEXTURE_2D; |
993 | glEnable GL_TEXTURE_2D; |
990 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
994 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
991 | |
995 | |
992 | glColor @{$self->{fg}}; |
996 | glColor @{$self->{fg}}; |
993 | |
997 | |
994 | my $x = |
998 | my $x = |
995 | $self->{align} < 0 ? $self->{padding} |
999 | $self->{align} < 0 ? $self->{padding} |
996 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1000 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
997 | : ($self->{w} - $tex->{w}) * 0.5; |
1001 | : ($self->{w} - $tex->{w}) * 0.5; |
998 | |
1002 | |
999 | glTranslate int $x, int +($self->{h} - $tex->{h}) * 0.5, 0; |
1003 | $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); |
1000 | $tex->draw_quad (0, 0); |
|
|
1001 | |
1004 | |
1002 | glDisable GL_TEXTURE_2D; |
1005 | glDisable GL_TEXTURE_2D; |
1003 | glDisable GL_BLEND; |
1006 | glDisable GL_BLEND; |
1004 | } |
1007 | } |
1005 | |
1008 | |
… | |
… | |
1033 | |
1036 | |
1034 | $self->{last_activity} = $::NOW; |
1037 | $self->{last_activity} = $::NOW; |
1035 | |
1038 | |
1036 | $self->{text} = $text; |
1039 | $self->{text} = $text; |
1037 | $self->{layout}->set_width ($self->{w}); |
1040 | $self->{layout}->set_width ($self->{w}); |
1038 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); |
1041 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
1039 | |
1042 | |
1040 | $text =~ s/./*/g if $self->{hidden}; |
1043 | $text =~ s/./*/g if $self->{hidden}; |
1041 | |
1044 | |
1042 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
1045 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
1043 | |
1046 | |
… | |
… | |
1424 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1427 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1425 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1428 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1426 | |
1429 | |
1427 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1430 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1428 | |
1431 | |
|
|
1432 | $hi = $value + 1 if $lo == $hi; |
|
|
1433 | |
1429 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
1434 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
1430 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
1435 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
1431 | |
1436 | |
1432 | $page = int $page * $inner_w / ($hi - $lo); |
1437 | $page = int $page * $inner_w / ($hi - $lo); |
1433 | $value = int +($value - $lo) * $inner_w / ($hi - $lo); |
1438 | $value = int +($value - $lo) * $inner_w / ($hi - $lo); |
… | |
… | |
1465 | |
1470 | |
1466 | sub new { |
1471 | sub new { |
1467 | my $class = shift; |
1472 | my $class = shift; |
1468 | |
1473 | |
1469 | my $self = $class->SUPER::new ( |
1474 | my $self = $class->SUPER::new ( |
|
|
1475 | req_w => $::WIDTH / 6, |
|
|
1476 | req_h => $::HEIGHT / 6, |
|
|
1477 | fontsize => $::FONTSIZE, |
|
|
1478 | @_, |
|
|
1479 | |
|
|
1480 | layout => (new CFClient::Layout), |
1470 | par => [], |
1481 | par => [], |
1471 | @_, |
1482 | height => 0, |
1472 | children => [ |
1483 | children => [ |
1473 | (new CFClient::UI::Empty expand => 1), |
1484 | (new CFClient::UI::Empty expand => 1), |
1474 | (new CFClient::UI::Slider vertical => 1), |
1485 | (new CFClient::UI::Slider vertical => 1), |
1475 | ], |
1486 | ], |
1476 | ); |
1487 | ); |
1477 | |
1488 | |
|
|
1489 | $self->{children}[1]->connect (changed => sub { |
|
|
1490 | $self->update; |
|
|
1491 | }); |
|
|
1492 | |
1478 | $self |
1493 | $self |
|
|
1494 | } |
|
|
1495 | |
|
|
1496 | sub set_fontsize { |
|
|
1497 | my ($self, $fontsize) = @_; |
|
|
1498 | |
|
|
1499 | $self->{fontsize} = $fontsize; |
|
|
1500 | $self->reflow; |
|
|
1501 | } |
|
|
1502 | |
|
|
1503 | sub text_height { |
|
|
1504 | my ($self, $text) = @_; |
|
|
1505 | |
|
|
1506 | my $layout = $self->{layout}; |
|
|
1507 | |
|
|
1508 | $layout->set_height ($self->{fontsize}); |
|
|
1509 | $layout->set_width ($self->{w}); |
|
|
1510 | $layout->set_text ($text); |
|
|
1511 | |
|
|
1512 | ($layout->size)[1] |
|
|
1513 | } |
|
|
1514 | |
|
|
1515 | sub reflow { |
|
|
1516 | my ($self) = @_; |
|
|
1517 | |
|
|
1518 | $self->{need_reflow}++; |
|
|
1519 | $self->update; |
|
|
1520 | } |
|
|
1521 | |
|
|
1522 | sub size_request { |
|
|
1523 | my ($self) = @_; |
|
|
1524 | |
|
|
1525 | ($self->{req_w}, $self->{req_h}) |
|
|
1526 | } |
|
|
1527 | |
|
|
1528 | sub size_allocate { |
|
|
1529 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1530 | |
|
|
1531 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
|
|
1532 | |
|
|
1533 | $self->{layout}->set_height ($self->{fontsize}); |
|
|
1534 | $self->{layout}->set_width ($self->{w}); |
|
|
1535 | |
|
|
1536 | $self->reflow; |
1479 | } |
1537 | } |
1480 | |
1538 | |
1481 | sub add_paragraph { |
1539 | sub add_paragraph { |
1482 | my ($self, $color, $text) = @_; |
1540 | my ($self, $color, $text) = @_; |
1483 | |
1541 | |
|
|
1542 | #TODO: intelligently "reformat" paragraph |
|
|
1543 | |
|
|
1544 | my $height = $self->text_height ($text); |
|
|
1545 | |
|
|
1546 | $self->{height} += $height; |
|
|
1547 | |
1484 | push @{$self->{par}}, [$color, $text]; |
1548 | push @{$self->{par}}, [$height, $color, $text]; |
1485 | my $count = scalar @{$self->{par}}; |
1549 | |
1486 | $self->{children}[1]{range} = [$count - 1, 0, $count, 1]; |
1550 | $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; |
1487 | $self->{children}[1]->update; |
1551 | $self->{children}[1]->update; |
1488 | } |
1552 | } |
1489 | |
1553 | |
|
|
1554 | sub update { |
|
|
1555 | my ($self) = @_; |
|
|
1556 | |
|
|
1557 | $self->SUPER::update; |
|
|
1558 | |
|
|
1559 | return unless $self->{h} > 0; |
|
|
1560 | |
|
|
1561 | delete $self->{texture}; |
|
|
1562 | |
|
|
1563 | $TOPLEVEL->on_refresh ($self, sub { |
|
|
1564 | if (delete $self->{need_reflow}) { |
|
|
1565 | my $height = 0; |
|
|
1566 | |
|
|
1567 | $height += $_->[0] = $self->text_height ($_->[2]) |
|
|
1568 | for @{$self->{par}}; |
|
|
1569 | |
|
|
1570 | $self->{height} = $height; |
|
|
1571 | |
|
|
1572 | $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
|
|
1573 | |
|
|
1574 | delete $self->{texture}; |
|
|
1575 | } |
|
|
1576 | |
|
|
1577 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
|
|
1578 | glClearColor 0, 0, 0, 1; |
|
|
1579 | glClear GL_COLOR_BUFFER_BIT; |
|
|
1580 | |
|
|
1581 | glEnable GL_BLEND; |
|
|
1582 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1583 | glEnable GL_TEXTURE_2D; |
|
|
1584 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1585 | |
|
|
1586 | my $top = int $self->{children}[1]{range}[0]; |
|
|
1587 | |
|
|
1588 | my $y0 = $top; |
|
|
1589 | my $y1 = $top + $self->{h}; |
|
|
1590 | |
|
|
1591 | my $y = 0; |
|
|
1592 | |
|
|
1593 | my $layout = $self->{layout}; |
|
|
1594 | |
|
|
1595 | for my $par (@{$self->{par}}) { |
|
|
1596 | my $h = $par->[0]; |
|
|
1597 | |
|
|
1598 | if ($y0 < $y + $h && $y < $y1) { |
|
|
1599 | $layout->set_text ($par->[2]); |
|
|
1600 | |
|
|
1601 | glColor @{ $par->[1] }; |
|
|
1602 | my ($W, $H) = $layout->size; |
|
|
1603 | CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); |
|
|
1604 | } |
|
|
1605 | |
|
|
1606 | $y += $h; |
|
|
1607 | } |
|
|
1608 | |
|
|
1609 | glDisable GL_TEXTURE_2D; |
|
|
1610 | glDisable GL_BLEND; |
|
|
1611 | }; |
|
|
1612 | }); |
|
|
1613 | } |
|
|
1614 | |
1490 | sub _draw { |
1615 | sub _draw { |
1491 | my ($self) = @_; |
1616 | my ($self) = @_; |
1492 | |
1617 | |
1493 | my $par = $self->{par}; |
1618 | if ($self->{texture}) { |
1494 | |
|
|
1495 | my $bottom = (scalar @$par) - 1; |
|
|
1496 | |
|
|
1497 | my $w = $self->{children}[0]{w}; |
|
|
1498 | my $y = $self->{h}; |
|
|
1499 | |
|
|
1500 | glEnable GL_BLEND; |
|
|
1501 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1502 | glEnable GL_TEXTURE_2D; |
1619 | glEnable GL_TEXTURE_2D; |
1503 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1620 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1504 | |
1621 | $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); |
1505 | # TODO: everything! |
|
|
1506 | while ($y > 0 && $bottom >= 0) { |
|
|
1507 | my $par = $par->[$bottom--]; |
|
|
1508 | |
|
|
1509 | my $layout = new CFClient::Layout; |
|
|
1510 | |
|
|
1511 | $layout->set_height ($::FONTSIZE); |
|
|
1512 | $layout->set_width ($w); |
|
|
1513 | $layout->set_text ($par->[1]); |
|
|
1514 | my $tex = new_from_layout CFClient::Texture $layout; |
|
|
1515 | $y -= $tex->{h}; |
|
|
1516 | |
|
|
1517 | glColor @{ $par->[0] }; |
|
|
1518 | $tex->draw_quad (0, $y); |
|
|
1519 | } |
|
|
1520 | |
|
|
1521 | glDisable GL_TEXTURE_2D; |
1622 | glDisable GL_TEXTURE_2D; |
1522 | glDisable GL_BLEND; |
1623 | } |
|
|
1624 | |
|
|
1625 | $self->{children}[1]->draw; |
|
|
1626 | |
1523 | } |
1627 | } |
1524 | |
1628 | |
1525 | ############################################################################# |
1629 | ############################################################################# |
1526 | |
1630 | |
1527 | package CFClient::UI::MapWidget; |
1631 | package CFClient::UI::MapWidget; |
… | |
… | |
1662 | $::CONN->user_send ("command stay fire"); |
1766 | $::CONN->user_send ("command stay fire"); |
1663 | } elsif ($sym == SDLK_a) { |
1767 | } elsif ($sym == SDLK_a) { |
1664 | $::CONN->user_send ("command apply"); |
1768 | $::CONN->user_send ("command apply"); |
1665 | } elsif ($sym == SDLK_QUOTE) { |
1769 | } elsif ($sym == SDLK_QUOTE) { |
1666 | $self->emit ('activate_console'); |
1770 | $self->emit ('activate_console'); |
|
|
1771 | } elsif ($sym == SDLK_SLASH) { |
|
|
1772 | $self->emit ('activate_console' => '/'); |
1667 | } elsif (exists $DIR{$sym}) { |
1773 | } elsif (exists $DIR{$sym}) { |
1668 | if ($mod & KMOD_SHIFT) { |
1774 | if ($mod & KMOD_SHIFT) { |
1669 | $self->{shft}++; |
1775 | $self->{shft}++; |
1670 | $::CONN->user_send ("command fire $DIR{$sym}[0]"); |
1776 | $::CONN->user_send ("command fire $DIR{$sym}[0]"); |
1671 | } elsif ($mod & KMOD_CTRL) { |
1777 | } elsif ($mod & KMOD_CTRL) { |
… | |
… | |
1775 | |
1881 | |
1776 | package CFClient::UI::Toplevel; |
1882 | package CFClient::UI::Toplevel; |
1777 | |
1883 | |
1778 | our @ISA = CFClient::UI::Container::; |
1884 | our @ISA = CFClient::UI::Container::; |
1779 | |
1885 | |
|
|
1886 | use SDL::OpenGL; |
|
|
1887 | |
1780 | sub size_request { |
1888 | sub size_request { |
1781 | ($::WIDTH, $::HEIGHT) |
1889 | ($::WIDTH, $::HEIGHT) |
1782 | } |
1890 | } |
1783 | |
1891 | |
1784 | sub size_allocate { |
1892 | sub size_allocate { |
… | |
… | |
1809 | $self->SUPER::add ($widget); |
1917 | $self->SUPER::add ($widget); |
1810 | |
1918 | |
1811 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
1919 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
1812 | } |
1920 | } |
1813 | |
1921 | |
|
|
1922 | sub on_refresh { |
|
|
1923 | my ($self, $id, $cb) = @_; |
|
|
1924 | |
|
|
1925 | $self->{refresh_hook}{$id} = $cb; |
|
|
1926 | } |
|
|
1927 | |
1814 | sub draw { |
1928 | sub draw { |
1815 | my ($self) = @_; |
1929 | my ($self) = @_; |
1816 | |
1930 | |
|
|
1931 | while (my $rcb = delete $self->{refresh_hook}) { |
|
|
1932 | $_->() for values %$rcb; |
|
|
1933 | } |
|
|
1934 | |
|
|
1935 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
|
|
1936 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1937 | glClear GL_COLOR_BUFFER_BIT; |
|
|
1938 | |
|
|
1939 | glMatrixMode GL_PROJECTION; |
|
|
1940 | glLoadIdentity; |
|
|
1941 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; |
|
|
1942 | glMatrixMode GL_MODELVIEW; |
|
|
1943 | glLoadIdentity; |
|
|
1944 | |
1817 | $self->_draw; |
1945 | $self->_draw; |
1818 | } |
1946 | } |
1819 | |
1947 | |
1820 | ############################################################################# |
1948 | ############################################################################# |
1821 | |
1949 | |