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