StoryDialogView.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. [DisallowMultipleComponent]
  7. public class StoryDialogView : MonoBehaviour
  8. {
  9. const int PageCount = 5;
  10. [Header("Prefab References")]
  11. [SerializeField] TMP_Text storyTitle;
  12. [SerializeField] TMP_Text dialogueText;
  13. [SerializeField] Button nextButton;
  14. [SerializeField] TMP_Text nextLabel;
  15. [SerializeField] Transform pageDotsRoot;
  16. readonly List<Image> pageDots = new List<Image>();
  17. Action completedCallback;
  18. Button cardButton;
  19. Button maskButton;
  20. Sprite activeDotSprite;
  21. Sprite inactiveDotSprite;
  22. int pageIndex;
  23. bool showing;
  24. bool wired;
  25. bool dotsReady;
  26. public bool IsOpen => gameObject.activeSelf;
  27. void Awake()
  28. {
  29. CacheReferences();
  30. EnsurePageDots();
  31. WireInteractions();
  32. if (!showing)
  33. gameObject.SetActive(false);
  34. }
  35. void OnEnable()
  36. {
  37. LocaleService.Changed += RefreshPage;
  38. }
  39. void OnDisable()
  40. {
  41. LocaleService.Changed -= RefreshPage;
  42. }
  43. void OnDestroy()
  44. {
  45. LocaleService.Changed -= RefreshPage;
  46. if (nextButton != null)
  47. nextButton.onClick.RemoveListener(Advance);
  48. if (cardButton != null)
  49. cardButton.onClick.RemoveListener(Advance);
  50. if (maskButton != null)
  51. maskButton.onClick.RemoveListener(Advance);
  52. }
  53. public void Show(Action onCompleted = null)
  54. {
  55. showing = true;
  56. completedCallback = onCompleted;
  57. pageIndex = 0;
  58. CacheReferences();
  59. EnsurePageDots();
  60. WireInteractions();
  61. transform.SetAsLastSibling();
  62. gameObject.SetActive(true);
  63. ApplyStaticStoryLabels();
  64. RefreshPage();
  65. }
  66. public void Hide()
  67. {
  68. showing = false;
  69. completedCallback = null;
  70. gameObject.SetActive(false);
  71. }
  72. public void Advance()
  73. {
  74. if (!gameObject.activeSelf)
  75. return;
  76. if (pageIndex >= PageCount - 1)
  77. {
  78. Complete();
  79. return;
  80. }
  81. pageIndex++;
  82. RefreshPage();
  83. }
  84. void Complete()
  85. {
  86. Action callback = completedCallback;
  87. completedCallback = null;
  88. SaveService.EnsureLoaded();
  89. SaveService.Data.story = 1;
  90. SaveService.SaveNow();
  91. showing = false;
  92. gameObject.SetActive(false);
  93. callback?.Invoke();
  94. }
  95. void ApplyStaticStoryLabels()
  96. {
  97. LocaleUI.SetText(transform, "IMG_BG/TMP_OwnerBadge", "story.owner");
  98. LocaleUI.SetText(transform, "IMG_BG/Image/TMP_CharacterName", "story.character");
  99. }
  100. public void RefreshLocale()
  101. {
  102. ApplyStaticStoryLabels();
  103. RefreshPage();
  104. }
  105. void RefreshPage()
  106. {
  107. int page = pageIndex + 1;
  108. if (storyTitle != null)
  109. storyTitle.text = L.Get("story.page" + page + ".title");
  110. if (dialogueText != null)
  111. dialogueText.text = L.Get("story.page" + page + ".body");
  112. if (nextLabel != null)
  113. {
  114. nextLabel.text = pageIndex >= PageCount - 1 ? L.Get("story.start_shift") : L.Get("common.next");
  115. nextLabel.fontSize = pageIndex >= PageCount - 1 ? 28f : 36f;
  116. }
  117. for (int i = 0; i < pageDots.Count; i++)
  118. pageDots[i].sprite = i == pageIndex ? activeDotSprite : inactiveDotSprite;
  119. }
  120. void CacheReferences()
  121. {
  122. if (storyTitle == null)
  123. storyTitle = UIViewUtility.Text(transform, "IMG_BG/TMP_StoryTitle");
  124. if (dialogueText == null)
  125. dialogueText = UIViewUtility.Text(transform, "IMG_BG/Image/TMP_DialogueText");
  126. if (nextButton == null)
  127. nextButton = UIViewUtility.Button(transform, "IMG_BG/BTN_Next", false);
  128. if (nextLabel == null && nextButton != null)
  129. nextLabel = nextButton.GetComponentInChildren<TMP_Text>(true);
  130. if (pageDotsRoot == null)
  131. pageDotsRoot = UIViewUtility.Find(transform, "IMG_BG/GRP_StoryPagination");
  132. UIViewUtility.EnableAutoSize(dialogueText, 30f, 20f);
  133. }
  134. void EnsurePageDots()
  135. {
  136. if (dotsReady || pageDotsRoot == null)
  137. return;
  138. Image template = null;
  139. for (int i = 0; i < pageDotsRoot.childCount; i++)
  140. {
  141. Image dot = pageDotsRoot.GetChild(i).GetComponent<Image>();
  142. if (dot == null)
  143. continue;
  144. if (template == null)
  145. template = dot;
  146. pageDots.Add(dot);
  147. }
  148. if (template == null)
  149. return;
  150. activeDotSprite = pageDots.Count > 0 ? pageDots[0].sprite : template.sprite;
  151. inactiveDotSprite = pageDots.Count > 1 ? pageDots[1].sprite : activeDotSprite;
  152. for (int i = 0; i < pageDots.Count; i++)
  153. pageDots[i].raycastTarget = false;
  154. while (pageDots.Count < PageCount)
  155. {
  156. GameObject clone = Instantiate(template.gameObject, pageDotsRoot, false);
  157. clone.name = "IMG_PageDot" + (pageDots.Count + 1);
  158. Image dot = clone.GetComponent<Image>();
  159. dot.raycastTarget = false;
  160. pageDots.Add(dot);
  161. }
  162. for (int i = PageCount; i < pageDots.Count; i++)
  163. pageDots[i].gameObject.SetActive(false);
  164. dotsReady = true;
  165. }
  166. void WireInteractions()
  167. {
  168. if (wired)
  169. return;
  170. UIViewUtility.Bind(nextButton, Advance);
  171. Transform card = UIViewUtility.Find(transform, "IMG_BG");
  172. if (card != null)
  173. {
  174. Graphic[] graphics = card.GetComponentsInChildren<Graphic>(true);
  175. for (int i = 0; i < graphics.Length; i++)
  176. graphics[i].raycastTarget = false;
  177. Graphic cardGraphic = card.GetComponent<Graphic>();
  178. if (cardGraphic != null)
  179. cardGraphic.raycastTarget = true;
  180. Graphic nextGraphic = nextButton != null ? nextButton.GetComponent<Graphic>() : null;
  181. if (nextGraphic != null)
  182. nextGraphic.raycastTarget = true;
  183. cardButton = UIViewUtility.EnsureButton(card);
  184. cardButton.transition = Selectable.Transition.None;
  185. cardButton.navigation = new Navigation { mode = Navigation.Mode.None };
  186. UIViewUtility.Bind(cardButton, Advance);
  187. }
  188. Transform mask = UIViewUtility.Find(transform, "IMG_Mask");
  189. if (mask != null)
  190. {
  191. maskButton = UIViewUtility.EnsureButton(mask);
  192. maskButton.transition = Selectable.Transition.None;
  193. maskButton.navigation = new Navigation { mode = Navigation.Mode.None };
  194. UIViewUtility.Bind(maskButton, Advance);
  195. }
  196. wired = true;
  197. }
  198. }