DialoguePanel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. this.messageShowType = ShowDialogueEventData.MessageShowType.Verbatim;
  159. }
  160. else if (eventConditionConfig.DialogueType == 2)
  161. {
  162. DialgueType1.gameObject.SetActive(false);
  163. DialgueType3.gameObject.SetActive(false);
  164. DialgueType2.gameObject.SetActive(true);
  165. showText = message2;
  166. shownextIcon = nextIcon2;
  167. this.messageShowType = ShowDialogueEventData.MessageShowType.Verbatim;
  168. }
  169. else if (eventConditionConfig.DialogueType == 3)
  170. {
  171. DialgueType1.gameObject.SetActive(false);
  172. DialgueType2.gameObject.SetActive(false);
  173. DialgueType3.gameObject.SetActive(true);
  174. showText = message3;
  175. shownextIcon = nextIcon3;
  176. this.messageShowType = ShowDialogueEventData.MessageShowType.Default;
  177. }
  178. if (eventConditionConfig.NPCID == 100)
  179. {
  180. DialgueType1.GetComponent<CustomStateController>().ChangeState(0);
  181. Text_Name1.text = PlayerManager.Instance.PlayerName;
  182. PLayerNameRoot.SetActive(true);
  183. }
  184. else if (eventConditionConfig.NPCID == 0)
  185. {
  186. PLayerNameRoot.SetActive(false);
  187. }
  188. else
  189. {
  190. EventNPC eventNpc = ConfigComponent.Instance.Get<EventNPC>(eventConditionConfig.NPCID);
  191. if (eventNpc.isNpc)
  192. {
  193. DialgueType1.GetComponent<CustomStateController>().ChangeState(1);
  194. PLayerNameRoot.SetActive(true);
  195. Text_Name2.text = LanguageManager.Instance.Text(eventNpc.name);
  196. }
  197. else
  198. {
  199. PLayerNameRoot.SetActive(false);
  200. }
  201. }
  202. StartShowMassge();
  203. }
  204. public static string ReverseUsingLoop(string input)
  205. {
  206. if (string.IsNullOrEmpty(input))
  207. return input;
  208. StringBuilder sb = new StringBuilder(input.Length);
  209. for (int i = input.Length - 1; i >= 0; i--)
  210. {
  211. sb.Append(input[i]);
  212. }
  213. return sb.ToString();
  214. }
  215. public void ShowPanel(int[] LanID, string[] icon,
  216. ShowDialogueEventData.MessageShowType messageShowType,
  217. Action<int?> finish)
  218. {
  219. showIconName = icon;
  220. this.dialogueMessaga = LanID;
  221. this.messageShowType = messageShowType;
  222. this.finish = finish;
  223. index = 0;
  224. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  225. StartShowMassge();
  226. }
  227. private async CTask StartShowMassge()
  228. {
  229. if (isOver)
  230. return;
  231. CTask cTask = CTask.Create();
  232. icon.onSpriteAlter = () =>
  233. {
  234. icon.SetNativeSize();
  235. icon.gameObject.SetActive(true);
  236. cTask.SetResult();
  237. };
  238. if (eventConditionConfig.NPCID != 100 && eventConditionConfig.NPCID != 0)
  239. {
  240. icon.gameObject.SetActive(false);
  241. EventNPC eventNpc = ConfigComponent.Instance.Get<EventNPC>(eventConditionConfig.NPCID);
  242. icon.icon_name = eventNpc.icon;
  243. }
  244. else
  245. {
  246. icon.gameObject.SetActive(true);
  247. cTask.SetResult();
  248. // icon.icon_name = "bg_lh_di1";
  249. }
  250. await cTask;
  251. if (eventConditionConfig.EventID == 10000)
  252. {
  253. Btn_Cancel.gameObject.SetActive(false);
  254. }
  255. else
  256. {
  257. Btn_Cancel.gameObject.SetActive(true);
  258. }
  259. Btn_Bag.gameObject.SetActive(false);
  260. if (index >= dialogueMessaga.Length)
  261. {
  262. isOver = true;
  263. if (eventConditionConfig.ID != 0 && !_isShowingOptions)
  264. {
  265. ShowOptions();
  266. }
  267. // 所有句子显示完成,检查是否有选项
  268. if (eventConditionConfig.ID != 0 && eventConditionConfig.optionType == 1 && !_isShowingOptions)
  269. {
  270. // ShowOptions();
  271. }
  272. else
  273. {
  274. if (eventConditionConfig.ID != 0 && _eventLinkData != null &&
  275. _eventLinkData.eventConditions.Count > 0)
  276. {
  277. foreach (var eventConditionData in _eventLinkData.eventConditions)
  278. {
  279. if (!EventSystemManager.Instance.IsEvenkLinkComplete(eventConditionData))
  280. {
  281. TipMessagePanel.OpenTipMessagePanel(EventHelper.GetTaskMessage(eventConditionData));
  282. return;
  283. }
  284. }
  285. }
  286. // if (eventConditionConfig.optionType == 3)
  287. // {
  288. // Btn_Bag.gameObject.SetActive(true);
  289. // List<ItemInfo> itemInfos = new List<ItemInfo>();
  290. // for (var i = 0; i < eventConditionConfig.PrizeIDs.Length; i++)
  291. // {
  292. // DropConfig dropConfig =
  293. // ConfigComponent.Instance.Get<DropConfig>(eventConditionConfig.PrizeIDs[i]);
  294. // if (dropConfig.dropType == 3)
  295. // {
  296. // ItemInfo itemInfo = new ItemInfo(dropConfig.dropGroupID[0],
  297. // eventConditionConfig.PrizeNums[i]);
  298. // itemInfos.Add(itemInfo);
  299. // }
  300. // else
  301. // {
  302. // List<ItemInfo> items = DropManager.Instance.DropItem(eventConditionConfig.PrizeIDs[i]);
  303. // itemInfos.AddRange(items);
  304. // }
  305. // }
  306. //
  307. // ShowItemMoveToTargetPanel showItemMoveToTargetUiPane =
  308. // UIManager.Instance.GetComponent<ShowItemMoveToTargetPanel>();
  309. // Vector3 target = showItemMoveToTargetUiPane.transform.worldToLocalMatrix *
  310. // Btn_Bag.transform.position;
  311. // Vector3 startPos = showItemMoveToTargetUiPane.transform.worldToLocalMatrix *
  312. // StartPos.transform.position;
  313. // int maxCount = 1;
  314. //
  315. // foreach (var itemInfo in itemInfos)
  316. // {
  317. // int addValue = (int)itemInfo.count.Value / maxCount;
  318. // int finishVale = (int)itemInfo.count.Value % maxCount;
  319. // // ItemInfo i = itemInfo;
  320. // showItemMoveToTargetUiPane.ShowPanel(startPos, target, maxCount, itemInfo.config.icon,
  321. // CombatItemShowEventData.ShowType.HeroExp, (imageMove) =>
  322. // {
  323. // int currAddValue = addValue;
  324. // if (imageMove.GroupIndex == maxCount - 1)
  325. // {
  326. // currAddValue += finishVale;
  327. // }
  328. // });
  329. // }
  330. //
  331. // await TimerComponent.Instance.WaitAsync(2000);
  332. // }
  333. // 无选项,关闭面板,返回 null
  334. UIManager.Instance.HideUIUIPanel(this);
  335. finish?.Invoke(null);
  336. }
  337. return;
  338. }
  339. _isShowNextButton = false;
  340. shownextIcon.SetActive(false);
  341. string m = LanguageManager.Instance.Text(dialogueMessaga[index], PlayerManager.Instance.PlayerName);
  342. // if (showIconName != null && index < showIconName.Length)
  343. // {
  344. // icon.icon_name = showIconName[index];
  345. // }
  346. index++;
  347. switch (messageShowType)
  348. {
  349. case ShowDialogueEventData.MessageShowType.Default:
  350. showText.text = m;
  351. ShowNextIcon();
  352. break;
  353. case ShowDialogueEventData.MessageShowType.Verbatim:
  354. _sb.Clear();
  355. _currShowMessage = m.ToCharArray();
  356. if (eventConditionConfig.DialogueType == 2 && eventConditionConfig.NPCID == 100 &&
  357. PlayerManager.Instance.PlayerName != String.Empty)
  358. {
  359. _sb.Append(PlayerManager.Instance.PlayerName + ":");
  360. }
  361. _sb.Append(_currShowMessage[0]);
  362. showText.text = _sb.ToString();
  363. _isUpdate = true;
  364. _currShowIndex = 1;
  365. _skipTyping = false;
  366. break;
  367. }
  368. LayoutRebuilder.ForceRebuildLayoutImmediate(Dialgue);
  369. }
  370. List<DialogueOptionWidget> dialogueOptionWidgets = new List<DialogueOptionWidget>();
  371. private async void ShowOptions()
  372. {
  373. await MarskPanel.OpenPanel();
  374. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  375. dialogueOptionWidgets.Clear();
  376. _isShowingOptions = true;
  377. if (eventConditionConfig.optionType == 1 && eventConditionConfig.optionPara1 != null)
  378. {
  379. OptionMarsk.SetActive(true);
  380. using (CTaskAwaitBuffer<DialogueOptionWidget> cts = new CTaskAwaitBuffer<DialogueOptionWidget>())
  381. {
  382. for (var i = 0; i < eventConditionConfig.optionPara1.Length; i++)
  383. {
  384. cts.AddTask(UIManager.Instance.CreateGComponent<DialogueOptionWidget>(null, OptionPos));
  385. }
  386. dialogueOptionWidgets = await cts.WaitAll();
  387. }
  388. for (var i = 0; i < eventConditionConfig.optionPara1.Length; i++)
  389. {
  390. dialogueOptionWidgets[i].CustomInit(i, CurrentEventList, eventConditionConfig.optionPara1[i],
  391. eventConditionConfig.ID, SelectOption);
  392. }
  393. LayoutRebuilder.ForceRebuildLayoutImmediate(OptionPos);
  394. OptionRoot.sizeDelta = OptionPos.sizeDelta;
  395. List<Vector2> pos = new List<Vector2>();
  396. for (var i = 0; i < dialogueOptionWidgets.Count; i++)
  397. {
  398. pos.Add(dialogueOptionWidgets[i].transform.anchoredPosition);
  399. }
  400. for (var i = 0; i < dialogueOptionWidgets.Count; i++)
  401. {
  402. dialogueOptionWidgets[i].transform.parent = OptionRoot;
  403. dialogueOptionWidgets[i].transform.anchoredPosition = pos[i];
  404. dialogueOptionWidgets[i].Animator.Play("DialogueOptionWidgetopen");
  405. await TimerComponent.Instance.WaitAsync(200);
  406. }
  407. // for (var i = dialogueOptionWidgets.Count - 1; i >= 0; i--)
  408. // {
  409. // Vector2 p = dialogueOptionWidgets[i].transform.anchoredPosition;
  410. // dialogueOptionWidgets[i].transform.SetParent(OptionRoot);
  411. // dialogueOptionWidgets[i].transform.anchoredPosition = p;
  412. // dialogueOptionWidgets[i].Animator.Play("DialogueOptionWidgetopen");
  413. // await TimerComponent.Instance.WaitAsync(200);
  414. // }
  415. }
  416. await MarskPanel.ClosePanel();
  417. }
  418. private async void SelectOption(DialogueOptionWidget obj)
  419. {
  420. if (obj.eventLinkData != null &&
  421. !EventSystemManager.Instance.IsCeekEventGroupComplete(obj.eventLinkData.eventConditions))
  422. {
  423. bool isOk = await EventTipsPanel.OpenPanel(obj.eventLinkData);
  424. if (isOk)
  425. {
  426. UIManager.Instance.HideUIUIPanel(this);
  427. finish?.Invoke(-1);
  428. }
  429. // TipMessagePanel.OpenTipMessagePanel(EventHelper.GetTaskMessage(selectedOptionID));
  430. return;
  431. }
  432. OptionMarsk.SetActive(false);
  433. int selectedOptionID = obj.eventConditionConfig.ID;
  434. UIManager.Instance.HideUIUIPanel(this);
  435. finish?.Invoke(selectedOptionID);
  436. }
  437. private void ShowNextIcon()
  438. {
  439. shownextIcon.SetActive(true);
  440. _isShowNextButton = true;
  441. }
  442. public override void Hide()
  443. {
  444. base.Hide();
  445. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Update);
  446. }
  447. public void Update()
  448. {
  449. // if (EventSystemManager.Instance.isOpenUi && eventConditionConfig.ID != 0)
  450. // return;
  451. if (!_isUpdate) return;
  452. if (messageShowType == ShowDialogueEventData.MessageShowType.Verbatim)
  453. {
  454. if (_currShowIndex >= _currShowMessage.Length)
  455. {
  456. _isUpdate = false;
  457. ShowNextIcon();
  458. if (index >= dialogueMessaga.Length)
  459. {
  460. if (eventConditionConfig.ID != 0 && !_isShowingOptions)
  461. {
  462. ShowOptions();
  463. }
  464. }
  465. return;
  466. }
  467. _currShowTime += Time.deltaTime;
  468. if (_currShowTime > _showTime)
  469. {
  470. _currShowTime -= _showTime;
  471. string v = _currShowMessage[_currShowIndex].ToString();
  472. if (v == "<")
  473. {
  474. int count = 0;
  475. int endIndex = 0;
  476. for (int i = _currShowIndex; i < _currShowMessage.Length - 1; i++)
  477. {
  478. count++;
  479. string ve = _currShowMessage[i].ToString();
  480. _sb.Append(ve);
  481. if (ve == ">")
  482. {
  483. string ve1 = _currShowMessage[i - 1].ToString();
  484. if (ve1 == "r")
  485. {
  486. endIndex = i;
  487. break;
  488. }
  489. }
  490. }
  491. _currShowIndex += count;
  492. }
  493. else
  494. {
  495. _sb.Append(_currShowMessage[_currShowIndex]);
  496. _currShowIndex++;
  497. }
  498. showText.text = _sb.ToString();
  499. }
  500. }
  501. }
  502. public async override CTask Close()
  503. {
  504. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  505. dialogueOptionWidgets.Clear();
  506. _isShowingOptions = false;
  507. _skipTyping = false;
  508. index = 0;
  509. eventConditionConfig = default;
  510. _eventLinkData = null;
  511. CurrentEventList = null;
  512. _currShowMessage = null;
  513. dialogueMessaga = null;
  514. // DialgueType1.gameObject.SetActive(false);
  515. // DialgueType2.gameObject.SetActive(false);
  516. OptionMarsk.SetActive(false);
  517. isOver = false;
  518. await base.Close();
  519. // DialgueType1Anim.Play("DialgueType1idle");
  520. }
  521. }
  522. }