Skip to content

Commit

Permalink
feat: proper DPI handling (#939)
Browse files Browse the repository at this point in the history
* feat: proper DPI handling

* Logical conclusion of event handler
  • Loading branch information
ividyon authored Jul 9, 2024
1 parent dba491f commit b11efe4
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/StudioCore/CFG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class CFG
public bool EnableCheckProgramUpdate = true;
public bool ShowUITooltips = true;
public float UIScale = 1.0f;
public bool UIScaleByDPI = true;
public bool EnableSoapstone = true;
public bool EnableTexturing = false;

Expand Down Expand Up @@ -280,6 +281,7 @@ private static void LoadConfig()
SaveConfig();
}
}
MapStudioNew.UIScaleChanged?.Invoke(null, EventArgs.Empty);
}

private static void LoadKeybinds()
Expand Down
63 changes: 53 additions & 10 deletions src/StudioCore/MapStudioNew.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using static Andre.Native.ImGuiBindings;
using Microsoft.Extensions.Logging;
using Octokit;
using Silk.NET.SDL;
using SoapstoneLib;
using SoulsFormats;
using StudioCore.Banks;
Expand All @@ -12,7 +13,6 @@
using StudioCore.ParamEditor;
using StudioCore.Platform;
using StudioCore.Resource;
using StudioCore.Scene;
using StudioCore.Tests;
using StudioCore.TextEditor;
using System;
Expand All @@ -23,10 +23,12 @@
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Threading;
using Veldrid;
using Veldrid.Sdl2;
using StudioCore.Interface;
using Renderer = StudioCore.Scene.Renderer;
using Thread = System.Threading.Thread;
using Version = System.Version;

namespace StudioCore;

Expand All @@ -41,14 +43,14 @@ public class MapStudioNew

public static bool LowRequirementsMode;

private readonly IGraphicsContext _context;
private static IGraphicsContext _context;

private readonly List<EditorScreen> _editors;
private readonly HelpWindow HelpWindow;

private readonly NewProjectOptions _newProjectOptions = new();
private readonly string _programTitle;
private readonly SettingsMenu _settingsMenu = new();
private readonly SettingsMenu _settingsMenu;

private readonly SoapstoneService _soapstoneService;
private readonly string _version;
Expand All @@ -72,11 +74,19 @@ public class MapStudioNew

private bool _standardProjectUIOpened = true;

public static EventHandler UIScaleChanged;
public static bool FontRebuildRequest;

public unsafe MapStudioNew(IGraphicsContext context, string version)
{
_version = version;
_programTitle = $"Dark Souls Map Studio version {_version}";

UIScaleChanged += (_, _) =>
{
FontRebuildRequest = true;
};

// Hack to make sure dialogs work before the main window is created
PlatformUtils.InitializeWindows(null);
CFG.AttemptLoadOrDefault();
Expand Down Expand Up @@ -104,6 +114,7 @@ public unsafe MapStudioNew(IGraphicsContext context, string version)

_soapstoneService = new SoapstoneService(_version, msbEditor);

_settingsMenu = new SettingsMenu();
_settingsMenu.MsbEditor = msbEditor;
_settingsMenu.ModelEditor = modelEditor;
_settingsMenu.ParamEditor = paramEditor;
Expand All @@ -112,7 +123,6 @@ public unsafe MapStudioNew(IGraphicsContext context, string version)
HelpWindow = new HelpWindow();

ImGui.GetIO()->ConfigFlags |= ImGuiConfigFlags.NavEnableKeyboard;
SetupFonts();
_context.ImguiRenderer.OnSetupDone();

ImGuiStyle* style = ImGui.GetStyle();
Expand Down Expand Up @@ -180,7 +190,7 @@ private unsafe void SetupFonts()
cfg->GlyphMinAdvanceX = 5.0f;
cfg->OversampleH = 5;
cfg->OversampleV = 5;
ImFontAtlasAddFontFromMemoryTTF(fonts, fontEnNative.ToPointer(), fontEn.Length, 14.0f * scale, cfg,
ImFontAtlasAddFontFromMemoryTTF(fonts, fontEnNative.ToPointer(), fontEn.Length, (float)Math.Round(14.0f * scale), cfg,
ImFontAtlasGetGlyphRangesDefault(fonts));
}

Expand Down Expand Up @@ -729,12 +739,13 @@ private unsafe void Update(float deltaseconds)
{
Tracy.___tracy_c_zone_context ctx = Tracy.TracyCZoneN(1, "Imgui");

UpdateDpi();
var scale = GetUIScale();

if (_settingsMenu.FontRebuildRequest)
if (FontRebuildRequest)
{
_context.ImguiRenderer.Update(deltaseconds, InputTracker.FrameSnapshot, SetupFonts);
_settingsMenu.FontRebuildRequest = false;
FontRebuildRequest = false;
}
else
{
Expand Down Expand Up @@ -1370,9 +1381,41 @@ private unsafe void Update(float deltaseconds)
_firstframe = false;
}

private const float DefaultDpi = 96f;
private static float _dpi = DefaultDpi;

public static float Dpi
{
get => _dpi;
set
{
if (Math.Abs(_dpi - value) < 0.0001f) return; // Skip doing anything if no difference

_dpi = value;
if (Math.Abs(value - _dpi) > 0.9f && CFG.Current.UIScaleByDPI)
UIScaleChanged?.Invoke(null, EventArgs.Empty);
}
}

private static unsafe void UpdateDpi()
{
if (SdlProvider.SDL.IsValueCreated && _context?.Window != null)
{
var window = _context.Window.SdlWindowHandle;
int index = SdlProvider.SDL.Value.GetWindowDisplayIndex(window);
float ddpi = 96f;
float _ = 0f;
SdlProvider.SDL.Value.GetDisplayDPI(index, ref ddpi, ref _, ref _);

Dpi = ddpi;
}
}

public static float GetUIScale()
{
// TODO: Multiply by monitor DPI when available.
return CFG.Current.UIScale;
var scale = CFG.Current.UIScale;
if (CFG.Current.UIScaleByDPI)
scale = scale / DefaultDpi * Dpi;
return scale;
}
}
40 changes: 29 additions & 11 deletions src/StudioCore/SettingsMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@
using System.Reflection;
using Veldrid;
using StudioCore.Interface;
using System.Globalization;

namespace StudioCore;

public class SettingsMenu
{
private KeyBind _currentKeyBind;
public bool FontRebuildRequest;
public bool MenuOpenState;
public ModelEditorScreen ModelEditor;
public MsbEditorScreen MsbEditor;
public ParamEditorScreen ParamEditor;
public ProjectSettings? ProjSettings = null;
public TextEditorScreen TextEditor;
private float _tempUiScale;

public SettingsMenu()
{
_tempUiScale = CFG.Current.UIScale;
}

public void SaveSettings()
{
Expand Down Expand Up @@ -58,20 +64,32 @@ private void DisplaySettings_System()
ShowHelpMarker("Adjusts the scale of the user interface throughout all of DSMS.");
ImGui.SameLine();
}
ImGui.SliderFloat("UI scale", ref CFG.Current.UIScale, 0.5f, 4.0f);

ImGui.SliderFloat("UI scale", ref _tempUiScale, 0.5f, 4.0f);
if (ImGui.IsItemDeactivatedAfterEdit())
{
// Round to 0.05
CFG.Current.UIScale = (float)Math.Round(CFG.Current.UIScale * 20) / 20;
FontRebuildRequest = true;
CFG.Current.UIScale = (float)Math.Round(_tempUiScale * 20) / 20;
MapStudioNew.UIScaleChanged?.Invoke(null, EventArgs.Empty);
_tempUiScale = CFG.Current.UIScale;
}

ImGui.SameLine();
if (ImGui.Button("Reset"))
{
CFG.Current.UIScale = CFG.Default.UIScale;
FontRebuildRequest = true;
_tempUiScale = CFG.Current.UIScale;
MapStudioNew.UIScaleChanged?.Invoke(null, EventArgs.Empty);
}

if (CFG.Current.ShowUITooltips)
{
ShowHelpMarker("Multiplies the user interface scale by your monitor's DPI setting.");
ImGui.SameLine();
}
ImGui.Checkbox($"Multiply UI scale by DPI ({(MapStudioNew.Dpi / 96).ToString("P0", new NumberFormatInfo { PercentPositivePattern = 1, PercentNegativePattern = 1 })})", ref CFG.Current.UIScaleByDPI);
if (ImGui.IsItemDeactivatedAfterEdit())
{
MapStudioNew.UIScaleChanged?.Invoke(null, EventArgs.Empty);
}
}

Expand All @@ -95,7 +113,7 @@ private void DisplaySettings_System()
}
if (ImGui.Checkbox("Chinese", ref CFG.Current.FontChinese))
{
FontRebuildRequest = true;
MapStudioNew.FontRebuildRequest = true;
}

if (CFG.Current.ShowUITooltips)
Expand All @@ -105,7 +123,7 @@ private void DisplaySettings_System()
}
if (ImGui.Checkbox("Korean", ref CFG.Current.FontKorean))
{
FontRebuildRequest = true;
MapStudioNew.FontRebuildRequest = true;
}

if (CFG.Current.ShowUITooltips)
Expand All @@ -115,7 +133,7 @@ private void DisplaySettings_System()
}
if (ImGui.Checkbox("Thai", ref CFG.Current.FontThai))
{
FontRebuildRequest = true;
MapStudioNew.FontRebuildRequest = true;
}

if (CFG.Current.ShowUITooltips)
Expand All @@ -125,7 +143,7 @@ private void DisplaySettings_System()
}
if (ImGui.Checkbox("Vietnamese", ref CFG.Current.FontVietnamese))
{
FontRebuildRequest = true;
MapStudioNew.FontRebuildRequest = true;
}

if (CFG.Current.ShowUITooltips)
Expand All @@ -135,7 +153,7 @@ private void DisplaySettings_System()
}
if (ImGui.Checkbox("Cyrillic", ref CFG.Current.FontCyrillic))
{
FontRebuildRequest = true;
MapStudioNew.FontRebuildRequest = true;
}
}

Expand Down

0 comments on commit b11efe4

Please sign in to comment.