From 7e79cbb34ced2eac5e520a1eaa8d840bb9617271 Mon Sep 17 00:00:00 2001 From: EqUiNoX Date: Wed, 6 Sep 2023 19:12:23 -0700 Subject: [PATCH] Fixed: hidpi modes --- Pandora.nfo | 2 +- Pandora/Program.cs | 6 ++-- Pandora/UI/ApplicationUI.cs | 52 +++++++++++++++++++---------------- Pandora/UI/ImGuiController.cs | 29 +++++++++++++------ Pandora/Window.cs | 16 ++++------- README.md | 2 +- 6 files changed, 60 insertions(+), 47 deletions(-) diff --git a/Pandora.nfo b/Pandora.nfo index 5c2d287..533ced6 100644 --- a/Pandora.nfo +++ b/Pandora.nfo @@ -13,7 +13,7 @@ Team Resurgent - [ Program .......................................... Pandora V1.0.0 ] + [ Program .......................................... Pandora V1.0.7 ] [ Type .................................................... Utiliyy ] [ Platform .................................... Windows, Linux, OSX ] [ OS Architecture ............................................. X64 ] diff --git a/Pandora/Program.cs b/Pandora/Program.cs index f38040a..632d8e8 100644 --- a/Pandora/Program.cs +++ b/Pandora/Program.cs @@ -2,9 +2,9 @@ try { - var version = "V1.0.6"; - var application = new ApplicationUI(); - application.Start(version); + var version = "V1.0.7"; + var application = new ApplicationUI(version); + application.Run(); } catch (Exception ex) { diff --git a/Pandora/UI/ApplicationUI.cs b/Pandora/UI/ApplicationUI.cs index 1a40db4..faf76f8 100644 --- a/Pandora/UI/ApplicationUI.cs +++ b/Pandora/UI/ApplicationUI.cs @@ -7,6 +7,7 @@ using System.Numerics; using System.Runtime.InteropServices; using OpenTK.Windowing.GraphicsLibraryFramework; +using System; namespace Pandora { @@ -24,8 +25,7 @@ public LogDetail(string logType, string message) } } - private Window? m_window; - private ImGuiController? m_controller; + private Window m_window; private RemoteContextDialog m_remoteContextDialog = new(); private FileContextDialog m_fileContextDialog = new(); private InputDialog m_inputDialog = new(); @@ -40,6 +40,7 @@ public LogDetail(string logType, string message) private bool m_showSplash = true; private int m_dialupHandle; private int m_disconnectHandle; + private string m_version; public string LocalSelectedFolder { get; set; } = Utility.GetApplicationPath() ?? string.Empty; @@ -48,12 +49,23 @@ public LogDetail(string logType, string message) [DllImport("dwmapi.dll", PreserveSig = true)] public static extern int DwmSetWindowAttribute(IntPtr hwnd, uint attr, ref int attrValue, int attrSize); - public void Start(string version) + public ApplicationUI(string version) + { + m_window = new Window(); + m_version = version; + } + + private Vector2 GetScaledWindowSize() + { + return new Vector2(m_window.Size.X, m_window.Size.Y) / m_window.Controller.GetScaleFactor(); + } + + public void Run() { var admin = Utility.IsAdmin() ? " ADMIN" : string.Empty; m_window = new Window(); - m_window.Title = $"Pandora - {version}{admin}"; + m_window.Title = $"Pandora - {m_version}{admin}"; m_window.Size = new OpenTK.Mathematics.Vector2i(1280, 720); m_window.VSync = OpenTK.Windowing.Common.VSyncMode.On; @@ -65,8 +77,6 @@ public void Start(string version) var iconImage = new OpenTK.Windowing.Common.Input.Image(resourceImage.Width, resourceImage.Height, byteSpan.ToArray()); m_window.Icon = new OpenTK.Windowing.Common.Input.WindowIcon(iconImage); - m_controller = new ImGuiController(m_window.Width, m_window.Height); - if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 22000, 0)) { int value = -1; @@ -169,12 +179,6 @@ private void LocalProcessChildFiles(string path, int width) private void RenderUI() { - if (m_window == null || - m_controller == null) - { - return; - } - if (m_remoteContextDialog.Render() == RemoteContextDialog.RemoteContextAction.Download) { var remotePath = m_remoteContextDialog.RemotePath; @@ -237,17 +241,19 @@ private void RenderUI() if (m_showSplash) { m_showSplash = false; - m_splashDialog.ShowdDialog(m_controller.SplashTexture); + m_splashDialog.ShowdDialog(m_window.Controller.SplashTexture); } ImGui.Begin("Main", ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoResize); - ImGui.SetWindowSize(new Vector2(m_window.Width, m_window.Height)); + ImGui.SetWindowSize(GetScaledWindowSize()); ImGui.SetWindowPos(new Vector2(0, 0), ImGuiCond.Always); ImGui.Text("Log:"); + var windowSize = ImGui.GetWindowSize(); + ImGuiTableFlags flags = ImGuiTableFlags.Resizable | ImGuiTableFlags.ScrollX | ImGuiTableFlags.ScrollY | ImGuiTableFlags.BordersOuter | ImGuiTableFlags.RowBg; - if (ImGui.BeginTable("tableLog", 2, flags, new Vector2(m_window.Width - 16, 170), 0.0f)) + if (ImGui.BeginTable("tableLog", 2, flags, new Vector2(windowSize.X - 16, 170), 0.0f)) { ImGui.TableSetupColumn("Log Type", ImGuiTableColumnFlags.WidthFixed, 75.0f, 0); ImGui.TableSetupColumn("Message", ImGuiTableColumnFlags.WidthStretch, 300.0f, 1); @@ -290,7 +296,7 @@ private void RenderUI() ImGui.Spacing(); - var halfWidth = m_window.Width / 2; + var halfWidth = (int)(windowSize.X / 2); ImGui.Text("Local:"); ImGui.SameLine(); @@ -311,7 +317,7 @@ private void RenderUI() var lineHeight = ImGui.GetTextLineHeight() + 4; - if (ImGui.BeginChildFrame(1, new Vector2(200, m_window.Height - (358 + 88)), ImGuiWindowFlags.None)) + if (ImGui.BeginChildFrame(1, new Vector2(200, windowSize.Y - (358 + 88)), ImGuiWindowFlags.None)) { var specialFolders = Utility.GetSpecialFolders(); foreach (var specialFolder in specialFolders) @@ -328,7 +334,7 @@ private void RenderUI() ImGui.SameLine(); - if (ImGui.BeginChildFrame(2, new Vector2(halfWidth - 222, m_window.Height - (358 + 88)), ImGuiWindowFlags.None)) + if (ImGui.BeginChildFrame(2, new Vector2(halfWidth - 222, windowSize.Y - (358 + 88)), ImGuiWindowFlags.None)) { var directoryInfo = new DirectoryInfo(LocalSelectedFolder); if (directoryInfo.Parent != null) @@ -359,7 +365,7 @@ private void RenderUI() { ImGui.BeginDisabled(); } - if (ImGui.BeginChildFrame(3, new Vector2(halfWidth - 14, m_window.Height - (358 + 88)), ImGuiWindowFlags.None)) + if (ImGui.BeginChildFrame(3, new Vector2(halfWidth - 14, windowSize.Y - (358 + 88)), ImGuiWindowFlags.None)) { if (m_client != null) { @@ -451,7 +457,7 @@ private void RenderUI() ImGui.Text("Downloads:"); - if (ImGui.BeginTable("tableDownloads", 4, flags, new Vector2(m_window.Width - 16, 100), 0.0f)) + if (ImGui.BeginTable("tableDownloads", 4, flags, new Vector2(windowSize.X - 16, 100), 0.0f)) { ImGui.TableSetupColumn("Progress", ImGuiTableColumnFlags.WidthFixed, 75.0f, 0); ImGui.TableSetupColumn("Remote Path", ImGuiTableColumnFlags.WidthFixed, 300.0f, 1); @@ -507,7 +513,7 @@ private void RenderUI() ImGui.EndTable(); } - ImGui.SetCursorPosY(m_window.Height - 40); + ImGui.SetCursorPosY(windowSize.Y - 40); if (m_client != null) { @@ -567,7 +573,7 @@ private void RenderUI() ImGui.SameLine(); - ImGui.SetCursorPosX(m_window.Width - 133); + ImGui.SetCursorPosX(windowSize.X - 133); if (ImGui.Button("Visit Patreon", new Vector2(125, 30))) { @@ -593,7 +599,7 @@ private void RenderUI() } } - ImGui.SetCursorPos(new Vector2(m_window.Width - 273, m_window.Height - 32)); + ImGui.SetCursorPos(new Vector2(windowSize.X - 273, windowSize.Y - 32)); ImGui.Text("Coded by EqUiNoX"); ImGui.End(); diff --git a/Pandora/UI/ImGuiController.cs b/Pandora/UI/ImGuiController.cs index 0e99568..637a76d 100644 --- a/Pandora/UI/ImGuiController.cs +++ b/Pandora/UI/ImGuiController.cs @@ -35,7 +35,13 @@ public class ImGuiController : IDisposable private int _windowWidth; private int _windowHeight; - private Vector2 _scaleFactor = Vector2.One; + private Vector2 _hdpiScale = Vector2.One; + private Vector2 _retinaScale = Vector2.One; + + public Vector2 GetScaleFactor() + { + return _hdpiScale / _retinaScale; + } public int SplashTexture => _splashTexture; @@ -180,10 +186,18 @@ private static void SetXboxTheme() style.PopupRounding = 6; } - public unsafe ImGuiController(int width, int height) + public unsafe ImGuiController(Window window) { - _windowWidth = width; - _windowHeight = height; + _windowWidth = window.ClientSize.X; + _windowHeight = window.ClientSize.Y; + + _hdpiScale = Vector2.One; + if (window.TryGetCurrentMonitorScale(out var scaleX, out var scaleY)) + { + _hdpiScale = new Vector2(scaleX, scaleY); + } + + _retinaScale = new Vector2(window.Width / (float)window.Size.X, window.Height / (float)window.Size.Y); int major = GL.GetInteger(GetPName.MajorVersion); int minor = GL.GetInteger(GetPName.MinorVersion); @@ -326,8 +340,7 @@ public void Update(float deltaSeconds, GameWindow wnd) private void SetPerFrameImGuiData(float deltaSeconds) { ImGuiIOPtr io = ImGui.GetIO(); - io.DisplaySize = new Vector2(_windowWidth / _scaleFactor.X, _windowHeight / _scaleFactor.Y); - io.DisplayFramebufferScale = _scaleFactor; + io.DisplaySize = new Vector2(_windowWidth, _windowHeight) / _hdpiScale; io.DeltaTime = deltaSeconds; } @@ -357,7 +370,7 @@ private void UpdateImGuiInput(GameWindow wnd) io.MouseDown[1] = MouseState[MouseButton.Right]; io.MouseDown[2] = MouseState[MouseButton.Middle]; - io.MousePos = new Vector2((int)MouseState.X, (int)MouseState.Y); + io.MousePos = new Vector2((int)MouseState.X, (int)MouseState.Y) / GetScaleFactor(); foreach (Keys key in Enum.GetValues(typeof(Keys))) { @@ -479,7 +492,7 @@ private void RenderImDrawData(ImDrawDataPtr draw_data) GL.BindVertexArray(_vertexArray); CheckGLError("VAO"); - draw_data.ScaleClipRects(io.DisplayFramebufferScale); + draw_data.ScaleClipRects(_hdpiScale); GL.Enable(EnableCap.Blend); GL.Enable(EnableCap.ScissorTest); diff --git a/Pandora/Window.cs b/Pandora/Window.cs index 71bd573..8daf7d2 100644 --- a/Pandora/Window.cs +++ b/Pandora/Window.cs @@ -2,13 +2,16 @@ using OpenTK.Mathematics; using OpenTK.Windowing.Common; using OpenTK.Windowing.Desktop; +using Pandora.UI; namespace Pandora { public class Window : GameWindow { public Window() : base(GameWindowSettings.Default, new NativeWindowSettings() { Size = new Vector2i(1600, 900), APIVersion = new Version(3, 3) }) - { } + { + Controller = new ImGuiController(this); + } public int Width => ClientSize.X; @@ -16,16 +19,7 @@ public class Window : GameWindow public Action? RenderUI { get; set; } - public ImGuiController? Controller { get; set; } - - protected override void OnLoad() - { - base.OnLoad(); - - Title += " - OpenGL Version: " + GL.GetString(StringName.Version); - - Controller = new ImGuiController(ClientSize.X, ClientSize.Y); - } + public ImGuiController Controller { get; set; } protected override void OnResize(ResizeEventArgs e) { diff --git a/README.md b/README.md index 82d9b53..362fc4a 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Get ready to open that box up nice and wide :) - [ Program .......................................... Pandora V1.0.4 ] + [ Program .......................................... Pandora V1.0.7 ] [ Type .............................................. Xbins Manager ] [ Patreon ....................https://www.patreon.com/teamresurgent ]