/// <summary> /// Loads a tile variant from the given XML element. /// </summary> /// <param name="fromElem">The XML element to load from.</param> /// <param name="tile">The tile type being loaded.</param> /// <param name="tileset">The tileset being loaded.</param> private static void LoadVariant(XElement fromElem, IsoTileType tile, TileSet tileset) { XAttribute imageAttr = fromElem.Attribute(XmlTileSetConstants.VARIANT_IMAGE_ATTR); XAttribute transpColorAttr = fromElem.Attribute(XmlTileSetConstants.VARIANT_TRANSPCOLOR_ATTR); if (imageAttr == null) { throw new TileSetException("Image not defined for tile variant!"); } if (transpColorAttr == null) { throw new TileSetException("Transparent color not defined for tile variant!"); } /// Read the image data. string imagePath = Path.Combine(tmpImageDir, imageAttr.Value); byte[] imageData = File.ReadAllBytes(imagePath); /// Create the new TileVariant object and add it to the tile type. IsoTileVariant newVariant = new IsoTileVariant(imageData, XmlHelper.LoadColor(transpColorAttr.Value), tileset); tile.AddVariant(newVariant); /// Load the cell data changesets. foreach (XElement childElem in fromElem.Elements()) { ICellDataChangeSet changeset = LoadCellDataChangeSet(childElem, tileset); newVariant.AddCellDataChangeset(changeset); } /// Register the variant to the tileset. tileset.RegisterVariant(newVariant); }
/// <summary> /// Adds a TileVariant to the list that corresponds to the current conditional branch or starts /// defining the default branch if there is no conditional branch in progress. /// </summary> /// <param name="variant">The new TileVariant to add to the branch.</param> public void AddVariant(IsoTileVariant variant) { if (this.tileset.IsFinalized) { throw new InvalidOperationException("TileSet already finalized!"); } if (variant == null) { throw new ArgumentNullException("variant"); } if (variant.Tileset != this.tileset) { throw new TileSetException("The given TileVariant is in another TileSet!"); } if (this.defaultBranch != null && this.tmpCurrentBranch == null) { /// Add the variant to the default branch. this.defaultBranch.Item1.Add(variant); } else if (this.defaultBranch == null && this.tmpCurrentBranch != null) { /// Add the variant to the currently defined conditional branch. this.tmpCurrentBranch.Item1.Add(variant); } else if (this.defaultBranch == null && this.tmpCurrentBranch == null) { /// Start the default branch. this.defaultBranch = new Tuple <List <IsoTileVariant>, IIsoTileCondition>(new List <IsoTileVariant>() { variant }, null); this.variants.Add(this.defaultBranch); } else { throw new InvalidOperationException("Unexpected state!"); } }
/// <summary> /// Registers the given variant with this tileset. /// </summary> /// <param name="variant">The variant to register.</param> public void RegisterVariant(IsoTileVariant variant) { if (this.isFinalized) { throw new InvalidOperationException("It is not possible to register tile variant for a finalized TileSet!"); } if (variant == null) { throw new ArgumentNullException("variant"); } if (variant.Tileset != this) { throw new InvalidOperationException("The variant is in another tileset!"); } if (this.allTileVariants.Contains(variant)) { throw new TileSetException("The variant has already been registered!"); } variant.SetIndex(this.allTileVariants.Count); this.allTileVariants.Add(variant); this.allTileVariantList.Add(variant); }