Add mouse hook to the color picker.

This commit is contained in:
Menotdan
2023-05-11 13:20:43 -04:00
parent a2d4207504
commit f94ae3f3fd
5 changed files with 51 additions and 4 deletions

View File

@@ -9,3 +9,6 @@ Color32 ScreenUtilsBase::GetPixelAt(int32 x, int32 y) {
Int2 ScreenUtilsBase::GetScreenCursorPosition() {
return { 0, 0 };
}
void ScreenUtilsBase::BlockAndReadMouse() {
}

View File

@@ -26,4 +26,9 @@ public:
/// </summary>
/// <returns>Cursor position, in screen coordinates.</returns>
API_FUNCTION() static Int2 GetScreenCursorPosition();
/// <summary>
/// Blocks mouse input and runs a callback
/// </summary>
API_FUNCTION() static void BlockAndReadMouse();
};

View File

@@ -7,8 +7,10 @@
#if PLATFORM_WINDOWS
#pragma comment(lib, "Gdi32.lib")
#endif
#include <Engine/Core/Log.h>
Color32 ScreenUtils::GetPixelAt(int32 x, int32 y) {
Color32 ScreenUtils::GetPixelAt(int32 x, int32 y)
{
HDC deviceContext = GetDC(NULL);
COLORREF color = GetPixel(deviceContext, x, y);
ReleaseDC(NULL, deviceContext);
@@ -17,10 +19,45 @@ Color32 ScreenUtils::GetPixelAt(int32 x, int32 y) {
return returnColor;
}
Int2 ScreenUtils::GetScreenCursorPosition() {
Int2 ScreenUtils::GetScreenCursorPosition()
{
POINT cursorPos;
GetCursorPos(&cursorPos);
Int2 returnCursorPos = { cursorPos.x, cursorPos.y };
return returnCursorPos;
}
static HHOOK _mouseCallbackHook;
LRESULT CALLBACK ScreenUtilsMouseCallback(
_In_ int nCode,
_In_ WPARAM wParam,
_In_ LPARAM lParam
)
{
LOG(Warning, "Hell lag. {0}", GetCurrentThreadId());
if (wParam != WM_LBUTTONDOWN) { // Return as early as possible.
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
if (nCode < 0) {
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
if (nCode >= 0 && wParam == WM_LBUTTONDOWN) { // Now try to run our code.
LOG(Warning, "Mouse callback hit. Skipping event. (hopefully)");
UnhookWindowsHookEx(_mouseCallbackHook);
return 1;
}
}
void ScreenUtils::BlockAndReadMouse()
{
_mouseCallbackHook = SetWindowsHookEx(WH_MOUSE_LL, ScreenUtilsMouseCallback, NULL, NULL);
if (_mouseCallbackHook == NULL)
{
LOG(Warning, "Failed to set mouse hook.");
LOG(Warning, "Error: {0}", GetLastError());
}
}

View File

@@ -11,6 +11,7 @@ public:
// [ScreenUtilsBase]
static Color32 GetPixelAt(int32 x, int32 y);
static Int2 GetScreenCursorPosition();
static void BlockAndReadMouse();
};
#endif