From 7e2cb169b928c995100272718b38afc024ed900f Mon Sep 17 00:00:00 2001 From: Anna Date: Fri, 19 Mar 2021 14:40:07 -0400 Subject: [PATCH] feat: allow selective window fields --- HUD Manager/Configuration/Window.cs | 31 +++++++++++++- HUD Manager/Hud.cs | 38 +++++++++++++++-- HUD Manager/Statuses.cs | 6 +-- HUD Manager/Ui/Editor/Tabs/Windows.cs | 59 +++++++++++++++++++++++---- 4 files changed, 117 insertions(+), 17 deletions(-) diff --git a/HUD Manager/Configuration/Window.cs b/HUD Manager/Configuration/Window.cs index ff5ed72..e3d2fb6 100755 --- a/HUD Manager/Configuration/Window.cs +++ b/HUD Manager/Configuration/Window.cs @@ -4,10 +4,23 @@ using Newtonsoft.Json; namespace HUD_Manager.Configuration { [Serializable] public class Window { + public const WindowComponent AllEnabled = WindowComponent.X | WindowComponent.Y; + public WindowComponent Enabled { get; set; } = WindowComponent.X | WindowComponent.Y; public Vector2 Position { get; set; } + public bool this[WindowComponent component] { + get => (this.Enabled & component) > 0; + set { + if (value) { + this.Enabled |= component; + } else { + this.Enabled &= ~component; + } + } + } + [JsonConstructor] public Window(WindowComponent enabled, Vector2 position) { this.Enabled = enabled; @@ -17,11 +30,25 @@ namespace HUD_Manager.Configuration { public Window(Vector2 position) { this.Position = position; } + + public Window Clone() { + return new Window(this.Enabled, new Vector2(this.Position.X, this.Position.Y)); + } + + public void UpdateEnabled(Window other) { + if (other[WindowComponent.X]) { + this.Position.X = other.Position.X; + } + + if (other[WindowComponent.Y]) { + this.Position.Y = other.Position.Y; + } + } } [Flags] public enum WindowComponent { - X, - Y, + X = 1 << 0, + Y = 1 << 1, } } diff --git a/HUD Manager/Hud.cs b/HUD Manager/Hud.cs index 1bc35e2..7649bf5 100644 --- a/HUD Manager/Hud.cs +++ b/HUD Manager/Hud.cs @@ -161,15 +161,16 @@ namespace HUD_Manager { } } - public void WriteEffectiveLayout(HudSlot slot, Guid id) { + private SavedLayout? GetEffectiveLayout(Guid id) { // find the node for this id var nodes = Node.BuildTree(this.Plugin.Config.Layouts); var node = nodes.Find(id); if (node == null) { - return; + return null; } var elements = new Dictionary(); + var windows = new Dictionary(); // get the ancestors and their elements for this node foreach (var ancestor in node.Ancestors().Reverse()) { @@ -181,6 +182,15 @@ namespace HUD_Manager { elements[element.Key].UpdateEnabled(element.Value); } + + foreach (var window in ancestor.Value.Windows) { + if (window.Value.Enabled == Window.AllEnabled || !windows.ContainsKey(window.Key)) { + windows[window.Key] = window.Value.Clone(); + continue; + } + + windows[window.Key].UpdateEnabled(window.Value); + } } // apply this node's elements @@ -193,7 +203,29 @@ namespace HUD_Manager { elements[element.Key].UpdateEnabled(element.Value); } - this.WriteLayout(slot, elements); + foreach (var window in node.Value.Windows) { + if (window.Value.Enabled == Window.AllEnabled || !windows.ContainsKey(window.Key)) { + windows[window.Key] = window.Value.Clone(); + continue; + } + + windows[window.Key].UpdateEnabled(window.Value); + } + + return new SavedLayout($"Effective {id}", elements, windows, Guid.Empty); + } + + public void WriteEffectiveLayout(HudSlot slot, Guid id) { + var effective = this.GetEffectiveLayout(id); + if (effective == null) { + return; + } + + this.WriteLayout(slot, effective.Elements); + + foreach (var window in effective.Windows) { + this.Plugin.GameFunctions.SetAddonPosition(window.Key, window.Value.Position.X, window.Value.Position.Y); + } } internal void ImportSlot(string name, HudSlot slot, bool save = true) { diff --git a/HUD Manager/Statuses.cs b/HUD Manager/Statuses.cs index 256def2..72b5f36 100644 --- a/HUD Manager/Statuses.cs +++ b/HUD Manager/Statuses.cs @@ -82,16 +82,12 @@ namespace HUD_Manager { return; // FIXME: do something better } - if (!this.Plugin.Config.Layouts.TryGetValue(layoutId, out var layout)) { + if (!this.Plugin.Config.Layouts.ContainsKey(layoutId)) { return; // FIXME: do something better } this.Plugin.Hud.WriteEffectiveLayout(this.Plugin.Config.StagingSlot, layoutId); this.Plugin.Hud.SelectSlot(this.Plugin.Config.StagingSlot, true); - - foreach (var entry in layout.Windows) { - this.Plugin.GameFunctions.SetAddonPosition(entry.Key, entry.Value.Position.X, entry.Value.Position.Y); - } } } diff --git a/HUD Manager/Ui/Editor/Tabs/Windows.cs b/HUD Manager/Ui/Editor/Tabs/Windows.cs index 51acdca..b8dcd80 100755 --- a/HUD Manager/Ui/Editor/Tabs/Windows.cs +++ b/HUD Manager/Ui/Editor/Tabs/Windows.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Numerics; using Dalamud.Interface; using HUD_Manager.Configuration; @@ -52,24 +53,68 @@ namespace HUD_Manager.Ui.Editor.Tabs { continue; } - var pos = entry.Value.Position; + var maxSettingWidth = ImGui.CalcTextSize("Setting").X; - var x = (int) pos.X; - if (ImGui.InputInt($"X##uimanager-window-{entry.Key}", ref x)) { - pos.X = (short) x; - this.Plugin.GameFunctions.SetAddonPosition(entry.Key, pos.X, pos.Y); + void DrawSettingName(string name) { + maxSettingWidth = Math.Max(maxSettingWidth, ImGui.CalcTextSize(name).X); + ImGui.TextUnformatted(name); + ImGui.NextColumn(); } + ImGui.Columns(3); + ImGui.SetColumnWidth(0, ImGui.CalcTextSize("Enabled").X + ImGui.GetStyle().ItemSpacing.X * 2); + + ImGui.TextUnformatted("Enabled"); + ImGui.NextColumn(); + + DrawSettingName("Setting"); + + ImGui.TextUnformatted("Control"); + ImGui.SameLine(ImGui.GetContentRegionAvail().X - ImGui.GetStyle().ItemSpacing.X * 3); if (ImGuiExt.IconButton(FontAwesomeIcon.Trash, $"uimanager-remove-window-{entry.Key}")) { toRemove.Add(entry.Key); } + ImGui.Separator(); + + void DrawEnabledCheckbox(string kind, WindowComponent component) { + ImGui.NextColumn(); + + var enabled = entry.Value[component]; + if (ImGui.Checkbox($"###{component}-enabled-{kind}", ref enabled)) { + entry.Value[component] = enabled; + this.Plugin.Config.Save(); + } + + ImGui.NextColumn(); + } + + var pos = entry.Value.Position; + + DrawEnabledCheckbox(entry.Key, WindowComponent.X); + + DrawSettingName("X"); + + var x = (int) pos.X; + if (ImGui.InputInt($"##uimanager-x-window-{entry.Key}", ref x)) { + pos.X = (short) x; + this.Plugin.GameFunctions.SetAddonPosition(entry.Key, pos.X, pos.Y); + } + + DrawEnabledCheckbox(entry.Key, WindowComponent.Y); + + DrawSettingName("Y"); + var y = (int) pos.Y; - if (ImGui.InputInt($"Y##uimanager-window-{entry.Key}", ref y)) { + if (ImGui.InputInt($"##uimanager-y-window-{entry.Key}", ref y)) { pos.Y = (short) y; this.Plugin.GameFunctions.SetAddonPosition(entry.Key, pos.X, pos.Y); } + + ImGui.SetColumnWidth(1, maxSettingWidth + ImGui.GetStyle().ItemSpacing.X * 2); + + ImGui.Columns(); } foreach (var remove in toRemove) {