… | |
… | |
66 | $hover->update if $hover && $hover->{can_hover}; |
66 | $hover->update if $hover && $hover->{can_hover}; |
67 | $HOVER->update if $HOVER && $HOVER->{can_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 | } |
|
|
72 | |
|
|
73 | # convert position array to integers |
|
|
74 | sub harmonize { |
|
|
75 | my ($vals) = @_; |
|
|
76 | |
|
|
77 | my $rem = 0; |
|
|
78 | |
|
|
79 | for ($vals) { |
|
|
80 | my $i = int $_ + $rem; |
|
|
81 | $rem += $_ - $i; |
|
|
82 | $_ = $i; |
|
|
83 | } |
71 | } |
84 | } |
72 | |
85 | |
73 | ############################################################################# |
86 | ############################################################################# |
74 | |
87 | |
75 | package CFClient::UI::Base; |
88 | package CFClient::UI::Base; |
… | |
… | |
733 | |
746 | |
734 | # linearly scale sizes |
747 | # linearly scale sizes |
735 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
748 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
736 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
749 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
737 | |
750 | |
738 | $_ = int $_ for @$ws; #TODO: avoid rounding problems |
751 | CFClient::UI::harmonize $ws; |
739 | $_ = int $_ for @$hs; #TODO: avoid rounding problems |
752 | CFClient::UI::harmonize $hs; |
740 | |
753 | |
741 | my $y; |
754 | my $y; |
742 | |
755 | |
743 | for my $r (0 .. $#{$self->{children}}) { |
756 | for my $r (0 .. $#{$self->{children}}) { |
744 | my $row = $self->{children}[$r] |
757 | my $row = $self->{children}[$r] |
… | |
… | |
832 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
845 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
833 | $h[$_] = $alloc_h; |
846 | $h[$_] = $alloc_h; |
834 | } |
847 | } |
835 | } |
848 | } |
836 | |
849 | |
|
|
850 | CFClient::UI::harmonize \@h; |
|
|
851 | |
837 | my $y = 0; |
852 | my $y = 0; |
838 | for (0 .. $#$children) { |
853 | for (0 .. $#$children) { |
839 | my $child = $children->[$_]; |
854 | my $child = $children->[$_]; |
840 | my $h = int $h[$_]; |
855 | my $h = $h[$_]; |
841 | $child->size_allocate ($y, 0, $h, $w); |
856 | $child->size_allocate ($y, 0, $h, $w); |
842 | |
857 | |
843 | $y += $h; |
858 | $y += $h; |
844 | } |
859 | } |
845 | } |
860 | } |
… | |
… | |
886 | |
901 | |
887 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
902 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
888 | } |
903 | } |
889 | } |
904 | } |
890 | |
905 | |
|
|
906 | CFClient::UI::harmonize \@h; |
|
|
907 | |
891 | my $y = 0; |
908 | my $y = 0; |
892 | for (0 .. $#$children) { |
909 | for (0 .. $#$children) { |
893 | my $child = $children->[$_]; |
910 | my $child = $children->[$_]; |
894 | my $h = int $h[$_]; |
911 | my $h = $h[$_]; |
895 | $child->size_allocate (0, $y, $w, $h); |
912 | $child->size_allocate (0, $y, $w, $h); |
896 | |
913 | |
897 | $y += $h; |
914 | $y += $h; |
898 | } |
915 | } |
899 | } |
916 | } |
… | |
… | |
1427 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1444 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1428 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1445 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1429 | |
1446 | |
1430 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1447 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1431 | |
1448 | |
|
|
1449 | $hi = $value + 1 if $lo == $hi; |
|
|
1450 | |
1432 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
1451 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
1433 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
1452 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
1434 | |
1453 | |
1435 | $page = int $page * $inner_w / ($hi - $lo); |
1454 | $page = int $page * $inner_w / ($hi - $lo); |
1436 | $value = int +($value - $lo) * $inner_w / ($hi - $lo); |
1455 | $value = int +($value - $lo) * $inner_w / ($hi - $lo); |
… | |
… | |
1482 | (new CFClient::UI::Empty expand => 1), |
1501 | (new CFClient::UI::Empty expand => 1), |
1483 | (new CFClient::UI::Slider vertical => 1), |
1502 | (new CFClient::UI::Slider vertical => 1), |
1484 | ], |
1503 | ], |
1485 | ); |
1504 | ); |
1486 | |
1505 | |
|
|
1506 | $self->{children}[1]->connect (changed => sub { |
|
|
1507 | $self->update; |
|
|
1508 | }); |
|
|
1509 | |
1487 | $self |
1510 | $self |
|
|
1511 | } |
|
|
1512 | |
|
|
1513 | sub set_fontsize { |
|
|
1514 | my ($self, $fontsize) = @_; |
|
|
1515 | |
|
|
1516 | $self->{fontsize} = $fontsize; |
|
|
1517 | $self->reflow; |
1488 | } |
1518 | } |
1489 | |
1519 | |
1490 | sub text_height { |
1520 | sub text_height { |
1491 | my ($self, $text) = @_; |
1521 | my ($self, $text) = @_; |
1492 | |
1522 | |
… | |
… | |
1500 | } |
1530 | } |
1501 | |
1531 | |
1502 | sub reflow { |
1532 | sub reflow { |
1503 | my ($self) = @_; |
1533 | my ($self) = @_; |
1504 | |
1534 | |
1505 | my $height = 0; |
1535 | $self->{need_reflow}++; |
1506 | |
1536 | $self->update; |
1507 | for (@{$self->{par}}) { |
|
|
1508 | $height += $_->[0] = $self->text_height ($_->[2]); |
|
|
1509 | } |
|
|
1510 | |
|
|
1511 | $self->{height} = $height; |
|
|
1512 | |
|
|
1513 | $self->{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
|
|
1514 | } |
1537 | } |
1515 | |
1538 | |
1516 | sub size_request { |
1539 | sub size_request { |
1517 | my ($self) = @_; |
1540 | my ($self) = @_; |
1518 | |
1541 | |
… | |
… | |
1526 | |
1549 | |
1527 | $self->{layout}->set_height ($self->{fontsize}); |
1550 | $self->{layout}->set_height ($self->{fontsize}); |
1528 | $self->{layout}->set_width ($self->{w}); |
1551 | $self->{layout}->set_width ($self->{w}); |
1529 | |
1552 | |
1530 | $self->reflow; |
1553 | $self->reflow; |
1531 | $self->update; |
|
|
1532 | } |
1554 | } |
1533 | |
1555 | |
1534 | sub add_paragraph { |
1556 | sub add_paragraph { |
1535 | my ($self, $color, $text) = @_; |
1557 | my ($self, $color, $text) = @_; |
1536 | |
1558 | |
… | |
… | |
1551 | |
1573 | |
1552 | $self->SUPER::update; |
1574 | $self->SUPER::update; |
1553 | |
1575 | |
1554 | return unless $self->{h} > 0; |
1576 | return unless $self->{h} > 0; |
1555 | |
1577 | |
|
|
1578 | delete $self->{texture}; |
|
|
1579 | |
|
|
1580 | $TOPLEVEL->on_refresh ($self, sub { |
|
|
1581 | if (delete $self->{need_reflow}) { |
|
|
1582 | my $height = 0; |
|
|
1583 | |
|
|
1584 | $height += $_->[0] = $self->text_height ($_->[2]) |
|
|
1585 | for @{$self->{par}}; |
|
|
1586 | |
|
|
1587 | $self->{height} = $height; |
|
|
1588 | |
|
|
1589 | $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
|
|
1590 | |
|
|
1591 | delete $self->{texture}; |
|
|
1592 | } |
|
|
1593 | |
1556 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
1594 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
1557 | glClearColor 0, 0, 0, 1; |
1595 | glClearColor 0, 0, 0, 1; |
1558 | glClear GL_COLOR_BUFFER_BIT; |
1596 | glClear GL_COLOR_BUFFER_BIT; |
1559 | |
1597 | |
1560 | my $y0 = $self->{height} - $self->{h}; |
|
|
1561 | my $y1 = $self->{height}; |
|
|
1562 | |
|
|
1563 | glEnable GL_BLEND; |
1598 | glEnable GL_BLEND; |
1564 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1599 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1565 | glEnable GL_TEXTURE_2D; |
1600 | glEnable GL_TEXTURE_2D; |
1566 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1601 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1567 | |
1602 | |
|
|
1603 | my $top = int $self->{children}[1]{range}[0]; |
|
|
1604 | |
|
|
1605 | my $y0 = $top; |
|
|
1606 | my $y1 = $top + $self->{h}; |
|
|
1607 | |
1568 | my $y = 0; |
1608 | my $y = 0; |
1569 | |
1609 | |
1570 | my $layout = $self->{layout}; |
1610 | my $layout = $self->{layout}; |
1571 | |
1611 | |
1572 | for my $par (@{$self->{par}}) { |
1612 | for my $par (@{$self->{par}}) { |
1573 | my $h = $par->[0]; |
1613 | my $h = $par->[0]; |
1574 | |
1614 | |
1575 | if ($y0 < $y + $h && $y < $y1) { |
1615 | if ($y0 < $y + $h && $y < $y1) { |
1576 | $layout->set_text ($par->[2]); |
1616 | $layout->set_text ($par->[2]); |
1577 | |
1617 | |
1578 | glColor @{ $par->[1] }; |
1618 | glColor @{ $par->[1] }; |
1579 | my ($W, $H) = $layout->size; |
1619 | my ($W, $H) = $layout->size; |
1580 | CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); |
1620 | CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); |
|
|
1621 | } |
|
|
1622 | |
|
|
1623 | $y += $h; |
1581 | } |
1624 | } |
1582 | |
1625 | |
1583 | $y += $h; |
|
|
1584 | } |
|
|
1585 | |
|
|
1586 | glDisable GL_TEXTURE_2D; |
1626 | glDisable GL_TEXTURE_2D; |
1587 | glDisable GL_BLEND; |
1627 | glDisable GL_BLEND; |
|
|
1628 | }; |
1588 | }; |
1629 | }); |
1589 | } |
1630 | } |
1590 | |
1631 | |
1591 | sub _draw { |
1632 | sub _draw { |
1592 | my ($self) = @_; |
1633 | my ($self) = @_; |
1593 | |
1634 | |
… | |
… | |
1631 | } |
1672 | } |
1632 | |
1673 | |
1633 | sub key_up { |
1674 | sub key_up { |
1634 | } |
1675 | } |
1635 | |
1676 | |
|
|
1677 | sub button_down { |
|
|
1678 | my ($self, $ev, $x, $y) = @_; |
|
|
1679 | |
|
|
1680 | $self->focus_in; |
|
|
1681 | |
|
|
1682 | if ($ev->button == 2) { |
|
|
1683 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
1684 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
|
|
1685 | |
|
|
1686 | $self->{motion} = sub { |
|
|
1687 | my ($ev, $x, $y) = @_; |
|
|
1688 | |
|
|
1689 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
1690 | |
|
|
1691 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
|
|
1692 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
|
|
1693 | |
|
|
1694 | $self->update; |
|
|
1695 | }; |
|
|
1696 | } |
|
|
1697 | } |
|
|
1698 | |
|
|
1699 | sub button_up { |
|
|
1700 | my ($self, $ev, $x, $y) = @_; |
|
|
1701 | |
|
|
1702 | delete $self->{motion}; |
|
|
1703 | } |
|
|
1704 | |
|
|
1705 | sub mouse_motion { |
|
|
1706 | my ($self, $ev, $x, $y) = @_; |
|
|
1707 | |
|
|
1708 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
|
|
1709 | } |
|
|
1710 | |
1636 | sub size_request { |
1711 | sub size_request { |
1637 | ( |
1712 | ( |
1638 | 1 + 32 * int $::WIDTH / 32, |
1713 | 1 + 32 * int $::WIDTH / 32, |
1639 | 1 + 32 * int $::HEIGHT / 32, |
1714 | 1 + 32 * int $::HEIGHT / 32, |
1640 | ) |
1715 | ) |
… | |
… | |
1648 | } |
1723 | } |
1649 | |
1724 | |
1650 | sub draw { |
1725 | sub draw { |
1651 | my ($self) = @_; |
1726 | my ($self) = @_; |
1652 | |
1727 | |
1653 | $self->{need_update}++;#d# |
|
|
1654 | if (delete $self->{need_update}) { |
1728 | if (delete $self->{need_update}) { |
1655 | glNewList $self->{list}, GL_COMPILE; |
1729 | glNewList $self->{list}, GL_COMPILE; |
1656 | |
1730 | |
1657 | my $sw = int $::WIDTH / 32; |
|
|
1658 | my $sh = int $::HEIGHT / 32; |
|
|
1659 | |
|
|
1660 | if ($::MAP) { |
1731 | if ($::MAP) { |
|
|
1732 | my $sw = int $::WIDTH / 32; |
|
|
1733 | my $sh = int $::HEIGHT / 32; |
|
|
1734 | |
|
|
1735 | my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
|
|
1736 | my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
|
|
1737 | |
|
|
1738 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
|
|
1739 | |
1661 | my ($w, $h, $data) = $::MAP->draw (0, 0, $sw, $sh); |
1740 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
1662 | |
1741 | |
1663 | if ($::CFG->{fow_enable}) { |
1742 | if ($::CFG->{fow_enable}) { |
1664 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
1743 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
1665 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
1744 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
1666 | glConvolutionFilter2D |
1745 | glConvolutionFilter2D |
… | |
… | |
1684 | format => GL_ALPHA; |
1763 | format => GL_ALPHA; |
1685 | |
1764 | |
1686 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
1765 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
1687 | |
1766 | |
1688 | glEnable GL_BLEND; |
1767 | glEnable GL_BLEND; |
|
|
1768 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1689 | glEnable GL_TEXTURE_2D; |
1769 | glEnable GL_TEXTURE_2D; |
1690 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1770 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1691 | |
1771 | |
1692 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
1772 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
1693 | $tex->draw_quad (0, 0, $w * 32, $h * 32); |
1773 | $tex->draw_quad (0, 0, $w * 32, $h * 32); |
1694 | |
1774 | |
1695 | glDisable GL_TEXTURE_2D; |
1775 | glDisable GL_TEXTURE_2D; |
1696 | glDisable GL_BLEND; |
1776 | glDisable GL_BLEND; |
1697 | } |
1777 | } |
|
|
1778 | |
|
|
1779 | # HACK BEGIN |
|
|
1780 | { |
|
|
1781 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
1782 | my ($w, $h) = (250, 250); |
|
|
1783 | |
|
|
1784 | glEnable GL_BLEND; |
|
|
1785 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1786 | glEnable GL_TEXTURE_2D; |
|
|
1787 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1788 | |
|
|
1789 | CFClient::Texture->new ( |
|
|
1790 | w => $w, |
|
|
1791 | h => $h, |
|
|
1792 | data => $::MAP->mapmap ($w, $h), |
|
|
1793 | type => GL_UNSIGNED_INT_8_8_8_8_REV |
|
|
1794 | )->draw_quad (100, 100); |
|
|
1795 | |
|
|
1796 | glDisable GL_TEXTURE_2D; |
|
|
1797 | glDisable GL_BLEND; |
|
|
1798 | } |
|
|
1799 | # HACK END |
1698 | } |
1800 | } |
1699 | |
1801 | |
1700 | glEndList; |
1802 | glEndList; |
1701 | } |
1803 | } |
1702 | |
1804 | |
|
|
1805 | glPushMatrix; |
1703 | glCallList $self->{list}; |
1806 | glCallList $self->{list}; |
|
|
1807 | glPopMatrix; |
1704 | |
1808 | |
1705 | if ($FOCUS != $self) { |
1809 | if ($FOCUS != $self) { |
1706 | glColor 64/255, 64/255, 64/255; |
1810 | glColor 64/255, 64/255, 64/255; |
1707 | glLogicOp GL_AND; |
1811 | glLogicOp GL_AND; |
1708 | glEnable GL_COLOR_LOGIC_OP; |
1812 | glEnable GL_COLOR_LOGIC_OP; |
… | |
… | |
1857 | |
1961 | |
1858 | package CFClient::UI::Toplevel; |
1962 | package CFClient::UI::Toplevel; |
1859 | |
1963 | |
1860 | our @ISA = CFClient::UI::Container::; |
1964 | our @ISA = CFClient::UI::Container::; |
1861 | |
1965 | |
|
|
1966 | use SDL::OpenGL; |
|
|
1967 | |
1862 | sub size_request { |
1968 | sub size_request { |
1863 | ($::WIDTH, $::HEIGHT) |
1969 | ($::WIDTH, $::HEIGHT) |
1864 | } |
1970 | } |
1865 | |
1971 | |
1866 | sub size_allocate { |
1972 | sub size_allocate { |
… | |
… | |
1891 | $self->SUPER::add ($widget); |
1997 | $self->SUPER::add ($widget); |
1892 | |
1998 | |
1893 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
1999 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
1894 | } |
2000 | } |
1895 | |
2001 | |
|
|
2002 | sub on_refresh { |
|
|
2003 | my ($self, $id, $cb) = @_; |
|
|
2004 | |
|
|
2005 | $self->{refresh_hook}{$id} = $cb; |
|
|
2006 | } |
|
|
2007 | |
1896 | sub draw { |
2008 | sub draw { |
1897 | my ($self) = @_; |
2009 | my ($self) = @_; |
1898 | |
2010 | |
|
|
2011 | while (my $rcb = delete $self->{refresh_hook}) { |
|
|
2012 | $_->() for values %$rcb; |
|
|
2013 | } |
|
|
2014 | |
|
|
2015 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
|
|
2016 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
2017 | glClear GL_COLOR_BUFFER_BIT; |
|
|
2018 | |
|
|
2019 | glMatrixMode GL_PROJECTION; |
|
|
2020 | glLoadIdentity; |
|
|
2021 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; |
|
|
2022 | glMatrixMode GL_MODELVIEW; |
|
|
2023 | glLoadIdentity; |
|
|
2024 | |
1899 | $self->_draw; |
2025 | $self->_draw; |
1900 | } |
2026 | } |
1901 | |
2027 | |
1902 | ############################################################################# |
2028 | ############################################################################# |
1903 | |
2029 | |