Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditorCombination / leftTop / TileEditCombination.cs
  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 : GridBg<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. Grid = UiNode.L_Grid.Instance;
  37. ContainerRoot = UiNode.L_CombinationRoot.Instance;
  38.  
  39. UiNode.UiPanel.AddEventListener(EventEnum.OnSelectCombinationCell, OnSelectCombinationCell);
  40. UiNode.UiPanel.AddEventListener(EventEnum.OnRemoveCombinationCell, OnRemoveCombinationCell);
  41. UiNode.UiPanel.AddEventListener(EventEnum.OnClearCombinationCell, OnClearCombinationCell);
  42.  
  43. //删除按钮点击事件
  44. UiNode.L_DeleteBtn.Instance.Pressed += OnDeleteClick;
  45. //聚焦按钮点击事件
  46. UiNode.L_FocusBtn.Instance.Pressed += OnFocusClick;
  47. //导入组合按钮点击事件
  48. UiNode.L_ImportBtn.Instance.Pressed += OnImportClick;
  49. }
  50.  
  51. public override void _Process(double delta)
  52. {
  53. if (!UiNode.UiPanel.IsOpen)
  54. {
  55. return;
  56. }
  57.  
  58. var brushRoot = UiNode.L_CombinationRoot.L_BrushRoot.Instance;
  59. var combinationRoot = UiNode.L_CombinationRoot.Instance;
  60. brushRoot.Position = combinationRoot.GetLocalMousePosition().FloorAdsorption(GameConfig.TileCellSizeVector2I) + _brushOffset;
  61.  
  62. if (_canvasDirty) //更新画布范围
  63. {
  64. _canvasDirty = false;
  65. if (_canvas.Count > 0)
  66. {
  67. var rect = Utils.CalcTileRect(_canvas.Keys);
  68. UiNode.L_CombinationRoot.L_RectBrush.Instance.SetDrawRect(
  69. rect.Position.X,
  70. rect.Position.Y,
  71. rect.Size.X,
  72. rect.Size.Y
  73. );
  74. }
  75. else
  76. {
  77. UiNode.L_CombinationRoot.L_RectBrush.Instance.ClearDrawRect();
  78. }
  79. }
  80. }
  81.  
  82. public override void _GuiInput(InputEvent @event)
  83. {
  84. base._GuiInput(@event);
  85. if (@event is InputEventMouse)
  86. {
  87. AcceptEvent();
  88. var brushRoot = UiNode.L_CombinationRoot.L_BrushRoot.Instance;
  89. var newPos = brushRoot.Position;
  90. sbyte flag = 0;
  91. //左键按下开始绘制
  92. if (_initBrush)
  93. {
  94. if (Input.IsMouseButtonPressed(MouseButton.Left)) //绘制
  95. {
  96. flag = 1;
  97. if (_brushPrevPos != newPos || !_drawBrushFlag)
  98. {
  99. _drawBrushFlag = true;
  100. _brushPrevPos = newPos;
  101. DrawBrush();
  102. }
  103. }
  104. else if (Input.IsMouseButtonPressed(MouseButton.Right)) //擦除
  105. {
  106. flag = -1;
  107. brushRoot.Modulate = new Color(1, 1, 1, 0.3f);
  108. if (_brushPrevPos != newPos || !_drawBrushFlag)
  109. {
  110. _drawBrushFlag = true;
  111. _brushPrevPos = newPos;
  112. EraseBrush();
  113. }
  114. }
  115. }
  116.  
  117. if (flag != 0)
  118. {
  119. _drawBrushFlag = false;
  120. }
  121.  
  122. if (flag != -1)
  123. {
  124. brushRoot.Modulate = Colors.White;
  125. }
  126. }
  127. }
  128.  
  129. //删除所有图块
  130. private void OnDeleteClick()
  131. {
  132. foreach (var keyValuePair in _canvas)
  133. {
  134. keyValuePair.Value.QueueFree();
  135. }
  136.  
  137. _canvas.Clear();
  138. _canvasDirty = true;
  139. }
  140. //点击聚焦按钮
  141. private void OnFocusClick()
  142. {
  143. var pos = Size / 2;
  144. var center = UiNode.L_CombinationRoot.L_RectBrush.Instance.GetCenterPosition();
  145. ContainerRoot.Position = pos - center * ContainerRoot.Scale;
  146. RefreshGridTrans();
  147. }
  148. //导入按钮点击
  149. private void OnImportClick()
  150. {
  151. var size = UiNode.L_CombinationRoot.L_RectBrush.Instance.GetRectSize();
  152. if (size == Vector2.Zero)
  153. {
  154. EditorWindowManager.ShowTips("警告", "请先绘制组合图块!");
  155. return;
  156. }
  157. else if (size == GameConfig.TileCellSizeVector2I)
  158. {
  159. EditorWindowManager.ShowTips("警告", "导入一格大小的组合图块没有任何意义!");
  160. return;
  161. }
  162. var originPos = UiNode.L_CombinationRoot.L_RectBrush.Instance.GetOriginPosition();
  163. var tempCell = new List<SerializeVector2>();
  164. var tempPos = new List<SerializeVector2>();
  165. foreach (var keyValuePair in _canvas)
  166. {
  167. var pos = keyValuePair.Key;
  168. var srcRect = keyValuePair.Value.RegionRect;
  169. tempCell.Add(new SerializeVector2(srcRect.Position.AsVector2I()));
  170. tempPos.Add(new SerializeVector2(pos - originPos));
  171. }
  172.  
  173. var cells = tempCell.ToArray();
  174. var positions = tempPos.ToArray();
  175. var src = UiNode.UiPanel.EditorPanel.TextureImage;
  176. var image = ImportCombinationData.GetPreviewTexture(src, cells, positions);
  177. var texture = ImageTexture.CreateFromImage(image);
  178. EditorWindowManager.ShowImportCombination("组合名称", texture, (name) =>
  179. {
  180. var combinationInfo = new TileCombinationInfo();
  181. combinationInfo.Id = "1";
  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. }