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.392 by root, Sat Jul 21 13:37:09 2007 UTC vs.
Revision 1.406 by root, Sat Jul 21 23:17:35 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";
649} 605}
650 606
651my $cntx;#d#
652sub DESTROY { 607sub DESTROY {
653 my ($self) = @_; 608 my ($self) = @_;
654 609
655 return if CFPlus::in_destruct; 610 return if CFPlus::in_destruct;
656 611
769 sort { $a->{z} <=> $b->{z} } 724 sort { $a->{z} <=> $b->{z} }
770 @{$self->{children}}, @widgets 725 @{$self->{children}}, @widgets
771 ]; 726 ];
772 727
773 $self->realloc; 728 $self->realloc;
729
730 map $_+0, @widgets
774} 731}
775 732
776sub children { 733sub children {
777 @{ $_[0]{children} } 734 @{ $_[0]{children} }
778} 735}
789} 746}
790 747
791sub clear { 748sub clear {
792 my ($self) = @_; 749 my ($self) = @_;
793 750
794 my $children = delete $self->{children}; 751 my $children = $self->{children};
795 $self->{children} = []; 752 $self->{children} = [];
796 753
797 for (@$children) { 754 for (@$children) {
798 delete $_->{parent}; 755 delete $_->{parent};
799 $_->hide; 756 $_->hide;
1093 $vp->{parent}{hslider}->set_value ($x); 1050 $vp->{parent}{hslider}->set_value ($x);
1094 $vp->{parent}{vslider}->set_value ($y); 1051 $vp->{parent}{vslider}->set_value ($y);
1095 1052
1096 0 1053 0
1097 }, 1054 },
1055 on_size_allocate => sub {
1056 my ($vp, $w, $h) = @_;
1057 $vp->{parent}->update_slider;
1058 0
1059 },
1098 ; 1060 ;
1099 1061
1100 $self->SUPER::add ($self->{vp}); 1062 $self->SUPER::add ($self->{vp});
1101 1063
1102 $self->add ($child) if $child; 1064 $self->add ($child) if $child;
1103 1065
1104 $self 1066 $self
1105} 1067}
1106
1107#TODO# update range on size_allocate depending on child?
1108 1068
1109sub add { 1069sub add {
1110 my ($self, $widget) = @_; 1070 my ($self, $widget) = @_;
1111 1071
1112 $self->{vp}->add ($self->{child} = $widget); 1072 $self->{vp}->add ($self->{child} = $widget);
1115sub update_slider { 1075sub update_slider {
1116 my ($self) = @_; 1076 my ($self) = @_;
1117 1077
1118 my $child = ($self->{vp} or return)->child; 1078 my $child = ($self->{vp} or return)->child;
1119 1079
1080 if ($self->{scroll_x}) {
1120 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1081 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1121 $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]);
1122 1083
1123 my $visible = $w1 > $w2; 1084 my $visible = $w1 > $w2;
1124 if ($visible != $self->{hslider}{visible}) { 1085 if ($visible != $self->{hslider_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}) {
1098 $self->{vslider_visible} = $visible;
1134 $visible ? $self->SUPER::add ($self->{vslider}) 1099 $visible ? $self->SUPER::add ($self->{vslider})
1135 : $self->SUPER::remove ($self->{vslider}); 1100 : $self->SUPER::remove ($self->{vslider});
1101 }
1102 }
1103}
1104
1105sub start_dragging {
1106 my ($self, $ev) = @_;
1107
1108 $self->grab_focus;
1109
1110 my $ox = $self->{vp}{view_x};
1111 my $oy = $self->{vp}{view_y};
1112
1113 $self->{motion} = sub {
1114 my ($ev, $x, $y) = @_;
1115
1116 $ox -= $ev->{xrel};
1117 $oy -= $ev->{yrel};
1118
1119 $self->{vp}->set_offset ($ox, $oy);
1136 } 1120 };
1137}
1138
1139sub update {
1140 my ($self) = @_;
1141
1142 $self->update_slider;
1143 $self->SUPER::update;
1144} 1121}
1145 1122
1146sub invoke_mouse_wheel { 1123sub invoke_mouse_wheel {
1147 my ($self, $ev) = @_; 1124 my ($self, $ev) = @_;
1148 1125
1149 $self->{vslider}->emit (mouse_wheel => $ev) 1126 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1150 if $ev->{dy};
1151
1152 $self->{hslider}->emit (mouse_wheel => $ev) 1127 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1153 if $ev->{dx};
1154 1128
1155 1 1129 1
1156} 1130}
1157 1131
1158sub invoke_button_down { 1132sub invoke_button_down {
1159 my ($self, $ev, $x, $y) = @_; 1133 my ($self, $ev, $x, $y) = @_;
1160 1134
1161 if ($ev->{button} == 2) { 1135 if ($ev->{button} == 2) {
1162 $self->grab_focus; 1136 $self->start_dragging ($ev);
1163
1164 my $ox = $self->{vp}{view_x} + $ev->{x};
1165 my $oy = $self->{vp}{view_y} + $ev->{y};
1166
1167 $self->{motion} = sub {
1168 my ($ev, $x, $y) = @_;
1169
1170 $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y});
1171 $self->update;
1172 };
1173
1174 return 1; 1137 return 1;
1175 } 1138 }
1176 1139
1177 0 1140 0
1178} 1141}
1646 my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; 1609 my ($c, $w, $cs) = @$widget{qw(col req_w colspan)};
1647 1610
1648 my $sw = sum @w[$c .. $c + $cs - 1]; 1611 my $sw = sum @w[$c .. $c + $cs - 1];
1649 1612
1650 if ($w > $sw) { 1613 if ($w > $sw) {
1651 $_ += ($w - $sw) / $cs for @w[$c .. $c + $cs - 1]; 1614 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1652 } 1615 }
1653 } 1616 }
1654 1617
1655 # second pass, rows 1618 # second pass, rows
1656 for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) { 1619 for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) {
1657 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; 1620 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)};
1658 1621
1659 my $sh = sum @h[$r .. $r + $rs - 1]; 1622 my $sh = sum @h[$r .. $r + $rs - 1];
1660 1623
1661 if ($h > $sh) { 1624 if ($h > $sh) {
1662 $_ += ($h - $sh) / $rs for @h[$r .. $r + $rs - 1]; 1625 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1663 } 1626 }
1664 } 1627 }
1665 1628
1666 (\@w, \@h) 1629 (\@w, \@h)
1667} 1630}
1723 1686
1724use List::Util qw(min max); 1687use List::Util qw(min max);
1725 1688
1726our @ISA = CFPlus::UI::Container::; 1689our @ISA = CFPlus::UI::Container::;
1727 1690
1728sub add { 1691sub add_fixed {
1729 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_; 1692 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1730 1693
1731 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h]; 1694 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1732 $self->SUPER::add ($child); 1695 $self->SUPER::add ($child);
1733} 1696}
1788 for my $child ($self->visible_children) { 1751 for my $child ($self->visible_children) {
1789 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1752 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1790 1753
1791 $x = _scale $pos, $x, $W; 1754 $x = _scale $pos, $x, $W;
1792 $y = _scale $pos, $x, $H; 1755 $y = _scale $pos, $x, $H;
1793 $w = _scale $size, $w, $child->{req_w}; 1756 $w = _scale $size, $w, $W;
1794 $h = _scale $size, $h, $child->{req_h}; 1757 $h = _scale $size, $h, $H;
1795 1758
1796 $child->configure ($x, $y, $w, $h); 1759 $child->configure ($x, $y, $w, $h);
1797 } 1760 }
1798 1761
1799 1 1762 1
2063 2026
2064 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2027 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2065 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2028 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2066 : ($self->{h} - $size->[1]) * 0.5); 2029 : ($self->{h} - $size->[1]) * 0.5);
2067 }; 2030 };
2068
2069 my $w = List::Util::min $self->{w} + 4, $size->[0];
2070 my $h = List::Util::min $self->{h} + 2, $size->[1];
2071 2031
2072 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2032 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2073} 2033}
2074 2034
2075############################################################################# 2035#############################################################################
3476 3436
3477 if ($self->{anim} && $self->{animspeed}) { 3437 if ($self->{anim} && $self->{animspeed}) {
3478 CFPlus::weaken (my $widget = $self); 3438 CFPlus::weaken (my $widget = $self);
3479 3439
3480 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3440 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3481 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed}; 3441 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3482 $self->{timer} = Event->timer ( 3442 $self->{timer} = Event->timer (
3483 parked => 1, 3443 parked => 1,
3484 cb => sub { 3444 cb => sub {
3485 return unless $::CONN && $widget; 3445 return unless $::CONN && $widget;
3486 3446
3523 3483
3524 if (my $anim = $::CONN->{anim}[$self->{anim}]) { 3484 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3525 if ($anim && @$anim) { 3485 if ($anim && @$anim) {
3526 delete $self->{wait_face}; 3486 delete $self->{wait_face};
3527 $self->{face} = $anim->[ $self->{frame} % @$anim ]; 3487 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3488 $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ];
3528 } 3489 }
3529 } 3490 }
3530} 3491}
3531 3492
3532sub size_request { 3493sub size_request {
3533 my ($self) = @_; 3494 my ($self) = @_;
3534 3495
3535 if ($::CONN) { 3496 if ($::CONN) {
3536 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3497 if (my $faceid = $::CONN->{faceid}[$self->{face}]) {
3537 if (my $tex = $::CONN->{texture}[$faceid]) { 3498 if (my $tex = $::CONN->{texture}[$faceid]) {
3499 $self->{tex} = $tex;
3538 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3500 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3539 } else { 3501 } else {
3540 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3502 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3541 $self->realloc; 3503 $self->realloc;
3542 }); 3504 });
3564} 3526}
3565 3527
3566sub _draw { 3528sub _draw {
3567 my ($self) = @_; 3529 my ($self) = @_;
3568 3530
3569 return unless $::CONN;
3570
3571 $self->SUPER::_draw; 3531 $self->SUPER::_draw;
3572 3532
3573 my $faceid = $::CONN->{faceid}[$self->{face}] 3533 if (my $tex = $self->{tex}) {
3574 or return;
3575
3576 my $tex = $::CONN->{texture}[$faceid];
3577
3578 if ($tex) {
3579 glEnable GL_TEXTURE_2D; 3534 glEnable GL_TEXTURE_2D;
3580 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3535 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3581 glColor 0, 0, 0, 1; 3536 glColor 0, 0, 0, 1;
3582 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3537 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3583 glDisable GL_TEXTURE_2D; 3538 glDisable GL_TEXTURE_2D;
4139 while ($self->{refresh_hook}) { 4094 while ($self->{refresh_hook}) {
4140 $_->() 4095 $_->()
4141 for values %{delete $self->{refresh_hook}}; 4096 for values %{delete $self->{refresh_hook}};
4142 } 4097 }
4143 4098
4144 if ($self->{realloc}) { 4099 while ($self->{realloc}) {
4145 my %queue; 4100 my %queue;
4146 my @queue; 4101 my @queue;
4147 my $widget; 4102 my $widget;
4148 4103
4149 outer: 4104 outer:
4196 } 4151 }
4197 } 4152 }
4198 4153
4199 delete $self->{realloc}{$widget+0}; 4154 delete $self->{realloc}{$widget+0};
4200 } 4155 }
4201 }
4202 4156
4203 while (my $size_alloc = delete $self->{size_alloc}) { 4157 while (my $size_alloc = delete $self->{size_alloc}) {
4204 my @queue = sort { $b->{visible} <=> $a->{visible} } 4158 my @queue = sort { $a->{visible} <=> $b->{visible} }
4205 values %$size_alloc; 4159 values %$size_alloc;
4206 4160
4207 while () { 4161 while () {
4208 my $widget = pop @queue || last; 4162 my $widget = pop @queue || last;
4209 4163
4210 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4164 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4211 4165
4212 $w = 0 if $w < 0;
4213 $h = 0 if $h < 0;
4214
4215 $w = max $widget->{min_w}, $w; 4166 $w = max $widget->{min_w}, $w;
4216 $h = max $widget->{min_h}, $h; 4167 $h = max $widget->{min_h}, $h;
4217 4168
4218# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4169# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4219# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4170# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4220 4171
4221 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4172 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4222 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4173 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4223 4174
4224 $w = int $w + 0.5; 4175 $w = int $w + 0.5;
4225 $h = int $h + 0.5; 4176 $h = int $h + 0.5;
4226 4177
4227 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4178 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
4228 $widget->{old_w} = $widget->{w}; 4179 $widget->{old_w} = $widget->{w};
4229 $widget->{old_h} = $widget->{h}; 4180 $widget->{old_h} = $widget->{h};
4230 4181
4231 $widget->{w} = $w; 4182 $widget->{w} = $w;
4232 $widget->{h} = $h; 4183 $widget->{h} = $h;
4233 4184
4234 $widget->emit (size_allocate => $w, $h); 4185 $widget->emit (size_allocate => $w, $h);
4186 }
4235 } 4187 }
4236 } 4188 }
4237 } 4189 }
4238 4190
4239 while ($self->{post_alloc_hook}) { 4191 while ($self->{post_alloc_hook}) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines