Per window DPI
This commit is contained in:
@@ -281,6 +281,8 @@ protected:
|
||||
String _title;
|
||||
CursorType _cursor;
|
||||
Vector2 _clientSize;
|
||||
int _dpi;
|
||||
float _dpiScale;
|
||||
|
||||
Vector2 _trackingMouseOffset;
|
||||
bool _isUsingMouseOffset;
|
||||
@@ -541,6 +543,22 @@ public:
|
||||
/// <returns>The screen space position.</returns>
|
||||
API_FUNCTION() virtual Vector2 ClientToScreen(const Vector2& clientPos) const = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the window DPI setting.
|
||||
/// </summary>
|
||||
API_PROPERTY() int GetDpi() const
|
||||
{
|
||||
return _dpi;
|
||||
}
|
||||
|
||||
// TODO: This doesn't actually include the custom DPI scale
|
||||
/// <summary>
|
||||
/// Gets the window DPI scale factor (1 is default). Includes custom DPI scale
|
||||
/// </summary>
|
||||
API_PROPERTY() float GetDpiScale() const
|
||||
{
|
||||
return Platform::CustomDpiScale * _dpiScale;
|
||||
}
|
||||
public:
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -7,8 +7,6 @@ namespace FlaxEngine
|
||||
{
|
||||
partial class Window
|
||||
{
|
||||
internal float _dpiScale;
|
||||
|
||||
/// <summary>
|
||||
/// Window closing delegate.
|
||||
/// </summary>
|
||||
@@ -176,7 +174,6 @@ namespace FlaxEngine
|
||||
private Window()
|
||||
{
|
||||
GUI = new WindowRootControl(this);
|
||||
_dpiScale = Platform.DpiScale;
|
||||
}
|
||||
|
||||
internal void Internal_OnShow()
|
||||
@@ -192,7 +189,7 @@ namespace FlaxEngine
|
||||
|
||||
internal void Internal_OnDraw()
|
||||
{
|
||||
Matrix3x3.Scaling(_dpiScale, out var scale);
|
||||
Matrix3x3.Scaling(DpiScale, out var scale);
|
||||
Render2D.PushTransform(ref scale);
|
||||
GUI.Draw();
|
||||
Render2D.PopTransform();
|
||||
@@ -200,7 +197,7 @@ namespace FlaxEngine
|
||||
|
||||
internal void Internal_OnResize(int width, int height)
|
||||
{
|
||||
GUI.Size = new Vector2(width / _dpiScale, height / _dpiScale);
|
||||
GUI.Size = new Vector2(width / DpiScale, height / DpiScale);
|
||||
}
|
||||
|
||||
internal void Internal_OnCharInput(char c)
|
||||
@@ -223,7 +220,7 @@ namespace FlaxEngine
|
||||
|
||||
internal void Internal_OnMouseDown(ref Vector2 mousePos, MouseButton button)
|
||||
{
|
||||
Vector2 pos = mousePos / _dpiScale;
|
||||
Vector2 pos = mousePos / DpiScale;
|
||||
|
||||
bool handled = false;
|
||||
MouseDown?.Invoke(ref pos, button, ref handled);
|
||||
@@ -235,7 +232,7 @@ namespace FlaxEngine
|
||||
|
||||
internal void Internal_OnMouseUp(ref Vector2 mousePos, MouseButton button)
|
||||
{
|
||||
Vector2 pos = mousePos / _dpiScale;
|
||||
Vector2 pos = mousePos / DpiScale;
|
||||
|
||||
bool handled = false;
|
||||
MouseUp?.Invoke(ref pos, button, ref handled);
|
||||
@@ -247,7 +244,7 @@ namespace FlaxEngine
|
||||
|
||||
internal void Internal_OnMouseDoubleClick(ref Vector2 mousePos, MouseButton button)
|
||||
{
|
||||
Vector2 pos = mousePos / _dpiScale;
|
||||
Vector2 pos = mousePos / DpiScale;
|
||||
|
||||
bool handled = false;
|
||||
MouseDoubleClick?.Invoke(ref pos, button, ref handled);
|
||||
@@ -259,7 +256,7 @@ namespace FlaxEngine
|
||||
|
||||
internal void Internal_OnMouseWheel(ref Vector2 mousePos, float delta)
|
||||
{
|
||||
Vector2 pos = mousePos / _dpiScale;
|
||||
Vector2 pos = mousePos / DpiScale;
|
||||
|
||||
bool handled = false;
|
||||
MouseWheel?.Invoke(ref pos, delta, ref handled);
|
||||
@@ -271,7 +268,7 @@ namespace FlaxEngine
|
||||
|
||||
internal void Internal_OnMouseMove(ref Vector2 mousePos)
|
||||
{
|
||||
Vector2 pos = mousePos / _dpiScale;
|
||||
Vector2 pos = mousePos / DpiScale;
|
||||
|
||||
MouseMove?.Invoke(ref pos);
|
||||
GUI.OnMouseMove(pos);
|
||||
@@ -285,7 +282,7 @@ namespace FlaxEngine
|
||||
|
||||
internal void Internal_OnTouchDown(ref Vector2 pointerPosition, int pointerId)
|
||||
{
|
||||
Vector2 pos = pointerPosition / _dpiScale;
|
||||
Vector2 pos = pointerPosition / DpiScale;
|
||||
|
||||
bool handled = false;
|
||||
TouchDown?.Invoke(ref pos, pointerId, ref handled);
|
||||
@@ -297,7 +294,7 @@ namespace FlaxEngine
|
||||
|
||||
internal void Internal_OnTouchMove(ref Vector2 pointerPosition, int pointerId)
|
||||
{
|
||||
Vector2 pos = pointerPosition / _dpiScale;
|
||||
Vector2 pos = pointerPosition / DpiScale;
|
||||
|
||||
bool handled = false;
|
||||
TouchMove?.Invoke(ref pos, pointerId, ref handled);
|
||||
@@ -309,7 +306,7 @@ namespace FlaxEngine
|
||||
|
||||
internal void Internal_OnTouchUp(ref Vector2 pointerPosition, int pointerId)
|
||||
{
|
||||
Vector2 pos = pointerPosition / _dpiScale;
|
||||
Vector2 pos = pointerPosition / DpiScale;
|
||||
|
||||
bool handled = false;
|
||||
TouchUp?.Invoke(ref pos, pointerId, ref handled);
|
||||
@@ -335,7 +332,7 @@ namespace FlaxEngine
|
||||
{
|
||||
if (HitTest != null)
|
||||
{
|
||||
Vector2 pos = mousePos / _dpiScale;
|
||||
Vector2 pos = mousePos / DpiScale;
|
||||
result = HitTest(ref pos);
|
||||
handled = true;
|
||||
}
|
||||
@@ -356,7 +353,7 @@ namespace FlaxEngine
|
||||
dragData = new DragDataText(data[0]);
|
||||
else
|
||||
dragData = new DragDataFiles(data);
|
||||
Vector2 pos = mousePos / _dpiScale;
|
||||
Vector2 pos = mousePos / DpiScale;
|
||||
return GUI.OnDragEnter(ref pos, dragData);
|
||||
}
|
||||
|
||||
@@ -367,7 +364,7 @@ namespace FlaxEngine
|
||||
dragData = new DragDataText(data[0]);
|
||||
else
|
||||
dragData = new DragDataFiles(data);
|
||||
Vector2 pos = mousePos / _dpiScale;
|
||||
Vector2 pos = mousePos / DpiScale;
|
||||
return GUI.OnDragMove(ref pos, dragData);
|
||||
}
|
||||
|
||||
@@ -378,7 +375,7 @@ namespace FlaxEngine
|
||||
dragData = new DragDataText(data[0]);
|
||||
else
|
||||
dragData = new DragDataFiles(data);
|
||||
Vector2 pos = mousePos / _dpiScale;
|
||||
Vector2 pos = mousePos / DpiScale;
|
||||
return GUI.OnDragDrop(ref pos, dragData);
|
||||
}
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ int32 CalculateDpi(HMODULE shCoreDll)
|
||||
|
||||
if (getDPIForMonitor)
|
||||
{
|
||||
HMONITOR monitor = GetPrimaryMonitorHandle();
|
||||
HMONITOR monitor = GetPrimaryMonitorHandle(); // TODO: Use the game window monitor
|
||||
|
||||
UINT x = 0, y = 0;
|
||||
HRESULT hr = getDPIForMonitor(monitor, 0, &x, &y);
|
||||
@@ -659,7 +659,7 @@ void WindowsPlatform::SetHighDpiAwarenessEnabled(bool enable)
|
||||
|
||||
if (setProcessDpiAwareness)
|
||||
{
|
||||
setProcessDpiAwareness(enable ? PROCESS_SYSTEM_DPI_AWARE : PROCESS_DPI_UNAWARE);
|
||||
setProcessDpiAwareness(enable ? PROCESS_PER_MONITOR_DPI_AWARE : PROCESS_DPI_UNAWARE);
|
||||
}
|
||||
|
||||
SystemDpi = CalculateDpi(shCoreDll);
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
// TODO: finish better borderless window on windows (fix mouse pos offset when maximized and fix white flicker on window show)
|
||||
#define WINDOWS_USE_NEW_BORDER_LESS 0
|
||||
|
||||
#define DefaultDPI 96
|
||||
|
||||
#if WINDOWS_USE_NEW_BORDER_LESS
|
||||
#pragma comment(lib, "Gdi32.lib")
|
||||
#endif
|
||||
@@ -95,6 +97,23 @@ WindowsWindow::WindowsWindow(const CreateWindowSettings& settings)
|
||||
nullptr,
|
||||
(HINSTANCE)Platform::Instance,
|
||||
nullptr);
|
||||
|
||||
_dpi = DefaultDPI;
|
||||
// TODO: Is this the correct way of doing this?
|
||||
const HMODULE user32Dll = LoadLibraryW(L"user32.dll");
|
||||
if (user32Dll)
|
||||
{
|
||||
typedef UINT(STDAPICALLTYPE* GetDpiForWindowProc)(HWND hwnd);
|
||||
const GetDpiForWindowProc getDpiForWindowProc = (GetDpiForWindowProc)GetProcAddress(user32Dll, "GetDpiForWindow");
|
||||
|
||||
if (getDpiForWindowProc)
|
||||
{
|
||||
_dpi = getDpiForWindowProc(_handle);
|
||||
}
|
||||
FreeLibrary(user32Dll);
|
||||
}
|
||||
|
||||
_dpiScale = (float)_dpi / (float)DefaultDPI;
|
||||
|
||||
// Validate result
|
||||
if (!HasHWND())
|
||||
@@ -966,6 +985,28 @@ LRESULT WindowsWindow::WndProc(UINT msg, WPARAM wParam, LPARAM lParam)
|
||||
break;
|
||||
}
|
||||
|
||||
case WM_DPICHANGED:
|
||||
{
|
||||
// Maybe https://stackoverflow.com/a/45110656
|
||||
_dpi = HIWORD(wParam);
|
||||
_dpiScale = (float)_dpi / (float)DefaultDPI;
|
||||
|
||||
|
||||
RECT* windowRect = (RECT*)lParam;
|
||||
SetWindowPos(_handle,
|
||||
nullptr,
|
||||
windowRect->left,
|
||||
windowRect->top,
|
||||
windowRect->right - windowRect->left,
|
||||
windowRect->bottom - windowRect->top,
|
||||
SWP_NOZORDER | SWP_NOACTIVATE);
|
||||
|
||||
|
||||
// Todo: Recalculate fonts
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_ENTERSIZEMOVE:
|
||||
{
|
||||
_isResizing = true;
|
||||
|
||||
@@ -67,7 +67,7 @@ bool FontManagerService::Init()
|
||||
ASSERT(Library == nullptr);
|
||||
|
||||
// Scale UI fonts to match the monitor DPI
|
||||
FontManager::FontScale = (float)Platform::GetDpi() / (float)DefaultDPI;
|
||||
FontManager::FontScale = (float)Platform::GetDpi() / (float)DefaultDPI; // TODO: Adjust this at runtime
|
||||
|
||||
// Init Free Type
|
||||
FreeTypeMemory.user = nullptr;
|
||||
|
||||
@@ -310,7 +310,7 @@ namespace FlaxEngine.GUI
|
||||
/// <summary>
|
||||
/// Gets the GUI window root control which contains that control (or null if not linked to any).
|
||||
/// </summary>
|
||||
public virtual WindowRootControl RootWindow => _parent?.RootWindow;
|
||||
public virtual WindowRootControl RootWindow => _parent?.RootWindow; // TODO: Why doesn't this just go "_root?.RootWindow" or something?
|
||||
|
||||
/// <summary>
|
||||
/// Gets screen position of the control (upper left corner).
|
||||
|
||||
@@ -221,7 +221,7 @@ namespace FlaxEngine.GUI
|
||||
/// </summary>
|
||||
public void SyncBackbufferSize()
|
||||
{
|
||||
float scale = ResolutionScale * Platform.DpiScale;
|
||||
float scale = ResolutionScale * (Root?.RootWindow?.Window?.DpiScale ?? 1); // TODO: Figure this out
|
||||
int width = Mathf.CeilToInt(Width * scale);
|
||||
int height = Mathf.CeilToInt(Height * scale);
|
||||
if (_customResolution.HasValue)
|
||||
|
||||
@@ -68,7 +68,7 @@ namespace FlaxEngine.GUI
|
||||
var parentWin = target.Root;
|
||||
if (parentWin == null)
|
||||
return;
|
||||
float dpiScale = Platform.DpiScale;
|
||||
float dpiScale = target.RootWindow.Window.DpiScale;
|
||||
Vector2 dpiSize = Size * dpiScale;
|
||||
Vector2 locationWS = target.PointToWindow(location);
|
||||
Vector2 locationSS = parentWin.PointToScreen(locationWS);
|
||||
@@ -185,7 +185,7 @@ namespace FlaxEngine.GUI
|
||||
{
|
||||
if (_window)
|
||||
{
|
||||
_window.ClientSize = Size * Platform.DpiScale;
|
||||
_window.ClientSize = Size * _window.DpiScale;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -151,7 +151,7 @@ namespace FlaxEngine.GUI
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Vector2 TrackingMouseOffset => _window.TrackingMouseOffset / _window._dpiScale;
|
||||
public override Vector2 TrackingMouseOffset => _window.TrackingMouseOffset / _window.DpiScale;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override WindowRootControl RootWindow => this;
|
||||
@@ -159,8 +159,8 @@ namespace FlaxEngine.GUI
|
||||
/// <inheritdoc />
|
||||
public override Vector2 MousePosition
|
||||
{
|
||||
get => _window.MousePosition / _window._dpiScale;
|
||||
set => _window.MousePosition = value * _window._dpiScale;
|
||||
get => _window.MousePosition / _window.DpiScale;
|
||||
set => _window.MousePosition = value * _window.DpiScale;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -234,13 +234,13 @@ namespace FlaxEngine.GUI
|
||||
/// <inheritdoc />
|
||||
public override Vector2 PointFromScreen(Vector2 location)
|
||||
{
|
||||
return _window.ScreenToClient(location) / _window._dpiScale;
|
||||
return _window.ScreenToClient(location) / _window.DpiScale;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Vector2 PointToScreen(Vector2 location)
|
||||
{
|
||||
return _window.ClientToScreen(location * _window._dpiScale);
|
||||
return _window.ClientToScreen(location * _window.DpiScale);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
Reference in New Issue
Block a user