Newer
Older
DungeonShooting / DungeonShooting_Godot / editor / src / CodeHintPanel.cs
@小李xl 小李xl on 24 Sep 2022 2 KB 代码提示功能开发
  1. using System.Collections.Generic;
  2. using Godot;
  3.  
  4. namespace DScript.GodotEditor
  5. {
  6. /// <summary>
  7. /// 代码补全提示
  8. /// </summary>
  9. public class CodeHintPanel : Popup
  10. {
  11. //补全选项
  12. [Export] private PackedScene CodeHintItem;
  13.  
  14. /// <summary>
  15. /// 获取实例
  16. /// </summary>
  17. public static CodeHintPanel Instance { get; private set; }
  18.  
  19. /// <summary>
  20. /// 当前选提示组件活动的项
  21. /// </summary>
  22. public int ActiveIndex
  23. {
  24. get => _activeIndex;
  25. set => SetActiveIndex(value);
  26. }
  27. private int _activeIndex = -1;
  28.  
  29. //提示项父容器
  30. private VBoxContainer _itemContainer;
  31.  
  32. //当前已启用项的列表
  33. private List<CodeHintItem> _activeItemList = new List<CodeHintItem>();
  34. public CodeHintPanel()
  35. {
  36. Instance = this;
  37. }
  38.  
  39. public override void _Ready()
  40. {
  41. _itemContainer = GetNode<VBoxContainer>("ScrollContainer/VBoxContainer");
  42.  
  43. for (int i = 0; i < 10; i++)
  44. {
  45. var item = CreateItem();
  46. item.CodeText = i.ToString();
  47. }
  48. }
  49.  
  50. public override void _Process(float delta)
  51. {
  52. if (Input.IsActionJustPressed("ui_down") && _activeItemList.Count > 1) //按下下键
  53. {
  54. var index = ActiveIndex;
  55. index += 1;
  56. if (index >= _activeItemList.Count)
  57. {
  58. index = 0;
  59. }
  60.  
  61. ActiveIndex = index;
  62. }
  63. else if (Input.IsActionJustPressed("ui_up") && _activeItemList.Count > 1) //按下上键
  64. {
  65. var index = ActiveIndex;
  66. index -= 1;
  67. if (index < 0)
  68. {
  69. index = _activeItemList.Count - 1;
  70. }
  71.  
  72. ActiveIndex = index;
  73. }
  74. }
  75.  
  76. /// <summary>
  77. /// 显示提示面板
  78. /// </summary>
  79. public void ShowPanel(Vector2 pos)
  80. {
  81. RectPosition = pos;
  82. Popup_();
  83. }
  84.  
  85. /// <summary>
  86. /// 设置活动项
  87. /// </summary>
  88. private void SetActiveIndex(int index)
  89. {
  90. //禁用之前的
  91. if (_activeIndex >= 0 && _activeIndex < _activeItemList.Count)
  92. {
  93. _activeItemList[_activeIndex].SetActive(false);
  94. }
  95.  
  96. _activeIndex = index;
  97.  
  98. //启用现在的
  99. if (index >= 0 && index < _activeItemList.Count)
  100. {
  101. _activeItemList[index].SetActive(true);
  102. }
  103. }
  104.  
  105. /// <summary>
  106. /// 创建提示项
  107. /// </summary>
  108. private CodeHintItem CreateItem()
  109. {
  110. var item = CodeHintItem.Instance<CodeHintItem>();
  111. _itemContainer.AddChild(item);
  112. item.Index = _activeItemList.Count;
  113. _activeItemList.Add(item);
  114. return item;
  115. }
  116. }
  117. }