DailyButtonVisuals.cs 5.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using TMPro;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.Sprites;
  5. namespace DevilLink2.UI
  6. {
  7. // Visual-only composition of the existing two Buttons; never attaches click listeners.
  8. internal static class DailyButtonVisuals
  9. {
  10. internal static void Install(DailyCheckInScreen daily, GameplayArtCatalog art)
  11. {
  12. if(daily.CloseButton.transform.Find("DailyCloseFace"))return;
  13. var font=Resources.Load<TMP_FontAsset>("Fonts & Materials/LiberationSans SDF");
  14. // Missing font leaves the original legible labels intact.
  15. StyleClose(daily.CloseButton);
  16. if(art && art.Normal)
  17. {
  18. var rect=(RectTransform)daily.ClaimButton.transform;
  19. var graphic=UIBuilder.Rect("DailyClaimArtwork",rect,0,0,rect.rect.width,rect.rect.height).gameObject.AddComponent<DailyClaimImage>();
  20. graphic.sprite=art.Normal;graphic.raycastTarget=false;graphic.color=Color.white;
  21. graphic.transform.SetAsFirstSibling();
  22. var gradient=rect.GetComponent<UIGradient>();if(gradient)gradient.enabled=false;
  23. var shadow=rect.GetComponent<Shadow>();if(shadow)shadow.enabled=false;
  24. rect.GetComponent<Image>().color=Color.clear; // Original raycast surface remains enabled.
  25. daily.ClaimButton.targetGraphic=graphic;daily.ClaimButton.transition=Selectable.Transition.SpriteSwap;
  26. daily.ClaimButton.spriteState=new SpriteState{highlightedSprite=art.Normal,selectedSprite=art.Normal,pressedSprite=art.Pressed?art.Pressed:art.Normal,disabledSprite=art.Disabled?art.Disabled:art.Normal};
  27. }
  28. Label(daily.CloseButton,"X",18,font);
  29. Label(daily.ClaimButton,"CLAIM",18,font);
  30. }
  31. private static void StyleClose(Button button)
  32. {
  33. var rect=(RectTransform)button.transform;
  34. // Keep the 46x44 raycast surface; paint only a centered 32px circular control.
  35. var edge=rect.GetComponent<RoundedPanel>();edge.color=Color.clear;
  36. var gradient=rect.GetComponent<UIGradient>();if(gradient)gradient.enabled=false;
  37. var shadow=rect.GetComponent<Shadow>();if(shadow)shadow.enabled=false;
  38. var rim=UIBuilder.Fill("DailyCloseRim",rect,UIBuilder.Hex("8E7449"),UIBuilder.Hex("5B442E"),7,6,32,32,16);
  39. rim.transform.SetAsFirstSibling();rim.raycastTarget=false;
  40. var face=UIBuilder.Fill("DailyCloseFace",rect,UIBuilder.Hex("392443"),UIBuilder.Hex("21142F"),8,7,30,30,15);
  41. face.transform.SetSiblingIndex(rim.transform.GetSiblingIndex()+1);face.raycastTarget=false;
  42. button.targetGraphic=face;button.transition=Selectable.Transition.ColorTint;
  43. var colors=button.colors;colors.normalColor=Color.white;colors.highlightedColor=new Color(1,.93f,.8f);
  44. colors.selectedColor=colors.normalColor;colors.pressedColor=new Color(.63f,.52f,.72f);
  45. colors.disabledColor=new Color(.42f,.4f,.46f,.7f);colors.fadeDuration=.08f;button.colors=colors;
  46. }
  47. private static void Label(Button button,string value,int size,TMP_FontAsset font)
  48. {
  49. var legacy=button.GetComponentInChildren<Text>(true);
  50. if(!font){if(legacy){legacy.text=value;legacy.color=UIBuilder.LightGold;}return;}
  51. var rect=(RectTransform)button.transform;
  52. var label=UIBuilder.Rect("DailyButtonLabel",rect,5,2,rect.rect.width-10,rect.rect.height-4).gameObject.AddComponent<TextMeshProUGUI>();
  53. label.font=font;label.text=value;label.fontSize=size;label.fontStyle=FontStyles.Bold;
  54. label.alignment=TextAlignmentOptions.Center;label.color=UIBuilder.LightGold;
  55. label.textWrappingMode=TextWrappingModes.NoWrap;label.raycastTarget=false;
  56. if(legacy)legacy.enabled=false;
  57. }
  58. }
  59. // Source-preserving horizontal stretch: fixed ends and central gem, two elastic spans.
  60. internal sealed class DailyClaimImage : Image
  61. {
  62. protected override void OnPopulateMesh(VertexHelper mesh)
  63. {
  64. var active=overrideSprite?overrideSprite:sprite;
  65. if(!active){base.OnPopulateMesh(mesh);return;}
  66. mesh.Clear();var rect=GetPixelAdjustedRect();var uv=DataUtility.GetOuterUV(active);
  67. float w=active.rect.width,h=active.rect.height,scale=rect.height/h;
  68. float edge=w*.2f,center=w*.16f;
  69. scale=Mathf.Min(scale,rect.width/(edge*2+center));
  70. float cap=edge*scale,half=center*scale*.5f;
  71. var xs=new[]{rect.xMin,rect.xMin+cap,rect.center.x-half,rect.center.x+half,rect.xMax-cap,rect.xMax};
  72. var sx=new[]{0f,.2f,.42f,.58f,.8f,1f};
  73. for(int i=0;i<5;i++)
  74. {
  75. int start=mesh.currentVertCount;
  76. float left=Mathf.Lerp(uv.x,uv.z,sx[i]),right=Mathf.Lerp(uv.x,uv.z,sx[i+1]);
  77. mesh.AddVert(new Vector3(xs[i],rect.yMin),color,new Vector2(left,uv.y));
  78. mesh.AddVert(new Vector3(xs[i],rect.yMax),color,new Vector2(left,uv.w));
  79. mesh.AddVert(new Vector3(xs[i+1],rect.yMax),color,new Vector2(right,uv.w));
  80. mesh.AddVert(new Vector3(xs[i+1],rect.yMin),color,new Vector2(right,uv.y));
  81. mesh.AddTriangle(start,start+1,start+2);mesh.AddTriangle(start,start+2,start+3);
  82. }
  83. }
  84. }
  85. }