refactor(UXAssist): extract UI layout helpers from MyWindow

This commit is contained in:
2026-06-23 08:28:46 +08:00
parent 54c1700fcd
commit 06c3532acc
2 changed files with 92 additions and 54 deletions
+80
View File
@@ -0,0 +1,80 @@
using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using UXAssist.Common;
namespace UXAssist.UI;
public static class LayoutHelper
{
private static void AddElement(float x, float y, RectTransform rect, RectTransform parent = null)
{
if (rect != null)
{
Util.NormalizeRectWithTopLeft(rect, x, y, parent);
}
}
public static Text AddText(float x, float y, RectTransform parent, string label, int fontSize = 14, string objName = "label")
{
var src = UIRoot.instance.uiGame.assemblerWindow.stateText;
var txt = UnityEngine.Object.Instantiate(src);
txt.gameObject.name = objName;
txt.text = label.Translate();
txt.color = new Color(1f, 1f, 1f, 0.4f);
txt.alignment = TextAnchor.MiddleLeft;
txt.fontSize = fontSize;
txt.rectTransform.sizeDelta = new Vector2(txt.preferredWidth + 8f, txt.preferredHeight + 8f);
AddElement(x, y, txt.rectTransform, parent);
return txt;
}
public static UIButton AddTipsButton(float x, float y, RectTransform parent, string label, string tip, string content, string objName = "tips-button")
{
var src = UIRoot.instance.galaxySelect.sandboxToggle.gameObject.transform.parent.Find("tip-button");
var dst = UnityEngine.Object.Instantiate(src);
dst.gameObject.name = objName;
var btn = dst.GetComponent<UIButton>();
Util.NormalizeRectWithTopLeft(btn, x, y, parent);
btn.tips.topLevel = true;
btn.tips.tipTitle = label;
btn.tips.tipText = tip;
btn.UpdateTip();
return btn;
}
public static UIButton AddButton(float x, float y, RectTransform parent, string text = "", int fontSize = 16, string objName = "button", UnityAction onClick = null)
{
return AddButton(x, y, 150f, parent, text, fontSize, objName, onClick);
}
public static UIButton AddButton(float x, float y, float width, RectTransform parent, string text = "", int fontSize = 16, string objName = "button", UnityAction onClick = null)
{
var panel = UIRoot.instance.uiGame.statWindow.performancePanelUI;
var btn = UnityEngine.Object.Instantiate(panel.cpuActiveButton);
btn.gameObject.name = objName;
var rect = Util.NormalizeRectWithTopLeft(btn, x, y, parent);
rect.sizeDelta = new Vector2(width, rect.sizeDelta.y);
var l = btn.gameObject.transform.Find("button-text").GetComponent<Localizer>();
var t = btn.gameObject.transform.Find("button-text").GetComponent<Text>();
if (l != null)
{
l.stringKey = text;
l.translation = text.Translate();
}
if (t != null)
{
t.text = text.Translate();
}
t.fontSize = fontSize;
btn.tip = null;
btn.tips = new UIButton.TipSettings();
btn.button.onClick.RemoveAllListeners();
if (onClick != null) btn.button.onClick.AddListener(onClick);
return btn;
}
}
+12 -54
View File
@@ -115,53 +115,29 @@ public class MyWindow : ManualBehaviour
// }
}
private static void AddElement(float x, float y, RectTransform rect, RectTransform parent = null)
{
if (rect != null)
{
Util.NormalizeRectWithTopLeft(rect, x, y, parent);
}
}
[Obsolete("Use LayoutHelper")]
public static Text AddText(float x, float y, RectTransform parent, string label, int fontSize = 14, string objName = "label")
{
var src = UIRoot.instance.uiGame.assemblerWindow.stateText;
var txt = Instantiate(src);
txt.gameObject.name = objName;
txt.text = label.Translate();
txt.color = new Color(1f, 1f, 1f, 0.4f);
txt.alignment = TextAnchor.MiddleLeft;
txt.fontSize = fontSize;
txt.rectTransform.sizeDelta = new Vector2(txt.preferredWidth + 8f, txt.preferredHeight + 8f);
AddElement(x, y, txt.rectTransform, parent);
return txt;
return LayoutHelper.AddText(x, y, parent, label, fontSize, objName);
}
public Text AddText2(float x, float y, RectTransform parent, string label, int fontSize = 14, string objName = "label")
{
var text = AddText(x, y, parent, label, fontSize, objName);
var text = LayoutHelper.AddText(x, y, parent, label, fontSize, objName);
_maxX = Math.Max(_maxX, x + text.rectTransform.sizeDelta.x);
MaxY = Math.Max(MaxY, y + text.rectTransform.sizeDelta.y);
return text;
}
[Obsolete("Use LayoutHelper")]
public static UIButton AddTipsButton(float x, float y, RectTransform parent, string label, string tip, string content, string objName = "tips-button")
{
var src = UIRoot.instance.galaxySelect.sandboxToggle.gameObject.transform.parent.Find("tip-button");
var dst = Instantiate(src);
dst.gameObject.name = objName;
var btn = dst.GetComponent<UIButton>();
Util.NormalizeRectWithTopLeft(btn, x, y, parent);
btn.tips.topLevel = true;
btn.tips.tipTitle = label;
btn.tips.tipText = tip;
btn.UpdateTip();
return btn;
return LayoutHelper.AddTipsButton(x, y, parent, label, tip, content, objName);
}
public UIButton AddTipsButton2(float x, float y, RectTransform parent, string label, string tip, string content, string objName = "tips-button")
{
var tipsButton = AddTipsButton(x, y, parent, label, tip, content, objName);
var tipsButton = LayoutHelper.AddTipsButton(x, y, parent, label, tip, content, objName);
var rect = tipsButton.transform as RectTransform;
if (rect != null)
{
@@ -179,32 +155,14 @@ public class MyWindow : ManualBehaviour
public UIButton AddButton(float x, float y, float width, RectTransform parent, string text = "", int fontSize = 16, string objName = "button", UnityAction onClick = null)
{
var panel = UIRoot.instance.uiGame.statWindow.performancePanelUI;
var btn = Instantiate(panel.cpuActiveButton);
btn.gameObject.name = objName;
var rect = Util.NormalizeRectWithTopLeft(btn, x, y, parent);
rect.sizeDelta = new Vector2(width, rect.sizeDelta.y);
var l = btn.gameObject.transform.Find("button-text").GetComponent<Localizer>();
var t = btn.gameObject.transform.Find("button-text").GetComponent<Text>();
if (l != null)
var btn = LayoutHelper.AddButton(x, y, width, parent, text, fontSize, objName, onClick);
var rect = btn.transform as RectTransform;
if (rect != null)
{
l.stringKey = text;
l.translation = text.Translate();
_maxX = Math.Max(_maxX, x + rect.sizeDelta.x);
MaxY = Math.Max(MaxY, y + rect.sizeDelta.y);
}
if (t != null)
{
t.text = text.Translate();
}
t.fontSize = fontSize;
btn.tip = null;
btn.tips = new UIButton.TipSettings();
btn.button.onClick.RemoveAllListeners();
if (onClick != null) btn.button.onClick.AddListener(onClick);
_maxX = Math.Max(_maxX, x + rect.sizeDelta.x);
MaxY = Math.Max(MaxY, y + rect.sizeDelta.y);
return btn;
}
@@ -539,7 +497,7 @@ public class MyWindowWithTabs : MyWindow
public void AddTabGroup(RectTransform parent, string label, string objName = "tabl-group-label")
{
AddText(28, _tabY - 2, parent, label, 16, objName);
LayoutHelper.AddText(28, _tabY - 2, parent, label, 16, objName);
_tabY += 28f;
}