StoryViewUI.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. for (int i = 0; i < prePopulateButtons; i++)
  49. {
  50. GameObject obj = Instantiate(storyBranchPrefab, branchContainer);
  51. obj.name = obj.name.Replace("(Clone)", "");
  52. obj.SetActive(false);
  53. buttons.Add(obj);
  54. }
  55. }
  56. void OnEnable()
  57. {
  58. EventManager.broadcast.StartListening(storyName, OnStoryStep);
  59. UiManager.RegisterElement("StoryView", this, this.transform, isVisible);
  60. }
  61. void OnDisable()
  62. {
  63. EventManager.broadcast.StopListening(storyName, OnStoryStep);
  64. UiManager.UnregisterElement(this);
  65. }
  66. private void OnStoryStep(StoryStepData storyStep)
  67. {
  68. switch (storyStep.category)
  69. {
  70. case StoryStepType.Line:
  71. if(showLines) ShowStoryLine(storyStep);
  72. break;
  73. case StoryStepType.Branch:
  74. if(showBranches) ShowStoryBranch(storyStep);
  75. break;
  76. case StoryStepType.Path:
  77. ShowStoryPath(storyStep);
  78. break;
  79. default:
  80. break;
  81. }
  82. }
  83. private void ShowStoryLine(StoryStepData storyStep)
  84. {
  85. if(showLines == false) return;
  86. ShowPanel();
  87. if(showMultipleLines)
  88. {
  89. GameObject obj = Instantiate(storyLinePrefab, lineContainer);
  90. obj.name = obj.name.Replace("(Clone)", "");
  91. StoryViewText line = obj.GetComponent<StoryViewText>();
  92. line.Setup(storyStep);
  93. lines.Add(obj);
  94. StartCoroutine(AutoScroll());
  95. }
  96. else lineText.Setup(storyStep);
  97. if(usePortraits) ShowPortrait(storyStep);
  98. }
  99. private void ShowStoryBranch(StoryStepData storyStep)
  100. {
  101. if(buttons != null)
  102. {
  103. for (int i = storyStep.branches.Count; i < buttons.Count; i++)
  104. {
  105. //Destroy(buttons[i]);
  106. buttons[i].SetActive(false);
  107. }
  108. }
  109. buttons = new List<GameObject>();
  110. ShowPanel();
  111. for (int i = 0; i < storyStep.branches.Count; i++)
  112. {
  113. GameObject obj;
  114. if(buttons.Count > i)
  115. {
  116. obj = buttons[i];
  117. obj.SetActive(true);
  118. }
  119. else
  120. {
  121. obj = Instantiate(storyBranchPrefab, branchContainer);
  122. obj.name = obj.name.Replace("(Clone)", "");
  123. }
  124. StoryViewButton btn = obj.GetComponent<StoryViewButton>();
  125. btn.Init(storyName, storyStep, i, this);
  126. buttons.Add(obj);
  127. StartCoroutine(AutoScroll());
  128. }
  129. lastBranch = storyStep;
  130. }
  131. private void ShowStoryPath(StoryStepData storyStep)
  132. {
  133. if(lastBranch == null) return;
  134. if(hidePanelAfterChoice) Hide();
  135. foreach (var btn in buttons) Destroy(btn);
  136. buttons = new List<GameObject>();
  137. lastBranch = null;
  138. if(destroyLinesAfterPath) foreach (var line in lines) Destroy(line);
  139. }
  140. private void ShowPortrait(StoryStepData storyStep)
  141. {
  142. var s = storyStep.text;
  143. var name = s.IndexOf(" ") > -1
  144. ? s.Substring(0,s.IndexOf(" "))
  145. : s;
  146. if (name.EndsWith(":"))
  147. {
  148. name = name.Remove(name.Length - 1, 1);
  149. foreach(KeyValuePair<string, Sprite> keyValue in portraits)
  150. {
  151. string key = keyValue.Key;
  152. if(key == name)
  153. {
  154. Sprite sprite = keyValue.Value;
  155. portraitImage.sprite = sprite;
  156. portraitContainer.SetActive(true);
  157. return;
  158. }
  159. }
  160. }
  161. portraitContainer.SetActive(false);
  162. }
  163. public void ButtonClicked(StoryStepData storyStep)
  164. {
  165. //Debug.Log("Button clicked");
  166. if(hidePanelAfterChoice) UiManager.HideElement(this);
  167. }
  168. public void Show()
  169. {
  170. isVisible = true;
  171. ShowPanel();
  172. }
  173. public void Hide()
  174. {
  175. isVisible = false;
  176. panel.gameObject.SetActive(false);
  177. }
  178. public bool IsVisible() => isVisible;
  179. private void ShowPanel()
  180. {
  181. if(isVisible) panel.gameObject.SetActive(true);
  182. }
  183. private IEnumerator AutoScroll()
  184. {
  185. if(lineContainer != null)
  186. LayoutRebuilder.ForceRebuildLayoutImmediate(lineContainer.GetComponent<RectTransform>());
  187. yield return new WaitForEndOfFrame();
  188. yield return new WaitForEndOfFrame();
  189. if(scrollRect != null) scrollRect.verticalNormalizedPosition = 0;
  190. }
  191. }
  192. }