ToyDraggableBrushSprite.cpp
4.06 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
//
//  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;
}