void ITilemapToolEnvironment.PerformEditTiles(EditTilemapActionType actionType, Tilemap tilemap, Point2 pos, Grid<bool> brush, ITileDrawSource source, Point2 sourceOffset) { Grid<Tile> drawPatch = new Grid<Tile>(brush.Width, brush.Height); source.FillTarget(drawPatch, sourceOffset); UndoRedoManager.Do(new EditTilemapAction( tilemap, actionType, pos, drawPatch, brush, this.actionTool.Settings.UseAutoTiling)); }
public EditTilemapAction(Tilemap tilemap, EditTilemapActionType type, Point2 origin, Grid <Tile> newTiles, Grid <bool> editMask, AutoTilePaintMode autoTileMode) { if (tilemap == null) { throw new ArgumentNullException("tilemap"); } if (newTiles == null) { throw new ArgumentNullException("newTiles"); } if (editMask == null) { throw new ArgumentNullException("editMask"); } Point2 size = new Point2(newTiles.Width, newTiles.Height); Point2 margin = Point2.Zero; // If we'll update neighbouring AutoTiles, we'll need a one tile margin // to properly support Undo / Redo and painting. if (autoTileMode == AutoTilePaintMode.Full) { margin = new Point2(1, 1); } // Extend the editing area by the above margin. size.X += margin.X * 2; size.Y += margin.Y * 2; origin.X -= margin.X; origin.Y -= margin.Y; // Clamp the specified editing area to what is actually valid for this tilemap Point2 clampedOrigin = origin; Point2 clampedSize = size; clampedSize.X += Math.Min(clampedOrigin.X, 0); clampedSize.Y += Math.Min(clampedOrigin.Y, 0); clampedOrigin.X = MathF.Clamp(clampedOrigin.X, 0, tilemap.Size.X); clampedOrigin.Y = MathF.Clamp(clampedOrigin.Y, 0, tilemap.Size.Y); clampedSize.X = MathF.Clamp(clampedSize.X, 0, tilemap.Size.X - clampedOrigin.X); clampedSize.Y = MathF.Clamp(clampedSize.Y, 0, tilemap.Size.Y - clampedOrigin.Y); this.tilemap = tilemap; this.type = type; this.origin = clampedOrigin; this.autoTileMode = autoTileMode; // Copy edited tiles and masking to the operation's tilemap-clamped space this.newTiles = new Grid <Tile>(clampedSize.X, clampedSize.Y); this.editMask = new Grid <bool>(clampedSize.X, clampedSize.Y); this.editMaskAutoTile = new Grid <bool>(clampedSize.X, clampedSize.Y); newTiles.CopyTo(this.newTiles, margin.X + Math.Min(origin.X, 0), margin.X + Math.Min(origin.Y, 0)); editMask.CopyTo(this.editMask, margin.Y + Math.Min(origin.X, 0), margin.Y + Math.Min(origin.Y, 0)); editMask.CopyTo(this.editMaskAutoTile, margin.Y + Math.Min(origin.X, 0), margin.Y + Math.Min(origin.Y, 0)); // If we're updating neighbouring AutoTiles, expand the AutoTile mask by one // so we'll properly undo them too, despite not painting them directly. if (autoTileMode == AutoTilePaintMode.Full) { ExpandMask(this.editMaskAutoTile); } }