rimworld-blax-laws/Source/BlaxLaws/UI/ActIcons.cs

98 lines
3.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Reflection;
using HarmonyLib;
using RimWorld;
using UnityEngine;
using Verse;
namespace BlaxLaws
{
/// <summary>Textures the Laws tab uses, all vanilla.</summary>
[StaticConstructorOnStartup]
internal static class LawsTex
{
/// <summary>The overlap icon: vanilla's warning sign, or the info button when a texture pack lacks it.</summary>
public static readonly Texture2D Overlap = ContentFinder<Texture2D>.Get("UI/Widgets/Warning", false) ?? TexButton.Info;
public static readonly Texture2D InBook = Widgets.CheckboxOnTex;
}
/// <summary>
/// The icon of a catalogue entry (design: "Act picker"): the thing's icon for ate, used and bills, the
/// interaction symbol for interactions, nothing otherwise (the label carries it). Never throws into the
/// GUI: a broken modded icon logs once and draws nothing.
/// </summary>
public static class ActIcons
{
private static readonly Dictionary<InteractionDef, Texture2D> symbols = new Dictionary<InteractionDef, Texture2D>();
// InteractionDef.symbol is private; reading the path first keeps ContentFinder from logging an error
// for a modded interaction without one.
private static readonly FieldInfo SymbolPath = AccessTools.Field(typeof(InteractionDef), "symbol");
/// <summary>Whether the entry draws an icon.</summary>
public static bool HasIcon(ActEntry e)
{
switch (e?.def)
{
case ThingDef _:
return true;
case RecipeDef r:
return r.UIIconThing != null || r.UIIcon != null;
case InteractionDef i:
return Symbol(i) != null;
default:
return false;
}
}
public static void Draw(Rect rect, ActEntry e)
{
if (e?.def == null)
return;
try
{
switch (e.def)
{
case ThingDef t:
Widgets.DefIcon(rect, t);
break;
case RecipeDef r when r.UIIconThing != null || r.UIIcon != null:
Widgets.DefIcon(rect, r);
break;
case InteractionDef i:
Texture2D tex = Symbol(i);
if (tex != null)
GUI.DrawTexture(rect, tex);
break;
}
}
catch (Exception ex)
{
LawsLog.Once("Icon of " + e.key, ex);
}
}
private static Texture2D Symbol(InteractionDef def)
{
if (symbols.TryGetValue(def, out Texture2D tex))
return tex;
tex = null;
try
{
// Ideoligion and faction symbols depend on the initiator; the picker shows the def's own symbol only.
string path = SymbolPath?.GetValue(def) as string;
if (!path.NullOrEmpty() && def.symbolSource == InteractionSymbolSource.Def)
tex = def.GetSymbol();
}
catch (Exception ex)
{
LawsLog.Once("Symbol of " + def.defName, ex);
}
symbols[def] = tex;
return tex;
}
}
}