DialoguePanel.cs 7.2 KB

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