using System; using TMPro; using UnityEngine; using UnityEngine.UI; [DisallowMultipleComponent] public class SettingsScreenView : MonoBehaviour { public event Action CloseRequested; public event Action MessageRequested; public event Action LedgerRequested; const string ContentPath = "IMG_BG/Scroll View/Viewport/Content"; static readonly (string group, string locale)[] LanguageOptions = { ("GRP_US", "en"), ("GRP_ES", "es"), ("GRP_BR", "pt-BR"), ("GRP_DE", "de"), ("GRP_FR", "fr"), ("GRP_IT", "it"), }; [Header("Prefab References")] [SerializeField] Image musicToggle; [SerializeField] Image sfxToggle; [SerializeField] Image motionToggle; [SerializeField] Button ledgerButton; [SerializeField] Button resetButton; [SerializeField] Button closeButton; [SerializeField] TMP_Text localSaveValue; [SerializeField] TMP_Text currentLanguageValue; [SerializeField] Transform resetSaveGroup; [SerializeField] Transform languagePanel; [SerializeField] Button languageOpenButton; [SerializeField] Button languageConfirmButton; [SerializeField] Button languageCancelButton; [SerializeField] Button resetCancelButton; [SerializeField] Button resetConfirmButton; Sprite toggleOnSprite; Sprite toggleOffSprite; ScrollRect scrollRect; string pendingLocale; bool wired; bool showing; void Awake() { ResolveReferences(); LoadSprites(); WireButtons(); HideConfirm(); HideLanguagePanel(); if (!showing) gameObject.SetActive(false); } void OnEnable() { LocaleService.Changed += Refresh; } void OnDisable() { LocaleService.Changed -= Refresh; } void OnDestroy() { LocaleService.Changed -= Refresh; if (closeButton != null) closeButton.onClick.RemoveListener(RequestClose); if (ledgerButton != null) ledgerButton.onClick.RemoveListener(RequestLedger); if (resetButton != null) resetButton.onClick.RemoveListener(RequestReset); if (resetCancelButton != null) resetCancelButton.onClick.RemoveListener(HideConfirm); if (resetConfirmButton != null) resetConfirmButton.onClick.RemoveListener(ConfirmReset); } public void Show() { showing = true; ResolveReferences(); LoadSprites(); WireButtons(); HideConfirm(); HideLanguagePanel(); transform.SetAsLastSibling(); gameObject.SetActive(true); Refresh(); ResetScroll(); } public void Hide() { showing = false; HideConfirm(); HideLanguagePanel(); gameObject.SetActive(false); } public void Refresh() { if (!Application.isPlaying) return; UILocaleScreens.ApplySettings(transform); var settings = SaveService.Data.settings; ApplyToggle(musicToggle, settings.bgm); ApplyToggle(sfxToggle, settings.sfx); ApplyToggle(motionToggle, settings.reduceMotion); if (localSaveValue != null) { localSaveValue.text = SaveService.SaveAvailable ? L.Get("settings.save_available") : L.Get("settings.save_unavailable"); UIViewUtility.EnableAutoSize(localSaveValue, 26f, 17f); } if (currentLanguageValue != null) currentLanguageValue.text = LocaleService.DisplayLanguageName(LocaleService.CurrentLocale); UpdateLanguageSelectionHighlight(); } void ResolveReferences() { if (musicToggle == null) musicToggle = UIViewUtility.Image(transform, ContentPath + "/GRP_Audio/IMG_MusicRow/TGL_Img"); if (sfxToggle == null) sfxToggle = UIViewUtility.Image(transform, ContentPath + "/GRP_Audio/IMG_SoundFxRow/TGL_Img"); string displayPath = ContentPath + "/GRP_Display/IMG_MusicRow/TGL_Img"; if (motionToggle == null) { motionToggle = UIViewUtility.Image(transform, displayPath); if (motionToggle == null) motionToggle = UIViewUtility.Image(transform, ContentPath + "/GRP_Desplay/IMG_MusicRow/TGL_Img"); } Transform displayGroup = UIViewUtility.Find(transform, ContentPath + "/GRP_Display"); if (displayGroup == null) displayGroup = UIViewUtility.Find(transform, ContentPath + "/GRP_Desplay"); if (displayGroup != null) displayGroup.name = "GRP_Display"; if (ledgerButton == null) ledgerButton = UIViewUtility.Button(transform, ContentPath + "/GRP_Data/IMG_MusicRow/Button", false); if (resetButton == null) resetButton = UIViewUtility.Button(transform, ContentPath + "/GRP_Data/IMG_SoundFxRow/Button", false); if (closeButton == null) closeButton = UIViewUtility.Button(transform, "IMG_BG/BTN_ClosePause", false); if (resetSaveGroup == null) resetSaveGroup = transform.Find("GRP_ResetSave"); if (languagePanel == null) languagePanel = transform.Find("GRP_SelectLanuage"); if (languageOpenButton == null) languageOpenButton = UIViewUtility.Button(transform, ContentPath + "/GRP_Data/IMG_SwitchLanuage/Button", false); if (languageConfirmButton == null && languagePanel != null) languageConfirmButton = UIViewUtility.Button(languagePanel, "IMG_BG/BTN_Confirm", false); if (languageCancelButton == null && languagePanel != null) languageCancelButton = UIViewUtility.Button(languagePanel, "IMG_BG/BTN_Cancel", false); if (resetCancelButton == null && resetSaveGroup != null) resetCancelButton = UIViewUtility.Button(resetSaveGroup, "IMG_BG/BTN_Cancel", false); if (resetConfirmButton == null && resetSaveGroup != null) resetConfirmButton = UIViewUtility.Button(resetSaveGroup, "IMG_BG/BTN_Confirm", false); if (currentLanguageValue == null) currentLanguageValue = UIViewUtility.Text(transform, ContentPath + "/GRP_Data/IMG_SwitchLanuage/TMP_RowDescription"); if (localSaveValue == null) localSaveValue = UIViewUtility.Text(transform, ContentPath + "/GRP_About/IMG_SoundFxRow/TMP_RowValue"); if (scrollRect == null) scrollRect = UIViewUtility.Get(transform, "IMG_BG/Scroll View"); } void LoadSprites() { if (toggleOnSprite == null || toggleOffSprite == null) { Sprite[] toggleSprites = Resources.LoadAll("UI/11_setting/spr_toggle"); for (int i = 0; i < toggleSprites.Length; i++) { if (toggleSprites[i].name == "on") toggleOnSprite = toggleSprites[i]; else if (toggleSprites[i].name == "off") toggleOffSprite = toggleSprites[i]; } } } void WireButtons() { if (wired) return; if (closeButton != null) UIViewUtility.Bind(closeButton, RequestClose); if (ledgerButton != null) UIViewUtility.Bind(ledgerButton, RequestLedger); if (resetButton != null) UIViewUtility.Bind(resetButton, RequestReset); if (resetCancelButton != null) UIViewUtility.Bind(resetCancelButton, HideConfirm); if (resetConfirmButton != null) UIViewUtility.Bind(resetConfirmButton, ConfirmReset); if (languageOpenButton != null) UIViewUtility.Bind(languageOpenButton, OpenLanguagePanel); if (languageConfirmButton != null) UIViewUtility.Bind(languageConfirmButton, ConfirmLanguage); if (languageCancelButton != null) UIViewUtility.Bind(languageCancelButton, HideLanguagePanel); WireLanguageCards(); BindToggleRow(musicToggle, ToggleMusic); BindToggleRow(sfxToggle, ToggleSfx); BindToggleRow(motionToggle, ToggleMotion); wired = true; } void WireLanguageCards() { if (languagePanel == null) return; for (int i = 0; i < LanguageOptions.Length; i++) { string groupName = LanguageOptions[i].group; string locale = LanguageOptions[i].locale; Transform card = languagePanel.Find("IMG_BG/GRP_Content/" + groupName); if (card == null) card = UIViewUtility.Find(languagePanel, "IMG_BG/GRP_Content/" + groupName); if (card == null) continue; Button button = UIViewUtility.EnsureButton(card); UIViewUtility.ConfigureButton(button, card.GetComponent()); string captured = locale; UIViewUtility.Bind(button, () => SelectPendingLocale(captured)); } } void SelectPendingLocale(string locale) { pendingLocale = locale; UpdateLanguageSelectionHighlight(); } void UpdateLanguageSelectionHighlight() { if (languagePanel == null) return; string active = string.IsNullOrEmpty(pendingLocale) ? LocaleService.CurrentLocale : pendingLocale; for (int i = 0; i < LanguageOptions.Length; i++) { Transform card = languagePanel.Find("IMG_BG/GRP_Content/" + LanguageOptions[i].group); if (card == null) continue; Transform select = card.Find("IMG_Select"); if (select != null) select.gameObject.SetActive(LanguageOptions[i].locale == active); } } void OpenLanguagePanel() { if (languagePanel == null) ResolveReferences(); pendingLocale = LocaleService.CurrentLocale; if (languagePanel != null) { languagePanel.gameObject.SetActive(true); languagePanel.SetAsLastSibling(); UpdateLanguageSelectionHighlight(); } } void ConfirmLanguage() { if (!string.IsNullOrEmpty(pendingLocale)) LocaleService.SetLocale(pendingLocale); HideLanguagePanel(); Refresh(); } void HideLanguagePanel() { if (languagePanel != null) languagePanel.gameObject.SetActive(false); } void BindToggleRow(Image toggle, UnityEngine.Events.UnityAction action) { if (toggle == null) return; Transform row = toggle.transform.parent; if (row != null) { Button rowButton = UIViewUtility.EnsureButton(row); UIViewUtility.ConfigureButton(rowButton, row.GetComponent()); UIViewUtility.Bind(rowButton, action); } Button toggleButton = UIViewUtility.EnsureButton(toggle.transform); UIViewUtility.ConfigureButton(toggleButton, toggle); UIViewUtility.Bind(toggleButton, action); } void ToggleMusic() { var settings = SaveService.Data.settings; settings.bgm = !settings.bgm; SaveService.SaveNow(); GameAudio.RefreshSettings(); Refresh(); } void ToggleSfx() { var settings = SaveService.Data.settings; settings.sfx = !settings.sfx; SaveService.SaveNow(); GameAudio.RefreshSettings(); Refresh(); } void ToggleMotion() { var settings = SaveService.Data.settings; settings.reduceMotion = !settings.reduceMotion; SaveService.SaveNow(); Refresh(); } void ApplyToggle(Image target, bool value) { if (target == null) return; Sprite sprite = value ? toggleOnSprite : toggleOffSprite; if (sprite != null) target.sprite = sprite; } void RequestClose() { HideConfirm(); HideLanguagePanel(); CloseRequested?.Invoke(); } void RequestLedger() { LedgerRequested?.Invoke(); } void RequestReset() { if (resetSaveGroup == null) ResolveReferences(); if (resetSaveGroup != null) { resetSaveGroup.gameObject.SetActive(true); resetSaveGroup.SetAsLastSibling(); } } void ConfirmReset() { HideConfirm(); SaveService.ResetToDefaults(); GameAudio.RefreshSettings(); Refresh(); MessageRequested?.Invoke(L.Get("toast.save_reset")); CloseRequested?.Invoke(); } void HideConfirm() { if (resetSaveGroup != null) resetSaveGroup.gameObject.SetActive(false); } void ResetScroll() { if (scrollRect == null) return; scrollRect.StopMovement(); scrollRect.verticalNormalizedPosition = 1f; } }