diff --git a/cocos2dx/extensions/CCControlExtension/CCControlButton.cpp b/cocos2dx/extensions/CCControlExtension/CCControlButton.cpp index d84cd089bf..0cd8b80aca 100644 --- a/cocos2dx/extensions/CCControlExtension/CCControlButton.cpp +++ b/cocos2dx/extensions/CCControlExtension/CCControlButton.cpp @@ -28,6 +28,7 @@ #include "CCScale9Sprite.h" #include "CCPointExtension.h" #include "CCLabelTTF.h" +#include "CCLabelBMFont.h" #include "CCAction.h" #include "CCActionInterval.h" @@ -50,6 +51,11 @@ CCControlButton::~CCControlButton() //initialisers +bool CCControlButton::init() +{ + return this->initWithLabelAndBackgroundSprite(CCLabelTTF::labelWithString("", "Helvetica", 12), CCScale9Sprite::node()); +} + bool CCControlButton::initWithLabelAndBackgroundSprite(CCNode* node, CCScale9Sprite* backgroundSprite) { @@ -62,6 +68,7 @@ bool CCControlButton::initWithLabelAndBackgroundSprite(CCNode* node, CCScale9Spr setIsTouchEnabled(true); pushed=false; + m_zoomOnTouchDown = true; m_nState=CCControlStateInitial; m_currentTitle=NULL; m_backgroundSprite=NULL; @@ -186,10 +193,13 @@ void CCControlButton::setIsHighlighted(bool enabled) } needsLayout(); - float scaleValue = (getIsHighlighted() && getIsEnabled() && !getIsSelected()) ? 1.1f : 1.0f; - CCAction *zoomAction =CCScaleTo::actionWithDuration(0.05f, scaleValue); - zoomAction->setTag(kZoomActionTag); - runAction(zoomAction); + if(m_zoomOnTouchDown) + { + float scaleValue = (getIsHighlighted() && getIsEnabled() && !getIsSelected()) ? 1.1f : 1.0f; + CCAction *zoomAction =CCScaleTo::actionWithDuration(0.05f, scaleValue); + zoomAction->setTag(kZoomActionTag); + runAction(zoomAction); + } } void CCControlButton::setAdjustBackgroundImage(bool adjustBackgroundImage) { @@ -202,6 +212,36 @@ bool CCControlButton::getAdjustBackgroundImage() return m_adjustBackgroundImage; } +CCSize CCControlButton::getPreferredSize() +{ + return this->m_preferredSize; +} + +void CCControlButton::setPreferredSize(CCSize preferredSize) +{ + if (preferredSize.width == 0 && preferredSize.height == 0) + { + this->m_adjustBackgroundImage = true; + } + else + { + this->m_adjustBackgroundImage = false; + + // TODO Was: "for (id key in backgroundSpriteDispatchTable_)" + CCDictElement * key = NULL; + CCDICT_FOREACH(m_backgroundSpriteDispatchTable, key) + { + int i = 0; // TODO + //CCScale9Sprite * sprite = m_backgroundSpriteDispatchTable->objectForKey(key); + //sprite->setPreferredSize(preferredSize); + } + } + + this->m_preferredSize = preferredSize; + + this->needsLayout(); +} + CCString* CCControlButton::getTitleForState(CCControlState state) { CCString* title=(CCString*)m_titleDispatchTable->objectForKey(state); @@ -289,6 +329,75 @@ void CCControlButton::setTitleLabelForState(CCNode* titleLabel, CCControlState s } } +void CCControlButton::setTitleTTFForState(const char * fntFile, CCControlState state) +{ + CCString * title = this->getTitleForState(state); + if (!title) title = new CCString(""); + this->setTitleLabelForState(CCLabelTTF::labelWithString(title->getCString(), fntFile, 12), state); +} + +const char * CCControlButton::getTitleTTFForState(CCControlState state) +{ + CCLabelProtocol* label = dynamic_cast(this->getTitleLabelForState(state)); + CCLabelTTF* labelTTF = dynamic_cast(label); + if(labelTTF != 0) + { + return labelTTF->getFontName(); + } + else + { + return ""; + } +} + +void CCControlButton::setTitleTTFSizeForState(float size, CCControlState state) +{ + CCLabelProtocol* label = dynamic_cast(this->getTitleLabelForState(state)); + if(label) + { + CCLabelTTF* labelTTF = dynamic_cast(label); + if(labelTTF != 0) + { + return labelTTF->setFontSize(size); + } + } +} + +float CCControlButton::getTitleTTFSizeForState(CCControlState state) +{ + CCLabelProtocol* label = dynamic_cast(this->getTitleLabelForState(state)); + CCLabelTTF* labelTTF = dynamic_cast(label); + if(labelTTF != 0) + { + return labelTTF->getFontSize(); + } + else + { + return 0; + } +} + +void CCControlButton::setTitleBMFontForState(const char * fntFile, CCControlState state) +{ + CCString * title = this->getTitleForState(state); + if (!title) title = new CCString(""); + this->setTitleLabelForState(CCLabelBMFont::labelWithString(title->getCString(), fntFile), state); +} + +const char * CCControlButton::getTitleBMFontForState(CCControlState state) +{ + CCLabelProtocol* label = dynamic_cast(this->getTitleLabelForState(state)); + CCLabelBMFont* labelBMFont = dynamic_cast(label); + if(labelBMFont != 0) + { + return labelBMFont->getFntFile(); + } + else + { + return ""; + } +} + CCScale9Sprite* CCControlButton::getBackgroundSpriteForState(CCControlState state) { @@ -307,7 +416,7 @@ void CCControlButton::setBackgroundSpriteForState(CCScale9Sprite* sprite, CCCont if (previousSprite) { removeChild(previousSprite, true); - m_backgroundSpriteDispatchTable->removeObjectForKey(state); + m_backgroundSpriteDispatchTable->removeObjectForKey(state); } m_backgroundSpriteDispatchTable->setObject(sprite, state); @@ -315,6 +424,11 @@ void CCControlButton::setBackgroundSpriteForState(CCScale9Sprite* sprite, CCCont sprite->setAnchorPoint(ccp(0.5f, 0.5f)); addChild(sprite); + if (this->m_preferredSize.width != 0 || this->m_preferredSize.height != 0) + { + sprite->setPreferredSize(this->m_preferredSize); + } + // If the current state if equal to the given state we update the layout if (getState() == state) { @@ -322,6 +436,13 @@ void CCControlButton::setBackgroundSpriteForState(CCScale9Sprite* sprite, CCCont } } +void CCControlButton::setBackgroundSpriteFrameForState(CCSpriteFrame * spriteFrame, CCControlState state) +{ + CCScale9Sprite * sprite = CCScale9Sprite::spriteWithSpriteFrame(spriteFrame); + this->setBackgroundSpriteForState(sprite, state); +} + + void CCControlButton::needsLayout() { // Hide the background and the label @@ -462,4 +583,16 @@ void CCControlButton::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) sendActionsForControlEvents(CCControlEventTouchCancel); } +CCControlButton * CCControlButton::node() +{ + CCControlButton *pControlButton = new CCControlButton(); + if (pControlButton && pControlButton->init()) + { + pControlButton->autorelease(); + return pControlButton; + } + CC_SAFE_DELETE(pControlButton); + return NULL; +} + NS_CC_EXT_END diff --git a/cocos2dx/extensions/CCControlExtension/CCControlButton.h b/cocos2dx/extensions/CCControlExtension/CCControlButton.h index 056e9a89cc..ecc62e3bd5 100644 --- a/cocos2dx/extensions/CCControlExtension/CCControlButton.h +++ b/cocos2dx/extensions/CCControlExtension/CCControlButton.h @@ -51,9 +51,12 @@ protected: /** Adjust the background image. YES by default. If the property is set to NO, the background will use the prefered size of the background image. */ - CC_PROPERTY(bool, m_adjustBackgroundImage, AdjustBackgroundImage); + CC_PROPERTY(bool, m_adjustBackgroundImage, AdjustBackgroundImage); + + CC_PROPERTY(CCSize, m_preferredSize, PreferredSize); + + CC_SYNTHESIZE(bool, m_zoomOnTouchDown, ZoomOnTouchDown); - /** The current title that is displayed on the button. */ CC_SYNTHESIZE_READONLY(CCString*, m_currentTitle, CurrentTitle); /** The current color used to display the title. */ @@ -85,6 +88,7 @@ protected: public: + virtual bool init(); virtual bool initWithLabelAndBackgroundSprite(CCNode* label, CCScale9Sprite* backgroundSprite); static CCControlButton* buttonWithLabelAndBackgroundSprite(CCNode* label, CCScale9Sprite* backgroundSprite); @@ -162,6 +166,21 @@ public: */ virtual void setTitleLabelForState(CCNode* label, CCControlState state); + virtual void setTitleTTFForState(const char * fntFile, CCControlState state); + virtual const char * getTitleTTFForState(CCControlState state); + + virtual void setTitleTTFSizeForState(float size, CCControlState state); + virtual float getTitleTTFSizeForState(CCControlState state); + + /** + * Sets the font of the label, changes the label to a CCLabelBMFont if neccessary. + * @param fntFile The name of the font to change to + * @param state The state that uses the specified fntFile. The values are described + * in "CCControlState". + */ + virtual void setTitleBMFontForState(const char * fntFile, CCControlState state); + virtual const char * getTitleBMFontForState(CCControlState state); + /** * Returns the background sprite used for a state. * @@ -179,6 +198,16 @@ public: */ virtual void setBackgroundSpriteForState(CCScale9Sprite* sprite, CCControlState state); + /** + * Sets the background spriteFrame to use for the specified button state. + * + * @param spriteFrame The background spriteFrame to use for the specified state. + * @param state The state that uses the specified image. The values are described + * in "CCControlState". + */ + virtual void setBackgroundSpriteFrameForState(CCSpriteFrame * spriteFrame, CCControlState state); + + static CCControlButton * node(); }; NS_CC_EXT_END