Files
ReWindow/include/ReWindow/data/WindowsEventLoop.h
Redacted c0ee42b782
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m27s
RWindow::Flash windows.
2025-01-28 18:40:46 -05:00

140 lines
4.4 KiB
C++

#pragma once
#ifdef _WIN32
#include <Windows.h>
#include <ReWindow/types/Window.h>
using namespace ReWindow;
//Event loop.
// TODO move this. It can't be in the RWindow class because it has to be publicly visible.
inline LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
RWindow* window = reinterpret_cast<RWindow*>( GetWindowLongPtr(hwnd, GWLP_USERDATA) );
switch (uMsg) {
case WM_CLOSE: {
DestroyWindow(hwnd);
window->processOnClose();
}
case WM_DESTROY: {
exit(0);
}
case WM_SIZE: {
window->SetSizeWithoutEvent({ LOWORD(lParam), HIWORD(lParam) });
auto eventData = WindowResizeRequestEvent();
eventData.Size = { window->GetWidth(), window->GetHeight() };
window->SetLastKnownWindowSize({ window->GetWidth(), window->GetHeight() });
// TODO: Implement eWindow->processOnResize()
window->OnResizeRequest(eventData);
window->OnResizeRequestEvent(eventData);
break;
}
case WM_SETFOCUS: {
window->processFocusIn();
window->SetFlag(WindowFlag::IN_FOCUS, true);
// TODO actually check if it's flashing.
FLASHWINFO fi;
fi.cbSize = sizeof(FLASHWINFO);
fi.hwnd = hwnd;
fi.dwFlags = FLASHW_STOP;
fi.uCount = 0;
fi.dwTimeout = 0;
FlashWindowEx(&fi);
break;
}
case WM_KILLFOCUS: {
window->processFocusOut();
window->SetFlag(WindowFlag::IN_FOCUS, false);
}
case WM_SETCURSOR: {
if (LOWORD(lParam) == HTCLIENT && window->GetCursorVisible() == false)
SetCursor(nullptr);
break;
}
case WM_KEYDOWN: {
auto key = GetKeyFromWindowsScancode((WindowsScancode) wParam);
//Key repeat fix.
if (!window->previousKeyboard.PressedKeys[key])
window->processKeyPress(key);
break;
}
case WM_KEYUP: {
auto key = GetKeyFromWindowsScancode((WindowsScancode) wParam);
window->processKeyRelease(key);
break;
}
//Mouse Buttons.
case WM_MOUSEWHEEL: {
int wheel_delta = GET_WHEEL_DELTA_WPARAM(wParam);
// TODO: Determine sign of wheel_delta for each direction, (and on linux too), and document this.
window->processMouseWheel(wheel_delta);\
break;
}
case WM_LBUTTONDOWN: {
window->processMousePress(MouseButtons::Left);
break;
}
case WM_LBUTTONUP: {
window->processMouseRelease(MouseButtons::Left);
break;
}
case WM_RBUTTONDOWN: {
window->processMousePress(MouseButtons::Right);
break;
}
case WM_RBUTTONUP: {
window->processMouseRelease(MouseButtons::Right);
break;
}
case WM_MBUTTONDOWN: {
window->processMousePress(MouseButtons::Middle);
break;
}
case WM_MBUTTONUP: {
window->processMouseRelease(MouseButtons::Middle);
break;
}
case WM_XBUTTONDOWN: {
WORD button = GET_XBUTTON_WPARAM(wParam);
if (button == XBUTTON1)
window->processMousePress(MouseButtons::Mouse4);
if (button == XBUTTON2)
window->processMousePress(MouseButtons::Mouse5);
break;
}
case WM_XBUTTONUP: {
WORD button = GET_XBUTTON_WPARAM(wParam);
if (button == XBUTTON1)
window->processMouseRelease(MouseButtons::Mouse4);
if (button == XBUTTON2)
window->processMouseRelease(MouseButtons::Mouse5);
break;
}
//This is the same as "Motion Notify" in the X Window System.
case WM_MOUSEMOVE:
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
#endif