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