2015-06-25 17:48:15 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2015 Neo Kim (neo.kim@neofect.com)
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2015-06-25 17:48:15 +08:00
|
|
|
|
|
|
|
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 __UIRADIOBUTTON_H__
|
|
|
|
#define __UIRADIOBUTTON_H__
|
|
|
|
|
|
|
|
#include "ui/UIAbstractCheckButton.h"
|
|
|
|
#include "ui/GUIExport.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup ui
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2015-06-25 17:48:15 +08:00
|
|
|
class CC_GUI_DLL RadioButton : public AbstractCheckButton
|
|
|
|
{
|
2015-06-30 19:31:31 +08:00
|
|
|
|
|
|
|
DECLARE_CLASS_GUI_INFO
|
|
|
|
|
|
|
|
friend class RadioButtonGroup;
|
|
|
|
|
2015-06-25 17:48:15 +08:00
|
|
|
public:
|
2015-06-30 19:31:31 +08:00
|
|
|
/**
|
|
|
|
* Radio button event types.
|
|
|
|
*/
|
|
|
|
enum class EventType
|
|
|
|
{
|
|
|
|
SELECTED,
|
|
|
|
UNSELECTED
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A callback which will be called after certain RadioButton event issue.
|
|
|
|
* @see `RadioButton::EventType`
|
|
|
|
*/
|
|
|
|
typedef std::function<void(RadioButton* radioButton, EventType)> ccRadioButtonCallback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor.
|
|
|
|
*
|
|
|
|
* @lua new
|
|
|
|
*/
|
|
|
|
RadioButton();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default destructor.
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
virtual ~RadioButton();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create and return a empty RadioButton instance pointer.
|
|
|
|
*/
|
|
|
|
static RadioButton* create();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
static RadioButton* create(const std::string& backGround,
|
|
|
|
const std::string& backGroundSelected,
|
|
|
|
const std::string& cross,
|
|
|
|
const std::string& backGroundDisabled,
|
|
|
|
const std::string& frontCrossDisabled,
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
static RadioButton* create(const std::string& backGround,
|
|
|
|
const std::string& cross,
|
|
|
|
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);
|
|
|
|
|
|
|
|
virtual std::string getDescription() const override;
|
|
|
|
|
2015-06-25 17:48:15 +08:00
|
|
|
protected:
|
2015-06-30 19:31:31 +08:00
|
|
|
virtual void releaseUpEvent() override;
|
2015-06-25 17:48:15 +08:00
|
|
|
|
2015-06-30 19:31:31 +08:00
|
|
|
virtual void dispatchSelectChangedEvent(bool selected) override;
|
|
|
|
|
|
|
|
virtual Widget* createCloneInstance() override;
|
|
|
|
virtual void copySpecialProperties(Widget* model) override;
|
2015-07-02 14:20:06 +08:00
|
|
|
|
2015-06-30 19:31:31 +08:00
|
|
|
ccRadioButtonCallback _radioButtonEventCallback;
|
2015-07-02 10:28:14 +08:00
|
|
|
RadioButtonGroup* _group;
|
2015-06-30 19:31:31 +08:00
|
|
|
|
2015-06-25 17:48:15 +08:00
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
*/
|
2015-06-25 17:48:15 +08:00
|
|
|
class CC_GUI_DLL RadioButtonGroup : public Widget
|
|
|
|
{
|
2015-06-30 19:31:31 +08:00
|
|
|
friend class RadioButton;
|
2015-06-25 17:48:15 +08:00
|
|
|
public:
|
2015-06-30 19:31:31 +08:00
|
|
|
/**
|
|
|
|
* Radio button group event types.
|
|
|
|
*/
|
|
|
|
enum class EventType
|
|
|
|
{
|
|
|
|
SELECT_CHANGED,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A callback which will be called after RadioButtonGroup event issue.
|
|
|
|
* @see `RadioButtonGroup::EventType`
|
|
|
|
*/
|
|
|
|
typedef std::function<void(RadioButton* radioButton, int index, EventType)> ccRadioButtonGroupCallback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor.
|
|
|
|
*
|
|
|
|
* @lua new
|
|
|
|
*/
|
|
|
|
RadioButtonGroup();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default destructor.
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
virtual ~RadioButtonGroup();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create and return a empty RadioButtonGroup instance pointer.
|
|
|
|
*/
|
|
|
|
static RadioButtonGroup* create();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
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.
|
|
|
|
*/
|
2015-06-30 19:31:31 +08:00
|
|
|
virtual int getSelectedButtonIndex() const;
|
|
|
|
|
2015-07-02 14:20:06 +08:00
|
|
|
/**
|
|
|
|
* Select a radio button by index.
|
|
|
|
*
|
|
|
|
* @param index of the radio button
|
|
|
|
*/
|
2015-06-30 19:31:31 +08:00
|
|
|
virtual void setSelectedButton(int index);
|
2015-07-02 14:20:06 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Select a radio button by instance.
|
|
|
|
*
|
|
|
|
* @param radio button instance
|
|
|
|
*/
|
2015-06-30 19:31:31 +08:00
|
|
|
virtual void setSelectedButton(RadioButton* radioButton);
|
|
|
|
|
2015-08-07 17:45:56 +08:00
|
|
|
/**
|
|
|
|
* Select a radio button by index without event dispatch.
|
|
|
|
*
|
|
|
|
* @param index of the radio button
|
|
|
|
*/
|
|
|
|
virtual void setSelectedButtonWithoutEvent(int index);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select a radio button by instance without event dispatch.
|
|
|
|
*
|
|
|
|
* @param radio button instance
|
|
|
|
*/
|
|
|
|
virtual void setSelectedButtonWithoutEvent(RadioButton* radioButton);
|
|
|
|
|
2015-07-02 14:20:06 +08:00
|
|
|
/**
|
|
|
|
* Add a radio button into this group.
|
|
|
|
*
|
|
|
|
* @param radio button instance
|
|
|
|
*/
|
2015-06-30 19:31:31 +08:00
|
|
|
virtual void addRadioButton(RadioButton* radioButton);
|
2015-07-02 14:20:06 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a radio button from this group.
|
|
|
|
*
|
|
|
|
* @param radio button instance
|
|
|
|
*/
|
2015-06-30 19:31:31 +08:00
|
|
|
virtual void removeRadioButton(RadioButton* radioButton);
|
2015-07-02 10:30:14 +08:00
|
|
|
|
2015-07-28 19:23:22 +08:00
|
|
|
/**
|
|
|
|
* Remove all radio button from this group.
|
|
|
|
*/
|
|
|
|
virtual void removeAllRadioButtons();
|
|
|
|
|
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
|
|
|
|
*/
|
2015-07-02 10:30:14 +08:00
|
|
|
ssize_t getNumberOfRadioButtons() const;
|
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.
|
|
|
|
*/
|
2015-07-02 10:30:14 +08:00
|
|
|
RadioButton* getRadioButtonByIndex(int index) const;
|
2015-06-30 19:31:31 +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.
|
|
|
|
*/
|
2015-06-30 19:31:31 +08:00
|
|
|
void setAllowedNoSelection(bool allowedNoSelection);
|
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.
|
|
|
|
*/
|
2015-06-30 19:31:31 +08:00
|
|
|
bool isAllowedNoSelection() const;
|
|
|
|
|
|
|
|
virtual std::string getDescription() const override;
|
|
|
|
|
2015-06-25 17:48:15 +08:00
|
|
|
protected:
|
2015-06-30 19:31:31 +08:00
|
|
|
virtual Widget* createCloneInstance() override;
|
|
|
|
virtual void copySpecialProperties(Widget* model) override;
|
|
|
|
|
|
|
|
void onChangedRadioButtonSelect(RadioButton* radioButton);
|
|
|
|
void deselect();
|
2015-07-02 10:30:14 +08:00
|
|
|
|
|
|
|
Vector<RadioButton*> _radioButtons;
|
2015-06-30 19:31:31 +08:00
|
|
|
ccRadioButtonGroupCallback _radioButtonGroupEventCallback;
|
|
|
|
RadioButton* _selectedRadioButton;
|
|
|
|
bool _allowedNoSelection;
|
|
|
|
|
2015-06-25 17:48:15 +08:00
|
|
|
};
|
2015-06-30 19:31:31 +08:00
|
|
|
|
2015-06-25 17:48:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
// end of ui group
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
#endif /* defined(__UIRADIOBUTTON_H__) */
|