cocos2dx 3.x c++实现一个win32自定义按钮

由于cocos2dx的按钮创建过于复杂还不支持win32悬停功能

所以考虑自己封装一个

myButton.h

#pragma once   
#pragma execution_character_set("utf-8")    
#include"cocos2d.h"
#include"游戏数据.h"
using namespace std;
using namespace cocos2d;
class myButton:public Sprite
{
public:
    myButton(string normalPath, string onPath, string downPath, Node* fatherNode, Point position);//普通myButton构造
    //按下透明myButton构造
    //音效myButton构造
    std::function<void()> myButtonCall;
    virtual ~myButton();
private:
    string normalPath;
    string onPath;
    string downPath;
    Rect rect;
    Node* fatherNode;
    Point position;
    int mouseState;//0松开 1按下 
    int textureState;//0正常 1悬停
    void update(float dt);
    void setNoormalTexture();
    void setOnTexture();
    void setDownTexture();
};

myButton.cpp 

#include "myButton.h"
myButton::myButton(string normalPath,string onPath, string downPath,Node* fatherNode, Point position)
{
    this->normalPath = normalPath;
    this->onPath = onPath;
    this->downPath = downPath;
    this->fatherNode = fatherNode;
    this->position = position;
    mouseState = 0;
    textureState = 0;

   initWithFile(normalPath);
    autorelease();
    setPosition(this->position);
    fatherNode->addChild(this, 10);
    

    auto fatherNodeRect = fatherNode->getBoundingBox();
    rect = this->getBoundingBox();
    rect.origin.x = rect.origin.x + fatherNodeRect.origin.x;
    rect.origin.y = rect.origin.y + fatherNodeRect.origin.y;

    auto listener = EventListenerTouchOneByOne::create();
    listener->setSwallowTouches(true);
    listener->onTouchBegan = [=](Touch* touch, Event* event)
    {
        Point touchPosition = Director::getInstance()->convertToGL(touch->getLocationInView());
        if (rect.containsPoint(touchPosition))
        {
            mouseState = 1;
            setDownTexture();
            return true;
        }
        return false;
    };
    listener->onTouchMoved = [=](Touch* touch, Event* event)
    {
        Point touchPosition = Director::getInstance()->convertToGL(touch->getLocationInView());
        if (rect.containsPoint(touchPosition))
        {
            setDownTexture();
        }
        else
        {
            setNoormalTexture();
        }
    };
    listener->onTouchEnded = [=](Touch* touch, Event* event)
    {
        Point touchPosition = Director::getInstance()->convertToGL(touch->getLocationInView());
        if (rect.containsPoint(touchPosition))
        {
            myButtonCall();
        }
        mouseState = 0;
        textureState = 0;
        setNoormalTexture();
    };
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
    this->scheduleUpdate();//开启定时器
}

void myButton::update(float dt)
{
    if (rect.containsPoint(游戏设置数据类::游戏设置数据.当前鼠标坐标) && mouseState == 0 && textureState == 0)
    {
        textureState = 1;
        setOnTexture();
    }

    if (rect.containsPoint(游戏设置数据类::游戏设置数据.当前鼠标坐标) == false && textureState == 1)
    {
        textureState = 0;
        setNoormalTexture();
    }
}

void myButton::setNoormalTexture()
{
    log("setNoormalTexture");
    setTexture(normalPath);
}

void myButton::setOnTexture()
{
    log("setOnTexture");
    setTexture(onPath);
}

void myButton::setDownTexture()
{
    log("setDownTexture");
    setTexture(downPath);
}

myButton::~myButton()
{

}

创建过程

首先创建鼠标监听器 监控鼠标位置 因为要实现悬停功能 鼠标的坐标自己定义吧  

    auto listenerMouse = EventListenerMouse::create();
    listenerMouse->onMouseMove = [&](EventMouse* event) {
        游戏设置数据类::游戏设置数据.当前鼠标坐标.x = event->getCursorX();
        游戏设置数据类::游戏设置数据.当前鼠标坐标.y = event->getCursorY();
        //log("%f,%f", 游戏设置数据类::游戏设置数据.当前鼠标坐标.x, 游戏设置数据类::游戏设置数据.当前鼠标坐标.y);
        //鼠标指针->setPosition(当前鼠标坐标);
    };
    _eventDispatcher->addEventListenerWithFixedPriority(listenerMouse, 1); 

auto myButton1 = new myButton(
        "picture/ui/biaoti/start1.png",
        "picture/ui/biaoti/start2.png",
        "picture/ui/biaoti/start3.png",
        this, ccp(200, 300));
    myButton1->myButtonCall = [=]()
    {
        log("我的按钮触发了");
    };

可以在此基础上继续拓展其他功能 

 大家有什么意见欢迎评论。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值