Newer
Older
DungeonShooting / DungeonShooting_Godot / addons / dungeonShooting_plugin / Plugin.cs
@小李xl 小李xl on 6 May 2023 7 KB 处理ActivityMark表达式参数
  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. };
  63. public override void _Process(double delta)
  64. {
  65. Instance = this;
  66. if (_uiMonitor != null)
  67. {
  68. _uiMonitor.Process((float) delta);
  69. }
  70. else
  71. {
  72. _uiMonitor = new NodeMonitor();
  73. _uiMonitor.SceneNodeChangeEvent += GenerateUiCode;
  74. OnSceneChanged(GetEditorInterface().GetEditedSceneRoot());
  75. }
  76. }
  77.  
  78. public override void _EnterTree()
  79. {
  80. Instance = this;
  81.  
  82. if (_customTypeInfos != null)
  83. {
  84. //注册自定义节点
  85. foreach (var customTypeInfo in _customTypeInfos)
  86. {
  87. try
  88. {
  89. var script = GD.Load<Script>(customTypeInfo.ScriptPath);
  90. var icon = GD.Load<Texture2D>(customTypeInfo.IconPath);
  91. AddCustomType(customTypeInfo.Name, customTypeInfo.ParentName, script, icon);
  92. }
  93. catch (Exception e)
  94. {
  95. GD.PrintErr(e.ToString());
  96. }
  97. }
  98. }
  99.  
  100. _editorTools = GD.Load<PackedScene>(ResourcePath.prefab_ui_EditorTools_tscn).Instantiate<EditorToolsPanel>();
  101. var editorMainScreen = GetEditorInterface().GetEditorMainScreen();
  102. editorMainScreen.AddChild(_editorTools);
  103.  
  104. try
  105. {
  106. _editorTools.OnCreateUi();
  107. }
  108. catch (Exception e)
  109. {
  110. GD.PrintErr(e.ToString());
  111. }
  112.  
  113. try
  114. {
  115. _editorTools.OnShowUi();
  116. }
  117. catch (Exception e)
  118. {
  119. GD.PrintErr(e.ToString());
  120. }
  121. _MakeVisible(false);
  122. //场景切换事件
  123. SceneChanged += OnSceneChanged;
  124.  
  125. _uiMonitor = new NodeMonitor();
  126. _uiMonitor.SceneNodeChangeEvent += GenerateUiCode;
  127. OnSceneChanged(GetEditorInterface().GetEditedSceneRoot());
  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. GD.PrintErr(e.ToString());
  144. }
  145. }
  146. }
  147.  
  148. if (_editorTools != null)
  149. {
  150. try
  151. {
  152. _editorTools.OnHideUi();
  153. }
  154. catch (Exception e)
  155. {
  156. GD.PrintErr(e.ToString());
  157. }
  158.  
  159. try
  160. {
  161. _editorTools.OnDisposeUi();
  162. }
  163. catch (Exception e)
  164. {
  165. GD.PrintErr(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. }
  256. #endif