using System.Collections.Generic; using RimWorld; using UnityEngine; using Verse; namespace BlaxCastes { /// /// 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. /// public class Dialog_ManageCastes : Dialog_ManagePolicies { 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 NoCastes = new List(); private static Texture2D[] medCareIcons; public Dialog_ManageCastes() : this(null) { } /// Opens with the given caste selected, or with none selected when it is null. 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 ------------------------------------------------------------------ /// A new caste with the next free id, labelled "Caste N", managing nothing. protected override Caste CreateNewPolicy() => CasteManager.CreateCaste(); protected override List GetPolicies() => Comp?.Castes ?? NoCastes; protected override Caste GetDefaultPolicy() => Comp?.DefaultCaste; protected override void SetDefaultPolicy(Caste policy) => CasteManager.SetDefault(policy); /// Members become casteless and keep their policies; rows pointing at the caste fall back to No caste. 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); } /// The vanilla Assign tab's "Manage ... policies" text for the slot's editor. 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> PolicyMenu(Caste caste, CasteSlot slot) { yield return new Widgets.DropdownMenuElement { 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 { option = new FloatMenuOption(local.label, () => CasteManager.SetSlot(caste, slot, local)), payload = local, }; } } private static IEnumerable> MedCareMenu(Caste caste) { yield return new Widgets.DropdownMenuElement { 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 { option = new FloatMenuOption(care.GetLabel().CapitalizeFirst(), () => CasteManager.SetSlot(caste, CasteSlot.MedCare, care), MedCareIcon(care), Color.white), payload = i, }; } } /// The same icons the vanilla medical care button uses. Loaded on first draw. private static Texture2D MedCareIcon(MedicalCareCategory care) { if (medCareIcons == null) { medCareIcons = new[] { ContentFinder.Get("UI/Icons/Medical/NoCare", reportFailure: false), ContentFinder.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 -------------------------- /// The game's policies that fit a slot, in database order. Empty for MedCare. public static IEnumerable PoliciesFor(CasteSlot slot) { Game game = Current.Game; if (game == null) yield break; IEnumerable 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; } } /// /// What ticking the managed box sets: the database's default policy, or the colonist default /// medical care. Null when no game is loaded. /// 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; } } /// /// 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. /// 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 ---------------------------------------------------------------- /// /// 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. /// 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; } } }