DialoguePanel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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 && PlayerManager.Instance.PlayerName != String.Empty)
  354. {
  355. _sb.Append(PlayerManager.Instance.PlayerName + ":");
  356. }
  357. _sb.Append(_currShowMessage[0]);
  358. showText.text = _sb.ToString();
  359. _isUpdate = true;
  360. _currShowIndex = 1;
  361. _skipTyping = false;
  362. break;
  363. }
  364. LayoutRebuilder.ForceRebuildLayoutImmediate(Dialgue);
  365. }
  366. private async void ShowOptions()
  367. {
  368. await MarskPanel.OpenPanel();
  369. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  370. _isShowingOptions = true;
  371. if (eventConditionConfig.optionType == 1 && eventConditionConfig.optionPara1 != null)
  372. {
  373. OptionMarsk.SetActive(true);
  374. for (var i = 0; i < eventConditionConfig.optionPara1.Length; i++)
  375. {
  376. int index = eventConditionConfig.optionPara1.Length - 1 - i;
  377. DialogueOptionWidget dialogueOptionWidget =
  378. await UIManager.Instance.CreateGComponent<DialogueOptionWidget>(null, OptionRoot);
  379. dialogueOptionWidget.CustomInit(i, CurrentEventList, eventConditionConfig.optionPara1[i],
  380. eventConditionConfig.ID, SelectOption);
  381. dialogueOptionWidget.transform.position = OptionPos[index].position;
  382. await TimerComponent.Instance.WaitAsync(200);
  383. }
  384. LayoutRebuilder.ForceRebuildLayoutImmediate(Dialgue);
  385. }
  386. await MarskPanel.ClosePanel();
  387. }
  388. private async void SelectOption(DialogueOptionWidget obj)
  389. {
  390. if (obj.eventLinkData != null &&
  391. !EventSystemManager.Instance.IsCeekEventGroupComplete(obj.eventLinkData.eventConditions))
  392. {
  393. bool isOk = await EventTipsPanel.OpenPanel(obj.eventLinkData);
  394. if (isOk)
  395. {
  396. UIManager.Instance.HideUIUIPanel(this);
  397. finish?.Invoke(-1);
  398. }
  399. // TipMessagePanel.OpenTipMessagePanel(EventHelper.GetTaskMessage(selectedOptionID));
  400. return;
  401. }
  402. OptionMarsk.SetActive(false);
  403. int selectedOptionID = obj.eventConditionConfig.ID;
  404. UIManager.Instance.HideUIUIPanel(this);
  405. finish?.Invoke(selectedOptionID);
  406. }
  407. private void ShowNextIcon()
  408. {
  409. shownextIcon.SetActive(true);
  410. _isShowNextButton = true;
  411. }
  412. public override void Hide()
  413. {
  414. base.Hide();
  415. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Update);
  416. }
  417. public void Update()
  418. {
  419. // if (EventSystemManager.Instance.isOpenUi && eventConditionConfig.ID != 0)
  420. // return;
  421. if (!_isUpdate) return;
  422. if (messageShowType == ShowDialogueEventData.MessageShowType.Verbatim)
  423. {
  424. if (_currShowIndex >= _currShowMessage.Length)
  425. {
  426. _isUpdate = false;
  427. ShowNextIcon();
  428. if (index >= dialogueMessaga.Length)
  429. {
  430. if (eventConditionConfig.ID != 0 && !_isShowingOptions)
  431. {
  432. ShowOptions();
  433. }
  434. }
  435. return;
  436. }
  437. _currShowTime += Time.deltaTime;
  438. if (_currShowTime > _showTime)
  439. {
  440. _currShowTime -= _showTime;
  441. string v = _currShowMessage[_currShowIndex].ToString();
  442. if (v == "<")
  443. {
  444. int count = 0;
  445. int endIndex = 0;
  446. for (int i = _currShowIndex; i < _currShowMessage.Length - 1; i++)
  447. {
  448. count++;
  449. string ve = _currShowMessage[i].ToString();
  450. _sb.Append(ve);
  451. if (ve == ">")
  452. {
  453. string ve1 = _currShowMessage[i - 1].ToString();
  454. if (ve1 == "r")
  455. {
  456. endIndex = i;
  457. break;
  458. }
  459. }
  460. }
  461. _currShowIndex += count;
  462. }
  463. else
  464. {
  465. _sb.Append(_currShowMessage[_currShowIndex]);
  466. _currShowIndex++;
  467. }
  468. showText.text = _sb.ToString();
  469. }
  470. }
  471. }
  472. public async override CTask Close()
  473. {
  474. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  475. _isShowingOptions = false;
  476. _skipTyping = false;
  477. index = 0;
  478. eventConditionConfig = default;
  479. _eventLinkData = null;
  480. CurrentEventList = null;
  481. _currShowMessage = null;
  482. dialogueMessaga = null;
  483. // DialgueType1.gameObject.SetActive(false);
  484. // DialgueType2.gameObject.SetActive(false);
  485. OptionMarsk.SetActive(false);
  486. isOver = false;
  487. await base.Close();
  488. // DialgueType1Anim.Play("DialgueType1idle");
  489. }
  490. }
  491. }