StoryViewUI.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using KairoEngine.Core;
  6. using Sirenix.OdinInspector;
  7. using KairoEngine.UI;
  8. using KairoEngine.StorySystem;
  9. namespace KairoEngine.StorySystem.UI
  10. {
  11. [HideMonoScript]
  12. public class StoryViewUI : SerializedMonoBehaviour, IUiSystemElement
  13. {
  14. public string storyName = "Storyline";
  15. public Transform panel;
  16. [BoxGroup("Story Lines")]public bool showLines = false;
  17. [BoxGroup("Story Lines"), ShowIf("@showLines")] public bool showMultipleLines = true;
  18. [BoxGroup("Story Lines"), ShowIf("@showLines && showMultipleLines")] public bool destroyLinesAfterPath = true;
  19. [BoxGroup("Story Lines"), ShowIf("@showLines && showMultipleLines")] public GameObject storyLinePrefab;
  20. [BoxGroup("Story Lines"), ShowIf("@showLines && showMultipleLines")] public Transform lineContainer;
  21. [BoxGroup("Story Lines"), ShowIf("@showLines && !showMultipleLines")] public StoryViewText lineText;
  22. [BoxGroup("Story Lines"), ShowIf("@showLines && showMultipleLines")] public ScrollRect scrollRect = null;
  23. [BoxGroup("Story Branches")] public bool showBranches = true;
  24. [BoxGroup("Story Branches"), ShowIf("@showBranches")] public bool hidePanelAfterChoice = true;
  25. [BoxGroup("Story Branches"), ShowIf("@showBranches")] public GameObject storyBranchPrefab;
  26. [BoxGroup("Story Branches"), ShowIf("@showBranches")] public Transform branchContainer;
  27. [BoxGroup("Story Branches"), ShowIf("@showBranches")] public int prePopulateButtons = 0;
  28. [BoxGroup("Portraits")] public bool usePortraits = false;
  29. [BoxGroup("Portraits"), ShowIf("@usePortraits")] public Image portraitImage;
  30. [BoxGroup("Portraits"), ShowIf("@usePortraits")] public GameObject portraitContainer;
  31. [BoxGroup("Portraits"), ShowInInspector, PropertySpace(4,2), ShowIf("@usePortraits")]
  32. public Dictionary<string, Sprite> portraits = new Dictionary<string, Sprite>();
  33. private List<GameObject> buttons;
  34. private StoryStepData lastBranch;
  35. private List<GameObject> lines = new List<GameObject>();
  36. private bool isVisible = false;
  37. void Awake()
  38. {
  39. // Test if all variables are present
  40. if(panel == null) Debug.LogError("Panel is missing in StoryViewUI", this.gameObject);
  41. if(showLines && storyLinePrefab == null) Debug.LogError("StoryViewUI is missing a Story line Prefab", this.gameObject);
  42. if(showLines && lineContainer == null) Debug.LogError("StoryViewUI is missing a Story line Container", this.gameObject);
  43. if(showBranches && storyBranchPrefab == null) Debug.LogError("StoryViewUI is missing a Story Branch Prefab", this.gameObject);
  44. if(showBranches && branchContainer == null) Debug.LogError("StoryViewUI is missing a Story Branch Container", this.gameObject);
  45. }
  46. void Start()
  47. {
  48. if(buttons != null)
  49. {
  50. for (int i = 0; i < buttons.Count; i++) Destroy(buttons[i]);
  51. }
  52. buttons.Clear();
  53. GameObject[] oldButtons = branchContainer.GetComponentsInChildren<GameObject>();
  54. if(oldButtons != null)
  55. {
  56. for (int i = 0; i < oldButtons.Length; i++) Destroy(oldButtons[i]);
  57. }
  58. for (int i = 0; i < prePopulateButtons; i++)
  59. {
  60. GameObject obj = Instantiate(storyBranchPrefab, branchContainer);
  61. obj.name = obj.name.Replace("(Clone)", "");
  62. obj.SetActive(false);
  63. buttons.Add(obj);
  64. }
  65. }
  66. void OnEnable()
  67. {
  68. EventManager.broadcast.StartListening(storyName, OnStoryStep);
  69. UiManager.RegisterElement("StoryView", this, this.transform, isVisible);
  70. }
  71. void OnDisable()
  72. {
  73. EventManager.broadcast.StopListening(storyName, OnStoryStep);
  74. UiManager.UnregisterElement(this);
  75. }
  76. private void OnStoryStep(StoryStepData storyStep)
  77. {
  78. switch (storyStep.category)
  79. {
  80. case StoryStepType.Line:
  81. if(showLines) ShowStoryLine(storyStep);
  82. break;
  83. case StoryStepType.Branch:
  84. if(showBranches) ShowStoryBranch(storyStep);
  85. break;
  86. case StoryStepType.Path:
  87. ShowStoryPath(storyStep);
  88. break;
  89. default:
  90. break;
  91. }
  92. }
  93. private void ShowStoryLine(StoryStepData storyStep)
  94. {
  95. if(showLines == false) return;
  96. ShowPanel();
  97. if(showMultipleLines)
  98. {
  99. GameObject obj = Instantiate(storyLinePrefab, lineContainer);
  100. obj.name = obj.name.Replace("(Clone)", "");
  101. StoryViewText line = obj.GetComponent<StoryViewText>();
  102. line.Setup(storyStep);
  103. lines.Add(obj);
  104. StartCoroutine(AutoScroll());
  105. }
  106. else lineText.Setup(storyStep);
  107. if(usePortraits) ShowPortrait(storyStep);
  108. }
  109. private void ShowStoryBranch(StoryStepData storyStep)
  110. {
  111. if(buttons != null)
  112. {
  113. for (int i = 0; i < buttons.Count; i++)
  114. {
  115. //Destroy(buttons[i]);
  116. buttons[i].SetActive(false);
  117. }
  118. }
  119. buttons = new List<GameObject>();
  120. ShowPanel();
  121. for (int i = 0; i < storyStep.branches.Count; i++)
  122. {
  123. GameObject obj;
  124. if(buttons.Count > i)
  125. {
  126. obj = buttons[i];
  127. obj.SetActive(true);
  128. }
  129. else
  130. {
  131. obj = Instantiate(storyBranchPrefab, branchContainer);
  132. obj.name = obj.name.Replace("(Clone)", "");
  133. }
  134. StoryViewButton btn = obj.GetComponent<StoryViewButton>();
  135. btn.Init(storyName, storyStep, i, this);
  136. buttons.Add(obj);
  137. StartCoroutine(AutoScroll());
  138. }
  139. lastBranch = storyStep;
  140. }
  141. private void ShowStoryPath(StoryStepData storyStep)
  142. {
  143. if(lastBranch == null) return;
  144. if(hidePanelAfterChoice) Hide();
  145. foreach (var btn in buttons) Destroy(btn);
  146. buttons = new List<GameObject>();
  147. lastBranch = null;
  148. if(destroyLinesAfterPath) foreach (var line in lines) Destroy(line);
  149. }
  150. private void ShowPortrait(StoryStepData storyStep)
  151. {
  152. var s = storyStep.text;
  153. var name = s.IndexOf(" ") > -1
  154. ? s.Substring(0,s.IndexOf(" "))
  155. : s;
  156. if (name.EndsWith(":"))
  157. {
  158. name = name.Remove(name.Length - 1, 1);
  159. foreach(KeyValuePair<string, Sprite> keyValue in portraits)
  160. {
  161. string key = keyValue.Key;
  162. if(key == name)
  163. {
  164. Sprite sprite = keyValue.Value;
  165. portraitImage.sprite = sprite;
  166. portraitContainer.SetActive(true);
  167. return;
  168. }
  169. }
  170. }
  171. portraitContainer.SetActive(false);
  172. }
  173. public void ButtonClicked(StoryStepData storyStep)
  174. {
  175. //Debug.Log("Button clicked");
  176. if(hidePanelAfterChoice) UiManager.HideElement(this);
  177. }
  178. public void Show()
  179. {
  180. isVisible = true;
  181. ShowPanel();
  182. }
  183. public void Hide()
  184. {
  185. isVisible = false;
  186. panel.gameObject.SetActive(false);
  187. }
  188. public bool IsVisible() => isVisible;
  189. private void ShowPanel()
  190. {
  191. if(isVisible) panel.gameObject.SetActive(true);
  192. }
  193. private IEnumerator AutoScroll()
  194. {
  195. if(lineContainer != null)
  196. LayoutRebuilder.ForceRebuildLayoutImmediate(lineContainer.GetComponent<RectTransform>());
  197. yield return new WaitForEndOfFrame();
  198. yield return new WaitForEndOfFrame();
  199. if(scrollRect != null) scrollRect.verticalNormalizedPosition = 0;
  200. }
  201. }
  202. }