This commit is contained in:
XiaoFeng 2015-07-27 11:55:18 +08:00
parent bd0bdae530
commit f410fcd737
5 changed files with 138 additions and 21 deletions

View File

@ -427,11 +427,37 @@ ActionTimeline* ActionTimelineCache::loadAnimationActionWithFlatBuffersFile(cons
CC_ASSERT(FileUtils::getInstance()->isFileExist(fullPath));
Data buf = FileUtils::getInstance()->getDataFromFile(fullPath);
action = createActionWithDataBuffer(buf);
_animationActions.insert(fileName, action);
auto csparsebinary = GetCSParseBinary(buf.getBytes());
return action;
}
ActionTimeline* ActionTimelineCache::loadAnimationWithDataBuffer(const cocos2d::Data data, const std::string fileName)
{
// if already exists an action with filename, then return this action
ActionTimeline* action = _animationActions.at(fileName);
if (action)
return action;
std::string path = fileName;
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(fileName.c_str());
CC_ASSERT(FileUtils::getInstance()->isFileExist(fullPath));
action = createActionWithDataBuffer(data);
_animationActions.insert(fileName, action);
return action;
}
inline ActionTimeline* ActionTimelineCache::createActionWithDataBuffer(const cocos2d::Data data)
{
auto csparsebinary = GetCSParseBinary(data.getBytes());
auto nodeAction = csparsebinary->action();
action = ActionTimeline::create();
auto action = ActionTimeline::create();
int duration = nodeAction->duration();
action->setDuration(duration);
@ -461,8 +487,6 @@ ActionTimeline* ActionTimelineCache::loadAnimationActionWithFlatBuffersFile(cons
action->addTimeline(timeline);
}
_animationActions.insert(fileName, action);
return action;
}

View File

@ -31,6 +31,7 @@ THE SOFTWARE.
#include "cocostudio/DictionaryHelper.h"
#include "CCTimelineMacro.h"
#include "cocostudio/CocosStudioExport.h"
#include "cocos2d.h"
namespace flatbuffers
{
@ -82,6 +83,7 @@ public:
ActionTimeline* createActionWithFlatBuffersFile(const std::string& fileName);
ActionTimeline* loadAnimationActionWithFlatBuffersFile(const std::string& fileName);
ActionTimeline* loadAnimationWithDataBuffer(const cocos2d::Data data, const std::string fileName);
ActionTimeline* createActionWithFlatBuffersForSimulator(const std::string& fileName);
@ -119,6 +121,7 @@ protected:
void loadEasingDataWithFlatBuffers(Frame* frame, const flatbuffers::EasingData* flatbuffers);
inline ActionTimeline* createActionWithDataBuffer(const cocos2d::Data data);
protected:
typedef std::function<Frame*(const rapidjson::Value& json)> FrameCreateFunc;

View File

@ -288,11 +288,20 @@ Node* CSLoader::createNode(const std::string &filename, const ccNodeLoadCallback
return nullptr;
}
std::string CSLoader::getExtentionName(const std::string& name)
{
std::string result = "";
std::string path = name;
size_t pos = path.find_last_of('.');
result = path.substr(pos + 1, path.length());
return result;
}
ActionTimeline* CSLoader::createTimeline(const std::string &filename)
{
std::string path = filename;
size_t pos = path.find_last_of('.');
std::string suffix = path.substr(pos + 1, path.length());
std::string suffix = getExtentionName(filename);
CCLOG("suffix = %s", suffix.c_str());
ActionTimelineCache* cache = ActionTimelineCache::getInstance();
@ -309,6 +318,27 @@ ActionTimeline* CSLoader::createTimeline(const std::string &filename)
return nullptr;
}
ActionTimeline* CSLoader::createTimelineWithDataBuffer(const Data data, const std::string& filename)
{
std::string suffix = getExtentionName(filename);
CCLOG("suffix = %s", suffix.c_str());
ActionTimelineCache* cache = ActionTimelineCache::getInstance();
if (suffix == "csb")
{
return cache->loadAnimationWithDataBuffer(data, filename);
}
else if (suffix == "json" || suffix == "ExportJson")
{
std::string content((char *)data.getBytes(), data.getSize());
return cache->loadAnimationActionWithContent(filename, content);
}
return nullptr;
}
/*
ActionTimelineNode* CSLoader::createActionTimelineNode(const std::string& filename)
{
@ -794,6 +824,54 @@ Component* CSLoader::loadComAudio(const rapidjson::Value &json)
return audio;
}
cocos2d::Node* CSLoader::createNodeWithDataBuffer(const Data data)
{
return createNodeWithDataBuffer(data, nullptr);
}
Node * CSLoader::createNodeWithDataBuffer(const Data data, const ccNodeLoadCallback &callback)
{
CSLoader * loader = CSLoader::getInstance();
Node * node = nullptr;
do
{
CC_BREAK_IF(data.isNull() || data.getSize() <= 0);
auto csparsebinary = GetCSParseBinary(data.getBytes());
CC_BREAK_IF(nullptr == csparsebinary);
auto csBuildId = csparsebinary->version();
if (csBuildId)
{
CCASSERT(strcmp(loader->_csBuildID.c_str(), csBuildId->c_str()) == 0,
StringUtils::format("%s%s%s%s%s%s%s%s%s%s",
"The reader build id of your Cocos exported file(",
csBuildId->c_str(),
") and the reader build id in your Cocos2d-x(",
loader->_csBuildID.c_str(),
") are not match.\n",
"Please get the correct reader(build id ",
csBuildId->c_str(),
")from ",
"http://www.cocos2d-x.org/filedown/cocos-reader",
" and replace it in your Cocos2d-x").c_str());
}
// decode plist
auto textures = csparsebinary->textures();
int textureSize = csparsebinary->textures()->size();
CCLOG("textureSize = %d", textureSize);
for (int i = 0; i < textureSize; ++i)
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(textures->Get(i)->c_str());
}
node = loader->nodeWithFlatBuffers(csparsebinary->nodeTree(), callback);
} while (0);
loader->reconstructNestNode(node);
return node;
}
Node* CSLoader::createNodeWithFlatBuffersFile(const std::string &filename)
{
return createNodeWithFlatBuffersFile(filename, nullptr);
@ -803,6 +881,13 @@ Node* CSLoader::createNodeWithFlatBuffersFile(const std::string &filename, const
{
Node* node = nodeWithFlatBuffersFile(filename, callback);
reconstructNestNode(node);
return node;
}
inline void CSLoader::reconstructNestNode(cocos2d::Node * node)
{
/* To reconstruct nest node as WidgetCallBackHandlerProtocol. */
auto callbackHandler = dynamic_cast<WidgetCallBackHandlerProtocol *>(node);
if (callbackHandler)
@ -819,9 +904,6 @@ Node* CSLoader::createNodeWithFlatBuffersFile(const std::string &filename, const
CCLOG("after pop back _rootNode name = %s", _rootNode->getName().c_str());
}
}
/**/
return node;
}
Node* CSLoader::nodeWithFlatBuffersFile(const std::string &fileName)
@ -896,8 +978,9 @@ Node* CSLoader::nodeWithFlatBuffers(const flatbuffers::NodeTree *nodetree, const
cocostudio::timeline::ActionTimeline* action = nullptr;
if (filePath != "" && FileUtils::getInstance()->isFileExist(filePath))
{
node = createNodeWithFlatBuffersFile(filePath, callback);
action = cocostudio::timeline::ActionTimelineCache::getInstance()->createActionWithFlatBuffersFile(filePath);
Data buf = FileUtils::getInstance()->getDataFromFile(filePath);
node = createNodeWithDataBuffer(buf, callback);
action = timeline::ActionTimelineCache::getInstance()->loadAnimationWithDataBuffer(buf, filePath);
}
else
{

View File

@ -79,7 +79,11 @@ public:
static cocos2d::Node* createNode(const std::string& filename);
static cocos2d::Node* createNode(const std::string& filename, const ccNodeLoadCallback& callback);
static cocos2d::Node* createNodeWithDataBuffer(const Data data);
static cocos2d::Node* createNodeWithDataBuffer(const Data data, const ccNodeLoadCallback &callback);
static cocostudio::timeline::ActionTimeline* createTimeline(const std::string& filename);
static cocostudio::timeline::ActionTimeline* createTimelineWithDataBuffer(const Data data, const std::string& filename);
/*
static cocostudio::timeline::ActionTimelineNode* createActionTimelineNode(const std::string& filename);
@ -143,6 +147,9 @@ protected:
std::string getGUIClassName(const std::string &name);
std::string getWidgetReaderClassName(cocos2d::ui::Widget *widget);
inline void reconstructNestNode(cocos2d::Node * node);
static inline std::string getExtentionName(const std::string& name);
typedef std::function<cocos2d::Node*(const rapidjson::Value& json)> NodeCreateFunc;
typedef std::pair<std::string, NodeCreateFunc> Pair;

View File

@ -66,11 +66,11 @@ void TestActionTimeline::onEnter()
{
ActionTimelineBaseTest::onEnter();
Node* node = CSLoader::createNode("ActionTimeline/DemoPlayer.csb");
ActionTimeline* action = CSLoader::createTimeline("ActionTimeline/DemoPlayer.csb");
Data data = FileUtils::getInstance()->getDataFromFile("ActionTimeline/DemoPlayer.csb");
Node* node = CSLoader::createNodeWithDataBuffer(data);
ActionTimeline* action = CSLoader::createTimelineWithDataBuffer(data, "ActionTimeline/DemoPlayer.csb");
node->runAction(action);
action->gotoFrameAndPlay(0);
// ActionTimelineNode* node = CSLoader::createActionTimelineNode("ActionTimeline/DemoPlayer.csb", 0, 40, true);
node->setScale(0.2f);
node->setPosition(VisibleRect::center());