您好,登錄后才能下訂單哦!
#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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。