DialoguePanel.cs 13 KB

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