DialogueBubblePanel.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 EventConditionConfig 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.Top);
  32. dialoguePanel.ShowPanel(id, finish);
  33. }
  34. private void Init()
  35. {
  36. _skipTyping = false;
  37. _isShowingOptions = false;
  38. }
  39. protected override void AddEvent()
  40. {
  41. }
  42. protected override void DelEvent()
  43. {
  44. }
  45. public override void AddButtonEvent()
  46. {
  47. button_bg.onClick.AddListener(NextShow);
  48. }
  49. private void SkipTyping()
  50. {
  51. _skipTyping = true;
  52. _timerEntity?.Dispose();
  53. _timerEntity = null;
  54. if (_isUpdate)
  55. {
  56. _sb.Clear();
  57. _sb.Append(_currShowMessage);
  58. message.text = _sb.ToString();
  59. _currShowIndex = _currShowMessage.Length;
  60. _isUpdate = false;
  61. }
  62. }
  63. private void NextShow()
  64. {
  65. if (_isShowingOptions)
  66. return;
  67. if (_isUpdate)
  68. {
  69. SkipTyping();
  70. }
  71. else
  72. {
  73. StartShowMassge();
  74. }
  75. }
  76. public void ShowPanel(int id,
  77. Action<int?> finish)
  78. {
  79. eventConditionConfig = ConfigComponent.Instance.Get<EventConditionConfig>(id);
  80. this.dialogueMessaga = eventConditionConfig.LanID;
  81. this.finish = finish;
  82. index = 0;
  83. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  84. StartShowMassge();
  85. }
  86. private void StartShowMassge()
  87. {
  88. _timerEntity?.Dispose();
  89. _timerEntity = null;
  90. if (index >= dialogueMessaga.Length)
  91. {
  92. UIManager.Instance.HideUIUIPanel(this);
  93. finish?.Invoke(null);
  94. return;
  95. }
  96. _isShowNextButton = false;
  97. string m = LanguageManager.Instance.Text(dialogueMessaga[index]);
  98. index++;
  99. switch (messageShowType)
  100. {
  101. case ShowDialogueEventData.MessageShowType.Default:
  102. message.text = m;
  103. break;
  104. case ShowDialogueEventData.MessageShowType.Verbatim:
  105. _sb.Clear();
  106. _currShowMessage = m.ToCharArray();
  107. _sb.Append(_currShowMessage[0]);
  108. message.text = _sb.ToString();
  109. _isUpdate = true;
  110. _currShowIndex = 1;
  111. _skipTyping = false;
  112. break;
  113. }
  114. }
  115. public override void Hide()
  116. {
  117. base.Hide();
  118. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Update);
  119. }
  120. private TimerEntity _timerEntity;
  121. public void Update()
  122. {
  123. // Vector3 p = CombatDrive.Instance.CombatController.CombatHeroController.playerHeroEntity.GameObject.transform.Find("hitpos").position;
  124. // p.x -= 0.2f;
  125. // p.y += 0.7f;
  126. // p = CombatDrive.Instance.CombatController.CombatCameraControllder.Camera.WorldToScreenPoint(p);
  127. // p = UIManager.Instance.UICamera.ScreenToWorldPoint(p);
  128. //
  129. //
  130. // p = transform.worldToLocalMatrix * p;
  131. //
  132. // dotPoint.anchoredPosition = p;
  133. Vector3 worldPos = CombatDrive.Instance.CombatController.CombatHeroController.playerHeroEntity.combatHeroGameObject.hpTransform.position;
  134. Vector3 p = UIManager.Instance.CurrCustomCameraStack.camera.WorldToScreenPoint(worldPos);
  135. Vector3 p2 = UIManager.Instance.UICamera.ScreenToWorldPoint(p);
  136. dotPoint.transform.position = p2;
  137. if (!_isUpdate) return;
  138. if (messageShowType == ShowDialogueEventData.MessageShowType.Verbatim)
  139. {
  140. if (_currShowIndex >= _currShowMessage.Length)
  141. {
  142. _isUpdate = false;
  143. _timerEntity?.Dispose();
  144. _timerEntity = null;
  145. _timerEntity = TimerComponent.Instance.AddTimer(800, () =>
  146. {
  147. _timerEntity?.Dispose();
  148. _timerEntity = null;
  149. StartShowMassge();
  150. });
  151. return;
  152. }
  153. _currShowTime += Time.deltaTime;
  154. if (_currShowTime > _showTime)
  155. {
  156. _currShowTime -= _showTime;
  157. _sb.Append(_currShowMessage[_currShowIndex]);
  158. _currShowIndex++;
  159. message.text = _sb.ToString();
  160. }
  161. }
  162. }
  163. public override void Close()
  164. {
  165. UIManager.Instance.DormancyAllGComponent<DialogueOptionWidget>();
  166. _isShowingOptions = false;
  167. _skipTyping = false;
  168. _timerEntity?.Dispose();
  169. _timerEntity = null;
  170. base.Close();
  171. }
  172. }
  173. }