#ifndef _RED_REDREADER_H_ #define _RED_REDREADER_H_ #include #include #include #include #include "2d/CCNode.h" #include "base/CCData.h" #include "base/CCMap.h" #include "REDSequence.h" #include "extensions/GUI/CCControlExtension/CCControl.h" #include "REDAnimationManager.h" #include "Protobuf/redream.pb.h" #include "Rebolt/ReboltRedManager.h" #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) #ifdef ABSOLUTE #undef ABSOLUTE #endif #endif #define RED_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(T, METHOD) static T * METHOD() { \ T * ptr = new (std::nothrow) T(); \ if(ptr != NULL) { \ ptr->autorelease(); \ return ptr; \ } \ CC_SAFE_DELETE(ptr); \ return NULL; \ } #define RED_STATIC_NEW_AUTORELEASE_OBJECT_WITH_INIT_METHOD(T, METHOD) static T * METHOD() { \ T * ptr = new (std::nothrow) T(); \ if(ptr != NULL && ptr->init()) { \ ptr->autorelease(); \ return ptr; \ } \ CC_SAFE_DELETE(ptr); \ return NULL; \ } #define RED_VERSION 3 namespace redream { /** * @addtogroup redream * @{ */ class REDFile : public cocos2d::Node { private: cocos2d::Node *_REDFileNode; public: REDFile(); static REDFile* create(); cocos2d::Node* getREDFileNode(); void setREDFileNode(Node *pNode); // retain }; /* Forward declaration. */ class NodeLoader; class NodeLoaderLibrary; class NodeLoaderListener; class REDMemberVariableAssigner; class REDSelectorResolver; class REDAnimationManager; class REDKeyframe; class ReboltRedManager; /** * @brief Parse REDI file which is generated by redream */ class CC_DLL REDReader : public cocos2d::Ref { #pragma mark - add by songqingyu private: void readFrameNames(); bool _isBatchNode = false; std::unordered_map> _batchPlistFrameNames; cocos2d::Vector _refreshNodesByEndBatch; std::unordered_set _plistSt; public: void setPlist(std::unordered_set plistSt,std::unordered_set replacePlistSt); std::unordered_set getPlist(){return _plistSt;}; void setBatchNode(bool batchReader){ _isBatchNode = batchReader; } /// plist纹理资源列表 例: ///读取batchred文件的图集的FrameName的集合 ///@return 是否解析成功 bool readFrameNamesFromFile(const char *pREDFileName); const std::unordered_map>& getBatchPlistFrameNames(){return _batchPlistFrameNames;}; ///之前好几个地方都写一遍此逻辑去获取图片,现在统一放在一个方法里面 ///根据file和sheet去获取SpriteFrame如果没有去找单图,单图没有,用空图替换,不会崩溃 cocos2d::SpriteFrame* getSpriteFrameByName(std::string spriteFile,std::string spriteSheet,bool useDefaultEmpty=true); #pragma mark end - public: enum class PropertyType { POSITION = 0, SIZE, POINT, POINT_LOCK, SCALE_LOCK, DEGREES, INTEGER, FLOAT, FLOAT_VAR, CHECK, SPRITEFRAME, TEXTURE, BYTE, COLOR3, COLOR4F_VAR, FLIP, BLEND_MODE, FNT_FILE, TEXT, FONT_TTF, INTEGER_LABELED, BLOCK, ANIMATION, RED_FILE, STRING, BLOCK_CONTROL, FLOAT_SCALE, FLOAT_XY, SPINE_SKELFRAME, SPINE_SKEL, SPINE_ATLAS, FRAME_SET, PERCENT, COLOR4, CALLBACKS, LOCALIZATION, TABLE_VIEW_RED_FILE, PB_VERTS_FILE, SeparatorCheck, MinMaxGradientData, MinMaxCurveData, EmissionData, FloatXYZ, MaterialFile, MATERIAL_BALL, BAKE_DATA_FILE, BAKE_ANIMATION, POLYGON_FILE, WISE, }; enum class FloatType { _0 = 0, _1, MINUS1, _05, INTEGER, FULL }; enum class PlatformType { ALL = 0, IOS, MAC }; enum class TargetType { NONE = 0, DOCUMENT_ROOT = 1, OWNER = 2, REDWISE_EVENT = 3, }; enum class PositionType { RELATIVE_BOTTOM_LEFT, RELATIVE_TOP_LEFT, RELATIVE_TOP_RIGHT, RELATIVE_BOTTOM_RIGHT, PERCENT, MULTIPLY_RESOLUTION, }; enum class SizeType { ABSOLUTE, PERCENT, RELATIVE_CONTAINER, HORIZONTAL_PERCENT, VERTICAL_PERCENT, MULTIPLY_RESOLUTION, }; enum class ScaleType { ABSOLUTE, MULTIPLY_RESOLUTION }; /** * @js NA * @lua NA */ REDReader(NodeLoaderLibrary *pNodeLoaderLibrary, REDMemberVariableAssigner *pREDMemberVariableAssigner = NULL, REDSelectorResolver *pREDSelectorResolver = NULL, NodeLoaderListener *pNodeLoaderListener = NULL); /** * @js NA * @lua NA */ REDReader(REDReader *ccbReader); /** * @js NA * @lua NA */ virtual ~REDReader(); /** * @js NA * @lua NA */ REDReader(); void setREDRootPath(const char* ccbRootPath); const std::string& getREDRootPath() const; cocos2d::Node* readNodeGraphFromFile(const char *pREDFileName); cocos2d::Node* readNodeGraphFromFile(const char *pREDFileName, cocos2d::Ref *pOwner); cocos2d::Node* readNodeGraphFromFile(const char *pREDFileName, cocos2d::Ref *pOwner, const cocos2d::Size &parentSize); /** * @js NA * @lua NA */ cocos2d::Node* readNodeGraphFromData(std::shared_ptr data, cocos2d::Ref *pOwner, const cocos2d::Size &parentSize); /** @lua NA */ cocos2d::Scene* createSceneWithNodeGraphFromFile(const char *pREDFileName); /** @lua NA */ cocos2d::Scene* createSceneWithNodeGraphFromFile(const char *pREDFileName, cocos2d::Ref *pOwner); /** @lua NA */ cocos2d::Scene* createSceneWithNodeGraphFromFile(const char *pREDFileName, cocos2d::Ref *pOwner, const cocos2d::Size &parentSize); /** * @js NA * @lua NA */ REDMemberVariableAssigner* getREDMemberVariableAssigner(); /** * @js NA * @lua NA */ REDSelectorResolver* getREDSelectorResolver(); /** * @js getActionManager * @lua getActionManager */ REDAnimationManager* getAnimationManager(); /** * @js setActionManager * @lua setActionManager */ void setAnimationManager(REDAnimationManager *pAnimationManager); /** Used in NodeLoader::parseProperties() * @js NA * @lua NA */ std::set* getAnimatedProperties(); /** * @js NA * @lua NA */ std::set& getLoadedSpriteSheet(); /** * @js NA * @lua NA */ cocos2d::Ref* getOwner(); /* Utility methods. * @js NA * @lua NA */ static std::string lastPathComponent(const char* pString); /** * @js NA * @lua NA */ static std::string deletePathExtension(const char* pString); /** * @js NA * @lua NA */ static std::string toLowerCase(const char* pString); /** * @js NA * @lua NA */ static bool endsWith(const char* pString, const char* pEnding); /** * @js NA * @lua NA */ const std::string& readCachedString(int idx); /** * @js NA * @lua NA */ bool isJSControlled(); bool readCallbackKeyframesForSeq(REDSequence* seq, RedreamInfo::Sequences& sequences); bool readSoundKeyframesForSeq(REDSequence* seq, RedreamInfo::Sequences& sequences); bool readWiseKeyframesForSeq(REDSequence* seq, RedreamInfo::Sequences& sequences); cocos2d::ValueVector getOwnerCallbackNames(); cocos2d::Vector& getOwnerCallbackNodes(); cocos2d::ValueVector& getOwnerCallbackControlEvents(); cocos2d::ValueVector getOwnerOutletNames(); cocos2d::Vector& getOwnerOutletNodes(); cocos2d::Vector& getNodesWithAnimationManagers(); cocos2d::Vector& getAnimationManagersForNodes(); typedef cocos2d::Map REDAnimationManagerMap; typedef std::shared_ptr REDAnimationManagerMapPtr; typedef cocos2d::Map ReboltRedManagerMap; typedef std::shared_ptr ReboltRedManagerMapPtr; /** * @js NA * @lua NA */ REDAnimationManagerMapPtr getAnimationManagers(); /** * @js NA * @lua NA */ void setAnimationManagers(REDAnimationManagerMapPtr x); /** * @js NA * @lua NA */ void addOwnerCallbackName(const std::string& name); /** * @js NA * @lua NA */ void addOwnerCallbackNode(cocos2d::Node *node); void addOwnerCallbackControlEvents(cocos2d::extension::Control::EventType type); /** * @js NA * @lua NA */ void addDocumentCallbackName(const std::string& name); /** * @js NA * @lua NA */ void addDocumentCallbackNode(cocos2d::Node *node); void addDocumentCallbackControlEvents(cocos2d::extension::Control::EventType eventType); /** * @js NA * @lua NA */ static float getResolutionScale(); static void setResolutionScale(float scale); /** * @js NA * @lua NA */ cocos2d::Node* readFileWithCleanUp(bool bCleanUp, REDAnimationManagerMapPtr am, ReboltRedManagerMapPtr rm, std::function callback = nullptr); void readFileRedream(const RedreamInfo::Rebolt& reboltBuf); void addOwnerOutletName(std::string name); void addOwnerOutletNode(cocos2d::Node *node); static void releaseAllREDFileCache(); ReboltRedManagerMapPtr getReboltRedManagerMapPtr(); void setReboltRedManagerMapPtr(ReboltRedManagerMapPtr x); void setReboltRedManager(ReboltRedManager* rrm); ReboltRedManager* getReboltRedManager(); bool isReboltNode(); private: //add by yutao std::shared_ptr getBytesFromREDFileByName(std::string ccbFullFilePath); void cleanUpNodeGraph(cocos2d::Node *pNode); bool readSequences(); REDKeyframe* readKeyframe(PropertyType type, const RedreamInfo::Keyframe& KeyframeProtobuf); bool readHeader(); cocos2d::Node* readNodeGraph(); cocos2d::Node* readNodeGraph(cocos2d::Node * pParent, const RedreamInfo::Node& protoBufNode); bool getBit(); void alignBits(); bool init(); friend class NodeLoader; private: std::shared_ptr _data; unsigned char *_bytes; RedreamInfo::RedFile* _redFileProtobuf; int _currentByte; int _currentBit; std::vector _stringCache; std::set _loadedSpriteSheets; cocos2d::Ref *_owner; REDAnimationManager* _animationManager; //retain REDAnimationManagerMapPtr _animationManagers; std::set *_animatedProps; NodeLoaderLibrary *_nodeLoaderLibrary; NodeLoaderListener *_nodeLoaderListener; REDMemberVariableAssigner *_REDMemberVariableAssigner; REDSelectorResolver *_REDSelectorResolver; std::vector _ownerOutletNames; cocos2d::Vector _ownerOutletNodes; cocos2d::Vector _nodesWithAnimationManagers; cocos2d::Vector _animationManagersForNodes; std::vector _ownerCallbackNames; cocos2d::Vector _ownerCallbackNodes; cocos2d::ValueVector _ownerOwnerCallbackControlEvents; std::string _REDRootPath; // bool _jsControlled; protected: /// 给动画管理,行为树管理等管理器设置根节点 /// @param node 根节点 virtual void setRootNodeForOrtherManger(cocos2d::Node* node); /// 读取动画的属性 /// @param node 节点指针 /// @param nodeProtobuf protobuf 数据 virtual void readAnimatedProperties(cocos2d::Node* node, const RedreamInfo::Node& nodeProtobuf); virtual void assignReboltName(const std::string& reboltName, const std::string& reboltId, cocos2d::Node* node); virtual void assignREDMemberVariable(TargetType memberVarAssignmentType, std::string memberVarAssignmentName, std::string className, cocos2d::Node* node); virtual void assignCustomProperty(NodeLoader* ccNodeLoader, cocos2d::Node* node); private: ReboltRedManager* _reboltRedManager; ReboltRedManagerMapPtr _reboltRedManagers; bool _isReboltNode; }; // end of effects group /// @} } #endif