Newer
Older
DungeonShooting / DungeonShooting_Godot / editor / src / Editor.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Godot;
  5.  
  6. namespace DScript.GodotEditor
  7. {
  8. /// <summary>
  9. /// 编辑器类
  10. /// </summary>
  11. public class Editor : Control
  12. {
  13.  
  14. private class ShortcutKeyData
  15. {
  16. public int key;
  17. public bool ctrl;
  18. public bool shift;
  19. public bool alt;
  20. public MethodInfo methodInfo;
  21. public bool isPressed;
  22. }
  23.  
  24. /// <summary>
  25. /// 获取编辑器实例
  26. /// </summary>
  27. public static Editor Instance => _instance;
  28.  
  29. private static Editor _instance;
  30.  
  31. private List<ShortcutKeyData> _shortcutKeyMethods = new List<ShortcutKeyData>();
  32.  
  33. public Editor()
  34. {
  35. _instance = this;
  36. }
  37.  
  38. public override void _Ready()
  39. {
  40. ScannerAssembly();
  41. }
  42.  
  43. public override void _Process(float delta)
  44. {
  45. if (Input.IsKeyPressed((int)KeyList.Control))
  46. {
  47.  
  48. }
  49. }
  50.  
  51. public override void _Input(InputEvent @event)
  52. {
  53. if (!Visible)
  54. {
  55. return;
  56. }
  57.  
  58. if (@event is InputEventKey eventKey)
  59. {
  60. uint key = eventKey.Scancode;
  61. //检测快捷键
  62. if (key != (int)KeyList.Control && key != (int)KeyList.Shift && key != (int)KeyList.Alt)
  63. {
  64. for (int i = 0; i < _shortcutKeyMethods.Count; i++)
  65. {
  66. var item = _shortcutKeyMethods[i];
  67. var flag = key == item.key && eventKey.Pressed && item.ctrl == eventKey.Control &&
  68. item.shift == eventKey.Shift && item.alt == eventKey.Alt;
  69. if (flag)
  70. {
  71. //触发快捷键调用
  72. if (!item.isPressed)
  73. {
  74. item.isPressed = true;
  75. item.methodInfo.Invoke(null, new object[0]);
  76. }
  77. }
  78. else
  79. {
  80. item.isPressed = false;
  81. }
  82. }
  83. }
  84. }
  85. }
  86.  
  87. /// <summary>
  88. /// 扫描程序集, 并完成注册标记
  89. /// </summary>
  90. private void ScannerAssembly()
  91. {
  92. var types = GetType().Assembly.GetTypes();
  93. foreach (var type in types)
  94. {
  95. if (type.Namespace == null || !type.Namespace.StartsWith("DScript.GodotEditor")) continue;
  96. MethodInfo[] methods =
  97. type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  98. foreach (var method in methods)
  99. {
  100. Attribute mAttribute;
  101. //快捷键注册
  102. if ((mAttribute = Attribute.GetCustomAttribute(method, typeof(EditorShortcutKey), false)) !=
  103. null)
  104. {
  105. var att = (EditorShortcutKey)mAttribute;
  106. var data = new ShortcutKeyData();
  107. data.key = (int)att.Key;
  108. data.methodInfo = method;
  109. data.shift = att.Shift;
  110. data.ctrl = att.Ctrl;
  111. data.alt = att.Alt;
  112. _shortcutKeyMethods.Add(data);
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }