using System.Collections.Generic;
using System.IO;
using Lumina.Excel.GeneratedSheets;
using Newtonsoft.Json;
namespace Dalamud.Game.Text.SeStringHandling.Payloads;
///
/// An SeString Payload representing a UI foreground color applied to following text payloads.
///
public class UIForegroundPayload : Payload
{
private UIColor color;
[JsonProperty]
private ushort colorKey;
///
/// Initializes a new instance of the class.
/// Creates a new UIForegroundPayload for the given UIColor key.
///
/// A UIColor key.
public UIForegroundPayload(ushort colorKey)
{
this.colorKey = colorKey;
}
///
/// Initializes a new instance of the class.
/// Creates a new UIForegroundPayload for the given UIColor key.
///
internal UIForegroundPayload()
{
}
///
/// Gets a payload representing disabling foreground color on following text.
///
// TODO Make this work with DI
public static UIForegroundPayload UIForegroundOff => new(0);
///
public override PayloadType Type => PayloadType.UIForeground;
///
/// Gets a value indicating whether or not this payload represents applying a foreground color, or disabling one.
///
public bool IsEnabled => this.ColorKey != 0;
///
/// Gets a Lumina UIColor object representing this payload. The actual color data is at UIColor.UIForeground.
///
///
/// The value is evaluated lazily and cached.
///
[JsonIgnore]
public UIColor UIColor => this.color ??= this.DataResolver.GetExcelSheet().GetRow(this.colorKey);
///
/// Gets or sets the color key used as a lookup in the UIColor table for this foreground color.
///
[JsonIgnore]
public ushort ColorKey
{
get
{
return this.colorKey;
}
set
{
this.colorKey = value;
this.color = null;
this.Dirty = true;
}
}
///
/// Gets the Red/Green/Blue/Alpha values for this foreground color, encoded as a typical hex color.
///
[JsonIgnore]
public uint RGBA => this.UIColor.UIForeground;
///
/// Gets the ABGR value for this foreground color, as ImGui requires it in PushColor.
///
[JsonIgnore]
public uint ABGR => Interface.ColorHelpers.SwapEndianness(this.UIColor.UIForeground);
///
public override string ToString()
{
return $"{this.Type} - UIColor: {this.colorKey} color: {(this.IsEnabled ? this.RGBA : 0)}";
}
///
protected override byte[] EncodeImpl()
{
var colorBytes = MakeInteger(this.colorKey);
var chunkLen = colorBytes.Length + 1;
var bytes = new List(new byte[]
{
START_BYTE, (byte)SeStringChunkType.UIForeground, (byte)chunkLen,
});
bytes.AddRange(colorBytes);
bytes.Add(END_BYTE);
return bytes.ToArray();
}
///
protected override void DecodeImpl(BinaryReader reader, long endOfStream)
{
this.colorKey = (ushort)GetInteger(reader);
}
}