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, ISerializationListener
  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. public void OnBeforeSerialize()
  81. {
  82. OnHideUi();
  83. }
  84. public void OnAfterDeserialize()
  85. {
  86. OnShowUi();
  87. }
  88.  
  89. /// <summary>
  90. /// Tips 关闭信号回调
  91. /// </summary>
  92. private void OnTipsClose()
  93. {
  94. if (_onTipsClose != null)
  95. {
  96. _onTipsClose();
  97. _onTipsClose = null;
  98. }
  99. }
  100.  
  101. /// <summary>
  102. /// Confirm 确认信号回调
  103. /// </summary>
  104. private void OnConfirm()
  105. {
  106. if (_onConfirmClose != null)
  107. {
  108. _onConfirmClose(true);
  109. _onConfirmClose = null;
  110. }
  111. }
  112.  
  113. /// <summary>
  114. /// Confirm 取消信号回调
  115. /// </summary>
  116. private void OnCanceled()
  117. {
  118. if (_onConfirmClose != null)
  119. {
  120. _onConfirmClose(false);
  121. _onConfirmClose = null;
  122. }
  123. }
  124. /// <summary>
  125. /// 打开提示窗口, 并设置宽高
  126. /// </summary>
  127. /// <param name="title">窗口标题</param>
  128. /// <param name="message">显示内容</param>
  129. /// <param name="width">窗口宽度</param>
  130. /// <param name="height">窗口高度</param>
  131. /// <param name="onClose">当窗口关闭时的回调</param>
  132. public void ShowTips(string title, string message, int width, int height, Action onClose = null)
  133. {
  134. var tips = L_Tips.Instance;
  135. tips.Size = new Vector2I(width, height);
  136. tips.Title = title;
  137. tips.DialogText = message;
  138. _onTipsClose = onClose;
  139. tips.Show();
  140. }
  141. /// <summary>
  142. /// 打开提示窗口
  143. /// </summary>
  144. /// <param name="title">窗口标题</param>
  145. /// <param name="message">显示内容</param>
  146. /// <param name="onClose">当窗口关闭时的回调</param>
  147. public void ShowTips(string title, string message, Action onClose = null)
  148. {
  149. ShowTips(title, message, 350, 200, onClose);
  150. }
  151.  
  152. /// <summary>
  153. /// 关闭提示窗口
  154. /// </summary>
  155. public void CloseTips()
  156. {
  157. L_Tips.Instance.Hide();
  158. _onTipsClose = null;
  159. }
  160.  
  161. /// <summary>
  162. /// 打开询问窗口, 并设置宽高
  163. /// </summary>
  164. /// <param name="title">窗口标题</param>
  165. /// <param name="message">显示内容</param>
  166. /// <param name="width">窗口宽度</param>
  167. /// <param name="height">窗口高度</param>
  168. /// <param name="onClose">当窗口关闭时的回调, 参数如果为 true 表示点击了确定按钮</param>
  169. public void ShowConfirm(string title, string message, int width, int height, Action<bool> onClose = null)
  170. {
  171. var confirm = L_Confirm.Instance;
  172. confirm.Size = new Vector2I(width, height);
  173. confirm.Title = title;
  174. confirm.DialogText = message;
  175. _onConfirmClose = onClose;
  176. confirm.Show();
  177. }
  178. /// <summary>
  179. /// 打开询问窗口
  180. /// </summary>
  181. /// <param name="title">窗口标题</param>
  182. /// <param name="message">显示内容</param>
  183. /// <param name="onClose">当窗口关闭时的回调, 参数如果为 true 表示点击了确定按钮</param>
  184. public void ShowConfirm(string title, string message, Action<bool> onClose = null)
  185. {
  186. ShowConfirm(title, message, 350, 200, onClose);
  187. }
  188.  
  189. /// <summary>
  190. /// 关闭询问窗口
  191. /// </summary>
  192. public void CloseConfirm()
  193. {
  194. L_Confirm.Instance.Hide();
  195. _onConfirmClose = null;
  196. }
  197.  
  198. /// <summary>
  199. /// 重新生成当前ui的代码
  200. /// </summary>
  201. private void OnGenerateCurrentUiCode()
  202. {
  203. if (Plugin.Plugin.Instance != null)
  204. {
  205. var root = Plugin.Plugin.Instance.GetEditorInterface().GetEditedSceneRoot();
  206. if (root != null && Plugin.Plugin.Instance.CheckIsUi(root))
  207. {
  208. if (UiGenerator.GenerateUiCodeFromEditor(root))
  209. {
  210. ShowTips("提示", "生成UI代码执行成功!");
  211. }
  212. else
  213. {
  214. ShowTips("错误", "生成UI代码执行失败! 前往控制台查看错误日志!");
  215. }
  216. }
  217. else
  218. {
  219. ShowTips("错误", "当前的场景不是受管束的UI场景!");
  220. }
  221. }
  222. }
  223. /// <summary>
  224. /// 创建Ui
  225. /// </summary>
  226. private void OnCreateUI()
  227. {
  228. var uiName = L_ScrollContainer.L_MarginContainer.L_VBoxContainer.L_HBoxContainer3.L_LineEdit.Instance.Text;
  229. ShowConfirm("提示", "是否创建UI:" + uiName, (result) =>
  230. {
  231. if (result)
  232. {
  233. //检查名称是否合规
  234. if (!Regex.IsMatch(uiName, "^[A-Z][a-zA-Z0-9]*$"))
  235. {
  236. ShowTips("错误", "UI名称'" + uiName + "'不符合名称约束, UI名称只允许大写字母开头, 且名称中只允许出现大小字母和数字!");
  237. return;
  238. }
  239.  
  240. //检查是否有同名的Ui
  241. var path = GameConfig.UiPrefabDir + uiName + ".tscn";
  242. if (File.Exists(path))
  243. {
  244. ShowTips("错误", "已经存在相同名称'" + uiName + "'的UI了, 不能重复创建!");
  245. return;
  246. }
  247. //执行创建操作
  248. if (UiGenerator.CreateUi(uiName, true))
  249. {
  250. ShowTips("提示", "创建UI成功!");
  251. }
  252. else
  253. {
  254. ShowTips("错误", "创建UI失败! 前往控制台查看错误日志!");
  255. }
  256. }
  257. });
  258. }
  259.  
  260. /// <summary>
  261. /// 更新 ResourcePath
  262. /// </summary>
  263. private void GenerateResourcePath()
  264. {
  265. if (ResourcePathGenerator.Generate())
  266. {
  267. ShowTips("提示", "ResourcePath.cs生成完成!");
  268. }
  269. else
  270. {
  271. ShowTips("错误", "ResourcePath.cs生成失败! 前往控制台查看错误日志!");
  272. }
  273. }
  274. /// <summary>
  275. /// 重新生成UiManagerMethods.cs代码
  276. /// </summary>
  277. private void GenerateUiManagerMethods()
  278. {
  279. if (UiManagerMethodsGenerator.Generate())
  280. {
  281. ShowTips("提示", "生成UiManagerMethods.cs代码执行完成!");
  282. }
  283. else
  284. {
  285. ShowTips("错误", "生成UiManagerMethods.cs代码执行失败! 前往控制台查看错误日志!");
  286. }
  287. }
  288. /// <summary>
  289. /// 导出excel表
  290. /// </summary>
  291. private void ExportExcel()
  292. {
  293. if (ExcelGenerator.ExportExcel())
  294. {
  295. ShowTips("提示", "导出Excel表成功!");
  296. }
  297. else
  298. {
  299. ShowTips("错误", "导出Excel表失败,请查看控制台日志!");
  300. }
  301. }
  302.  
  303. /// <summary>
  304. /// 使用资源管理器打开excel表文件夹
  305. /// </summary>
  306. private void OpenExportExcelFolder()
  307. {
  308. var path = Environment.CurrentDirectory + "\\excel";
  309. System.Diagnostics.Process.Start("explorer.exe", path);
  310. }
  311. /// <summary>
  312. /// 在编辑器中打开一个提示窗口
  313. /// </summary>
  314. public static void ShowTipsInEditor(string title, string message, Action onClose)
  315. {
  316. var editorToolsInstance = UiManager.Get_EditorTools_Instance();
  317. if (editorToolsInstance.Length > 0)
  318. {
  319. editorToolsInstance[0].ShowTips(title, message, onClose);
  320. }
  321. }
  322. /// <summary>
  323. /// 在编辑器中打开一个询问窗口
  324. /// </summary>
  325. public static void ShowConfirmInEditor(string title, string message, Action<bool> onClose = null)
  326. {
  327. var editorToolsInstance = UiManager.Get_EditorTools_Instance();
  328. if (editorToolsInstance.Length > 0)
  329. {
  330. editorToolsInstance[0].ShowConfirm(title, message, onClose);
  331. }
  332. }
  333. #else
  334. public override void OnShowUi()
  335. {
  336. }
  337. public override void OnHideUi()
  338. {
  339. }
  340. #endif
  341. }