From f61fde764ba0d96835e56f8a50080554ff0a0d3d Mon Sep 17 00:00:00 2001 From: minggo Date: Wed, 31 Aug 2011 17:14:29 +0800 Subject: [PATCH] fixed #703: refactor Accelerometer --- .../CCLayer.cpp | 10 +-- .../android/CCAccelerometer_android.cpp | 17 +++-- .../android/CCAccelerometer_android.h | 3 +- .../ios/AccelerometerDelegateWrapper.h | 1 - .../ios/AccelerometerDelegateWrapper.mm | 16 +++-- cocos2dx/platform/ios/CCAccelerometer_ios.h | 3 +- cocos2dx/platform/ios/CCAccelerometer_ios.mm | 7 +- .../platform/win32/CCAccelerometer_win32.h | 4 +- .../wophone/CCAccelerometer_wophone.cpp | 64 +++++++++---------- .../wophone/CCAccelerometer_wophone.h | 7 +- 10 files changed, 61 insertions(+), 71 deletions(-) diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index a0dce5fe3e..537cad5059 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -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(); diff --git a/cocos2dx/platform/android/CCAccelerometer_android.cpp b/cocos2dx/platform/android/CCAccelerometer_android.cpp index cba35a71ca..c5cd8cc9ee 100644 --- a/cocos2dx/platform/android/CCAccelerometer_android.cpp +++ b/cocos2dx/platform/android/CCAccelerometer_android.cpp @@ -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) diff --git a/cocos2dx/platform/android/CCAccelerometer_android.h b/cocos2dx/platform/android/CCAccelerometer_android.h index b34de5b01a..e2d85cbde5 100644 --- a/cocos2dx/platform/android/CCAccelerometer_android.h +++ b/cocos2dx/platform/android/CCAccelerometer_android.h @@ -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: diff --git a/cocos2dx/platform/ios/AccelerometerDelegateWrapper.h b/cocos2dx/platform/ios/AccelerometerDelegateWrapper.h index f4c7b4621e..8cde30d697 100644 --- a/cocos2dx/platform/ios/AccelerometerDelegateWrapper.h +++ b/cocos2dx/platform/ios/AccelerometerDelegateWrapper.h @@ -38,6 +38,5 @@ + (id) sharedAccelerometerDispather; - (id) init; - (void) addDelegate: (cocos2d::CCAccelerometerDelegate *) delegate; -- (void) removeDelegate: (cocos2d::CCAccelerometerDelegate *) delegate; @end diff --git a/cocos2dx/platform/ios/AccelerometerDelegateWrapper.mm b/cocos2dx/platform/ios/AccelerometerDelegateWrapper.mm index 3c1abc5392..c6153b1fe3 100644 --- a/cocos2dx/platform/ios/AccelerometerDelegateWrapper.mm +++ b/cocos2dx/platform/ios/AccelerometerDelegateWrapper.mm @@ -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 diff --git a/cocos2dx/platform/ios/CCAccelerometer_ios.h b/cocos2dx/platform/ios/CCAccelerometer_ios.h index 06ca8ae5b7..52d5c22a8f 100644 --- a/cocos2dx/platform/ios/CCAccelerometer_ios.h +++ b/cocos2dx/platform/ios/CCAccelerometer_ios.h @@ -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; diff --git a/cocos2dx/platform/ios/CCAccelerometer_ios.mm b/cocos2dx/platform/ios/CCAccelerometer_ios.mm index 1cd3c88328..ad372f92dd 100644 --- a/cocos2dx/platform/ios/CCAccelerometer_ios.mm +++ b/cocos2dx/platform/ios/CCAccelerometer_ios.mm @@ -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]; } diff --git a/cocos2dx/platform/win32/CCAccelerometer_win32.h b/cocos2dx/platform/win32/CCAccelerometer_win32.h index 513a88a8dc..ddcc125f27 100644 --- a/cocos2dx/platform/win32/CCAccelerometer_win32.h +++ b/cocos2dx/platform/win32/CCAccelerometer_win32.h @@ -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 diff --git a/cocos2dx/platform/wophone/CCAccelerometer_wophone.cpp b/cocos2dx/platform/wophone/CCAccelerometer_wophone.cpp index 7e0b204f33..6308c4e41f 100644 --- a/cocos2dx/platform/wophone/CCAccelerometer_wophone.cpp +++ b/cocos2dx/platform/wophone/CCAccelerometer_wophone.cpp @@ -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) diff --git a/cocos2dx/platform/wophone/CCAccelerometer_wophone.h b/cocos2dx/platform/wophone/CCAccelerometer_wophone.h index dd6a7b8d02..64dd1e4e90 100644 --- a/cocos2dx/platform/wophone/CCAccelerometer_wophone.h +++ b/cocos2dx/platform/wophone/CCAccelerometer_wophone.h @@ -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