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