using System; using System.Collections.Generic; using HarmonyLib; using RimWorld; using UnityEngine; using Verse; namespace BlaxDefaults { /// /// The "Defaults" button in the storage tab. Work package "tab" (design.md: "Defaults button in the /// storage tab"). ITab_Shells and ITab_BiosculpterNutritionStorage don't override FillTab, so they get it /// too. The tab is 300 by 480 contracted by 10 and the priority button takes 160 px of the 35 px top row, /// so the button starts at x 180 in tab coordinates; when IsPrioritySettingVisible is false the top row is /// 20 px and the button is a 20 px icon. Both end at x 274, clear of the tab's own close X: /// InspectTabBase.DoTabGUI draws Widgets.CloseButtonFor(rect.AtZero()) before FillTab /// (Verse/InspectTabBase.cs:61), at (width - 22, 4, 18, 18) = (278, 4, 18, 18) (Verse/Widgets.cs:1697), in /// the same coordinates (an ImmediateWindow with Margin 0). The design's (180, 10, 110, 29) and (270, 10) /// covered that X, which takes the click first. Clicking opens FloatMenu(StorageDefaultsMenu.OptionsFor(parent)); /// the tooltip is StorageDefaultsMenu.Tooltip(parent). No button when OptionsFor returns nothing. /// public static class StorageTabButton { public static readonly Rect ButtonRect = new Rect(180f, 10f, 94f, 29f); public static readonly Rect IconRect = new Rect(254f, 10f, 20f, 20f); /// The tab's close X for a tab this wide, as Widgets.CloseButtonFor places it. public static Rect CloseXRect(float tabWidth) => new Rect(tabWidth - 18f - 4f, 4f, 18f, 18f); private const int TooltipId = 0x42D3_7A01; /// The protected ITab_Storage.SelStoreSettingsParent (virtual; ITab_Shells overrides it). public static readonly Func SelStoreSettingsParent = AccessTools.MethodDelegate>(AccessTools.PropertyGetter(typeof(ITab_Storage), "SelStoreSettingsParent")); /// The protected ITab_Storage.IsPrioritySettingVisible. public static readonly Func IsPrioritySettingVisible = AccessTools.MethodDelegate>(AccessTools.PropertyGetter(typeof(ITab_Storage), "IsPrioritySettingVisible")); /// Draws the button for the tab's selected parent. Called from the FillTab postfix. public static void Draw(ITab_Storage tab) { IStoreSettingsParent parent = SelStoreSettingsParent(tab); // HasButton is OptionsFor(parent).Count > 0 without building the options every frame. if (!StorageDefaultsMenu.HasButton(parent)) return; bool full = IsPrioritySettingVisible(tab); Rect rect = full ? ButtonRect : IconRect; GameFont font = Text.Font; TextAnchor anchor = Text.Anchor; bool clicked; try { Text.Font = GameFont.Small; clicked = full ? Widgets.ButtonText(rect, "BlaxDefaults_TabButton".Translate()) : Widgets.ButtonImage(rect, TexUI.MakeDefault); } finally { Text.Font = font; Text.Anchor = anchor; } TooltipHandler.TipRegion(rect, () => StorageDefaultsMenu.Tooltip(parent) ?? "", TooltipId); if (clicked) { List options = StorageDefaultsMenu.OptionsFor(parent); if (options.Count > 0) Find.WindowStack.Add(new FloatMenu(options)); } } } [HarmonyPatch(typeof(ITab_Storage), "FillTab")] public static class Patch_ITab_Storage_FillTab { public static void Postfix(ITab_Storage __instance) { try { if (BlaxDefaultsMod.Enabled) StorageTabButton.Draw(__instance); } catch (Exception e) { HookGuard.Report("ITab_Storage.FillTab", e); } } } }