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, ISerializationListener
  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 && Instance == this)
  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. #region MyRegion
  77.  
  78. if (_customTypeInfos != null)
  79. {
  80. //注册自定义节点
  81. foreach (var customTypeInfo in _customTypeInfos)
  82. {
  83. try
  84. {
  85. var script = GD.Load<Script>(customTypeInfo.ScriptPath);
  86. var icon = GD.Load<Texture2D>(customTypeInfo.IconPath);
  87. AddCustomType(customTypeInfo.Name, customTypeInfo.ParentName, script, icon);
  88. }
  89. catch (Exception e)
  90. {
  91. Debug.LogError(e.ToString());
  92. }
  93. }
  94. }
  95.  
  96. _editorTools = GD.Load<PackedScene>(ResourcePath.prefab_ui_EditorTools_tscn).Instantiate<EditorToolsPanel>();
  97. var editorMainScreen = GetEditorInterface().GetEditorMainScreen();
  98. editorMainScreen.AddChild(_editorTools);
  99.  
  100. try
  101. {
  102. _editorTools.OnCreateUi();
  103. }
  104. catch (Exception e)
  105. {
  106. Debug.LogError(e.ToString());
  107. }
  108.  
  109. try
  110. {
  111. _editorTools.OnShowUi();
  112. }
  113. catch (Exception e)
  114. {
  115. Debug.LogError(e.ToString());
  116. }
  117. _MakeVisible(false);
  118. #endregion
  119. //场景切换事件
  120. SceneChanged += OnSceneChanged;
  121.  
  122. _uiMonitor = new NodeMonitor();
  123. _uiMonitor.SceneNodeChangeEvent += GenerateUiCode;
  124.  
  125. OnSceneChanged(GetEditorInterface().GetEditedSceneRoot());
  126. }
  127. public void OnBeforeSerialize()
  128. {
  129. SceneChanged -= OnSceneChanged;
  130. }
  131. public void OnAfterDeserialize()
  132. {
  133. SceneChanged += OnSceneChanged;
  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. Debug.LogError(e.ToString());
  150. }
  151. }
  152. }
  153.  
  154. if (_editorTools != null)
  155. {
  156. try
  157. {
  158. _editorTools.OnHideUi();
  159. }
  160. catch (Exception e)
  161. {
  162. Debug.LogError(e.ToString());
  163. }
  164.  
  165. try
  166. {
  167. _editorTools.OnDestroyUi();
  168. }
  169. catch (Exception e)
  170. {
  171. Debug.LogError(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. #endif