using System; using System.Collections.Generic; using System.Linq; using System.Text; using RimWorld; using Verse; namespace BlaxDefaults { /// The four entries of the Defaults menu, in menu order. public enum StorageMenuEntry { MakeDefault, LoadDefault, LoadGameDefault, Forget, } /// /// The Defaults button's menu. Work package "tab" (design.md: "Defaults button in the storage tab"). /// Entries: "Make default for new shelves" (always naming the kind), "Load your default", "Load the game's /// default", "Forget the default for new shelves" (asks first). On a stockpile each entry opens a submenu /// of StorageKinds.StockpileTypes. Load your default = kind.LoadDefault (the game's default, then the /// template); Load the game's default = kind.LoadGameDefault. A linked shelf (StorageGroup) gives its first /// member's def, and Load writes the group's settings. /// public static class StorageDefaultsMenu { /// The entries in menu order. public static readonly StorageMenuEntry[] Entries = { StorageMenuEntry.MakeDefault, StorageMenuEntry.LoadDefault, StorageMenuEntry.LoadGameDefault, StorageMenuEntry.Forget, }; /// /// The kinds this parent offers: every stockpile type for a Zone_Stockpile; the def for a covered /// building (StorageKinds.IsCovered), its comp, a Blueprint_Storage (BuildDef), a Frame (BuildDef) or a /// StorageGroup (first member's def); empty for anything else, such as a Zone subclass with /// ISlotGroupParent from Preset Filtered Zones. /// public static List KindsFor(IStoreSettingsParent parent) { var kinds = new List(); if (parent == null) return kinds; if (parent is Zone_Stockpile) { foreach (StockpileType type in StorageKinds.StockpileTypes) kinds.Add(StorageKinds.ForStockpile(type)); return kinds; } ThingDef def = parent is StorageGroup group ? DefOf(group.members?.FirstOrDefault()) : DefOf(parent); if (def != null && StorageKinds.IsCovered(def)) kinds.Add(StorageKinds.ForDef(def)); return kinds; } /// /// The building def a storage parent or group member stands for: a blueprint's or frame's BuildDef, a /// thing's def, a comp's thing's def. Null for anything else. /// public static ThingDef DefOf(object parentOrMember) { switch (parentOrMember) { case Blueprint_Build blueprint: return blueprint.BuildDef; case Frame frame: return frame.BuildDef; case Thing thing: return thing.def; case ThingComp comp: return comp.parent?.def; default: return null; } } /// The settings the menu reads and writes: parent.GetStoreSettings() (a group's settings for a linked shelf). public static StorageSettings SettingsOf(IStoreSettingsParent parent) { return parent?.GetStoreSettings(); } /// /// True when the parent gets the Defaults button: it has settings and at least one kind. The same test /// as OptionsFor(parent) being non-empty, without building the options every frame. /// public static bool HasButton(IStoreSettingsParent parent) => SettingsOf(parent) != null && KindsFor(parent).Count > 0; /// The menu options; empty when the parent gets no button. public static List OptionsFor(IStoreSettingsParent parent) { var options = new List(); List kinds = KindsFor(parent); if (kinds.Count == 0 || SettingsOf(parent) == null) return options; if (kinds[0].IsStockpile) { foreach (StorageMenuEntry entry in Entries) options.Add(SubmenuOption(parent, entry, kinds)); } else { foreach (StorageMenuEntry entry in Entries) options.Add(EntryOption(parent, entry, kinds[0], EntryLabel(entry, kinds[0]))); } return options; } /// /// The submenu a stockpile's entry opens: one option per stockpile type, labelled with the type /// ("Stockpile", "Dumping stockpile"). Empty when the parent offers no kinds. /// public static List Submenu(IStoreSettingsParent parent, StorageMenuEntry entry) { var options = new List(); if (SettingsOf(parent) == null) return options; foreach (StorageKind kind in KindsFor(parent)) options.Add(EntryOption(parent, entry, kind, kind.Label.CapitalizeFirst())); return options; } /// The label of an entry for one building kind: "Make default for new shelves". public static string EntryLabel(StorageMenuEntry entry, StorageKind kind) { switch (entry) { case StorageMenuEntry.MakeDefault: return "BlaxDefaults_TabMakeDefault".Translate(kind.LabelPlural); case StorageMenuEntry.LoadDefault: return "BlaxDefaults_TabLoadDefault".Translate(); case StorageMenuEntry.LoadGameDefault: return "BlaxDefaults_TabLoadGameDefault".Translate(); default: return "BlaxDefaults_TabForget".Translate(kind.LabelPlural); } } /// The label of a stockpile entry, which opens the type submenu: "Make default for a stockpile type". public static string StockpileEntryLabel(StorageMenuEntry entry) { switch (entry) { case StorageMenuEntry.MakeDefault: return "BlaxDefaults_TabMakeDefaultType".Translate(); case StorageMenuEntry.LoadDefault: return "BlaxDefaults_TabLoadDefault".Translate(); case StorageMenuEntry.LoadGameDefault: return "BlaxDefaults_TabLoadGameDefault".Translate(); default: return "BlaxDefaults_TabForgetType".Translate(); } } /// /// "New shelves start from your default." or "...from the game's default."; on a stockpile, the list of /// every type that has a default. /// public static string Tooltip(IStoreSettingsParent parent) { List kinds = KindsFor(parent); if (kinds.Count == 0) return null; if (kinds[0].IsStockpile) { if (!BlaxDefaultsMod.StockpilesOn) return "BlaxDefaults_TabTipStockpilesOff".Translate(); List withDefault = kinds.Where(k => k.Saved != null).ToList(); if (withDefault.Count == 0) return "BlaxDefaults_TabTipStockpilesNone".Translate(); var sb = new StringBuilder("BlaxDefaults_TabTipStockpilesSome".Translate().Resolve()); foreach (StorageKind k in withDefault) sb.Append("\n- ").Append(k.Label.CapitalizeFirst()); if (withDefault.Count < kinds.Count) sb.Append("\n\n").Append("BlaxDefaults_TabTipStockpilesRest".Translate().Resolve()); return sb.ToString(); } StorageKind kind = kinds[0]; if (!BlaxDefaultsMod.StorageOn) return "BlaxDefaults_TabTipStorageOff".Translate(kind.LabelPlural); return (kind.Saved != null ? "BlaxDefaults_TabTipYours" : "BlaxDefaults_TabTipGame").Translate(kind.LabelPlural); } /// /// Saves kind.Capture(settings) under the kind's key, keeping the names of the previous default that /// don't resolve now (an inactive mod or DLC), as the edit window does. /// public static void MakeDefault(StorageKind kind, StorageSettings settings) { if (kind == null || settings == null) return; StorageTemplate t = kind.Capture(settings); t.KeepUnresolvedFrom(kind.Saved); DefaultsLibrary.Set(t); } /// The game's default for the kind, then its saved template, written onto settings. public static void LoadDefault(StorageKind kind, StorageSettings settings) { if (kind == null || settings == null) return; kind.LoadDefault(settings, kind.Saved); } /// The game's default for the kind, written onto settings. public static void LoadGameDefault(StorageKind kind, StorageSettings settings) { if (kind == null || settings == null) return; kind.LoadGameDefault(settings); } /// Forgets the kind's default; with confirm, after a Dialog_MessageBox.CreateConfirmation. public static void Forget(StorageKind kind, bool confirm) { if (kind == null) return; if (!confirm) { DefaultsLibrary.Forget(kind.Key); return; } Find.WindowStack.Add(Dialog_MessageBox.CreateConfirmation( "BlaxDefaults_TabForgetConfirm".Translate(kind.LabelPlural), () => { if (DefaultsLibrary.Forget(kind.Key)) Messages.Message("BlaxDefaults_TabForgotten".Translate(kind.LabelPlural), MessageTypeDefOf.NeutralEvent, historical: false); }, destructive: true)); } // ---- options --------------------------------------------------------------------- /// A stockpile entry: opens the type submenu; disabled when no type has a default to load or forget. private static FloatMenuOption SubmenuOption(IStoreSettingsParent parent, StorageMenuEntry entry, List kinds) { bool enabled = !NeedsSaved(entry) || kinds.Any(k => k.Saved != null); Action open = null; if (enabled) open = () => Find.WindowStack.Add(new FloatMenu(Submenu(parent, entry))); var option = new FloatMenuOption(StockpileEntryLabel(entry), open); if (!enabled) option.tooltip = new TipSignal("BlaxDefaults_TabNoStockpileDefault".Translate()); return option; } /// One entry for one kind. Load your default and Forget are disabled without a saved default. private static FloatMenuOption EntryOption(IStoreSettingsParent parent, StorageMenuEntry entry, StorageKind kind, string label) { bool saved = kind.Saved != null; string plural = kind.LabelPlural; Action action = null; string tip; switch (entry) { case StorageMenuEntry.MakeDefault: action = () => DoMakeDefault(parent, kind); tip = "BlaxDefaults_TabMakeDefaultTip".Translate(plural); break; case StorageMenuEntry.LoadDefault: if (saved) action = () => LoadDefault(kind, SettingsOf(parent)); tip = saved ? "BlaxDefaults_TabLoadDefaultTip".Translate(plural) : "BlaxDefaults_TabNoDefault".Translate(plural); break; case StorageMenuEntry.LoadGameDefault: action = () => LoadGameDefault(kind, SettingsOf(parent)); tip = "BlaxDefaults_TabLoadGameDefaultTip".Translate(plural); break; default: if (saved) action = () => Forget(kind, confirm: true); tip = saved ? "BlaxDefaults_TabForgetTip".Translate(plural) : "BlaxDefaults_TabNoDefault".Translate(plural); break; } return new FloatMenuOption(label, action) { tooltip = new TipSignal(tip) }; } private static bool NeedsSaved(StorageMenuEntry entry) => entry == StorageMenuEntry.LoadDefault || entry == StorageMenuEntry.Forget; private static void DoMakeDefault(IStoreSettingsParent parent, StorageKind kind) { StorageSettings settings = SettingsOf(parent); if (settings == null) return; MakeDefault(kind, settings); if (DefaultsLibrary.LastWriteOk) Messages.Message("BlaxDefaults_TabMadeDefault".Translate(kind.LabelPlural), MessageTypeDefOf.NeutralEvent, historical: false); else Messages.Message("BlaxDefaults_TabWriteFailed".Translate(), MessageTypeDefOf.RejectInput, historical: false); } } }