12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586 |
- /****************************************************************************
- Copyright (c) 2013-2017 Chukong Technologies Inc.
- 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.
- ****************************************************************************/
- #include "editor-support/cocostudio/CCSGUIReader.h"
- #include <fstream>
- #include <iostream>
- #include "ui/CocosGUI.h"
- #include "platform/CCFileUtils.h"
- #include "2d/CCSpriteFrameCache.h"
- #include "base/CCDirector.h"
- #include "base/ccUtils.h"
- #include "editor-support/cocostudio/CCActionManagerEx.h"
- #include "editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.h"
- #include "editor-support/cocostudio/WidgetReader/CheckBoxReader/CheckBoxReader.h"
- #include "editor-support/cocostudio/WidgetReader/SliderReader/SliderReader.h"
- #include "editor-support/cocostudio/WidgetReader/ImageViewReader/ImageViewReader.h"
- #include "editor-support/cocostudio/WidgetReader/LoadingBarReader/LoadingBarReader.h"
- #include "editor-support/cocostudio/WidgetReader/TextAtlasReader/TextAtlasReader.h"
- #include "editor-support/cocostudio/WidgetReader/TextReader/TextReader.h"
- #include "editor-support/cocostudio/WidgetReader/TextBMFontReader/TextBMFontReader.h"
- #include "editor-support/cocostudio/WidgetReader/TextFieldReader/TextFieldReader.h"
- #include "editor-support/cocostudio/WidgetReader/LayoutReader/LayoutReader.h"
- #include "editor-support/cocostudio/WidgetReader/PageViewReader/PageViewReader.h"
- #include "editor-support/cocostudio/WidgetReader/ScrollViewReader/ScrollViewReader.h"
- #include "editor-support/cocostudio/WidgetReader/ListViewReader/ListViewReader.h"
- #include "editor-support/cocostudio/CocoLoader.h"
- #include "tinyxml2.h"
- using namespace cocos2d;
- using namespace cocos2d::ui;
- namespace cocostudio {
-
- static GUIReader* sharedReader = nullptr;
- GUIReader::GUIReader():
- m_strFilePath("")
- {
- ObjectFactory* factoryCreate = ObjectFactory::getInstance();
-
- factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(ButtonReader));
- factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(CheckBoxReader));
- factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(SliderReader));
- factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(ImageViewReader));
- factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(LoadingBarReader));
- factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(TextAtlasReader));
- factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(TextReader));
- factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(TextBMFontReader));
- factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(TextFieldReader));
- factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(LayoutReader));
- factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(PageViewReader));
- factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(ScrollViewReader));
- factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(ListViewReader));
-
- factoryCreate->registerType(CREATE_CLASS_GUI_INFO(Button));
- factoryCreate->registerType(CREATE_CLASS_GUI_INFO(CheckBox));
- factoryCreate->registerType(CREATE_CLASS_GUI_INFO(ImageView));
- factoryCreate->registerType(CREATE_CLASS_GUI_INFO(Text));
- factoryCreate->registerType(CREATE_CLASS_GUI_INFO(TextAtlas));
- factoryCreate->registerType(CREATE_CLASS_GUI_INFO(TextBMFont));
- factoryCreate->registerType(CREATE_CLASS_GUI_INFO(LoadingBar));
- factoryCreate->registerType(CREATE_CLASS_GUI_INFO(Slider));
- factoryCreate->registerType(CREATE_CLASS_GUI_INFO(TextField));
- factoryCreate->registerType(CREATE_CLASS_GUI_INFO(Layout));
- factoryCreate->registerType(CREATE_CLASS_GUI_INFO(ListView));
- factoryCreate->registerType(CREATE_CLASS_GUI_INFO(PageView));
- factoryCreate->registerType(CREATE_CLASS_GUI_INFO(ScrollView));
- }
- GUIReader::~GUIReader()
- {
- }
- GUIReader* GUIReader::getInstance()
- {
- if (!sharedReader)
- {
- sharedReader = new (std::nothrow) GUIReader();
- }
- return sharedReader;
- }
- void GUIReader::destroyInstance()
- {
- CC_SAFE_DELETE(sharedReader);
- }
- int GUIReader::getVersionInteger(const char *str)
- {
- std::string strVersion = str;
- size_t length = strVersion.length();
- if (length < 7)
- {
- return 0;
- }
- size_t pos = strVersion.find_first_of(".");
- std::string t = strVersion.substr(0,pos);
- strVersion = strVersion.substr(pos+1,strVersion.length()-1);
-
- pos = strVersion.find_first_of(".");
- std::string h = strVersion.substr(0,pos);
- strVersion = strVersion.substr(pos+1,strVersion.length()-1);
-
- pos = strVersion.find_first_of(".");
- std::string te = strVersion.substr(0,pos);
- strVersion = strVersion.substr(pos+1,strVersion.length()-1);
-
- pos = strVersion.find_first_of(".");
- std::string s = strVersion.substr(0,pos);
-
- int it = atoi(t.c_str());
- int ih = atoi(h.c_str());
- int ite = atoi(te.c_str());
- int is = atoi(s.c_str());
-
- int iVersion = it*1000+ih*100+ite*10+is;
- // CCLOG("iversion %d",iVersion);
- return iVersion;
- /************************/
- }
- void GUIReader::storeFileDesignSize(const char *fileName, const cocos2d::Size &size)
- {
- std::string keyWidth = fileName;
- keyWidth.append("width");
- std::string keyHeight = fileName;
- keyHeight.append("height");
- _fileDesignSizes[keyWidth] = cocos2d::Value(size.width);
- _fileDesignSizes[keyHeight] = cocos2d::Value(size.height);
- }
- cocos2d::Size GUIReader::getFileDesignSize(const char* fileName) const
- {
- std::string keyWidth = fileName;
- keyWidth.append("width");
- std::string keyHeight = fileName;
- keyHeight.append("height");
- float w = _fileDesignSizes.at(keyWidth).asFloat();
- float h = _fileDesignSizes.at(keyHeight).asFloat();
- return Size(w, h);
- }
-
- void GUIReader::registerTypeAndCallBack(const std::string& classType,
- ObjectFactory::Instance ins,
- Ref *object,
- SEL_ParseEvent callBack)
- {
- ObjectFactory* factoryCreate = ObjectFactory::getInstance();
-
- ObjectFactory::TInfo t(classType, ins);
- factoryCreate->registerType(t);
-
- if (object)
- {
- _mapObject.insert(ParseObjectMap::value_type(classType, object));
- }
-
- if (callBack)
- {
- _mapParseSelector.insert(ParseCallBackMap::value_type(classType, callBack));
- }
- }
- void GUIReader::registerTypeAndCallBack(const std::string& classType,
- ObjectFactory::InstanceFunc ins,
- Ref *object,
- SEL_ParseEvent callBack)
- {
- ObjectFactory* factoryCreate = ObjectFactory::getInstance();
- ObjectFactory::TInfo t(classType, ins);
- factoryCreate->registerType(t);
- if (object)
- {
- _mapObject.insert(ParseObjectMap::value_type(classType, object));
- }
- if (callBack)
- {
- _mapParseSelector.insert(ParseCallBackMap::value_type(classType, callBack));
- }
- }
- Widget* GUIReader::widgetFromJsonFile(const char *fileName)
- {
- std::string jsonpath;
- rapidjson::Document jsonDict;
- jsonpath = fileName;
- // jsonpath = CCFileUtils::getInstance()->fullPathForFilename(fileName);
- size_t pos = jsonpath.find_last_of('/');
- m_strFilePath = jsonpath.substr(0,pos+1);
- std::string contentStr = FileUtils::getInstance()->getStringFromFile(jsonpath);
- jsonDict.Parse<0>(contentStr.c_str());
- if (jsonDict.HasParseError())
- {
- CCLOG("GetParseError %d\n",jsonDict.GetParseError());
- }
- Widget* widget = nullptr;
- const char* fileVersion = DICTOOL->getStringValue_json(jsonDict, "version");
- WidgetPropertiesReader * pReader = nullptr;
- if (fileVersion)
- {
- int versionInteger = getVersionInteger(fileVersion);
- if (versionInteger < 250)
- {
- pReader = new (std::nothrow) WidgetPropertiesReader0250();
- widget = pReader->createWidget(jsonDict, m_strFilePath.c_str(), fileName);
- }
- else
- {
- pReader = new (std::nothrow) WidgetPropertiesReader0300();
- widget = pReader->createWidget(jsonDict, m_strFilePath.c_str(), fileName);
- }
- }
- else
- {
- pReader = new (std::nothrow) WidgetPropertiesReader0250();
- widget = pReader->createWidget(jsonDict, m_strFilePath.c_str(), fileName);
- }
-
- CC_SAFE_DELETE(pReader);
- return widget;
- }
-
- std::string WidgetPropertiesReader::getWidgetReaderClassName(Widget* widget)
- {
- std::string readerName;
-
- // 1st., custom widget parse properties of parent widget with parent widget reader
- if (dynamic_cast<Button*>(widget))
- {
- readerName = "ButtonReader";
- }
- else if (dynamic_cast<CheckBox*>(widget))
- {
- readerName = "CheckBoxReader";
- }
- else if (dynamic_cast<ImageView*>(widget))
- {
- readerName = "ImageViewReader";
- }
- else if (dynamic_cast<TextAtlas*>(widget))
- {
- readerName = "TextAtlasReader";
- }
- else if (dynamic_cast<TextBMFont*>(widget))
- {
- readerName = "TextBMFontReader";
- }
- else if (dynamic_cast<Text*>(widget))
- {
- readerName = "TextReader";
- }
- else if (dynamic_cast<LoadingBar*>(widget))
- {
- readerName = "LoadingBarReader";
- }
- else if (dynamic_cast<Slider*>(widget))
- {
- readerName = "SliderReader";
- }
- else if (dynamic_cast<TextField*>(widget))
- {
- readerName = "TextFieldReader";
- }
- else if (dynamic_cast<ListView*>(widget))
- {
- readerName = "ListViewReader";
- }
- else if (dynamic_cast<PageView*>(widget))
- {
- readerName = "PageViewReader";
- }
- else if (dynamic_cast<ScrollView*>(widget))
- {
- readerName = "ScrollViewReader";
- }
-
- else if (dynamic_cast<Layout*>(widget))
- {
- readerName = "LayoutReader";
- }
- else if (dynamic_cast<Widget*>(widget))
- {
- readerName = "WidgetReader";
- }
-
- return readerName;
- }
- std::string WidgetPropertiesReader::getGUIClassName(const std::string &name)
- {
- std::string convertedClassName = name;
- if (name == "Panel")
- {
- convertedClassName = "Layout";
- }
- else if (name == "TextArea")
- {
- convertedClassName = "Text";
- }
- else if (name == "TextButton")
- {
- convertedClassName = "Button";
- }
- else if (name == "Label")
- {
- convertedClassName = "Text";
- }
- else if (name == "LabelAtlas")
- {
- convertedClassName = "TextAtlas";
- }
- else if (name == "LabelBMFont")
- {
- convertedClassName = "TextBMFont";
- }
-
-
- return convertedClassName;
- }
-
- cocos2d::ui::Widget* WidgetPropertiesReader::createGUI(const std::string &classname)
- {
- std::string name = this->getGUIClassName(classname);
- Ref* object = ObjectFactory::getInstance()->createObject(name);
-
- return dynamic_cast<ui::Widget*>(object);
- }
- WidgetReaderProtocol* WidgetPropertiesReader::createWidgetReaderProtocol(const std::string &classname)
- {
- Ref* object = ObjectFactory::getInstance()->createObject(classname);
-
- return dynamic_cast<WidgetReaderProtocol*>(object);
- }
- Widget* GUIReader::widgetFromBinaryFile(const char *fileName)
- {
- std::string jsonpath;
- rapidjson::Document jsonDict;
- jsonpath = fileName;
- // jsonpath = CCFileUtils::getInstance()->fullPathForFilename(fileName);
- size_t pos = jsonpath.find_last_of('/');
- m_strFilePath = jsonpath.substr(0,pos+1);
- std::string fullPath = FileUtils::getInstance()->fullPathForFilename(fileName);
- auto fileData = FileUtils::getInstance()->getDataFromFile(fullPath);
- auto fileDataBytes = fileData.getBytes();
- auto fileDataSize = fileData.getSize();
-
- const char* fileVersion = "";
- ui::Widget* widget = nullptr;
- if (fileDataBytes != nullptr && fileDataSize > 0)
- {
- CocoLoader tCocoLoader;
- if(true == tCocoLoader.ReadCocoBinBuff((char*)fileDataBytes))
- {
- stExpCocoNode* tpRootCocoNode = tCocoLoader.GetRootCocoNode();
-
- rapidjson::Type tType = tpRootCocoNode->GetType(&tCocoLoader);
- if (rapidjson::kObjectType == tType || rapidjson::kArrayType == tType)
- {
- stExpCocoNode *tpChildArray = tpRootCocoNode->GetChildArray(&tCocoLoader);
-
-
- for (int i = 0; i < tpRootCocoNode->GetChildNum(); ++i) {
- std::string key = tpChildArray[i].GetName(&tCocoLoader);
- if (key == "version") {
- fileVersion = tpChildArray[i].GetValue(&tCocoLoader);
- break;
- }
- }
-
- WidgetPropertiesReader * pReader = nullptr;
- if (fileVersion)
- {
- int versionInteger = getVersionInteger(fileVersion);
- if (versionInteger < 250)
- {
- CCASSERT(0, "You current studio doesn't support binary format, please upgrade to the latest version!");
- pReader = new (std::nothrow) WidgetPropertiesReader0250();
- widget = pReader->createWidgetFromBinary(&tCocoLoader, tpRootCocoNode, fileName);
- }
- else
- {
- pReader = new (std::nothrow) WidgetPropertiesReader0300();
- widget = pReader->createWidgetFromBinary(&tCocoLoader, tpRootCocoNode, fileName);
- }
- }
- else
- {
- pReader = new (std::nothrow) WidgetPropertiesReader0250();
- widget = pReader->createWidgetFromBinary(&tCocoLoader, tpRootCocoNode, fileName);
- }
-
- CC_SAFE_DELETE(pReader);
- }
- }
- }
-
- return widget;
-
- }
- std::string WidgetPropertiesReader::getWidgetReaderClassName(const std::string& classname)
- {
- // create widget reader to parse properties of widget
- std::string readerName = classname;
- if (readerName == "Panel")
- {
- readerName = "Layout";
- }
- else if (readerName == "TextArea")
- {
- readerName = "Text";
- }
- else if (readerName == "TextButton")
- {
- readerName = "Button";
- }
- else if (readerName == "Label")
- {
- readerName = "Text";
- }
- else if (readerName == "LabelAtlas")
- {
- readerName = "TextAtlas";
- }
- else if (readerName == "LabelBMFont")
- {
- readerName = "TextBMFont";
- }
- readerName.append("Reader");
- return readerName;
- }
- void WidgetPropertiesReader::setAnchorPointForWidget(cocos2d::ui::Widget *widget, const rapidjson::Value &options)
- {
- bool isAnchorPointXExists = DICTOOL->checkObjectExist_json(options, "anchorPointX");
- float anchorPointXInFile;
- if (isAnchorPointXExists) {
- anchorPointXInFile = DICTOOL->getFloatValue_json(options, "anchorPointX");
- }else{
- anchorPointXInFile = widget->getAnchorPoint().x;
- }
-
- bool isAnchorPointYExists = DICTOOL->checkObjectExist_json(options, "anchorPointY");
- float anchorPointYInFile;
- if (isAnchorPointYExists) {
- anchorPointYInFile = DICTOOL->getFloatValue_json(options, "anchorPointY");
- }
- else{
- anchorPointYInFile = widget->getAnchorPoint().y;
- }
-
- if (isAnchorPointXExists || isAnchorPointYExists) {
- widget->setAnchorPoint(Vec2(anchorPointXInFile, anchorPointYInFile));
- }
- }
-
- Widget* WidgetPropertiesReader0250::createWidget(const rapidjson::Value& data, const char* fullPath, const char* fileName)
- {
- m_strFilePath = fullPath;
- int texturesCount = DICTOOL->getArrayCount_json(data, "textures");
-
- for (int i=0; i<texturesCount; i++)
- {
- const char* file = DICTOOL->getStringValueFromArray_json(data, "textures", i);
- std::string tp = fullPath;
- tp.append(file);
- SpriteFrameCache::getInstance()->addSpriteFramesWithFile(tp);
- }
- float fileDesignWidth = DICTOOL->getFloatValue_json(data, "designWidth");
- float fileDesignHeight = DICTOOL->getFloatValue_json(data, "designHeight");
- if (fileDesignWidth <= 0 || fileDesignHeight <= 0) {
- CCLOGERROR("Read design size error!\n");
- Size winSize = Director::getInstance()->getWinSize();
- GUIReader::getInstance()->storeFileDesignSize(fileName, winSize);
- }
- else
- {
- GUIReader::getInstance()->storeFileDesignSize(fileName, Size(fileDesignWidth, fileDesignHeight));
- }
- const rapidjson::Value& widgetTree = DICTOOL->getSubDictionary_json(data, "widgetTree");
- Widget* widget = widgetFromJsonDictionary(widgetTree);
-
- /* *********temp********* */
- if (widget->getContentSize().equals(Size::ZERO))
- {
- Layout* rootWidget = dynamic_cast<Layout*>(widget);
- rootWidget->setContentSize(Size(fileDesignWidth, fileDesignHeight));
- }
- /* ********************** */
-
- // widget->setFileDesignSize(Size(fileDesignWidth, fileDesignHeight));
- const rapidjson::Value& actions = DICTOOL->getSubDictionary_json(data, "animation");
- /* *********temp********* */
- // ActionManager::getInstance()->releaseActions();
- /* ********************** */
- // CCLOG("file name == [%s]",fileName);
- Ref* rootWidget = (Ref*) widget;
- ActionManagerEx::getInstance()->initWithDictionary(fileName,actions,rootWidget);
- return widget;
- }
- Widget* WidgetPropertiesReader0250::widgetFromJsonDictionary(const rapidjson::Value&data)
- {
- Widget* widget = nullptr;
- const char* classname = DICTOOL->getStringValue_json(data, "classname");
- const rapidjson::Value& uiOptions = DICTOOL->getSubDictionary_json(data, "options");
- if (classname && strcmp(classname, "Button") == 0)
- {
- widget = cocos2d::ui::Button::create();
- setPropsForButtonFromJsonDictionary(widget, uiOptions);
- }
- else if (classname && strcmp(classname, "CheckBox") == 0)
- {
- widget = CheckBox::create();
- setPropsForCheckBoxFromJsonDictionary(widget, uiOptions);
- }
- else if (classname && strcmp(classname, "Label") == 0)
- {
- widget = cocos2d::ui::Text::create();
- setPropsForLabelFromJsonDictionary(widget, uiOptions);
- }
- else if (classname && strcmp(classname, "LabelAtlas") == 0)
- {
- widget = cocos2d::ui::TextAtlas::create();
- setPropsForLabelAtlasFromJsonDictionary(widget, uiOptions);
- }
- else if (classname && strcmp(classname, "LoadingBar") == 0)
- {
- widget = cocos2d::ui::LoadingBar::create();
- setPropsForLoadingBarFromJsonDictionary(widget, uiOptions);
- }else if (classname && strcmp(classname, "ScrollView") == 0){
- widget = cocos2d::ui::ScrollView::create();
- setPropsForScrollViewFromJsonDictionary(widget, uiOptions);
- }
- else if (classname && strcmp(classname, "TextArea") == 0)
- {
- widget = cocos2d::ui::Text::create();
- setPropsForLabelFromJsonDictionary(widget, uiOptions);
- }
- else if (classname && strcmp(classname, "TextButton") == 0)
- {
- widget = cocos2d::ui::Button::create();
- setPropsForButtonFromJsonDictionary(widget, uiOptions);
- }
- else if (classname && strcmp(classname, "TextField") == 0)
- {
- widget = cocos2d::ui::TextField::create();
- setPropsForTextFieldFromJsonDictionary(widget, uiOptions);
- }
- else if (classname && strcmp(classname, "ImageView") == 0)
- {
- widget = cocos2d::ui::ImageView::create();
- setPropsForImageViewFromJsonDictionary(widget, uiOptions);
- }
- else if (classname && strcmp(classname, "Panel") == 0)
- {
- widget = Layout::create();
- setPropsForLayoutFromJsonDictionary(widget, uiOptions);
- }
- else if (classname && strcmp(classname, "Slider") == 0)
- {
- widget = cocos2d::ui::Slider::create();
- setPropsForSliderFromJsonDictionary(widget, uiOptions);
- }
- else if (classname && strcmp(classname, "LabelBMFont") == 0)
- {
- widget = cocos2d::ui::TextBMFont::create();
- setPropsForLabelBMFontFromJsonDictionary(widget, uiOptions);
- }
- else if (classname && strcmp(classname, "DragPanel") == 0)
- {
- widget = cocos2d::ui::ScrollView::create();
- setPropsForScrollViewFromJsonDictionary(widget, uiOptions);
- }
-
- int childrenCount = DICTOOL->getArrayCount_json(data, "children");
- for (int i=0;i<childrenCount;i++)
- {
- const rapidjson::Value& subData = DICTOOL->getDictionaryFromArray_json(data, "children", i);
- Widget* child = widgetFromJsonDictionary(subData);
- if (child)
- {
- widget->addChild(child);
- }
- }
-
- return widget;
- }
- void WidgetPropertiesReader0250::setPropsForWidgetFromJsonDictionary(Widget*widget,const rapidjson::Value&options)
- {
- bool ignoreSizeExsit = DICTOOL->checkObjectExist_json(options, "ignoreSize");
- if (ignoreSizeExsit)
- {
- widget->ignoreContentAdaptWithSize(DICTOOL->getBooleanValue_json(options, "ignoreSize"));
- }
-
- float w = DICTOOL->getFloatValue_json(options, "width");
- float h = DICTOOL->getFloatValue_json(options, "height");
- widget->setContentSize(Size(w, h));
-
- widget->setTag(DICTOOL->getIntValue_json(options, "tag"));
- widget->setActionTag(DICTOOL->getIntValue_json(options, "actiontag"));
- widget->setTouchEnabled(DICTOOL->getBooleanValue_json(options, "touchAble"));
- const char* name = DICTOOL->getStringValue_json(options, "name");
- const char* widgetName = name?name:"default";
- widget->setName(widgetName);
- float x = DICTOOL->getFloatValue_json(options, "x");
- float y = DICTOOL->getFloatValue_json(options, "y");
- widget->setPosition(Vec2(x,y));
- bool sx = DICTOOL->checkObjectExist_json(options, "scaleX");
- if (sx)
- {
- widget->setScaleX(DICTOOL->getFloatValue_json(options, "scaleX"));
- }
- bool sy = DICTOOL->checkObjectExist_json(options, "scaleY");
- if (sy)
- {
- widget->setScaleY(DICTOOL->getFloatValue_json(options, "scaleY"));
- }
- bool rt = DICTOOL->checkObjectExist_json(options, "rotation");
- if (rt)
- {
- widget->setRotation(DICTOOL->getFloatValue_json(options, "rotation"));
- }
- bool vb = DICTOOL->checkObjectExist_json(options, "visible");
- if (vb)
- {
- widget->setVisible(DICTOOL->getBooleanValue_json(options, "visible"));
- }
- int z = DICTOOL->getIntValue_json(options, "ZOrder");
- widget->setLocalZOrder(z);
- }
- void WidgetPropertiesReader0250::setColorPropsForWidgetFromJsonDictionary(Widget *widget, const rapidjson::Value&options)
- {
- bool op = DICTOOL->checkObjectExist_json(options, "opacity");
- if (op)
- {
- widget->setOpacity(DICTOOL->getIntValue_json(options, "opacity"));
- }
- bool cr = DICTOOL->checkObjectExist_json(options, "colorR");
- bool cg = DICTOOL->checkObjectExist_json(options, "colorG");
- bool cb = DICTOOL->checkObjectExist_json(options, "colorB");
- int colorR = cr ? DICTOOL->getIntValue_json(options, "colorR") : 255;
- int colorG = cg ? DICTOOL->getIntValue_json(options, "colorG") : 255;
- int colorB = cb ? DICTOOL->getIntValue_json(options, "colorB") : 255;
- widget->setColor(Color3B(colorR, colorG, colorB));
-
- this->setAnchorPointForWidget(widget, options);
-
- bool flipX = DICTOOL->getBooleanValue_json(options, "flipX");
- bool flipY = DICTOOL->getBooleanValue_json(options, "flipY");
- widget->setFlippedX(flipX);
- widget->setFlippedY(flipY);
- }
- void WidgetPropertiesReader0250::setPropsForButtonFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
- {
- setPropsForWidgetFromJsonDictionary(widget, options);
- cocos2d::ui::Button* button = static_cast<Button*>(widget);
- bool scale9Enable = DICTOOL->getBooleanValue_json(options, "scale9Enable");
- button->setScale9Enabled(scale9Enable);
-
- std::string tp_n = m_strFilePath;
- std::string tp_p = m_strFilePath;
- std::string tp_d = m_strFilePath;
-
- const char* normalFileName = DICTOOL->getStringValue_json(options, "normal");
- const char* pressedFileName = DICTOOL->getStringValue_json(options, "pressed");
- const char* disabledFileName = DICTOOL->getStringValue_json(options, "disabled");
-
- const char* normalFileName_tp = (normalFileName && (strcmp(normalFileName, "") != 0))?tp_n.append(normalFileName).c_str():nullptr;
- const char* pressedFileName_tp = (pressedFileName && (strcmp(pressedFileName, "") != 0))?tp_p.append(pressedFileName).c_str():nullptr;
- const char* disabledFileName_tp = (disabledFileName && (strcmp(disabledFileName, "") != 0))?tp_d.append(disabledFileName).c_str():nullptr;
- bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
- if (scale9Enable)
- {
- float cx = DICTOOL->getFloatValue_json(options, "capInsetsX");
- float cy = DICTOOL->getFloatValue_json(options, "capInsetsY");
- float cw = DICTOOL->getFloatValue_json(options, "capInsetsWidth");
- float ch = DICTOOL->getFloatValue_json(options, "capInsetsHeight");
-
- if (useMergedTexture)
- {
- button->loadTextures(normalFileName, pressedFileName, disabledFileName,Widget::TextureResType::PLIST);
- }
- else
- {
- button->loadTextures(normalFileName_tp, pressedFileName_tp, disabledFileName_tp);
- }
- button->setCapInsets(Rect(cx, cy, cw, ch));
- bool sw = DICTOOL->checkObjectExist_json(options, "scale9Width");
- bool sh = DICTOOL->checkObjectExist_json(options, "scale9Height");
- if (sw && sh)
- {
- float swf = DICTOOL->getFloatValue_json(options, "scale9Width");
- float shf = DICTOOL->getFloatValue_json(options, "scale9Height");
- button->setContentSize(Size(swf, shf));
- }
- }
- else
- {
- if (useMergedTexture)
- {
- button->loadTextures(normalFileName, pressedFileName, disabledFileName,Widget::TextureResType::PLIST);
- }
- else
- {
- button->loadTextures(normalFileName_tp, pressedFileName_tp, disabledFileName_tp);
- }
- }
- bool tt = DICTOOL->checkObjectExist_json(options, "text");
- if (tt)
- {
- const char* text = DICTOOL->getStringValue_json(options, "text");
- if (text)
- {
- button->setTitleText(text);
- }
- }
- bool cr = DICTOOL->checkObjectExist_json(options, "textColorR");
- bool cg = DICTOOL->checkObjectExist_json(options, "textColorG");
- bool cb = DICTOOL->checkObjectExist_json(options, "textColorB");
- int cri = cr?DICTOOL->getIntValue_json(options, "textColorR"):255;
- int cgi = cg?DICTOOL->getIntValue_json(options, "textColorG"):255;
- int cbi = cb?DICTOOL->getIntValue_json(options, "textColorB"):255;
- button->setTitleColor(Color3B(cri,cgi,cbi));
- bool fs = DICTOOL->checkObjectExist_json(options, "fontSize");
- if (fs)
- {
- button->setTitleFontSize(DICTOOL->getIntValue_json(options, "fontSize"));
- }
- bool fn = DICTOOL->checkObjectExist_json(options, "fontName");
- if (fn)
- {
- const char * szTemp = DICTOOL->getStringValue_json(options, "fontName");
- if (szTemp && *szTemp)
- button->setTitleFontName(szTemp);
- else
- button->setTitleFontName(std::string(""));
- }
- setColorPropsForWidgetFromJsonDictionary(widget,options);
- }
- void WidgetPropertiesReader0250::setPropsForCheckBoxFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
- {
- setPropsForWidgetFromJsonDictionary(widget, options);
- CheckBox* checkBox = static_cast<CheckBox*>(widget);
- const char* backGroundFileName = DICTOOL->getStringValue_json(options, "backGroundBox");
- const char* backGroundSelectedFileName = DICTOOL->getStringValue_json(options, "backGroundBoxSelected");
- const char* frontCrossFileName = DICTOOL->getStringValue_json(options, "frontCross");
- const char* backGroundDisabledFileName = DICTOOL->getStringValue_json(options, "backGroundBoxDisabled");
- const char* frontCrossDisabledFileName = DICTOOL->getStringValue_json(options, "frontCrossDisabled");
-
- std::string tp_b = m_strFilePath;
- std::string tp_bs = m_strFilePath;
- std::string tp_c = m_strFilePath;
- std::string tp_bd = m_strFilePath;
- std::string tp_cd = m_strFilePath;
-
- const char* backGroundFileName_tp = (backGroundFileName && (strcmp(backGroundFileName, "") != 0))?tp_b.append(backGroundFileName).c_str():nullptr;
- const char* backGroundSelectedFileName_tp = (backGroundSelectedFileName && (strcmp(backGroundSelectedFileName, "") != 0))?tp_bs.append(backGroundSelectedFileName).c_str():nullptr;
- const char* frontCrossFileName_tp = (frontCrossFileName && (strcmp(frontCrossFileName, "") != 0))?tp_c.append(frontCrossFileName).c_str():nullptr;
- const char* backGroundDisabledFileName_tp = (backGroundDisabledFileName && (strcmp(backGroundDisabledFileName, "") != 0))?tp_bd.append(backGroundDisabledFileName).c_str():nullptr;
- const char* frontCrossDisabledFileName_tp = (frontCrossDisabledFileName && (strcmp(frontCrossDisabledFileName, "") != 0))?tp_cd.append(frontCrossDisabledFileName).c_str():nullptr;
- bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
-
- if (useMergedTexture)
- {
- checkBox->loadTextures(backGroundFileName, backGroundSelectedFileName, frontCrossFileName,backGroundDisabledFileName,frontCrossDisabledFileName,Widget::TextureResType::PLIST);
- }
- else
- {
- checkBox->loadTextures(backGroundFileName_tp, backGroundSelectedFileName_tp, frontCrossFileName_tp,backGroundDisabledFileName_tp,frontCrossDisabledFileName_tp);
- }
- checkBox->setSelected(DICTOOL->getBooleanValue_json(options, "selectedState"));
- setColorPropsForWidgetFromJsonDictionary(widget,options);
- }
- void WidgetPropertiesReader0250::setPropsForImageViewFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
- {
- setPropsForWidgetFromJsonDictionary(widget, options);
- cocos2d::ui::ImageView* imageView = static_cast<ImageView*>(widget);
- const char* imageFileName = DICTOOL->getStringValue_json(options, "fileName");
- bool scale9EnableExist = DICTOOL->checkObjectExist_json(options, "scale9Enable");
- bool scale9Enable = false;
- if (scale9EnableExist)
- {
- scale9Enable = DICTOOL->getBooleanValue_json(options, "scale9Enable");
- }
- imageView->setScale9Enabled(scale9Enable);
-
- std::string tp_i = m_strFilePath;
- const char* imageFileName_tp = nullptr;
- if (imageFileName && (strcmp(imageFileName, "") != 0))
- {
- imageFileName_tp = tp_i.append(imageFileName).c_str();
- }
-
- bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
- if (scale9Enable)
- {
- if (useMergedTexture)
- {
- imageView->loadTexture(imageFileName,Widget::TextureResType::PLIST);
- }
- else
- {
- imageView->loadTexture(imageFileName_tp);
- }
-
- bool sw = DICTOOL->checkObjectExist_json(options, "scale9Width");
- bool sh = DICTOOL->checkObjectExist_json(options, "scale9Height");
- if (sw && sh)
- {
- float swf = DICTOOL->getFloatValue_json(options, "scale9Width");
- float shf = DICTOOL->getFloatValue_json(options, "scale9Height");
- imageView->setContentSize(Size(swf, shf));
- }
-
- float cx = DICTOOL->getFloatValue_json(options, "capInsetsX");
- float cy = DICTOOL->getFloatValue_json(options, "capInsetsY");
- float cw = DICTOOL->getFloatValue_json(options, "capInsetsWidth");
- float ch = DICTOOL->getFloatValue_json(options, "capInsetsHeight");
- imageView->setCapInsets(Rect(cx, cy, cw, ch));
-
- }
- else
- {
- if (useMergedTexture)
- {
- imageView->loadTexture(imageFileName,Widget::TextureResType::PLIST);
- }
- else
- {
- imageView->loadTexture(imageFileName_tp);
- }
- }
- setColorPropsForWidgetFromJsonDictionary(widget,options);
- }
- void WidgetPropertiesReader0250::setPropsForLabelFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
- {
- setPropsForWidgetFromJsonDictionary(widget, options);
- cocos2d::ui::Text* label = static_cast<cocos2d::ui::Text*>(widget);
- bool touchScaleChangeAble = DICTOOL->getBooleanValue_json(options, "touchScaleEnable");
- label->setTouchScaleChangeEnabled(touchScaleChangeAble);
- const char* text = DICTOOL->getStringValue_json(options, "text");
- label->setString(text);
- bool fs = DICTOOL->checkObjectExist_json(options, "fontSize");
- if (fs)
- {
- label->setFontSize(DICTOOL->getIntValue_json(options, "fontSize"));
- }
- bool fn = DICTOOL->checkObjectExist_json(options, "fontName");
- if (fn)
- {
- const char * szTemp = DICTOOL->getStringValue_json(options, "fontName");
- if (szTemp && *szTemp)
- label->setFontName(szTemp);
- else
- label->setFontName(std::string(""));
- }
- bool aw = DICTOOL->checkObjectExist_json(options, "areaWidth");
- bool ah = DICTOOL->checkObjectExist_json(options, "areaHeight");
- if (aw && ah)
- {
- Size size = Size(DICTOOL->getFloatValue_json(options, "areaWidth"),DICTOOL->getFloatValue_json(options,"areaHeight"));
- label->setTextAreaSize(size);
- }
- bool ha = DICTOOL->checkObjectExist_json(options, "hAlignment");
- if (ha)
- {
- label->setTextHorizontalAlignment((TextHAlignment)DICTOOL->getIntValue_json(options, "hAlignment"));
- }
- bool va = DICTOOL->checkObjectExist_json(options, "vAlignment");
- if (va)
- {
- label->setTextVerticalAlignment((TextVAlignment)DICTOOL->getIntValue_json(options, "vAlignment"));
- }
- setColorPropsForWidgetFromJsonDictionary(widget,options);
- }
- void WidgetPropertiesReader0250::setPropsForLabelAtlasFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
- {
- setPropsForWidgetFromJsonDictionary(widget, options);
- cocos2d::ui::TextAtlas* labelAtlas = static_cast<cocos2d::ui::TextAtlas*>(widget);
- bool sv = DICTOOL->checkObjectExist_json(options, "stringValue");
- bool cmf = DICTOOL->checkObjectExist_json(options, "charMapFile");
- bool iw = DICTOOL->checkObjectExist_json(options, "itemWidth");
- bool ih = DICTOOL->checkObjectExist_json(options, "itemHeight");
- bool scm = DICTOOL->checkObjectExist_json(options, "startCharMap");
- if (sv && cmf && iw && ih && scm && (strcmp(DICTOOL->getStringValue_json(options, "charMapFile"), "") != 0))
- {
- std::string tp_c = m_strFilePath;
- const char* cmft = DICTOOL->getStringValue_json(options, "charMapFile");
- const char* cmf_tp = tp_c.append(cmft).c_str();
-
- labelAtlas->setProperty(DICTOOL->getStringValue_json(options, "stringValue"),cmf_tp,DICTOOL->getIntValue_json(options, "itemWidth"),DICTOOL->getIntValue_json(options,"itemHeight"),DICTOOL->getStringValue_json(options, "startCharMap"));
- labelAtlas->setProperty(DICTOOL->getStringValue_json(options, "stringValue"),cmf_tp,DICTOOL->getIntValue_json(options, "itemWidth") / CC_CONTENT_SCALE_FACTOR() ,DICTOOL->getIntValue_json(options,"itemHeight") / CC_CONTENT_SCALE_FACTOR(), DICTOOL->getStringValue_json(options, "startCharMap"));
- }
- setColorPropsForWidgetFromJsonDictionary(widget,options);
- }
- void WidgetPropertiesReader0250::setPropsForLayoutFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
- {
- setPropsForWidgetFromJsonDictionary(widget, options);
- Layout* containerWidget = static_cast<Layout*>(widget);
- if (!dynamic_cast<cocos2d::ui::ScrollView*>(containerWidget)
- && !dynamic_cast<cocos2d::ui::ListView*>(containerWidget))
- {
- containerWidget->setClippingEnabled(DICTOOL->getBooleanValue_json(options, "clipAble"));
- }
- Layout* panel = (Layout*)widget;
- bool backGroundScale9Enable = DICTOOL->getBooleanValue_json(options, "backGroundScale9Enable");
- panel->setBackGroundImageScale9Enabled(backGroundScale9Enable);
- int cr = DICTOOL->getIntValue_json(options, "bgColorR");
- int cg = DICTOOL->getIntValue_json(options, "bgColorG");
- int cb = DICTOOL->getIntValue_json(options, "bgColorB");
-
- int scr = DICTOOL->getIntValue_json(options, "bgStartColorR");
- int scg = DICTOOL->getIntValue_json(options, "bgStartColorG");
- int scb = DICTOOL->getIntValue_json(options, "bgStartColorB");
-
- int ecr = DICTOOL->getIntValue_json(options, "bgEndColorR");
- int ecg = DICTOOL->getIntValue_json(options, "bgEndColorG");
- int ecb = DICTOOL->getIntValue_json(options, "bgEndColorB");
-
- float bgcv1 = DICTOOL->getFloatValue_json(options, "vectorX");
- float bgcv2 = DICTOOL->getFloatValue_json(options, "vectorY");
- panel->setBackGroundColorVector(Vec2(bgcv1, bgcv2));
-
- int co = DICTOOL->getIntValue_json(options, "bgColorOpacity");
-
- int colorType = DICTOOL->getIntValue_json(options, "colorType");
- panel->setBackGroundColorType(Layout::BackGroundColorType(colorType));
- panel->setBackGroundColor(Color3B(scr, scg, scb),Color3B(ecr, ecg, ecb));
- panel->setBackGroundColor(Color3B(cr, cg, cb));
- panel->setBackGroundColorOpacity(co);
-
- std::string tp_b = m_strFilePath;
- const char* imageFileName = DICTOOL->getStringValue_json(options, "backGroundImage");
- const char* imageFileName_tp = (imageFileName && (strcmp(imageFileName, "") != 0))?tp_b.append(imageFileName).c_str():nullptr;
- bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
- if (backGroundScale9Enable)
- {
- float cx = DICTOOL->getFloatValue_json(options, "capInsetsX");
- float cy = DICTOOL->getFloatValue_json(options, "capInsetsY");
- float cw = DICTOOL->getFloatValue_json(options, "capInsetsWidth");
- float ch = DICTOOL->getFloatValue_json(options, "capInsetsHeight");
- if (useMergedTexture)
- {
- panel->setBackGroundImage(imageFileName,Widget::TextureResType::PLIST);
- }
- else
- {
- panel->setBackGroundImage(imageFileName_tp);
- }
- panel->setBackGroundImageCapInsets(Rect(cx, cy, cw, ch));
- }
- else
- {
-
- if (useMergedTexture)
- {
- panel->setBackGroundImage(imageFileName,Widget::TextureResType::PLIST);
- }
- else
- {
- panel->setBackGroundImage(imageFileName_tp);
- }
- }
- setColorPropsForWidgetFromJsonDictionary(widget,options);
- }
- void WidgetPropertiesReader0250::setPropsForScrollViewFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
- {
- setPropsForLayoutFromJsonDictionary(widget, options);
- cocos2d::ui::ScrollView* scrollView = static_cast<cocos2d::ui::ScrollView*>(widget);
- float innerWidth = DICTOOL->getFloatValue_json(options, "innerWidth");
- float innerHeight = DICTOOL->getFloatValue_json(options, "innerHeight");
- scrollView->setInnerContainerSize(Size(innerWidth, innerHeight));
- int direction = DICTOOL->getFloatValue_json(options, "direction");
- scrollView->setDirection((ScrollView::Direction)direction);
- scrollView->setBounceEnabled(DICTOOL->getBooleanValue_json(options, "bounceEnable"));
- setColorPropsForWidgetFromJsonDictionary(widget,options);
- }
- void WidgetPropertiesReader0250::setPropsForSliderFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
- {
- setPropsForWidgetFromJsonDictionary(widget, options);
- cocos2d::ui::Slider* slider = static_cast<cocos2d::ui::Slider*>(widget);
-
- bool barTextureScale9Enable = DICTOOL->getBooleanValue_json(options, "barTextureScale9Enable");
- slider->setScale9Enabled(barTextureScale9Enable);
- bool bt = DICTOOL->checkObjectExist_json(options, "barFileName");
- float barLength = DICTOOL->getFloatValue_json(options, "length");
- bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
- if (bt)
- {
- if (barTextureScale9Enable)
- {
- std::string tp_b = m_strFilePath;
- const char* imageFileName = DICTOOL->getStringValue_json(options, "barFileName");
- const char* imageFileName_tp = (imageFileName && (strcmp(imageFileName, "") != 0))?tp_b.append(imageFileName).c_str():nullptr;
- if (useMergedTexture)
- {
- slider->loadBarTexture(imageFileName,Widget::TextureResType::PLIST);
- }
- else
- {
- slider->loadBarTexture(imageFileName_tp);
- }
- slider->setContentSize(Size(barLength, slider->getContentSize().height));
- }
- else
- {
- std::string tp_b = m_strFilePath;
- const char* imageFileName = DICTOOL->getStringValue_json(options, "barFileName");
- const char* imageFileName_tp = (imageFileName && (strcmp(imageFileName, "") != 0))?tp_b.append(imageFileName).c_str():nullptr;
- if (useMergedTexture)
- {
- slider->loadBarTexture(imageFileName,Widget::TextureResType::PLIST);
- }
- else
- {
- slider->loadBarTexture(imageFileName_tp);
- }
- }
- }
- std::string tp_n = m_strFilePath;
- std::string tp_p = m_strFilePath;
- std::string tp_d = m_strFilePath;
-
- const char* normalFileName = DICTOOL->getStringValue_json(options, "ballNormal");
- const char* pressedFileName = DICTOOL->getStringValue_json(options, "ballPressed");
- const char* disabledFileName = DICTOOL->getStringValue_json(options, "ballDisabled");
-
- const char* normalFileName_tp = (normalFileName && (strcmp(normalFileName, "") != 0))?tp_n.append(normalFileName).c_str():nullptr;
- const char* pressedFileName_tp = (pressedFileName && (strcmp(pressedFileName, "") != 0))?tp_p.append(pressedFileName).c_str():nullptr;
- const char* disabledFileName_tp = (disabledFileName && (strcmp(disabledFileName, "") != 0))?tp_d.append(disabledFileName).c_str():nullptr;
- if (useMergedTexture)
- {
- slider->loadSlidBallTextures(normalFileName,pressedFileName,disabledFileName,Widget::TextureResType::PLIST);
- }
- else
- {
- slider->loadSlidBallTextures(normalFileName_tp,pressedFileName_tp,disabledFileName_tp);
- }
- slider->setPercent(DICTOOL->getIntValue_json(options, "percent"));
-
- std::string tp_b = m_strFilePath;
- const char* imageFileName = DICTOOL->getStringValue_json(options, "progressBarFileName");
- const char* imageFileName_tp = (imageFileName && (strcmp(imageFileName, "") != 0))?tp_b.append(imageFileName).c_str():nullptr;
- if (useMergedTexture)
- {
- slider->loadProgressBarTexture(imageFileName, Widget::TextureResType::PLIST);
- }
- else
- {
- slider->loadProgressBarTexture(imageFileName_tp);
- }
- setColorPropsForWidgetFromJsonDictionary(widget,options);
- }
- void WidgetPropertiesReader0250::setPropsForTextFieldFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
- {
- setPropsForWidgetFromJsonDictionary(widget, options);
- cocos2d::ui::TextField* textField = static_cast<cocos2d::ui::TextField*>(widget);
- bool ph = DICTOOL->checkObjectExist_json(options, "placeHolder");
- if (ph)
- {
- textField->setPlaceHolder(DICTOOL->getStringValue_json(options, "placeHolder"));
- }
- textField->setString(DICTOOL->getStringValue_json(options, "text"));
- bool fs = DICTOOL->checkObjectExist_json(options, "fontSize");
- if (fs)
- {
- textField->setFontSize(DICTOOL->getIntValue_json(options, "fontSize"));
- }
- bool fn = DICTOOL->checkObjectExist_json(options, "fontName");
- if (fn)
- {
- const char * szTemp = DICTOOL->getStringValue_json(options, "fontName");
- if (szTemp && *szTemp)
- textField->setFontName(szTemp);
- else
- textField->setFontName(std::string(""));
- }
- bool tsw = DICTOOL->checkObjectExist_json(options, "touchSizeWidth");
- bool tsh = DICTOOL->checkObjectExist_json(options, "touchSizeHeight");
- if (tsw && tsh)
- {
- textField->setTouchSize(Size(DICTOOL->getFloatValue_json(options, "touchSizeWidth"), DICTOOL->getFloatValue_json(options,"touchSizeHeight")));
- }
-
- float dw = DICTOOL->getFloatValue_json(options, "width");
- float dh = DICTOOL->getFloatValue_json(options, "height");
- if (dw > 0.0f || dh > 0.0f)
- {
- //textField->setSize(Size(dw, dh));
- }
- bool maxLengthEnable = DICTOOL->getBooleanValue_json(options, "maxLengthEnable");
- textField->setMaxLengthEnabled(maxLengthEnable);
-
- if (maxLengthEnable)
- {
- int maxLength = DICTOOL->getIntValue_json(options, "maxLength");
- textField->setMaxLength(maxLength);
- }
- bool passwordEnable = DICTOOL->getBooleanValue_json(options, "passwordEnable");
- textField->setPasswordEnabled(passwordEnable);
- if (passwordEnable)
- {
- textField->setPasswordStyleText(DICTOOL->getStringValue_json(options, "passwordStyleText"));
- }
- setColorPropsForWidgetFromJsonDictionary(widget,options);
- }
- void WidgetPropertiesReader0250::setPropsForLoadingBarFromJsonDictionary(Widget *widget, const rapidjson::Value&options)
- {
- setPropsForWidgetFromJsonDictionary(widget, options);
- cocos2d::ui::LoadingBar* loadingBar = static_cast<cocos2d::ui::LoadingBar*>(widget);
- bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
- std::string tp_b = m_strFilePath;
- const char*imageFileName = DICTOOL->getStringValue_json(options, "texture");
- const char* imageFileName_tp = (imageFileName && (strcmp(imageFileName, "") != 0))?tp_b.append(imageFileName).c_str():nullptr;
- if (useMergedTexture)
- {
- loadingBar->loadTexture(imageFileName,Widget::TextureResType::PLIST);
- }
- else
- {
- loadingBar->loadTexture(imageFileName_tp);
- }
- loadingBar->setDirection(LoadingBar::Direction(DICTOOL->getIntValue_json(options, "direction")));
- loadingBar->setPercent(DICTOOL->getIntValue_json(options, "percent"));
- setColorPropsForWidgetFromJsonDictionary(widget,options);
- }
- void WidgetPropertiesReader0250::setPropsForLabelBMFontFromJsonDictionary(Widget *widget, const rapidjson::Value&options)
- {
-
- setPropsForWidgetFromJsonDictionary(widget, options);
-
- cocos2d::ui::TextBMFont* labelBMFont = static_cast<cocos2d::ui::TextBMFont*>(widget);
-
- std::string tp_c = m_strFilePath;
- const char* cmft = DICTOOL->getStringValue_json(options, "fileName");
- const char* cmf_tp = tp_c.append(cmft).c_str();
-
- labelBMFont->setFntFile(cmf_tp);
-
- const char* text = DICTOOL->getStringValue_json(options, "text");
- labelBMFont->setString(text);
-
- setColorPropsForWidgetFromJsonDictionary(widget,options);
- }
-
- void WidgetPropertiesReader0250::setPropsForAllWidgetFromJsonDictionary(WidgetReaderProtocol* /*reader*/, Widget* /*widget*/, const rapidjson::Value& /*options*/)
- {
-
- }
- void WidgetPropertiesReader0250::setPropsForAllCustomWidgetFromJsonDictionary(const std::string& /*classType*/,
- cocos2d::ui::Widget* /*widget*/,
- const rapidjson::Value& /*customOptions*/)
- {
-
- }
- Widget* WidgetPropertiesReader0250::createWidgetFromBinary(CocoLoader* /*cocoLoader*/,
- stExpCocoNode* /*pCocoNode*/,
- const char* /*fileName*/)
- {
- return nullptr;
- }
- Widget* WidgetPropertiesReader0250::widgetFromBinary(CocoLoader* /*cocoLoader*/,
- stExpCocoNode* /*pCocoNode*/)
- {
- return nullptr;
- }
- void WidgetPropertiesReader0250::setPropsForAllWidgetFromBinary(WidgetReaderProtocol* /*reader*/,
- cocos2d::ui::Widget* /*widget*/,
- CocoLoader* /*cocoLoader*/,
- stExpCocoNode* /*pCocoNode*/)
- {}
- /*0.3.0.0~1.0.0.0*/
- Widget* WidgetPropertiesReader0300::createWidget(const rapidjson::Value& data, const char* fullPath, const char* fileName)
- {
- m_strFilePath = fullPath;
- int texturesCount = DICTOOL->getArrayCount_json(data, "textures");
-
- for (int i=0; i<texturesCount; i++)
- {
- const char* file = DICTOOL->getStringValueFromArray_json(data, "textures", i);
- std::string tp = fullPath;
- tp.append(file);
- SpriteFrameCache::getInstance()->addSpriteFramesWithFile(tp);
- }
- float fileDesignWidth = DICTOOL->getFloatValue_json(data, "designWidth");
- float fileDesignHeight = DICTOOL->getFloatValue_json(data, "designHeight");
- if (fileDesignWidth <= 0 || fileDesignHeight <= 0) {
- CCLOGERROR("Read design size error!\n");
- Size winSize = Director::getInstance()->getWinSize();
- GUIReader::getInstance()->storeFileDesignSize(fileName, winSize);
- }
- else
- {
- GUIReader::getInstance()->storeFileDesignSize(fileName, Size(fileDesignWidth, fileDesignHeight));
- }
- const rapidjson::Value& widgetTree = DICTOOL->getSubDictionary_json(data, "widgetTree");
- Widget* widget = widgetFromJsonDictionary(widgetTree);
-
- /* *********temp********* */
- if (widget->getContentSize().equals(Size::ZERO))
- {
- Layout* rootWidget = dynamic_cast<Layout*>(widget);
- rootWidget->setContentSize(Size(fileDesignWidth, fileDesignHeight));
- }
- /* ********************** */
-
- // widget->setFileDesignSize(Size(fileDesignWidth, fileDesignHeight));
- const rapidjson::Value& actions = DICTOOL->getSubDictionary_json(data, "animation");
- /* *********temp********* */
- // ActionManager::getInstance()->releaseActions();
- /* ********************** */
- // CCLOG("file name == [%s]",fileName);
- Ref* rootWidget = (Ref*) widget;
- ActionManagerEx::getInstance()->initWithDictionary(fileName,actions,rootWidget);
- return widget;
- }
-
- cocos2d::ui::Widget* WidgetPropertiesReader0300::createWidgetFromBinary(CocoLoader* cocoLoader,stExpCocoNode* cocoNode, const char* fileName)
- {
-
- stExpCocoNode *tpChildArray = cocoNode->GetChildArray(cocoLoader);
- float fileDesignWidth = 0.0f;
- float fileDesignHeight = 0.0f;
-
- Widget* widget = nullptr;
- for (int i = 0; i < cocoNode->GetChildNum(); ++i) {
- std::string key = tpChildArray[i].GetName(cocoLoader);
-
- if (key == "textures") {
- int texturesCount = tpChildArray[i].GetChildNum();
- for (int j=0; j<texturesCount; j++)
- {
- std::string file;
- stExpCocoNode *textureCountsArray = tpChildArray[i].GetChildArray(cocoLoader);
- file = textureCountsArray[j].GetValue(cocoLoader);
- SpriteFrameCache::getInstance()->addSpriteFramesWithFile(file);
- }
- }else if (key == "designWidth"){
- fileDesignWidth = utils::atof(tpChildArray[i].GetValue(cocoLoader));
- }else if (key == "designHeight"){
- fileDesignHeight = utils::atof(tpChildArray[i].GetValue(cocoLoader));
- }else if (key == "widgetTree"){
-
- if (fileDesignWidth <= 0 || fileDesignHeight <= 0) {
- CCLOGERROR("Read design size error!\n");
- Size winSize = Director::getInstance()->getWinSize();
- GUIReader::getInstance()->storeFileDesignSize(fileName, winSize);
- }
- else
- {
- GUIReader::getInstance()->storeFileDesignSize(fileName, Size(fileDesignWidth, fileDesignHeight));
- }
-
-
- stExpCocoNode *widgetTreeNode = &tpChildArray[i];
- rapidjson::Type tType = tpChildArray[i].GetType(cocoLoader);
-
- if (rapidjson::kObjectType == tType)
- {
- widget = widgetFromBinary(cocoLoader, widgetTreeNode);
- }
-
- if (widget->getContentSize().equals(Size::ZERO))
- {
- Layout* rootWidget = dynamic_cast<Layout*>(widget);
- rootWidget->setContentSize(Size(fileDesignWidth, fileDesignHeight));
- }
- }
- }
-
- /* ********************** */
- /* ********************** */
- stExpCocoNode *optionChildNode = cocoNode->GetChildArray(cocoLoader);
- for (int k = 0; k < cocoNode->GetChildNum(); ++k) {
- std::string key = optionChildNode[k].GetName(cocoLoader);
- if (key == "animation") {
- Ref* rootWidget = (Ref*) widget;
- ActionManagerEx::getInstance()->initWithBinary(fileName,rootWidget,cocoLoader, &optionChildNode[k]);
- break;
- }
- }
- return widget;
- }
- Widget* WidgetPropertiesReader0300::widgetFromBinary(CocoLoader* cocoLoader, stExpCocoNode* cocoNode)
- {
- Widget* widget = nullptr;
- stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
- stExpCocoNode *optionsNode = nullptr;
- stExpCocoNode *childrenNode = nullptr;
- int elementCount = cocoNode->GetChildNum();
- std::string classname;
-
- for (int i = 0; i < elementCount; ++i) {
- std::string key = stChildArray[i].GetName(cocoLoader);
- std::string value = stChildArray[i].GetValue(cocoLoader);
-
- if (key == "classname" )
- {
- if (!value.empty())
- {
- classname = value;
- widget = this->createGUI(classname);
- }
- else
- {
- CCLOG("Warning!!! classname not found!");
- }
- }else if(key == "children"){
- childrenNode = &stChildArray[i];
- }else if(key == "options"){
- optionsNode = &stChildArray[i];
- }
- }
-
- std::string readerName = this->getWidgetReaderClassName(classname);
-
- WidgetReaderProtocol* reader = this->createWidgetReaderProtocol(readerName);
-
- if (reader)
- {
- // widget parse with widget reader
- setPropsForAllWidgetFromBinary(reader, widget, cocoLoader,optionsNode);
- }
- else
- {
- // 1st., custom widget parse properties of parent widget with parent widget reader
- readerName = this->getWidgetReaderClassName(widget);
- reader = this->createWidgetReaderProtocol(readerName);
-
-
- if (reader && widget) {
- setPropsForAllWidgetFromBinary(reader, widget, cocoLoader, optionsNode);
- // 2nd., custom widget parse with custom reader
- //2nd. parse custom property
- const char* customProperty = nullptr;
- stExpCocoNode *optionChildNode = optionsNode->GetChildArray(cocoLoader);
- for (int k = 0; k < optionsNode->GetChildNum(); ++k) {
- std::string key = optionChildNode[k].GetName(cocoLoader);
- if (key == "customProperty") {
- customProperty = optionChildNode[k].GetValue(cocoLoader);
- break;
- }
- }
-
- rapidjson::Document customJsonDict;
- customJsonDict.Parse<0>(customProperty);
- if (customJsonDict.HasParseError())
- {
- CCLOG("GetParseError %d\n", customJsonDict.GetParseError());
- }
- setPropsForAllCustomWidgetFromJsonDictionary(classname, widget, customJsonDict);
- }else{
- CCLOG("Widget or WidgetReader doesn't exists!!! Please check your csb file.");
- }
-
- }
-
- //parse children
- if (nullptr != childrenNode) {
- rapidjson::Type tType22 = childrenNode->GetType(cocoLoader);
- if (tType22 == rapidjson::kArrayType) {
-
- int childrenCount = childrenNode->GetChildNum();
- stExpCocoNode* innerChildArray = childrenNode->GetChildArray(cocoLoader);
- for (int i=0; i < childrenCount; ++i) {
- rapidjson::Type tType = innerChildArray[i].GetType(cocoLoader);
-
- if (tType == rapidjson::kObjectType) {
-
- Widget *child = widgetFromBinary(cocoLoader, &innerChildArray[i]);
-
- if (child)
- {
- PageView* pageView = dynamic_cast<PageView*>(widget);
- if (pageView)
- {
- pageView->addPage(static_cast<Layout*>(child));
- }
- else
- {
- ListView* listView = dynamic_cast<ListView*>(widget);
- if (listView)
- {
- listView->pushBackCustomItem(child);
- }
- else
- {
- if (nullptr == dynamic_cast<Layout*>(widget))
- {
- if (child->getPositionType() == ui::Widget::PositionType::PERCENT)
- {
- child->setPositionPercent(Vec2(child->getPositionPercent().x + widget->getAnchorPoint().x,
- child->getPositionPercent().y + widget->getAnchorPoint().y));
- }
- child->setPosition(Vec2(child->getPositionX() + widget->getAnchorPointInPoints().x,
- child->getPositionY() + widget->getAnchorPointInPoints().y));
- }
- widget->addChild(child);
- }
- }
- }
-
- }
- }
- }
- }
-
- return widget;
- }
-
- void WidgetPropertiesReader0300::setPropsForAllWidgetFromBinary(WidgetReaderProtocol* reader,
- cocos2d::ui::Widget* widget,
- CocoLoader* cocoLoader,
- stExpCocoNode* cocoNode)
- {
- reader->setPropsFromBinary(widget, cocoLoader, cocoNode);
- }
-
- void WidgetPropertiesReader0300::setPropsForAllCustomWidgetFromBinary(const std::string& /*classType*/,
- cocos2d::ui::Widget* /*widget*/,
- CocoLoader* /*cocoLoader*/,
- stExpCocoNode* /*pCocoNode*/) {
- //TODO: custom property
- }
-
- Widget* WidgetPropertiesReader0300::widgetFromJsonDictionary(const rapidjson::Value& data)
- {
- const char* classname = DICTOOL->getStringValue_json(data, "classname");
- const rapidjson::Value& uiOptions = DICTOOL->getSubDictionary_json(data, "options");
- Widget* widget = this->createGUI(classname);
- // CCLOG("classname = %s", classname);
- std::string readerName = this->getWidgetReaderClassName(classname);
-
- WidgetReaderProtocol* reader = this->createWidgetReaderProtocol(readerName);
-
- if (reader)
- {
- // widget parse with widget reader
- setPropsForAllWidgetFromJsonDictionary(reader, widget, uiOptions);
- }
- else
- {
- readerName = this->getWidgetReaderClassName(widget);
-
- reader = dynamic_cast<WidgetReaderProtocol*>(ObjectFactory::getInstance()->createObject(readerName));
-
- if (reader && widget) {
- setPropsForAllWidgetFromJsonDictionary(reader, widget, uiOptions);
-
- // 2nd., custom widget parse with custom reader
- const char* customProperty = DICTOOL->getStringValue_json(uiOptions, "customProperty");
- rapidjson::Document customJsonDict;
- customJsonDict.Parse<0>(customProperty);
- if (customJsonDict.HasParseError())
- {
- CCLOG("GetParseError %d\n", customJsonDict.GetParseError());
- }
- setPropsForAllCustomWidgetFromJsonDictionary(classname, widget, customJsonDict);
- }else{
- CCLOG("Widget or WidgetReader doesn't exists!!! Please check your json file.");
- }
-
- }
-
- int childrenCount = DICTOOL->getArrayCount_json(data, "children");
- for (int i = 0; i < childrenCount; i++)
- {
- const rapidjson::Value& subData = DICTOOL->getDictionaryFromArray_json(data, "children", i);
- cocos2d::ui::Widget* child = widgetFromJsonDictionary(subData);
- if (child)
- {
- PageView* pageView = dynamic_cast<PageView*>(widget);
- if (pageView)
- {
- pageView->addPage(static_cast<Layout*>(child));
- }
- else
- {
- ListView* listView = dynamic_cast<ListView*>(widget);
- if (listView)
- {
- listView->pushBackCustomItem(child);
- }
- else
- {
- if (nullptr == dynamic_cast<Layout*>(widget))
- {
- if (child->getPositionType() == ui::Widget::PositionType::PERCENT)
- {
- child->setPositionPercent(Vec2(child->getPositionPercent().x + widget->getAnchorPoint().x, child->getPositionPercent().y + widget->getAnchorPoint().y));
- }
- child->setPosition(Vec2(child->getPositionX() + widget->getAnchorPointInPoints().x, child->getPositionY() + widget->getAnchorPointInPoints().y));
- }
- widget->addChild(child);
- }
- }
- }
- }
- return widget;
- }
-
- void WidgetPropertiesReader0300::setPropsForAllWidgetFromJsonDictionary(WidgetReaderProtocol *reader, Widget *widget, const rapidjson::Value &options)
- {
- reader->setPropsFromJsonDictionary(widget, options);
- }
-
- void WidgetPropertiesReader0300::setPropsForAllCustomWidgetFromJsonDictionary(const std::string &classType,
- cocos2d::ui::Widget *widget,
- const rapidjson::Value &customOptions)
- {
- GUIReader* guiReader = GUIReader::getInstance();
-
- std::map<std::string, Ref*> *object_map = guiReader->getParseObjectMap();
- Ref* object = (*object_map)[classType];
-
- std::map<std::string, SEL_ParseEvent> *selector_map = guiReader->getParseCallBackMap();
- SEL_ParseEvent selector = (*selector_map)[classType];
-
- if (object && selector)
- {
- (object->*selector)(classType, widget, customOptions);
- }
- }
-
- }
|