LobbyScreenView.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System;
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. [DisallowMultipleComponent]
  6. public class LobbyScreenView : MonoBehaviour
  7. {
  8. [Header("Layout")]
  9. [SerializeField] float selectedCardScale = 1.02f;
  10. public event Action<ModeDefinition> ModeSelected;
  11. public event Action PlayRequested;
  12. public event Action CheckInRequested;
  13. public event Action ShopRequested;
  14. public event Action DrawRequested;
  15. public event Action HelpRequested;
  16. public event Action ProfileRequested;
  17. public event Action SettingsRequested;
  18. public event Action AddCoinsRequested;
  19. TMP_Text playerName;
  20. TMP_Text playerLevel;
  21. TMP_Text playerDescription;
  22. Image playerAvatar;
  23. TMP_Text coinBalance;
  24. TMP_Text topAward;
  25. TMP_Text selectedModeInfo;
  26. TMP_Text movesLabel;
  27. TMP_Text fruitsLabel;
  28. Transform checkInDot;
  29. Transform drawDot;
  30. readonly ModeCard[] modeCards = new ModeCard[3];
  31. bool wired;
  32. sealed class ModeCard
  33. {
  34. public ModeDefinition definition;
  35. public Transform root;
  36. public TMP_Text levelsTag;
  37. public TMP_Text level;
  38. public TMP_Text description;
  39. public TMP_Text progress;
  40. public Transform progressFill;
  41. public Graphic background;
  42. public Outline outline;
  43. }
  44. void Awake()
  45. {
  46. CacheReferences();
  47. UIViewUtility.EnableAutoSize(playerName, 38f, 24f);
  48. WireButtons();
  49. }
  50. void OnEnable()
  51. {
  52. SaveService.Changed += HandleSaveChanged;
  53. LocaleService.Changed += HandleSaveChanged;
  54. Refresh();
  55. }
  56. void OnDisable()
  57. {
  58. SaveService.Changed -= HandleSaveChanged;
  59. LocaleService.Changed -= HandleSaveChanged;
  60. }
  61. public void Show()
  62. {
  63. gameObject.SetActive(true);
  64. Refresh();
  65. }
  66. public void Hide()
  67. {
  68. gameObject.SetActive(false);
  69. }
  70. void HandleSaveChanged()
  71. {
  72. if (isActiveAndEnabled)
  73. Refresh();
  74. }
  75. void CacheReferences()
  76. {
  77. playerName = UIViewUtility.Text(transform, "IMG_PlayerBarBG/TMP_PlayerName");
  78. playerLevel = UIViewUtility.Text(transform, "IMG_PlayerBarBG/BTN_PlayerAvatar/IMG_PlayerLevelBG/TMP_PlayerLevel");
  79. playerDescription = UIViewUtility.Text(transform, "IMG_PlayerBarBG/TMP_PlayerDescription");
  80. playerAvatar = UIViewUtility.Image(transform, "IMG_PlayerBarBG/BTN_PlayerAvatar/IMG_PlayerAvatarIcon");
  81. coinBalance = UIViewUtility.Text(transform, "IMG_PlayerBarBG/IMG_CoinBalanceBG/TMP_CoinBalance");
  82. topAward = UIViewUtility.Text(transform, "IMG_TopAwardRibbon/TMP_TopAwardLabel");
  83. selectedModeInfo = UIViewUtility.Text(transform, "IMG_MainBoardFrame/IMG_NowServingBoard/TMP_SelectedModeInfo");
  84. movesLabel = UIViewUtility.Text(transform, "IMG_MainBoardFrame/IMG_NowServingBoard/IMG_MovesBadge/TMP_MovesCount");
  85. fruitsLabel = UIViewUtility.Text(transform, "IMG_MainBoardFrame/IMG_NowServingBoard/IMG_MovesBadge (1)/TMP_FruitsCount");
  86. checkInDot = UIViewUtility.Find(transform, "IMG_BottomNavBG/IMG_CheckInIcon/IMG_CheckInNotification");
  87. drawDot = UIViewUtility.Find(transform, "IMG_BottomNavBG/IMG_DrawIcon/IMG_CheckInNotification");
  88. string[] paths =
  89. {
  90. "IMG_MainBoardFrame/SCROLL_MainScroll/Viewport/Content/BTN_ClassicStand",
  91. "IMG_MainBoardFrame/SCROLL_MainScroll/Viewport/Content/BTN_RushStand",
  92. "IMG_MainBoardFrame/SCROLL_MainScroll/Viewport/Content/BTN_SealedStand"
  93. };
  94. for (int i = 0; i < GameDefinitions.Modes.Length && i < modeCards.Length; i++)
  95. {
  96. Transform root = UIViewUtility.Find(transform, paths[i]);
  97. if (root == null)
  98. continue;
  99. modeCards[i] = new ModeCard
  100. {
  101. definition = GameDefinitions.Modes[i],
  102. root = root,
  103. levelsTag = UIViewUtility.Text(root, "Image (1)/TMP_TagLevels"),
  104. level = UIViewUtility.Text(root, "TMP_ModeLevel"),
  105. description = UIViewUtility.Text(root, "TMP_ModeDescription"),
  106. progress = UIViewUtility.Text(root, "TMP_ModeProgress"),
  107. progressFill = UIViewUtility.Find(root, "IMG_ClassicProgressBG/IMG_ClassicProgressFill"),
  108. background = root.GetComponent<Graphic>()
  109. };
  110. modeCards[i].outline = root.GetComponent<Outline>();
  111. if (modeCards[i].outline == null)
  112. modeCards[i].outline = root.gameObject.AddComponent<Outline>();
  113. modeCards[i].outline.effectColor = new Color32(255, 215, 92, 235);
  114. modeCards[i].outline.effectDistance = new Vector2(5f, -5f);
  115. modeCards[i].outline.useGraphicAlpha = true;
  116. modeCards[i].outline.enabled = false;
  117. }
  118. }
  119. void WireButtons()
  120. {
  121. if (wired)
  122. return;
  123. for (int i = 0; i < modeCards.Length; i++)
  124. {
  125. ModeCard card = modeCards[i];
  126. if (card == null || card.root == null)
  127. continue;
  128. ModeDefinition captured = card.definition;
  129. Button button = UIViewUtility.EnsureButton(card.root);
  130. UIViewUtility.ConfigureButton(button, card.root.GetComponent<Graphic>());
  131. UIViewUtility.Bind(button, () => SelectMode(captured));
  132. }
  133. UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_MainBoardFrame/IMG_NowServingBoard/BTN_Play"), () => PlayRequested?.Invoke());
  134. UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_BottomNavBG/IMG_CheckInIcon"), () => CheckInRequested?.Invoke());
  135. UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_BottomNavBG/IMG_ShopIcon"), () => ShopRequested?.Invoke());
  136. UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_BottomNavBG/IMG_DrawIcon"), () => DrawRequested?.Invoke());
  137. UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_BottomNavBG/IMG_HelpIcon"), () => HelpRequested?.Invoke());
  138. UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_BottomNavBG/IMG_ProfileIcon"), () => ProfileRequested?.Invoke());
  139. UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_PlayerBarBG/BTN_PlayerAvatar"), () => ProfileRequested?.Invoke());
  140. UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_PlayerBarBG/IMG_CoinBalanceBG/BTN_AddCoins"), () => AddCoinsRequested?.Invoke());
  141. UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_PlayerBarBG/BTN_Settings"), () => SettingsRequested?.Invoke());
  142. wired = true;
  143. }
  144. void SelectMode(ModeDefinition mode)
  145. {
  146. if (mode == null)
  147. return;
  148. SaveService.SetSelectedMode(mode.key);
  149. Refresh();
  150. ModeSelected?.Invoke(mode);
  151. }
  152. public void Refresh()
  153. {
  154. if (!Application.isPlaying)
  155. return;
  156. UILocaleScreens.ApplyLobby(transform);
  157. var save = SaveService.Data;
  158. string selectedKey = save.selectedMode;
  159. if (playerName != null)
  160. playerName.text = save.player.name;
  161. int level = SaveService.PlayerLevel();
  162. if (playerLevel != null)
  163. playerLevel.text = L.F("lobby.lv_short", level);
  164. if (playerDescription != null)
  165. playerDescription.text = L.F("lobby.local_player", level);
  166. if (playerAvatar != null)
  167. {
  168. Sprite avatar = UIViewUtility.LoadAvatarSprite(save.player.avatar);
  169. if (avatar != null)
  170. playerAvatar.sprite = avatar;
  171. }
  172. if (coinBalance != null)
  173. coinBalance.text = UIViewUtility.FormatNumber(save.coins);
  174. if (topAward != null)
  175. topAward.text = L.F("lobby.top_award", UIViewUtility.FormatNumber(SaveService.TopAward()));
  176. for (int i = 0; i < modeCards.Length; i++)
  177. {
  178. ModeCard card = modeCards[i];
  179. if (card == null)
  180. continue;
  181. int cleared = save.cleared.Get(card.definition.key);
  182. int cardLevel = Mathf.Clamp(cleared + 1, 1, GameDefinitions.MaxLevel);
  183. float normalized = cleared / (float)GameDefinitions.MaxLevel;
  184. if (card.level != null)
  185. card.level.text = L.F("lobby.lv_progress", cardLevel, GameDefinitions.MaxLevel);
  186. if (card.levelsTag != null)
  187. card.levelsTag.text = L.F("lobby.levels_count", GameDefinitions.MaxLevel);
  188. if (card.description != null)
  189. card.description.text = GameLocale.ModeDescription(card.definition.key);
  190. if (card.progress != null)
  191. card.progress.text = cleared + "/" + GameDefinitions.MaxLevel;
  192. UIViewUtility.SetProgress(card.progressFill, normalized);
  193. bool selected = selectedKey == card.definition.key;
  194. card.root.localScale = selected ? Vector3.one * selectedCardScale : Vector3.one;
  195. if (card.background != null)
  196. card.background.color = selected ? Color.white : new Color(0.92f, 0.92f, 0.92f, 1f);
  197. if (card.outline != null)
  198. card.outline.enabled = selected;
  199. }
  200. ModeDefinition selectedMode = GameDefinitions.GetMode(selectedKey);
  201. int selectedCleared = save.cleared.Get(selectedMode.key);
  202. int selectedLevel = Mathf.Clamp(selectedCleared + 1, 1, GameDefinitions.MaxLevel);
  203. if (selectedModeInfo != null)
  204. selectedModeInfo.text = L.F("lobby.mode_level_selected", GameLocale.ModeName(selectedMode.key), selectedLevel);
  205. if (movesLabel != null)
  206. movesLabel.text = ParsePreviewMoves(selectedMode.previewMoves);
  207. if (fruitsLabel != null)
  208. fruitsLabel.text = ParsePreviewFruits(selectedMode.previewFruits);
  209. if (checkInDot != null)
  210. checkInDot.gameObject.SetActive(SaveService.CanCheckInToday());
  211. if (drawDot != null)
  212. drawDot.gameObject.SetActive(SaveService.CanDraw());
  213. }
  214. static string ParsePreviewMoves(string preview)
  215. {
  216. if (string.IsNullOrEmpty(preview))
  217. return preview;
  218. string digits = "";
  219. for (int i = 0; i < preview.Length; i++)
  220. {
  221. if (char.IsDigit(preview[i]))
  222. digits += preview[i];
  223. }
  224. if (int.TryParse(digits, out int moves))
  225. return GameLocale.PreviewMoves(moves);
  226. return preview;
  227. }
  228. static string ParsePreviewFruits(string preview)
  229. {
  230. if (string.IsNullOrEmpty(preview))
  231. return preview;
  232. string digits = "";
  233. for (int i = 0; i < preview.Length; i++)
  234. {
  235. if (char.IsDigit(preview[i]))
  236. digits += preview[i];
  237. }
  238. if (int.TryParse(digits, out int count))
  239. return GameLocale.PreviewFruits(count);
  240. return preview;
  241. }
  242. }