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
{
/// The pawn has no membership record. Nothing in the menu matches it, so painting from it does nothing.
None,
Casteless,
Caste,
Reset,
Edit,
}
///
/// 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.
///
public readonly struct CasteChoice : IEquatable
{
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();
}
/// One entry of the cell menu. A null Action means the entry is shown but disabled.
public sealed class CasteMenuEntry
{
public CasteChoice Choice;
public string Label;
public Action Action;
}
///
/// 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.
///
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);
}
///
/// 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".
///
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;
}
/// The cell tooltip: the state, every overridden slot with both values, and how to use the cell.
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 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();
}
/// True while the pawn waits in the join queue for the next tick.
public static bool IsPending(Pawn pawn) => JoinDefaults.IsQueued(pawn);
// ---- menu and painting -------------------------------------------------------------------
/// What the cell currently holds, as a dropdown payload.
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);
}
///
/// 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.
///
public static List MenuEntries(Pawn pawn)
{
var entries = new List();
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> MenuFor(Pawn pawn)
{
foreach (CasteMenuEntry entry in MenuEntries(pawn))
{
yield return new Widgets.DropdownMenuElement
{
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));
/// By caste label, then id; pawns without a caste last.
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);
}
}
}