Skip to content

Instantly share code, notes, and snippets.

@Sirelon
Last active February 8, 2016 14:29
Show Gist options
  • Save Sirelon/c62f23b83d707a6424ba to your computer and use it in GitHub Desktop.
Save Sirelon/c62f23b83d707a6424ba to your computer and use it in GitHub Desktop.
(Cocos2dx C++) Create AppButton with combound images. (!!! Need to fix some align for text, if button has more that 1 image. Need to fix position for BOTTOM and TOP).
//
// AppButton.cpp
//
//
// Created by Alexandr Romanishin on 08.02.16.
//
//
#include "AppButton.hpp"
using namespace cocos2d;
using namespace cocos2d::ui;
static const float ZOOM_ACTION_TIME_STEP = 0.05f;
AppButton* AppButton::createYellowButton(const ButtonModel & model){
auto button = AppButton::create();
button->loadTextureNormal("btn_yellow.png");
if (!model.getText().empty()){
button->setTitleText(model.getText());
button->setTitleColor(Color3B::WHITE);
button->setTitleFontSize(AppText::fontSizeSmall());
button->setTitleFontName(AppText::defFontName());
button->setTitleAlignment(TextHAlignment::CENTER);
}
if (!model.getImageRes().empty()){
button->setCompoundImage(model.getImageRes(), SIDE::CENTER);
}
return button;
}
AppButton* AppButton::createGreenButton(const ButtonModel & model){
auto button = AppButton::create();
button->loadTextureNormal("btn_green.png");
button->setTitleColor(Color3B::WHITE);
button->setTitleText(model.getText());
button->setTitleFontSize(AppText::fontSizeSmall());
button->setTitleFontName(AppText::defFontName());
button->setTitleAlignment(TextHAlignment::CENTER);
if (!model.getImageRes().empty()){
button->setCompoundImage(model.getImageRes(), SIDE::LEFT);
}
return button;
}
bool AppButton::isSelected(){
return _isSelected;
}
void AppButton::setSelected(bool isSelected){
_isSelected = isSelected;
if (isSelected)
setBrightStyle(BrightStyle::HIGHLIGHT);
else
setBrightStyle(BrightStyle::NORMAL);
}
void AppButton::setCompoundImage(std::string imageRes, const SIDE side){
auto itemImg = ImageView::create(imageRes);
switch (side) {
case LEFT:
itemImg->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
itemImg->setPosition(Vec2(itemImg->getContentSize().width + 5, getContentSize().height / 2));
_imgRendererLeft = itemImg;
break;
case RIGHT:
itemImg->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT);
itemImg->setPosition(Vec2(getContentSize().width + 15, getContentSize().height / 2));
_imgRendererRight = itemImg;
break;
case CENTER:
itemImg->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
itemImg->setPosition(Vec2(getContentSize().width / 2, getContentSize().height / 2));
_imgRendererCenter = itemImg;
break;
case TOP:
itemImg->setAnchorPoint(Vec2::ANCHOR_MIDDLE_BOTTOM);
itemImg->setPosition(Vec2(getContentSize().width / 2, itemImg->getContentSize().height + 5));
_imgRendererTop = itemImg;
break;
case BOTTOM:
itemImg->setAnchorPoint(Vec2::ANCHOR_MIDDLE_TOP);
itemImg->setPosition(Vec2(getContentSize().width / 2, 5));
_imgRendererBottom = itemImg;
break;
default:
break;
}
ignoreContentAdaptWithSize(false);
addChild(itemImg);
}
void AppButton::onPressStateChangedToNormal()
{
Button::onPressStateChangedToNormal();
if (_pressedTextureLoaded)
{
if (_pressedActionEnabled)
{
if(nullptr != _imgRendererLeft)
{
_imgRendererLeft->stopAllActions();
if (_unifySize)
{
cocos2d::Action *zoomTitleAction = cocos2d::ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0f, 1.0f);
_imgRendererLeft->runAction(zoomTitleAction);
}
else
{
_imgRendererLeft->setScaleX(1.0f);
_imgRendererLeft->setScaleY(1.0f);
}
}
if(nullptr != _imgRendererRight)
{
_imgRendererRight->stopAllActions();
if (_unifySize)
{
cocos2d::Action *zoomTitleAction = cocos2d::ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0f, 1.0f);
_imgRendererRight->runAction(zoomTitleAction);
}
else
{
_imgRendererRight->setScaleX(1.0f);
_imgRendererRight->setScaleY(1.0f);
}
}
if(nullptr != _imgRendererCenter)
{
_imgRendererCenter->stopAllActions();
if (_unifySize)
{
cocos2d::Action *zoomTitleAction = cocos2d::ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0f, 1.0f);
_imgRendererCenter->runAction(zoomTitleAction);
}
else
{
_imgRendererCenter->setScaleX(1.0f);
_imgRendererCenter->setScaleY(1.0f);
}
}
if(nullptr != _imgRendererTop)
{
_imgRendererTop->stopAllActions();
if (_unifySize)
{
cocos2d::Action *zoomTitleAction = cocos2d::ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0f, 1.0f);
_imgRendererTop->runAction(zoomTitleAction);
}
else
{
_imgRendererTop->setScaleX(1.0f);
_imgRendererTop->setScaleY(1.0f);
}
}
if(nullptr != _imgRendererBottom)
{
_imgRendererBottom->stopAllActions();
if (_unifySize)
{
cocos2d::Action *zoomTitleAction = cocos2d::ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0f, 1.0f);
_imgRendererBottom->runAction(zoomTitleAction);
}
else
{
_imgRendererBottom->setScaleX(1.0f);
_imgRendererBottom->setScaleY(1.0f);
}
}
}
}
else
{
if(nullptr != _imgRendererLeft)
{
_imgRendererLeft->stopAllActions();
_imgRendererLeft->setScaleX(1.0f);
_imgRendererLeft->setScaleY(1.0f);
}
if(nullptr != _imgRendererRight)
{
_imgRendererRight->stopAllActions();
_imgRendererRight->setScaleX(1.0f);
_imgRendererRight->setScaleY(1.0f);
}
if(nullptr != _imgRendererCenter)
{
_imgRendererCenter->stopAllActions();
_imgRendererCenter->setScaleX(1.0f);
_imgRendererCenter->setScaleY(1.0f);
}
if(nullptr != _imgRendererTop)
{
_imgRendererTop->stopAllActions();
_imgRendererTop->setScaleX(1.0f);
_imgRendererTop->setScaleY(1.0f);
}
if(nullptr != _imgRendererBottom)
{
_imgRendererBottom->stopAllActions();
_imgRendererBottom->setScaleX(1.0f);
_imgRendererBottom->setScaleY(1.0f);
}
}
}
void AppButton::onPressStateChangedToPressed()
{
Button::onPressStateChangedToPressed();
if (_pressedTextureLoaded)
{
if (_pressedActionEnabled)
{
Action *zoomTitleAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP,
1.0f + _zoomScale, 1.0f + _zoomScale);
if(nullptr != _imgRendererLeft)
{
_imgRendererLeft->stopAllActions();
_imgRendererLeft->runAction(zoomTitleAction);
}
if(nullptr != _imgRendererRight)
{
_imgRendererRight->stopAllActions();
_imgRendererRight->runAction(zoomTitleAction);
}
if(nullptr != _imgRendererCenter)
{
_imgRendererCenter->stopAllActions();
_imgRendererCenter->runAction(zoomTitleAction);
}
if(nullptr != _imgRendererTop)
{
_imgRendererTop->stopAllActions();
_imgRendererTop->runAction(zoomTitleAction);
}
if(nullptr != _imgRendererBottom)
{
_imgRendererBottom->stopAllActions();
_imgRendererBottom->runAction(zoomTitleAction);
}
}
}
else
{
if(nullptr != _imgRendererLeft)
{
_imgRendererLeft->stopAllActions();
_imgRendererLeft->setScaleX(1.0f + _zoomScale);
_imgRendererLeft->setScaleY(1.0f + _zoomScale);
}
if(nullptr != _imgRendererRight)
{
_imgRendererRight->stopAllActions();
_imgRendererRight->setScaleX(1.0f + _zoomScale);
_imgRendererRight->setScaleY(1.0f + _zoomScale);
}
if(nullptr != _imgRendererCenter)
{
_imgRendererCenter->stopAllActions();
_imgRendererCenter->setScaleX(1.0f + _zoomScale);
_imgRendererCenter->setScaleY(1.0f + _zoomScale);
}
if(nullptr != _imgRendererTop)
{
_imgRendererTop->stopAllActions();
_imgRendererTop->setScaleX(1.0f + _zoomScale);
_imgRendererTop->setScaleY(1.0f + _zoomScale);
}
if(nullptr != _imgRendererBottom)
{
_imgRendererBottom->stopAllActions();
_imgRendererBottom->setScaleX(1.0f + _zoomScale);
_imgRendererBottom->setScaleY(1.0f + _zoomScale);
}
}
}
void AppButton::onPressStateChangedToDisabled()
{
Button::onPressStateChangedToDisabled();
}
void AppButton::onSizeChanged()
{
Button::onSizeChanged();
CCLOG("onSizeChanged");
if (nullptr != _imgRendererLeft){
_titleRenderer->setPosition(_contentSize.width * 0.5f + _imgRendererLeft->getContentSize().width * 0.5, _contentSize.height * 0.5f);
}
if (nullptr != _imgRendererRight){
_titleRenderer->setPosition(_contentSize.width * 0.5f - _imgRendererRight->getContentSize().width * 0.5, _contentSize.height * 0.5f);
}
if (nullptr != _imgRendererTop){
_titleRenderer->setPosition(_contentSize.width * 0.5f, _contentSize.height * 0.5f - _imgRendererTop->getContentSize().height * 0.5);
}
if (nullptr != _imgRendererBottom){
_titleRenderer->setPosition(_contentSize.width * 0.5f, _contentSize.height * 0.5f + _imgRendererBottom->getContentSize().height * 0.5);
}
}
//
// AppButton.hpp
//
//
// Created by Alexandr Romanishin on 08.02.16.
//
//
#ifndef AppButton_hpp
#define AppButton_hpp
#include <stdio.h>
#include <cocos2d.h>
#include <ui/CocosGUI.h>
#include "AppText.hpp"
typedef enum {
TOP,
BOTTOM,
RIGHT,
LEFT,
CENTER
} SIDE;
class ButtonModel;
class AppButton : public cocos2d::ui::Button{
public:
CREATE_FUNC(AppButton);
static AppButton* createGreenButton(const ButtonModel & model);
static AppButton* createYellowButton(const ButtonModel & model);
bool isSelected();
void setSelected(bool isSelected);
void setCompoundImage(std::string imageRes, const SIDE side);
protected:
virtual void onPressStateChangedToNormal() override;
virtual void onPressStateChangedToPressed() override;
virtual void onPressStateChangedToDisabled() override;
virtual void onSizeChanged() override;
private:
bool _isSelected;
cocos2d::ui::ImageView* _imgRendererLeft;
cocos2d::ui::ImageView* _imgRendererRight;
cocos2d::ui::ImageView* _imgRendererTop;
cocos2d::ui::ImageView* _imgRendererBottom;
cocos2d::ui::ImageView* _imgRendererCenter;
};
class ButtonModel{
public:
ButtonModel() {}
ButtonModel(const std::string & imgRes, const cocos2d::ui::Widget::ccWidgetClickCallback & callback)
: _imgRes(imgRes), _callback(callback) {}
ButtonModel(const std::string & text, const std::string & imgRes, const cocos2d::ui::Widget::ccWidgetClickCallback & callback)
: _imgRes(imgRes), _callback(callback), _text(text) {}
CC_SYNTHESIZE_READONLY_PASS_BY_REF(std::string, _text, Text);
CC_SYNTHESIZE_READONLY_PASS_BY_REF(std::string, _imgRes, ImageRes);
CC_SYNTHESIZE_READONLY_PASS_BY_REF(cocos2d::ui::Widget::ccWidgetClickCallback, _callback, Callback);
};
#endif /* AppButton_hpp */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment