2013-02-16 19:36:21 +08:00
|
|
|
#include "CCDevice.h"
|
2013-09-03 18:22:03 +08:00
|
|
|
#include "ccTypes.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCEventDispatcher.h"
|
|
|
|
#include "CCEventAcceleration.h"
|
2013-10-29 16:29:29 +08:00
|
|
|
#include "CCDirector.h"
|
2013-09-03 18:22:03 +08:00
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
|
|
|
|
// Accelerometer
|
2013-10-08 13:44:35 +08:00
|
|
|
#import<CoreMotion/CoreMotion.h>
|
2013-10-08 10:11:52 +08:00
|
|
|
#import<CoreFoundation/CoreFoundation.h>
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
@interface CCAccelerometerDispatcher : NSObject<UIAccelerometerDelegate>
|
|
|
|
{
|
|
|
|
cocos2d::Acceleration *_acceleration;
|
2013-10-08 10:11:52 +08:00
|
|
|
CMMotionManager *_motionManager;
|
2013-09-03 18:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) sharedAccelerometerDispather;
|
|
|
|
- (id) init;
|
|
|
|
- (void) setAccelerometerEnabled: (bool) isEnabled;
|
|
|
|
- (void) setAccelerometerInterval:(float) interval;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation CCAccelerometerDispatcher
|
|
|
|
|
|
|
|
static CCAccelerometerDispatcher* s_pAccelerometerDispatcher;
|
|
|
|
|
|
|
|
+ (id) sharedAccelerometerDispather
|
|
|
|
{
|
|
|
|
if (s_pAccelerometerDispatcher == nil) {
|
|
|
|
s_pAccelerometerDispatcher = [[self alloc] init];
|
|
|
|
}
|
|
|
|
|
|
|
|
return s_pAccelerometerDispatcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
2013-11-12 08:07:46 +08:00
|
|
|
if( (self = [super init]) ) {
|
|
|
|
_acceleration = new cocos2d::Acceleration();
|
|
|
|
_motionManager = [[CMMotionManager alloc] init];
|
|
|
|
}
|
2013-09-03 18:22:03 +08:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
s_pAccelerometerDispatcher = nullptr;
|
|
|
|
delete _acceleration;
|
2013-10-08 10:11:52 +08:00
|
|
|
[_motionManager release];
|
2013-09-03 18:22:03 +08:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setAccelerometerEnabled: (bool) isEnabled
|
|
|
|
{
|
|
|
|
if (isEnabled)
|
|
|
|
{
|
2013-10-08 10:11:52 +08:00
|
|
|
[_motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
|
|
|
|
[self accelerometer:accelerometerData];
|
|
|
|
}];
|
2013-09-03 18:22:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-08 10:11:52 +08:00
|
|
|
[_motionManager stopAccelerometerUpdates];
|
2013-09-03 18:22:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) setAccelerometerInterval:(float)interval
|
|
|
|
{
|
2013-10-08 10:11:52 +08:00
|
|
|
_motionManager.accelerometerUpdateInterval = interval;
|
2013-09-03 18:22:03 +08:00
|
|
|
}
|
|
|
|
|
2013-10-08 10:11:52 +08:00
|
|
|
- (void)accelerometer:(CMAccelerometerData *)accelerometerData
|
2013-09-03 18:22:03 +08:00
|
|
|
{
|
2013-10-08 10:11:52 +08:00
|
|
|
_acceleration->x = accelerometerData.acceleration.x;
|
|
|
|
_acceleration->y = accelerometerData.acceleration.y;
|
|
|
|
_acceleration->z = accelerometerData.acceleration.z;
|
|
|
|
_acceleration->timestamp = accelerometerData.timestamp;
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
cocos2d::EventAcceleration event(*_acceleration);
|
2013-10-29 16:20:42 +08:00
|
|
|
auto dispatcher = cocos2d::Director::getInstance()->getEventDispatcher();
|
|
|
|
dispatcher->dispatchEvent(&event);
|
2013-09-03 18:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
//
|
2013-02-16 19:36:21 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int Device::getDPI()
|
2013-02-16 19:36:21 +08:00
|
|
|
{
|
2013-02-17 17:13:06 +08:00
|
|
|
static int dpi = -1;
|
|
|
|
|
|
|
|
if (dpi == -1)
|
|
|
|
{
|
|
|
|
float scale = 1.0f;
|
|
|
|
|
|
|
|
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
|
|
|
|
scale = [[UIScreen mainScreen] scale];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
|
|
dpi = 132 * scale;
|
|
|
|
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
|
|
|
|
dpi = 163 * scale;
|
|
|
|
} else {
|
|
|
|
dpi = 160 * scale;
|
|
|
|
}
|
2013-02-16 19:36:21 +08:00
|
|
|
}
|
|
|
|
return dpi;
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Device::setAccelerometerEnabled(bool isEnabled)
|
|
|
|
{
|
|
|
|
[[CCAccelerometerDispatcher sharedAccelerometerDispather] setAccelerometerEnabled:isEnabled];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::setAccelerometerInterval(float interval)
|
|
|
|
{
|
|
|
|
[[CCAccelerometerDispatcher sharedAccelerometerDispather] setAccelerometerInterval:interval];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-16 19:36:21 +08:00
|
|
|
NS_CC_END
|