码迷,mamicode.com
首页 > 其他好文 > 详细

ControlButton按钮事件

时间:2015-01-14 18:27:58      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:control   button   

#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)
{

}



本文出自 “风中的一粒沙” 博客,请务必保留此出处http://libinqi456.blog.51cto.com/4819343/1603767

ControlButton按钮事件

标签:control   button   

原文地址:http://libinqi456.blog.51cto.com/4819343/1603767

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!