增加鼠标和按键监听

This commit is contained in:
Nomango 2017-10-13 17:14:00 +08:00
parent 8da04219bf
commit fe192ad78d
15 changed files with 582 additions and 229 deletions

View File

@ -42,7 +42,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">

View File

@ -15,6 +15,18 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
node->setSize(30, 180);
scene->add(node);
auto mouselistener = new EMouseListener(L"listener", [=] {
if (!EMouseMsg::isLButtonDown())
{
if (EMouseMsg::getMsg() == EMouseMsg::MOVE)
{
node->setPos(EMouseMsg::getPos());
}
}
});
EMsgManager::addListener(mouselistener);
app.enterScene(scene);
app.run();

View File

@ -99,7 +99,7 @@ bool e2d::EApp::init(e2d::EString title, UINT32 width, UINT32 height, bool bShow
GetHWnd() = CreateWindow(
L"E2DApp",
m_sTitle.c_str(),
WS_OVERLAPPEDWINDOW,
WS_OVERLAPPEDWINDOW | CS_DBLCLKS,
CW_USEDEFAULT,
CW_USEDEFAULT,
static_cast<UINT>(ceil(width * dpiX / 96.f)),
@ -595,7 +595,16 @@ LRESULT e2d::EApp::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam
case WM_MOUSEMOVE:
case WM_MOUSEWHEEL:
{
EMsgManager::setMouseMsg(message);
EMsgManager::MouseProc(message, wParam, lParam);
}
result = 0;
break;
case WM_KEYDOWN:
case WM_KEYUP:
case WM_CHAR:
{
EMsgManager::KeyboardProc(message, wParam, lParam);
}
result = 0;
break;

View File

@ -42,7 +42,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
@ -196,6 +196,10 @@
<ClCompile Include="Base\EObject.cpp" />
<ClCompile Include="Base\EScene.cpp" />
<ClCompile Include="Node\ENode.cpp" />
<ClCompile Include="Tool\EKeyListener.cpp" />
<ClCompile Include="Tool\EListener.cpp" />
<ClCompile Include="Tool\EMouseClickListener.cpp" />
<ClCompile Include="Tool\EMouseDraggedListener.cpp" />
<ClCompile Include="Tool\EMouseListener.cpp" />
<ClCompile Include="Tool\EMsgManager.cpp" />
<ClCompile Include="Tool\EObjectManager.cpp" />

View File

@ -42,6 +42,18 @@
<ClCompile Include="Tool\EMouseListener.cpp">
<Filter>Tool\Listener</Filter>
</ClCompile>
<ClCompile Include="Tool\EListener.cpp">
<Filter>Tool\Listener</Filter>
</ClCompile>
<ClCompile Include="Tool\EKeyListener.cpp">
<Filter>Tool\Listener</Filter>
</ClCompile>
<ClCompile Include="Tool\EMouseClickListener.cpp">
<Filter>Tool\Listener</Filter>
</ClCompile>
<ClCompile Include="Tool\EMouseDraggedListener.cpp">
<Filter>Tool\Listener</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Win\winbase.h">

View File

@ -93,22 +93,22 @@ e2d::ERect e2d::ENode::getRect() const
void e2d::ENode::setX(int x)
{
m_Rect.TopLeft().x = x;
m_Rect.MoveToX(x);
}
void e2d::ENode::setY(int y)
{
m_Rect.TopLeft().y = y;
m_Rect.MoveToY(y);
}
void e2d::ENode::setPos(int x, int y)
{
m_Rect.TopLeft().SetPoint(x, y);
m_Rect.MoveToXY(x, y);
}
void e2d::ENode::setPos(EPoint p)
{
m_Rect.TopLeft() = p;
m_Rect.MoveToXY(p.x, p.y);
}
void e2d::ENode::move(int x, int y)
@ -137,7 +137,7 @@ void e2d::ENode::setSize(UINT32 width, UINT32 height)
setHeight(height);
}
void e2d::ENode::setSize(e2d::ESize size)
void e2d::ENode::setSize(ESize size)
{
setSize(size.cx, size.cy);
}
@ -153,7 +153,7 @@ void e2d::ENode::setRect(EPoint leftTop, EPoint rightBottom)
m_Rect.BottomRight() = rightBottom;
}
void e2d::ENode::setRect(e2d::ERect rect)
void e2d::ENode::setRect(ERect rect)
{
m_Rect = rect;
}
@ -168,7 +168,7 @@ void e2d::ENode::setZOrder(int z)
m_nZOrder = z;
}
void e2d::ENode::setParent(e2d::ENode * parent)
void e2d::ENode::setParent(ENode * parent)
{
m_pParent = parent;
}
@ -183,7 +183,7 @@ e2d::EScene * &e2d::ENode::getParentScene()
return m_pParentScene;
}
void e2d::ENode::setParentScene(e2d::EScene * scene)
void e2d::ENode::setParentScene(EScene * scene)
{
m_pParentScene = scene;
}

View File

@ -0,0 +1,33 @@
#include "..\etools.h"
e2d::EKeyListener::EKeyListener()
: EListener()
{
}
e2d::EKeyListener::EKeyListener(EString name)
: EListener(name)
{
}
e2d::EKeyListener::EKeyListener(const KEY_LISTENER_CALLBACK & callback)
: EListener()
{
m_callback = callback;
}
e2d::EKeyListener::EKeyListener(EString name, const KEY_LISTENER_CALLBACK & callback)
: EListener(name)
{
m_callback = callback;
}
void e2d::EKeyListener::runCallback()
{
m_callback();
}
void e2d::EKeyListener::setCallback(const KEY_LISTENER_CALLBACK & callback)
{
m_callback = callback;
}

60
Easy2D/Tool/EListener.cpp Normal file
View File

@ -0,0 +1,60 @@
#include "..\etools.h"
e2d::EListener::EListener()
: m_bRunning(false)
, m_bWaiting(false)
, m_sName(L"")
, m_pParentScene(nullptr)
{
}
e2d::EListener::EListener(EString name)
: EListener()
{
m_sName = name;
}
bool e2d::EListener::isRunning() const
{
return m_bRunning && !m_bWaiting;
}
void e2d::EListener::start()
{
m_bRunning = true;
}
void e2d::EListener::stop()
{
m_bRunning = false;
}
void e2d::EListener::wait()
{
m_bWaiting = true;
}
void e2d::EListener::notify()
{
m_bWaiting = false;
}
e2d::EString e2d::EListener::getName() const
{
return m_sName;
}
e2d::EScene * e2d::EListener::getParentScene() const
{
return m_pParentScene;
}
void e2d::EListener::setName(EString name)
{
m_sName = name;
}
void e2d::EListener::setParentScene(EScene * scene)
{
if (scene != nullptr) m_pParentScene = scene;
}

View File

@ -0,0 +1,30 @@
#include "..\etools.h"
e2d::EMouseClickListener::EMouseClickListener()
: EMouseListener()
{
}
e2d::EMouseClickListener::EMouseClickListener(EString name)
: EMouseListener(name)
{
}
e2d::EMouseClickListener::EMouseClickListener(const MOUSE_LISTENER_CALLBACK & callback)
: EMouseListener(callback)
{
}
e2d::EMouseClickListener::EMouseClickListener(EString name, const MOUSE_LISTENER_CALLBACK & callback)
: EMouseListener(name, callback)
{
}
void e2d::EMouseClickListener::runCallback()
{
if (EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DOWN ||
EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DBLCLK)
{
EMouseListener::runCallback();
}
}

View File

@ -0,0 +1,35 @@
#include "..\etools.h"
e2d::EMouseDraggedListener::EMouseDraggedListener()
: EMouseListener()
{
}
e2d::EMouseDraggedListener::EMouseDraggedListener(EString name)
: EMouseListener(name)
{
}
e2d::EMouseDraggedListener::EMouseDraggedListener(const MOUSE_DRAG_LISTENER_CALLBACK & callback)
: EMouseListener()
{
}
e2d::EMouseDraggedListener::EMouseDraggedListener(EString name, const MOUSE_DRAG_LISTENER_CALLBACK & callback)
: EMouseListener(name)
{
}
void e2d::EMouseDraggedListener::runCallback()
{
if (EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DOWN ||
EMouseMsg::getMsg() == EMouseMsg::LBUTTON_DBLCLK)
{
EMouseListener::runCallback();
}
}
void e2d::EMouseDraggedListener::setCallback(const MOUSE_DRAG_LISTENER_CALLBACK & callback)
{
m_callback = callback;
}

View File

@ -1,69 +1,33 @@
#include "..\etools.h"
e2d::EMouseListener::EMouseListener()
: m_bRunning(false)
, m_bWaiting(false)
, m_sName(L"")
, m_callback([] {})
, m_pParentScene(nullptr)
: EListener()
{
}
e2d::EMouseListener::EMouseListener(EString name)
: EMouseListener()
: EListener(name)
{
m_sName = name;
}
e2d::EMouseListener::EMouseListener(const MOUSE_CALLBACK & callback)
: EMouseListener()
e2d::EMouseListener::EMouseListener(const MOUSE_LISTENER_CALLBACK & callback)
: EListener()
{
m_callback = callback;
}
e2d::EMouseListener::EMouseListener(EString name, const MOUSE_CALLBACK & callback)
: EMouseListener()
e2d::EMouseListener::EMouseListener(EString name, const MOUSE_LISTENER_CALLBACK & callback)
: EListener(name)
{
m_sName = name;
m_callback = callback;
}
bool e2d::EMouseListener::isRunnint() const
{
return m_bRunning && !m_bWaiting;
}
void e2d::EMouseListener::start()
{
m_bRunning = true;
}
void e2d::EMouseListener::stop()
{
m_bRunning = false;
}
void e2d::EMouseListener::wait()
{
m_bWaiting = true;
}
void e2d::EMouseListener::notify()
{
m_bWaiting = false;
}
void e2d::EMouseListener::runCallback()
{
m_callback();
}
e2d::EString e2d::EMouseListener::getName() const
void e2d::EMouseListener::setCallback(const MOUSE_LISTENER_CALLBACK & callback)
{
return m_sName;
}
e2d::EScene * e2d::EMouseListener::getParentScene()
{
return m_pParentScene;
m_callback = callback;
}

View File

@ -2,81 +2,110 @@
#include "..\Win\winbase.h"
#include <vector>
static POINT p;
static e2d::EMouseMsg mouseMsg;
// 鼠标消息
e2d::EMouseMsg mouseMsg;
// 鼠标消息监听器
std::vector<e2d::EMouseListener*> m_vMouseListeners;
// 按键消息监听器
//std::vector<e2d::EKeyListener*> m_vKeyListeners;
void e2d::EMsgManager::setMouseMsg(UINT message)
DWORD e2d::EMouseMsg::getX()
{
return LOWORD(mouseMsg.m_lParam);
}
DWORD e2d::EMouseMsg::getY()
{
return HIWORD(mouseMsg.m_lParam);
}
e2d::EPoint e2d::EMouseMsg::getPos()
{
return EPoint(LOWORD(mouseMsg.m_lParam), HIWORD(mouseMsg.m_lParam));
}
bool e2d::EMouseMsg::isLButtonDown()
{
return GET_KEYSTATE_WPARAM(mouseMsg.m_wParam) == MK_LBUTTON;
}
bool e2d::EMouseMsg::isMButtonDown()
{
return GET_KEYSTATE_WPARAM(mouseMsg.m_wParam) == MK_MBUTTON;
}
bool e2d::EMouseMsg::isRButtonDown()
{
return GET_KEYSTATE_WPARAM(mouseMsg.m_wParam) == MK_RBUTTON;
}
bool e2d::EMouseMsg::isShiftDown()
{
return GET_KEYSTATE_WPARAM(mouseMsg.m_wParam) == MK_SHIFT;
}
bool e2d::EMouseMsg::isCtrlDown()
{
return GET_KEYSTATE_WPARAM(mouseMsg.m_wParam) == MK_CONTROL;
}
DWORD e2d::EMouseMsg::getWheelDelta()
{
return GET_WHEEL_DELTA_WPARAM(mouseMsg.m_wParam);
}
e2d::EMouseMsg::MSG e2d::EMouseMsg::getMsg()
{
return MSG(mouseMsg.m_nMsg);
}
void e2d::EMsgManager::MouseProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// 保存鼠标消息
mouseMsg.m_nMsg = message;
mouseMsg.m_wParam = wParam;
mouseMsg.m_lParam = lParam;
// 执行鼠标消息监听函数
for (auto mlistener : m_vMouseListeners)
{
if (mlistener->isRunning())
{
mlistener->runCallback();
}
}
}
void e2d::EMsgManager::KeyboardProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_LBUTTONUP:
mouseMsg.m_bLButtonDown = false;
break;
case WM_LBUTTONDOWN:
mouseMsg.m_bLButtonDown = true;
break;
case WM_LBUTTONDBLCLK:
mouseMsg.m_bLButtonDown = true;
break;
case WM_MBUTTONUP:
mouseMsg.m_bMButtonDown = false;
break;
case WM_MBUTTONDOWN:
mouseMsg.m_bMButtonDown = true;
break;
case WM_MBUTTONDBLCLK:
mouseMsg.m_bMButtonDown = true;
break;
case WM_RBUTTONUP:
mouseMsg.m_bRButtonDown = false;
break;
case WM_RBUTTONDOWN:
mouseMsg.m_bRButtonDown = true;
break;
case WM_RBUTTONDBLCLK:
mouseMsg.m_bRButtonDown = true;
break;
case WM_MOUSEMOVE:
{
GetCursorPos(&p);
ScreenToClient(GetHWnd(), &p);
mouseMsg.m_Pos = p;
break;
}
case WM_MOUSEWHEEL:
case WM_KEYDOWN:
case WM_KEYUP:
case WM_CHAR:
break;
}
for (auto mlistener : m_vMouseListeners)
{
//mlistener->
if (mlistener->isRunning())
{
mlistener->runCallback();
}
}
}
e2d::EMouseMsg::MESSAGE e2d::EMsgManager::getMouseMsg()
{
return e2d::EMouseMsg::MESSAGE(mouseMsg.m_nMsg);
}
void e2d::EMsgManager::addListener(e2d::EMouseListener * listener)
{
if (listener)
{
listener->start();
listener->retain();
listener->setParentScene(EApp::get()->getLoadingScene());
m_vMouseListeners.push_back(listener);
}
}

View File

@ -9,9 +9,6 @@ namespace e2d
typedef std::wstring EString;
//typedef std::function<void(VK_KEY)> KEY_CALLBACK;
typedef std::function<void()> MOUSE_CALLBACK;
typedef CSize ESize;
typedef CPoint EPoint;
@ -26,6 +23,11 @@ typedef struct
} ESize_F;
typedef std::function<void()> KEY_LISTENER_CALLBACK;
typedef std::function<void()> MOUSE_LISTENER_CALLBACK;
typedef std::function<void(EPoint begin, EPoint end)> MOUSE_DRAG_LISTENER_CALLBACK;
class EColor
{
public:

View File

@ -7,6 +7,8 @@ namespace e2d
class ENode :
public EObject
{
friend EScene;
public:
ENode();
@ -52,7 +54,7 @@ public:
virtual e2d::ENode* &getParent();
// 获取节点所在场景
e2d::EScene * &getParentScene();
EScene * &getParentScene();
// 设置节点是否显示
virtual void setVisiable(
@ -138,19 +140,18 @@ public:
// 设置父节点
virtual void setParent(
e2d::ENode* parent
ENode* parent
);
// 设置节点所在场景
void setParentScene(
e2d::EScene * scene
EScene * scene
);
protected:
friend e2d::EScene;
int m_nZOrder;
bool m_bVisiable;
e2d::ERect m_Rect;
ERect m_Rect;
EScene * m_pParentScene;
ENode * m_pParent;

View File

@ -27,6 +27,199 @@ private:
class EMouseMsg
{
friend EMsgManager;
public:
// 鼠标消息集合
enum MSG
{
MOVE = 0x0200, // 鼠标移动
LBUTTON_DOWN, // 鼠标左键按下
LBUTTON_UP, // 鼠标左键抬起
LBUTTON_DBLCLK, // 鼠标左键双击
RBUTTON_DOWN, // 鼠标右键按下
RBUTTON_UP, // 鼠标右键抬起
RBUTTON_DBLCLK, // 鼠标右键双击
MBUTTON_DOWN, // 鼠标中键按下
MBUTTON_UP, // 鼠标中键抬起
MBUTTON_DBLCLK, // 鼠标中键双击
WHEEL // 滑动滚轮
};
// 获取鼠标横坐标
static DWORD getX();
// 获取鼠标纵坐标
static DWORD getY();
// 获取鼠标坐标
static EPoint getPos();
// 获取鼠标左键按下状态
static bool isLButtonDown();
// 获取鼠标中键按下状态
static bool isMButtonDown();
// 获取鼠标右键按下状态
static bool isRButtonDown();
// 获取 Shift 按键状态
static bool isShiftDown();
// 获取 Ctrl 按键状态
static bool isCtrlDown();
// 获取鼠标滚轮值
static DWORD getWheelDelta();
// 获取当前鼠标消息
static MSG getMsg();
protected:
UINT m_nMsg = 0;
WPARAM m_wParam = 0;
LPARAM m_lParam = 0;
};
class EListener :
public EObject
{
public:
EListener();
EListener(
EString name
);
// 获取监听器状态
bool isRunning() const;
// 启动监听
void start();
// 停止监听
void stop();
// 进入等待状态
void wait();
// 唤醒
void notify();
// 获取监听器名称
EString getName() const;
// 获取监听器所在场景
EScene * getParentScene() const;
// 设置监听器名称
void setName(
EString name
);
// 设置监听器所在场景
void setParentScene(
EScene * scene
);
protected:
EString m_sName;
bool m_bRunning;
bool m_bWaiting;
EScene * m_pParentScene;
};
class EMouseListener :
public EListener
{
public:
EMouseListener();
EMouseListener(
EString name
);
EMouseListener(
const MOUSE_LISTENER_CALLBACK &callback
);
EMouseListener(
EString name,
const MOUSE_LISTENER_CALLBACK &callback
);
// 执行监听器回调函数
virtual void runCallback();
// 设置监听器回调函数
void setCallback(const MOUSE_LISTENER_CALLBACK &callback);
protected:
MOUSE_LISTENER_CALLBACK m_callback;
};
class EMouseClickListener :
public EMouseListener
{
public:
EMouseClickListener();
EMouseClickListener(
EString name
);
EMouseClickListener(
const MOUSE_LISTENER_CALLBACK &callback
);
EMouseClickListener(
EString name,
const MOUSE_LISTENER_CALLBACK &callback
);
// 执行监听器回调函数
virtual void runCallback() override;
};
class EMouseDraggedListener :
public EMouseListener
{
public:
EMouseDraggedListener();
EMouseDraggedListener(
EString name
);
EMouseDraggedListener(
const MOUSE_DRAG_LISTENER_CALLBACK &callback
);
EMouseDraggedListener(
EString name,
const MOUSE_DRAG_LISTENER_CALLBACK &callback
);
// 执行监听器回调函数
virtual void runCallback() override;
// 设置监听器回调函数
void setCallback(const MOUSE_DRAG_LISTENER_CALLBACK &callback);
protected:
EPoint m_Begin;
EPoint m_End;
MOUSE_DRAG_LISTENER_CALLBACK m_callback;
};
class EKeyMsg
{
public:
// 鼠标消息集合
@ -44,73 +237,36 @@ public:
MBUTTON_DBLCLK, // 鼠标中键双击
WHEEL // 滑动滚轮
};
public:
EPoint m_Pos;
UINT m_nMsg = 0;
bool m_bLButtonDown = false;
bool m_bRButtonDown = false;
bool m_bMButtonDown = false;
};
class EMouseListener :
public EObject
class EKeyListener :
public EListener
{
friend EMsgManager;
public:
EMouseListener();
EKeyListener();
EMouseListener(
EKeyListener(
EString name
);
);
EMouseListener(
const MOUSE_CALLBACK &callback
);
EKeyListener(
const KEY_LISTENER_CALLBACK &callback
);
EMouseListener(
EKeyListener(
EString name,
const MOUSE_CALLBACK &callback
);
// 获取监听器状态
bool isRunnint() const;
// 启动监听
void start();
// 停止监听
void stop();
// 进入等待状态
void wait();
// 唤醒
void notify();
const KEY_LISTENER_CALLBACK &callback
);
// 执行监听器回调函数
void runCallback();
// 获取监听器名称
EString getName() const;
// 获取监听器所在场景
EScene * getParentScene();
// 设置监听器名称
void setName(EString name);
// 设置监听器回调函数
void setCallback(const MOUSE_CALLBACK &callback);
void setCallback(const KEY_LISTENER_CALLBACK &callback);
protected:
EString m_sName;
bool m_bRunning;
bool m_bWaiting;
MOUSE_CALLBACK m_callback;
EScene * m_pParentScene;
KEY_LISTENER_CALLBACK m_callback;
};
@ -119,11 +275,17 @@ class EMsgManager
friend EApp;
public:
static void setMouseMsg(
UINT message
static void MouseProc(
UINT message,
WPARAM wParam,
LPARAM lParam
);
static e2d::EMouseMsg::MESSAGE getMouseMsg();
static void KeyboardProc(
UINT message,
WPARAM wParam,
LPARAM lParam
);
static void addListener(
EMouseListener * listener