/// <summary> /// Loads the sprites of the given sprite palette from the given XML-node. /// </summary> /// <typeparam name="TVariant">The type of the enumeration that determines the variants of a sprite.</typeparam> /// <param name="spritePaletteElem">The XML-node to load from.</param> /// <param name="palette">The target sprite palette.</param> /// <param name="defaultVariant">The default variant that should be used when no variant was found for a sprite definition.</param> private static void LoadSprites <TVariant>(XElement spritePaletteElem, SpritePalette <TVariant> palette, TVariant defaultVariant) where TVariant : struct { foreach (XElement spriteElem in spritePaletteElem.Elements(SPRITE_ELEM)) { XAttribute spriteNameAttr = spriteElem.Attribute(SPRITE_NAME_ATTR); XAttribute spriteVariantAttr = spriteElem.Attribute(SPRITE_VARIANT_ATTR); XAttribute sourceRegionAttr = spriteElem.Attribute(SPRITE_SOURCEREGION_ATTR); XAttribute offsetAttr = spriteElem.Attribute(SPRITE_OFFSET_ATTR); if (spriteNameAttr == null) { throw new InvalidOperationException("Sprite name not defined for a sprite in sprite palette!"); } if (sourceRegionAttr == null) { throw new InvalidOperationException("Source region not defined in sprite palette!"); } TVariant variant = defaultVariant; if (spriteVariantAttr != null) { if (!EnumMap <TVariant, string> .TryDemap(spriteVariantAttr.Value, out variant)) { throw new InvalidOperationException(string.Format("Unexpected sprite variant '{0}'!", spriteVariantAttr.Value)); } } palette.AddSprite(spriteNameAttr.Value, variant, XmlHelper.LoadIntRectangle(sourceRegionAttr.Value), offsetAttr != null ? XmlHelper.LoadIntVector(offsetAttr.Value) : new RCIntVector(0, 0)); } }
/// <summary> /// Loads a multi-variant sprite palette definition from the given XML node. /// </summary> /// <param name="spritePaletteElem">The XML node to load from.</param> /// <param name="defaultVariant">The default variant that should be used when no variant was found for a sprite definition.</param> /// <param name="imagesDir">The directory of the referenced images.</param> /// <returns>The constructed sprite palette.</returns> public static ISpritePalette <TVariant> LoadSpritePalette <TVariant>(XElement spritePaletteElem, TVariant defaultVariant, string imagesDir) where TVariant : struct { XAttribute imageAttr = spritePaletteElem.Attribute(SPRITEPALETTE_IMAGE_ATTR); XAttribute transpColorAttr = spritePaletteElem.Attribute(SPRITEPALETTE_TRANSPCOLOR_ATTR); XAttribute ownerMaskColorAttr = spritePaletteElem.Attribute(SPRITEPALETTE_MASKCOLOR_ATTR); if (imageAttr == null) { throw new InvalidOperationException("Image not defined for sprite palette!"); } /// Read the image data. string imagePath = Path.Combine(imagesDir, imageAttr.Value); byte[] imageData = File.ReadAllBytes(imagePath); /// Create the sprite palette object. SpritePalette <TVariant> spritePalette = new SpritePalette <TVariant>(imageData, transpColorAttr != null ? XmlHelper.LoadColor(transpColorAttr.Value) : RCColor.Undefined, ownerMaskColorAttr != null ? XmlHelper.LoadColor(ownerMaskColorAttr.Value) : RCColor.Undefined); /// Load the sprites. LoadSprites(spritePaletteElem, spritePalette, defaultVariant); return(spritePalette); }