#ifndef __HControlButton_H__
#define __HControlButton_H__
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
//用于标识当前按钮的状态
typedef enum{
touch_begin,
touch_down,
touch_up,
}tagForTouch;
class HControlButton :public CCNode
{
public:
HControlButton();
~HControlButton();
CREATE_FUNC(HControlButton);
//创建按钮,其中name_png为按钮的背景图片,button_title为按钮图片上要显示的文字,num为文字的透明度0-100,0为透明
void CreateButton(const char* name_png,const char* button_title="0",unsigned int num=0);
//绑写按钮事件
void BindButtonEven();
/* 当鼠标处于按下并曾经点中按钮时,则触发一次 */
void touchDownAction(Ref *sender, Control::EventType controlEvent);
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标进入按钮范围,则触发一次 */
void touchDragEnterAction(Ref *sender, Control::EventType controlEvent);
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标离开按钮范围,则触发一次 */
void touchDragExitAction(Ref *sender, Control::EventType controlEvent);
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标进入按钮范围,则触发,只要达到条件,就不断触发 */
void touchDragInsideAction(Ref *sender, Control::EventType controlEvent);
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标离开按钮范围,则触发,只要达到条件,就不断触发 */
void touchDragOutsideAction(Ref *sender, Control::EventType controlEvent);
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标松开且在按钮范围内,则触发一次 */
void touchUpInsideAction(Ref *sender, Control::EventType controlEvent);
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标松开且在按钮范围外,则触发一次 */
void touchUpOutsideAction(Ref *sender, Control::EventType controlEvent);
/* 暂时没有发现能用鼠标触发这个事件的操作,看了注释,应该是由其它事件中断按钮事件而触发的 */
void touchCancelAction(Ref *sender, Control::EventType controlEvent);
//是否按下按钮
bool isTouch;
private:
//按钮控件变量
ControlButton* controlBtn;
};
#endif
//******************************************************
#include "HControlButton.h"
HControlButton::HControlButton():controlBtn(NULL),isTouch(false)
{
}
HControlButton::~HControlButton()
{
}
void HControlButton::CreateButton(const char* name_png,const char* button_title,unsigned int num)
{
//*************************************
// Add the button
auto backgroundButton = Scale9Sprite::create(name_png);
//得到按钮图片的大小
int png_height=static_cast<int>(backgroundButton->getContentSize().height);
int png_width=static_cast<int>( backgroundButton->getContentSize().width);
auto backgroundHighlightedButton = Scale9Sprite::create(name_png);
auto titleButton = Label::createWithTTF("", "fonts/Marker Felt.ttf", 30);
//titleButton->setColor(Color3B(159, 168, 176));
//创建按钮
controlBtn = ControlButton::create(titleButton,backgroundButton);
//要显示的图片大小
controlBtn->setPreferredSize(Size(png_width,png_height));
this->addChild(controlBtn);
//绑定事件
BindButtonEven();
}
void HControlButton::BindButtonEven()
{
if(!controlBtn)
return;
// Sets up event handlers
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HControlButton::touchDownAction), Control::EventType::TOUCH_DOWN);
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HControlButton::touchDragInsideAction), Control::EventType::DRAG_INSIDE);
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HControlButton::touchDragOutsideAction), Control::EventType::DRAG_OUTSIDE);
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HControlButton::touchDragEnterAction), Control::EventType::DRAG_ENTER);
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HControlButton::touchDragExitAction), Control::EventType::DRAG_EXIT);
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HControlButton::touchUpInsideAction), Control::EventType::TOUCH_UP_INSIDE);
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HControlButton::touchUpOutsideAction), Control::EventType::TOUCH_UP_OUTSIDE);
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HControlButton::touchCancelAction), Control::EventType::TOUCH_CANCEL);
}
/* 当鼠标处于按下并曾经点中按钮时,则触发一次 */
void HControlButton::touchDownAction(CCObject* pSender, Control::EventType controlEvent)
{
isTouch=true;
log("touchDownAction");
}
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标进入按钮范围,则触发一次 */
void HControlButton::touchDragEnterAction(CCObject* pSender, Control::EventType controlEvent)
{
}
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标离开按钮范围,则触发一次 */
void HControlButton::touchDragExitAction(CCObject* pSender, Control::EventType controlEvent)
{
}
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标进入按钮范围,则触发,只要达到条件,就不断触发 */
void HControlButton::touchDragInsideAction(CCObject* pSender, Control::EventType controlEvent)
{
}
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标离开按钮范围,则触发,只要达到条件,就不断触发 */
void HControlButton::touchDragOutsideAction(CCObject* pSender, Control::EventType controlEvent)
{
}
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标松开且在按钮范围内,则触发一次 */
void HControlButton::touchUpInsideAction(CCObject* pSender, Control::EventType controlEvent)
{
isTouch=false;
}
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标松开且在按钮范围外,则触发一次 */
void HControlButton::touchUpOutsideAction(CCObject* pSender, Control::EventType controlEvent)
{
}
/* 暂时没有发现能用鼠标触发这个事件的操作,看了注释,应该是由其它事件中断按钮事件而触发的 */
void HControlButton::touchCancelAction(CCObject* pSender, Control::EventType controlEvent)
{
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。