axmol/core/ui/UIRadioButton.h

296 lines
8.5 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2015 Neo Kim (neo.kim@neofect.com)
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
https://axis-project.github.io/
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 __UIRADIOBUTTON_H__
#define __UIRADIOBUTTON_H__
#include "ui/UIAbstractCheckButton.h"
#include "ui/GUIExport.h"
/**
* @addtogroup ui
* @{
*/
NS_AX_BEGIN
2021-12-25 10:04:45 +08:00
namespace ui
{
class RadioButtonGroup;
2015-07-02 14:20:06 +08:00
/**
* RadioButton is a specific type of two-states button that is similar to CheckBox.
* Additionally, it can be used together with RadioButtonGroup to interact with other radio buttons.
*/
2022-07-15 19:17:01 +08:00
class AX_GUI_DLL RadioButton : public AbstractCheckButton
{
DECLARE_CLASS_GUI_INFO
2021-12-25 10:04:45 +08:00
friend class RadioButtonGroup;
2021-12-25 10:04:45 +08:00
public:
/**
* Radio button event types.
*/
enum class EventType
{
SELECTED,
UNSELECTED
};
2021-12-25 10:04:45 +08:00
/**
* A callback which will be called after certain RadioButton event issue.
* @see `RadioButton::EventType`
*/
typedef std::function<void(RadioButton* radioButton, EventType)> ccRadioButtonCallback;
2021-12-25 10:04:45 +08:00
/**
* Default constructor.
*
* @lua new
*/
RadioButton();
2021-12-25 10:04:45 +08:00
/**
* Default destructor.
*
* @lua NA
*/
virtual ~RadioButton();
2021-12-25 10:04:45 +08:00
/**
* Create and return a empty RadioButton instance pointer.
*/
static RadioButton* create();
2021-12-25 10:04:45 +08:00
/**
* Create a radio button with various images.
*
* @param backGround backGround texture.
* @param backGroundSelected backGround selected state texture.
* @param cross cross texture.
* @param backGroundDisabled backGround disabled state texture.
* @param frontCrossDisabled cross dark state texture.
* @param texType @see `Widget::TextureResType`
*
* @return A RadioButton instance pointer.
*/
2021-12-26 23:26:34 +08:00
static RadioButton* create(std::string_view backGround,
std::string_view backGroundSelected,
std::string_view cross,
std::string_view backGroundDisabled,
std::string_view frontCrossDisabled,
2021-12-25 10:04:45 +08:00
TextureResType texType = TextureResType::LOCAL);
/**
* Another factory method to create a RadioButton instance.
* This method uses less resource to create a RadioButton.
* @param backGround The background image name in `std::string`.
* @param cross The cross image name in `std::string`.
* @param texType The texture's resource type in `Widget::TextureResType`.
* @return A RadioButton instance pointer
*/
2021-12-26 23:26:34 +08:00
static RadioButton* create(std::string_view backGround,
std::string_view cross,
2021-12-25 10:04:45 +08:00
TextureResType texType = TextureResType::LOCAL);
/**
* Add a callback function which would be called when radio button is selected or unselected.
*@param callback A std::function with type @see `ccRadioButtonCallback`
*/
void addEventListener(const ccRadioButtonCallback& callback);
2021-12-25 10:04:45 +08:00
virtual std::string getDescription() const override;
2021-12-25 10:04:45 +08:00
protected:
virtual void releaseUpEvent() override;
virtual void dispatchSelectChangedEvent(bool selected) override;
2021-12-25 10:04:45 +08:00
virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;
2021-12-25 10:04:45 +08:00
ccRadioButtonCallback _radioButtonEventCallback;
RadioButtonGroup* _group;
};
2015-07-02 14:20:06 +08:00
/**
* RadioButtonGroup groups designated radio buttons to make them interact to each other.
* In one RadioButtonGroup, only one or no RadioButton can be checked.
*/
2022-07-15 19:17:01 +08:00
class AX_GUI_DLL RadioButtonGroup : public Widget
{
friend class RadioButton;
2021-12-25 10:04:45 +08:00
public:
/**
* Radio button group event types.
*/
enum class EventType
{
SELECT_CHANGED,
};
2021-12-25 10:04:45 +08:00
/**
* A callback which will be called after RadioButtonGroup event issue.
* @see `RadioButtonGroup::EventType`
*/
typedef std::function<void(RadioButton* radioButton, int index, EventType)> ccRadioButtonGroupCallback;
2021-12-25 10:04:45 +08:00
/**
* Default constructor.
*
* @lua new
*/
RadioButtonGroup();
2021-12-25 10:04:45 +08:00
/**
* Default destructor.
*
* @lua NA
*/
virtual ~RadioButtonGroup();
2021-12-25 10:04:45 +08:00
/**
* Create and return a empty RadioButtonGroup instance pointer.
*/
static RadioButtonGroup* create();
2021-12-25 10:04:45 +08:00
/**
* Add a callback function which would be called when radio button is selected or unselected.
*@param callback A std::function with type @see `ccRadioButtonGroupCallback`
*/
void addEventListener(const ccRadioButtonGroupCallback& callback);
2021-12-25 10:04:45 +08:00
2015-07-02 14:20:06 +08:00
/**
* Get the index of selected radio button.
*
* @return the selected button's index. Returns -1 if no button is selected.
*/
virtual int getSelectedButtonIndex() const;
2021-12-25 10:04:45 +08:00
2015-07-02 14:20:06 +08:00
/**
* Select a radio button by index.
*
* @param index of the radio button
*/
virtual void setSelectedButton(int index);
2021-12-25 10:04:45 +08:00
2015-07-02 14:20:06 +08:00
/**
* Select a radio button by instance.
*
* @param radio button instance
*/
virtual void setSelectedButton(RadioButton* radioButton);
2021-12-25 10:04:45 +08:00
/**
* Select a radio button by index without event dispatch.
*
* @param index of the radio button
*/
virtual void setSelectedButtonWithoutEvent(int index);
2021-12-25 10:04:45 +08:00
/**
* Select a radio button by instance without event dispatch.
*
* @param radio button instance
*/
virtual void setSelectedButtonWithoutEvent(RadioButton* radioButton);
2021-12-25 10:04:45 +08:00
2015-07-02 14:20:06 +08:00
/**
* Add a radio button into this group.
*
* @param radio button instance
*/
virtual void addRadioButton(RadioButton* radioButton);
2021-12-25 10:04:45 +08:00
2015-07-02 14:20:06 +08:00
/**
* Remove a radio button from this group.
*
* @param radio button instance
*/
virtual void removeRadioButton(RadioButton* radioButton);
2021-12-25 10:04:45 +08:00
2015-07-28 19:23:22 +08:00
/**
* Remove all radio button from this group.
*/
virtual void removeAllRadioButtons();
2021-12-25 10:04:45 +08:00
2015-07-02 14:20:06 +08:00
/**
* Get the number of radio buttons in this group.
*
* @return the number of radio buttons in this group
*/
ssize_t getNumberOfRadioButtons() const;
2021-12-25 10:04:45 +08:00
2015-07-02 14:20:06 +08:00
/**
* Get a radio button in this group by index.
*
* @param index of the radio button
* @return radio button instance. Returns nullptr if out of index.
*/
RadioButton* getRadioButtonByIndex(int index) const;
2021-12-25 10:04:45 +08:00
2015-07-02 14:20:06 +08:00
/**
* Set a flag for allowing no-selection feature.
* If it is allowed, no radio button can be selected.
* If it is not allowed, one radio button must be selected all time except it is empty.
* Default is not allowed.
*
* @param true means allowing no-selection, false means disallowing no-selection.
*/
void setAllowedNoSelection(bool allowedNoSelection);
2021-12-25 10:04:45 +08:00
2015-07-02 14:20:06 +08:00
/**
* Query whether no-selection is allowed or not.
*
* @param true means no-selection is allowed, false means no-selection is not allowed.
*/
bool isAllowedNoSelection() const;
2021-12-25 10:04:45 +08:00
virtual std::string getDescription() const override;
2021-12-25 10:04:45 +08:00
protected:
virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;
2021-12-25 10:04:45 +08:00
void onChangedRadioButtonSelect(RadioButton* radioButton);
void deselect();
2021-12-25 10:04:45 +08:00
Vector<RadioButton*> _radioButtons;
ccRadioButtonGroupCallback _radioButtonGroupEventCallback;
RadioButton* _selectedRadioButton;
bool _allowedNoSelection;
};
2021-12-25 10:04:45 +08:00
} // namespace ui
NS_AX_END
// end of ui group
/// @}
#endif /* defined(__UIRADIOBUTTON_H__) */