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