DialoguePanel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. if (eventConditionConfig.ID != 0 && !_isShowingOptions)
  80. {
  81. ShowOptions();
  82. }
  83. // else
  84. // {
  85. // // 无选项,关闭面板,返回 null
  86. // UIManager.Instance.HideUIUIPanel(this);
  87. // finish?.Invoke(null);
  88. // }
  89. // return;
  90. }
  91. }
  92. }
  93. private void NextShow()
  94. {
  95. if (eventConditionConfig.optionType == 1 && _isShowingOptions)
  96. return;
  97. // if (_isShowingOptions)
  98. // return;
  99. if (_isUpdate)
  100. {
  101. SkipTyping();
  102. }
  103. else
  104. {
  105. StartShowMassge();
  106. }
  107. }
  108. public void ShowPanel(int id, string[] icon,
  109. ShowDialogueEventData.MessageShowType messageShowType,
  110. Action<int?> finish)
  111. {
  112. eventConditionConfig = ConfigComponent.Instance.Get<EventConditionConfig>(id);
  113. showIconName = icon;
  114. this.dialogueMessaga = eventConditionConfig.LanID;
  115. this.messageShowType = messageShowType;
  116. this.finish = finish;
  117. index = 0;
  118. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  119. StartShowMassge();
  120. }
  121. public void ShowPanel(int[] LanID, string[] icon,
  122. ShowDialogueEventData.MessageShowType messageShowType,
  123. Action<int?> finish)
  124. {
  125. showIconName = icon;
  126. this.dialogueMessaga = LanID;
  127. this.messageShowType = messageShowType;
  128. this.finish = finish;
  129. index = 0;
  130. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  131. StartShowMassge();
  132. }
  133. private void StartShowMassge()
  134. {
  135. if (index >= dialogueMessaga.Length)
  136. {
  137. if (eventConditionConfig.ID != 0 && !_isShowingOptions)
  138. {
  139. ShowOptions();
  140. }
  141. // 所有句子显示完成,检查是否有选项
  142. if (eventConditionConfig.ID != 0 && eventConditionConfig.optionType == 1 && !_isShowingOptions)
  143. {
  144. // ShowOptions();
  145. }
  146. else
  147. {
  148. if (eventConditionConfig.ID != 0 && eventConditionConfig.ConditionType != 0)
  149. {
  150. if (!EventSystemManager.Instance.CheckCondition(eventConditionConfig.ConditionType, eventConditionConfig.ConditionPara))
  151. {
  152. TipMessagePanel.OpenTipMessagePanel(EventHelper.GetTaskMessage(eventConditionConfig.ID));
  153. return;
  154. }
  155. }
  156. // 无选项,关闭面板,返回 null
  157. UIManager.Instance.HideUIUIPanel(this);
  158. finish?.Invoke(null);
  159. }
  160. return;
  161. }
  162. _isShowNextButton = false;
  163. nextIcon.SetActive(false);
  164. string m = LanguageManager.Instance.Text(dialogueMessaga[index]);
  165. if (showIconName != null && index < showIconName.Length)
  166. {
  167. icon.icon_name = showIconName[index];
  168. }
  169. index++;
  170. switch (messageShowType)
  171. {
  172. case ShowDialogueEventData.MessageShowType.Default:
  173. message.text = m;
  174. ShowNextIcon();
  175. break;
  176. case ShowDialogueEventData.MessageShowType.Verbatim:
  177. _sb.Clear();
  178. _currShowMessage = m.ToCharArray();
  179. _sb.Append(_currShowMessage[0]);
  180. message.text = _sb.ToString();
  181. _isUpdate = true;
  182. _currShowIndex = 1;
  183. _skipTyping = false;
  184. break;
  185. }
  186. LayoutRebuilder.ForceRebuildLayoutImmediate(Dialgue);
  187. }
  188. private async void ShowOptions()
  189. {
  190. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  191. _isShowingOptions = true;
  192. if (eventConditionConfig.optionType == 1 && eventConditionConfig.optionPara1 != null)
  193. {
  194. foreach (var op in eventConditionConfig.optionPara1)
  195. {
  196. // EventConditionConfig eventConditionConfig = ConfigComponent.Instance.Get<EventConditionConfig>(op);
  197. DialogueOptionWidget dialogueOptionWidget =
  198. await UIManager.Instance.CreateGComponent<DialogueOptionWidget>(null, OptionRoot);
  199. dialogueOptionWidget.CustomInit(op, eventConditionConfig.ID, SelectOption);
  200. }
  201. }
  202. DialogueOptionWidget dialogueOptionWidget1 =
  203. await UIManager.Instance.CreateGComponent<DialogueOptionWidget>(null, OptionRoot);
  204. dialogueOptionWidget1.CustomInit((DialogueOptionWidget obj) =>
  205. {
  206. UIManager.Instance.HideUIUIPanel(this);
  207. finish?.Invoke(-1);
  208. });
  209. LayoutRebuilder.ForceRebuildLayoutImmediate(Dialgue);
  210. }
  211. private void SelectOption(DialogueOptionWidget obj)
  212. {
  213. int selectedOptionID = obj.eventConditionConfig.ID;
  214. if (obj.eventConditionConfig.ID != 0 && obj.eventConditionConfig.ConditionType != 0)
  215. {
  216. if (!EventSystemManager.Instance.CheckCondition(obj.eventConditionConfig.ConditionType, obj.eventConditionConfig.ConditionPara))
  217. {
  218. TipMessagePanel.OpenTipMessagePanel(EventHelper.GetTaskMessage(selectedOptionID));
  219. return;
  220. }
  221. }
  222. UIManager.Instance.HideUIUIPanel(this);
  223. finish?.Invoke(selectedOptionID);
  224. }
  225. private void ShowNextIcon()
  226. {
  227. nextIcon.SetActive(true);
  228. _isShowNextButton = true;
  229. }
  230. public override void Hide()
  231. {
  232. base.Hide();
  233. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Update);
  234. }
  235. public void Update()
  236. {
  237. if (EventSystemManager.Instance.isOpenUi && eventConditionConfig.ID != 0)
  238. return;
  239. if (!_isUpdate) return;
  240. if (messageShowType == ShowDialogueEventData.MessageShowType.Verbatim)
  241. {
  242. if (_currShowIndex >= _currShowMessage.Length)
  243. {
  244. _isUpdate = false;
  245. ShowNextIcon();
  246. if (index >= dialogueMessaga.Length)
  247. {
  248. // // 所有句子显示完成,检查是否有选项
  249. // if (eventConditionConfig.optionType == 1 && !_isShowingOptions)
  250. // {
  251. // ShowOptions();
  252. // }
  253. if (eventConditionConfig.ID != 0 && !_isShowingOptions)
  254. {
  255. ShowOptions();
  256. }
  257. }
  258. // StartShowMassge();
  259. // ShowNextIcon();
  260. return;
  261. }
  262. _currShowTime += Time.deltaTime;
  263. if (_currShowTime > _showTime)
  264. {
  265. _currShowTime -= _showTime;
  266. _sb.Append(_currShowMessage[_currShowIndex]);
  267. _currShowIndex++;
  268. message.text = _sb.ToString();
  269. }
  270. }
  271. }
  272. public override void Close()
  273. {
  274. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  275. _isShowingOptions = false;
  276. _skipTyping = false;
  277. index = 0;
  278. eventConditionConfig = default;
  279. _currShowMessage = null;
  280. dialogueMessaga = null;
  281. base.Close();
  282. }
  283. }
  284. }