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"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "support/CCPointExtension.h"
|
|
|
|
#include "label_nodes/CCLabelTTF.h"
|
|
|
|
#include "label_nodes/CCLabelBMFont.h"
|
|
|
|
#include "actions/CCAction.h"
|
|
|
|
#include "actions/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,
|
|
|
|
};
|
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
CCControlButton::CCControlButton()
|
|
|
|
: m_currentTitle(NULL)
|
|
|
|
, m_currentTitleColor(ccWHITE)
|
|
|
|
, m_doesAdjustBackgroundImage(false)
|
|
|
|
, m_titleLabel(NULL)
|
|
|
|
, m_backgroundSprite(NULL)
|
|
|
|
, m_zoomOnTouchDown(false)
|
|
|
|
, m_isPushed(false)
|
|
|
|
, m_bParentInited(false)
|
|
|
|
, m_titleDispatchTable(NULL)
|
|
|
|
, m_titleColorDispatchTable(NULL)
|
|
|
|
, m_titleLabelDispatchTable(NULL)
|
|
|
|
, m_backgroundSpriteDispatchTable(NULL)
|
|
|
|
, m_marginH(CCControlButtonMarginLR)
|
|
|
|
, m_marginV(CCControlButtonMarginTB)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCControlButton::~CCControlButton()
|
|
|
|
{
|
2013-01-21 10:16:07 +08:00
|
|
|
CC_SAFE_RELEASE(m_currentTitle);
|
|
|
|
CC_SAFE_RELEASE(m_titleLabel);
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RELEASE(m_backgroundSpriteDispatchTable);
|
|
|
|
CC_SAFE_RELEASE(m_titleLabelDispatchTable);
|
|
|
|
CC_SAFE_RELEASE(m_titleColorDispatchTable);
|
|
|
|
CC_SAFE_RELEASE(m_titleDispatchTable);
|
2012-09-25 16:57:51 +08:00
|
|
|
CC_SAFE_RELEASE(m_backgroundSprite);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//initialisers
|
|
|
|
|
2012-06-02 03:29:48 +08:00
|
|
|
bool CCControlButton::init()
|
|
|
|
{
|
2012-06-20 11:48:31 +08:00
|
|
|
return this->initWithLabelAndBackgroundSprite(CCLabelTTF::create("", "Helvetica", 12), CCScale9Sprite::create());
|
2012-06-02 03:29:48 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
bool CCControlButton::initWithLabelAndBackgroundSprite(CCNode* node, CCScale9Sprite* backgroundSprite)
|
|
|
|
{
|
|
|
|
if (CCControl::init())
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
CCAssert(node != NULL, "Label must not be nil.");
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLabelProtocol* label = dynamic_cast<CCLabelProtocol*>(node);
|
|
|
|
CCRGBAProtocol* rgbaLabel = dynamic_cast<CCRGBAProtocol*>(node);
|
2012-09-25 16:57:51 +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
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
m_bParentInited = true;
|
|
|
|
|
|
|
|
// Initialize the button state tables
|
|
|
|
this->setTitleDispatchTable(CCDictionary::create());
|
|
|
|
this->setTitleColorDispatchTable(CCDictionary::create());
|
|
|
|
this->setTitleLabelDispatchTable(CCDictionary::create());
|
|
|
|
this->setBackgroundSpriteDispatchTable(CCDictionary::create());
|
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
setTouchEnabled(true);
|
2012-09-25 16:57:51 +08:00
|
|
|
m_isPushed = false;
|
2012-06-02 03:29:48 +08:00
|
|
|
m_zoomOnTouchDown = true;
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_currentTitle=NULL;
|
|
|
|
|
|
|
|
// Adjust the background image by default
|
2012-09-25 16:57:51 +08:00
|
|
|
setAdjustBackgroundImage(true);
|
|
|
|
setPreferredSize(CCSizeZero);
|
2012-06-01 18:07:45 +08:00
|
|
|
// Zooming button by default
|
|
|
|
m_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);
|
2012-04-19 14:35:52 +08:00
|
|
|
setAnchorPoint(ccp(0.5f, 0.5f));
|
|
|
|
|
|
|
|
// 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
|
2012-09-25 16:57:51 +08:00
|
|
|
setColor(ccc3(255.0f, 255.0f, 255.0f));
|
|
|
|
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
|
|
|
|
|
2012-06-14 16:05:58 +08:00
|
|
|
CCString* tempString = CCString::create(label->getString());
|
2012-04-19 14:35:52 +08:00
|
|
|
//tempString->autorelease();
|
|
|
|
setTitleForState(tempString, CCControlStateNormal);
|
|
|
|
setTitleColorForState(rgbaLabel->getColor(), CCControlStateNormal);
|
|
|
|
setTitleLabelForState(node, CCControlStateNormal);
|
|
|
|
setBackgroundSpriteForState(backgroundSprite, CCControlStateNormal);
|
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
setLabelAnchorPoint(ccp(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;
|
|
|
|
}
|
|
|
|
//couldn't init the CCControl
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCControlButton* CCControlButton::create(CCNode* label, CCScale9Sprite* backgroundSprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCControlButton *pRet = new CCControlButton();
|
|
|
|
pRet->initWithLabelAndBackgroundSprite(label, backgroundSprite);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCControlButton::initWithTitleAndFontNameAndFontSize(string title, const char * fontName, float fontSize)
|
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLabelTTF *label = CCLabelTTF::create(title.c_str(), fontName, fontSize);
|
|
|
|
return initWithLabelAndBackgroundSprite(label, CCScale9Sprite::create());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCControlButton* CCControlButton::create(string title, const char * fontName, float fontSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCControlButton *pRet = new CCControlButton();
|
|
|
|
pRet->initWithTitleAndFontNameAndFontSize(title, fontName, fontSize);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCControlButton::initWithBackgroundSprite(CCScale9Sprite* sprite)
|
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLabelTTF *label = CCLabelTTF::create("", "Arial", 30);//
|
2012-04-19 14:35:52 +08:00
|
|
|
return initWithLabelAndBackgroundSprite(label, sprite);
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCControlButton* CCControlButton::create(CCScale9Sprite* sprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCControlButton *pRet = new CCControlButton();
|
|
|
|
pRet->initWithBackgroundSprite(sprite);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CCControlButton::setMargins(int marginH, int marginV)
|
|
|
|
{
|
2013-03-05 16:26:37 +08:00
|
|
|
m_marginV = marginV;
|
|
|
|
m_marginH = marginH;
|
2012-04-19 14:35:52 +08:00
|
|
|
needsLayout();
|
|
|
|
}
|
|
|
|
|
2012-06-15 16:47:30 +08:00
|
|
|
void CCControlButton::setEnabled(bool enabled)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-15 16:47:30 +08:00
|
|
|
CCControl::setEnabled(enabled);
|
2012-04-19 14:35:52 +08:00
|
|
|
needsLayout();
|
|
|
|
}
|
|
|
|
|
2012-06-15 16:47:30 +08:00
|
|
|
void CCControlButton::setSelected(bool enabled)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-15 16:47:30 +08:00
|
|
|
CCControl::setSelected(enabled);
|
2012-04-19 14:35:52 +08:00
|
|
|
needsLayout();
|
|
|
|
}
|
|
|
|
|
2012-06-15 16:47:30 +08:00
|
|
|
void CCControlButton::setHighlighted(bool enabled)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-03-05 16:26:37 +08:00
|
|
|
if (enabled == true)
|
|
|
|
{
|
|
|
|
m_eState = CCControlStateHighlighted;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_eState = CCControlStateNormal;
|
|
|
|
}
|
|
|
|
|
2012-06-15 16:47:30 +08:00
|
|
|
CCControl::setHighlighted(enabled);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-03-05 16:26:37 +08:00
|
|
|
CCAction *action = getActionByTag(kZoomActionTag);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (action)
|
|
|
|
{
|
|
|
|
stopAction(action);
|
|
|
|
}
|
|
|
|
needsLayout();
|
2012-06-01 18:07:45 +08:00
|
|
|
if( m_zoomOnTouchDown )
|
|
|
|
{
|
2012-06-15 16:47:30 +08:00
|
|
|
float scaleValue = (isHighlighted() && isEnabled() && !isSelected()) ? 1.1f : 1.0f;
|
2013-03-05 16:26:37 +08:00
|
|
|
CCAction *zoomAction = CCScaleTo::create(0.05f, scaleValue);
|
2012-06-01 18:07:45 +08:00
|
|
|
zoomAction->setTag(kZoomActionTag);
|
|
|
|
runAction(zoomAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlButton::setZoomOnTouchDown(bool zoomOnTouchDown)
|
|
|
|
{
|
|
|
|
m_zoomOnTouchDown = zoomOnTouchDown;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCControlButton::getZoomOnTouchDown()
|
|
|
|
{
|
|
|
|
return m_zoomOnTouchDown;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlButton::setPreferredSize(CCSize size)
|
|
|
|
{
|
|
|
|
if(size.width == 0 && size.height == 0)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
m_doesAdjustBackgroundImage = true;
|
2012-06-01 18:07:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
m_doesAdjustBackgroundImage = false;
|
2012-06-01 18:07:45 +08:00
|
|
|
CCDictElement * item = NULL;
|
|
|
|
CCDICT_FOREACH(m_backgroundSpriteDispatchTable, item)
|
|
|
|
{
|
|
|
|
CCScale9Sprite* sprite = (CCScale9Sprite*)item->getObject();
|
|
|
|
sprite->setPreferredSize(size);
|
|
|
|
}
|
|
|
|
}
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
m_preferredSize = size;
|
2012-06-01 18:07:45 +08:00
|
|
|
needsLayout();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-01 18:07:45 +08:00
|
|
|
CCSize CCControlButton::getPreferredSize()
|
|
|
|
{
|
|
|
|
return m_preferredSize;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-06-01 18:07:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void CCControlButton::setAdjustBackgroundImage(bool adjustBackgroundImage)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
m_doesAdjustBackgroundImage=adjustBackgroundImage;
|
2012-04-19 14:35:52 +08:00
|
|
|
needsLayout();
|
|
|
|
}
|
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
bool CCControlButton::doesAdjustBackgroundImage()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
return m_doesAdjustBackgroundImage;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-06-02 06:10:20 +08:00
|
|
|
CCPoint CCControlButton::getLabelAnchorPoint()
|
|
|
|
{
|
|
|
|
return this->m_labelAnchorPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlButton::setLabelAnchorPoint(CCPoint labelAnchorPoint)
|
|
|
|
{
|
|
|
|
this->m_labelAnchorPoint = labelAnchorPoint;
|
2012-09-25 16:57:51 +08:00
|
|
|
if (m_titleLabel != NULL)
|
|
|
|
{
|
|
|
|
this->m_titleLabel->setAnchorPoint(labelAnchorPoint);
|
|
|
|
}
|
2012-06-02 06:10:20 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCString* CCControlButton::getTitleForState(CCControlState state)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
if (m_titleDispatchTable != NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
CCString* title=(CCString*)m_titleDispatchTable->objectForKey(state);
|
|
|
|
if (title)
|
|
|
|
{
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
return (CCString*)m_titleDispatchTable->objectForKey(CCControlStateNormal);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-09-25 16:57:51 +08:00
|
|
|
return CCString::create("");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlButton::setTitleForState(CCString* title, CCControlState state)
|
|
|
|
{
|
|
|
|
m_titleDispatchTable->removeObjectForKey(state);
|
|
|
|
|
|
|
|
if (title)
|
|
|
|
{
|
|
|
|
m_titleDispatchTable->setObject(title, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the current state if equal to the given state we update the layout
|
|
|
|
if (getState() == state)
|
|
|
|
{
|
|
|
|
needsLayout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const ccColor3B CCControlButton::getTitleColorForState(CCControlState state)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
ccColor3B returnColor = ccWHITE;
|
|
|
|
do
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
CC_BREAK_IF(NULL == m_titleColorDispatchTable);
|
|
|
|
CCColor3bObject* colorObject=(CCColor3bObject*)m_titleColorDispatchTable->objectForKey(state);
|
|
|
|
if (colorObject)
|
|
|
|
{
|
2013-03-05 16:26:37 +08:00
|
|
|
returnColor = colorObject->value;
|
2012-09-25 16:57:51 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-03-05 16:26:37 +08:00
|
|
|
colorObject = (CCColor3bObject*)m_titleColorDispatchTable->objectForKey(CCControlStateNormal);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlButton::setTitleColorForState(ccColor3B color, CCControlState state)
|
|
|
|
{
|
|
|
|
//ccColor3B* colorValue=&color;
|
|
|
|
m_titleColorDispatchTable->removeObjectForKey(state);
|
|
|
|
CCColor3bObject* pColor3bObject = new CCColor3bObject(color);
|
|
|
|
pColor3bObject->autorelease();
|
|
|
|
m_titleColorDispatchTable->setObject(pColor3bObject, state);
|
|
|
|
|
|
|
|
// If the current state if equal to the given state we update the layout
|
|
|
|
if (getState() == state)
|
|
|
|
{
|
|
|
|
needsLayout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CCNode* CCControlButton::getTitleLabelForState(CCControlState state)
|
|
|
|
{
|
2013-03-05 16:26:37 +08:00
|
|
|
CCNode* titleLabel = (CCNode*)m_titleLabelDispatchTable->objectForKey(state);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (titleLabel)
|
|
|
|
{
|
|
|
|
return titleLabel;
|
|
|
|
}
|
|
|
|
return (CCNode*)m_titleLabelDispatchTable->objectForKey(CCControlStateNormal);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlButton::setTitleLabelForState(CCNode* titleLabel, CCControlState state)
|
|
|
|
{
|
|
|
|
CCNode* previousLabel = (CCNode*)m_titleLabelDispatchTable->objectForKey(state);
|
|
|
|
if (previousLabel)
|
|
|
|
{
|
|
|
|
removeChild(previousLabel, true);
|
|
|
|
m_titleLabelDispatchTable->removeObjectForKey(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_titleLabelDispatchTable->setObject(titleLabel, state);
|
2012-06-15 15:10:40 +08:00
|
|
|
titleLabel->setVisible(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
titleLabel->setAnchorPoint(ccp(0.5f, 0.5f));
|
|
|
|
addChild(titleLabel, 1);
|
|
|
|
|
|
|
|
// If the current state if equal to the given state we update the layout
|
|
|
|
if (getState() == state)
|
|
|
|
{
|
|
|
|
needsLayout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-02 03:29:48 +08:00
|
|
|
void CCControlButton::setTitleTTFForState(const char * fntFile, CCControlState state)
|
|
|
|
{
|
|
|
|
CCString * title = this->getTitleForState(state);
|
2012-09-25 16:57:51 +08:00
|
|
|
if (!title)
|
|
|
|
{
|
|
|
|
title = CCString::create("");
|
|
|
|
}
|
2012-06-20 11:48:31 +08:00
|
|
|
this->setTitleLabelForState(CCLabelTTF::create(title->getCString(), fntFile, 12), state);
|
2012-06-02 03:29:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char * CCControlButton::getTitleTTFForState(CCControlState state)
|
|
|
|
{
|
|
|
|
CCLabelProtocol* label = dynamic_cast<CCLabelProtocol*>(this->getTitleLabelForState(state));
|
|
|
|
CCLabelTTF* labelTTF = dynamic_cast<CCLabelTTF*>(label);
|
|
|
|
if(labelTTF != 0)
|
|
|
|
{
|
|
|
|
return labelTTF->getFontName();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlButton::setTitleTTFSizeForState(float size, CCControlState state)
|
|
|
|
{
|
|
|
|
CCLabelProtocol* label = dynamic_cast<CCLabelProtocol*>(this->getTitleLabelForState(state));
|
|
|
|
if(label)
|
|
|
|
{
|
|
|
|
CCLabelTTF* labelTTF = dynamic_cast<CCLabelTTF*>(label);
|
|
|
|
if(labelTTF != 0)
|
|
|
|
{
|
|
|
|
return labelTTF->setFontSize(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float CCControlButton::getTitleTTFSizeForState(CCControlState state)
|
|
|
|
{
|
|
|
|
CCLabelProtocol* label = dynamic_cast<CCLabelProtocol*>(this->getTitleLabelForState(state));
|
|
|
|
CCLabelTTF* labelTTF = dynamic_cast<CCLabelTTF*>(label);
|
|
|
|
if(labelTTF != 0)
|
|
|
|
{
|
|
|
|
return labelTTF->getFontSize();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlButton::setTitleBMFontForState(const char * fntFile, CCControlState state)
|
|
|
|
{
|
|
|
|
CCString * title = this->getTitleForState(state);
|
2012-09-25 16:57:51 +08:00
|
|
|
if (!title)
|
|
|
|
{
|
|
|
|
title = CCString::create("");
|
|
|
|
}
|
2012-06-20 11:48:31 +08:00
|
|
|
this->setTitleLabelForState(CCLabelBMFont::create(title->getCString(), fntFile), state);
|
2012-06-02 03:29:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char * CCControlButton::getTitleBMFontForState(CCControlState state)
|
|
|
|
{
|
|
|
|
CCLabelProtocol* label = dynamic_cast<CCLabelProtocol*>(this->getTitleLabelForState(state));
|
|
|
|
CCLabelBMFont* labelBMFont = dynamic_cast<CCLabelBMFont*>(label);
|
|
|
|
if(labelBMFont != 0)
|
|
|
|
{
|
|
|
|
return labelBMFont->getFntFile();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CCScale9Sprite* CCControlButton::getBackgroundSpriteForState(CCControlState state)
|
|
|
|
{
|
2013-03-05 16:26:37 +08:00
|
|
|
CCScale9Sprite* backgroundSprite = (CCScale9Sprite*)m_backgroundSpriteDispatchTable->objectForKey(state);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (backgroundSprite)
|
|
|
|
{
|
|
|
|
return backgroundSprite;
|
|
|
|
}
|
|
|
|
return (CCScale9Sprite*)m_backgroundSpriteDispatchTable->objectForKey(CCControlStateNormal);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CCControlButton::setBackgroundSpriteForState(CCScale9Sprite* sprite, CCControlState state)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
CCSize oldPreferredSize = m_preferredSize;
|
|
|
|
|
|
|
|
CCScale9Sprite* previousBackgroundSprite = (CCScale9Sprite*)m_backgroundSpriteDispatchTable->objectForKey(state);
|
|
|
|
if (previousBackgroundSprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
removeChild(previousBackgroundSprite, true);
|
2012-06-02 03:29:48 +08:00
|
|
|
m_backgroundSpriteDispatchTable->removeObjectForKey(state);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
m_backgroundSpriteDispatchTable->setObject(sprite, state);
|
2012-06-15 15:10:40 +08:00
|
|
|
sprite->setVisible(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
sprite->setAnchorPoint(ccp(0.5f, 0.5f));
|
|
|
|
addChild(sprite);
|
|
|
|
|
2012-06-02 03:29:48 +08:00
|
|
|
if (this->m_preferredSize.width != 0 || this->m_preferredSize.height != 0)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
if (oldPreferredSize.equals(m_preferredSize))
|
|
|
|
{
|
2012-09-25 17:26:09 +08:00
|
|
|
// Force update of preferred size
|
2012-09-25 16:57:51 +08:00
|
|
|
sprite->setPreferredSize(CCSizeMake(oldPreferredSize.width+1, oldPreferredSize.height+1));
|
|
|
|
}
|
|
|
|
|
2012-06-02 03:29:48 +08:00
|
|
|
sprite->setPreferredSize(this->m_preferredSize);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-02 03:29:48 +08:00
|
|
|
void CCControlButton::setBackgroundSpriteFrameForState(CCSpriteFrame * spriteFrame, CCControlState state)
|
|
|
|
{
|
2012-06-20 11:48:31 +08:00
|
|
|
CCScale9Sprite * sprite = CCScale9Sprite::createWithSpriteFrame(spriteFrame);
|
2012-06-02 03:29:48 +08:00
|
|
|
this->setBackgroundSpriteForState(sprite, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void CCControlButton::needsLayout()
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
if (!m_bParentInited) {
|
|
|
|
return;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
// Hide the background and the label
|
2012-09-25 16:57:51 +08:00
|
|
|
if (m_titleLabel != NULL) {
|
|
|
|
m_titleLabel->setVisible(false);
|
|
|
|
}
|
|
|
|
if (m_backgroundSprite) {
|
|
|
|
m_backgroundSprite->setVisible(false);
|
|
|
|
}
|
2012-06-02 06:10:20 +08:00
|
|
|
// Update anchor of all labels
|
|
|
|
this->setLabelAnchorPoint(this->m_labelAnchorPoint);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Update the label to match with the current state
|
2012-09-25 16:57:51 +08:00
|
|
|
CC_SAFE_RELEASE(m_currentTitle);
|
|
|
|
m_currentTitle = getTitleForState(m_eState);
|
|
|
|
CC_SAFE_RETAIN(m_currentTitle);
|
|
|
|
|
2013-03-05 16:26:37 +08:00
|
|
|
m_currentTitleColor = getTitleColorForState(m_eState);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
|
|
|
this->setTitleLabel(getTitleLabelForState(m_eState));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CCLabelProtocol* label = dynamic_cast<CCLabelProtocol*>(m_titleLabel);
|
2012-09-25 16:57:51 +08:00
|
|
|
if (label && m_currentTitle)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
label->setString(m_currentTitle->getCString());
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCRGBAProtocol* rgbaLabel = dynamic_cast<CCRGBAProtocol*>(m_titleLabel);
|
|
|
|
if (rgbaLabel)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
rgbaLabel->setColor(m_currentTitleColor);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
if (m_titleLabel != NULL)
|
|
|
|
{
|
|
|
|
m_titleLabel->setPosition(ccp (getContentSize().width / 2, getContentSize().height / 2));
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Update the background sprite
|
2012-09-25 16:57:51 +08:00
|
|
|
this->setBackgroundSprite(this->getBackgroundSpriteForState(m_eState));
|
|
|
|
if (m_backgroundSprite != NULL)
|
|
|
|
{
|
|
|
|
m_backgroundSprite->setPosition(ccp (getContentSize().width / 2, getContentSize().height / 2));
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Get the title label size
|
2012-09-25 16:57:51 +08:00
|
|
|
CCSize titleLabelSize;
|
|
|
|
if (m_titleLabel != NULL)
|
|
|
|
{
|
|
|
|
titleLabelSize = m_titleLabel->boundingBox().size;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Adjust the background image if necessary
|
2012-09-25 16:57:51 +08:00
|
|
|
if (m_doesAdjustBackgroundImage)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Add the margins
|
2012-09-25 16:57:51 +08:00
|
|
|
if (m_backgroundSprite != NULL)
|
|
|
|
{
|
|
|
|
m_backgroundSprite->setContentSize(CCSizeMake(titleLabelSize.width + m_marginH * 2, titleLabelSize.height + m_marginV * 2));
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//TODO: should this also have margins if one of the preferred sizes is relaxed?
|
2012-09-25 16:57:51 +08:00
|
|
|
if (m_backgroundSprite != NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
CCSize preferredSize = m_backgroundSprite->getPreferredSize();
|
|
|
|
if (preferredSize.width <= 0)
|
|
|
|
{
|
|
|
|
preferredSize.width = titleLabelSize.width;
|
|
|
|
}
|
|
|
|
if (preferredSize.height <= 0)
|
|
|
|
{
|
|
|
|
preferredSize.height = titleLabelSize.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_backgroundSprite->setContentSize(preferredSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the content size
|
2012-09-25 16:57:51 +08:00
|
|
|
CCRect rectTitle;
|
|
|
|
if (m_titleLabel != NULL)
|
|
|
|
{
|
|
|
|
rectTitle = m_titleLabel->boundingBox();
|
|
|
|
}
|
|
|
|
CCRect rectBackground;
|
|
|
|
if (m_backgroundSprite != NULL)
|
|
|
|
{
|
|
|
|
rectBackground = m_backgroundSprite->boundingBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
CCRect maxRect = CCControlUtils::CCRectUnion(rectTitle, rectBackground);
|
2012-04-19 14:35:52 +08:00
|
|
|
setContentSize(CCSizeMake(maxRect.size.width, maxRect.size.height));
|
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
if (m_titleLabel != NULL)
|
|
|
|
{
|
|
|
|
m_titleLabel->setPosition(ccp(getContentSize().width/2, getContentSize().height/2));
|
|
|
|
// Make visible the background and the label
|
|
|
|
m_titleLabel->setVisible(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_backgroundSprite != NULL)
|
|
|
|
{
|
|
|
|
m_backgroundSprite->setPosition(ccp(getContentSize().width/2, getContentSize().height/2));
|
|
|
|
m_backgroundSprite->setVisible(true);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CCControlButton::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
|
|
|
|
{
|
2012-10-30 23:10:05 +08:00
|
|
|
if (!isTouchInside(pTouch) || !isEnabled() || !isVisible())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-05 16:26:37 +08:00
|
|
|
m_isPushed = true;
|
2012-06-15 16:47:30 +08:00
|
|
|
this->setHighlighted(true);
|
2012-04-19 14:35:52 +08:00
|
|
|
sendActionsForControlEvents(CCControlEventTouchDown);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControlButton::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
|
|
|
|
{
|
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);
|
2012-04-19 14:35:52 +08:00
|
|
|
sendActionsForControlEvents(CCControlEventTouchDragEnter);
|
|
|
|
}
|
2012-09-25 16:57:51 +08:00
|
|
|
else if (isTouchMoveInside && isHighlighted())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
sendActionsForControlEvents(CCControlEventTouchDragInside);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
sendActionsForControlEvents(CCControlEventTouchDragExit);
|
|
|
|
}
|
2012-09-25 16:57:51 +08:00
|
|
|
else if (!isTouchMoveInside && !isHighlighted())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
sendActionsForControlEvents(CCControlEventTouchDragOutside);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void CCControlButton::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
m_isPushed = false;
|
2012-06-15 16:47:30 +08:00
|
|
|
setHighlighted(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
if (isTouchInside(pTouch))
|
|
|
|
{
|
|
|
|
sendActionsForControlEvents(CCControlEventTouchUpInside);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sendActionsForControlEvents(CCControlEventTouchUpOutside);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-07 17:20:30 +08:00
|
|
|
void CCControlButton::setOpacity(GLubyte opacity)
|
|
|
|
{
|
2013-02-28 11:55:36 +08:00
|
|
|
// XXX fixed me if not correct
|
|
|
|
CCControl::setOpacity(opacity);
|
|
|
|
// m_cOpacity = opacity;
|
|
|
|
//
|
|
|
|
// CCObject* child;
|
|
|
|
// CCArray* children=getChildren();
|
|
|
|
// CCARRAY_FOREACH(children, child)
|
|
|
|
// {
|
|
|
|
// CCRGBAProtocol* pNode = dynamic_cast<CCRGBAProtocol*>(child);
|
|
|
|
// if (pNode)
|
|
|
|
// {
|
|
|
|
// pNode->setOpacity(opacity);
|
|
|
|
// }
|
|
|
|
// }
|
2012-06-07 17:20:30 +08:00
|
|
|
CCDictElement * item = NULL;
|
|
|
|
CCDICT_FOREACH(m_backgroundSpriteDispatchTable, item)
|
|
|
|
{
|
|
|
|
CCScale9Sprite* sprite = (CCScale9Sprite*)item->getObject();
|
|
|
|
sprite->setOpacity(opacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GLubyte CCControlButton::getOpacity()
|
|
|
|
{
|
2013-02-28 11:55:36 +08:00
|
|
|
return _realOpacity;
|
2012-06-07 17:20:30 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void CCControlButton::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
m_isPushed = false;
|
2012-06-15 16:47:30 +08:00
|
|
|
setHighlighted(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
sendActionsForControlEvents(CCControlEventTouchCancel);
|
|
|
|
}
|
|
|
|
|
2012-06-20 11:48:31 +08:00
|
|
|
CCControlButton* CCControlButton::create()
|
2012-06-02 03:29:48 +08:00
|
|
|
{
|
|
|
|
CCControlButton *pControlButton = new CCControlButton();
|
|
|
|
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
|