rimworld-blax-defaults/Source/BlaxDefaults/ForbidRulesSection.cs

154 lines
7.6 KiB
C#
Raw Permalink Normal View History

using System.Collections.Generic;
using RimWorld;
using UnityEngine;
using Verse;
namespace BlaxDefaults
{
/// <summary>
/// The "Forbid rules" section of the settings page. Work package "forbid" (design.md, 1.1). Order: the
/// category toggle (forbidRules); per rule its checkbox ("Gear your colony drops", "Gear enemies drop"), a row
/// saying what its filter allows with Edit and Forget (Forget asks first), "Where", the strip switch (disabled
/// until the rule has a filter) and "Use the suggested setup" (asks first); a line saying what neutral pawns drop keeps
/// the game's flags; the three corpse switches. Ticking a rule without a filter saves its suggested setup and
/// says so in a message. Works from the main menu: nothing here needs a running game.
/// </summary>
public static class ForbidRulesSection
{
private const float ButtonWidth = 90f;
private const float ButtonHeight = 28f;
private const float RowPad = 4f;
/// <summary>The summary row per rule, cached by template reference (library entries are replaced, never edited).</summary>
private static readonly Dictionary<ForbidRule, (StorageTemplate template, string summary)> summaries =
new Dictionary<ForbidRule, (StorageTemplate, string)>();
public static void Draw(Listing_Standard l)
{
BlaxDefaultsSettings s = BlaxDefaultsMod.Settings;
if (s == null)
return;
l.CheckboxLabeled("BlaxDefaults_ForbidToggle".Translate(), ref s.forbidRules, "BlaxDefaults_ForbidToggleTip".Translate());
foreach (ForbidRule rule in ForbidRules.All)
DrawRule(l, rule);
using (new TextBlock(Color.gray))
l.Label("BlaxDefaults_ForbidNeutral".Translate());
l.Gap(6f);
l.Label("BlaxDefaults_ForbidCorpsesHeader".Translate());
l.CheckboxLabeled("BlaxDefaults_ForbidCorpsesAnimal".Translate(), ref s.animalCorpsesAllowed, "BlaxDefaults_ForbidCorpsesAnimalTip".Translate());
l.CheckboxLabeled("BlaxDefaults_ForbidCorpsesEnemy".Translate(), ref s.enemyCorpsesAllowed, "BlaxDefaults_ForbidCorpsesEnemyTip".Translate());
l.CheckboxLabeled("BlaxDefaults_ForbidCorpsesOther".Translate(), ref s.otherCorpsesAllowed, "BlaxDefaults_ForbidCorpsesOtherTip".Translate());
}
private static void DrawRule(Listing_Standard l, ForbidRule rule)
{
l.Gap(6f);
bool ticked = rule.Ticked;
bool was = ticked;
l.CheckboxLabeled(rule.Label, ref ticked, (rule.Group == ForbidGroup.Colony ? "BlaxDefaults_ForbidColonyRuleTip" : "BlaxDefaults_ForbidEnemyRuleTip").Translate());
if (ticked != was)
Tick(rule, ticked);
DrawFilterRow(l, rule);
string where = rule.Anywhere ? "BlaxDefaults_ForbidWhereAnywhere".Translate() : "BlaxDefaults_ForbidWhereHome".Translate();
if (l.ButtonTextLabeledPct("BlaxDefaults_ForbidWhere".Translate(), where, 0.6f, tooltip: "BlaxDefaults_ForbidWhereTip".Translate()))
{
Find.WindowStack.Add(new FloatMenu(new List<FloatMenuOption>
{
new FloatMenuOption("BlaxDefaults_ForbidWhereAnywhere".Translate(), () => rule.Anywhere = true),
new FloatMenuOption("BlaxDefaults_ForbidWhereHome".Translate(), () => rule.Anywhere = false),
}));
}
bool strip = rule.StripTicked;
Rect stripRect = l.GetRect(Text.LineHeight);
bool hasFilter = rule.HasFilter;
Widgets.CheckboxLabeled(stripRect, "BlaxDefaults_ForbidStrip".Translate(), ref strip, disabled: !hasFilter);
TooltipHandler.TipRegion(stripRect, hasFilter ? "BlaxDefaults_ForbidStripTip".Translate() : "BlaxDefaults_ForbidStripNeedsFilter".Translate());
if (hasFilter)
rule.StripTicked = strip;
l.Gap(2f);
if (l.ButtonText("BlaxDefaults_ForbidUseSuggested".Translate(), widthPct: 0.6f))
ConfirmSuggested(rule);
}
/// <summary>The checkbox: ticking a rule without a filter saves the suggested setup and says so.</summary>
public static void Tick(ForbidRule rule, bool on)
{
if (rule.SetTicked(on))
Messages.Message("BlaxDefaults_ForbidUsedSuggested".Translate(rule.Label, ForbidRules.Summary(rule.Template) ?? ""), MessageTypeDefOf.NeutralEvent, historical: false);
}
private static void DrawFilterRow(Listing_Standard l, ForbidRule rule)
{
StorageTemplate t = rule.Template;
string text;
Color color = Color.white;
if (t != null)
{
text = Summary(rule, t);
}
else if (rule.Ticked && ForbidRules.On)
{
text = "BlaxDefaults_ForbidFilterMissing".Translate();
color = ColorLibrary.RedReadable;
}
else
{
text = "BlaxDefaults_ForbidNoFilter".Translate();
color = Color.gray;
}
float textWidth = l.ColumnWidth - 2f * ButtonWidth - 3f * 6f;
float height = Mathf.Max(ButtonHeight + 2f * RowPad, Text.CalcHeight(text, textWidth) + 2f * RowPad);
Rect row = l.GetRect(height);
Widgets.DrawHighlightIfMouseover(row);
using (new TextBlock(color))
Widgets.Label(new Rect(row.x + 6f, row.y + RowPad, textWidth, height - 2f * RowPad), text);
float buttonY = row.y + (height - ButtonHeight) / 2f;
var forgetRect = new Rect(row.xMax - ButtonWidth, buttonY, ButtonWidth, ButtonHeight);
var editRect = new Rect(forgetRect.x - 6f - ButtonWidth, buttonY, ButtonWidth, ButtonHeight);
if (Widgets.ButtonText(editRect, "BlaxDefaults_PageEdit".Translate()))
OpenEditor(rule);
if (t != null && Widgets.ButtonText(forgetRect, "BlaxDefaults_PageForget".Translate()))
ConfirmForget(rule);
l.Gap(2f);
}
private static string Summary(ForbidRule rule, StorageTemplate t)
{
if (summaries.TryGetValue(rule, out var cached) && ReferenceEquals(cached.template, t))
return cached.summary;
string summary = ForbidRules.Summary(t);
summaries[rule] = (t, summary);
return summary;
}
/// <summary>Edit: the storage editor on the saved filter, or on the suggested setup when there is none yet.</summary>
public static Dialog_EditStorageTemplate Editor(ForbidRule rule) =>
new Dialog_EditStorageTemplate(StorageKinds.ForForbid(rule), (rule.Template ?? rule.Suggested()).CloneTemplate());
private static void OpenEditor(ForbidRule rule) => Find.WindowStack.Add(Editor(rule));
private static void ConfirmForget(ForbidRule rule)
{
Find.WindowStack.Add(Dialog_MessageBox.CreateConfirmation(
"BlaxDefaults_ForbidForgetConfirm".Translate(rule.Label),
rule.Forget,
destructive: true));
}
private static void ConfirmSuggested(ForbidRule rule)
{
Find.WindowStack.Add(Dialog_MessageBox.CreateConfirmation(
"BlaxDefaults_ForbidUseSuggestedConfirm".Translate(rule.Label,
(rule.Group == ForbidGroup.Colony ? "BlaxDefaults_ForbidSuggestedColony" : "BlaxDefaults_ForbidSuggestedEnemies").Translate()),
() => rule.UseSuggested()));
}
}
}