Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / bottomTips / BottomTipsPanel.cs
@lijincheng lijincheng on 6 Jul 2023 3 KB 补上注释
  1. using System.Collections;
  2. using Godot;
  3.  
  4. namespace UI.BottomTips;
  5.  
  6. /// <summary>
  7. /// 底部提示面板
  8. /// </summary>
  9. public partial class BottomTipsPanel : BottomTips
  10. {
  11. private long _id = -1;
  12. private float _offsetY;
  13. //动画播放时间
  14. private float _animationTime = 0.5f;
  15. //动画移动的像素
  16. private int _movePixel = 153;
  17.  
  18. public override void OnCreateUi()
  19. {
  20. _offsetY = L_Panel.Instance.Position.Y - (Position.Y + Size.Y);
  21. }
  22.  
  23. public override void OnShowUi()
  24. {
  25.  
  26. }
  27.  
  28. public override void OnHideUi()
  29. {
  30. }
  31.  
  32. /// <summary>
  33. /// 执行入场流程
  34. /// </summary>
  35. public void PlayInStep(Texture2D icon, string message)
  36. {
  37. if (_id >= 0)
  38. {
  39. StopCoroutine(_id);
  40. HideUi();
  41. }
  42. _id = StartCoroutine(RunAnimation(icon, message));
  43. }
  44.  
  45. /// <summary>
  46. /// 设置图标
  47. /// </summary>
  48. public void SetIcon(Texture2D icon)
  49. {
  50. L_Panel.L_MarginContainer.L_CenterContainer.L_HBoxContainer.L_AspectRatioContainer.L_TextureRect.Instance
  51. .Texture = icon;
  52. }
  53. /// <summary>
  54. /// 设置文本内容
  55. /// </summary>
  56. public void SetMessage(string message)
  57. {
  58. L_Panel.L_MarginContainer.L_CenterContainer.L_HBoxContainer.L_Label.Instance.Text = message;
  59. }
  60.  
  61. private IEnumerator RunAnimation(Texture2D icon, string message)
  62. {
  63. //还原位置
  64. var pos = L_Panel.Instance.Position;
  65. pos.Y = Position.Y + Size.Y + _offsetY;
  66. L_Panel.Instance.Position = pos;
  67. SetIcon(icon);
  68. SetMessage(message);
  69. yield return 0;
  70. ShowUi();
  71. L_Panel.Instance.ResetSize();
  72. yield return 0;
  73. //重新计算中心点
  74. pos.X = Size.X / 2 - L_Panel.Instance.Size.X / 2;
  75. L_Panel.Instance.Position = pos;
  76. yield return 0;
  77.  
  78. //向上移动
  79. var frame = 60 * _animationTime;
  80. var stepPixel = _movePixel / frame;
  81. for (var i = 0; i < frame; i++)
  82. {
  83. pos.X = L_Panel.Instance.Position.X;
  84. pos.Y -= stepPixel;
  85. L_Panel.Instance.Position = pos;
  86. yield return 0;
  87. }
  88.  
  89. yield return new WaitForSeconds(3.5f);
  90. //向下移动
  91. for (var i = 0; i < frame; i++)
  92. {
  93. pos.X = L_Panel.Instance.Position.X;
  94. pos.Y += stepPixel;
  95. L_Panel.Instance.Position = pos;
  96. yield return 0;
  97. }
  98.  
  99. HideUi();
  100. _id = -1;
  101. }
  102. private static BottomTipsPanel _instance;
  103. public static void Init()
  104. {
  105. _instance = UiManager.Open_BottomTips();
  106. _instance.HideUi();
  107. }
  108.  
  109. /// <summary>
  110. /// 打开Tips, 并设置图标和内容
  111. /// </summary>
  112. /// <param name="icon">显示图标</param>
  113. /// <param name="message">显示消息</param>
  114. public static void ShowTips(string icon, string message)
  115. {
  116. ShowTips(ResourceManager.Load<Texture2D>(icon), message);
  117. }
  118.  
  119. /// <summary>
  120. /// 打开Tips, 并设置图标和内容
  121. /// </summary>
  122. /// <param name="icon">显示图标</param>
  123. /// <param name="message">显示消息</param>
  124. public static void ShowTips(Texture2D icon, string message)
  125. {
  126. _instance.PlayInStep(icon, message);
  127. }
  128. }