axmol/cocos2dx/platform/ios/AccelerometerDelegateWrappe...

125 lines
3.5 KiB
Plaintext
Raw Normal View History

2010-11-22 10:48:19 +08:00
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
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 "AccelerometerDelegateWrapper.h"
@implementation AccelerometerDispatcher
static AccelerometerDispatcher* s_pAccelerometerDispatcher;
@synthesize delegateWrappers;
+ (id) sharedAccelerometerDispather
{
if (s_pAccelerometerDispatcher == nil) {
s_pAccelerometerDispatcher = [[self alloc] init];
}
return s_pAccelerometerDispatcher;
}
- (id) init
{
self.delegateWrappers = [NSMutableArray arrayWithCapacity:4];
2010-11-23 14:40:10 +08:00
[[UIAccelerometer sharedAccelerometer] setDelegate: self];
2010-11-22 10:48:19 +08:00
return self;
}
- (void) dealloc
{
2010-11-23 14:40:10 +08:00
[[UIAccelerometer sharedAccelerometer] setDelegate: nil];
2010-11-22 10:48:19 +08:00
[delegateWrappers release];
[super dealloc];
}
2011-03-09 16:19:20 +08:00
- (id) findDelegateWrapperByDelegate: (cocos2d::CCAccelerometerDelegate *) delegate
2010-11-22 10:48:19 +08:00
{
for (AccelerometerDelegateWrapper *wrapper in delegateWrappers) {
if (wrapper.delegate_ == delegate) {
return wrapper;
}
}
return nil;
}
2011-03-09 16:19:20 +08:00
- (void) addDelegate: (cocos2d::CCAccelerometerDelegate *) delegate
2010-11-22 10:48:19 +08:00
{
[delegateWrappers addObject: [AccelerometerDelegateWrapper delegateWrapperWithDelegate:delegate]];
}
2011-03-09 16:19:20 +08:00
- (void) removeDelegate: (cocos2d::CCAccelerometerDelegate *) delegate
2010-11-22 10:48:19 +08:00
{
[delegateWrappers removeObject:[self findDelegateWrapperByDelegate:delegate]];
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
2011-03-09 16:19:20 +08:00
cocos2d::CCAcceleration accelerationCpp;
2010-11-22 10:48:19 +08:00
accelerationCpp.x = acceleration.x;
accelerationCpp.y = acceleration.y;
accelerationCpp.z = acceleration.z;
accelerationCpp.timestamp = acceleration.timestamp;
for (AccelerometerDelegateWrapper *wrapper in delegateWrappers) {
[wrapper didAccelerate: &accelerationCpp];
}
}
@end
@implementation AccelerometerDelegateWrapper
@synthesize delegate_;
2011-03-09 16:19:20 +08:00
+ (id)delegateWrapperWithDelegate:(cocos2d::CCAccelerometerDelegate *)delegate
2010-11-22 10:48:19 +08:00
{
return [[self alloc] initWithDelegate: delegate];
}
2011-03-09 16:19:20 +08:00
- (id) initWithDelegate: (cocos2d::CCAccelerometerDelegate *)delegate
2010-11-22 10:48:19 +08:00
{
delegate->AccelerometerKeep();
self.delegate_ = delegate;
return self;
}
2011-03-09 16:19:20 +08:00
- (void) didAccelerate: (cocos2d::CCAcceleration *)acceleration
2010-11-22 10:48:19 +08:00
{
self.delegate_->didAccelerate(acceleration);
}
- (void) dealloc
{
self.delegate_->AccelerometerDestroy();
[super dealloc];
}
@end