2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-01-04 12:36:20 +08:00
|
|
|
https://adxeproject.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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 <UIKit/UIKit.h>
|
|
|
|
|
|
|
|
#include "platform/CCDevice.h"
|
|
|
|
#include "base/ccTypes.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#include "base/CCEventAcceleration.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "platform/apple/CCDevice-apple.h"
|
|
|
|
|
|
|
|
// Accelerometer
|
|
|
|
#if !defined(CC_TARGET_OS_TVOS)
|
2021-12-31 12:12:40 +08:00
|
|
|
# import <CoreMotion/CoreMotion.h>
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
2021-12-31 12:12:40 +08:00
|
|
|
#import <CoreFoundation/CoreFoundation.h>
|
2019-11-23 20:27:39 +08:00
|
|
|
#import <CoreText/CoreText.h>
|
|
|
|
// Vibrate
|
|
|
|
#import <AudioToolbox/AudioToolbox.h>
|
|
|
|
|
|
|
|
const float MAX_MEASURE_HEIGHT = 10000;
|
|
|
|
|
|
|
|
static NSAttributedString* __attributedStringWithFontSize(NSMutableAttributedString* attributedString, CGFloat fontSize)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[attributedString beginEditing];
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
[attributedString enumerateAttribute:NSFontAttributeName
|
|
|
|
inRange:NSMakeRange(0, attributedString.length)
|
|
|
|
options:0
|
|
|
|
usingBlock:^(id value, NSRange range, BOOL* stop) {
|
|
|
|
UIFont* font = value;
|
|
|
|
font = [font fontWithSize:fontSize];
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
[attributedString removeAttribute:NSFontAttributeName range:range];
|
|
|
|
[attributedString addAttribute:NSFontAttributeName value:font range:range];
|
|
|
|
}];
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
[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;
|
2021-12-31 12:12:40 +08:00
|
|
|
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;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return startH;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
static CGSize _calculateShrinkedSizeForString(NSAttributedString** str,
|
2019-11-23 20:27:39 +08:00
|
|
|
id font,
|
|
|
|
CGSize constrainSize,
|
|
|
|
bool enableWrap,
|
|
|
|
int& newFontSize)
|
|
|
|
{
|
|
|
|
CGRect actualSize = CGRectMake(0, 0, constrainSize.width + 1, constrainSize.height + 1);
|
2021-12-31 12:12:40 +08:00
|
|
|
int fontSize = [font pointSize];
|
|
|
|
fontSize = fontSize + 1;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!enableWrap)
|
|
|
|
{
|
|
|
|
while (actualSize.size.width > constrainSize.width || actualSize.size.height > constrainSize.height)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
fontSize = fontSize - 1;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
if (fontSize < 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
actualSize = CGRectMake(0, 0, 0, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
NSMutableAttributedString* mutableString = [[*str mutableCopy] autorelease];
|
|
|
|
*str = __attributedStringWithFontSize(mutableString, fontSize);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
CTFramesetterRef framesetter =
|
|
|
|
CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef) * str);
|
2019-11-23 20:27:39 +08:00
|
|
|
CGSize targetSize = CGSizeMake(MAX_MEASURE_HEIGHT, MAX_MEASURE_HEIGHT);
|
2021-12-31 12:12:40 +08:00
|
|
|
CGSize fitSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [(*str) length]),
|
|
|
|
NULL, targetSize, NULL);
|
2019-11-23 20:27:39 +08:00
|
|
|
CFRelease(framesetter);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (fitSize.width == 0 || fitSize.height == 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
actualSize.size = fitSize;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
if (constrainSize.width <= 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
constrainSize.width = fitSize.width;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
if (constrainSize.height <= 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
constrainSize.height = fitSize.height;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
if (fontSize <= 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
while (actualSize.size.height > constrainSize.height || actualSize.size.width > constrainSize.width)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
fontSize = fontSize - 1;
|
2021-12-31 12:12:40 +08:00
|
|
|
if (fontSize < 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
actualSize = CGRectMake(0, 0, 0, 0);
|
|
|
|
break;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
NSMutableAttributedString* mutableString = [[*str mutableCopy] autorelease];
|
|
|
|
*str = __attributedStringWithFontSize(mutableString, fontSize);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
CGSize fitSize = [*str boundingRectWithSize:CGSizeMake(constrainSize.width, MAX_MEASURE_HEIGHT)
|
2021-12-31 12:12:40 +08:00
|
|
|
options:(NSStringDrawingUsesLineFragmentOrigin)context:nil]
|
|
|
|
.size;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
if (fitSize.width == 0 || fitSize.height == 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
actualSize.size = fitSize;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
if (constrainSize.height <= 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
constrainSize.height = fitSize.height;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
if (constrainSize.width <= 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
constrainSize.width = fitSize.width;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
if (fontSize <= 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newFontSize = fontSize;
|
|
|
|
|
|
|
|
return CGSizeMake(ceilf(actualSize.size.width), ceilf(actualSize.size.height));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SENSOR_DELAY_GAME 0.02
|
|
|
|
|
|
|
|
#if !defined(CC_TARGET_OS_TVOS)
|
2021-12-31 12:12:40 +08:00
|
|
|
@interface CCAccelerometerDispatcher : NSObject {
|
|
|
|
cocos2d::Acceleration* _acceleration;
|
|
|
|
CMMotionManager* _motionManager;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
+ (id)sharedAccelerometerDispatcher;
|
|
|
|
- (id)init;
|
|
|
|
- (void)setAccelerometerEnabled:(bool)isEnabled;
|
|
|
|
- (void)setAccelerometerInterval:(float)interval;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation CCAccelerometerDispatcher
|
|
|
|
|
|
|
|
static CCAccelerometerDispatcher* s_pAccelerometerDispatcher;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
+ (id)sharedAccelerometerDispatcher
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (s_pAccelerometerDispatcher == nil)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
s_pAccelerometerDispatcher = [[self alloc] init];
|
|
|
|
}
|
|
|
|
|
|
|
|
return s_pAccelerometerDispatcher;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (id)init
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
_acceleration = new cocos2d::Acceleration();
|
|
|
|
_motionManager = [[CMMotionManager alloc] init];
|
2019-11-23 20:27:39 +08:00
|
|
|
_motionManager.accelerometerUpdateInterval = SENSOR_DELAY_GAME;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)dealloc
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
s_pAccelerometerDispatcher = nullptr;
|
|
|
|
delete _acceleration;
|
|
|
|
[_motionManager release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)setAccelerometerEnabled:(bool)isEnabled
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (isEnabled)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
[_motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
|
|
|
|
withHandler:^(CMAccelerometerData* accelerometerData, NSError* error) {
|
|
|
|
[self accelerometer:accelerometerData];
|
|
|
|
}];
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[_motionManager stopAccelerometerUpdates];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)setAccelerometerInterval:(float)interval
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_motionManager.accelerometerUpdateInterval = interval;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)accelerometer:(CMAccelerometerData*)accelerometerData
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
_acceleration->x = accelerometerData.acceleration.x;
|
|
|
|
_acceleration->y = accelerometerData.acceleration.y;
|
|
|
|
_acceleration->z = accelerometerData.acceleration.z;
|
2019-11-23 20:27:39 +08:00
|
|
|
_acceleration->timestamp = accelerometerData.timestamp;
|
|
|
|
|
|
|
|
double tmp = _acceleration->x;
|
|
|
|
UIInterfaceOrientation orientation;
|
|
|
|
if (@available(iOS 13.0, *))
|
|
|
|
{
|
|
|
|
orientation = [[[UIApplication sharedApplication].windows[0] windowScene] interfaceOrientation];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Fallback on earlier versions
|
|
|
|
orientation = [[UIApplication sharedApplication] statusBarOrientation];
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
switch (orientation)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
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;
|
|
|
|
default:
|
|
|
|
NSAssert(false, @"unknown orientation");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cocos2d::EventAcceleration event(*_acceleration);
|
|
|
|
auto dispatcher = cocos2d::Director::getInstance()->getEventDispatcher();
|
|
|
|
dispatcher->dispatchEvent(&event);
|
|
|
|
}
|
|
|
|
@end
|
2021-12-31 12:12:40 +08:00
|
|
|
#endif // !defined(CC_TARGET_OS_TVOS)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
int Device::getDPI()
|
|
|
|
{
|
|
|
|
static int dpi = -1;
|
|
|
|
|
|
|
|
if (dpi == -1)
|
|
|
|
{
|
|
|
|
float scale = 1.0f;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
scale = [[UIScreen mainScreen] scale];
|
|
|
|
}
|
|
|
|
|
|
|
|
UIUserInterfaceIdiom userInterfaceIdiom = [UIDevice.currentDevice userInterfaceIdiom];
|
2021-12-31 12:12:40 +08:00
|
|
|
if (userInterfaceIdiom == UIUserInterfaceIdiomPad)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
dpi = 132 * scale;
|
2021-12-31 12:12:40 +08:00
|
|
|
}
|
|
|
|
else if (userInterfaceIdiom == UIUserInterfaceIdiomPhone)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
dpi = 163 * scale;
|
2021-12-31 12:12:40 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
dpi = 160 * scale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dpi;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::setAccelerometerEnabled(bool isEnabled)
|
|
|
|
{
|
|
|
|
#if !defined(CC_TARGET_OS_TVOS)
|
|
|
|
[[CCAccelerometerDispatcher sharedAccelerometerDispatcher] setAccelerometerEnabled:isEnabled];
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::setAccelerometerInterval(float interval)
|
|
|
|
{
|
|
|
|
#if !defined(CC_TARGET_OS_TVOS)
|
|
|
|
[[CCAccelerometerDispatcher sharedAccelerometerDispatcher] setAccelerometerInterval:interval];
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
unsigned int height;
|
|
|
|
unsigned int width;
|
2021-12-31 12:12:40 +08:00
|
|
|
bool isPremultipliedAlpha;
|
|
|
|
bool hasShadow;
|
|
|
|
CGSize shadowOffset;
|
|
|
|
float shadowBlur;
|
|
|
|
float shadowOpacity;
|
|
|
|
bool hasStroke;
|
|
|
|
float strokeColorR;
|
|
|
|
float strokeColorG;
|
|
|
|
float strokeColorB;
|
|
|
|
float strokeColorA;
|
|
|
|
float strokeSize;
|
|
|
|
float tintColorR;
|
|
|
|
float tintColorG;
|
|
|
|
float tintColorB;
|
|
|
|
float tintColorA;
|
|
|
|
|
|
|
|
unsigned char* data;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
} tImageInfo;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
static CGSize _calculateStringSize(NSAttributedString* str,
|
|
|
|
id font,
|
|
|
|
CGSize* constrainSize,
|
|
|
|
bool enableWrap,
|
|
|
|
int overflow)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CGSize textRect = CGSizeZero;
|
2021-12-31 12:12:40 +08:00
|
|
|
textRect.width = constrainSize->width > 0 ? constrainSize->width : MAX_MEASURE_HEIGHT;
|
|
|
|
textRect.height = constrainSize->height > 0 ? constrainSize->height : MAX_MEASURE_HEIGHT;
|
|
|
|
|
|
|
|
if (overflow == 1)
|
|
|
|
{
|
|
|
|
if (!enableWrap)
|
|
|
|
{
|
|
|
|
textRect.width = MAX_MEASURE_HEIGHT;
|
2019-11-23 20:27:39 +08:00
|
|
|
textRect.height = MAX_MEASURE_HEIGHT;
|
2021-12-31 12:12:40 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
textRect.height = MAX_MEASURE_HEIGHT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CGSize dim;
|
|
|
|
dim = [str boundingRectWithSize:CGSizeMake(textRect.width, textRect.height)
|
2021-12-31 12:12:40 +08:00
|
|
|
options:(NSStringDrawingUsesLineFragmentOrigin)context:nil]
|
|
|
|
.size;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
dim.width = ceilf(dim.width);
|
2019-11-23 20:27:39 +08:00
|
|
|
dim.height = ceilf(dim.height);
|
|
|
|
|
|
|
|
return dim;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
static id _createSystemFont(const char* fontName, int size)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +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.
|
2019-11-23 20:27:39 +08:00
|
|
|
fntName = [[fntName lastPathComponent] stringByDeletingPathExtension];
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// create the font
|
|
|
|
id font = [UIFont fontWithName:fntName size:size];
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!font)
|
|
|
|
{
|
|
|
|
font = [UIFont systemFontOfSize:size];
|
|
|
|
}
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
static bool _initWithString(const char* text,
|
|
|
|
cocos2d::Device::TextAlign align,
|
|
|
|
const char* fontName,
|
|
|
|
int size,
|
|
|
|
tImageInfo* info,
|
|
|
|
bool enableWrap,
|
|
|
|
int overflow)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
CC_BREAK_IF(!text || !info);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
id font = _createSystemFont(fontName, size);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(!font);
|
|
|
|
|
|
|
|
NSString* str = [NSString stringWithUTF8String:text];
|
2019-11-23 20:27:39 +08:00
|
|
|
CC_BREAK_IF(!str);
|
|
|
|
|
|
|
|
CGSize dimensions;
|
2021-12-31 12:12:40 +08:00
|
|
|
dimensions.width = info->width;
|
|
|
|
dimensions.height = info->height;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
NSTextAlignment nsAlign = FontUtils::_calculateTextAlignment(align);
|
2019-11-23 20:27:39 +08:00
|
|
|
NSMutableParagraphStyle* paragraphStyle = FontUtils::_calculateParagraphStyle(enableWrap, overflow);
|
2021-12-31 12:12:40 +08:00
|
|
|
paragraphStyle.alignment = nsAlign;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// measure text size with specified font and determine the rectangle to draw text in
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UIColor* foregroundColor = [UIColor colorWithRed:info->tintColorR
|
|
|
|
green:info->tintColorG
|
|
|
|
blue:info->tintColorB
|
|
|
|
alpha:info->tintColorA];
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// adjust text rect according to overflow
|
2021-12-31 12:12:40 +08:00
|
|
|
NSMutableDictionary* tokenAttributesDict = [NSMutableDictionary
|
|
|
|
dictionaryWithObjectsAndKeys:foregroundColor, NSForegroundColorAttributeName, font, NSFontAttributeName,
|
|
|
|
paragraphStyle, NSParagraphStyleAttributeName, nil];
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
NSAttributedString* stringWithAttributes =
|
|
|
|
[[[NSAttributedString alloc] initWithString:str attributes:tokenAttributesDict] autorelease];
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
int shrinkFontSize = size;
|
|
|
|
CGSize realDimensions;
|
2021-12-31 12:12:40 +08:00
|
|
|
if (overflow == 2)
|
|
|
|
{
|
|
|
|
realDimensions =
|
|
|
|
_calculateShrinkedSizeForString(&stringWithAttributes, font, dimensions, enableWrap, shrinkFontSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
realDimensions = _calculateStringSize(stringWithAttributes, font, &dimensions, enableWrap, overflow);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_BREAK_IF(realDimensions.width <= 0 || realDimensions.height <= 0);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (dimensions.width <= 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
dimensions.width = realDimensions.width;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
if (dimensions.height <= 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
dimensions.height = realDimensions.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
// compute start point
|
|
|
|
CGFloat yPadding = _calculateTextDrawStartHeight(align, realDimensions, dimensions);
|
|
|
|
CGFloat xPadding = FontUtils::_calculateTextDrawStartWidth(align, realDimensions, dimensions);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
NSInteger POTWide = dimensions.width;
|
|
|
|
NSInteger POTHigh = dimensions.height;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
CGRect textRect = CGRectMake(xPadding, yPadding, realDimensions.width, realDimensions.height);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
NSUInteger textureSize = POTWide * POTHigh * 4;
|
2021-12-31 12:12:40 +08:00
|
|
|
unsigned char* data = (unsigned char*)malloc(sizeof(unsigned char) * textureSize);
|
2019-11-23 20:27:39 +08:00
|
|
|
memset(data, 0, textureSize);
|
|
|
|
|
|
|
|
// draw text
|
2021-12-31 12:12:40 +08:00
|
|
|
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
|
|
|
CGContextRef context = CGBitmapContextCreate(data, POTWide, POTHigh, 8, POTWide * 4, colorSpace,
|
|
|
|
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!context)
|
|
|
|
{
|
|
|
|
CGColorSpaceRelease(colorSpace);
|
|
|
|
CC_SAFE_FREE(data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// text color
|
2021-12-31 12:12:40 +08:00
|
|
|
CGContextSetRGBFillColor(context, info->tintColorR, info->tintColorG, info->tintColorB, info->tintColorA);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// move Y rendering to the top of the image
|
|
|
|
CGContextTranslateCTM(context, 0.0f, POTHigh);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
// NOTE: NSString draws in UIKit referential i.e. renders upside-down compared to CGBitmapContext referential
|
2019-11-23 20:27:39 +08:00
|
|
|
CGContextScaleCTM(context, 1.0f, -1.0f);
|
|
|
|
// store the current context
|
|
|
|
UIGraphicsPushContext(context);
|
|
|
|
|
|
|
|
CGColorSpaceRelease(colorSpace);
|
|
|
|
|
|
|
|
CGContextSetShouldSubpixelQuantizeFonts(context, false);
|
|
|
|
|
|
|
|
CGContextBeginTransparencyLayerWithRect(context, textRect, NULL);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
if (info->hasStroke)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CGContextSetTextDrawingMode(context, kCGTextStroke);
|
2021-12-31 12:12:40 +08:00
|
|
|
UIColor* strokeColor = [UIColor colorWithRed:info->strokeColorR
|
|
|
|
green:info->strokeColorG
|
|
|
|
blue:info->strokeColorB
|
|
|
|
alpha:info->strokeColorA];
|
|
|
|
|
|
|
|
NSMutableDictionary* tokenAttributesDict2 = [NSMutableDictionary
|
|
|
|
dictionaryWithObjectsAndKeys:foregroundColor, NSForegroundColorAttributeName, font, NSFontAttributeName,
|
|
|
|
paragraphStyle, NSParagraphStyleAttributeName, nil];
|
|
|
|
[tokenAttributesDict2 setObject:[NSNumber numberWithFloat:info->strokeSize / shrinkFontSize * 100]
|
2019-11-23 20:27:39 +08:00
|
|
|
forKey:NSStrokeWidthAttributeName];
|
2021-12-31 12:12:40 +08:00
|
|
|
[tokenAttributesDict2 setObject:strokeColor forKey:NSStrokeColorAttributeName];
|
|
|
|
|
|
|
|
NSAttributedString* strokeString =
|
|
|
|
[[[NSAttributedString alloc] initWithString:str attributes:tokenAttributesDict2] autorelease];
|
|
|
|
|
|
|
|
if (overflow == 2)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_calculateShrinkedSizeForString(&strokeString, font, dimensions, enableWrap, shrinkFontSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
[strokeString drawInRect:textRect];
|
|
|
|
}
|
|
|
|
|
|
|
|
CGContextSetTextDrawingMode(context, kCGTextFill);
|
|
|
|
|
|
|
|
// actually draw the text in the context
|
|
|
|
[stringWithAttributes drawInRect:textRect];
|
|
|
|
|
|
|
|
CGContextEndTransparencyLayer(context);
|
|
|
|
|
|
|
|
// pop the context
|
|
|
|
UIGraphicsPopContext();
|
|
|
|
|
|
|
|
// release the context
|
|
|
|
CGContextRelease(context);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// output params
|
|
|
|
info->data = data;
|
|
|
|
info->isPremultipliedAlpha = true;
|
|
|
|
info->width = static_cast<int>(POTWide);
|
|
|
|
info->height = static_cast<int>(POTHigh);
|
2021-12-31 12:12:40 +08:00
|
|
|
bRet = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Data Device::getTextureDataForText(const char* text,
|
|
|
|
const FontDefinition& textDefinition,
|
|
|
|
TextAlign align,
|
|
|
|
int& width,
|
|
|
|
int& height,
|
|
|
|
bool& hasPremultipliedAlpha)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Data ret;
|
|
|
|
|
2021-12-31 12:12:40 +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;
|
|
|
|
info.strokeColorR = textDefinition._stroke._strokeColor.r / 255.0f;
|
|
|
|
info.strokeColorG = textDefinition._stroke._strokeColor.g / 255.0f;
|
|
|
|
info.strokeColorB = textDefinition._stroke._strokeColor.b / 255.0f;
|
|
|
|
info.strokeColorA = textDefinition._stroke._strokeAlpha / 255.0f;
|
|
|
|
info.strokeSize = textDefinition._stroke._strokeSize;
|
|
|
|
info.tintColorR = textDefinition._fontFillColor.r / 255.0f;
|
|
|
|
info.tintColorG = textDefinition._fontFillColor.g / 255.0f;
|
|
|
|
info.tintColorB = textDefinition._fontFillColor.b / 255.0f;
|
|
|
|
info.tintColorA = textDefinition._fontAlpha / 255.0f;
|
|
|
|
|
|
|
|
if (!_initWithString(text, align, textDefinition._fontName.c_str(), textDefinition._fontSize, &info,
|
|
|
|
textDefinition._enableWrap, textDefinition._overflow))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
height = info.height;
|
2021-12-31 12:12:40 +08:00
|
|
|
width = info.width;
|
|
|
|
ret.fastSet(info.data, width * height * 4);
|
2019-11-23 20:27:39 +08:00
|
|
|
hasPremultipliedAlpha = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::setKeepScreenOn(bool value)
|
|
|
|
{
|
|
|
|
[[UIApplication sharedApplication] setIdleTimerDisabled:(BOOL)value];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2021-12-31 12:12:40 +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.
|
2019-11-23 20:27:39 +08:00
|
|
|
@param duration ignored for iOS
|
|
|
|
*/
|
|
|
|
void Device::vibrate(float duration)
|
|
|
|
{
|
|
|
|
// See http://stackoverflow.com/questions/4724980/making-the-iphone-vibrate
|
|
|
|
// should vibrate no matter it is silient or not
|
2021-12-31 12:12:40 +08:00
|
|
|
if ([[UIDevice currentDevice].model isEqualToString:@"iPhone"])
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
AudioServicesPlaySystemSound(1352); // works ALWAYS as of this post
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Not an iPhone, so doesn't have vibrate
|
|
|
|
// play the less annoying tick noise or one of your own
|
2021-12-31 12:12:40 +08:00
|
|
|
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|