diff --git a/Dalamud.CorePlugin/PluginImpl.cs b/Dalamud.CorePlugin/PluginImpl.cs index 903f7c11a..de7e66270 100644 --- a/Dalamud.CorePlugin/PluginImpl.cs +++ b/Dalamud.CorePlugin/PluginImpl.cs @@ -3,7 +3,6 @@ using System.IO; using Dalamud.Configuration.Internal; using Dalamud.Game.Command; -using Dalamud.Interface.Internal.Windows; using Dalamud.Interface.Windowing; using Dalamud.Logging; using Dalamud.Plugin; @@ -63,7 +62,6 @@ namespace Dalamud.CorePlugin this.Interface = pluginInterface; this.windowSystem.AddWindow(new PluginWindow()); - this.windowSystem.AddWindow(new StyleEditorWindow()); this.Interface.UiBuilder.Draw += this.OnDraw; this.Interface.UiBuilder.OpenConfigUi += this.OnOpenConfigUi; diff --git a/Dalamud/Configuration/Internal/DalamudConfiguration.cs b/Dalamud/Configuration/Internal/DalamudConfiguration.cs index 2a0e0ce66..390d9c9e5 100644 --- a/Dalamud/Configuration/Internal/DalamudConfiguration.cs +++ b/Dalamud/Configuration/Internal/DalamudConfiguration.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; + using Dalamud.Game.Text; using Dalamud.Interface.Internal.Windows.StyleEditor; using Newtonsoft.Json; diff --git a/Dalamud/Interface/Internal/DalamudInterface.cs b/Dalamud/Interface/Internal/DalamudInterface.cs index 35a13ea8b..cfccedd39 100644 --- a/Dalamud/Interface/Internal/DalamudInterface.cs +++ b/Dalamud/Interface/Internal/DalamudInterface.cs @@ -46,6 +46,7 @@ namespace Dalamud.Interface.Internal private readonly ScratchpadWindow scratchpadWindow; private readonly SettingsWindow settingsWindow; private readonly SelfTestWindow selfTestWindow; + private readonly StyleEditorWindow styleEditorWindow; private ulong frameCount = 0; @@ -79,6 +80,7 @@ namespace Dalamud.Interface.Internal this.scratchpadWindow = new ScratchpadWindow() { IsOpen = false }; this.settingsWindow = new SettingsWindow() { IsOpen = false }; this.selfTestWindow = new SelfTestWindow() { IsOpen = false }; + this.styleEditorWindow = new StyleEditorWindow() { IsOpen = false }; this.WindowSystem.AddWindow(this.changelogWindow); this.WindowSystem.AddWindow(this.colorDemoWindow); @@ -93,12 +95,11 @@ namespace Dalamud.Interface.Internal this.WindowSystem.AddWindow(this.scratchpadWindow); this.WindowSystem.AddWindow(this.settingsWindow); this.WindowSystem.AddWindow(this.selfTestWindow); + this.WindowSystem.AddWindow(this.styleEditorWindow); ImGuiManagedAsserts.AssertsEnabled = true; Service.Get().Draw += this.OnDraw; - - Log.Information("Windows added"); } /// @@ -212,6 +213,11 @@ namespace Dalamud.Interface.Internal /// public void OpenSelfTest() => this.selfTestWindow.IsOpen = true; + /// + /// Opens the . + /// + public void OpenStyleEditor() => this.styleEditorWindow.IsOpen = true; + #endregion #region Close @@ -303,6 +309,11 @@ namespace Dalamud.Interface.Internal /// public void ToggleSelfTestWindow() => this.selfTestWindow.Toggle(); + /// + /// Toggles the . + /// + public void ToggleStyleEditorWindow() => this.selfTestWindow.Toggle(); + #endregion private void OnDraw() @@ -452,6 +463,11 @@ namespace Dalamud.Interface.Internal this.OpenSelfTest(); } + if (ImGui.MenuItem("Open Style Editor")) + { + this.OpenStyleEditor(); + } + ImGui.Separator(); if (ImGui.MenuItem("Unload Dalamud")) diff --git a/Dalamud.CorePlugin/StyleEditorWindow.cs b/Dalamud/Interface/Internal/Windows/StyleEditor/StyleEditorWindow.cs similarity index 71% rename from Dalamud.CorePlugin/StyleEditorWindow.cs rename to Dalamud/Interface/Internal/Windows/StyleEditor/StyleEditorWindow.cs index dba102d6f..32aa9ade8 100644 --- a/Dalamud.CorePlugin/StyleEditorWindow.cs +++ b/Dalamud/Interface/Internal/Windows/StyleEditor/StyleEditorWindow.cs @@ -1,58 +1,104 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; + +using CheapLoc; using Dalamud.Configuration.Internal; +using Dalamud.Data; using Dalamud.Interface.Colors; using Dalamud.Interface.Components; -using Dalamud.Interface.Internal.Windows.StyleEditor; using Dalamud.Interface.Windowing; using ImGuiNET; -using JetBrains.Annotations; +using Lumina.Excel.GeneratedSheets; using Serilog; -namespace Dalamud.Interface.Internal.Windows +namespace Dalamud.Interface.Internal.Windows.StyleEditor { + /// + /// Window for the Dalamud style editor. + /// public class StyleEditorWindow : Window { private ImGuiColorEditFlags alphaFlags = ImGuiColorEditFlags.None; private StyleModel workStyle = StyleModel.DalamudStandard; private int currentSel = 0; - private string initialStyle; + private string initialStyle = string.Empty; + private bool didSave = false; + private string renameText = string.Empty; + private bool renameModalDrawing = false; + + /// + /// Initializes a new instance of the class. + /// public StyleEditorWindow() : base("Dalamud Style Editor") { this.IsOpen = true; + this.SizeConstraints = new WindowSizeConstraints + { + MinimumSize = new Vector2(890, 560), + MaximumSize = new Vector2(10000, 10000), + }; + } + + /// + public override void OnOpen() + { + this.didSave = false; var config = Service.Get(); config.SavedStyles ??= new List(); this.currentSel = config.SavedStyles.FindIndex(x => x.Name == config.ChosenStyle); this.initialStyle = config.ChosenStyle; + + base.OnOpen(); } + /// + public override void OnClose() + { + if (!this.didSave) + { + var config = Service.Get(); + var newStyle = config.SavedStyles.FirstOrDefault(x => x.Name == this.initialStyle); + newStyle?.Apply(); + } + + base.OnClose(); + } + + /// public override void Draw() { var config = Service.Get(); + var renameModalTitle = Loc.Localize("RenameStyleModalTitle", "Rename Style"); - var style = ImGui.GetStyle(); + var appliedThisFrame = false; + var styleAry = config.SavedStyles.Select(x => x.Name).ToArray(); ImGui.Text("Choose Style:"); - if (ImGui.Combo("###styleChooserCombo", ref this.currentSel, config.SavedStyles.Select(x => x.Name).ToArray(), 1)) + if (ImGui.Combo("###styleChooserCombo", ref this.currentSel, styleAry, styleAry.Length)) { var newStyle = config.SavedStyles[this.currentSel]; newStyle.Apply(); + appliedThisFrame = true; } if (ImGui.Button("Add new style")) { var newStyle = StyleModel.DalamudStandard; - newStyle.Name = "New Style"; + newStyle.Name = GetRandomName(); config.SavedStyles.Add(newStyle); this.currentSel = config.SavedStyles.Count - 1; + newStyle.Apply(); + appliedThisFrame = true; + config.Save(); } @@ -63,6 +109,7 @@ namespace Dalamud.Interface.Internal.Windows this.currentSel--; var newStyle = config.SavedStyles[this.currentSel]; newStyle.Apply(); + appliedThisFrame = true; config.SavedStyles.RemoveAt(this.currentSel + 1); @@ -74,12 +121,27 @@ namespace Dalamud.Interface.Internal.Windows ImGui.SameLine(); + if (ImGuiComponents.IconButton(FontAwesomeIcon.Pen) && this.currentSel != 0) + { + var newStyle = config.SavedStyles[this.currentSel]; + this.renameText = newStyle.Name; + + this.renameModalDrawing = true; + ImGui.OpenPopup(renameModalTitle); + } + + if (ImGui.IsItemHovered()) + ImGui.SetTooltip("Rename style"); + + ImGui.SameLine(); + ImGuiHelpers.ScaledDummy(5); ImGui.SameLine(); if (ImGuiComponents.IconButton(FontAwesomeIcon.FileExport)) { - ImGui.SetClipboardText(StyleModel.Get().ToJsonEncoded()); + var newStyle = config.SavedStyles[this.currentSel]; + ImGui.SetClipboardText(newStyle.ToEncoded()); } if (ImGui.IsItemHovered()) @@ -93,10 +155,15 @@ namespace Dalamud.Interface.Internal.Windows try { - var newStyle = StyleModel.FromJsonEncoded(styleJson); + var newStyle = StyleModel.FromEncoded(styleJson); + + newStyle.Name ??= GetRandomName(); config.SavedStyles.Add(newStyle); newStyle.Apply(); + appliedThisFrame = true; + + Log.Information("Applying: " + newStyle.Name); this.currentSel = config.SavedStyles.Count - 1; @@ -119,8 +186,14 @@ namespace Dalamud.Interface.Internal.Windows { ImGui.TextColored(ImGuiColors.DalamudRed, "You cannot edit the \"Dalamud Standard\" style. Please add a new style first."); } + else if (appliedThisFrame) + { + ImGui.Text("Applying style..."); + } else if (ImGui.BeginTabBar("StyleEditorTabs")) { + var style = ImGui.GetStyle(); + if (ImGui.BeginTabItem("Variables")) { ImGui.BeginChild($"ScrollingVars", ImGuiHelpers.ScaledVector2(0, -32), true, ImGuiWindowFlags.HorizontalScrollbar | ImGuiWindowFlags.NoBackground); @@ -153,9 +226,9 @@ namespace Dalamud.Interface.Internal.Windows ImGui.SliderFloat("TabRounding", ref style.TabRounding, 0.0f, 12.0f, "%.0f"); ImGui.Text("Alignment"); ImGui.SliderFloat2("WindowTitleAlign", ref style.WindowTitleAlign, 0.0f, 1.0f, "%.2f"); - var window_menu_button_position = (int)style.WindowMenuButtonPosition + 1; - if (ImGui.Combo("WindowMenuButtonPosition", ref window_menu_button_position, "None\0Left\0Right\0")) - style.WindowMenuButtonPosition = (ImGuiDir)(window_menu_button_position - 1); + var windowMenuButtonPosition = (int)style.WindowMenuButtonPosition + 1; + if (ImGui.Combo("WindowMenuButtonPosition", ref windowMenuButtonPosition, "None\0Left\0Right\0")) + style.WindowMenuButtonPosition = (ImGuiDir)(windowMenuButtonPosition - 1); ImGui.SliderFloat2("ButtonTextAlign", ref style.ButtonTextAlign, 0.0f, 1.0f, "%.2f"); ImGui.SameLine(); ImGuiComponents.HelpMarker("Alignment applies when a button is larger than its text content."); @@ -223,9 +296,6 @@ namespace Dalamud.Interface.Internal.Windows if (ImGui.Button("Close")) { - var newStyle = config.SavedStyles.FirstOrDefault(x => x.Name == this.initialStyle); - newStyle?.Apply(); - this.IsOpen = false; } @@ -238,9 +308,43 @@ namespace Dalamud.Interface.Internal.Windows var newStyle = StyleModel.Get(); newStyle.Name = config.ChosenStyle; config.SavedStyles[this.currentSel] = newStyle; + newStyle.Apply(); config.Save(); + this.didSave = true; + + this.IsOpen = false; } + + if (ImGui.BeginPopupModal(renameModalTitle, ref this.renameModalDrawing, ImGuiWindowFlags.AlwaysAutoResize | ImGuiWindowFlags.NoScrollbar)) + { + ImGui.Text("Please enter the new name for this style."); + ImGui.Spacing(); + + ImGui.InputText("###renameModalInput", ref this.renameText, 255); + + const float buttonWidth = 120f; + ImGui.SetCursorPosX((ImGui.GetWindowWidth() - buttonWidth) / 2); + + if (ImGui.Button("OK", new Vector2(buttonWidth, 40))) + { + config.SavedStyles[this.currentSel].Name = this.renameText; + config.Save(); + + ImGui.CloseCurrentPopup(); + } + + ImGui.EndPopup(); + } + } + + private static string GetRandomName() + { + var data = Service.Get(); + var names = data.GetExcelSheet(ClientLanguage.English); + var rng = new Random(); + + return names.ElementAt(rng.Next(0, names.Count() - 1)).Singular.RawString; } } } diff --git a/Dalamud/Interface/Internal/Windows/StyleEditor/StyleModel.cs b/Dalamud/Interface/Internal/Windows/StyleEditor/StyleModel.cs index 48184c758..c8d43e775 100644 --- a/Dalamud/Interface/Internal/Windows/StyleEditor/StyleModel.cs +++ b/Dalamud/Interface/Internal/Windows/StyleEditor/StyleModel.cs @@ -1,17 +1,30 @@ using System; using System.Collections.Generic; -using System.IO; -using System.IO.Compression; using System.Numerics; -using System.Text; +using Dalamud.Utility; using ImGuiNET; using Newtonsoft.Json; namespace Dalamud.Interface.Internal.Windows.StyleEditor { + /// + /// Class representing a serializable ImGui style. + /// public class StyleModel { + /// + /// Initializes a new instance of the class. + /// + private StyleModel() + { + this.Colors = new Dictionary(); + this.Name = "Unknown"; + } + + /// + /// Gets the standard Dalamud look. + /// public static StyleModel DalamudStandard => new() { Name = "Dalamud Standard", @@ -104,6 +117,8 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor }, }; +#pragma warning disable SA1600 + [JsonProperty("name")] public string Name { get; set; } @@ -188,81 +203,18 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor [JsonProperty("aa")] public Vector2 DisplaySafeAreaPadding { get; set; } +#pragma warning restore SA1600 + + /// + /// Gets or sets a dictionary mapping ImGui color names to colors. + /// [JsonProperty("col")] public Dictionary Colors { get; set; } - public static void CopyTo(Stream src, Stream dest) { - byte[] bytes = new byte[4096]; - - int cnt; - - while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0) { - dest.Write(bytes, 0, cnt); - } - } - - public static byte[] Zip(string str) { - var bytes = Encoding.UTF8.GetBytes(str); - - using (var msi = new MemoryStream(bytes)) - using (var mso = new MemoryStream()) { - using (var gs = new GZipStream(mso, CompressionMode.Compress)) { - //msi.CopyTo(gs); - CopyTo(msi, gs); - } - - return mso.ToArray(); - } - } - - public static string Unzip(byte[] bytes) { - using (var msi = new MemoryStream(bytes)) - using (var mso = new MemoryStream()) { - using (var gs = new GZipStream(msi, CompressionMode.Decompress)) { - //gs.CopyTo(mso); - CopyTo(gs, mso); - } - - return Encoding.UTF8.GetString(mso.ToArray()); - } - } - - - public string ToJsonEncoded() => "DS1" + System.Convert.ToBase64String(Zip(JsonConvert.SerializeObject(this))); - - public void Apply() - { - var style = ImGui.GetStyle(); - - style.Alpha = this.Alpha; - style.WindowPadding = this.WindowPadding; - style.WindowRounding = this.WindowRounding; - style.WindowBorderSize = this.WindowBorderSize; - style.WindowTitleAlign = this.WindowTitleAlign; - style.WindowMenuButtonPosition = this.WindowMenuButtonPosition; - style.ChildRounding = this.ChildRounding; - style.ChildBorderSize = this.ChildBorderSize; - style.PopupRounding = this.PopupRounding; - style.FramePadding = this.FramePadding; - style.FrameRounding = this.FrameRounding; - style.FrameBorderSize = this.FrameBorderSize; - style.ItemSpacing = this.ItemSpacing; - style.ItemInnerSpacing = this.ItemInnerSpacing; - style.CellPadding = this.CellPadding; - style.TouchExtraPadding = this.TouchExtraPadding; - style.IndentSpacing = this.IndentSpacing; - style.ScrollbarSize = this.ScrollbarSize; - style.ScrollbarRounding = this.ScrollbarRounding; - style.GrabMinSize = this.GrabMinSize; - style.GrabRounding = this.GrabRounding; - style.LogSliderDeadzone = this.LogSliderDeadzone; - style.TabRounding = this.TabRounding; - style.TabBorderSize = this.TabBorderSize; - style.ButtonTextAlign = this.ButtonTextAlign; - style.SelectableTextAlign = this.SelectableTextAlign; - style.DisplaySafeAreaPadding = this.DisplaySafeAreaPadding; - } - + /// + /// Get a instance via ImGui. + /// + /// The newly created instance. public static StyleModel Get() { var model = new StyleModel(); @@ -311,10 +263,67 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor return model; } - public static StyleModel FromJsonEncoded(string data) + /// + /// Get a instance from a compressed base64 string. + /// + /// The string to decode. + /// A decompressed . + public static StyleModel? FromEncoded(string data) { - var json = Unzip(Convert.FromBase64String(data.Substring(2))); + 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() + { + var style = ImGui.GetStyle(); + + style.Alpha = this.Alpha; + style.WindowPadding = this.WindowPadding; + style.WindowRounding = this.WindowRounding; + style.WindowBorderSize = this.WindowBorderSize; + style.WindowTitleAlign = this.WindowTitleAlign; + style.WindowMenuButtonPosition = this.WindowMenuButtonPosition; + style.ChildRounding = this.ChildRounding; + style.ChildBorderSize = this.ChildBorderSize; + style.PopupRounding = this.PopupRounding; + style.FramePadding = this.FramePadding; + style.FrameRounding = this.FrameRounding; + style.FrameBorderSize = this.FrameBorderSize; + style.ItemSpacing = this.ItemSpacing; + style.ItemInnerSpacing = this.ItemInnerSpacing; + style.CellPadding = this.CellPadding; + style.TouchExtraPadding = this.TouchExtraPadding; + style.IndentSpacing = this.IndentSpacing; + style.ScrollbarSize = this.ScrollbarSize; + style.ScrollbarRounding = this.ScrollbarRounding; + style.GrabMinSize = this.GrabMinSize; + style.GrabRounding = this.GrabRounding; + style.LogSliderDeadzone = this.LogSliderDeadzone; + style.TabRounding = this.TabRounding; + style.TabBorderSize = this.TabBorderSize; + style.ButtonTextAlign = this.ButtonTextAlign; + style.SelectableTextAlign = this.SelectableTextAlign; + style.DisplaySafeAreaPadding = this.DisplaySafeAreaPadding; + + foreach (var imGuiCol in Enum.GetValues()) + { + if (imGuiCol == ImGuiCol.COUNT) + { + continue; + } + + style.Colors[(int)imGuiCol] = this.Colors[imGuiCol.ToString()]; + } + } } } diff --git a/Dalamud/Utility/Util.cs b/Dalamud/Utility/Util.cs index db0056ec0..cded71473 100644 --- a/Dalamud/Utility/Util.cs +++ b/Dalamud/Utility/Util.cs @@ -1,5 +1,7 @@ using System; using System.Diagnostics; +using System.IO; +using System.IO.Compression; using System.Linq; using System.Reflection; using System.Text; @@ -205,6 +207,60 @@ namespace Dalamud.Utility return text; } + /// + /// Compress a string using GZip. + /// + /// The input string. + /// The compressed output bytes. + public static byte[] CompressString(string str) + { + var bytes = Encoding.UTF8.GetBytes(str); + + using (var msi = new MemoryStream(bytes)) + using (var mso = new MemoryStream()) + { + using (var gs = new GZipStream(mso, CompressionMode.Compress)) + { + CopyTo(msi, gs); + } + + return mso.ToArray(); + } + } + + /// + /// Decompress a string using GZip. + /// + /// The input bytes. + /// The compressed output string. + public static string DecompressString(byte[] bytes) + { + using (var msi = new MemoryStream(bytes)) + using (var mso = new MemoryStream()) + { + using (var gs = new GZipStream(msi, CompressionMode.Decompress)) + { + CopyTo(gs, mso); + } + + return Encoding.UTF8.GetString(mso.ToArray()); + } + } + + /// + /// Copy one stream to another. + /// + /// The source stream. + /// The destination stream. + /// The maximum length to copy. + public static void CopyTo(Stream src, Stream dest, int len = 4069) + { + var bytes = new byte[len]; + int cnt; + + while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0) dest.Write(bytes, 0, cnt); + } + // TODO: Someone implement GetUTF8String with some IntPtr overloads. // while(Marshal.ReadByte(0, sz) != 0) { sz++; } }