Merge pull request #493 from minggo/iss675

issue 675
This commit is contained in:
minggo 2011-08-29 22:52:21 -07:00
commit ab470ad76d
9 changed files with 73 additions and 296 deletions

View File

@ -49,11 +49,6 @@ class CC_DLL CCAccelerometerDelegate
{ {
public: public:
virtual void didAccelerate(CCAcceleration* pAccelerationValue) {CC_UNUSED_PARAM(pAccelerationValue);} 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 } //namespace cocos2d

View File

@ -67,8 +67,6 @@ public:
virtual void keep(void); virtual void keep(void);
virtual void didAccelerate(CCAcceleration* pAccelerationValue) {CC_UNUSED_PARAM(pAccelerationValue);} virtual void didAccelerate(CCAcceleration* pAccelerationValue) {CC_UNUSED_PARAM(pAccelerationValue);}
virtual void AccelerometerDestroy(void);
virtual void AccelerometerKeep(void);
virtual void KeypadDestroy(); virtual void KeypadDestroy();
virtual void KeypadKeep(); virtual void KeypadKeep();

View File

@ -96,16 +96,6 @@ void CCLayer::keep(void)
this->retain(); this->retain();
} }
void CCLayer::AccelerometerDestroy(void)
{
this->release();
}
void CCLayer::AccelerometerKeep(void)
{
this->retain();
}
void CCLayer::KeypadDestroy() void CCLayer::KeypadDestroy()
{ {
this->release(); this->release();

View File

@ -34,18 +34,17 @@ namespace cocos2d
{ {
CCAccelerometer* CCAccelerometer::m_spCCAccelerometer = NULL; CCAccelerometer* CCAccelerometer::m_spCCAccelerometer = NULL;
CCAccelerometer::CCAccelerometer() { CCAccelerometer::CCAccelerometer() : m_pAccelDelegate(NULL)
m_pAccelDelegates = new std::list<CCAccelerometerDelegate*>(); {
} }
CCAccelerometer::~CCAccelerometer() { CCAccelerometer::~CCAccelerometer()
if ( m_pAccelDelegates ) { {
delete m_pAccelDelegates; m_spCCAccelerometer = NULL;
m_pAccelDelegates = NULL;
}
} }
CCAccelerometer* CCAccelerometer::sharedAccelerometer() { CCAccelerometer* CCAccelerometer::sharedAccelerometer()
{
if (m_spCCAccelerometer == NULL) if (m_spCCAccelerometer == NULL)
{ {
@ -55,34 +54,25 @@ namespace cocos2d
return m_spCCAccelerometer; return m_spCCAccelerometer;
} }
void CCAccelerometer::removeDelegate(CCAccelerometerDelegate* pDelegate) { void CCAccelerometer::removeDelegate(CCAccelerometerDelegate* pDelegate)
m_pAccelDelegates->remove(pDelegate); {
if ( 0 == m_pAccelDelegates->size() ) {
disableAccelerometerJNI(); disableAccelerometerJNI();
} }
}
void CCAccelerometer::addDelegate(CCAccelerometerDelegate* pDelegate) { void CCAccelerometer::addDelegate(CCAccelerometerDelegate* pDelegate)
if ( 0 == m_pAccelDelegates->size() ) { {
m_pAccelDelegate = pDelegate;
enableAccelerometerJNI(); enableAccelerometerJNI();
} }
m_pAccelDelegates->push_front(pDelegate); void CCAccelerometer::update(float x, float y, float z, long sensorTimeStamp)
} {
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.x = -((double)x / TG3_GRAVITY_EARTH);
m_obAccelerationValue.y = -((double)y / TG3_GRAVITY_EARTH); m_obAccelerationValue.y = -((double)y / TG3_GRAVITY_EARTH);
m_obAccelerationValue.z = -((double)z / TG3_GRAVITY_EARTH); m_obAccelerationValue.z = -((double)z / TG3_GRAVITY_EARTH);
m_obAccelerationValue.timestamp = (double)sensorTimeStamp; m_obAccelerationValue.timestamp = (double)sensorTimeStamp;
for(std::list<CCAccelerometerDelegate*>::const_iterator it = m_pAccelDelegates->begin(); it != m_pAccelDelegates->end(); ++it) m_pAccelDelegate->didAccelerate(&m_obAccelerationValue);
{
(*it)->didAccelerate(&m_obAccelerationValue);
}
}
} }
} // end of namespace cococs2d } // end of namespace cococs2d

View File

@ -27,7 +27,6 @@ THE SOFTWARE.
#include "CCCommon.h" #include "CCCommon.h"
#include "CCAccelerometerDelegate.h" #include "CCAccelerometerDelegate.h"
#include <list>
namespace cocos2d { namespace cocos2d {
@ -45,7 +44,7 @@ public:
private: private:
static CCAccelerometer* m_spCCAccelerometer; static CCAccelerometer* m_spCCAccelerometer;
std::list<CCAccelerometerDelegate*>* m_pAccelDelegates; CCAccelerometerDelegate* m_pAccelDelegate;
CCAcceleration m_obAccelerationValue; CCAcceleration m_obAccelerationValue;
}; };

View File

@ -28,10 +28,12 @@
@interface AccelerometerDispatcher : NSObject<UIAccelerometerDelegate> @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) sharedAccelerometerDispather;
- (id) init; - (id) init;
@ -39,16 +41,3 @@
- (void) removeDelegate: (cocos2d::CCAccelerometerDelegate *) delegate; - (void) removeDelegate: (cocos2d::CCAccelerometerDelegate *) delegate;
@end @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

View File

@ -28,7 +28,8 @@
static AccelerometerDispatcher* s_pAccelerometerDispatcher; static AccelerometerDispatcher* s_pAccelerometerDispatcher;
@synthesize delegateWrappers; @synthesize delegate_;
@synthesize acceleration_;
+ (id) sharedAccelerometerDispather + (id) sharedAccelerometerDispather
{ {
@ -41,107 +42,62 @@ static AccelerometerDispatcher* s_pAccelerometerDispatcher;
- (id) init - (id) init
{ {
self.delegateWrappers = [NSMutableArray arrayWithCapacity:4]; acceleration_ = new cocos2d::CCAcceleration();
[[UIAccelerometer sharedAccelerometer] setDelegate: self];
return self; return self;
} }
- (void) dealloc - (void) dealloc
{ {
[[UIAccelerometer sharedAccelerometer] setDelegate: nil]; s_pAccelerometerDispatcher = 0;
[delegateWrappers release]; delegate_ = 0;
delete acceleration_;
[super dealloc]; [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 - (void) addDelegate: (cocos2d::CCAccelerometerDelegate *) delegate
{ {
[delegateWrappers addObject: [AccelerometerDelegateWrapper delegateWrapperWithDelegate:delegate]]; [[UIAccelerometer sharedAccelerometer] setDelegate:self];
delegate_ = delegate;
} }
- (void) removeDelegate: (cocos2d::CCAccelerometerDelegate *) delegate - (void) removeDelegate: (cocos2d::CCAccelerometerDelegate *) delegate
{ {
[delegateWrappers removeObject:[self findDelegateWrapperByDelegate:delegate]]; [[UIAccelerometer sharedAccelerometer] setDelegate:nil];
delegate_ = 0;
} }
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration - (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; double tmp = acceleration_->x;
accelerationCpp.y = acceleration.y;
accelerationCpp.z = acceleration.z;
accelerationCpp.timestamp = acceleration.timestamp;
double tmp = accelerationCpp.x;
switch ([[UIApplication sharedApplication] statusBarOrientation]) switch ([[UIApplication sharedApplication] statusBarOrientation])
{ {
case UIInterfaceOrientationLandscapeRight: case UIInterfaceOrientationLandscapeRight:
accelerationCpp.x = -acceleration.y; acceleration_->x = -acceleration_->y;
accelerationCpp.y = tmp; acceleration_->y = tmp;
break; break;
case UIInterfaceOrientationLandscapeLeft: case UIInterfaceOrientationLandscapeLeft:
accelerationCpp.x = acceleration.y; acceleration_->x = acceleration_->y;
accelerationCpp.y = -tmp; acceleration_->y = -tmp;
break; break;
case UIInterfaceOrientationPortraitUpsideDown: case UIInterfaceOrientationPortraitUpsideDown:
accelerationCpp.x = -accelerationCpp.y; acceleration_->x = -acceleration_->y;
accelerationCpp.y = -tmp; acceleration_->y = -tmp;
break; break;
case UIInterfaceOrientationPortrait: case UIInterfaceOrientationPortrait:
break; break;
} }
for (AccelerometerDelegateWrapper *wrapper in delegateWrappers) { delegate_->didAccelerate(acceleration_);
[wrapper didAccelerate: &accelerationCpp];
}
} }
@end @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

View File

@ -27,72 +27,13 @@ THE SOFTWARE.
#include "TCOM_Generic_Method_IIDs.h" #include "TCOM_Generic_Method_IIDs.h"
//Ö»ÄÜ°üº¬Ò»´Î // Can only include once
#include "TCOM_Sensors_IIDs.h" #include "TCOM_Sensors_IIDs.h"
namespace cocos2d { namespace cocos2d {
static CCAccelerometer s_Accelerometer; static CCAccelerometer s_Accelerometer;
//------------------------------------------------------------------
//
// CCAccelerometerHandler
//
//------------------------------------------------------------------
CCAccelerometerDelegate* CCAccelerometerHandler::getDelegate()
{
return m_pDelegate;
}
CCAccelerometerHandler::~CCAccelerometerHandler()
{
m_pDelegate->AccelerometerDestroy();
}
void CCAccelerometerHandler::setDelegate(CCAccelerometerDelegate *pDelegate)
{
if (pDelegate)
{
pDelegate->AccelerometerKeep();
}
if (m_pDelegate)
{
m_pDelegate->AccelerometerDestroy();
}
m_pDelegate = pDelegate;
}
bool CCAccelerometerHandler::initWithDelegate(CCAccelerometerDelegate *pDelegate)
{
assert(pDelegate != NULL);
m_pDelegate = pDelegate;
pDelegate->AccelerometerKeep();
return true;
}
CCAccelerometerHandler* CCAccelerometerHandler::handlerWithDelegate(CCAccelerometerDelegate *pDelegate)
{
CCAccelerometerHandler* pHandler = new CCAccelerometerHandler;
if (pHandler)
{
if (pHandler->initWithDelegate(pDelegate))
{
pHandler->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pHandler);
}
}
return pHandler;
}
//------------------------------------------------------------------ //------------------------------------------------------------------
// //
// CCAccelerometer // CCAccelerometer
@ -100,19 +41,19 @@ CCAccelerometerHandler* CCAccelerometerHandler::handlerWithDelegate(CCAccelerome
//------------------------------------------------------------------ //------------------------------------------------------------------
CCAccelerometer::CCAccelerometer() CCAccelerometer::CCAccelerometer()
: m_pSensor(NULL) : m_pSensor(NULL)
, m_pDelegate(NULL)
{ {
m_pDelegates = new AccDelegateArray;
} }
CCAccelerometer::~CCAccelerometer() CCAccelerometer::~CCAccelerometer()
{ {
m_pDelegates->release();
if (m_pSensor) if (m_pSensor)
{ {
m_pSensor->Release(); m_pSensor->Release();
m_pSensor = NULL; m_pSensor = NULL;
} }
m_pDelegate = NULL;
} }
CCAccelerometer* CCAccelerometer::sharedAccelerometer() CCAccelerometer* CCAccelerometer::sharedAccelerometer()
@ -122,55 +63,16 @@ CCAccelerometer* CCAccelerometer::sharedAccelerometer()
void CCAccelerometer::removeDelegate(CCAccelerometerDelegate* pDelegate) void CCAccelerometer::removeDelegate(CCAccelerometerDelegate* pDelegate)
{ {
CCAccelerometerHandler *pHandler; m_pDelegate = NULL;
CCMutableArray<CCAccelerometerHandler*>::CCMutableArrayIterator iter;
if (pDelegate)
{
for (iter = m_pDelegates->begin(); iter != m_pDelegates->end(); ++iter)
{
pHandler = *iter;
if (pHandler && pHandler->getDelegate() == pDelegate)
{
m_pDelegates->removeObject(pHandler);
break;
}
}
}
if (0 == m_pDelegates->count())
{
m_pSensor->Release(); m_pSensor->Release();
m_pSensor = NULL; m_pSensor = NULL;
}
} }
void CCAccelerometer::addDelegate(CCAccelerometerDelegate* pDelegate) void CCAccelerometer::addDelegate(CCAccelerometerDelegate* pDelegate)
{ {
CCAccelerometerHandler *pHandlerIter; m_pDelegate = pDelegate;
CCMutableArray<CCAccelerometerHandler*>::CCMutableArrayIterator iter;
if (pDelegate)
{
for (iter = m_pDelegates->begin(); iter != m_pDelegates->end(); ++iter)
{
pHandlerIter = *iter;
if (pHandlerIter && pHandlerIter->getDelegate() == pDelegate)
{
// this delegate have existed
return;
}
}
}
CCAccelerometerHandler* pHandler = CCAccelerometerHandler::handlerWithDelegate(pDelegate);
if (pHandler)
{
m_pDelegates->addObject(pHandler);
if (!m_pSensor)
{
m_pSensor = TCOM_Sensors_DataType_Client::GetInstance(); m_pSensor = TCOM_Sensors_DataType_Client::GetInstance();
if (m_pSensor) if (m_pSensor)
@ -187,25 +89,11 @@ void CCAccelerometer::addDelegate(CCAccelerometerDelegate* pDelegate)
{ {
CCLOG("cocos2d: The Accelerometer Sensor Open failed"); CCLOG("cocos2d: The Accelerometer Sensor Open failed");
} }
}
}
} }
void CCAccelerometer::didAccelerate(CCAcceleration* pAccelerationValue) void CCAccelerometer::didAccelerate(CCAcceleration* pAccelerationValue)
{ {
CCAccelerometerHandler *pHandler; m_pDelegate->didAccelerate(pAccelerationValue);
CCAccelerometerDelegate *pDelegate;
CCMutableArray<CCAccelerometerHandler*>::CCMutableArrayIterator iter;
if (m_pDelegates->count() > 0)
{
for (iter = m_pDelegates->begin(); iter != m_pDelegates->end(); ++iter)
{
pHandler = *iter;
pDelegate = pHandler->getDelegate();
pDelegate->didAccelerate(pAccelerationValue);
}
}
} }
}//namespace cocos2d }//namespace cocos2d

View File

@ -27,37 +27,11 @@ THE SOFTWARE.
#include "CCAccelerometerDelegate.h" #include "CCAccelerometerDelegate.h"
#include "TG3.h" #include "TG3.h"
#include "CCMutableArray.h"
#include "CCCommon.h" #include "CCCommon.h"
#include "TCOM_Sensors_Interface.h" #include "TCOM_Sensors_Interface.h"
namespace cocos2d { namespace cocos2d {
/**
@brief
CCAccelerometerHandler
Object than contains the CCAccelerometerDelegate.
*/
class CC_DLL CCAccelerometerHandler : public CCObject
{
public:
virtual ~CCAccelerometerHandler(void);
/** delegate */
CCAccelerometerDelegate* getDelegate();
void setDelegate(CCAccelerometerDelegate *pDelegate);
/** initializes a CCAccelerometerHandler with a delegate */
virtual bool initWithDelegate(CCAccelerometerDelegate *pDelegate);
public:
/** allocates a AccelerometerHandler with a delegate */
static CCAccelerometerHandler* handlerWithDelegate(CCAccelerometerDelegate *pDelegate);
protected:
CCAccelerometerDelegate* m_pDelegate;
};
/** /**
@brief @brief
The CCAccelerometer class lets you register to receive The CCAccelerometer class lets you register to receive
@ -90,9 +64,7 @@ public:
void didAccelerate(CCAcceleration* pAccelerationValue); void didAccelerate(CCAcceleration* pAccelerationValue);
protected: protected:
typedef CCMutableArray<CCAccelerometerHandler*> AccDelegateArray; CCAccelerometerDelegate* m_pDelegate;
AccDelegateArray* m_pDelegates;
TCOM_Sensors_DataType_Client* m_pSensor; TCOM_Sensors_DataType_Client* m_pSensor;
}; };