Merge pull request #498 from minggo/iss703

fixed #703: refactor Accelerometer
This commit is contained in:
minggo 2011-08-31 18:30:22 -07:00
commit 018036eb28
10 changed files with 61 additions and 71 deletions

View File

@ -148,11 +148,11 @@ void CCLayer::setIsAccelerometerEnabled(bool enabled)
{ {
if (enabled) if (enabled)
{ {
CCAccelerometer::sharedAccelerometer()->addDelegate(this); CCAccelerometer::sharedAccelerometer()->setDelegate(this);
} }
else else
{ {
CCAccelerometer::sharedAccelerometer()->removeDelegate(this); CCAccelerometer::sharedAccelerometer()->setDelegate(NULL);
} }
} }
} }
@ -200,7 +200,7 @@ void CCLayer::onEnter()
// add this layer to concern the Accelerometer Sensor // add this layer to concern the Accelerometer Sensor
if (m_bIsAccelerometerEnabled) if (m_bIsAccelerometerEnabled)
{ {
CCAccelerometer::sharedAccelerometer()->addDelegate(this); CCAccelerometer::sharedAccelerometer()->setDelegate(this);
} }
// add this layer to concern the kaypad msg // add this layer to concern the kaypad msg
@ -220,7 +220,7 @@ void CCLayer::onExit()
// remove this layer from the delegates who concern Accelerometer Sensor // remove this layer from the delegates who concern Accelerometer Sensor
if (m_bIsAccelerometerEnabled) if (m_bIsAccelerometerEnabled)
{ {
CCAccelerometer::sharedAccelerometer()->removeDelegate(this); CCAccelerometer::sharedAccelerometer()->setDelegate(NULL);
} }
// remove this layer from the delegates who concern the kaypad msg // remove this layer from the delegates who concern the kaypad msg
@ -236,7 +236,7 @@ void CCLayer::onEnterTransitionDidFinish()
{ {
if (m_bIsAccelerometerEnabled) if (m_bIsAccelerometerEnabled)
{ {
CCAccelerometer::sharedAccelerometer()->addDelegate(this); CCAccelerometer::sharedAccelerometer()->setDelegate(this);
} }
CCNode::onEnterTransitionDidFinish(); CCNode::onEnterTransitionDidFinish();

View File

@ -54,15 +54,18 @@ namespace cocos2d
return m_spCCAccelerometer; return m_spCCAccelerometer;
} }
void CCAccelerometer::removeDelegate(CCAccelerometerDelegate* pDelegate) void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)
{
m_pAccelDelegate = pDelegate;
if (pDelegate)
{
enableAccelerometerJNI();
}
else
{ {
disableAccelerometerJNI(); disableAccelerometerJNI();
} }
void CCAccelerometer::addDelegate(CCAccelerometerDelegate* pDelegate)
{
m_pAccelDelegate = pDelegate;
enableAccelerometerJNI();
} }
void CCAccelerometer::update(float x, float y, float z, long sensorTimeStamp) void CCAccelerometer::update(float x, float y, float z, long sensorTimeStamp)

View File

@ -38,8 +38,7 @@ public:
static CCAccelerometer* sharedAccelerometer(); static CCAccelerometer* sharedAccelerometer();
void removeDelegate(CCAccelerometerDelegate* pDelegate); void setDelegate(CCAccelerometerDelegate* pDelegate);
void addDelegate(CCAccelerometerDelegate* pDelegate);
void update(float x, float y, float z, long sensorTimeStamp); void update(float x, float y, float z, long sensorTimeStamp);
private: private:

View File

@ -38,6 +38,5 @@
+ (id) sharedAccelerometerDispather; + (id) sharedAccelerometerDispather;
- (id) init; - (id) init;
- (void) addDelegate: (cocos2d::CCAccelerometerDelegate *) delegate; - (void) addDelegate: (cocos2d::CCAccelerometerDelegate *) delegate;
- (void) removeDelegate: (cocos2d::CCAccelerometerDelegate *) delegate;
@end @end

View File

@ -56,14 +56,16 @@ static AccelerometerDispatcher* s_pAccelerometerDispatcher;
- (void) addDelegate: (cocos2d::CCAccelerometerDelegate *) delegate - (void) addDelegate: (cocos2d::CCAccelerometerDelegate *) delegate
{ {
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
delegate_ = delegate; delegate_ = delegate;
}
- (void) removeDelegate: (cocos2d::CCAccelerometerDelegate *) delegate if (delegate_)
{
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
}
else
{ {
[[UIAccelerometer sharedAccelerometer] setDelegate:nil]; [[UIAccelerometer sharedAccelerometer] setDelegate:nil];
delegate_ = 0; }
} }
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration

View File

@ -37,8 +37,7 @@ public:
static CCAccelerometer* sharedAccelerometer(); static CCAccelerometer* sharedAccelerometer();
void removeDelegate(CCAccelerometerDelegate* pDelegate); void setDelegate(CCAccelerometerDelegate* pDelegate);
void addDelegate(CCAccelerometerDelegate* pDelegate);
private: private:
static CCAccelerometer* m_spUIAccelerometer; static CCAccelerometer* m_spUIAccelerometer;

View File

@ -46,12 +46,7 @@ namespace cocos2d {
return m_spUIAccelerometer; return m_spUIAccelerometer;
} }
void CCAccelerometer::removeDelegate(CCAccelerometerDelegate* pDelegate) void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)
{
[[AccelerometerDispatcher sharedAccelerometerDispather] removeDelegate:pDelegate];
}
void CCAccelerometer::addDelegate(CCAccelerometerDelegate* pDelegate)
{ {
[[AccelerometerDispatcher sharedAccelerometerDispather] addDelegate:pDelegate]; [[AccelerometerDispatcher sharedAccelerometerDispather] addDelegate:pDelegate];
} }

View File

@ -34,9 +34,7 @@ public:
~CCAccelerometer(); ~CCAccelerometer();
static CCAccelerometer* sharedAccelerometer() { return NULL; } static CCAccelerometer* sharedAccelerometer() { return NULL; }
void setDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);}
void removeDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);}
void addDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);}
}; };
}//namespace cocos2d }//namespace cocos2d

View File

@ -61,20 +61,12 @@ CCAccelerometer* CCAccelerometer::sharedAccelerometer()
return &s_Accelerometer; return &s_Accelerometer;
} }
void CCAccelerometer::removeDelegate(CCAccelerometerDelegate* pDelegate) void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)
{
m_pDelegate = NULL;
if (m_pSensor)
{
m_pSensor->Release();
m_pSensor = NULL;
}
}
void CCAccelerometer::addDelegate(CCAccelerometerDelegate* pDelegate)
{ {
m_pDelegate = pDelegate; m_pDelegate = pDelegate;
if (m_pDelegate)
{
do do
{ {
if (m_pSensor) if (m_pSensor)
@ -99,7 +91,15 @@ void CCAccelerometer::addDelegate(CCAccelerometerDelegate* pDelegate)
CCLOG("cocos2d: The Accelerometer Sensor Open failed"); CCLOG("cocos2d: The Accelerometer Sensor Open failed");
} }
} while (0); } while (0);
}
else
{
if (m_pSensor)
{
m_pSensor->Release();
m_pSensor = NULL;
}
}
} }
void CCAccelerometer::didAccelerate(CCAcceleration* pAccelerationValue) void CCAccelerometer::didAccelerate(CCAcceleration* pAccelerationValue)

View File

@ -51,12 +51,7 @@ public:
/** /**
@brief add delegate to concern accelerometer sensor @brief add delegate to concern accelerometer sensor
*/ */
void addDelegate(CCAccelerometerDelegate* pDelegate); void setDelegate(CCAccelerometerDelegate* pDelegate);
/**
@brief remove the delegate from the delegates who concern Accelerometer Sensor
*/
void removeDelegate(CCAccelerometerDelegate* pDelegate);
/** /**
@brief call delegates' didAccelerate function @brief call delegates' didAccelerate function