Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditorTerrain / up / TerrainCell.cs
@小李xl 小李xl on 20 Jan 2024 3 KB 擦除自动地形
  1. using Godot;
  2.  
  3. namespace UI.TileSetEditorTerrain;
  4.  
  5. public class TerrainCell : UiCell<TileSetEditorTerrain.RightCell, byte>
  6. {
  7. /// <summary>
  8. /// 已经赋值并连接的MaskCell
  9. /// </summary>
  10. public MaskCell ConnectMaskCell { get; set; }
  11. /// <summary>
  12. /// 鼠标是否悬停
  13. /// </summary>
  14. public bool Hover { get; set; }
  15. /// <summary>
  16. /// 是否放置了图块
  17. /// </summary>
  18. public bool IsPutDownTexture { get; private set; }
  19. /// <summary>
  20. /// 图块在 Source 中的位置, 单位: 像素
  21. /// </summary>
  22. public Vector2I TextureCell { get; private set; }
  23. private TileSetEditorTerrainPanel _panel;
  24. public override void OnInit()
  25. {
  26. _panel = CellNode.UiPanel;
  27. CellNode.Instance.Draw += OnDraw;
  28. }
  29.  
  30. /// <summary>
  31. /// 拖拽放置Cell
  32. /// </summary>
  33. public bool OnDropCell(MaskCell maskCell)
  34. {
  35. if (CellNode.Instance.IsMouseInRect())
  36. {
  37. OnDropData(maskCell);
  38. return true;
  39. }
  40.  
  41. return false;
  42. }
  43.  
  44. public override void Process(float delta)
  45. {
  46. CellNode.Instance.QueueRedraw();
  47. }
  48. /// <summary>
  49. /// 擦除当前选择的图块
  50. /// </summary>
  51. public void EraseCell()
  52. {
  53. var flag = IsPutDownTexture;
  54. ClearCellTexture();
  55. if (flag)
  56. {
  57. ClearTerrainBitData();
  58. if (ConnectMaskCell != null)
  59. {
  60. ConnectMaskCell.SetUseFlag(false);
  61. ConnectMaskCell.SetConnectTerrainCell(null);
  62. }
  63. }
  64. }
  65.  
  66. /// <summary>
  67. /// 设置选择的Cell
  68. /// </summary>
  69. public void SetCell(Rect2I rect)
  70. {
  71. TextureCell = rect.Position;
  72. var sprite2D = CellNode.L_CellTexture.Instance;
  73. sprite2D.Texture = _panel.EditorPanel.Texture;
  74. sprite2D.RegionEnabled = true;
  75. sprite2D.RegionRect = rect;
  76. IsPutDownTexture = true;
  77. }
  78.  
  79. /// <summary>
  80. /// 清除选中的cell
  81. /// </summary>
  82. public void ClearCellTexture()
  83. {
  84. CellNode.L_CellTexture.Instance.Texture = null;
  85. IsPutDownTexture = false;
  86. }
  87.  
  88. /// <summary>
  89. /// 清除存储的地形掩码数据
  90. /// </summary>
  91. public void ClearTerrainBitData()
  92. {
  93. SetTerrainBitData(null);
  94. }
  95.  
  96. private void OnDropData(MaskCell maskCell)
  97. {
  98. SetCell(maskCell.Data);
  99. SetTerrainBitData(new []{ TextureCell.X, TextureCell.Y });
  100. if (ConnectMaskCell != maskCell)
  101. {
  102. if (ConnectMaskCell != null)
  103. {
  104. ConnectMaskCell.SetUseFlag(false);
  105. ConnectMaskCell.SetConnectTerrainCell(null);
  106. }
  107. maskCell.SetConnectTerrainCell(this);
  108. maskCell.SetUseFlag(true);
  109. }
  110. }
  111. private void SetTerrainBitData(int[] cellData)
  112. {
  113. if (cellData == null)
  114. {
  115. _panel.CurrTerrain?.RemoveTerrainCell(Index, Data);
  116. _panel.CurrTerrain?.RefreshReady(_panel.EditorPanel.TileSetSourceIndex, _panel.CurrTerrainIndex);
  117. }
  118. else
  119. {
  120. _panel.CurrTerrain?.SetTerrainCell(Index, Data, cellData);
  121. _panel.CurrTerrain?.RefreshReady(_panel.EditorPanel.TileSetSourceIndex, _panel.CurrTerrainIndex);
  122. }
  123.  
  124. EventManager.EmitEvent(EventEnum.OnTileSetDirty);
  125. }
  126. private void OnDraw()
  127. {
  128. if (ConnectMaskCell != null && (Hover || ConnectMaskCell.Hover))
  129. {
  130. CellNode.Instance.DrawRect(
  131. new Rect2(Vector2.Zero, CellNode.Instance.Size),
  132. new Color(0, 1, 0, 0.2f)
  133. );
  134. }
  135. }
  136. }