170 lines
7.5 KiB
C#
170 lines
7.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using RimWorld;
|
|
using UnityEngine;
|
|
using Verse;
|
|
|
|
namespace BlaxCastes
|
|
{
|
|
/// <summary>
|
|
/// The join defaults window, laid out like the vanilla medical defaults: a title, a short text, then
|
|
/// one row per way of joining with a label on the left and a caste picker on the right. Picks go
|
|
/// through CasteManager.SetJoinRow. Without Ideology the slave rows are hidden, unless a save made with
|
|
/// Ideology still has slaves or a slave row picks a caste (ShowsSlaveRows).
|
|
/// </summary>
|
|
public class Dialog_JoinDefaults : Window
|
|
{
|
|
private const float LabelWidth = 200f;
|
|
private const float ButtonWidth = 160f;
|
|
private const float RowHeight = 28f;
|
|
private const float RowStep = 34f;
|
|
|
|
public Dialog_JoinDefaults()
|
|
{
|
|
forcePause = true;
|
|
doCloseX = true;
|
|
doCloseButton = true;
|
|
closeOnClickedOutside = true;
|
|
absorbInputAroundWindow = true;
|
|
}
|
|
|
|
public override Vector2 InitialSize => new Vector2(406f, 470f);
|
|
|
|
public override string CloseButtonText => "OK".Translate();
|
|
|
|
/// <summary>The rows the window shows: all five while it shows the slave rows (ShowsSlaveRows), the other three otherwise.</summary>
|
|
public static List<string> VisibleRows()
|
|
{
|
|
bool slaves = ShowsSlaveRows();
|
|
var rows = new List<string>();
|
|
foreach (string reason in JoinReason.Rows)
|
|
{
|
|
if (!slaves && IsSlaveRow(reason))
|
|
continue;
|
|
rows.Add(reason);
|
|
}
|
|
return rows;
|
|
}
|
|
|
|
public static bool IsSlaveRow(string reason) => reason == JoinReason.BoughtSlave || reason == JoinReason.NewSlave;
|
|
|
|
/// <summary>
|
|
/// The bought-as-slave and new-slave rows show with Ideology. Without it they show only while they still
|
|
/// matter: the colony has slaves (a save made with Ideology) or a slave row picks a caste. They then
|
|
/// carry a "Needs Ideology" note (SlaveRowsNote).
|
|
/// </summary>
|
|
public static bool ShowsSlaveRows()
|
|
{
|
|
if (ModsConfig.IdeologyActive)
|
|
return true;
|
|
GameComponent_BlaxCastes comp = GameComponent_BlaxCastes.Instance;
|
|
if (comp != null && JoinReason.Rows.Any(r => IsSlaveRow(r) && comp.GetRow(r).mode != JoinRowMode.NoCaste))
|
|
return true;
|
|
return Current.Game != null && PawnsFinder.AllMapsCaravansAndTravellingTransporters_Alive_OfPlayerFaction.Any(p => p.IsSlaveOfColony);
|
|
}
|
|
|
|
/// <summary>The note under the slave rows while they show without Ideology, or null.</summary>
|
|
public static string SlaveRowsNote() =>
|
|
!ModsConfig.IdeologyActive && ShowsSlaveRows() ? (string)"BlaxCastes_JoinSlaveRowsNoIdeology".Translate() : null;
|
|
|
|
public override void DoWindowContents(Rect inRect)
|
|
{
|
|
GameComponent_BlaxCastes comp = GameComponent_BlaxCastes.Instance;
|
|
float y = 0f;
|
|
using (new TextBlock(GameFont.Medium))
|
|
Widgets.Label(inRect, ref y, "BlaxCastes_JoinDefaultsTitle".Translate());
|
|
Text.Font = GameFont.Small;
|
|
Widgets.Label(inRect, ref y, "BlaxCastes_JoinDefaultsDesc".Translate());
|
|
if (comp == null)
|
|
return;
|
|
if (!JoinDefaults.Active)
|
|
{
|
|
y += 4f;
|
|
using (new TextBlock(ColorLibrary.RedReadable))
|
|
Widgets.Label(inRect, ref y, "BlaxCastes_JoinDefaultsOff".Translate());
|
|
}
|
|
if (comp.Castes.Count == 0)
|
|
{
|
|
y += 4f;
|
|
using (new TextBlock(ColoredText.SubtleGrayColor))
|
|
Widgets.Label(inRect, ref y, "BlaxCastes_JoinDefaultsNoCastes".Translate());
|
|
}
|
|
y += 10f;
|
|
|
|
Text.Anchor = TextAnchor.MiddleLeft;
|
|
foreach (string reason in VisibleRows())
|
|
DoRow(inRect, ref y, reason);
|
|
Text.Anchor = TextAnchor.UpperLeft;
|
|
string note = SlaveRowsNote();
|
|
if (note != null)
|
|
{
|
|
using (new TextBlock(GameFont.Tiny, ColoredText.SubtleGrayColor))
|
|
Widgets.Label(inRect, ref y, note);
|
|
}
|
|
}
|
|
|
|
private static void DoRow(Rect inRect, ref float y, string reason)
|
|
{
|
|
var row = new Rect(inRect.x, y, inRect.width, RowHeight);
|
|
var label = new Rect(inRect.x, y, LabelWidth, RowHeight);
|
|
var button = new Rect(inRect.xMax - ButtonWidth, y, ButtonWidth, RowHeight);
|
|
if (Mouse.IsOver(row))
|
|
Widgets.DrawLightHighlight(row);
|
|
TooltipHandler.TipRegionByKey(row, DlcSupport.IdeologyKey("BlaxCastes_RowTip_" + reason));
|
|
Widgets.LabelFit(label, ("BlaxCastes_Row_" + reason).Translate());
|
|
if (Widgets.ButtonText(button, RowButtonLabel(reason).Truncate(button.width - 12f)))
|
|
Find.WindowStack.Add(new FloatMenu(RowMenu(reason)));
|
|
y += RowStep;
|
|
}
|
|
|
|
/// <summary>The picker's label: No caste, Default caste (with the default's name) or the caste's name.</summary>
|
|
public static string RowButtonLabel(string reason)
|
|
{
|
|
GameComponent_BlaxCastes comp = GameComponent_BlaxCastes.Instance;
|
|
if (comp == null)
|
|
return "BlaxCastes_NoCaste".Translate();
|
|
JoinRow row = comp.GetRow(reason);
|
|
switch (row.mode)
|
|
{
|
|
case JoinRowMode.DefaultCaste:
|
|
return comp.HasCaste(comp.DefaultCaste)
|
|
? "BlaxCastes_DefaultCasteNamed".Translate(comp.DefaultCaste.label.Named("CASTE"))
|
|
: "BlaxCastes_DefaultCasteNone".Translate();
|
|
case JoinRowMode.SpecificCaste:
|
|
return comp.HasCaste(row.caste) ? row.caste.label : (string)"BlaxCastes_NoCaste".Translate();
|
|
default:
|
|
return "BlaxCastes_NoCaste".Translate();
|
|
}
|
|
}
|
|
|
|
/// <summary>The picker's menu: No caste, Default caste, then every caste by label (the Caste column's order). Public so tests can invoke entries.</summary>
|
|
public static List<FloatMenuOption> RowMenu(string reason)
|
|
{
|
|
var options = new List<FloatMenuOption>
|
|
{
|
|
new FloatMenuOption("BlaxCastes_NoCaste".Translate(), () => CasteManager.SetJoinRow(reason, JoinRowMode.NoCaste)),
|
|
new FloatMenuOption(DefaultOptionLabel(), () => CasteManager.SetJoinRow(reason, JoinRowMode.DefaultCaste)),
|
|
};
|
|
GameComponent_BlaxCastes comp = GameComponent_BlaxCastes.Instance;
|
|
if (comp != null)
|
|
{
|
|
foreach (Caste caste in comp.Castes.Where(x => x != null).OrderBy(x => x.label, StringComparer.CurrentCultureIgnoreCase).ThenBy(x => x.id))
|
|
{
|
|
Caste c = caste;
|
|
options.Add(new FloatMenuOption(c.label, () => CasteManager.SetJoinRow(reason, JoinRowMode.SpecificCaste, c)));
|
|
}
|
|
}
|
|
return options;
|
|
}
|
|
|
|
private static string DefaultOptionLabel()
|
|
{
|
|
GameComponent_BlaxCastes comp = GameComponent_BlaxCastes.Instance;
|
|
return comp != null && comp.HasCaste(comp.DefaultCaste)
|
|
? "BlaxCastes_DefaultCasteNamed".Translate(comp.DefaultCaste.label.Named("CASTE"))
|
|
: "BlaxCastes_DefaultCasteNone".Translate();
|
|
}
|
|
}
|
|
}
|