mirror of https://github.com/axmolengine/axmol.git
issue 675: dispatch accelerometer data to one delegate
This commit is contained in:
parent
762360650b
commit
b38105a70b
|
@ -3,6 +3,16 @@
|
|||
#include "cocos2d.h"
|
||||
#include "SimpleAudioEngine.h"
|
||||
|
||||
extern "C" {
|
||||
#include "lua.h"
|
||||
}
|
||||
extern "C" {
|
||||
#include "lualib.h"
|
||||
#include "lauxlib.h"
|
||||
}
|
||||
|
||||
#include "tolua++.h"
|
||||
|
||||
USING_NS_CC;
|
||||
using namespace CocosDenshion;
|
||||
|
||||
|
@ -73,6 +83,9 @@ bool AppDelegate::initInstance()
|
|||
|
||||
bool AppDelegate::applicationDidFinishLaunching()
|
||||
{
|
||||
lua_State* l;
|
||||
l = lua_open();
|
||||
|
||||
// initialize director
|
||||
CCDirector *pDirector = CCDirector::sharedDirector();
|
||||
pDirector->setOpenGLView(&CCEGLView::sharedOpenGLView());
|
||||
|
|
|
@ -49,11 +49,6 @@ class CC_DLL CCAccelerometerDelegate
|
|||
{
|
||||
public:
|
||||
virtual void didAccelerate(CCAcceleration* pAccelerationValue) {CC_UNUSED_PARAM(pAccelerationValue);}
|
||||
|
||||
//! call the release() in child layer
|
||||
virtual void AccelerometerDestroy(void) {}
|
||||
//! call the retain() in child layer
|
||||
virtual void AccelerometerKeep(void) {}
|
||||
};
|
||||
|
||||
} //namespace cocos2d
|
||||
|
|
|
@ -67,8 +67,6 @@ public:
|
|||
virtual void keep(void);
|
||||
|
||||
virtual void didAccelerate(CCAcceleration* pAccelerationValue) {CC_UNUSED_PARAM(pAccelerationValue);}
|
||||
virtual void AccelerometerDestroy(void);
|
||||
virtual void AccelerometerKeep(void);
|
||||
|
||||
virtual void KeypadDestroy();
|
||||
virtual void KeypadKeep();
|
||||
|
|
|
@ -96,16 +96,6 @@ void CCLayer::keep(void)
|
|||
this->retain();
|
||||
}
|
||||
|
||||
void CCLayer::AccelerometerDestroy(void)
|
||||
{
|
||||
this->release();
|
||||
}
|
||||
|
||||
void CCLayer::AccelerometerKeep(void)
|
||||
{
|
||||
this->retain();
|
||||
}
|
||||
|
||||
void CCLayer::KeypadDestroy()
|
||||
{
|
||||
this->release();
|
||||
|
|
|
@ -34,18 +34,17 @@ namespace cocos2d
|
|||
{
|
||||
CCAccelerometer* CCAccelerometer::m_spCCAccelerometer = NULL;
|
||||
|
||||
CCAccelerometer::CCAccelerometer() {
|
||||
m_pAccelDelegates = new std::list<CCAccelerometerDelegate*>();
|
||||
CCAccelerometer::CCAccelerometer() : m_pAccelDelegate(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
CCAccelerometer::~CCAccelerometer() {
|
||||
if ( m_pAccelDelegates ) {
|
||||
delete m_pAccelDelegates;
|
||||
m_pAccelDelegates = NULL;
|
||||
}
|
||||
CCAccelerometer::~CCAccelerometer()
|
||||
{
|
||||
m_spCCAccelerometer = NULL;
|
||||
}
|
||||
|
||||
CCAccelerometer* CCAccelerometer::sharedAccelerometer() {
|
||||
CCAccelerometer* CCAccelerometer::sharedAccelerometer()
|
||||
{
|
||||
|
||||
if (m_spCCAccelerometer == NULL)
|
||||
{
|
||||
|
@ -55,34 +54,25 @@ namespace cocos2d
|
|||
return m_spCCAccelerometer;
|
||||
}
|
||||
|
||||
void CCAccelerometer::removeDelegate(CCAccelerometerDelegate* pDelegate) {
|
||||
m_pAccelDelegates->remove(pDelegate);
|
||||
|
||||
if ( 0 == m_pAccelDelegates->size() ) {
|
||||
disableAccelerometerJNI();
|
||||
}
|
||||
void CCAccelerometer::removeDelegate(CCAccelerometerDelegate* pDelegate)
|
||||
{
|
||||
disableAccelerometerJNI();
|
||||
}
|
||||
|
||||
void CCAccelerometer::addDelegate(CCAccelerometerDelegate* pDelegate) {
|
||||
if ( 0 == m_pAccelDelegates->size() ) {
|
||||
enableAccelerometerJNI();
|
||||
}
|
||||
|
||||
m_pAccelDelegates->push_front(pDelegate);
|
||||
void CCAccelerometer::addDelegate(CCAccelerometerDelegate* pDelegate)
|
||||
{
|
||||
m_pAccelDelegate = pDelegate;
|
||||
enableAccelerometerJNI();
|
||||
}
|
||||
|
||||
void CCAccelerometer::update(float x, float y, float z, long sensorTimeStamp) {
|
||||
if ( m_pAccelDelegates != NULL && !m_pAccelDelegates->empty() ) {
|
||||
m_obAccelerationValue.x = -((double)x / TG3_GRAVITY_EARTH);
|
||||
m_obAccelerationValue.y = -((double)y / TG3_GRAVITY_EARTH);
|
||||
m_obAccelerationValue.z = -((double)z / TG3_GRAVITY_EARTH);
|
||||
m_obAccelerationValue.timestamp = (double)sensorTimeStamp;
|
||||
void CCAccelerometer::update(float x, float y, float z, long sensorTimeStamp)
|
||||
{
|
||||
m_obAccelerationValue.x = -((double)x / TG3_GRAVITY_EARTH);
|
||||
m_obAccelerationValue.y = -((double)y / TG3_GRAVITY_EARTH);
|
||||
m_obAccelerationValue.z = -((double)z / TG3_GRAVITY_EARTH);
|
||||
m_obAccelerationValue.timestamp = (double)sensorTimeStamp;
|
||||
|
||||
for(std::list<CCAccelerometerDelegate*>::const_iterator it = m_pAccelDelegates->begin(); it != m_pAccelDelegates->end(); ++it)
|
||||
{
|
||||
(*it)->didAccelerate(&m_obAccelerationValue);
|
||||
}
|
||||
}
|
||||
m_pAccelDelegate->didAccelerate(&m_obAccelerationValue);
|
||||
}
|
||||
} // end of namespace cococs2d
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ public:
|
|||
|
||||
private:
|
||||
static CCAccelerometer* m_spCCAccelerometer;
|
||||
std::list<CCAccelerometerDelegate*>* m_pAccelDelegates;
|
||||
CCAccelerometerDelegate* m_pAccelDelegate;
|
||||
CCAcceleration m_obAccelerationValue;
|
||||
};
|
||||
|
||||
|
|
|
@ -28,10 +28,12 @@
|
|||
|
||||
@interface AccelerometerDispatcher : NSObject<UIAccelerometerDelegate>
|
||||
{
|
||||
NSMutableArray *delegateWrappers;
|
||||
cocos2d::CCAccelerometerDelegate *delegate_;
|
||||
cocos2d::CCAcceleration *acceleration_;
|
||||
}
|
||||
|
||||
@property(readwrite, retain) NSMutableArray* delegateWrappers;
|
||||
@property(readwrite) cocos2d::CCAccelerometerDelegate *delegate_;
|
||||
@property(readwrite) cocos2d::CCAcceleration *acceleration_;
|
||||
|
||||
+ (id) sharedAccelerometerDispather;
|
||||
- (id) init;
|
||||
|
@ -39,16 +41,3 @@
|
|||
- (void) removeDelegate: (cocos2d::CCAccelerometerDelegate *) delegate;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@interface AccelerometerDelegateWrapper : NSObject {
|
||||
cocos2d::CCAccelerometerDelegate *delegate_;
|
||||
}
|
||||
|
||||
@property(readwrite) cocos2d::CCAccelerometerDelegate *delegate_;
|
||||
|
||||
+ (id) delegateWrapperWithDelegate:(cocos2d::CCAccelerometerDelegate *)delegate;
|
||||
- (id) initWithDelegate: (cocos2d::CCAccelerometerDelegate *)delegate;
|
||||
- (void) didAccelerate: (cocos2d::CCAcceleration *)acceleration;
|
||||
|
||||
@end
|
||||
|
|
|
@ -28,12 +28,13 @@
|
|||
|
||||
static AccelerometerDispatcher* s_pAccelerometerDispatcher;
|
||||
|
||||
@synthesize delegateWrappers;
|
||||
@synthesize delegate_;
|
||||
@synthesize acceleration_;
|
||||
|
||||
+ (id) sharedAccelerometerDispather
|
||||
{
|
||||
if (s_pAccelerometerDispatcher == nil) {
|
||||
s_pAccelerometerDispatcher = [[self alloc] init];
|
||||
s_pAccelerometerDispatcher = [self alloc];
|
||||
}
|
||||
|
||||
return s_pAccelerometerDispatcher;
|
||||
|
@ -41,107 +42,62 @@ static AccelerometerDispatcher* s_pAccelerometerDispatcher;
|
|||
|
||||
- (id) init
|
||||
{
|
||||
self.delegateWrappers = [NSMutableArray arrayWithCapacity:4];
|
||||
[[UIAccelerometer sharedAccelerometer] setDelegate: self];
|
||||
|
||||
acceleration_ = new cocos2d::CCAcceleration();
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
[[UIAccelerometer sharedAccelerometer] setDelegate: nil];
|
||||
[delegateWrappers release];
|
||||
s_pAccelerometerDispatcher = 0;
|
||||
delegate_ = 0;
|
||||
delete acceleration_;
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (id) findDelegateWrapperByDelegate: (cocos2d::CCAccelerometerDelegate *) delegate
|
||||
{
|
||||
for (AccelerometerDelegateWrapper *wrapper in delegateWrappers) {
|
||||
if (wrapper.delegate_ == delegate) {
|
||||
return wrapper;
|
||||
}
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void) addDelegate: (cocos2d::CCAccelerometerDelegate *) delegate
|
||||
{
|
||||
[delegateWrappers addObject: [AccelerometerDelegateWrapper delegateWrapperWithDelegate:delegate]];
|
||||
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
|
||||
delegate_ = delegate;
|
||||
}
|
||||
|
||||
- (void) removeDelegate: (cocos2d::CCAccelerometerDelegate *) delegate
|
||||
{
|
||||
[delegateWrappers removeObject:[self findDelegateWrapperByDelegate:delegate]];
|
||||
[[UIAccelerometer sharedAccelerometer] setDelegate:nil];
|
||||
delegate_ = 0;
|
||||
}
|
||||
|
||||
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
|
||||
{
|
||||
cocos2d::CCAcceleration accelerationCpp;
|
||||
{
|
||||
acceleration_->x = acceleration.x;
|
||||
acceleration_->y = acceleration.y;
|
||||
acceleration_->z = acceleration.z;
|
||||
acceleration_->timestamp = acceleration.timestamp;
|
||||
|
||||
accelerationCpp.x = acceleration.x;
|
||||
accelerationCpp.y = acceleration.y;
|
||||
accelerationCpp.z = acceleration.z;
|
||||
accelerationCpp.timestamp = acceleration.timestamp;
|
||||
|
||||
double tmp = accelerationCpp.x;
|
||||
double tmp = acceleration_->x;
|
||||
|
||||
switch ([[UIApplication sharedApplication] statusBarOrientation])
|
||||
{
|
||||
case UIInterfaceOrientationLandscapeRight:
|
||||
accelerationCpp.x = -acceleration.y;
|
||||
accelerationCpp.y = tmp;
|
||||
acceleration_->x = -acceleration_->y;
|
||||
acceleration_->y = tmp;
|
||||
break;
|
||||
|
||||
case UIInterfaceOrientationLandscapeLeft:
|
||||
accelerationCpp.x = acceleration.y;
|
||||
accelerationCpp.y = -tmp;
|
||||
acceleration_->x = acceleration_->y;
|
||||
acceleration_->y = -tmp;
|
||||
break;
|
||||
|
||||
case UIInterfaceOrientationPortraitUpsideDown:
|
||||
accelerationCpp.x = -accelerationCpp.y;
|
||||
accelerationCpp.y = -tmp;
|
||||
acceleration_->x = -acceleration_->y;
|
||||
acceleration_->y = -tmp;
|
||||
break;
|
||||
|
||||
case UIInterfaceOrientationPortrait:
|
||||
break;
|
||||
}
|
||||
|
||||
for (AccelerometerDelegateWrapper *wrapper in delegateWrappers) {
|
||||
[wrapper didAccelerate: &accelerationCpp];
|
||||
}
|
||||
delegate_->didAccelerate(acceleration_);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@implementation AccelerometerDelegateWrapper
|
||||
|
||||
@synthesize delegate_;
|
||||
|
||||
+ (id)delegateWrapperWithDelegate:(cocos2d::CCAccelerometerDelegate *)delegate
|
||||
{
|
||||
return [[self alloc] initWithDelegate: delegate];
|
||||
}
|
||||
|
||||
- (id) initWithDelegate: (cocos2d::CCAccelerometerDelegate *)delegate
|
||||
{
|
||||
delegate->AccelerometerKeep();
|
||||
self.delegate_ = delegate;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) didAccelerate: (cocos2d::CCAcceleration *)acceleration
|
||||
{
|
||||
self.delegate_->didAccelerate(acceleration);
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
self.delegate_->AccelerometerDestroy();
|
||||
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
Loading…
Reference in New Issue