DialoguePanel.cs 11 KB

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