Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditor / TileSetEditorPanel.cs
  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. /// <summary>
  115. /// 初始化数据
  116. /// </summary>
  117. public void InitData(TileSetSplit tileSetSplit)
  118. {
  119. IsDirty = false;
  120. tileSetSplit.ReloadTileSetInfo();
  121. TileSetSplit = tileSetSplit;
  122. TileSetInfo = tileSetSplit.TileSetInfo.Clone();
  123. RefreshTitle();
  124. //初始化下拉框
  125. if (TileSetInfo.Sources.Count > 0)
  126. {
  127. var optionButton = S_OptionButton.Instance;
  128. foreach (var tileSetSourceInfo in TileSetInfo.Sources)
  129. {
  130. optionButton.AddItem(tileSetSourceInfo.Name);
  131. }
  132.  
  133. optionButton.Selected = 0;
  134. OnOptionChange(0);
  135. }
  136. }
  137.  
  138. //设置标题显示内容
  139. private void RefreshTitle()
  140. {
  141. if (IsDirty)
  142. {
  143. S_Title.Instance.Text = "正在编辑:" + TileSetInfo.Name + "*";
  144. }
  145. else
  146. {
  147. S_Title.Instance.Text = "正在编辑:" + TileSetInfo.Name;
  148. }
  149. }
  150.  
  151. /// <summary>
  152. /// 设置纹理使用的纹理
  153. /// </summary>
  154. public void SetTextureData(Image image)
  155. {
  156. if (image == null)
  157. {
  158. InitTexture = false;
  159. TextureImage = null;
  160. Texture.SetImage(_emptyImage);
  161. CellHorizontal = 0;
  162. CellVertical = 0;
  163. }
  164. else
  165. {
  166. if (TileSetSourceInfo == null)
  167. {
  168. return;
  169. }
  170. InitTexture = true;
  171. Texture.SetImage(image);
  172. TextureImage = image;
  173. CellHorizontal = image.GetWidth() / GameConfig.TileCellSize;
  174. CellVertical = image.GetHeight() / GameConfig.TileCellSize;
  175. }
  176. //派发事件
  177. EventManager.EmitEvent(EventEnum.OnSetTileTexture, Texture);
  178. }
  179.  
  180. /// <summary>
  181. /// 设置背景颜色
  182. /// </summary>
  183. public void SetBgColor(Color color)
  184. {
  185. BgColor = color;
  186. //派发事件
  187. EventManager.EmitEvent(EventEnum.OnSetTileSetBgColor, color);
  188. }
  189. /// <summary>
  190. /// 将二维位置转换为索引的函数
  191. /// </summary>
  192. public int CellPositionToIndex(Vector2I pos)
  193. {
  194. return pos.Y * CellHorizontal + pos.X;
  195. }
  196. /// <summary>
  197. /// 返回Cell的坐标是否在纹理区域内
  198. /// </summary>
  199. public bool IsCellPositionInTexture(Vector2I cell)
  200. {
  201. return cell.X >= 0 && cell.Y >= 0 && cell.X < CellHorizontal && cell.Y < CellVertical;
  202. }
  203.  
  204. //返回上一级按钮点击
  205. private void OnBackClick()
  206. {
  207. if (IsDirty)
  208. {
  209. EditorWindowManager.ShowConfirm("提示", "当前TileSet修改的数据还未保存,是否退出编辑?",
  210. "保存并退出", "直接退出", "取消"
  211. , index =>
  212. {
  213. if (index == 0) //保存并退出
  214. {
  215. OnSaveClick();
  216. OpenPrevUi();
  217. }
  218. else if (index == 1) //直接退出
  219. {
  220. OpenPrevUi();
  221. }
  222. }
  223. );
  224. }
  225. else
  226. {
  227. OpenPrevUi();
  228. }
  229. }
  230.  
  231. //删除资源
  232. private void OnDeleteSourceClick()
  233. {
  234. var optionButton = S_OptionButton.Instance;
  235. var selectIndex = optionButton.Selected;
  236. if (selectIndex == 0)
  237. {
  238. EditorWindowManager.ShowTips("警告", "不允许删除 Main Source!");
  239. return;
  240. }
  241. else if (selectIndex > 0)
  242. {
  243. EditorWindowManager.ShowConfirm("提示", "是否需要删除该资源!", v =>
  244. {
  245. if (v)
  246. {
  247. var name = optionButton.GetItemText(selectIndex);
  248. var findIndex = TileSetInfo.Sources.FindIndex(info => info.Name == name);
  249. if (findIndex >= 0)
  250. {
  251. TileSetInfo.Sources[findIndex].Destroy();
  252. TileSetInfo.Sources.RemoveAt(findIndex);
  253. }
  254.  
  255. var index = optionButton.ItemCount - 2;
  256. optionButton.RemoveItem(selectIndex);
  257. optionButton.Selected = index;
  258. OnOptionChange(index);
  259. }
  260. });
  261. }
  262. else
  263. {
  264. EditorWindowManager.ShowTips("提示", "请选择需要删除的资源!");
  265. }
  266. }
  267.  
  268. //创建资源
  269. private void OnAddSourceClick()
  270. {
  271. EditorWindowManager.ShowInput("创建资源", "资源名称:", null, (value, isSubmit) =>
  272. {
  273. if (isSubmit)
  274. {
  275. if (TileSetInfo.Sources.FindIndex(info => info.Name == value) >= 0)
  276. {
  277. EditorWindowManager.ShowTips("错误", "该资源名称已存在!");
  278. return false;
  279. }
  280. var source = new TileSetSourceInfo();
  281. source.InitData();
  282. source.Name = value;
  283. TileSetInfo.Sources.Add(source);
  284. EventManager.EmitEvent(EventEnum.OnCreateTileSetSource, source);
  285. var optionButton = S_OptionButton.Instance;
  286. optionButton.AddItem(value);
  287. var selectIndex = optionButton.ItemCount - 1;
  288. optionButton.Selected = selectIndex;
  289. OnOptionChange(selectIndex);
  290. }
  291.  
  292. return true;
  293. });
  294. }
  295.  
  296. //选中资源
  297. private void OnOptionChange(long index)
  298. {
  299. if (index >= 0)
  300. {
  301. TabGrid.Visible = true;
  302. TabGrid.SelectIndex = 0;
  303. TileSetSourceInfo = TileSetInfo.Sources[(int)index];
  304. TileSetSourceIndex = (int)index;
  305. SetTextureData(TileSetSourceInfo.GetSourceImage());
  306. }
  307. else
  308. {
  309. TabGrid.Visible = false;
  310. TabGrid.SelectIndex = -1;
  311. TileSetSourceInfo = null;
  312. TileSetSourceIndex = -1;
  313. SetTextureData(null);
  314. }
  315. //派发选择资源事件
  316. EventManager.EmitEvent(EventEnum.OnSelectTileSetSource, TileSetSourceInfo);
  317. }
  318.  
  319. //保存
  320. private void OnSaveClick()
  321. {
  322. TileSetSplit.SetTileSetInfo(TileSetInfo.Clone());
  323. EventManager.EmitEvent(EventEnum.OnTileSetSave, TileSetSplit);
  324. IsDirty = false;
  325. RefreshTitle();
  326. }
  327. //数据脏了
  328. private void OnTileSetDirty(object obj)
  329. {
  330. IsDirty = true;
  331. RefreshTitle();
  332. }
  333. }