Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / editorTools / EditorToolsPanel.cs
@小李xl 小李xl on 9 Nov 2023 10 KB 制作3个道具
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. using Godot;
  6.  
  7. #if TOOLS
  8. using Generator;
  9. #endif
  10.  
  11. namespace UI.EditorTools;
  12.  
  13. /// <summary>
  14. /// Godot编辑器扩展工具
  15. /// </summary>
  16. [Tool]
  17. public partial class EditorToolsPanel : EditorTools
  18. {
  19. #if TOOLS
  20. //Tips 关闭回调
  21. private Action _onTipsClose;
  22.  
  23. //询问窗口关闭
  24. private Action<bool> _onConfirmClose;
  25.  
  26. //存放创建房间中选择组的下拉框数据
  27. private Dictionary<int, string> _createRoomGroupValueMap;
  28. //存放创建房间中选择类型的下拉框数据
  29. private Dictionary<int, string> _createRoomTypeValueMap;
  30.  
  31. public override void OnShowUi()
  32. {
  33. //tips
  34. _onTipsClose = null;
  35. L_Tips.Instance.OkButtonText = "确定";
  36. L_Tips.Instance.CloseRequested += OnTipsClose;
  37. L_Tips.Instance.Confirmed += OnTipsClose;
  38. L_Tips.Instance.Canceled += OnTipsClose;
  39.  
  40. //confirm
  41. _onConfirmClose = null;
  42. L_Confirm.Instance.OkButtonText = "确定";
  43. L_Confirm.Instance.CancelButtonText = "取消";
  44. L_Confirm.Instance.Canceled += OnCanceled;
  45. L_Confirm.Instance.CloseRequested += OnCanceled;
  46. L_Confirm.Instance.Confirmed += OnConfirm;
  47.  
  48. var container = L_ScrollContainer.L_MarginContainer.L_VBoxContainer;
  49. //重新生成 ResourcePath
  50. container.L_HBoxContainer.L_Button.Instance.Pressed += GenerateResourcePath;
  51. //重新生成ui代码
  52. container.L_HBoxContainer4.L_Button.Instance.Pressed += OnGenerateCurrentUiCode;
  53. //创建ui
  54. container.L_HBoxContainer3.L_Button.Instance.Pressed += OnCreateUI;
  55. //重新生成UiManagerMethods.cs代码
  56. container.L_HBoxContainer5.L_Button.Instance.Pressed += GenerateUiManagerMethods;
  57. //导出excel表
  58. container.L_HBoxContainer7.L_Button.Instance.Pressed += ExportExcel;
  59. }
  60.  
  61. public override void OnHideUi()
  62. {
  63. L_Tips.Instance.CloseRequested -= OnTipsClose;
  64. L_Tips.Instance.Confirmed -= OnTipsClose;
  65. L_Tips.Instance.Canceled -= OnTipsClose;
  66. L_Confirm.Instance.Canceled -= OnCanceled;
  67. L_Confirm.Instance.CloseRequested -= OnCanceled;
  68. L_Confirm.Instance.Confirmed -= OnConfirm;
  69. var container = L_ScrollContainer.L_MarginContainer.L_VBoxContainer;
  70. container.L_HBoxContainer.L_Button.Instance.Pressed -= GenerateResourcePath;
  71. container.L_HBoxContainer4.L_Button.Instance.Pressed -= OnGenerateCurrentUiCode;
  72. container.L_HBoxContainer3.L_Button.Instance.Pressed -= OnCreateUI;
  73. container.L_HBoxContainer5.L_Button.Instance.Pressed -= GenerateUiManagerMethods;
  74. container.L_HBoxContainer7.L_Button.Instance.Pressed -= ExportExcel;
  75. }
  76.  
  77. /// <summary>
  78. /// Tips 关闭信号回调
  79. /// </summary>
  80. private void OnTipsClose()
  81. {
  82. if (_onTipsClose != null)
  83. {
  84. _onTipsClose();
  85. _onTipsClose = null;
  86. }
  87. }
  88.  
  89. /// <summary>
  90. /// Confirm 确认信号回调
  91. /// </summary>
  92. private void OnConfirm()
  93. {
  94. if (_onConfirmClose != null)
  95. {
  96. _onConfirmClose(true);
  97. _onConfirmClose = null;
  98. }
  99. }
  100.  
  101. /// <summary>
  102. /// Confirm 取消信号回调
  103. /// </summary>
  104. private void OnCanceled()
  105. {
  106. if (_onConfirmClose != null)
  107. {
  108. _onConfirmClose(false);
  109. _onConfirmClose = null;
  110. }
  111. }
  112. /// <summary>
  113. /// 打开提示窗口, 并设置宽高
  114. /// </summary>
  115. /// <param name="title">窗口标题</param>
  116. /// <param name="message">显示内容</param>
  117. /// <param name="width">窗口宽度</param>
  118. /// <param name="height">窗口高度</param>
  119. /// <param name="onClose">当窗口关闭时的回调</param>
  120. public void ShowTips(string title, string message, int width, int height, Action onClose = null)
  121. {
  122. var tips = L_Tips.Instance;
  123. tips.Size = new Vector2I(width, height);
  124. tips.Title = title;
  125. tips.DialogText = message;
  126. _onTipsClose = onClose;
  127. tips.Show();
  128. }
  129. /// <summary>
  130. /// 打开提示窗口
  131. /// </summary>
  132. /// <param name="title">窗口标题</param>
  133. /// <param name="message">显示内容</param>
  134. /// <param name="onClose">当窗口关闭时的回调</param>
  135. public void ShowTips(string title, string message, Action onClose = null)
  136. {
  137. ShowTips(title, message, 350, 200, onClose);
  138. }
  139.  
  140. /// <summary>
  141. /// 关闭提示窗口
  142. /// </summary>
  143. public void CloseTips()
  144. {
  145. L_Tips.Instance.Hide();
  146. _onTipsClose = null;
  147. }
  148.  
  149. /// <summary>
  150. /// 打开询问窗口, 并设置宽高
  151. /// </summary>
  152. /// <param name="title">窗口标题</param>
  153. /// <param name="message">显示内容</param>
  154. /// <param name="width">窗口宽度</param>
  155. /// <param name="height">窗口高度</param>
  156. /// <param name="onClose">当窗口关闭时的回调, 参数如果为 true 表示点击了确定按钮</param>
  157. public void ShowConfirm(string title, string message, int width, int height, Action<bool> onClose = null)
  158. {
  159. var confirm = L_Confirm.Instance;
  160. confirm.Size = new Vector2I(width, height);
  161. confirm.Title = title;
  162. confirm.DialogText = message;
  163. _onConfirmClose = onClose;
  164. confirm.Show();
  165. }
  166. /// <summary>
  167. /// 打开询问窗口
  168. /// </summary>
  169. /// <param name="title">窗口标题</param>
  170. /// <param name="message">显示内容</param>
  171. /// <param name="onClose">当窗口关闭时的回调, 参数如果为 true 表示点击了确定按钮</param>
  172. public void ShowConfirm(string title, string message, Action<bool> onClose = null)
  173. {
  174. ShowConfirm(title, message, 350, 200, onClose);
  175. }
  176.  
  177. /// <summary>
  178. /// 关闭询问窗口
  179. /// </summary>
  180. public void CloseConfirm()
  181. {
  182. L_Confirm.Instance.Hide();
  183. _onConfirmClose = null;
  184. }
  185.  
  186. /// <summary>
  187. /// 重新生成当前ui的代码
  188. /// </summary>
  189. private void OnGenerateCurrentUiCode()
  190. {
  191. if (Plugin.Plugin.Instance != null)
  192. {
  193. var root = Plugin.Plugin.Instance.GetEditorInterface().GetEditedSceneRoot();
  194. if (root != null && Plugin.Plugin.Instance.CheckIsUi(root))
  195. {
  196. if (UiGenerator.GenerateUiCodeFromEditor(root))
  197. {
  198. ShowTips("提示", "生成UI代码执行成功!");
  199. }
  200. else
  201. {
  202. ShowTips("错误", "生成UI代码执行失败! 前往控制台查看错误日志!");
  203. }
  204. }
  205. else
  206. {
  207. ShowTips("错误", "当前的场景不是受管束的UI场景!");
  208. }
  209. }
  210. }
  211. /// <summary>
  212. /// 创建Ui
  213. /// </summary>
  214. private void OnCreateUI()
  215. {
  216. var uiName = L_ScrollContainer.L_MarginContainer.L_VBoxContainer.L_HBoxContainer3.L_LineEdit.Instance.Text;
  217. ShowConfirm("提示", "是否创建UI:" + uiName, (result) =>
  218. {
  219. if (result)
  220. {
  221. //检查名称是否合规
  222. if (!Regex.IsMatch(uiName, "^[A-Z][a-zA-Z0-9]*$"))
  223. {
  224. ShowTips("错误", "UI名称'" + uiName + "'不符合名称约束, UI名称只允许大写字母开头, 且名称中只允许出现大小字母和数字!");
  225. return;
  226. }
  227.  
  228. //检查是否有同名的Ui
  229. var path = GameConfig.UiPrefabDir + uiName + ".tscn";
  230. if (File.Exists(path))
  231. {
  232. ShowTips("错误", "已经存在相同名称'" + uiName + "'的UI了, 不能重复创建!");
  233. return;
  234. }
  235. //执行创建操作
  236. if (UiGenerator.CreateUi(uiName, true))
  237. {
  238. ShowTips("提示", "创建UI成功!");
  239. }
  240. else
  241. {
  242. ShowTips("错误", "创建UI失败! 前往控制台查看错误日志!");
  243. }
  244. }
  245. });
  246. }
  247.  
  248. /// <summary>
  249. /// 更新 ResourcePath
  250. /// </summary>
  251. private void GenerateResourcePath()
  252. {
  253. if (ResourcePathGenerator.Generate())
  254. {
  255. ShowTips("提示", "ResourcePath.cs生成完成!");
  256. }
  257. else
  258. {
  259. ShowTips("错误", "ResourcePath.cs生成失败! 前往控制台查看错误日志!");
  260. }
  261. }
  262. /// <summary>
  263. /// 重新生成UiManagerMethods.cs代码
  264. /// </summary>
  265. private void GenerateUiManagerMethods()
  266. {
  267. if (UiManagerMethodsGenerator.Generate())
  268. {
  269. ShowTips("提示", "生成UiManagerMethods.cs代码执行完成!");
  270. }
  271. else
  272. {
  273. ShowTips("错误", "生成UiManagerMethods.cs代码执行失败! 前往控制台查看错误日志!");
  274. }
  275. }
  276. /// <summary>
  277. /// 导出excel表
  278. /// </summary>
  279. private void ExportExcel()
  280. {
  281. ExcelGenerator.ExportExcel();
  282. ShowTips("提示", "已启动导表程序, 注意查看控制台信息!");
  283. }
  284. /// <summary>
  285. /// 在编辑器中打开一个提示窗口
  286. /// </summary>
  287. public static void ShowTipsInEditor(string title, string message, Action onClose)
  288. {
  289. var editorToolsInstance = UiManager.Get_EditorTools_Instance();
  290. if (editorToolsInstance.Length > 0)
  291. {
  292. editorToolsInstance[0].ShowTips(title, message, onClose);
  293. }
  294. }
  295. /// <summary>
  296. /// 在编辑器中打开一个询问窗口
  297. /// </summary>
  298. public static void ShowConfirmInEditor(string title, string message, Action<bool> onClose = null)
  299. {
  300. var editorToolsInstance = UiManager.Get_EditorTools_Instance();
  301. if (editorToolsInstance.Length > 0)
  302. {
  303. editorToolsInstance[0].ShowConfirm(title, message, onClose);
  304. }
  305. }
  306. #else
  307. public override void OnShowUi()
  308. {
  309. }
  310. public override void OnHideUi()
  311. {
  312. }
  313. #endif
  314. }