mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2026-08-05 15:20:15 +08:00
refactor(UXAssist): extract UI layout helpers from MyWindow
This commit is contained in:
@@ -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
@@ -115,53 +115,29 @@ public class MyWindow : ManualBehaviour
|
|||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void AddElement(float x, float y, RectTransform rect, RectTransform parent = null)
|
[Obsolete("Use LayoutHelper")]
|
||||||
{
|
|
||||||
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")
|
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;
|
return LayoutHelper.AddText(x, y, parent, label, fontSize, objName);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Text AddText2(float x, float y, RectTransform parent, string label, int fontSize = 14, string objName = "label")
|
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);
|
_maxX = Math.Max(_maxX, x + text.rectTransform.sizeDelta.x);
|
||||||
MaxY = Math.Max(MaxY, y + text.rectTransform.sizeDelta.y);
|
MaxY = Math.Max(MaxY, y + text.rectTransform.sizeDelta.y);
|
||||||
return text;
|
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")
|
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");
|
return LayoutHelper.AddTipsButton(x, y, parent, label, tip, content, objName);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public UIButton AddTipsButton2(float x, float y, RectTransform parent, string label, string tip, string content, string objName = "tips-button")
|
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;
|
var rect = tipsButton.transform as RectTransform;
|
||||||
if (rect != null)
|
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)
|
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 = LayoutHelper.AddButton(x, y, width, parent, text, fontSize, objName, onClick);
|
||||||
var btn = Instantiate(panel.cpuActiveButton);
|
var rect = btn.transform as RectTransform;
|
||||||
btn.gameObject.name = objName;
|
if (rect != null)
|
||||||
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);
|
|
||||||
|
|
||||||
_maxX = Math.Max(_maxX, x + rect.sizeDelta.x);
|
_maxX = Math.Max(_maxX, x + rect.sizeDelta.x);
|
||||||
MaxY = Math.Max(MaxY, y + rect.sizeDelta.y);
|
MaxY = Math.Max(MaxY, y + rect.sizeDelta.y);
|
||||||
|
}
|
||||||
|
|
||||||
return btn;
|
return btn;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -539,7 +497,7 @@ public class MyWindowWithTabs : MyWindow
|
|||||||
|
|
||||||
public void AddTabGroup(RectTransform parent, string label, string objName = "tabl-group-label")
|
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;
|
_tabY += 28f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user