Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditorCombination / leftTop / TileEditCombination.cs
@小李xl 小李xl on 18 Dec 2023 9 KB 导入组合, 开发中
  1. using System;
  2. using System.Collections.Generic;
  3. using Godot;
  4.  
  5. namespace UI.TileSetEditorCombination;
  6.  
  7. public partial class TileEditCombination : GridBg<TileSetEditorCombination.LeftTopBg>
  8. {
  9. // ------------------------------- 笔刷相关 -------------------------------
  10. //笔刷数据, kay: 代表原图中的坐标, 单位: 格
  11. private Dictionary<Vector2I, CombinationCell> _brushData = new Dictionary<Vector2I, CombinationCell>();
  12.  
  13. //笔刷偏移, 单位: 像素
  14. private Vector2I _brushOffset = Vector2I.Zero;
  15. //上一帧笔刷位置
  16. private Vector2 _brushPrevPos = Vector2.Zero;
  17. private bool _initBrush = false;
  18. private bool _drawBrushFlag = false;
  19. private int _xStart = int.MaxValue;
  20. private int _yStart = int.MaxValue;
  21. private int _xEnd = int.MinValue;
  22. private int _yEnd = int.MinValue;
  23. // -----------------------------------------------------------------------
  24. // ------------------------------- 画布相关 -------------------------------
  25. //画布数据, kay: 绘制坐标, 单位: 像素
  26. private Dictionary<Vector2I, CombinationCell> _canvas = new Dictionary<Vector2I, CombinationCell>();
  27. //画布数据是否需要更新
  28. private bool _canvasDirty = false;
  29. // -----------------------------------------------------------------------
  30.  
  31. public override void SetUiNode(IUiNode uiNode)
  32. {
  33. base.SetUiNode(uiNode);
  34. UiNode.L_CombinationRoot.L_RectBrush.Instance.Root = UiNode.L_CombinationRoot.Instance;
  35. Grid = UiNode.L_Grid.Instance;
  36. ContainerRoot = UiNode.L_CombinationRoot.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. //单位: 像素
  67. var canvasXStart = int.MaxValue;
  68. var canvasYStart = int.MaxValue;
  69. var canvasXEnd = int.MinValue;
  70. var canvasYEnd = int.MinValue;
  71.  
  72. foreach (var keyValuePair in _canvas)
  73. {
  74. var pos = keyValuePair.Key;
  75. canvasXStart = Mathf.Min(pos.X, canvasXStart);
  76. canvasYStart = Mathf.Min(pos.Y, canvasYStart);
  77. canvasXEnd = Mathf.Max(pos.X + GameConfig.TileCellSize, canvasXEnd);
  78. canvasYEnd = Mathf.Max(pos.Y + GameConfig.TileCellSize, canvasYEnd);
  79. }
  80. UiNode.L_CombinationRoot.L_RectBrush.Instance.SetDrawRect(
  81. canvasXStart,
  82. canvasYStart,
  83. canvasXEnd - canvasXStart,
  84. canvasYEnd - canvasYStart
  85. );
  86. }
  87. else
  88. {
  89. UiNode.L_CombinationRoot.L_RectBrush.Instance.ClearDrawRect();
  90. }
  91. }
  92. }
  93.  
  94. public override void _GuiInput(InputEvent @event)
  95. {
  96. base._GuiInput(@event);
  97. if (@event is InputEventMouse)
  98. {
  99. AcceptEvent();
  100. var brushRoot = UiNode.L_CombinationRoot.L_BrushRoot.Instance;
  101. var newPos = brushRoot.Position;
  102. sbyte flag = 0;
  103. //左键按下开始绘制
  104. if (_initBrush)
  105. {
  106. if (Input.IsMouseButtonPressed(MouseButton.Left)) //绘制
  107. {
  108. flag = 1;
  109. if (_brushPrevPos != newPos || !_drawBrushFlag)
  110. {
  111. _drawBrushFlag = true;
  112. _brushPrevPos = newPos;
  113. DrawBrush();
  114. }
  115. }
  116. else if (Input.IsMouseButtonPressed(MouseButton.Right)) //擦除
  117. {
  118. flag = -1;
  119. brushRoot.Modulate = new Color(1, 1, 1, 0.3f);
  120. if (_brushPrevPos != newPos || !_drawBrushFlag)
  121. {
  122. _drawBrushFlag = true;
  123. _brushPrevPos = newPos;
  124. EraseBrush();
  125. }
  126. }
  127. }
  128.  
  129. if (flag != 0)
  130. {
  131. _drawBrushFlag = false;
  132. }
  133.  
  134. if (flag != -1)
  135. {
  136. brushRoot.Modulate = Colors.White;
  137. }
  138. }
  139. }
  140.  
  141. //删除所有图块
  142. private void OnDeleteClick()
  143. {
  144. foreach (var keyValuePair in _canvas)
  145. {
  146. keyValuePair.Value.QueueFree();
  147. }
  148.  
  149. _canvas.Clear();
  150. _canvasDirty = true;
  151. }
  152. //点击聚焦按钮
  153. private void OnFocusClick()
  154. {
  155. var pos = Size / 2;
  156. var center = UiNode.L_CombinationRoot.L_RectBrush.Instance.GetCenterPosition();
  157. ContainerRoot.Position = pos - center * ContainerRoot.Scale;
  158. RefreshGridTrans();
  159. }
  160. //导入按钮点击
  161. private void OnImportClick()
  162. {
  163. var size = UiNode.L_CombinationRoot.L_RectBrush.Instance.GetRectSize();
  164. if (size == Vector2.Zero)
  165. {
  166. EditorWindowManager.ShowTips("警告", "请先绘制组合图块!");
  167. return;
  168. }
  169. else if (size == GameConfig.TileCellSizeVector2I)
  170. {
  171. EditorWindowManager.ShowTips("警告", "导入一格大小的组合图块没有任何意义!");
  172. return;
  173. }
  174. }
  175. //绘制笔刷
  176. private void DrawBrush()
  177. {
  178. var brushRoot = UiNode.L_CombinationRoot.L_BrushRoot.Instance;
  179. foreach (var keyValuePair in _brushData)
  180. {
  181. var combinationCell = keyValuePair.Value;
  182. var pos = (combinationCell.Position + brushRoot.Position).AsVector2I();
  183. if (_canvas.TryGetValue(pos, out var canvasCell))
  184. {
  185. canvasCell.CloneFrom(combinationCell);
  186. }
  187. else
  188. {
  189. canvasCell = (CombinationCell)combinationCell.Duplicate();
  190. canvasCell.Position = pos;
  191. UiNode.L_CombinationRoot.L_CanvasRoot.AddChild(canvasCell);
  192. _canvas.Add(pos, canvasCell);
  193. _canvasDirty = true;
  194. }
  195. }
  196. }
  197.  
  198. //擦除笔刷
  199. private void EraseBrush()
  200. {
  201. var brushRoot = UiNode.L_CombinationRoot.L_BrushRoot.Instance;
  202. foreach (var keyValuePair in _brushData)
  203. {
  204. var combinationCell = keyValuePair.Value;
  205. var pos = (combinationCell.Position + brushRoot.Position).AsVector2I();
  206. if (_canvas.TryGetValue(pos, out var canvasCell))
  207. {
  208. canvasCell.QueueFree();
  209. _canvas.Remove(pos);
  210. _canvasDirty = true;
  211. }
  212. }
  213. }
  214.  
  215. //选中组合的图块
  216. private void OnSelectCombinationCell(object obj)
  217. {
  218. if (obj is Vector2I cell && !_brushData.ContainsKey(cell))
  219. {
  220. _initBrush = true;
  221. var cellData = new CombinationCell();
  222. cellData.Position = cell * GameConfig.TileCellSize;
  223. cellData.InitData(UiNode.UiPanel.EditorPanel.Texture, cell);
  224. UiNode.L_CombinationRoot.L_BrushRoot.AddChild(cellData);
  225. _brushData.Add(cell, cellData);
  226. //计算起始点和终点
  227. _xStart = Mathf.Min(cell.X, _xStart);
  228. _yStart = Mathf.Min(cell.Y, _yStart);
  229. _xEnd = Mathf.Max(cell.X, _xEnd);
  230. _yEnd = Mathf.Max(cell.Y, _yEnd);
  231. _brushOffset = new Vector2I(-(_xStart + (_xEnd - _xStart) / 2), -(_yStart + (_yEnd - _yStart) / 2)) * GameConfig.TileCellSize;
  232. }
  233. }
  234. //移除组合图块
  235. private void OnRemoveCombinationCell(object obj)
  236. {
  237. if (obj is Vector2I cell)
  238. {
  239. if (_brushData.TryGetValue(cell, out var cellData))
  240. {
  241. cellData.QueueFree();
  242. _brushData.Remove(cell);
  243. }
  244. }
  245. }
  246.  
  247. //移除所有组合图块
  248. private void OnClearCombinationCell(object obj)
  249. {
  250. foreach (var keyValuePair in _brushData)
  251. {
  252. keyValuePair.Value.QueueFree();
  253. }
  254. _brushData.Clear();
  255. _initBrush = false;
  256. _brushOffset = Vector2I.Zero;
  257. _xStart = int.MaxValue;
  258. _yStart = int.MaxValue;
  259. _xEnd = int.MinValue;
  260. _yEnd = int.MinValue;
  261. }
  262. }