Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditorTerrain / up / TerrainCell.cs
@小李xl 小李xl on 10 Jan 2024 2 KB 重写拖拽设置Terrain
  1. using Godot;
  2.  
  3. namespace UI.TileSetEditorTerrain;
  4.  
  5. public class TerrainCell : UiCell<TileSetEditorTerrain.RightCell, byte>
  6. {
  7. /// <summary>
  8. /// 是否放置了图块
  9. /// </summary>
  10. public bool IsPutDownTexture { get; private set; }
  11. /// <summary>
  12. /// 图块在 Source 中的位置, 单位: 像素
  13. /// </summary>
  14. public Vector2I TextureCell { get; private set; }
  15. private TileSetEditorTerrainPanel _panel;
  16. public override void OnInit()
  17. {
  18. _panel = CellNode.UiPanel;
  19. CellNode.Instance.GuiInput += OnGuiInput;
  20. }
  21.  
  22. public bool OnDropCell(MaskCell maskCell)
  23. {
  24. if (CellNode.Instance.IsMouseInRect())
  25. {
  26. OnDropData(maskCell);
  27. return true;
  28. }
  29.  
  30. return false;
  31. }
  32.  
  33. private void OnGuiInput(InputEvent @event)
  34. {
  35. if (@event is InputEventMouseButton mouseEvent)
  36. {
  37. if (mouseEvent.ButtonIndex == MouseButton.Right && mouseEvent.Pressed) //右键擦除图块
  38. {
  39. CellNode.Instance.AcceptEvent();
  40. var flag = IsPutDownTexture;
  41. ClearCell();
  42. if (flag)
  43. {
  44. SetTerrainBitData(null);
  45. }
  46. }
  47. }
  48. }
  49.  
  50. /// <summary>
  51. /// 设置选择的Cell
  52. /// </summary>
  53. public void SetCell(Rect2I rect)
  54. {
  55. TextureCell = rect.Position;
  56. var sprite2D = CellNode.L_CellTexture.Instance;
  57. sprite2D.Texture = _panel.EditorPanel.Texture;
  58. sprite2D.RegionEnabled = true;
  59. sprite2D.RegionRect = rect;
  60. IsPutDownTexture = true;
  61. }
  62.  
  63. /// <summary>
  64. /// 清除选中的cell
  65. /// </summary>
  66. public void ClearCell()
  67. {
  68. CellNode.L_CellTexture.Instance.Texture = null;
  69. IsPutDownTexture = false;
  70. }
  71.  
  72. private void OnDropData(MaskCell maskCell)
  73. {
  74. SetCell(maskCell.Data);
  75. SetTerrainBitData(new []{ TextureCell.X, TextureCell.Y });
  76. }
  77. private void SetTerrainBitData(int[] cellData)
  78. {
  79. if (cellData == null)
  80. {
  81. _panel.EditorPanel.TileSetSourceInfo.Terrain.RemoveTerrainCell(Index, Data);
  82. }
  83. else
  84. {
  85. _panel.EditorPanel.TileSetSourceInfo.Terrain.SetTerrainCell(Index, Data, cellData);
  86. }
  87.  
  88. EventManager.EmitEvent(EventEnum.OnTileSetDirty);
  89. }
  90. }