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
|
|
|
, _currentTitle(NULL)
|
|
|
|
, _currentTitleColor(Color3B::WHITE)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _titleLabel(NULL)
|
|
|
|
, _backgroundSprite(NULL)
|
|
|
|
, _zoomOnTouchDown(false)
|
|
|
|
, _titleDispatchTable(NULL)
|
|
|
|
, _titleColorDispatchTable(NULL)
|
|
|
|
, _titleLabelDispatchTable(NULL)
|
|
|
|
, _backgroundSpriteDispatchTable(NULL)
|
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(_currentTitle);
|
|
|
|
CC_SAFE_RELEASE(_titleLabel);
|
|
|
|
CC_SAFE_RELEASE(_backgroundSpriteDispatchTable);
|
|
|
|
CC_SAFE_RELEASE(_titleLabelDispatchTable);
|
|
|
|
CC_SAFE_RELEASE(_titleColorDispatchTable);
|
|
|
|
CC_SAFE_RELEASE(_titleDispatchTable);
|
|
|
|
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-07-20 13:01:27 +08:00
|
|
|
CCASSERT(node != NULL, "Label must not be nil.");
|
2013-06-20 14:15:53 +08:00
|
|
|
LabelProtocol* label = dynamic_cast<LabelProtocol*>(node);
|
|
|
|
RGBAProtocol* rgbaLabel = dynamic_cast<RGBAProtocol*>(node);
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(backgroundSprite != NULL, "Background sprite must not be nil.");
|
|
|
|
CCASSERT(label != NULL || rgbaLabel!=NULL || backgroundSprite != NULL, "");
|
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
|
|
|
|
|
|
|
// Initialize the button state tables
|
2013-06-20 14:15:53 +08:00
|
|
|
this->setTitleDispatchTable(Dictionary::create());
|
|
|
|
this->setTitleColorDispatchTable(Dictionary::create());
|
|
|
|
this->setTitleLabelDispatchTable(Dictionary::create());
|
|
|
|
this->setBackgroundSpriteDispatchTable(Dictionary::create());
|
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
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_currentTitle=NULL;
|
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-06-20 14:15:53 +08:00
|
|
|
String* tempString = String::create(label->getString());
|
2012-04-19 14:35:52 +08:00
|
|
|
//tempString->autorelease();
|
2013-07-26 14:37:26 +08:00
|
|
|
setTitleForState(tempString, Control::State::NORMAL);
|
|
|
|
setTitleColorForState(rgbaLabel->getColor(), Control::State::NORMAL);
|
|
|
|
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-06-20 14:15:53 +08:00
|
|
|
bool ControlButton::initWithTitleAndFontNameAndFontSize(string title, const char * fontName, float fontSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
LabelTTF *label = LabelTTF::create(title.c_str(), fontName, fontSize);
|
|
|
|
return initWithLabelAndBackgroundSprite(label, Scale9Sprite::create());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ControlButton* ControlButton::create(string title, const char * 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-06-20 14:15:53 +08:00
|
|
|
void ControlButton::setPreferredSize(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-06-20 14:15:53 +08:00
|
|
|
DictElement * item = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCDICT_FOREACH(_backgroundSpriteDispatchTable, item)
|
2012-06-01 18:07:45 +08:00
|
|
|
{
|
2013-07-09 14:40:43 +08:00
|
|
|
Scale9Sprite* sprite = static_cast<Scale9Sprite*>(item->getObject());
|
2012-06-01 18:07:45 +08:00
|
|
|
sprite->setPreferredSize(size);
|
|
|
|
}
|
|
|
|
}
|
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-06-20 14:15:53 +08:00
|
|
|
Size ControlButton::getPreferredSize()
|
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-06-20 14:15:53 +08:00
|
|
|
Point ControlButton::getLabelAnchorPoint()
|
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-06-20 14:15:53 +08:00
|
|
|
void ControlButton::setLabelAnchorPoint(Point labelAnchorPoint)
|
2012-06-02 06:10:20 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_labelAnchorPoint = labelAnchorPoint;
|
|
|
|
if (_titleLabel != NULL)
|
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-07-26 14:37:26 +08:00
|
|
|
String* ControlButton::getTitleForState(State state)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_titleDispatchTable != NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 14:37:26 +08:00
|
|
|
String* title=(String*)_titleDispatchTable->objectForKey((int)state);
|
2012-09-25 16:57:51 +08:00
|
|
|
if (title)
|
|
|
|
{
|
|
|
|
return title;
|
|
|
|
}
|
2013-07-26 14:37:26 +08:00
|
|
|
return (String*)_titleDispatchTable->objectForKey((int)Control::State::NORMAL);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
return String::create("");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
void ControlButton::setTitleForState(String* title, State state)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 14:37:26 +08:00
|
|
|
_titleDispatchTable->removeObjectForKey((int)state);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (title)
|
|
|
|
{
|
2013-07-26 14:37:26 +08:00
|
|
|
_titleDispatchTable->setObject(title, (int)state);
|
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
|
|
|
do
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_BREAK_IF(NULL == _titleColorDispatchTable);
|
2013-07-26 14:37:26 +08:00
|
|
|
Color3bObject* colorObject=(Color3bObject*)_titleColorDispatchTable->objectForKey((int)state);
|
2012-09-25 16:57:51 +08:00
|
|
|
if (colorObject)
|
|
|
|
{
|
2013-03-05 16:26:37 +08:00
|
|
|
returnColor = colorObject->value;
|
2012-09-25 16:57:51 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
colorObject = (Color3bObject*)_titleColorDispatchTable->objectForKey((int)Control::State::NORMAL);
|
2012-09-25 16:57:51 +08:00
|
|
|
if (colorObject)
|
|
|
|
{
|
2013-03-05 16:26:37 +08:00
|
|
|
returnColor = colorObject->value;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return returnColor;
|
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
void ControlButton::setTitleColorForState(Color3B color, State state)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-05 16:49:22 +08:00
|
|
|
//Color3B* colorValue=&color;
|
2013-07-26 14:37:26 +08:00
|
|
|
_titleColorDispatchTable->removeObjectForKey((int)state);
|
2013-06-20 14:15:53 +08:00
|
|
|
Color3bObject* pColor3bObject = new Color3bObject(color);
|
2012-04-19 14:35:52 +08:00
|
|
|
pColor3bObject->autorelease();
|
2013-07-26 14:37:26 +08:00
|
|
|
_titleColorDispatchTable->setObject(pColor3bObject, (int)state);
|
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-07-26 14:37:26 +08:00
|
|
|
Node* titleLabel = (Node*)_titleLabelDispatchTable->objectForKey((int)state);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (titleLabel)
|
|
|
|
{
|
|
|
|
return titleLabel;
|
|
|
|
}
|
2013-07-26 14:37:26 +08:00
|
|
|
return (Node*)_titleLabelDispatchTable->objectForKey((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-07-26 14:37:26 +08:00
|
|
|
Node* previousLabel = (Node*)_titleLabelDispatchTable->objectForKey((int)state);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (previousLabel)
|
|
|
|
{
|
|
|
|
removeChild(previousLabel, true);
|
2013-07-26 14:37:26 +08:00
|
|
|
_titleLabelDispatchTable->removeObjectForKey((int)state);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
_titleLabelDispatchTable->setObject(titleLabel, (int)state);
|
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-07-26 14:37:26 +08:00
|
|
|
void ControlButton::setTitleTTFForState(const char * fntFile, State state)
|
2012-06-02 03:29:48 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
String * title = this->getTitleForState(state);
|
2012-09-25 16:57:51 +08:00
|
|
|
if (!title)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
title = String::create("");
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
this->setTitleLabelForState(LabelTTF::create(title->getCString(), fntFile, 12), state);
|
2012-06-02 03:29:48 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
const char * 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-07 09:05:13 +08:00
|
|
|
return labelTTF->getFontName().c_str();
|
2012-06-02 03:29:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-26 14:37:26 +08:00
|
|
|
void ControlButton::setTitleBMFontForState(const char * fntFile, State state)
|
2012-06-02 03:29:48 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
String * title = this->getTitleForState(state);
|
2012-09-25 16:57:51 +08:00
|
|
|
if (!title)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
title = String::create("");
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
this->setTitleLabelForState(LabelBMFont::create(title->getCString(), fntFile), state);
|
2012-06-02 03:29:48 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
const char * 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();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-26 14:37:26 +08:00
|
|
|
Scale9Sprite* backgroundSprite = (Scale9Sprite*)_backgroundSpriteDispatchTable->objectForKey((int)state);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (backgroundSprite)
|
|
|
|
{
|
|
|
|
return backgroundSprite;
|
|
|
|
}
|
2013-07-26 14:37:26 +08:00
|
|
|
return (Scale9Sprite*)_backgroundSpriteDispatchTable->objectForKey((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-07-26 14:37:26 +08:00
|
|
|
Scale9Sprite* previousBackgroundSprite = (Scale9Sprite*)_backgroundSpriteDispatchTable->objectForKey((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-07-26 14:37:26 +08:00
|
|
|
_backgroundSpriteDispatchTable->removeObjectForKey((int)state);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
_backgroundSpriteDispatchTable->setObject(sprite, (int)state);
|
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-06-15 14:03:30 +08:00
|
|
|
if (_titleLabel != NULL) {
|
|
|
|
_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
|
|
|
CC_SAFE_RELEASE(_currentTitle);
|
|
|
|
_currentTitle = getTitleForState(_state);
|
|
|
|
CC_SAFE_RETAIN(_currentTitle);
|
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-06-15 14:03:30 +08:00
|
|
|
if (label && _currentTitle)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
label->setString(_currentTitle->getCString());
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
RGBAProtocol* rgbaLabel = dynamic_cast<RGBAProtocol*>(_titleLabel);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (rgbaLabel)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
rgbaLabel->setColor(_currentTitleColor);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_titleLabel != NULL)
|
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));
|
|
|
|
if (_backgroundSprite != NULL)
|
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-06-15 14:03:30 +08:00
|
|
|
if (_titleLabel != NULL)
|
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-06-15 14:03:30 +08:00
|
|
|
if (_backgroundSprite != NULL)
|
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-06-15 14:03:30 +08:00
|
|
|
if (_backgroundSprite != NULL)
|
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-06-15 14:03:30 +08:00
|
|
|
if (_titleLabel != NULL)
|
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-06-15 14:03:30 +08:00
|
|
|
if (_backgroundSprite != NULL)
|
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-06-15 14:03:30 +08:00
|
|
|
if (_titleLabel != NULL)
|
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-06-15 14:03:30 +08:00
|
|
|
if (_backgroundSprite != NULL)
|
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-06-20 14:15:53 +08:00
|
|
|
for (Node *c = this->_parent; c != NULL; 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-06-20 14:15:53 +08:00
|
|
|
DictElement * item = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCDICT_FOREACH(_backgroundSpriteDispatchTable, item)
|
2012-06-07 17:20:30 +08:00
|
|
|
{
|
2013-07-09 14:40:43 +08:00
|
|
|
Scale9Sprite* sprite = static_cast<Scale9Sprite*>(item->getObject());
|
2012-06-07 17:20:30 +08:00
|
|
|
sprite->setOpacity(opacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-20 14:15:53 +08:00
|
|
|
DictElement * item = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCDICT_FOREACH(_backgroundSpriteDispatchTable, item)
|
2013-05-08 04:48:58 +08:00
|
|
|
{
|
2013-07-09 14:40:43 +08:00
|
|
|
Scale9Sprite* sprite = static_cast<Scale9Sprite*>(item->getObject());
|
2013-05-08 04:48:58 +08:00
|
|
|
sprite->setColor(color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_END
|