Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditor / TileSetEditorPanel.cs
@小李xl 小李xl on 18 Jan 2024 10 KB TileMap编辑器绘制组合
  1. using System.Linq;
  2. using Godot;
  3.  
  4. namespace UI.TileSetEditor;
  5.  
  6. public partial class TileSetEditorPanel : TileSetEditor
  7. {
  8. /// <summary>
  9. /// 数据是否脏了
  10. /// </summary>
  11. public bool IsDirty { get; private set; }
  12.  
  13. /// <summary>
  14. /// 编辑使用的 tileSetSplit 数据
  15. /// </summary>
  16. public TileSetSplit TileSetSplit { get; private set; }
  17.  
  18. /// <summary>
  19. /// 编辑使用的 tileSetInfo 数据
  20. /// </summary>
  21. public TileSetInfo TileSetInfo { get; private set; }
  22. /// <summary>
  23. /// 当前正在使用的 TileSetSourceInfo 数据
  24. /// </summary>
  25. public TileSetSourceInfo TileSetSourceInfo { get; private set; }
  26. /// <summary>
  27. /// 当前正在使用的 TileSetSourceInfo 的索引
  28. /// </summary>
  29. public int TileSetSourceIndex { get; private set; }
  30. /// <summary>
  31. /// 是否初始化过纹理
  32. /// </summary>
  33. public bool InitTexture { get; private set; }
  34. /// <summary>
  35. /// 纹理
  36. /// </summary>
  37. public ImageTexture Texture { get; private set; }
  38. /// <summary>
  39. /// 纹理的Image对象
  40. /// </summary>
  41. public Image TextureImage { get; private set; }
  42.  
  43. /// <summary>
  44. /// 背景颜色
  45. /// </summary>
  46. public Color BgColor { get; private set; }
  47. /// <summary>
  48. /// Cell 横轴数量
  49. /// </summary>
  50. public int CellHorizontal { get; private set; }
  51. /// <summary>
  52. /// Cell 纵轴数量
  53. /// </summary>
  54. public int CellVertical { get; private set; }
  55.  
  56. /// <summary>
  57. /// 页签对象
  58. /// </summary>
  59. public UiGrid<Tab, TileSetEditorTabData> TabGrid { get; private set; }
  60.  
  61. private Image _emptyImage;
  62.  
  63. public override void OnCreateUi()
  64. {
  65. _emptyImage = Image.Create(1, 1, false, Image.Format.Rgba8);
  66. Texture = new ImageTexture();
  67. Texture.SetImage(_emptyImage);
  68. S_Back.Instance.Visible = PrevUi != null;
  69. S_Back.Instance.Pressed += OnBackClick;
  70.  
  71. TabGrid = new UiGrid<Tab, TileSetEditorTabData>(S_Tab, typeof(TileSetEditorTabCell));
  72. TabGrid.SetHorizontalExpand(true);
  73. TabGrid.SetCellOffset(new Vector2I(0, 5));
  74. TabGrid.Add(new TileSetEditorTabData()
  75. {
  76. Text = "纹理",
  77. UiName = UiManager.UiNames.TileSetEditorImport,
  78. });
  79. TabGrid.Add(new TileSetEditorTabData()
  80. {
  81. Text = "地形",
  82. UiName = UiManager.UiNames.TileSetEditorTerrain,
  83. });
  84. TabGrid.Add(new TileSetEditorTabData()
  85. {
  86. Text = "组合",
  87. UiName = UiManager.UiNames.TileSetEditorCombination,
  88. });
  89. TabGrid.Visible = false;
  90.  
  91. S_DeleteButton.Instance.Pressed += OnDeleteSourceClick;
  92. S_AddButton.Instance.Pressed += OnAddSourceClick;
  93. S_OptionButton.Instance.ItemSelected += OnOptionChange;
  94. S_Save.Instance.Pressed += OnSaveClick;
  95. AddEventListener(EventEnum.OnTileSetDirty, OnTileSetDirty);
  96. }
  97.  
  98. public override void OnDestroyUi()
  99. {
  100. TabGrid.Destroy();
  101. if (Texture != null)
  102. {
  103. Texture.Dispose();
  104. }
  105. _emptyImage.Dispose();
  106. TextureImage = null;
  107.  
  108. if (TileSetInfo != null)
  109. {
  110. TileSetInfo.Destroy();
  111. }
  112. }
  113.  
  114. public override void Process(float delta)
  115. {
  116. if (TabGrid.Visible && TileSetSourceInfo != null)
  117. {
  118. //纹理红点
  119. ((TileSetEditorTabCell)TabGrid.GetCell(0)).CellNode.L_ErrorIcon.Instance.Visible = !InitTexture;
  120. //地形红点
  121. var terrainTab = (TileSetEditorTabCell)TabGrid.GetCell(1);
  122. var flag = true;
  123. foreach (var terrainInfo in TileSetSourceInfo.Terrain)
  124. {
  125. if (!terrainInfo.Ready)
  126. {
  127. flag = false;
  128. break;
  129. }
  130. }
  131. terrainTab.CellNode.L_ErrorIcon.Instance.Visible = !flag;
  132. }
  133. }
  134.  
  135. /// <summary>
  136. /// 初始化数据
  137. /// </summary>
  138. public void InitData(TileSetSplit tileSetSplit)
  139. {
  140. IsDirty = false;
  141. tileSetSplit.ReloadTileSetInfo();
  142. TileSetSplit = tileSetSplit;
  143. TileSetInfo = tileSetSplit.TileSetInfo.Clone();
  144. RefreshTitle();
  145. //初始化下拉框
  146. if (TileSetInfo.Sources.Count > 0)
  147. {
  148. var optionButton = S_OptionButton.Instance;
  149. foreach (var tileSetSourceInfo in TileSetInfo.Sources)
  150. {
  151. optionButton.AddItem(tileSetSourceInfo.Name);
  152. }
  153.  
  154. optionButton.Selected = 0;
  155. OnOptionChange(0);
  156. }
  157. }
  158.  
  159. //设置标题显示内容
  160. private void RefreshTitle()
  161. {
  162. if (IsDirty)
  163. {
  164. S_Title.Instance.Text = "正在编辑:" + TileSetInfo.Name + "*";
  165. }
  166. else
  167. {
  168. S_Title.Instance.Text = "正在编辑:" + TileSetInfo.Name;
  169. }
  170. }
  171.  
  172. /// <summary>
  173. /// 设置纹理使用的纹理
  174. /// </summary>
  175. public void SetTextureData(Image image)
  176. {
  177. if (image == null)
  178. {
  179. InitTexture = false;
  180. TextureImage = null;
  181. Texture.SetImage(_emptyImage);
  182. CellHorizontal = 0;
  183. CellVertical = 0;
  184. }
  185. else
  186. {
  187. if (TileSetSourceInfo == null)
  188. {
  189. return;
  190. }
  191. InitTexture = true;
  192. Texture.SetImage(image);
  193. TextureImage = image;
  194. CellHorizontal = image.GetWidth() / GameConfig.TileCellSize;
  195. CellVertical = image.GetHeight() / GameConfig.TileCellSize;
  196. }
  197. //派发事件
  198. EventManager.EmitEvent(EventEnum.OnSetTileTexture, Texture);
  199. }
  200.  
  201. /// <summary>
  202. /// 设置背景颜色
  203. /// </summary>
  204. public void SetBgColor(Color color)
  205. {
  206. BgColor = color;
  207. //派发事件
  208. EventManager.EmitEvent(EventEnum.OnSetTileSetBgColor, color);
  209. }
  210. /// <summary>
  211. /// 将二维位置转换为索引的函数
  212. /// </summary>
  213. public int CellPositionToIndex(Vector2I pos)
  214. {
  215. return pos.Y * CellHorizontal + pos.X;
  216. }
  217. /// <summary>
  218. /// 返回Cell的坐标是否在纹理区域内
  219. /// </summary>
  220. public bool IsCellPositionInTexture(Vector2I cell)
  221. {
  222. return cell.X >= 0 && cell.Y >= 0 && cell.X < CellHorizontal && cell.Y < CellVertical;
  223. }
  224.  
  225. //返回上一级按钮点击
  226. private void OnBackClick()
  227. {
  228. if (IsDirty)
  229. {
  230. EditorWindowManager.ShowConfirm("提示", "当前TileSet修改的数据还未保存,是否退出编辑?",
  231. "保存并退出", "直接退出", "取消"
  232. , index =>
  233. {
  234. if (index == 0) //保存并退出
  235. {
  236. OnSaveClick();
  237. OpenPrevUi();
  238. }
  239. else if (index == 1) //直接退出
  240. {
  241. OpenPrevUi();
  242. }
  243. }
  244. );
  245. }
  246. else
  247. {
  248. OpenPrevUi();
  249. }
  250. }
  251.  
  252. //删除资源
  253. private void OnDeleteSourceClick()
  254. {
  255. var optionButton = S_OptionButton.Instance;
  256. var selectIndex = optionButton.Selected;
  257. if (selectIndex == 0)
  258. {
  259. EditorWindowManager.ShowTips("警告", "不允许删除 Main Source!");
  260. return;
  261. }
  262. else if (selectIndex > 0)
  263. {
  264. EditorWindowManager.ShowConfirm("提示", "是否需要删除该资源!", v =>
  265. {
  266. if (v)
  267. {
  268. var name = optionButton.GetItemText(selectIndex);
  269. var findIndex = TileSetInfo.Sources.FindIndex(info => info.Name == name);
  270. if (findIndex >= 0)
  271. {
  272. TileSetInfo.Sources[findIndex].Destroy();
  273. TileSetInfo.Sources.RemoveAt(findIndex);
  274. }
  275.  
  276. var index = optionButton.ItemCount - 2;
  277. optionButton.RemoveItem(selectIndex);
  278. optionButton.Selected = index;
  279. OnOptionChange(index);
  280. //标记数据脏了
  281. EventManager.EmitEvent(EventEnum.OnTileSetDirty);
  282. }
  283. });
  284. }
  285. else
  286. {
  287. EditorWindowManager.ShowTips("提示", "请选择需要删除的资源!");
  288. }
  289. }
  290.  
  291. //创建资源
  292. private void OnAddSourceClick()
  293. {
  294. EditorWindowManager.ShowInput("创建资源", "资源名称:", null, (value, isSubmit) =>
  295. {
  296. if (isSubmit)
  297. {
  298. if (TileSetInfo.Sources.FindIndex(info => info.Name == value) >= 0)
  299. {
  300. EditorWindowManager.ShowTips("错误", "该资源名称已存在!");
  301. return false;
  302. }
  303. var source = new TileSetSourceInfo();
  304. source.InitData();
  305. source.Name = value;
  306. TileSetInfo.Sources.Add(source);
  307. EventManager.EmitEvent(EventEnum.OnCreateTileSetSource, source);
  308. var optionButton = S_OptionButton.Instance;
  309. optionButton.AddItem(value);
  310. var selectIndex = optionButton.ItemCount - 1;
  311. optionButton.Selected = selectIndex;
  312. OnOptionChange(selectIndex);
  313. }
  314.  
  315. return true;
  316. });
  317. }
  318.  
  319. //选中资源
  320. private void OnOptionChange(long index)
  321. {
  322. if (index >= 0)
  323. {
  324. TabGrid.Visible = true;
  325. TabGrid.SelectIndex = 0;
  326. TileSetSourceInfo = TileSetInfo.Sources[(int)index];
  327. TileSetSourceIndex = (int)index;
  328. SetTextureData(TileSetSourceInfo.GetSourceImage());
  329. }
  330. else
  331. {
  332. TabGrid.Visible = false;
  333. TabGrid.SelectIndex = -1;
  334. TileSetSourceInfo = null;
  335. TileSetSourceIndex = -1;
  336. SetTextureData(null);
  337. }
  338. //派发选择资源事件
  339. EventManager.EmitEvent(EventEnum.OnSelectTileSetSource, TileSetSourceInfo);
  340. }
  341.  
  342. //保存
  343. private void OnSaveClick()
  344. {
  345. TileSetSplit.SetTileSetInfo(TileSetInfo.Clone());
  346. EventManager.EmitEvent(EventEnum.OnTileSetSave, TileSetSplit);
  347. IsDirty = false;
  348. RefreshTitle();
  349. }
  350. //数据脏了
  351. private void OnTileSetDirty(object obj)
  352. {
  353. IsDirty = true;
  354. RefreshTitle();
  355. }
  356. }