Fix game viewport size to not include DPI scale (screen-space uses it)

#2976
This commit is contained in:
Wojtek Figat
2025-02-26 20:55:57 +01:00
parent 2d956ebb36
commit 809fd2653a
3 changed files with 13 additions and 4 deletions

View File

@@ -1550,9 +1550,9 @@ namespace FlaxEditor
// Handle case when Game window is not selected in tab view
var dockedTo = gameWin.ParentDockPanel;
if (dockedTo != null && dockedTo.SelectedTab != gameWin && dockedTo.SelectedTab != null)
result = dockedTo.SelectedTab.Size * root.DpiScale;
result = dockedTo.SelectedTab.Size;
else
result = gameWin.Viewport.Size * root.DpiScale;
result = gameWin.Viewport.Size;
result = Float2.Round(result);
}

View File

@@ -30,7 +30,7 @@ DECLARE_SCRIPTING_TYPE_NO_SPAWN(Screen);
API_PROPERTY() static void SetIsFullscreen(bool value);
/// <summary>
/// Gets the window size.
/// Gets the window size (in screen-space, includes DPI scale).
/// </summary>
/// <returns>The value</returns>
API_PROPERTY() static Float2 GetSize();
@@ -50,7 +50,7 @@ DECLARE_SCRIPTING_TYPE_NO_SPAWN(Screen);
API_FUNCTION() static Float2 GameViewportToScreen(const Float2& viewportPos);
/// <summary>
/// Sets the window size.
/// Sets the window size (in screen-space, includes DPI scale).
/// </summary>
/// <remarks>
/// Resizing may not happen immediately. It will be performed before next frame rendering.

View File

@@ -246,11 +246,16 @@ Ray Camera::ConvertMouseToRay(const Float2& mousePosition, const Viewport& viewp
Viewport Camera::GetViewport() const
{
Viewport result = Viewport(Float2::Zero);
float dpiScale = Platform::GetDpiScale();
#if USE_EDITOR
// Editor
if (Editor::Managed)
{
result.Size = Editor::Managed->GetGameWindowSize();
if (auto* window = Editor::Managed->GetGameWindow())
dpiScale = window->GetDpiScale();
}
#else
// Game
auto mainWin = Engine::MainWindow;
@@ -258,9 +263,13 @@ Viewport Camera::GetViewport() const
{
const auto size = mainWin->GetClientSize();
result.Size = size;
dpiScale = mainWin->GetDpiScale();
}
#endif
// Remove DPI scale (game viewport coords are unscaled)
result.Size /= dpiScale;
// Fallback to the default value
if (result.Size.MinValue() <= ZeroTolerance)
result.Size = Float2(1280, 720);