using System;
using System.Collections.Generic;
using System.Reflection;
using HarmonyLib;
using RimWorld;
using UnityEngine;
using Verse;
namespace BlaxLaws
{
/// Textures the Laws tab uses, all vanilla.
[StaticConstructorOnStartup]
internal static class LawsTex
{
/// The overlap icon: vanilla's warning sign, or the info button when a texture pack lacks it.
public static readonly Texture2D Overlap = ContentFinder.Get("UI/Widgets/Warning", false) ?? TexButton.Info;
public static readonly Texture2D InBook = Widgets.CheckboxOnTex;
}
///
/// 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.
///
public static class ActIcons
{
private static readonly Dictionary symbols = new Dictionary();
// 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");
/// Whether the entry draws an icon.
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;
}
}
}