HexLobbyCarousel.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. using TMPro;
  8. namespace HexaCrazy
  9. {
  10. // HTML: transform .42s cubic-bezier(.22,.9,.24,1), side scale .68,
  11. // opacity .5, brightness .5, saturation .6, blur 1.6px.
  12. public sealed class HexLobbyCarousel : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler
  13. {
  14. public const float Duration=.42f, SideScale=.68f, SideOffset=522f*206f/276f;
  15. public int Selected {get;private set;}
  16. public bool IsAnimating => motion!=null;
  17. HexGameApp app; Card[] cards; Coroutine motion; Vector2 dragStart;
  18. sealed class Tint {public Graphic graphic;public Color color;public VertexGradient gradient;public bool hasGradient;}
  19. sealed class Card
  20. {
  21. public RectTransform rect;public CanvasGroup group;public Material material;
  22. public Tint[] tints;public float focus;
  23. }
  24. public void Initialize(HexGameApp owner,Button[] buttons,int selected)
  25. {
  26. app=owner;Selected=selected;var shader=Resources.Load<Shader>("HexCarousel");
  27. cards=buttons.Select(b=>
  28. {
  29. var c=new Card{rect=(RectTransform)b.transform,group=b.gameObject.AddComponent<CanvasGroup>(),material=new Material(shader)};
  30. c.material.name="Carousel card "+b.name;
  31. c.tints=b.GetComponentsInChildren<Graphic>().Select(g=>
  32. {
  33. var t=g as TextMeshProUGUI;
  34. if(t==null)g.material=c.material;
  35. return new Tint{graphic=g,color=g.color,hasGradient=t!=null&&t.enableVertexGradient,gradient=t!=null?t.colorGradient:new VertexGradient(Color.white)};
  36. }).ToArray();return c;
  37. }).ToArray();
  38. Snap();
  39. }
  40. static int Position(int index,int selected){int d=index-selected;return d>1?-1:d< -1?1:d;}
  41. public void Select(int index,bool immediate=false)
  42. {
  43. index=(index%3+3)%3;if(index==Selected&&!immediate)return;
  44. Selected=index;app.CarouselSelectionChanged(index);
  45. if(motion!=null){StopCoroutine(motion);motion=null;}
  46. cards[index].rect.SetAsLastSibling();
  47. if(immediate||app.Profile.Data.reduceMotion){Snap();return;}
  48. motion=StartCoroutine(Animate());
  49. }
  50. void Snap()
  51. {
  52. for(int i=0;i<cards.Length;i++)
  53. {
  54. int p=Position(i,Selected);cards[i].rect.anchoredPosition=new Vector2(375+p*SideOffset,-306);
  55. cards[i].rect.localScale=Vector3.one*(p==0?1:SideScale);Apply(cards[i],p==0?1:0);
  56. }
  57. cards[Selected].rect.SetAsLastSibling();
  58. }
  59. IEnumerator Animate()
  60. {
  61. 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();
  62. for(float elapsed=0;elapsed<Duration;elapsed+=Time.unscaledDeltaTime)
  63. {
  64. float t=Mathf.Clamp01(elapsed/Duration),e=Ease(t);
  65. for(int i=0;i<cards.Length;i++)
  66. {
  67. int p=Position(i,Selected);cards[i].rect.anchoredPosition=Vector2.Lerp(positions[i],new Vector2(375+p*SideOffset,-306),e);
  68. cards[i].rect.localScale=Vector3.one*Mathf.Lerp(scales[i],p==0?1:SideScale,e);
  69. Apply(cards[i],Mathf.Lerp(focuses[i],p==0?1:0,e));
  70. }
  71. yield return null;
  72. }
  73. Snap();motion=null;
  74. }
  75. // Solve the CSS Bezier's x coordinate before evaluating y; this preserves its timing.
  76. static float Ease(float x)
  77. {
  78. float lo=0,hi=1,t=.5f;
  79. 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;}
  80. float v=1-t;return 3*v*v*t*.9f+3*v*t*t+t*t*t;
  81. }
  82. static Color Dim(Color original,float focus)
  83. {
  84. var c=Desaturate(original,focus);float brightness=Mathf.Lerp(.5f,1,focus);c.r*=brightness;c.g*=brightness;c.b*=brightness;return c;
  85. }
  86. 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));}
  87. static void Apply(Card card,float focus)
  88. {
  89. card.focus=focus;card.group.alpha=Mathf.Lerp(.5f,1,focus);
  90. card.material.SetFloat("_Saturation",Mathf.Lerp(.6f,1,focus));card.material.SetFloat("_Blur",1.6f*(1-focus));
  91. float brightness=Mathf.Lerp(.5f,1,focus);card.material.SetColor("_Color",new Color(brightness,brightness,brightness,1));
  92. foreach(var v in card.tints)
  93. {
  94. var text=v.graphic as TextMeshProUGUI;
  95. v.graphic.color=text!=null?Dim(v.color,focus):v.color;
  96. 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));
  97. }
  98. }
  99. public void OnBeginDrag(PointerEventData e){dragStart=app.ScreenToDesign(e.position);e.eligibleForClick=false;}
  100. public void OnDrag(PointerEventData e){e.eligibleForClick=false;}
  101. public void OnEndDrag(PointerEventData e)
  102. {
  103. var delta=app.ScreenToDesign(e.position)-dragStart;
  104. 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));}
  105. }
  106. void OnDestroy(){if(cards!=null)foreach(var card in cards)if(card.material!=null)Destroy(card.material);}
  107. }
  108. }