update: KeyEvent

This commit is contained in:
Nomango 2018-09-11 15:31:50 +08:00
parent 816f3f4dde
commit 0b40bbf0bc
5 changed files with 38 additions and 70 deletions

View File

@ -23,24 +23,19 @@ namespace e2d
LPARAM l_param LPARAM l_param
); );
// 获取事件类型
KeyEvent::Type GetType() const;
// 获取按键键值 // 获取按键键值
KeyCode GetCode() const; KeyCode GetCode() const;
// 获取按键次数 // 获取按键次数
int GetCount() const; int GetCount() const;
// 获取事件类型
KeyEvent::Type GetType() const;
// VK 键值转换
static KeyCode ToKeyCode(
WPARAM w_param
);
protected: protected:
int count_; UINT message_;
KeyCode code_; WPARAM w_param_;
KeyEvent::Type type_; LPARAM l_param_;
}; };
@ -106,22 +101,4 @@ namespace e2d
LPARAM l_param_; LPARAM l_param_;
}; };
// 事件接收目标
class EventTarget
{
public:
// 分发鼠标消息
virtual bool Dispatch(
const MouseEvent& e,
bool handled
) = 0;
// 分发按键消息
virtual bool Dispatch(
const KeyEvent& e,
bool handled
) = 0;
};
} }

View File

@ -113,7 +113,6 @@ namespace e2d
// 场景 // 场景
class Scene class Scene
: public Ref : public Ref
, public EventTarget
{ {
public: public:
Scene(); Scene();
@ -158,16 +157,14 @@ namespace e2d
void Draw(); void Draw();
// 分发鼠标消息 // 分发鼠标消息
virtual bool Dispatch( virtual void Dispatch(
const MouseEvent& e, const MouseEvent& e
bool handled );
) override;
// 分发按键消息 // 分发按键消息
virtual bool Dispatch( virtual void Dispatch(
const KeyEvent& e, const KeyEvent& e
bool handled );
) override;
// 设置转换矩阵 // 设置转换矩阵
void SetTransform( void SetTransform(
@ -417,7 +414,6 @@ namespace e2d
// 节点 // 节点
class Node class Node
: public Ref : public Ref
, public EventTarget
{ {
friend class Game; friend class Game;
friend class Scene; friend class Scene;
@ -813,13 +809,13 @@ namespace e2d
virtual bool Dispatch( virtual bool Dispatch(
const MouseEvent& e, const MouseEvent& e,
bool handled bool handled
) override; );
// 分发按键消息 // 分发按键消息
virtual bool Dispatch( virtual bool Dispatch(
const KeyEvent& e, const KeyEvent& e,
bool handled bool handled
) override; );
protected: protected:
E2D_DISABLE_COPY(Node); E2D_DISABLE_COPY(Node);

View File

@ -2,30 +2,15 @@
e2d::KeyEvent::KeyEvent(UINT message, WPARAM w_param, LPARAM l_param) e2d::KeyEvent::KeyEvent(UINT message, WPARAM w_param, LPARAM l_param)
: code_(KeyCode(w_param)) : message_(message)
, type_(Type(message)) , w_param_(w_param)
, count_(static_cast<int>((DWORD)l_param & 0x0000FFFF)) , l_param_(l_param)
{ {
} }
e2d::KeyCode e2d::KeyEvent::GetCode() const e2d::KeyCode e2d::KeyEvent::GetCode() const
{ {
return code_; switch (w_param_)
}
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)
{ {
case 'A': return KeyCode::A; case 'A': return KeyCode::A;
case 'B': return KeyCode::B; case 'B': return KeyCode::B;
@ -83,3 +68,13 @@ e2d::KeyCode e2d::KeyEvent::ToKeyCode(WPARAM w_param)
default: return KeyCode::Unknown; default: return KeyCode::Unknown;
} }
} }
int e2d::KeyEvent::GetCount() const
{
return static_cast<int>((DWORD)l_param_ & 0x0000FFFF);
}
e2d::KeyEvent::Type e2d::KeyEvent::GetType() const
{
return Type(message_);
}

View File

@ -423,9 +423,10 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT msg, WPARAM w_param, LPARAM l_param
if (game->IsTransitioning()) if (game->IsTransitioning())
break; 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; result = 0;
@ -440,9 +441,10 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT msg, WPARAM w_param, LPARAM l_param
if (game->IsTransitioning()) if (game->IsTransitioning())
break; 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; result = 0;

View File

@ -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_) 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_) if (root_)
{ {
return root_->Dispatch(e, handled); root_->Dispatch(e, false);
} }
return false;
} }
void e2d::Scene::SetTransform(const D2D1::Matrix3x2F& matrix) void e2d::Scene::SetTransform(const D2D1::Matrix3x2F& matrix)