public static void Draw(this SpriteBatch spriteBatch, NinePatchRegion2D ninePatchRegion, Rectangle destinationRectangle, Color color, Rectangle?clippingRectangle = null) { var destinationPatches = ninePatchRegion.CreatePatches(destinationRectangle); var sourcePatches = ninePatchRegion.SourcePatches; for (var i = 0; i < sourcePatches.Length; i++) { var sourcePatch = sourcePatches[i]; var destinationPatch = destinationPatches[i]; if (clippingRectangle.HasValue) { sourcePatch = ClipSourceRectangle(sourcePatch, destinationPatch, clippingRectangle.Value); destinationPatch = ClipDestinationRectangle(destinationPatch, clippingRectangle.Value); Draw(spriteBatch, ninePatchRegion.Texture, sourcePatch, destinationPatch, color, clippingRectangle); } else { if (destinationPatch.Width > 0 && destinationPatch.Height > 0) { spriteBatch.Draw(ninePatchRegion.Texture, sourceRectangle: sourcePatch, destinationRectangle: destinationPatch, color: color); } } } }
/// <summary> /// Creates a new nine patch texture region and adds it to the list of the <see cref="TextureAtlas" />' regions. /// </summary> /// <param name="name">Name of the texture region.</param> /// <param name="x">X coordinate of the region's top left corner.</param> /// <param name="y">Y coordinate of the region's top left corner.</param> /// <param name="width">Width of the texture region.</param> /// <param name="height">Height of the texture region.</param> /// <param name="thickness">Thickness of the nine patch region.</param> /// <returns>Created texture region.</returns> public NinePatchRegion2D CreateNinePatchRegion(string name, int x, int y, int width, int height, Thickness thickness) { if (_regionsByName.ContainsKey(name)) { throw new InvalidOperationException($"Region {name} already exists in the texture atlas"); } var textureRegion = new TextureRegion2D(name, Texture, x, y, width, height); var ninePatchRegion = new NinePatchRegion2D(textureRegion, thickness); AddRegion(ninePatchRegion); return(ninePatchRegion); }