2014-01-07 11:25:07 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
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.
****************************************************************************/
2014-01-28 08:44:10 +08:00
2014-09-10 08:17:07 +08:00
#include "platform/CCPlatformConfig.h"
2014-01-28 08:44:10 +08:00
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
2016-03-21 11:02:00 +08:00
#include "platform/CCDevice.h"
2014-04-30 08:37:36 +08:00
#include "base/ccTypes.h"
2016-06-15 10:43:15 +08:00
#include "platform/apple/CCDevice-apple.h"
2014-04-30 08:37:36 +08:00
#include "base/CCEventDispatcher.h"
#include "base/CCEventAcceleration.h"
#include "base/CCDirector.h"
2013-09-03 18:22:03 +08:00
#import <UIKit/UIKit.h>
// Accelerometer
2015-12-29 07:59:36 +08:00
#if !defined(CC_TARGET_OS_TVOS)
2013-10-08 13:44:35 +08:00
#import<CoreMotion/CoreMotion.h>
2015-12-29 07:59:36 +08:00
#endif
2013-10-08 10:11:52 +08:00
#import<CoreFoundation/CoreFoundation.h>
2016-06-15 10:43:15 +08:00
#import <CoreText/CoreText.h>
2015-06-25 17:36:57 +08:00
// Vibrate
#import <AudioToolbox/AudioToolbox.h>
2016-06-15 10:43:15 +08:00
static NSAttributedString* __attributedStringWithFontSize(NSMutableAttributedString* attributedString, CGFloat fontSize)
{
{
[attributedString beginEditing];
[attributedString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
UIFont* font = value;
font = [font fontWithSize:fontSize];
[attributedString removeAttribute:NSFontAttributeName range:range];
[attributedString addAttribute:NSFontAttributeName value:font range:range];
}];
[attributedString endEditing];
}
return [[attributedString copy] autorelease];
}
static CGFloat _calculateTextDrawStartHeight(cocos2d::Device::TextAlign align, CGSize realDimensions, CGSize dimensions)
{
float startH = 0;
// vertical alignment
unsigned int vAlignment = ((int)align >> 4) & 0x0F;
switch (vAlignment) {
//bottom
case 2:startH = dimensions.height - realDimensions.height;break;
//top
case 1:startH = 0;break;
//center
case 3: startH = (dimensions.height - realDimensions.height) / 2;break;
default:
break;
}
return startH;
}
static CGSize _calculateShrinkedSizeForString(NSAttributedString **str, id font, CGSize constrainSize, bool enableWrap, int& newFontSize)
{
CGRect actualSize = CGRectMake(0, 0, constrainSize.width + 1, constrainSize.height + 1);
int fontSize = [font pointSize];
fontSize = fontSize + 1;
if (!enableWrap) {
while (actualSize.size.width > constrainSize.width ||
actualSize.size.height > constrainSize.height) {
fontSize = fontSize - 1;
NSMutableAttributedString *mutableString = [[*str mutableCopy] autorelease];
*str = __attributedStringWithFontSize(mutableString, fontSize);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)*str);
CGSize targetSize = CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX);
CGSize fitSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [(*str) length]), NULL, targetSize, NULL);
CFRelease(framesetter);
if (fitSize.width == 0 || fitSize.height == 0) {
continue;
}
actualSize.size = fitSize;
if (constrainSize.width <= 0) {
constrainSize.width = fitSize.width;
}
if (constrainSize.height <= 0) {
constrainSize.height = fitSize.height;
}
if (fontSize <= 0) {
break;
}
}
}
else {
while (actualSize.size.height > constrainSize.height ||
actualSize.size.width > constrainSize.width) {
fontSize = fontSize - 1;
NSMutableAttributedString *mutableString = [[*str mutableCopy] autorelease];
*str = __attributedStringWithFontSize(mutableString, fontSize);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)*str);
CGSize targetSize = CGSizeMake(constrainSize.width, CGFLOAT_MAX);
CGSize fitSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [(*str) length]), NULL, targetSize, NULL);
CFRelease(framesetter);
if (fitSize.width == 0 || fitSize.height == 0) {
continue;
}
actualSize.size = fitSize;
if (constrainSize.height <= 0) {
constrainSize.height = fitSize.height;
}
if (constrainSize.width <= 0) {
constrainSize.width = fitSize.width;
}
if (fontSize <= 0) {
break;
}
}
}
newFontSize = fontSize;
return CGSizeMake(actualSize.size.width, actualSize.size.height);
}
2015-02-11 16:28:43 +08:00
#define SENSOR_DELAY_GAME 0.02
2015-12-29 07:59:36 +08:00
#if !defined(CC_TARGET_OS_TVOS)
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
}
2015-10-21 14:44:51 +08:00
+ (id) sharedAccelerometerDispatcher;
2013-09-03 18:22:03 +08:00
- (id) init;
- (void) setAccelerometerEnabled: (bool) isEnabled;
- (void) setAccelerometerInterval:(float) interval;
@end
@implementation CCAccelerometerDispatcher
static CCAccelerometerDispatcher* s_pAccelerometerDispatcher;
2015-10-21 14:44:51 +08:00
+ (id) sharedAccelerometerDispatcher
2013-09-03 18:22:03 +08:00
{
if (s_pAccelerometerDispatcher == nil) {
s_pAccelerometerDispatcher = [[self alloc] init];
}
2016-06-15 10:43:15 +08:00
2013-09-03 18:22:03 +08:00
return s_pAccelerometerDispatcher;
}
- (id) init
{
2013-11-12 08:07:46 +08:00
if( (self = [super init]) ) {
2015-12-16 14:02:55 +08:00
_acceleration = new (std::nothrow) cocos2d::Acceleration();
2013-11-12 08:07:46 +08:00
_motionManager = [[CMMotionManager alloc] init];
2015-02-11 16:28:43 +08:00
_motionManager.accelerometerUpdateInterval = SENSOR_DELAY_GAME;
2013-11-12 08:07:46 +08:00
}
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;
2016-06-15 10:43:15 +08:00
2013-09-03 18:22:03 +08:00
double tmp = _acceleration->x;
2016-06-15 10:43:15 +08:00
2013-09-03 18:22:03 +08:00
switch ([[UIApplication sharedApplication] statusBarOrientation])
{
case UIInterfaceOrientationLandscapeRight:
_acceleration->x = -_acceleration->y;
_acceleration->y = tmp;
break;
2016-06-15 10:43:15 +08:00
2013-09-03 18:22:03 +08:00
case UIInterfaceOrientationLandscapeLeft:
_acceleration->x = _acceleration->y;
_acceleration->y = -tmp;
break;
2016-06-15 10:43:15 +08:00
2013-09-03 18:22:03 +08:00
case UIInterfaceOrientationPortraitUpsideDown:
_acceleration->x = -_acceleration->y;
_acceleration->y = -tmp;
break;
2016-06-15 10:43:15 +08:00
2013-09-03 18:22:03 +08:00
case UIInterfaceOrientationPortrait:
break;
2014-06-09 17:06:40 +08:00
default:
NSAssert(false, @"unknow orientation");
2013-09-03 18:22:03 +08:00
}
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
2015-12-29 07:59:36 +08:00
#endif // !defined(CC_TARGET_OS_TVOS)
2013-09-03 18:22:03 +08:00
//
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;
2016-06-15 10:43:15 +08:00
2013-02-17 17:13:06 +08:00
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
scale = [[UIScreen mainScreen] scale];
}
2016-06-15 10:43:15 +08:00
2013-02-17 17:13:06 +08:00
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)
{
2015-12-29 07:59:36 +08:00
#if !defined(CC_TARGET_OS_TVOS)
2015-10-21 14:44:51 +08:00
[[CCAccelerometerDispatcher sharedAccelerometerDispatcher] setAccelerometerEnabled:isEnabled];
2015-12-29 07:59:36 +08:00
#endif
2013-09-03 18:22:03 +08:00
}
void Device::setAccelerometerInterval(float interval)
{
2015-12-29 07:59:36 +08:00
#if !defined(CC_TARGET_OS_TVOS)
2015-10-21 14:44:51 +08:00
[[CCAccelerometerDispatcher sharedAccelerometerDispatcher] setAccelerometerInterval:interval];
2015-12-29 07:59:36 +08:00
#endif
2013-09-03 18:22:03 +08:00
}
2014-02-13 21:08:43 +08:00
typedef struct
{
unsigned int height;
unsigned int width;
bool isPremultipliedAlpha;
bool hasShadow;
CGSize shadowOffset;
float shadowBlur;
float shadowOpacity;
bool hasStroke;
float strokeColorR;
float strokeColorG;
float strokeColorB;
2015-01-20 16:04:14 +08:00
float strokeColorA;
2014-02-13 21:08:43 +08:00
float strokeSize;
float tintColorR;
float tintColorG;
float tintColorB;
2015-01-20 16:04:14 +08:00
float tintColorA;
2014-02-13 21:08:43 +08:00
2016-06-15 10:43:15 +08:00
unsigned char* data;
2014-04-01 14:47:52 +08:00
2016-06-15 10:43:15 +08:00
} tImageInfo;
2014-04-01 14:47:52 +08:00
2016-06-15 10:43:15 +08:00
static CGSize _calculateStringSize(NSAttributedString *str, id font, CGSize *constrainSize, bool enableWrap, int overflow)
2014-02-13 21:08:43 +08:00
{
CGSize textRect = CGSizeZero;
textRect.width = constrainSize->width > 0 ? constrainSize->width
2016-06-15 10:43:15 +08:00
: CGFLOAT_MAX;
2014-02-13 21:08:43 +08:00
textRect.height = constrainSize->height > 0 ? constrainSize->height
2016-06-15 10:43:15 +08:00
: CGFLOAT_MAX;
2014-02-13 21:08:43 +08:00
2016-06-15 10:43:15 +08:00
if (overflow == 1) {
if(!enableWrap) {
textRect.width = CGFLOAT_MAX;
textRect.height = CGFLOAT_MAX;
} else {
textRect.height = CGFLOAT_MAX;
}
2014-09-22 18:01:13 +08:00
}
2016-06-15 10:43:15 +08:00
CGSize dim;
dim = [str boundingRectWithSize:CGSizeMake(textRect.width, textRect.height)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
context:nil].size;
2014-06-11 21:49:08 +08:00
2014-04-01 14:47:52 +08:00
dim.width = ceilf(dim.width);
dim.height = ceilf(dim.height);
2016-06-15 10:43:15 +08:00
2014-02-13 21:08:43 +08:00
return dim;
}
2016-06-15 10:43:15 +08:00
static id _createSystemFont( const char * fontName, int size)
2014-02-13 21:08:43 +08:00
{
2016-06-15 10:43:15 +08:00
NSString * fntName = [NSString stringWithUTF8String:fontName];
// On iOS custom fonts must be listed beforehand in the App info.plist (in order to be usable) and referenced only the by the font family name itself when
// calling [UIFont fontWithName]. Therefore even if the developer adds 'SomeFont.ttf' or 'fonts/SomeFont.ttf' to the App .plist, the font must
// be referenced as 'SomeFont' when calling [UIFont fontWithName]. Hence we strip out the folder path components and the extension here in order to get just
// the font family name itself. This stripping step is required especially for references to user fonts stored in CCB files; CCB files appear to store
// the '.ttf' extensions when referring to custom fonts.
fntName = [[fntName lastPathComponent] stringByDeletingPathExtension];
// create the font
id font = [UIFont fontWithName:fntName size:size];
2014-04-01 14:47:52 +08:00
2016-06-15 10:43:15 +08:00
if (!font)
{
font = [UIFont systemFontOfSize:size];
}
return font;
}
static bool _initWithString(const char * text, cocos2d::Device::TextAlign align, const char * fontName, int size, tImageInfo* info, bool enableWrap, int overflow)
{
2014-02-13 21:08:43 +08:00
bool bRet = false;
do
{
CC_BREAK_IF(! text || ! info);
2016-06-15 10:43:15 +08:00
id font = _createSystemFont(fontName, size);
2014-02-13 21:08:43 +08:00
CC_BREAK_IF(! font);
2016-06-15 10:43:15 +08:00
NSString * str = [NSString stringWithUTF8String:text];
CGSize dimensions;
dimensions.width = info->width;
dimensions.height = info->height;
NSTextAlignment nsAlign = FontUtils::_calculateTextAlignment(align);
NSMutableParagraphStyle* paragraphStyle = FontUtils::_calculateParagraphStyle(enableWrap, overflow);
paragraphStyle.alignment = nsAlign;
// measure text size with specified font and determine the rectangle to draw text in
UIColor *foregroundColor = [UIColor colorWithRed:info->tintColorR
green:info->tintColorG
blue:info->tintColorB
alpha:info->tintColorA];
2016-06-22 10:22:05 +08:00
// adjust text rect according to overflow
2016-06-15 10:43:15 +08:00
NSMutableDictionary* tokenAttributesDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
foregroundColor,NSForegroundColorAttributeName,
font, NSFontAttributeName,
paragraphStyle, NSParagraphStyleAttributeName, nil];
NSAttributedString *stringWithAttributes =[[[NSAttributedString alloc] initWithString:str
attributes:tokenAttributesDict] autorelease];
int shrinkFontSize = size;
CGSize realDimensions;
if (overflow == 2) {
realDimensions = _calculateShrinkedSizeForString(&stringWithAttributes, font, dimensions, enableWrap, shrinkFontSize);
} else {
realDimensions = _calculateStringSize(stringWithAttributes, font, &dimensions, enableWrap, overflow);
2014-02-13 21:08:43 +08:00
}
2016-06-15 10:43:15 +08:00
2014-02-13 21:08:43 +08:00
2016-06-15 10:43:15 +08:00
CC_BREAK_IF(realDimensions.width <= 0 || realDimensions.height <= 0);
if (dimensions.width <= 0) {
dimensions.width = realDimensions.width;
2014-02-13 21:08:43 +08:00
}
2016-06-15 10:43:15 +08:00
if (dimensions.height <= 0) {
dimensions.height = realDimensions.height;
2014-02-13 21:08:43 +08:00
}
2016-06-15 10:43:15 +08:00
// compute start point
CGFloat yPadding = _calculateTextDrawStartHeight(align, realDimensions, dimensions);
CGFloat xPadding = FontUtils::_calculateTextDrawStartWidth(align, realDimensions, dimensions);
2014-02-13 21:08:43 +08:00
2016-06-15 10:43:15 +08:00
NSInteger POTWide = dimensions.width;
NSInteger POTHigh = dimensions.height;
2014-02-13 21:08:43 +08:00
2016-06-15 10:43:15 +08:00
CGRect textRect = CGRectMake(xPadding, yPadding,
realDimensions.width, realDimensions.height);
NSUInteger textureSize = POTWide * POTHigh * 4;
unsigned char* data = (unsigned char*)malloc(sizeof(unsigned char) * textureSize);
memset(data, 0, textureSize);
2014-02-13 21:08:43 +08:00
// draw text
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(data,
2016-06-15 10:43:15 +08:00
POTWide,
POTHigh,
2014-02-13 21:08:43 +08:00
8,
2016-06-15 10:43:15 +08:00
POTWide * 4,
2014-02-13 21:08:43 +08:00
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
if (!context)
{
CGColorSpaceRelease(colorSpace);
2014-02-18 10:11:04 +08:00
CC_SAFE_FREE(data);
2014-02-13 21:08:43 +08:00
break;
}
2016-06-15 10:43:15 +08:00
2014-02-13 21:08:43 +08:00
// text color
2016-06-15 10:43:15 +08:00
CGContextSetRGBFillColor(context,
info->tintColorR,
info->tintColorG,
info->tintColorB,
info->tintColorA);
2014-02-13 21:08:43 +08:00
// move Y rendering to the top of the image
2016-06-15 10:43:15 +08:00
CGContextTranslateCTM(context, 0.0f, POTHigh);
2014-02-13 21:08:43 +08:00
2016-06-15 10:43:15 +08:00
//NOTE: NSString draws in UIKit referential i.e. renders upside-down compared to CGBitmapContext referential
CGContextScaleCTM(context, 1.0f, -1.0f);
2014-02-13 21:08:43 +08:00
// store the current context
UIGraphicsPushContext(context);
2016-06-15 10:43:15 +08:00
2014-02-13 21:08:43 +08:00
CGColorSpaceRelease(colorSpace);
2016-06-15 10:43:15 +08:00
2014-03-24 11:33:52 +08:00
CGContextSetShouldSubpixelQuantizeFonts(context, false);
2016-06-15 10:43:15 +08:00
CGContextBeginTransparencyLayerWithRect(context, textRect, NULL);
2014-03-24 11:33:52 +08:00
if ( info->hasStroke )
2014-02-13 21:08:43 +08:00
{
2014-03-24 11:33:52 +08:00
CGContextSetTextDrawingMode(context, kCGTextStroke);
2016-06-15 10:43:15 +08:00
UIColor *strokeColor = [UIColor colorWithRed:info->strokeColorR
green:info->strokeColorG
blue:info->strokeColorB
alpha:info->strokeColorA];
2015-12-29 07:59:36 +08:00
2014-03-24 11:33:52 +08:00
2016-06-15 10:43:15 +08:00
NSMutableDictionary* tokenAttributesDict2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
foregroundColor,NSForegroundColorAttributeName,
font, NSFontAttributeName,
paragraphStyle, NSParagraphStyleAttributeName, nil];
[tokenAttributesDict2 setObject:[NSNumber numberWithFloat: info->strokeSize / shrinkFontSize * 100]
forKey:NSStrokeWidthAttributeName];
[tokenAttributesDict2 setObject:strokeColor forKey:NSStrokeColorAttributeName];
NSAttributedString *strokeString =[[[NSAttributedString alloc] initWithString:str
attributes:tokenAttributesDict2] autorelease];
if(overflow == 2){
_calculateShrinkedSizeForString(&strokeString, font, dimensions, enableWrap, shrinkFontSize);
2014-03-24 11:33:52 +08:00
}
2016-06-15 10:43:15 +08:00
[strokeString drawInRect:textRect];
2014-02-13 21:08:43 +08:00
}
2016-06-15 10:43:15 +08:00
2014-03-24 11:33:52 +08:00
CGContextSetTextDrawingMode(context, kCGTextFill);
2015-12-29 07:59:36 +08:00
2014-02-13 21:08:43 +08:00
// actually draw the text in the context
2016-06-15 10:43:15 +08:00
[stringWithAttributes drawInRect:textRect];
2015-12-29 07:59:36 +08:00
2014-02-13 21:08:43 +08:00
CGContextEndTransparencyLayer(context);
2016-06-15 10:43:15 +08:00
2014-02-13 21:08:43 +08:00
// pop the context
UIGraphicsPopContext();
2016-06-15 10:43:15 +08:00
2014-02-13 21:08:43 +08:00
// release the context
CGContextRelease(context);
2016-06-15 10:43:15 +08:00
2014-02-13 21:08:43 +08:00
// output params
info->data = data;
info->isPremultipliedAlpha = true;
2016-06-15 10:43:15 +08:00
info->width = static_cast<int>(POTWide);
info->height = static_cast<int>(POTHigh);
2014-02-13 21:08:43 +08:00
bRet = true;
2016-06-15 10:43:15 +08:00
2014-02-13 21:08:43 +08:00
} while (0);
2016-06-15 10:43:15 +08:00
2014-02-13 21:08:43 +08:00
return bRet;
}
2014-05-06 10:36:55 +08:00
Data Device::getTextureDataForText(const char * text, const FontDefinition& textDefinition, TextAlign align, int &width, int &height, bool& hasPremultipliedAlpha)
2014-02-13 21:08:43 +08:00
{
Data ret;
2016-06-15 10:43:15 +08:00
2014-02-13 21:08:43 +08:00
do {
tImageInfo info = {0};
info.width = textDefinition._dimensions.width;
info.height = textDefinition._dimensions.height;
info.hasShadow = textDefinition._shadow._shadowEnabled;
info.shadowOffset.width = textDefinition._shadow._shadowOffset.width;
info.shadowOffset.height = textDefinition._shadow._shadowOffset.height;
info.shadowBlur = textDefinition._shadow._shadowBlur;
info.shadowOpacity = textDefinition._shadow._shadowOpacity;
info.hasStroke = textDefinition._stroke._strokeEnabled;
2014-08-07 10:04:29 +08:00
info.strokeColorR = textDefinition._stroke._strokeColor.r / 255.0f;
info.strokeColorG = textDefinition._stroke._strokeColor.g / 255.0f;
info.strokeColorB = textDefinition._stroke._strokeColor.b / 255.0f;
2015-03-09 20:20:17 +08:00
info.strokeColorA = textDefinition._stroke._strokeAlpha / 255.0f;
2014-02-13 21:08:43 +08:00
info.strokeSize = textDefinition._stroke._strokeSize;
2014-08-07 10:04:29 +08:00
info.tintColorR = textDefinition._fontFillColor.r / 255.0f;
info.tintColorG = textDefinition._fontFillColor.g / 255.0f;
info.tintColorB = textDefinition._fontFillColor.b / 255.0f;
2015-01-20 16:04:14 +08:00
info.tintColorA = textDefinition._fontAlpha / 255.0f;
2016-06-15 10:43:15 +08:00
if (! _initWithString(text, align, textDefinition._fontName.c_str(), textDefinition._fontSize, &info, textDefinition._enableWrap, textDefinition._overflow))
2014-02-13 21:08:43 +08:00
{
break;
}
2015-09-22 18:25:43 +08:00
height = info.height;
width = info.width;
2015-06-25 17:36:57 +08:00
ret.fastSet(info.data,width * height * 4);
hasPremultipliedAlpha = true;
2014-02-13 21:08:43 +08:00
} while (0);
2016-06-15 10:43:15 +08:00
2014-02-13 21:08:43 +08:00
return ret;
}
2013-09-03 18:22:03 +08:00
2014-08-18 20:41:12 +08:00
void Device::setKeepScreenOn(bool value)
{
2014-09-18 02:52:30 +08:00
[[UIApplication sharedApplication] setIdleTimerDisabled:(BOOL)value];
2014-08-18 20:41:12 +08:00
}
2015-06-25 17:36:57 +08:00
/*!
2015-10-23 15:37:33 +08:00
@brief Only works on iOS devices that support vibration (such as iPhone). Should only be used for important alerts. Use risks rejection in iTunes Store.
2015-06-25 17:36:57 +08:00
@param duration ignored for iOS
*/
void Device::vibrate(float duration)
{
// See https://developer.apple.com/library/ios/documentation/AudioToolbox/Reference/SystemSoundServicesReference/index.html#//apple_ref/c/econst/kSystemSoundID_Vibrate
CC_UNUSED_PARAM(duration);
2016-06-15 10:43:15 +08:00
2015-06-25 17:36:57 +08:00
// automatically vibrates for approximately 0.4 seconds
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
}
2013-02-16 19:36:21 +08:00
NS_CC_END
2014-01-28 08:44:10 +08:00
#endif // CC_PLATFORM_IOS