2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __UIABSTRACTCHECKBUTTON_H__
|
|
|
|
#define __UIABSTRACTCHECKBUTTON_H__
|
|
|
|
|
|
|
|
#include "ui/UIWidget.h"
|
|
|
|
#include "ui/GUIExport.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup ui
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
NS_CC_BEGIN
|
|
|
|
class Sprite;
|
|
|
|
struct CC_DLL ResourceData;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace ui
|
|
|
|
{
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* AbstractCheckButton is a specific type of two-states button that can be either checked or unchecked.
|
|
|
|
*/
|
|
|
|
class CC_GUI_DLL AbstractCheckButton : public Widget
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Load all textures for initializing a check button.
|
|
|
|
*
|
|
|
|
* @param background The background image name.
|
|
|
|
* @param backgroundSelected The background selected image name.
|
|
|
|
* @param cross The cross image name.
|
|
|
|
* @param backgroundDisabled The background disabled state texture.
|
|
|
|
* @param frontCrossDisabled The front cross disabled state image name.
|
|
|
|
* @param texType @see `Widget::TextureResType`
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void loadTextures(std::string_view background,
|
|
|
|
std::string_view backgroundSelected,
|
|
|
|
std::string_view cross,
|
|
|
|
std::string_view backgroundDisabled,
|
|
|
|
std::string_view frontCrossDisabled,
|
2019-11-23 20:27:39 +08:00
|
|
|
TextureResType texType = TextureResType::LOCAL);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Load background texture for check button.
|
|
|
|
*
|
|
|
|
* @param backGround The background image name.
|
|
|
|
* @param type @see `Widget::TextureResType`
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void loadTextureBackGround(std::string_view backGround, TextureResType type = TextureResType::LOCAL);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Load background selected state texture for check button.
|
|
|
|
*
|
|
|
|
* @param backGroundSelected The background selected state image name.
|
|
|
|
* @param texType @see `Widget::TextureResType`
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void loadTextureBackGroundSelected(std::string_view backGroundSelected,
|
2021-12-25 10:04:45 +08:00
|
|
|
TextureResType texType = TextureResType::LOCAL);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Load cross texture for check button.
|
|
|
|
*
|
|
|
|
* @param crossTextureName The cross texture name.
|
|
|
|
* @param texType @see `Widget::TextureResType`
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void loadTextureFrontCross(std::string_view crossTextureName, TextureResType texType = TextureResType::LOCAL);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Load background disabled state texture for checkbox.
|
|
|
|
*
|
|
|
|
* @param backGroundDisabled The background disabled state texture name.
|
|
|
|
*
|
|
|
|
* @param texType @see `Widget::TextureResType`
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void loadTextureBackGroundDisabled(std::string_view backGroundDisabled,
|
2021-12-25 10:04:45 +08:00
|
|
|
TextureResType texType = TextureResType::LOCAL);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Load frontcross disabled texture for checkbox.
|
|
|
|
*
|
|
|
|
* @param frontCrossDisabled The front cross disabled state texture name.
|
|
|
|
* @param texType @see `Widget::TextureResType`
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void loadTextureFrontCrossDisabled(std::string_view frontCrossDisabled,
|
2021-12-25 10:04:45 +08:00
|
|
|
TextureResType texType = TextureResType::LOCAL);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Query whether CheckBox is selected or not.
|
|
|
|
*@return true means "selected", false otherwise.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isSelected() const;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Change CheckBox state.
|
|
|
|
* Set to true will cause the CheckBox's state to "selected", false otherwise.
|
|
|
|
*@param selected Set to true will change CheckBox to selected state, false otherwise.
|
|
|
|
*/
|
|
|
|
void setSelected(bool selected);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// override functions
|
2021-10-23 23:27:14 +08:00
|
|
|
virtual Vec2 getVirtualRendererSize() const override;
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual Node* getVirtualRenderer() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/** When user pressed the CheckBox, the button will zoom to a scale.
|
|
|
|
* The final scale of the CheckBox equals (CheckBox original scale + _zoomScale)
|
|
|
|
* @since v3.3
|
|
|
|
*/
|
|
|
|
void setZoomScale(float scale);
|
|
|
|
/**
|
|
|
|
* @brief Return a zoom scale
|
|
|
|
* @return A zoom scale of Checkbox.
|
|
|
|
* @since v3.3
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
float getZoomScale() const;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* @brief Return the sprite instance of background
|
|
|
|
* @return the sprite instance of background.
|
|
|
|
*/
|
|
|
|
Sprite* getRendererBackground() const { return _backGroundBoxRenderer; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* @brief Return the sprite instance of background when selected
|
|
|
|
* @return the sprite instance of background when selected
|
|
|
|
*/
|
|
|
|
Sprite* getRendererBackgroundSelected() const { return _backGroundSelectedBoxRenderer; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* @brief Return the sprite instance of front cross
|
|
|
|
* @return the sprite instance of front cross
|
|
|
|
*/
|
|
|
|
Sprite* getRendererFrontCross() const { return _frontCrossRenderer; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* @brief Return the sprite instance of background when disabled
|
|
|
|
* @return the sprite instance of background when disabled
|
|
|
|
*/
|
|
|
|
Sprite* getRendererBackgroundDisabled() const { return _backGroundBoxDisabledRenderer; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* @brief Return the sprite instance of front cross when disabled
|
|
|
|
* @return the sprite instance of front cross when disabled
|
|
|
|
*/
|
|
|
|
Sprite* getRendererFrontCrossDisabled() const { return _frontCrossDisabledRenderer; }
|
|
|
|
|
|
|
|
ResourceData getBackNormalFile();
|
|
|
|
ResourceData getBackPressedFile();
|
|
|
|
ResourceData getBackDisabledFile();
|
|
|
|
ResourceData getCrossNormalFile();
|
|
|
|
ResourceData getCrossDisabledFile();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_CONSTRUCTOR_ACCESS : virtual bool init() override;
|
2021-12-26 23:26:34 +08:00
|
|
|
virtual bool init(std::string_view backGround,
|
|
|
|
std::string_view backGroundSelected,
|
|
|
|
std::string_view cross,
|
|
|
|
std::string_view backGroundDisabled,
|
|
|
|
std::string_view frontCrossDisabled,
|
2019-11-23 20:27:39 +08:00
|
|
|
TextureResType texType = TextureResType::LOCAL);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Default constructor.
|
|
|
|
*/
|
|
|
|
AbstractCheckButton();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Default destructor.
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
virtual ~AbstractCheckButton();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual void initRenderer() override;
|
|
|
|
virtual void onPressStateChangedToNormal() override;
|
|
|
|
virtual void onPressStateChangedToPressed() override;
|
|
|
|
virtual void onPressStateChangedToDisabled() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
void setupBackgroundTexture();
|
|
|
|
void loadTextureBackGround(SpriteFrame* spriteFrame);
|
|
|
|
void setupBackgroundSelectedTexture();
|
|
|
|
void loadTextureBackGroundSelected(SpriteFrame* spriteFrame);
|
|
|
|
void setupFrontCrossTexture();
|
|
|
|
void loadTextureFrontCross(SpriteFrame* spriteframe);
|
|
|
|
void setupBackgroundDisable();
|
|
|
|
void loadTextureBackGroundDisabled(SpriteFrame* spriteframe);
|
|
|
|
void setupFrontCrossDisableTexture();
|
|
|
|
void loadTextureFrontCrossDisabled(SpriteFrame* spriteframe);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual void dispatchSelectChangedEvent(bool selected) = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual void onSizeChanged() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
void backGroundTextureScaleChangedWithSize();
|
|
|
|
void backGroundSelectedTextureScaleChangedWithSize();
|
|
|
|
void frontCrossTextureScaleChangedWithSize();
|
|
|
|
void backGroundDisabledTextureScaleChangedWithSize();
|
|
|
|
void frontCrossDisabledTextureScaleChangedWithSize();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual void copySpecialProperties(Widget* model) override;
|
|
|
|
virtual void adaptRenderers() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
protected:
|
|
|
|
Sprite* _backGroundBoxRenderer;
|
|
|
|
Sprite* _backGroundSelectedBoxRenderer;
|
|
|
|
Sprite* _frontCrossRenderer;
|
|
|
|
Sprite* _backGroundBoxDisabledRenderer;
|
|
|
|
Sprite* _frontCrossDisabledRenderer;
|
|
|
|
bool _isSelected;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
bool _isBackgroundSelectedTextureLoaded;
|
|
|
|
bool _isBackgroundDisabledTextureLoaded;
|
|
|
|
bool _isFrontCrossDisabledTextureLoaded;
|
|
|
|
TextureResType _backGroundTexType;
|
|
|
|
TextureResType _backGroundSelectedTexType;
|
|
|
|
TextureResType _frontCrossTexType;
|
|
|
|
TextureResType _backGroundDisabledTexType;
|
|
|
|
TextureResType _frontCrossDisabledTexType;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
float _zoomScale;
|
|
|
|
float _backgroundTextureScaleX;
|
|
|
|
float _backgroundTextureScaleY;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
bool _backGroundBoxRendererAdaptDirty;
|
|
|
|
bool _backGroundSelectedBoxRendererAdaptDirty;
|
|
|
|
bool _frontCrossRendererAdaptDirty;
|
|
|
|
bool _backGroundBoxDisabledRendererAdaptDirty;
|
|
|
|
bool _frontCrossDisabledRendererAdaptDirty;
|
|
|
|
|
|
|
|
std::string _backGroundFileName;
|
|
|
|
std::string _backGroundSelectedFileName;
|
|
|
|
std::string _frontCrossFileName;
|
|
|
|
std::string _backGroundDisabledFileName;
|
|
|
|
std::string _frontCrossDisabledFileName;
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
} // namespace ui
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
// end of ui group
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
#endif /* defined(__UIABSTRACTCHECKBUTTON_H__) */
|