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