Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditorCombination / leftTop / TileEditCombination.cs
@小李xl 小李xl on 6 Jan 2024 10 KB 保存TileSet数据完成
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Godot;
  5.  
  6. namespace UI.TileSetEditorCombination;
  7.  
  8. public partial class TileEditCombination : EditorGridBg<TileSetEditorCombination.LeftTopBg>
  9. {
  10. // ------------------------------- 笔刷相关 -------------------------------
  11. //笔刷数据, kay: 代表原图中的坐标, 单位: 格
  12. private Dictionary<Vector2I, CombinationCell> _brushData = new Dictionary<Vector2I, CombinationCell>();
  13.  
  14. //笔刷偏移, 单位: 像素
  15. private Vector2I _brushOffset = Vector2I.Zero;
  16. //上一帧笔刷位置
  17. private Vector2 _brushPrevPos = Vector2.Zero;
  18. private bool _initBrush = false;
  19. private bool _drawBrushFlag = false;
  20. private int _xStart = int.MaxValue;
  21. private int _yStart = int.MaxValue;
  22. private int _xEnd = int.MinValue;
  23. private int _yEnd = int.MinValue;
  24. // -----------------------------------------------------------------------
  25. // ------------------------------- 画布相关 -------------------------------
  26. //画布数据, kay: 绘制坐标, 单位: 像素
  27. private Dictionary<Vector2I, CombinationCell> _canvas = new Dictionary<Vector2I, CombinationCell>();
  28. //画布数据是否需要更新
  29. private bool _canvasDirty = false;
  30. // -----------------------------------------------------------------------
  31.  
  32. public override void SetUiNode(IUiNode uiNode)
  33. {
  34. base.SetUiNode(uiNode);
  35. UiNode.L_CombinationRoot.L_RectBrush.Instance.Root = UiNode.L_CombinationRoot.Instance;
  36. InitNode(UiNode.L_CombinationRoot.Instance, UiNode.L_Grid.Instance);
  37.  
  38. UiNode.UiPanel.AddEventListener(EventEnum.OnSelectCombinationCell, OnSelectCombinationCell);
  39. UiNode.UiPanel.AddEventListener(EventEnum.OnRemoveCombinationCell, OnRemoveCombinationCell);
  40. UiNode.UiPanel.AddEventListener(EventEnum.OnClearCombinationCell, OnClearCombinationCell);
  41.  
  42. //删除按钮点击事件
  43. UiNode.L_DeleteBtn.Instance.Pressed += OnDeleteClick;
  44. //聚焦按钮点击事件
  45. UiNode.L_FocusBtn.Instance.Pressed += OnFocusClick;
  46. //导入组合按钮点击事件
  47. UiNode.L_ImportBtn.Instance.Pressed += OnImportClick;
  48. }
  49.  
  50. public override void _Process(double delta)
  51. {
  52. if (!UiNode.UiPanel.IsOpen)
  53. {
  54. return;
  55. }
  56.  
  57. var brushRoot = UiNode.L_CombinationRoot.L_BrushRoot.Instance;
  58. var combinationRoot = UiNode.L_CombinationRoot.Instance;
  59. brushRoot.Position = combinationRoot.GetLocalMousePosition().FloorAdsorption(GameConfig.TileCellSizeVector2I) + _brushOffset;
  60.  
  61. if (_canvasDirty) //更新画布范围
  62. {
  63. _canvasDirty = false;
  64. if (_canvas.Count > 0)
  65. {
  66. var rect = Utils.CalcTileRect(_canvas.Keys);
  67. UiNode.L_CombinationRoot.L_RectBrush.Instance.SetDrawRect(
  68. rect.Position.X,
  69. rect.Position.Y,
  70. rect.Size.X,
  71. rect.Size.Y
  72. );
  73. }
  74. else
  75. {
  76. UiNode.L_CombinationRoot.L_RectBrush.Instance.ClearDrawRect();
  77. }
  78. }
  79. }
  80.  
  81. public override void _GuiInput(InputEvent @event)
  82. {
  83. base._GuiInput(@event);
  84. if (@event is InputEventMouse)
  85. {
  86. AcceptEvent();
  87. var brushRoot = UiNode.L_CombinationRoot.L_BrushRoot.Instance;
  88. var newPos = brushRoot.Position;
  89. sbyte flag = 0;
  90. //左键按下开始绘制
  91. if (_initBrush)
  92. {
  93. if (Input.IsMouseButtonPressed(MouseButton.Left)) //绘制
  94. {
  95. flag = 1;
  96. if (_brushPrevPos != newPos || !_drawBrushFlag)
  97. {
  98. _drawBrushFlag = true;
  99. _brushPrevPos = newPos;
  100. DrawBrush();
  101. }
  102. }
  103. else if (Input.IsMouseButtonPressed(MouseButton.Right)) //擦除
  104. {
  105. flag = -1;
  106. brushRoot.Modulate = new Color(1, 1, 1, 0.3f);
  107. if (_brushPrevPos != newPos || !_drawBrushFlag)
  108. {
  109. _drawBrushFlag = true;
  110. _brushPrevPos = newPos;
  111. EraseBrush();
  112. }
  113. }
  114. }
  115.  
  116. if (flag != 0)
  117. {
  118. _drawBrushFlag = false;
  119. }
  120.  
  121. if (flag != -1)
  122. {
  123. brushRoot.Modulate = Colors.White;
  124. }
  125. }
  126. }
  127.  
  128. //删除所有图块
  129. private void OnDeleteClick()
  130. {
  131. foreach (var keyValuePair in _canvas)
  132. {
  133. keyValuePair.Value.QueueFree();
  134. }
  135.  
  136. _canvas.Clear();
  137. _canvasDirty = true;
  138. }
  139. //点击聚焦按钮
  140. private void OnFocusClick()
  141. {
  142. Utils.DoFocusNode(ContainerRoot, Size, UiNode.L_CombinationRoot.L_RectBrush.Instance.GetCenterPosition() * 2);
  143. RefreshGridTrans();
  144. }
  145. //导入按钮点击
  146. private void OnImportClick()
  147. {
  148. var size = UiNode.L_CombinationRoot.L_RectBrush.Instance.GetRectSize();
  149. if (size == Vector2.Zero)
  150. {
  151. EditorWindowManager.ShowTips("警告", "请先绘制组合图块!");
  152. return;
  153. }
  154. else if (size == GameConfig.TileCellSizeVector2I)
  155. {
  156. EditorWindowManager.ShowTips("警告", "导入一格大小的组合图块没有任何意义!");
  157. return;
  158. }
  159. var originPos = UiNode.L_CombinationRoot.L_RectBrush.Instance.GetOriginPosition();
  160. var tempCell = new List<SerializeVector2>();
  161. var tempPos = new List<SerializeVector2>();
  162. foreach (var keyValuePair in _canvas)
  163. {
  164. var pos = keyValuePair.Key;
  165. var srcRect = keyValuePair.Value.RegionRect;
  166. tempCell.Add(new SerializeVector2(srcRect.Position.AsVector2I()));
  167. tempPos.Add(new SerializeVector2(pos - originPos));
  168. }
  169.  
  170. var cells = tempCell.ToArray();
  171. var positions = tempPos.ToArray();
  172.  
  173. var tileSetEditorPanel = UiNode.UiPanel.EditorPanel;
  174. var color = tileSetEditorPanel.BgColor;
  175. var src = tileSetEditorPanel.TextureImage;
  176. var image = ImportCombinationData.GetPreviewTexture(src, cells, positions);
  177. var texture = ImageTexture.CreateFromImage(image);
  178. EditorWindowManager.ShowImportCombination("组合", color, texture, (name) =>
  179. {
  180. var combinationInfo = new TileCombinationInfo();
  181. combinationInfo.Id = DateTime.UtcNow.Ticks.ToString();
  182. combinationInfo.Name = name;
  183. combinationInfo.Cells = cells;
  184. combinationInfo.Positions = positions;
  185. var data = new ImportCombinationData(texture, combinationInfo);
  186. //派发导入组合图块事件
  187. EventManager.EmitEvent(EventEnum.OnImportCombination, data);
  188. }, () => //取消添加组件
  189. {
  190. image.Dispose();
  191. texture.Dispose();
  192. });
  193. }
  194. //绘制笔刷
  195. private void DrawBrush()
  196. {
  197. var brushRoot = UiNode.L_CombinationRoot.L_BrushRoot.Instance;
  198. foreach (var keyValuePair in _brushData)
  199. {
  200. var combinationCell = keyValuePair.Value;
  201. var pos = (combinationCell.Position + brushRoot.Position).AsVector2I();
  202. if (_canvas.TryGetValue(pos, out var canvasCell))
  203. {
  204. canvasCell.CloneFrom(combinationCell);
  205. }
  206. else
  207. {
  208. canvasCell = (CombinationCell)combinationCell.Duplicate();
  209. canvasCell.Position = pos;
  210. UiNode.L_CombinationRoot.L_CanvasRoot.AddChild(canvasCell);
  211. _canvas.Add(pos, canvasCell);
  212. _canvasDirty = true;
  213. }
  214. }
  215. }
  216.  
  217. //擦除笔刷
  218. private void EraseBrush()
  219. {
  220. var brushRoot = UiNode.L_CombinationRoot.L_BrushRoot.Instance;
  221. foreach (var keyValuePair in _brushData)
  222. {
  223. var combinationCell = keyValuePair.Value;
  224. var pos = (combinationCell.Position + brushRoot.Position).AsVector2I();
  225. if (_canvas.TryGetValue(pos, out var canvasCell))
  226. {
  227. canvasCell.QueueFree();
  228. _canvas.Remove(pos);
  229. _canvasDirty = true;
  230. }
  231. }
  232. }
  233.  
  234. //选中组合的图块
  235. private void OnSelectCombinationCell(object obj)
  236. {
  237. if (obj is Vector2I cell && !_brushData.ContainsKey(cell))
  238. {
  239. _initBrush = true;
  240. var cellData = new CombinationCell();
  241. cellData.Position = cell * GameConfig.TileCellSize;
  242. cellData.InitData(UiNode.UiPanel.EditorPanel.Texture, cell);
  243. UiNode.L_CombinationRoot.L_BrushRoot.AddChild(cellData);
  244. _brushData.Add(cell, cellData);
  245. //计算起始点和终点
  246. _xStart = Mathf.Min(cell.X, _xStart);
  247. _yStart = Mathf.Min(cell.Y, _yStart);
  248. _xEnd = Mathf.Max(cell.X, _xEnd);
  249. _yEnd = Mathf.Max(cell.Y, _yEnd);
  250. _brushOffset = new Vector2I(-(_xStart + (_xEnd - _xStart) / 2), -(_yStart + (_yEnd - _yStart) / 2)) * GameConfig.TileCellSize;
  251. }
  252. }
  253. //移除组合图块
  254. private void OnRemoveCombinationCell(object obj)
  255. {
  256. if (obj is Vector2I cell)
  257. {
  258. if (_brushData.TryGetValue(cell, out var cellData))
  259. {
  260. cellData.QueueFree();
  261. _brushData.Remove(cell);
  262. }
  263. }
  264. }
  265.  
  266. //移除所有组合图块
  267. private void OnClearCombinationCell(object obj)
  268. {
  269. foreach (var keyValuePair in _brushData)
  270. {
  271. keyValuePair.Value.QueueFree();
  272. }
  273. _brushData.Clear();
  274. _initBrush = false;
  275. _brushOffset = Vector2I.Zero;
  276. _xStart = int.MaxValue;
  277. _yStart = int.MaxValue;
  278. _xEnd = int.MinValue;
  279. _yEnd = int.MinValue;
  280. }
  281. }