win32 鼠标点击及消息管理系统
我们来添加鼠标点击的响应和消息管理系统。
先来看消息管理。
/********************************************************************
Copyright(C), 2012-2013,
FileName:Events.h
Description:
Author:cloud
Created:2014/11/21
history:
21:11:2014 9:35 by
*********************************************************************/
#pragma once
namespace cloud
{
const unsigned int MSG_Enter = 1;
const unsigned int MSG_Exit = 2;
const unsigned int MSG_SingleTouchBegan = 3;
const unsigned int MSG_SingleTouchMoved = 4;
const unsigned int MSG_SingleTouchEnded = 5;
const unsigned int MSG_User = 10000;
}/********************************************************************
Copyright(C), 2012-2013,
FileName:EventManager.h
Description:
Author:cloud
Created:2014/11/21
history:
21:11:2014 9:35 by
*********************************************************************/
#pragma once
#include "base/Singleton.h"
#include "export/Events.h"
namespace cloud
{
class EventManager :public Singleton<EventManager>
{
public:
DECLARE_SINGLETON_CREATE_DESTROY
EventManager();
~EventManager();
void sendMessage(unsigned int message,void* param);
};
}/********************************************************************
Copyright(C), 2012-2013,
FileName:EventManager.cpp
Description:
Author:cloud
Created:2014/11/21
history:
21:11:2014 9:35 by
*********************************************************************/
#pragma once
#include "EventManager.h"
#include "base/Engine.h"
namespace cloud
{
IMPLEMENT_SINGLETON_ALL(EventManager);
EventManager::EventManager()
{
}
EventManager::~EventManager()
{
}
void EventManager::sendMessage(unsigned int message,void* param)
{
Scene * scene = Engine::getInstance()->getRunningScene();
if (scene)
{
scene->dispatchMessage(message,param);
}
}
}bool Node::dispatchMessage(unsigned int message,void* param)
{
if (getChildren()->size() > 0)//删除当前节点子节点
{
std::vector<Node*>::iterator itChild = _children.begin();
while (itChild != _children.end())
{
if((*itChild)->dispatchMessage(message,param))
return true;
++itChild;
}
}
return handerMessage(message,param);
}
bool Node::handerMessage(unsigned int message,void* param)
{
return false;
}它会把消息依次投递给子节点。子节点handerMessage如果返回true。表示该消息已经处理过了,就不会再投递了。
再看鼠标点击。
static bool _captured = false;
static float _mouseX = 0.f;
static float _mouseY = 0.f;
static void onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify)
{
Size frameSize = Engine::getInstance()->getFrameSize();
if(GLFW_MOUSE_BUTTON_LEFT == button)
{
if(GLFW_PRESS == action)
{
_captured = true;
if (_mouseX < frameSize.width && _mouseX > 0.f && _mouseY > 0.f && _mouseY < frameSize.height)
{
Point touchPoint = Point(_mouseX,_mouseY);
EventManager::getInstance()->sendMessage(MSG_SingleTouchBegan,&touchPoint);
}
}
else if(GLFW_RELEASE == action)
{
if (_captured)
{
_captured = false;
if (_mouseX < frameSize.width && _mouseX > 0.f && _mouseY > 0.f && _mouseY < frameSize.height)
{
Point touchPoint = Point(_mouseX,_mouseY);
EventManager::getInstance()->sendMessage(MSG_SingleTouchEnded,&touchPoint);
}
}
}
}
}
static void onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
{
Size frameSize = Engine::getInstance()->getFrameSize();
_mouseX = (float)x;
_mouseY = frameSize.height - (float)y;
if (_captured)
{
if (_mouseX < frameSize.width && _mouseX > 0.f && _mouseY > 0.f && _mouseY < frameSize.height)
{
Point touchPoint = Point(_mouseX,_mouseY);
EventManager::getInstance()->sendMessage(MSG_SingleTouchMoved,&touchPoint);
}
}
}bool GameScene::handerMessage(unsigned int message,void* param)
{
switch (message)
{
case MSG_SingleTouchBegan:
{
Point* touPoint = (Point*) param;
Rect rect = spr1->getRect(spr1->getPosition());
if (rect.containsPoint(*touPoint))
{
LOG("touch begin is in sprite\r\n");
}
}
break;
case MSG_SingleTouchMoved:
{
Point* touPoint = (Point*) param;
Rect rect = spr1->getRect(spr1->getPosition());
if (rect.containsPoint(*touPoint))
{
LOG("touch move is in sprite\r\n");
}
}
break;
case MSG_SingleTouchEnded:
{
Point* touPoint = (Point*) param;
Rect rect = spr1->getRect(spr1->getPosition());
if (rect.containsPoint(*touPoint))
{
LOG("touch end is in sprite\r\n");
}
}
break;
default:
break;
}
return true;
}
本文介绍了一个基于Win32的鼠标点击及消息管理系统。通过使用事件管理器类,文章详细解释了如何处理鼠标左键的按下、移动及释放事件,并将这些事件转化为自定义的消息类型,最终传递给游戏场景中的节点进行处理。

被折叠的 条评论
为什么被折叠?



