ScalingUtils.cpp 4.69 KB
//
//  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);
}