287 lines
13 KiB
C#
287 lines
13 KiB
C#
|
|
using System.Collections.Generic;
|
||
|
|
using RimWorld;
|
||
|
|
using UnityEngine;
|
||
|
|
using Verse;
|
||
|
|
|
||
|
|
namespace BlaxCastes
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// The caste manager, built on the vanilla policy manager: the base class draws the caste list and
|
||
|
|
/// handles new, duplicate, rename, delete (with its confirm) and make default. The right side has one
|
||
|
|
/// row per slot: a managed checkbox, a dropdown and a pencil that opens the vanilla policy editor.
|
||
|
|
/// Every change goes through CasteManager, so member sync and the delete rules always apply.
|
||
|
|
/// The base layout changed in 1.6, so DoContentsRect only draws inside the rect it is given.
|
||
|
|
/// </summary>
|
||
|
|
public class Dialog_ManageCastes : Dialog_ManagePolicies<Caste>
|
||
|
|
{
|
||
|
|
private const float RowHeight = 32f;
|
||
|
|
private const float RowGap = 6f;
|
||
|
|
private const float SlotLabelWidth = 110f;
|
||
|
|
private const float IconSize = 24f;
|
||
|
|
private const float Gap = 8f;
|
||
|
|
|
||
|
|
private static readonly List<Caste> NoCastes = new List<Caste>();
|
||
|
|
private static Texture2D[] medCareIcons;
|
||
|
|
|
||
|
|
public Dialog_ManageCastes() : this(null)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Opens with the given caste selected, or with none selected when it is null.</summary>
|
||
|
|
public Dialog_ManageCastes(Caste caste) : base(caste)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
protected override string TitleKey => "BlaxCastes_ManageCastesTitle";
|
||
|
|
|
||
|
|
protected override string TipKey => "BlaxCastes_ManageCastesTip";
|
||
|
|
|
||
|
|
public override Vector2 InitialSize => new Vector2(700f, 540f);
|
||
|
|
|
||
|
|
private static GameComponent_BlaxCastes Comp => GameComponent_BlaxCastes.Instance;
|
||
|
|
|
||
|
|
public override void DoWindowContents(Rect inRect)
|
||
|
|
{
|
||
|
|
GameComponent_BlaxCastes comp = Comp;
|
||
|
|
// A policy deleted in a vanilla editor opened from here must not stay in a caste slot.
|
||
|
|
CasteManager.CheckPolicies(comp);
|
||
|
|
if (SelectedPolicy != null && (comp == null || !comp.HasCaste(SelectedPolicy)))
|
||
|
|
SelectedPolicy = null;
|
||
|
|
base.DoWindowContents(inRect);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- base class hooks ------------------------------------------------------------------
|
||
|
|
|
||
|
|
/// <summary>A new caste with the next free id, labelled "Caste N", managing nothing.</summary>
|
||
|
|
protected override Caste CreateNewPolicy() => CasteManager.CreateCaste();
|
||
|
|
|
||
|
|
protected override List<Caste> GetPolicies() => Comp?.Castes ?? NoCastes;
|
||
|
|
|
||
|
|
protected override Caste GetDefaultPolicy() => Comp?.DefaultCaste;
|
||
|
|
|
||
|
|
protected override void SetDefaultPolicy(Caste policy) => CasteManager.SetDefault(policy);
|
||
|
|
|
||
|
|
/// <summary>Members become casteless and keep their policies; rows pointing at the caste fall back to No caste.</summary>
|
||
|
|
protected override AcceptanceReport TryDeletePolicy(Caste policy)
|
||
|
|
{
|
||
|
|
if (CasteManager.DeleteCaste(policy))
|
||
|
|
return AcceptanceReport.WasAccepted;
|
||
|
|
return "BlaxCastes_DeleteFailed".Translate(policy?.label ?? "");
|
||
|
|
}
|
||
|
|
|
||
|
|
protected override void DoContentsRect(Rect rect)
|
||
|
|
{
|
||
|
|
Caste caste = SelectedPolicy;
|
||
|
|
if (caste == null)
|
||
|
|
return;
|
||
|
|
float y = rect.y;
|
||
|
|
foreach (CasteSlot slot in CasteUtility.AllSlots)
|
||
|
|
{
|
||
|
|
DoSlotRow(new Rect(rect.x, y, rect.width, RowHeight), caste, slot);
|
||
|
|
y += RowHeight + RowGap;
|
||
|
|
}
|
||
|
|
y += Gap;
|
||
|
|
if (y < rect.yMax)
|
||
|
|
{
|
||
|
|
using (new TextBlock(GameFont.Tiny, Color.gray))
|
||
|
|
Widgets.Label(new Rect(rect.x, y, rect.width, rect.yMax - y), "BlaxCastes_ManagerHint".Translate());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- slot rows -----------------------------------------------------------------------
|
||
|
|
|
||
|
|
private static void DoSlotRow(Rect row, Caste caste, CasteSlot slot)
|
||
|
|
{
|
||
|
|
float iconY = row.y + (row.height - IconSize) / 2f;
|
||
|
|
Rect labelRect = new Rect(row.x, row.y, SlotLabelWidth, row.height);
|
||
|
|
Rect checkRect = new Rect(labelRect.xMax, iconY, IconSize, IconSize);
|
||
|
|
Rect pencilRect = new Rect(row.xMax - IconSize, iconY, IconSize, IconSize);
|
||
|
|
Rect dropRect = new Rect(checkRect.xMax + Gap, row.y + 2f, 0f, row.height - 4f);
|
||
|
|
dropRect.xMax = pencilRect.x - Gap;
|
||
|
|
|
||
|
|
if (Mouse.IsOver(row))
|
||
|
|
Widgets.DrawHighlight(row);
|
||
|
|
|
||
|
|
using (new TextBlock(TextAnchor.MiddleLeft))
|
||
|
|
Widgets.Label(labelRect, CasteUtility.SlotLabel(slot).Truncate(labelRect.width));
|
||
|
|
|
||
|
|
bool managed = caste.IsManaged(slot);
|
||
|
|
bool wasManaged = managed;
|
||
|
|
Widgets.Checkbox(checkRect.x, checkRect.y, ref managed, IconSize);
|
||
|
|
TooltipHandler.TipRegion(checkRect, "BlaxCastes_ManagedTip".Translate());
|
||
|
|
if (managed != wasManaged)
|
||
|
|
SetManaged(caste, slot, managed);
|
||
|
|
|
||
|
|
string valueLabel = CasteUtility.ValueLabel(slot, caste.GetSlot(slot)).CapitalizeFirst();
|
||
|
|
if (slot == CasteSlot.MedCare)
|
||
|
|
{
|
||
|
|
Widgets.Dropdown(dropRect, caste, c => c.MedCare, c => MedCareMenu(c), valueLabel.Truncate(dropRect.width));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
Widgets.Dropdown(dropRect, caste, c => c.GetSlot(slot) as Policy, c => PolicyMenu(c, slot), valueLabel.Truncate(dropRect.width));
|
||
|
|
if (Widgets.ButtonImage(pencilRect, TexButton.Rename, tooltip: EditorTip(slot)))
|
||
|
|
OpenPolicyEditor(slot, caste.GetSlot(slot) as Policy);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>The vanilla Assign tab's "Manage ... policies" text for the slot's editor.</summary>
|
||
|
|
private static string EditorTip(CasteSlot slot)
|
||
|
|
{
|
||
|
|
switch (slot)
|
||
|
|
{
|
||
|
|
case CasteSlot.Apparel: return "ManageApparelPolicies".Translate();
|
||
|
|
case CasteSlot.Food: return "ManageFoodPolicies".Translate();
|
||
|
|
case CasteSlot.Drug: return "ManageDrugPolicies".Translate();
|
||
|
|
case CasteSlot.Reading: return "ManageReadingPolicies".Translate();
|
||
|
|
default: return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static IEnumerable<Widgets.DropdownMenuElement<Policy>> PolicyMenu(Caste caste, CasteSlot slot)
|
||
|
|
{
|
||
|
|
yield return new Widgets.DropdownMenuElement<Policy>
|
||
|
|
{
|
||
|
|
option = new FloatMenuOption("BlaxCastes_Unmanaged".Translate(), () => CasteManager.SetSlot(caste, slot, null)),
|
||
|
|
payload = null,
|
||
|
|
};
|
||
|
|
foreach (Policy policy in PoliciesFor(slot))
|
||
|
|
{
|
||
|
|
Policy local = policy;
|
||
|
|
yield return new Widgets.DropdownMenuElement<Policy>
|
||
|
|
{
|
||
|
|
option = new FloatMenuOption(local.label, () => CasteManager.SetSlot(caste, slot, local)),
|
||
|
|
payload = local,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static IEnumerable<Widgets.DropdownMenuElement<int>> MedCareMenu(Caste caste)
|
||
|
|
{
|
||
|
|
yield return new Widgets.DropdownMenuElement<int>
|
||
|
|
{
|
||
|
|
option = new FloatMenuOption("BlaxCastes_Unmanaged".Translate(), () => CasteManager.SetSlot(caste, CasteSlot.MedCare, null)),
|
||
|
|
payload = Caste.Unmanaged,
|
||
|
|
};
|
||
|
|
for (int i = (int)MedicalCareCategory.NoCare; i <= (int)MedicalCareCategory.Best; i++)
|
||
|
|
{
|
||
|
|
var care = (MedicalCareCategory)i;
|
||
|
|
yield return new Widgets.DropdownMenuElement<int>
|
||
|
|
{
|
||
|
|
option = new FloatMenuOption(care.GetLabel().CapitalizeFirst(), () => CasteManager.SetSlot(caste, CasteSlot.MedCare, care), MedCareIcon(care), Color.white),
|
||
|
|
payload = i,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>The same icons the vanilla medical care button uses. Loaded on first draw.</summary>
|
||
|
|
private static Texture2D MedCareIcon(MedicalCareCategory care)
|
||
|
|
{
|
||
|
|
if (medCareIcons == null)
|
||
|
|
{
|
||
|
|
medCareIcons = new[]
|
||
|
|
{
|
||
|
|
ContentFinder<Texture2D>.Get("UI/Icons/Medical/NoCare", reportFailure: false),
|
||
|
|
ContentFinder<Texture2D>.Get("UI/Icons/Medical/NoMeds", reportFailure: false),
|
||
|
|
ThingDefOf.MedicineHerbal?.uiIcon,
|
||
|
|
ThingDefOf.MedicineIndustrial?.uiIcon,
|
||
|
|
ThingDefOf.MedicineUltratech?.uiIcon,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
int i = (int)care;
|
||
|
|
return i >= 0 && i < medCareIcons.Length ? medCareIcons[i] : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void OpenPolicyEditor(CasteSlot slot, Policy policy)
|
||
|
|
{
|
||
|
|
switch (slot)
|
||
|
|
{
|
||
|
|
case CasteSlot.Apparel:
|
||
|
|
Find.WindowStack.Add(new Dialog_ManageApparelPolicies(policy as ApparelPolicy));
|
||
|
|
break;
|
||
|
|
case CasteSlot.Food:
|
||
|
|
Find.WindowStack.Add(new Dialog_ManageFoodPolicies(policy as FoodPolicy));
|
||
|
|
break;
|
||
|
|
case CasteSlot.Drug:
|
||
|
|
Find.WindowStack.Add(new Dialog_ManageDrugPolicies(policy as DrugPolicy));
|
||
|
|
break;
|
||
|
|
case CasteSlot.Reading:
|
||
|
|
Find.WindowStack.Add(new Dialog_ManageReadingPolicies(policy as ReadingPolicy));
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- helpers the rows use; public so the tests can drive them --------------------------
|
||
|
|
|
||
|
|
/// <summary>The game's policies that fit a slot, in database order. Empty for MedCare.</summary>
|
||
|
|
public static IEnumerable<Policy> PoliciesFor(CasteSlot slot)
|
||
|
|
{
|
||
|
|
Game game = Current.Game;
|
||
|
|
if (game == null)
|
||
|
|
yield break;
|
||
|
|
IEnumerable<Policy> source;
|
||
|
|
switch (slot)
|
||
|
|
{
|
||
|
|
case CasteSlot.Apparel: source = game.outfitDatabase?.AllOutfits; break;
|
||
|
|
case CasteSlot.Food: source = game.foodRestrictionDatabase?.AllFoodRestrictions; break;
|
||
|
|
case CasteSlot.Drug: source = game.drugPolicyDatabase?.AllPolicies; break;
|
||
|
|
case CasteSlot.Reading: source = game.readingPolicyDatabase?.AllReadingPolicies; break;
|
||
|
|
default: source = null; break;
|
||
|
|
}
|
||
|
|
if (source == null)
|
||
|
|
yield break;
|
||
|
|
foreach (Policy p in source)
|
||
|
|
{
|
||
|
|
if (p != null)
|
||
|
|
yield return p;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// What ticking the managed box sets: the database's default policy, or the colonist default
|
||
|
|
/// medical care. Null when no game is loaded.
|
||
|
|
/// </summary>
|
||
|
|
public static object DefaultValue(CasteSlot slot)
|
||
|
|
{
|
||
|
|
Game game = Current.Game;
|
||
|
|
if (game == null)
|
||
|
|
return null;
|
||
|
|
switch (slot)
|
||
|
|
{
|
||
|
|
case CasteSlot.Apparel: return game.outfitDatabase?.DefaultOutfit();
|
||
|
|
case CasteSlot.Food: return game.foodRestrictionDatabase?.DefaultFoodRestriction();
|
||
|
|
case CasteSlot.Drug: return game.drugPolicyDatabase?.DefaultDrugPolicy();
|
||
|
|
case CasteSlot.Reading: return game.readingPolicyDatabase?.DefaultReadingPolicy();
|
||
|
|
case CasteSlot.MedCare: return (int)(game.playSettings?.defaultCareForColonist ?? MedicalCareCategory.Best);
|
||
|
|
default: return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// The managed checkbox. Ticking a slot sets the default value (members follow, see
|
||
|
|
/// CasteManager.SetSlot); unticking leaves every pawn as it is. Returns how many members moved.
|
||
|
|
/// </summary>
|
||
|
|
public static int SetManaged(Caste caste, CasteSlot slot, bool managed)
|
||
|
|
{
|
||
|
|
if (caste == null || caste.IsManaged(slot) == managed)
|
||
|
|
return 0;
|
||
|
|
return CasteManager.SetSlot(caste, slot, managed ? DefaultValue(slot) : null);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- first-load message ----------------------------------------------------------------
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Posts the message that points to Assign > Manage castes. The game component calls this once
|
||
|
|
/// per save and stores the result. Returns false (try again on the next load) while the Caste
|
||
|
|
/// column is turned off, because the message would point to a button that is not there.
|
||
|
|
/// </summary>
|
||
|
|
public static bool ShowFirstLoadMessage()
|
||
|
|
{
|
||
|
|
if (BlaxCastesMod.Settings != null && !BlaxCastesMod.Settings.showAssignColumn)
|
||
|
|
return false;
|
||
|
|
string text = "BlaxCastes_FirstLoadMessage".Translate();
|
||
|
|
LongEventHandler.ExecuteWhenFinished(() => Messages.Message(text, MessageTypeDefOf.NeutralEvent));
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|