refactor: generic style model API; push & pop (not implemented yet)

This commit is contained in:
goat 2021-10-20 03:04:47 +02:00
parent e8f395b8cc
commit c82f6aa0a6
No known key found for this signature in database
GPG key ID: 7773BB5B43BA52E5
7 changed files with 234 additions and 105 deletions

View file

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using Dalamud.Game.Text; using Dalamud.Game.Text;
using Dalamud.Interface.Internal.Windows.StyleEditor; using Dalamud.Interface.Style;
using Newtonsoft.Json; using Newtonsoft.Json;
using Serilog; using Serilog;
using Serilog.Events; using Serilog.Events;

View file

@ -15,6 +15,7 @@ using Dalamud.Interface.Internal.ManagedAsserts;
using Dalamud.Interface.Internal.Windows; using Dalamud.Interface.Internal.Windows;
using Dalamud.Interface.Internal.Windows.SelfTest; using Dalamud.Interface.Internal.Windows.SelfTest;
using Dalamud.Interface.Internal.Windows.StyleEditor; using Dalamud.Interface.Internal.Windows.StyleEditor;
using Dalamud.Interface.Style;
using Dalamud.Interface.Windowing; using Dalamud.Interface.Windowing;
using Dalamud.Logging; using Dalamud.Logging;
using Dalamud.Logging.Internal; using Dalamud.Logging.Internal;
@ -513,7 +514,7 @@ namespace Dalamud.Interface.Internal
if (ImGui.MenuItem("Dump style")) if (ImGui.MenuItem("Dump style"))
{ {
var info = string.Empty; var info = string.Empty;
var style = StyleModel.Get(); var style = StyleModelV1.Get();
var enCulture = new CultureInfo("en-US"); var enCulture = new CultureInfo("en-US");
foreach (var propertyInfo in typeof(StyleModel).GetProperties(BindingFlags.Public | BindingFlags.Instance)) foreach (var propertyInfo in typeof(StyleModel).GetProperties(BindingFlags.Public | BindingFlags.Instance))

View file

@ -19,6 +19,7 @@ using Dalamud.Hooking.Internal;
using Dalamud.Interface.Internal.ManagedAsserts; using Dalamud.Interface.Internal.ManagedAsserts;
using Dalamud.Interface.Internal.Notifications; using Dalamud.Interface.Internal.Notifications;
using Dalamud.Interface.Internal.Windows.StyleEditor; using Dalamud.Interface.Internal.Windows.StyleEditor;
using Dalamud.Interface.Style;
using Dalamud.Interface.Windowing; using Dalamud.Interface.Windowing;
using Dalamud.Utility; using Dalamud.Utility;
using ImGuiNET; using ImGuiNET;
@ -368,27 +369,27 @@ namespace Dalamud.Interface.Internal
this.SetupFonts(); 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> { StyleModel.DalamudStandard, StyleModel.DalamudClassic }; configuration.SavedStyles = new List<StyleModel> { StyleModelV1.DalamudStandard, StyleModelV1.DalamudClassic };
configuration.ChosenStyle = StyleModel.DalamudStandard.Name; configuration.ChosenStyle = StyleModelV1.DalamudStandard.Name;
} }
else if (configuration.SavedStyles.Count == 1) 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[0] = StyleModelV1.DalamudStandard;
configuration.SavedStyles[1] = StyleModel.DalamudClassic; configuration.SavedStyles[1] = StyleModelV1.DalamudClassic;
var style = configuration.SavedStyles.FirstOrDefault(x => x.Name == configuration.ChosenStyle); var style = configuration.SavedStyles.FirstOrDefault(x => x.Name == configuration.ChosenStyle);
if (style == null) if (style == null)
{ {
style = StyleModel.DalamudStandard; style = StyleModelV1.DalamudStandard;
configuration.ChosenStyle = style.Name; configuration.ChosenStyle = style.Name;
configuration.Save(); configuration.Save();
} }

View file

@ -9,6 +9,7 @@ using Dalamud.Configuration.Internal;
using Dalamud.Data; using Dalamud.Data;
using Dalamud.Interface.Colors; using Dalamud.Interface.Colors;
using Dalamud.Interface.Components; using Dalamud.Interface.Components;
using Dalamud.Interface.Style;
using Dalamud.Interface.Windowing; using Dalamud.Interface.Windowing;
using ImGuiNET; using ImGuiNET;
using Lumina.Excel.GeneratedSheets; using Lumina.Excel.GeneratedSheets;
@ -78,7 +79,7 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor
var renameModalTitle = Loc.Localize("RenameStyleModalTitle", "Rename Style"); var renameModalTitle = Loc.Localize("RenameStyleModalTitle", "Rename Style");
var workStyle = config.SavedStyles[this.currentSel]; var workStyle = config.SavedStyles[this.currentSel];
workStyle.BuiltInColors ??= StyleModel.DalamudStandard.BuiltInColors; workStyle.BuiltInColors ??= StyleModelV1.DalamudStandard.BuiltInColors;
var appliedThisFrame = false; var appliedThisFrame = false;
@ -95,7 +96,7 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor
{ {
this.SaveStyle(); this.SaveStyle();
var newStyle = StyleModel.DalamudStandard; var newStyle = StyleModelV1.DalamudStandard;
newStyle.Name = GetRandomName(); newStyle.Name = GetRandomName();
config.SavedStyles.Add(newStyle); config.SavedStyles.Add(newStyle);
@ -146,9 +147,9 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor
if (ImGuiComponents.IconButton(FontAwesomeIcon.FileExport)) if (ImGuiComponents.IconButton(FontAwesomeIcon.FileExport))
{ {
var selectedStyle = config.SavedStyles[this.currentSel]; var selectedStyle = config.SavedStyles[this.currentSel];
var newStyle = StyleModel.Get(); var newStyle = StyleModelV1.Get();
newStyle.Name = selectedStyle.Name; newStyle.Name = selectedStyle.Name;
ImGui.SetClipboardText(newStyle.ToEncoded()); ImGui.SetClipboardText(newStyle.Serialize());
} }
if (ImGui.IsItemHovered()) if (ImGui.IsItemHovered())
@ -164,7 +165,7 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor
try try
{ {
var newStyle = StyleModel.FromEncoded(styleJson); var newStyle = StyleModel.Deserialize(styleJson);
newStyle.Name ??= GetRandomName(); newStyle.Name ??= GetRandomName();
@ -296,11 +297,19 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor
ImGui.Separator(); 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); 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)) if (ImGui.ColorEdit4("##color", ref color, ImGuiColorEditFlags.AlphaBar | this.alphaFlags))
{ {
property.SetValue(workStyle.BuiltInColors, color); property.SetValue(workStyle.BuiltInColors, color);
@ -382,7 +391,7 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor
var config = Service<DalamudConfiguration>.Get(); var config = Service<DalamudConfiguration>.Get();
var newStyle = StyleModel.Get(); var newStyle = StyleModelV1.Get();
newStyle.Name = config.SavedStyles[this.currentSel].Name; newStyle.Name = config.SavedStyles[this.currentSel].Name;
config.SavedStyles[this.currentSel] = newStyle; config.SavedStyles[this.currentSel] = newStyle;
newStyle.Apply(); newStyle.Apply();

View file

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

View file

@ -0,0 +1,83 @@
using System;
using Dalamud.Interface.Colors;
using Dalamud.Utility;
using Newtonsoft.Json;
namespace Dalamud.Interface.Style
{
/// <summary>
/// Superclass for all versions of the Dalamud style model.
/// </summary>
public abstract class StyleModel
{
/// <summary>
/// Gets or sets the name of the style model.
/// </summary>
[JsonProperty("name")]
public string Name { get; set; } = "Unknown";
/// <summary>
/// Gets or sets class representing Dalamud-builtin <see cref="ImGuiColors"/>.
/// </summary>
[JsonProperty("dol")]
public DalamudColors? BuiltInColors { get; set; }
/// <summary>
/// Gets or sets version number of this model.
/// </summary>
[JsonProperty("ver")]
public int Version { get; set; }
/// <summary>
/// Deserialize a style model.
/// </summary>
/// <param name="model">The serialized model.</param>
/// <returns>The deserialized model.</returns>
/// <exception cref="ArgumentException">Thrown in case the version of the model is not known.</exception>
public static StyleModel? Deserialize(string model)
{
var json = Util.DecompressString(Convert.FromBase64String(model.Substring(3)));
if (model.StartsWith(StyleModelV1.SerializedPrefix))
return JsonConvert.DeserializeObject<StyleModelV1>(json);
throw new ArgumentException("Was not a compressed style model.");
}
/// <summary>
/// Serialize this style model.
/// </summary>
/// <returns>Serialized style model as string.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the version of the style model is unknown.</exception>
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)));
}
/// <summary>
/// Apply this style model to ImGui.
/// </summary>
public abstract void Apply();
/// <summary>
/// Push this StyleModel into the ImGui style/color stack.
/// </summary>
public abstract void Push();
/// <summary>
/// Pop this style model from the ImGui style/color stack.
/// </summary>
public abstract void Pop();
}
}

View file

@ -1,23 +1,22 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics; using System.Numerics;
using Dalamud.Interface.Colors; using Dalamud.Interface.Colors;
using Dalamud.Utility;
using ImGuiNET; using ImGuiNET;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace Dalamud.Interface.Internal.Windows.StyleEditor namespace Dalamud.Interface.Style
{ {
/// <summary> /// <summary>
/// Class representing a serializable ImGui style. /// Version one of the Dalamud style model.
/// </summary> /// </summary>
internal class StyleModel public class StyleModelV1 : StyleModel
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="StyleModel"/> class. /// Initializes a new instance of the <see cref="StyleModelV1"/> class.
/// </summary> /// </summary>
private StyleModel() private StyleModelV1()
{ {
this.Colors = new Dictionary<string, Vector4>(); this.Colors = new Dictionary<string, Vector4>();
this.Name = "Unknown"; this.Name = "Unknown";
@ -26,7 +25,7 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor
/// <summary> /// <summary>
/// Gets the standard Dalamud look. /// Gets the standard Dalamud look.
/// </summary> /// </summary>
public static StyleModel DalamudStandard => new() public static StyleModelV1 DalamudStandard => new()
{ {
Name = "Dalamud Standard", Name = "Dalamud Standard",
@ -135,7 +134,7 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor
/// <summary> /// <summary>
/// Gets the standard Dalamud look. /// Gets the standard Dalamud look.
/// </summary> /// </summary>
public static StyleModel DalamudClassic => new() public static StyleModelV1 DalamudClassic => new()
{ {
Name = "Dalamud Classic", Name = "Dalamud Classic",
@ -241,10 +240,12 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor
}, },
}; };
#pragma warning disable SA1600 /// <summary>
/// Gets the version prefix for this version.
/// </summary>
public static string SerializedPrefix => "DS1";
[JsonProperty("name")] #pragma warning disable SA1600
public string Name { get; set; }
[JsonProperty("a")] [JsonProperty("a")]
public float Alpha { get; set; } public float Alpha { get; set; }
@ -335,19 +336,13 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor
[JsonProperty("col")] [JsonProperty("col")]
public Dictionary<string, Vector4> Colors { get; set; } public Dictionary<string, Vector4> Colors { get; set; }
/// <summary>
/// Gets or sets class representing Dalamud-builtin <see cref="ImGuiColors"/>.
/// </summary>
[JsonProperty("dol")]
public DalamudColors? BuiltInColors { get; set; }
/// <summary> /// <summary>
/// Get a <see cref="StyleModel"/> instance via ImGui. /// Get a <see cref="StyleModel"/> instance via ImGui.
/// </summary> /// </summary>
/// <returns>The newly created <see cref="StyleModel"/> instance.</returns> /// <returns>The newly created <see cref="StyleModel"/> instance.</returns>
public static StyleModel Get() public static StyleModelV1 Get()
{ {
var model = new StyleModel(); var model = new StyleModelV1();
var style = ImGui.GetStyle(); var style = ImGui.GetStyle();
model.Alpha = style.Alpha; model.Alpha = style.Alpha;
@ -407,27 +402,10 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor
return model; return model;
} }
/// <summary>
/// Get a <see cref="StyleModel"/> instance from a compressed base64 string.
/// </summary>
/// <param name="data">The string to decode.</param>
/// <returns>A decompressed <see cref="StyleModel"/>.</returns>
public static StyleModel? FromEncoded(string data)
{
var json = Util.DecompressString(Convert.FromBase64String(data.Substring(3)));
return JsonConvert.DeserializeObject<StyleModel>(json);
}
/// <summary>
/// Get this <see cref="StyleModel"/> instance as a encoded base64 string.
/// </summary>
/// <returns>The encoded base64 string.</returns>
public string ToEncoded() => "DS1" + Convert.ToBase64String(Util.CompressString(JsonConvert.SerializeObject(this)));
/// <summary> /// <summary>
/// Apply this StyleModel via ImGui. /// Apply this StyleModel via ImGui.
/// </summary> /// </summary>
public void Apply() public override void Apply()
{ {
var style = ImGui.GetStyle(); var style = ImGui.GetStyle();
@ -472,56 +450,16 @@ namespace Dalamud.Interface.Internal.Windows.StyleEditor
this.BuiltInColors?.Apply(); this.BuiltInColors?.Apply();
} }
#pragma warning disable SA1600 /// <inheritdoc/>
public override void Push()
public class DalamudColors
{ {
[JsonProperty("a")] throw new NotImplementedException();
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;
}
} }
#pragma warning restore SA1600 /// <inheritdoc/>
public override void Pop()
{
throw new NotImplementedException();
}
} }
} }