WheelVisualDesign.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System.Globalization;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using DevilLink2.Gameplay;
  5. namespace DevilLink2.UI
  6. {
  7. // Wheel-only art direction, reading existing UI-bound values; no reward or ticket mutations.
  8. public sealed class WheelVisualDesign : MonoBehaviour
  9. {
  10. private const float WheelRadius=160,IconRadius=WheelRadius*.7125f,TextRadius=WheelRadius*.5f;
  11. private RectTransform[] inventoryIcons;private Text[] inventoryNames;
  12. private LuckyWheelScreen screen;private Text[] inventory;
  13. public static void Install(LuckyWheelScreen screen,GameplayArtCatalog art,LobbyArtCatalog shared)
  14. {
  15. if(screen.GetComponent<WheelVisualDesign>())return;
  16. var view=screen.gameObject.AddComponent<WheelVisualDesign>();view.screen=screen;view.inventory=new Text[3];view.inventoryIcons=new RectTransform[3];view.inventoryNames=new Text[3];
  17. var face=screen.Wheel.parent;
  18. var oldRim=face.Find("GoldRim");if(oldRim)oldRim.gameObject.SetActive(false);
  19. var cluster=UIBuilder.Rect("WheelDesignCluster",face,0,0,340,354);
  20. screen.Wheel.SetParent(cluster,false);UIBuilder.Place(screen.Wheel,10,22,320,320);
  21. screen.Wheel.pivot=new Vector2(.5f,.5f);screen.Wheel.anchoredPosition+=new Vector2(160,-160);
  22. for(int i=0;i<8;i++){
  23. var reward=RewardCatalog.Wheel(i+1);float angle=LuckyWheelScreen.SectorAngle(i+1);
  24. var group=UIBuilder.Rect("RewardGroup"+(i+1),screen.Wheel,WheelRadius,WheelRadius,0,0);
  25. group.pivot=new Vector2(.5f,.5f);group.localRotation=Quaternion.Euler(0,0,-angle);
  26. var label=screen.SectorLabels[i];label.transform.SetParent(group,false);
  27. CenterChild(label.rectTransform,new Vector2(0,TextRadius),new Vector2(54,26));
  28. label.horizontalOverflow=HorizontalWrapMode.Overflow;label.verticalOverflow=VerticalWrapMode.Overflow;
  29. label.fontSize=11;label.fontStyle=FontStyle.Bold;label.supportRichText=true;label.color=UIBuilder.Hex("F5E9D0");
  30. string primary=reward.Kind==RewardKind.Coins?reward.Amount.ToString("N0",CultureInfo.InvariantCulture):reward.Kind.ToString().ToUpperInvariant();
  31. string secondary=reward.Kind==RewardKind.Coins?"COINS":reward.Kind==RewardKind.Time?"+30s":"x"+reward.Amount;
  32. label.text="<size=11>"+primary+"</size>\n<size=10><color=#D3B67C>"+secondary+"</color></size>";
  33. var icon=screen.Wheel.Find("SectorIcon"+(i+1));
  34. if(icon){icon.SetParent(group,false);CenterChild((RectTransform)icon,new Vector2(0,IconRadius),new Vector2(26,26));}
  35. var divider=screen.Wheel.Find("SectorDivider"+i);if(divider)divider.gameObject.SetActive(false);
  36. float edge=i*45*Mathf.Deg2Rad;
  37. if(shared && shared.PointerGem)LobbyArtView.Picture("RimGem"+i,screen.Wheel,shared.PointerGem,156+Mathf.Sin(edge)*153,155-Mathf.Cos(edge)*153,8,10);
  38. }
  39. var arrow=face.Find("FixedPointer");if(arrow)arrow.gameObject.SetActive(false);
  40. var gem=face.Find("PointerGem");if(gem){gem.SetParent(cluster,false);UIBuilder.Place((RectTransform)gem,163,3,14,19);}
  41. var pointer=UIBuilder.Rect("WheelPointerMetal",cluster,142,-14,56,52).gameObject;
  42. pointer.AddComponent<CanvasRenderer>();pointer.AddComponent<WheelPointerGraphic>().raycastTarget=false;
  43. if(gem)gem.gameObject.SetActive(false); // The shield now contains its own faceted jewel.
  44. var spin=screen.SpinButton;
  45. if(spin is UIFeedbackButton feedbackButton)feedbackButton.PreserveAppearanceDuringModalLock=true;spin.transform.SetParent(cluster,false);UIBuilder.Place((RectTransform)spin.transform,130,142,80,80);
  46. // Single-layer diagnostic design: the root is the purple face; no inset or gold backing.
  47. var rim=spin.GetComponent<RoundedPanel>();rim.Radius=40;rim.color=Color.white;
  48. var gradient=spin.GetComponent<UIGradient>();if(gradient){gradient.enabled=false;}
  49. spin.gameObject.AddComponent<WheelSpinFaceLighting>();
  50. spin.targetGraphic=rim;spin.transition=Selectable.Transition.None;
  51. // Cancel a possible inherited ColorTint tween and restore neutral renderer tint once.
  52. rim.CrossFadeColor(Color.white,0,true,true);
  53. var ornament=UIBuilder.Rect("SpinOrnament",spin.transform,-8,-8,96,96);
  54. ornament.gameObject.AddComponent<CanvasRenderer>();ornament.gameObject.AddComponent<WheelSpinOrnament>().raycastTarget=false;
  55. var text=spin.GetComponentInChildren<Text>();UIBuilder.Place(text.rectTransform,0,0,80,80);text.fontSize=18;text.color=UIBuilder.LightGold;text.transform.SetAsLastSibling();
  56. UIBuilder.Place(screen.StatusLabel.rectTransform,12,64,340,26);screen.StatusLabel.fontSize=12;
  57. // One shared information bar, not three independent product cards.
  58. // Preserve the measurement slot; the compact backing sits naturally inside it.
  59. var balances=UIBuilder.Rect("WheelInventory",face,0,0,340,60);
  60. UIBuilder.Fill("InventoryBacking",balances,UIBuilder.Hex("392442"),UIBuilder.Hex("1D142B"),0,3,340,54,10);
  61. var sprites=new[]{art?art.Hint:null,art?art.Shuffle:null,art?art.Time:null};
  62. for(int i=0;i<3;i++){
  63. float width=340f/3;
  64. var column=UIBuilder.Rect("InventoryColumn"+i,balances,i*width,0,width,60);
  65. var image=LobbyArtView.Picture("InventoryIcon"+i,column,sprites[i],0,18,24,24);
  66. view.inventoryIcons[i]=image?image.rectTransform:null;
  67. view.inventory[i]=UIBuilder.Label("InventoryValue"+i,column,"",14,UIBuilder.Hex("F5E9D0"),0,26,40,22,TextAnchor.MiddleLeft);
  68. view.inventory[i].horizontalOverflow=HorizontalWrapMode.Overflow;view.inventory[i].verticalOverflow=VerticalWrapMode.Overflow;
  69. var name=UIBuilder.Label("InventoryName"+i,column,new[]{"HINT","SHUFFLE","TIME"}[i],9,UIBuilder.Hex("B5A0BF"),0,11,width,13);view.inventoryNames[i]=name;
  70. name.horizontalOverflow=HorizontalWrapMode.Overflow;
  71. }
  72. // Keep the bound source label for Refresh and other existing consumers, without duplicate drawing.
  73. screen.BalanceLabel.enabled=false;
  74. UIBuilder.Place(screen.FeedbackLabel.rectTransform,18,508,340,28);screen.FeedbackLabel.fontSize=15;screen.FeedbackLabel.fontStyle=FontStyle.Bold;screen.FeedbackLabel.alignment=TextAnchor.MiddleCenter;screen.FeedbackLabel.color=UIBuilder.Hex("E2BC75");
  75. var note=face.Find("Note") as RectTransform;if(note){UIBuilder.Place(note,12,618,340,18);var label=note.GetComponent<Text>();label.fontSize=9;label.color=UIBuilder.Hex("8F7D9B");}
  76. view.SyncInventory();view.RefreshLabelFacing();view.StyleFeedback();
  77. }
  78. private string lastBalance,lastFeedback;
  79. private void LateUpdate(){if(!screen)return;SyncInventory();RefreshLabelFacing();StyleFeedback();}
  80. private void StyleFeedback()
  81. {
  82. var label=screen.FeedbackLabel;if(lastFeedback==label.text)return;lastFeedback=label.text;
  83. // Preserve the fixed 28px slot: normal prompts get +25%; long error text keeps its old size.
  84. var settings=label.GetGenerationSettings(Vector2.zero);settings.fontSize=15;settings.resizeTextForBestFit=false;settings.horizontalOverflow=HorizontalWrapMode.Overflow;
  85. float width=label.cachedTextGeneratorForLayout.GetPreferredWidth(label.text,settings)/label.pixelsPerUnit;
  86. label.fontSize=width<=label.rectTransform.rect.width?15:12;
  87. }
  88. private void SyncInventory()
  89. {
  90. if(lastBalance==screen.BalanceLabel.text)return;lastBalance=screen.BalanceLabel.text;
  91. var values=lastBalance.Split('|');
  92. for(int i=0;i<inventory.Length;i++){string token=i+1<values.Length?values[i+1].Trim():"";int split=token.LastIndexOf(' ');inventory[i].text=split>=0?token.Substring(split+1):"—";LayoutInventory(i);}
  93. }
  94. private void LayoutInventory(int index)
  95. {
  96. var value=inventory[index];float width=((RectTransform)value.transform.parent).rect.width;
  97. float numberWidth=Mathf.Max(10,Mathf.Ceil(Mathf.Max(value.preferredWidth,inventoryNames[index].preferredWidth))+2);
  98. float left=(width-(24+6+numberWidth))*.5f;
  99. if(inventoryIcons[index])UIBuilder.Place(inventoryIcons[index],left,18,24,24);
  100. UIBuilder.Place(value.rectTransform,left+30,26,numberWidth,22);
  101. UIBuilder.Place(inventoryNames[index].rectTransform,left+30,11,numberWidth,13);inventoryNames[index].alignment=TextAnchor.MiddleLeft;
  102. }
  103. private static void CenterChild(RectTransform child,Vector2 position,Vector2 size)
  104. {
  105. child.anchorMin=child.anchorMax=new Vector2(.5f,.5f);child.pivot=new Vector2(.5f,.5f);
  106. child.anchoredPosition=position;child.sizeDelta=size;child.localRotation=Quaternion.identity;child.localScale=Vector3.one;
  107. }
  108. public const float ReadingCorrectionSeconds=.18f;
  109. private float lastSettledAngle=float.NaN,correctionElapsed;
  110. private RewardResult lastFacingResult;
  111. private bool correctionActive;
  112. private readonly float[] correctionFrom=new float[8],correctionTo=new float[8];
  113. public void PrepareForSpin()
  114. {
  115. // Called synchronously after service success, before the wheel animation starts moving.
  116. correctionActive=false;correctionElapsed=0;lastSettledAngle=float.NaN;lastFacingResult=null;
  117. if(!screen || screen.SectorLabels==null)return;
  118. for(int i=0;i<screen.SectorLabels.Length;i++)if(screen.SectorLabels[i])screen.SectorLabels[i].rectTransform.localRotation=Quaternion.identity;
  119. }
  120. private void RefreshLabelFacing()
  121. {
  122. if(screen.IsSpinning){correctionActive=false;return;}
  123. float settled=screen.Wheel.localEulerAngles.z;
  124. if(!Mathf.Approximately(settled,lastSettledAngle) || !ReferenceEquals(lastFacingResult,screen.LastResult)){
  125. lastSettledAngle=settled;lastFacingResult=screen.LastResult;correctionElapsed=0;
  126. for(int i=0;i<8;i++){
  127. var label=screen.SectorLabels[i].rectTransform;
  128. correctionFrom[i]=label.localEulerAngles.z;
  129. float angle=Mathf.DeltaAngle(0,settled-LuckyWheelScreen.SectorAngle(i+1));
  130. correctionTo[i]=Mathf.Abs(angle)>90.1f?180:0;
  131. }
  132. // Initial hidden construction is composed directly; every completed spin is eased.
  133. correctionActive=screen.LastResult!=null;
  134. ApplyTextFacing(correctionActive?0:1);return;
  135. }
  136. if(!correctionActive)return;
  137. correctionElapsed=Mathf.Min(ReadingCorrectionSeconds,correctionElapsed+Time.unscaledDeltaTime);
  138. float t=correctionElapsed/ReadingCorrectionSeconds;
  139. ApplyTextFacing(t*t*(3-2*t));
  140. if(t>=1)correctionActive=false;
  141. }
  142. private void ApplyTextFacing(float progress)
  143. {
  144. for(int i=0;i<8;i++)screen.SectorLabels[i].rectTransform.localRotation=Quaternion.Euler(0,0,Mathf.LerpAngle(correctionFrom[i],correctionTo[i],progress));
  145. }
  146. private void OnDisable()
  147. {
  148. // No delayed callbacks survive closing/rebinding; reopening measures the current labels anew.
  149. correctionActive=false;correctionElapsed=0;lastSettledAngle=float.NaN;lastFacingResult=null;
  150. }
  151. }
  152. [RequireComponent(typeof(CanvasRenderer))]
  153. internal sealed class WheelPointerGraphic : Image
  154. {
  155. protected override void OnEnable()
  156. {
  157. base.OnEnable();sprite=Resources.Load<Sprite>("WheelOrnaments/Wheel_Pointer_Ornate_R12") ?? Resources.Load<Sprite>("WheelOrnaments/Wheel_Pointer_Ornate");
  158. preserveAspect=true;raycastTarget=false;
  159. }
  160. protected override void OnPopulateMesh(VertexHelper mesh)
  161. {
  162. if(sprite){base.OnPopulateMesh(mesh);return;}
  163. WheelDecorationMesh.Pointer(mesh,GetPixelAdjustedRect());
  164. }
  165. }
  166. }