Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / common / editor / toolBtn / EditorTilePen.cs
@小李xl 小李xl on 17 Jul 6 KB 地牢编辑器移植完成
  1.  
  2. using Godot;
  3. using UI.MapEditor;
  4.  
  5. /// <summary>
  6. /// 瓷砖画笔
  7. /// </summary>
  8. public class EditorTilePen : EditorToolBase
  9. {
  10. private bool _isPressed;
  11. private MouseButton _buttonIndex;
  12.  
  13. //单次绘制是否改变过tile数据
  14. private bool _changeFlag = false;
  15.  
  16. private Vector2 _mousePosition;
  17. private Vector2I _mouseCellPosition;
  18.  
  19. //上一帧鼠标所在的cell坐标
  20. private Vector2I _prevMouseCellPosition = new Vector2I(-99999, -99999);
  21.  
  22. public EditorTilePen(EditorTileMap editorTileMap) : base(
  23. ResourcePath.resource_sprite_ui_commonIcon_PenTool_png, "绘制瓷砖", true, editorTileMap, EditorToolEnum.TilePen)
  24. {
  25. }
  26.  
  27. public override void Process(float delta)
  28. {
  29. var mousePosition = EditorTileMap.GetLocalMousePosition();
  30. //绘制2x2地形
  31. if ((!_isPressed || _buttonIndex != MouseButton.Right) &&
  32. EditorTileMap.CurrBrushType == EditorTileMap.TileMapDrawMode.Terrain &&
  33. EditorTileMap.CurrTerrain != null &&
  34. EditorTileMap.CurrTerrain.TerrainInfo.TerrainType == 1)
  35. {
  36. mousePosition -= new Vector2(GameConfig.TileCellSize / 2f, GameConfig.TileCellSize / 2f);
  37. }
  38.  
  39. _mouseCellPosition = EditorTileMap.LocalToMap(mousePosition);
  40. _mousePosition = _mouseCellPosition * GameConfig.TileCellSize;
  41. }
  42.  
  43. public override void OnMapInputEvent(InputEvent @event)
  44. {
  45. if (@event is InputEventMouseButton mouseButton)
  46. {
  47. _buttonIndex = mouseButton.ButtonIndex;
  48. if (mouseButton.ButtonIndex == MouseButton.Left || mouseButton.ButtonIndex == MouseButton.Right)
  49. {
  50. _isPressed = mouseButton.Pressed;
  51. if (!_isPressed)
  52. {
  53. _changeFlag = false;
  54. }
  55. else if (_buttonIndex == MouseButton.Left) //绘制
  56. {
  57. DoDrawCell();
  58. }
  59. else if (_buttonIndex == MouseButton.Right) //擦除
  60. {
  61. DoEraseCell();
  62. }
  63. }
  64. }
  65. else if (@event is InputEventMouseMotion && _isPressed)
  66. {
  67. if (_buttonIndex == MouseButton.Left) //绘制
  68. {
  69. DoDrawCell();
  70. }
  71. else if (_buttonIndex == MouseButton.Right) //擦除
  72. {
  73. DoEraseCell();
  74. }
  75. }
  76. }
  77.  
  78. private void DoDrawCell()
  79. {
  80. if (_prevMouseCellPosition != _mouseCellPosition || !_changeFlag) //鼠标位置变过
  81. {
  82. _changeFlag = true;
  83. _prevMouseCellPosition = _mouseCellPosition;
  84. //绘制图块
  85. EditorTileMap.SetSingleCell(_mouseCellPosition);
  86. }
  87. }
  88.  
  89. private void DoEraseCell()
  90. {
  91. var position = EditorTileMap.GetLocalMousePosition();
  92. var mouseCellPosition = EditorTileMap.LocalToMap(position);
  93. if (_prevMouseCellPosition != mouseCellPosition || !_changeFlag) //鼠标位置变过
  94. {
  95. _changeFlag = true;
  96. _prevMouseCellPosition = mouseCellPosition;
  97. //绘制图块
  98. EditorTileMap.EraseSingleCell(mouseCellPosition);
  99. }
  100. }
  101.  
  102. public override void OnMapDrawTool(CanvasItem brush)
  103. {
  104. var canvasItem = brush;
  105. if (EditorTileMap.CurrLayer.Layer == MapLayer.AutoFloorLayer) //选择自动地板层
  106. {
  107. DrawCellOutline(canvasItem);
  108. }
  109. else //自定义层
  110. {
  111. if (EditorTileMap.CurrBrushType == EditorTileMap.TileMapDrawMode.Free ||
  112. EditorTileMap.CurrBrushType == EditorTileMap.TileMapDrawMode.Combination) //自由绘制 或者 绘制组合
  113. {
  114. if (_isPressed && _buttonIndex == MouseButton.Right) //按下了左键擦除
  115. {
  116. DrawCellOutline(canvasItem);
  117. }
  118. else //正常绘制
  119. {
  120. foreach (var item in EditorTileMap.CurrBrush)
  121. {
  122. var rect = new Rect2(
  123. _mousePosition + (item.Key + EditorTileMap.BrushOffset) * GameConfig.TileCellSize,
  124. GameConfig.TileCellSize, GameConfig.TileCellSize);
  125. var srcRect = new Rect2(item.Value * GameConfig.TileCellSize, GameConfig.TileCellSize,
  126. GameConfig.TileCellSize);
  127. canvasItem.DrawTextureRectRegion(EditorTileMap.CurrBrushTexture, rect, srcRect,
  128. new Color(1, 1, 1, 0.3f));
  129. }
  130. }
  131. }
  132. else if (EditorTileMap.CurrBrushType == EditorTileMap.TileMapDrawMode.Terrain) //绘制地形
  133. {
  134. if (EditorTileMap.CurrTerrain == null) //未选择地形
  135. {
  136. DrawCellOutline(canvasItem);
  137. }
  138. else if (EditorTileMap.CurrTerrain.TerrainInfo.TerrainType == 0) //3x3
  139. {
  140. DrawCellOutline(canvasItem);
  141. }
  142. else //2x2
  143. {
  144. DrawCellOutline(canvasItem);
  145. if (!_isPressed || _buttonIndex != MouseButton.Right) //没按下左键擦除
  146. {
  147. DrawCellOutline(canvasItem, new Vector2I(GameConfig.TileCellSize, 0));
  148. DrawCellOutline(canvasItem, new Vector2I(0, GameConfig.TileCellSize));
  149. DrawCellOutline(canvasItem,
  150. new Vector2I(GameConfig.TileCellSize, GameConfig.TileCellSize));
  151. }
  152. }
  153. }
  154. }
  155. }
  156.  
  157. private void DrawCellOutline(CanvasItem canvasItem)
  158. {
  159. canvasItem.DrawRect(new Rect2(_mousePosition, EditorTileMap.TileSet.TileSize),
  160. Colors.White, false, 2f / EditorTileMap.Scale.X);
  161. }
  162.  
  163. private void DrawCellOutline(CanvasItem canvasItem, Vector2I offset)
  164. {
  165. canvasItem.DrawRect(
  166. new Rect2(_mousePosition + offset, EditorTileMap.TileSet.TileSize),
  167. Colors.White, false, 2f / EditorTileMap.Scale.X);
  168. }
  169. }