issue #2304:use function to simplify acceremeter call back

This commit is contained in:
minggo 2013-06-19 16:10:53 +08:00
parent b91e432258
commit ccbd0a831d
24 changed files with 185 additions and 267 deletions

View File

@ -235,11 +235,11 @@ void CCLayer::setAccelerometerEnabled(bool enabled)
CCDirector* pDirector = CCDirector::sharedDirector();
if (enabled)
{
pDirector->getAccelerometer()->setDelegate(this);
pDirector->getAccelerometer()->setDelegate(CC_CALLBACK_1(CCLayer::didAccelerate, this));
}
else
{
pDirector->getAccelerometer()->setDelegate(NULL);
pDirector->getAccelerometer()->setDelegate(nullptr);
}
}
}
@ -379,7 +379,7 @@ void CCLayer::onEnter()
// add this layer to concern the Accelerometer Sensor
if (_accelerometerEnabled)
{
pDirector->getAccelerometer()->setDelegate(this);
pDirector->getAccelerometer()->setDelegate(CC_CALLBACK_1(CCLayer::didAccelerate, this));
}
// add this layer to concern the keypad msg
@ -419,7 +419,7 @@ void CCLayer::onEnterTransitionDidFinish()
if (_accelerometerEnabled)
{
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getAccelerometer()->setDelegate(this);
pDirector->getAccelerometer()->setDelegate(CC_CALLBACK_1(CCLayer::didAccelerate, this));
}
CCNode::onEnterTransitionDidFinish();

View File

@ -60,7 +60,7 @@ All features from CCNode are valid, plus the following new features:
- It can receive iPhone Touches
- It can receive Accelerometer input
*/
class CC_DLL CCLayer : public CCNode, public CCTouchDelegate, public CCAccelerometerDelegate, public CCKeypadDelegate
class CC_DLL CCLayer : public CCNode, public CCTouchDelegate, public CCKeypadDelegate
{
public:
CCLayer();

View File

@ -43,17 +43,6 @@ public:
CCAcceleration(): x(0), y(0), z(0), timestamp(0) {}
};
/**
@brief
The CCAccelerometerDelegate defines a single method for
receiving acceleration-related data from the system.
*/
class CC_DLL CCAccelerometerDelegate
{
public:
virtual void didAccelerate(CCAcceleration* pAccelerationValue) {CC_UNUSED_PARAM(pAccelerationValue);}
};
NS_CC_END
#endif

View File

@ -32,7 +32,7 @@ THE SOFTWARE.
namespace cocos2d
{
CCAccelerometer::CCAccelerometer() : _accelDelegate(NULL)
CCAccelerometer::CCAccelerometer() : _function(nullptr)
{
}
@ -41,11 +41,11 @@ namespace cocos2d
}
void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)
void CCAccelerometer::setDelegate(std::function<void(CCAcceleration*)> function)
{
_accelDelegate = pDelegate;
_function = function;
if (pDelegate)
if (_function)
{
enableAccelerometerJNI();
}
@ -63,14 +63,14 @@ namespace cocos2d
void CCAccelerometer::update(float x, float y, float z, long sensorTimeStamp)
{
if (_accelDelegate)
if (_function)
{
_accelerationValue.x = -((double)x / TG3_GRAVITY_EARTH);
_accelerationValue.y = -((double)y / TG3_GRAVITY_EARTH);
_accelerationValue.z = -((double)z / TG3_GRAVITY_EARTH);
_accelerationValue.timestamp = (double)sensorTimeStamp;
_accelDelegate->didAccelerate(&_accelerationValue);
_function(&_accelerationValue);
}
}
} // end of namespace cococs2d

View File

@ -27,21 +27,22 @@ THE SOFTWARE.
#include "platform/CCCommon.h"
#include "platform/CCAccelerometerDelegate.h"
#include <functional>
namespace cocos2d {
class CC_DLL CCAccelerometer
class CCAccelerometer
{
public:
CCAccelerometer();
~CCAccelerometer();
void setDelegate(CCAccelerometerDelegate* pDelegate);
void setDelegate(std::function<void(CCAcceleration*)> function);
void setAccelerometerInterval(float interval);
void update(float x, float y, float z, long sensorTimeStamp);
private:
CCAccelerometerDelegate* _accelDelegate;
std::function<void(CCAcceleration*)> _function;
CCAcceleration _accelerationValue;
};

View File

@ -31,7 +31,7 @@ int CCAccelerometer::_initialOrientationAngle = 0;
CCAccelerometer::CCAccelerometer()
{
_accelDelegate = NULL;
_function = nullptr;
_initialOrientationAngle = atoi(getenv("ORIENTATION"));
}
@ -40,14 +40,14 @@ CCAccelerometer::~CCAccelerometer()
}
void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)
void CCAccelerometer::setDelegate(std::function<void(CCAcceleration*)> function)
{
_accelDelegate = pDelegate;
_function = function;
}
void CCAccelerometer::update(long timeStamp, double x, double y, double z)
{
if ( _accelDelegate != NULL)
if ( _function != NULL)
{
if (getenv("WIDTH"))
{
@ -81,7 +81,7 @@ void CCAccelerometer::update(long timeStamp, double x, double y, double z)
_accelerationValue.z = z;
_accelerationValue.timestamp = (double)timeStamp;
_accelDelegate->didAccelerate(&_accelerationValue);
_function(&_accelerationValue);
}
}

View File

@ -25,6 +25,7 @@ THE SOFTWARE.
#ifndef __PLATFORM_CCACCELEROMETER_BLACKBERRY_H__
#define __PLATFORM_CCACCELEROMETER_BLACKBERRY_H__
#include <functional>
#include "platform/CCAccelerometerDelegate.h"
NS_CC_BEGIN
@ -35,12 +36,12 @@ public:
CCAccelerometer();
~CCAccelerometer();
void setDelegate(CCAccelerometerDelegate* pDelegate);
void setDelegate(std::function<void(CCAcceleration*)> function);
void setAccelerometerInterval(float interval) {CC_UNUSED_PARAM(interval);};
void update(long sensorTimeStamp, double x, double y, double z);
private:
CCAccelerometerDelegate* _accelDelegate;
std::function<void(CCAcceleration*)> _function;
CCAcceleration _accelerationValue;
static int _initialOrientationAngle;
};

View File

@ -9,6 +9,7 @@
#define CCACCELEROMETER_H_
#include "platform/CCAccelerometerDelegate.h"
#include <functional>
namespace cocos2d {
@ -20,9 +21,9 @@ public:
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);};
void removeDelegate(std::function<void(CCAcceleration*)> function) {CC_UNUSED_PARAM(function);};
void addDelegate(std::function<void(CCAcceleration*)> function) {CC_UNUSED_PARAM(function);};
void setDelegate(std::function<void(CCAcceleration*)> function) {CC_UNUSED_PARAM(function);};
void setAccelerometerInterval(float interval) {CC_UNUSED_PARAM(interval);};
};

View File

@ -1,43 +0,0 @@
/****************************************************************************
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.
****************************************************************************/
#import <Foundation/Foundation.h>
#import "CCAccelerometerDelegate.h"
#import <UIKit/UIKit.h>
@interface AccelerometerDispatcher : NSObject<UIAccelerometerDelegate>
{
cocos2d::CCAccelerometerDelegate *delegate_;
cocos2d::CCAcceleration *acceleration_;
}
@property(readwrite) cocos2d::CCAccelerometerDelegate *delegate_;
@property(readwrite) cocos2d::CCAcceleration *acceleration_;
+ (id) sharedAccelerometerDispather;
- (id) init;
- (void) addDelegate: (cocos2d::CCAccelerometerDelegate *) delegate;
- (void) setAccelerometerInterval:(float)interval;
@end

View File

@ -1,115 +0,0 @@
/****************************************************************************
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.
****************************************************************************/
#import "AccelerometerDelegateWrapper.h"
@implementation AccelerometerDispatcher
static AccelerometerDispatcher* s_pAccelerometerDispatcher;
@synthesize delegate_;
@synthesize acceleration_;
+ (id) sharedAccelerometerDispather
{
if (s_pAccelerometerDispatcher == nil) {
s_pAccelerometerDispatcher = [[self alloc] init];
}
return s_pAccelerometerDispatcher;
}
- (id) init
{
acceleration_ = new cocos2d::CCAcceleration();
return self;
}
- (void) dealloc
{
s_pAccelerometerDispatcher = 0;
delegate_ = 0;
delete acceleration_;
[super dealloc];
}
- (void) addDelegate: (cocos2d::CCAccelerometerDelegate *) delegate
{
delegate_ = delegate;
if (delegate_)
{
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
}
else
{
[[UIAccelerometer sharedAccelerometer] setDelegate:nil];
}
}
-(void) setAccelerometerInterval:(float)interval
{
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:interval];
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
if (! delegate_)
{
return;
}
acceleration_->x = acceleration.x;
acceleration_->y = acceleration.y;
acceleration_->z = acceleration.z;
acceleration_->timestamp = acceleration.timestamp;
double tmp = acceleration_->x;
switch ([[UIApplication sharedApplication] statusBarOrientation])
{
case UIInterfaceOrientationLandscapeRight:
acceleration_->x = -acceleration_->y;
acceleration_->y = tmp;
break;
case UIInterfaceOrientationLandscapeLeft:
acceleration_->x = acceleration_->y;
acceleration_->y = -tmp;
break;
case UIInterfaceOrientationPortraitUpsideDown:
acceleration_->x = -acceleration_->y;
acceleration_->y = -tmp;
break;
case UIInterfaceOrientationPortrait:
break;
}
delegate_->didAccelerate(acceleration_);
}
@end

View File

@ -25,17 +25,18 @@ THE SOFTWARE.
#ifndef __PLATFORM_IPHONE_CCACCELEROMETER_H__
#define __PLATFORM_IPHONE_CCACCELEROMETER_H__
#include <functional>
#include "platform/CCAccelerometerDelegate.h"
NS_CC_BEGIN
class CC_DLL CCAccelerometer
class CCAccelerometer
{
public:
CCAccelerometer();
~CCAccelerometer();
void setDelegate(CCAccelerometerDelegate* pDelegate);
void setDelegate(std::function<void(CCAcceleration*)> function);
void setAccelerometerInterval(float interval);
};

View File

@ -23,21 +23,124 @@
****************************************************************************/
#include "CCAccelerometer.h"
#include "AccelerometerDelegateWrapper.h"
#import <Foundation/Foundation.h>
#import "CCAccelerometerDelegate.h"
#import <UIKit/UIKit.h>
#import <functional>
@interface AccelerometerDispatcher : NSObject<UIAccelerometerDelegate>
{
std::function<void(cocos2d::CCAcceleration*)> _function;
cocos2d::CCAcceleration *_acceleration;
}
+ (id) sharedAccelerometerDispather;
- (id) init;
- (void) addDelegate: (std::function<void(cocos2d::CCAcceleration*)>) function;
- (void) setAccelerometerInterval:(float)interval;
@end
@implementation AccelerometerDispatcher
static AccelerometerDispatcher* s_pAccelerometerDispatcher;
+ (id) sharedAccelerometerDispather
{
if (s_pAccelerometerDispatcher == nil) {
s_pAccelerometerDispatcher = [[self alloc] init];
}
return s_pAccelerometerDispatcher;
}
- (id) init
{
_acceleration = new cocos2d::CCAcceleration();
return self;
}
- (void) dealloc
{
s_pAccelerometerDispatcher = nullptr;
_function = nullptr;
delete _acceleration;
[super dealloc];
}
- (void) addDelegate: (std::function<void(cocos2d::CCAcceleration*)>) function
{
_function = function;
if (_function)
{
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
}
else
{
[[UIAccelerometer sharedAccelerometer] setDelegate:nil];
}
}
-(void) setAccelerometerInterval:(float)interval
{
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:interval];
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
if (! _function)
{
return;
}
_acceleration->x = acceleration.x;
_acceleration->y = acceleration.y;
_acceleration->z = acceleration.z;
_acceleration->timestamp = acceleration.timestamp;
double tmp = _acceleration->x;
switch ([[UIApplication sharedApplication] statusBarOrientation])
{
case UIInterfaceOrientationLandscapeRight:
_acceleration->x = -_acceleration->y;
_acceleration->y = tmp;
break;
case UIInterfaceOrientationLandscapeLeft:
_acceleration->x = _acceleration->y;
_acceleration->y = -tmp;
break;
case UIInterfaceOrientationPortraitUpsideDown:
_acceleration->x = -_acceleration->y;
_acceleration->y = -tmp;
break;
case UIInterfaceOrientationPortrait:
break;
}
_function(_acceleration);
}
@end
/** Implementation of CCAccelerometer
*/
NS_CC_BEGIN
CCAccelerometer::CCAccelerometer()
{
}
CCAccelerometer::~CCAccelerometer()
{
}
void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)
CCAccelerometer::CCAccelerometer() {}
CCAccelerometer::~CCAccelerometer() {}
void CCAccelerometer::setDelegate(std::function<void (CCAcceleration *)> function)
{
[[AccelerometerDispatcher sharedAccelerometerDispather] addDelegate:pDelegate];
[[AccelerometerDispatcher sharedAccelerometerDispather] addDelegate:function];
}
void CCAccelerometer::setAccelerometerInterval(float interval)
@ -46,4 +149,3 @@ void CCAccelerometer::setAccelerometerInterval(float interval)
}
NS_CC_END

View File

@ -123,29 +123,6 @@ static bool _initWithImage(CGImageRef cgImage, tImageInfo *pImageinfo)
return true;
}
static bool _initWithFile(const char* path, tImageInfo *pImageinfo)
{
CGImageRef CGImage;
UIImage *jpg;
UIImage *png;
bool ret;
// convert jpg to png before loading the texture
NSString *fullPath = [NSString stringWithUTF8String:path];
jpg = [[UIImage alloc] initWithContentsOfFile: fullPath];
png = [[UIImage alloc] initWithData:UIImagePNGRepresentation(jpg)];
CGImage = png.CGImage;
ret = _initWithImage(CGImage, pImageinfo);
[png release];
[jpg release];
return ret;
}
static bool _initWithData(void * pBuffer, int length, tImageInfo *pImageinfo)
{
bool ret = false;
@ -482,7 +459,7 @@ bool CCImage::initWithImageData(void * pData,
CC_BREAK_IF(! pData || nDataLen <= 0);
if (eFmt == kFmtRawData)
{
bRet = _initWithRawData(pData, nDataLen, nWidth, nHeight, nBitsPerComponent, false);
bRet = initWithRawData(pData, nDataLen, nWidth, nHeight, nBitsPerComponent, false);
}
else if (eFmt == kFmtWebp)
{
@ -506,7 +483,7 @@ bool CCImage::initWithImageData(void * pData,
return bRet;
}
bool CCImage::_initWithRawData(void *pData, int nDatalen, int nWidth, int nHeight, int nBitsPerComponent, bool bPreMulti)
bool CCImage::initWithRawData(void *pData, int nDatalen, int nWidth, int nHeight, int nBitsPerComponent, bool bPreMulti)
{
bool bRet = false;
do

View File

@ -9,6 +9,7 @@
#define CCACCELEROMETER_H_
#include "platform/CCAccelerometerDelegate.h"
#include <functional>
namespace cocos2d {
@ -20,9 +21,9 @@ public:
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);};
void removeDelegate(std::function<void(CCAcceleration*)> function) {CC_UNUSED_PARAM(function);};
void addDelegate(std::function<void(CCAcceleration*)> function) {CC_UNUSED_PARAM(function);};
void setDelegate(std::function<void(CCAcceleration*)> function) {CC_UNUSED_PARAM(function);};
void setAccelerometerInterval(float interval) {CC_UNUSED_PARAM(interval);};
};

View File

@ -30,7 +30,7 @@ namespace cocos2d
CCAccelerometer* CCAccelerometer::_spCCAccelerometer = NULL;
CCAccelerometer::CCAccelerometer() : _accelDelegate(NULL)
CCAccelerometer::CCAccelerometer() : _function(nullptr)
{
}
@ -53,11 +53,11 @@ CCAccelerometer* CCAccelerometer::sharedAccelerometer()
return _spCCAccelerometer;
}
void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)
void CCAccelerometer::setDelegate(std::function<void(CCAcceleration*)> function)
{
_accelDelegate = pDelegate;
_function = function;
if (pDelegate)
if (_function)
{
if (s3eAccelerometerStart() != S3E_RESULT_SUCCESS)
{
@ -72,14 +72,14 @@ void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)
void CCAccelerometer::update(float x, float y, float z, uint64 sensorTimeStamp)
{
if (_accelDelegate)
if (_function)
{
_accelerationValue.x = ((double)x)/S3E_ACCELEROMETER_1G ;
_accelerationValue.y = ((double)y)/S3E_ACCELEROMETER_1G ;
_accelerationValue.z = ((double)z)/S3E_ACCELEROMETER_1G ;
_accelerationValue.timestamp = (double)(sensorTimeStamp / 1000.0);
_accelDelegate->didAccelerate(&_accelerationValue);
_function(&_accelerationValue);
}
}

View File

@ -27,7 +27,7 @@
#include "CCAccelerometerDelegate.h"
//#include "CCMutableArray.h"
#include "ccCommon.h"
#include <functional>
namespace cocos2d {
@ -47,13 +47,13 @@ public:
*/
static CCAccelerometer* sharedAccelerometer();
void setDelegate(CCAccelerometerDelegate* pDelegate);
void setDelegate(std::function<void(CCAcceleration*)> function);
void setAccelerometerInterval(float interval) {CC_UNUSED_PARAM(interval);};
void update(float x, float y, float z, uint64 sensorTimeStamp);
private:
static CCAccelerometer* _spCCAccelerometer;
CCAccelerometerDelegate* _accelDelegate;
std::function<void(CCAcceleration*)> _function;
CCAcceleration _accelerationValue;
};

View File

@ -26,6 +26,7 @@ THE SOFTWARE.
#define __CCACCELEROMETER_H__
#include "platform/CCAccelerometerDelegate.h"
#include <functional>
namespace cocos2d {
@ -37,8 +38,8 @@ public:
static CCAccelerometer* sharedAccelerometer() { return NULL; };
void removeDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);};
void addDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);};
void removeDelegate(std::function<void(CCAcceleration*)> function) {CC_UNUSED_PARAM(function);};
void addDelegate(std::function<void(CCAcceleration*)> function) {CC_UNUSED_PARAM(function);};
void setDelegate(CCAccelerometerDelegate* pDelegate) {CC_UNUSED_PARAM(pDelegate);};
void setAccelerometerInterval(float interval) {CC_UNUSED_PARAM(interval);};
};

View File

@ -30,8 +30,8 @@ using namespace Tizen::Uix::Sensor;
NS_CC_BEGIN
CCAccelerometer::CCAccelerometer()
: _accelDelegate(NULL)
, __sensorMgr(NULL)
: _function(nullptr)
, __sensorMgr(nullptr)
{
}
@ -40,11 +40,11 @@ CCAccelerometer::~CCAccelerometer()
}
void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)
void CCAccelerometer::setDelegate(std::function<void(CCAcceleration*)> function)
{
_accelDelegate = pDelegate;
_function = function;
if (pDelegate)
if (_function)
{
startSensor();
}
@ -97,7 +97,7 @@ void CCAccelerometer::stopSensor()
void CCAccelerometer::OnDataReceived(SensorType sensorType, SensorData& sensorData , result r)
{
if (_accelDelegate)
if (_function)
{
AccelerationSensorData& data = static_cast<AccelerationSensorData&>(sensorData);
AppLog("AccelerationSensorData x = %5.4f , y = %5.4f, z = %5.4f ", data.x,data.y,data.z);
@ -107,7 +107,7 @@ void CCAccelerometer::OnDataReceived(SensorType sensorType, SensorData& sensorDa
_accelerationValue.z = -data.z;
_accelerationValue.timestamp = data.timestamp;
_accelDelegate->didAccelerate(&_accelerationValue);
_function(&_accelerationValue);
}
}

View File

@ -29,6 +29,7 @@ THE SOFTWARE.
#include "platform/CCCommon.h"
#include "platform/CCAccelerometerDelegate.h"
#include <FUix.h>
#include <functional>
NS_CC_BEGIN
@ -38,7 +39,7 @@ public:
CCAccelerometer();
~CCAccelerometer();
void setDelegate(CCAccelerometerDelegate* pDelegate);
void setDelegate(std::function<void(CCAcceleration*)> function);
void setAccelerometerInterval(float interval);
void startSensor();
void stopSensor();
@ -46,7 +47,7 @@ public:
virtual void OnDataReceived(Tizen::Uix::Sensor::SensorType sensorType, Tizen::Uix::Sensor::SensorData& sensorData , result r);
private:
CCAccelerometerDelegate* _accelDelegate;
std::function<void(CCAcceleration*)> _function;
CCAcceleration _accelerationValue;
Tizen::Uix::Sensor::SensorManager* __sensorMgr;
};

View File

@ -148,7 +148,7 @@ namespace
NS_CC_BEGIN
CCAccelerometer::CCAccelerometer() :
_accelDelegate(NULL)
_function(nullptr)
{
memset(&_accelerationValue, 0, sizeof(_accelerationValue));
}
@ -158,14 +158,14 @@ CCAccelerometer::~CCAccelerometer()
}
void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)
void CCAccelerometer::setDelegate(std::function<void(CCAcceleration*)> function)
{
_accelDelegate = pDelegate;
_function = function;
// Enable/disable the accelerometer.
// Well, there isn't one on Win32 so we don't do anything other than register
// and deregister ourselves from the Windows Key handler.
if (pDelegate)
if (_function)
{
// Register our handler
CCEGLView::sharedOpenGLView()->setAccelerometerKeyHook( &myAccelerometerKeyHook );
@ -185,7 +185,7 @@ void CCAccelerometer::setAccelerometerInterval(float interval)
void CCAccelerometer::update( double x,double y,double z,double timestamp )
{
if (_accelDelegate)
if (_function)
{
_accelerationValue.x = x;
_accelerationValue.y = y;
@ -193,7 +193,7 @@ void CCAccelerometer::update( double x,double y,double z,double timestamp )
_accelerationValue.timestamp = timestamp;
// Delegate
_accelDelegate->didAccelerate(&_accelerationValue);
_function(&_accelerationValue);
}
}

View File

@ -26,6 +26,7 @@ THE SOFTWARE.
#define __PLATFORM_WIN32_UIACCELEROMETER_H__
#include "platform/CCAccelerometerDelegate.h"
#include <functional>
NS_CC_BEGIN
@ -35,12 +36,12 @@ public:
CCAccelerometer();
~CCAccelerometer();
void setDelegate(CCAccelerometerDelegate* pDelegate);
void setDelegate(std::function<void(CCAcceleration*)> function);
void setAccelerometerInterval(float interval);
void update( double x,double y,double z,double timestamp );
private:
CCAcceleration _accelerationValue;
CCAccelerometerDelegate* _accelDelegate;
std::function<void(CCAcceleration*)> _function;
};
NS_CC_END

View File

@ -1 +1 @@
06a3716ff2fdcaa883bed71ffe53793daf0f3980
08c5c5cd8dcd5b6d72678c72cd156682e33f8553

View File

@ -174,11 +174,11 @@ void CCInputDelegate::setAccelerometerEnabled(bool enabled)
CCDirector* pDirector = CCDirector::sharedDirector();
if (enabled)
{
pDirector->getAccelerometer()->setDelegate(this);
pDirector->getAccelerometer()->setDelegate(CC_CALLBACK_1(CCInputDelegate::didAccelerate, this));
}
else
{
pDirector->getAccelerometer()->setDelegate(NULL);
pDirector->getAccelerometer()->setDelegate(nullptr);
}
}
}

View File

@ -30,7 +30,7 @@ THE SOFTWARE.
NS_CC_EXT_BEGIN
class CCInputDelegate : public CCTouchDelegate, public CCAccelerometerDelegate, public CCKeypadDelegate
class CCInputDelegate : public CCTouchDelegate, public CCKeypadDelegate
{
protected:
CCInputDelegate(void);