123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- /****************************************************************************
- 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.
- ****************************************************************************/
- #include "ui/UIRadioButton.h"
- NS_CC_BEGIN
- namespace ui {
-
- IMPLEMENT_CLASS_GUI_INFO(RadioButton)
- RadioButton::RadioButton() :
- _radioButtonEventCallback(nullptr),
- _group(nullptr)
- {
- }
- RadioButton::~RadioButton()
- {
- _radioButtonEventCallback = nullptr;
- _group = nullptr;
- }
- RadioButton* RadioButton::create()
- {
- RadioButton* widget = new (std::nothrow) RadioButton();
- if (widget && widget->init())
- {
- widget->autorelease();
- return widget;
- }
- CC_SAFE_DELETE(widget);
- return nullptr;
- }
- RadioButton* RadioButton::create(const std::string& backGround,
- const std::string& backGroundSelected,
- const std::string& cross,
- const std::string& backGroundDisabled,
- const std::string& frontCrossDisabled,
- TextureResType texType)
- {
- RadioButton *pWidget = new (std::nothrow) RadioButton;
- if (pWidget && pWidget->init(backGround,
- backGroundSelected,
- cross,
- backGroundDisabled,
- frontCrossDisabled,
- texType))
- {
- pWidget->autorelease();
- return pWidget;
- }
- CC_SAFE_DELETE(pWidget);
- return nullptr;
- }
- RadioButton* RadioButton::create(const std::string& backGround,
- const std::string& cross,
- TextureResType texType)
- {
- RadioButton *pWidget = new (std::nothrow) RadioButton;
- if (pWidget && pWidget->init(backGround,
- "",
- cross,
- "",
- "",
- texType))
- {
- pWidget->autorelease();
- return pWidget;
- }
- CC_SAFE_DELETE(pWidget);
- return nullptr;
- }
- void RadioButton::dispatchSelectChangedEvent(bool selected)
- {
- EventType eventType = (selected ? EventType::SELECTED : EventType::UNSELECTED);
- this->retain();
- if (_radioButtonEventCallback)
- {
- _radioButtonEventCallback(this, eventType);
- }
- if (_ccEventCallback)
- {
- _ccEventCallback(this, static_cast<int>(eventType));
- }
-
- if(selected && _group != nullptr)
- {
- _group->onChangedRadioButtonSelect(this);
- }
- this->release();
- }
- void RadioButton::addEventListener(const ccRadioButtonCallback& callback)
- {
- _radioButtonEventCallback = callback;
- }
- void RadioButton::releaseUpEvent(Touch* touch)
- {
- Widget::releaseUpEvent(touch);
-
- if (!_isSelected)
- {
- setSelected(true);
- dispatchSelectChangedEvent(true);
- }
- }
- std::string RadioButton::getDescription() const
- {
- return "RadioButton";
- }
- Widget* RadioButton::createCloneInstance()
- {
- return RadioButton::create();
- }
- void RadioButton::copySpecialProperties(Widget *widget)
- {
- RadioButton* radioButton = dynamic_cast<RadioButton*>(widget);
- if (radioButton)
- {
- AbstractCheckButton::copySpecialProperties(widget);
- _radioButtonEventCallback = radioButton->_radioButtonEventCallback;
- _ccEventCallback = radioButton->_ccEventCallback;
- _group = radioButton->_group;
- }
- }
- RadioButtonGroup::RadioButtonGroup() :
- _radioButtonGroupEventCallback(nullptr),
- _selectedRadioButton(nullptr),
- _allowedNoSelection(false)
- {
- }
- RadioButtonGroup::~RadioButtonGroup()
- {
- _radioButtonGroupEventCallback = nullptr;
- _selectedRadioButton = nullptr;
- _radioButtons.clear();
- }
- RadioButtonGroup* RadioButtonGroup::create()
- {
- RadioButtonGroup* widget = new (std::nothrow) RadioButtonGroup();
- if (widget && widget->init())
- {
- widget->autorelease();
- return widget;
- }
- CC_SAFE_DELETE(widget);
- return nullptr;
- }
- void RadioButtonGroup::addEventListener(const ccRadioButtonGroupCallback& callback)
- {
- _radioButtonGroupEventCallback = callback;
- }
- void RadioButtonGroup::addRadioButton(RadioButton* radioButton)
- {
- if(radioButton != nullptr)
- {
- CCASSERT(!radioButton->_group, "It already belongs to a group!");
- radioButton->_group = this;
- _radioButtons.pushBack(radioButton);
-
- if(!_allowedNoSelection && _selectedRadioButton == nullptr)
- {
- setSelectedButtonWithoutEvent(radioButton);
- }
- }
- }
- void RadioButtonGroup::removeRadioButton(RadioButton* radioButton)
- {
- ssize_t index = _radioButtons.getIndex(radioButton);
- if( index == CC_INVALID_INDEX )
- {
- CCLOGERROR("The radio button does not belong to this group!");
- return;
- }
-
- if(radioButton != nullptr)
- {
- radioButton->_group = nullptr;
- if(radioButton == _selectedRadioButton)
- {
- deselect();
- }
- _radioButtons.erase(index);
-
- if(!_allowedNoSelection && _selectedRadioButton == nullptr && !_radioButtons.empty())
- {
- setSelectedButtonWithoutEvent(0);
- }
- }
- }
- void RadioButtonGroup::removeAllRadioButtons()
- {
- while(!_radioButtons.empty())
- {
- removeRadioButton(_radioButtons.at(0));
- }
- }
- ssize_t RadioButtonGroup::getNumberOfRadioButtons() const
- {
- return _radioButtons.size();
- }
- RadioButton* RadioButtonGroup::getRadioButtonByIndex(int index) const
- {
- if(index >= _radioButtons.size())
- {
- CCLOGERROR("Out of array index! length=%d, requestedIndex=%d", (int)_radioButtons.size(), index);
- return nullptr;
- }
- return _radioButtons.at(index);
- }
- void RadioButtonGroup::deselect()
- {
- if(_selectedRadioButton != nullptr)
- {
- _selectedRadioButton->setSelected(false);
- _selectedRadioButton->dispatchSelectChangedEvent(false);
- }
- _selectedRadioButton = nullptr;
- }
- int RadioButtonGroup::getSelectedButtonIndex() const
- {
- return (int) _radioButtons.getIndex(_selectedRadioButton);
- }
- void RadioButtonGroup::setSelectedButton(int index)
- {
- CCASSERT(index < _radioButtons.size(), "Out of array index!");
- setSelectedButton(_radioButtons.at(index));
- }
- void RadioButtonGroup::setSelectedButton(RadioButton* radioButton)
- {
- setSelectedButtonWithoutEvent(radioButton);
- onChangedRadioButtonSelect(_selectedRadioButton);
- }
- void RadioButtonGroup::setSelectedButtonWithoutEvent(int index)
- {
- setSelectedButtonWithoutEvent(_radioButtons.at(index));
- }
- void RadioButtonGroup::setSelectedButtonWithoutEvent(RadioButton* radioButton)
- {
- if(!_allowedNoSelection && radioButton == nullptr)
- {
- return;
- }
- if(_selectedRadioButton == radioButton)
- {
- return;
- }
- if(radioButton != nullptr && !_radioButtons.contains(radioButton))
- {
- CCLOGERROR("The radio button does not belong to this group!");
- return;
- }
-
- deselect();
- _selectedRadioButton = radioButton;
- if(_selectedRadioButton != nullptr)
- {
- _selectedRadioButton->setSelected(true);
- }
- }
- std::string RadioButtonGroup::getDescription() const
- {
- return "RadioButtonGroup";
- }
- void RadioButtonGroup::setAllowedNoSelection(bool allowedNoSelection)
- {
- _allowedNoSelection = allowedNoSelection;
- if(!_allowedNoSelection && _selectedRadioButton == nullptr)
- {
- if (_radioButtons.size() > 0)
- {
- setSelectedButton(_radioButtons.at(0));
- }
- }
- }
- bool RadioButtonGroup::isAllowedNoSelection() const
- {
- return _allowedNoSelection;
- }
- Widget* RadioButtonGroup::createCloneInstance()
- {
- return RadioButtonGroup::create();
- }
- void RadioButtonGroup::copySpecialProperties(Widget *widget)
- {
- RadioButtonGroup* radioButtonGroup = dynamic_cast<RadioButtonGroup*>(widget);
- if (radioButtonGroup)
- {
- _radioButtonGroupEventCallback = radioButtonGroup->_radioButtonGroupEventCallback;
- _ccEventCallback = radioButtonGroup->_ccEventCallback;
- _selectedRadioButton = radioButtonGroup->_selectedRadioButton;
- _allowedNoSelection = radioButtonGroup->_allowedNoSelection;
-
- _radioButtons.clear();
- for(const auto& radioButton : radioButtonGroup->_radioButtons)
- {
- _radioButtons.pushBack(radioButton);
- }
- }
- }
- void RadioButtonGroup::onChangedRadioButtonSelect(RadioButton* radioButton)
- {
- if(_selectedRadioButton != radioButton)
- {
- deselect();
- _selectedRadioButton = radioButton;
- }
-
- this->retain();
- if (_radioButtonGroupEventCallback)
- {
- int index = (int) _radioButtons.getIndex(radioButton);
- _radioButtonGroupEventCallback(_selectedRadioButton, index, EventType::SELECT_CHANGED);
- }
- if (_ccEventCallback)
- {
- _ccEventCallback(this, static_cast<int>(EventType::SELECT_CHANGED));
- }
- this->release();
- }
-
- }
- NS_CC_END
|