Magic_Game/src/kiwano/platform/win32/WindowImpl.cpp

800 lines
23 KiB
C++
Raw Normal View History

2020-01-17 11:24:24 +08:00
// Copyright (c) 2016-2020 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
2020-01-17 11:24:24 +08:00
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2020-01-21 10:09:55 +08:00
//
2020-01-17 11:24:24 +08:00
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
2020-01-17 11:24:24 +08:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
2020-02-14 22:01:56 +08:00
#include <kiwano/platform/Window.h>
2020-01-17 11:24:24 +08:00
2020-02-15 17:32:32 +08:00
#if defined(KGE_PLATFORM_WINDOWS)
2020-01-17 11:24:24 +08:00
2020-02-22 19:44:50 +08:00
#include <memory>
2020-02-15 17:32:32 +08:00
#include <array>
2020-02-14 22:01:56 +08:00
#include <kiwano/core/Keys.h>
2020-02-10 14:41:19 +08:00
#include <kiwano/core/Exception.h>
2020-05-24 11:26:21 +08:00
#include <kiwano/utils/Logger.h>
#include <kiwano/event/Events.h>
2020-01-21 10:09:55 +08:00
#include <kiwano/platform/Application.h>
2020-05-17 21:02:43 +08:00
#include <kiwano/render/Renderer.h>
2020-05-22 21:07:32 +08:00
#include <kiwano/render/DirectX/D3DDeviceResources.h>
2020-02-14 22:01:56 +08:00
#include <Windowsx.h> // GET_X_LPARAM, GET_Y_LPARAM
#include <imm.h> // ImmAssociateContext
2020-01-17 11:24:24 +08:00
#pragma comment(lib, "imm32.lib")
namespace kiwano
{
2020-02-14 22:01:56 +08:00
KGE_DECLARE_SMART_PTR(WindowWin32Impl);
class KGE_API WindowWin32Impl : public Window
{
2020-02-14 22:01:56 +08:00
public:
WindowWin32Impl();
virtual ~WindowWin32Impl();
2020-02-19 12:09:50 +08:00
void Init(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable, bool fullscreen);
2020-02-14 22:01:56 +08:00
2020-02-19 12:09:50 +08:00
void SetTitle(const String& title) override;
2020-02-14 22:01:56 +08:00
void SetIcon(uint32_t icon_resource) override;
void Resize(uint32_t width, uint32_t height) override;
2020-05-20 11:55:53 +08:00
void SetMinimumSize(uint32_t width, uint32_t height) override;
2020-05-20 23:38:24 +08:00
void SetMaximumSize(uint32_t width, uint32_t height) override;
2020-02-14 22:01:56 +08:00
void SetCursor(CursorType cursor) override;
2020-05-22 21:07:32 +08:00
void SetResolution(uint32_t width, uint32_t height, bool fullscreen) override;
Vector<Resolution> GetResolutions() override;
2020-02-14 22:01:56 +08:00
void PumpEvents() override;
DWORD GetStyle() const;
void UpdateCursor();
2020-02-14 22:01:56 +08:00
LRESULT MessageProc(HWND, UINT32, WPARAM, LPARAM);
static LRESULT CALLBACK StaticWndProc(HWND, UINT32, WPARAM, LPARAM);
private:
bool resizable_;
2020-05-20 11:55:53 +08:00
bool is_resizing_;
bool is_minimized_;
2020-02-14 22:01:56 +08:00
CursorType mouse_cursor_;
String device_name_;
2020-02-14 22:01:56 +08:00
2020-05-22 21:07:32 +08:00
Vector<Resolution> resolutions_;
std::array<KeyCode, 256> key_map_;
2020-02-14 22:01:56 +08:00
};
2020-02-19 12:09:50 +08:00
WindowPtr Window::Create(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable,
2020-02-14 22:01:56 +08:00
bool fullscreen)
2020-01-21 10:09:55 +08:00
{
2020-02-20 22:27:09 +08:00
WindowWin32ImplPtr ptr = memory::New<WindowWin32Impl>();
2020-02-14 22:01:56 +08:00
if (ptr)
{
ptr->Init(title, width, height, icon, resizable, fullscreen);
}
return ptr;
}
2020-02-14 22:01:56 +08:00
} // namespace kiwano
namespace kiwano
{
#define WINDOW_FIXED_STYLE WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX
#define WINDOW_RESIZABLE_STYLE WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_SIZEBOX | WS_MAXIMIZEBOX
2020-01-21 10:09:55 +08:00
namespace
{
MONITORINFOEXA GetMoniterInfoEx(HWND hwnd)
2020-01-21 10:09:55 +08:00
{
HMONITOR monitor = ::MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
MONITORINFOEXA monitor_info;
2020-01-21 10:09:55 +08:00
2020-04-14 17:52:38 +08:00
memset(&monitor_info, 0, sizeof(monitor_info));
monitor_info.cbSize = sizeof(monitor_info);
::GetMonitorInfoA(monitor, &monitor_info);
2020-01-21 10:09:55 +08:00
return monitor_info;
}
void AdjustWindow(uint32_t width, uint32_t height, DWORD style, uint32_t* win_width, uint32_t* win_height)
{
RECT rc;
::SetRect(&rc, 0, 0, (int)width, (int)height);
::AdjustWindowRect(&rc, style, false);
2020-01-17 11:24:24 +08:00
2020-01-21 10:09:55 +08:00
*win_width = rc.right - rc.left;
*win_height = rc.bottom - rc.top;
2020-01-17 11:24:24 +08:00
MONITORINFOEXA info = GetMoniterInfoEx(NULL);
2020-01-21 10:09:55 +08:00
uint32_t screenw = info.rcWork.right - info.rcWork.left;
uint32_t screenh = info.rcWork.bottom - info.rcWork.top;
if (*win_width > screenw)
*win_width = screenw;
if (*win_height > screenh)
*win_height = screenh;
}
} // namespace
2020-02-14 22:01:56 +08:00
WindowWin32Impl::WindowWin32Impl()
: resizable_(false)
2020-05-20 11:55:53 +08:00
, is_resizing_(false)
, is_minimized_(false)
2020-01-21 10:09:55 +08:00
, 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
2020-04-14 17:08:15 +08:00
for (size_t i = 0; i < 10; ++i)
2020-01-21 10:09:55 +08:00
key_map_['0' + i] = KeyCode(size_t(KeyCode::Num0) + i);
// Numpad 0 - 9
2020-04-14 17:08:15 +08:00
for (size_t i = 0; i < 10; ++i)
2020-01-21 10:09:55 +08:00
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);
}
2020-03-31 14:07:13 +08:00
WindowWin32Impl::~WindowWin32Impl()
{
if (handle_)
{
::DestroyWindow(handle_);
handle_ = nullptr;
}
2020-03-31 14:07:13 +08:00
}
2020-01-21 10:09:55 +08:00
2020-02-19 12:09:50 +08:00
void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable,
2020-02-14 22:01:56 +08:00
bool fullscreen)
2020-01-21 10:09:55 +08:00
{
HINSTANCE hinst = GetModuleHandle(nullptr);
WNDCLASSEXA wcex = { 0 };
2020-01-21 10:09:55 +08:00
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpszClassName = "KiwanoAppWnd";
2020-01-21 10:09:55 +08:00
wcex.style = CS_HREDRAW | CS_VREDRAW /* | CS_DBLCLKS */;
2020-02-14 22:01:56 +08:00
wcex.lpfnWndProc = WindowWin32Impl::StaticWndProc;
2020-01-21 10:09:55 +08:00
wcex.hIcon = nullptr;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = sizeof(LONG_PTR);
wcex.hInstance = hinst;
wcex.hbrBackground = (HBRUSH)::GetStockObject(BLACK_BRUSH);
2020-01-21 10:09:55 +08:00
wcex.lpszMenuName = nullptr;
wcex.hCursor = ::LoadCursor(hinst, IDC_ARROW);
2020-01-21 10:09:55 +08:00
2020-02-02 15:47:48 +08:00
if (icon)
2020-01-21 10:09:55 +08:00
{
wcex.hIcon = (HICON)::LoadImage(hinst, MAKEINTRESOURCE(icon), IMAGE_ICON, 0, 0,
LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
2020-01-21 10:09:55 +08:00
}
::RegisterClassExA(&wcex);
2020-01-21 10:09:55 +08:00
// Get the nearest monitor to this window
HMONITOR monitor = ::MonitorFromPoint(POINT{ 0, 0 }, MONITOR_DEFAULTTOPRIMARY);
// Get the target monitor info
MONITORINFOEXA monitor_info_ex;
memset(&monitor_info_ex, 0, sizeof(MONITORINFOEXA));
monitor_info_ex.cbSize = sizeof(MONITORINFOEXA);
::GetMonitorInfoA(monitor, &monitor_info_ex);
2020-01-21 10:09:55 +08:00
// Save the device name
device_name_ = monitor_info_ex.szDevice;
2020-01-21 10:09:55 +08:00
2020-02-02 15:47:48 +08:00
int left = -1, top = -1;
2020-01-21 10:09:55 +08:00
if (fullscreen)
2020-01-21 10:09:55 +08:00
{
top = monitor_info_ex.rcMonitor.top;
left = monitor_info_ex.rcMonitor.left;
if (width > static_cast<uint32_t>(monitor_info_ex.rcWork.right - left))
width = static_cast<uint32_t>(monitor_info_ex.rcWork.right - left);
if (height > static_cast<uint32_t>(monitor_info_ex.rcWork.bottom - top))
height = static_cast<uint32_t>(monitor_info_ex.rcWork.bottom - top);
}
else
{
uint32_t screenw = monitor_info_ex.rcWork.right - monitor_info_ex.rcWork.left;
uint32_t screenh = monitor_info_ex.rcWork.bottom - monitor_info_ex.rcWork.top;
uint32_t 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;
width = win_width;
height = win_height;
}
2020-05-20 23:38:24 +08:00
width_ = width;
height_ = height;
resizable_ = resizable;
handle_ = ::CreateWindowExA(fullscreen ? WS_EX_TOPMOST : 0, "KiwanoAppWnd", title.c_str(), GetStyle(), left, top,
width, height, nullptr, nullptr, hinst, nullptr);
2020-01-21 10:09:55 +08:00
if (handle_ == nullptr)
{
::UnregisterClassA("KiwanoAppWnd", hinst);
2020-02-13 22:35:04 +08:00
KGE_THROW_SYSTEM_ERROR(HRESULT_FROM_WIN32(GetLastError()), "Create window failed");
2020-01-21 10:09:55 +08:00
}
// disable imm
::ImmAssociateContext(handle_, nullptr);
// use Application instance in message loop
::SetWindowLongPtrA(handle_, GWLP_USERDATA, LONG_PTR(this));
2020-01-21 10:09:55 +08:00
::ShowWindow(handle_, SW_SHOWNORMAL);
::UpdateWindow(handle_);
}
2020-01-17 11:24:24 +08:00
2020-02-14 22:01:56 +08:00
void WindowWin32Impl::PumpEvents()
2020-01-21 10:09:55 +08:00
{
MSG msg;
while (::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
2020-01-21 10:09:55 +08:00
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
2020-01-21 10:09:55 +08:00
}
}
2020-02-19 12:09:50 +08:00
void WindowWin32Impl::SetTitle(const String& title)
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(handle_);
::SetWindowTextA(handle_, title.c_str());
2020-01-21 10:09:55 +08:00
}
2020-02-14 22:01:56 +08:00
void WindowWin32Impl::SetIcon(uint32_t icon_resource)
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(handle_);
2020-01-21 10:09:55 +08:00
HINSTANCE hinstance = ::GetModuleHandle(nullptr);
HICON icon = (HICON)::LoadImage(hinstance, MAKEINTRESOURCE(icon_resource), IMAGE_ICON, 0, 0,
LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
::SendMessage(handle_, WM_SETICON, ICON_BIG, (LPARAM)icon);
::SendMessage(handle_, WM_SETICON, ICON_SMALL, (LPARAM)icon);
2020-01-21 10:09:55 +08:00
}
2020-02-14 22:01:56 +08:00
void WindowWin32Impl::Resize(uint32_t width, uint32_t height)
2020-01-21 10:09:55 +08:00
{
KGE_ASSERT(handle_);
2020-01-21 10:09:55 +08:00
2020-05-17 21:02:43 +08:00
RECT rc = { 0, 0, LONG(width), LONG(height) };
::AdjustWindowRect(&rc, GetStyle(), false);
2020-01-21 10:09:55 +08:00
2020-05-17 21:02:43 +08:00
width = rc.right - rc.left;
height = rc.bottom - rc.top;
2020-05-17 21:02:43 +08:00
MONITORINFOEXA info = GetMoniterInfoEx(handle_);
uint32_t screenw = uint32_t(info.rcWork.right - info.rcWork.left);
uint32_t screenh = uint32_t(info.rcWork.bottom - info.rcWork.top);
int left = screenw > width ? ((screenw - width) / 2) : 0;
int top = screenh > height ? ((screenh - height) / 2) : 0;
2020-05-17 21:02:43 +08:00
::SetWindowPos(handle_, 0, left, top, width, height, SWP_NOZORDER | SWP_NOACTIVATE);
2020-01-21 10:09:55 +08:00
}
2020-05-20 11:55:53 +08:00
void WindowWin32Impl::SetMinimumSize(uint32_t width, uint32_t height)
{
min_width_ = width;
min_height_ = height;
}
2020-05-20 23:38:24 +08:00
void WindowWin32Impl::SetMaximumSize(uint32_t width, uint32_t height)
{
max_width_ = width;
max_height_ = height;
}
2020-02-14 22:01:56 +08:00
void WindowWin32Impl::SetCursor(CursorType cursor)
2020-01-21 10:09:55 +08:00
{
mouse_cursor_ = cursor;
}
2020-05-22 21:07:32 +08:00
void WindowWin32Impl::SetResolution(uint32_t width, uint32_t height, bool fullscreen)
{
auto d3d = kiwano::graphics::directx::GetD3DDeviceResources();
if (fullscreen)
{
HRESULT hr = d3d->ResizeTarget(width, height);
KGE_THROW_IF_FAILED(hr, "DXGI ResizeTarget failed!");
hr = d3d->SetFullscreenState(fullscreen);
KGE_THROW_IF_FAILED(hr, "DXGI SetFullscreenState failed!");
}
else
{
HRESULT hr = d3d->SetFullscreenState(fullscreen);
KGE_THROW_IF_FAILED(hr, "DXGI SetFullscreenState failed!");
hr = d3d->ResizeTarget(width, height);
KGE_THROW_IF_FAILED(hr, "DXGI ResizeTarget failed!");
}
is_fullscreen_ = fullscreen;
}
Vector<Resolution> WindowWin32Impl::GetResolutions()
{
if (resolutions_.empty())
{
auto d3d = kiwano::graphics::directx::GetD3DDeviceResources();
DXGI_MODE_DESC* mode_descs = nullptr;
int mode_num = 0;
HRESULT hr = d3d->GetDisplaySettings(&mode_descs, &mode_num);
if (SUCCEEDED(hr))
{
std::unique_ptr<DXGI_MODE_DESC[]> mode_list(mode_descs);
if (mode_list)
{
for (int i = 0; i < mode_num; i++)
{
Resolution res;
res.width = mode_descs[i].Width;
res.height = mode_descs[i].Height;
res.refresh_rate = 0;
if (mode_descs[i].RefreshRate.Denominator > 0)
{
res.refresh_rate = mode_descs[i].RefreshRate.Numerator / mode_descs[i].RefreshRate.Denominator;
}
if (!resolutions_.empty())
{
auto& back = resolutions_.back();
if (back.width == res.width && back.height == res.height
&& back.refresh_rate == res.refresh_rate)
continue;
}
resolutions_.push_back(res);
}
}
}
else
{
KGE_THROW_IF_FAILED(hr, "DXGI GetDisplaySettings failed!");
}
}
return resolutions_;
}
2020-02-14 22:01:56 +08:00
DWORD WindowWin32Impl::GetStyle() const
2020-01-21 10:09:55 +08:00
{
2020-05-17 21:02:43 +08:00
return (resizable_ ? (WINDOW_RESIZABLE_STYLE) : (WINDOW_FIXED_STYLE));
2020-01-21 10:09:55 +08:00
}
2020-02-14 22:01:56 +08:00
void WindowWin32Impl::UpdateCursor()
2020-01-21 10:09:55 +08:00
{
LPTSTR win32_cursor = IDC_ARROW;
switch (mouse_cursor_)
{
case CursorType::Arrow:
win32_cursor = IDC_ARROW;
break;
case CursorType::TextInput:
win32_cursor = IDC_IBEAM;
break;
case CursorType::SizeAll:
win32_cursor = IDC_SIZEALL;
break;
case CursorType::SizeWE:
win32_cursor = IDC_SIZEWE;
break;
case CursorType::SizeNS:
win32_cursor = IDC_SIZENS;
break;
case CursorType::SizeNESW:
win32_cursor = IDC_SIZENESW;
break;
case CursorType::SizeNWSE:
win32_cursor = IDC_SIZENWSE;
break;
case CursorType::Hand:
win32_cursor = IDC_HAND;
break;
}
::SetCursor(::LoadCursor(nullptr, win32_cursor));
2020-01-21 10:09:55 +08:00
}
2020-02-14 22:01:56 +08:00
LRESULT WindowWin32Impl::MessageProc(HWND hwnd, UINT32 msg, WPARAM wparam, LPARAM lparam)
2020-01-21 10:09:55 +08:00
{
switch (msg)
{
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
{
2020-02-14 17:12:13 +08:00
KeyCode key = this->key_map_[size_t(wparam)];
2020-01-21 10:09:55 +08:00
if (key != KeyCode::Unknown)
{
KeyDownEventPtr evt = new KeyDownEvent;
evt->code = key;
2020-02-14 17:12:13 +08:00
this->PushEvent(evt);
2020-01-21 10:09:55 +08:00
}
}
break;
case WM_KEYUP:
case WM_SYSKEYUP:
{
2020-02-14 17:12:13 +08:00
KeyCode key = this->key_map_[size_t(wparam)];
2020-01-21 10:09:55 +08:00
if (key != KeyCode::Unknown)
{
KeyUpEventPtr evt = new KeyUpEvent;
evt->code = key;
2020-02-14 17:12:13 +08:00
this->PushEvent(evt);
2020-01-21 10:09:55 +08:00
}
}
break;
case WM_CHAR:
{
KeyCharEventPtr evt = new KeyCharEvent;
evt->value = char(wparam);
2020-02-14 17:12:13 +08:00
this->PushEvent(evt);
2020-01-21 10:09:55 +08:00
}
break;
case WM_LBUTTONDOWN:
case WM_LBUTTONDBLCLK:
case WM_RBUTTONDOWN:
case WM_RBUTTONDBLCLK:
case WM_MBUTTONDOWN:
case WM_MBUTTONDBLCLK:
{
MouseDownEventPtr evt = new MouseDownEvent;
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;
}
else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK)
{
evt->button = MouseButton::Middle;
}
2020-02-14 17:12:13 +08:00
this->PushEvent(evt);
2020-01-21 10:09:55 +08:00
}
break;
case WM_LBUTTONUP:
case WM_MBUTTONUP:
case WM_RBUTTONUP:
{
MouseUpEventPtr evt = new MouseUpEvent;
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;
}
else if (msg == WM_MBUTTONUP)
{
evt->button = MouseButton::Middle;
}
2020-02-14 17:12:13 +08:00
this->PushEvent(evt);
2020-01-21 10:09:55 +08:00
}
break;
case WM_MOUSEMOVE:
{
MouseMoveEventPtr evt = new MouseMoveEvent;
evt->pos = Point((float)GET_X_LPARAM(lparam), (float)GET_Y_LPARAM(lparam));
2020-02-14 17:12:13 +08:00
this->PushEvent(evt);
2020-01-21 10:09:55 +08:00
}
break;
case WM_MOUSEWHEEL:
{
MouseWheelEventPtr evt = new MouseWheelEvent;
evt->pos = Point((float)GET_X_LPARAM(lparam), (float)GET_Y_LPARAM(lparam));
evt->wheel = GET_WHEEL_DELTA_WPARAM(wparam) / (float)WHEEL_DELTA;
2020-02-14 17:12:13 +08:00
this->PushEvent(evt);
2020-01-21 10:09:55 +08:00
}
break;
case WM_SIZE:
{
if (SIZE_MAXHIDE == wparam || SIZE_MINIMIZED == wparam)
{
2020-02-10 13:47:00 +08:00
KGE_SYS_LOG("Window minimized");
2020-05-20 11:55:53 +08:00
is_minimized_ = true;
// Pause game when window is minimized
if (Application::GetInstance().IsRunning())
{
TimerPtr timer = Application::GetInstance().GetTimer();
timer->Pause();
}
}
else if (SIZE_MAXIMIZED == wparam)
{
2020-05-20 23:38:24 +08:00
KGE_SYS_LOG("Window maximized");
2020-05-20 11:55:53 +08:00
if (is_minimized_)
{
is_minimized_ = false;
if (Application::GetInstance().IsRunning())
{
TimerPtr timer = Application::GetInstance().GetTimer();
2020-05-20 23:38:24 +08:00
timer->Resume();
2020-05-20 11:55:53 +08:00
}
}
2020-01-21 10:09:55 +08:00
}
2020-05-20 11:55:53 +08:00
else if (wparam == SIZE_RESTORED)
2020-01-21 10:09:55 +08:00
{
2020-05-20 11:55:53 +08:00
if (is_minimized_)
{
KGE_SYS_LOG("Window restored");
// the window was restored and was previously minimized
is_minimized_ = false;
if (Application::GetInstance().IsRunning())
{
TimerPtr timer = Application::GetInstance().GetTimer();
2020-05-20 23:38:24 +08:00
timer->Resume();
2020-05-20 11:55:53 +08:00
}
}
else if (is_resizing_)
{
// DO NOTHING until the dragging / resizing has stopped.
}
else
{
this->width_ = ((uint32_t)(short)LOWORD(lparam));
this->height_ = ((uint32_t)(short)HIWORD(lparam));
2020-01-21 10:09:55 +08:00
2020-05-20 11:55:53 +08:00
WindowResizedEventPtr evt = new WindowResizedEvent;
evt->width = this->GetWidth();
evt->height = this->GetHeight();
this->PushEvent(evt);
2020-05-20 23:38:24 +08:00
KGE_SYS_LOG("Window resized to (%d, %d)", this->width_, this->height_);
2020-05-20 11:55:53 +08:00
}
}
}
break;
case WM_ENTERSIZEMOVE:
{
is_resizing_ = true;
if (Application::GetInstance().IsRunning())
{
TimerPtr timer = Application::GetInstance().GetTimer();
timer->Pause();
2020-01-21 10:09:55 +08:00
}
2020-05-20 11:55:53 +08:00
return 0;
2020-01-21 10:09:55 +08:00
}
2020-05-20 11:55:53 +08:00
case WM_EXITSIZEMOVE:
{
is_resizing_ = false;
if (Application::GetInstance().IsRunning())
{
TimerPtr timer = Application::GetInstance().GetTimer();
timer->Resume();
}
2020-05-20 23:38:24 +08:00
// Send window resized event when client size changed
RECT client_rect = { 0 };
::GetClientRect(hwnd, &client_rect);
uint32_t client_width = uint32_t(client_rect.right - client_rect.left);
uint32_t client_height = uint32_t(client_rect.bottom - client_rect.top);
if (client_width != this->GetWidth() || client_height != this->GetHeight())
{
KGE_SYS_LOG("Window resized to (%d, %d)", client_width, client_height);
this->width_ = client_width;
this->height_ = client_height;
WindowResizedEventPtr evt = new WindowResizedEvent;
evt->width = this->GetWidth();
evt->height = this->GetHeight();
this->PushEvent(evt);
}
2020-05-20 11:55:53 +08:00
return 0;
}
case WM_GETMINMAXINFO:
{
2020-05-20 23:38:24 +08:00
if (min_width_ || min_height_)
{
((MINMAXINFO*)lparam)->ptMinTrackSize = POINT{ LONG(min_width_), LONG(min_height_) };
}
if (max_width_ || max_height_)
{
((MINMAXINFO*)lparam)->ptMaxTrackSize = POINT{ LONG(max_width_), LONG(max_height_) };
}
return 0;
2020-05-20 11:55:53 +08:00
}
2020-01-21 10:09:55 +08:00
case WM_MOVE:
{
WindowMovedEventPtr evt = new WindowMovedEvent;
evt->x = GET_X_LPARAM(lparam);
evt->y = GET_Y_LPARAM(lparam);
2020-02-14 17:12:13 +08:00
this->PushEvent(evt);
2020-01-21 10:09:55 +08:00
}
break;
2020-05-20 11:55:53 +08:00
case WM_MENUCHAR:
{
// Disables the crazy beeping sound when pressing a mnemonic key.
// Simply tell Windows that we want the menu closed.
return MAKELRESULT(0, MNC_CLOSE);
}
2020-01-21 10:09:55 +08:00
case WM_ACTIVATE:
{
2020-05-20 00:55:51 +08:00
bool active = (LOWORD(wparam) != WA_INACTIVE);
2020-01-21 10:09:55 +08:00
WindowFocusChangedEventPtr evt = new WindowFocusChangedEvent;
2020-05-20 00:55:51 +08:00
evt->focus = active;
2020-02-14 17:12:13 +08:00
this->PushEvent(evt);
2020-01-21 10:09:55 +08:00
}
break;
2020-05-17 21:02:43 +08:00
case WM_SETFOCUS:
{
if (is_fullscreen_)
{
// TODO restore to fullscreen mode
// Renderer::GetInstance().SetResolution();
}
}
break;
case WM_KILLFOCUS:
{
if (is_fullscreen_)
{
// TODO exit fullscreen mode
// ::ShowWindow(handle_, SW_MINIMIZE);
}
}
break;
2020-01-21 10:09:55 +08:00
case WM_SETTEXT:
{
2020-02-10 13:47:00 +08:00
KGE_SYS_LOG("Window title changed");
2020-01-21 10:09:55 +08:00
2020-04-14 12:28:29 +08:00
this->title_ = strings::WideToNarrow(reinterpret_cast<LPCWSTR>(lparam));
2020-01-21 10:09:55 +08:00
WindowTitleChangedEventPtr evt = new WindowTitleChangedEvent;
2020-02-14 17:12:13 +08:00
evt->title = this->title_;
this->PushEvent(evt);
2020-01-21 10:09:55 +08:00
}
break;
case WM_SETICON:
{
2020-02-10 13:47:00 +08:00
KGE_SYS_LOG("Window icon changed");
2020-01-21 10:09:55 +08:00
}
break;
case WM_DISPLAYCHANGE:
{
2020-02-10 13:47:00 +08:00
KGE_SYS_LOG("The display resolution has changed");
2020-01-21 10:09:55 +08:00
2020-05-17 21:02:43 +08:00
::InvalidateRect(hwnd, NULL, FALSE);
2020-01-21 10:09:55 +08:00
}
break;
case WM_SETCURSOR:
{
2020-02-14 17:12:13 +08:00
this->UpdateCursor();
2020-01-21 10:09:55 +08:00
}
break;
case WM_CLOSE:
{
2020-02-10 13:47:00 +08:00
KGE_SYS_LOG("Window is closing");
2020-01-21 10:09:55 +08:00
WindowClosedEventPtr evt = new WindowClosedEvent;
2020-02-14 17:12:13 +08:00
this->PushEvent(evt);
this->SetShouldClose(true);
return 0;
2020-01-21 10:09:55 +08:00
}
break;
case WM_DESTROY:
{
2020-02-10 13:47:00 +08:00
KGE_SYS_LOG("Window was destroyed");
2020-01-21 10:09:55 +08:00
::PostQuitMessage(0);
return 0;
}
break;
}
return ::DefWindowProcA(hwnd, msg, wparam, lparam);
2020-01-21 10:09:55 +08:00
}
2020-02-14 22:01:56 +08:00
LRESULT CALLBACK WindowWin32Impl::StaticWndProc(HWND hwnd, UINT32 msg, WPARAM wparam, LPARAM lparam)
2020-02-14 17:12:13 +08:00
{
LONG_PTR ptr = static_cast<LONG_PTR>(::GetWindowLongPtrA(hwnd, GWLP_USERDATA));
2020-02-14 17:12:13 +08:00
if (ptr)
{
2020-02-14 22:01:56 +08:00
WindowWin32Impl* window = reinterpret_cast<WindowWin32Impl*>(ptr);
2020-02-14 17:12:13 +08:00
return window->MessageProc(hwnd, msg, wparam, lparam);
}
return ::DefWindowProcA(hwnd, msg, wparam, lparam);
2020-02-14 17:12:13 +08:00
}
2020-01-21 10:09:55 +08:00
} // namespace kiwano
2020-01-17 11:24:24 +08:00
#endif