ShopProductVisuals.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace DevilLink2.UI
  4. {
  5. // Shop-only composition. Keeps all original bound texts, Buttons and purchase listeners.
  6. public sealed class ShopProductVisuals : MonoBehaviour
  7. {
  8. private ShopScreen shop;private RectTransform items,balance;private ShopTab lastTab;private bool styled;
  9. private float lastWidth=-1,lastBalanceWidth=-1;private string lastBalanceText;private RectTransform balanceHeader;
  10. public static void Install(ShopScreen shop,LobbyArtCatalog shared)
  11. {
  12. if(shop.GetComponent<ShopProductVisuals>())return;
  13. var view=shop.gameObject.AddComponent<ShopProductVisuals>();view.shop=shop;
  14. view.items=(RectTransform)shop.BuyButtons[0].transform.parent.parent;
  15. var text=shop.CoinsLabel;var r=text.rectTransform;var pos=r.anchoredPosition;var size=r.sizeDelta;
  16. // Keep the original economic text binding; Shop alone moves this group into its header.
  17. view.balance=UIBuilder.Rect("Balance",r.parent,pos.x,-pos.y,size.x,size.y);
  18. text.name="BalanceValue";r.SetParent(view.balance,false);
  19. var icon=LobbyArtView.Picture("ShopCoin",view.balance,shared?shared.CoinIcon:null,0,6,20,20);
  20. if(icon){var ir=icon.rectTransform;ir.anchorMin=ir.anchorMax=ir.pivot=new Vector2(1,1);ir.anchoredPosition=new Vector2(-144,-3);}
  21. r.anchorMin=r.anchorMax=r.pivot=new Vector2(1,1);r.anchoredPosition=Vector2.zero;r.sizeDelta=new Vector2(136,32);
  22. text.fontSize=14;text.fontStyle=FontStyle.Bold;text.color=UIBuilder.LightGold;text.alignment=TextAnchor.MiddleLeft;
  23. text.resizeTextForBestFit=true;text.resizeTextMinSize=8;text.resizeTextMaxSize=14;
  24. text.raycastTarget=false;if(icon)icon.raycastTarget=false;
  25. view.balance.gameObject.AddComponent<RectMask2D>();
  26. for(int i=0;i<3;i++){
  27. var card=shop.BuyButtons[i].transform.parent;
  28. var gradient=card.GetComponent<UIGradient>();if(gradient){gradient.Top=UIBuilder.Hex("352440");gradient.Bottom=UIBuilder.Hex("1D142C");card.GetComponent<Graphic>().SetVerticesDirty();}
  29. var trim=UIBuilder.Rect("ShopCardTrim",card,0,0,370,140);trim.anchorMin=Vector2.zero;trim.anchorMax=Vector2.one;trim.offsetMin=trim.offsetMax=Vector2.zero;
  30. trim.gameObject.AddComponent<CanvasRenderer>();trim.gameObject.AddComponent<ShopCardTrim>().raycastTarget=false;
  31. var name=card.Find("Name").GetComponent<Text>();name.fontSize=14;name.fontStyle=FontStyle.Bold;name.color=UIBuilder.LightGold;
  32. var desc=card.Find("Description").GetComponent<Text>();desc.fontSize=11;desc.color=UIBuilder.Hex("C6B8D4");desc.horizontalOverflow=HorizontalWrapMode.Wrap;
  33. shop.StockLabels[i].color=UIBuilder.Hex("AB9BB9");shop.PriceLabels[i].color=UIBuilder.Hex("F1CB85");shop.PriceLabels[i].fontStyle=FontStyle.Bold;
  34. var buy=shop.BuyButtons[i];var colors=buy.colors;colors.highlightedColor=colors.selectedColor=colors.normalColor;buy.colors=colors;
  35. // Existing SpriteSwap already maps hover/selected to Normal; do not replace its gold frame.
  36. }
  37. foreach(var tab in new[]{shop.ItemsButton,shop.RecordsButton}){
  38. var colors=tab.colors;colors.normalColor=colors.highlightedColor=colors.selectedColor=colors.disabledColor=Color.white;colors.fadeDuration=0;tab.colors=colors;
  39. }
  40. UIBuilder.Place((RectTransform)shop.ItemsButton.transform,18,104,179,46);
  41. UIBuilder.Place((RectTransform)shop.RecordsButton.transform,209,104,179,46);
  42. view.Layout();view.LayoutBalance();view.StyleTabs();
  43. }
  44. private void LateUpdate(){if(!shop)return;Layout();LayoutBalance();if(!styled || lastTab!=shop.CurrentTab)StyleTabs();}
  45. private void Layout()
  46. {
  47. float width=Mathf.Min(370,((RectTransform)items.parent).rect.width);if(width<250 || Mathf.Approximately(lastWidth,width))return;lastWidth=width;
  48. items.sizeDelta=new Vector2(width,items.sizeDelta.y);
  49. for(int i=0;i<3;i++){
  50. var card=(RectTransform)shop.BuyButtons[i].transform.parent;UIBuilder.Place(card,0,i*150,width,140);
  51. var icon=card.Find("ProductIcon") as RectTransform;if(icon)LayoutProductIcon(icon,i);
  52. UIBuilder.Place((RectTransform)card.Find("Name"),76,10,width-90,28);
  53. UIBuilder.Place((RectTransform)card.Find("Description"),76,40,width-90,36);
  54. UIBuilder.Place(shop.StockLabels[i].rectTransform,14,80,width-154,22);
  55. UIBuilder.Place(shop.PriceLabels[i].rectTransform,14,106,width-154,24);
  56. UIBuilder.Place((RectTransform)shop.BuyButtons[i].transform,width-126,82,112,46);
  57. }
  58. }
  59. private void LayoutBalance()
  60. {
  61. if(!balanceHeader){
  62. var header=shop.GetComponentInChildren<PopupHeader>(true);if(!header)return;
  63. balanceHeader=(RectTransform)header.transform;balance.SetParent(balanceHeader,false);
  64. }
  65. // Reserve the centered title and corner ornaments independently of amount length.
  66. float width=Mathf.Max(1,balanceHeader.rect.width*.5f-80);
  67. UIBuilder.Place(balance,32,10,width,32);
  68. var value=shop.CoinsLabel;
  69. if(lastBalanceText==value.text && Mathf.Approximately(lastBalanceWidth,width))return;
  70. lastBalanceText=value.text;lastBalanceWidth=width;
  71. float amountWidth=Mathf.Min(Mathf.Ceil(value.preferredWidth)+2,Mathf.Max(1,width-26));
  72. UIBuilder.Place(value.rectTransform,26,4,amountWidth,24);
  73. var icon=balance.Find("ShopCoin") as RectTransform;
  74. if(icon)UIBuilder.Place(icon,0,6,20,20);
  75. }
  76. // Alpha>=16 bounds measured from the three existing PNGs; positions use top-left image coordinates.
  77. private static readonly Rect[] ProductBounds={
  78. new Rect(6f/250,9f/214,239f/250,201f/214),
  79. new Rect(5f/219,5f/176,210f/219,167f/176),
  80. new Rect(6f/178,6f/239,168f/178,228f/239)};
  81. private static readonly float[] ProductExtent={40,42,45.5f};
  82. private static void LayoutProductIcon(RectTransform rect,int index)
  83. {
  84. var image=rect.GetComponent<Image>();
  85. if(!image || !image.sprite){UIBuilder.Place(rect,14,12,48,48);return;}
  86. var bounds=ProductBounds[index];var size=image.sprite.rect.size;
  87. float scale=ProductExtent[index]/Mathf.Max(bounds.width*size.x,bounds.height*size.y);
  88. float w=size.x*scale,h=size.y*scale;
  89. // Common 48x48 logical region (14,12); align the visible bounds center, not transparent padding.
  90. UIBuilder.Place(rect,38-bounds.center.x*w,36-bounds.center.y*h,w,h);
  91. image.preserveAspect=true;
  92. }
  93. private void StyleTabs()
  94. {
  95. lastTab=shop.CurrentTab;styled=true;
  96. foreach(var tab in new[]{shop.ItemsButton,shop.RecordsButton}){
  97. bool selected=tab==shop.ItemsButton?lastTab==ShopTab.Items:lastTab==ShopTab.CoinRecords;
  98. var g=tab.GetComponent<UIGradient>();if(g){g.Top=UIBuilder.Hex(selected?"50305C":"241C30");g.Bottom=UIBuilder.Hex(selected?"30203E":"191321");tab.GetComponent<Graphic>().SetVerticesDirty();}
  99. var label=tab.GetComponentInChildren<Text>(true);label.color=UIBuilder.Hex(selected?"F3D391":"AA9CB9");label.fontStyle=selected?FontStyle.Bold:FontStyle.Normal;
  100. }
  101. }
  102. }
  103. [RequireComponent(typeof(CanvasRenderer))]
  104. internal sealed class ShopCardTrim : MaskableGraphic
  105. {
  106. protected override void OnPopulateMesh(VertexHelper mesh)
  107. {
  108. mesh.Clear();var r=GetPixelAdjustedRect();var gold=UIBuilder.Hex("79603F");const int steps=8;const float radius=12,thickness=.8f;
  109. for(int corner=0;corner<4;corner++){
  110. var c=new Vector2(corner==0||corner==3?r.xMax-radius-1:r.xMin+radius+1,corner<2?r.yMax-radius-1:r.yMin+radius+1);
  111. for(int i=0;i<steps;i++){
  112. float a=(corner*90+i*90f/steps)*Mathf.Deg2Rad,b=(corner*90+(i+1)*90f/steps)*Mathf.Deg2Rad;
  113. Quad(mesh,c+new Vector2(Mathf.Cos(a),Mathf.Sin(a))*radius,c+new Vector2(Mathf.Cos(b),Mathf.Sin(b))*radius,c+new Vector2(Mathf.Cos(b),Mathf.Sin(b))*(radius-thickness),c+new Vector2(Mathf.Cos(a),Mathf.Sin(a))*(radius-thickness),gold);
  114. }
  115. }
  116. Line(mesh,new Vector2(r.xMin+13,r.yMax-1),new Vector2(r.xMax-13,r.yMax-1),gold);
  117. Line(mesh,new Vector2(r.xMin+13,r.yMin+1),new Vector2(r.xMax-13,r.yMin+1),gold);
  118. Line(mesh,new Vector2(r.xMin+1,r.yMin+13),new Vector2(r.xMin+1,r.yMax-13),gold);
  119. Line(mesh,new Vector2(r.xMax-1,r.yMin+13),new Vector2(r.xMax-1,r.yMax-13),gold);
  120. foreach(float x in new[]{r.xMin+8,r.xMax-8})foreach(float y in new[]{r.yMin+8,r.yMax-8}){
  121. var c=new Vector2(x,y);Quad(mesh,c+Vector2.up*2,c+Vector2.right*1.2f,c-Vector2.up*2,c-Vector2.right*1.2f,UIBuilder.Hex("A58450"));
  122. }
  123. }
  124. private static void Line(VertexHelper m,Vector2 a,Vector2 b,Color c){var n=new Vector2(-(b-a).y,(b-a).x).normalized*.35f;Quad(m,a+n,b+n,b-n,a-n,c);}
  125. private static void Quad(VertexHelper m,Vector2 a,Vector2 b,Vector2 c,Vector2 d,Color color){int n=m.currentVertCount;m.AddVert(a,color,Vector2.zero);m.AddVert(b,color,Vector2.zero);m.AddVert(c,color,Vector2.zero);m.AddVert(d,color,Vector2.zero);m.AddTriangle(n,n+1,n+2);m.AddTriangle(n,n+2,n+3);}
  126. }
  127. }