2012-04-19 14:35:52 +08:00
|
|
|
/*
|
2012-09-25 17:26:09 +08:00
|
|
|
* Copyright (c) 2012 cocos2d-x.org
|
|
|
|
* http://www.cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
*
|
|
|
|
* Copyright 2011 Yannick Loriot.
|
|
|
|
* http://yannickloriot.com
|
|
|
|
*
|
|
|
|
* 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 "CCControlButton.h"
|
|
|
|
#include "CCScale9Sprite.h"
|
2013-10-15 18:00:03 +08:00
|
|
|
#include "CCLabelTTF.h"
|
|
|
|
#include "CCLabelBMFont.h"
|
|
|
|
#include "CCAction.h"
|
|
|
|
#include "CCActionInterval.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_BEGIN
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
kZoomActionTag = 0xCCCB0001,
|
|
|
|
};
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlButton::ControlButton()
|
2013-07-29 14:07:57 +08:00
|
|
|
: _isPushed(false)
|
|
|
|
, _parentInited(false)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _doesAdjustBackgroundImage(false)
|
2013-07-29 14:07:57 +08:00
|
|
|
, _currentTitleColor(Color3B::WHITE)
|
2013-12-18 15:40:31 +08:00
|
|
|
, _titleLabel(nullptr)
|
|
|
|
, _backgroundSprite(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _zoomOnTouchDown(false)
|
2013-06-20 14:15:53 +08:00
|
|
|
, _marginV(ControlButtonMarginTB)
|
|
|
|
, _marginH(ControlButtonMarginLR)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlButton::~ControlButton()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_titleLabel);
|
|
|
|
CC_SAFE_RELEASE(_backgroundSprite);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//initialisers
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ControlButton::init()
|
2012-06-02 03:29:48 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
return this->initWithLabelAndBackgroundSprite(LabelTTF::create("", "Helvetica", 12), Scale9Sprite::create());
|
2012-06-02 03:29:48 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ControlButton::initWithLabelAndBackgroundSprite(Node* node, Scale9Sprite* backgroundSprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
if (Control::init())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 15:40:31 +08:00
|
|
|
CCASSERT(node != nullptr, "Label must not be nil.");
|
2013-06-20 14:15:53 +08:00
|
|
|
LabelProtocol* label = dynamic_cast<LabelProtocol*>(node);
|
2013-12-18 15:40:31 +08:00
|
|
|
CCASSERT(backgroundSprite != nullptr, "Background sprite must not be nil.");
|
2013-12-19 17:05:59 +08:00
|
|
|
CCASSERT(label != nullptr || backgroundSprite != nullptr, "");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_parentInited = true;
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_isPushed = false;
|
|
|
|
_zoomOnTouchDown = true;
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Adjust the background image by default
|
2012-09-25 16:57:51 +08:00
|
|
|
setAdjustBackgroundImage(true);
|
2013-07-12 14:47:36 +08:00
|
|
|
setPreferredSize(Size::ZERO);
|
2012-06-01 18:07:45 +08:00
|
|
|
// Zooming button by default
|
2013-06-15 14:03:30 +08:00
|
|
|
_zoomOnTouchDown = true;
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Set the default anchor point
|
2012-06-15 15:10:40 +08:00
|
|
|
ignoreAnchorPointForPosition(false);
|
2013-07-12 14:11:55 +08:00
|
|
|
setAnchorPoint(Point(0.5f, 0.5f));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Set the nodes
|
2012-09-25 16:57:51 +08:00
|
|
|
setTitleLabel(node);
|
|
|
|
setBackgroundSprite(backgroundSprite);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Set the default color and opacity
|
2013-07-05 16:49:22 +08:00
|
|
|
setColor(Color3B(255.0f, 255.0f, 255.0f));
|
2012-09-25 16:57:51 +08:00
|
|
|
setOpacity(255.0f);
|
2012-06-15 15:10:40 +08:00
|
|
|
setOpacityModifyRGB(true);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Initialize the dispatch table
|
|
|
|
|
2013-12-18 15:40:31 +08:00
|
|
|
setTitleForState(label->getString(), Control::State::NORMAL);
|
2013-12-06 18:07:16 +08:00
|
|
|
setTitleColorForState(node->getColor(), Control::State::NORMAL);
|
2013-07-26 14:37:26 +08:00
|
|
|
setTitleLabelForState(node, Control::State::NORMAL);
|
|
|
|
setBackgroundSpriteForState(backgroundSprite, Control::State::NORMAL);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-12 14:11:55 +08:00
|
|
|
setLabelAnchorPoint(Point(0.5f, 0.5f));
|
2012-06-02 06:10:20 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Layout update
|
|
|
|
needsLayout();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
//couldn't init the Control
|
2012-04-19 14:35:52 +08:00
|
|
|
else
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlButton* ControlButton::create(Node* label, Scale9Sprite* backgroundSprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlButton *pRet = new ControlButton();
|
2012-04-19 14:35:52 +08:00
|
|
|
pRet->initWithLabelAndBackgroundSprite(label, backgroundSprite);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-11-22 05:43:59 +08:00
|
|
|
bool ControlButton::initWithTitleAndFontNameAndFontSize(const std::string& title, const std::string& fontName, float fontSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-22 05:43:59 +08:00
|
|
|
LabelTTF *label = LabelTTF::create(title, fontName, fontSize);
|
2013-06-20 14:15:53 +08:00
|
|
|
return initWithLabelAndBackgroundSprite(label, Scale9Sprite::create());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-22 05:43:59 +08:00
|
|
|
ControlButton* ControlButton::create(const std::string& title, const std::string& fontName, float fontSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlButton *pRet = new ControlButton();
|
2012-04-19 14:35:52 +08:00
|
|
|
pRet->initWithTitleAndFontNameAndFontSize(title, fontName, fontSize);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ControlButton::initWithBackgroundSprite(Scale9Sprite* sprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
LabelTTF *label = LabelTTF::create("", "Arial", 30);//
|
2012-04-19 14:35:52 +08:00
|
|
|
return initWithLabelAndBackgroundSprite(label, sprite);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlButton* ControlButton::create(Scale9Sprite* sprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlButton *pRet = new ControlButton();
|
2012-04-19 14:35:52 +08:00
|
|
|
pRet->initWithBackgroundSprite(sprite);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlButton::setMargins(int marginH, int marginV)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_marginV = marginV;
|
|
|
|
_marginH = marginH;
|
2012-04-19 14:35:52 +08:00
|
|
|
needsLayout();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlButton::setEnabled(bool enabled)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Control::setEnabled(enabled);
|
2012-04-19 14:35:52 +08:00
|
|
|
needsLayout();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlButton::setSelected(bool enabled)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Control::setSelected(enabled);
|
2012-04-19 14:35:52 +08:00
|
|
|
needsLayout();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlButton::setHighlighted(bool enabled)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-03-05 16:26:37 +08:00
|
|
|
if (enabled == true)
|
|
|
|
{
|
2013-07-26 14:37:26 +08:00
|
|
|
_state = Control::State::HIGH_LIGHTED;
|
2013-03-05 16:26:37 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-26 14:37:26 +08:00
|
|
|
_state = Control::State::NORMAL;
|
2013-03-05 16:26:37 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Control::setHighlighted(enabled);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Action *action = getActionByTag(kZoomActionTag);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (action)
|
|
|
|
{
|
|
|
|
stopAction(action);
|
|
|
|
}
|
|
|
|
needsLayout();
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _zoomOnTouchDown )
|
2012-06-01 18:07:45 +08:00
|
|
|
{
|
2012-06-15 16:47:30 +08:00
|
|
|
float scaleValue = (isHighlighted() && isEnabled() && !isSelected()) ? 1.1f : 1.0f;
|
2013-06-20 14:15:53 +08:00
|
|
|
Action *zoomAction = ScaleTo::create(0.05f, scaleValue);
|
2012-06-01 18:07:45 +08:00
|
|
|
zoomAction->setTag(kZoomActionTag);
|
|
|
|
runAction(zoomAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlButton::setZoomOnTouchDown(bool zoomOnTouchDown)
|
2012-06-01 18:07:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_zoomOnTouchDown = zoomOnTouchDown;
|
2012-06-01 18:07:45 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ControlButton::getZoomOnTouchDown()
|
2012-06-01 18:07:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _zoomOnTouchDown;
|
2012-06-01 18:07:45 +08:00
|
|
|
}
|
|
|
|
|
2013-11-22 05:43:59 +08:00
|
|
|
void ControlButton::setPreferredSize(const Size& size)
|
2012-06-01 18:07:45 +08:00
|
|
|
{
|
|
|
|
if(size.width == 0 && size.height == 0)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_doesAdjustBackgroundImage = true;
|
2012-06-01 18:07:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_doesAdjustBackgroundImage = false;
|
2013-12-18 15:40:31 +08:00
|
|
|
|
|
|
|
for (auto iter = _backgroundSpriteDispatchTable.begin(); iter != _backgroundSpriteDispatchTable.end(); ++iter)
|
2012-06-01 18:07:45 +08:00
|
|
|
{
|
2013-12-18 15:40:31 +08:00
|
|
|
iter->second->setPreferredSize(size);
|
2012-06-01 18:07:45 +08:00
|
|
|
}
|
|
|
|
}
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_preferredSize = size;
|
2012-06-01 18:07:45 +08:00
|
|
|
needsLayout();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-22 05:43:59 +08:00
|
|
|
const Size& ControlButton::getPreferredSize() const
|
2012-06-01 18:07:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _preferredSize;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-06-01 18:07:45 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlButton::setAdjustBackgroundImage(bool adjustBackgroundImage)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_doesAdjustBackgroundImage=adjustBackgroundImage;
|
2012-04-19 14:35:52 +08:00
|
|
|
needsLayout();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ControlButton::doesAdjustBackgroundImage()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _doesAdjustBackgroundImage;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-22 05:43:59 +08:00
|
|
|
const Point& ControlButton::getLabelAnchorPoint() const
|
2012-06-02 06:10:20 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return this->_labelAnchorPoint;
|
2012-06-02 06:10:20 +08:00
|
|
|
}
|
|
|
|
|
2013-11-22 05:43:59 +08:00
|
|
|
void ControlButton::setLabelAnchorPoint(const Point& labelAnchorPoint)
|
2012-06-02 06:10:20 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_labelAnchorPoint = labelAnchorPoint;
|
2013-12-18 15:40:31 +08:00
|
|
|
if (_titleLabel != nullptr)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_titleLabel->setAnchorPoint(labelAnchorPoint);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2012-06-02 06:10:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 15:40:31 +08:00
|
|
|
std::string ControlButton::getTitleForState(State state)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 15:40:31 +08:00
|
|
|
auto iter = _titleDispatchTable.find((int)state);
|
|
|
|
if (iter != _titleDispatchTable.end())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 15:40:31 +08:00
|
|
|
return iter->second;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-18 15:40:31 +08:00
|
|
|
|
|
|
|
iter = _titleDispatchTable.find((int)Control::State::NORMAL);
|
|
|
|
|
|
|
|
return iter != _titleDispatchTable.end() ? iter->second : "";
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 15:40:31 +08:00
|
|
|
void ControlButton::setTitleForState(const std::string& title, State state)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 15:40:31 +08:00
|
|
|
_titleDispatchTable.erase((int)state);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 15:40:31 +08:00
|
|
|
if (!title.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 15:40:31 +08:00
|
|
|
_titleDispatchTable[(int)state] = title;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the current state if equal to the given state we update the layout
|
|
|
|
if (getState() == state)
|
|
|
|
{
|
|
|
|
needsLayout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
Color3B ControlButton::getTitleColorForState(State state) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-08 18:11:32 +08:00
|
|
|
Color3B returnColor = Color3B::WHITE;
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-12-18 15:40:31 +08:00
|
|
|
auto iter = _titleColorDispatchTable.find((int)state);
|
|
|
|
if (iter != _titleColorDispatchTable.end())
|
|
|
|
{
|
|
|
|
returnColor = iter->second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
iter = _titleColorDispatchTable.find((int)Control::State::NORMAL);
|
|
|
|
if (iter != _titleColorDispatchTable.end())
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-12-18 15:40:31 +08:00
|
|
|
returnColor = iter->second;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2013-12-18 15:40:31 +08:00
|
|
|
}
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return returnColor;
|
|
|
|
}
|
|
|
|
|
2013-11-22 05:43:59 +08:00
|
|
|
void ControlButton::setTitleColorForState(const Color3B& color, State state)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 15:40:31 +08:00
|
|
|
_titleColorDispatchTable.erase((int)state);
|
|
|
|
_titleColorDispatchTable[(int)state] = color;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// If the current state if equal to the given state we update the layout
|
|
|
|
if (getState() == state)
|
|
|
|
{
|
|
|
|
needsLayout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
Node* ControlButton::getTitleLabelForState(State state)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 15:40:31 +08:00
|
|
|
Node* titleLabel = _titleLabelDispatchTable.at((int)state);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (titleLabel)
|
|
|
|
{
|
|
|
|
return titleLabel;
|
|
|
|
}
|
2013-12-18 15:40:31 +08:00
|
|
|
return _titleLabelDispatchTable.at((int)Control::State::NORMAL);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
void ControlButton::setTitleLabelForState(Node* titleLabel, State state)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 15:40:31 +08:00
|
|
|
Node* previousLabel = _titleLabelDispatchTable.at((int)state);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (previousLabel)
|
|
|
|
{
|
|
|
|
removeChild(previousLabel, true);
|
2013-12-18 15:40:31 +08:00
|
|
|
_titleLabelDispatchTable.erase((int)state);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 15:40:31 +08:00
|
|
|
_titleLabelDispatchTable.insert((int)state, titleLabel);
|
2012-06-15 15:10:40 +08:00
|
|
|
titleLabel->setVisible(false);
|
2013-07-12 14:11:55 +08:00
|
|
|
titleLabel->setAnchorPoint(Point(0.5f, 0.5f));
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(titleLabel, 1);
|
|
|
|
|
|
|
|
// If the current state if equal to the given state we update the layout
|
|
|
|
if (getState() == state)
|
|
|
|
{
|
|
|
|
needsLayout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-15 09:19:16 +08:00
|
|
|
void ControlButton::setTitleTTFForState(const std::string& fntFile, State state)
|
2012-06-02 03:29:48 +08:00
|
|
|
{
|
2013-12-18 15:40:31 +08:00
|
|
|
std::string title = this->getTitleForState(state);
|
|
|
|
this->setTitleLabelForState(LabelTTF::create(title, fntFile, 12), state);
|
2012-06-02 03:29:48 +08:00
|
|
|
}
|
|
|
|
|
2013-11-15 09:19:16 +08:00
|
|
|
const std::string& ControlButton::getTitleTTFForState(State state)
|
2012-06-02 03:29:48 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
LabelProtocol* label = dynamic_cast<LabelProtocol*>(this->getTitleLabelForState(state));
|
|
|
|
LabelTTF* labelTTF = dynamic_cast<LabelTTF*>(label);
|
2012-06-02 03:29:48 +08:00
|
|
|
if(labelTTF != 0)
|
|
|
|
{
|
2013-11-15 09:19:16 +08:00
|
|
|
return labelTTF->getFontName();
|
2012-06-02 03:29:48 +08:00
|
|
|
}
|
2013-11-15 09:19:16 +08:00
|
|
|
|
|
|
|
static std::string ret("");
|
|
|
|
return ret;
|
2012-06-02 03:29:48 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
void ControlButton::setTitleTTFSizeForState(float size, State state)
|
2012-06-02 03:29:48 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
LabelProtocol* label = dynamic_cast<LabelProtocol*>(this->getTitleLabelForState(state));
|
2012-06-02 03:29:48 +08:00
|
|
|
if(label)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
LabelTTF* labelTTF = dynamic_cast<LabelTTF*>(label);
|
2012-06-02 03:29:48 +08:00
|
|
|
if(labelTTF != 0)
|
|
|
|
{
|
|
|
|
return labelTTF->setFontSize(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
float ControlButton::getTitleTTFSizeForState(State state)
|
2012-06-02 03:29:48 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
LabelProtocol* label = dynamic_cast<LabelProtocol*>(this->getTitleLabelForState(state));
|
|
|
|
LabelTTF* labelTTF = dynamic_cast<LabelTTF*>(label);
|
2012-06-02 03:29:48 +08:00
|
|
|
if(labelTTF != 0)
|
|
|
|
{
|
|
|
|
return labelTTF->getFontSize();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-15 09:19:16 +08:00
|
|
|
void ControlButton::setTitleBMFontForState(const std::string& fntFile, State state)
|
2012-06-02 03:29:48 +08:00
|
|
|
{
|
2013-12-18 15:40:31 +08:00
|
|
|
std::string title = this->getTitleForState(state);
|
|
|
|
this->setTitleLabelForState(LabelBMFont::create(title, fntFile), state);
|
2012-06-02 03:29:48 +08:00
|
|
|
}
|
|
|
|
|
2013-11-15 09:19:16 +08:00
|
|
|
const std::string& ControlButton::getTitleBMFontForState(State state)
|
2012-06-02 03:29:48 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
LabelProtocol* label = dynamic_cast<LabelProtocol*>(this->getTitleLabelForState(state));
|
|
|
|
LabelBMFont* labelBMFont = dynamic_cast<LabelBMFont*>(label);
|
2012-06-02 03:29:48 +08:00
|
|
|
if(labelBMFont != 0)
|
|
|
|
{
|
|
|
|
return labelBMFont->getFntFile();
|
|
|
|
}
|
2013-11-15 09:19:16 +08:00
|
|
|
|
|
|
|
static std::string ret("");
|
|
|
|
return ret;
|
2012-06-02 03:29:48 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
Scale9Sprite* ControlButton::getBackgroundSpriteForState(State state)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 15:40:31 +08:00
|
|
|
auto backgroundSprite = _backgroundSpriteDispatchTable.at((int)state);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (backgroundSprite)
|
|
|
|
{
|
|
|
|
return backgroundSprite;
|
|
|
|
}
|
2013-12-18 15:40:31 +08:00
|
|
|
return _backgroundSpriteDispatchTable.at((int)Control::State::NORMAL);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
void ControlButton::setBackgroundSpriteForState(Scale9Sprite* sprite, State state)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Size oldPreferredSize = _preferredSize;
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-12-18 15:40:31 +08:00
|
|
|
auto previousBackgroundSprite = _backgroundSpriteDispatchTable.at((int)state);
|
2012-09-25 16:57:51 +08:00
|
|
|
if (previousBackgroundSprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
removeChild(previousBackgroundSprite, true);
|
2013-12-18 15:40:31 +08:00
|
|
|
_backgroundSpriteDispatchTable.erase((int)state);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 15:40:31 +08:00
|
|
|
_backgroundSpriteDispatchTable.insert((int)state, sprite);
|
2012-06-15 15:10:40 +08:00
|
|
|
sprite->setVisible(false);
|
2013-07-12 14:11:55 +08:00
|
|
|
sprite->setAnchorPoint(Point(0.5f, 0.5f));
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(sprite);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (this->_preferredSize.width != 0 || this->_preferredSize.height != 0)
|
2012-06-02 03:29:48 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (oldPreferredSize.equals(_preferredSize))
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2012-09-25 17:26:09 +08:00
|
|
|
// Force update of preferred size
|
2013-07-12 14:30:26 +08:00
|
|
|
sprite->setPreferredSize(Size(oldPreferredSize.width+1, oldPreferredSize.height+1));
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
sprite->setPreferredSize(this->_preferredSize);
|
2012-06-02 03:29:48 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// If the current state if equal to the given state we update the layout
|
|
|
|
if (getState() == state)
|
|
|
|
{
|
|
|
|
needsLayout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
void ControlButton::setBackgroundSpriteFrameForState(SpriteFrame * spriteFrame, State state)
|
2012-06-02 03:29:48 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Scale9Sprite * sprite = Scale9Sprite::createWithSpriteFrame(spriteFrame);
|
2012-06-02 03:29:48 +08:00
|
|
|
this->setBackgroundSpriteForState(sprite, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlButton::needsLayout()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_parentInited) {
|
2012-09-25 16:57:51 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
// Hide the background and the label
|
2013-12-18 15:40:31 +08:00
|
|
|
if (_titleLabel != nullptr) {
|
2013-06-15 14:03:30 +08:00
|
|
|
_titleLabel->setVisible(false);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_backgroundSprite) {
|
|
|
|
_backgroundSprite->setVisible(false);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2012-06-02 06:10:20 +08:00
|
|
|
// Update anchor of all labels
|
2013-06-15 14:03:30 +08:00
|
|
|
this->setLabelAnchorPoint(this->_labelAnchorPoint);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Update the label to match with the current state
|
2013-06-15 14:03:30 +08:00
|
|
|
_currentTitle = getTitleForState(_state);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_currentTitleColor = getTitleColorForState(_state);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
this->setTitleLabel(getTitleLabelForState(_state));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
LabelProtocol* label = dynamic_cast<LabelProtocol*>(_titleLabel);
|
2013-12-18 15:40:31 +08:00
|
|
|
if (label && !_currentTitle.empty())
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-12-18 15:40:31 +08:00
|
|
|
label->setString(_currentTitle);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-12-06 18:07:16 +08:00
|
|
|
if (_titleLabel)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-12-06 18:07:16 +08:00
|
|
|
_titleLabel->setColor(_currentTitleColor);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2013-12-18 15:40:31 +08:00
|
|
|
if (_titleLabel != nullptr)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-07-12 14:11:55 +08:00
|
|
|
_titleLabel->setPosition(Point (getContentSize().width / 2, getContentSize().height / 2));
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Update the background sprite
|
2013-06-15 14:03:30 +08:00
|
|
|
this->setBackgroundSprite(this->getBackgroundSpriteForState(_state));
|
2013-12-18 15:40:31 +08:00
|
|
|
if (_backgroundSprite != nullptr)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-07-12 14:11:55 +08:00
|
|
|
_backgroundSprite->setPosition(Point (getContentSize().width / 2, getContentSize().height / 2));
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Get the title label size
|
2013-06-20 14:15:53 +08:00
|
|
|
Size titleLabelSize;
|
2013-12-18 15:40:31 +08:00
|
|
|
if (_titleLabel != nullptr)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-07-17 09:16:04 +08:00
|
|
|
titleLabelSize = _titleLabel->getBoundingBox().size;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Adjust the background image if necessary
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_doesAdjustBackgroundImage)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Add the margins
|
2013-12-18 15:40:31 +08:00
|
|
|
if (_backgroundSprite != nullptr)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-07-12 14:30:26 +08:00
|
|
|
_backgroundSprite->setContentSize(Size(titleLabelSize.width + _marginH * 2, titleLabelSize.height + _marginV * 2));
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//TODO: should this also have margins if one of the preferred sizes is relaxed?
|
2013-12-18 15:40:31 +08:00
|
|
|
if (_backgroundSprite != nullptr)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Size preferredSize = _backgroundSprite->getPreferredSize();
|
2012-09-25 16:57:51 +08:00
|
|
|
if (preferredSize.width <= 0)
|
|
|
|
{
|
|
|
|
preferredSize.width = titleLabelSize.width;
|
|
|
|
}
|
|
|
|
if (preferredSize.height <= 0)
|
|
|
|
{
|
|
|
|
preferredSize.height = titleLabelSize.height;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_backgroundSprite->setContentSize(preferredSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the content size
|
2013-06-20 14:15:53 +08:00
|
|
|
Rect rectTitle;
|
2013-12-18 15:40:31 +08:00
|
|
|
if (_titleLabel != nullptr)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-07-17 09:16:04 +08:00
|
|
|
rectTitle = _titleLabel->getBoundingBox();
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
Rect rectBackground;
|
2013-12-18 15:40:31 +08:00
|
|
|
if (_backgroundSprite != nullptr)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-07-17 09:16:04 +08:00
|
|
|
rectBackground = _backgroundSprite->getBoundingBox();
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Rect maxRect = ControlUtils::RectUnion(rectTitle, rectBackground);
|
2013-07-12 14:30:26 +08:00
|
|
|
setContentSize(Size(maxRect.size.width, maxRect.size.height));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 15:40:31 +08:00
|
|
|
if (_titleLabel != nullptr)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-07-12 14:11:55 +08:00
|
|
|
_titleLabel->setPosition(Point(getContentSize().width/2, getContentSize().height/2));
|
2012-09-25 16:57:51 +08:00
|
|
|
// Make visible the background and the label
|
2013-06-15 14:03:30 +08:00
|
|
|
_titleLabel->setVisible(true);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 15:40:31 +08:00
|
|
|
if (_backgroundSprite != nullptr)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-07-12 14:11:55 +08:00
|
|
|
_backgroundSprite->setPosition(Point(getContentSize().width/2, getContentSize().height/2));
|
2013-06-15 14:03:30 +08:00
|
|
|
_backgroundSprite->setVisible(true);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
bool ControlButton::onTouchBegan(Touch *pTouch, Event *pEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-04-09 02:57:53 +08:00
|
|
|
if (!isTouchInside(pTouch) || !isEnabled() || !isVisible() || !hasVisibleParents() )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-18 15:40:31 +08:00
|
|
|
for (Node *c = this->_parent; c != nullptr; c = c->getParent())
|
2013-03-19 20:56:38 +08:00
|
|
|
{
|
|
|
|
if (c->isVisible() == false)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_isPushed = true;
|
2012-06-15 16:47:30 +08:00
|
|
|
this->setHighlighted(true);
|
2013-07-26 14:37:26 +08:00
|
|
|
sendActionsForControlEvents(Control::EventType::TOUCH_DOWN);
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void ControlButton::onTouchMoved(Touch *pTouch, Event *pEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
if (!isEnabled() || !isPushed() || isSelected())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
if (isHighlighted())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-15 16:47:30 +08:00
|
|
|
setHighlighted(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isTouchMoveInside = isTouchInside(pTouch);
|
2012-09-25 16:57:51 +08:00
|
|
|
if (isTouchMoveInside && !isHighlighted())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-15 16:47:30 +08:00
|
|
|
setHighlighted(true);
|
2013-07-26 14:37:26 +08:00
|
|
|
sendActionsForControlEvents(Control::EventType::DRAG_ENTER);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-09-25 16:57:51 +08:00
|
|
|
else if (isTouchMoveInside && isHighlighted())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 14:37:26 +08:00
|
|
|
sendActionsForControlEvents(Control::EventType::DRAG_INSIDE);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-09-25 16:57:51 +08:00
|
|
|
else if (!isTouchMoveInside && isHighlighted())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-15 16:47:30 +08:00
|
|
|
setHighlighted(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
sendActionsForControlEvents(Control::EventType::DRAG_EXIT);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-09-25 16:57:51 +08:00
|
|
|
else if (!isTouchMoveInside && !isHighlighted())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 14:37:26 +08:00
|
|
|
sendActionsForControlEvents(Control::EventType::DRAG_OUTSIDE);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2013-09-03 18:22:03 +08:00
|
|
|
void ControlButton::onTouchEnded(Touch *pTouch, Event *pEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_isPushed = false;
|
2012-06-15 16:47:30 +08:00
|
|
|
setHighlighted(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
if (isTouchInside(pTouch))
|
|
|
|
{
|
2013-07-26 14:37:26 +08:00
|
|
|
sendActionsForControlEvents(Control::EventType::TOUCH_UP_INSIDE);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-26 14:37:26 +08:00
|
|
|
sendActionsForControlEvents(Control::EventType::TOUCH_UP_OUTSIDE);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ControlButton::setOpacity(GLubyte opacity)
|
2012-06-07 17:20:30 +08:00
|
|
|
{
|
2013-02-28 11:55:36 +08:00
|
|
|
// XXX fixed me if not correct
|
2013-06-20 14:15:53 +08:00
|
|
|
Control::setOpacity(opacity);
|
2013-06-15 14:03:30 +08:00
|
|
|
// _opacity = opacity;
|
2013-02-28 11:55:36 +08:00
|
|
|
//
|
2013-06-20 14:15:53 +08:00
|
|
|
// Object* child;
|
|
|
|
// Array* children=getChildren();
|
2013-02-28 11:55:36 +08:00
|
|
|
// CCARRAY_FOREACH(children, child)
|
|
|
|
// {
|
2013-06-20 14:15:53 +08:00
|
|
|
// RGBAProtocol* pNode = dynamic_cast<RGBAProtocol*>(child);
|
2013-02-28 11:55:36 +08:00
|
|
|
// if (pNode)
|
|
|
|
// {
|
|
|
|
// pNode->setOpacity(opacity);
|
|
|
|
// }
|
|
|
|
// }
|
2013-12-18 15:40:31 +08:00
|
|
|
|
|
|
|
for (auto iter = _backgroundSpriteDispatchTable.begin(); iter != _backgroundSpriteDispatchTable.end(); ++iter)
|
2012-06-07 17:20:30 +08:00
|
|
|
{
|
2013-12-18 15:40:31 +08:00
|
|
|
iter->second->setOpacity(opacity);
|
2012-06-07 17:20:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 15:06:38 +08:00
|
|
|
GLubyte ControlButton::getOpacity() const
|
2012-06-07 17:20:30 +08:00
|
|
|
{
|
2013-02-28 11:55:36 +08:00
|
|
|
return _realOpacity;
|
2012-06-07 17:20:30 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void ControlButton::setColor(const Color3B & color)
|
2013-05-08 04:48:58 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Control::setColor(color);
|
2013-05-08 04:48:58 +08:00
|
|
|
|
2013-12-18 15:40:31 +08:00
|
|
|
for (auto iter = _backgroundSpriteDispatchTable.begin(); iter != _backgroundSpriteDispatchTable.end(); ++iter)
|
2013-05-08 04:48:58 +08:00
|
|
|
{
|
2013-12-18 15:40:31 +08:00
|
|
|
iter->second->setColor(color);
|
2013-05-08 04:48:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 17:32:50 +08:00
|
|
|
const Color3B& ControlButton::getColor() const
|
2013-05-08 04:48:58 +08:00
|
|
|
{
|
|
|
|
return _realColor;
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void ControlButton::onTouchCancelled(Touch *pTouch, Event *pEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_isPushed = false;
|
2012-06-15 16:47:30 +08:00
|
|
|
setHighlighted(false);
|
2013-07-26 14:37:26 +08:00
|
|
|
sendActionsForControlEvents(Control::EventType::TOUCH_CANCEL);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlButton* ControlButton::create()
|
2012-06-02 03:29:48 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlButton *pControlButton = new ControlButton();
|
2012-06-02 03:29:48 +08:00
|
|
|
if (pControlButton && pControlButton->init())
|
|
|
|
{
|
|
|
|
pControlButton->autorelease();
|
|
|
|
return pControlButton;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pControlButton);
|
2013-12-18 15:40:31 +08:00
|
|
|
return nullptr;
|
2012-06-02 03:29:48 +08:00
|
|
|
}
|
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_END
|