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()
  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. //重新打包房间配置
  38. container.L_HBoxContainer2.L_Button.Instance.Pressed += GenerateRoomConfig;
  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. container.L_HBoxContainer6.L_Button.Instance.Pressed += GenerateDungeonRoom;
  47. }
  48.  
  49. public override void OnHideUi()
  50. {
  51. L_Tips.Instance.CloseRequested -= OnTipsClose;
  52. L_Tips.Instance.Confirmed -= OnTipsClose;
  53. L_Tips.Instance.Canceled -= OnTipsClose;
  54. L_Confirm.Instance.Canceled -= OnCanceled;
  55. L_Confirm.Instance.CloseRequested -= OnCanceled;
  56. L_Confirm.Instance.Confirmed -= OnConfirm;
  57. var container = L_ScrollContainer.L_MarginContainer.L_VBoxContainer;
  58. container.L_HBoxContainer.L_Button.Instance.Pressed -= GenerateResourcePath;
  59. container.L_HBoxContainer2.L_Button.Instance.Pressed -= GenerateRoomConfig;
  60. container.L_HBoxContainer4.L_Button.Instance.Pressed -= OnGenerateCurrentUiCode;
  61. container.L_HBoxContainer3.L_Button.Instance.Pressed -= OnCreateUI;
  62. container.L_HBoxContainer5.L_Button.Instance.Pressed -= GenerateUiManagerMethods;
  63. container.L_HBoxContainer6.L_Button.Instance.Pressed -= GenerateDungeonRoom;
  64. }
  65.  
  66. /// <summary>
  67. /// Tips 关闭信号回调
  68. /// </summary>
  69. private void OnTipsClose()
  70. {
  71. if (_onTipsClose != null)
  72. {
  73. _onTipsClose();
  74. _onTipsClose = null;
  75. }
  76. }
  77.  
  78. /// <summary>
  79. /// Confirm 确认信号回调
  80. /// </summary>
  81. private void OnConfirm()
  82. {
  83. if (_onConfirmClose != null)
  84. {
  85. _onConfirmClose(true);
  86. _onConfirmClose = null;
  87. }
  88. }
  89.  
  90. /// <summary>
  91. /// Confirm 取消信号回调
  92. /// </summary>
  93. private void OnCanceled()
  94. {
  95. if (_onConfirmClose != null)
  96. {
  97. _onConfirmClose(false);
  98. _onConfirmClose = null;
  99. }
  100. }
  101. /// <summary>
  102. /// 打开提示窗口, 并设置宽高
  103. /// </summary>
  104. /// <param name="title">窗口标题</param>
  105. /// <param name="message">显示内容</param>
  106. /// <param name="width">窗口宽度</param>
  107. /// <param name="height">窗口高度</param>
  108. /// <param name="onClose">当窗口关闭时的回调</param>
  109. public void ShowTips(string title, string message, int width, int height, Action onClose = null)
  110. {
  111. var tips = L_Tips.Instance;
  112. tips.Size = new Vector2I(width, height);
  113. tips.Title = title;
  114. tips.DialogText = message;
  115. _onTipsClose = onClose;
  116. tips.Show();
  117. }
  118. /// <summary>
  119. /// 打开提示窗口
  120. /// </summary>
  121. /// <param name="title">窗口标题</param>
  122. /// <param name="message">显示内容</param>
  123. /// <param name="onClose">当窗口关闭时的回调</param>
  124. public void ShowTips(string title, string message, Action onClose = null)
  125. {
  126. ShowTips(title, message, 200, 124, onClose);
  127. }
  128.  
  129. /// <summary>
  130. /// 关闭提示窗口
  131. /// </summary>
  132. public void CloseTips()
  133. {
  134. L_Tips.Instance.Hide();
  135. _onTipsClose = null;
  136. }
  137.  
  138. /// <summary>
  139. /// 打开询问窗口, 并设置宽高
  140. /// </summary>
  141. /// <param name="title">窗口标题</param>
  142. /// <param name="message">显示内容</param>
  143. /// <param name="width">窗口宽度</param>
  144. /// <param name="height">窗口高度</param>
  145. /// <param name="onClose">当窗口关闭时的回调, 参数如果为 true 表示点击了确定按钮</param>
  146. public void ShowConfirm(string title, string message, int width, int height, Action<bool> onClose = null)
  147. {
  148. var confirm = L_Confirm.Instance;
  149. confirm.Size = new Vector2I(width, height);
  150. confirm.Title = title;
  151. confirm.DialogText = message;
  152. _onConfirmClose = onClose;
  153. confirm.Show();
  154. }
  155. /// <summary>
  156. /// 打开询问窗口
  157. /// </summary>
  158. /// <param name="title">窗口标题</param>
  159. /// <param name="message">显示内容</param>
  160. /// <param name="onClose">当窗口关闭时的回调, 参数如果为 true 表示点击了确定按钮</param>
  161. public void ShowConfirm(string title, string message, Action<bool> onClose = null)
  162. {
  163. ShowConfirm(title, message, 200, 124, onClose);
  164. }
  165.  
  166. /// <summary>
  167. /// 关闭询问窗口
  168. /// </summary>
  169. public void CloseConfirm()
  170. {
  171. L_Confirm.Instance.Hide();
  172. _onConfirmClose = null;
  173. }
  174.  
  175. /// <summary>
  176. /// 重新生成当前ui的代码
  177. /// </summary>
  178. private void OnGenerateCurrentUiCode()
  179. {
  180. #if TOOLS
  181. if (Plugin.Plugin.Instance != null)
  182. {
  183. var root = Plugin.Plugin.Instance.GetEditorInterface().GetEditedSceneRoot();
  184. if (root != null && Plugin.Plugin.Instance.CheckIsUi(root))
  185. {
  186. if (UiGenerator.GenerateUiCodeFromEditor(root))
  187. {
  188. ShowTips("提示", "生成UI代码执行成功!");
  189. }
  190. else
  191. {
  192. ShowTips("错误", "生成UI代码执行失败! 前往控制台查看错误日志!");
  193. }
  194. }
  195. else
  196. {
  197. ShowTips("错误", "当前的场景不是受管束的UI场景!");
  198. }
  199. }
  200. #endif
  201. }
  202. /// <summary>
  203. /// 创建Ui
  204. /// </summary>
  205. private void OnCreateUI()
  206. {
  207. var uiName = L_ScrollContainer.L_MarginContainer.L_VBoxContainer.L_HBoxContainer3.L_LineEdit.Instance.Text;
  208. ShowConfirm("提示", "是否创建UI:" + uiName, (result) =>
  209. {
  210. if (result)
  211. {
  212. //检查名称是否合规
  213. if (!Regex.IsMatch(uiName, "^[A-Z][a-zA-Z0-9]*$"))
  214. {
  215. ShowTips("错误", "UI名称'" + uiName + "'不符合名称约束, UI名称只允许大写字母开头, 且名称中只允许出现大小字母和数字!");
  216. return;
  217. }
  218.  
  219. //检查是否有同名的Ui
  220. var path = GameConfig.UiPrefabDir + uiName + ".tscn";
  221. if (File.Exists(path))
  222. {
  223. ShowTips("错误", "已经存在相同名称'" + uiName + "'的UI了, 不能重复创建!");
  224. return;
  225. }
  226. //执行创建操作
  227. if (UiGenerator.CreateUi(uiName, true))
  228. {
  229. ShowTips("提示", "创建UI成功!");
  230. }
  231. else
  232. {
  233. ShowTips("错误", "创建UI失败! 前往控制台查看错误日志!");
  234. }
  235. }
  236. });
  237. }
  238.  
  239. /// <summary>
  240. /// 更新 ResourcePath
  241. /// </summary>
  242. private void GenerateResourcePath()
  243. {
  244. if (ResourcePathGenerator.Generate())
  245. {
  246. ShowTips("提示", "ResourcePath.cs生成完成!");
  247. }
  248. else
  249. {
  250. ShowTips("错误", "ResourcePath.cs生成失败! 前往控制台查看错误日志!");
  251. }
  252. }
  253.  
  254. /// <summary>
  255. /// 重新打包房间配置
  256. /// </summary>
  257. private void GenerateRoomConfig()
  258. {
  259. if (DungeonRoomGenerator.GenerateRoomConfig())
  260. {
  261. ShowTips("提示", "打包地牢房间配置执行完成!");
  262. }
  263. else
  264. {
  265. ShowTips("错误", "打包地牢房间配置执行失败! 前往控制台查看错误日志!");
  266. }
  267. }
  268.  
  269. /// <summary>
  270. /// 重新生成UiManagerMethods.cs代码
  271. /// </summary>
  272. private void GenerateUiManagerMethods()
  273. {
  274. if (UiManagerMethodsGenerator.Generate())
  275. {
  276. ShowTips("提示", "生成UiManagerMethods.cs代码执行完成!");
  277. }
  278. else
  279. {
  280. ShowTips("错误", "生成UiManagerMethods.cs代码执行失败! 前往控制台查看错误日志!");
  281. }
  282. }
  283.  
  284. /// <summary>
  285. /// 创建地牢房间
  286. /// </summary>
  287. private void GenerateDungeonRoom()
  288. {
  289. var roomName = L_ScrollContainer.L_MarginContainer.L_VBoxContainer.L_HBoxContainer6.L_LineEdit.Instance.Text;
  290. ShowConfirm("提示", "是否创建房间:" + roomName, (result) =>
  291. {
  292. if (result)
  293. {
  294. //检查名称是否合规
  295. if (!Regex.IsMatch(roomName, "^\\w*$"))
  296. {
  297. ShowTips("错误", "房间名称'" + roomName + "'不符合名称约束, 房间名称只允许包含大写字母和数字!");
  298. return;
  299. }
  300.  
  301. //检查是否有同名的Ui
  302. var path = GameConfig.RoomTileDir + roomName + ".tscn";
  303. if (File.Exists(path))
  304. {
  305. ShowTips("错误", "已经存在相同名称'" + roomName + "'的房间了, 不能重复创建!");
  306. return;
  307. }
  308. //执行创建操作
  309. if (DungeonRoomGenerator.CreateDungeonRoom(roomName, true))
  310. {
  311. ShowTips("提示", "创建房间成功!");
  312. }
  313. else
  314. {
  315. ShowTips("错误", "创建房间失败! 前往控制台查看错误日志!");
  316. }
  317. }
  318. });
  319. }
  320. }