Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / mapEditorMapTile / FreeTileTab.cs
@小李xl 小李xl on 18 Jan 2024 5 KB 细节调整
  1. using System.Collections.Generic;
  2. using Godot;
  3.  
  4. namespace UI.MapEditorMapTile;
  5.  
  6. /// <summary>
  7. /// 自由笔刷页签
  8. /// </summary>
  9. public partial class FreeTileTab : EditorGridBg<MapEditorMapTile.Tab1>
  10. {
  11. private ImageTexture _texture;
  12. private Sprite2D _sprite;
  13. private Control _brush;
  14.  
  15. //图像宽度
  16. private int _width;
  17. //图像高度
  18. private int _height;
  19. private bool _leftPressed;
  20. private Vector2I _prevPos;
  21. private List<Vector2I> _selectCells = new List<Vector2I>();
  22. public override void SetUiNode(IUiNode uiNode)
  23. {
  24. base.SetUiNode(uiNode);
  25. InitNode(UiNode.L_TabRoot.Instance, UiNode.L_Grid.Instance);
  26. _texture = new ImageTexture();
  27. UiNode.UiPanel.EditorTileMap.SetCurrBrushTexture(_texture);
  28. _sprite = UiNode.L_TabRoot.L_TileSprite.Instance;
  29. _sprite.Texture = _texture;
  30. _brush = UiNode.L_TabRoot.L_Brush.Instance;
  31. _brush.Draw += OnBrushDraw;
  32. //聚焦按钮
  33. UiNode.L_FocusBtn.Instance.Pressed += OnFocusClick;
  34. }
  35.  
  36. protected override void Dispose(bool disposing)
  37. {
  38. _texture.Dispose();
  39. }
  40.  
  41. public override void _Process(double delta)
  42. {
  43. if (Visible)
  44. {
  45. _brush.QueueRedraw();
  46. }
  47. }
  48.  
  49. public override void _GuiInput(InputEvent @event)
  50. {
  51. base._GuiInput(@event);
  52.  
  53. if (@event is InputEventMouseButton mouseButton)
  54. {
  55. if (mouseButton.ButtonIndex == MouseButton.Left) //左键选中
  56. {
  57. if (UiNode.L_TabRoot.Instance.IsMouseInRect())
  58. {
  59. _leftPressed = mouseButton.Pressed;
  60. if (_leftPressed)
  61. {
  62. //清理之前的格子
  63. _selectCells.Clear();
  64. UiNode.UiPanel.EditorTileMap.ClearCurrBrushAtlasCoords();
  65. //当前格子
  66. var atlasCoords = Utils.GetMouseCellPosition(UiNode.L_TabRoot.Instance);
  67. _prevPos = atlasCoords * GameConfig.TileCellSize;
  68. _selectCells.Add(_prevPos);
  69. UiNode.UiPanel.EditorTileMap.AddCurrBrushAtlasCoords(atlasCoords, atlasCoords);
  70. }
  71. }
  72.  
  73. }
  74. else if (!_leftPressed && mouseButton.ButtonIndex == MouseButton.Right) //右键擦除
  75. {
  76. if (UiNode.L_TabRoot.Instance.IsMouseInRect())
  77. {
  78. var atlasCoords = Utils.GetMouseCellPosition(UiNode.L_TabRoot.Instance);
  79. _selectCells.Remove(atlasCoords * GameConfig.TileCellSize);
  80. UiNode.UiPanel.EditorTileMap.RemoveCurrBrushAtlasCoords(atlasCoords);
  81. }
  82. }
  83. }
  84. else if (_leftPressed && @event is InputEventMouseMotion && UiNode.L_TabRoot.Instance.IsMouseInRect())
  85. {
  86. //多选格子
  87. var atlasCoords = Utils.GetMouseCellPosition(UiNode.L_TabRoot.Instance);
  88. var pos = atlasCoords * GameConfig.TileCellSize;
  89. if (pos != _prevPos)
  90. {
  91. _prevPos = pos;
  92. if (!_selectCells.Contains(pos))
  93. {
  94. _selectCells.Add(pos);
  95. UiNode.UiPanel.EditorTileMap.AddCurrBrushAtlasCoords(atlasCoords, atlasCoords);
  96. }
  97. }
  98. }
  99. }
  100.  
  101. /// <summary>
  102. /// 设置显示的纹理
  103. /// </summary>
  104. public void SetImage(Image image)
  105. {
  106. //清理之前的格子
  107. _selectCells.Clear();
  108. UiNode.UiPanel.EditorTileMap.ClearCurrBrushAtlasCoords();
  109. _texture.SetImage(image);
  110. var texture = UiNode.L_TabRoot.L_TileSprite.Instance.Texture;
  111. if (texture != null)
  112. {
  113. _width = texture.GetWidth();
  114. _height = texture.GetHeight();
  115. }
  116. else
  117. {
  118. _width = 0;
  119. _height = 0;
  120. }
  121. var root = UiNode.L_TabRoot.Instance;
  122. root.Size = new Vector2(_width, _height);
  123. }
  124.  
  125. /// <summary>
  126. /// 聚焦
  127. /// </summary>
  128. public void OnFocusClick()
  129. {
  130. Utils.DoFocusNode(ContainerRoot, Size, new Vector2(_width, _height));
  131. RefreshGridTrans();
  132. }
  133.  
  134. //绘制辅助线
  135. private void OnBrushDraw()
  136. {
  137. var root = UiNode.L_TabRoot.Instance;
  138. //绘制区域
  139. _brush.DrawRect(
  140. new Rect2(Vector2.Zero, new Vector2(_width, _height)), new Color(1, 1, 0, 0.5f), false,
  141. 2f / root.Scale.X
  142. );
  143. //绘制悬停的区域
  144. if (root.IsMouseInRect())
  145. {
  146. var position = Utils.GetMouseCellPosition(root) * GameConfig.TileCellSize;
  147. _brush.DrawRect(
  148. new Rect2(position, GameConfig.TileCellSizeVector2I),
  149. Colors.Green, false, 3f / root.Scale.X
  150. );
  151. }
  152. //绘制选中的格子
  153. foreach (var cell in _selectCells)
  154. {
  155. _brush.DrawRect(
  156. new Rect2(cell, GameConfig.TileCellSizeVector2I),
  157. Colors.White, false, 2f / root.Scale.X
  158. );
  159. _brush.DrawRect(
  160. new Rect2(cell, GameConfig.TileCellSizeVector2I),
  161. new Color(0, 1, 0, 0.1f), true
  162. );
  163. }
  164. }
  165. }