DialogueBubblePanel.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Text;
  3. using Common.Utility.CombatEvent;
  4. using Core.Language;
  5. using Excel2Json;
  6. using Fort23.Core;
  7. using Fort23.UTool;
  8. using GameLogic.Combat;
  9. using UnityEngine;
  10. namespace Fort23.Mono
  11. {
  12. [UIBinding(prefab = "DialogueBubblePanel")]
  13. public partial class DialogueBubblePanel : UIPanel
  14. {
  15. private int[] dialogueMessaga;
  16. private ShowDialogueEventData.MessageShowType messageShowType = ShowDialogueEventData.MessageShowType.Verbatim;
  17. private Action<int?> finish;
  18. private int index;
  19. private char[] _currShowMessage;
  20. private float _showTime = 0.05f;
  21. private float _currShowTime = 0;
  22. protected StringBuilder _sb = new StringBuilder();
  23. private int _currShowIndex;
  24. private bool _isUpdate;
  25. private bool _isShowNextButton;
  26. private EventLinkConfig eventConditionConfig;
  27. private bool _skipTyping;
  28. private bool _isShowingOptions;
  29. public static async CTask OpenDialoguePanel(int id, Action<int?> finish)
  30. {
  31. DialogueBubblePanel dialoguePanel = await UIManager.Instance.LoadAndOpenPanel<DialogueBubblePanel>(null, UILayer.Bottom, isFocus: false);
  32. dialoguePanel.ShowPanel(id, finish);
  33. }
  34. private void Init()
  35. {
  36. isAddStack = false;
  37. _skipTyping = false;
  38. _isShowingOptions = false;
  39. }
  40. protected override void AddEvent()
  41. {
  42. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  43. }
  44. protected override void DelEvent()
  45. {
  46. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Update);
  47. }
  48. public override void AddButtonEvent()
  49. {
  50. button_bg.onClick.AddListener(NextShow);
  51. }
  52. private void SkipTyping()
  53. {
  54. _skipTyping = true;
  55. _timerEntity?.Dispose();
  56. _timerEntity = null;
  57. if (_isUpdate)
  58. {
  59. _sb.Clear();
  60. _sb.Append(_currShowMessage);
  61. message.text = _sb.ToString();
  62. _currShowIndex = _currShowMessage.Length;
  63. _isUpdate = false;
  64. }
  65. }
  66. private void NextShow()
  67. {
  68. if (_isShowingOptions)
  69. return;
  70. if (_isUpdate)
  71. {
  72. SkipTyping();
  73. }
  74. else
  75. {
  76. StartShowMassge();
  77. }
  78. }
  79. public void ShowPanel(int id,
  80. Action<int?> finish)
  81. {
  82. eventConditionConfig = ConfigComponent.Instance.Get<EventLinkConfig>(id);
  83. this.dialogueMessaga = eventConditionConfig.LanID;
  84. this.finish = finish;
  85. index = 0;
  86. StartShowMassge();
  87. }
  88. private void StartShowMassge()
  89. {
  90. _timerEntity?.Dispose();
  91. _timerEntity = null;
  92. if (index >= dialogueMessaga.Length)
  93. {
  94. UIManager.Instance.HideUIUIPanel(this);
  95. finish?.Invoke(null);
  96. return;
  97. }
  98. _isShowNextButton = false;
  99. string m = LanguageManager.Instance.Text(dialogueMessaga[index]);
  100. index++;
  101. switch (messageShowType)
  102. {
  103. case ShowDialogueEventData.MessageShowType.Default:
  104. message.text = m;
  105. break;
  106. case ShowDialogueEventData.MessageShowType.Verbatim:
  107. _sb.Clear();
  108. _currShowMessage = m.ToCharArray();
  109. if (_currShowMessage.Length > 0)
  110. _sb.Append(_currShowMessage[0]);
  111. message.text = _sb.ToString();
  112. _isUpdate = true;
  113. _currShowIndex = 1;
  114. _skipTyping = false;
  115. break;
  116. }
  117. }
  118. public override void Hide()
  119. {
  120. base.Hide();
  121. }
  122. private TimerEntity _timerEntity;
  123. public void Update()
  124. {
  125. if (UIManager.Instance.currOpenPanel == null || UIManager.Instance.currOpenPanel.GetType() != typeof(MainPanel) || UIManager.Instance.popUIPanels.Count > 0)
  126. {
  127. return;
  128. }
  129. Vector3 worldPos = CombatDrive.Instance.CombatController.playerHeroEntity.combatHeroGameObject.hpTransform.position;
  130. Vector3 p = UIManager.Instance.CurrCustomCameraStack.camera.WorldToScreenPoint(worldPos);
  131. Vector3 p2 = UIManager.Instance.UICamera.ScreenToWorldPoint(p);
  132. dotPoint.transform.position = p2;
  133. if (!_isUpdate) return;
  134. if (messageShowType == ShowDialogueEventData.MessageShowType.Verbatim)
  135. {
  136. if (_currShowIndex >= _currShowMessage.Length)
  137. {
  138. _isUpdate = false;
  139. _timerEntity?.Dispose();
  140. _timerEntity = null;
  141. _timerEntity = TimerComponent.Instance.AddTimer(800, () =>
  142. {
  143. _timerEntity?.Dispose();
  144. _timerEntity = null;
  145. StartShowMassge();
  146. });
  147. return;
  148. }
  149. _currShowTime += Time.deltaTime;
  150. if (_currShowTime > _showTime)
  151. {
  152. _currShowTime -= _showTime;
  153. _sb.Append(_currShowMessage[_currShowIndex]);
  154. _currShowIndex++;
  155. message.text = _sb.ToString();
  156. }
  157. }
  158. }
  159. public override void Close()
  160. {
  161. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  162. _isShowingOptions = false;
  163. _skipTyping = false;
  164. _timerEntity?.Dispose();
  165. _timerEntity = null;
  166. base.Close();
  167. }
  168. }
  169. }