LayoutParser.h 2.17 KB
//
//  LayoutParser.h
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 16.05.2017.
//
//

#ifndef LayoutParser_h
#define LayoutParser_h

#include "json/document.h"
#include "LayoutObject.h"
#include "ScenarioObject.h"

class LayoutViewInterface
{
    public:
        virtual void addLayer(cocos2d::Layer*){
        }
        virtual void addObject(std::string objectName, cocos2d::Node* object){
        }
        virtual void addScenarioObject(std::string objectName, ScenarioObject* object){
        }
        virtual bool touchHandlerForWidget(std::string objectName, cocos2d::ui::Widget::TouchEventType touchEventType){
            return false;
        }
        virtual void setupBackgroundMusic(std::string backgroundMusicPath){
        }
        virtual ScenarioObject* getObjectByName(std::string objectName){
            return NULL;
        }
};

class LayoutParser
{
    public:
    
        static LayoutParser& getInstance()
        {
            static LayoutParser instance;
            return instance;
        }
    
        void loadLayoutFromJSONFile(const std::string& jsonFilePath, LayoutViewInterface* scene);
        void loadLayoutFromDownloadedJSONFile(const std::string& jsonFilePath, LayoutViewInterface* scene);
        void loadLayoutFromJSONString(const std::string& jsonString, LayoutViewInterface* scene);
    
    private:
        LayoutParser()
        {
            _parsedDocument = NULL;
        }
    
        CC_DISALLOW_COPY_AND_ASSIGN(LayoutParser);
    
        rapidjson::Document* _parsedDocument;
        float _savedBgStretchScale;
    
        void parseChildObjects(LayoutViewInterface* scene, rapidjson::Value& nodeData, cocos2d::Node* createdNode, std::string objectLayoutsFolder = "", std::string resFolder = "", std::string altResFolder = "", bool isTopLayer = false);
        cocos2d::Node* parseObject(LayoutViewInterface* scene, rapidjson::Value& nodeData, cocos2d::Node* parentNode, std::string objectLayoutsFolder = "", std::string resFolder = "", std::string altResFolder = "", bool isTopLayer = false);
        const rapidjson::Value* getValueForKey(std::string key, const rapidjson::Value& mainNodeData);
    
};

#endif /* LayoutParser_h */