DialoguePanel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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.Collections.Generic;
  9. using System.Linq;
  10. using Fort23.Core;
  11. using GameLogic.Bag;
  12. using GameLogic.Player;
  13. using UnityEngine.UI;
  14. namespace Fort23.Mono
  15. {
  16. [UIBinding(prefab = "DialoguePanel")]
  17. public partial class DialoguePanel : UIPanel
  18. {
  19. private bool isShowAnima;
  20. private int[] dialogueMessaga;
  21. private ShowDialogueEventData.MessageShowType messageShowType;
  22. private Action<int?> finish;
  23. private int index;
  24. private char[] _currShowMessage;
  25. private float _showTime = 0.03f;
  26. private float _currShowTime = 0;
  27. protected StringBuilder _sb = new StringBuilder();
  28. private int _currShowIndex;
  29. private bool _isUpdate;
  30. private bool _isShowNextButton;
  31. private string[] showIconName;
  32. private EventLinkConfig eventConditionConfig;
  33. private bool _skipTyping;
  34. private bool _isShowingOptions;
  35. private AccountFileInfo.EventList CurrentEventList;
  36. private AccountFileInfo.EventLinkData _eventLinkData;
  37. private int type;
  38. private Text showText;
  39. private GameObject shownextIcon;
  40. private bool isOver;
  41. public static async CTask OpenDialoguePanel(AccountFileInfo.EventList CurrentEventList, int id, string[] icon,
  42. ShowDialogueEventData.MessageShowType messageShowType,
  43. Action<int?> finish, bool isShowAnima)
  44. {
  45. DialoguePanel dialoguePanel = await UIManager.Instance.LoadAndOpenPanel<DialoguePanel>(null, UILayer.Top,
  46. uiData: new object[] { CurrentEventList, id, icon, messageShowType, finish, isShowAnima });
  47. // if (isShowAnima)
  48. // {
  49. // dialoguePanel.DialgueType1Anim.Play("DialgueType1open");
  50. // }
  51. // else
  52. // {
  53. // dialoguePanel.DialgueType1Anim.Play("DialgueType1idle1");
  54. // }
  55. // dialoguePanel.ShowPanel(CurrentEventList, id, icon, messageShowType, finish);
  56. }
  57. public static async void OpenDialoguePanel(int[] LanID, string[] icon,
  58. ShowDialogueEventData.MessageShowType messageShowType,
  59. Action<int?> finish)
  60. {
  61. DialoguePanel dialoguePanel = await UIManager.Instance.LoadAndOpenPanel<DialoguePanel>(null, UILayer.Top);
  62. dialoguePanel.ShowPanel(LanID, icon, messageShowType, finish);
  63. }
  64. public async override CTask Show()
  65. {
  66. EventManager.Instance.Dispatch(CustomEventType.DialoguePanelOpen, null);
  67. base.Show();
  68. if (isShowAnima)
  69. {
  70. DialgueType1Anim.Play("DialgueType1open");
  71. }
  72. else
  73. {
  74. DialgueType1Anim.Play("DialgueType1idle1");
  75. }
  76. }
  77. public async override CTask<bool> AsyncInit(object[] uiData)
  78. {
  79. await ShowPanel(uiData[0] as AccountFileInfo.EventList, (int)uiData[1], uiData[2] as string[],
  80. (ShowDialogueEventData.MessageShowType)uiData[3], uiData[4] as Action<int?>, (bool)uiData[5]);
  81. return await base.AsyncInit(uiData);
  82. }
  83. private void Init()
  84. {
  85. isAddStack = false;
  86. _skipTyping = false;
  87. _isShowingOptions = false;
  88. }
  89. protected override void AddEvent()
  90. {
  91. }
  92. protected override void DelEvent()
  93. {
  94. }
  95. public override void AddButtonEvent()
  96. {
  97. nextButton.onClick.AddListener(NextShow);
  98. Btn_Cancel.onClick.AddListener(() =>
  99. {
  100. UIManager.Instance.HideUIUIPanel(this);
  101. finish?.Invoke(-1);
  102. });
  103. }
  104. private void SkipTyping()
  105. {
  106. _skipTyping = true;
  107. if (_isUpdate)
  108. {
  109. _sb.Clear();
  110. _sb.Append(_currShowMessage);
  111. showText.text = _sb.ToString();
  112. _currShowIndex = _currShowMessage.Length;
  113. _isUpdate = false;
  114. ShowNextIcon();
  115. if (index >= dialogueMessaga.Length)
  116. {
  117. if (eventConditionConfig.ID != 0 && !_isShowingOptions)
  118. {
  119. ShowOptions();
  120. }
  121. }
  122. }
  123. }
  124. private void NextShow()
  125. {
  126. if (eventConditionConfig.optionType == 1 && _isShowingOptions)
  127. return;
  128. if (_isUpdate)
  129. {
  130. SkipTyping();
  131. }
  132. else
  133. {
  134. StartShowMassge();
  135. }
  136. }
  137. public async CTask ShowPanel(AccountFileInfo.EventList CurrentEventList, int id, string[] icon,
  138. ShowDialogueEventData.MessageShowType messageShowType,
  139. Action<int?> finish, bool isShowAnima)
  140. {
  141. this.isShowAnima = isShowAnima;
  142. this.CurrentEventList = CurrentEventList;
  143. eventConditionConfig = ConfigComponent.Instance.Get<EventLinkConfig>(id);
  144. _eventLinkData = CurrentEventList.eventLinks.FirstOrDefault(el => el.eventLinkId == id);
  145. showIconName = icon;
  146. this.dialogueMessaga = eventConditionConfig.LanID;
  147. this.messageShowType = messageShowType;
  148. this.finish = finish;
  149. index = 0;
  150. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  151. if (eventConditionConfig.DialogueType == 0 || eventConditionConfig.DialogueType == 1)
  152. {
  153. DialgueType1.gameObject.SetActive(true);
  154. DialgueType2.gameObject.SetActive(false);
  155. DialgueType3.gameObject.SetActive(false);
  156. showText = message;
  157. shownextIcon = nextIcon;
  158. }
  159. else if (eventConditionConfig.DialogueType == 2)
  160. {
  161. DialgueType1.gameObject.SetActive(false);
  162. DialgueType3.gameObject.SetActive(false);
  163. DialgueType2.gameObject.SetActive(true);
  164. showText = message2;
  165. shownextIcon = nextIcon2;
  166. }
  167. else if (eventConditionConfig.DialogueType == 3)
  168. {
  169. DialgueType1.gameObject.SetActive(false);
  170. DialgueType2.gameObject.SetActive(false);
  171. DialgueType3.gameObject.SetActive(true);
  172. showText = message3;
  173. shownextIcon = nextIcon3;
  174. }
  175. if (eventConditionConfig.NPCID == 100)
  176. {
  177. DialgueType1.GetComponent<CustomStateController>().ChangeState(0);
  178. Text_Name1.text = PlayerManager.Instance.PlayerName;
  179. PLayerNameRoot.SetActive(true);
  180. }
  181. else if (eventConditionConfig.NPCID == 0)
  182. {
  183. PLayerNameRoot.SetActive(false);
  184. }
  185. else
  186. {
  187. EventNPC eventNpc = ConfigComponent.Instance.Get<EventNPC>(eventConditionConfig.NPCID);
  188. if (eventNpc.isNpc)
  189. {
  190. DialgueType1.GetComponent<CustomStateController>().ChangeState(1);
  191. PLayerNameRoot.SetActive(true);
  192. Text_Name2.text = LanguageManager.Instance.Text(eventNpc.name);
  193. }
  194. else
  195. {
  196. PLayerNameRoot.SetActive(false);
  197. }
  198. }
  199. StartShowMassge();
  200. }
  201. public static string ReverseUsingLoop(string input)
  202. {
  203. if (string.IsNullOrEmpty(input))
  204. return input;
  205. StringBuilder sb = new StringBuilder(input.Length);
  206. for (int i = input.Length - 1; i >= 0; i--)
  207. {
  208. sb.Append(input[i]);
  209. }
  210. return sb.ToString();
  211. }
  212. public void ShowPanel(int[] LanID, string[] icon,
  213. ShowDialogueEventData.MessageShowType messageShowType,
  214. Action<int?> finish)
  215. {
  216. showIconName = icon;
  217. this.dialogueMessaga = LanID;
  218. this.messageShowType = messageShowType;
  219. this.finish = finish;
  220. index = 0;
  221. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  222. StartShowMassge();
  223. }
  224. private async CTask StartShowMassge()
  225. {
  226. if (isOver)
  227. return;
  228. CTask cTask = CTask.Create();
  229. icon.onSpriteAlter = () =>
  230. {
  231. icon.SetNativeSize();
  232. icon.gameObject.SetActive(true);
  233. cTask.SetResult();
  234. };
  235. if (eventConditionConfig.NPCID != 100 && eventConditionConfig.NPCID != 0)
  236. {
  237. icon.gameObject.SetActive(false);
  238. EventNPC eventNpc = ConfigComponent.Instance.Get<EventNPC>(eventConditionConfig.NPCID);
  239. icon.icon_name = eventNpc.icon;
  240. }
  241. else
  242. {
  243. icon.gameObject.SetActive(true);
  244. cTask.SetResult();
  245. // icon.icon_name = "bg_lh_di1";
  246. }
  247. await cTask;
  248. if (eventConditionConfig.EventID == 10000)
  249. {
  250. Btn_Cancel.gameObject.SetActive(false);
  251. }
  252. else
  253. {
  254. Btn_Cancel.gameObject.SetActive(true);
  255. }
  256. Btn_Bag.gameObject.SetActive(false);
  257. if (index >= dialogueMessaga.Length)
  258. {
  259. isOver = true;
  260. if (eventConditionConfig.ID != 0 && !_isShowingOptions)
  261. {
  262. ShowOptions();
  263. }
  264. // 所有句子显示完成,检查是否有选项
  265. if (eventConditionConfig.ID != 0 && eventConditionConfig.optionType == 1 && !_isShowingOptions)
  266. {
  267. // ShowOptions();
  268. }
  269. else
  270. {
  271. if (eventConditionConfig.ID != 0 && _eventLinkData != null &&
  272. _eventLinkData.eventConditions.Count > 0)
  273. {
  274. foreach (var eventConditionData in _eventLinkData.eventConditions)
  275. {
  276. if (!EventSystemManager.Instance.IsEvenkLinkComplete(eventConditionData))
  277. {
  278. TipMessagePanel.OpenTipMessagePanel(EventHelper.GetTaskMessage(eventConditionData));
  279. return;
  280. }
  281. }
  282. }
  283. // if (eventConditionConfig.optionType == 3)
  284. // {
  285. // Btn_Bag.gameObject.SetActive(true);
  286. // List<ItemInfo> itemInfos = new List<ItemInfo>();
  287. // for (var i = 0; i < eventConditionConfig.PrizeIDs.Length; i++)
  288. // {
  289. // DropConfig dropConfig =
  290. // ConfigComponent.Instance.Get<DropConfig>(eventConditionConfig.PrizeIDs[i]);
  291. // if (dropConfig.dropType == 3)
  292. // {
  293. // ItemInfo itemInfo = new ItemInfo(dropConfig.dropGroupID[0],
  294. // eventConditionConfig.PrizeNums[i]);
  295. // itemInfos.Add(itemInfo);
  296. // }
  297. // else
  298. // {
  299. // List<ItemInfo> items = DropManager.Instance.DropItem(eventConditionConfig.PrizeIDs[i]);
  300. // itemInfos.AddRange(items);
  301. // }
  302. // }
  303. //
  304. // ShowItemMoveToTargetPanel showItemMoveToTargetUiPane =
  305. // UIManager.Instance.GetComponent<ShowItemMoveToTargetPanel>();
  306. // Vector3 target = showItemMoveToTargetUiPane.transform.worldToLocalMatrix *
  307. // Btn_Bag.transform.position;
  308. // Vector3 startPos = showItemMoveToTargetUiPane.transform.worldToLocalMatrix *
  309. // StartPos.transform.position;
  310. // int maxCount = 1;
  311. //
  312. // foreach (var itemInfo in itemInfos)
  313. // {
  314. // int addValue = (int)itemInfo.count.Value / maxCount;
  315. // int finishVale = (int)itemInfo.count.Value % maxCount;
  316. // // ItemInfo i = itemInfo;
  317. // showItemMoveToTargetUiPane.ShowPanel(startPos, target, maxCount, itemInfo.config.icon,
  318. // CombatItemShowEventData.ShowType.HeroExp, (imageMove) =>
  319. // {
  320. // int currAddValue = addValue;
  321. // if (imageMove.GroupIndex == maxCount - 1)
  322. // {
  323. // currAddValue += finishVale;
  324. // }
  325. // });
  326. // }
  327. //
  328. // await TimerComponent.Instance.WaitAsync(2000);
  329. // }
  330. // 无选项,关闭面板,返回 null
  331. UIManager.Instance.HideUIUIPanel(this);
  332. finish?.Invoke(null);
  333. }
  334. return;
  335. }
  336. _isShowNextButton = false;
  337. shownextIcon.SetActive(false);
  338. string m = LanguageManager.Instance.Text(dialogueMessaga[index], PlayerManager.Instance.PlayerName);
  339. // if (showIconName != null && index < showIconName.Length)
  340. // {
  341. // icon.icon_name = showIconName[index];
  342. // }
  343. index++;
  344. switch (messageShowType)
  345. {
  346. case ShowDialogueEventData.MessageShowType.Default:
  347. showText.text = m;
  348. ShowNextIcon();
  349. break;
  350. case ShowDialogueEventData.MessageShowType.Verbatim:
  351. _sb.Clear();
  352. _currShowMessage = m.ToCharArray();
  353. if (eventConditionConfig.DialogueType == 2 && eventConditionConfig.NPCID == 100 &&
  354. PlayerManager.Instance.PlayerName != String.Empty)
  355. {
  356. _sb.Append(PlayerManager.Instance.PlayerName + ":");
  357. }
  358. _sb.Append(_currShowMessage[0]);
  359. showText.text = _sb.ToString();
  360. _isUpdate = true;
  361. _currShowIndex = 1;
  362. _skipTyping = false;
  363. break;
  364. }
  365. LayoutRebuilder.ForceRebuildLayoutImmediate(Dialgue);
  366. }
  367. List<DialogueOptionWidget> dialogueOptionWidgets = new List<DialogueOptionWidget>();
  368. private async void ShowOptions()
  369. {
  370. await MarskPanel.OpenPanel();
  371. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  372. dialogueOptionWidgets.Clear();
  373. _isShowingOptions = true;
  374. if (eventConditionConfig.optionType == 1 && eventConditionConfig.optionPara1 != null)
  375. {
  376. OptionMarsk.SetActive(true);
  377. using (CTaskAwaitBuffer<DialogueOptionWidget> cts = new CTaskAwaitBuffer<DialogueOptionWidget>())
  378. {
  379. for (var i = 0; i < eventConditionConfig.optionPara1.Length; i++)
  380. {
  381. cts.AddTask(UIManager.Instance.CreateGComponent<DialogueOptionWidget>(null, OptionPos));
  382. }
  383. dialogueOptionWidgets = await cts.WaitAll();
  384. }
  385. for (var i = 0; i < eventConditionConfig.optionPara1.Length; i++)
  386. {
  387. dialogueOptionWidgets[i].CustomInit(i, CurrentEventList, eventConditionConfig.optionPara1[i],
  388. eventConditionConfig.ID, SelectOption);
  389. }
  390. LayoutRebuilder.ForceRebuildLayoutImmediate(OptionPos);
  391. OptionRoot.sizeDelta = OptionPos.sizeDelta;
  392. List<Vector2> pos = new List<Vector2>();
  393. for (var i = 0; i < dialogueOptionWidgets.Count; i++)
  394. {
  395. pos.Add(dialogueOptionWidgets[i].transform.anchoredPosition);
  396. }
  397. for (var i = 0; i < dialogueOptionWidgets.Count; i++)
  398. {
  399. dialogueOptionWidgets[i].transform.parent = OptionRoot;
  400. dialogueOptionWidgets[i].transform.anchoredPosition = pos[i];
  401. dialogueOptionWidgets[i].Animator.Play("DialogueOptionWidgetopen");
  402. await TimerComponent.Instance.WaitAsync(200);
  403. }
  404. // for (var i = dialogueOptionWidgets.Count - 1; i >= 0; i--)
  405. // {
  406. // Vector2 p = dialogueOptionWidgets[i].transform.anchoredPosition;
  407. // dialogueOptionWidgets[i].transform.SetParent(OptionRoot);
  408. // dialogueOptionWidgets[i].transform.anchoredPosition = p;
  409. // dialogueOptionWidgets[i].Animator.Play("DialogueOptionWidgetopen");
  410. // await TimerComponent.Instance.WaitAsync(200);
  411. // }
  412. }
  413. await MarskPanel.ClosePanel();
  414. }
  415. private async void SelectOption(DialogueOptionWidget obj)
  416. {
  417. if (obj.eventLinkData != null &&
  418. !EventSystemManager.Instance.IsCeekEventGroupComplete(obj.eventLinkData.eventConditions))
  419. {
  420. bool isOk = await EventTipsPanel.OpenPanel(obj.eventLinkData);
  421. if (isOk)
  422. {
  423. UIManager.Instance.HideUIUIPanel(this);
  424. finish?.Invoke(-1);
  425. }
  426. // TipMessagePanel.OpenTipMessagePanel(EventHelper.GetTaskMessage(selectedOptionID));
  427. return;
  428. }
  429. OptionMarsk.SetActive(false);
  430. int selectedOptionID = obj.eventConditionConfig.ID;
  431. UIManager.Instance.HideUIUIPanel(this);
  432. finish?.Invoke(selectedOptionID);
  433. }
  434. private void ShowNextIcon()
  435. {
  436. shownextIcon.SetActive(true);
  437. _isShowNextButton = true;
  438. }
  439. public override void Hide()
  440. {
  441. base.Hide();
  442. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Update);
  443. }
  444. public void Update()
  445. {
  446. // if (EventSystemManager.Instance.isOpenUi && eventConditionConfig.ID != 0)
  447. // return;
  448. if (!_isUpdate) return;
  449. if (messageShowType == ShowDialogueEventData.MessageShowType.Verbatim)
  450. {
  451. if (_currShowIndex >= _currShowMessage.Length)
  452. {
  453. _isUpdate = false;
  454. ShowNextIcon();
  455. if (index >= dialogueMessaga.Length)
  456. {
  457. if (eventConditionConfig.ID != 0 && !_isShowingOptions)
  458. {
  459. ShowOptions();
  460. }
  461. }
  462. return;
  463. }
  464. _currShowTime += Time.deltaTime;
  465. if (_currShowTime > _showTime)
  466. {
  467. _currShowTime -= _showTime;
  468. string v = _currShowMessage[_currShowIndex].ToString();
  469. if (v == "<")
  470. {
  471. int count = 0;
  472. int endIndex = 0;
  473. for (int i = _currShowIndex; i < _currShowMessage.Length - 1; i++)
  474. {
  475. count++;
  476. string ve = _currShowMessage[i].ToString();
  477. _sb.Append(ve);
  478. if (ve == ">")
  479. {
  480. string ve1 = _currShowMessage[i - 1].ToString();
  481. if (ve1 == "r")
  482. {
  483. endIndex = i;
  484. break;
  485. }
  486. }
  487. }
  488. _currShowIndex += count;
  489. }
  490. else
  491. {
  492. _sb.Append(_currShowMessage[_currShowIndex]);
  493. _currShowIndex++;
  494. }
  495. showText.text = _sb.ToString();
  496. }
  497. }
  498. }
  499. public async override CTask Close()
  500. {
  501. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  502. dialogueOptionWidgets.Clear();
  503. _isShowingOptions = false;
  504. _skipTyping = false;
  505. index = 0;
  506. eventConditionConfig = default;
  507. _eventLinkData = null;
  508. CurrentEventList = null;
  509. _currShowMessage = null;
  510. dialogueMessaga = null;
  511. // DialgueType1.gameObject.SetActive(false);
  512. // DialgueType2.gameObject.SetActive(false);
  513. OptionMarsk.SetActive(false);
  514. isOver = false;
  515. await base.Close();
  516. // DialgueType1Anim.Play("DialgueType1idle");
  517. }
  518. }
  519. }