From 0b40bbf0bc7237ecc454d3f3995da340a826ae48 Mon Sep 17 00:00:00 2001 From: Nomango Date: Tue, 11 Sep 2018 15:31:50 +0800 Subject: [PATCH] update: KeyEvent --- core/e2devent.h | 35 ++++++----------------------------- core/e2dobject.h | 20 ++++++++------------ core/events/KeyEvent.cpp | 33 ++++++++++++++------------------- core/modules/Window.cpp | 10 ++++++---- core/objects/Scene.cpp | 10 ++++------ 5 files changed, 38 insertions(+), 70 deletions(-) diff --git a/core/e2devent.h b/core/e2devent.h index fedb137c..c19d48ca 100644 --- a/core/e2devent.h +++ b/core/e2devent.h @@ -23,24 +23,19 @@ namespace e2d LPARAM l_param ); + // 获取事件类型 + KeyEvent::Type GetType() const; + // 获取按键键值 KeyCode GetCode() const; // 获取按键次数 int GetCount() const; - // 获取事件类型 - KeyEvent::Type GetType() const; - - // VK 键值转换 - static KeyCode ToKeyCode( - WPARAM w_param - ); - protected: - int count_; - KeyCode code_; - KeyEvent::Type type_; + UINT message_; + WPARAM w_param_; + LPARAM l_param_; }; @@ -106,22 +101,4 @@ namespace e2d LPARAM l_param_; }; - - // 事件接收目标 - class EventTarget - { - public: - // 分发鼠标消息 - virtual bool Dispatch( - const MouseEvent& e, - bool handled - ) = 0; - - // 分发按键消息 - virtual bool Dispatch( - const KeyEvent& e, - bool handled - ) = 0; - }; - } \ No newline at end of file diff --git a/core/e2dobject.h b/core/e2dobject.h index eded1957..355a7646 100644 --- a/core/e2dobject.h +++ b/core/e2dobject.h @@ -113,7 +113,6 @@ namespace e2d // 场景 class Scene : public Ref - , public EventTarget { public: Scene(); @@ -158,16 +157,14 @@ namespace e2d void Draw(); // 分发鼠标消息 - virtual bool Dispatch( - const MouseEvent& e, - bool handled - ) override; + virtual void Dispatch( + const MouseEvent& e + ); // 分发按键消息 - virtual bool Dispatch( - const KeyEvent& e, - bool handled - ) override; + virtual void Dispatch( + const KeyEvent& e + ); // 设置转换矩阵 void SetTransform( @@ -417,7 +414,6 @@ namespace e2d // 节点 class Node : public Ref - , public EventTarget { friend class Game; friend class Scene; @@ -813,13 +809,13 @@ namespace e2d virtual bool Dispatch( const MouseEvent& e, bool handled - ) override; + ); // 分发按键消息 virtual bool Dispatch( const KeyEvent& e, bool handled - ) override; + ); protected: E2D_DISABLE_COPY(Node); diff --git a/core/events/KeyEvent.cpp b/core/events/KeyEvent.cpp index ca124ab2..54f6cdc4 100644 --- a/core/events/KeyEvent.cpp +++ b/core/events/KeyEvent.cpp @@ -2,30 +2,15 @@ e2d::KeyEvent::KeyEvent(UINT message, WPARAM w_param, LPARAM l_param) - : code_(KeyCode(w_param)) - , type_(Type(message)) - , count_(static_cast((DWORD)l_param & 0x0000FFFF)) + : message_(message) + , w_param_(w_param) + , l_param_(l_param) { } e2d::KeyCode e2d::KeyEvent::GetCode() const { - return code_; -} - -int e2d::KeyEvent::GetCount() const -{ - return count_; -} - -e2d::KeyEvent::Type e2d::KeyEvent::GetType() const -{ - return type_; -} - -e2d::KeyCode e2d::KeyEvent::ToKeyCode(WPARAM w_param) -{ - switch (w_param) + switch (w_param_) { case 'A': return KeyCode::A; case 'B': return KeyCode::B; @@ -83,3 +68,13 @@ e2d::KeyCode e2d::KeyEvent::ToKeyCode(WPARAM w_param) default: return KeyCode::Unknown; } } + +int e2d::KeyEvent::GetCount() const +{ + return static_cast((DWORD)l_param_ & 0x0000FFFF); +} + +e2d::KeyEvent::Type e2d::KeyEvent::GetType() const +{ + return Type(message_); +} diff --git a/core/modules/Window.cpp b/core/modules/Window.cpp index 52193c9e..ead39ba3 100644 --- a/core/modules/Window.cpp +++ b/core/modules/Window.cpp @@ -423,9 +423,10 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT msg, WPARAM w_param, LPARAM l_param if (game->IsTransitioning()) break; - if (game->GetCurrentScene()) + auto curr_scene = game->GetCurrentScene(); + if (curr_scene) { - game->GetCurrentScene()->Dispatch(MouseEvent(msg, w_param, l_param), false); + curr_scene->Dispatch(MouseEvent(msg, w_param, l_param)); } } result = 0; @@ -440,9 +441,10 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT msg, WPARAM w_param, LPARAM l_param if (game->IsTransitioning()) break; - if (game->GetCurrentScene()) + auto curr_scene = game->GetCurrentScene(); + if (curr_scene) { - game->GetCurrentScene()->Dispatch(KeyEvent(msg, w_param, l_param), false); + curr_scene->Dispatch(KeyEvent(msg, w_param, l_param)); } } result = 0; diff --git a/core/objects/Scene.cpp b/core/objects/Scene.cpp index a543d9d1..0b64a647 100644 --- a/core/objects/Scene.cpp +++ b/core/objects/Scene.cpp @@ -83,22 +83,20 @@ void e2d::Scene::Draw() } } -bool e2d::Scene::Dispatch(const MouseEvent & e, bool handled) +void e2d::Scene::Dispatch(const MouseEvent & e) { if (root_) { - return root_->Dispatch(e, handled); + root_->Dispatch(e, false); } - return false; } -bool e2d::Scene::Dispatch(const KeyEvent & e, bool handled) +void e2d::Scene::Dispatch(const KeyEvent & e) { if (root_) { - return root_->Dispatch(e, handled); + root_->Dispatch(e, false); } - return false; } void e2d::Scene::SetTransform(const D2D1::Matrix3x2F& matrix)