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.401 by root, Sat Jul 21 20:05:37 2007 UTC vs.
Revision 1.413 by root, Mon Jul 23 15:30:45 2007 UTC

593 593
594# using global variables seems a bit hacky, but passing through all drawing 594# using global variables seems a bit hacky, but passing through all drawing
595# functions seems pointless. 595# functions seems pointless.
596our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn 596our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
597 597
598sub draw { 598#sub draw {
599 my ($self) = @_; 599#CFPlus.xs
600
601 return unless $self->{h} && $self->{w};
602
603 # update screen rectangle
604 local $draw_x = $draw_x + $self->{x};
605 local $draw_y = $draw_y + $self->{y};
606
607 # skip widgets that are entirely outside the drawing area
608 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
609 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
610
611 glPushMatrix;
612 glTranslate $self->{x}, $self->{y}, 0;
613
614 if ($self == $HOVER && $self->{can_hover}) {
615 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
616 glEnable GL_BLEND;
617 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
618 glBegin GL_QUADS;
619 glVertex 0 , 0;
620 glVertex $self->{w}, 0;
621 glVertex $self->{w}, $self->{h};
622 glVertex 0 , $self->{h};
623 glEnd;
624 glDisable GL_BLEND;
625 }
626
627 if ($ENV{CFPLUS_DEBUG} & 1) {
628 glPushMatrix;
629 glColor 1, 1, 0, 1;
630 glTranslate 0.375, 0.375;
631 glBegin GL_LINE_LOOP;
632 glVertex 0 , 0;
633 glVertex $self->{w} - 1, 0;
634 glVertex $self->{w} - 1, $self->{h} - 1;
635 glVertex 0 , $self->{h} - 1;
636 glEnd;
637 glPopMatrix;
638 #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
639 }
640
641 $self->_draw;
642 glPopMatrix;
643}
644 600
645sub _draw { 601sub _draw {
646 my ($self) = @_; 602 my ($self) = @_;
647 603
648 warn "no draw defined for $self\n"; 604 warn "no draw defined for $self\n";
769 @{$self->{children}}, @widgets 725 @{$self->{children}}, @widgets
770 ]; 726 ];
771 727
772 $self->realloc; 728 $self->realloc;
773 729
730 $self->emit (c_add => \@widgets);
731
774 map $_+0, @widgets 732 map $_+0, @widgets
775} 733}
776 734
777sub children { 735sub children {
778 @{ $_[0]{children} } 736 @{ $_[0]{children} }
779} 737}
780 738
781sub remove { 739sub remove {
782 my ($self, $child) = @_; 740 my ($self, @widgets) = @_;
783 741
742 $self->emit (c_remove => \@widgets);
743
744 for my $child (@widgets) {
784 delete $child->{parent}; 745 delete $child->{parent};
785 $child->hide; 746 $child->hide;
786
787 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 747 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
748 }
788 749
789 $self->realloc; 750 $self->realloc;
790} 751}
791 752
792sub clear { 753sub clear {
820} 781}
821 782
822sub _draw { 783sub _draw {
823 my ($self) = @_; 784 my ($self) = @_;
824 785
825 $_->draw for @{$self->{children}}; 786 $_->draw for $self->visible_children;
826} 787}
827 788
828############################################################################# 789#############################################################################
829 790
830package CFPlus::UI::Bin; 791package CFPlus::UI::Bin;
840} 801}
841 802
842sub add { 803sub add {
843 my ($self, $child) = @_; 804 my ($self, $child) = @_;
844 805
845 $self->SUPER::remove ($_) for @{ $self->{children} }; 806 $self->clear;
846 $self->SUPER::add ($child); 807 $self->SUPER::add ($child);
847} 808}
848 809
849sub remove { 810sub remove {
850 my ($self, $widget) = @_; 811 my ($self, $widget) = @_;
1730 1691
1731use List::Util qw(min max); 1692use List::Util qw(min max);
1732 1693
1733our @ISA = CFPlus::UI::Container::; 1694our @ISA = CFPlus::UI::Container::;
1734 1695
1735sub add_fixed {
1736 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1737
1738 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1739 $self->SUPER::add ($child);
1740}
1741
1742sub _scale($$$) { 1696sub _scale($$$) {
1743 my ($mode, $val, $max) = @_; 1697 my ($rel, $val, $max) = @_;
1744 1698
1745 $mode eq "abs" ? $val 1699 $rel ? $val * $max : $val
1746 : $mode eq "rel" ? $val * $max
1747 : 0
1748} 1700}
1749 1701
1750sub size_request { 1702sub size_request {
1751 my ($self) = @_; 1703 my ($self) = @_;
1752 1704
1753 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1705 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1754 1706
1755 # determine overall size by querying abs widgets 1707 # determine overall size by querying abs widgets
1756 for my $child ($self->visible_children) { 1708 for my $child ($self->visible_children) {
1757 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1709 unless ($child->{c_rel}) {
1710 my $x = $child->{c_x};
1711 my $y = $child->{c_y};
1758 1712
1759 if ($pos eq "abs") {
1760 $w = _scale $size, $w, $child->{req_w};
1761 $h = _scale $size, $h, $child->{req_h};
1762
1763 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1713 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1764 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1714 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1765 } 1715 }
1766 } 1716 }
1767 1717
1768 my $W = $x2 - $x1; 1718 my $W = $x2 - $x1;
1769 my $H = $y2 - $y1; 1719 my $H = $y2 - $y1;
1770 1720
1771 # now layout remaining widgets 1721 # now layout remaining widgets
1772 for my $child ($self->visible_children) { 1722 for my $child ($self->visible_children) {
1773 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1723 if ($child->{c_rel}) {
1724 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1725 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1774 1726
1775 if ($pos ne "abs") {
1776 $x = _scale $pos, $x, $W;
1777 $y = _scale $pos, $x, $H;
1778 $w = _scale $size, $w, $child->{req_w};
1779 $h = _scale $size, $h, $child->{req_h};
1780
1781 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1727 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1782 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1728 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1783 } 1729 }
1784 } 1730 }
1785 1731
1786 my $W = $x2 - $x1; 1732 my $W = $x2 - $x1;
1787 my $H = $y2 - $y1; 1733 my $H = $y2 - $y1;
1791 1737
1792sub invoke_size_allocate { 1738sub invoke_size_allocate {
1793 my ($self, $W, $H) = @_; 1739 my ($self, $W, $H) = @_;
1794 1740
1795 for my $child ($self->visible_children) { 1741 for my $child ($self->visible_children) {
1796 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1742 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1743 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1797 1744
1798 $x = _scale $pos, $x, $W; 1745 $x += $child->{c_halign} * $child->{req_w};
1799 $y = _scale $pos, $x, $H; 1746 $y += $child->{c_valign} * $child->{req_h};
1800 $w = _scale $size, $w, $child->{req_w};
1801 $h = _scale $size, $h, $child->{req_h};
1802 1747
1803 $child->configure ($x, $y, $w, $h); 1748 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1804 } 1749 }
1805 1750
1806 1 1751 1
1807} 1752}
1808 1753
1812 1757
1813our @ISA = CFPlus::UI::Container::; 1758our @ISA = CFPlus::UI::Container::;
1814 1759
1815sub size_request { 1760sub size_request {
1816 my ($self) = @_; 1761 my ($self) = @_;
1762
1763 my @children = $self->visible_children;
1817 1764
1818 $self->{vertical} 1765 $self->{vertical}
1819 ? ( 1766 ? (
1820 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1767 (List::Util::max map $_->{req_w}, @children),
1821 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1768 (List::Util::sum map $_->{req_h}, @children),
1822 ) 1769 )
1823 : ( 1770 : (
1824 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1771 (List::Util::sum map $_->{req_w}, @children),
1825 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1772 (List::Util::max map $_->{req_h}, @children),
1826 ) 1773 )
1827} 1774}
1828 1775
1829sub invoke_size_allocate { 1776sub invoke_size_allocate {
1830 my ($self, $w, $h) = @_; 1777 my ($self, $w, $h) = @_;
2071 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2018 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2072 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2019 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2073 : ($self->{h} - $size->[1]) * 0.5); 2020 : ($self->{h} - $size->[1]) * 0.5);
2074 }; 2021 };
2075 2022
2076 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2023# unless ($self->{list}) {
2077 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2024# $self->{list} = CFPlus::OpenGL::glGenList;
2025# CFPlus::OpenGL::glNewList $self->{list};
2026# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2027# CFPlus::OpenGL::glEndList;
2028# }
2029#
2030# CFPlus::OpenGL::glCallList $self->{list};
2078 2031
2079 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2032 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2080} 2033}
2034
2035#sub destroy {
2036# my ($self) = @_;
2037#
2038# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2039#
2040# $self->SUPER::destroy;
2041#}
2081 2042
2082############################################################################# 2043#############################################################################
2083 2044
2084package CFPlus::UI::EntryBase; 2045package CFPlus::UI::EntryBase;
2085 2046
2870 2831
2871 $self->SUPER::invoke_button_down ($ev, $x, $y); 2832 $self->SUPER::invoke_button_down ($ev, $x, $y);
2872 2833
2873 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 2834 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2874 2835
2875 $self->invoke_mouse_motion ($ev, $x, $y) 2836 $self->invoke_mouse_motion ($ev, $x, $y);
2837
2838 1
2876} 2839}
2877 2840
2878sub invoke_mouse_motion { 2841sub invoke_mouse_motion {
2879 my ($self, $ev, $x, $y) = @_; 2842 my ($self, $ev, $x, $y) = @_;
2880 2843
2900 2863
2901 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 2864 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2902 2865
2903 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 2866 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2904 2867
2905 ! ! $delta 2868 1
2906} 2869}
2907 2870
2908sub update { 2871sub update {
2909 my ($self) = @_; 2872 my ($self) = @_;
2910 2873
3041} 3004}
3042 3005
3043sub size_request { 3006sub size_request {
3044 my ($self) = @_; 3007 my ($self) = @_;
3045 3008
3046 my ($empty, $slider) = @{ $self->{children} }; 3009 my ($empty, $slider) = $self->visible_children;
3047 3010
3048 local $self->{children} = [$empty, $slider]; 3011 local $self->{children} = [$empty, $slider];
3049 $self->SUPER::size_request 3012 $self->SUPER::size_request
3050} 3013}
3051 3014
3483 3446
3484 if ($self->{anim} && $self->{animspeed}) { 3447 if ($self->{anim} && $self->{animspeed}) {
3485 CFPlus::weaken (my $widget = $self); 3448 CFPlus::weaken (my $widget = $self);
3486 3449
3487 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3450 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3488 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed}; 3451 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3489 $self->{timer} = Event->timer ( 3452 $self->{timer} = Event->timer (
3490 parked => 1, 3453 parked => 1,
3491 cb => sub { 3454 cb => sub {
3492 return unless $::CONN && $widget; 3455 return unless $::CONN && $widget;
3493 3456
3530 3493
3531 if (my $anim = $::CONN->{anim}[$self->{anim}]) { 3494 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3532 if ($anim && @$anim) { 3495 if ($anim && @$anim) {
3533 delete $self->{wait_face}; 3496 delete $self->{wait_face};
3534 $self->{face} = $anim->[ $self->{frame} % @$anim ]; 3497 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3498 $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ];
3535 } 3499 }
3536 } 3500 }
3537} 3501}
3538 3502
3539sub size_request { 3503sub size_request {
3540 my ($self) = @_; 3504 my ($self) = @_;
3541 3505
3542 if ($::CONN) { 3506 if ($::CONN) {
3543 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3507 if (my $faceid = $::CONN->{faceid}[$self->{face}]) {
3544 if (my $tex = $::CONN->{texture}[$faceid]) { 3508 if (my $tex = $::CONN->{texture}[$faceid]) {
3509 $self->{tex} = $tex;
3545 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3510 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3546 } else { 3511 } else {
3547 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3512 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3548 $self->realloc; 3513 $self->realloc;
3549 }); 3514 });
3571} 3536}
3572 3537
3573sub _draw { 3538sub _draw {
3574 my ($self) = @_; 3539 my ($self) = @_;
3575 3540
3576 return unless $::CONN;
3577
3578 $self->SUPER::_draw; 3541 $self->SUPER::_draw;
3579 3542
3580 my $faceid = $::CONN->{faceid}[$self->{face}] 3543 if (my $tex = $self->{tex}) {
3581 or return;
3582
3583 my $tex = $::CONN->{texture}[$faceid];
3584
3585 if ($tex) {
3586 glEnable GL_TEXTURE_2D; 3544 glEnable GL_TEXTURE_2D;
3587 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3545 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3588 glColor 0, 0, 0, 1; 3546 glColor 0, 0, 0, 1;
3589 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3547 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3590 glDisable GL_TEXTURE_2D; 3548 glDisable GL_TEXTURE_2D;
3792 ); 3750 );
3793 3751
3794 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3752 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3795 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3753 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3796 3754
3755 {
3756 Scalar::Util::weaken (my $wself = $self);
3757
3758 $self->{multiplexer}->connect (c_add => sub {
3759 my ($mplex, $widgets) = @_;
3760
3761 for my $child (@$widgets) {
3762 Scalar::Util::weaken $child;
3763 $child->{c_tab_} ||= do {
3764 my $tab =
3765 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base")
3766 ? $child->{c_tab}
3767 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
3768
3769 $tab->connect (activate => sub {
3770 $wself->set_current_page ($child);
3771 });
3772
3773 $tab
3774 };
3775
3776 $self->{buttonbar}->add ($child->{c_tab_});
3777 }
3778 });
3779
3780 $self->{multiplexer}->connect (c_remove => sub {
3781 my ($mplex, $widgets) = @_;
3782
3783 for my $child (@$widgets) {
3784 $wself->{buttonbar}->remove ($child->{c_tab_});
3785 }
3786 });
3787 }
3788
3797 $self 3789 $self
3798} 3790}
3799 3791
3800sub add { 3792sub add {
3793 my ($self, @widgets) = @_;
3794
3795 $self->{multiplexer}->add (@widgets)
3796}
3797
3798sub remove {
3799 my ($self, @widgets) = @_;
3800
3801 $self->{multiplexer}->remove (@widgets)
3802}
3803
3804sub add_tab {
3801 my ($self, $title, $widget, $tooltip) = @_; 3805 my ($self, $title, $widget, $tooltip) = @_;
3802 3806
3803 CFPlus::weaken $self; 3807 $title = [$title, $tooltip] unless ref $title;
3808 $widget->{c_tab} = $title;
3804 3809
3805 $self->{buttonbar}->add (new CFPlus::UI::Button
3806 markup => $title,
3807 tooltip => $tooltip,
3808 on_activate => sub { $self->set_current_page ($widget) },
3809 );
3810
3811 $self->{multiplexer}->add ($widget); 3810 $self->add ($widget);
3812} 3811}
3813 3812
3814sub get_current_page { 3813sub get_current_page {
3815 my ($self) = @_; 3814 my ($self) = @_;
3816 3815

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines