diff --git a/cocos/ui/UICheckBox.cpp b/cocos/ui/UICheckBox.cpp index d24edfdacb..16c1628600 100644 --- a/cocos/ui/UICheckBox.cpp +++ b/cocos/ui/UICheckBox.cpp @@ -75,6 +75,49 @@ CheckBox* CheckBox::create() CC_SAFE_DELETE(widget); return nullptr; } + +CheckBox* CheckBox::create(const std::string& backGround, + const std::string& backGroundSeleted, + const std::string& cross, + const std::string& backGroundDisabled, + const std::string& frontCrossDisabled, + TextureResType texType) +{ + CheckBox *pWidget = new CheckBox; + if (pWidget && pWidget->init(backGround, + backGroundSeleted, + cross, + backGroundDisabled, + frontCrossDisabled, + texType)) + { + pWidget->autorelease(); + return pWidget; + } + CC_SAFE_DELETE(pWidget); + return nullptr; +} + +bool CheckBox::init(const std::string& backGround, + const std::string& backGroundSeleted, + const std::string& cross, + const std::string& backGroundDisabled, + const std::string& frontCrossDisabled, + TextureResType texType) +{ + bool bRet = true; + do { + if (!Widget::init()) { + bRet = false; + break; + } + + setSelectedState(false); + setTouchEnabled(true); + loadTextures(backGround, backGroundSeleted, cross, backGroundDisabled, frontCrossDisabled,texType); + } while (0); + return bRet; +} bool CheckBox::init() { @@ -102,7 +145,12 @@ void CheckBox::initRenderer() addProtectedChild(_frontCrossDisabledRenderer, FRONTCROSSDISABLED_RENDERER_Z, -1); } -void CheckBox::loadTextures(const char *backGround, const char *backGroundSelected, const char *cross,const char* backGroundDisabled,const char* frontCrossDisabled,TextureResType texType) +void CheckBox::loadTextures(const std::string& backGround, + const std::string& backGroundSelected, + const std::string& cross, + const std::string& backGroundDisabled, + const std::string& frontCrossDisabled, + TextureResType texType) { loadTextureBackGround(backGround,texType); loadTextureBackGroundSelected(backGroundSelected,texType); @@ -111,9 +159,9 @@ void CheckBox::loadTextures(const char *backGround, const char *backGroundSelect loadTextureFrontCrossDisabled(frontCrossDisabled,texType); } -void CheckBox::loadTextureBackGround(const char *backGround,TextureResType texType) +void CheckBox::loadTextureBackGround(const std::string& backGround,TextureResType texType) { - if (!backGround || strcmp(backGround, "") == 0) + if (backGround.empty()) { return; } @@ -137,9 +185,9 @@ void CheckBox::loadTextureBackGround(const char *backGround,TextureResType texTy updateRGBAToRenderer(_backGroundBoxRenderer); } -void CheckBox::loadTextureBackGroundSelected(const char *backGroundSelected,TextureResType texType) +void CheckBox::loadTextureBackGroundSelected(const std::string& backGroundSelected,TextureResType texType) { - if (!backGroundSelected || strcmp(backGroundSelected, "") == 0) + if (backGroundSelected.empty()) { return; } @@ -163,9 +211,9 @@ void CheckBox::loadTextureBackGroundSelected(const char *backGroundSelected,Text updateRGBAToRenderer(_backGroundSelectedBoxRenderer); } -void CheckBox::loadTextureFrontCross(const char *cross,TextureResType texType) +void CheckBox::loadTextureFrontCross(const std::string& cross,TextureResType texType) { - if (!cross || strcmp(cross, "") == 0) + if (cross.empty()) { return; } @@ -189,9 +237,9 @@ void CheckBox::loadTextureFrontCross(const char *cross,TextureResType texType) updateRGBAToRenderer(_frontCrossRenderer); } -void CheckBox::loadTextureBackGroundDisabled(const char *backGroundDisabled,TextureResType texType) +void CheckBox::loadTextureBackGroundDisabled(const std::string& backGroundDisabled,TextureResType texType) { - if (!backGroundDisabled || strcmp(backGroundDisabled, "") == 0) + if (backGroundDisabled.empty()) { return; } @@ -215,9 +263,9 @@ void CheckBox::loadTextureBackGroundDisabled(const char *backGroundDisabled,Text updateRGBAToRenderer(_backGroundBoxDisabledRenderer); } -void CheckBox::loadTextureFrontCrossDisabled(const char *frontCrossDisabled,TextureResType texType) +void CheckBox::loadTextureFrontCrossDisabled(const std::string& frontCrossDisabled,TextureResType texType) { - if (!frontCrossDisabled || strcmp(frontCrossDisabled, "") == 0) + if (frontCrossDisabled.empty()) { return; } diff --git a/cocos/ui/UICheckBox.h b/cocos/ui/UICheckBox.h index 86aef77e94..ab704a547c 100644 --- a/cocos/ui/UICheckBox.h +++ b/cocos/ui/UICheckBox.h @@ -64,6 +64,26 @@ public: * Allocates and initializes. */ static CheckBox* create(); + + /** + * create an checkbox + * + * @param backGround backGround texture. + * + * @param backGroundSelected backGround selected state texture. + * + * @param cross cross texture. + * + * @param frontCrossDisabled cross dark state texture. + * + * @param texType @see UI_TEX_TYPE_LOCAL + */ + static CheckBox* create(const std::string& backGround, + const std::string& backGroundSeleted, + const std::string& cross, + const std::string& backGroundDisabled, + const std::string& frontCrossDisabled, + TextureResType texType = UI_TEX_TYPE_LOCAL); /** * Load textures for checkbox. @@ -78,7 +98,12 @@ public: * * @param texType @see UI_TEX_TYPE_LOCAL */ - void loadTextures(const char* backGround,const char* backGroundSelected,const char* cross,const char* backGroundDisabled,const char* frontCrossDisabled,TextureResType texType = UI_TEX_TYPE_LOCAL); + void loadTextures(const std::string& backGround, + const std::string& backGroundSelected, + const std::string& cross, + const std::string& backGroundDisabled, + const std::string& frontCrossDisabled, + TextureResType texType = UI_TEX_TYPE_LOCAL); /** * Load backGround texture for checkbox. @@ -87,7 +112,7 @@ public: * * @param texType @see UI_TEX_TYPE_LOCAL */ - void loadTextureBackGround(const char* backGround,TextureResType type = UI_TEX_TYPE_LOCAL); + void loadTextureBackGround(const std::string& backGround,TextureResType type = UI_TEX_TYPE_LOCAL); /** * Load backGroundSelected texture for checkbox. @@ -96,7 +121,7 @@ public: * * @param texType @see UI_TEX_TYPE_LOCAL */ - void loadTextureBackGroundSelected(const char* backGroundSelected,TextureResType texType = UI_TEX_TYPE_LOCAL); + void loadTextureBackGroundSelected(const std::string& backGroundSelected,TextureResType texType = UI_TEX_TYPE_LOCAL); /** * Load cross texture for checkbox. @@ -105,7 +130,7 @@ public: * * @param texType @see UI_TEX_TYPE_LOCAL */ - void loadTextureFrontCross(const char* cross,TextureResType texType = UI_TEX_TYPE_LOCAL); + void loadTextureFrontCross(const std::string&,TextureResType texType = UI_TEX_TYPE_LOCAL); /** * Load backGroundDisabled texture for checkbox. @@ -114,7 +139,7 @@ public: * * @param texType @see UI_TEX_TYPE_LOCAL */ - void loadTextureBackGroundDisabled(const char* backGroundDisabled,TextureResType texType = UI_TEX_TYPE_LOCAL); + void loadTextureBackGroundDisabled(const std::string& backGroundDisabled,TextureResType texType = UI_TEX_TYPE_LOCAL); /** * Load frontCrossDisabled texture for checkbox. @@ -123,7 +148,7 @@ public: * * @param texType @see UI_TEX_TYPE_LOCAL */ - void loadTextureFrontCrossDisabled(const char* frontCrossDisabled,TextureResType texType = UI_TEX_TYPE_LOCAL); + void loadTextureFrontCrossDisabled(const std::string& frontCrossDisabled,TextureResType texType = UI_TEX_TYPE_LOCAL); /** * Sets selcted state for checkbox. @@ -161,6 +186,12 @@ public: CC_CONSTRUCTOR_ACCESS: virtual bool init() override; + virtual bool init(const std::string& backGround, + const std::string& backGroundSeleted, + const std::string& cross, + const std::string& backGroundDisabled, + const std::string& frontCrossDisabled, + TextureResType texType = UI_TEX_TYPE_LOCAL); protected: virtual void initRenderer() override; diff --git a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest.cpp b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest.cpp index 0b5f68f63a..4e71babee2 100644 --- a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest.cpp +++ b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest.cpp @@ -39,13 +39,17 @@ bool UICheckBoxTest::init() _uiLayer->addChild(alert); // Create the checkbox - CheckBox* checkBox = CheckBox::create(); + CheckBox* checkBox = CheckBox::create("cocosui/check_box_normal.png", + "cocosui/check_box_normal_press.png", + "cocosui/check_box_active.png", + "cocosui/check_box_normal_disable.png", + "cocosui/check_box_active_disable.png"); checkBox->setTouchEnabled(true); - checkBox->loadTextures("cocosui/check_box_normal.png", - "cocosui/check_box_normal_press.png", - "cocosui/check_box_active.png", - "cocosui/check_box_normal_disable.png", - "cocosui/check_box_active_disable.png"); +// checkBox->loadTextures("cocosui/check_box_normal.png", +// "cocosui/check_box_normal_press.png", +// "cocosui/check_box_active.png", +// "cocosui/check_box_normal_disable.png", +// "cocosui/check_box_active_disable.png"); checkBox->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f)); checkBox->addEventListenerCheckBox(this, checkboxselectedeventselector(UICheckBoxTest::selectedEvent));