Fixed: view coordinate conversion broken by previous commit.

Without this fix, -CPView convertPoint:fromView was broken due to the changes made in #1998. In particular, the transform for the fromView would be calculated and then just thrown away if the views were in the same window. This happened to work in some specific cases and the lack of unit tests concealed the error.

This fix properly applies the transforms when two views are in the same window without one of them necessarily being inside the other.

Refs #1998.
This commit is contained in:
Alexander Ljungberg committed 2013-11-04 00:03:31 +00:00
1 parent 2508fa7a97
commit 6cbfa30652
2 files changed
+70 -7

No files matched your search

+13 -7
View File
@@ -3516,7 +3516,6 @@ var _CPViewGetTransform = function(/*CPView*/ fromView, /*CPView */ toView)
}
view = view._superview;
}
// If we hit toView, then we're done.
@@ -3555,8 +3554,8 @@ var _CPViewGetTransform = function(/*CPView*/ fromView, /*CPView */ toView)
if (view._boundsTransform)
{
var inverseBoundsTransform = CGAffineTransformMakeIdentity();
inverseBoundsTransform.tx -= view._inverseBoundsTransform.tx * transform2.a;
inverseBoundsTransform.ty -= view._inverseBoundsTransform.ty * transform2.d;
inverseBoundsTransform.tx -= view._inverseBoundsTransform.tx * transform2.a;
inverseBoundsTransform.ty -= view._inverseBoundsTransform.ty * transform2.d;
CGAffineTransformConcatTo(transform2, inverseBoundsTransform, transform2);
}
@@ -3567,10 +3566,17 @@ var _CPViewGetTransform = function(/*CPView*/ fromView, /*CPView */ toView)
transform2.tx = -transform2.tx;
transform2.ty = -transform2.ty;
if (sameWindow)
transform = transform2;
else
CGAffineTransformConcatTo(transform, transform2, transform);
if (view === fromView)
{
// toView is inside of fromView
return transform2;
}
CGAffineTransformConcatTo(transform, transform2, transform);
return transform;
/* var views = [],
view = toView;
+57
View File
@@ -125,4 +125,61 @@
[self assert:nil equals:[viewA nextValidKeyView]];
}
- (void)testConvertPoint_fromView_shouldChangeNothingForSameView
{
var tView0 = [CPView new],
aWindow = [CPWindow new];
[aWindow setContentView:tView0];
[tView0 setFrame:CGRectMake(3, 5, 13, 17)];
[self assertTrue:CGPointEqualToPoint(CGPointMake(7, 11), [tView0 convertPoint:CGPointMake(7, 11) fromView:tView0])]
}
- (void)testConvertPoint_fromView_shouldAddSubviewCoordinatesWhenMovingUp
{
var tView0 = [CPView new],
subView0 = [CPView new],
aWindow = [CPWindow new];
[aWindow setContentView:tView0];
[tView0 addSubview:subView0];
[tView0 setFrame:CGRectMake(30, 50, 130, 170)];
[subView0 setFrame:CGRectMake(3, 5, 13, 17)];
[self assertTrue:CGPointEqualToPoint(CGPointMake(10, 16), [tView0 convertPoint:CGPointMake(7, 11) fromView:subView0])]
}
- (void)testConvertPoint_fromView_shouldWorkBetweenSiblingViews
{
var tView0 = [CPView new],
subView0 = [CPView new],
aWindow = [CPWindow new];
[[aWindow contentView] addSubview:tView0];
[[aWindow contentView] addSubview:subView0];
[tView0 setFrame:CGRectMake(30, 50, 130, 170)];
[subView0 setFrame:CGRectMake(3, 5, 13, 17)];
[self assertTrue:CGPointEqualToPoint(CGPointMake(34, 56), [subView0 convertPoint:CGPointMake(7, 11) fromView:tView0])]
}
- (void)testConvertPoint_fromView_shouldSubtractSubviewCoordinatesWhenMovingDown
{
var tView0 = [CPView new],
subView0 = [CPView new],
aWindow = [CPWindow new];
[aWindow setContentView:tView0];
[tView0 addSubview:subView0];
[tView0 setFrame:CGRectMake(30, 50, 130, 170)];
[subView0 setFrame:CGRectMake(3, 5, 13, 17)];
[self assertTrue:CGPointEqualToPoint(CGPointMake(4, 6), [subView0 convertPoint:CGPointMake(7, 11) fromView:tView0])]
}
@end