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
);
// 获取事件类型
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;
};
}

View File

@ -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);

View File

@ -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<int>((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<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())
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;

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_)
{
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)