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.105 by root, Fri Apr 14 14:55:28 2006 UTC vs.
Revision 1.112 by root, Sat Apr 15 12:29:46 2006 UTC

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
74sub 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
75package CFClient::UI::Base; 88package 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
751 CFClient::UI::harmonize $ws;
752 CFClient::UI::harmonize $hs;
753
738 my $y; 754 my $y;
739 755
740 for my $r (0 .. $#{$self->{children}}) { 756 for my $r (0 .. $#{$self->{children}}) {
741 my $row = $self->{children}[$r] 757 my $row = $self->{children}[$r]
742 or next; 758 or next;
829 $alloc_h += ($h - $req_h) * $child->{expand} / $exp; 845 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
830 $h[$_] = $alloc_h; 846 $h[$_] = $alloc_h;
831 } 847 }
832 } 848 }
833 849
850 CFClient::UI::harmonize \@h;
851
834 my $y = 0; 852 my $y = 0;
835 for (0 .. $#$children) { 853 for (0 .. $#$children) {
836 my $child = $children->[$_]; 854 my $child = $children->[$_];
837 my $h = int $h[$_]; 855 my $h = $h[$_];
838 $child->size_allocate ($y, 0, $h, $w); 856 $child->size_allocate ($y, 0, $h, $w);
839 857
840 $y += $h; 858 $y += $h;
841 } 859 }
842} 860}
883 901
884 $h[$_] += ($h - $req_h) * $child->{expand} / $exp; 902 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
885 } 903 }
886 } 904 }
887 905
906 CFClient::UI::harmonize \@h;
907
888 my $y = 0; 908 my $y = 0;
889 for (0 .. $#$children) { 909 for (0 .. $#$children) {
890 my $child = $children->[$_]; 910 my $child = $children->[$_];
891 my $h = int $h[$_]; 911 my $h = $h[$_];
892 $child->size_allocate (0, $y, $w, $h); 912 $child->size_allocate (0, $y, $w, $h);
893 913
894 $y += $h; 914 $y += $h;
895 } 915 }
896} 916}
1424 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1444 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1425 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1445 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1426 1446
1427 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1447 my ($value, $lo, $hi, $page) = @{$self->{range}};
1428 1448
1449 $hi = $value + 1 if $lo == $hi;
1450
1429 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 1451 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1430 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
1431 1453
1432 $page = int $page * $inner_w / ($hi - $lo); 1454 $page = int $page * $inner_w / ($hi - $lo);
1433 $value = int +($value - $lo) * $inner_w / ($hi - $lo); 1455 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1479 (new CFClient::UI::Empty expand => 1), 1501 (new CFClient::UI::Empty expand => 1),
1480 (new CFClient::UI::Slider vertical => 1), 1502 (new CFClient::UI::Slider vertical => 1),
1481 ], 1503 ],
1482 ); 1504 );
1483 1505
1506 $self->{children}[1]->connect (changed => sub {
1507 $self->update;
1508 });
1509
1484 $self 1510 $self
1511}
1512
1513sub set_fontsize {
1514 my ($self, $fontsize) = @_;
1515
1516 $self->{fontsize} = $fontsize;
1517 $self->reflow;
1485} 1518}
1486 1519
1487sub text_height { 1520sub text_height {
1488 my ($self, $text) = @_; 1521 my ($self, $text) = @_;
1489 1522
1497} 1530}
1498 1531
1499sub reflow { 1532sub reflow {
1500 my ($self) = @_; 1533 my ($self) = @_;
1501 1534
1502 my $height = 0; 1535 $self->{need_reflow}++;
1503 1536 $self->update;
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} 1537}
1512 1538
1513sub size_request { 1539sub size_request {
1514 my ($self) = @_; 1540 my ($self) = @_;
1515 1541
1523 1549
1524 $self->{layout}->set_height ($self->{fontsize}); 1550 $self->{layout}->set_height ($self->{fontsize});
1525 $self->{layout}->set_width ($self->{w}); 1551 $self->{layout}->set_width ($self->{w});
1526 1552
1527 $self->reflow; 1553 $self->reflow;
1528 $self->update;
1529} 1554}
1530 1555
1531sub add_paragraph { 1556sub add_paragraph {
1532 my ($self, $color, $text) = @_; 1557 my ($self, $color, $text) = @_;
1533 1558
1548 1573
1549 $self->SUPER::update; 1574 $self->SUPER::update;
1550 1575
1551 return unless $self->{h} > 0; 1576 return unless $self->{h} > 0;
1552 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
1553 $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 {
1554 glClearColor 0, 0, 0, 1; 1595 glClearColor 0, 0, 0, 1;
1555 glClear GL_COLOR_BUFFER_BIT; 1596 glClear GL_COLOR_BUFFER_BIT;
1556 1597
1557 my $y0 = $self->{height} - $self->{h};
1558 my $y1 = $self->{height};
1559
1560 glEnable GL_BLEND; 1598 glEnable GL_BLEND;
1561 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1599 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1562 glEnable GL_TEXTURE_2D; 1600 glEnable GL_TEXTURE_2D;
1563 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1601 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1564 1602
1603 my $top = int $self->{children}[1]{range}[0];
1604
1605 my $y0 = $top;
1606 my $y1 = $top + $self->{h};
1607
1565 my $y = 0; 1608 my $y = 0;
1566 1609
1567 my $layout = $self->{layout}; 1610 my $layout = $self->{layout};
1568 1611
1569 for my $par (@{$self->{par}}) { 1612 for my $par (@{$self->{par}}) {
1570 my $h = $par->[0]; 1613 my $h = $par->[0];
1571 1614
1572 if ($y0 < $y + $h && $y < $y1) { 1615 if ($y0 < $y + $h && $y < $y1) {
1573 $layout->set_text ($par->[2]); 1616 $layout->set_text ($par->[2]);
1574 1617
1575 glColor @{ $par->[1] }; 1618 glColor @{ $par->[1] };
1576 my ($W, $H) = $layout->size; 1619 my ($W, $H) = $layout->size;
1577 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;
1578 } 1624 }
1579 1625
1580 $y += $h;
1581 }
1582
1583 glDisable GL_TEXTURE_2D; 1626 glDisable GL_TEXTURE_2D;
1584 glDisable GL_BLEND; 1627 glDisable GL_BLEND;
1628 };
1585 }; 1629 });
1586} 1630}
1587 1631
1588sub _draw { 1632sub _draw {
1589 my ($self) = @_; 1633 my ($self) = @_;
1590 1634
1592 glEnable GL_TEXTURE_2D; 1636 glEnable GL_TEXTURE_2D;
1593 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1637 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1594 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 1638 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1595 glDisable GL_TEXTURE_2D; 1639 glDisable GL_TEXTURE_2D;
1596 } 1640 }
1641
1642 $self->{children}[1]->draw;
1597 1643
1598} 1644}
1599 1645
1600############################################################################# 1646#############################################################################
1601 1647
1626} 1672}
1627 1673
1628sub key_up { 1674sub key_up {
1629} 1675}
1630 1676
1677sub 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
1699sub button_up {
1700 my ($self, $ev, $x, $y) = @_;
1701
1702 delete $self->{motion};
1703}
1704
1705sub mouse_motion {
1706 my ($self, $ev, $x, $y) = @_;
1707
1708 $self->{motion}->($ev, $x, $y) if $self->{motion};
1709}
1710
1631sub size_request { 1711sub size_request {
1632 ( 1712 (
1633 1 + 32 * int $::WIDTH / 32, 1713 1 + 32 * int $::WIDTH / 32,
1634 1 + 32 * int $::HEIGHT / 32, 1714 1 + 32 * int $::HEIGHT / 32,
1635 ) 1715 )
1643} 1723}
1644 1724
1645sub draw { 1725sub draw {
1646 my ($self) = @_; 1726 my ($self) = @_;
1647 1727
1648 $self->{need_update}++;#d#
1649 if (delete $self->{need_update}) { 1728 if (delete $self->{need_update}) {
1650 glNewList $self->{list}, GL_COMPILE; 1729 glNewList $self->{list}, GL_COMPILE;
1651 1730
1652 my $sw = int $::WIDTH / 32;
1653 my $sh = int $::HEIGHT / 32;
1654
1655 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
1656 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);
1657 1741
1658 if ($::CFG->{fow_enable}) { 1742 if ($::CFG->{fow_enable}) {
1659 if ($::CFG->{fow_smooth}) { # smooth fog of war 1743 if ($::CFG->{fow_smooth}) { # smooth fog of war
1660 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; 1744 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1661 glConvolutionFilter2D 1745 glConvolutionFilter2D
1679 format => GL_ALPHA; 1763 format => GL_ALPHA;
1680 1764
1681 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; 1765 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1682 1766
1683 glEnable GL_BLEND; 1767 glEnable GL_BLEND;
1768 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1684 glEnable GL_TEXTURE_2D; 1769 glEnable GL_TEXTURE_2D;
1685 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1770 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1686 1771
1687 glColor +($::CFG->{fow_intensity}) x 3, 1; 1772 glColor +($::CFG->{fow_intensity}) x 3, 1;
1688 $tex->draw_quad (0, 0, $w * 32, $h * 32); 1773 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1689 1774
1690 glDisable GL_TEXTURE_2D; 1775 glDisable GL_TEXTURE_2D;
1691 glDisable GL_BLEND; 1776 glDisable GL_BLEND;
1692 } 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
1693 } 1800 }
1694 1801
1695 glEndList; 1802 glEndList;
1696 } 1803 }
1697 1804
1805 glPushMatrix;
1698 glCallList $self->{list}; 1806 glCallList $self->{list};
1807 glPopMatrix;
1699 1808
1700 if ($FOCUS != $self) { 1809 if ($FOCUS != $self) {
1701 glColor 64/255, 64/255, 64/255; 1810 glColor 64/255, 64/255, 64/255;
1702 glLogicOp GL_AND; 1811 glLogicOp GL_AND;
1703 glEnable GL_COLOR_LOGIC_OP; 1812 glEnable GL_COLOR_LOGIC_OP;
1852 1961
1853package CFClient::UI::Toplevel; 1962package CFClient::UI::Toplevel;
1854 1963
1855our @ISA = CFClient::UI::Container::; 1964our @ISA = CFClient::UI::Container::;
1856 1965
1966use SDL::OpenGL;
1967
1857sub size_request { 1968sub size_request {
1858 ($::WIDTH, $::HEIGHT) 1969 ($::WIDTH, $::HEIGHT)
1859} 1970}
1860 1971
1861sub size_allocate { 1972sub size_allocate {
1886 $self->SUPER::add ($widget); 1997 $self->SUPER::add ($widget);
1887 1998
1888 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); 1999 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1889} 2000}
1890 2001
2002sub on_refresh {
2003 my ($self, $id, $cb) = @_;
2004
2005 $self->{refresh_hook}{$id} = $cb;
2006}
2007
1891sub draw { 2008sub draw {
1892 my ($self) = @_; 2009 my ($self) = @_;
1893 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
1894 $self->_draw; 2025 $self->_draw;
1895} 2026}
1896 2027
1897############################################################################# 2028#############################################################################
1898 2029

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines