axmol/cocos/gui/UICheckBox.cpp

495 lines
15 KiB
C++
Raw Normal View History

2013-09-13 22:20:20 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
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 "gui/UICheckBox.h"
2013-09-13 22:20:20 +08:00
namespace gui {
2013-09-13 22:20:20 +08:00
2013-11-06 16:04:06 +08:00
2013-09-13 22:20:20 +08:00
UICheckBox::UICheckBox():
2013-11-14 11:37:46 +08:00
_backGroundBoxRenderer(nullptr),
_backGroundSelectedBoxRenderer(nullptr),
_frontCrossRenderer(nullptr),
_backGroundBoxDisabledRenderer(nullptr),
_frontCrossDisabledRenderer(nullptr),
2013-09-16 20:54:13 +08:00
_isSelected(true),
2013-11-14 11:37:46 +08:00
_checkBoxEventListener(nullptr),
_checkBoxEventSelector(nullptr),
2013-09-16 20:54:13 +08:00
_backGroundTexType(UI_TEX_TYPE_LOCAL),
_backGroundSelectedTexType(UI_TEX_TYPE_LOCAL),
_frontCrossTexType(UI_TEX_TYPE_LOCAL),
_backGroundDisabledTexType(UI_TEX_TYPE_LOCAL),
2013-11-06 16:04:06 +08:00
_frontCrossDisabledTexType(UI_TEX_TYPE_LOCAL),
_backGroundFileName(""),
_backGroundSelectedFileName(""),
_frontCrossFileName(""),
_backGroundDisabledFileName(""),
_frontCrossDisabledFileName("")
2013-09-13 22:20:20 +08:00
{
}
UICheckBox::~UICheckBox()
{
2013-11-14 11:37:46 +08:00
_checkBoxEventListener = nullptr;
_checkBoxEventSelector = nullptr;
2013-09-13 22:20:20 +08:00
}
UICheckBox* UICheckBox::create()
{
UICheckBox* widget = new UICheckBox();
if (widget && widget->init())
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
2013-11-14 11:37:46 +08:00
return nullptr;
2013-09-13 22:20:20 +08:00
}
bool UICheckBox::init()
{
if (UIWidget::init())
{
setSelectedState(false);
return true;
}
return false;
}
void UICheckBox::initRenderer()
{
UIWidget::initRenderer();
2013-11-06 16:04:06 +08:00
_backGroundBoxRenderer = cocos2d::Sprite::create();
_backGroundSelectedBoxRenderer = cocos2d::Sprite::create();
_frontCrossRenderer = cocos2d::Sprite::create();
_backGroundBoxDisabledRenderer = cocos2d::Sprite::create();
_frontCrossDisabledRenderer = cocos2d::Sprite::create();
2013-09-16 20:54:13 +08:00
_renderer->addChild(_backGroundBoxRenderer);
_renderer->addChild(_backGroundSelectedBoxRenderer);
_renderer->addChild(_frontCrossRenderer);
_renderer->addChild(_backGroundBoxDisabledRenderer);
_renderer->addChild(_frontCrossDisabledRenderer);
2013-09-13 22:20:20 +08:00
}
void UICheckBox::loadTextures(const char *backGround, const char *backGroundSelected, const char *cross,const char* backGroundDisabled,const char* frontCrossDisabled,TextureResType texType)
{
loadTextureBackGround(backGround,texType);
loadTextureBackGroundSelected(backGroundSelected,texType);
loadTextureFrontCross(cross,texType);
loadTextureBackGroundDisabled(backGroundDisabled,texType);
loadTextureFrontCrossDisabled(frontCrossDisabled,texType);
}
void UICheckBox::loadTextureBackGround(const char *backGround,TextureResType texType)
{
if (!backGround || strcmp(backGround, "") == 0)
{
return;
}
2013-11-06 16:04:06 +08:00
_backGroundFileName = backGround;
2013-09-16 20:54:13 +08:00
_backGroundTexType = texType;
switch (_backGroundTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-09-16 20:54:13 +08:00
_backGroundBoxRenderer->initWithFile(backGround);
2013-09-13 22:20:20 +08:00
break;
case UI_TEX_TYPE_PLIST:
2013-09-16 20:54:13 +08:00
_backGroundBoxRenderer->initWithSpriteFrameName(backGround);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
2013-09-16 20:54:13 +08:00
_backGroundBoxRenderer->setColor(getColor());
_backGroundBoxRenderer->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
backGroundTextureScaleChangedWithSize();
}
void UICheckBox::loadTextureBackGroundSelected(const char *backGroundSelected,TextureResType texType)
{
if (!backGroundSelected || strcmp(backGroundSelected, "") == 0)
{
return;
}
2013-11-06 16:04:06 +08:00
_backGroundSelectedFileName = backGroundSelected;
2013-09-16 20:54:13 +08:00
_backGroundSelectedTexType = texType;
switch (_backGroundSelectedTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-09-16 20:54:13 +08:00
_backGroundSelectedBoxRenderer->initWithFile(backGroundSelected);
2013-09-13 22:20:20 +08:00
break;
case UI_TEX_TYPE_PLIST:
2013-09-16 20:54:13 +08:00
_backGroundSelectedBoxRenderer->initWithSpriteFrameName(backGroundSelected);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
2013-09-16 20:54:13 +08:00
_backGroundSelectedBoxRenderer->setColor(getColor());
_backGroundSelectedBoxRenderer->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
backGroundSelectedTextureScaleChangedWithSize();
}
void UICheckBox::loadTextureFrontCross(const char *cross,TextureResType texType)
{
if (!cross || strcmp(cross, "") == 0)
{
return;
}
2013-11-06 16:04:06 +08:00
_frontCrossFileName = cross;
2013-09-16 20:54:13 +08:00
_frontCrossTexType = texType;
switch (_frontCrossTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-09-16 20:54:13 +08:00
_frontCrossRenderer->initWithFile(cross);
2013-09-13 22:20:20 +08:00
break;
case UI_TEX_TYPE_PLIST:
2013-09-16 20:54:13 +08:00
_frontCrossRenderer->initWithSpriteFrameName(cross);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
2013-09-16 20:54:13 +08:00
_frontCrossRenderer->setColor(getColor());
_frontCrossRenderer->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
frontCrossTextureScaleChangedWithSize();
}
void UICheckBox::loadTextureBackGroundDisabled(const char *backGroundDisabled,TextureResType texType)
{
if (!backGroundDisabled || strcmp(backGroundDisabled, "") == 0)
{
return;
}
2013-11-06 16:04:06 +08:00
_backGroundDisabledFileName = backGroundDisabled;
2013-09-16 20:54:13 +08:00
_backGroundDisabledTexType = texType;
switch (_backGroundDisabledTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-09-16 20:54:13 +08:00
_backGroundBoxDisabledRenderer->initWithFile(backGroundDisabled);
2013-09-13 22:20:20 +08:00
break;
case UI_TEX_TYPE_PLIST:
2013-09-16 20:54:13 +08:00
_backGroundBoxDisabledRenderer->initWithSpriteFrameName(backGroundDisabled);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
2013-09-16 20:54:13 +08:00
_backGroundBoxDisabledRenderer->setColor(getColor());
_backGroundBoxDisabledRenderer->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
backGroundDisabledTextureScaleChangedWithSize();
}
void UICheckBox::loadTextureFrontCrossDisabled(const char *frontCrossDisabled,TextureResType texType)
{
if (!frontCrossDisabled || strcmp(frontCrossDisabled, "") == 0)
{
return;
}
2013-11-06 16:04:06 +08:00
_frontCrossDisabledFileName = frontCrossDisabled;
2013-09-16 20:54:13 +08:00
_frontCrossDisabledTexType = texType;
switch (_frontCrossDisabledTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-09-16 20:54:13 +08:00
_frontCrossDisabledRenderer->initWithFile(frontCrossDisabled);
2013-09-13 22:20:20 +08:00
break;
case UI_TEX_TYPE_PLIST:
2013-09-16 20:54:13 +08:00
_frontCrossDisabledRenderer->initWithSpriteFrameName(frontCrossDisabled);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
2013-09-16 20:54:13 +08:00
_frontCrossDisabledRenderer->setColor(getColor());
_frontCrossRenderer->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
frontCrossDisabledTextureScaleChangedWithSize();
}
2013-11-06 16:04:06 +08:00
void UICheckBox::onTouchEnded(const cocos2d::Point &touchPoint)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
if (_focus)
2013-09-13 22:20:20 +08:00
{
releaseUpEvent();
2013-09-16 20:54:13 +08:00
if (_isSelected){
2013-09-13 22:20:20 +08:00
setSelectedState(false);
unSelectedEvent();
}
else
{
setSelectedState(true);
selectedEvent();
}
}
setFocused(false);
2013-09-16 15:32:52 +08:00
_widgetParent->checkChildInfo(2,this,touchPoint);
2013-09-13 22:20:20 +08:00
}
void UICheckBox::onPressStateChangedToNormal()
{
2013-09-16 20:54:13 +08:00
_backGroundBoxRenderer->setVisible(true);
_backGroundSelectedBoxRenderer->setVisible(false);
_backGroundBoxDisabledRenderer->setVisible(false);
_frontCrossDisabledRenderer->setVisible(false);
2013-09-13 22:20:20 +08:00
}
void UICheckBox::onPressStateChangedToPressed()
{
2013-09-16 20:54:13 +08:00
_backGroundBoxRenderer->setVisible(false);
_backGroundSelectedBoxRenderer->setVisible(true);
_backGroundBoxDisabledRenderer->setVisible(false);
_frontCrossDisabledRenderer->setVisible(false);
2013-09-13 22:20:20 +08:00
}
void UICheckBox::onPressStateChangedToDisabled()
{
2013-09-16 20:54:13 +08:00
_backGroundBoxRenderer->setVisible(false);
_backGroundSelectedBoxRenderer->setVisible(false);
_backGroundBoxDisabledRenderer->setVisible(true);
_frontCrossRenderer->setVisible(false);
if (_isSelected)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_frontCrossDisabledRenderer->setVisible(true);
2013-09-13 22:20:20 +08:00
}
}
void UICheckBox::setSelectedState(bool selected)
{
2013-09-16 20:54:13 +08:00
if (selected == _isSelected)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 20:54:13 +08:00
_isSelected = selected;
_frontCrossRenderer->setVisible(_isSelected);
2013-09-13 22:20:20 +08:00
}
bool UICheckBox::getSelectedState()
{
2013-09-16 20:54:13 +08:00
return _isSelected;
2013-09-13 22:20:20 +08:00
}
void UICheckBox::selectedEvent()
{
2013-11-08 18:43:06 +08:00
if (_checkBoxEventListener && _checkBoxEventSelector)
2013-09-13 22:20:20 +08:00
{
2013-11-08 18:43:06 +08:00
(_checkBoxEventListener->*_checkBoxEventSelector)(this,CHECKBOX_STATE_EVENT_SELECTED);
2013-09-13 22:20:20 +08:00
}
}
void UICheckBox::unSelectedEvent()
{
2013-11-08 18:43:06 +08:00
if (_checkBoxEventListener && _checkBoxEventSelector)
2013-09-13 22:20:20 +08:00
{
2013-11-08 18:43:06 +08:00
(_checkBoxEventListener->*_checkBoxEventSelector)(this,CHECKBOX_STATE_EVENT_UNSELECTED);
2013-09-13 22:20:20 +08:00
}
}
2013-11-08 18:43:06 +08:00
void UICheckBox::addEventListenerCheckBox(cocos2d::Object *target, SEL_SelectedStateEvent selector)
2013-09-13 22:20:20 +08:00
{
2013-11-08 18:43:06 +08:00
_checkBoxEventListener = target;
_checkBoxEventSelector = selector;
2013-09-13 22:20:20 +08:00
}
void UICheckBox::setFlipX(bool flipX)
{
2013-09-16 20:54:13 +08:00
_backGroundBoxRenderer->setFlippedX(flipX);
_backGroundSelectedBoxRenderer->setFlippedX(flipX);
_frontCrossRenderer->setFlippedX(flipX);
_backGroundBoxDisabledRenderer->setFlippedX(flipX);
_frontCrossDisabledRenderer->setFlippedX(flipX);
2013-09-13 22:20:20 +08:00
}
void UICheckBox::setFlipY(bool flipY)
{
2013-09-16 20:54:13 +08:00
_backGroundBoxRenderer->setFlippedY(flipY);
_backGroundSelectedBoxRenderer->setFlippedY(flipY);
_frontCrossRenderer->setFlippedY(flipY);
_backGroundBoxDisabledRenderer->setFlippedY(flipY);
_frontCrossDisabledRenderer->setFlippedY(flipY);
2013-09-13 22:20:20 +08:00
}
bool UICheckBox::isFlipX()
{
2013-09-16 20:54:13 +08:00
return _backGroundBoxRenderer->isFlippedX();
2013-09-13 22:20:20 +08:00
}
bool UICheckBox::isFlipY()
{
2013-09-16 20:54:13 +08:00
return _backGroundBoxRenderer->isFlippedY();
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UICheckBox::setAnchorPoint(const cocos2d::Point &pt)
2013-09-13 22:20:20 +08:00
{
UIWidget::setAnchorPoint(pt);
2013-09-16 20:54:13 +08:00
_backGroundBoxRenderer->setAnchorPoint(pt);
_backGroundSelectedBoxRenderer->setAnchorPoint(pt);
_backGroundBoxDisabledRenderer->setAnchorPoint(pt);
_frontCrossRenderer->setAnchorPoint(pt);
_frontCrossDisabledRenderer->setAnchorPoint(pt);
2013-09-13 22:20:20 +08:00
}
void UICheckBox::onSizeChanged()
{
backGroundTextureScaleChangedWithSize();
backGroundSelectedTextureScaleChangedWithSize();
frontCrossTextureScaleChangedWithSize();
backGroundDisabledTextureScaleChangedWithSize();
frontCrossDisabledTextureScaleChangedWithSize();
}
2013-11-06 16:04:06 +08:00
const cocos2d::Size& UICheckBox::getContentSize() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
return _backGroundBoxRenderer->getContentSize();
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
cocos2d::Node* UICheckBox::getVirtualRenderer()
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
return _backGroundBoxRenderer;
2013-09-13 22:20:20 +08:00
}
void UICheckBox::backGroundTextureScaleChangedWithSize()
{
2013-09-16 15:32:52 +08:00
if (_ignoreSize)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_backGroundBoxRenderer->setScale(1.0f);
_size = _backGroundBoxRenderer->getContentSize();
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
cocos2d::Size textureSize = _backGroundBoxRenderer->getContentSize();
2013-09-13 22:20:20 +08:00
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
{
2013-09-16 20:54:13 +08:00
_backGroundBoxRenderer->setScale(1.0f);
2013-09-13 22:20:20 +08:00
return;
}
2013-09-16 15:32:52 +08:00
float scaleX = _size.width / textureSize.width;
float scaleY = _size.height / textureSize.height;
2013-09-16 20:54:13 +08:00
_backGroundBoxRenderer->setScaleX(scaleX);
_backGroundBoxRenderer->setScaleY(scaleY);
2013-09-13 22:20:20 +08:00
}
}
void UICheckBox::backGroundSelectedTextureScaleChangedWithSize()
{
2013-09-16 15:32:52 +08:00
if (_ignoreSize)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_backGroundSelectedBoxRenderer->setScale(1.0f);
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
cocos2d::Size textureSize = _backGroundSelectedBoxRenderer->getContentSize();
2013-09-13 22:20:20 +08:00
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
{
2013-09-16 20:54:13 +08:00
_backGroundSelectedBoxRenderer->setScale(1.0f);
2013-09-13 22:20:20 +08:00
return;
}
2013-09-16 15:32:52 +08:00
float scaleX = _size.width / textureSize.width;
float scaleY = _size.height / textureSize.height;
2013-09-16 20:54:13 +08:00
_backGroundSelectedBoxRenderer->setScaleX(scaleX);
_backGroundSelectedBoxRenderer->setScaleY(scaleY);
2013-09-13 22:20:20 +08:00
}
}
void UICheckBox::frontCrossTextureScaleChangedWithSize()
{
2013-09-16 15:32:52 +08:00
if (_ignoreSize)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_frontCrossRenderer->setScale(1.0f);
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
cocos2d::Size textureSize = _frontCrossRenderer->getContentSize();
2013-09-13 22:20:20 +08:00
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
{
2013-09-16 20:54:13 +08:00
_frontCrossRenderer->setScale(1.0f);
2013-09-13 22:20:20 +08:00
return;
}
2013-09-16 15:32:52 +08:00
float scaleX = _size.width / textureSize.width;
float scaleY = _size.height / textureSize.height;
2013-09-16 20:54:13 +08:00
_frontCrossRenderer->setScaleX(scaleX);
_frontCrossRenderer->setScaleY(scaleY);
2013-09-13 22:20:20 +08:00
}
}
void UICheckBox::backGroundDisabledTextureScaleChangedWithSize()
{
2013-09-16 15:32:52 +08:00
if (_ignoreSize)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_backGroundBoxDisabledRenderer->setScale(1.0f);
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
cocos2d::Size textureSize = _backGroundBoxDisabledRenderer->getContentSize();
2013-09-13 22:20:20 +08:00
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
{
2013-09-16 20:54:13 +08:00
_backGroundBoxDisabledRenderer->setScale(1.0f);
2013-09-13 22:20:20 +08:00
return;
}
2013-09-16 15:32:52 +08:00
float scaleX = _size.width / textureSize.width;
float scaleY = _size.height / textureSize.height;
2013-09-16 20:54:13 +08:00
_backGroundBoxDisabledRenderer->setScaleX(scaleX);
_backGroundBoxDisabledRenderer->setScaleY(scaleY);
2013-09-13 22:20:20 +08:00
}
}
void UICheckBox::frontCrossDisabledTextureScaleChangedWithSize()
{
2013-09-16 15:32:52 +08:00
if (_ignoreSize)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_frontCrossDisabledRenderer->setScale(1.0f);
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
cocos2d::Size textureSize = _frontCrossDisabledRenderer->getContentSize();
2013-09-13 22:20:20 +08:00
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
{
2013-09-16 20:54:13 +08:00
_frontCrossDisabledRenderer->setScale(1.0f);
2013-09-13 22:20:20 +08:00
return;
}
2013-09-16 15:32:52 +08:00
float scaleX = _size.width / textureSize.width;
float scaleY = _size.height / textureSize.height;
2013-09-16 20:54:13 +08:00
_frontCrossDisabledRenderer->setScaleX(scaleX);
_frontCrossDisabledRenderer->setScaleY(scaleY);
2013-09-13 22:20:20 +08:00
}
}
2013-09-17 17:59:20 +08:00
const char* UICheckBox::getDescription() const
{
return "CheckBox";
}
2013-11-06 16:04:06 +08:00
UIWidget* UICheckBox::createCloneInstance()
{
return UICheckBox::create();
}
void UICheckBox::copySpecialProperties(UIWidget *widget)
{
UICheckBox* checkBox = dynamic_cast<UICheckBox*>(widget);
if (checkBox)
{
loadTextureBackGround(checkBox->_backGroundFileName.c_str(), checkBox->_backGroundTexType);
loadTextureBackGroundSelected(checkBox->_backGroundSelectedFileName.c_str(), checkBox->_backGroundSelectedTexType);
loadTextureFrontCross(checkBox->_frontCrossFileName.c_str(), checkBox->_frontCrossTexType);
loadTextureBackGroundDisabled(checkBox->_backGroundDisabledFileName.c_str(), checkBox->_backGroundDisabledTexType);
loadTextureFrontCrossDisabled(checkBox->_frontCrossDisabledFileName.c_str(), checkBox->_frontCrossDisabledTexType);
setSelectedState(checkBox->_isSelected);
}
}
}