using System; using TMPro; using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; namespace BuffaloSiege { public sealed class UIFactory { public readonly ArtCatalog Art;public Action ClickSound; public UIFactory(ArtCatalog a){Art=a;} public static RectTransform Root(string name,Transform parent) {var r=new GameObject(name,typeof(RectTransform)).GetComponent();r.SetParent(parent,false);return r;} public static void Fill(RectTransform r,float left=0,float top=0,float right=0,float bottom=0) {r.anchorMin=Vector2.zero;r.anchorMax=Vector2.one;r.pivot=new Vector2(.5f,.5f);r.offsetMin=new Vector2(left,bottom);r.offsetMax=new Vector2(-right,-top);} public static void Place(RectTransform r,float x,float y,float w,float h) {r.anchorMin=r.anchorMax=new Vector2(0,1);r.pivot=new Vector2(0,1);r.anchoredPosition=new Vector2(x,-y);r.sizeDelta=new Vector2(w,h);} GameObject Make(string name,Transform parent,GameObject prefab) {var o=prefab?UnityEngine.Object.Instantiate(prefab,parent,false):new GameObject(name,typeof(RectTransform));o.name=name;if(!prefab)o.transform.SetParent(parent,false);o.SetActive(true);return o;} public Image Panel(Transform parent,string name,float x,float y,float w,float h,string sprite="ui.panel.frame") {var o=Make(name,parent,Art.panelPrefab);var im=o.GetComponent()??o.AddComponent();im.sprite=Art.Get(sprite);im.type=UnityEngine.UI.Image.Type.Sliced;im.pixelsPerUnitMultiplier=sprite=="ui.button.wood"?2.5f:1;im.raycastTarget=false;im.color=Color.white;Place((RectTransform)o.transform,x,y,w,h);return im;} public Image Image(Transform parent,string id,float x,float y,float w,float h,bool preserve=true) {var r=Root(id,parent);Place(r,x,y,w,h);var im=r.gameObject.AddComponent();im.sprite=Art.Get(id);im.preserveAspect=preserve;im.raycastTarget=false;return im;} public Image Solid(Transform parent,string name,Color color,float x,float y,float w,float h) {var r=Root(name,parent);Place(r,x,y,w,h);var im=r.gameObject.AddComponent();im.color=color;im.raycastTarget=false;return im;} public TMP_Text Text(Transform parent,string value,float x,float y,float w,float h,float size=28,TextAlignmentOptions align=TextAlignmentOptions.MidlineLeft,Color? color=null,bool small=false) { var o=Make("Text_"+(value.Length>24?value.Substring(0,24):value),parent,Art.textPrefab);Place((RectTransform)o.transform,x,y,w,h);var t=o.GetComponent()??o.AddComponent(); t.font=small?Art.body:Art.serif;t.fontSize=size;t.fontSizeMax=size;t.fontSizeMin=size*.82f;t.enableAutoSizing=true;t.text=value;t.alignment=align;t.color=color??Art.cream;t.raycastTarget=false;t.margin=new Vector4(2,0,2,0);t.textWrappingMode=TextWrappingModes.Normal;t.overflowMode=TextOverflowModes.Ellipsis;return t; } public Button Button(Transform parent,string label,float x,float y,float w,float h,Action action,bool gold=false,float size=28) { var o=Make("Button_"+label,parent,Art.buttonPrefab);Place((RectTransform)o.transform,x,y,w,h);var im=o.GetComponent()??o.AddComponent();im.sprite=Art.Get(gold?"ui.button.gold":"ui.button.wood");im.type=UnityEngine.UI.Image.Type.Sliced;im.pixelsPerUnitMultiplier=gold?1:2.5f;im.raycastTarget=true; var b=o.GetComponent