HexUI.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6. namespace HexaCrazy
  7. {
  8. public static class HexUI
  9. {
  10. public static TMP_FontAsset Display, Body;
  11. public static Action Tap;
  12. public static Color Cream = ColorOf("FFF0CC"), Gold=ColorOf("F5CB76"), Muted=ColorOf("CCB790");
  13. static readonly Dictionary<string,Sprite> sprites=new Dictionary<string,Sprite>();
  14. public static readonly string[] Symbols={"01_Cherries","02_Lemon","03_Seven","04_Grapes","05_Bell","06_Watermelon","08_Star","09_Strawberry"};
  15. public static readonly string[] Chips={"chip_red","chip_yellow","chip_violet","chip_magenta","chip_orange","chip_green","chip_blue","chip_teal"};
  16. public static readonly string[] ItemIcons={"hammer","swap","bonus","lock"};
  17. public static float HexHeightRatio => Sprite("well").rect.height/Sprite("well").rect.width;
  18. public static bool Resizable(string asset) =>
  19. asset=="card" || asset=="panel" || asset=="primary" || asset=="bar" || asset=="badge" ||
  20. asset=="dock_frame" || asset=="wallet_frame" || asset=="switch_track" || asset=="nickname_field";
  21. public static Color ColorOf(string value) { ColorUtility.TryParseHtmlString("#"+value,out var c); return c; }
  22. public static Sprite Sprite(string name) { if(!sprites.TryGetValue(name,out var s)) { s=Resources.Load<Sprite>("HexaArt/"+name); if(s!=null) sprites[name]=s; } return s; }
  23. public static RectTransform Node(Transform parent,string name,float x,float y,float w,float h)
  24. {
  25. var go=new GameObject(name,typeof(RectTransform)); var rt=go.GetComponent<RectTransform>(); rt.SetParent(parent,false);
  26. 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;
  27. }
  28. public static Image Image(Transform parent,string name,string asset,float x,float y,float w,float h,Color? color=null,bool sliced=false)
  29. {
  30. var rt=Node(parent,name,x,y,w,h); var im=rt.gameObject.AddComponent<Image>(); im.sprite=asset==null?null:Sprite(asset); im.color=color??Color.white; im.raycastTarget=false;
  31. if(im.sprite!=null)
  32. {
  33. if((sliced||Resizable(asset))&&im.sprite.border.sqrMagnitude>0)
  34. {
  35. im.type=UnityEngine.UI.Image.Type.Sliced;
  36. // Endcaps scale uniformly to the requested height; only the middle extends.
  37. im.pixelsPerUnitMultiplier=asset=="card"||asset=="panel"?2:im.sprite.rect.height/Mathf.Max(1,h);
  38. }
  39. else im.preserveAspect=true;
  40. if(asset=="ornament")
  41. {
  42. float naturalWidth=h*im.sprite.rect.width/im.sprite.rect.height;
  43. float line=(w-naturalWidth)*.5f-8;
  44. 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);}
  45. }
  46. }
  47. return im;
  48. }
  49. 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)
  50. {
  51. var rt=Node(parent,name,x,y,w,h); var t=rt.gameObject.AddComponent<TextMeshProUGUI>(); t.font=display?Display:Body;
  52. t.text=value; t.fontSize=size; t.color=color??Cream; t.alignment=TextAlignmentOptions.Center; t.raycastTarget=false;
  53. t.textWrappingMode=TextWrappingModes.Normal; t.overflowMode=TextOverflowModes.Truncate; t.margin=Vector4.zero;
  54. t.enableAutoSizing=true; t.fontSizeMax=size; t.fontSizeMin=Mathf.Min(size,Mathf.Max(16,size*.70f));
  55. if(display) { t.enableVertexGradient=true; t.colorGradient=new VertexGradient(ColorOf("FFF5D3"),ColorOf("FFF5D3"),ColorOf("D9A03C"),ColorOf("D9A03C")); }
  56. return t;
  57. }
  58. 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)
  59. {
  60. var im=Image(parent,name,asset,x,y,w,h,asset==null?Color.clear:Color.white,asset=="panel"||asset=="card"); im.raycastTarget=true;
  61. var b=im.gameObject.AddComponent<Button>(); var colors=b.colors; colors.highlightedColor=new Color(1,1,.9f); colors.pressedColor=new Color(.75f,.63f,.72f); colors.disabledColor=new Color(.5f,.45f,.48f,.75f); b.colors=colors;
  62. b.onClick.AddListener(()=>{Tap?.Invoke();action?.Invoke();});
  63. if(!string.IsNullOrEmpty(caption)) Text(im.transform,"Label",caption,w/2,h/2,w-16,h-10,size,display);
  64. return b;
  65. }
  66. public static Button IconButton(Transform p,string name,string icon,float x,float y,float size,Action action)
  67. { var b=Button(p,name,"",x,y,size,size,action); Image(b.transform,"Icon","icon_"+icon,size/2,size/2,size*.52f,size*.52f); return b; }
  68. public static HexProgress Fill(Transform p,string name,float x,float y,float w,float h)
  69. {var fill=Node(p,name,x,y,w,h).gameObject.AddComponent<HexProgress>();fill.raycastTarget=false;return fill;}
  70. public static RectTransform Tile(Transform p,int color,int count,float x,float y,float width,bool countLabel=true)
  71. {
  72. var rt=Node(p,"Stack_"+color,x,y,width,width*HexHeightRatio); float height=width*HexHeightRatio,dep=Math.Min(width*.045f,80f/Math.Max(1,count-1));
  73. for(int k=0;k<count;k++) Image(rt,"Layer_"+k,Chips[color],width/2,height/2-k*dep,width,height);
  74. Image(rt,"OriginalSymbol",Symbols[color],width/2,height*.43f-(count-1)*dep,width*.64f,width*.62f);
  75. if(countLabel) { Image(rt,"CountBadge","badge",width/2,height*.83f-(count-1)*dep,width*.4f,height*.26f); Text(rt,"Count","×"+count,width/2,height*.83f-(count-1)*dep,width*.4f,height*.24f,width*.18f); }
  76. return rt;
  77. }
  78. public static void Clear(Transform parent)
  79. { for(int i=parent.childCount-1;i>=0;i--) { var g=parent.GetChild(i).gameObject; g.SetActive(false); if(Application.isPlaying) UnityEngine.Object.Destroy(g); else UnityEngine.Object.DestroyImmediate(g); } }
  80. }
  81. // A resolution-independent gold capsule keeps its round caps at every score.
  82. [RequireComponent(typeof(CanvasRenderer))]
  83. public sealed class HexProgress : MaskableGraphic
  84. {
  85. float value;
  86. float shine=-1,flash;public bool FeedbackStyle;
  87. public float Shine {get=>shine;set{shine=value;SetVerticesDirty();}}
  88. public float Flash {get=>flash;set{flash=value;SetVerticesDirty();}}
  89. public float Value {get=>value;set{this.value=Mathf.Clamp01(value);SetVerticesDirty();}}
  90. protected override void OnPopulateMesh(VertexHelper mesh)
  91. {
  92. mesh.Clear();var rect=rectTransform.rect;float w=rect.width*value,h=rect.height;if(w<=0||h<=0)return;
  93. if(FeedbackStyle){DrawFeedback(mesh,rect,w,h);return;}
  94. float r=Mathf.Min(w,h)*.5f,x=rect.xMin,y=rect.yMin;
  95. var top=HexUI.ColorOf("FFF1A0");var bottom=HexUI.ColorOf("D9901E");
  96. Action<Vector2> add=p=>mesh.AddVert(p,Color.Lerp(bottom,top,Mathf.InverseLerp(y,y+h,p.y)),Vector2.zero);
  97. add(new Vector2(x+w*.5f,y+h*.5f));
  98. var corners=new[]{new Vector2(x+r,y+h-r),new Vector2(x+r,y+r),new Vector2(x+w-r,y+r),new Vector2(x+w-r,y+h-r)};
  99. for(int c=0;c<4;c++)for(int i=0;i<=8;i++){float angle=(90+c*90+i*90f/8)*Mathf.Deg2Rad;add(corners[c]+new Vector2(Mathf.Cos(angle),Mathf.Sin(angle))*r);}
  100. for(int i=1;i<=36;i++)mesh.AddTriangle(0,i,i==36?1:i+1);
  101. }
  102. void DrawFeedback(VertexHelper mesh,Rect rect,float w,float h)
  103. {
  104. float radius=Mathf.Min(w,h)*.5f;int columns=Mathf.Max(8,Mathf.CeilToInt(w/5));
  105. for(int i=0;i<=columns;i++)
  106. {
  107. float x=w*i/columns,extent=h*.5f;
  108. if(x<radius)extent=Mathf.Sqrt(Mathf.Max(0,radius*radius-(x-radius)*(x-radius)));
  109. else if(x>w-radius)extent=Mathf.Sqrt(Mathf.Max(0,radius*radius-(x-w+radius)*(x-w+radius)));
  110. float t=x/w;var c=Color.Lerp(HexUI.ColorOf("C9870F"),HexUI.ColorOf("FFF6D8"),Mathf.Pow(t,.7f));
  111. float sweep=Mathf.Clamp01(1-Mathf.Abs(t-shine)/.18f)*.65f;c=Color.Lerp(c,Color.white,Mathf.Clamp01(sweep+flash*.48f));
  112. var dark=Color.Lerp(c,HexUI.ColorOf("9B5A0B"),.28f);var light=Color.Lerp(c,Color.white,.24f);
  113. mesh.AddVert(new Vector3(rect.xMin+x,rect.center.y-extent),dark,Vector2.zero);mesh.AddVert(new Vector3(rect.xMin+x,rect.center.y+extent),light,Vector2.zero);
  114. if(i>0){int j=(i-1)*2;mesh.AddTriangle(j,j+1,j+2);mesh.AddTriangle(j+1,j+3,j+2);}
  115. }
  116. }
  117. }
  118. }