using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; namespace HexaCrazy { public static class HexUI { public static TMP_FontAsset Display, Body; public static Action Tap; public static Color Cream = ColorOf("FFF0CC"), Gold=ColorOf("F5CB76"), Muted=ColorOf("CCB790"); static readonly Dictionary sprites=new Dictionary(); public static readonly string[] Symbols={"01_Cherries","02_Lemon","03_Seven","04_Grapes","05_Bell","06_Watermelon","08_Star","09_Strawberry"}; public static readonly string[] Chips={"chip_red","chip_yellow","chip_violet","chip_magenta","chip_orange","chip_green","chip_blue","chip_teal"}; public static readonly string[] ItemIcons={"hammer","swap","bonus","lock"}; public static float HexHeightRatio => Sprite("well").rect.height/Sprite("well").rect.width; public static bool Resizable(string asset) => asset=="card" || asset=="panel" || asset=="primary" || asset=="bar" || asset=="badge" || asset=="dock_frame" || asset=="wallet_frame" || asset=="switch_track" || asset=="nickname_field"; public static Color ColorOf(string value) { ColorUtility.TryParseHtmlString("#"+value,out var c); return c; } public static Sprite Sprite(string name) { if(!sprites.TryGetValue(name,out var s)) { s=Resources.Load("HexaArt/"+name); if(s!=null) sprites[name]=s; } return s; } public static RectTransform Node(Transform parent,string name,float x,float y,float w,float h) { var go=new GameObject(name,typeof(RectTransform)); var rt=go.GetComponent(); rt.SetParent(parent,false); rt.anchorMin=rt.anchorMax=new Vector2(0,1); rt.pivot=new Vector2(.5f,.5f); rt.anchoredPosition=new Vector2(x,-y); rt.sizeDelta=new Vector2(w,h); return rt; } public static Image Image(Transform parent,string name,string asset,float x,float y,float w,float h,Color? color=null,bool sliced=false) { var rt=Node(parent,name,x,y,w,h); var im=rt.gameObject.AddComponent(); im.sprite=asset==null?null:Sprite(asset); im.color=color??Color.white; im.raycastTarget=false; if(im.sprite!=null) { if((sliced||Resizable(asset))&&im.sprite.border.sqrMagnitude>0) { im.type=UnityEngine.UI.Image.Type.Sliced; // Endcaps scale uniformly to the requested height; only the middle extends. im.pixelsPerUnitMultiplier=asset=="card"||asset=="panel"?2:im.sprite.rect.height/Mathf.Max(1,h); } else im.preserveAspect=true; if(asset=="ornament") { float naturalWidth=h*im.sprite.rect.width/im.sprite.rect.height; float line=(w-naturalWidth)*.5f-8; if(line>10){Image(rt,"RuleLeft",null,line*.5f,h*.5f,line,1.2f,Gold);Image(rt,"RuleRight",null,w-line*.5f,h*.5f,line,1.2f,Gold);} } } return im; } public static TextMeshProUGUI Text(Transform parent,string name,string value,float x,float y,float w,float h,float size=24,bool display=false,Color? color=null) { var rt=Node(parent,name,x,y,w,h); var t=rt.gameObject.AddComponent(); t.font=display?Display:Body; t.text=value; t.fontSize=size; t.color=color??Cream; t.alignment=TextAlignmentOptions.Center; t.raycastTarget=false; t.textWrappingMode=TextWrappingModes.Normal; t.overflowMode=TextOverflowModes.Truncate; t.margin=Vector4.zero; t.enableAutoSizing=true; t.fontSizeMax=size; t.fontSizeMin=Mathf.Min(size,Mathf.Max(16,size*.70f)); if(display) { t.enableVertexGradient=true; t.colorGradient=new VertexGradient(ColorOf("FFF5D3"),ColorOf("FFF5D3"),ColorOf("D9A03C"),ColorOf("D9A03C")); } return t; } public static Button Button(Transform parent,string name,string caption,float x,float y,float w,float h,Action action,string asset="button",float size=24,bool display=false) { var im=Image(parent,name,asset,x,y,w,h,asset==null?Color.clear:Color.white,asset=="panel"||asset=="card"); im.raycastTarget=true; var b=im.gameObject.AddComponent