mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2026-11-19 17:05:20 +08:00
fix(uxassist): respect blueprint text and icon bounds
Account for the native background's trimmed border, keep checkbox captions independently sized, and place Auto-fill after the measured title while reserving the native icon. Limit changes to owned controls. Assisted-by: Cindy (https://cindy.app)
This commit is contained in:
1 parent
51af1430bc
commit
19d19ce38d
4 files changed
+108
-40
No files matched your search
@@ -122,6 +122,8 @@ public class BlueprintDescriptionPatch : PatchImpl<BlueprintDescriptionPatch>
|
||||
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 MyCornerComboBox _basisCombo;
|
||||
@@ -135,15 +137,19 @@ public class BlueprintDescriptionPatch : PatchImpl<BlueprintDescriptionPatch>
|
||||
_inspector = inspector;
|
||||
_canvas = inspector.rectTrans.GetComponentInParent<Canvas>().rootCanvas;
|
||||
_viewport = inspector.descGroupTrans.GetComponentInParent<ScrollRect>()?.viewport;
|
||||
_descriptionLabel = inspector.descGroupTrans.Find("label").GetComponent<Text>();
|
||||
_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, 13, "blueprint-description-options", ToggleOptions);
|
||||
I18NKeys.BlueprintDescriptionOptions, 12, "blueprint-description-options", ToggleOptions);
|
||||
_entryText = _entry.transform.Find("button-text").GetComponent<Text>();
|
||||
_entry.tips = inspector.tipButton2.tips;
|
||||
_entry.tips.topLevel = true;
|
||||
Util.NormalizeRectWithTopRight(_entry, 0, 0, inspector.descGroupTrans);
|
||||
_entryText.resizeTextForBestFit = false;
|
||||
_entryText.alignment = TextAnchor.MiddleCenter;
|
||||
Util.NormalizeRectWithMargin(_entryText, 1, 8, 1, 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<Image>();
|
||||
@@ -171,8 +177,14 @@ public class BlueprintDescriptionPatch : PatchImpl<BlueprintDescriptionPatch>
|
||||
_button = LayoutHelper.AddButton(0, 0, _panel,
|
||||
I18NKeys.BlueprintGenerateDescription, 15, "blueprint-auto-fill-description", Generate);
|
||||
_buttonText = _button.transform.Find("button-text").GetComponent<Text>();
|
||||
_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).WithSmallerBox(20f);
|
||||
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);
|
||||
@@ -187,10 +199,14 @@ public class BlueprintDescriptionPatch : PatchImpl<BlueprintDescriptionPatch>
|
||||
var combo = _basisCombo.GetComponent<UIComboBox>();
|
||||
combo.CanInput = false;
|
||||
combo.autoWidth = false;
|
||||
combo.ItemHeight = 24;
|
||||
Util.NormalizeRectWithMargin(_basisCombo.transform.Find("Main Button"), 0, 0, 0, 0);
|
||||
combo.m_Text.alignment = TextAnchor.MiddleLeft;
|
||||
combo.m_ListItemRes.GetComponentInChildren<Text>().alignment = TextAnchor.MiddleLeft;
|
||||
combo.m_Text.resizeTextForBestFit = false;
|
||||
Util.NormalizeRectWithMargin(combo.m_Text, 2, 8, 2, 24);
|
||||
var itemText = combo.m_ListItemRes.GetComponentInChildren<Text>();
|
||||
itemText.alignment = TextAnchor.MiddleLeft;
|
||||
itemText.resizeTextForBestFit = false;
|
||||
Util.NormalizeRectWithMargin(itemText, 2, 8, 2, 8);
|
||||
_basisHelp = LayoutHelper.AddTipsButton(Left, 68, _panel,
|
||||
I18NKeys.BlueprintProductionBasis.Translate(), I18NKeys.BlueprintProductionBasisTip.Translate(), "");
|
||||
UpdateLabels();
|
||||
@@ -211,13 +227,13 @@ public class BlueprintDescriptionPatch : PatchImpl<BlueprintDescriptionPatch>
|
||||
if (_inspector.blueprint != null) SyncDefault();
|
||||
else _checkbox.Checked = false;
|
||||
CloseOptions();
|
||||
_entry.gameObject.SetActive(_inspector.blueprint != null);
|
||||
UpdateLabels();
|
||||
}
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
SyncDefault();
|
||||
_entry.gameObject.SetActive(_inspector.blueprint != null);
|
||||
PositionEntry();
|
||||
}
|
||||
|
||||
private void ToggleOptions()
|
||||
@@ -243,6 +259,7 @@ public class BlueprintDescriptionPatch : PatchImpl<BlueprintDescriptionPatch>
|
||||
|
||||
public void HandleInput()
|
||||
{
|
||||
PositionEntry();
|
||||
if (!_optionsOpen) return;
|
||||
var combo = _basisCombo.GetComponent<UIComboBox>();
|
||||
if (VFInput.escape)
|
||||
@@ -265,20 +282,36 @@ public class BlueprintDescriptionPatch : PatchImpl<BlueprintDescriptionPatch>
|
||||
CloseOptions();
|
||||
}
|
||||
|
||||
private Rect BoundsInPopupParent(RectTransform rect)
|
||||
private Rect BoundsIn(RectTransform rect, Transform parent)
|
||||
{
|
||||
rect.GetWorldCorners(_corners);
|
||||
var min = new Vector2(float.PositiveInfinity, float.PositiveInfinity);
|
||||
var max = new Vector2(float.NegativeInfinity, float.NegativeInfinity);
|
||||
foreach (var corner in _corners)
|
||||
{
|
||||
var point = _panel.parent.InverseTransformPoint(corner);
|
||||
var point = parent.InverseTransformPoint(corner);
|
||||
min = Vector2.Min(min, point);
|
||||
max = Vector2.Max(max, point);
|
||||
}
|
||||
return Rect.MinMaxRect(min.x, min.y, max.x, max.y);
|
||||
}
|
||||
|
||||
private void PositionEntry()
|
||||
{
|
||||
var parent = _inspector.descGroupTrans;
|
||||
var label = BoundsIn(_descriptionLabel.rectTransform, parent);
|
||||
var labelWidth = parent.InverseTransformVector(_descriptionLabel.transform.TransformVector(
|
||||
new Vector3(Util.GetPreferredWidth(_descriptionLabel), 0, 0))).x;
|
||||
var left = label.xMin + labelWidth + 8;
|
||||
var right = Math.Min(parent.rect.xMax, BoundsIn(_descriptionIcon, parent).xMin) - 8;
|
||||
var entryRect = (RectTransform)_entry.transform;
|
||||
var visible = _inspector.blueprint != null && left + entryRect.rect.width <= right;
|
||||
_entry.gameObject.SetActive(visible);
|
||||
if (!visible) return;
|
||||
Util.NormalizeRectWithTopLeft(_entry, left - parent.rect.xMin,
|
||||
parent.rect.yMax - label.yMax, parent);
|
||||
}
|
||||
|
||||
private void PositionOptions()
|
||||
{
|
||||
if (!_optionsOpen) return;
|
||||
@@ -287,18 +320,23 @@ public class BlueprintDescriptionPatch : PatchImpl<BlueprintDescriptionPatch>
|
||||
CloseOptions();
|
||||
return;
|
||||
}
|
||||
var anchor = BoundsInPopupParent((RectTransform)_entry.transform);
|
||||
var screen = BoundsInPopupParent((RectTransform)_canvas.transform);
|
||||
if (!anchor.Overlaps(screen) || _viewport && !anchor.Overlaps(BoundsInPopupParent(_viewport)))
|
||||
var anchor = BoundsIn((RectTransform)_entry.transform, _panel.parent);
|
||||
var screen = BoundsIn((RectTransform)_canvas.transform, _panel.parent);
|
||||
if (!anchor.Overlaps(screen) || _viewport && !anchor.Overlaps(BoundsIn(_viewport, _panel.parent)))
|
||||
{
|
||||
CloseOptions();
|
||||
return;
|
||||
}
|
||||
var combo = _basisCombo.GetComponent<UIComboBox>();
|
||||
var extra = combo.isDroppedDown ? combo.DropDownCount * combo.ItemHeight + 2 * combo.ItemPadding + 2 : 0;
|
||||
var footerTop = 112 + extra;
|
||||
_panel.sizeDelta = new Vector2(_panel.sizeDelta.x, footerTop + 30 + Left);
|
||||
Util.NormalizeRectWithTopRight(_button, Left, footerTop, _panel);
|
||||
var comboRect = (RectTransform)_basisCombo.transform;
|
||||
var footerTop = -comboRect.anchoredPosition.y + comboRect.rect.height + 16 + extra;
|
||||
var image = _panel.GetComponent<Image>();
|
||||
var padding = UnityEngine.Sprites.DataUtility.GetPadding(image.sprite) / image.pixelsPerUnit;
|
||||
var right = Mathf.Ceil(padding.z) + Left;
|
||||
var bottom = Mathf.Ceil(padding.y) + Left;
|
||||
_panel.sizeDelta = new Vector2(_panel.sizeDelta.x, footerTop + ((RectTransform)_button.transform).rect.height + bottom);
|
||||
Util.NormalizeRectWithTopRight(_button, right, footerTop, _panel);
|
||||
if (!TryGetPopupPosition(anchor, screen, _panel.sizeDelta, out var position))
|
||||
{
|
||||
CloseOptions();
|
||||
@@ -328,7 +366,7 @@ public class BlueprintDescriptionPatch : PatchImpl<BlueprintDescriptionPatch>
|
||||
{
|
||||
_entryText.text = I18NKeys.BlueprintDescriptionOptions.Translate();
|
||||
((RectTransform)_entry.transform).sizeDelta =
|
||||
new Vector2(Util.GetPreferredWidth(_entryText) + 16, 20);
|
||||
new Vector2(Util.GetPreferredWidth(_entryText) + 20, 22);
|
||||
_entry.tips.tipTitle = I18NKeys.BlueprintAutoFillDescription.Translate();
|
||||
_entry.tips.tipText = I18NKeys.BlueprintDescriptionOptionsTip.Translate();
|
||||
_entry.UpdateTip();
|
||||
@@ -342,27 +380,54 @@ public class BlueprintDescriptionPatch : PatchImpl<BlueprintDescriptionPatch>
|
||||
_basisHelp.tips.tipTitle = I18NKeys.BlueprintProductionBasis.Translate();
|
||||
_basisHelp.tips.tipText = I18NKeys.BlueprintProductionBasisTip.Translate();
|
||||
_basisHelp.UpdateTip();
|
||||
var width = Math.Max(90f, Util.GetPreferredWidth(_buttonText) + 20f);
|
||||
((RectTransform)_button.transform).sizeDelta = new Vector2(width, 30);
|
||||
var image = _panel.GetComponent<Image>();
|
||||
// Sliced window artwork has transparent borders; content margins start inside its visible fill.
|
||||
var padding = UnityEngine.Sprites.DataUtility.GetPadding(image.sprite) / image.pixelsPerUnit;
|
||||
var left = Mathf.Ceil(padding.x) + Left;
|
||||
var right = Mathf.Ceil(padding.z) + Left;
|
||||
var top = Mathf.Ceil(padding.w) + 10;
|
||||
var titleHeight = Math.Max(30, Mathf.Ceil(_title.preferredHeight) + 4);
|
||||
Util.NormalizeRectWithTopLeft(_title, left, top, _panel);
|
||||
Util.NormalizeRectWithTopRight(_closeButton, right, top, _panel);
|
||||
var rowTop = top + titleHeight + 8;
|
||||
var labelHeight = Math.Max(26, Mathf.Ceil(_checkbox.labelText.preferredHeight) + 4);
|
||||
Util.NormalizeRectWithTopLeft(_checkbox, left, rowTop + (labelHeight - 20) / 2, _panel);
|
||||
var checkLabel = _checkbox.labelText.rectTransform;
|
||||
checkLabel.anchorMin = checkLabel.anchorMax = new Vector2(0, 0.5f);
|
||||
checkLabel.pivot = new Vector2(0, 0.5f);
|
||||
checkLabel.anchoredPosition = new Vector2(28, 0);
|
||||
checkLabel.sizeDelta = new Vector2(Util.GetPreferredWidth(_checkbox.labelText) + 4, labelHeight);
|
||||
rowTop += labelHeight + 8;
|
||||
var labelWidth = Util.GetPreferredWidth(_basisLabel);
|
||||
_basisLabel.rectTransform.sizeDelta = new Vector2(labelWidth, 28);
|
||||
var helpRect = (RectTransform)_basisHelp.transform;
|
||||
Util.NormalizeRectWithTopLeft(_basisHelp, Left + labelWidth + ControlsGap,
|
||||
68 + (28 - helpRect.rect.height) / 2, _panel);
|
||||
var comboLeft = Left + labelWidth + ControlsGap + helpRect.rect.width + ControlsGap;
|
||||
var itemText = combo.m_ListItemRes.GetComponentInChildren<Text>();
|
||||
var comboWidth = 180f;
|
||||
var textHeight = 0f;
|
||||
foreach (var key in new[] { I18NKeys.BlueprintFinalProducts, I18NKeys.BlueprintBottleneckIntermediate })
|
||||
{
|
||||
combo.m_Text.text = key.Translate();
|
||||
itemText.text = key.Translate();
|
||||
comboWidth = Math.Max(comboWidth, Util.GetPreferredWidth(combo.m_Text) + 40);
|
||||
textHeight = Math.Max(textHeight, Math.Max(combo.m_Text.preferredHeight, itemText.preferredHeight));
|
||||
}
|
||||
_basisCombo.SetIndex(index);
|
||||
Util.NormalizeRectWithTopLeft(_basisCombo, comboLeft, 68, _panel);
|
||||
_basisCombo.SetSize(comboWidth, 28);
|
||||
var popupWidth = Math.Max(320, Math.Max(comboLeft + comboWidth + Left,
|
||||
Util.GetPreferredWidth(_title) + Left * 2 + 50));
|
||||
combo.ItemHeight = (int)Math.Max(24, Mathf.Ceil(textHeight) + 4);
|
||||
var rowHeight = Math.Max(28, Math.Max(Mathf.Ceil(_basisLabel.preferredHeight), Mathf.Ceil(textHeight)) + 4);
|
||||
Util.NormalizeRectWithTopLeft(_basisLabel, left, rowTop, _panel);
|
||||
_basisLabel.rectTransform.sizeDelta = new Vector2(labelWidth, rowHeight);
|
||||
var helpRect = (RectTransform)_basisHelp.transform;
|
||||
Util.NormalizeRectWithTopLeft(_basisHelp, left + labelWidth + ControlsGap,
|
||||
rowTop + (rowHeight - helpRect.rect.height) / 2, _panel);
|
||||
var comboLeft = left + labelWidth + ControlsGap + helpRect.rect.width + ControlsGap;
|
||||
Util.NormalizeRectWithTopLeft(_basisCombo, comboLeft, rowTop, _panel);
|
||||
_basisCombo.SetSize(comboWidth, rowHeight);
|
||||
var width = Math.Max(90f, Util.GetPreferredWidth(_buttonText) + 20);
|
||||
((RectTransform)_button.transform).sizeDelta = new Vector2(width,
|
||||
Math.Max(30, Mathf.Ceil(_buttonText.preferredHeight) + 8));
|
||||
var popupWidth = Math.Max(320, Math.Max(comboLeft + comboWidth + right,
|
||||
Util.GetPreferredWidth(_title) + left + right + 50));
|
||||
_panel.sizeDelta = new Vector2(popupWidth, _panel.sizeDelta.y);
|
||||
_title.rectTransform.sizeDelta = new Vector2(popupWidth - Left * 2 - 50, 24);
|
||||
_title.rectTransform.sizeDelta = new Vector2(popupWidth - left - right - 50, titleHeight);
|
||||
PositionEntry();
|
||||
PositionOptions();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user