StoryViewUI.cs 7.3 KB

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