| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- using System;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- [DisallowMultipleComponent]
- public class LobbyScreenView : MonoBehaviour
- {
- [Header("Layout")]
- [SerializeField] float selectedCardScale = 1.02f;
- public event Action<ModeDefinition> ModeSelected;
- public event Action PlayRequested;
- public event Action CheckInRequested;
- public event Action ShopRequested;
- public event Action DrawRequested;
- public event Action HelpRequested;
- public event Action ProfileRequested;
- public event Action SettingsRequested;
- public event Action AddCoinsRequested;
- TMP_Text playerName;
- TMP_Text playerLevel;
- TMP_Text playerDescription;
- Image playerAvatar;
- TMP_Text coinBalance;
- TMP_Text topAward;
- TMP_Text selectedModeInfo;
- TMP_Text movesLabel;
- TMP_Text fruitsLabel;
- Transform checkInDot;
- Transform drawDot;
- readonly ModeCard[] modeCards = new ModeCard[3];
- bool wired;
- sealed class ModeCard
- {
- public ModeDefinition definition;
- public Transform root;
- public TMP_Text levelsTag;
- public TMP_Text level;
- public TMP_Text description;
- public TMP_Text progress;
- public Transform progressFill;
- public Graphic background;
- public Outline outline;
- }
- void Awake()
- {
- CacheReferences();
- UIViewUtility.EnableAutoSize(playerName, 38f, 24f);
- WireButtons();
- }
- void OnEnable()
- {
- SaveService.Changed += HandleSaveChanged;
- LocaleService.Changed += HandleSaveChanged;
- Refresh();
- }
- void OnDisable()
- {
- SaveService.Changed -= HandleSaveChanged;
- LocaleService.Changed -= HandleSaveChanged;
- }
- public void Show()
- {
- gameObject.SetActive(true);
- Refresh();
- }
- public void Hide()
- {
- gameObject.SetActive(false);
- }
- void HandleSaveChanged()
- {
- if (isActiveAndEnabled)
- Refresh();
- }
- void CacheReferences()
- {
- playerName = UIViewUtility.Text(transform, "IMG_PlayerBarBG/TMP_PlayerName");
- playerLevel = UIViewUtility.Text(transform, "IMG_PlayerBarBG/BTN_PlayerAvatar/IMG_PlayerLevelBG/TMP_PlayerLevel");
- playerDescription = UIViewUtility.Text(transform, "IMG_PlayerBarBG/TMP_PlayerDescription");
- playerAvatar = UIViewUtility.Image(transform, "IMG_PlayerBarBG/BTN_PlayerAvatar/IMG_PlayerAvatarIcon");
- coinBalance = UIViewUtility.Text(transform, "IMG_PlayerBarBG/IMG_CoinBalanceBG/TMP_CoinBalance");
- topAward = UIViewUtility.Text(transform, "IMG_TopAwardRibbon/TMP_TopAwardLabel");
- selectedModeInfo = UIViewUtility.Text(transform, "IMG_MainBoardFrame/IMG_NowServingBoard/TMP_SelectedModeInfo");
- movesLabel = UIViewUtility.Text(transform, "IMG_MainBoardFrame/IMG_NowServingBoard/IMG_MovesBadge/TMP_MovesCount");
- fruitsLabel = UIViewUtility.Text(transform, "IMG_MainBoardFrame/IMG_NowServingBoard/IMG_MovesBadge (1)/TMP_FruitsCount");
- checkInDot = UIViewUtility.Find(transform, "IMG_BottomNavBG/IMG_CheckInIcon/IMG_CheckInNotification");
- drawDot = UIViewUtility.Find(transform, "IMG_BottomNavBG/IMG_DrawIcon/IMG_CheckInNotification");
- string[] paths =
- {
- "IMG_MainBoardFrame/SCROLL_MainScroll/Viewport/Content/BTN_ClassicStand",
- "IMG_MainBoardFrame/SCROLL_MainScroll/Viewport/Content/BTN_RushStand",
- "IMG_MainBoardFrame/SCROLL_MainScroll/Viewport/Content/BTN_SealedStand"
- };
- for (int i = 0; i < GameDefinitions.Modes.Length && i < modeCards.Length; i++)
- {
- Transform root = UIViewUtility.Find(transform, paths[i]);
- if (root == null)
- continue;
- modeCards[i] = new ModeCard
- {
- definition = GameDefinitions.Modes[i],
- root = root,
- levelsTag = UIViewUtility.Text(root, "Image (1)/TMP_TagLevels"),
- level = UIViewUtility.Text(root, "TMP_ModeLevel"),
- description = UIViewUtility.Text(root, "TMP_ModeDescription"),
- progress = UIViewUtility.Text(root, "TMP_ModeProgress"),
- progressFill = UIViewUtility.Find(root, "IMG_ClassicProgressBG/IMG_ClassicProgressFill"),
- background = root.GetComponent<Graphic>()
- };
- modeCards[i].outline = root.GetComponent<Outline>();
- if (modeCards[i].outline == null)
- modeCards[i].outline = root.gameObject.AddComponent<Outline>();
- modeCards[i].outline.effectColor = new Color32(255, 215, 92, 235);
- modeCards[i].outline.effectDistance = new Vector2(5f, -5f);
- modeCards[i].outline.useGraphicAlpha = true;
- modeCards[i].outline.enabled = false;
- }
- }
- void WireButtons()
- {
- if (wired)
- return;
- for (int i = 0; i < modeCards.Length; i++)
- {
- ModeCard card = modeCards[i];
- if (card == null || card.root == null)
- continue;
- ModeDefinition captured = card.definition;
- Button button = UIViewUtility.EnsureButton(card.root);
- UIViewUtility.ConfigureButton(button, card.root.GetComponent<Graphic>());
- UIViewUtility.Bind(button, () => SelectMode(captured));
- }
- UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_MainBoardFrame/IMG_NowServingBoard/BTN_Play"), () => PlayRequested?.Invoke());
- UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_BottomNavBG/IMG_CheckInIcon"), () => CheckInRequested?.Invoke());
- UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_BottomNavBG/IMG_ShopIcon"), () => ShopRequested?.Invoke());
- UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_BottomNavBG/IMG_DrawIcon"), () => DrawRequested?.Invoke());
- UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_BottomNavBG/IMG_HelpIcon"), () => HelpRequested?.Invoke());
- UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_BottomNavBG/IMG_ProfileIcon"), () => ProfileRequested?.Invoke());
- UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_PlayerBarBG/BTN_PlayerAvatar"), () => ProfileRequested?.Invoke());
- UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_PlayerBarBG/IMG_CoinBalanceBG/BTN_AddCoins"), () => AddCoinsRequested?.Invoke());
- UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_PlayerBarBG/BTN_Settings"), () => SettingsRequested?.Invoke());
- wired = true;
- }
- void SelectMode(ModeDefinition mode)
- {
- if (mode == null)
- return;
- SaveService.SetSelectedMode(mode.key);
- Refresh();
- ModeSelected?.Invoke(mode);
- }
- public void Refresh()
- {
- if (!Application.isPlaying)
- return;
- UILocaleScreens.ApplyLobby(transform);
- var save = SaveService.Data;
- string selectedKey = save.selectedMode;
- if (playerName != null)
- playerName.text = save.player.name;
- int level = SaveService.PlayerLevel();
- if (playerLevel != null)
- playerLevel.text = L.F("lobby.lv_short", level);
- if (playerDescription != null)
- playerDescription.text = L.F("lobby.local_player", level);
- if (playerAvatar != null)
- {
- Sprite avatar = UIViewUtility.LoadAvatarSprite(save.player.avatar);
- if (avatar != null)
- playerAvatar.sprite = avatar;
- }
- if (coinBalance != null)
- coinBalance.text = UIViewUtility.FormatNumber(save.coins);
- if (topAward != null)
- topAward.text = L.F("lobby.top_award", UIViewUtility.FormatNumber(SaveService.TopAward()));
- for (int i = 0; i < modeCards.Length; i++)
- {
- ModeCard card = modeCards[i];
- if (card == null)
- continue;
- int cleared = save.cleared.Get(card.definition.key);
- int cardLevel = Mathf.Clamp(cleared + 1, 1, GameDefinitions.MaxLevel);
- float normalized = cleared / (float)GameDefinitions.MaxLevel;
- if (card.level != null)
- card.level.text = L.F("lobby.lv_progress", cardLevel, GameDefinitions.MaxLevel);
- if (card.levelsTag != null)
- card.levelsTag.text = L.F("lobby.levels_count", GameDefinitions.MaxLevel);
- if (card.description != null)
- card.description.text = GameLocale.ModeDescription(card.definition.key);
- if (card.progress != null)
- card.progress.text = cleared + "/" + GameDefinitions.MaxLevel;
- UIViewUtility.SetProgress(card.progressFill, normalized);
- bool selected = selectedKey == card.definition.key;
- card.root.localScale = selected ? Vector3.one * selectedCardScale : Vector3.one;
- if (card.background != null)
- card.background.color = selected ? Color.white : new Color(0.92f, 0.92f, 0.92f, 1f);
- if (card.outline != null)
- card.outline.enabled = selected;
- }
- ModeDefinition selectedMode = GameDefinitions.GetMode(selectedKey);
- int selectedCleared = save.cleared.Get(selectedMode.key);
- int selectedLevel = Mathf.Clamp(selectedCleared + 1, 1, GameDefinitions.MaxLevel);
- if (selectedModeInfo != null)
- selectedModeInfo.text = L.F("lobby.mode_level_selected", GameLocale.ModeName(selectedMode.key), selectedLevel);
- if (movesLabel != null)
- movesLabel.text = ParsePreviewMoves(selectedMode.previewMoves);
- if (fruitsLabel != null)
- fruitsLabel.text = ParsePreviewFruits(selectedMode.previewFruits);
- if (checkInDot != null)
- checkInDot.gameObject.SetActive(SaveService.CanCheckInToday());
- if (drawDot != null)
- drawDot.gameObject.SetActive(SaveService.CanDraw());
- }
- static string ParsePreviewMoves(string preview)
- {
- if (string.IsNullOrEmpty(preview))
- return preview;
- string digits = "";
- for (int i = 0; i < preview.Length; i++)
- {
- if (char.IsDigit(preview[i]))
- digits += preview[i];
- }
- if (int.TryParse(digits, out int moves))
- return GameLocale.PreviewMoves(moves);
- return preview;
- }
- static string ParsePreviewFruits(string preview)
- {
- if (string.IsNullOrEmpty(preview))
- return preview;
- string digits = "";
- for (int i = 0; i < preview.Length; i++)
- {
- if (char.IsDigit(preview[i]))
- digits += preview[i];
- }
- if (int.TryParse(digits, out int count))
- return GameLocale.PreviewFruits(count);
- return preview;
- }
- }
|