public bool ResourceEquals(ArrangerElementModel model) { if (model is null) { return(false); } return(model.FileAddress == FileAddress && model.DataFileKey == DataFileKey && model.PaletteKey == PaletteKey && model.CodecName == CodecName && model.PositionX == PositionX && model.PositionY == PositionY && model.Mirror == Mirror & model.Rotation == Rotation); }
public static ScatteredArrangerModel MapToModel(this ScatteredArranger arranger, Dictionary <IProjectResource, string> resourceMap) { var model = new ScatteredArrangerModel() { Name = arranger.Name, ArrangerElementSize = arranger.ArrangerElementSize, ElementPixelSize = arranger.ElementPixelSize, Layout = arranger.Layout, ColorType = arranger.ColorType }; model.ElementGrid = new ArrangerElementModel[model.ArrangerElementSize.Width, model.ArrangerElementSize.Height]; for (int x = 0; x < model.ElementGrid.GetLength(0); x++) { for (int y = 0; y < model.ElementGrid.GetLength(1); y++) { if (arranger.GetElement(x, y) is ArrangerElement el) { model.ElementGrid[x, y] = MapToModel(el, x, y); } } } return(model); ArrangerElementModel MapToModel(ArrangerElement el, int elemX, int elemY) { var model = new ArrangerElementModel { FileAddress = el.SourceAddress, PositionX = elemX, PositionY = elemY, CodecName = el.Codec.Name, Mirror = el.Mirror, Rotation = el.Rotation }; if (el.Source is not null && resourceMap.TryGetValue(el.Source, out var dataFileKey)) { model.DataFileKey = dataFileKey; } if (el.Palette is not null && resourceMap.TryGetValue(el.Palette, out var paletteKey)) { model.PaletteKey = paletteKey; } return(model); } }
private bool TryDeserializeScatteredArranger(XElement element, string resourceName, out ScatteredArrangerModel arrangerModel) { var model = new ScatteredArrangerModel(); model.Name = resourceName; var elementsx = int.Parse(element.Attribute("elementsx").Value); // Width of arranger in elements var elementsy = int.Parse(element.Attribute("elementsy").Value); // Height of arranger in elements var width = int.Parse(element.Attribute("width").Value); // Width of element in pixels var height = int.Parse(element.Attribute("height").Value); // Height of element in pixels var defaultCodecName = element.Attribute("defaultcodec").Value; var defaultDataFileKey = element.Attribute("defaultdatafile").Value; var defaultPaletteKey = element.Attribute("defaultpalette")?.Value ?? _globalDefaultPalette.Name; var layoutName = element.Attribute("layout").Value; var colorType = element.Attribute("color")?.Value ?? "indexed"; var elementList = element.Descendants("element"); if (layoutName == "tiled") { model.Layout = ElementLayout.Tiled; } else if (layoutName == "single") { model.Layout = ElementLayout.Single; } else { throw new XmlException($"Unsupported arranger layout type ('{layoutName}') for arranger '{model.Name}'"); } if (colorType == "indexed") { model.ColorType = PixelColorType.Indexed; } else if (colorType == "direct") { model.ColorType = PixelColorType.Direct; } else { throw new XmlException($"Unsupported pixel color type ('{colorType}') for arranger '{model.Name}'"); } model.ArrangerElementSize = new Size(elementsx, elementsy); model.ElementGrid = new ArrangerElementModel[elementsx, elementsy]; model.ElementPixelSize = new Size(width, height); var xmlElements = elementList.Select(e => new { fileoffset = long.Parse(e.Attribute("fileoffset").Value, System.Globalization.NumberStyles.HexNumber), bitoffset = e.Attribute("bitoffset"), posx = int.Parse(e.Attribute("posx").Value), posy = int.Parse(e.Attribute("posy").Value), format = e.Attribute("codec"), palette = e.Attribute("palette"), datafile = e.Attribute("datafile"), mirror = e.Attribute("mirror"), rotation = e.Attribute("rotation") }); foreach (var xmlElement in xmlElements) { var el = new ArrangerElementModel(); el.DataFileKey = xmlElement.datafile?.Value ?? defaultDataFileKey; el.PaletteKey = xmlElement.palette?.Value ?? defaultPaletteKey; el.CodecName = xmlElement.format?.Value ?? defaultCodecName; el.PositionX = xmlElement.posx; el.PositionY = xmlElement.posy; if (xmlElement.bitoffset is not null) { el.FileAddress = new BitAddress(xmlElement.fileoffset, int.Parse(xmlElement.bitoffset.Value)); } else { el.FileAddress = new BitAddress(xmlElement.fileoffset, 0); } el.Mirror = xmlElement.mirror?.Value switch { "none" => MirrorOperation.None, "horizontal" => MirrorOperation.Horizontal, "vertical" => MirrorOperation.Vertical, "both" => MirrorOperation.Both, _ => MirrorOperation.None }; el.Rotation = xmlElement.rotation?.Value switch { "none" => RotationOperation.None, "left" => RotationOperation.Left, "right" => RotationOperation.Right, "turn" => RotationOperation.Turn, _ => RotationOperation.None }; model.ElementGrid[xmlElement.posx, xmlElement.posy] = el; } arrangerModel = model; return(true); } }