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