2010-11-10 16:29:52 +08:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2011-03-09 17:28:35 +08:00
|
|
|
|
#include "CCAccelerometer_wophone.h"
|
2011-03-07 18:20:48 +08:00
|
|
|
|
#include "CCMacros.h"
|
2010-11-10 16:29:52 +08:00
|
|
|
|
|
|
|
|
|
#include "TCOM_Generic_Method_IIDs.h"
|
|
|
|
|
|
|
|
|
|
//ֻ<>ܰ<EFBFBD><DCB0><EFBFBD>һ<EFBFBD><D2BB>
|
|
|
|
|
#include "TCOM_Sensors_IIDs.h"
|
|
|
|
|
|
|
|
|
|
namespace cocos2d {
|
|
|
|
|
|
2011-03-07 18:20:48 +08:00
|
|
|
|
static CCAccelerometer s_Accelerometer;
|
2010-11-10 16:29:52 +08:00
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
//
|
2011-03-07 18:20:48 +08:00
|
|
|
|
// CCAccelerometerHandler
|
2010-11-10 16:29:52 +08:00
|
|
|
|
//
|
|
|
|
|
//------------------------------------------------------------------
|
2011-03-07 18:20:48 +08:00
|
|
|
|
CCAccelerometerDelegate* CCAccelerometerHandler::getDelegate()
|
2010-11-10 16:29:52 +08:00
|
|
|
|
{
|
|
|
|
|
return m_pDelegate;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 18:20:48 +08:00
|
|
|
|
CCAccelerometerHandler::~CCAccelerometerHandler()
|
2010-11-10 16:29:52 +08:00
|
|
|
|
{
|
2010-11-18 18:27:05 +08:00
|
|
|
|
m_pDelegate->AccelerometerDestroy();
|
2010-11-10 16:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 18:20:48 +08:00
|
|
|
|
void CCAccelerometerHandler::setDelegate(CCAccelerometerDelegate *pDelegate)
|
2010-11-10 16:29:52 +08:00
|
|
|
|
{
|
|
|
|
|
if (pDelegate)
|
|
|
|
|
{
|
2010-11-18 18:27:05 +08:00
|
|
|
|
pDelegate->AccelerometerKeep();
|
2010-11-10 16:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_pDelegate)
|
|
|
|
|
{
|
2010-11-18 18:27:05 +08:00
|
|
|
|
m_pDelegate->AccelerometerDestroy();
|
2010-11-10 16:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
m_pDelegate = pDelegate;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 18:20:48 +08:00
|
|
|
|
bool CCAccelerometerHandler::initWithDelegate(CCAccelerometerDelegate *pDelegate)
|
2010-11-10 16:29:52 +08:00
|
|
|
|
{
|
|
|
|
|
assert(pDelegate != NULL);
|
|
|
|
|
|
|
|
|
|
m_pDelegate = pDelegate;
|
2010-11-18 18:27:05 +08:00
|
|
|
|
pDelegate->AccelerometerKeep();
|
2010-11-10 16:29:52 +08:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 18:20:48 +08:00
|
|
|
|
CCAccelerometerHandler* CCAccelerometerHandler::handlerWithDelegate(CCAccelerometerDelegate *pDelegate)
|
2010-11-10 16:29:52 +08:00
|
|
|
|
{
|
2011-03-07 18:20:48 +08:00
|
|
|
|
CCAccelerometerHandler* pHandler = new CCAccelerometerHandler;
|
2010-11-10 16:29:52 +08:00
|
|
|
|
|
|
|
|
|
if (pHandler)
|
|
|
|
|
{
|
|
|
|
|
if (pHandler->initWithDelegate(pDelegate))
|
|
|
|
|
{
|
|
|
|
|
pHandler->autorelease();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-03-07 18:20:48 +08:00
|
|
|
|
CC_SAFE_RELEASE_NULL(pHandler);
|
2010-11-10 16:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pHandler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
//
|
2011-03-07 18:20:48 +08:00
|
|
|
|
// CCAccelerometer
|
2010-11-10 16:29:52 +08:00
|
|
|
|
//
|
|
|
|
|
//------------------------------------------------------------------
|
2011-03-07 18:20:48 +08:00
|
|
|
|
CCAccelerometer::CCAccelerometer()
|
2010-11-10 16:29:52 +08:00
|
|
|
|
: m_pSensor(NULL)
|
|
|
|
|
{
|
|
|
|
|
m_pDelegates = new AccDelegateArray;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 18:20:48 +08:00
|
|
|
|
CCAccelerometer::~CCAccelerometer()
|
2010-11-10 16:29:52 +08:00
|
|
|
|
{
|
|
|
|
|
m_pDelegates->release();
|
|
|
|
|
|
|
|
|
|
if (m_pSensor)
|
|
|
|
|
{
|
|
|
|
|
m_pSensor->Release();
|
|
|
|
|
m_pSensor = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 18:20:48 +08:00
|
|
|
|
CCAccelerometer* CCAccelerometer::sharedAccelerometer()
|
2010-11-10 16:29:52 +08:00
|
|
|
|
{
|
|
|
|
|
return &s_Accelerometer;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 18:20:48 +08:00
|
|
|
|
void CCAccelerometer::removeDelegate(CCAccelerometerDelegate* pDelegate)
|
2010-11-10 16:29:52 +08:00
|
|
|
|
{
|
2011-03-07 18:20:48 +08:00
|
|
|
|
CCAccelerometerHandler *pHandler;
|
|
|
|
|
CCMutableArray<CCAccelerometerHandler*>::CCMutableArrayIterator iter;
|
2010-11-10 16:29:52 +08:00
|
|
|
|
|
|
|
|
|
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 = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 18:20:48 +08:00
|
|
|
|
void CCAccelerometer::addDelegate(CCAccelerometerDelegate* pDelegate)
|
2010-11-10 16:29:52 +08:00
|
|
|
|
{
|
2011-03-11 17:41:19 +08:00
|
|
|
|
CCAccelerometerHandler *pHandlerIter;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 18:20:48 +08:00
|
|
|
|
CCAccelerometerHandler* pHandler = CCAccelerometerHandler::handlerWithDelegate(pDelegate);
|
2010-11-10 16:29:52 +08:00
|
|
|
|
|
|
|
|
|
if (pHandler)
|
|
|
|
|
{
|
|
|
|
|
m_pDelegates->addObject(pHandler);
|
|
|
|
|
|
|
|
|
|
if (!m_pSensor)
|
|
|
|
|
{
|
|
|
|
|
m_pSensor = TCOM_Sensors_DataType_Client::GetInstance();
|
|
|
|
|
|
|
|
|
|
if (m_pSensor)
|
|
|
|
|
{
|
|
|
|
|
m_pSensor->StartUp();
|
2010-11-16 15:13:20 +08:00
|
|
|
|
m_pSensor->SetDelay(TG3_SENSOR_DELAY_FASTEST);
|
2010-11-10 16:29:52 +08:00
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-09 14:40:05 +08:00
|
|
|
|
void CCAccelerometer::didAccelerate(CCAcceleration* pAccelerationValue)
|
2010-11-10 16:29:52 +08:00
|
|
|
|
{
|
2011-03-07 18:20:48 +08:00
|
|
|
|
CCAccelerometerHandler *pHandler;
|
|
|
|
|
CCAccelerometerDelegate *pDelegate;
|
|
|
|
|
CCMutableArray<CCAccelerometerHandler*>::CCMutableArrayIterator iter;
|
2010-11-10 16:29:52 +08:00
|
|
|
|
|
|
|
|
|
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
|