Skip to content

Commit

Permalink
Improved drag, rotation, scale for single point nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Lidholt committed Aug 29, 2012
1 parent bc0e5cb commit b7fd9c7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CocosBuilder/CocosBuilder.xcodeproj/project.pbxproj
Expand Up @@ -237,6 +237,7 @@
E35DBC2514F4FEF90070A6E4 /* InspectorTexture.xib in Resources */ = {isa = PBXBuildFile; fileRef = E35DBC2414F4FEF80070A6E4 /* InspectorTexture.xib */; };
E35DBC2814F4FF410070A6E4 /* InspectorTexture.m in Sources */ = {isa = PBXBuildFile; fileRef = E35DBC2714F4FF3F0070A6E4 /* InspectorTexture.m */; };
E360789015EBA5420040A172 /* sel-frame.png in Resources */ = {isa = PBXBuildFile; fileRef = E360788F15EBA5420040A172 /* sel-frame.png */; };
E36078BC15EE0B2C0040A172 /* sel-round.png in Resources */ = {isa = PBXBuildFile; fileRef = E36078BB15EE0B2C0040A172 /* sel-round.png */; };
E36A27CF152263B4004986A8 /* CCAction.m in Sources */ = {isa = PBXBuildFile; fileRef = E36A2711152263B4004986A8 /* CCAction.m */; };
E36A27D0152263B4004986A8 /* CCActionCamera.m in Sources */ = {isa = PBXBuildFile; fileRef = E36A2713152263B4004986A8 /* CCActionCamera.m */; };
E36A27D1152263B4004986A8 /* CCActionCatmullRom.m in Sources */ = {isa = PBXBuildFile; fileRef = E36A2715152263B4004986A8 /* CCActionCatmullRom.m */; };
Expand Down Expand Up @@ -1078,6 +1079,7 @@
E35DBC2614F4FF3E0070A6E4 /* InspectorTexture.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InspectorTexture.h; sourceTree = "<group>"; };
E35DBC2714F4FF3F0070A6E4 /* InspectorTexture.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = InspectorTexture.m; sourceTree = "<group>"; };
E360788F15EBA5420040A172 /* sel-frame.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "sel-frame.png"; sourceTree = "<group>"; };
E36078BB15EE0B2C0040A172 /* sel-round.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "sel-round.png"; sourceTree = "<group>"; };
E36A2710152263B4004986A8 /* CCAction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCAction.h; sourceTree = "<group>"; };
E36A2711152263B4004986A8 /* CCAction.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCAction.m; sourceTree = "<group>"; };
E36A2712152263B4004986A8 /* CCActionCamera.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCActionCamera.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1909,6 +1911,7 @@
776041EF1498FCE50078095D /* Stage Assets */ = {
isa = PBXGroup;
children = (
E36078BB15EE0B2C0040A172 /* sel-round.png */,
E360788F15EBA5420040A172 /* sel-frame.png */,
E3F3F84A153DBA43005443EE /* notes-close-down.png */,
E3F3F848153DB893005443EE /* notes-close.png */,
Expand Down Expand Up @@ -3977,6 +3980,7 @@
E3B19F8015E65B51000B023E /* ccbParticleSnow.png in Resources */,
E3B19F8115E65B51000B023E /* ccbParticleStars.png in Resources */,
E360789015EBA5420040A172 /* sel-frame.png in Resources */,
E36078BC15EE0B2C0040A172 /* sel-round.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file added CocosBuilder/Resources/sel-round.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion CocosBuilder/ccBuilder/CocosScene.h
Expand Up @@ -49,7 +49,8 @@ enum {
kCCBTransformHandleDownInside,
kCCBTransformHandleMove,
kCCBTransformHandleScale,
kCCBTransformHandleRotate
kCCBTransformHandleRotate,
kCCBTransformHandleAnchorPoint,
};

enum {
Expand Down
100 changes: 69 additions & 31 deletions CocosBuilder/ccBuilder/CocosScene.m
Expand Up @@ -46,6 +46,9 @@
#import "CCScale9Sprite.h"

#define kCCBSelectionOutset 3
#define kCCBSinglePointSelectionRadius 23
#define kCCBAnchorPointRadius 6
#define kCCBTransformHandleRadius 5

static CocosScene* sharedCocosScene;

Expand Down Expand Up @@ -371,7 +374,7 @@ - (void) updateSelection
selmarkCenter.position = center;
[selectionLayer addChild:selmarkCenter z:1];

CGPoint minCorner = center;
//CGPoint minCorner = center;

if (node.contentSize.width > 0 && node.contentSize.height > 0)
{
Expand All @@ -383,7 +386,7 @@ - (void) updateSelection
CGPoint bl = [node convertToWorldSpace: ccp(0,0)];
CGPoint br = [node convertToWorldSpace: ccp(node.contentSize.width,0)];
CGPoint tl = [node convertToWorldSpace: ccp(0,node.contentSize.height)];
CGPoint tr = [node convertToWorldSpace: ccp(node.contentSize.width,node.contentSize.height)];
//CGPoint tr = [node convertToWorldSpace: ccp(node.contentSize.width,node.contentSize.height)];

// Width & height of selection
float width = (int)ccpLength(ccpSub(br, bl));
Expand All @@ -409,6 +412,7 @@ - (void) updateSelection
sel.preferedSize = CGSizeMake(width + kCCBSelectionOutset * 2, height + kCCBSelectionOutset * 2);
[selectionLayer addChild:sel];

/*
minCorner.x = MIN(minCorner.x, bl.x);
minCorner.x = MIN(minCorner.x, br.x);
minCorner.x = MIN(minCorner.x, tl.x);
Expand All @@ -418,15 +422,27 @@ - (void) updateSelection
minCorner.y = MIN(minCorner.y, br.y);
minCorner.y = MIN(minCorner.y, tl.y);
minCorner.y = MIN(minCorner.y, tr.y);
*/
}
else
{
CGPoint pos = [node convertToWorldSpace: ccp(0,0)];

CCSprite* sel = [CCSprite spriteWithFile:@"sel-round.png"];
sel.anchorPoint = ccp(0.5f, 00.5f);
sel.position = pos;
[selectionLayer addChild:sel];
}

/*
if (minCorner.x < 10+15) minCorner.x = 10+15;
if (minCorner.y < 36+15) minCorner.y = 36+15;
if (minCorner.x > self.contentSize.width - 28*3+6) minCorner.x = self.contentSize.width - 28*3+6;
if (minCorner.y > self.contentSize.height+6) minCorner.y = self.contentSize.height+6;
minCorner.x = (int)minCorner.x;
minCorner.y = (int)minCorner.y;
*/
}
}
}
Expand Down Expand Up @@ -475,16 +491,35 @@ - (int) transformHandleUnderPt:(CGPoint)pt
{
for (CCNode* node in appDelegate.selectedNodes)
{
CGPoint bl = [node convertToWorldSpace: ccp(0,0)];
CGPoint br = [node convertToWorldSpace: ccp(node.contentSize.width,0)];
CGPoint tl = [node convertToWorldSpace: ccp(0,node.contentSize.height)];
CGPoint tr = [node convertToWorldSpace: ccp(node.contentSize.width,node.contentSize.height)];
CGPoint center = [node.parent convertToWorldSpace: node.position];
if (ccpDistance(pt, center) < kCCBAnchorPointRadius) return kCCBTransformHandleAnchorPoint;

transformScalingNode = node;
if (ccpDistance(pt, bl) < 5) return kCCBTransformHandleScale;
if (ccpDistance(pt, br) < 5) return kCCBTransformHandleScale;
if (ccpDistance(pt, tl) < 5) return kCCBTransformHandleScale;
if (ccpDistance(pt, tr) < 5) return kCCBTransformHandleScale;
if (node.contentSize.width == 0 || node.contentSize.height == 0)
{
CGPoint l = ccpAdd(center, ccp(kCCBSinglePointSelectionRadius, 0));
CGPoint r = ccpAdd(center, ccp(-kCCBSinglePointSelectionRadius, 0));
CGPoint t = ccpAdd(center, ccp(0, kCCBSinglePointSelectionRadius));
CGPoint b = ccpAdd(center, ccp(0, -kCCBSinglePointSelectionRadius));

transformScalingNode = node;
if (ccpDistance(pt, l) < kCCBTransformHandleRadius) return kCCBTransformHandleScale;
if (ccpDistance(pt, r) < kCCBTransformHandleRadius) return kCCBTransformHandleScale;
if (ccpDistance(pt, t) < kCCBTransformHandleRadius) return kCCBTransformHandleScale;
if (ccpDistance(pt, b) < kCCBTransformHandleRadius) return kCCBTransformHandleScale;
}
else
{
CGPoint bl = [node convertToWorldSpace: ccp(0,0)];
CGPoint br = [node convertToWorldSpace: ccp(node.contentSize.width,0)];
CGPoint tl = [node convertToWorldSpace: ccp(0,node.contentSize.height)];
CGPoint tr = [node convertToWorldSpace: ccp(node.contentSize.width,node.contentSize.height)];

transformScalingNode = node;
if (ccpDistance(pt, bl) < kCCBTransformHandleRadius) return kCCBTransformHandleScale;
if (ccpDistance(pt, br) < kCCBTransformHandleRadius) return kCCBTransformHandleScale;
if (ccpDistance(pt, tl) < kCCBTransformHandleRadius) return kCCBTransformHandleScale;
if (ccpDistance(pt, tr) < kCCBTransformHandleRadius) return kCCBTransformHandleScale;
}
}

transformScalingNode = NULL;
Expand All @@ -497,38 +532,34 @@ - (void) nodesUnderPt:(CGPoint)pt rootNode:(CCNode*) node nodes:(NSMutableArray*

NodeInfo* parentInfo = node.parent.userObject;
PlugInNode* parentPlugIn = parentInfo.plugIn;

if (parentPlugIn && !parentPlugIn.canHaveChildren) return;

CGRect hitRect = [node boundingBox];

// Extend the hit area if it's too small
if (node.contentSize.width < 10)
if (node.contentSize.width == 0 || node.contentSize.height == 0)
{
hitRect.origin.x -= 5;
hitRect.size.width += 10;
}

if (node.contentSize.height < 10)
{
hitRect.origin.y -= 5;
hitRect.size.height += 10;
CGPoint worldPos = [node.parent convertToWorldSpace:node.position];
if (ccpDistance(worldPos, pt) < kCCBSinglePointSelectionRadius)
{
[nodes addObject:node];
}
}

CCNode* parent = node.parent;
CGPoint ptLocal = [parent convertToNodeSpace:pt];

if (CGRectContainsPoint(hitRect, ptLocal))
else
{
[nodes addObject:node];
CGRect hitRect = [node boundingBox];

CCNode* parent = node.parent;
CGPoint ptLocal = [parent convertToNodeSpace:pt];

if (CGRectContainsPoint(hitRect, ptLocal))
{
[nodes addObject:node];
}
}

// Visit children
for (int i = 0; i < [node.children count]; i++)
{
[self nodesUnderPt:pt rootNode:[node.children objectAtIndex:i] nodes:nodes];
}

}

- (BOOL) isLocalCoordinateSystemFlipped:(CCNode*)node
Expand Down Expand Up @@ -572,6 +603,13 @@ - (BOOL) ccMouseDown:(NSEvent *)event

// Transform handles
int th = [self transformHandleUnderPt:pos];
BOOL transformedNodeZeroSize = (transformScalingNode.contentSize.width == 0 || transformScalingNode.contentSize.height == 0);

if (th == kCCBTransformHandleAnchorPoint && !transformedNodeZeroSize)
{
// Move anchor point
return YES;
}
if (th == kCCBTransformHandleScale && appDelegate.selectedNode != rootNode)
{
if ([event modifierFlags] & NSAlternateKeyMask)
Expand Down

0 comments on commit b7fd9c7

Please sign in to comment.