using System; using System.Collections.Generic; using System.Linq; using HarmonyLib; using UnityEngine; using UnityEngine.UI; using UXAssist.Common; using UXAssist.Production; using UXAssist.UI; using Util = UXAssist.UI.Util; namespace UXAssist.Patches; public class BlueprintDescriptionPatch : PatchImpl { private static readonly Dictionary Inspectors = new(); public static void Start() => Enable(true); public static void Uninit() => Enable(false); protected override void OnDisable() { foreach (var controls in Inspectors.Values) controls.Destroy(); Inspectors.Clear(); } [HarmonyPostfix, HarmonyPatch(typeof(UIBlueprintInspector), nameof(UIBlueprintInspector._OnCreate))] private static void OnCreate(UIBlueprintInspector __instance) { GetControls(__instance); } [HarmonyPostfix, HarmonyPatch(typeof(UIBlueprintInspector), nameof(UIBlueprintInspector._OnOpen))] private static void OnOpen(UIBlueprintInspector __instance) { GetControls(__instance)?.ResetSession(); } [HarmonyPostfix, HarmonyPatch(typeof(UIBlueprintInspector), nameof(UIBlueprintInspector._OnClose))] private static void OnClose(UIBlueprintInspector __instance) { if (Inspectors.TryGetValue(__instance, out var controls)) controls.ResetSession(); } [HarmonyPostfix, HarmonyPatch(typeof(UIBlueprintInspector), nameof(UIBlueprintInspector._OnFree))] private static void OnFree(UIBlueprintInspector __instance) { if (!Inspectors.TryGetValue(__instance, out var controls)) return; controls.Destroy(); Inspectors.Remove(__instance); } [HarmonyPostfix, HarmonyPatch(typeof(UIBlueprintInspector), nameof(UIBlueprintInspector._OnDestroy))] private static void OnDestroy(UIBlueprintInspector __instance) { OnFree(__instance); } [HarmonyPostfix, HarmonyPatch(typeof(UIBlueprintInspector), nameof(UIBlueprintInspector.Refresh))] private static void Refresh(UIBlueprintInspector __instance) { try { GetControls(__instance)?.Refresh(); } catch (Exception exception) { UXAssist.Logger.LogError($"Blueprint description inspector refresh failed: {exception}"); } } [HarmonyPostfix, HarmonyPatch(typeof(UIBlueprintInspector), nameof(UIBlueprintInspector._OnUpdate))] private static void OnUpdate(UIBlueprintInspector __instance) { if (Inspectors.TryGetValue(__instance, out var controls)) controls.HandleInput(); } [HarmonyPostfix, HarmonyPatch(typeof(UIBlueprintInspector), nameof(UIBlueprintInspector.OnLanguageChanged))] private static void OnLanguageChanged(UIBlueprintInspector __instance) { try { if (Inspectors.TryGetValue(__instance, out var controls)) controls.UpdateLabels(); } catch (Exception exception) { UXAssist.Logger.LogError($"Blueprint description localization refresh failed: {exception}"); } } private static InspectorControls GetControls(UIBlueprintInspector inspector) { if (!inspector) return null; if (Inspectors.TryGetValue(inspector, out var controls)) return controls; try { controls = new InspectorControls(inspector); Inspectors.Add(inspector, controls); return controls; } catch (Exception exception) { UXAssist.Logger.LogError($"Blueprint description controls could not be created: {exception}"); return null; } } private sealed class InspectorControls { private const float Left = 12f; private const float ControlsGap = 6f; private readonly UIBlueprintInspector _inspector; private RectTransform _panel; private UIButton _entry; private Text _entryText; private UIButton _button; private Text _buttonText; private UIButton _closeButton; private Text _title; private readonly Vector3[] _corners = new Vector3[4]; private readonly Canvas _canvas; private readonly RectTransform _viewport; private readonly Text _descriptionLabel; private readonly RectTransform _descriptionIcon; private bool _optionsOpen; private MyCheckBox _checkbox; private UIComboBox _basisCombo; private Text _basisLabel; private UIButton _basisHelp; private BlueprintData _currentBlueprint; private bool _manualProliferation; public InspectorControls(UIBlueprintInspector inspector) { _inspector = inspector; _canvas = inspector.rectTrans.GetComponentInParent().rootCanvas; _viewport = inspector.descGroupTrans.GetComponentInParent()?.viewport; _descriptionLabel = inspector.descGroupTrans.Find("label").GetComponent(); _descriptionIcon = (RectTransform)inspector.descTextInput.transform.Find("sel-icon-btn"); try { // The native description has a 22px title row above its editable text. _entry = LayoutHelper.AddButton(0, 0, inspector.descGroupTrans, I18NKeys.BlueprintDescriptionOptions, 12, "blueprint-description-options", ToggleOptions); _entryText = _entry.transform.Find("button-text").GetComponent(); _entry.tips = inspector.tipButton2.tips; _entry.tips.topLevel = true; _entryText.resizeTextForBestFit = false; Util.NormalizeRectWithMargin(_entryText, 0, 8, 0, 8); // Reuse the native background, including its blur material, only while options are open. var source = UIRoot.instance.uiGame.inserterWindow.transform.Find("panel-bg").GetComponent(); _panel = UnityEngine.Object.Instantiate(source, inspector.rectTrans).rectTransform; _panel.gameObject.name = "uxassist-blueprint-description-options"; _panel.gameObject.SetActive(false); for (var i = _panel.childCount - 1; i >= 0; i--) { var child = _panel.GetChild(i).gameObject; child.SetActive(false); UnityEngine.Object.Destroy(child); } _panel.anchorMin = _panel.anchorMax = new Vector2(0, 1); _panel.pivot = new Vector2(0, 1); _panel.GetComponent().raycastTarget = true; _title = LayoutHelper.AddText(Left, 10, _panel, I18NKeys.BlueprintAutoFillDescription, 16); _title.color = _entryText.color; var closeSource = UIRoot.instance.uiGame.inserterWindow.transform .Find("panel-bg/btn-box/close-wnd-btn").GetComponent(); _closeButton = UnityEngine.Object.Instantiate(closeSource, _panel); Util.ResetButton(_closeButton); // The source's persistent callback closes the original inserter window. _closeButton.button.onClick = new Button.ButtonClickedEvent(); _closeButton.button.onClick.AddListener(CloseOptions); Util.NormalizeRectWithTopRight(_closeButton, 0, 0, _panel); _button = LayoutHelper.AddButton(0, 0, _panel, I18NKeys.BlueprintGenerateDescription, 15, "blueprint-auto-fill-description", Generate); _buttonText = _button.transform.Find("button-text").GetComponent(); _buttonText.resizeTextForBestFit = false; _buttonText.alignment = TextAnchor.MiddleCenter; Util.NormalizeRectWithMargin(_buttonText, 4, 10, 4, 10); _checkbox = MyCheckBox.CreateCheckBox(Left, 42, _panel, false, I18NKeys.BlueprintProliferation.Translate(), 15); _checkbox.rectTrans.sizeDelta = new Vector2(20, 20); _checkbox.checkImage.rectTransform.sizeDelta = new Vector2(20, 20); _checkbox.labelText.verticalOverflow = VerticalWrapMode.Overflow; _checkbox.gameObject.name = "blueprint-proliferation"; _checkbox.OnChecked += OnProliferationChecked; _basisLabel = LayoutHelper.AddText(Left, 68, _panel, I18NKeys.BlueprintProductionBasis, 14); _basisCombo = UnityEngine.Object.Instantiate(UIRoot.instance.optionWindow.resolutionComp, _panel); _basisCombo.gameObject.name = "blueprint-production-basis"; _basisCombo.gameObject.SetActive(true); foreach (var button in _basisCombo.GetComponentsInChildren(true)) Util.ResetButton(button); var combo = _basisCombo; // Runtime resolution rows are cloned as children, but their private tracking list is not serialized. for (var i = combo.m_DropDownContent.childCount - 1; i >= 0; i--) { var button = combo.m_DropDownContent.GetChild(i).GetComponent