axmol/cocos/editor-support/cocostudio/WidgetReader/LoadingBarReader/LoadingBarReader.cpp

377 lines
14 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
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-03-04 16:51:35 +08:00
#include "editor-support/cocostudio/WidgetReader/LoadingBarReader/LoadingBarReader.h"
2014-11-21 15:15:38 +08:00
2014-03-11 17:13:54 +08:00
#include "ui/UILoadingBar.h"
2016-04-18 15:09:21 +08:00
#include "2d/CCSpriteFrameCache.h"
#include "platform/CCFileUtils.h"
#include "editor-support/cocostudio/CocoLoader.h"
#include "editor-support/cocostudio/CSParseBinary_generated.h"
#include "editor-support/cocostudio/FlatBuffersSerialize.h"
2014-11-21 15:15:38 +08:00
#include "tinyxml2.h"
2014-11-21 15:15:38 +08:00
#include "flatbuffers/flatbuffers.h"
2014-03-04 16:51:35 +08:00
2014-03-06 16:15:03 +08:00
USING_NS_CC;
using namespace ui;
2014-11-21 15:15:38 +08:00
using namespace flatbuffers;
2014-03-06 16:15:03 +08:00
2014-03-04 16:51:35 +08:00
namespace cocostudio
{
static const char* P_Scale9Enable = "scale9Enable";
static const char* P_CapInsetsX = "capInsetsX";
static const char* P_CapInsetsY = "capInsetsY";
static const char* P_CapInsetsWidth = "capInsetsWidth";
static const char* P_CapInsetsHeight = "capInsetsHeight";
static const char* P_TextureData = "textureData";
static const char* P_Direction = "direction";
static const char* P_Percent = "percent";
static LoadingBarReader* instanceLoadingBar = nullptr;
2014-03-04 16:51:35 +08:00
2014-11-21 15:15:38 +08:00
IMPLEMENT_CLASS_NODE_READER_INFO(LoadingBarReader)
2014-03-04 16:51:35 +08:00
LoadingBarReader::LoadingBarReader()
{
}
LoadingBarReader::~LoadingBarReader()
{
}
LoadingBarReader* LoadingBarReader::getInstance()
{
if (!instanceLoadingBar)
{
instanceLoadingBar = new (std::nothrow) LoadingBarReader();
2014-03-04 16:51:35 +08:00
}
return instanceLoadingBar;
}
2015-03-30 16:46:33 +08:00
void LoadingBarReader::destroyInstance()
{
CC_SAFE_DELETE(instanceLoadingBar);
}
2014-06-23 10:02:09 +08:00
void LoadingBarReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
{
2014-06-23 10:02:09 +08:00
WidgetReader::setPropsFromBinary(widget, cocoLoader, cocoNode);
LoadingBar* loadingBar = static_cast<LoadingBar*>(widget);
2014-06-11 09:35:24 +08:00
this->beginSetBasicProperties(widget);
float capsx = 0.0f, capsy = 0.0, capsWidth = 0.0, capsHeight = 0.0f;
2014-06-18 14:44:00 +08:00
int percent = loadingBar->getPercent();
2014-07-01 16:31:17 +08:00
stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
2014-06-23 10:02:09 +08:00
for (int i = 0; i < cocoNode->GetChildNum(); ++i) {
std::string key = stChildArray[i].GetName(cocoLoader);
2014-07-01 16:31:17 +08:00
std::string value = stChildArray[i].GetValue(cocoLoader);
2014-06-19 15:16:56 +08:00
//read all basic properties of widget
CC_BASIC_PROPERTY_BINARY_READER
//read all color related properties of widget
CC_COLOR_PROPERTY_BINARY_READER
2014-06-11 09:35:24 +08:00
else if (key == P_Scale9Enable) {
loadingBar->setScale9Enabled(valueToBool(value));
}
else if (key == P_TextureData){
2014-07-01 16:31:17 +08:00
stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
2016-09-12 09:45:34 +08:00
std::string resType = backGroundChildren[2].GetValue(cocoLoader);
Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
2014-06-23 10:02:09 +08:00
std::string backgroundValue = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
loadingBar->loadTexture(backgroundValue, imageFileNameType);
}
else if(key == P_CapInsetsX){
capsx = valueToFloat(value);
}else if(key == P_CapInsetsY){
capsy = valueToFloat(value);
}else if(key == P_CapInsetsWidth){
capsWidth = valueToFloat(value);
}else if(key == P_CapInsetsHeight){
capsHeight = valueToFloat(value);
}else if(key == P_Direction){
loadingBar->setDirection((LoadingBar::Direction)valueToInt(value));
}else if(key == P_Percent){
2014-06-18 14:44:00 +08:00
percent = valueToInt(value);
}
} //end of for loop
if (loadingBar->isScale9Enabled()) {
loadingBar->setCapInsets(Rect(capsx, capsy, capsWidth, capsHeight));
}
2014-06-18 14:44:00 +08:00
loadingBar->setPercent(percent);
2014-06-11 09:35:24 +08:00
this->endSetBasicProperties(widget);
}
2014-03-04 16:51:35 +08:00
void LoadingBarReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
{
WidgetReader::setPropsFromJsonDictionary(widget, options);
LoadingBar* loadingBar = static_cast<LoadingBar*>(widget);
const rapidjson::Value& imageFileNameDic = DICTOOL->getSubDictionary_json(options, P_TextureData);
int imageFileNameType = DICTOOL->getIntValue_json(imageFileNameDic, P_ResourceType);
std::string imageFileName = this->getResourcePath(imageFileNameDic, P_Path, (Widget::TextureResType)imageFileNameType);
loadingBar->loadTexture(imageFileName, (Widget::TextureResType)imageFileNameType);
2014-04-04 17:25:20 +08:00
2014-03-04 16:51:35 +08:00
/* gui mark add load bar scale9 parse */
bool scale9Enable = DICTOOL->getBooleanValue_json(options, P_Scale9Enable);
2014-03-04 16:51:35 +08:00
loadingBar->setScale9Enabled(scale9Enable);
2014-07-01 14:01:27 +08:00
float cx = DICTOOL->getFloatValue_json(options, P_CapInsetsX);
float cy = DICTOOL->getFloatValue_json(options, P_CapInsetsY);
float cw = DICTOOL->getFloatValue_json(options, P_CapInsetsWidth,1);
float ch = DICTOOL->getFloatValue_json(options, P_CapInsetsHeight,1);
if (scale9Enable) {
2014-03-04 16:51:35 +08:00
loadingBar->setCapInsets(Rect(cx, cy, cw, ch));
2014-07-01 14:01:27 +08:00
2014-03-04 16:51:35 +08:00
}
2014-07-01 14:01:27 +08:00
float width = DICTOOL->getFloatValue_json(options, P_Width);
float height = DICTOOL->getFloatValue_json(options, P_Height);
loadingBar->setContentSize(Size(width, height));
2014-07-01 14:01:27 +08:00
2014-03-04 16:51:35 +08:00
/**/
loadingBar->setDirection(LoadingBar::Direction(DICTOOL->getIntValue_json(options, P_Direction)));
2014-07-01 10:57:12 +08:00
loadingBar->setPercent(DICTOOL->getIntValue_json(options, P_Percent,100));
2014-03-04 16:51:35 +08:00
WidgetReader::setColorPropsFromJsonDictionary(widget, options);
2014-12-01 12:46:29 +08:00
}
2014-10-09 18:28:09 +08:00
2014-11-21 15:15:38 +08:00
Offset<Table> LoadingBarReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
flatbuffers::FlatBufferBuilder *builder)
2014-10-09 18:28:09 +08:00
{
2014-11-21 15:15:38 +08:00
auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
2014-10-09 18:28:09 +08:00
2014-11-21 15:15:38 +08:00
std::string path = "";
std::string plistFile = "";
int resourceType = 0;
2014-10-09 18:28:09 +08:00
2014-11-21 15:15:38 +08:00
int percent = 80;
2014-10-09 18:28:09 +08:00
int direction = 0;
// attributes
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
{
std::string name = attribute->Name();
std::string value = attribute->Value();
if (name == "ProgressType")
{
direction = (value == "Left_To_Right") ? 0 : 1;
}
else if (name == "ProgressInfo")
{
percent = atoi(value.c_str());
}
attribute = attribute->Next();
}
// child elements
const tinyxml2::XMLElement* child = objectData->FirstChildElement();
while (child)
{
std::string name = child->Name();
if (name == "ImageFileData")
{
2014-11-21 15:15:38 +08:00
std::string texture = "";
std::string texturePng = "";
2014-10-15 17:20:54 +08:00
attribute = child->FirstAttribute();
2014-10-09 18:28:09 +08:00
while (attribute)
{
2014-10-15 17:20:54 +08:00
name = attribute->Name();
2014-10-09 18:28:09 +08:00
std::string value = attribute->Value();
if (name == "Path")
{
path = value;
}
else if (name == "Type")
{
2014-11-21 15:15:38 +08:00
resourceType = getResourceType(value);
2014-10-09 18:28:09 +08:00
}
else if (name == "Plist")
{
plistFile = value;
2014-11-21 15:15:38 +08:00
texture = value;
2014-10-09 18:28:09 +08:00
}
attribute = attribute->Next();
}
2014-11-21 15:15:38 +08:00
if (resourceType == 1)
2014-10-09 18:28:09 +08:00
{
2014-11-21 15:15:38 +08:00
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
2014-12-11 19:15:04 +08:00
fbs->_textures.push_back(builder->CreateString(texture));
2014-10-09 18:28:09 +08:00
}
}
child = child->NextSiblingElement();
}
2014-11-21 15:15:38 +08:00
auto options = CreateLoadingBarOptions(*builder,
widgetOptions,
CreateResourceData(*builder,
builder->CreateString(path),
builder->CreateString(plistFile),
resourceType),
percent,
direction);
return *(Offset<Table>*)(&options);
}
void LoadingBarReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *loadingBarOptions)
{
LoadingBar* loadingBar = static_cast<LoadingBar*>(node);
auto options = (LoadingBarOptions*)loadingBarOptions;
bool fileExist = false;
std::string errorFilePath = "";
2014-11-21 15:15:38 +08:00
auto imageFileNameDic = options->textureData();
int imageFileNameType = imageFileNameDic->resourceType();
std::string imageFileName = imageFileNameDic->path()->c_str();
switch (imageFileNameType)
{
case 0:
{
if (FileUtils::getInstance()->isFileExist(imageFileName))
{
fileExist = true;
}
else if (SpriteFrameCache::getInstance()->getSpriteFrameByName(imageFileName))
{
fileExist = true;
imageFileNameType = 1;
}
else
{
errorFilePath = imageFileName;
fileExist = false;
}
break;
}
case 1:
{
std::string plist = imageFileNameDic->plistFile()->c_str();
SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(imageFileName);
if (spriteFrame)
{
fileExist = true;
}
else
{
if (FileUtils::getInstance()->isFileExist(plist))
{
ValueMap value = FileUtils::getInstance()->getValueMapFromFile(plist);
ValueMap metadata = value["metadata"].asValueMap();
std::string textureFileName = metadata["textureFileName"].asString();
if (!FileUtils::getInstance()->isFileExist(textureFileName))
{
errorFilePath = textureFileName;
}
}
else
{
errorFilePath = plist;
}
fileExist = false;
}
break;
}
default:
break;
}
if (fileExist)
{
loadingBar->loadTexture(imageFileName, (Widget::TextureResType)imageFileNameType);
}
2014-11-21 15:15:38 +08:00
int direction = options->direction();
loadingBar->setDirection(LoadingBar::Direction(direction));
int percent = options->percent();
2014-10-09 18:28:09 +08:00
loadingBar->setPercent(percent);
2014-11-21 15:15:38 +08:00
auto widgetReader = WidgetReader::getInstance();
widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
}
Node* LoadingBarReader::createNodeWithFlatBuffers(const flatbuffers::Table *loadingBarOptions)
2014-11-21 15:15:38 +08:00
{
LoadingBar* loadingBar = LoadingBar::create();
setPropsWithFlatBuffers(loadingBar, (Table*)loadingBarOptions);
return loadingBar;
}
int LoadingBarReader::getResourceType(std::string key)
{
if(key == "Normal" || key == "Default")
{
return 0;
}
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
if(fbs->_isSimulator)
{
if(key == "MarkedSubImage")
{
return 0;
}
}
return 1;
2014-10-09 18:28:09 +08:00
}
2014-03-04 16:51:35 +08:00
}