Add Platform::GetMousePosition/WindowsPlatform::SetMousePosition for unified access to screen-space mouse position on all platforms

#1109
This commit is contained in:
Wojtek Figat
2023-05-28 15:30:16 +02:00
parent 482d516c2e
commit 9cdd1cbc45
10 changed files with 45 additions and 33 deletions

View File

@@ -5,6 +5,7 @@
#include "Engine/Platform/MemoryStats.h"
#include "Engine/Platform/MessageBox.h"
#include "Engine/Platform/FileSystem.h"
#include "Engine/Platform/Window.h"
#include "Engine/Platform/User.h"
#include "Engine/Core/Log.h"
#include "Engine/Core/Types/DateTime.h"
@@ -520,6 +521,21 @@ void PlatformBase::CreateGuid(Guid& result)
result = Guid(dateThingHigh, randomThing | (sequentialThing << 16), cyclesThing, dateThingLow);
}
Float2 PlatformBase::GetMousePosition()
{
const Window* win = Engine::MainWindow;
if (win)
return win->ClientToScreen(win->GetMousePosition());
return Float2::Minimum;
}
void PlatformBase::SetMousePosition(const Float2& position)
{
const Window* win = Engine::MainWindow;
if (win)
win->SetMousePosition(win->ScreenToClient(position));
}
Float2 PlatformBase::GetVirtualDesktopSize()
{
return Platform::GetVirtualDesktopBounds().Size;

View File

@@ -660,6 +660,18 @@ public:
public:
/// <summary>
/// Gets the mouse cursor position in screen-space coordinates.
/// </summary>
/// <returns>Mouse cursor coordinates.</returns>
API_PROPERTY() static Float2 GetMousePosition();
/// <summary>
/// Sets the mouse cursor position in screen-space coordinates.
/// </summary>
/// <param name="position">Cursor position to set.</param>
API_PROPERTY() static void SetMousePosition(const Float2& position);
/// <summary>
/// Gets the origin position and size of the monitor at the given screen-space location.
/// </summary>

View File

@@ -2643,10 +2643,8 @@ Float2 LinuxPlatform::GetMousePosition()
{
if (!xDisplay)
return Float2::Zero;
int32 x, y;
int32 x = 0, y = 0;
uint32 screenCount = (uint32)X11::XScreenCount(xDisplay);
for (uint32 i = 0; i < screenCount; i++)
{
X11::Window outRoot, outChild;
@@ -2655,7 +2653,6 @@ Float2 LinuxPlatform::GetMousePosition()
if (X11::XQueryPointer(xDisplay, X11::XRootWindow(xDisplay, i), &outRoot, &outChild, &x, &y, &childX, &childY, &mask))
break;
}
return Float2((float)x, (float)y);
}

View File

@@ -158,27 +158,6 @@ void UWPPlatform::OpenUrl(const StringView& url)
// TODO: add support for OpenUrl on UWP
}
Float2 UWPPlatform::GetMousePosition()
{
// Use the main window
auto win = Engine::MainWindow;
if (win)
{
return win->ClientToScreen(win->GetMousePosition());
}
return Float2::Minimum;
}
void UWPPlatform::SetMousePosition(const Float2& pos)
{
// Use the main window
auto win = Engine::MainWindow;
if (win)
{
win->SetMousePosition(win->ScreenToClient(pos));
}
}
Float2 UWPPlatform::GetDesktopSize()
{
Float2 result;

View File

@@ -37,8 +37,6 @@ public:
static bool GetHasFocus();
static bool CanOpenUrl(const StringView& url);
static void OpenUrl(const StringView& url);
static Float2 GetMousePosition();
static void SetMousePosition(const Float2& pos);
static Rectangle GetMonitorBounds(const Float2& screenPos);
static Float2 GetDesktopSize();
static Rectangle GetVirtualDesktopBounds();

View File

@@ -109,7 +109,7 @@ public:
static void CreateGuid(Guid& result);
static String GetMainDirectory();
static String GetExecutableFilePath();
static struct Guid GetUniqueDeviceId();
static Guid GetUniqueDeviceId();
static String GetWorkingDirectory();
static bool SetWorkingDirectory(const String& path);
static void FreeLibrary(void* handle);

View File

@@ -825,6 +825,18 @@ void WindowsPlatform::OpenUrl(const StringView& url)
::ShellExecuteW(nullptr, TEXT("open"), *url, nullptr, nullptr, SW_SHOWNORMAL);
}
Float2 WindowsPlatform::GetMousePosition()
{
POINT cursorPos;
GetCursorPos(&cursorPos);
return Float2((float)cursorPos.x, (float)cursorPos.y);
}
void WindowsPlatform::SetMousePosition(const Float2& pos)
{
::SetCursorPos((int)pos.X, (int)pos.Y);
}
struct GetMonitorBoundsData
{
Float2 Pos;

View File

@@ -71,6 +71,8 @@ public:
static bool GetHasFocus();
static bool CanOpenUrl(const StringView& url);
static void OpenUrl(const StringView& url);
static Float2 GetMousePosition();
static void SetMousePosition(const Float2& pos);
static Rectangle GetMonitorBounds(const Float2& screenPos);
static Float2 GetDesktopSize();
static Rectangle GetVirtualDesktopBounds();