HLayoutObject.cpp 2.44 KB
//
//  HLayoutObject.cpp
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 17.05.2017.
//
//

#include <stdio.h>

#include "HLayoutObject.h"
#include "HScalingUtils.h"
#include "HJSONParseUtils.h"
#include "HLayoutParser.h"

HLayoutObject::~HLayoutObject()
{
    
}

void HLayoutObject::loadCommonPropertiesFromJSON(const rapidjson::Value& jsonValue, HLayoutViewInterface* scene, const std::string resFolder, const std::string altResFolder)
{
    if(jsonValue.HasMember("type")){
        const auto& classNameJSON = jsonValue["type"];
        this->className = classNameJSON.GetString();
    } else {
        this->className = "";
    }

   if(jsonValue.HasMember("name")){
        const auto& objectNameJSON = jsonValue["name"];
        this->objectName = objectNameJSON.GetString();
   } else {
        this->objectName = "";
   }
    
    cocos2d::Node* thisAsNode = dynamic_cast<cocos2d::Node*>(this);

    if(jsonValue.HasMember("scale")){
        const auto& scaleJSON = jsonValue["scale"];
        if(scaleJSON.IsFloat() || scaleJSON.IsInt()){
            thisAsNode->setScale(scaleJSON.GetFloat());
        }
    }
    
    if(jsonValue.HasMember("rotation")){
        const auto& rotationJSON = jsonValue["rotation"];
        if(rotationJSON.IsFloat() || rotationJSON.IsInt()){
            thisAsNode->setRotation(rotationJSON.GetFloat());
        }
    }
    
    if(jsonValue.HasMember("hidden")){
        const auto& hiddenJSON = jsonValue["hidden"];
        if(hiddenJSON.IsBool()){
            thisAsNode->setVisible(!hiddenJSON.GetBool());
        }
    }
    
    if(HJSONParseUtils::checkMemberBool(jsonValue, "visible", false)){
        thisAsNode->setVisible(false);
    }

    if(jsonValue.HasMember("anchorPoint")){
        const auto& anchorPointJSON = jsonValue["anchorPoint"];
        float x = anchorPointJSON["x"].GetFloat();
        float y = anchorPointJSON["y"].GetFloat();
        thisAsNode->setAnchorPoint(cocos2d::Point(x, y));
    }

    if(jsonValue.HasMember("width") && jsonValue.HasMember("height")){
        float width = jsonValue["width"].GetFloat();
        float height = jsonValue["height"].GetFloat();
        width = HScalingUtils::configureNodeDimension(width);
        height = HScalingUtils::configureNodeDimension(height);
        thisAsNode->setContentSize(cocos2d::Size(width, height));
    }
}

void HLayoutObject::prepareSize(const rapidjson::Value& jsonValue, float& width, float& height)
{
    width = 0;
    height = 0;
}