2014-06-11 11:10:07 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
#include "CCActionTimelineCache.h"
|
2014-10-09 18:28:09 +08:00
|
|
|
#include "CSLoader.h"
|
2014-06-13 18:51:32 +08:00
|
|
|
#include "CCFrame.h"
|
|
|
|
#include "CCTimeLine.h"
|
|
|
|
#include "CCActionTimeline.h"
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "2d/CCSpriteFrameCache.h"
|
|
|
|
#include "2d/CCSpriteFrame.h"
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
#include "cocostudio/CSParseBinary_generated.h"
|
|
|
|
|
|
|
|
#include "tinyxml2/tinyxml2.h"
|
|
|
|
#include "flatbuffers/flatbuffers.h"
|
|
|
|
#include "flatbuffers/util.h"
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-12-08 14:32:33 +08:00
|
|
|
#include "cocostudio/FlatBuffersSerialize.h"
|
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
#include <fstream>
|
|
|
|
|
2014-06-11 11:10:07 +08:00
|
|
|
using namespace cocos2d;
|
2014-11-21 15:15:38 +08:00
|
|
|
using namespace flatbuffers;
|
2014-06-11 11:10:07 +08:00
|
|
|
|
|
|
|
namespace cocostudio {
|
|
|
|
namespace timeline{
|
|
|
|
|
|
|
|
static const char* FrameType_VisibleFrame = "VisibleFrame";
|
|
|
|
static const char* FrameType_PositionFrame = "PositionFrame";
|
|
|
|
static const char* FrameType_ScaleFrame = "ScaleFrame";
|
|
|
|
static const char* FrameType_RotationFrame = "RotationFrame";
|
|
|
|
static const char* FrameType_SkewFrame = "SkewFrame";
|
|
|
|
static const char* FrameType_RotationSkewFrame = "RotationSkewFrame";
|
2014-10-20 12:43:01 +08:00
|
|
|
static const char* FrameType_AnchorFrame = "AnchorPointFrame";
|
2014-06-11 11:10:07 +08:00
|
|
|
static const char* FrameType_InnerActionFrame = "InnerActionFrame";
|
|
|
|
static const char* FrameType_ColorFrame = "ColorFrame";
|
|
|
|
static const char* FrameType_TextureFrame = "TextureFrame";
|
|
|
|
static const char* FrameType_EventFrame = "EventFrame";
|
|
|
|
static const char* FrameType_ZOrderFrame = "ZOrderFrame";
|
|
|
|
|
|
|
|
static const char* ACTION = "action";
|
|
|
|
static const char* DURATION = "duration";
|
|
|
|
static const char* TIMELINES = "timelines";
|
|
|
|
static const char* FRAME_TYPE = "frameType";
|
|
|
|
static const char* FRAMES = "frames";
|
|
|
|
static const char* FRAME_INDEX = "frameIndex";
|
|
|
|
static const char* TWEEN = "tween";
|
|
|
|
static const char* TIME_SPEED = "speed";
|
|
|
|
static const char* ACTION_TAG = "actionTag";
|
|
|
|
static const char* INNER_ACTION = "innerActionType";
|
|
|
|
static const char* START_FRAME = "startFrame";
|
|
|
|
|
|
|
|
static const char* X = "x";
|
|
|
|
static const char* Y = "y";
|
|
|
|
static const char* ROTATION = "rotation";
|
|
|
|
static const char* ALPHA = "alpha";
|
|
|
|
static const char* RED = "red";
|
|
|
|
static const char* GREEN = "green";
|
|
|
|
static const char* BLUE = "blue";
|
|
|
|
static const char* Value = "value";
|
|
|
|
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
static ActionTimelineCache* _sharedActionCache = nullptr;
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
ActionTimelineCache* ActionTimelineCache::getInstance()
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
if (! _sharedActionCache)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
_sharedActionCache = new (std::nothrow) ActionTimelineCache();
|
2014-06-11 11:10:07 +08:00
|
|
|
_sharedActionCache->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _sharedActionCache;
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
void ActionTimelineCache::destroyInstance()
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_sharedActionCache);
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
void ActionTimelineCache::purge()
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
_animationActions.clear();
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
void ActionTimelineCache::init()
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
using namespace std::placeholders;
|
2014-06-13 16:03:03 +08:00
|
|
|
_funcs.insert(Pair(FrameType_VisibleFrame, std::bind(&ActionTimelineCache::loadVisibleFrame, this, _1)));
|
|
|
|
_funcs.insert(Pair(FrameType_PositionFrame, std::bind(&ActionTimelineCache::loadPositionFrame, this, _1)));
|
|
|
|
_funcs.insert(Pair(FrameType_ScaleFrame, std::bind(&ActionTimelineCache::loadScaleFrame, this, _1)));
|
|
|
|
_funcs.insert(Pair(FrameType_RotationFrame, std::bind(&ActionTimelineCache::loadRotationFrame, this, _1)));
|
|
|
|
_funcs.insert(Pair(FrameType_SkewFrame, std::bind(&ActionTimelineCache::loadSkewFrame, this, _1)));
|
|
|
|
_funcs.insert(Pair(FrameType_RotationSkewFrame, std::bind(&ActionTimelineCache::loadRotationSkewFrame, this, _1)));
|
|
|
|
_funcs.insert(Pair(FrameType_AnchorFrame, std::bind(&ActionTimelineCache::loadAnchorPointFrame, this, _1)));
|
|
|
|
_funcs.insert(Pair(FrameType_InnerActionFrame, std::bind(&ActionTimelineCache::loadInnerActionFrame, this, _1)));
|
|
|
|
_funcs.insert(Pair(FrameType_ColorFrame, std::bind(&ActionTimelineCache::loadColorFrame, this, _1)));
|
|
|
|
_funcs.insert(Pair(FrameType_TextureFrame, std::bind(&ActionTimelineCache::loadTextureFrame, this, _1)));
|
|
|
|
_funcs.insert(Pair(FrameType_EventFrame, std::bind(&ActionTimelineCache::loadEventFrame, this, _1)));
|
|
|
|
_funcs.insert(Pair(FrameType_ZOrderFrame, std::bind(&ActionTimelineCache::loadZOrderFrame, this, _1)));
|
2014-06-11 11:10:07 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
void ActionTimelineCache::removeAction(const std::string& fileName)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
if (_animationActions.find(fileName) != _animationActions.end())
|
|
|
|
{
|
|
|
|
_animationActions.erase(fileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
ActionTimeline* ActionTimelineCache::createAction(const std::string& filename)
|
|
|
|
{
|
|
|
|
std::string path = filename;
|
|
|
|
size_t pos = path.find_last_of('.');
|
|
|
|
std::string suffix = path.substr(pos + 1, path.length());
|
|
|
|
CCLOG("suffix = %s", suffix.c_str());
|
|
|
|
|
|
|
|
ActionTimelineCache* cache = ActionTimelineCache::getInstance();
|
|
|
|
|
|
|
|
if (suffix == "csb")
|
|
|
|
{
|
2014-12-01 12:46:29 +08:00
|
|
|
return cache->createActionWithFlatBuffersFile(filename);
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
else if (suffix == "json" || suffix == "ExportJson")
|
|
|
|
{
|
|
|
|
return cache->createActionFromJson(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActionTimeline* ActionTimelineCache::createActionFromJson(const std::string& fileName)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
2014-06-13 16:03:03 +08:00
|
|
|
ActionTimeline* action = _animationActions.at(fileName);
|
2014-06-11 11:10:07 +08:00
|
|
|
if (action == nullptr)
|
|
|
|
{
|
|
|
|
action = loadAnimationActionWithFile(fileName);
|
|
|
|
}
|
|
|
|
return action->clone();
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
ActionTimeline* ActionTimelineCache::loadAnimationActionWithFile(const std::string& fileName)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
// Read content from file
|
2014-08-29 15:39:52 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(fileName);
|
2014-06-11 11:10:07 +08:00
|
|
|
std::string contentStr = FileUtils::getInstance()->getStringFromFile(fullPath);
|
|
|
|
|
|
|
|
return loadAnimationActionWithContent(fileName, contentStr);
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
ActionTimeline* ActionTimelineCache::loadAnimationActionWithContent(const std::string&fileName, const std::string& content)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
// if already exists an action with filename, then return this action
|
2014-06-13 16:03:03 +08:00
|
|
|
ActionTimeline* action = _animationActions.at(fileName);
|
2014-06-11 11:10:07 +08:00
|
|
|
if(action)
|
|
|
|
return action;
|
|
|
|
|
|
|
|
rapidjson::Document doc;
|
|
|
|
doc.Parse<0>(content.c_str());
|
|
|
|
if (doc.HasParseError())
|
|
|
|
{
|
|
|
|
CCLOG("GetParseError %s\n", doc.GetParseError());
|
|
|
|
}
|
|
|
|
|
|
|
|
const rapidjson::Value& json = DICTOOL->getSubDictionary_json(doc, ACTION);
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
action = ActionTimeline::create();
|
2014-06-11 11:10:07 +08:00
|
|
|
|
|
|
|
action->setDuration(DICTOOL->getIntValue_json(json, DURATION));
|
|
|
|
action->setTimeSpeed(DICTOOL->getFloatValue_json(json, TIME_SPEED, 1.0f));
|
|
|
|
|
|
|
|
int timelineLength = DICTOOL->getArrayCount_json(json, TIMELINES);
|
|
|
|
for (int i = 0; i<timelineLength; i++)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& dic = DICTOOL->getSubDictionary_json(json, TIMELINES, i);
|
|
|
|
Timeline* timeline = loadTimeline(dic);
|
|
|
|
|
|
|
|
if(timeline)
|
|
|
|
action->addTimeline(timeline);
|
|
|
|
}
|
|
|
|
|
|
|
|
_animationActions.insert(fileName, action);
|
|
|
|
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
Timeline* ActionTimelineCache::loadTimeline(const rapidjson::Value& json)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
Timeline* timeline = nullptr;
|
|
|
|
|
|
|
|
// get frame type
|
|
|
|
const char* frameType = DICTOOL->getStringValue_json(json, FRAME_TYPE);
|
|
|
|
if(frameType == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if(frameType && _funcs.find(frameType) != _funcs.end())
|
|
|
|
{
|
|
|
|
timeline = Timeline::create();
|
|
|
|
|
|
|
|
int actionTag = DICTOOL->getIntValue_json(json, ACTION_TAG);
|
|
|
|
timeline->setActionTag(actionTag);
|
|
|
|
|
|
|
|
FrameCreateFunc func = _funcs.at(frameType);
|
|
|
|
|
|
|
|
int length = DICTOOL->getArrayCount_json(json, FRAMES);
|
|
|
|
for (int i = 0; i<length; i++)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& dic = DICTOOL->getSubDictionary_json(json, FRAMES, i);
|
|
|
|
|
|
|
|
Frame* frame = nullptr;
|
|
|
|
|
|
|
|
if (func != nullptr)
|
|
|
|
{
|
|
|
|
frame = func(dic);
|
|
|
|
|
|
|
|
int frameIndex = DICTOOL->getIntValue_json(dic, FRAME_INDEX);
|
|
|
|
frame->setFrameIndex(frameIndex);
|
|
|
|
|
|
|
|
bool tween = DICTOOL->getBooleanValue_json(dic, TWEEN, false);
|
|
|
|
frame->setTween(tween);
|
|
|
|
}
|
|
|
|
|
|
|
|
timeline->addFrame(frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return timeline;
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
Frame* ActionTimelineCache::loadVisibleFrame(const rapidjson::Value& json)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
VisibleFrame* frame = VisibleFrame::create();
|
|
|
|
|
|
|
|
bool visible = DICTOOL->getBooleanValue_json(json, Value);
|
|
|
|
frame->setVisible(visible);
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
Frame* ActionTimelineCache::loadPositionFrame(const rapidjson::Value& json)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
PositionFrame* frame = PositionFrame::create();
|
|
|
|
|
|
|
|
float x = DICTOOL->getFloatValue_json(json, X);
|
|
|
|
float y = DICTOOL->getFloatValue_json(json, Y);
|
|
|
|
frame->setPosition(Point(x,y));
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
Frame* ActionTimelineCache::loadScaleFrame(const rapidjson::Value& json)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
ScaleFrame* frame = ScaleFrame::create();
|
|
|
|
|
|
|
|
float scalex = DICTOOL->getFloatValue_json(json, X);
|
|
|
|
float scaley = DICTOOL->getFloatValue_json(json, Y);
|
|
|
|
|
|
|
|
frame->setScaleX(scalex);
|
|
|
|
frame->setScaleY(scaley);
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
Frame* ActionTimelineCache::loadSkewFrame(const rapidjson::Value& json)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
SkewFrame* frame = SkewFrame::create();
|
|
|
|
|
|
|
|
float skewx = DICTOOL->getFloatValue_json(json, X);
|
|
|
|
float skewy = DICTOOL->getFloatValue_json(json, Y);
|
|
|
|
|
|
|
|
frame->setSkewX(skewx);
|
|
|
|
frame->setSkewY(skewy);
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
Frame* ActionTimelineCache::loadRotationSkewFrame(const rapidjson::Value& json)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
RotationSkewFrame* frame = RotationSkewFrame::create();
|
|
|
|
|
|
|
|
float skewx = DICTOOL->getFloatValue_json(json, X);
|
|
|
|
float skewy = DICTOOL->getFloatValue_json(json, Y);
|
|
|
|
|
|
|
|
frame->setSkewX(skewx);
|
|
|
|
frame->setSkewY(skewy);
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
Frame* ActionTimelineCache::loadRotationFrame(const rapidjson::Value& json)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
RotationFrame* frame = RotationFrame::create();
|
|
|
|
|
|
|
|
float rotation = DICTOOL->getFloatValue_json(json, ROTATION);
|
|
|
|
frame->setRotation(rotation);
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
Frame* ActionTimelineCache::loadAnchorPointFrame (const rapidjson::Value& json)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
AnchorPointFrame* frame = AnchorPointFrame::create();
|
|
|
|
|
|
|
|
float anchorx = DICTOOL->getFloatValue_json(json, X);
|
|
|
|
float anchory = DICTOOL->getFloatValue_json(json, Y);
|
|
|
|
|
|
|
|
frame->setAnchorPoint(Point(anchorx, anchory));
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
Frame* ActionTimelineCache::loadInnerActionFrame(const rapidjson::Value& json)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
InnerActionFrame* frame = InnerActionFrame::create();
|
|
|
|
|
|
|
|
InnerActionType type = (InnerActionType)DICTOOL->getIntValue_json(json, INNER_ACTION);
|
|
|
|
int startFrame = DICTOOL->getIntValue_json(json, START_FRAME);
|
|
|
|
|
|
|
|
frame->setInnerActionType(type);
|
|
|
|
frame->setStartFrameIndex(startFrame);
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
Frame* ActionTimelineCache::loadColorFrame(const rapidjson::Value& json)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
ColorFrame* frame = ColorFrame::create();
|
|
|
|
|
|
|
|
GLubyte alpha = (GLubyte)DICTOOL->getIntValue_json(json, ALPHA);
|
|
|
|
GLubyte red = (GLubyte)DICTOOL->getIntValue_json(json, RED);
|
|
|
|
GLubyte green = (GLubyte)DICTOOL->getIntValue_json(json, GREEN);
|
|
|
|
GLubyte blue = (GLubyte)DICTOOL->getIntValue_json(json, BLUE);
|
|
|
|
|
|
|
|
frame->setAlpha(alpha);
|
|
|
|
frame->setColor(Color3B(red, green, blue));
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
Frame* ActionTimelineCache::loadTextureFrame(const rapidjson::Value& json)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
TextureFrame* frame = TextureFrame::create();
|
|
|
|
|
|
|
|
const char* texture = DICTOOL->getStringValue_json(json, Value);
|
|
|
|
|
2014-06-23 10:12:54 +08:00
|
|
|
if(texture != nullptr)
|
2014-06-11 21:11:22 +08:00
|
|
|
{
|
|
|
|
std::string path = texture;
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2014-06-11 21:11:22 +08:00
|
|
|
SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(path);
|
|
|
|
if(spriteFrame == nullptr)
|
|
|
|
{
|
2014-10-09 18:28:09 +08:00
|
|
|
std::string jsonPath = CSLoader::getInstance()->getJsonPath();
|
2014-06-11 21:11:22 +08:00
|
|
|
path = jsonPath + texture;
|
|
|
|
}
|
|
|
|
|
2014-06-13 19:52:53 +08:00
|
|
|
frame->setTextureName(path);
|
2014-06-11 21:11:22 +08:00
|
|
|
}
|
2014-06-11 11:10:07 +08:00
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
Frame* ActionTimelineCache::loadEventFrame(const rapidjson::Value& json)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
EventFrame* frame = EventFrame::create();
|
|
|
|
|
|
|
|
const char* evnt = DICTOOL->getStringValue_json(json, Value);
|
|
|
|
|
2014-06-23 10:12:54 +08:00
|
|
|
if(evnt != nullptr)
|
2014-06-11 11:10:07 +08:00
|
|
|
frame->setEvent(evnt);
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
Frame* ActionTimelineCache::loadZOrderFrame(const rapidjson::Value& json)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
ZOrderFrame* frame = ZOrderFrame::create();
|
|
|
|
|
|
|
|
int zorder = DICTOOL->getIntValue_json(json, Value);
|
|
|
|
frame->setZOrder(zorder);
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
ActionTimeline* ActionTimelineCache::createActionWithFlatBuffersFile(const std::string &fileName)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
|
|
|
ActionTimeline* action = _animationActions.at(fileName);
|
2014-11-21 15:15:38 +08:00
|
|
|
if (action == NULL)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-11-21 15:15:38 +08:00
|
|
|
action = loadAnimationActionWithFlatBuffersFile(fileName);
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
return action->clone();
|
|
|
|
}
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
ActionTimeline* ActionTimelineCache::loadAnimationActionWithFlatBuffersFile(const std::string &fileName)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
|
|
|
// if already exists an action with filename, then return this action
|
|
|
|
ActionTimeline* action = _animationActions.at(fileName);
|
|
|
|
if (action)
|
|
|
|
return action;
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
std::string path = fileName;
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(fileName.c_str());
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-12-03 10:54:16 +08:00
|
|
|
CC_ASSERT(FileUtils::getInstance()->isFileExist(fullPath));
|
|
|
|
|
|
|
|
Data buf = FileUtils::getInstance()->getDataFromFile(fullPath);
|
|
|
|
|
|
|
|
auto csparsebinary = GetCSParseBinary(buf.getBytes());
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-12-03 10:54:16 +08:00
|
|
|
auto nodeAction = csparsebinary->action();
|
2014-11-21 15:15:38 +08:00
|
|
|
action = ActionTimeline::create();
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
int duration = nodeAction->duration();
|
|
|
|
action->setDuration(duration);
|
|
|
|
float speed = nodeAction->speed();
|
|
|
|
action->setTimeSpeed(speed);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
auto timelines = nodeAction->timeLines();
|
|
|
|
int timelineLength = timelines->size();
|
|
|
|
for (int i = 0; i < timelineLength; i++)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-11-21 15:15:38 +08:00
|
|
|
auto timelineFlatBuf = timelines->Get(i);
|
|
|
|
Timeline* timeline = loadTimelineWithFlatBuffers(timelineFlatBuf);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
|
|
|
if (timeline)
|
|
|
|
action->addTimeline(timeline);
|
|
|
|
}
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
_animationActions.insert(fileName, action);
|
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
Timeline* ActionTimelineCache::loadTimelineWithFlatBuffers(const flatbuffers::TimeLine *flatbuffers)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
|
|
|
Timeline* timeline = nullptr;
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
// get frame type
|
|
|
|
std::string frameType = flatbuffers->frameType()->c_str();
|
|
|
|
if(frameType == "")
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
CCLOG("frameType = %s", frameType.c_str());
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
if(frameType != "")
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
|
|
|
timeline = Timeline::create();
|
2014-11-21 15:15:38 +08:00
|
|
|
|
|
|
|
int actionTag = flatbuffers->actionTag();
|
2014-10-09 18:28:09 +08:00
|
|
|
timeline->setActionTag(actionTag);
|
|
|
|
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
auto framesFlatbuf = flatbuffers->frames();
|
|
|
|
int length = framesFlatbuf->size();
|
|
|
|
for (int i = 0; i < length; i++)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-11-21 15:15:38 +08:00
|
|
|
auto frameFlatbuf = framesFlatbuf->Get(i);
|
|
|
|
Frame* frame = nullptr;
|
|
|
|
|
|
|
|
if (frameType == FrameType_VisibleFrame)
|
|
|
|
{
|
|
|
|
auto visibleFrame = frameFlatbuf->visibleFrame();
|
|
|
|
frame = loadVisibleFrameWithFlatBuffers(visibleFrame);
|
|
|
|
}
|
|
|
|
else if (frameType == FrameType_ZOrderFrame)
|
|
|
|
{
|
|
|
|
auto zOrderFrame = frameFlatbuf->zOrderFrame();
|
|
|
|
frame = loadZOrderFrameWithFlatBuffers(zOrderFrame);
|
|
|
|
}
|
2014-11-27 16:49:19 +08:00
|
|
|
else if (frameType == FrameType_RotationSkewFrame)
|
2014-11-21 15:15:38 +08:00
|
|
|
{
|
|
|
|
auto rotationSkewFrame = frameFlatbuf->rotationSkewFrame();
|
|
|
|
frame = loadRotationSkewFrameWithFlatBuffers(rotationSkewFrame);
|
|
|
|
}
|
|
|
|
else if (frameType == FrameType_EventFrame)
|
|
|
|
{
|
|
|
|
auto eventFrame = frameFlatbuf->eventFrame();
|
|
|
|
frame = loadEventFrameWithFlatBuffers(eventFrame);
|
|
|
|
}
|
|
|
|
else if (frameType == FrameType_AnchorFrame)
|
|
|
|
{
|
|
|
|
auto anchorPointFrame = frameFlatbuf->anchorPointFrame();
|
|
|
|
frame = loadAnchorPointFrameWithFlatBuffers(anchorPointFrame);
|
|
|
|
}
|
|
|
|
else if (frameType == FrameType_PositionFrame)
|
|
|
|
{
|
|
|
|
auto potisionFrame = frameFlatbuf->positionFrame();
|
|
|
|
frame = loadPositionFrameWithFlatBuffers(potisionFrame);
|
|
|
|
}
|
|
|
|
else if (frameType == FrameType_ScaleFrame)
|
|
|
|
{
|
|
|
|
auto scaleFrame = frameFlatbuf->scaleFrame();
|
|
|
|
frame = loadScaleFrameWithFlatBuffers(scaleFrame);
|
|
|
|
}
|
|
|
|
else if (frameType == FrameType_ColorFrame)
|
|
|
|
{
|
|
|
|
auto colorFrame = frameFlatbuf->colorFrame();
|
|
|
|
frame = loadColorFrameWithFlatBuffers(colorFrame);
|
|
|
|
}
|
|
|
|
else if (frameType == FrameType_TextureFrame)
|
|
|
|
{
|
|
|
|
auto textureFrame = frameFlatbuf->textureFrame();
|
|
|
|
frame = loadTextureFrameWithFlatBuffers(textureFrame);
|
|
|
|
}
|
|
|
|
|
2014-10-09 18:28:09 +08:00
|
|
|
timeline->addFrame(frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return timeline;
|
|
|
|
}
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
Frame* ActionTimelineCache::loadVisibleFrameWithFlatBuffers(const flatbuffers::TimeLineBoolFrame *flatbuffers)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
|
|
|
VisibleFrame* frame = VisibleFrame::create();
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
bool visible = flatbuffers->value();
|
|
|
|
frame->setVisible(visible);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
CCLOG("visible = %d", visible);
|
|
|
|
|
|
|
|
int frameIndex = flatbuffers->frameIndex();
|
|
|
|
frame->setFrameIndex(frameIndex);
|
|
|
|
|
|
|
|
bool tween = flatbuffers->tween();
|
|
|
|
frame->setTween(tween);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
Frame* ActionTimelineCache::loadZOrderFrameWithFlatBuffers(const flatbuffers::TimeLineIntFrame *flatbuffers)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-11-21 15:15:38 +08:00
|
|
|
ZOrderFrame* frame = ZOrderFrame::create();
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
int zorder = flatbuffers->value();
|
|
|
|
frame->setZOrder(zorder);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
CCLOG("zorder = %d", zorder);
|
|
|
|
|
|
|
|
int frameIndex = flatbuffers->frameIndex();
|
|
|
|
frame->setFrameIndex(frameIndex);
|
|
|
|
|
|
|
|
bool tween = flatbuffers->tween();
|
|
|
|
frame->setTween(tween);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
Frame* ActionTimelineCache::loadRotationSkewFrameWithFlatBuffers(const flatbuffers::TimeLinePointFrame *flatbuffers)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-11-21 15:15:38 +08:00
|
|
|
RotationSkewFrame* frame = RotationSkewFrame::create();
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
auto f_rotationSkew = flatbuffers->postion();
|
|
|
|
Vec2 rotationSkew(f_rotationSkew->x(), f_rotationSkew->y());
|
|
|
|
frame->setSkewX(rotationSkew.x);
|
|
|
|
frame->setSkewY(rotationSkew.y);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
int frameIndex = flatbuffers->frameIndex();
|
|
|
|
frame->setFrameIndex(frameIndex);
|
|
|
|
|
|
|
|
bool tween = flatbuffers->tween();
|
|
|
|
frame->setTween(tween);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
Frame* ActionTimelineCache::loadEventFrameWithFlatBuffers(const flatbuffers::TimeLineStringFrame *flatbuffers)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-11-21 15:15:38 +08:00
|
|
|
EventFrame* frame = EventFrame::create();
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
std::string event = flatbuffers->value()->c_str();
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
if (event != "")
|
|
|
|
frame->setEvent(event);
|
|
|
|
|
|
|
|
CCLOG("event = %s", event.c_str());
|
|
|
|
|
|
|
|
int frameIndex = flatbuffers->frameIndex();
|
|
|
|
frame->setFrameIndex(frameIndex);
|
|
|
|
|
|
|
|
bool tween = flatbuffers->tween();
|
|
|
|
frame->setTween(tween);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
Frame* ActionTimelineCache::loadAnchorPointFrameWithFlatBuffers(const flatbuffers::TimeLinePointFrame *flatbuffers)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
|
|
|
AnchorPointFrame* frame = AnchorPointFrame::create();
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
auto f_anchorPoint = flatbuffers->postion();
|
|
|
|
Vec2 anchorPoint(f_anchorPoint->x(), f_anchorPoint->y());
|
|
|
|
frame->setAnchorPoint(anchorPoint);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
int frameIndex = flatbuffers->frameIndex();
|
|
|
|
frame->setFrameIndex(frameIndex);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
bool tween = flatbuffers->tween();
|
|
|
|
frame->setTween(tween);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
Frame* ActionTimelineCache::loadPositionFrameWithFlatBuffers(const flatbuffers::TimeLinePointFrame *flatbuffers)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-11-21 15:15:38 +08:00
|
|
|
PositionFrame* frame = PositionFrame::create();
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
auto f_position = flatbuffers->postion();
|
|
|
|
Vec2 position(f_position->x(), f_position->y());
|
|
|
|
frame->setPosition(position);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
int frameIndex = flatbuffers->frameIndex();
|
|
|
|
frame->setFrameIndex(frameIndex);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
bool tween = flatbuffers->tween();
|
|
|
|
frame->setTween(tween);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
Frame* ActionTimelineCache::loadScaleFrameWithFlatBuffers(const flatbuffers::TimeLinePointFrame *flatbuffers)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-11-21 15:15:38 +08:00
|
|
|
ScaleFrame* frame = ScaleFrame::create();
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
auto f_scale = flatbuffers->postion();
|
|
|
|
Vec2 scale(f_scale->x(), f_scale->y());
|
|
|
|
frame->setScaleX(scale.x);
|
|
|
|
frame->setScaleY(scale.y);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
int frameIndex = flatbuffers->frameIndex();
|
|
|
|
frame->setFrameIndex(frameIndex);
|
|
|
|
|
|
|
|
bool tween = flatbuffers->tween();
|
|
|
|
frame->setTween(tween);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
Frame* ActionTimelineCache::loadColorFrameWithFlatBuffers(const flatbuffers::TimeLineColorFrame *flatbuffers)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-11-21 15:15:38 +08:00
|
|
|
ColorFrame* frame = ColorFrame::create();
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
auto f_color = flatbuffers->color();
|
|
|
|
Color3B color(f_color->r(), f_color->g(), f_color->b());
|
|
|
|
frame->setColor(color);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-12-09 15:13:09 +08:00
|
|
|
int alpha = f_color->a();
|
|
|
|
frame->setAlpha(alpha);
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
int frameIndex = flatbuffers->frameIndex();
|
|
|
|
frame->setFrameIndex(frameIndex);
|
|
|
|
|
|
|
|
bool tween = flatbuffers->tween();
|
|
|
|
frame->setTween(tween);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
Frame* ActionTimelineCache::loadTextureFrameWithFlatBuffers(const flatbuffers::TimeLineTextureFrame *flatbuffers)
|
2014-10-09 18:28:09 +08:00
|
|
|
{
|
2014-12-10 12:36:14 +08:00
|
|
|
std::string path = "";
|
|
|
|
int resourceType = 0;
|
|
|
|
std::string plist = "";
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
TextureFrame* frame = TextureFrame::create();
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-12-10 12:36:14 +08:00
|
|
|
auto fileNameData = flatbuffers->fileNameData();
|
|
|
|
|
|
|
|
resourceType = fileNameData->resourceType();
|
|
|
|
switch (resourceType)
|
2014-12-08 15:48:24 +08:00
|
|
|
{
|
2014-12-10 12:36:14 +08:00
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
path = fileNameData->path()->c_str();
|
|
|
|
if (FileUtils::getInstance()->isFileExist(path))
|
|
|
|
{
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path);
|
|
|
|
path = fullPath;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
path = "";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
plist = fileNameData->plistFile()->c_str();
|
|
|
|
if (FileUtils::getInstance()->isFileExist(plist))
|
|
|
|
{
|
|
|
|
path = fileNameData->path()->c_str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
path = "";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2014-12-08 15:48:24 +08:00
|
|
|
}
|
2014-12-10 12:36:14 +08:00
|
|
|
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
frame->setTextureName(path);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
int frameIndex = flatbuffers->frameIndex();
|
|
|
|
frame->setFrameIndex(frameIndex);
|
|
|
|
|
|
|
|
bool tween = flatbuffers->tween();
|
|
|
|
frame->setTween(tween);
|
2014-10-09 18:28:09 +08:00
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
2014-12-08 14:32:33 +08:00
|
|
|
|
|
|
|
ActionTimeline* ActionTimelineCache::createActionWithFlatBuffersForSimulator(const std::string& fileName)
|
|
|
|
{
|
|
|
|
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
|
|
|
|
fbs->_isSimulator = true;
|
|
|
|
auto builder = fbs->createFlatBuffersWithXMLFileForSimulator(fileName);
|
|
|
|
|
|
|
|
ActionTimeline* action = ActionTimeline::create();
|
|
|
|
|
|
|
|
auto csparsebinary = GetCSParseBinary(builder->GetBufferPointer());
|
|
|
|
auto nodeAction = csparsebinary->action();
|
|
|
|
|
|
|
|
action = ActionTimeline::create();
|
|
|
|
|
|
|
|
int duration = nodeAction->duration();
|
|
|
|
action->setDuration(duration);
|
|
|
|
|
|
|
|
float speed = nodeAction->speed();
|
|
|
|
action->setTimeSpeed(speed);
|
|
|
|
|
|
|
|
auto timeLines = nodeAction->timeLines();
|
|
|
|
int timelineLength = timeLines->size();
|
|
|
|
for (int i = 0; i < timelineLength; i++)
|
|
|
|
{
|
|
|
|
auto timelineFlatBuf = timeLines->Get(i);
|
|
|
|
Timeline* timeline = loadTimelineWithFlatBuffers(timelineFlatBuf);
|
|
|
|
|
|
|
|
if (timeline)
|
|
|
|
action->addTimeline(timeline);
|
|
|
|
}
|
|
|
|
|
|
|
|
fbs->deleteFlatBufferBuilder();
|
|
|
|
|
|
|
|
return action;
|
|
|
|
}
|
2014-06-11 11:10:07 +08:00
|
|
|
|
|
|
|
}
|
2014-06-20 11:36:48 +08:00
|
|
|
}
|