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