296 lines
13 KiB
C#
296 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using RimWorld;
|
|
using Verse;
|
|
|
|
namespace BlaxDefaults
|
|
{
|
|
/// <summary>The four entries of the Defaults menu, in menu order.</summary>
|
|
public enum StorageMenuEntry
|
|
{
|
|
MakeDefault,
|
|
LoadDefault,
|
|
LoadGameDefault,
|
|
Forget,
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public static class StorageDefaultsMenu
|
|
{
|
|
/// <summary>The entries in menu order.</summary>
|
|
public static readonly StorageMenuEntry[] Entries =
|
|
{
|
|
StorageMenuEntry.MakeDefault, StorageMenuEntry.LoadDefault, StorageMenuEntry.LoadGameDefault, StorageMenuEntry.Forget,
|
|
};
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public static List<StorageKind> KindsFor(IStoreSettingsParent parent)
|
|
{
|
|
var kinds = new List<StorageKind>();
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>The settings the menu reads and writes: parent.GetStoreSettings() (a group's settings for a linked shelf).</summary>
|
|
public static StorageSettings SettingsOf(IStoreSettingsParent parent)
|
|
{
|
|
return parent?.GetStoreSettings();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public static bool HasButton(IStoreSettingsParent parent) => SettingsOf(parent) != null && KindsFor(parent).Count > 0;
|
|
|
|
/// <summary>The menu options; empty when the parent gets no button.</summary>
|
|
public static List<FloatMenuOption> OptionsFor(IStoreSettingsParent parent)
|
|
{
|
|
var options = new List<FloatMenuOption>();
|
|
List<StorageKind> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public static List<FloatMenuOption> Submenu(IStoreSettingsParent parent, StorageMenuEntry entry)
|
|
{
|
|
var options = new List<FloatMenuOption>();
|
|
if (SettingsOf(parent) == null)
|
|
return options;
|
|
foreach (StorageKind kind in KindsFor(parent))
|
|
options.Add(EntryOption(parent, entry, kind, kind.Label.CapitalizeFirst()));
|
|
return options;
|
|
}
|
|
|
|
/// <summary>The label of an entry for one building kind: "Make default for new shelves".</summary>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>The label of a stockpile entry, which opens the type submenu: "Make default for a stockpile type".</summary>
|
|
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();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// "New shelves start from your default." or "...from the game's default."; on a stockpile, the list of
|
|
/// every type that has a default.
|
|
/// </summary>
|
|
public static string Tooltip(IStoreSettingsParent parent)
|
|
{
|
|
List<StorageKind> kinds = KindsFor(parent);
|
|
if (kinds.Count == 0)
|
|
return null;
|
|
if (kinds[0].IsStockpile)
|
|
{
|
|
if (!BlaxDefaultsMod.StockpilesOn)
|
|
return "BlaxDefaults_TabTipStockpilesOff".Translate();
|
|
List<StorageKind> 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>The game's default for the kind, then its saved template, written onto settings.</summary>
|
|
public static void LoadDefault(StorageKind kind, StorageSettings settings)
|
|
{
|
|
if (kind == null || settings == null)
|
|
return;
|
|
kind.LoadDefault(settings, kind.Saved);
|
|
}
|
|
|
|
/// <summary>The game's default for the kind, written onto settings.</summary>
|
|
public static void LoadGameDefault(StorageKind kind, StorageSettings settings)
|
|
{
|
|
if (kind == null || settings == null)
|
|
return;
|
|
kind.LoadGameDefault(settings);
|
|
}
|
|
|
|
/// <summary>Forgets the kind's default; with confirm, after a Dialog_MessageBox.CreateConfirmation.</summary>
|
|
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 ---------------------------------------------------------------------
|
|
|
|
/// <summary>A stockpile entry: opens the type submenu; disabled when no type has a default to load or forget.</summary>
|
|
private static FloatMenuOption SubmenuOption(IStoreSettingsParent parent, StorageMenuEntry entry, List<StorageKind> 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;
|
|
}
|
|
|
|
/// <summary>One entry for one kind. Load your default and Forget are disabled without a saved default.</summary>
|
|
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);
|
|
}
|
|
}
|
|
}
|