using System; using TMPro; using UnityEngine; using UnityEngine.UI; [DisallowMultipleComponent] public class ProfileScreenView : MonoBehaviour { public event Action CloseRequested; public event Action MessageRequested; readonly AvatarCell[] avatarCells = new AvatarCell[GameDefinitions.AvatarSpriteIndices.Length]; readonly Transform[] modeFills = new Transform[3]; readonly TMP_Text[] modeValues = new TMP_Text[3]; TMP_Text playerName; TMP_Text playerLevel; TMP_Text playerDescription; Image playerAvatar; TMP_Text coinBalance; TMP_Text levelsCleared; TMP_Text setsServed; TMP_Text bestScore; TMP_Text coinsEarned; TMP_InputField nameInput; Button saveNameButton; bool wired; sealed class AvatarCell { public int spriteIndex; public Transform root; public Image image; public Transform selectedFrame; public Transform selectedCheck; } void Awake() { CacheReferences(); UIViewUtility.EnableAutoSize(playerName, 40f, 22f); 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_BG/GRP_PlayerBar/TMP_PlayerName"); playerLevel = UIViewUtility.Text(transform, "IMG_BG/GRP_PlayerBar/IMG_PlayerLevelBG/TMP_PlayerLevel"); playerDescription = UIViewUtility.Text(transform, "IMG_BG/GRP_PlayerBar/TMP_PlayerDescription"); playerAvatar = UIViewUtility.Image(transform, "IMG_BG/GRP_PlayerBar/BTN_PlayerAvatar/IMG_PlayerAvatarIcon"); coinBalance = UIViewUtility.Text(transform, "IMG_BG/GRP_PlayerBar/IMG_CoinPanel/TMP_CoinBalance"); levelsCleared = UIViewUtility.Text(transform, "IMG_BG/GRP_PlayerBar/GRP_PlayerStats/GRP_LevelsCleared/TMP_Count"); setsServed = UIViewUtility.Text(transform, "IMG_BG/GRP_PlayerBar/GRP_PlayerStats/GRP_SetsServed/TMP_Count"); bestScore = UIViewUtility.Text(transform, "IMG_BG/GRP_PlayerBar/GRP_PlayerStats/GRP_BestScore/TMP_Count"); coinsEarned = UIViewUtility.Text(transform, "IMG_BG/GRP_PlayerBar/GRP_PlayerStats/GRP_CoinsEarned/TMP_Count"); nameInput = UIViewUtility.Get(transform, "IMG_BG/GRP_EditName/INP_PlayerName"); saveNameButton = UIViewUtility.Button(transform, "IMG_BG/GRP_EditName/BTN_Save"); string[] modePaths = { "GRP_ClassicStand", "GRP_RushHour", "GRP_SealedStand" }; for (int i = 0; i < modePaths.Length; i++) { modeFills[i] = UIViewUtility.Find(transform, "IMG_BG/GRP_ModeProgress/" + modePaths[i] + "/IMG_ProgressFill"); modeValues[i] = UIViewUtility.Text(transform, "IMG_BG/GRP_ModeProgress/" + modePaths[i] + "/TMP_ProgressValue"); } for (int i = 0; i < avatarCells.Length; i++) { Transform root = UIViewUtility.Find(transform, "IMG_BG/GRP_AvatarSelect/GRP_AvatarRows/IMG_Avatar" + (i + 1)); if (root == null) continue; avatarCells[i] = new AvatarCell { spriteIndex = GameDefinitions.AvatarSpriteIndices[i], root = root, image = UIViewUtility.Image(root, "Image"), selectedFrame = UIViewUtility.Find(root, "IMG_Select"), selectedCheck = UIViewUtility.Find(root, "IMG_AvatarSelectedCheck") }; } UIViewUtility.SetGraphicVisible(transform, "IMG_BG/GRP_AvatarSelect/GRP_AvatarRows/IMG_Avatar13", false); UIViewUtility.SetGraphicVisible(transform, "IMG_BG/GRP_AvatarSelect/GRP_AvatarRows/IMG_Avatar14", false); } void WireButtons() { if (wired) return; UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_BG/BTN_ClosePause"), () => CloseRequested?.Invoke()); UIViewUtility.Bind(saveNameButton, SaveName); for (int i = 0; i < avatarCells.Length; i++) { AvatarCell cell = avatarCells[i]; if (cell == null) continue; int capturedIndex = cell.spriteIndex; Button button = UIViewUtility.EnsureButton(cell.root); UIViewUtility.ConfigureButton(button, cell.root.GetComponent()); UIViewUtility.Bind(button, () => SelectAvatar(capturedIndex)); } wired = true; } void SelectAvatar(int spriteIndex) { SaveService.SetAvatar(spriteIndex); Refresh(); MessageRequested?.Invoke(L.Get("toast.avatar_updated")); } void SaveName() { string raw = nameInput != null ? nameInput.text : ""; if (!SaveService.SetPlayerName(raw, out string sanitized)) { GameAudio.PlaySfx(GameSfx.Error); MessageRequested?.Invoke(L.Get("toast.name_short")); return; } if (nameInput != null) nameInput.text = sanitized; GameAudio.PlaySfx(GameSfx.Buy); Refresh(); MessageRequested?.Invoke(L.Get("toast.name_updated")); } public void Refresh() { if (!Application.isPlaying) return; UILocaleScreens.ApplyProfile(transform); var save = SaveService.Data; int level = SaveService.PlayerLevel(); if (playerName != null) playerName.text = save.player.name; if (playerLevel != null) playerLevel.text = L.F("profile.level", level); if (playerDescription != null) playerDescription.text = L.F("profile.player_id", save.player.id); if (nameInput != null && nameInput.placeholder != null) { var placeholder = nameInput.placeholder as TMP_Text; if (placeholder != null) placeholder.text = L.Get("profile.name_placeholder"); } 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 (levelsCleared != null) levelsCleared.text = UIViewUtility.FormatNumber(save.stats.clears); if (setsServed != null) setsServed.text = UIViewUtility.FormatNumber(save.stats.served); if (bestScore != null) bestScore.text = UIViewUtility.FormatNumber(save.stats.best); if (coinsEarned != null) coinsEarned.text = UIViewUtility.FormatNumber(save.stats.earned); for (int i = 0; i < GameDefinitions.Modes.Length; i++) { ModeDefinition mode = GameDefinitions.Modes[i]; int cleared = save.cleared.Get(mode.key); if (modeValues[i] != null) modeValues[i].text = cleared + "/" + GameDefinitions.MaxLevel; UIViewUtility.SetProgress(modeFills[i], cleared / (float)GameDefinitions.MaxLevel); } for (int i = 0; i < avatarCells.Length; i++) { AvatarCell cell = avatarCells[i]; if (cell == null) continue; bool selected = cell.spriteIndex == save.player.avatar; if (cell.selectedFrame != null) cell.selectedFrame.gameObject.SetActive(selected); if (cell.selectedCheck != null) cell.selectedCheck.gameObject.SetActive(selected); if (cell.image != null) { Sprite sprite = UIViewUtility.LoadAvatarSprite(cell.spriteIndex); if (sprite != null) cell.image.sprite = sprite; } } if (nameInput != null && !nameInput.isFocused) nameInput.text = save.player.name; } }