From 297e02877959cabb803b6ade2e9759bed4b37067 Mon Sep 17 00:00:00 2001 From: Nomango Date: Fri, 17 Jan 2020 16:46:17 +0800 Subject: [PATCH] Update Input --- src/kiwano-imgui/ImGuiModule.cpp | 44 +++--- src/kiwano/2d/Stage.cpp | 2 +- src/kiwano/core/Director.cpp | 7 +- src/kiwano/core/event/KeyEvent.cpp | 4 +- src/kiwano/core/event/KeyEvent.h | 4 +- src/kiwano/core/event/MouseEvent.cpp | 6 +- src/kiwano/core/event/MouseEvent.h | 6 +- src/kiwano/core/keys.h | 166 +++++++++++------------ src/kiwano/platform/Input.cpp | 90 +++++++----- src/kiwano/platform/Input.h | 66 ++++++--- src/kiwano/platform/Window.cpp | 5 - src/kiwano/platform/Window.h | 4 +- src/kiwano/platform/win32/WindowImpl.cpp | 104 +++++++++----- 13 files changed, 294 insertions(+), 214 deletions(-) diff --git a/src/kiwano-imgui/ImGuiModule.cpp b/src/kiwano-imgui/ImGuiModule.cpp index 53a96b4b..db42aff2 100644 --- a/src/kiwano-imgui/ImGuiModule.cpp +++ b/src/kiwano-imgui/ImGuiModule.cpp @@ -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()) { - int button = dynamic_cast(evt)->button; + MouseButton button = dynamic_cast(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()) { - int button = dynamic_cast(evt)->button; + MouseButton button = dynamic_cast(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()) { - int key = dynamic_cast(evt)->code; - io.KeysDown[key] = true; + KeyCode key = dynamic_cast(evt)->code; + io.KeysDown[(int)key] = true; } else if (evt->IsType()) { - int key = dynamic_cast(evt)->code; - io.KeysDown[key] = false; + KeyCode key = dynamic_cast(evt)->code; + io.KeysDown[(int)key] = false; } else if (evt->IsType()) { diff --git a/src/kiwano/2d/Stage.cpp b/src/kiwano/2d/Stage.cpp index 6a6eedf7..e8d7cf7a 100644 --- a/src/kiwano/2d/Stage.cpp +++ b/src/kiwano/2d/Stage.cpp @@ -48,7 +48,7 @@ namespace kiwano void Stage::RenderBorder(RenderContext& ctx) { - ctx.SetBrushOpacity(1.0f); + ctx.SetBrushOpacity(GetDisplayedOpacity()); if (!border_fill_brush_) { diff --git a/src/kiwano/core/Director.cpp b/src/kiwano/core/Director.cpp index aaf44e2c..60f8e215 100644 --- a/src/kiwano/core/Director.cpp +++ b/src/kiwano/core/Director.cpp @@ -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); } diff --git a/src/kiwano/core/event/KeyEvent.cpp b/src/kiwano/core/event/KeyEvent.cpp index f66e69c4..f61ebde0 100644 --- a/src/kiwano/core/event/KeyEvent.cpp +++ b/src/kiwano/core/event/KeyEvent.cpp @@ -10,13 +10,13 @@ namespace kiwano KeyDownEvent::KeyDownEvent() : KeyEvent(KGE_EVENT(KeyDownEvent)) - , code(0) + , code() { } KeyUpEvent::KeyUpEvent() : KeyEvent(KGE_EVENT(KeyUpEvent)) - , code(0) + , code() { } diff --git a/src/kiwano/core/event/KeyEvent.h b/src/kiwano/core/event/KeyEvent.h index 5ac0604d..7a1d3053 100644 --- a/src/kiwano/core/event/KeyEvent.h +++ b/src/kiwano/core/event/KeyEvent.h @@ -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(); }; diff --git a/src/kiwano/core/event/MouseEvent.cpp b/src/kiwano/core/event/MouseEvent.cpp index b661567e..9f3cce55 100644 --- a/src/kiwano/core/event/MouseEvent.cpp +++ b/src/kiwano/core/event/MouseEvent.cpp @@ -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() { } diff --git a/src/kiwano/core/event/MouseEvent.h b/src/kiwano/core/event/MouseEvent.h index bbfdca02..332b2e5e 100644 --- a/src/kiwano/core/event/MouseEvent.h +++ b/src/kiwano/core/event/MouseEvent.h @@ -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(); }; diff --git a/src/kiwano/core/keys.h b/src/kiwano/core/keys.h index 06a91537..9da0ea5e 100644 --- a/src/kiwano/core/keys.h +++ b/src/kiwano/core/keys.h @@ -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 }; } diff --git a/src/kiwano/platform/Input.cpp b/src/kiwano/platform/Input.cpp index 1fb900d7..71949b2b 100644 --- a/src/kiwano/platform/Input.cpp +++ b/src/kiwano/platform/Input.cpp @@ -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()) { - UpdateKey(dynamic_cast(evt)->button, true); + UpdateButton(dynamic_cast(evt)->button, true); } else if (evt->IsType()) { - UpdateKey(dynamic_cast(evt)->button, false); + UpdateButton(dynamic_cast(evt)->button, false); } } else if (evt->IsType()) diff --git a/src/kiwano/platform/Input.h b/src/kiwano/platform/Input.h index 9e59e8d5..2c81194b 100644 --- a/src/kiwano/platform/Input.h +++ b/src/kiwano/platform/Input.h @@ -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 keys_; - std::array keys_pressed_; - std::array keys_released_; + + enum KeyIndex : size_t + { + Current = 0, + Prev + }; + + std::array buttons_[2]; + std::array keys_[2]; }; } diff --git a/src/kiwano/platform/Window.cpp b/src/kiwano/platform/Window.cpp index 2386a43c..a259a5b6 100644 --- a/src/kiwano/platform/Window.cpp +++ b/src/kiwano/platform/Window.cpp @@ -89,11 +89,6 @@ namespace kiwano void Window::Destroy() { - while (!event_queue_.empty()) - { - event_queue_.pop(); - } - should_close_ = true; } diff --git a/src/kiwano/platform/Window.h b/src/kiwano/platform/Window.h index c9f3c53e..e27e41e8 100644 --- a/src/kiwano/platform/Window.h +++ b/src/kiwano/platform/Window.h @@ -181,9 +181,9 @@ namespace kiwano /** * \~chinese - * @brief 是否需要关闭 + * @brief 窗口是否需要关闭 */ - virtual bool ShouldClose() = 0; + bool ShouldClose(); /** * \~chinese diff --git a/src/kiwano/platform/win32/WindowImpl.cpp b/src/kiwano/platform/win32/WindowImpl.cpp index 7614a0f2..2b814aaa 100644 --- a/src/kiwano/platform/win32/WindowImpl.cpp +++ b/src/kiwano/platform/win32/WindowImpl.cpp @@ -28,6 +28,7 @@ #include #include +#include // GET_X_LPARAM, GET_Y_LPARAM #include // 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 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(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(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(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;