diff --git a/Dalamud/Configuration/Internal/DalamudConfiguration.cs b/Dalamud/Configuration/Internal/DalamudConfiguration.cs index d589c539f..35992cffc 100644 --- a/Dalamud/Configuration/Internal/DalamudConfiguration.cs +++ b/Dalamud/Configuration/Internal/DalamudConfiguration.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.IO; using Dalamud.Game.Text; -using Dalamud.Interface.Internal.Windows.StyleEditor; +using Dalamud.Interface.Style; using Newtonsoft.Json; using Serilog; using Serilog.Events; diff --git a/Dalamud/Interface/Internal/DalamudInterface.cs b/Dalamud/Interface/Internal/DalamudInterface.cs index 097a31682..f6137dc1a 100644 --- a/Dalamud/Interface/Internal/DalamudInterface.cs +++ b/Dalamud/Interface/Internal/DalamudInterface.cs @@ -15,6 +15,7 @@ using Dalamud.Interface.Internal.ManagedAsserts; using Dalamud.Interface.Internal.Windows; using Dalamud.Interface.Internal.Windows.SelfTest; using Dalamud.Interface.Internal.Windows.StyleEditor; +using Dalamud.Interface.Style; using Dalamud.Interface.Windowing; using Dalamud.Logging; using Dalamud.Logging.Internal; @@ -513,7 +514,7 @@ namespace Dalamud.Interface.Internal if (ImGui.MenuItem("Dump style")) { var info = string.Empty; - var style = StyleModel.Get(); + var style = StyleModelV1.Get(); var enCulture = new CultureInfo("en-US"); foreach (var propertyInfo in typeof(StyleModel).GetProperties(BindingFlags.Public | BindingFlags.Instance)) diff --git a/Dalamud/Interface/Internal/InterfaceManager.cs b/Dalamud/Interface/Internal/InterfaceManager.cs index 5a188c963..4284c876b 100644 --- a/Dalamud/Interface/Internal/InterfaceManager.cs +++ b/Dalamud/Interface/Internal/InterfaceManager.cs @@ -19,6 +19,7 @@ using Dalamud.Hooking.Internal; using Dalamud.Interface.Internal.ManagedAsserts; using Dalamud.Interface.Internal.Notifications; using Dalamud.Interface.Internal.Windows.StyleEditor; +using Dalamud.Interface.Style; using Dalamud.Interface.Windowing; using Dalamud.Utility; using ImGuiNET; @@ -368,27 +369,27 @@ namespace Dalamud.Interface.Internal this.SetupFonts(); - if (configuration.SavedStyles == null || configuration.SavedStyles.All(x => x.Name != StyleModel.DalamudStandard.Name)) + if (configuration.SavedStyles == null || configuration.SavedStyles.All(x => x.Name != StyleModelV1.DalamudStandard.Name)) { - configuration.SavedStyles = new List { StyleModel.DalamudStandard, StyleModel.DalamudClassic }; - configuration.ChosenStyle = StyleModel.DalamudStandard.Name; + configuration.SavedStyles = new List { StyleModelV1.DalamudStandard, StyleModelV1.DalamudClassic }; + configuration.ChosenStyle = StyleModelV1.DalamudStandard.Name; } else if (configuration.SavedStyles.Count == 1) { - configuration.SavedStyles.Add(StyleModel.DalamudClassic); + configuration.SavedStyles.Add(StyleModelV1.DalamudClassic); } - else if (configuration.SavedStyles[1].Name != StyleModel.DalamudClassic.Name) + else if (configuration.SavedStyles[1].Name != StyleModelV1.DalamudClassic.Name) { - configuration.SavedStyles.Insert(1, StyleModel.DalamudClassic); + configuration.SavedStyles.Insert(1, StyleModelV1.DalamudClassic); } - configuration.SavedStyles[0] = StyleModel.DalamudStandard; - configuration.SavedStyles[1] = StyleModel.DalamudClassic; + configuration.SavedStyles[0] = StyleModelV1.DalamudStandard; + configuration.SavedStyles[1] = StyleModelV1.DalamudClassic; var style = configuration.SavedStyles.FirstOrDefault(x => x.Name == configuration.ChosenStyle); if (style == null) { - style = StyleModel.DalamudStandard; + style = StyleModelV1.DalamudStandard; configuration.ChosenStyle = style.Name; configuration.Save(); } diff --git a/Dalamud/Interface/Internal/Windows/StyleEditor/StyleEditorWindow.cs b/Dalamud/Interface/Internal/Windows/StyleEditor/StyleEditorWindow.cs index ce284492e..5b2c3d90c 100644 --- a/Dalamud/Interface/Internal/Windows/StyleEditor/StyleEditorWindow.cs +++ b/Dalamud/Interface/Internal/Windows/StyleEditor/StyleEditorWindow.cs @@ -9,6 +9,7 @@ using Dalamud.Configuration.Internal; using Dalamud.Data; using Dalamud.Interface.Colors; using Dalamud.Interface.Components; +using Dalamud.Interface.Style; using Dalamud.Interface.Windowing; using ImGuiNET; using Lumina.Excel.GeneratedSheets; @@ -78,7 +79,7 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor var renameModalTitle = Loc.Localize("RenameStyleModalTitle", "Rename Style"); var workStyle = config.SavedStyles[this.currentSel]; - workStyle.BuiltInColors ??= StyleModel.DalamudStandard.BuiltInColors; + workStyle.BuiltInColors ??= StyleModelV1.DalamudStandard.BuiltInColors; var appliedThisFrame = false; @@ -95,7 +96,7 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor { this.SaveStyle(); - var newStyle = StyleModel.DalamudStandard; + var newStyle = StyleModelV1.DalamudStandard; newStyle.Name = GetRandomName(); config.SavedStyles.Add(newStyle); @@ -146,9 +147,9 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor if (ImGuiComponents.IconButton(FontAwesomeIcon.FileExport)) { var selectedStyle = config.SavedStyles[this.currentSel]; - var newStyle = StyleModel.Get(); + var newStyle = StyleModelV1.Get(); newStyle.Name = selectedStyle.Name; - ImGui.SetClipboardText(newStyle.ToEncoded()); + ImGui.SetClipboardText(newStyle.Serialize()); } if (ImGui.IsItemHovered()) @@ -164,7 +165,7 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor try { - var newStyle = StyleModel.FromEncoded(styleJson); + var newStyle = StyleModel.Deserialize(styleJson); newStyle.Name ??= GetRandomName(); @@ -296,11 +297,19 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor ImGui.Separator(); - foreach (var property in typeof(StyleModel.DalamudColors).GetProperties(BindingFlags.Public | BindingFlags.Instance)) + foreach (var property in typeof(DalamudColors).GetProperties(BindingFlags.Public | BindingFlags.Instance)) { ImGui.PushID(property.Name); - var color = (Vector4)property.GetValue(workStyle.BuiltInColors); + var colorVal = property.GetValue(workStyle.BuiltInColors); + if (colorVal == null) + { + colorVal = property.GetValue(StyleModelV1.DalamudStandard.BuiltInColors); + property.SetValue(workStyle.BuiltInColors, colorVal); + } + + var color = (Vector4)colorVal; + if (ImGui.ColorEdit4("##color", ref color, ImGuiColorEditFlags.AlphaBar | this.alphaFlags)) { property.SetValue(workStyle.BuiltInColors, color); @@ -382,7 +391,7 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor var config = Service.Get(); - var newStyle = StyleModel.Get(); + var newStyle = StyleModelV1.Get(); newStyle.Name = config.SavedStyles[this.currentSel].Name; config.SavedStyles[this.currentSel] = newStyle; newStyle.Apply(); diff --git a/Dalamud/Interface/Style/DalamudColors.cs b/Dalamud/Interface/Style/DalamudColors.cs new file mode 100644 index 000000000..a674ee4b2 --- /dev/null +++ b/Dalamud/Interface/Style/DalamudColors.cs @@ -0,0 +1,97 @@ +using System.Numerics; + +using Dalamud.Interface.Colors; +using Newtonsoft.Json; + +namespace Dalamud.Interface.Style +{ + #pragma warning disable SA1600 + + public class DalamudColors + { + [JsonProperty("a")] + public Vector4? DalamudRed { get; set; } + + [JsonProperty("b")] + public Vector4? DalamudGrey { get; set; } + + [JsonProperty("c")] + public Vector4? DalamudGrey2 { get; set; } + + [JsonProperty("d")] + public Vector4? DalamudGrey3 { get; set; } + + [JsonProperty("e")] + public Vector4? DalamudWhite { get; set; } + + [JsonProperty("f")] + public Vector4? DalamudWhite2 { get; set; } + + [JsonProperty("g")] + public Vector4? DalamudOrange { get; set; } + + [JsonProperty("h")] + public Vector4? TankBlue { get; set; } + + [JsonProperty("i")] + public Vector4? HealerGreen { get; set; } + + [JsonProperty("j")] + public Vector4? DPSRed { get; set; } + + public void Apply() + { + if (this.DalamudRed.HasValue) + { + ImGuiColors.DalamudRed = this.DalamudRed.Value; + } + + if (this.DalamudGrey.HasValue) + { + ImGuiColors.DalamudGrey = this.DalamudGrey.Value; + } + + if (this.DalamudGrey2.HasValue) + { + ImGuiColors.DalamudGrey2 = this.DalamudGrey2.Value; + } + + if (this.DalamudGrey3.HasValue) + { + ImGuiColors.DalamudGrey3 = this.DalamudGrey3.Value; + } + + if (this.DalamudWhite.HasValue) + { + ImGuiColors.DalamudWhite = this.DalamudWhite.Value; + } + + if (this.DalamudWhite2.HasValue) + { + ImGuiColors.DalamudWhite2 = this.DalamudWhite2.Value; + } + + if (this.DalamudOrange.HasValue) + { + ImGuiColors.DalamudOrange = this.DalamudOrange.Value; + } + + if (this.TankBlue.HasValue) + { + ImGuiColors.TankBlue = this.TankBlue.Value; + } + + if (this.HealerGreen.HasValue) + { + ImGuiColors.HealerGreen = this.HealerGreen.Value; + } + + if (this.DPSRed.HasValue) + { + ImGuiColors.DPSRed = this.DPSRed.Value; + } + } + } + +#pragma warning restore SA1600 +} diff --git a/Dalamud/Interface/Style/StyleModel.cs b/Dalamud/Interface/Style/StyleModel.cs new file mode 100644 index 000000000..9bb95afb0 --- /dev/null +++ b/Dalamud/Interface/Style/StyleModel.cs @@ -0,0 +1,83 @@ +using System; + +using Dalamud.Interface.Colors; +using Dalamud.Utility; +using Newtonsoft.Json; + +namespace Dalamud.Interface.Style +{ + /// + /// Superclass for all versions of the Dalamud style model. + /// + public abstract class StyleModel + { + /// + /// Gets or sets the name of the style model. + /// + [JsonProperty("name")] + public string Name { get; set; } = "Unknown"; + + /// + /// Gets or sets class representing Dalamud-builtin . + /// + [JsonProperty("dol")] + public DalamudColors? BuiltInColors { get; set; } + + /// + /// Gets or sets version number of this model. + /// + [JsonProperty("ver")] + public int Version { get; set; } + + /// + /// Deserialize a style model. + /// + /// The serialized model. + /// The deserialized model. + /// Thrown in case the version of the model is not known. + public static StyleModel? Deserialize(string model) + { + var json = Util.DecompressString(Convert.FromBase64String(model.Substring(3))); + + if (model.StartsWith(StyleModelV1.SerializedPrefix)) + return JsonConvert.DeserializeObject(json); + + throw new ArgumentException("Was not a compressed style model."); + } + + /// + /// Serialize this style model. + /// + /// Serialized style model as string. + /// Thrown when the version of the style model is unknown. + public string Serialize() + { + string prefix; + switch (this) + { + case StyleModelV1: + prefix = StyleModelV1.SerializedPrefix; + break; + default: + throw new ArgumentOutOfRangeException(); + } + + return prefix + Convert.ToBase64String(Util.CompressString(JsonConvert.SerializeObject(this))); + } + + /// + /// Apply this style model to ImGui. + /// + public abstract void Apply(); + + /// + /// Push this StyleModel into the ImGui style/color stack. + /// + public abstract void Push(); + + /// + /// Pop this style model from the ImGui style/color stack. + /// + public abstract void Pop(); + } +} diff --git a/Dalamud/Interface/Internal/Windows/StyleEditor/StyleModel.cs b/Dalamud/Interface/Style/StyleModelV1.cs similarity index 86% rename from Dalamud/Interface/Internal/Windows/StyleEditor/StyleModel.cs rename to Dalamud/Interface/Style/StyleModelV1.cs index 0f5effe11..3792df4f4 100644 --- a/Dalamud/Interface/Internal/Windows/StyleEditor/StyleModel.cs +++ b/Dalamud/Interface/Style/StyleModelV1.cs @@ -1,23 +1,22 @@ -using System; +using System; using System.Collections.Generic; using System.Numerics; using Dalamud.Interface.Colors; -using Dalamud.Utility; using ImGuiNET; using Newtonsoft.Json; -namespace Dalamud.Interface.Internal.Windows.StyleEditor +namespace Dalamud.Interface.Style { /// - /// Class representing a serializable ImGui style. + /// Version one of the Dalamud style model. /// - internal class StyleModel + public class StyleModelV1 : StyleModel { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// - private StyleModel() + private StyleModelV1() { this.Colors = new Dictionary(); this.Name = "Unknown"; @@ -26,7 +25,7 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor /// /// Gets the standard Dalamud look. /// - public static StyleModel DalamudStandard => new() + public static StyleModelV1 DalamudStandard => new() { Name = "Dalamud Standard", @@ -135,7 +134,7 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor /// /// Gets the standard Dalamud look. /// - public static StyleModel DalamudClassic => new() + public static StyleModelV1 DalamudClassic => new() { Name = "Dalamud Classic", @@ -241,10 +240,12 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor }, }; -#pragma warning disable SA1600 + /// + /// Gets the version prefix for this version. + /// + public static string SerializedPrefix => "DS1"; - [JsonProperty("name")] - public string Name { get; set; } +#pragma warning disable SA1600 [JsonProperty("a")] public float Alpha { get; set; } @@ -335,19 +336,13 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor [JsonProperty("col")] public Dictionary Colors { get; set; } - /// - /// Gets or sets class representing Dalamud-builtin . - /// - [JsonProperty("dol")] - public DalamudColors? BuiltInColors { get; set; } - /// /// Get a instance via ImGui. /// /// The newly created instance. - public static StyleModel Get() + public static StyleModelV1 Get() { - var model = new StyleModel(); + var model = new StyleModelV1(); var style = ImGui.GetStyle(); model.Alpha = style.Alpha; @@ -407,27 +402,10 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor return model; } - /// - /// Get a instance from a compressed base64 string. - /// - /// The string to decode. - /// A decompressed . - public static StyleModel? FromEncoded(string data) - { - var json = Util.DecompressString(Convert.FromBase64String(data.Substring(3))); - return JsonConvert.DeserializeObject(json); - } - - /// - /// Get this instance as a encoded base64 string. - /// - /// The encoded base64 string. - public string ToEncoded() => "DS1" + Convert.ToBase64String(Util.CompressString(JsonConvert.SerializeObject(this))); - /// /// Apply this StyleModel via ImGui. /// - public void Apply() + public override void Apply() { var style = ImGui.GetStyle(); @@ -472,56 +450,16 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor this.BuiltInColors?.Apply(); } -#pragma warning disable SA1600 - - public class DalamudColors + /// + public override void Push() { - [JsonProperty("a")] - public Vector4 DalamudRed { get; set; } - - [JsonProperty("b")] - public Vector4 DalamudGrey { get; set; } - - [JsonProperty("c")] - public Vector4 DalamudGrey2 { get; set; } - - [JsonProperty("d")] - public Vector4 DalamudGrey3 { get; set; } - - [JsonProperty("e")] - public Vector4 DalamudWhite { get; set; } - - [JsonProperty("f")] - public Vector4 DalamudWhite2 { get; set; } - - [JsonProperty("g")] - public Vector4 DalamudOrange { get; set; } - - [JsonProperty("h")] - public Vector4 TankBlue { get; set; } - - [JsonProperty("i")] - public Vector4 HealerGreen { get; set; } - - [JsonProperty("j")] - public Vector4 DPSRed { get; set; } - - public void Apply() - { - ImGuiColors.DalamudRed = this.DalamudRed; - ImGuiColors.DalamudGrey = this.DalamudGrey; - ImGuiColors.DalamudGrey2 = this.DalamudGrey2; - ImGuiColors.DalamudGrey3 = this.DalamudGrey3; - ImGuiColors.DalamudWhite = this.DalamudWhite; - ImGuiColors.DalamudWhite2 = this.DalamudWhite2; - ImGuiColors.DalamudOrange = this.DalamudOrange; - ImGuiColors.TankBlue = this.TankBlue; - ImGuiColors.HealerGreen = this.HealerGreen; - ImGuiColors.DPSRed = this.DPSRed; - } + throw new NotImplementedException(); } -#pragma warning restore SA1600 - + /// + public override void Pop() + { + throw new NotImplementedException(); + } } }