| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- using UnityEngine;
- using UnityEngine.UI;
- namespace DevilLink2.UI
- {
- // Shop-only composition. Keeps all original bound texts, Buttons and purchase listeners.
- public sealed class ShopProductVisuals : MonoBehaviour
- {
- private ShopScreen shop;private RectTransform items,balance;private ShopTab lastTab;private bool styled;
- private float lastWidth=-1,lastBalanceWidth=-1;private string lastBalanceText;private RectTransform balanceHeader;
- public static void Install(ShopScreen shop,LobbyArtCatalog shared)
- {
- if(shop.GetComponent<ShopProductVisuals>())return;
- var view=shop.gameObject.AddComponent<ShopProductVisuals>();view.shop=shop;
- view.items=(RectTransform)shop.BuyButtons[0].transform.parent.parent;
- var text=shop.CoinsLabel;var r=text.rectTransform;var pos=r.anchoredPosition;var size=r.sizeDelta;
- // Keep the original economic text binding; Shop alone moves this group into its header.
- view.balance=UIBuilder.Rect("Balance",r.parent,pos.x,-pos.y,size.x,size.y);
- text.name="BalanceValue";r.SetParent(view.balance,false);
- var icon=LobbyArtView.Picture("ShopCoin",view.balance,shared?shared.CoinIcon:null,0,6,20,20);
- if(icon){var ir=icon.rectTransform;ir.anchorMin=ir.anchorMax=ir.pivot=new Vector2(1,1);ir.anchoredPosition=new Vector2(-144,-3);}
- r.anchorMin=r.anchorMax=r.pivot=new Vector2(1,1);r.anchoredPosition=Vector2.zero;r.sizeDelta=new Vector2(136,32);
- text.fontSize=14;text.fontStyle=FontStyle.Bold;text.color=UIBuilder.LightGold;text.alignment=TextAnchor.MiddleLeft;
- text.resizeTextForBestFit=true;text.resizeTextMinSize=8;text.resizeTextMaxSize=14;
- text.raycastTarget=false;if(icon)icon.raycastTarget=false;
- view.balance.gameObject.AddComponent<RectMask2D>();
- for(int i=0;i<3;i++){
- var card=shop.BuyButtons[i].transform.parent;
- var gradient=card.GetComponent<UIGradient>();if(gradient){gradient.Top=UIBuilder.Hex("352440");gradient.Bottom=UIBuilder.Hex("1D142C");card.GetComponent<Graphic>().SetVerticesDirty();}
- var trim=UIBuilder.Rect("ShopCardTrim",card,0,0,370,140);trim.anchorMin=Vector2.zero;trim.anchorMax=Vector2.one;trim.offsetMin=trim.offsetMax=Vector2.zero;
- trim.gameObject.AddComponent<CanvasRenderer>();trim.gameObject.AddComponent<ShopCardTrim>().raycastTarget=false;
- var name=card.Find("Name").GetComponent<Text>();name.fontSize=14;name.fontStyle=FontStyle.Bold;name.color=UIBuilder.LightGold;
- var desc=card.Find("Description").GetComponent<Text>();desc.fontSize=11;desc.color=UIBuilder.Hex("C6B8D4");desc.horizontalOverflow=HorizontalWrapMode.Wrap;
- shop.StockLabels[i].color=UIBuilder.Hex("AB9BB9");shop.PriceLabels[i].color=UIBuilder.Hex("F1CB85");shop.PriceLabels[i].fontStyle=FontStyle.Bold;
- var buy=shop.BuyButtons[i];var colors=buy.colors;colors.highlightedColor=colors.selectedColor=colors.normalColor;buy.colors=colors;
- // Existing SpriteSwap already maps hover/selected to Normal; do not replace its gold frame.
- }
- foreach(var tab in new[]{shop.ItemsButton,shop.RecordsButton}){
- var colors=tab.colors;colors.normalColor=colors.highlightedColor=colors.selectedColor=colors.disabledColor=Color.white;colors.fadeDuration=0;tab.colors=colors;
- }
- UIBuilder.Place((RectTransform)shop.ItemsButton.transform,18,104,179,46);
- UIBuilder.Place((RectTransform)shop.RecordsButton.transform,209,104,179,46);
- view.Layout();view.LayoutBalance();view.StyleTabs();
- }
- private void LateUpdate(){if(!shop)return;Layout();LayoutBalance();if(!styled || lastTab!=shop.CurrentTab)StyleTabs();}
- private void Layout()
- {
- float width=Mathf.Min(370,((RectTransform)items.parent).rect.width);if(width<250 || Mathf.Approximately(lastWidth,width))return;lastWidth=width;
- items.sizeDelta=new Vector2(width,items.sizeDelta.y);
- for(int i=0;i<3;i++){
- var card=(RectTransform)shop.BuyButtons[i].transform.parent;UIBuilder.Place(card,0,i*150,width,140);
- var icon=card.Find("ProductIcon") as RectTransform;if(icon)LayoutProductIcon(icon,i);
- UIBuilder.Place((RectTransform)card.Find("Name"),76,10,width-90,28);
- UIBuilder.Place((RectTransform)card.Find("Description"),76,40,width-90,36);
- UIBuilder.Place(shop.StockLabels[i].rectTransform,14,80,width-154,22);
- UIBuilder.Place(shop.PriceLabels[i].rectTransform,14,106,width-154,24);
- UIBuilder.Place((RectTransform)shop.BuyButtons[i].transform,width-126,82,112,46);
- }
- }
- private void LayoutBalance()
- {
- if(!balanceHeader){
- var header=shop.GetComponentInChildren<PopupHeader>(true);if(!header)return;
- balanceHeader=(RectTransform)header.transform;balance.SetParent(balanceHeader,false);
- }
- // Reserve the centered title and corner ornaments independently of amount length.
- float width=Mathf.Max(1,balanceHeader.rect.width*.5f-80);
- UIBuilder.Place(balance,32,10,width,32);
- var value=shop.CoinsLabel;
- if(lastBalanceText==value.text && Mathf.Approximately(lastBalanceWidth,width))return;
- lastBalanceText=value.text;lastBalanceWidth=width;
- float amountWidth=Mathf.Min(Mathf.Ceil(value.preferredWidth)+2,Mathf.Max(1,width-26));
- UIBuilder.Place(value.rectTransform,26,4,amountWidth,24);
- var icon=balance.Find("ShopCoin") as RectTransform;
- if(icon)UIBuilder.Place(icon,0,6,20,20);
- }
- // Alpha>=16 bounds measured from the three existing PNGs; positions use top-left image coordinates.
- private static readonly Rect[] ProductBounds={
- new Rect(6f/250,9f/214,239f/250,201f/214),
- new Rect(5f/219,5f/176,210f/219,167f/176),
- new Rect(6f/178,6f/239,168f/178,228f/239)};
- private static readonly float[] ProductExtent={40,42,45.5f};
- private static void LayoutProductIcon(RectTransform rect,int index)
- {
- var image=rect.GetComponent<Image>();
- if(!image || !image.sprite){UIBuilder.Place(rect,14,12,48,48);return;}
- var bounds=ProductBounds[index];var size=image.sprite.rect.size;
- float scale=ProductExtent[index]/Mathf.Max(bounds.width*size.x,bounds.height*size.y);
- float w=size.x*scale,h=size.y*scale;
- // Common 48x48 logical region (14,12); align the visible bounds center, not transparent padding.
- UIBuilder.Place(rect,38-bounds.center.x*w,36-bounds.center.y*h,w,h);
- image.preserveAspect=true;
- }
- private void StyleTabs()
- {
- lastTab=shop.CurrentTab;styled=true;
- foreach(var tab in new[]{shop.ItemsButton,shop.RecordsButton}){
- bool selected=tab==shop.ItemsButton?lastTab==ShopTab.Items:lastTab==ShopTab.CoinRecords;
- 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();}
- var label=tab.GetComponentInChildren<Text>(true);label.color=UIBuilder.Hex(selected?"F3D391":"AA9CB9");label.fontStyle=selected?FontStyle.Bold:FontStyle.Normal;
- }
- }
- }
- [RequireComponent(typeof(CanvasRenderer))]
- internal sealed class ShopCardTrim : MaskableGraphic
- {
- protected override void OnPopulateMesh(VertexHelper mesh)
- {
- mesh.Clear();var r=GetPixelAdjustedRect();var gold=UIBuilder.Hex("79603F");const int steps=8;const float radius=12,thickness=.8f;
- for(int corner=0;corner<4;corner++){
- 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);
- for(int i=0;i<steps;i++){
- float a=(corner*90+i*90f/steps)*Mathf.Deg2Rad,b=(corner*90+(i+1)*90f/steps)*Mathf.Deg2Rad;
- 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);
- }
- }
- Line(mesh,new Vector2(r.xMin+13,r.yMax-1),new Vector2(r.xMax-13,r.yMax-1),gold);
- Line(mesh,new Vector2(r.xMin+13,r.yMin+1),new Vector2(r.xMax-13,r.yMin+1),gold);
- Line(mesh,new Vector2(r.xMin+1,r.yMin+13),new Vector2(r.xMin+1,r.yMax-13),gold);
- Line(mesh,new Vector2(r.xMax-1,r.yMin+13),new Vector2(r.xMax-1,r.yMax-13),gold);
- foreach(float x in new[]{r.xMin+8,r.xMax-8})foreach(float y in new[]{r.yMin+8,r.yMax-8}){
- 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"));
- }
- }
- 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);}
- 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);}
- }
- }
|