public static void Draw(
            this SpriteBatch spriteBatch, NinePatchRegion2D ninePatchRegion, RectangleF destinationRectangle, Color color, RectangleF?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).ToRectangle();
                    destinationPatch = ClipDestinationRectangle(destinationPatch, clippingRectangle.Value).ToRectangle();
                    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);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static void Draw(this SpriteBatch spriteBatch, NinePatchRegion2D ninePatchRegion,
                                Rectangle destinationRectangle, Color color)
        {
            var destinationPatches = ninePatchRegion.CreatePatches(destinationRectangle);
            var sourcePatches      = ninePatchRegion.SourcePatches;

            for (var i = 0; i < sourcePatches.Length; i++)
            {
                spriteBatch.Draw(ninePatchRegion.Texture, sourceRectangle: sourcePatches[i],
                                 destinationRectangle: destinationPatches[i], color: color);
            }
        }
Ejemplo n.º 3
0
        /// <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);
        }