ScalingUtils.cpp
4.69 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
133
134
135
136
137
138
139
140
141
//
// ScalingUtils.cpp
// SteveMaggieCpp
//
// Created by Katarzyna Kalinowska-Górska on 18.05.2017.
//
//
#include <stdio.h>
#include "ScalingUtils.h"
float ScalingUtils::getAdjustedContentScaleFactor(){
auto cocosScaleFactor = cocos2d::Director::getInstance()->getContentScaleFactor();
if(isSmallDevice()){
return cocosScaleFactor*2;
} else {
return cocosScaleFactor;
}
}
bool ScalingUtils::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 ScalingUtils::isElementTooSmallForSmallDevice(int elementWidth){
return elementWidth < 300;
}
float ScalingUtils::getScaleForSmallDevice(){
return 1.4f;
}
float ScalingUtils::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 ScalingUtils::getDeviceAspectRatio(){
static float currentDeviceAspectRatio = cocos2d::Director::getInstance()->getWinSize().width / cocos2d::Director::getInstance()->getWinSize().height;
return currentDeviceAspectRatio;
}
void ScalingUtils::setScaledScreenSurplusSize(float width, float height)
{
_scaledScreenSurplusWidth = width;
_scaledScreenSurplusWidth = height;
}
float ScalingUtils::getScaledScreenSurplusWidth()
{
return _scaledScreenSurplusWidth;
}
float ScalingUtils::getScaledScreenSurplusHeight()
{
return _scaledScreenSurplusHeight;
}
float ScalingUtils::widthScale(){
auto winSize = cocos2d::Director::getInstance()->getWinSize();
auto ipadProSize = getDesignSize();
return winSize.width / ipadProSize.width;// * designDpi / dpi;
}
float ScalingUtils::heightScale(){
auto winSize = cocos2d::Director::getInstance()->getWinSize();
auto ipadProSize = getDesignSize();
return winSize.height / ipadProSize.height;// * designDpi / dpi;
}
float ScalingUtils::scaleAspectFitToDesignIpadProSize(){
auto winSize = cocos2d::Director::getInstance()->getWinSize();
auto ipadProSize = getDesignSize();
return MIN(winSize.width / ipadProSize.width, winSize.height / ipadProSize.height);
}
float ScalingUtils::scaleAspectFillToDesignIpadProSize(){
auto winSize = cocos2d::Director::getInstance()->getWinSize();
auto ipadProSize = getDesignSize();
return MAX(winSize.width / ipadProSize.width, winSize.height / ipadProSize.height);
}
float ScalingUtils::getScaleForFont(){
return 1.f/getAdjustedContentScaleFactor();
}
//
cocos2d::Size ScalingUtils::getDesignSize(){
return cocos2d::Size(2732, 2048);
}
float ScalingUtils::getDesignDpi(){
return 266.f;
}
float ScalingUtils::imageAspectFillGetScale(cocos2d::Size nodeSize, cocos2d::Size targetSize)
{
return MAX(targetSize.width / nodeSize.width, targetSize.height / nodeSize.height);
}
float ScalingUtils::imageAspectFitGetScale(cocos2d::Size nodeSize, cocos2d::Size targetSize)
{
return MIN(targetSize.width / nodeSize.width, targetSize.height / nodeSize.height);
}
float ScalingUtils::configureNodeDimension(float originalDimension)
{
float scale = getAdjustedContentScaleFactor();//scocos2d::Director::getInstance()->getContentScaleFactor();
return originalDimension/scale;
}
cocos2d::Point ScalingUtils::configurePoint(cocos2d::Point point)
{
auto scale = getAdjustedContentScaleFactor();//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 ScalingUtils::configureRect(cocos2d::Rect rect)
{
auto scale = getAdjustedContentScaleFactor();//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);
}