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

586 lines
23 KiB
C++
Raw Normal View History

2014-03-04 16:51:35 +08:00
#include "SliderReader.h"
2014-03-11 17:13:54 +08:00
#include "ui/UISlider.h"
#include "cocostudio/CocoLoader.h"
#include "cocostudio/CSParseBinary.pb.h"
2014-10-09 18:28:09 +08:00
#include "tinyxml2/tinyxml2.h"
2014-03-04 16:51:35 +08:00
2014-03-06 16:15:03 +08:00
USING_NS_CC;
using namespace ui;
2014-03-04 16:51:35 +08:00
namespace cocostudio
{
static const char* P_Scale9Enable = "scale9Enable";
static const char* P_Percent = "percent";
static const char* P_BarFileNameData = "barFileNameData";
static const char* P_Length = "length";
static const char* P_BallNormalData = "ballNormalData";
static const char* P_BallPressedData = "ballPressedData";
static const char* P_BallDisabledData = "ballDisabledData";
static const char* P_ProgressBarData = "progressBarData";
static SliderReader* instanceSliderReader = nullptr;
2014-03-04 16:51:35 +08:00
IMPLEMENT_CLASS_WIDGET_READER_INFO(SliderReader)
SliderReader::SliderReader()
{
}
SliderReader::~SliderReader()
{
}
SliderReader* SliderReader::getInstance()
{
if (!instanceSliderReader)
{
instanceSliderReader = new (std::nothrow) SliderReader();
2014-03-04 16:51:35 +08:00
}
return instanceSliderReader;
}
2014-06-23 10:02:09 +08:00
void SliderReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode* cocoNode)
{
2014-06-11 09:35:24 +08:00
this->beginSetBasicProperties(widget);
Slider* slider = static_cast<Slider*>(widget);
float barLength = 0.0f;
2014-06-18 14:44:00 +08:00
int percent = slider->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
//control custom properties
else if (key == P_Scale9Enable) {
slider->setScale9Enabled(valueToBool(value));
}
else if(key == P_Percent){
2014-06-18 14:44:00 +08:00
percent = valueToInt(value);
}else if(key == P_BarFileNameData){
2014-07-01 16:31:17 +08:00
stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
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);
slider->loadBarTexture(backgroundValue, imageFileNameType);
}else if(key == P_Length){
barLength = valueToFloat(value);
}else if(key == P_BallNormalData){
2014-07-01 16:31:17 +08:00
stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
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);
slider->loadSlidBallTextureNormal(backgroundValue, imageFileNameType);
}else if(key == P_BallPressedData){
2014-07-01 16:31:17 +08:00
stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
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);
slider->loadSlidBallTexturePressed(backgroundValue, imageFileNameType);
}else if(key == P_BallDisabledData){
2014-07-01 16:31:17 +08:00
stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
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);
slider->loadSlidBallTextureDisabled(backgroundValue, imageFileNameType);
}else if(key == P_ProgressBarData){
2014-07-01 16:31:17 +08:00
stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
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);
slider->loadProgressBarTexture(backgroundValue, imageFileNameType);
}
} //end of for loop
if (slider->isScale9Enabled()) {
slider->setContentSize(Size(barLength, slider->getContentSize().height));
}
2014-06-18 14:44:00 +08:00
slider->setPercent(percent);
2014-06-11 09:35:24 +08:00
this->endSetBasicProperties(widget);
}
2014-03-04 16:51:35 +08:00
void SliderReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
{
WidgetReader::setPropsFromJsonDictionary(widget, options);
2014-04-04 17:35:15 +08:00
2014-03-04 16:51:35 +08:00
Slider* slider = static_cast<Slider*>(widget);
bool barTextureScale9Enable = DICTOOL->getBooleanValue_json(options, P_Scale9Enable);
2014-03-04 16:51:35 +08:00
slider->setScale9Enabled(barTextureScale9Enable);
2014-04-04 17:35:15 +08:00
slider->setPercent(DICTOOL->getIntValue_json(options, P_Percent));
2014-04-04 17:35:15 +08:00
2014-07-01 14:01:27 +08:00
// bool bt = DICTOOL->checkObjectExist_json(options, P_BarFileName);
2014-07-01 10:57:12 +08:00
float barLength = DICTOOL->getFloatValue_json(options, P_Length,290);
2014-07-01 14:01:27 +08:00
const rapidjson::Value& imageFileNameDic = DICTOOL->getSubDictionary_json(options, P_BarFileNameData);
int imageFileNameType = DICTOOL->getIntValue_json(imageFileNameDic, P_ResourceType);
std::string imageFileName = this->getResourcePath(imageFileNameDic, P_Path, (Widget::TextureResType)imageFileNameType);
slider->loadBarTexture(imageFileName, (Widget::TextureResType)imageFileNameType);
2014-04-04 17:35:15 +08:00
2014-07-01 14:01:27 +08:00
if (barTextureScale9Enable)
{
slider->setContentSize(Size(barLength, slider->getContentSize().height));
2014-03-04 16:51:35 +08:00
}
2014-04-04 17:35:15 +08:00
//loading normal slider ball texture
const rapidjson::Value& normalDic = DICTOOL->getSubDictionary_json(options, P_BallNormalData);
int normalType = DICTOOL->getIntValue_json(normalDic, P_ResourceType);
2014-07-01 14:01:27 +08:00
imageFileName = this->getResourcePath(normalDic, P_Path, (Widget::TextureResType)normalType);
slider->loadSlidBallTextureNormal(imageFileName, (Widget::TextureResType)normalType);
2014-04-04 17:35:15 +08:00
2014-03-04 16:51:35 +08:00
2014-04-04 17:35:15 +08:00
//loading slider ball press texture
const rapidjson::Value& pressedDic = DICTOOL->getSubDictionary_json(options, P_BallPressedData);
int pressedType = DICTOOL->getIntValue_json(pressedDic, P_ResourceType);
std::string pressedFileName = this->getResourcePath(pressedDic, P_Path, (Widget::TextureResType)pressedType);
slider->loadSlidBallTexturePressed(pressedFileName, (Widget::TextureResType)pressedType);
2014-03-04 16:51:35 +08:00
2014-04-04 17:35:15 +08:00
//loading silder ball disable texture
const rapidjson::Value& disabledDic = DICTOOL->getSubDictionary_json(options, P_BallDisabledData);
int disabledType = DICTOOL->getIntValue_json(disabledDic, P_ResourceType);
std::string disabledFileName = this->getResourcePath(disabledDic, P_Path, (Widget::TextureResType)disabledType);
slider->loadSlidBallTextureDisabled(disabledFileName, (Widget::TextureResType)disabledType);
2014-03-04 16:51:35 +08:00
2014-04-04 17:35:15 +08:00
//load slider progress texture
const rapidjson::Value& progressBarDic = DICTOOL->getSubDictionary_json(options, P_ProgressBarData);
int progressBarType = DICTOOL->getIntValue_json(progressBarDic, P_ResourceType);
std::string progressBarFileName = this->getResourcePath(progressBarDic, P_Path, (Widget::TextureResType)progressBarType);
slider->loadProgressBarTexture(progressBarFileName, (Widget::TextureResType)progressBarType);
2014-04-04 17:35:15 +08:00
2014-03-04 16:51:35 +08:00
WidgetReader::setColorPropsFromJsonDictionary(widget, options);
}
2014-10-09 18:28:09 +08:00
void SliderReader::setPropsFromProtocolBuffers(ui::Widget *widget, const protocolbuffers::NodeTree &nodeTree)
{
WidgetReader::setPropsFromProtocolBuffers(widget, nodeTree);
Slider* slider = static_cast<Slider*>(widget);
const protocolbuffers::SliderOptions& options = nodeTree.slideroptions();
std::string protocolBuffersPath = GUIReader::getInstance()->getFilePath();
bool barTextureScale9Enable = options.scale9enable();
2014-10-20 10:15:44 +08:00
if (barTextureScale9Enable)
{
slider->setUnifySizeEnabled(false);
}
2014-10-09 18:28:09 +08:00
slider->setScale9Enabled(barTextureScale9Enable);
slider->setPercent(options.percent());
// bool bt = DICTOOL->checkObjectExist_json(options, P_BarFileName);
float barLength = options.has_length() ? options.length() : 290;
const protocolbuffers::ResourceData& imageFileNameDic = options.barfilenamedata();
int imageFileNameType = imageFileNameDic.resourcetype();
std::string imageFileName = this->getResourcePath(imageFileNameDic.path(), (Widget::TextureResType)imageFileNameType);
slider->loadBarTexture(imageFileName, (Widget::TextureResType)imageFileNameType);
if (barTextureScale9Enable)
{
slider->setContentSize(Size(barLength, slider->getContentSize().height));
}
//loading normal slider ball texture
const protocolbuffers::ResourceData& normalDic = options.ballnormaldata();
int normalType = normalDic.resourcetype();
imageFileName = this->getResourcePath(normalDic.path(), (Widget::TextureResType)normalType);
slider->loadSlidBallTextureNormal(imageFileName, (Widget::TextureResType)normalType);
//loading slider ball press texture
const protocolbuffers::ResourceData& pressedDic = options.ballpresseddata();
int pressedType = pressedDic.resourcetype();
std::string pressedFileName = this->getResourcePath(pressedDic.path(), (Widget::TextureResType)pressedType);
slider->loadSlidBallTexturePressed(pressedFileName, (Widget::TextureResType)pressedType);
//loading silder ball disable texture
const protocolbuffers::ResourceData& disabledDic = options.balldisableddata();
int disabledType = disabledDic.resourcetype();
std::string disabledFileName = this->getResourcePath(disabledDic.path(), (Widget::TextureResType)disabledType);
slider->loadSlidBallTextureDisabled(disabledFileName, (Widget::TextureResType)disabledType);
//load slider progress texture
const protocolbuffers::ResourceData& progressBarDic = options.progressbardata();
int progressBarType = progressBarDic.resourcetype();
std::string progressBarFileName = this->getResourcePath(progressBarDic.path(), (Widget::TextureResType)progressBarType);
slider->loadProgressBarTexture(progressBarFileName, (Widget::TextureResType)progressBarType);
bool displaystate = true;
if(options.has_displaystate())
{
displaystate = options.displaystate();
}
slider->setBright(displaystate);
// other commonly protperties
WidgetReader::setColorPropsFromProtocolBuffers(widget, nodeTree);
}
void SliderReader::setPropsFromXML(cocos2d::ui::Widget *widget, const tinyxml2::XMLElement *objectData)
{
WidgetReader::setPropsFromXML(widget, objectData);
Slider* slider = static_cast<Slider*>(widget);
std::string xmlPath = GUIReader::getInstance()->getFilePath();
bool scale9Enabled = false;
float cx = 0.0f, cy = 0.0f, cw = 0.0f, ch = 0.0f;
float swf = 0.0f, shf = 0.0f;
int percent = 0;
int opacity = 255;
// attributes
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
{
std::string name = attribute->Name();
std::string value = attribute->Value();
if (name == "Scale9Enable")
{
if (value == "True")
{
scale9Enabled = true;
}
}
else if (name == "Scale9OriginX")
{
cx = atof(value.c_str());
}
else if (name == "Scale9OriginY")
{
cy = atof(value.c_str());
}
else if (name == "Scale9Width")
{
cw = atof(value.c_str());
}
else if (name == "Scale9Height")
{
ch = atof(value.c_str());
}
else if (name == "Length")
{
}
else if (name == "PercentInfo")
{
percent = atoi(value.c_str());
}
else if (name == "DisplayState")
{
slider->setBright((value == "True") ? true : false);
if (value == "False")
{
slider->setTouchEnabled(false);
}
}
else if (name == "Alpha")
{
opacity = atoi(value.c_str());
}
attribute = attribute->Next();
}
// child elements
const tinyxml2::XMLElement* child = objectData->FirstChildElement();
while (child)
{
std::string name = child->Name();
if (name == "BackGroundData")
{
2014-10-15 17:20:54 +08:00
attribute = child->FirstAttribute();
2014-10-09 18:28:09 +08:00
int resourceType = 0;
std::string path = "", plistFile = "";
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")
{
resourceType = (value == "Normal" || value == "Default" || value == "MarkedSubImage") ? 0 : 1;
}
else if (name == "Plist")
{
plistFile = value;
}
attribute = attribute->Next();
}
switch (resourceType)
{
case 0:
{
slider->loadBarTexture(xmlPath + path, Widget::TextureResType::LOCAL);
break;
}
case 1:
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(xmlPath + plistFile);
slider->loadBarTexture(path, Widget::TextureResType::PLIST);
break;
}
default:
break;
}
}
else if (name == "BallNormalData")
{
2014-10-15 17:20:54 +08:00
attribute = child->FirstAttribute();
2014-10-09 18:28:09 +08:00
int resourceType = 0;
std::string path = "", plistFile = "";
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")
{
resourceType = (value == "Normal" || value == "Default" || value == "MarkedSubImage") ? 0 : 1;
}
else if (name == "Plist")
{
plistFile = value;
}
attribute = attribute->Next();
}
switch (resourceType)
{
case 0:
{
slider->loadSlidBallTextureNormal(xmlPath + path, Widget::TextureResType::LOCAL);
break;
}
case 1:
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(xmlPath + plistFile);
slider->loadSlidBallTextureNormal(path, Widget::TextureResType::PLIST);
break;
}
default:
break;
}
}
else if (name == "BallPressedData")
{
2014-10-15 17:20:54 +08:00
attribute = child->FirstAttribute();
2014-10-09 18:28:09 +08:00
int resourceType = 0;
std::string path = "", plistFile = "";
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")
{
resourceType = (value == "Normal" || value == "Default" || value == "MarkedSubImage") ? 0 : 1;
}
else if (name == "Plist")
{
plistFile = value;
}
attribute = attribute->Next();
}
switch (resourceType)
{
case 0:
{
slider->loadSlidBallTexturePressed(xmlPath + path, Widget::TextureResType::LOCAL);
break;
}
case 1:
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(xmlPath + plistFile);
slider->loadSlidBallTexturePressed(path, Widget::TextureResType::PLIST);
break;
}
default:
break;
}
}
else if (name == "BallDisabledData")
{
2014-10-15 17:20:54 +08:00
attribute = child->FirstAttribute();
2014-10-09 18:28:09 +08:00
int resourceType = 0;
std::string path = "", plistFile = "";
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")
{
resourceType = (value == "Normal" || value == "Default" || value == "MarkedSubImage") ? 0 : 1;
}
else if (name == "Plist")
{
plistFile = value;
}
attribute = attribute->Next();
}
switch (resourceType)
{
case 0:
{
slider->loadSlidBallTextureDisabled(xmlPath + path, Widget::TextureResType::LOCAL);
break;
}
case 1:
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(xmlPath + plistFile);
slider->loadSlidBallTextureDisabled(path, Widget::TextureResType::PLIST);
break;
}
default:
break;
}
}
else if (name == "ProgressBarData")
{
2014-10-15 17:20:54 +08:00
attribute = child->FirstAttribute();
2014-10-09 18:28:09 +08:00
int resourceType = 0;
std::string path = "", plistFile = "";
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")
{
resourceType = (value == "Normal" || value == "Default" || value == "MarkedSubImage") ? 0 : 1;
}
else if (name == "Plist")
{
plistFile = value;
}
attribute = attribute->Next();
}
switch (resourceType)
{
case 0:
{
slider->loadProgressBarTexture(xmlPath + path, Widget::TextureResType::LOCAL);
break;
}
case 1:
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(xmlPath + plistFile);
slider->loadProgressBarTexture(path, Widget::TextureResType::PLIST);
break;
}
default:
break;
}
}
child = child->NextSiblingElement();
}
slider->setScale9Enabled(scale9Enabled);
if (scale9Enabled)
{
slider->setCapInsets(Rect(cx, cy, cw, ch));
slider->setContentSize(Size(swf, shf));
}
slider->setPercent(percent);
slider->setOpacity(opacity);
}
2014-03-04 16:51:35 +08:00
}