Update Input

This commit is contained in:
Nomango 2020-01-17 16:46:17 +08:00
parent bcc92abbef
commit 297e028779
13 changed files with 294 additions and 214 deletions

View File

@ -37,22 +37,22 @@ namespace kiwano
io.ImeWindowHandle = target_window_; 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. // 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_Tab] = (int)KeyCode::Tab;
io.KeyMap[ImGuiKey_LeftArrow] = KeyCode::Left; io.KeyMap[ImGuiKey_LeftArrow] = (int)KeyCode::Left;
io.KeyMap[ImGuiKey_RightArrow] = KeyCode::Right; io.KeyMap[ImGuiKey_RightArrow] = (int)KeyCode::Right;
io.KeyMap[ImGuiKey_UpArrow] = KeyCode::Up; io.KeyMap[ImGuiKey_UpArrow] = (int)KeyCode::Up;
io.KeyMap[ImGuiKey_DownArrow] = KeyCode::Down; io.KeyMap[ImGuiKey_DownArrow] = (int)KeyCode::Down;
io.KeyMap[ImGuiKey_Delete] = KeyCode::Delete; io.KeyMap[ImGuiKey_Delete] = (int)KeyCode::Delete;
io.KeyMap[ImGuiKey_Backspace] = KeyCode::Back; io.KeyMap[ImGuiKey_Backspace] = (int)KeyCode::Back;
io.KeyMap[ImGuiKey_Space] = KeyCode::Space; io.KeyMap[ImGuiKey_Space] = (int)KeyCode::Space;
io.KeyMap[ImGuiKey_Enter] = KeyCode::Enter; io.KeyMap[ImGuiKey_Enter] = (int)KeyCode::Enter;
io.KeyMap[ImGuiKey_Escape] = KeyCode::Esc; io.KeyMap[ImGuiKey_Escape] = (int)KeyCode::Esc;
io.KeyMap[ImGuiKey_A] = KeyCode::A; io.KeyMap[ImGuiKey_A] = (int)KeyCode::A;
io.KeyMap[ImGuiKey_C] = KeyCode::C; io.KeyMap[ImGuiKey_C] = (int)KeyCode::C;
io.KeyMap[ImGuiKey_V] = KeyCode::V; io.KeyMap[ImGuiKey_V] = (int)KeyCode::V;
io.KeyMap[ImGuiKey_X] = KeyCode::X; io.KeyMap[ImGuiKey_X] = (int)KeyCode::X;
io.KeyMap[ImGuiKey_Y] = KeyCode::Y; io.KeyMap[ImGuiKey_Y] = (int)KeyCode::Y;
io.KeyMap[ImGuiKey_Z] = KeyCode::Z; io.KeyMap[ImGuiKey_Z] = (int)KeyCode::Z;
ImGui_Impl_Init(Renderer::Instance()); ImGui_Impl_Init(Renderer::Instance());
} }
@ -104,7 +104,7 @@ namespace kiwano
{ {
if (evt->IsType<MouseDownEvent>()) if (evt->IsType<MouseDownEvent>())
{ {
int button = dynamic_cast<MouseDownEvent*>(evt)->button; MouseButton button = dynamic_cast<MouseDownEvent*>(evt)->button;
int index = 0; int index = 0;
if (button == MouseButton::Left) index = 0; if (button == MouseButton::Left) index = 0;
else if (button == MouseButton::Right) index = 1; else if (button == MouseButton::Right) index = 1;
@ -113,7 +113,7 @@ namespace kiwano
} }
else if (evt->IsType<MouseUpEvent>()) else if (evt->IsType<MouseUpEvent>())
{ {
int button = dynamic_cast<MouseUpEvent*>(evt)->button; MouseButton button = dynamic_cast<MouseUpEvent*>(evt)->button;
int index = 0; int index = 0;
if (button == MouseButton::Left) index = 0; if (button == MouseButton::Left) index = 0;
else if (button == MouseButton::Right) index = 1; else if (button == MouseButton::Right) index = 1;
@ -130,13 +130,13 @@ namespace kiwano
{ {
if (evt->IsType<KeyDownEvent>()) if (evt->IsType<KeyDownEvent>())
{ {
int key = dynamic_cast<KeyDownEvent*>(evt)->code; KeyCode key = dynamic_cast<KeyDownEvent*>(evt)->code;
io.KeysDown[key] = true; io.KeysDown[(int)key] = true;
} }
else if (evt->IsType<KeyUpEvent>()) else if (evt->IsType<KeyUpEvent>())
{ {
int key = dynamic_cast<KeyUpEvent*>(evt)->code; KeyCode key = dynamic_cast<KeyUpEvent*>(evt)->code;
io.KeysDown[key] = false; io.KeysDown[(int)key] = false;
} }
else if (evt->IsType<KeyCharEvent>()) else if (evt->IsType<KeyCharEvent>())
{ {

View File

@ -48,7 +48,7 @@ namespace kiwano
void Stage::RenderBorder(RenderContext& ctx) void Stage::RenderBorder(RenderContext& ctx)
{ {
ctx.SetBrushOpacity(1.0f); ctx.SetBrushOpacity(GetDisplayedOpacity());
if (!border_fill_brush_) if (!border_fill_brush_)
{ {

View File

@ -117,8 +117,8 @@ namespace kiwano
current_stage_.reset(); current_stage_.reset();
next_stage_.reset(); next_stage_.reset();
debug_actor_.reset();
transition_.reset(); transition_.reset();
debug_actor_.reset();
} }
void Director::OnUpdate(Duration dt) void Director::OnUpdate(Duration dt)
@ -163,11 +163,8 @@ namespace kiwano
else if (current_stage_) else if (current_stage_)
{ {
current_stage_->Render(ctx); current_stage_->Render(ctx);
}
if (render_border_enabled_) if (render_border_enabled_)
{
if (current_stage_)
{ {
current_stage_->RenderBorder(ctx); current_stage_->RenderBorder(ctx);
} }

View File

@ -10,13 +10,13 @@ namespace kiwano
KeyDownEvent::KeyDownEvent() KeyDownEvent::KeyDownEvent()
: KeyEvent(KGE_EVENT(KeyDownEvent)) : KeyEvent(KGE_EVENT(KeyDownEvent))
, code(0) , code()
{ {
} }
KeyUpEvent::KeyUpEvent() KeyUpEvent::KeyUpEvent()
: KeyEvent(KGE_EVENT(KeyUpEvent)) : KeyEvent(KGE_EVENT(KeyUpEvent))
, code(0) , code()
{ {
} }

View File

@ -49,7 +49,7 @@ namespace kiwano
: public KeyEvent : public KeyEvent
{ {
public: public:
KeyCode::Value code; ///< ¼üÖµ KeyCode code; ///< ¼üÖµ
KeyDownEvent(); KeyDownEvent();
}; };
@ -60,7 +60,7 @@ namespace kiwano
: public KeyEvent : public KeyEvent
{ {
public: public:
KeyCode::Value code; ///< ¼üÖµ KeyCode code; ///< ¼üÖµ
KeyUpEvent(); KeyUpEvent();
}; };

View File

@ -15,19 +15,19 @@ namespace kiwano
MouseDownEvent::MouseDownEvent() MouseDownEvent::MouseDownEvent()
: MouseEvent(KGE_EVENT(MouseDownEvent)) : MouseEvent(KGE_EVENT(MouseDownEvent))
, button(0) , button()
{ {
} }
MouseUpEvent::MouseUpEvent() MouseUpEvent::MouseUpEvent()
: MouseEvent(KGE_EVENT(MouseUpEvent)) : MouseEvent(KGE_EVENT(MouseUpEvent))
, button(0) , button()
{ {
} }
MouseClickEvent::MouseClickEvent() MouseClickEvent::MouseClickEvent()
: MouseEvent(KGE_EVENT(MouseClickEvent)) : MouseEvent(KGE_EVENT(MouseClickEvent))
, button(0) , button()
{ {
} }

View File

@ -65,7 +65,7 @@ namespace kiwano
: public MouseEvent : public MouseEvent
{ {
public: public:
MouseButton::Value button; ///< 報炎囚峙 MouseButton button; ///< 報炎囚峙
MouseDownEvent(); MouseDownEvent();
}; };
@ -76,7 +76,7 @@ namespace kiwano
: public MouseEvent : public MouseEvent
{ {
public: public:
MouseButton::Value button; ///< 報炎囚峙 MouseButton button; ///< 報炎囚峙
MouseUpEvent(); MouseUpEvent();
}; };
@ -87,7 +87,7 @@ namespace kiwano
: public MouseEvent : public MouseEvent
{ {
public: public:
MouseButton::Value button; ///< 報炎囚峙 MouseButton button; ///< 報炎囚峙
MouseClickEvent(); MouseClickEvent();
}; };

View File

@ -25,104 +25,98 @@ namespace kiwano
{ {
/// \~chinese /// \~chinese
/// @brief 報炎梓囚 /// @brief 報炎梓囚
struct MouseButton enum class MouseButton
{ {
typedef int Value; Left, ///< 鼠标左键
Right, ///< 鼠标右键
Middle, ///< 鼠标中键
enum : Value Last
{
Left = VK_LBUTTON, ///< 鼠标左键
Right = VK_RBUTTON, ///< 鼠标右键
Middle = VK_MBUTTON ///< 鼠标中键
};
}; };
/// \~chinese /// \~chinese
/// @brief 梓囚囚峙 /// @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 A, ///< A键
{ B, ///< B键
Unknown = 0, ///< 未知 C, ///< C键
Up = VK_UP, ///< 上键 D, ///< D键
Left = VK_LEFT, ///< 左键 E, ///< E键
Right = VK_RIGHT, ///< 右键 F, ///< F键
Down = VK_DOWN, ///< 下键 G, ///< G键
Enter = VK_RETURN, ///< 回车键 H, ///< H键
Space = VK_SPACE, ///< 空格键 I, ///< I键
Esc = VK_ESCAPE, ///< 退出键 J, ///< J键
Ctrl = VK_CONTROL, ///< CTRL键 K, ///< K键
Shift = VK_SHIFT, ///< SHIFT键 L, ///< L键
Alt = VK_MENU, ///< ALT键 M, ///< M键
Tab = VK_TAB, ///< TAB键 N, ///< N键
Delete = VK_DELETE, ///< 删除键 O, ///< O键
Back = VK_BACK, ///< 退格键 P, ///< P键
Super = VK_LWIN, ///< Cmd/Super/Windows键 Q, ///< Q键
R, ///< R键
S, ///< S键
T, ///< T键
U, ///< U键
V, ///< V键
W, ///< W键
X, ///< X键
Y, ///< Y键
Z, ///< Z键
A = 0x41, ///< A键 Num0, ///< 数字0键
B, ///< B键 Num1, ///< 数字1键
C, ///< C键 Num2, ///< 数字2键
D, ///< D键 Num3, ///< 数字3键
E, ///< E键 Num4, ///< 数字4键
F, ///< F键 Num5, ///< 数字5键
G, ///< G键 Num6, ///< 数字6键
H, ///< H键 Num7, ///< 数字7键
I, ///< I键 Num8, ///< 数字8键
J, ///< J键 Num9, ///< 数字9键
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 = 0x30, ///< 数字0键 Numpad0, ///< 数字小键盘0键
Num1, ///< 数字1键 Numpad1, ///< 数字小键盘1键
Num2, ///< 数字2键 Numpad2, ///< 数字小键盘2键
Num3, ///< 数字3键 Numpad3, ///< 数字小键盘3键
Num4, ///< 数字4键 Numpad4, ///< 数字小键盘4键
Num5, ///< 数字5键 Numpad5, ///< 数字小键盘5键
Num6, ///< 数字6键 Numpad6, ///< 数字小键盘6键
Num7, ///< 数字7键 Numpad7, ///< 数字小键盘7键
Num8, ///< 数字8键 Numpad8, ///< 数字小键盘8键
Num9, ///< 数字9键 Numpad9, ///< 数字小键盘9键
Numpad0 = VK_NUMPAD0, ///< 数字小键盘0键 F1, ///< F1键
Numpad1, ///< 数字小键盘1键 F2, ///< F2键
Numpad2, ///< 数字小键盘2键 F3, ///< F3键
Numpad3, ///< 数字小键盘3键 F4, ///< F4键
Numpad4, ///< 数字小键盘4键 F5, ///< F5键
Numpad5, ///< 数字小键盘5键 F6, ///< F6键
Numpad6, ///< 数字小键盘6键 F7, ///< F7键
Numpad7, ///< 数字小键盘7键 F8, ///< F8键
Numpad8, ///< 数字小键盘8键 F9, ///< F9键
Numpad9, ///< 数字小键盘9键 F10, ///< F10键
F11, ///< F11键
F12, ///< F12键
F1 = VK_F1, ///< F1键 Last
F2, ///< F2键
F3, ///< F3键
F4, ///< F4键
F5, ///< F5键
F6, ///< F6键
F7, ///< F7键
F8, ///< F8键
F9, ///< F9键
F10, ///< F10键
F11, ///< F11键
F12, ///< F12键
};
}; };
} }

View File

@ -26,10 +26,10 @@
namespace kiwano namespace kiwano
{ {
Input::Input() Input::Input()
: want_update_(false) : want_update_keys_(false)
, want_update_buttons_(false)
, buttons_{}
, keys_{} , keys_{}
, keys_pressed_{}
, keys_released_{}
{ {
} }
@ -39,37 +39,59 @@ namespace kiwano
void Input::AfterUpdate() 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); if (want_update_buttons_)
keys_released_.fill(false); {
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 == KeyCode::Unknown || key == KeyCode::Last)
if (key_or_btn >= 0 && key_or_btn < KEY_NUM) return false;
return keys_[key_or_btn]; return keys_[Current][size_t(key)];
return false;
} }
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 == KeyCode::Unknown || key == KeyCode::Last)
if (key_or_btn >= 0 && key_or_btn < KEY_NUM) return false;
return keys_pressed_[key_or_btn]; return keys_[Current][size_t(key)] && !keys_[Prev][size_t(key)];
return false;
} }
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 == KeyCode::Unknown || key == KeyCode::Last)
if (key_or_btn >= 0 && key_or_btn < KEY_NUM) return false;
return keys_released_[key_or_btn]; return !keys_[Current][size_t(key)] && keys_[Prev][size_t(key)];
return false; }
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 float Input::GetMouseX() const
@ -87,16 +109,22 @@ namespace kiwano
return mouse_pos_; return mouse_pos_;
} }
void Input::UpdateKey(int key, bool down) void Input::UpdateKey(KeyCode key, bool down)
{ {
if (down && !keys_[key]) if (key == KeyCode::Unknown || key == KeyCode::Last)
keys_pressed_[key] = true; return;
if (!down && keys_[key])
keys_released_[key] = true;
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) void Input::UpdateMousePos(const Point& pos)
@ -114,11 +142,11 @@ namespace kiwano
} }
else if (evt->IsType<MouseDownEvent>()) else if (evt->IsType<MouseDownEvent>())
{ {
UpdateKey(dynamic_cast<MouseDownEvent*>(evt)->button, true); UpdateButton(dynamic_cast<MouseDownEvent*>(evt)->button, true);
} }
else if (evt->IsType<MouseUpEvent>()) else if (evt->IsType<MouseUpEvent>())
{ {
UpdateKey(dynamic_cast<MouseUpEvent*>(evt)->button, false); UpdateButton(dynamic_cast<MouseUpEvent*>(evt)->button, false);
} }
} }
else if (evt->IsType<KeyEvent>()) else if (evt->IsType<KeyEvent>())

View File

@ -41,30 +41,44 @@ namespace kiwano
public: public:
/** /**
* \~chinese * \~chinese
* @brief * @brief
* @param key_or_btn * @param key
* @return
* @see kiwano::KeyCode kiwano::MouseButton
*/ */
bool IsDown(int key_or_btn) const; bool IsDown(KeyCode key) const;
/** /**
* \~chinese * \~chinese
* @brief * @brief
* @param key_or_btn * @param key
* @return
* @see kiwano::KeyCode kiwano::MouseButton
*/ */
bool WasPressed(int key_or_btn) const; bool WasPressed(KeyCode key) const;
/** /**
* \~chinese * \~chinese
* @brief * @brief
* @param key_or_btn * @param key
* @return
* @see kiwano::KeyCode kiwano::MouseButton
*/ */
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 * \~chinese
@ -101,17 +115,27 @@ namespace kiwano
~Input(); ~Input();
void UpdateKey(int, bool); void UpdateKey(KeyCode key, bool down);
void UpdateButton(MouseButton btn, bool down);
void UpdateMousePos(const Point& pos); void UpdateMousePos(const Point& pos);
private: 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_; Point mouse_pos_;
std::array<bool, KEY_NUM> keys_;
std::array<bool, KEY_NUM> keys_pressed_; enum KeyIndex : size_t
std::array<bool, KEY_NUM> keys_released_; {
Current = 0,
Prev
};
std::array<bool, BUTTON_NUM> buttons_[2];
std::array<bool, KEY_NUM> keys_[2];
}; };
} }

View File

@ -89,11 +89,6 @@ namespace kiwano
void Window::Destroy() void Window::Destroy()
{ {
while (!event_queue_.empty())
{
event_queue_.pop();
}
should_close_ = true; should_close_ = true;
} }

View File

@ -181,9 +181,9 @@ namespace kiwano
/** /**
* \~chinese * \~chinese
* @brief * @brief
*/ */
virtual bool ShouldClose() = 0; bool ShouldClose();
/** /**
* \~chinese * \~chinese

View File

@ -28,6 +28,7 @@
#include <kiwano/core/event/WindowEvent.h> #include <kiwano/core/event/WindowEvent.h>
#include <kiwano/core/Logger.h> #include <kiwano/core/Logger.h>
#include <Windowsx.h> // GET_X_LPARAM, GET_Y_LPARAM
#include <imm.h> // ImmAssociateContext #include <imm.h> // ImmAssociateContext
#pragma comment(lib, "imm32.lib") #pragma comment(lib, "imm32.lib")
@ -117,14 +118,12 @@ namespace kiwano
void SetCursor(CursorType cursor) override; void SetCursor(CursorType cursor) override;
bool ShouldClose() override;
void Destroy() override; void Destroy() override;
private: private:
void PumpEvents() override; void PumpEvents() override;
DWORD GetWindowStyle() const; DWORD GetStyle() const;
void UpdateCursor(); void UpdateCursor();
@ -138,6 +137,8 @@ namespace kiwano
wchar_t* device_name_; wchar_t* device_name_;
WindowHandle handle_; WindowHandle handle_;
CursorType mouse_cursor_; CursorType mouse_cursor_;
std::array<KeyCode, 256> key_map_;
}; };
WindowImpl::WindowImpl() WindowImpl::WindowImpl()
@ -146,7 +147,48 @@ namespace kiwano
, is_fullscreen_(false) , is_fullscreen_(false)
, resizable_(false) , resizable_(false)
, mouse_cursor_(CursorType::Arrow) , 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() WindowImpl::~WindowImpl()
@ -216,7 +258,7 @@ namespace kiwano
uint32_t screenh = monitor_info_ex.rcWork.bottom - monitor_info_ex.rcWork.top; uint32_t screenh = monitor_info_ex.rcWork.bottom - monitor_info_ex.rcWork.top;
uint32_t win_width, win_height; 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; left = monitor_info_ex.rcWork.left + (screenw - win_width) / 2;
top = monitor_info_ex.rcWork.top + (screenh - win_height) / 2; top = monitor_info_ex.rcWork.top + (screenh - win_height) / 2;
@ -228,7 +270,7 @@ namespace kiwano
is_fullscreen_ ? WS_EX_TOPMOST : 0, is_fullscreen_ ? WS_EX_TOPMOST : 0,
KGE_WND_CLASS_NAME, KGE_WND_CLASS_NAME,
config.title.c_str(), config.title.c_str(),
GetWindowStyle(), GetStyle(),
left, left,
top, top,
width, width,
@ -309,7 +351,7 @@ namespace kiwano
if (handle_ && !is_fullscreen_) if (handle_ && !is_fullscreen_)
{ {
RECT rc = { 0, 0, LONG(width), LONG(height) }; RECT rc = { 0, 0, LONG(width), LONG(height) };
::AdjustWindowRect(&rc, GetWindowStyle(), false); ::AdjustWindowRect(&rc, GetStyle(), false);
width = rc.right - rc.left; width = rc.right - rc.left;
height = rc.bottom - rc.top; height = rc.bottom - rc.top;
@ -335,7 +377,7 @@ namespace kiwano
MONITORINFOEX info = GetMoniterInfoEx(handle_); 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); ::SetWindowPos(handle_, HWND_TOPMOST, info.rcMonitor.top, info.rcMonitor.left, width, height, SWP_NOACTIVATE);
} }
else else
@ -350,7 +392,7 @@ namespace kiwano
int left = screenw > width ? ((screenw - width) / 2) : 0; int left = screenw > width ? ((screenw - width) / 2) : 0;
int top = screenh > height ? ((screenh - height) / 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); ::SetWindowPos(handle_, HWND_NOTOPMOST, left, top, width, height, SWP_DRAWFRAME | SWP_FRAMECHANGED);
} }
@ -363,11 +405,6 @@ namespace kiwano
mouse_cursor_ = cursor; mouse_cursor_ = cursor;
} }
bool WindowImpl::ShouldClose()
{
return handle_ == nullptr;
}
void WindowImpl::Destroy() void WindowImpl::Destroy()
{ {
if (is_fullscreen_) if (is_fullscreen_)
@ -388,7 +425,7 @@ namespace kiwano
Window::Destroy(); Window::Destroy();
} }
DWORD WindowImpl::GetWindowStyle() const DWORD WindowImpl::GetStyle() const
{ {
return is_fullscreen_ ? (WINDOW_FULLSCREEN_STYLE) : (resizable_ ? (WINDOW_RESIZABLE_STYLE) : (WINDOW_FIXED_STYLE)); return is_fullscreen_ ? (WINDOW_FULLSCREEN_STYLE) : (resizable_ ? (WINDOW_RESIZABLE_STYLE) : (WINDOW_FIXED_STYLE));
} }
@ -447,25 +484,33 @@ namespace kiwano
case WM_KEYDOWN: case WM_KEYDOWN:
case WM_SYSKEYDOWN: case WM_SYSKEYDOWN:
{ {
KeyDownEventPtr evt = new KeyDownEvent; KeyCode key = window->key_map_[size_t(wparam)];
evt->code = static_cast<int>(wparam); if (key != KeyCode::Unknown)
window->PushEvent(evt); {
KeyDownEventPtr evt = new KeyDownEvent;
evt->code = key;
window->PushEvent(evt);
}
} }
break; break;
case WM_KEYUP: case WM_KEYUP:
case WM_SYSKEYUP: case WM_SYSKEYUP:
{ {
KeyUpEventPtr evt = new KeyUpEvent; KeyCode key = window->key_map_[size_t(wparam)];
evt->code = static_cast<int>(wparam); if (key != KeyCode::Unknown)
window->PushEvent(evt); {
KeyUpEventPtr evt = new KeyUpEvent;
evt->code = key;
window->PushEvent(evt);
}
} }
break; break;
case WM_CHAR: case WM_CHAR:
{ {
KeyCharEventPtr evt = new KeyCharEvent; KeyCharEventPtr evt = new KeyCharEvent;
evt->value = static_cast<char>(wparam); evt->value = char(wparam);
window->PushEvent(evt); window->PushEvent(evt);
} }
break; break;
@ -475,7 +520,7 @@ namespace kiwano
case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK: case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:
{ {
MouseDownEventPtr evt = new MouseDownEvent; 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; } if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) { evt->button = MouseButton::Left; }
else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) { evt->button = MouseButton::Right; } else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) { evt->button = MouseButton::Right; }
@ -490,7 +535,7 @@ namespace kiwano
case WM_RBUTTONUP: case WM_RBUTTONUP:
{ {
MouseUpEventPtr evt = new MouseUpEvent; 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; } if (msg == WM_LBUTTONUP) { evt->button = MouseButton::Left; }
else if (msg == WM_RBUTTONUP) { evt->button = MouseButton::Right; } else if (msg == WM_RBUTTONUP) { evt->button = MouseButton::Right; }
@ -503,7 +548,7 @@ namespace kiwano
case WM_MOUSEMOVE: case WM_MOUSEMOVE:
{ {
MouseMoveEventPtr evt = new MouseMoveEvent; 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); window->PushEvent(evt);
} }
break; break;
@ -511,7 +556,7 @@ namespace kiwano
case WM_MOUSEWHEEL: case WM_MOUSEWHEEL:
{ {
MouseWheelEventPtr evt = new MouseWheelEvent; 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; evt->wheel = GET_WHEEL_DELTA_WPARAM(wparam) / (float)WHEEL_DELTA;
window->PushEvent(evt); window->PushEvent(evt);
} }
@ -525,7 +570,7 @@ namespace kiwano
} }
else 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))); window->SetInternalSize(((uint32_t)(short)LOWORD(lparam)), ((uint32_t)(short)HIWORD(lparam)));
@ -539,12 +584,9 @@ namespace kiwano
case WM_MOVE: case WM_MOVE:
{ {
int x = ((int)(short)LOWORD(lparam));
int y = ((int)(short)HIWORD(lparam));
WindowMovedEventPtr evt = new WindowMovedEvent; WindowMovedEventPtr evt = new WindowMovedEvent;
evt->x = x; evt->x = GET_X_LPARAM(lparam);
evt->y = y; evt->y = GET_Y_LPARAM(lparam);
window->PushEvent(evt); window->PushEvent(evt);
} }
break; break;