From 691df4cb8075c26ebbab33b192310a5090c68499 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 17 Mar 2021 00:04:16 +0100 Subject: [PATCH] Fix Dpi issues when RootWindow is null Fix #344 --- Source/Editor/GUI/Docking/DockPanel.cs | 2 +- Source/Editor/Surface/ContextMenu/ContentFinder.cs | 2 +- Source/Engine/UI/GUI/Common/RichTextBoxBase.cs | 12 ++++++------ Source/Engine/UI/GUI/Common/TextBox.cs | 4 ++-- Source/Engine/UI/GUI/Control.cs | 5 +++++ Source/Engine/UI/GUI/RenderOutputControl.cs | 2 +- 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/Source/Editor/GUI/Docking/DockPanel.cs b/Source/Editor/GUI/Docking/DockPanel.cs index 74f2d5ab0..f968c2501 100644 --- a/Source/Editor/GUI/Docking/DockPanel.cs +++ b/Source/Editor/GUI/Docking/DockPanel.cs @@ -124,7 +124,7 @@ namespace FlaxEditor.GUI.Docking throw new InvalidOperationException("Missing parent window."); var control = _tabsProxy != null ? (Control)_tabsProxy : this; var clientPos = control.PointToWindow(Vector2.Zero); - return new Rectangle(parentWin.PointToScreen(clientPos), control.Size * RootWindow.DpiScale); + return new Rectangle(parentWin.PointToScreen(clientPos), control.Size * DpiScale); } } diff --git a/Source/Editor/Surface/ContextMenu/ContentFinder.cs b/Source/Editor/Surface/ContextMenu/ContentFinder.cs index 6089c2ca7..fc0f43472 100644 --- a/Source/Editor/Surface/ContextMenu/ContentFinder.cs +++ b/Source/Editor/Surface/ContextMenu/ContentFinder.cs @@ -108,7 +108,7 @@ namespace FlaxEditor.Surface.ContextMenu { _resultPanel.DisposeChildren(); - var dpiScale = RootWindow.DpiScale; + var dpiScale = DpiScale; if (items.Count == 0) { diff --git a/Source/Engine/UI/GUI/Common/RichTextBoxBase.cs b/Source/Engine/UI/GUI/Common/RichTextBoxBase.cs index 4844cc880..0ef44594b 100644 --- a/Source/Engine/UI/GUI/Common/RichTextBoxBase.cs +++ b/Source/Engine/UI/GUI/Common/RichTextBoxBase.cs @@ -124,7 +124,7 @@ namespace FlaxEngine.GUI var font = textBlock.Style.Font.GetFont(); if (font) { - height = font.Height / RootWindow.DpiScale; + height = font.Height / DpiScale; return textBlock.Bounds.UpperLeft; } } @@ -136,7 +136,7 @@ namespace FlaxEngine.GUI var font = textBlock.Style.Font.GetFont(); if (font) { - height = font.Height / RootWindow.DpiScale; + height = font.Height / DpiScale; return textBlock.Bounds.UpperRight; } } @@ -151,7 +151,7 @@ namespace FlaxEngine.GUI var font = textBlock.Style.Font.GetFont(); if (!font) break; - height = font.Height / RootWindow.DpiScale; + height = font.Height / DpiScale; return textBlock.Bounds.Location + font.GetCharPosition(_text, ref textBlock.Range, index - textBlock.Range.StartIndex); } } @@ -166,7 +166,7 @@ namespace FlaxEngine.GUI var font = textBlock.Style.Font.GetFont(); if (!font) break; - height = font.Height / RootWindow.DpiScale; + height = font.Height / DpiScale; return textBlock.Bounds.UpperRight; } } @@ -280,7 +280,7 @@ namespace FlaxEngine.GUI { Vector2 leftEdge = selection.StartIndex <= textBlock.Range.StartIndex ? textBlock.Bounds.UpperLeft : font.GetCharPosition(_text, selection.StartIndex); Vector2 rightEdge = selection.EndIndex >= textBlock.Range.EndIndex ? textBlock.Bounds.UpperRight : font.GetCharPosition(_text, selection.EndIndex); - float height = font.Height / RootWindow.DpiScale; + float height = font.Height / DpiScale; float alpha = Mathf.Min(1.0f, Mathf.Cos(_animateTime * BackgroundSelectedFlashSpeed) * 0.5f + 1.3f); alpha *= alpha; Color selectionColor = Color.White * alpha; @@ -330,7 +330,7 @@ namespace FlaxEngine.GUI if (textBlock.Style.UnderlineBrush != null) { var underLineHeight = 2.0f; - var height = font.Height / RootWindow.DpiScale; + var height = font.Height / DpiScale; var underlineRect = new Rectangle(textBlock.Bounds.Location.X, textBlock.Bounds.Location.Y + height - underLineHeight * 0.5f, textBlock.Bounds.Width, underLineHeight); textBlock.Style.UnderlineBrush.Draw(underlineRect, textBlock.Style.Color); } diff --git a/Source/Engine/UI/GUI/Common/TextBox.cs b/Source/Engine/UI/GUI/Common/TextBox.cs index f454159ec..0a6f11660 100644 --- a/Source/Engine/UI/GUI/Common/TextBox.cs +++ b/Source/Engine/UI/GUI/Common/TextBox.cs @@ -107,7 +107,7 @@ namespace FlaxEngine.GUI return Vector2.Zero; } - height = font.Height / RootWindow.DpiScale; + height = font.Height / DpiScale; return font.GetCharPosition(_text, index, ref _layout); } @@ -159,7 +159,7 @@ namespace FlaxEngine.GUI { Vector2 leftEdge = font.GetCharPosition(_text, SelectionLeft, ref _layout); Vector2 rightEdge = font.GetCharPosition(_text, SelectionRight, ref _layout); - float fontHeight = font.Height / RootWindow.DpiScale; + float fontHeight = font.Height / DpiScale; // Draw selection background float alpha = Mathf.Min(1.0f, Mathf.Cos(_animateTime * BackgroundSelectedFlashSpeed) * 0.5f + 1.3f); diff --git a/Source/Engine/UI/GUI/Control.cs b/Source/Engine/UI/GUI/Control.cs index 3062c3088..fb6424b58 100644 --- a/Source/Engine/UI/GUI/Control.cs +++ b/Source/Engine/UI/GUI/Control.cs @@ -312,6 +312,11 @@ namespace FlaxEngine.GUI /// public virtual WindowRootControl RootWindow => _root?.RootWindow; + /// + /// Gets the control DPI scale factor (1 is default). Includes custom DPI scale. + /// + public float DpiScale => _root?.RootWindow?.DpiScale ?? Platform.DpiScale; + /// /// Gets screen position of the control (upper left corner). /// diff --git a/Source/Engine/UI/GUI/RenderOutputControl.cs b/Source/Engine/UI/GUI/RenderOutputControl.cs index c12b1844e..9a7f6dfb1 100644 --- a/Source/Engine/UI/GUI/RenderOutputControl.cs +++ b/Source/Engine/UI/GUI/RenderOutputControl.cs @@ -224,7 +224,7 @@ namespace FlaxEngine.GUI /// public void SyncBackbufferSize() { - float scale = ResolutionScale * (RootWindow?.DpiScale ?? Platform.DpiScale); + float scale = ResolutionScale * DpiScale; int width = Mathf.CeilToInt(Width * scale); int height = Mathf.CeilToInt(Height * scale); if (_customResolution.HasValue)