ToyDraggableBrushSprite.cpp 4.06 KB
//
//  ToyDraggableBrushSprite.cpp
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 17.05.2017.
//
//

#include <stdio.h>
#include "ToyDraggableBrushSprite.h"
#include "ToyJSONParseUtils.h"


ToyDraggableBrushSprite* ToyDraggableBrushSprite::create(const std::string& filepath)
{
    ToyDraggableBrushSprite * sprite = new (std::nothrow) ToyDraggableBrushSprite();
    if(sprite && sprite->initWithFile(filepath))
    {
        sprite->autorelease();
        return sprite;
    }
    CC_SAFE_DELETE(sprite);
    return nullptr;
}

bool ToyDraggableBrushSprite::initWithFile(const std::string& filepath)
{
    if (!ToyPlainSprite::initWithFile(filepath)) {
        return false;
    }
    
    _brushMode = NONE;
    _brushThickness = 0;
    _brushWidth = 0;
    _brushPosition = cocos2d::Point(0,0);
    _demoRoute.clear();
    
    return true;
}

void ToyDraggableBrushSprite::loadPropertiesFromJSON(const rapidjson::Value& jsonValue, ToyLayoutViewInterface* scene, const std::string resFolder, const std::string altResFolder)
{
    ToyPlainSprite::loadPropertiesFromJSON(jsonValue, scene, resFolder, altResFolder);
    
    if (ToyJSONParseUtils::hasMemberString(jsonValue, "brushMode")){
        std::string horizontal = "HORIZONTAL";
        if(horizontal == jsonValue["brushMode"].GetString()){
            _brushMode = HORIZONTAL;
        }
    }
    
    if (ToyJSONParseUtils::hasMemberFloat(jsonValue, "brushThickness")){
        _brushThickness = jsonValue["brushThickness"].GetFloat();
    }
    
    if (ToyJSONParseUtils::hasMemberFloat(jsonValue, "brushWidth")){
        _brushWidth = jsonValue["brushWidth"].GetFloat();
    }
    
    if (ToyJSONParseUtils::hasMemberPoint(jsonValue, "brushPosition")) {
        _brushPosition = ToyJSONParseUtils::getMemberPoint(jsonValue, "brushPosition");
    }
    
    if (ToyJSONParseUtils::hasMemberArray(jsonValue, "demoRoute")) {
        for(const auto& point : jsonValue["demoRoute"].GetArray()){
            if (ToyJSONParseUtils::isPoint(point)) {
                _demoRoute.push_back(ToyJSONParseUtils::getPoint(point));
            }
        }
    }
}

cocos2d::Rect ToyDraggableBrushSprite::paintingRect()
{
    if (_brushMode == HORIZONTAL) {
        return cocos2d::Rect(_brushPosition.x, _brushPosition.y - _brushThickness/2, _brushWidth, _brushThickness);
    }
    
    return cocos2d::Rect(0,0,0,0);
}

ToyGeometryUtils::ThickSegment ToyDraggableBrushSprite::getSegmentToPaintInRect(cocos2d::Rect rect)
{
    ToyGeometryUtils::ThickSegment segment;
    segment.thickness = _brushThickness;

    if (_brushMode == HORIZONTAL) {
        segment.point1 = cocos2d::Point(rect.origin.x, rect.getMidY());
        segment.point2 = cocos2d::Point(rect.origin.x + rect.size.width, rect.getMidY());
    }
    
    return segment;
}

std::vector<ToyGeometryUtils::ThickSegment> ToyDraggableBrushSprite::getSegmentsToPaint(cocos2d::Rect previousRect, cocos2d::Rect currentRect)
{
    std::vector<ToyGeometryUtils::ThickSegment> segments;
    
    if (previousRect.size.width != 0 && currentRect.size.width != 0) {
        
        if(_brushMode == HORIZONTAL){
            auto thickness = _brushWidth*0.34;
            
            auto point11 = cocos2d::Point(previousRect.getMidX(), previousRect.getMidY());
            auto point21 = cocos2d::Point(currentRect.getMidX(), currentRect.getMidY());
            
            auto point12 = cocos2d::Point(previousRect.getMaxX() - thickness/2, previousRect.getMidY());
            auto point22 = cocos2d::Point(currentRect.getMaxX() - thickness/2, currentRect.getMidY());
            
            auto point13 = cocos2d::Point(previousRect.getMinX() + thickness/2, previousRect.getMidY());
            auto point23 = cocos2d::Point(currentRect.getMinX() + thickness/2, currentRect.getMidY());
            
            segments.push_back(ToyGeometryUtils::ThickSegment(point11, point21, thickness));
            segments.push_back(ToyGeometryUtils::ThickSegment(point12, point22, thickness));
            segments.push_back(ToyGeometryUtils::ThickSegment(point13, point23, thickness));
        }
    }
    
    return segments;
}