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