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