360 lines
17 KiB
C#
360 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using HarmonyLib;
|
|
using RimWorld;
|
|
using UnityEngine;
|
|
using Verse;
|
|
using Verse.Sound;
|
|
|
|
namespace BlaxCastes
|
|
{
|
|
public enum CasteChoiceKind
|
|
{
|
|
/// <summary>The pawn has no membership record. Nothing in the menu matches it, so painting from it does nothing.</summary>
|
|
None,
|
|
Casteless,
|
|
Caste,
|
|
Reset,
|
|
Edit,
|
|
}
|
|
|
|
/// <summary>
|
|
/// The dropdown payload of the Caste column. Painting copies the source cell's payload onto every
|
|
/// cell it passes and runs the menu entry with the same payload, so Reset and Edit get kinds of their
|
|
/// own and a pawn without a record paints nothing.
|
|
/// </summary>
|
|
public readonly struct CasteChoice : IEquatable<CasteChoice>
|
|
{
|
|
public readonly CasteChoiceKind Kind;
|
|
public readonly Caste Caste;
|
|
|
|
public CasteChoice(CasteChoiceKind kind, Caste caste = null)
|
|
{
|
|
Kind = kind;
|
|
Caste = kind == CasteChoiceKind.Caste ? caste : null;
|
|
}
|
|
|
|
public bool Equals(CasteChoice other) => Kind == other.Kind && ReferenceEquals(Caste, other.Caste);
|
|
|
|
public override bool Equals(object obj) => obj is CasteChoice other && Equals(other);
|
|
|
|
public override int GetHashCode() => ((int)Kind * 397) ^ (Caste?.id ?? 0);
|
|
|
|
public override string ToString() => Kind == CasteChoiceKind.Caste ? "Caste(" + Caste?.label + ")" : Kind.ToString();
|
|
}
|
|
|
|
/// <summary>One entry of the cell menu. A null Action means the entry is shown but disabled.</summary>
|
|
public sealed class CasteMenuEntry
|
|
{
|
|
public CasteChoice Choice;
|
|
public string Label;
|
|
public Action Action;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The Caste column in the Assign tab, modelled on the vanilla apparel policy column. A cell shows
|
|
/// the pawn's caste ("*" when a managed slot differs from the caste), "(pending)" while a join that changes its caste waits
|
|
/// for the next tick, or "Unchangeable" for quest lodgers. Click to pick, or drag down to paint.
|
|
/// The header holds the "Manage castes" and "Defaults" buttons in the same 65 px area the apparel
|
|
/// column uses for its button. In the Work tab the header stays as tall as the work columns' (50 px),
|
|
/// so their label guide lines still reach the cells; there only "Manage castes" sits above the label.
|
|
/// </summary>
|
|
public class PawnColumnWorker_BlaxCaste : PawnColumnWorker
|
|
{
|
|
public const string DefName = "BlaxCastes_Caste";
|
|
public const int TopAreaHeight = 65;
|
|
public const int MinCellWidth = 100;
|
|
public const int OptimalCellWidth = 140;
|
|
public const int MaxCellWidth = 160;
|
|
|
|
private const float HeaderButtonHeight = 19f;
|
|
private const float HeaderButtonGap = 2f;
|
|
/// <summary>The bottom of the header the base class makes clickable for sorting (PawnColumnWorker.GetInteractableHeaderRect).</summary>
|
|
private const int SortAreaHeight = 25;
|
|
|
|
private static GameComponent_BlaxCastes Comp => GameComponent_BlaxCastes.Instance;
|
|
|
|
// ---- header ------------------------------------------------------------------------------
|
|
|
|
public override void DoHeader(Rect rect, PawnTable table)
|
|
{
|
|
base.DoHeader(rect, table);
|
|
MouseoverSounds.DoRegion(rect);
|
|
Rect manage;
|
|
Rect defaults = default;
|
|
bool compact = IsWorkTable(table);
|
|
if (compact)
|
|
{
|
|
// One button right above the label and the sort area, inside the work columns' 50 px.
|
|
manage = new Rect(rect.x, rect.yMax - CompactBottom(table) - HeaderButtonGap - HeaderButtonHeight, rect.width, HeaderButtonHeight);
|
|
}
|
|
else
|
|
{
|
|
// Two stacked buttons above the 25 px sort area at the bottom of the header.
|
|
float top = rect.y + (rect.height - TopAreaHeight);
|
|
manage = new Rect(rect.x, top, rect.width, HeaderButtonHeight);
|
|
defaults = new Rect(rect.x, manage.yMax + HeaderButtonGap, rect.width, HeaderButtonHeight);
|
|
}
|
|
using (new TextBlock(GameFont.Tiny))
|
|
{
|
|
if (Widgets.ButtonText(manage, "BlaxCastes_ManageCastesButton".Translate().Truncate(manage.width - 4f)))
|
|
Find.WindowStack.Add(new Dialog_ManageCastes());
|
|
if (!compact && Widgets.ButtonText(defaults, "BlaxCastes_JoinDefaultsButton".Translate().Truncate(defaults.width - 4f)))
|
|
Find.WindowStack.Add(new Dialog_JoinDefaults());
|
|
}
|
|
TooltipHandler.TipRegion(manage, "BlaxCastes_ManageCastesButtonTip".Translate());
|
|
if (!compact)
|
|
TooltipHandler.TipRegion(defaults, "BlaxCastes_JoinDefaultsButtonTip".Translate());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 65 px for the two buttons, as the outfit column. In the Work tab only the label and one button,
|
|
/// which fit in the work columns' 50 px, so the header does not grow there.
|
|
/// </summary>
|
|
public override int GetMinHeaderHeight(PawnTable table)
|
|
{
|
|
if (IsWorkTable(table))
|
|
return CompactBottom(table) + Mathf.CeilToInt(HeaderButtonGap * 2f + HeaderButtonHeight);
|
|
return Mathf.Max(base.GetMinHeaderHeight(table), TopAreaHeight);
|
|
}
|
|
|
|
/// <summary>What the compact header keeps free at its bottom: the label, at least the 25 px sort area the base class makes clickable.</summary>
|
|
private int CompactBottom(PawnTable table) => Mathf.Max(base.GetMinHeaderHeight(table), SortAreaHeight);
|
|
|
|
private static AccessTools.FieldRef<PawnTable, PawnTableDef> tableDefField;
|
|
private static bool tableDefFieldFailed;
|
|
|
|
/// <summary>The table's def (PawnTable keeps it private), or null if it cannot be read.</summary>
|
|
public static PawnTableDef TableDefOf(PawnTable table)
|
|
{
|
|
if (table == null || tableDefFieldFailed)
|
|
return null;
|
|
try
|
|
{
|
|
tableDefField ??= AccessTools.FieldRefAccess<PawnTable, PawnTableDef>("def");
|
|
return tableDefField(table);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
tableDefFieldFailed = true;
|
|
Log.Warning("[Blax Castes] Could not read a pawn table's def; the Caste column uses its full header everywhere: " + e.Message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>The Work tab's table, where the header keeps the work columns' height.</summary>
|
|
public static bool IsWorkTable(PawnTable table) => TableDefOf(table) == PawnTableDefOf.Work;
|
|
|
|
public override int GetMinWidth(PawnTable table) => Mathf.Max(base.GetMinWidth(table), MinCellWidth);
|
|
|
|
public override int GetMaxWidth(PawnTable table) => Mathf.Max(GetMinWidth(table), Mathf.Min(base.GetMaxWidth(table), MaxCellWidth));
|
|
|
|
public override int GetOptimalWidth(PawnTable table) => Mathf.Clamp(OptimalCellWidth, GetMinWidth(table), GetMaxWidth(table));
|
|
|
|
// ---- cell --------------------------------------------------------------------------------
|
|
|
|
public override void DoCell(Rect rect, Pawn pawn, PawnTable table)
|
|
{
|
|
if (Comp == null || !CasteUtility.CanBeMember(pawn))
|
|
return;
|
|
Rect cell = new Rect(rect.x, rect.y + 2f, rect.width - 4f, rect.height - 4f);
|
|
string label = CellLabel(pawn);
|
|
if (pawn.IsQuestLodger())
|
|
{
|
|
using (new TextBlock(TextAnchor.MiddleCenter))
|
|
Widgets.Label(cell, label.Truncate(cell.width));
|
|
TooltipHandler.TipRegion(cell, "BlaxCastes_CellLodgerTip".Translate());
|
|
return;
|
|
}
|
|
string dragLabel = Comp.CasteOf(pawn)?.label ?? label;
|
|
Widgets.Dropdown(cell, pawn, ChoiceOf, MenuFor, label.Truncate(cell.width), dragLabel: dragLabel, paintable: true);
|
|
if (Mouse.IsOver(cell))
|
|
TooltipHandler.TipRegion(cell, () => CellTip(pawn), pawn.thingIDNumber ^ 0x2B1A5C);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The cell text: "Unchangeable" for quest lodgers, "(pending)" while a queued join will change the caste, the caste
|
|
/// label with "*" when a managed slot differs, "No caste" when casteless by choice, else "None".
|
|
/// </summary>
|
|
public static string CellLabel(Pawn pawn)
|
|
{
|
|
if (pawn == null)
|
|
return "";
|
|
if (pawn.IsQuestLodger())
|
|
return "Unchangeable".Translate();
|
|
if (IsPending(pawn))
|
|
return "BlaxCastes_CellPending".Translate();
|
|
CasteMembership m = Comp?.MembershipOf(pawn);
|
|
if (m == null)
|
|
return "BlaxCastes_CellNone".Translate();
|
|
if (m.caste == null)
|
|
return "BlaxCastes_NoCaste".Translate();
|
|
return CasteManager.Overrides(pawn).Count > 0 ? m.caste.label + "*" : m.caste.label;
|
|
}
|
|
|
|
/// <summary>The cell tooltip: the state, every overridden slot with both values, and how to use the cell.</summary>
|
|
public static string CellTip(Pawn pawn)
|
|
{
|
|
var sb = new StringBuilder();
|
|
CasteMembership m = Comp?.MembershipOf(pawn);
|
|
if (IsPending(pawn))
|
|
sb.AppendLine("BlaxCastes_CellPendingTip".Translate());
|
|
if (m == null)
|
|
{
|
|
sb.AppendLine("BlaxCastes_CellNoneTip".Translate());
|
|
}
|
|
else if (m.caste == null)
|
|
{
|
|
sb.AppendLine(m.reason == JoinReason.CasteDeleted
|
|
? "BlaxCastes_CellCasteDeletedTip".Translate()
|
|
: "BlaxCastes_CellCastelessTip".Translate());
|
|
}
|
|
else
|
|
{
|
|
sb.AppendLine("BlaxCastes_CellCasteTip".Translate(m.caste.label));
|
|
if (StarterCastes.IsSlaveOutfit(m.caste.ApparelPolicy))
|
|
sb.AppendLine("BlaxCastes_SlaveOutfitCold".Translate());
|
|
List<CasteSlot> overrides = CasteManager.Overrides(pawn);
|
|
if (overrides.Count > 0)
|
|
{
|
|
sb.AppendLine();
|
|
sb.AppendLine("BlaxCastes_CellOverrides".Translate());
|
|
foreach (CasteSlot slot in overrides)
|
|
sb.AppendLine(" " + OverrideLine(pawn, m.caste, slot));
|
|
}
|
|
}
|
|
sb.AppendLine();
|
|
sb.Append("BlaxCastes_CellHint".Translate());
|
|
return sb.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// One overridden slot in the cell tooltip: the slot with both values; "Schedule" alone (24 hours do
|
|
/// not fit a line); the work types that differ, each with both priorities.
|
|
/// </summary>
|
|
public static string OverrideLine(Pawn pawn, Caste caste, CasteSlot slot)
|
|
{
|
|
switch (slot)
|
|
{
|
|
case CasteSlot.Schedule:
|
|
return "BlaxCastes_CellOverrideSchedule".Translate(CasteUtility.SlotLabel(slot));
|
|
case CasteSlot.Work:
|
|
IEnumerable<string> types = CasteManager.WorkOverrides(pawn).Select(def => (string)"BlaxCastes_CellOverrideWorkType".Translate(
|
|
WorkSlot.TypeLabel(def), WorkSlot.PriorityLabel(WorkSlot.Effective(pawn.workSettings.GetPriority(def))),
|
|
WorkSlot.PriorityLabel(caste.WorkPriorityOf(def.defName))));
|
|
return "BlaxCastes_CellOverrideWork".Translate(CasteUtility.SlotLabel(slot), string.Join(", ", types));
|
|
default:
|
|
string mine = CasteUtility.ValueLabel(slot, CasteUtility.CurrentValue(pawn, slot));
|
|
string casteValue = CasteUtility.ValueLabel(slot, caste.GetSlot(slot));
|
|
return "BlaxCastes_CellOverrideLine".Translate(CasteUtility.SlotLabel(slot), mine, casteValue);
|
|
}
|
|
}
|
|
|
|
/// <summary>True while the pawn waits for a join that will change its caste on the next tick.</summary>
|
|
public static bool IsPending(Pawn pawn) => JoinDefaults.WillChange(pawn);
|
|
|
|
// ---- menu and painting -------------------------------------------------------------------
|
|
|
|
/// <summary>What the cell currently holds, as a dropdown payload.</summary>
|
|
public static CasteChoice ChoiceOf(Pawn pawn)
|
|
{
|
|
CasteMembership m = Comp?.MembershipOf(pawn);
|
|
if (m == null)
|
|
return new CasteChoice(CasteChoiceKind.None);
|
|
return m.caste == null ? new CasteChoice(CasteChoiceKind.Casteless) : new CasteChoice(CasteChoiceKind.Caste, m.caste);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The menu entries in order: No caste (casteless by choice), every caste by label, Reset to caste
|
|
/// (disabled without a caste) and Edit caste. Building them needs no GUI.
|
|
/// </summary>
|
|
public static List<CasteMenuEntry> MenuEntries(Pawn pawn)
|
|
{
|
|
var entries = new List<CasteMenuEntry>();
|
|
GameComponent_BlaxCastes comp = Comp;
|
|
if (comp == null || pawn == null)
|
|
return entries;
|
|
entries.Add(new CasteMenuEntry
|
|
{
|
|
Choice = new CasteChoice(CasteChoiceKind.Casteless),
|
|
Label = "BlaxCastes_NoCaste".Translate(),
|
|
Action = () => CasteManager.MakeCasteless(pawn),
|
|
});
|
|
foreach (Caste caste in comp.Castes.Where(c => c != null).OrderBy(c => c.label, StringComparer.CurrentCultureIgnoreCase).ThenBy(c => c.id))
|
|
{
|
|
Caste local = caste;
|
|
entries.Add(new CasteMenuEntry
|
|
{
|
|
Choice = new CasteChoice(CasteChoiceKind.Caste, local),
|
|
Label = local.label,
|
|
Action = () => CasteManager.Assign(pawn, local, false, JoinReason.Manual),
|
|
});
|
|
}
|
|
Caste current = comp.CasteOf(pawn);
|
|
entries.Add(new CasteMenuEntry
|
|
{
|
|
Choice = new CasteChoice(CasteChoiceKind.Reset),
|
|
Label = "BlaxCastes_MenuReset".Translate(),
|
|
Action = current == null ? null : () => CasteManager.ResetToCaste(pawn),
|
|
});
|
|
entries.Add(new CasteMenuEntry
|
|
{
|
|
Choice = new CasteChoice(CasteChoiceKind.Edit),
|
|
Label = "BlaxCastes_MenuEdit".Translate() + "...",
|
|
Action = () => Find.WindowStack.Add(new Dialog_ManageCastes(comp.CasteOf(pawn))),
|
|
});
|
|
return entries;
|
|
}
|
|
|
|
private static IEnumerable<Widgets.DropdownMenuElement<CasteChoice>> MenuFor(Pawn pawn)
|
|
{
|
|
foreach (CasteMenuEntry entry in MenuEntries(pawn))
|
|
{
|
|
CasteMenuEntry local = entry;
|
|
Action action = local.Action == null ? null : () =>
|
|
{
|
|
if (!SkipWhilePainting(pawn, local.Choice, Widgets.Painting))
|
|
local.Action();
|
|
};
|
|
yield return new Widgets.DropdownMenuElement<CasteChoice>
|
|
{
|
|
option = new FloatMenuOption(local.Label, action),
|
|
payload = local.Choice,
|
|
};
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Painting runs the matching menu entry on every GUI event over a cell, the start cell included.
|
|
/// Assigning rewrites every managed slot, so a pawn already in the painted caste (or already
|
|
/// casteless) is skipped while painting, and its hand changes stay. A click still rewrites.
|
|
/// </summary>
|
|
public static bool SkipWhilePainting(Pawn pawn, CasteChoice choice, bool painting)
|
|
{
|
|
if (!painting || pawn == null)
|
|
return false;
|
|
CasteChoice current = ChoiceOf(pawn);
|
|
return (choice.Kind == CasteChoiceKind.Caste || choice.Kind == CasteChoiceKind.Casteless) && current.Equals(choice);
|
|
}
|
|
|
|
// ---- sorting -----------------------------------------------------------------------------
|
|
|
|
public override int Compare(Pawn a, Pawn b) => CompareCastes(Comp?.CasteOf(a), Comp?.CasteOf(b));
|
|
|
|
/// <summary>By caste label, then id; pawns without a caste last.</summary>
|
|
public static int CompareCastes(Caste x, Caste y)
|
|
{
|
|
if (ReferenceEquals(x, y))
|
|
return 0;
|
|
if (x == null)
|
|
return 1;
|
|
if (y == null)
|
|
return -1;
|
|
int byLabel = string.Compare(x.label, y.label, StringComparison.CurrentCultureIgnoreCase);
|
|
return byLabel != 0 ? byLabel : x.id.CompareTo(y.id);
|
|
}
|
|
}
|
|
}
|