| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- 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<string,Sprite> sprites=new Dictionary<string,Sprite>();
- 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<Sprite>("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<RectTransform>(); 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<Image>(); 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<TextMeshProUGUI>(); 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<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;
- b.onClick.AddListener(()=>{Tap?.Invoke();action?.Invoke();});
- if(!string.IsNullOrEmpty(caption)) Text(im.transform,"Label",caption,w/2,h/2,w-16,h-10,size,display);
- return b;
- }
- public static Button IconButton(Transform p,string name,string icon,float x,float y,float size,Action action)
- { 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; }
- public static HexProgress Fill(Transform p,string name,float x,float y,float w,float h)
- {var fill=Node(p,name,x,y,w,h).gameObject.AddComponent<HexProgress>();fill.raycastTarget=false;return fill;}
- public static RectTransform Tile(Transform p,int color,int count,float x,float y,float width,bool countLabel=true)
- {
- 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));
- for(int k=0;k<count;k++) Image(rt,"Layer_"+k,Chips[color],width/2,height/2-k*dep,width,height);
- Image(rt,"OriginalSymbol",Symbols[color],width/2,height*.43f-(count-1)*dep,width*.64f,width*.62f);
- 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); }
- return rt;
- }
- public static void Clear(Transform parent)
- { 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); } }
- }
- // A resolution-independent gold capsule keeps its round caps at every score.
- [RequireComponent(typeof(CanvasRenderer))]
- public sealed class HexProgress : MaskableGraphic
- {
- float value;
- float shine=-1,flash;public bool FeedbackStyle;
- public float Shine {get=>shine;set{shine=value;SetVerticesDirty();}}
- public float Flash {get=>flash;set{flash=value;SetVerticesDirty();}}
- public float Value {get=>value;set{this.value=Mathf.Clamp01(value);SetVerticesDirty();}}
- protected override void OnPopulateMesh(VertexHelper mesh)
- {
- mesh.Clear();var rect=rectTransform.rect;float w=rect.width*value,h=rect.height;if(w<=0||h<=0)return;
- if(FeedbackStyle){DrawFeedback(mesh,rect,w,h);return;}
- float r=Mathf.Min(w,h)*.5f,x=rect.xMin,y=rect.yMin;
- var top=HexUI.ColorOf("FFF1A0");var bottom=HexUI.ColorOf("D9901E");
- Action<Vector2> add=p=>mesh.AddVert(p,Color.Lerp(bottom,top,Mathf.InverseLerp(y,y+h,p.y)),Vector2.zero);
- add(new Vector2(x+w*.5f,y+h*.5f));
- 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)};
- 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);}
- for(int i=1;i<=36;i++)mesh.AddTriangle(0,i,i==36?1:i+1);
- }
- void DrawFeedback(VertexHelper mesh,Rect rect,float w,float h)
- {
- float radius=Mathf.Min(w,h)*.5f;int columns=Mathf.Max(8,Mathf.CeilToInt(w/5));
- for(int i=0;i<=columns;i++)
- {
- float x=w*i/columns,extent=h*.5f;
- if(x<radius)extent=Mathf.Sqrt(Mathf.Max(0,radius*radius-(x-radius)*(x-radius)));
- else if(x>w-radius)extent=Mathf.Sqrt(Mathf.Max(0,radius*radius-(x-w+radius)*(x-w+radius)));
- float t=x/w;var c=Color.Lerp(HexUI.ColorOf("C9870F"),HexUI.ColorOf("FFF6D8"),Mathf.Pow(t,.7f));
- float sweep=Mathf.Clamp01(1-Mathf.Abs(t-shine)/.18f)*.65f;c=Color.Lerp(c,Color.white,Mathf.Clamp01(sweep+flash*.48f));
- var dark=Color.Lerp(c,HexUI.ColorOf("9B5A0B"),.28f);var light=Color.Lerp(c,Color.white,.24f);
- 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);
- if(i>0){int j=(i-1)*2;mesh.AddTriangle(j,j+1,j+2);mesh.AddTriangle(j+1,j+3,j+2);}
- }
- }
- }
- }
|