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

}

转载于:https://blog.51cto.com/libinqi456/1603767

ControlButton按钮事件相关推荐

  1. CRM:把 isv.config.xml 按钮事件移动到 entity.onload()

    大家都知道在ISV.CONFIG里可以添加按钮,然后添加按钮事件,比如下面: <Entities>   <Entity name="account">    ...

  2. .Net 转战 Android 4.4 日常笔记(4)--按钮事件和国际化

    原文:.Net 转战 Android 4.4 日常笔记(4)--按钮事件和国际化 我们知道资源被注册到R.java我们通过R.java就可以读取到界面中的组件.跟我们.net一样,通过ID来读取组件 ...

  3. Android按钮事件的4种写法

    经过前两篇blog的铺垫,我们今天热身一下,做个简单的例子. 目录结构还是引用上篇blog的截图. 具体实现代码: public class MainActivity extends Activity ...

  4. 安卓之页面跳转与传值和按钮事件

    一:新建页面 即新建Activity,new-other-Android Activity,next, 新建Activity的时候, 1:eclipse会自动创建Layout,我们发现Layout目录 ...

  5. 【“零起点”--百度地图手机SDK】如何添加地图图层+按钮事件+水平垂直布局?...

    摘要:我们在这一章将学习到如何通过按钮事件来控制地图图层(交通流量图,卫星图)的显示,以及如何对android应用进行水平和垂直布局. -------------------------------- ...

  6. vue 表单验证按钮事件交由父组件触发

    vue 表单验证按钮事件交由父组件触发,不直接再子组件上操作的方法 子组件: //内容部分 <Form ref="formCustom" :model="formC ...

  7. duilib基本布局学习(模仿百度网盘)及绑定按钮事件

    使用的网易版本的duilib: https://github.com/netease-im/NIM_Duilib_Framework 重写demo中basic.xml页面的布局,熟悉布局语法,类似于h ...

  8. php离开界面监听,js实现用户离开页面前提示是否离开此页面的方法(包括浏览器按钮事件)...

    本文实例讲述了js实现用户离开页面前提示是否离开此页面的方法(包括浏览器按钮事件).分享给大家供大家参考.具体如下: 用户离开页面前,提示是否离开此页面(包括浏览器按钮事件) window.onbef ...

  9. 转自JIM Wang:把 isv.config.xml 按钮事件移动到 entity.onload()

    把 isv.config.xml 按钮事件移动到 entity.onload() 大家都知道在ISV.CONFIG里可以添加按钮,然后添加按钮事件,比如下面: <Entities>   & ...

最新文章

  1. 扫盲:关于Android手机内存ROM、RAM还有SD卡的解释
  2. gradle idea java ssm_应用框架:IDEA+Gradle创建MyBatis+SpringMVC项目
  3. oracle 默认 服务器,Oracle的默认用户密码
  4. java 数组的get set_java.lang.reflect.Array.setBoolean()方法
  5. foxit phantom pdf 7.3_Jpeg to Pdf Converter 3000批量将图片转为PDF的方法
  6. java 蓝桥杯历届试题 回文数字(题解)
  7. 外媒晒一加7 Pro相机实拍样张:不惧极速F1赛车
  8. 快戳!Python 开发者的福音来啦!
  9. delphi listview动态添加图片_Java 添加PDF图章(印章)——图片图章、动态图章
  10. MarkDown -- 基本语法
  11. 普元 EOS Platform 7.6 Studio导出流程par包,在workspace中导入报错:租户令牌不正确,部署失败
  12. three.js各种版本的编辑器
  13. Intelligent driver model(IDM)
  14. 低延迟视频传输 UDP JPEG图像压缩 opencv
  15. Blue Screen Of Death ( BSOD ) 错误信息解析解释
  16. Mapped Statements collection does not contain value for 解决方法之一
  17. 前端复习之DOM、BOM
  18. bl小说里面有个机器人管家_《真实的人类》第二季开拍 机器人管家与小女孩重逢 播出时间未定...
  19. 基于Android Q 修改默认音量等级
  20. LintCode(158)

热门文章

  1. php 魔术方法 说明
  2. WinCE中命令行工具Viewbin简介(查看nk.bin中包含的文件)
  3. 嵌入式课程安排 嵌入式培训课程大纲参考
  4. (转)关于数据库主键和外键(终于弄懂啦)
  5. SQL2K数据库开发二之查看和修改Sample数据库
  6. centos7 lvm管理 把/home空间转移给/
  7. **IOS:xib文件解析(xib和storyboard的比较,一个轻量级一个重量级)
  8. SqlServer时间函数的使用例子整理
  9. 【十五分钟Talkshow】fmplan(十五分钟计划)的初步想法
  10. 引起路由器重启的“元凶”