DialoguePanel.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using System.Text;
  2. using Common.Utility.CombatEvent;
  3. using Core.Language;
  4. using Excel2Json;
  5. using Fort23.UTool;
  6. using UnityEngine;
  7. using System;
  8. using Fort23.Core;
  9. using UnityEngine.UI;
  10. namespace Fort23.Mono
  11. {
  12. [UIBinding(prefab = "DialoguePanel")]
  13. public partial class DialoguePanel : UIPanel
  14. {
  15. private int[] dialogueMessaga;
  16. private ShowDialogueEventData.MessageShowType messageShowType;
  17. private Action<int?> finish;
  18. private int index;
  19. private char[] _currShowMessage;
  20. private float _showTime = 0.05f;
  21. private float _currShowTime = 0;
  22. protected StringBuilder _sb = new StringBuilder();
  23. private int _currShowIndex;
  24. private bool _isUpdate;
  25. private bool _isShowNextButton;
  26. private string[] showIconName;
  27. private EventConditionConfig eventConditionConfig;
  28. private bool _skipTyping;
  29. private bool _isShowingOptions;
  30. private int type;
  31. public static async void OpenDialoguePanel(int id, string[] icon,
  32. ShowDialogueEventData.MessageShowType messageShowType,
  33. Action<int?> finish)
  34. {
  35. DialoguePanel dialoguePanel = await UIManager.Instance.LoadAndOpenPanel<DialoguePanel>(null, UILayer.Top);
  36. dialoguePanel.ShowPanel(id, icon, messageShowType, finish);
  37. }
  38. public static async void OpenDialoguePanel( int[] LanID, string[] icon,
  39. ShowDialogueEventData.MessageShowType messageShowType,
  40. Action<int?> finish)
  41. {
  42. DialoguePanel dialoguePanel = await UIManager.Instance.LoadAndOpenPanel<DialoguePanel>(null, UILayer.Top);
  43. dialoguePanel.ShowPanel(LanID, icon, messageShowType, finish);
  44. }
  45. private void Init()
  46. {
  47. isAddStack = false;
  48. _skipTyping = false;
  49. _isShowingOptions = false;
  50. }
  51. protected override void AddEvent()
  52. {
  53. }
  54. protected override void DelEvent()
  55. {
  56. }
  57. public override void AddButtonEvent()
  58. {
  59. nextButton.onClick.AddListener(NextShow);
  60. }
  61. private void SkipTyping()
  62. {
  63. _skipTyping = true;
  64. if (_isUpdate)
  65. {
  66. _sb.Clear();
  67. _sb.Append(_currShowMessage);
  68. message.text = _sb.ToString();
  69. _currShowIndex = _currShowMessage.Length;
  70. _isUpdate = false;
  71. ShowNextIcon();
  72. if (index >= dialogueMessaga.Length)
  73. {
  74. // 所有句子显示完成,检查是否有选项
  75. if (eventConditionConfig.optionType == 1 && !_isShowingOptions)
  76. {
  77. ShowOptions();
  78. }
  79. // else
  80. // {
  81. // // 无选项,关闭面板,返回 null
  82. // UIManager.Instance.HideUIUIPanel(this);
  83. // finish?.Invoke(null);
  84. // }
  85. // return;
  86. }
  87. }
  88. }
  89. private void NextShow()
  90. {
  91. if (_isShowingOptions)
  92. return;
  93. if (_isUpdate)
  94. {
  95. SkipTyping();
  96. }
  97. else
  98. {
  99. StartShowMassge();
  100. }
  101. }
  102. public void ShowPanel(int id, string[] icon,
  103. ShowDialogueEventData.MessageShowType messageShowType,
  104. Action<int?> finish)
  105. {
  106. eventConditionConfig = ConfigComponent.Instance.Get<EventConditionConfig>(id);
  107. showIconName = icon;
  108. this.dialogueMessaga = eventConditionConfig.LanID;
  109. this.messageShowType = messageShowType;
  110. this.finish = finish;
  111. index = 0;
  112. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  113. StartShowMassge();
  114. }
  115. public void ShowPanel( int[] LanID, string[] icon,
  116. ShowDialogueEventData.MessageShowType messageShowType,
  117. Action<int?> finish)
  118. {
  119. showIconName = icon;
  120. this.dialogueMessaga = LanID;
  121. this.messageShowType = messageShowType;
  122. this.finish = finish;
  123. index = 0;
  124. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  125. StartShowMassge();
  126. }
  127. private void StartShowMassge()
  128. {
  129. if (index >= dialogueMessaga.Length)
  130. {
  131. // 所有句子显示完成,检查是否有选项
  132. if (eventConditionConfig.ID != 0 && eventConditionConfig.optionType == 1 && !_isShowingOptions)
  133. {
  134. ShowOptions();
  135. }
  136. else
  137. {
  138. // 无选项,关闭面板,返回 null
  139. UIManager.Instance.HideUIUIPanel(this);
  140. finish?.Invoke(null);
  141. }
  142. return;
  143. }
  144. _isShowNextButton = false;
  145. nextIcon.SetActive(false);
  146. string m = LanguageManager.Instance.Text(dialogueMessaga[index]);
  147. if (showIconName != null && index < showIconName.Length)
  148. {
  149. icon.icon_name = showIconName[index];
  150. }
  151. index++;
  152. switch (messageShowType)
  153. {
  154. case ShowDialogueEventData.MessageShowType.Default:
  155. message.text = m;
  156. ShowNextIcon();
  157. break;
  158. case ShowDialogueEventData.MessageShowType.Verbatim:
  159. _sb.Clear();
  160. _currShowMessage = m.ToCharArray();
  161. _sb.Append(_currShowMessage[0]);
  162. message.text = _sb.ToString();
  163. _isUpdate = true;
  164. _currShowIndex = 1;
  165. _skipTyping = false;
  166. break;
  167. }
  168. LayoutRebuilder.ForceRebuildLayoutImmediate(Dialgue);
  169. }
  170. private async void ShowOptions()
  171. {
  172. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  173. _isShowingOptions = true;
  174. foreach (var op in eventConditionConfig.optionPara1)
  175. {
  176. // EventConditionConfig eventConditionConfig = ConfigComponent.Instance.Get<EventConditionConfig>(op);
  177. DialogueOptionWidget dialogueOptionWidget =
  178. await UIManager.Instance.CreateGComponent<DialogueOptionWidget>(null, OptionRoot);
  179. dialogueOptionWidget.CustomInit(op, eventConditionConfig.ID, SelectOption);
  180. }
  181. LayoutRebuilder.ForceRebuildLayoutImmediate(Dialgue);
  182. }
  183. private void SelectOption(DialogueOptionWidget obj)
  184. {
  185. int selectedOptionID = obj.eventConditionConfig.ID;
  186. UIManager.Instance.HideUIUIPanel(this);
  187. finish?.Invoke(selectedOptionID);
  188. }
  189. private void ShowNextIcon()
  190. {
  191. nextIcon.SetActive(true);
  192. _isShowNextButton = true;
  193. }
  194. public override void Hide()
  195. {
  196. base.Hide();
  197. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Update);
  198. }
  199. public void Update()
  200. {
  201. if(EventSystemManager.Instance.isOpenUi)
  202. return;
  203. if (!_isUpdate) return;
  204. if (messageShowType == ShowDialogueEventData.MessageShowType.Verbatim)
  205. {
  206. if (_currShowIndex >= _currShowMessage.Length)
  207. {
  208. _isUpdate = false;
  209. ShowNextIcon();
  210. if (index >= dialogueMessaga.Length)
  211. {
  212. // 所有句子显示完成,检查是否有选项
  213. if (eventConditionConfig.optionType == 1 && !_isShowingOptions)
  214. {
  215. ShowOptions();
  216. }
  217. }
  218. // StartShowMassge();
  219. // ShowNextIcon();
  220. return;
  221. }
  222. _currShowTime += Time.deltaTime;
  223. if (_currShowTime > _showTime)
  224. {
  225. _currShowTime -= _showTime;
  226. _sb.Append(_currShowMessage[_currShowIndex]);
  227. _currShowIndex++;
  228. message.text = _sb.ToString();
  229. }
  230. }
  231. }
  232. public override void Close()
  233. {
  234. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  235. _isShowingOptions = false;
  236. _skipTyping = false;
  237. base.Close();
  238. }
  239. }
  240. }