LobbyArtView.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using TMPro;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace DevilLink2.UI
  5. {
  6. /// <summary>Lobby-only decoration of the existing controls. Owns no navigation or player data.</summary>
  7. public sealed class LobbyArtView : MonoBehaviour
  8. {
  9. public Image Background { get; private set; }
  10. public TextMeshProUGUI Coins { get; private set; }
  11. public TextMeshProUGUI Tickets { get; private set; }
  12. public const float CardWidth = 130, CardHeight = 286, CardTop = 476;
  13. public static LobbyArtView Install(LobbyScreen screen, LobbyArtCatalog art)
  14. {
  15. var existing = screen.GetComponent<LobbyArtView>();
  16. if (existing) return existing;
  17. var view = screen.gameObject.AddComponent<LobbyArtView>();
  18. if (art) view.Build(screen, art);
  19. return view;
  20. }
  21. private void Build(LobbyScreen screen, LobbyArtCatalog art)
  22. {
  23. Transform root = screen.transform;
  24. if (art.Background)
  25. {
  26. var clip = UIBuilder.Rect("LobbyArtworkViewport", root, 0, 0, 430, 932);
  27. clip.gameObject.AddComponent<RectMask2D>(); clip.SetSiblingIndex(1);
  28. Background = Picture("Castle", clip, art.Background, 0, 0, 430, 932);
  29. // Cover rather than stretch; clipping is confined to this background-only viewport.
  30. Background.rectTransform.pivot = new Vector2(.5f, .5f);
  31. var fitter = Background.gameObject.AddComponent<AspectRatioFitter>();
  32. // In EditMode, None recomputes aspectRatio from the current rect. Set the mode first.
  33. // EnvelopeParent owns anchors and size; do not overwrite them after configuration.
  34. fitter.aspectMode = AspectRatioFitter.AspectMode.EnvelopeParent;
  35. fitter.aspectRatio = art.Background.rect.width / art.Background.rect.height;
  36. }
  37. var logo = Picture("LobbyLogo", root, art.Logo, 65, 145, 300, 142);
  38. if (!logo) UIBuilder.Label("LobbyTitle", root, "DEVIL LINK 2", 30, UIBuilder.LightGold, 45, 158, 340, 60);
  39. else LobbyLogoAnimation.Install(screen, logo);
  40. if (art.Panel)
  41. {
  42. var panel = Picture("ModeHeadingPanel", root, art.Panel, 43, 311, 344, 161);
  43. UIBuilder.Label("Heading", panel.transform, "CHOOSE YOUR MODE", 19, UIBuilder.LightGold, 30, 65, 284, 30);
  44. UIBuilder.Label("Caption", panel.transform, "3 MODES / 20 LEVELS EACH", 11, UIBuilder.TextColor, 30, 100, 284, 24);
  45. }
  46. UIBuilder.Place((RectTransform)root.Find("TopHUD"), 0, 0, 430, 56);
  47. var top = root.Find("TopHUD");
  48. var topGradient = top.GetComponent<UIGradient>();
  49. if (topGradient) { topGradient.Top=UIBuilder.Hex("30203F"); topGradient.Bottom=UIBuilder.Hex("140E24"); }
  50. var topImage=top.GetComponent<Image>(); if(topImage)topImage.color=UIBuilder.Hex("241630");
  51. var seam=UIBuilder.Rect("PlayerBarDivider",top,12,54,406,1).gameObject.AddComponent<Image>();
  52. seam.color=new Color(.62f,.46f,.24f,.5f);seam.raycastTarget=false;
  53. UIBuilder.Place(screen.NameLabel.rectTransform, 62, 8, 240, 24);
  54. // Both resource backgrounds are exactly 3:1; their '+' remains in the button hit area.
  55. if (art.Coins)
  56. {
  57. var rect = (RectTransform)root.Find("CoinDisplay");
  58. UIBuilder.Place(rect, 12, 62, 198, 66);
  59. HideGraphic(rect); Picture("Art", rect, art.Coins, 0, 0, 198, 66);
  60. root.Find("CoinIcon").gameObject.SetActive(false);
  61. // The Button scales this resource root; all visible values must share that ancestor.
  62. Coins = Value("CoinsTMP", rect, 54, 21, 100, 24);
  63. screen.CoinsLabel.transform.SetParent(rect,false);
  64. UIBuilder.Place(screen.CoinsLabel.rectTransform, 54, 21, 100, 24);
  65. screen.CoinsLabel.enabled = !Coins;
  66. }
  67. if (art.Tickets)
  68. {
  69. var rect = (RectTransform)root.Find("TicketDisplay");
  70. UIBuilder.Place(rect, 220, 62, 198, 66);
  71. HideGraphic(rect); Picture("Art", rect, art.Tickets, 0, 0, 198, 66);
  72. var button = rect.gameObject.AddComponent<UIFeedbackButton>();
  73. button.targetGraphic = rect.GetComponent<Graphic>(); button.targetGraphic.raycastTarget = true;
  74. screen.ConfigureTicket(button);
  75. root.Find("TicketIcon").gameObject.SetActive(false);
  76. Tickets = Value("TicketsTMP", rect, 54, 21, 100, 24);
  77. screen.TicketsLabel.transform.SetParent(rect,false);
  78. UIBuilder.Place(screen.TicketsLabel.rectTransform, 54, 21, 100, 24);
  79. screen.TicketsLabel.enabled = !Tickets;
  80. }
  81. for (int i = 0; i < screen.Cards.Length; i++)
  82. {
  83. var card = screen.Cards[i]; var rect = (RectTransform)card.transform;
  84. UIBuilder.Place(rect, 12 + i * 138, CardTop, CardWidth, CardHeight);
  85. Sprite sprite = art.Mode(i);
  86. if (sprite)
  87. {
  88. foreach (Transform child in rect) if (child != card.ProgressLabel.transform) child.gameObject.SetActive(false);
  89. HideGraphic(rect);
  90. var artwork = Picture("ModeArtwork", rect, sprite, 0, 0, 130, 238);
  91. if (card.Button is UIFeedbackButton feedback) feedback.EnablePressScale = false;
  92. // Static rim/title plus real independent animated subject sprites. No click overlay.
  93. LobbyModeMagic.Install(screen, artwork, i);
  94. // The former body is hidden; retain the existing button and bind it to the root hit area.
  95. card.Button.targetGraphic = rect.GetComponent<Graphic>();
  96. card.Button.targetGraphic.raycastTarget = true;
  97. }
  98. else
  99. {
  100. // Missing card art: replace overflowing placeholder decoration with compact readable labels.
  101. foreach (Transform child in rect) if (child != card.ProgressLabel.transform) child.gameObject.SetActive(false);
  102. UIBuilder.Label("FallbackModeName", rect, UIManager.ModeName(card.Mode), 18, UIBuilder.LightGold, 6, 46, 118, 100);
  103. card.Button.targetGraphic = rect.GetComponent<Graphic>(); card.Button.targetGraphic.raycastTarget = true;
  104. }
  105. var backing = UIBuilder.Fill("ProgressBacking", rect, new Color(.025f,.018f,.045f,.68f),
  106. new Color(.025f,.018f,.045f,.55f), 1, 243, 128, 42, 5);
  107. backing.transform.SetAsLastSibling();
  108. UIBuilder.Place(card.ProgressLabel.rectTransform, 2, 243, 126, 42);
  109. card.ProgressLabel.fontSize = 13;
  110. card.ProgressLabel.alignment = TextAnchor.MiddleCenter;
  111. card.ProgressLabel.horizontalOverflow = HorizontalWrapMode.Overflow;
  112. card.ProgressLabel.lineSpacing = 1.05f;
  113. card.ProgressLabel.color = UIBuilder.TextColor;
  114. var textShadow = card.ProgressLabel.gameObject.AddComponent<Shadow>();
  115. textShadow.effectColor = new Color(0,0,0,.65f); textShadow.effectDistance = new Vector2(0,-1);
  116. card.ProgressLabel.transform.SetAsLastSibling();
  117. }
  118. var buttons = new[] { screen.DailyButton, screen.WheelButton, screen.ShopButton, screen.ProfileButton, screen.SettingsButton, screen.HelpButton };
  119. var dockSprite = Resources.Load<Sprite>("LobbyNavigation/DockFade");
  120. if (dockSprite) {
  121. var dock = Picture("NavigationDockFade", root, dockSprite, 0, 796, 430, 136);
  122. dock.preserveAspect = false;
  123. dock.rectTransform.anchorMin = Vector2.zero; dock.rectTransform.anchorMax = Vector2.right;
  124. dock.rectTransform.pivot = new Vector2(.5f, 0); dock.rectTransform.anchoredPosition = Vector2.zero;
  125. dock.rectTransform.sizeDelta = new Vector2(0, 136);
  126. dock.transform.SetSiblingIndex(buttons[0].transform.GetSiblingIndex());
  127. }
  128. var navigationNames = new[] { "Daily", "Wheel", "Shop", "Profile", "Settings", "Help" };
  129. var plate = Resources.Load<Sprite>("LobbyNavigation/ButtonFace");
  130. for (int i = 0; i < buttons.Length; i++)
  131. {
  132. var button = buttons[i]; var rect = (RectTransform)button.transform;
  133. UIBuilder.Place(rect, 14 + i * 68, 836, 62, 80);
  134. // Keep the existing root raycast and Button, but render one static native plate.
  135. var border = rect.GetComponent<RoundedPanel>(); border.Radius = 12;
  136. HideGraphic(rect); border.raycastTarget = true;
  137. Image face;
  138. if (plate) {
  139. face = Picture("NavigationFace", rect, plate, 1, 1, 60, 78);
  140. face.type = Image.Type.Sliced; face.preserveAspect = false; face.pixelsPerUnitMultiplier = 2;
  141. } else {
  142. face = UIBuilder.Rect("NavigationFace", rect, 1, 1, 60, 78).gameObject.AddComponent<Image>();
  143. face.sprite = Resources.Load<Sprite>("ProfileProgressCapsule"); face.type = Image.Type.Sliced;
  144. face.pixelsPerUnitMultiplier = 4; face.color = UIBuilder.Hex("24172F"); face.raycastTarget = false;
  145. }
  146. face.transform.SetAsFirstSibling(); button.targetGraphic = face;
  147. button.transition = Selectable.Transition.ColorTint;
  148. var colors = button.colors;
  149. colors.normalColor = colors.highlightedColor = colors.selectedColor = Color.white;
  150. colors.pressedColor = new Color(.78f, .72f, .85f, 1);
  151. colors.disabledColor = new Color(.42f, .40f, .48f, 1); colors.fadeDuration = 0;
  152. colors.colorMultiplier = 1; button.colors = colors;
  153. // Derived from the existing painted glyphs; source PNGs/catalog references stay intact.
  154. var sprite = Resources.Load<Sprite>("LobbyNavigation/" + navigationNames[i]);
  155. if (!sprite) sprite = i == 5 ? art.HelpIcon : art.Navigation(i);
  156. if (sprite) Picture("NavigationArtwork", rect, sprite, 9, 4, 44, 44);
  157. else UIBuilder.Label("NavigationFallback", rect, i == 5 ? "?" : navigationNames[i].Substring(0,1), 24,
  158. UIBuilder.LightGold, 9, 4, 44, 44);
  159. // Captions have their own clear row, never cover any part of the icon.
  160. var label = rect.Find("Label").GetComponent<Text>();
  161. UIBuilder.Place(label.rectTransform, 2, 54, 58, 20);
  162. label.color = UIBuilder.Hex("EAD7AB"); label.fontSize = 10; label.alignment = TextAnchor.MiddleCenter;
  163. label.horizontalOverflow = HorizontalWrapMode.Overflow;
  164. label.transform.SetAsLastSibling();
  165. }
  166. screen.ConfigureArtValues(Coins, Tickets);
  167. }
  168. public static Image Picture(string name, Transform parent, Sprite sprite, float x, float y, float width, float height)
  169. {
  170. if (!sprite) return null;
  171. var image = UIBuilder.Rect(name, parent, x, y, width, height).gameObject.AddComponent<Image>();
  172. image.sprite = sprite; image.preserveAspect = true; image.raycastTarget = false; image.color = Color.white;
  173. return image;
  174. }
  175. private static void HideGraphic(RectTransform rect)
  176. {
  177. // Preserve the original target and its raycasts for prefab/test compatibility.
  178. var graphic = rect.GetComponent<Graphic>(); if (graphic) graphic.color = Color.clear;
  179. var gradient = rect.GetComponent<UIGradient>(); if (gradient) gradient.enabled = false;
  180. var shadow = rect.GetComponent<Shadow>(); if (shadow) shadow.enabled = false;
  181. }
  182. private static TextMeshProUGUI Value(string name, Transform parent, float x, float y, float w, float h)
  183. {
  184. var font = Resources.Load<TMP_FontAsset>("Fonts & Materials/LiberationSans SDF");
  185. if (!font) return null;
  186. var rect = UIBuilder.Rect(name, parent, x, y, w, h);
  187. var text = rect.gameObject.AddComponent<TextMeshProUGUI>();
  188. text.font = font;
  189. text.fontSize = 18; text.fontStyle = FontStyles.Bold; text.color = UIBuilder.LightGold;
  190. text.alignment = TextAlignmentOptions.MidlineRight; text.enableAutoSizing = true;
  191. text.fontSizeMin = 8; text.fontSizeMax = 18; text.raycastTarget = false;
  192. text.textWrappingMode = TextWrappingModes.NoWrap; text.overflowMode = TextOverflowModes.Truncate;
  193. return text;
  194. }
  195. }
  196. }