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