using System; using TMPro; using UnityEngine; using UnityEngine.UI; namespace Fateforge { public static class Ui { public static RectTransform Rect(Transform parent,string name,float x,float y,float width,float height) {var go=new GameObject(name,typeof(RectTransform));go.transform.SetParent(parent,false);var rect=go.GetComponent();Place(rect,x,y,width,height);return rect;} public static void Place(RectTransform rect,float x,float y,float width,float height) {rect.anchorMin=rect.anchorMax=new Vector2(.5f,1);rect.pivot=new Vector2(.5f,1);rect.anchoredPosition=new Vector2(x,-y);rect.sizeDelta=new Vector2(width,height);} public static void Stretch(RectTransform r,float left=0,float top=0,float right=0,float bottom=0) {r.anchorMin=Vector2.zero;r.anchorMax=Vector2.one;r.offsetMin=new Vector2(left,bottom);r.offsetMax=new Vector2(-right,-top);} public static Image Image(RectTransform rect,Sprite sprite,Color color,bool raycast=false) {var image=rect.gameObject.AddComponent();image.sprite=sprite;image.color=color;image.raycastTarget=raycast;if(sprite!=null&&sprite.border!=Vector4.zero)image.type=UnityEngine.UI.Image.Type.Sliced;return image;} public static TMP_Text Text(Transform parent,string name,string value,float x,float y,float w,float h,float size,UiTheme theme,TextAlignmentOptions align=TextAlignmentOptions.Center) {var rect=Rect(parent,name,x,y,w,h);var text=rect.gameObject.AddComponent();text.font=theme.bodyFont;text.text=value;text.fontSize=size;text.enableAutoSizing=true;text.fontSizeMin=size*.75f;text.fontSizeMax=size;text.color=theme.text;text.alignment=align;text.textWrappingMode=TextWrappingModes.Normal;text.overflowMode=TextOverflowModes.Ellipsis;text.raycastTarget=false;return text;} public static Button Button(Transform parent,string name,string label,float x,float y,float w,float h,UiTheme theme,Action action=null,bool primary=false) {var rect=Rect(parent,name,x,y,w,h);Image(rect,null,Color.clear,true);var surface=Rect(rect,"Surface",0,0,w,h);Stretch(surface);var image=Image(surface,theme.panelSprite,Color.white);var button=rect.gameObject.AddComponent