Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditorCombination / leftTop / TileEditCombination.cs
@小李xl 小李xl on 24 Jan 2024 10 KB 代码调整
  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
  9. {
  10. public new TileSetEditorCombination.LeftTopBg UiNode => (TileSetEditorCombination.LeftTopBg)base.UiNode;
  11. // ------------------------------- 笔刷相关 -------------------------------
  12. //笔刷数据, kay: 代表原图中的坐标, 单位: 格
  13. private Dictionary<Vector2I, CombinationCell> _brushData = new Dictionary<Vector2I, CombinationCell>();
  14.  
  15. //笔刷偏移, 单位: 像素
  16. private Vector2I _brushOffset = Vector2I.Zero;
  17. //上一帧笔刷位置
  18. private Vector2 _brushPrevPos = Vector2.Zero;
  19. private bool _initBrush = false;
  20. private bool _drawBrushFlag = false;
  21. private int _xStart = int.MaxValue;
  22. private int _yStart = int.MaxValue;
  23. private int _xEnd = int.MinValue;
  24. private int _yEnd = int.MinValue;
  25. // -----------------------------------------------------------------------
  26. // ------------------------------- 画布相关 -------------------------------
  27. //画布数据, kay: 绘制坐标, 单位: 像素
  28. private Dictionary<Vector2I, CombinationCell> _canvas = new Dictionary<Vector2I, CombinationCell>();
  29. //画布数据是否需要更新
  30. private bool _canvasDirty = false;
  31. // -----------------------------------------------------------------------
  32.  
  33. public override void SetUiNode(IUiNode uiNode)
  34. {
  35. base.SetUiNode(uiNode);
  36. UiNode.L_CombinationRoot.L_RectBrush.Instance.Root = UiNode.L_CombinationRoot.Instance;
  37. InitNode(UiNode.L_CombinationRoot.Instance, UiNode.L_Grid.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 += ClearAllCell;
  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. /// <summary>
  130. /// 删除已经绘制的图块
  131. /// </summary>
  132. public void ClearAllCell()
  133. {
  134. foreach (var keyValuePair in _canvas)
  135. {
  136. keyValuePair.Value.QueueFree();
  137. }
  138.  
  139. _canvas.Clear();
  140. _canvasDirty = true;
  141. }
  142. //点击聚焦按钮
  143. private void OnFocusClick()
  144. {
  145. Utils.DoFocusNode(ContainerRoot, Size, UiNode.L_CombinationRoot.L_RectBrush.Instance.GetCenterPosition() * 2);
  146. RefreshGridTrans();
  147. }
  148. //导入按钮点击
  149. private void OnImportClick()
  150. {
  151. var size = UiNode.L_CombinationRoot.L_RectBrush.Instance.GetRectSize();
  152. if (UiNode.UiPanel.EditorPanel.TextureImage == null)
  153. {
  154. EditorWindowManager.ShowTips("警告", "未选择纹理资源!");
  155. return;
  156. }
  157. else if (size == Vector2.Zero)
  158. {
  159. EditorWindowManager.ShowTips("警告", "请先绘制组合图块!");
  160. return;
  161. }
  162. else if (size == GameConfig.TileCellSizeVector2I)
  163. {
  164. EditorWindowManager.ShowTips("警告", "导入一格大小的组合图块没有任何意义!");
  165. return;
  166. }
  167. var originPos = UiNode.L_CombinationRoot.L_RectBrush.Instance.GetOriginPosition();
  168. var tempCell = new List<SerializeVector2>();
  169. var tempPos = new List<SerializeVector2>();
  170. foreach (var keyValuePair in _canvas)
  171. {
  172. var pos = keyValuePair.Key;
  173. var srcRect = keyValuePair.Value.RegionRect;
  174. tempCell.Add(new SerializeVector2(srcRect.Position.AsVector2I()));
  175. tempPos.Add(new SerializeVector2(pos - originPos));
  176. }
  177.  
  178. var cells = tempCell.ToArray();
  179. var positions = tempPos.ToArray();
  180.  
  181. var tileSetEditorPanel = UiNode.UiPanel.EditorPanel;
  182. var color = tileSetEditorPanel.BgColor;
  183. var src = tileSetEditorPanel.TextureImage;
  184. var image = ImportCombinationData.GetPreviewTexture(src, cells, positions);
  185. var texture = ImageTexture.CreateFromImage(image);
  186. EditorWindowManager.ShowImportCombination("组合", color, texture, (name) =>
  187. {
  188. var combinationInfo = new TileCombinationInfo();
  189. combinationInfo.Id = DateTime.UtcNow.Ticks.ToString();
  190. combinationInfo.Name = name;
  191. combinationInfo.Cells = cells;
  192. combinationInfo.Positions = positions;
  193. var data = new ImportCombinationData(texture, combinationInfo);
  194. //派发导入组合图块事件
  195. EventManager.EmitEvent(EventEnum.OnImportCombination, data);
  196. }, () => //取消添加组件
  197. {
  198. image.Dispose();
  199. texture.Dispose();
  200. });
  201. }
  202. //绘制笔刷
  203. private void DrawBrush()
  204. {
  205. var brushRoot = UiNode.L_CombinationRoot.L_BrushRoot.Instance;
  206. foreach (var keyValuePair in _brushData)
  207. {
  208. var combinationCell = keyValuePair.Value;
  209. var pos = (combinationCell.Position + brushRoot.Position).AsVector2I();
  210. if (_canvas.TryGetValue(pos, out var canvasCell))
  211. {
  212. canvasCell.CloneFrom(combinationCell);
  213. }
  214. else
  215. {
  216. canvasCell = (CombinationCell)combinationCell.Duplicate();
  217. canvasCell.Position = pos;
  218. UiNode.L_CombinationRoot.L_CanvasRoot.AddChild(canvasCell);
  219. _canvas.Add(pos, canvasCell);
  220. _canvasDirty = true;
  221. }
  222. }
  223. }
  224.  
  225. //擦除操作
  226. private void EraseBrush()
  227. {
  228. var brushRoot = UiNode.L_CombinationRoot.L_BrushRoot.Instance;
  229. foreach (var keyValuePair in _brushData)
  230. {
  231. var combinationCell = keyValuePair.Value;
  232. var pos = (combinationCell.Position + brushRoot.Position).AsVector2I();
  233. if (_canvas.TryGetValue(pos, out var canvasCell))
  234. {
  235. canvasCell.QueueFree();
  236. _canvas.Remove(pos);
  237. _canvasDirty = true;
  238. }
  239. }
  240. }
  241.  
  242. //选中组合的图块
  243. private void OnSelectCombinationCell(object obj)
  244. {
  245. if (obj is Vector2I cell && !_brushData.ContainsKey(cell))
  246. {
  247. _initBrush = true;
  248. var cellData = new CombinationCell();
  249. cellData.Position = cell * GameConfig.TileCellSize;
  250. cellData.InitData(UiNode.UiPanel.EditorPanel.Texture, cell);
  251. UiNode.L_CombinationRoot.L_BrushRoot.AddChild(cellData);
  252. _brushData.Add(cell, cellData);
  253. //计算起始点和终点
  254. _xStart = Mathf.Min(cell.X, _xStart);
  255. _yStart = Mathf.Min(cell.Y, _yStart);
  256. _xEnd = Mathf.Max(cell.X, _xEnd);
  257. _yEnd = Mathf.Max(cell.Y, _yEnd);
  258. _brushOffset = new Vector2I(-(_xStart + (_xEnd - _xStart) / 2), -(_yStart + (_yEnd - _yStart) / 2)) * GameConfig.TileCellSize;
  259. }
  260. }
  261. //移除组合图块
  262. private void OnRemoveCombinationCell(object obj)
  263. {
  264. if (obj is Vector2I cell)
  265. {
  266. if (_brushData.TryGetValue(cell, out var cellData))
  267. {
  268. cellData.QueueFree();
  269. _brushData.Remove(cell);
  270. }
  271. }
  272. }
  273.  
  274. //移除所有组合图块
  275. private void OnClearCombinationCell(object obj)
  276. {
  277. foreach (var keyValuePair in _brushData)
  278. {
  279. keyValuePair.Value.QueueFree();
  280. }
  281. _brushData.Clear();
  282. _initBrush = false;
  283. _brushOffset = Vector2I.Zero;
  284. _xStart = int.MaxValue;
  285. _yStart = int.MaxValue;
  286. _xEnd = int.MinValue;
  287. _yEnd = int.MinValue;
  288. }
  289. }