ToyScenarioObject.h
3.26 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
//
//  ToyScenarioObject.h
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 24.05.2017.
//
//
#ifndef ToyScenarioObject_h
#define ToyScenarioObject_h
#include <stdio.h>
#include "ToyActionData.h"
#include "json/document.h"
#include <map>
class ActionParseDelegate;
// weird, because the whole parser has been translated from JavaScript
class ToyScenarioObject
{
    public:
    
        std::string scenarioObjectName;
        std::string scenarioObjectClassName;
    
        ToyScenarioObject(){
            _customData = NULL;
            _jsonToyValueStorage = NULL;
            scenarioObjectName = "";
            scenarioObjectClassName = "";
        };
    
        virtual ~ToyScenarioObject(){
            if(_jsonToyValueStorage){
                delete _jsonToyValueStorage;
            }
            for(auto pair : _animations){
                delete pair.second;
            }
            
            
            
        };
    
//        virtual void callFunctionByName(std::string methodName, const rapidjson::Value* arguments, ActionParseDelegate* parseDelegate){
//            // do nothing
//        };
    
        virtual void callFunctionByName(std::string methodName, const rapidjson::Value* arguments, ActionParseDelegate* parseDelegate, std::function<void()> callback = [](){}){
            // do nothing
            callback();
        };
    
        virtual void setProperty(std::string propertyName, const rapidjson::Value& newValue, ActionParseDelegate* parseDelegate) {
            // do nothing
        };
        virtual std::string getPropertyAsString(std::string propertyName = ""){
            if(propertyName == "objectName"){
                return scenarioObjectName;
            }
            return "NULL";
        };
    
        virtual ToyScenarioObject* getToyScenarioObjectByName(std::string objectName){
            return NULL;
        };
    
        virtual void insertAnimationWithId(std::string animationId, const rapidjson::Value& animationData){
            
            if(_animations.find(animationId) == _animations.end()){
                rapidjson::Value* animationDataCopy = new rapidjson::Value();
                animationDataCopy->CopyFrom(animationData, this->getJsonStorage()->GetAllocator());
                _animations.insert({animationId, animationDataCopy});
            }
        };
    
        virtual const rapidjson::Value* getAnimationById(std::string animationId){
            return _animations[animationId];
        };
    
        virtual void setCustomData(void* data){
            _customData = data;
        };
    
        virtual void* getCustomData(){
            return _customData;
        };
    
        virtual std::string getObjectName(){
            return scenarioObjectName;
        };
    
        virtual std::string getClassName(){
            return scenarioObjectClassName;
        };
    
    protected:
        rapidjson::Document* _jsonToyValueStorage;
        std::map<std::string, rapidjson::Value*> _animations;
        void* _customData;
    
        rapidjson::Document* getJsonStorage(){
            if(!_jsonToyValueStorage){
                _jsonToyValueStorage = new rapidjson::Document();
            }
            return _jsonToyValueStorage;
        };
    
};
#endif /* ToyScenarioObject_h */
