fixed #703: refactor Accelerometer

This commit is contained in:
minggo 2011-08-31 17:14:29 +08:00
parent ec7040f4b0
commit f61fde764b
10 changed files with 61 additions and 71 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -61,45 +61,45 @@ CCAccelerometer* CCAccelerometer::sharedAccelerometer()
return &s_Accelerometer;
}
void CCAccelerometer::removeDelegate(CCAccelerometerDelegate* pDelegate)
{
m_pDelegate = NULL;
if (m_pSensor)
{
m_pSensor->Release();
m_pSensor = NULL;
}
}
void CCAccelerometer::addDelegate(CCAccelerometerDelegate* pDelegate)
void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)
{
m_pDelegate = pDelegate;
do
if (m_pDelegate)
{
do
{
if (m_pSensor)
{
break;
}
m_pSensor = TCOM_Sensors_DataType_Client::GetInstance();
if (m_pSensor)
{
m_pSensor->StartUp();
m_pSensor->SetDelay(TG3_SENSOR_DELAY_FASTEST);
TApplication* pApp = TApplication::GetCurrentApplication();
TWindow* pWnd = pApp->GetActiveWindow();
m_pSensor->SetWindowCtrlId(pWnd->GetWindowHwndId(), 0);
m_pSensor->Activate(TG3_SENSOR_TYPE_ACCELEROMETER, TRUE);
}
else
{
CCLOG("cocos2d: The Accelerometer Sensor Open failed");
}
} while (0);
}
else
{
if (m_pSensor)
{
break;
m_pSensor->Release();
m_pSensor = NULL;
}
m_pSensor = TCOM_Sensors_DataType_Client::GetInstance();
if (m_pSensor)
{
m_pSensor->StartUp();
m_pSensor->SetDelay(TG3_SENSOR_DELAY_FASTEST);
TApplication* pApp = TApplication::GetCurrentApplication();
TWindow* pWnd = pApp->GetActiveWindow();
m_pSensor->SetWindowCtrlId(pWnd->GetWindowHwndId(), 0);
m_pSensor->Activate(TG3_SENSOR_TYPE_ACCELEROMETER, TRUE);
}
else
{
CCLOG("cocos2d: The Accelerometer Sensor Open failed");
}
} while (0);
}
}
void CCAccelerometer::didAccelerate(CCAcceleration* pAccelerationValue)

View File

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