Newer
Older
DungeonShooting / DungeonShooting_Godot / addons / dungeonShooting_plugin / Plugin.cs
  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. "ActivityObjectTemplate",
  46. "Node",
  47. "res://src/framework/activity/ActivityObjectTemplate.cs",
  48. "res://addons/dungeonShooting_plugin/ActivityObject.svg"
  49. ),
  50. new CustomTypeInfo(
  51. "ActivityMark",
  52. "Node2D",
  53. "res://src/framework/map/mark/ActivityMark.cs",
  54. "res://addons/dungeonShooting_plugin/Mark.svg"
  55. ),
  56. new CustomTypeInfo(
  57. "EnemyMark",
  58. "Node2D",
  59. "res://src/framework/map/mark/EnemyMark.cs",
  60. "res://addons/dungeonShooting_plugin/Mark.svg"
  61. ),
  62. new CustomTypeInfo(
  63. "WeaponMark",
  64. "Node2D",
  65. "res://src/framework/map/mark/WeaponMark.cs",
  66. "res://addons/dungeonShooting_plugin/Mark.svg"
  67. ),
  68. };
  69. public override void _Process(double delta)
  70. {
  71. Instance = this;
  72. if (_uiMonitor != null)
  73. {
  74. _uiMonitor.Process((float) delta);
  75. }
  76. else
  77. {
  78. _uiMonitor = new NodeMonitor();
  79. _uiMonitor.SceneNodeChangeEvent += GenerateUiCode;
  80. OnSceneChanged(GetEditorInterface().GetEditedSceneRoot());
  81. }
  82. }
  83.  
  84. public override void _EnterTree()
  85. {
  86. Instance = this;
  87.  
  88. if (_customTypeInfos != null)
  89. {
  90. //注册自定义节点
  91. foreach (var customTypeInfo in _customTypeInfos)
  92. {
  93. try
  94. {
  95. var script = GD.Load<Script>(customTypeInfo.ScriptPath);
  96. var icon = GD.Load<Texture2D>(customTypeInfo.IconPath);
  97. AddCustomType(customTypeInfo.Name, customTypeInfo.ParentName, script, icon);
  98. }
  99. catch (Exception e)
  100. {
  101. GD.PrintErr(e.ToString());
  102. }
  103. }
  104. }
  105.  
  106. _editorTools = GD.Load<PackedScene>(ResourcePath.prefab_ui_EditorTools_tscn).Instantiate<EditorToolsPanel>();
  107. var editorMainScreen = GetEditorInterface().GetEditorMainScreen();
  108. editorMainScreen.AddChild(_editorTools);
  109.  
  110. try
  111. {
  112. _editorTools.OnCreateUi();
  113. }
  114. catch (Exception e)
  115. {
  116. GD.PrintErr(e.ToString());
  117. }
  118.  
  119. try
  120. {
  121. _editorTools.OnShowUi();
  122. }
  123. catch (Exception e)
  124. {
  125. GD.PrintErr(e.ToString());
  126. }
  127. _MakeVisible(false);
  128. //场景切换事件
  129. SceneChanged += OnSceneChanged;
  130.  
  131. _uiMonitor = new NodeMonitor();
  132. _uiMonitor.SceneNodeChangeEvent += GenerateUiCode;
  133. OnSceneChanged(GetEditorInterface().GetEditedSceneRoot());
  134. }
  135.  
  136. public override void _ExitTree()
  137. {
  138. //移除自定义节点
  139. if (_customTypeInfos != null)
  140. {
  141. foreach (var customTypeInfo in _customTypeInfos)
  142. {
  143. try
  144. {
  145. RemoveCustomType(customTypeInfo.Name);
  146. }
  147. catch (Exception e)
  148. {
  149. GD.PrintErr(e.ToString());
  150. }
  151. }
  152. }
  153.  
  154. if (_editorTools != null)
  155. {
  156. try
  157. {
  158. _editorTools.OnHideUi();
  159. }
  160. catch (Exception e)
  161. {
  162. GD.PrintErr(e.ToString());
  163. }
  164.  
  165. try
  166. {
  167. _editorTools.OnDisposeUi();
  168. }
  169. catch (Exception e)
  170. {
  171. GD.PrintErr(e.ToString());
  172. }
  173.  
  174. _editorTools.Free();
  175. _editorTools = null;
  176. }
  177.  
  178. SceneChanged -= OnSceneChanged;
  179.  
  180. if (_uiMonitor != null)
  181. {
  182. _uiMonitor.SceneNodeChangeEvent -= GenerateUiCode;
  183. _uiMonitor = null;
  184. }
  185. }
  186.  
  187. public override bool _HasMainScreen()
  188. {
  189. return true;
  190. }
  191.  
  192. public override Texture2D _GetPluginIcon()
  193. {
  194. return GD.Load<Texture2D>("res://addons/dungeonShooting_plugin/Tool.svg");
  195. }
  196.  
  197. public override string _GetPluginName()
  198. {
  199. return "Tools";
  200. }
  201.  
  202. public override void _MakeVisible(bool visible)
  203. {
  204. if (_editorTools != null)
  205. {
  206. _editorTools.Visible = visible;
  207. }
  208. }
  209. /// <summary>
  210. /// 检查节点是否为UI节点
  211. /// </summary>
  212. public bool CheckIsUi(Node node)
  213. {
  214. var resourcePath = node.GetScript().As<CSharpScript>()?.ResourcePath;
  215. if (resourcePath == null)
  216. {
  217. return false;
  218. }
  219.  
  220. if (resourcePath.StartsWith("res://src/game/ui/") && resourcePath.EndsWith("Panel.cs"))
  221. {
  222. var index = resourcePath.LastIndexOf("/", StringComparison.Ordinal);
  223. var uiName = resourcePath.Substring(index + 1, resourcePath.Length - index - 8 - 1);
  224. var codePath = "res://src/game/ui/" + uiName.FirstToLower() + "/" + uiName + "Panel.cs";
  225. if (ResourceLoader.Exists(codePath))
  226. {
  227. return true;
  228. }
  229. }
  230.  
  231. return false;
  232. }
  233.  
  234. /// <summary>
  235. /// 执行生成ui代码操作
  236. /// </summary>
  237. public void GenerateUiCode(Node node)
  238. {
  239. UiGenerator.GenerateUiCodeFromEditor(node);
  240. }
  241.  
  242. /// <summary>
  243. /// 切换场景
  244. /// </summary>
  245. private void OnSceneChanged(Node node)
  246. {
  247. if (_uiMonitor != null)
  248. {
  249. _uiMonitor.ChangeCurrentNode(null);
  250. if (node != null)
  251. {
  252. if (CheckIsUi(node))
  253. {
  254. _uiMonitor.ChangeCurrentNode(node);
  255. }
  256. }
  257. }
  258. }
  259.  
  260. }
  261. }
  262. #endif