public TextureGrid CreateDeepCopy() { var retval = new TextureGrid(FormattingMetaData.CreateCopy(), TextureTileFormattingMetaData.CreateCopy()) { X = X, Y = Y, Width = Width, Height = Height, AnchorX = AnchorX, AnchorY = AnchorY, CellWidth = CellWidth, CellHeight = CellHeight, CellSpacing = CellSpacing, MatcherType = MatcherType, Pattern = Pattern, Name = Name, }; foreach (var tile in Tiles) { retval.Tiles.Add(tile.CreateDeepCopy()); } foreach (var m in CellMappings) { retval.CellMappings.Add(m.CreateCopy()); } return(retval); }
static void ProcessCellMapElements(TextureGrid grid, XElement metadata) { var dict = new Dictionary <string, CellMappingDeclaration>(); foreach (var t in metadata.Elements().Where(e => e.Name.LocalName == "cell-mapping")) { var key = (string?)t.AttributeLocal("key"); var name = (string?)t.AttributeLocal("name"); var comment = (string?)t.AttributeLocal("comment"); var color = t.AttributeLocal("highlight-color").AsColor(); if (string.IsNullOrWhiteSpace(key)) { if (string.IsNullOrWhiteSpace(name)) { continue; } key = name.TrimStart().Substring(0, 1); } if (!dict.TryGetValue(key, out _)) { dict.Add(key, new CellMappingDeclaration() { Key = key, Name = name, Comment = comment, HighlightColor = color ?? TextureParserExtensions.Colors[dict.Count % TextureParserExtensions.Colors.Count] }); } } var legacyCellMapElements = (string?)metadata.AttributeLocal("cell-map-elements"); if (!string.IsNullOrEmpty(legacyCellMapElements)) { foreach (var element in legacyCellMapElements.Split(null)) { if (!dict.TryGetValue(element, out _)) { dict.Add(element, new CellMappingDeclaration() { Key = element, HighlightColor = TextureParserExtensions.Colors[dict.Count % TextureParserExtensions.Colors.Count] }); } } } }
public static TextureGrid ReadGrid(XElement grid, TexturePackLoaderContext context) { var name = (string?)grid.AttributeLocal("name") ?? "Unnamed Grid"; var x = (int?)grid.AttributeLocal("x") ?? throw new XmlParseException("Required attribute 'x' not found", grid); var y = (int?)grid.AttributeLocal("y") ?? throw new XmlParseException("Required attribute 'y' not found", grid); var width = (int?)grid.AttributeLocal("cell-width") ?? (int?)grid.AttributeLocal("width"); var height = (int?)grid.AttributeLocal("cell-height") ?? (int?)grid.AttributeLocal("height"); var border = (int?)grid.AttributeLocal("cell-spacing") ?? (int?)grid.AttributeLocal("border") ?? 0; var anchorX = (int?)grid.AttributeLocal("anchor-x"); var anchorY = (int?)grid.AttributeLocal("anchor-y"); var gridValue = new TextureGrid { Name = name, X = x, Y = y, CellWidth = width, CellHeight = height, AnchorX = anchorX, AnchorY = anchorY, CellSpacing = border }; var metadata = grid.ElementLocal("metadata"); if (metadata != null) { gridValue.MatcherType = ParseMatchType(metadata, (string?)metadata.AttributeLocal("matcher-type"), MatcherType.Basic); gridValue.Pattern = (string?)metadata.AttributeLocal("pattern"); gridValue.Width = (int?)metadata.AttributeLocal("grid-width"); gridValue.Height = (int?)metadata.AttributeLocal("grid-height"); ProcessCellMapElements(gridValue, metadata); } var tiles = from e in grid.Elements() where e.Name.LocalName == "tile" select ReadTiles(e); gridValue.Tiles.AddRange(tiles); ParseFormattingInfo(grid, gridValue.FormattingMetaData); return(gridValue); }
public static XElement GenerateGrid(TextureGrid grid) { var gridElement = new XElement(Namespace + "grid"); var me = GenerateFormattingMetaData(grid.FormattingMetaData); grid.Width.ForNonNull(w => me.Add(new XAttribute("grid-width", w))); grid.Height.ForNonNull(h => me.Add(new XAttribute("grid-height", h))); foreach (var cellMapElement in grid.CellMappings) { var m = new XElement(Namespace + "cell-mapping"); cellMapElement.Key.ForNotEmpty(e => m.Add(new XAttribute("key", e))); cellMapElement.Name.ForNotEmpty(e => m.Add(new XAttribute("name", e))); cellMapElement.Comment.ForNotEmpty(e => m.Add(new XAttribute("comment", e))); cellMapElement.HighlightColor.AsText().ForNotEmpty(e => m.Add(new XAttribute("highlight-color", e))); } if (!string.IsNullOrWhiteSpace(grid.Pattern)) { me.Add(new XAttribute("pattern", grid.Pattern)); } me.Add(new XAttribute("matcher-type", grid.MatcherType)); gridElement.Add(me); gridElement.Add(new XAttribute("name", grid.Name ?? "")); gridElement.Add(new XAttribute("x", grid.X)); gridElement.Add(new XAttribute("y", grid.Y)); gridElement.Add(new XAttribute("half-cell-hint", grid.MatcherType == MatcherType.Corner)); gridElement.Add(new XAttribute("cell-spacing", grid.CellSpacing)); grid.CellWidth.ForNonNull(w => gridElement.Add(new XAttribute("cell-width", w))); grid.CellHeight.ForNonNull(w => gridElement.Add(new XAttribute("cell-height", w))); grid.AnchorX.ForNonNull(x => gridElement.Add(new XAttribute("anchor-x", x))); grid.AnchorY.ForNonNull(y => gridElement.Add(new XAttribute("anchor-y", y))); gridElement.AddRange(grid.Tiles.Select(GenerateTile)); return(gridElement); }
public TileTextureCollection WithTextureGrid(TextureGrid g) { Grids.Add(g); return(this); }