I am using mouse position for drawing a selection Marquee and Panning the Camera (it is RTS like Style Game).
I am using GetMousePosition
inside the tick.
In most cases it works fine However I have found an Issue when:
The frame is CPU Bounded (on unlimited engine frame rate (no Vsync, no smoothing no suchj things))
and some of the Mouse Buttons Pressed the mouse position is not getting updated properly - in case it updates once per 2 seconds (approximately not really a time pattern).
For Now I was able to find a work around for Windows and macOS only:
PlayerController.h
#if PLATFORM_WINDOWS
#include "Windows/WindowsSystemIncludes.h"
#include "AllowWindowsPlatformTypes.h"
#include <Windows.h>
#include "HideWindowsPlatformTypes.h"
#endif //Windows
#if PLATFORM_MAC
private:
#import <Cocoa/Cocoa.h>
NSWindow * m_windowHandle;
#endif
#if PLATFORM_WINDOWS
private:
HWND m_windowHandle;
#endif
PlayerController.cpp
void APlayerController::BeginPlay()
{
Super::BeginPlay();
// bShowMouseCursor = true;
#if PLATFORM_MAC
NSArray * windows = [[NSApplication sharedApplication] windows];
m_windowHandle = windows[0];
#endif
#if PLATFORM_WINDOWS
m_windowHandle = GetActiveWindow();
#endif
//...
//...
//...
}
void APlayerController::Tick( float DeltaSeconds )
{
Super::Tick(DeltaSeconds);
//Get ViewPortSize
// (0,0) is at top left
int32 viewportX, viewportY;
GetViewportSize(viewportX, viewportY);
float mouseX = 100, mouseY = 100;
//If Window in focus
bool windowInFocus = true;
bool mouseInsideWindow = true;
//Mouse warp for Mac
#if PLATFORM_MAC
windowInFocus = (bool)m_windowHandle.isKeyWindow;
if(windowInFocus)
{
//If Window is in focus -> we must ensure the MouseWarp
NSPoint mouse = [NSEvent mouseLocation];
bool bx = false ,by = false;
//If - Mouse is inside the Window we don't care!
if(!CGRectContainsPoint((CGRect)m_windowHandle.frame, (CGPoint)mouse))
{
mouseInsideWindow = false;
//WarpX
if (mouse.x < m_windowHandle.frame.origin.x)
{
mouseX = 0;
bx = true;
}
else if(mouse.x > m_windowHandle.frame.origin.x + m_windowHandle.frame.size.width)
{
mouseX = viewportX;
bx = true;
}
//WarpY //Y has opposite coordinates in unreal
if (mouse.y < m_windowHandle.frame.origin.y)
{
mouseY = viewportY;
by = true;
}
else if(mouse.y >= m_windowHandle.frame.origin.y + m_windowHandle.frame.size.height)
{
mouseY = 0;
by = true;
}
float temp;
if(bx && !by)
{
//get y
GetMousePosition(temp, mouseY);
}
else if (by && !bx)
{
//get x
GetMousePosition(mouseX, temp);
}
}
}
else
{
//macOS Escape
return;
}
#endif
//Do mouse warp for Windows ://
#if PLATFORM_WINDOWS
POINT p;
if (!GetCursorPos(&p))
{
//cursor position now in p.x and p.y
return;
}
if (!ScreenToClient(m_windowHandle, &p))
{
//p.x and p.y are now relative to hwnd's client area
return;
}
mouseX = (float)p.x;
mouseY = (float)p.y;
#endif
//if play in editor get the mouse as is.
if (GetWorld()->WorldType == EWorldType::PIE)
{
GetMousePosition(mouseX, mouseY);
}
//At This Point The Mouse Coordinates will be stored in mouseX, mouseY float variables.
//...
//...
//...
}
Is there a better nicer way to fix it ?