Update Input
This commit is contained in:
parent
bcc92abbef
commit
297e028779
|
|
@ -37,22 +37,22 @@ namespace kiwano
|
|||
io.ImeWindowHandle = target_window_;
|
||||
|
||||
// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array that we will update during the application lifetime.
|
||||
io.KeyMap[ImGuiKey_Tab] = KeyCode::Tab;
|
||||
io.KeyMap[ImGuiKey_LeftArrow] = KeyCode::Left;
|
||||
io.KeyMap[ImGuiKey_RightArrow] = KeyCode::Right;
|
||||
io.KeyMap[ImGuiKey_UpArrow] = KeyCode::Up;
|
||||
io.KeyMap[ImGuiKey_DownArrow] = KeyCode::Down;
|
||||
io.KeyMap[ImGuiKey_Delete] = KeyCode::Delete;
|
||||
io.KeyMap[ImGuiKey_Backspace] = KeyCode::Back;
|
||||
io.KeyMap[ImGuiKey_Space] = KeyCode::Space;
|
||||
io.KeyMap[ImGuiKey_Enter] = KeyCode::Enter;
|
||||
io.KeyMap[ImGuiKey_Escape] = KeyCode::Esc;
|
||||
io.KeyMap[ImGuiKey_A] = KeyCode::A;
|
||||
io.KeyMap[ImGuiKey_C] = KeyCode::C;
|
||||
io.KeyMap[ImGuiKey_V] = KeyCode::V;
|
||||
io.KeyMap[ImGuiKey_X] = KeyCode::X;
|
||||
io.KeyMap[ImGuiKey_Y] = KeyCode::Y;
|
||||
io.KeyMap[ImGuiKey_Z] = KeyCode::Z;
|
||||
io.KeyMap[ImGuiKey_Tab] = (int)KeyCode::Tab;
|
||||
io.KeyMap[ImGuiKey_LeftArrow] = (int)KeyCode::Left;
|
||||
io.KeyMap[ImGuiKey_RightArrow] = (int)KeyCode::Right;
|
||||
io.KeyMap[ImGuiKey_UpArrow] = (int)KeyCode::Up;
|
||||
io.KeyMap[ImGuiKey_DownArrow] = (int)KeyCode::Down;
|
||||
io.KeyMap[ImGuiKey_Delete] = (int)KeyCode::Delete;
|
||||
io.KeyMap[ImGuiKey_Backspace] = (int)KeyCode::Back;
|
||||
io.KeyMap[ImGuiKey_Space] = (int)KeyCode::Space;
|
||||
io.KeyMap[ImGuiKey_Enter] = (int)KeyCode::Enter;
|
||||
io.KeyMap[ImGuiKey_Escape] = (int)KeyCode::Esc;
|
||||
io.KeyMap[ImGuiKey_A] = (int)KeyCode::A;
|
||||
io.KeyMap[ImGuiKey_C] = (int)KeyCode::C;
|
||||
io.KeyMap[ImGuiKey_V] = (int)KeyCode::V;
|
||||
io.KeyMap[ImGuiKey_X] = (int)KeyCode::X;
|
||||
io.KeyMap[ImGuiKey_Y] = (int)KeyCode::Y;
|
||||
io.KeyMap[ImGuiKey_Z] = (int)KeyCode::Z;
|
||||
|
||||
ImGui_Impl_Init(Renderer::Instance());
|
||||
}
|
||||
|
|
@ -104,7 +104,7 @@ namespace kiwano
|
|||
{
|
||||
if (evt->IsType<MouseDownEvent>())
|
||||
{
|
||||
int button = dynamic_cast<MouseDownEvent*>(evt)->button;
|
||||
MouseButton button = dynamic_cast<MouseDownEvent*>(evt)->button;
|
||||
int index = 0;
|
||||
if (button == MouseButton::Left) index = 0;
|
||||
else if (button == MouseButton::Right) index = 1;
|
||||
|
|
@ -113,7 +113,7 @@ namespace kiwano
|
|||
}
|
||||
else if (evt->IsType<MouseUpEvent>())
|
||||
{
|
||||
int button = dynamic_cast<MouseUpEvent*>(evt)->button;
|
||||
MouseButton button = dynamic_cast<MouseUpEvent*>(evt)->button;
|
||||
int index = 0;
|
||||
if (button == MouseButton::Left) index = 0;
|
||||
else if (button == MouseButton::Right) index = 1;
|
||||
|
|
@ -130,13 +130,13 @@ namespace kiwano
|
|||
{
|
||||
if (evt->IsType<KeyDownEvent>())
|
||||
{
|
||||
int key = dynamic_cast<KeyDownEvent*>(evt)->code;
|
||||
io.KeysDown[key] = true;
|
||||
KeyCode key = dynamic_cast<KeyDownEvent*>(evt)->code;
|
||||
io.KeysDown[(int)key] = true;
|
||||
}
|
||||
else if (evt->IsType<KeyUpEvent>())
|
||||
{
|
||||
int key = dynamic_cast<KeyUpEvent*>(evt)->code;
|
||||
io.KeysDown[key] = false;
|
||||
KeyCode key = dynamic_cast<KeyUpEvent*>(evt)->code;
|
||||
io.KeysDown[(int)key] = false;
|
||||
}
|
||||
else if (evt->IsType<KeyCharEvent>())
|
||||
{
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ namespace kiwano
|
|||
|
||||
void Stage::RenderBorder(RenderContext& ctx)
|
||||
{
|
||||
ctx.SetBrushOpacity(1.0f);
|
||||
ctx.SetBrushOpacity(GetDisplayedOpacity());
|
||||
|
||||
if (!border_fill_brush_)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -117,8 +117,8 @@ namespace kiwano
|
|||
|
||||
current_stage_.reset();
|
||||
next_stage_.reset();
|
||||
debug_actor_.reset();
|
||||
transition_.reset();
|
||||
debug_actor_.reset();
|
||||
}
|
||||
|
||||
void Director::OnUpdate(Duration dt)
|
||||
|
|
@ -163,11 +163,8 @@ namespace kiwano
|
|||
else if (current_stage_)
|
||||
{
|
||||
current_stage_->Render(ctx);
|
||||
}
|
||||
|
||||
if (render_border_enabled_)
|
||||
{
|
||||
if (current_stage_)
|
||||
if (render_border_enabled_)
|
||||
{
|
||||
current_stage_->RenderBorder(ctx);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,13 +10,13 @@ namespace kiwano
|
|||
|
||||
KeyDownEvent::KeyDownEvent()
|
||||
: KeyEvent(KGE_EVENT(KeyDownEvent))
|
||||
, code(0)
|
||||
, code()
|
||||
{
|
||||
}
|
||||
|
||||
KeyUpEvent::KeyUpEvent()
|
||||
: KeyEvent(KGE_EVENT(KeyUpEvent))
|
||||
, code(0)
|
||||
, code()
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ namespace kiwano
|
|||
: public KeyEvent
|
||||
{
|
||||
public:
|
||||
KeyCode::Value code; ///< ¼üÖµ
|
||||
KeyCode code; ///< ¼üÖµ
|
||||
|
||||
KeyDownEvent();
|
||||
};
|
||||
|
|
@ -60,7 +60,7 @@ namespace kiwano
|
|||
: public KeyEvent
|
||||
{
|
||||
public:
|
||||
KeyCode::Value code; ///< ¼üÖµ
|
||||
KeyCode code; ///< ¼üÖµ
|
||||
|
||||
KeyUpEvent();
|
||||
};
|
||||
|
|
|
|||
|
|
@ -15,19 +15,19 @@ namespace kiwano
|
|||
|
||||
MouseDownEvent::MouseDownEvent()
|
||||
: MouseEvent(KGE_EVENT(MouseDownEvent))
|
||||
, button(0)
|
||||
, button()
|
||||
{
|
||||
}
|
||||
|
||||
MouseUpEvent::MouseUpEvent()
|
||||
: MouseEvent(KGE_EVENT(MouseUpEvent))
|
||||
, button(0)
|
||||
, button()
|
||||
{
|
||||
}
|
||||
|
||||
MouseClickEvent::MouseClickEvent()
|
||||
: MouseEvent(KGE_EVENT(MouseClickEvent))
|
||||
, button(0)
|
||||
, button()
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ namespace kiwano
|
|||
: public MouseEvent
|
||||
{
|
||||
public:
|
||||
MouseButton::Value button; ///< 報炎囚峙
|
||||
MouseButton button; ///< 報炎囚峙
|
||||
|
||||
MouseDownEvent();
|
||||
};
|
||||
|
|
@ -76,7 +76,7 @@ namespace kiwano
|
|||
: public MouseEvent
|
||||
{
|
||||
public:
|
||||
MouseButton::Value button; ///< 報炎囚峙
|
||||
MouseButton button; ///< 報炎囚峙
|
||||
|
||||
MouseUpEvent();
|
||||
};
|
||||
|
|
@ -87,7 +87,7 @@ namespace kiwano
|
|||
: public MouseEvent
|
||||
{
|
||||
public:
|
||||
MouseButton::Value button; ///< 報炎囚峙
|
||||
MouseButton button; ///< 報炎囚峙
|
||||
|
||||
MouseClickEvent();
|
||||
};
|
||||
|
|
|
|||
|
|
@ -25,104 +25,98 @@ namespace kiwano
|
|||
{
|
||||
/// \~chinese
|
||||
/// @brief 報炎梓囚
|
||||
struct MouseButton
|
||||
enum class MouseButton
|
||||
{
|
||||
typedef int Value;
|
||||
Left, ///< 鼠标左键
|
||||
Right, ///< 鼠标右键
|
||||
Middle, ///< 鼠标中键
|
||||
|
||||
enum : Value
|
||||
{
|
||||
Left = VK_LBUTTON, ///< 鼠标左键
|
||||
Right = VK_RBUTTON, ///< 鼠标右键
|
||||
Middle = VK_MBUTTON ///< 鼠标中键
|
||||
};
|
||||
Last
|
||||
};
|
||||
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 梓囚囚峙
|
||||
struct KeyCode
|
||||
enum class KeyCode
|
||||
{
|
||||
typedef int Value;
|
||||
Unknown, ///< 未知
|
||||
Up, ///< 上键
|
||||
Left, ///< 左键
|
||||
Right, ///< 右键
|
||||
Down, ///< 下键
|
||||
Enter, ///< 回车键
|
||||
Space, ///< 空格键
|
||||
Esc, ///< 退出键
|
||||
Ctrl, ///< CTRL键
|
||||
Shift, ///< SHIFT键
|
||||
Alt, ///< ALT键
|
||||
Tab, ///< TAB键
|
||||
Delete, ///< 删除键
|
||||
Back, ///< 退格键
|
||||
Super, ///< Cmd|Super|Windows键
|
||||
|
||||
enum : Value
|
||||
{
|
||||
Unknown = 0, ///< 未知
|
||||
Up = VK_UP, ///< 上键
|
||||
Left = VK_LEFT, ///< 左键
|
||||
Right = VK_RIGHT, ///< 右键
|
||||
Down = VK_DOWN, ///< 下键
|
||||
Enter = VK_RETURN, ///< 回车键
|
||||
Space = VK_SPACE, ///< 空格键
|
||||
Esc = VK_ESCAPE, ///< 退出键
|
||||
Ctrl = VK_CONTROL, ///< CTRL键
|
||||
Shift = VK_SHIFT, ///< SHIFT键
|
||||
Alt = VK_MENU, ///< ALT键
|
||||
Tab = VK_TAB, ///< TAB键
|
||||
Delete = VK_DELETE, ///< 删除键
|
||||
Back = VK_BACK, ///< 退格键
|
||||
Super = VK_LWIN, ///< Cmd/Super/Windows键
|
||||
A, ///< A键
|
||||
B, ///< B键
|
||||
C, ///< C键
|
||||
D, ///< D键
|
||||
E, ///< E键
|
||||
F, ///< F键
|
||||
G, ///< G键
|
||||
H, ///< H键
|
||||
I, ///< I键
|
||||
J, ///< J键
|
||||
K, ///< K键
|
||||
L, ///< L键
|
||||
M, ///< M键
|
||||
N, ///< N键
|
||||
O, ///< O键
|
||||
P, ///< P键
|
||||
Q, ///< Q键
|
||||
R, ///< R键
|
||||
S, ///< S键
|
||||
T, ///< T键
|
||||
U, ///< U键
|
||||
V, ///< V键
|
||||
W, ///< W键
|
||||
X, ///< X键
|
||||
Y, ///< Y键
|
||||
Z, ///< Z键
|
||||
|
||||
A = 0x41, ///< A键
|
||||
B, ///< B键
|
||||
C, ///< C键
|
||||
D, ///< D键
|
||||
E, ///< E键
|
||||
F, ///< F键
|
||||
G, ///< G键
|
||||
H, ///< H键
|
||||
I, ///< I键
|
||||
J, ///< J键
|
||||
K, ///< K键
|
||||
L, ///< L键
|
||||
M, ///< M键
|
||||
N, ///< N键
|
||||
O, ///< O键
|
||||
P, ///< P键
|
||||
Q, ///< Q键
|
||||
R, ///< R键
|
||||
S, ///< S键
|
||||
T, ///< T键
|
||||
U, ///< U键
|
||||
V, ///< V键
|
||||
W, ///< W键
|
||||
X, ///< X键
|
||||
Y, ///< Y键
|
||||
Z, ///< Z键
|
||||
Num0, ///< 数字0键
|
||||
Num1, ///< 数字1键
|
||||
Num2, ///< 数字2键
|
||||
Num3, ///< 数字3键
|
||||
Num4, ///< 数字4键
|
||||
Num5, ///< 数字5键
|
||||
Num6, ///< 数字6键
|
||||
Num7, ///< 数字7键
|
||||
Num8, ///< 数字8键
|
||||
Num9, ///< 数字9键
|
||||
|
||||
Num0 = 0x30, ///< 数字0键
|
||||
Num1, ///< 数字1键
|
||||
Num2, ///< 数字2键
|
||||
Num3, ///< 数字3键
|
||||
Num4, ///< 数字4键
|
||||
Num5, ///< 数字5键
|
||||
Num6, ///< 数字6键
|
||||
Num7, ///< 数字7键
|
||||
Num8, ///< 数字8键
|
||||
Num9, ///< 数字9键
|
||||
Numpad0, ///< 数字小键盘0键
|
||||
Numpad1, ///< 数字小键盘1键
|
||||
Numpad2, ///< 数字小键盘2键
|
||||
Numpad3, ///< 数字小键盘3键
|
||||
Numpad4, ///< 数字小键盘4键
|
||||
Numpad5, ///< 数字小键盘5键
|
||||
Numpad6, ///< 数字小键盘6键
|
||||
Numpad7, ///< 数字小键盘7键
|
||||
Numpad8, ///< 数字小键盘8键
|
||||
Numpad9, ///< 数字小键盘9键
|
||||
|
||||
Numpad0 = VK_NUMPAD0, ///< 数字小键盘0键
|
||||
Numpad1, ///< 数字小键盘1键
|
||||
Numpad2, ///< 数字小键盘2键
|
||||
Numpad3, ///< 数字小键盘3键
|
||||
Numpad4, ///< 数字小键盘4键
|
||||
Numpad5, ///< 数字小键盘5键
|
||||
Numpad6, ///< 数字小键盘6键
|
||||
Numpad7, ///< 数字小键盘7键
|
||||
Numpad8, ///< 数字小键盘8键
|
||||
Numpad9, ///< 数字小键盘9键
|
||||
F1, ///< F1键
|
||||
F2, ///< F2键
|
||||
F3, ///< F3键
|
||||
F4, ///< F4键
|
||||
F5, ///< F5键
|
||||
F6, ///< F6键
|
||||
F7, ///< F7键
|
||||
F8, ///< F8键
|
||||
F9, ///< F9键
|
||||
F10, ///< F10键
|
||||
F11, ///< F11键
|
||||
F12, ///< F12键
|
||||
|
||||
F1 = VK_F1, ///< F1键
|
||||
F2, ///< F2键
|
||||
F3, ///< F3键
|
||||
F4, ///< F4键
|
||||
F5, ///< F5键
|
||||
F6, ///< F6键
|
||||
F7, ///< F7键
|
||||
F8, ///< F8键
|
||||
F9, ///< F9键
|
||||
F10, ///< F10键
|
||||
F11, ///< F11键
|
||||
F12, ///< F12键
|
||||
};
|
||||
Last
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,10 +26,10 @@
|
|||
namespace kiwano
|
||||
{
|
||||
Input::Input()
|
||||
: want_update_(false)
|
||||
: want_update_keys_(false)
|
||||
, want_update_buttons_(false)
|
||||
, buttons_{}
|
||||
, keys_{}
|
||||
, keys_pressed_{}
|
||||
, keys_released_{}
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -39,37 +39,59 @@ namespace kiwano
|
|||
|
||||
void Input::AfterUpdate()
|
||||
{
|
||||
if (want_update_)
|
||||
if (want_update_keys_)
|
||||
{
|
||||
want_update_ = false;
|
||||
want_update_keys_ = false;
|
||||
::memcpy(keys_[Prev].data(), keys_[Current].data(), KEY_NUM * sizeof(bool));
|
||||
}
|
||||
|
||||
keys_pressed_.fill(false);
|
||||
keys_released_.fill(false);
|
||||
if (want_update_buttons_)
|
||||
{
|
||||
want_update_buttons_ = false;
|
||||
buttons_[Prev] = buttons_[Current];
|
||||
}
|
||||
}
|
||||
|
||||
bool Input::IsDown(int key_or_btn) const
|
||||
bool Input::IsDown(KeyCode key) const
|
||||
{
|
||||
KGE_ASSERT(key_or_btn >= 0 && key_or_btn < KEY_NUM);
|
||||
if (key_or_btn >= 0 && key_or_btn < KEY_NUM)
|
||||
return keys_[key_or_btn];
|
||||
return false;
|
||||
if (key == KeyCode::Unknown || key == KeyCode::Last)
|
||||
return false;
|
||||
return keys_[Current][size_t(key)];
|
||||
}
|
||||
|
||||
bool Input::WasPressed(int key_or_btn) const
|
||||
bool Input::WasPressed(KeyCode key) const
|
||||
{
|
||||
KGE_ASSERT(key_or_btn >= 0 && key_or_btn < KEY_NUM);
|
||||
if (key_or_btn >= 0 && key_or_btn < KEY_NUM)
|
||||
return keys_pressed_[key_or_btn];
|
||||
return false;
|
||||
if (key == KeyCode::Unknown || key == KeyCode::Last)
|
||||
return false;
|
||||
return keys_[Current][size_t(key)] && !keys_[Prev][size_t(key)];
|
||||
}
|
||||
|
||||
bool Input::WasReleased(int key_or_btn) const
|
||||
bool Input::WasReleased(KeyCode key) const
|
||||
{
|
||||
KGE_ASSERT(key_or_btn >= 0 && key_or_btn < KEY_NUM);
|
||||
if (key_or_btn >= 0 && key_or_btn < KEY_NUM)
|
||||
return keys_released_[key_or_btn];
|
||||
return false;
|
||||
if (key == KeyCode::Unknown || key == KeyCode::Last)
|
||||
return false;
|
||||
return !keys_[Current][size_t(key)] && keys_[Prev][size_t(key)];
|
||||
}
|
||||
|
||||
bool Input::IsDown(MouseButton btn) const
|
||||
{
|
||||
if (btn == MouseButton::Last)
|
||||
return false;
|
||||
return buttons_[Current][size_t(btn)];
|
||||
}
|
||||
|
||||
bool Input::WasPressed(MouseButton btn) const
|
||||
{
|
||||
if (btn == MouseButton::Last)
|
||||
return false;
|
||||
return buttons_[Current][size_t(btn)] && !buttons_[Prev][size_t(btn)];
|
||||
}
|
||||
|
||||
bool Input::WasReleased(MouseButton btn) const
|
||||
{
|
||||
if (btn == MouseButton::Last)
|
||||
return false;
|
||||
return !buttons_[Current][size_t(btn)] && buttons_[Prev][size_t(btn)];
|
||||
}
|
||||
|
||||
float Input::GetMouseX() const
|
||||
|
|
@ -87,16 +109,22 @@ namespace kiwano
|
|||
return mouse_pos_;
|
||||
}
|
||||
|
||||
void Input::UpdateKey(int key, bool down)
|
||||
void Input::UpdateKey(KeyCode key, bool down)
|
||||
{
|
||||
if (down && !keys_[key])
|
||||
keys_pressed_[key] = true;
|
||||
if (!down && keys_[key])
|
||||
keys_released_[key] = true;
|
||||
if (key == KeyCode::Unknown || key == KeyCode::Last)
|
||||
return;
|
||||
|
||||
keys_[key] = down;
|
||||
keys_[Current][size_t(key)] = down;
|
||||
want_update_keys_ = true;
|
||||
}
|
||||
|
||||
want_update_ = true;
|
||||
void Input::UpdateButton(MouseButton btn, bool down)
|
||||
{
|
||||
if (btn == MouseButton::Last)
|
||||
return;
|
||||
|
||||
buttons_[Current][size_t(btn)] = down;
|
||||
want_update_buttons_ = true;
|
||||
}
|
||||
|
||||
void Input::UpdateMousePos(const Point& pos)
|
||||
|
|
@ -114,11 +142,11 @@ namespace kiwano
|
|||
}
|
||||
else if (evt->IsType<MouseDownEvent>())
|
||||
{
|
||||
UpdateKey(dynamic_cast<MouseDownEvent*>(evt)->button, true);
|
||||
UpdateButton(dynamic_cast<MouseDownEvent*>(evt)->button, true);
|
||||
}
|
||||
else if (evt->IsType<MouseUpEvent>())
|
||||
{
|
||||
UpdateKey(dynamic_cast<MouseUpEvent*>(evt)->button, false);
|
||||
UpdateButton(dynamic_cast<MouseUpEvent*>(evt)->button, false);
|
||||
}
|
||||
}
|
||||
else if (evt->IsType<KeyEvent>())
|
||||
|
|
|
|||
|
|
@ -41,30 +41,44 @@ namespace kiwano
|
|||
public:
|
||||
/**
|
||||
* \~chinese
|
||||
* @brief 检测键盘或鼠标按键是否正被按下
|
||||
* @param key_or_btn 键值
|
||||
* @return 是否正被按下
|
||||
* @see kiwano::KeyCode kiwano::MouseButton
|
||||
* @brief 检测键盘按键是否正被按下
|
||||
* @param key 键值
|
||||
*/
|
||||
bool IsDown(int key_or_btn) const;
|
||||
bool IsDown(KeyCode key) const;
|
||||
|
||||
/**
|
||||
* \~chinese
|
||||
* @brief 检测键盘或鼠标按键是否刚被点击
|
||||
* @param key_or_btn 键值
|
||||
* @return 是否刚被点击
|
||||
* @see kiwano::KeyCode kiwano::MouseButton
|
||||
* @brief 检测键盘按键是否刚被点击
|
||||
* @param key 键值
|
||||
*/
|
||||
bool WasPressed(int key_or_btn) const;
|
||||
bool WasPressed(KeyCode key) const;
|
||||
|
||||
/**
|
||||
* \~chinese
|
||||
* @brief 检测键盘或鼠标按键是否刚抬起
|
||||
* @param key_or_btn 键值
|
||||
* @return 是否刚抬起
|
||||
* @see kiwano::KeyCode kiwano::MouseButton
|
||||
* @brief 检测键盘按键是否刚抬起
|
||||
* @param key 键值
|
||||
*/
|
||||
bool WasReleased(int key_or_btn) const;
|
||||
bool WasReleased(KeyCode key) const;
|
||||
/**
|
||||
* \~chinese
|
||||
* @brief 检测鼠标按键是否正被按下
|
||||
* @param btn 鼠标键值
|
||||
*/
|
||||
bool IsDown(MouseButton btn) const;
|
||||
|
||||
/**
|
||||
* \~chinese
|
||||
* @brief 检测鼠标按键是否刚被点击
|
||||
* @param btn 鼠标键值
|
||||
*/
|
||||
bool WasPressed(MouseButton btn) const;
|
||||
|
||||
/**
|
||||
* \~chinese
|
||||
* @brief 检测鼠标按键是否刚抬起
|
||||
* @param btn 鼠标键值
|
||||
*/
|
||||
bool WasReleased(MouseButton btn) const;
|
||||
|
||||
/**
|
||||
* \~chinese
|
||||
|
|
@ -101,17 +115,27 @@ namespace kiwano
|
|||
|
||||
~Input();
|
||||
|
||||
void UpdateKey(int, bool);
|
||||
void UpdateKey(KeyCode key, bool down);
|
||||
|
||||
void UpdateButton(MouseButton btn, bool down);
|
||||
|
||||
void UpdateMousePos(const Point& pos);
|
||||
|
||||
private:
|
||||
static const int KEY_NUM = 256;
|
||||
static const int KEY_NUM = int(KeyCode::Last);
|
||||
static const int BUTTON_NUM = int(MouseButton::Last);
|
||||
|
||||
bool want_update_;
|
||||
bool want_update_keys_;
|
||||
bool want_update_buttons_;
|
||||
Point mouse_pos_;
|
||||
std::array<bool, KEY_NUM> keys_;
|
||||
std::array<bool, KEY_NUM> keys_pressed_;
|
||||
std::array<bool, KEY_NUM> keys_released_;
|
||||
|
||||
enum KeyIndex : size_t
|
||||
{
|
||||
Current = 0,
|
||||
Prev
|
||||
};
|
||||
|
||||
std::array<bool, BUTTON_NUM> buttons_[2];
|
||||
std::array<bool, KEY_NUM> keys_[2];
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -89,11 +89,6 @@ namespace kiwano
|
|||
|
||||
void Window::Destroy()
|
||||
{
|
||||
while (!event_queue_.empty())
|
||||
{
|
||||
event_queue_.pop();
|
||||
}
|
||||
|
||||
should_close_ = true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -181,9 +181,9 @@ namespace kiwano
|
|||
|
||||
/**
|
||||
* \~chinese
|
||||
* @brief 是否需要关闭
|
||||
* @brief 窗口是否需要关闭
|
||||
*/
|
||||
virtual bool ShouldClose() = 0;
|
||||
bool ShouldClose();
|
||||
|
||||
/**
|
||||
* \~chinese
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@
|
|||
#include <kiwano/core/event/WindowEvent.h>
|
||||
#include <kiwano/core/Logger.h>
|
||||
|
||||
#include <Windowsx.h> // GET_X_LPARAM, GET_Y_LPARAM
|
||||
#include <imm.h> // ImmAssociateContext
|
||||
#pragma comment(lib, "imm32.lib")
|
||||
|
||||
|
|
@ -117,14 +118,12 @@ namespace kiwano
|
|||
|
||||
void SetCursor(CursorType cursor) override;
|
||||
|
||||
bool ShouldClose() override;
|
||||
|
||||
void Destroy() override;
|
||||
|
||||
private:
|
||||
void PumpEvents() override;
|
||||
|
||||
DWORD GetWindowStyle() const;
|
||||
DWORD GetStyle() const;
|
||||
|
||||
void UpdateCursor();
|
||||
|
||||
|
|
@ -138,6 +137,8 @@ namespace kiwano
|
|||
wchar_t* device_name_;
|
||||
WindowHandle handle_;
|
||||
CursorType mouse_cursor_;
|
||||
|
||||
std::array<KeyCode, 256> key_map_;
|
||||
};
|
||||
|
||||
WindowImpl::WindowImpl()
|
||||
|
|
@ -146,7 +147,48 @@ namespace kiwano
|
|||
, is_fullscreen_(false)
|
||||
, resizable_(false)
|
||||
, mouse_cursor_(CursorType::Arrow)
|
||||
, key_map_{}
|
||||
{
|
||||
// Keys
|
||||
key_map_[VK_UP] = KeyCode::Up;
|
||||
key_map_[VK_LEFT] = KeyCode::Left;
|
||||
key_map_[VK_RIGHT] = KeyCode::Right;
|
||||
key_map_[VK_DOWN] = KeyCode::Down;
|
||||
key_map_[VK_RETURN] = KeyCode::Enter;
|
||||
key_map_[VK_SPACE] = KeyCode::Space;
|
||||
key_map_[VK_ESCAPE] = KeyCode::Esc;
|
||||
key_map_[VK_CONTROL] = KeyCode::Ctrl;
|
||||
key_map_[VK_SHIFT] = KeyCode::Shift;
|
||||
key_map_[VK_MENU] = KeyCode::Alt;
|
||||
key_map_[VK_TAB] = KeyCode::Tab;
|
||||
key_map_[VK_DELETE] = KeyCode::Delete;
|
||||
key_map_[VK_BACK] = KeyCode::Back;
|
||||
|
||||
// VK_L* and VK_R*
|
||||
key_map_[VK_LCONTROL] = KeyCode::Ctrl;
|
||||
key_map_[VK_RCONTROL] = KeyCode::Ctrl;
|
||||
key_map_[VK_LSHIFT] = KeyCode::Shift;
|
||||
key_map_[VK_RSHIFT] = KeyCode::Shift;
|
||||
key_map_[VK_LMENU] = KeyCode::Alt;
|
||||
key_map_[VK_RMENU] = KeyCode::Alt;
|
||||
key_map_[VK_LWIN] = KeyCode::Super;
|
||||
key_map_[VK_RWIN] = KeyCode::Super;
|
||||
|
||||
// A - Z
|
||||
for (size_t i = 0, size = ('Z' - 'A'); i <= size; ++i)
|
||||
key_map_['A' + i] = KeyCode(size_t(KeyCode::A) + i);
|
||||
|
||||
// Num 0 - 9
|
||||
for (size_t i = 0; i < 9; ++i)
|
||||
key_map_['0' + i] = KeyCode(size_t(KeyCode::Num0) + i);
|
||||
|
||||
// Numpad 0 - 9
|
||||
for (size_t i = 0; i < 9; ++i)
|
||||
key_map_[VK_NUMPAD0 + i] = KeyCode(size_t(KeyCode::Numpad0) + i);
|
||||
|
||||
// F1 - F12
|
||||
for (size_t i = 0; i < 12; ++i)
|
||||
key_map_[VK_F1 + i] = KeyCode(size_t(KeyCode::F1) + i);
|
||||
}
|
||||
|
||||
WindowImpl::~WindowImpl()
|
||||
|
|
@ -216,7 +258,7 @@ namespace kiwano
|
|||
uint32_t screenh = monitor_info_ex.rcWork.bottom - monitor_info_ex.rcWork.top;
|
||||
|
||||
uint32_t win_width, win_height;
|
||||
AdjustWindow(width, height, GetWindowStyle(), &win_width, &win_height);
|
||||
AdjustWindow(width, height, GetStyle(), &win_width, &win_height);
|
||||
|
||||
left = monitor_info_ex.rcWork.left + (screenw - win_width) / 2;
|
||||
top = monitor_info_ex.rcWork.top + (screenh - win_height) / 2;
|
||||
|
|
@ -228,7 +270,7 @@ namespace kiwano
|
|||
is_fullscreen_ ? WS_EX_TOPMOST : 0,
|
||||
KGE_WND_CLASS_NAME,
|
||||
config.title.c_str(),
|
||||
GetWindowStyle(),
|
||||
GetStyle(),
|
||||
left,
|
||||
top,
|
||||
width,
|
||||
|
|
@ -309,7 +351,7 @@ namespace kiwano
|
|||
if (handle_ && !is_fullscreen_)
|
||||
{
|
||||
RECT rc = { 0, 0, LONG(width), LONG(height) };
|
||||
::AdjustWindowRect(&rc, GetWindowStyle(), false);
|
||||
::AdjustWindowRect(&rc, GetStyle(), false);
|
||||
|
||||
width = rc.right - rc.left;
|
||||
height = rc.bottom - rc.top;
|
||||
|
|
@ -335,7 +377,7 @@ namespace kiwano
|
|||
|
||||
MONITORINFOEX info = GetMoniterInfoEx(handle_);
|
||||
|
||||
::SetWindowLongPtr(handle_, GWL_STYLE, GetWindowStyle());
|
||||
::SetWindowLongPtr(handle_, GWL_STYLE, GetStyle());
|
||||
::SetWindowPos(handle_, HWND_TOPMOST, info.rcMonitor.top, info.rcMonitor.left, width, height, SWP_NOACTIVATE);
|
||||
}
|
||||
else
|
||||
|
|
@ -350,7 +392,7 @@ namespace kiwano
|
|||
int left = screenw > width ? ((screenw - width) / 2) : 0;
|
||||
int top = screenh > height ? ((screenh - height) / 2) : 0;
|
||||
|
||||
::SetWindowLongPtr(handle_, GWL_STYLE, GetWindowStyle());
|
||||
::SetWindowLongPtr(handle_, GWL_STYLE, GetStyle());
|
||||
::SetWindowPos(handle_, HWND_NOTOPMOST, left, top, width, height, SWP_DRAWFRAME | SWP_FRAMECHANGED);
|
||||
}
|
||||
|
||||
|
|
@ -363,11 +405,6 @@ namespace kiwano
|
|||
mouse_cursor_ = cursor;
|
||||
}
|
||||
|
||||
bool WindowImpl::ShouldClose()
|
||||
{
|
||||
return handle_ == nullptr;
|
||||
}
|
||||
|
||||
void WindowImpl::Destroy()
|
||||
{
|
||||
if (is_fullscreen_)
|
||||
|
|
@ -388,7 +425,7 @@ namespace kiwano
|
|||
Window::Destroy();
|
||||
}
|
||||
|
||||
DWORD WindowImpl::GetWindowStyle() const
|
||||
DWORD WindowImpl::GetStyle() const
|
||||
{
|
||||
return is_fullscreen_ ? (WINDOW_FULLSCREEN_STYLE) : (resizable_ ? (WINDOW_RESIZABLE_STYLE) : (WINDOW_FIXED_STYLE));
|
||||
}
|
||||
|
|
@ -447,25 +484,33 @@ namespace kiwano
|
|||
case WM_KEYDOWN:
|
||||
case WM_SYSKEYDOWN:
|
||||
{
|
||||
KeyDownEventPtr evt = new KeyDownEvent;
|
||||
evt->code = static_cast<int>(wparam);
|
||||
window->PushEvent(evt);
|
||||
KeyCode key = window->key_map_[size_t(wparam)];
|
||||
if (key != KeyCode::Unknown)
|
||||
{
|
||||
KeyDownEventPtr evt = new KeyDownEvent;
|
||||
evt->code = key;
|
||||
window->PushEvent(evt);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case WM_KEYUP:
|
||||
case WM_SYSKEYUP:
|
||||
{
|
||||
KeyUpEventPtr evt = new KeyUpEvent;
|
||||
evt->code = static_cast<int>(wparam);
|
||||
window->PushEvent(evt);
|
||||
KeyCode key = window->key_map_[size_t(wparam)];
|
||||
if (key != KeyCode::Unknown)
|
||||
{
|
||||
KeyUpEventPtr evt = new KeyUpEvent;
|
||||
evt->code = key;
|
||||
window->PushEvent(evt);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case WM_CHAR:
|
||||
{
|
||||
KeyCharEventPtr evt = new KeyCharEvent;
|
||||
evt->value = static_cast<char>(wparam);
|
||||
evt->value = char(wparam);
|
||||
window->PushEvent(evt);
|
||||
}
|
||||
break;
|
||||
|
|
@ -475,7 +520,7 @@ namespace kiwano
|
|||
case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:
|
||||
{
|
||||
MouseDownEventPtr evt = new MouseDownEvent;
|
||||
evt->pos = Point(((float)(int)(short)LOWORD(lparam)), ((float)(int)(short)HIWORD(lparam)));
|
||||
evt->pos = Point((float)GET_X_LPARAM(lparam), (float)GET_Y_LPARAM(lparam));
|
||||
|
||||
if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) { evt->button = MouseButton::Left; }
|
||||
else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) { evt->button = MouseButton::Right; }
|
||||
|
|
@ -490,7 +535,7 @@ namespace kiwano
|
|||
case WM_RBUTTONUP:
|
||||
{
|
||||
MouseUpEventPtr evt = new MouseUpEvent;
|
||||
evt->pos = Point(((float)(int)(short)LOWORD(lparam)), ((float)(int)(short)HIWORD(lparam)));
|
||||
evt->pos = Point((float)GET_X_LPARAM(lparam), (float)GET_Y_LPARAM(lparam));
|
||||
|
||||
if (msg == WM_LBUTTONUP) { evt->button = MouseButton::Left; }
|
||||
else if (msg == WM_RBUTTONUP) { evt->button = MouseButton::Right; }
|
||||
|
|
@ -503,7 +548,7 @@ namespace kiwano
|
|||
case WM_MOUSEMOVE:
|
||||
{
|
||||
MouseMoveEventPtr evt = new MouseMoveEvent;
|
||||
evt->pos = Point(((float)(int)(short)LOWORD(lparam)), ((float)(int)(short)HIWORD(lparam)));
|
||||
evt->pos = Point((float)GET_X_LPARAM(lparam), (float)GET_Y_LPARAM(lparam));
|
||||
window->PushEvent(evt);
|
||||
}
|
||||
break;
|
||||
|
|
@ -511,7 +556,7 @@ namespace kiwano
|
|||
case WM_MOUSEWHEEL:
|
||||
{
|
||||
MouseWheelEventPtr evt = new MouseWheelEvent;
|
||||
evt->pos = Point(((float)(int)(short)LOWORD(lparam)), ((float)(int)(short)HIWORD(lparam)));
|
||||
evt->pos = Point((float)GET_X_LPARAM(lparam), (float)GET_Y_LPARAM(lparam));
|
||||
evt->wheel = GET_WHEEL_DELTA_WPARAM(wparam) / (float)WHEEL_DELTA;
|
||||
window->PushEvent(evt);
|
||||
}
|
||||
|
|
@ -525,7 +570,7 @@ namespace kiwano
|
|||
}
|
||||
else
|
||||
{
|
||||
// KGE_SYS_LOG(L"WindowImpl resized");
|
||||
// KGE_SYS_LOG(L"Window resized");
|
||||
|
||||
window->SetInternalSize(((uint32_t)(short)LOWORD(lparam)), ((uint32_t)(short)HIWORD(lparam)));
|
||||
|
||||
|
|
@ -539,12 +584,9 @@ namespace kiwano
|
|||
|
||||
case WM_MOVE:
|
||||
{
|
||||
int x = ((int)(short)LOWORD(lparam));
|
||||
int y = ((int)(short)HIWORD(lparam));
|
||||
|
||||
WindowMovedEventPtr evt = new WindowMovedEvent;
|
||||
evt->x = x;
|
||||
evt->y = y;
|
||||
evt->x = GET_X_LPARAM(lparam);
|
||||
evt->y = GET_Y_LPARAM(lparam);
|
||||
window->PushEvent(evt);
|
||||
}
|
||||
break;
|
||||
|
|
|
|||
Loading…
Reference in New Issue