using System; using System.Collections.Generic; using System.Linq; using RimWorld; using UnityEngine; using Verse; namespace BlaxCastes { /// /// 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. The slave rows are hidden without Ideology. /// 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(); /// The rows the window shows: all five with Ideology, without the slave rows otherwise. public static List VisibleRows() { var rows = new List(); foreach (string reason in JoinReason.Rows) { if (!ModsConfig.IdeologyActive && (reason == JoinReason.BoughtSlave || reason == JoinReason.NewSlave)) continue; rows.Add(reason); } return rows; } 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; } 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, "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; } /// The picker's label: No caste, Default caste (with the default's name) or the caste's name. 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(); } } /// The picker's menu: No caste, Default caste, then every caste by label (the Caste column's order). Public so tests can invoke entries. public static List RowMenu(string reason) { var options = new List { 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(); } } }