23 DestroyWindow(_handle);
30 if (_windowCount == 0) {
31 UnregisterClassW(className, GetModuleHandleW(
nullptr));
35 SetWindowLongPtrW(_handle, GWLP_WNDPROC, _callback);
42 if (_windowCount == 0) {
43 _hinstance = GetModuleHandleW(
nullptr);
48 HDC screenDC = GetDC(
nullptr);
49 const uint16_t left =
static_cast<uint16_t
>(GetDeviceCaps(screenDC, HORZRES) - initInfo.
width) / 2;
50 const uint16_t top =
static_cast<uint16_t
>(GetDeviceCaps(screenDC, VERTRES) - initInfo.
height) / 2;
51 ReleaseDC(
nullptr, screenDC);
54 DWORD win32Style = WS_VISIBLE;
57 win32Style |= WS_POPUP;
60 win32Style |= WS_CAPTION | WS_MINIMIZEBOX;
64 win32Style |= WS_THICKFRAME | WS_MAXIMIZEBOX;
68 win32Style |= WS_SYSMENU;
74 RECT rectangle = { 0, 0, initInfo.
width, initInfo.
height };
77 AdjustWindowRect(&rectangle, win32Style,
false);
81 #pragma warning(disable : 4996) 82 std::vector<wchar_t> realTitle(initInfo.
title.length() + 1);
83 mbstowcs(&realTitle[0], initInfo.
title.c_str(), initInfo.
title.length() + 1);
86 _handle = CreateWindowW(
92 rectangle.right - rectangle.left,
93 rectangle.bottom - rectangle.top,
96 GetModuleHandleW(
nullptr),
101 if (!activateFullscreen()) {
102 LUG_LOG.error(
"WindowImpl::Win32::Init Can't activate full-screen");
106 setMouseCursorVisible(
true);
115 if (_fullscreenWindow ==
this) {
116 ChangeDisplaySettingsW(
nullptr, 0);
117 _fullscreenWindow =
nullptr;
349 while (PeekMessageW(&message,
nullptr, 0, 0, PM_REMOVE)) {
350 TranslateMessage(&message);
351 DispatchMessageW(&message);
355 if (!_events.empty()) {
356 event = _events.front();
369 _cursor = visible ? LoadCursor(
nullptr, IDC_ARROW) : NULL;
374 POINT mouseScreenPosition{mousePosition.x(), mousePosition.y()};
375 ClientToScreen(_handle, &mouseScreenPosition);
376 SetCursorPos(mouseScreenPosition.x, mouseScreenPosition.y);
401 if (GetClientRect(_handle, &rect)) {
402 _parent->_mode.width =
static_cast<uint16_t
>(rect.right - rect.left);
403 _parent->_mode.height =
static_cast<uint16_t
>(rect.bottom - rect.top);
405 if (!_parent->_mode.width || !_parent->_mode.height) {
409 LUG_LOG.error(
"WindowImpl::Win32::processWindowEvents Can't get window rect");
423 if (LOWORD(lParam) == HTCLIENT)
429 if (_keyRepeat || ((HIWORD(lParam) & KF_REPEAT) == 0)) {
431 configKeyEvent(e.
key, wParam, lParam);
440 configKeyEvent(e.
key, wParam, lParam);
444 if (_keyRepeat || ((lParam & (1 << 30)) == 0)) {
457 configMouseEvent(e.
mouse, wParam, lParam);
458 configMouseButtonEvent(e.
mouse, message, wParam, lParam);
466 configMouseEvent(e.
mouse, wParam, lParam);
467 configMouseButtonEvent(e.
mouse, message, wParam, lParam);
473 configMouseEvent(e.
mouse, wParam, lParam);
475 if (_mouseIsIn ==
false) {
488 configMouseEvent(e.
mouse, wParam, lParam);
495 configMouseEvent(e.
mouse, wParam, lParam);
496 configMouseWheelEvent(e.
mouse, message, wParam);
503 _events.push(std::move(e));
508 WNDCLASSW windowClass = {};
510 windowClass.style = 0;
512 windowClass.cbClsExtra = 0;
513 windowClass.cbWndExtra = 0;
514 windowClass.hInstance = _hinstance;
515 windowClass.hIcon = LoadIcon(
nullptr, IDI_WINLOGO);
516 windowClass.hCursor = NULL;
517 windowClass.hbrBackground = 0;
518 windowClass.lpszMenuName =
nullptr;
519 windowClass.lpszClassName = className;
521 if (!RegisterClassW(&windowClass)) {
522 MessageBox(
nullptr,
"Failed To Register The Window Class.",
"ERROR", MB_OK | MB_ICONEXCLAMATION);
529 devMode.dmSize =
sizeof(devMode);
530 devMode.dmPelsWidth = _parent->_mode.width;
531 devMode.dmPelsHeight = _parent->_mode.height;
532 devMode.dmBitsPerPel = 32;
533 devMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
536 if (ChangeDisplaySettingsW(&devMode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) {
538 if (MessageBox(
nullptr,
"Full-screen mode has failed to be initialized. Use Windowed Mode Instead?",
"Warning", MB_YESNO | MB_ICONEXCLAMATION) == IDYES) {
543 MessageBox(
nullptr,
"Program Will Now Close.",
"ERROR", MB_OK | MB_ICONSTOP);
549 SetWindowLongW(_handle, GWL_STYLE, WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
550 SetWindowLongW(_handle, GWL_EXSTYLE, WS_EX_APPWINDOW);
553 SetWindowPos(_handle, HWND_TOP, 0, 0, _parent->_mode.width, _parent->_mode.height, SWP_FRAMECHANGED);
554 ShowWindow(_handle, SW_SHOW);
557 _fullscreenWindow =
this;
562 WPARAM new_vk = wParam;
564 UINT scancode = (lParam & 0x00ff0000) >> 16;
565 int extended = (lParam & 0x01000000) != 0;
569 new_vk = MapVirtualKey(scancode, MAPVK_VSC_TO_VK_EX);
572 new_vk = extended ? VK_RCONTROL : VK_LCONTROL;
575 new_vk = extended ? VK_RMENU : VK_LMENU;
586 key.
code = getKeyCode(wParam, lParam);
587 key.
alt = HIWORD(GetAsyncKeyState(VK_MENU)) != 0;
588 key.
ctrl = HIWORD(GetAsyncKeyState(VK_CONTROL)) != 0;
589 key.
shift = HIWORD(GetAsyncKeyState(VK_SHIFT)) != 0;
590 key.
system = HIWORD(GetAsyncKeyState(VK_LWIN)) || HIWORD(GetAsyncKeyState(VK_RWIN));
616 key.
coord.
x =
static_cast<int32_t
>(LOWORD(lParam));
617 key.
coord.
y =
static_cast<int32_t
>(HIWORD(lParam));
629 getMouseCoord(mouse, lParam);
630 getMouseEventModifier(mouse, wParam);
634 int keyModifier = GET_KEYSTATE_WPARAM(wParam);
636 if (keyModifier & MK_CONTROL) {
640 if (keyModifier & MK_SHIFT) {
644 if (keyModifier & MK_LBUTTON) {
648 if (keyModifier & MK_MBUTTON) {
652 if (keyModifier & MK_RBUTTON) {
656 if (keyModifier & MK_XBUTTON1) {
660 if (keyModifier & MK_XBUTTON2) {
666 mouse.
coord.
x =
static_cast<int32_t
>(LOWORD(lParam));
667 mouse.
coord.
y =
static_cast<int32_t
>(HIWORD(lParam));
672 double xOffset = 0.0;
673 double yOffset = 0.0;
675 if (message == WM_MOUSEWHEEL) {
676 xOffset = GET_WHEEL_DELTA_WPARAM(wParam);
677 }
else if (message == WM_MOUSEHWHEEL) {
678 yOffset = GET_WHEEL_DELTA_WPARAM(wParam);
686 if (message == WM_CREATE) {
688 LONG_PTR window = (LONG_PTR)reinterpret_cast<CREATESTRUCT*>(lParam)->lpCreateParams;
691 SetWindowLongPtrW(handle, GWLP_USERDATA, window);
695 WindowImpl* window = handle ?
reinterpret_cast<WindowImpl*
>(GetWindowLongPtr(handle, GWLP_USERDATA)) :
nullptr;
702 return CallWindowProcW(reinterpret_cast<WNDPROC>(window->
_callback), handle, message, wParam, lParam);
707 if (message == WM_CLOSE) {
712 if ((message == WM_SYSCOMMAND) && (wParam == SC_KEYMENU)) {
716 return DefWindowProcW(handle, message, wParam, lParam);
bool activateFullscreen()
Represents a keyboard event.
bool left
True if left mouse button was pressed, False otherwise.
::lug::Window::Style style
void processWindowEvents(UINT message, WPARAM wParam, LPARAM lParam)
Keyboard::Key getKeyCode(WPARAM wParam, LPARAM lParam)
static LRESULT CALLBACK onEvent(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
bool init(const Window::InitInfo &initInfo)
static uint8_t _windowCount
Key
Abstraction of keyboard keys.
void setMousePos(const Math::Vec2i &mousePosition)
bool middle
True if middle mouse button was pressed, False otherwise.
Type type
The type of the event.
void registerWindow() const
HINSTANCE getHinstance() const
void setMouseCursorVisible(bool visible)
struct lug::Window::MouseEvent::@27 coord
The mouse coordinate if the event was triggered by movement.
Keyboard::Key code
The key that triggered the event.
static lug::Window::priv::WindowImpl * _fullscreenWindow
bool ctrl
True if ctrl was pressed, False otherwise.
Mouse::Button code
The button that triggered the event.
struct lug::Window::MouseEvent::@25::@28 scrollOffset
The distance in x and y that the scroll wheel has traveled.
bool ctrl
True if ctrl was pressed, False otherwise.
void configKeyEvent(KeyEvent &key, WPARAM wParam, LPARAM lParam)
bool shift
True if shift was pressed, False otherwise.
bool alt
True if alt was pressed, False otherwise.
LONG_PTR _callback
Stores the original event callback function of the control.
Mouse entered window event.
void configMouseWheelEvent(MouseEvent &mouse, UINT message, WPARAM wParam)
bool x2
True if extra 2 mouse button was pressed, False otherwise.
bool pollEvent(lug::Window::Event &event)
Represents a mouse event.
bool x1
True if extra 1 mouse button was pressed, False otherwise.
void getMouseEventModifier(MouseEvent &mouse, WPARAM wParam)
void setKeyRepeat(bool state)
static Keyboard::Key vkToLugKey(WPARAM key)
CharEvent character
A CharEvent.
void configMouseButtonEvent(MouseEvent &mouse, UINT message, WPARAM wParam, LPARAM lParam)
bool right
True if right mouse button was pressed, False otherwise.
void getMouseCoord(MouseEvent &mouse, LPARAM lParam)
wchar_t val
Raw value of the character pressed (with accents, etc.)
bool shift
True if shift was pressed, False otherwise.
bool system
True if system was pressed, False otherwise.
MouseEvent mouse
A MouseEvent.
void configMouseEvent(MouseEvent &mouse, WPARAM wParam, LPARAM lParam)