forked from cocos2d/cocos2d-objc-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
559 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
* | ||
* Copyright (c) 2008-2010 Ricardo Quesada | ||
* Copyright (c) 2011 Zynga Inc. | ||
* Copyright (c) 2013-2014 Cocos2D Authors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "cocos2d.h" | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
@interface CCSpriteMultiTouch : CCSprite | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
@property (nonatomic, readonly) NSUInteger touchCount; // number of active touches in sprite | ||
|
||
// defines how many touches it takes to activate drag, scale or rotate | ||
// 0 (zero) disables the functionality, and is default | ||
@property (nonatomic) NSUInteger touchCountForDrag; | ||
@property (nonatomic) NSUInteger touchCountForScale; | ||
@property (nonatomic) NSUInteger touchCountForRotate; | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
- (void)touchDrag; | ||
- (void)touchScale; | ||
- (void)touchRotate; | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
/* | ||
* cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
* | ||
* Copyright (c) 2008-2010 Ricardo Quesada | ||
* Copyright (c) 2011 Zynga Inc. | ||
* Copyright (c) 2013-2014 Cocos2D Authors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#import "CCSpriteMultiTouch.h" | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
#define CCSpriteMultiTouchMaxTouches 3 | ||
#define CCSpriteMultiTouchFuntionDisable 0 | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
@implementation CCSpriteMultiTouch | ||
{ | ||
// must be weak, to avoid retaining touches | ||
__weak UITouch *_touch[CCSpriteMultiTouchMaxTouches]; | ||
// variables used internally to | ||
CGPoint _dragCentre; | ||
CGPoint _scaleCentre; | ||
float _scaleStartDistance; | ||
float _scaleStartScale; | ||
CGPoint _rotationCentre; | ||
float _rotationLast; | ||
} | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
- (instancetype)init | ||
{ | ||
self = [super init]; | ||
// initialize | ||
_touchCountForDrag = CCSpriteMultiTouchFuntionDisable; | ||
_touchCountForScale = CCSpriteMultiTouchFuntionDisable; | ||
_touchCountForRotate = CCSpriteMultiTouchFuntionDisable; | ||
_touchCount = 0; | ||
// done | ||
return(self); | ||
} | ||
|
||
//---------------------------------------------------------------------- | ||
// helper functions | ||
|
||
// calculates the centre of all current touches | ||
- (CGPoint)centre | ||
{ | ||
CGPoint sum = CGPointZero; | ||
|
||
for (int index = 0; index < _touchCount; index ++) | ||
{ | ||
CGPoint pos = [_touch[index] locationInView:[CCDirector sharedDirector].view]; | ||
pos = [[CCDirector sharedDirector] convertToGL:pos]; | ||
sum = ccpAdd(sum, pos); | ||
} | ||
return(ccpMult(sum, 1.0f / (float)_touchCount)); | ||
} | ||
|
||
// calculates the average distance of oll touches from touch centre | ||
- (float)distance:(CGPoint)centre | ||
{ | ||
float sum = 0; | ||
for (int index = 0; index < _touchCount; index ++) | ||
{ | ||
CGPoint pos = [_touch[index] locationInView:[CCDirector sharedDirector].view]; | ||
pos = [[CCDirector sharedDirector] convertToGL:pos]; | ||
sum = sum + ccpDistance(centre, pos); | ||
} | ||
return(sum / (float)_touchCount); | ||
} | ||
|
||
// calculates the average rotation of all touches, relative to touch centre | ||
- (float)rotation:(CGPoint)centre | ||
{ | ||
float sum = 0; | ||
for (int index = 0; index < _touchCount; index ++) | ||
{ | ||
CGPoint pos = [_touch[index] locationInView:[CCDirector sharedDirector].view]; | ||
pos = [[CCDirector sharedDirector] convertToGL:pos]; | ||
sum = sum + ccpToAngle(ccpSub(pos, centre)); | ||
} | ||
return(sum / (float)_touchCount); | ||
} | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event | ||
{ | ||
// if max touches exceeded, drop the touch | ||
if (_touchCount == CCSpriteMultiTouchMaxTouches) | ||
{ | ||
[super touchBegan:touch withEvent:event]; | ||
return; | ||
} | ||
|
||
// add the touch to the list | ||
_touch[_touchCount] = touch; | ||
_touchCount ++; | ||
CCLOG(@"touches : %d", (int)_touchCount); | ||
|
||
// check if drag function activates | ||
if (_touchCount == _touchCountForDrag) _dragCentre = [self centre]; | ||
|
||
// check for scale function | ||
if (_touchCount == _touchCountForScale) | ||
{ | ||
_scaleCentre = [self centre]; | ||
_scaleStartDistance = [self distance:_scaleCentre]; | ||
_scaleStartScale = self.scale; | ||
} | ||
|
||
// check for rotate function | ||
if (_touchCount == _touchCountForRotate) | ||
{ | ||
_rotationCentre = [self centre]; | ||
_rotationLast = [self rotation:_rotationCentre]; | ||
} | ||
} | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
- (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event | ||
{ | ||
// check for dragging | ||
if (_touchCount == _touchCountForDrag) [self touchDrag]; | ||
|
||
// check for scaling | ||
if (_touchCount == _touchCountForScale) [self touchScale]; | ||
|
||
// check for rotation | ||
if (_touchCount == _touchCountForRotate) [self touchRotate]; | ||
} | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
- (void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event | ||
{ | ||
// remove the touch from the list | ||
for (int index = 0; index < _touchCount; index ++) | ||
{ | ||
if (_touch[index] == touch) | ||
{ | ||
_touch[index] = _touch[_touchCount - 1]; | ||
_touchCount --; | ||
return; | ||
} | ||
} | ||
} | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
- (void)touchDrag | ||
{ | ||
// OBS !! | ||
// dragging (for now) only supports defualt positioning | ||
CGPoint pos = [self centre]; | ||
CGPoint movement = ccpSub(pos, _dragCentre); | ||
self.position = ccpAdd(self.position, movement); | ||
_dragCentre = pos; | ||
} | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
- (void)touchScale | ||
{ | ||
_scaleCentre = [self centre]; | ||
float distance = [self distance:_scaleCentre]; | ||
self.scale = _scaleStartScale * (distance / _scaleStartDistance); | ||
} | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
- (void)touchRotate | ||
{ | ||
_rotationCentre = [self centre]; | ||
float rotation = [self rotation:_rotationCentre]; | ||
self.rotation = self.rotation - 180.0f / M_PI * (rotation - _rotationLast); | ||
_rotationLast = rotation; | ||
} | ||
|
||
//---------------------------------------------------------------------- | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
CCSpriteMultiTouch | ||
================== | ||
|
||
Type of class : Descendant of CCSprite | ||
Uses extension : [NONE] | ||
|
||
A sprite, capable of handling multitouch. The sprite can calculate movement, rotation and scale. | ||
|
||
To enable drag, scale and rotation, set the properties touchCountForDrag, touchCountForScale and touchCountForRotate, to something different than zero. Override the methods touchDrag, touchScale and touchRotate, to perform custom operations. | ||
|
||
Remember to set multitouchEnabled for the node, otherwise only first touch will be accepted. | ||
|
||
The #define CCSpriteMultiTouchMaxTouches, defines the maximum number of touches, accepted on the sprite (default 3) | ||
|
||
OBS!! | ||
===== | ||
Dragging for now only supports CCPositionTypePoints |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,9 @@ | |
objects = { | ||
|
||
/* Begin PBXBuildFile section */ | ||
0E21D306198A47A400E84AB1 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 0E21D305198A47A400E84AB1 /* README.md */; }; | ||
0E21D30C198A484600E84AB1 /* CCSpriteMultiTouch.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E21D30B198A484600E84AB1 /* CCSpriteMultiTouch.m */; }; | ||
0E21D30F198A637900E84AB1 /* CCSpriteMultiTouchTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E21D30E198A637900E84AB1 /* CCSpriteMultiTouchTest.m */; }; | ||
A6270E62193757E400196BD3 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A6270E61193757E400196BD3 /* QuartzCore.framework */; }; | ||
A6270E64193757E400196BD3 /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A6270E63193757E400196BD3 /* OpenGLES.framework */; }; | ||
A6270E66193757E400196BD3 /* OpenAL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A6270E65193757E400196BD3 /* OpenAL.framework */; }; | ||
|
@@ -118,6 +121,10 @@ | |
/* End PBXContainerItemProxy section */ | ||
|
||
/* Begin PBXFileReference section */ | ||
0E21D305198A47A400E84AB1 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = README.md; path = Extensions/CCSpriteMultiTouch/README.md; sourceTree = SOURCE_ROOT; }; | ||
0E21D30A198A484600E84AB1 /* CCSpriteMultiTouch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCSpriteMultiTouch.h; path = Extensions/CCSpriteMultiTouch/CCSpriteMultiTouch.h; sourceTree = SOURCE_ROOT; }; | ||
0E21D30B198A484600E84AB1 /* CCSpriteMultiTouch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CCSpriteMultiTouch.m; path = Extensions/CCSpriteMultiTouch/CCSpriteMultiTouch.m; sourceTree = SOURCE_ROOT; }; | ||
0E21D30E198A637900E84AB1 /* CCSpriteMultiTouchTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CCSpriteMultiTouchTest.m; path = Classes/Tests/CCSpriteMultiTouchTest.m; sourceTree = "<group>"; }; | ||
A6254BAF193A1438006BD09E /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = README.md; path = Extensions/CCNodeTag/README.md; sourceTree = SOURCE_ROOT; }; | ||
A6254BB1193A144A006BD09E /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = README.md; path = Extensions/CCDictionary/README.md; sourceTree = SOURCE_ROOT; }; | ||
A6254BB3193A145D006BD09E /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = README.md; path = Extensions/CCSpine/README.md; sourceTree = SOURCE_ROOT; }; | ||
|
@@ -261,6 +268,16 @@ | |
/* End PBXFrameworksBuildPhase section */ | ||
|
||
/* Begin PBXGroup section */ | ||
0E21D303198A45F800E84AB1 /* CCSpriteMultiTouch */ = { | ||
isa = PBXGroup; | ||
children = ( | ||
0E21D305198A47A400E84AB1 /* README.md */, | ||
0E21D30A198A484600E84AB1 /* CCSpriteMultiTouch.h */, | ||
0E21D30B198A484600E84AB1 /* CCSpriteMultiTouch.m */, | ||
); | ||
name = CCSpriteMultiTouch; | ||
sourceTree = "<group>"; | ||
}; | ||
A6270E55193757E300196BD3 = { | ||
isa = PBXGroup; | ||
children = ( | ||
|
@@ -434,6 +451,7 @@ | |
A6994F82194A2FAC00B97481 /* CCSpineTest.m */, | ||
A6994F83194A2FAC00B97481 /* CCTransformationNodeTest.m */, | ||
A6994F80194A2F3D00B97481 /* CCCropNodeTest.m */, | ||
0E21D30E198A637900E84AB1 /* CCSpriteMultiTouchTest.m */, | ||
A6994F7E194A2E2800B97481 /* Test helper classes */, | ||
); | ||
name = Tests; | ||
|
@@ -442,6 +460,7 @@ | |
A66C5808193A0C24009D2BD4 /* Extensions */ = { | ||
isa = PBXGroup; | ||
children = ( | ||
0E21D303198A45F800E84AB1 /* CCSpriteMultiTouch */, | ||
A6994F76194A2C8000B97481 /* CCCropNode */, | ||
A68895A91949A785002950DD /* CCTransformationNode */, | ||
A66C5809193A0C3C009D2BD4 /* CCNodeTag */, | ||
|
@@ -669,6 +688,7 @@ | |
A62712AD193769A900196BD3 /* TilesAtlassed-hd.png in Resources */, | ||
A6270E80193757E400196BD3 /* [email protected] in Resources */, | ||
A627129B193769A900196BD3 /* Interface-hd.plist in Resources */, | ||
0E21D306198A47A400E84AB1 /* README.md in Resources */, | ||
A6270EA0193757E400196BD3 /* iTunesArtwork in Resources */, | ||
A6270E9B193757E400196BD3 /* Icon-Spotlight-iOS7.png in Resources */, | ||
A6270E8F193757E400196BD3 /* [email protected] in Resources */, | ||
|
@@ -694,6 +714,7 @@ | |
isa = PBXSourcesBuildPhase; | ||
buildActionMask = 2147483647; | ||
files = ( | ||
0E21D30F198A637900E84AB1 /* CCSpriteMultiTouchTest.m in Sources */, | ||
A6994F85194A2FAC00B97481 /* CCTransformationNodeTest.m in Sources */, | ||
A627126E19375F2700196BD3 /* TestBase.m in Sources */, | ||
A66C5838193A0C87009D2BD4 /* CCSpineTexture.m in Sources */, | ||
|
@@ -712,6 +733,7 @@ | |
A66C5835193A0C87009D2BD4 /* CCSpineSkeleton.m in Sources */, | ||
A66C5836193A0C87009D2BD4 /* CCSpineSkin.m in Sources */, | ||
A6271107193757E600196BD3 /* main.m in Sources */, | ||
0E21D30C198A484600E84AB1 /* CCSpriteMultiTouch.m in Sources */, | ||
A66C5837193A0C87009D2BD4 /* CCSpineSprite.m in Sources */, | ||
A66C580E193A0C69009D2BD4 /* CCNodeTag.m in Sources */, | ||
A66C5832193A0C87009D2BD4 /* CCSpineBezierCurve.m in Sources */, | ||
|
Oops, something went wrong.