Skip to content

Instantly share code, notes, and snippets.

@LearnCocos2D
Last active December 11, 2015 07:49
Show Gist options
  • Save LearnCocos2D/4569305 to your computer and use it in GitHub Desktop.
Save LearnCocos2D/4569305 to your computer and use it in GitHub Desktop.
Multi-Touch & Motion Protocol and related classes from KoboldTouch (http://www.koboldtouch.com). Any controller can register itself as input receiver. All controllers support the multi touch protocol, just implement the methods you need: either receive all touches, or each touch individually, or both!
/** Defines the messages a delegate can implement to handle (multi) touch events.
Simply implement one or more of the KTTouchDelegate protocol messages and register with KTMultiTouchController.
To work with multiple touches iterate over multiTouchEvent.touchEvents. You can also get all touches from uiEvent:
NSSet* touches = [multiTouchEvent.uiEvent allTouches];
*/
@protocol KTMultiTouchProtocol <NSObject>
@optional
/** One or more touches began. The KTMultiTouchEvent contains an array with all active touches. */
-(void) touchesBeganWithEvent:(KTMultiTouchEvent*)multiTouchEvent;
/** One or more touches moved. The KTMultiTouchEvent contains an array with all active touches. */
-(void) touchesMovedWithEvent:(KTMultiTouchEvent*)multiTouchEvent;
/** One or more touches ended. The KTMultiTouchEvent contains an array with all active touches. */
-(void) touchesEndedWithEvent:(KTMultiTouchEvent*)multiTouchEvent;
/** One or more touches were cancelled. The KTMultiTouchEvent contains an array with all active touches. */
-(void) touchesCancelledWithEvent:(KTMultiTouchEvent*)multiTouchEvent;
/** A single touch began. Returns the KTTouchEvent for just that touch. */
-(void) touchBeganWithEvent:(KTTouchEvent*)touchEvent;
/** A single touch moved. Returns the KTTouchEvent for just that touch. */
-(void) touchMovedWithEvent:(KTTouchEvent*)touchEvent;
/** A single touch ended. Returns the KTTouchEvent for just that touch. */
-(void) touchEndedWithEvent:(KTTouchEvent*)touchEvent;
/** A single touch was cancelled. Returns the KTTouchEvent for just that touch. */
-(void) touchCancelledWithEvent:(KTTouchEvent*)touchEvent;
@end
/** Touch event that contains all information about a touch event. */
@interface KTTouchEvent : NSObject
/** The UIEvent/NSEvent for the touch. Use this to access allTouches. */
@property (nonatomic, weak) KTEvent* event;
/** The event's UITouch/NSTouch object. Use this to access touch phase, timestape, tapCount and whether the touch is being handled by a gesture recognizer. */
@property (nonatomic, weak) KTTouch* touch;
/** The touch location in GL (cocos2d) coordinates. */
@property (nonatomic, readonly) CGPoint locationInGLView;
/** The previous touch location in GL (cocos2d) coordinates. */
@property (nonatomic, readonly) CGPoint previousLocationInGLView;
/** If a touch delegate sets this property to YES, this particular touch will not be forwarded to other delegates. Defaults to NO. */
@property (nonatomic) BOOL swallowTouch;
// internal
-(void) reset;
-(BOOL) isValid;
-(void) updateWithEvent:(KTEvent*)event touch:(KTTouch*)touch;
@end
/** Touch event that contains all information about a multitouch event. */
@interface KTMultiTouchEvent : NSObject
{
@private
NSMutableArray* _touchEvents;
}
/** A set of KTTouchEvent objects representing each touch. */
@property (nonatomic) NSArray* touchEvents;
/** If a touch delegate sets this property to YES, the touches will not be forwarded to other delegates. Defaults to NO. */
@property (nonatomic) BOOL swallowTouches;
// internal
-(void) reset;
-(void) updateWithTouches:(NSSet*)touches event:(KTEvent*)event;
@end
/** Defines messages for motion events. */
@protocol KTMotionProtocol <NSObject>
@optional
/** Implement this method to receive accelerometer events. You must first enable the scene view controller's motion
controller and add your class as delegate to receive the event. */
-(void) accelerometerDidAccelerate:(KTAccelerometerData*)accelerometerData;
@end
#if KK_PLATFORM_IOS
#import <CoreMotion/CoreMotion.h>
typedef CMAcceleration KTAcceleration;
#elif KK_PLATFORM_MAC
typedef struct {
double x;
double y;
double z;
} KTAcceleration;
typedef NSObject CMAccelerometerData;
#endif
/** Contains acceleration values in CMAcceleration structs. The raw values are as reported by the accelerometer.
The smoothAcceleration values
*/
@interface KTAccelerometerData : NSObject
/** Filtering factor determines how much of the previously filtered values are used to create smoothAcceleration
and instantAcceleration. Default value is 0.1, range between 0.0 and 1.0.
A filteringFactor of 0.1 means that only 10% of the new raw acceleration values are used to update the smoothAcceleration
and instantAccleration properties.
*/
@property (nonatomic) double filteringFactor;
/** Raw acceleration values as reported by accelerometer. */
@property (nonatomic) KTAcceleration rawAcceleration;
/** Smoothed (low-pass filter) values where sudden changes in acceleration are reduced for a smoother (less sudden) change
in acceleration values. Typically used by accelerometer controlled games since raw acceleration is too "jittery" and "jumpy".
Implementation details:
http://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MotionEvents/MotionEvents.html#//apple_ref/doc/uid/TP40009541-CH4-SW11
*/
@property (nonatomic) KTAcceleration smoothAcceleration;
/** Instant acceleration (high-pass filter) isolates the change in motion by reducing the constant effect of gravity. Use this if you want to detect
sudden changes in motion, like hitting or shaking the device.
Implementation details:
http://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MotionEvents/MotionEvents.html#//apple_ref/doc/uid/TP40009541-CH4-SW13
*/
@property (nonatomic) KTAcceleration instantAcceleration;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment