DialogueBubblePanel.cs 5.2 KB

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