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.400 by root, Sat Jul 21 16:15:03 2007 UTC vs.
Revision 1.411 by root, Sun Jul 22 19:34:59 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";
1094 $vp->{parent}{hslider}->set_value ($x); 1050 $vp->{parent}{hslider}->set_value ($x);
1095 $vp->{parent}{vslider}->set_value ($y); 1051 $vp->{parent}{vslider}->set_value ($y);
1096 1052
1097 0 1053 0
1098 }, 1054 },
1055 on_size_allocate => sub {
1056 my ($vp, $w, $h) = @_;
1057 $vp->{parent}->update_slider;
1058 0
1059 },
1099 ; 1060 ;
1100 1061
1101 $self->SUPER::add ($self->{vp}); 1062 $self->SUPER::add ($self->{vp});
1102 1063
1103 $self->add ($child) if $child; 1064 $self->add ($child) if $child;
1114sub update_slider { 1075sub update_slider {
1115 my ($self) = @_; 1076 my ($self) = @_;
1116 1077
1117 my $child = ($self->{vp} or return)->child; 1078 my $child = ($self->{vp} or return)->child;
1118 1079
1080 if ($self->{scroll_x}) {
1119 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1081 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1120 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); 1082 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1121 1083
1122 my $visible = $w1 > $w2; 1084 my $visible = $w1 > $w2;
1123 if ($visible != $self->{hslider_visible}) { 1085 if ($visible != $self->{hslider_visible}) {
1124 $self->{hslider_visible} = $visible; 1086 $self->{hslider_visible} = $visible;
1125 $visible ? $self->SUPER::add ($self->{hslider}) 1087 $visible ? $self->SUPER::add ($self->{hslider})
1126 : $self->SUPER::remove ($self->{hslider}); 1088 : $self->SUPER::remove ($self->{hslider});
1089 }
1127 } 1090 }
1128 1091
1092 if ($self->{scroll_y}) {
1129 my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); 1093 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1130 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); 1094 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1131 1095
1132 my $visible = $h1 > $h2; 1096 my $visible = $h1 > $h2;
1133 if ($visible != $self->{vslider_visible}) { 1097 if ($visible != $self->{vslider_visible}) {
1134 $self->{vslider_visible} = $visible; 1098 $self->{vslider_visible} = $visible;
1135 $visible ? $self->SUPER::add ($self->{vslider}) 1099 $visible ? $self->SUPER::add ($self->{vslider})
1136 : $self->SUPER::remove ($self->{vslider}); 1100 : $self->SUPER::remove ($self->{vslider});
1101 }
1137 } 1102 }
1138} 1103}
1139 1104
1140sub start_dragging { 1105sub start_dragging {
1141 my ($self, $ev) = @_; 1106 my ($self, $ev) = @_;
1721 1686
1722use List::Util qw(min max); 1687use List::Util qw(min max);
1723 1688
1724our @ISA = CFPlus::UI::Container::; 1689our @ISA = CFPlus::UI::Container::;
1725 1690
1726sub add {
1727 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1728
1729 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1730 $self->SUPER::add ($child);
1731}
1732
1733sub _scale($$$) { 1691sub _scale($$$) {
1734 my ($mode, $val, $max) = @_; 1692 my ($rel, $val, $max) = @_;
1735 1693
1736 $mode eq "abs" ? $val 1694 $rel ? $val * $max : $val
1737 : $mode eq "rel" ? $val * $max
1738 : 0
1739} 1695}
1740 1696
1741sub size_request { 1697sub size_request {
1742 my ($self) = @_; 1698 my ($self) = @_;
1743 1699
1744 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1700 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1745 1701
1746 # determine overall size by querying abs widgets 1702 # determine overall size by querying abs widgets
1747 for my $child ($self->visible_children) { 1703 for my $child ($self->visible_children) {
1748 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1704 unless ($child->{c_rel}) {
1705 my $x = $child->{c_x};
1706 my $y = $child->{c_y};
1749 1707
1750 if ($pos eq "abs") {
1751 $w = _scale $size, $w, $child->{req_w};
1752 $h = _scale $size, $h, $child->{req_h};
1753
1754 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1708 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1755 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1709 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1756 } 1710 }
1757 } 1711 }
1758 1712
1759 my $W = $x2 - $x1; 1713 my $W = $x2 - $x1;
1760 my $H = $y2 - $y1; 1714 my $H = $y2 - $y1;
1761 1715
1762 # now layout remaining widgets 1716 # now layout remaining widgets
1763 for my $child ($self->visible_children) { 1717 for my $child ($self->visible_children) {
1764 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1718 if ($child->{c_rel}) {
1719 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1720 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1765 1721
1766 if ($pos ne "abs") {
1767 $x = _scale $pos, $x, $W;
1768 $y = _scale $pos, $x, $H;
1769 $w = _scale $size, $w, $child->{req_w};
1770 $h = _scale $size, $h, $child->{req_h};
1771
1772 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1722 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1773 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1723 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1774 } 1724 }
1775 } 1725 }
1776 1726
1777 my $W = $x2 - $x1; 1727 my $W = $x2 - $x1;
1778 my $H = $y2 - $y1; 1728 my $H = $y2 - $y1;
1782 1732
1783sub invoke_size_allocate { 1733sub invoke_size_allocate {
1784 my ($self, $W, $H) = @_; 1734 my ($self, $W, $H) = @_;
1785 1735
1786 for my $child ($self->visible_children) { 1736 for my $child ($self->visible_children) {
1787 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1737 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1738 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1788 1739
1789 $x = _scale $pos, $x, $W; 1740 $x += $child->{c_halign} * $child->{req_w};
1790 $y = _scale $pos, $x, $H; 1741 $y += $child->{c_valign} * $child->{req_h};
1791 $w = _scale $size, $w, $child->{req_w};
1792 $h = _scale $size, $h, $child->{req_h};
1793 1742
1794 $child->configure ($x, $y, $w, $h); 1743 $child->configure ($x, $y, $child->{req_w}, $child->{req_h});
1795 } 1744 }
1796 1745
1797 1 1746 1
1798} 1747}
1799 1748
2062 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2011 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2063 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2012 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2064 : ($self->{h} - $size->[1]) * 0.5); 2013 : ($self->{h} - $size->[1]) * 0.5);
2065 }; 2014 };
2066 2015
2067 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2016# unless ($self->{list}) {
2068 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2017# $self->{list} = CFPlus::OpenGL::glGenList;
2018# CFPlus::OpenGL::glNewList $self->{list};
2019# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2020# CFPlus::OpenGL::glEndList;
2021# }
2022#
2023# CFPlus::OpenGL::glCallList $self->{list};
2069 2024
2070 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2025 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2071} 2026}
2027
2028#sub destroy {
2029# my ($self) = @_;
2030#
2031# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2032#
2033# $self->SUPER::destroy;
2034#}
2072 2035
2073############################################################################# 2036#############################################################################
2074 2037
2075package CFPlus::UI::EntryBase; 2038package CFPlus::UI::EntryBase;
2076 2039
2861 2824
2862 $self->SUPER::invoke_button_down ($ev, $x, $y); 2825 $self->SUPER::invoke_button_down ($ev, $x, $y);
2863 2826
2864 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 2827 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2865 2828
2866 $self->invoke_mouse_motion ($ev, $x, $y) 2829 $self->invoke_mouse_motion ($ev, $x, $y);
2830
2831 1
2867} 2832}
2868 2833
2869sub invoke_mouse_motion { 2834sub invoke_mouse_motion {
2870 my ($self, $ev, $x, $y) = @_; 2835 my ($self, $ev, $x, $y) = @_;
2871 2836
2891 2856
2892 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 2857 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2893 2858
2894 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 2859 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2895 2860
2896 ! ! $delta 2861 1
2897} 2862}
2898 2863
2899sub update { 2864sub update {
2900 my ($self) = @_; 2865 my ($self) = @_;
2901 2866
3474 3439
3475 if ($self->{anim} && $self->{animspeed}) { 3440 if ($self->{anim} && $self->{animspeed}) {
3476 CFPlus::weaken (my $widget = $self); 3441 CFPlus::weaken (my $widget = $self);
3477 3442
3478 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3443 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3479 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed}; 3444 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3480 $self->{timer} = Event->timer ( 3445 $self->{timer} = Event->timer (
3481 parked => 1, 3446 parked => 1,
3482 cb => sub { 3447 cb => sub {
3483 return unless $::CONN && $widget; 3448 return unless $::CONN && $widget;
3484 3449
3521 3486
3522 if (my $anim = $::CONN->{anim}[$self->{anim}]) { 3487 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3523 if ($anim && @$anim) { 3488 if ($anim && @$anim) {
3524 delete $self->{wait_face}; 3489 delete $self->{wait_face};
3525 $self->{face} = $anim->[ $self->{frame} % @$anim ]; 3490 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3491 $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ];
3526 } 3492 }
3527 } 3493 }
3528} 3494}
3529 3495
3530sub size_request { 3496sub size_request {
3531 my ($self) = @_; 3497 my ($self) = @_;
3532 3498
3533 if ($::CONN) { 3499 if ($::CONN) {
3534 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3500 if (my $faceid = $::CONN->{faceid}[$self->{face}]) {
3535 if (my $tex = $::CONN->{texture}[$faceid]) { 3501 if (my $tex = $::CONN->{texture}[$faceid]) {
3502 $self->{tex} = $tex;
3536 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3503 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3537 } else { 3504 } else {
3538 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3505 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3539 $self->realloc; 3506 $self->realloc;
3540 }); 3507 });
3562} 3529}
3563 3530
3564sub _draw { 3531sub _draw {
3565 my ($self) = @_; 3532 my ($self) = @_;
3566 3533
3567 return unless $::CONN;
3568
3569 $self->SUPER::_draw; 3534 $self->SUPER::_draw;
3570 3535
3571 my $faceid = $::CONN->{faceid}[$self->{face}] 3536 if (my $tex = $self->{tex}) {
3572 or return;
3573
3574 my $tex = $::CONN->{texture}[$faceid];
3575
3576 if ($tex) {
3577 glEnable GL_TEXTURE_2D; 3537 glEnable GL_TEXTURE_2D;
3578 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3538 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3579 glColor 0, 0, 0, 1; 3539 glColor 0, 0, 0, 1;
3580 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3540 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3581 glDisable GL_TEXTURE_2D; 3541 glDisable GL_TEXTURE_2D;
3791sub add { 3751sub add {
3792 my ($self, $title, $widget, $tooltip) = @_; 3752 my ($self, $title, $widget, $tooltip) = @_;
3793 3753
3794 CFPlus::weaken $self; 3754 CFPlus::weaken $self;
3795 3755
3796 $self->{buttonbar}->add (new CFPlus::UI::Button 3756 unless (ref $title) {
3757 $title = new CFPlus::UI::Button
3797 markup => $title, 3758 markup => $title,
3798 tooltip => $tooltip, 3759 tooltip => $tooltip,
3760 ;
3761 }
3762
3799 on_activate => sub { $self->set_current_page ($widget) }, 3763 $title->connect (activate => sub { $self->set_current_page ($widget) });
3800 ); 3764 $self->{buttonbar}->add ($title);
3801 3765
3802 $self->{multiplexer}->add ($widget); 3766 $self->{multiplexer}->add ($widget);
3803} 3767}
3804 3768
3805sub get_current_page { 3769sub get_current_page {
4137 while ($self->{refresh_hook}) { 4101 while ($self->{refresh_hook}) {
4138 $_->() 4102 $_->()
4139 for values %{delete $self->{refresh_hook}}; 4103 for values %{delete $self->{refresh_hook}};
4140 } 4104 }
4141 4105
4142 if ($self->{realloc}) { 4106 while ($self->{realloc}) {
4143 my %queue; 4107 my %queue;
4144 my @queue; 4108 my @queue;
4145 my $widget; 4109 my $widget;
4146 4110
4147 outer: 4111 outer:
4194 } 4158 }
4195 } 4159 }
4196 4160
4197 delete $self->{realloc}{$widget+0}; 4161 delete $self->{realloc}{$widget+0};
4198 } 4162 }
4199 }
4200 4163
4201 while (my $size_alloc = delete $self->{size_alloc}) { 4164 while (my $size_alloc = delete $self->{size_alloc}) {
4202 my @queue = sort { $b->{visible} <=> $a->{visible} } 4165 my @queue = sort { $a->{visible} <=> $b->{visible} }
4203 values %$size_alloc; 4166 values %$size_alloc;
4204 4167
4205 while () { 4168 while () {
4206 my $widget = pop @queue || last; 4169 my $widget = pop @queue || last;
4207 4170
4208 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4171 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4209 4172
4210 $w = 0 if $w < 0;
4211 $h = 0 if $h < 0;
4212
4213 $w = max $widget->{min_w}, $w; 4173 $w = max $widget->{min_w}, $w;
4214 $h = max $widget->{min_h}, $h; 4174 $h = max $widget->{min_h}, $h;
4215 4175
4216# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4176# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4217# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4177# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4218 4178
4219 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4179 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4220 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4180 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4221 4181
4222 $w = int $w + 0.5; 4182 $w = int $w + 0.5;
4223 $h = int $h + 0.5; 4183 $h = int $h + 0.5;
4224 4184
4225 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4185 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
4226 $widget->{old_w} = $widget->{w}; 4186 $widget->{old_w} = $widget->{w};
4227 $widget->{old_h} = $widget->{h}; 4187 $widget->{old_h} = $widget->{h};
4228 4188
4229 $widget->{w} = $w; 4189 $widget->{w} = $w;
4230 $widget->{h} = $h; 4190 $widget->{h} = $h;
4231 4191
4232 $widget->emit (size_allocate => $w, $h); 4192 $widget->emit (size_allocate => $w, $h);
4193 }
4233 } 4194 }
4234 } 4195 }
4235 } 4196 }
4236 4197
4237 while ($self->{post_alloc_hook}) { 4198 while ($self->{post_alloc_hook}) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines