From b5125d4b3f7cdc0c7514a01764e5b5d4d85f80a7 Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Sat, 11 May 2024 00:26:44 +0200 Subject: [PATCH] Add code to save dalamud's window options (pinned/clickthrough/alpha) --- ImGui/IPersistableWindowConfig.cs | 8 +++ ImGui/LWindow.cs | 106 +++++++++++++++++++++++++++++- ImGui/WindowConfig.cs | 8 +++ 3 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 ImGui/IPersistableWindowConfig.cs create mode 100644 ImGui/WindowConfig.cs diff --git a/ImGui/IPersistableWindowConfig.cs b/ImGui/IPersistableWindowConfig.cs new file mode 100644 index 0000000..9e19cc6 --- /dev/null +++ b/ImGui/IPersistableWindowConfig.cs @@ -0,0 +1,8 @@ +namespace LLib.ImGui; + +public interface IPersistableWindowConfig +{ + WindowConfig? WindowConfig { get; } + + void SaveWindowConfig(); +} diff --git a/ImGui/LWindow.cs b/ImGui/LWindow.cs index 6e927cf..50a5db0 100644 --- a/ImGui/LWindow.cs +++ b/ImGui/LWindow.cs @@ -1,5 +1,7 @@ using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Numerics; +using System.Runtime.CompilerServices; using Dalamud.Interface; using Dalamud.Interface.Windowing; using ImGuiNET; @@ -7,10 +9,10 @@ using IG = ImGuiNET.ImGui; namespace LLib.ImGui; +[SuppressMessage("ReSharper", "SuspiciousTypeConversion.Global")] public abstract class LWindow : Window { - protected bool ClickedHeaderLastFrame { get; private set; } - protected bool ClickedHeaderCurrentFrame { get; private set; } + private bool _initializedConfig; protected LWindow(string name, ImGuiWindowFlags flags = ImGuiWindowFlags.None, bool forceMainWindow = false) : base(name, flags, forceMainWindow) @@ -51,11 +53,111 @@ public abstract class LWindow : Window }); } + + protected bool ClickedHeaderLastFrame { get; private set; } + protected bool ClickedHeaderCurrentFrame { get; private set; } + + protected bool IsPinned + { + get => InternalIsPinned(this); + set => InternalIsPinned(this) = value; + } + + protected bool IsClickthrough + { + get => InternalIsClickthrough(this); + set => InternalIsClickthrough(this) = value; + } + + protected int? Alpha + { + get + { + float? value = InternalAlpha(this); + return (int?)(10_0000 * value); + } + set => InternalAlpha(this) = value / 10_0000f; + } + + private void LoadWindowConfig() + { + if (this is IPersistableWindowConfig pwc) + { + WindowConfig? config = pwc.WindowConfig; + if (config != null) + { + if (AllowPinning) + IsPinned = config.IsPinned; + + if (AllowClickthrough) + IsClickthrough = config.IsClickthrough; + + Alpha = config.Alpha; + } + + _initializedConfig = true; + } + } + + private void UpdateWindowConfig() + { + if (this is IPersistableWindowConfig pwc && !IG.IsAnyMouseDown()) + { + WindowConfig? config = pwc.WindowConfig; + if (config != null) + { + bool changed = false; + if (AllowPinning && config.IsPinned != IsPinned) + { + config.IsPinned = IsPinned; + changed = true; + } + + if (AllowClickthrough && config.IsClickthrough != IsClickthrough) + { + config.IsClickthrough = IsClickthrough; + changed = true; + } + + if (config.Alpha != Alpha) + { + config.Alpha = Alpha; + changed = true; + } + + if (changed) + { + pwc.SaveWindowConfig(); + } + } + } + } + public override void PreDraw() { + if (!_initializedConfig) + LoadWindowConfig(); + base.PreDraw(); ClickedHeaderLastFrame = ClickedHeaderCurrentFrame; ClickedHeaderCurrentFrame = false; } + + public override void PostDraw() + { + base.PostDraw(); + + if (_initializedConfig) + UpdateWindowConfig(); + } + + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "internalIsPinned")] + private static extern ref bool InternalIsPinned(Window @this); + + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "internalIsClickthrough")] + private static extern ref bool InternalIsClickthrough(Window @this); + + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "internalAlpha")] + private static extern ref float? InternalAlpha(Window @this); } diff --git a/ImGui/WindowConfig.cs b/ImGui/WindowConfig.cs new file mode 100644 index 0000000..63e6a62 --- /dev/null +++ b/ImGui/WindowConfig.cs @@ -0,0 +1,8 @@ +namespace LLib.ImGui; + +public sealed class WindowConfig +{ + public bool IsPinned { get; set; } + public bool IsClickthrough { get; set; } + public int? Alpha { get; set; } +}