ToyScalingUtils.cpp
4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// ToyScalingUtils.cpp
// SteveMaggieCpp
//
// Created by Katarzyna Kalinowska-Górska on 18.05.2017.
//
//
#include <stdio.h>
#include "ToyScalingUtils.h"
bool ToyScalingUtils::isSmallDevice(){
int dpi = cocos2d::Device::getDPI();
float xInches = cocos2d::Director::getInstance()->getWinSize().width / dpi;
float yInches = cocos2d::Director::getInstance()->getWinSize().height / dpi;
float diagonalInches = sqrtf(xInches * xInches + yInches * yInches);
diagonalInches = roundf(diagonalInches * 100.0f) / 100.0f;
return diagonalInches < 7.0f;
}
bool ToyScalingUtils::isElementTooSmallForSmallDevice(int elementWidth){
return elementWidth < 300;
}
float ToyScalingUtils::getScaleForSmallDevice(){
return 1.4f;
}
float ToyScalingUtils::getAspectRatioBasedModifierForVelocity(){
static float designTabletAspectRatio = 2560.f / 1600.f;//my huawei media pad m5
static float currentDeviceAspectRatio = cocos2d::Director::getInstance()->getWinSize().width / cocos2d::Director::getInstance()->getWinSize().height;
if(currentDeviceAspectRatio < 1.4){ //around 1.333 is an ipad, and it's too difficult for ipads - so this is a fix
return 0.9f;//currentDeviceAspectRatio / designTabletAspectRatio;
}
return 1.0f; //else just leave the velocity as it is - let is adjust based on scale factor only
}
float ToyScalingUtils::getDeviceAspectRatio(){
static float currentDeviceAspectRatio = cocos2d::Director::getInstance()->getWinSize().width / cocos2d::Director::getInstance()->getWinSize().height;
return currentDeviceAspectRatio;
}
void ToyScalingUtils::setScaledScreenSurplusSize(float width, float height)
{
_scaledScreenSurplusWidth = width;
_scaledScreenSurplusWidth = height;
}
float ToyScalingUtils::getScaledScreenSurplusWidth()
{
return _scaledScreenSurplusWidth;
}
float ToyScalingUtils::getScaledScreenSurplusHeight()
{
return _scaledScreenSurplusHeight;
}
float ToyScalingUtils::widthScale(){
auto winSize = cocos2d::Director::getInstance()->getWinSize();
auto ipadProSize = getDesignSize();
return winSize.width / ipadProSize.width;// * designDpi / dpi;
}
float ToyScalingUtils::heightScale(){
auto winSize = cocos2d::Director::getInstance()->getWinSize();
auto ipadProSize = getDesignSize();
return winSize.height / ipadProSize.height;// * designDpi / dpi;
}
float ToyScalingUtils::scaleAspectFitToDesignIpadProSize(){
auto winSize = cocos2d::Director::getInstance()->getWinSize();
auto ipadProSize = getDesignSize();
return MIN(winSize.width / ipadProSize.width, winSize.height / ipadProSize.height);
}
float ToyScalingUtils::scaleAspectFillToDesignIpadProSize(){
auto winSize = cocos2d::Director::getInstance()->getWinSize();
auto ipadProSize = getDesignSize();
return MAX(winSize.width / ipadProSize.width, winSize.height / ipadProSize.height);
}
float ToyScalingUtils::getScaleForFont(){
return 1.f/cocos2d::Director::getInstance()->getContentScaleFactor();
}
//
cocos2d::Size ToyScalingUtils::getDesignSize(){
return cocos2d::Size(2732, 2048);
}
float ToyScalingUtils::getDesignDpi(){
return 266.f;
}
float ToyScalingUtils::imageAspectFillGetScale(cocos2d::Size nodeSize, cocos2d::Size targetSize)
{
return MAX(targetSize.width / nodeSize.width, targetSize.height / nodeSize.height);
}
float ToyScalingUtils::imageAspectFitGetScale(cocos2d::Size nodeSize, cocos2d::Size targetSize)
{
return MIN(targetSize.width / nodeSize.width, targetSize.height / nodeSize.height);
}
float ToyScalingUtils::configureNodeDimension(float originalDimension)
{
float scale = cocos2d::Director::getInstance()->getContentScaleFactor();
return originalDimension/scale;
}
cocos2d::Point ToyScalingUtils::configurePoint(cocos2d::Point point)
{
auto scale = cocos2d::Director::getInstance()->getContentScaleFactor();
auto additionalPaddingW = _scaledScreenSurplusWidth/2;
auto additionalPaddingH = _scaledScreenSurplusHeight/2;
return cocos2d::Point(point.x/scale+additionalPaddingW, point.y/scale+additionalPaddingH);
}
cocos2d::Rect ToyScalingUtils::configureRect(cocos2d::Rect rect)
{
auto scale = cocos2d::Director::getInstance()->getContentScaleFactor();
auto additionalPaddingW = _scaledScreenSurplusWidth/2;
auto additionalPaddingH = _scaledScreenSurplusHeight/2;
return cocos2d::Rect(rect.origin.x/scale+additionalPaddingW, rect.origin.y/scale+additionalPaddingH, rect.size.width/scale, rect.size.height/scale);
}