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