123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- /****************************************************************************
- Copyright (c) 2015 Neo Kim (neo.kim@neofect.com)
- 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 __UIRADIOBUTTON_H__
- #define __UIRADIOBUTTON_H__
- #include "ui/UIAbstractCheckButton.h"
- #include "ui/GUIExport.h"
- /**
- * @addtogroup ui
- * @{
- */
- NS_CC_BEGIN
- namespace ui {
- class RadioButtonGroup;
- /**
- * 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.
- */
- class CC_GUI_DLL RadioButton : public AbstractCheckButton
- {
- DECLARE_CLASS_GUI_INFO
-
- friend class RadioButtonGroup;
-
- public:
- /**
- * 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;
-
- protected:
- virtual void releaseUpEvent(Touch* touch=nullptr) override;
- virtual void dispatchSelectChangedEvent(bool selected) override;
-
- virtual Widget* createCloneInstance() override;
- virtual void copySpecialProperties(Widget* model) override;
-
- ccRadioButtonCallback _radioButtonEventCallback;
- RadioButtonGroup* _group;
-
- };
- /**
- * RadioButtonGroup groups designated radio buttons to make them interact to each other.
- * In one RadioButtonGroup, only one or no RadioButton can be checked.
- */
- class CC_GUI_DLL RadioButtonGroup : public Widget
- {
- friend class RadioButton;
- public:
- /**
- * 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);
-
- /**
- * Get the index of selected radio button.
- *
- * @return the selected button's index. Returns -1 if no button is selected.
- */
- virtual int getSelectedButtonIndex() const;
-
- /**
- * Select a radio button by index.
- *
- * @param index of the radio button
- */
- virtual void setSelectedButton(int index);
-
- /**
- * Select a radio button by instance.
- *
- * @param radio button instance
- */
- virtual void setSelectedButton(RadioButton* radioButton);
-
- /**
- * 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);
-
- /**
- * Add a radio button into this group.
- *
- * @param radio button instance
- */
- virtual void addRadioButton(RadioButton* radioButton);
-
- /**
- * Remove a radio button from this group.
- *
- * @param radio button instance
- */
- virtual void removeRadioButton(RadioButton* radioButton);
-
- /**
- * Remove all radio button from this group.
- */
- virtual void removeAllRadioButtons();
-
- /**
- * Get the number of radio buttons in this group.
- *
- * @return the number of radio buttons in this group
- */
- ssize_t getNumberOfRadioButtons() const;
-
- /**
- * 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;
-
- /**
- * 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);
-
- /**
- * 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;
-
- virtual std::string getDescription() const override;
-
- protected:
- virtual Widget* createCloneInstance() override;
- virtual void copySpecialProperties(Widget* model) override;
-
- void onChangedRadioButtonSelect(RadioButton* radioButton);
- void deselect();
-
- Vector<RadioButton*> _radioButtons;
- ccRadioButtonGroupCallback _radioButtonGroupEventCallback;
- RadioButton* _selectedRadioButton;
- bool _allowedNoSelection;
-
- };
-
- }
- NS_CC_END
- // end of ui group
- /// @}
- #endif /* defined(__UIRADIOBUTTON_H__) */
|