Newer
Older
DungeonShooting / DungeonShooting_Godot / addons / dungeonShooting_plugin / Plugin.cs
@lijincheng lijincheng on 1 Jun 2023 7 KB 修改中
  1. #if TOOLS
  2. using System;
  3. using Generator;
  4. using Godot;
  5. using UI.EditorTools;
  6.  
  7. namespace Plugin
  8. {
  9. [Tool]
  10. public partial class Plugin : EditorPlugin
  11. {
  12. /// <summary>
  13. /// 自定义节点类型数据
  14. /// </summary>
  15. private class CustomTypeInfo
  16. {
  17. public CustomTypeInfo(string name, string parentName, string scriptPath, string iconPath)
  18. {
  19. Name = name;
  20. ParentName = parentName;
  21. ScriptPath = scriptPath;
  22. IconPath = iconPath;
  23. }
  24.  
  25. public string Name;
  26. public string ParentName;
  27. public string ScriptPath;
  28. public string IconPath;
  29. }
  30. /// <summary>
  31. /// 当前插件实例
  32. /// </summary>
  33. public static Plugin Instance { get; private set; }
  34.  
  35. //工具面板
  36. private EditorToolsPanel _editorTools;
  37.  
  38. //ui监听器
  39. private NodeMonitor _uiMonitor;
  40.  
  41. //自定义节点
  42. private CustomTypeInfo[] _customTypeInfos = new CustomTypeInfo[]
  43. {
  44. new CustomTypeInfo(
  45. "ActivityMark",
  46. "Node2D",
  47. "res://src/framework/map/mark/ActivityMark.cs",
  48. "res://addons/dungeonShooting_plugin/Mark.svg"
  49. ),
  50. new CustomTypeInfo(
  51. "EnemyMark",
  52. "Node2D",
  53. "res://src/framework/map/mark/EnemyMark.cs",
  54. "res://addons/dungeonShooting_plugin/Mark.svg"
  55. ),
  56. };
  57. public override void _Process(double delta)
  58. {
  59. Instance = this;
  60. if (_uiMonitor != null)
  61. {
  62. _uiMonitor.Process((float) delta);
  63. }
  64. else
  65. {
  66. _uiMonitor = new NodeMonitor();
  67. _uiMonitor.SceneNodeChangeEvent += GenerateUiCode;
  68. OnSceneChanged(GetEditorInterface().GetEditedSceneRoot());
  69. }
  70. }
  71.  
  72. public override void _EnterTree()
  73. {
  74. Instance = this;
  75.  
  76. if (_customTypeInfos != null)
  77. {
  78. //注册自定义节点
  79. foreach (var customTypeInfo in _customTypeInfos)
  80. {
  81. try
  82. {
  83. var script = GD.Load<Script>(customTypeInfo.ScriptPath);
  84. var icon = GD.Load<Texture2D>(customTypeInfo.IconPath);
  85. AddCustomType(customTypeInfo.Name, customTypeInfo.ParentName, script, icon);
  86. }
  87. catch (Exception e)
  88. {
  89. GD.PrintErr(e.ToString());
  90. }
  91. }
  92. }
  93.  
  94. _editorTools = GD.Load<PackedScene>(ResourcePath.prefab_ui_EditorTools_tscn).Instantiate<EditorToolsPanel>();
  95. var editorMainScreen = GetEditorInterface().GetEditorMainScreen();
  96. editorMainScreen.AddChild(_editorTools);
  97.  
  98. try
  99. {
  100. _editorTools.OnCreateUi();
  101. }
  102. catch (Exception e)
  103. {
  104. GD.PrintErr(e.ToString());
  105. }
  106.  
  107. try
  108. {
  109. _editorTools.OnShowUi();
  110. }
  111. catch (Exception e)
  112. {
  113. GD.PrintErr(e.ToString());
  114. }
  115. _MakeVisible(false);
  116. //场景切换事件
  117. SceneChanged += OnSceneChanged;
  118.  
  119. _uiMonitor = new NodeMonitor();
  120. _uiMonitor.SceneNodeChangeEvent += GenerateUiCode;
  121. OnSceneChanged(GetEditorInterface().GetEditedSceneRoot());
  122. }
  123.  
  124. public override void _ExitTree()
  125. {
  126. //移除自定义节点
  127. if (_customTypeInfos != null)
  128. {
  129. foreach (var customTypeInfo in _customTypeInfos)
  130. {
  131. try
  132. {
  133. RemoveCustomType(customTypeInfo.Name);
  134. }
  135. catch (Exception e)
  136. {
  137. GD.PrintErr(e.ToString());
  138. }
  139. }
  140. }
  141.  
  142. if (_editorTools != null)
  143. {
  144. try
  145. {
  146. _editorTools.OnHideUi();
  147. }
  148. catch (Exception e)
  149. {
  150. GD.PrintErr(e.ToString());
  151. }
  152.  
  153. try
  154. {
  155. _editorTools.OnDisposeUi();
  156. }
  157. catch (Exception e)
  158. {
  159. GD.PrintErr(e.ToString());
  160. }
  161.  
  162. _editorTools.Free();
  163. _editorTools = null;
  164. }
  165.  
  166. SceneChanged -= OnSceneChanged;
  167.  
  168. if (_uiMonitor != null)
  169. {
  170. _uiMonitor.SceneNodeChangeEvent -= GenerateUiCode;
  171. _uiMonitor = null;
  172. }
  173. }
  174.  
  175. public override bool _HasMainScreen()
  176. {
  177. return true;
  178. }
  179.  
  180. public override Texture2D _GetPluginIcon()
  181. {
  182. return GD.Load<Texture2D>("res://addons/dungeonShooting_plugin/Tool.svg");
  183. }
  184.  
  185. public override string _GetPluginName()
  186. {
  187. return "Tools";
  188. }
  189.  
  190. public override void _MakeVisible(bool visible)
  191. {
  192. if (_editorTools != null)
  193. {
  194. _editorTools.Visible = visible;
  195. }
  196. }
  197. /// <summary>
  198. /// 检查节点是否为UI节点
  199. /// </summary>
  200. public bool CheckIsUi(Node node)
  201. {
  202. var resourcePath = node.GetScript().As<CSharpScript>()?.ResourcePath;
  203. if (resourcePath == null)
  204. {
  205. return false;
  206. }
  207.  
  208. if (resourcePath.StartsWith("res://src/game/ui/") && resourcePath.EndsWith("Panel.cs"))
  209. {
  210. var index = resourcePath.LastIndexOf("/", StringComparison.Ordinal);
  211. var uiName = resourcePath.Substring(index + 1, resourcePath.Length - index - 8 - 1);
  212. var codePath = "res://src/game/ui/" + uiName.FirstToLower() + "/" + uiName + "Panel.cs";
  213. if (ResourceLoader.Exists(codePath))
  214. {
  215. return true;
  216. }
  217. }
  218.  
  219. return false;
  220. }
  221.  
  222. /// <summary>
  223. /// 执行生成ui代码操作
  224. /// </summary>
  225. public void GenerateUiCode(Node node)
  226. {
  227. UiGenerator.GenerateUiCodeFromEditor(node);
  228. }
  229.  
  230. /// <summary>
  231. /// 切换场景
  232. /// </summary>
  233. private void OnSceneChanged(Node node)
  234. {
  235. if (_uiMonitor != null)
  236. {
  237. _uiMonitor.ChangeCurrentNode(null);
  238. if (node != null)
  239. {
  240. if (CheckIsUi(node))
  241. {
  242. _uiMonitor.ChangeCurrentNode(node);
  243. }
  244. }
  245. }
  246. }
  247.  
  248. }
  249. }
  250. #endif