feat: allow selective window fields

This commit is contained in:
Anna 2021-03-19 14:40:07 -04:00
parent 71fcb5bf5b
commit 7e2cb169b9
4 changed files with 117 additions and 17 deletions

View File

@ -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<short> 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<short> position) {
this.Enabled = enabled;
@ -17,11 +30,25 @@ namespace HUD_Manager.Configuration {
public Window(Vector2<short> position) {
this.Position = position;
}
public Window Clone() {
return new Window(this.Enabled, new Vector2<short>(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,
}
}

View File

@ -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<SavedLayout>.BuildTree(this.Plugin.Config.Layouts);
var node = nodes.Find(id);
if (node == null) {
return;
return null;
}
var elements = new Dictionary<ElementKind, Element>();
var windows = new Dictionary<string, Window>();
// 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) {

View File

@ -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);
}
}
}

View File

@ -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) {