diff --git a/zzre.core/assetregistry/Asset.cs b/zzre.core/assetregistry/Asset.cs index 0508d80a..a42053f1 100644 --- a/zzre.core/assetregistry/Asset.cs +++ b/zzre.core/assetregistry/Asset.cs @@ -67,6 +67,7 @@ public abstract class Asset : IAsset /// The of the apparent registry to be used during loading protected readonly ITagContainer diContainer; private readonly TaskCompletionSource completionSource = new(); + private string? description; private AssetHandle[] secondaryAssets = []; private int refCount; @@ -236,5 +237,10 @@ private void EnsureLocality(AssetHandle[] secondaryAssets) /// Produces a description of the asset to be shown in debug logs and tools /// A description of the asset instance for debugging - public override string ToString() => $"{GetType().Name} {ID}"; + public override sealed string ToString() => description ??= ToStringInner(); + + /// Produces a description of the asset to be shown in debug logs and tools + /// Used to cache description strings + /// A description of the asset instance for debugging + protected virtual string ToStringInner() => $"{GetType().Name} {ID}"; } diff --git a/zzre.core/imgui/ImGuiEx.cs b/zzre.core/imgui/ImGuiEx.cs index 566ade22..e057a12e 100644 --- a/zzre.core/imgui/ImGuiEx.cs +++ b/zzre.core/imgui/ImGuiEx.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Numerics; using System.Text; @@ -171,16 +172,16 @@ public static bool Hyperlink(string label, string text, bool addIcon = true, boo { if (label.Length > 0) { - Text(label); + ImGui.Text(label); SameLine(); } if (!isEnabled) { - Text(text); + ImGui.Text(text); return false; } PushStyleColor(ImGuiCol.Text, GetStyle().Colors[(int)ImGuiCol.ButtonHovered]); - Text(text + (addIcon ? " " + IconFonts.ForkAwesome.ExternalLink : "")); + ImGui.Text(text + (addIcon ? " " + IconFonts.ForkAwesome.ExternalLink : "")); PopStyleColor(); AddUnderLine(GetStyle().Colors[(int)(IsItemHovered() ? IsMouseDown(ImGuiMouseButton.Left) @@ -234,7 +235,7 @@ public static bool ColorEdit4(string label, ref FColor color, ImGuiColorEditFlag public static bool ValueRangeAnimation(string label, ref ValueRangeAnimation a, float min = float.MinValue, float max = float.MaxValue) { PushID(label); - Text(label + ':'); + ImGui.Text(label + ':'); Indent(); float minValue = a.value - a.width, maxValue = a.value + a.width; var result = DragFloatRange2("Range", ref minValue, ref maxValue, 1f, min, max); @@ -275,4 +276,36 @@ public static bool InputInt(string label, ref byte cur) cur = (byte)Math.Clamp(curI, byte.MinValue, byte.MaxValue); return result; } + + private static readonly int GuidCharLength = Guid.Empty.ToString().Length; + public static void Text(in Guid gguid) + { + Span chars = stackalloc char[GuidCharLength]; + var success = gguid.TryFormat(chars, out _); + System.Diagnostics.Debug.Assert(success); + ImGui.Text(chars); + } + + private static readonly Dictionary enumNames = []; + public static void Text(TEnum value) where TEnum : struct, Enum + { + if (!enumNames.TryGetValue(typeof(TEnum), out var names)) + { + var values = Enum.GetValues(); + var minValue = values.Min(e => e.GetHashCode()); + var maxValue = values.Max(e => e.GetHashCode()); + if (minValue != 0 || maxValue - minValue + 1 != values.Length) + throw new ArgumentException($"Enum is not sequential"); + names = new char[values.Length][]; + foreach (var v in values) + { + if (names[v.GetHashCode()] is not null) + throw new ArgumentException("Enum has duplicates"); + names[v.GetHashCode()] = v.ToString().ToCharArray(); + } + enumNames.Add(typeof(TEnum), names); + } + + ImGui.Text(names[value.GetHashCode()]); + } } diff --git a/zzre/assets/ActorAsset.cs b/zzre/assets/ActorAsset.cs index 3abecc3a..fc7cafb4 100644 --- a/zzre/assets/ActorAsset.cs +++ b/zzre/assets/ActorAsset.cs @@ -79,5 +79,5 @@ protected override void Unload() wingsAnimations = []; } - public override string ToString() => $"Actor {info.Name}"; + protected override string ToStringInner() => $"Actor {info.Name}"; } diff --git a/zzre/assets/AnimationAsset.cs b/zzre/assets/AnimationAsset.cs index 19b9e972..210f0136 100644 --- a/zzre/assets/AnimationAsset.cs +++ b/zzre/assets/AnimationAsset.cs @@ -44,5 +44,5 @@ protected override void Unload() animation = null; } - public override string ToString() => $"Animation {info.Name}"; + protected override string ToStringInner() => $"Animation {info.Name}"; } diff --git a/zzre/assets/ClumpAsset.cs b/zzre/assets/ClumpAsset.cs index 00489a49..e358d68d 100644 --- a/zzre/assets/ClumpAsset.cs +++ b/zzre/assets/ClumpAsset.cs @@ -51,7 +51,7 @@ protected override void Unload() mesh = null; } - public override string ToString() => $"Clump {info.Name} ({info.Directory})"; + protected override string ToStringInner() => $"Clump {info.Name} ({info.Directory})"; } public static unsafe partial class AssetExtensions diff --git a/zzre/assets/EffectCombinerAsset.cs b/zzre/assets/EffectCombinerAsset.cs index 36c7bd48..225e03ac 100644 --- a/zzre/assets/EffectCombinerAsset.cs +++ b/zzre/assets/EffectCombinerAsset.cs @@ -43,7 +43,7 @@ protected override void Unload() effectCombiner = null; } - public override string ToString() => $"EffectCombiner {info.Name}"; + protected override string ToStringInner() => $"EffectCombiner {info.Name}"; } partial class AssetExtensions diff --git a/zzre/assets/EffectMaterialAsset.cs b/zzre/assets/EffectMaterialAsset.cs index 99e5ecbf..45c46647 100644 --- a/zzre/assets/EffectMaterialAsset.cs +++ b/zzre/assets/EffectMaterialAsset.cs @@ -97,7 +97,7 @@ protected override void Unload() material = null; } - public override string ToString() => $"EffectMaterial {DebugName}"; + protected override string ToStringInner() => $"EffectMaterial {DebugName}"; } partial class AssetExtensions diff --git a/zzre/assets/ModelMaterialAsset.cs b/zzre/assets/ModelMaterialAsset.cs index bb111fad..9a54e416 100644 --- a/zzre/assets/ModelMaterialAsset.cs +++ b/zzre/assets/ModelMaterialAsset.cs @@ -86,7 +86,7 @@ protected override void Unload() material = null; } - public override string ToString() => DebugName; + protected override string ToStringInner() => DebugName; protected abstract IReadOnlyList TextureBasePaths { get; } protected abstract void SetMaterialVariant(ModelMaterial material); diff --git a/zzre/assets/SamplerAsset.cs b/zzre/assets/SamplerAsset.cs index fa1ffe6e..7c5a9b1a 100644 --- a/zzre/assets/SamplerAsset.cs +++ b/zzre/assets/SamplerAsset.cs @@ -49,7 +49,7 @@ protected override void Unload() sampler = null; } - public override string ToString() => DebugName; + protected override string ToStringInner() => DebugName; } public static unsafe partial class AssetExtensions diff --git a/zzre/assets/SoundAsset.cs b/zzre/assets/SoundAsset.cs index c53e79af..fe92d71e 100644 --- a/zzre/assets/SoundAsset.cs +++ b/zzre/assets/SoundAsset.cs @@ -169,7 +169,7 @@ protected override void Unload() buffer = null; } - public override string ToString() => $"SoundAsset {info.FullPath}"; + protected override string ToStringInner() => $"SoundAsset {info.FullPath}"; } partial class AssetExtensions diff --git a/zzre/assets/TextureAsset.cs b/zzre/assets/TextureAsset.cs index b1c91d92..174069cf 100644 --- a/zzre/assets/TextureAsset.cs +++ b/zzre/assets/TextureAsset.cs @@ -130,7 +130,7 @@ protected override void Unload() texture = null; } - public override string ToString() => path.Parts.Count > 1 + protected override string ToStringInner() => path.Parts.Count > 1 ? $"Texture {path.Parts[^1]} ({path.Parts[^2]})" : $"Texture {path.ToPOSIXString()}"; } diff --git a/zzre/assets/UIBitmapAsset.cs b/zzre/assets/UIBitmapAsset.cs index d27c43a4..30b02041 100644 --- a/zzre/assets/UIBitmapAsset.cs +++ b/zzre/assets/UIBitmapAsset.cs @@ -162,7 +162,7 @@ protected override void Unload() material = null; } - public override string ToString() => DebugName; + protected override string ToStringInner() => DebugName; } partial class AssetExtensions diff --git a/zzre/assets/WorldAsset.cs b/zzre/assets/WorldAsset.cs index b7a40e02..4d7540de 100644 --- a/zzre/assets/WorldAsset.cs +++ b/zzre/assets/WorldAsset.cs @@ -36,7 +36,7 @@ protected override void Unload() mesh = null; } - public override string ToString() => $"World {info.FullPath.Parts[^1]}"; + protected override string ToStringInner() => $"World {info.FullPath.Parts[^1]}"; } public static unsafe partial class AssetExtensions diff --git a/zzre/tools/AssetExplorer.cs b/zzre/tools/AssetExplorer.cs index 7d4399b5..962f8971 100644 --- a/zzre/tools/AssetExplorer.cs +++ b/zzre/tools/AssetExplorer.cs @@ -117,14 +117,14 @@ private void HandleContentFor(IAssetRegistryDebug registry) { TableNextRow(); int i = 0; - if (TableSetColumnIndex(i++)) Text(asset.ID.ToString()); + if (TableSetColumnIndex(i++)) ImGuiEx.Text(asset.ID); if (TableSetColumnIndex(i++)) Text(asset.Type.Name); if (TableSetColumnIndex(i++)) if (Selectable(asset.Name, selectedRow == asset.ID, ImGuiSelectableFlags.SpanAllColumns)) selectedRow = asset.ID; - if (TableSetColumnIndex(i++)) Text(asset.State.ToString()); + if (TableSetColumnIndex(i++)) ImGuiEx.Text(asset.State); if (TableSetColumnIndex(i++)) Text(asset.RefCount.ToString()); - if (TableSetColumnIndex(i++)) Text(asset.Priority.ToString()); + if (TableSetColumnIndex(i++)) ImGuiEx.Text(asset.Priority); if (selectedRow == asset.ID && focusSelectedRow) { SetScrollHereY();