From 229e614d4aa1ae038e1caf11c3d1b4cd0509e665 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 5 Jan 2020 01:33:13 +0100 Subject: [PATCH] added tiled map copying --- MLEM.Extended/Tiled/TiledExtensions.cs | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/MLEM.Extended/Tiled/TiledExtensions.cs b/MLEM.Extended/Tiled/TiledExtensions.cs index a4bb3ab..b50f2ae 100644 --- a/MLEM.Extended/Tiled/TiledExtensions.cs +++ b/MLEM.Extended/Tiled/TiledExtensions.cs @@ -112,5 +112,39 @@ namespace MLEM.Extended.Tiled { } } + public static TiledMap Copy(this TiledMap map) { + var newMap = new TiledMap(map.Name, map.Width, map.Height, map.TileWidth, map.TileHeight, map.RenderOrder, map.Orientation, map.BackgroundColor); + foreach (var tileset in map.Tilesets) + newMap.AddTileset(tileset, map.GetTilesetFirstGlobalIdentifier(tileset)); + foreach (var layer in map.Layers) + newMap.AddLayer(layer.Copy()); + foreach (var prop in map.Properties) + newMap.Properties.Add(prop.Key, prop.Value); + return newMap; + } + + public static TiledMapLayer Copy(this TiledMapLayer layer) { + TiledMapLayer newLayer; + if (layer is TiledMapTileLayer tile) { + var newTile = new TiledMapTileLayer(tile.Name, tile.Width, tile.Height, tile.TileWidth, tile.TileHeight, tile.Offset, tile.Opacity, tile.IsVisible); + for (ushort x = 0; x < tile.Width; x++) { + for (ushort y = 0; y < tile.Height; y++) + newTile.SetTile(x, y, tile.GetTile(x, y).GlobalTileIdentifierWithFlags); + } + newLayer = newTile; + } else if (layer is TiledMapObjectLayer obj) { + newLayer = new TiledMapObjectLayer(obj.Name, obj.Objects, obj.Color, obj.DrawOrder, obj.Offset, obj.Opacity, obj.IsVisible); + } else if (layer is TiledMapImageLayer img) { + newLayer = new TiledMapImageLayer(img.Name, img.Image, img.Position, img.Offset, img.Opacity, img.IsVisible); + } else if (layer is TiledMapGroupLayer group) { + newLayer = new TiledMapGroupLayer(group.Name, group.Layers.Select(l => l.Copy()).ToList(), group.Offset, group.Opacity, group.IsVisible); + } else { + return null; + } + foreach (var prop in layer.Properties) + newLayer.Properties.Add(prop.Key, prop.Value); + return newLayer; + } + } } \ No newline at end of file