PlainSprite.cpp 5.88 KB
//
//  PlainSprite.cpp
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 17.05.2017.
//
//

#include <stdio.h>
#include "PlainSprite.h"
#include "JSONParseUtils.h"
#include "cocos2d.h"
#include "MathUtils.h"
#include "ScalingUtils.h"

PlainSprite* PlainSprite::createWithTexture(cocos2d::Texture2D* texture)
{
    PlainSprite * sprite = new (std::nothrow) PlainSprite();
    if(sprite && sprite->initWithTexture(texture))
    {
        sprite->autorelease();
        return sprite;
    }
    CC_SAFE_DELETE(sprite);
    return nullptr;
}


PlainSprite* PlainSprite::createWithSpritePath(std::string spritePath)
{
    PlainSprite * sprite = new (std::nothrow) PlainSprite();
    if(sprite && sprite->initWithFile(spritePath))
    {
        sprite->autorelease();
        return sprite;
    }
    CC_SAFE_DELETE(sprite);
    return nullptr;
}

void PlainSprite::loadPropertiesFromJSON(const rapidjson::Value& jsonValue, LayoutViewInterface* scene, const std::string resFolder, const std::string altResFolder)
{
    this->loadCommonPropertiesFromJSON(jsonValue);
    
    if(jsonValue.HasMember("flippedX")){
        const auto& flippedXJSON = jsonValue["flippedX"];
        this->setFlippedX(flippedXJSON.GetBool());
    }

    if(jsonValue.HasMember("flippedY")){
        const auto& flippedYJSON = jsonValue["flippedY"];
        this->setFlippedY(flippedYJSON.GetBool());
    }
    
    if(jsonValue.HasMember("color")){
        const auto& color = jsonValue["color"];
        float r = color["r"].GetFloat();
        float g = color["g"].GetFloat();
        float b = color["b"].GetFloat();
        this->setColor(cocos2d::Color3B(r,g,b));
    }

}

void PlainSprite::prepareSize(const rapidjson::Value& jsonValue, float& width, float& height)
{
    width = this->getBoundingBox().size.width;
    height = this->getBoundingBox().size.height;
//    width = this->getContentSize().width;
//    height = this->getContentSize().height;
}

void PlainSprite::setProperty(std::string propertyName, const rapidjson::Value& newValue, ActionParseDelegate* parseDelegate)
{
    if(propertyName == "opacity"){
        auto value = newValue.GetInt();
        this->setOpacity(value);
    }
    else if(propertyName == "rotation"){
       auto value = newValue.GetFloat();
        this->setRotation(value);
    }
}

void PlainSprite::callFunctionByName(std::string methodName, const rapidjson::Value* arguments, ActionParseDelegate* parseDelegate, std::function<void()> callback)
{
    auto scale = 1/ScalingUtils::getAdjustedContentScaleFactor();
    
    if(methodName == "setVisible"){
        if((*arguments).IsArray()){
            this->setVisible((*arguments).GetArray()[0].GetBool());
        }
    }
    else if(methodName == "setFlippedX"){
        if((*arguments).IsArray()){
            this->setFlippedX((*arguments).GetArray()[0].GetBool());
        }
    }
    else if(methodName == "setFlippedY"){
        if((*arguments).IsArray()){
            this->setFlippedY((*arguments).GetArray()[0].GetBool());
        }
    }
    else if (methodName == "setScale"){
        if((*arguments).IsArray()){
            this->setScale((*arguments).GetArray()[0].GetFloat(), (*arguments).GetArray()[0].GetFloat());
        }
    }
    else if(methodName == "setOpacity"){
        if((*arguments).IsArray()){
            this->setOpacity((*arguments).GetArray()[0].GetInt());
        }
    }
    else if(methodName == "setPositionX"){
        if((*arguments).IsArray()){
            this->setPositionX((*arguments).GetArray()[0].GetFloat()*scale);
        }
    }
    else if(methodName == "setPositionY"){
        if((*arguments).IsArray()){
            this->setPositionY((*arguments).GetArray()[0].GetFloat()*scale);
        }
    }
    else if(methodName == "setPosition"){
        if((*arguments).IsArray()){
            auto point = JSONParseUtils::getPoint((*arguments).GetArray()[0]);
            MathUtils::multiplyPoint(point, scale);
            this->setPosition(point);
        }
    }
    else if(methodName == "resetPosition"){
        this->setPosition(originalPosition);
    }
//    else if(methodName == "setPositionFromCenter"){
//        if((*arguments).IsArray()){
//            auto point = JSONParseUtils::getPoint((*arguments).GetArray()[0]);
//            MathUtils::multiplyPoint(point, scale);
//            auto parentSize = getParent()->getBoundingBox().size;
//            this->setPosition(point.x + parentSize.width/2, point.y + parentSize.height/2);
//        }
//    }
    else if(methodName == "setTextureRect"){
        if((*arguments).IsArray()){
            auto newRect = JSONParseUtils::getRect((*arguments).GetArray()[0]);
            MathUtils::multiplyRectPosition(newRect, scale);
            this->setTextureRect(newRect);
        }
    }
    else if(methodName == "setContentSize"){
        if((*arguments).IsArray()){
            this->setContentSize(JSONParseUtils::getSize((*arguments).GetArray()[0]));
        }
    }
    else if(methodName == "setRotation"){
        if((*arguments).IsArray()){
            this->setRotation(((*arguments).GetArray()[0]).GetFloat());
        }
    }
    else if(methodName == "clipSprite"){
        if((*arguments).IsArray()){
            auto by = JSONParseUtils::getPoint((*arguments)[0]);
            auto prevTextureRect = this->getTextureRect();
            this->setTextureRect(cocos2d::Rect(prevTextureRect.origin.x - by.x, prevTextureRect.origin.y - by.y, prevTextureRect.size.width, prevTextureRect.size.height));
        }
    }
    else if(methodName == "stopAnimations"){
        this->stopAllActions();
    }
    else if(methodName == "setPicture"){
        if((*arguments).IsArray()){
            std::string imagePath = ((*arguments).GetArray()[0]).GetString();
            auto texture = cocos2d::Director::getInstance()->getTextureCache()->addImage(imagePath);
            this->setTexture(texture);
            this->setTextureRect(cocos2d::Rect(cocos2d::Point::ZERO, texture->getContentSize()));
        }
    }
}