| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using System;
- using System.Collections;
- using System.Linq;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- using TMPro;
- namespace HexaCrazy
- {
- // HTML: transform .42s cubic-bezier(.22,.9,.24,1), side scale .68,
- // opacity .5, brightness .5, saturation .6, blur 1.6px.
- public sealed class HexLobbyCarousel : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler
- {
- public const float Duration=.42f, SideScale=.68f, SideOffset=522f*206f/276f;
- public int Selected {get;private set;}
- public bool IsAnimating => motion!=null;
- HexGameApp app; Card[] cards; Coroutine motion; Vector2 dragStart;
- sealed class Tint {public Graphic graphic;public Color color;public VertexGradient gradient;public bool hasGradient;}
- sealed class Card
- {
- public RectTransform rect;public CanvasGroup group;public Material material;
- public Tint[] tints;public float focus;
- }
- public void Initialize(HexGameApp owner,Button[] buttons,int selected)
- {
- app=owner;Selected=selected;var shader=Resources.Load<Shader>("HexCarousel");
- cards=buttons.Select(b=>
- {
- var c=new Card{rect=(RectTransform)b.transform,group=b.gameObject.AddComponent<CanvasGroup>(),material=new Material(shader)};
- c.material.name="Carousel card "+b.name;
- c.tints=b.GetComponentsInChildren<Graphic>().Select(g=>
- {
- var t=g as TextMeshProUGUI;
- if(t==null)g.material=c.material;
- return new Tint{graphic=g,color=g.color,hasGradient=t!=null&&t.enableVertexGradient,gradient=t!=null?t.colorGradient:new VertexGradient(Color.white)};
- }).ToArray();return c;
- }).ToArray();
- Snap();
- }
- static int Position(int index,int selected){int d=index-selected;return d>1?-1:d< -1?1:d;}
- public void Select(int index,bool immediate=false)
- {
- index=(index%3+3)%3;if(index==Selected&&!immediate)return;
- Selected=index;app.CarouselSelectionChanged(index);
- if(motion!=null){StopCoroutine(motion);motion=null;}
- cards[index].rect.SetAsLastSibling();
- if(immediate||app.Profile.Data.reduceMotion){Snap();return;}
- motion=StartCoroutine(Animate());
- }
- void Snap()
- {
- for(int i=0;i<cards.Length;i++)
- {
- int p=Position(i,Selected);cards[i].rect.anchoredPosition=new Vector2(375+p*SideOffset,-306);
- cards[i].rect.localScale=Vector3.one*(p==0?1:SideScale);Apply(cards[i],p==0?1:0);
- }
- cards[Selected].rect.SetAsLastSibling();
- }
- IEnumerator Animate()
- {
- var positions=cards.Select(c=>c.rect.anchoredPosition).ToArray();var scales=cards.Select(c=>c.rect.localScale.x).ToArray();var focuses=cards.Select(c=>c.focus).ToArray();
- for(float elapsed=0;elapsed<Duration;elapsed+=Time.unscaledDeltaTime)
- {
- float t=Mathf.Clamp01(elapsed/Duration),e=Ease(t);
- for(int i=0;i<cards.Length;i++)
- {
- int p=Position(i,Selected);cards[i].rect.anchoredPosition=Vector2.Lerp(positions[i],new Vector2(375+p*SideOffset,-306),e);
- cards[i].rect.localScale=Vector3.one*Mathf.Lerp(scales[i],p==0?1:SideScale,e);
- Apply(cards[i],Mathf.Lerp(focuses[i],p==0?1:0,e));
- }
- yield return null;
- }
- Snap();motion=null;
- }
- // Solve the CSS Bezier's x coordinate before evaluating y; this preserves its timing.
- static float Ease(float x)
- {
- float lo=0,hi=1,t=.5f;
- for(int i=0;i<14;i++){t=(lo+hi)*.5f;float u=1-t;float bx=3*u*u*t*.22f+3*u*t*t*.24f+t*t*t;if(bx<x)lo=t;else hi=t;}
- float v=1-t;return 3*v*v*t*.9f+3*v*t*t+t*t*t;
- }
- static Color Dim(Color original,float focus)
- {
- var c=Desaturate(original,focus);float brightness=Mathf.Lerp(.5f,1,focus);c.r*=brightness;c.g*=brightness;c.b*=brightness;return c;
- }
- static Color Desaturate(Color original,float focus){float grey=original.grayscale;return Color.Lerp(new Color(grey,grey,grey,original.a),original,Mathf.Lerp(.6f,1,focus));}
- static void Apply(Card card,float focus)
- {
- card.focus=focus;card.group.alpha=Mathf.Lerp(.5f,1,focus);
- card.material.SetFloat("_Saturation",Mathf.Lerp(.6f,1,focus));card.material.SetFloat("_Blur",1.6f*(1-focus));
- float brightness=Mathf.Lerp(.5f,1,focus);card.material.SetColor("_Color",new Color(brightness,brightness,brightness,1));
- foreach(var v in card.tints)
- {
- var text=v.graphic as TextMeshProUGUI;
- v.graphic.color=text!=null?Dim(v.color,focus):v.color;
- if(text!=null&&v.hasGradient)text.colorGradient=new VertexGradient(Desaturate(v.gradient.topLeft,focus),Desaturate(v.gradient.topRight,focus),Desaturate(v.gradient.bottomLeft,focus),Desaturate(v.gradient.bottomRight,focus));
- }
- }
- public void OnBeginDrag(PointerEventData e){dragStart=app.ScreenToDesign(e.position);e.eligibleForClick=false;}
- public void OnDrag(PointerEventData e){e.eligibleForClick=false;}
- public void OnEndDrag(PointerEventData e)
- {
- var delta=app.ScreenToDesign(e.position)-dragStart;
- if(!app.Paused&&Mathf.Abs(delta.x)>45&&Mathf.Abs(delta.x)>Mathf.Abs(delta.y)){HexUI.Tap?.Invoke();Select(Selected+(delta.x<0?1:-1));}
- }
- void OnDestroy(){if(cards!=null)foreach(var card in cards)if(card.material!=null)Destroy(card.material);}
- }
- }
|