DialoguePanel.cs 12 KB

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