267 lines
12 KiB
C#
267 lines
12 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Text;
|
||
|
|
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 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.
|
||
|
|
/// </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;
|
||
|
|
|
||
|
|
private static GameComponent_BlaxCastes Comp => GameComponent_BlaxCastes.Instance;
|
||
|
|
|
||
|
|
// ---- header ------------------------------------------------------------------------------
|
||
|
|
|
||
|
|
public override void DoHeader(Rect rect, PawnTable table)
|
||
|
|
{
|
||
|
|
base.DoHeader(rect, table);
|
||
|
|
MouseoverSounds.DoRegion(rect);
|
||
|
|
// Two stacked buttons above the 25 px sort area at the bottom of the header.
|
||
|
|
float top = rect.y + (rect.height - TopAreaHeight);
|
||
|
|
Rect manage = new Rect(rect.x, top, rect.width, HeaderButtonHeight);
|
||
|
|
Rect 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 (Widgets.ButtonText(defaults, "BlaxCastes_JoinDefaultsButton".Translate().Truncate(defaults.width - 4f)))
|
||
|
|
Find.WindowStack.Add(new Dialog_JoinDefaults());
|
||
|
|
}
|
||
|
|
TooltipHandler.TipRegion(manage, "BlaxCastes_ManageCastesButtonTip".Translate());
|
||
|
|
TooltipHandler.TipRegion(defaults, "BlaxCastes_JoinDefaultsButtonTip".Translate());
|
||
|
|
}
|
||
|
|
|
||
|
|
public override int GetMinHeaderHeight(PawnTable table) => Mathf.Max(base.GetMinHeaderHeight(table), TopAreaHeight);
|
||
|
|
|
||
|
|
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 join is queued, 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("BlaxCastes_CellCastelessTip".Translate());
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
sb.AppendLine("BlaxCastes_CellCasteTip".Translate(m.caste.label));
|
||
|
|
List<CasteSlot> overrides = CasteManager.Overrides(pawn);
|
||
|
|
if (overrides.Count > 0)
|
||
|
|
{
|
||
|
|
sb.AppendLine();
|
||
|
|
sb.AppendLine("BlaxCastes_CellOverrides".Translate());
|
||
|
|
foreach (CasteSlot slot in overrides)
|
||
|
|
{
|
||
|
|
string mine = CasteUtility.ValueLabel(slot, CasteUtility.CurrentValue(pawn, slot));
|
||
|
|
string casteValue = CasteUtility.ValueLabel(slot, m.caste.GetSlot(slot));
|
||
|
|
sb.AppendLine(" " + "BlaxCastes_CellOverrideLine".Translate(CasteUtility.SlotLabel(slot), mine, casteValue));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
sb.AppendLine();
|
||
|
|
sb.Append("BlaxCastes_CellHint".Translate());
|
||
|
|
return sb.ToString();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>True while the pawn waits in the join queue for the next tick.</summary>
|
||
|
|
public static bool IsPending(Pawn pawn) => JoinDefaults.IsQueued(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))
|
||
|
|
{
|
||
|
|
yield return new Widgets.DropdownMenuElement<CasteChoice>
|
||
|
|
{
|
||
|
|
option = new FloatMenuOption(entry.Label, entry.Action),
|
||
|
|
payload = entry.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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|