2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
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
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace ui
|
|
|
|
{
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
IMPLEMENT_CLASS_GUI_INFO(RadioButton)
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
RadioButton::RadioButton() : _radioButtonEventCallback(nullptr), _group(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
RadioButton::~RadioButton()
|
|
|
|
{
|
|
|
|
_radioButtonEventCallback = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
_group = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RadioButton* RadioButton::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
RadioButton* widget = new RadioButton();
|
|
|
|
if (widget->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RadioButton* RadioButton::create(const std::string& backGround,
|
2021-12-25 10:04:45 +08:00
|
|
|
const std::string& backGroundSelected,
|
|
|
|
const std::string& cross,
|
|
|
|
const std::string& backGroundDisabled,
|
|
|
|
const std::string& frontCrossDisabled,
|
|
|
|
TextureResType texType)
|
|
|
|
{
|
|
|
|
RadioButton* pWidget = new RadioButton();
|
|
|
|
if (pWidget->init(backGround, backGroundSelected, cross, backGroundDisabled, frontCrossDisabled, texType))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
pWidget->autorelease();
|
|
|
|
return pWidget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pWidget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
RadioButton* RadioButton::create(const std::string& backGround, const std::string& cross, TextureResType texType)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
RadioButton* pWidget = new RadioButton();
|
|
|
|
if (pWidget->init(backGround, "", cross, "", "", texType))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (selected && _group != nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_group->onChangedRadioButtonSelect(this);
|
|
|
|
}
|
|
|
|
this->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioButton::addEventListener(const ccRadioButtonCallback& callback)
|
|
|
|
{
|
|
|
|
_radioButtonEventCallback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioButton::releaseUpEvent()
|
|
|
|
{
|
|
|
|
Widget::releaseUpEvent();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!_isSelected)
|
|
|
|
{
|
|
|
|
setSelected(true);
|
|
|
|
dispatchSelectChangedEvent(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RadioButton::getDescription() const
|
|
|
|
{
|
|
|
|
return "RadioButton";
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* RadioButton::createCloneInstance()
|
|
|
|
{
|
|
|
|
return RadioButton::create();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void RadioButton::copySpecialProperties(Widget* widget)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
RadioButton* radioButton = dynamic_cast<RadioButton*>(widget);
|
|
|
|
if (radioButton)
|
|
|
|
{
|
|
|
|
AbstractCheckButton::copySpecialProperties(widget);
|
|
|
|
_radioButtonEventCallback = radioButton->_radioButtonEventCallback;
|
2021-12-25 10:04:45 +08:00
|
|
|
_ccEventCallback = radioButton->_ccEventCallback;
|
|
|
|
_group = radioButton->_group;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
RadioButtonGroup::RadioButtonGroup()
|
|
|
|
: _radioButtonGroupEventCallback(nullptr), _selectedRadioButton(nullptr), _allowedNoSelection(false)
|
|
|
|
{}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
RadioButtonGroup::~RadioButtonGroup()
|
|
|
|
{
|
|
|
|
_radioButtonGroupEventCallback = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
_selectedRadioButton = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
_radioButtons.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
RadioButtonGroup* RadioButtonGroup::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
RadioButtonGroup* widget = new RadioButtonGroup();
|
|
|
|
if (widget->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioButtonGroup::addEventListener(const ccRadioButtonGroupCallback& callback)
|
|
|
|
{
|
|
|
|
_radioButtonGroupEventCallback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioButtonGroup::addRadioButton(RadioButton* radioButton)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (radioButton != nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(!radioButton->_group, "It already belongs to a group!");
|
|
|
|
radioButton->_group = this;
|
|
|
|
_radioButtons.pushBack(radioButton);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (!_allowedNoSelection && _selectedRadioButton == nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
setSelectedButtonWithoutEvent(radioButton);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioButtonGroup::removeRadioButton(RadioButton* radioButton)
|
|
|
|
{
|
|
|
|
ssize_t index = _radioButtons.getIndex(radioButton);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (index == CC_INVALID_INDEX)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCLOGERROR("The radio button does not belong to this group!");
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (radioButton != nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
radioButton->_group = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (radioButton == _selectedRadioButton)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
deselect();
|
|
|
|
}
|
|
|
|
_radioButtons.erase(index);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (!_allowedNoSelection && _selectedRadioButton == nullptr && !_radioButtons.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
setSelectedButtonWithoutEvent(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioButtonGroup::removeAllRadioButtons()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
while (!_radioButtons.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
removeRadioButton(_radioButtons.at(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t RadioButtonGroup::getNumberOfRadioButtons() const
|
|
|
|
{
|
|
|
|
return _radioButtons.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
RadioButton* RadioButtonGroup::getRadioButtonByIndex(int index) const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (index >= _radioButtons.size())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCLOGERROR("Out of array index! length=%d, requestedIndex=%d", (int)_radioButtons.size(), index);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return _radioButtons.at(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioButtonGroup::deselect()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_selectedRadioButton != nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_selectedRadioButton->setSelected(false);
|
|
|
|
_selectedRadioButton->dispatchSelectChangedEvent(false);
|
|
|
|
}
|
|
|
|
_selectedRadioButton = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RadioButtonGroup::getSelectedButtonIndex() const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return (int)_radioButtons.getIndex(_selectedRadioButton);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_allowedNoSelection && radioButton == nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_selectedRadioButton == radioButton)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (radioButton != nullptr && !_radioButtons.contains(radioButton))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCLOGERROR("The radio button does not belong to this group!");
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
deselect();
|
|
|
|
_selectedRadioButton = radioButton;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_selectedRadioButton != nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_selectedRadioButton->setSelected(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RadioButtonGroup::getDescription() const
|
|
|
|
{
|
|
|
|
return "RadioButtonGroup";
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioButtonGroup::setAllowedNoSelection(bool allowedNoSelection)
|
|
|
|
{
|
|
|
|
_allowedNoSelection = allowedNoSelection;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_allowedNoSelection && _selectedRadioButton == nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-08-18 14:29:09 +08:00
|
|
|
if (!_radioButtons.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
setSelectedButton(_radioButtons.at(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RadioButtonGroup::isAllowedNoSelection() const
|
|
|
|
{
|
|
|
|
return _allowedNoSelection;
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* RadioButtonGroup::createCloneInstance()
|
|
|
|
{
|
|
|
|
return RadioButtonGroup::create();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void RadioButtonGroup::copySpecialProperties(Widget* widget)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
RadioButtonGroup* radioButtonGroup = dynamic_cast<RadioButtonGroup*>(widget);
|
|
|
|
if (radioButtonGroup)
|
|
|
|
{
|
|
|
|
_radioButtonGroupEventCallback = radioButtonGroup->_radioButtonGroupEventCallback;
|
2021-12-25 10:04:45 +08:00
|
|
|
_ccEventCallback = radioButtonGroup->_ccEventCallback;
|
|
|
|
_selectedRadioButton = radioButtonGroup->_selectedRadioButton;
|
|
|
|
_allowedNoSelection = radioButtonGroup->_allowedNoSelection;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_radioButtons.clear();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& radioButton : radioButtonGroup->_radioButtons)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_radioButtons.pushBack(radioButton);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RadioButtonGroup::onChangedRadioButtonSelect(RadioButton* radioButton)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_selectedRadioButton != radioButton)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
deselect();
|
|
|
|
_selectedRadioButton = radioButton;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
this->retain();
|
|
|
|
if (_radioButtonGroupEventCallback)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
int index = (int)_radioButtons.getIndex(radioButton);
|
2019-11-23 20:27:39 +08:00
|
|
|
_radioButtonGroupEventCallback(_selectedRadioButton, index, EventType::SELECT_CHANGED);
|
|
|
|
}
|
|
|
|
if (_ccEventCallback)
|
|
|
|
{
|
|
|
|
_ccEventCallback(this, static_cast<int>(EventType::SELECT_CHANGED));
|
|
|
|
}
|
|
|
|
this->release();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
} // namespace ui
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
NS_CC_END
|