using System; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; [DisallowMultipleComponent] public class StoryDialogView : MonoBehaviour { const int PageCount = 5; [Header("Prefab References")] [SerializeField] TMP_Text storyTitle; [SerializeField] TMP_Text dialogueText; [SerializeField] Button nextButton; [SerializeField] TMP_Text nextLabel; [SerializeField] Transform pageDotsRoot; readonly List pageDots = new List(); Action completedCallback; Button cardButton; Button maskButton; Sprite activeDotSprite; Sprite inactiveDotSprite; int pageIndex; bool showing; bool wired; bool dotsReady; public bool IsOpen => gameObject.activeSelf; void Awake() { CacheReferences(); EnsurePageDots(); WireInteractions(); if (!showing) gameObject.SetActive(false); } void OnEnable() { LocaleService.Changed += RefreshPage; } void OnDisable() { LocaleService.Changed -= RefreshPage; } void OnDestroy() { LocaleService.Changed -= RefreshPage; if (nextButton != null) nextButton.onClick.RemoveListener(Advance); if (cardButton != null) cardButton.onClick.RemoveListener(Advance); if (maskButton != null) maskButton.onClick.RemoveListener(Advance); } public void Show(Action onCompleted = null) { showing = true; completedCallback = onCompleted; pageIndex = 0; CacheReferences(); EnsurePageDots(); WireInteractions(); transform.SetAsLastSibling(); gameObject.SetActive(true); ApplyStaticStoryLabels(); RefreshPage(); } public void Hide() { showing = false; completedCallback = null; gameObject.SetActive(false); } public void Advance() { if (!gameObject.activeSelf) return; if (pageIndex >= PageCount - 1) { Complete(); return; } pageIndex++; RefreshPage(); } void Complete() { Action callback = completedCallback; completedCallback = null; SaveService.EnsureLoaded(); SaveService.Data.story = 1; SaveService.SaveNow(); showing = false; gameObject.SetActive(false); callback?.Invoke(); } void ApplyStaticStoryLabels() { LocaleUI.SetText(transform, "IMG_BG/TMP_OwnerBadge", "story.owner"); LocaleUI.SetText(transform, "IMG_BG/Image/TMP_CharacterName", "story.character"); } public void RefreshLocale() { ApplyStaticStoryLabels(); RefreshPage(); } void RefreshPage() { int page = pageIndex + 1; if (storyTitle != null) storyTitle.text = L.Get("story.page" + page + ".title"); if (dialogueText != null) dialogueText.text = L.Get("story.page" + page + ".body"); if (nextLabel != null) { nextLabel.text = pageIndex >= PageCount - 1 ? L.Get("story.start_shift") : L.Get("common.next"); nextLabel.fontSize = pageIndex >= PageCount - 1 ? 28f : 36f; } for (int i = 0; i < pageDots.Count; i++) pageDots[i].sprite = i == pageIndex ? activeDotSprite : inactiveDotSprite; } void CacheReferences() { if (storyTitle == null) storyTitle = UIViewUtility.Text(transform, "IMG_BG/TMP_StoryTitle"); if (dialogueText == null) dialogueText = UIViewUtility.Text(transform, "IMG_BG/Image/TMP_DialogueText"); if (nextButton == null) nextButton = UIViewUtility.Button(transform, "IMG_BG/BTN_Next", false); if (nextLabel == null && nextButton != null) nextLabel = nextButton.GetComponentInChildren(true); if (pageDotsRoot == null) pageDotsRoot = UIViewUtility.Find(transform, "IMG_BG/GRP_StoryPagination"); UIViewUtility.EnableAutoSize(dialogueText, 30f, 20f); } void EnsurePageDots() { if (dotsReady || pageDotsRoot == null) return; Image template = null; for (int i = 0; i < pageDotsRoot.childCount; i++) { Image dot = pageDotsRoot.GetChild(i).GetComponent(); if (dot == null) continue; if (template == null) template = dot; pageDots.Add(dot); } if (template == null) return; activeDotSprite = pageDots.Count > 0 ? pageDots[0].sprite : template.sprite; inactiveDotSprite = pageDots.Count > 1 ? pageDots[1].sprite : activeDotSprite; for (int i = 0; i < pageDots.Count; i++) pageDots[i].raycastTarget = false; while (pageDots.Count < PageCount) { GameObject clone = Instantiate(template.gameObject, pageDotsRoot, false); clone.name = "IMG_PageDot" + (pageDots.Count + 1); Image dot = clone.GetComponent(); dot.raycastTarget = false; pageDots.Add(dot); } for (int i = PageCount; i < pageDots.Count; i++) pageDots[i].gameObject.SetActive(false); dotsReady = true; } void WireInteractions() { if (wired) return; UIViewUtility.Bind(nextButton, Advance); Transform card = UIViewUtility.Find(transform, "IMG_BG"); if (card != null) { Graphic[] graphics = card.GetComponentsInChildren(true); for (int i = 0; i < graphics.Length; i++) graphics[i].raycastTarget = false; Graphic cardGraphic = card.GetComponent(); if (cardGraphic != null) cardGraphic.raycastTarget = true; Graphic nextGraphic = nextButton != null ? nextButton.GetComponent() : null; if (nextGraphic != null) nextGraphic.raycastTarget = true; cardButton = UIViewUtility.EnsureButton(card); cardButton.transition = Selectable.Transition.None; cardButton.navigation = new Navigation { mode = Navigation.Mode.None }; UIViewUtility.Bind(cardButton, Advance); } Transform mask = UIViewUtility.Find(transform, "IMG_Mask"); if (mask != null) { maskButton = UIViewUtility.EnsureButton(mask); maskButton.transition = Selectable.Transition.None; maskButton.navigation = new Navigation { mode = Navigation.Mode.None }; UIViewUtility.Bind(maskButton, Advance); } wired = true; } }