RenderingLibrary.Graphics.NineSlice.RefreshSourceRectangles C# (CSharp) Method

RefreshSourceRectangles() private method

private RefreshSourceRectangles ( ) : void
return void
        private void RefreshSourceRectangles()
        {
            bool useMulti;
            var useAtlas = mSprites[(int) NineSliceSections.TopLeft].AtlasedTexture != null;

            if (useAtlas)
            {
                useMulti = mSprites[(int) NineSliceSections.TopLeft].AtlasedTexture.Name !=
                           mSprites[(int) NineSliceSections.Top].AtlasedTexture.Name;
            }
            else //not using atlas
            {
                useMulti = mSprites[(int)NineSliceSections.TopLeft].Texture != mSprites[(int)NineSliceSections.Top].Texture;
            }

            if (useMulti)
            {
                if ((!useAtlas && mSprites[(int)NineSliceSections.TopLeft].Texture == null) ||
                    (useAtlas && mSprites[(int)NineSliceSections.TopLeft].AtlasedTexture == null))
                {
                    for (var sprite = 0; sprite < mSprites.Count(); sprite++)
                    {
                        mSprites[sprite].SourceRectangle = null;
                    }
                }
                else
                {
                    for (var sprite = 0; sprite < mSprites.Count(); sprite++)
                    {
                        if (useAtlas)
                        {
                            if (sprite == (int) NineSliceSections.TopLeft)
                            {
                                mFullOutsideWidth = mSprites[sprite].AtlasedTexture.SourceRectangle.Width;
                                mFullInsideWidth = mSprites[sprite].AtlasedTexture.SourceRectangle.Width - (mFullOutsideWidth * 2);
                            }
                        }
                        else
                        {
                            mFullOutsideWidth = mSprites[(int)NineSliceSections.TopLeft].Texture.Width;
                            mFullInsideWidth = mSprites[(int)NineSliceSections.TopLeft].Texture.Width - (mFullOutsideWidth * 2);

                            mSprites[sprite].SourceRectangle = new Rectangle(0, 0, mSprites[sprite].Texture.Width, mSprites[sprite].Texture.Height);
                        }
                    }

                }
            }
            else if ((!useAtlas && mSprites[(int) NineSliceSections.TopLeft].Texture != null) ||
                     (useAtlas && mSprites[(int) NineSliceSections.TopLeft].AtlasedTexture != null))
            {
                int leftCoordinate;
                int rightCoordinate;
                int topCoordinate;
                int bottomCoordinate;

                if (useAtlas)
                {
                    var atlasedTexture = mSprites[(int) NineSliceSections.TopLeft].AtlasedTexture;

                    leftCoordinate = atlasedTexture.SourceRectangle.Left;
                    rightCoordinate = atlasedTexture.SourceRectangle.Right;
                    topCoordinate = atlasedTexture.SourceRectangle.Top;
                    bottomCoordinate = atlasedTexture.SourceRectangle.Bottom;
                }
                else
                {
                    var texture = mSprites[(int)NineSliceSections.TopLeft].Texture;

                    leftCoordinate = 0;
                    rightCoordinate = texture.Width;
                    topCoordinate = 0;
                    bottomCoordinate = texture.Height;
                }


                if (SourceRectangle.HasValue)
                {
                    leftCoordinate = SourceRectangle.Value.Left;
                    rightCoordinate = SourceRectangle.Value.Right;
                    topCoordinate = SourceRectangle.Value.Top;
                    bottomCoordinate = SourceRectangle.Value.Bottom;
                }

                int usedWidth = rightCoordinate - leftCoordinate;
                int usedHeight = bottomCoordinate - topCoordinate;

                mFullOutsideWidth = (usedWidth + 1) / 3;
                mFullInsideWidth = usedWidth - (mFullOutsideWidth * 2);

                mFullOutsideHeight = (usedHeight + 1) / 3;
                mFullInsideHeight = usedHeight - (mFullOutsideHeight * 2);

                int outsideWidth = System.Math.Min(mFullOutsideWidth, RenderingLibrary.Math.MathFunctions.RoundToInt(Width / 2)); ;
                int outsideHeight = System.Math.Min(mFullOutsideHeight, RenderingLibrary.Math.MathFunctions.RoundToInt(Height / 2));
                int insideWidth = mFullInsideWidth;
                int insideHeight = mFullInsideHeight;

                mSprites[(int)NineSliceSections.TopLeft].SourceRectangle = new Rectangle(
                    leftCoordinate + 0,
                    topCoordinate + 0,
                    outsideWidth,
                    outsideHeight);
                mSprites[(int)NineSliceSections.Top].SourceRectangle = new Rectangle(
                    leftCoordinate + outsideWidth,
                    topCoordinate + 0,
                    insideWidth,
                    outsideHeight);
                mSprites[(int)NineSliceSections.TopRight].SourceRectangle = new Rectangle(
                    leftCoordinate + insideWidth + outsideWidth,
                    topCoordinate + 0,
                    outsideWidth,
                    outsideHeight);

                mSprites[(int)NineSliceSections.Left].SourceRectangle = new Rectangle(
                    leftCoordinate + 0,
                    topCoordinate + outsideHeight,
                    outsideWidth,
                    insideHeight);
                mSprites[(int)NineSliceSections.Center].SourceRectangle = new Rectangle(
                    leftCoordinate + outsideWidth,
                    topCoordinate + outsideHeight,
                    insideWidth,
                    insideHeight);
                mSprites[(int)NineSliceSections.Right].SourceRectangle = new Rectangle(
                    leftCoordinate + outsideWidth + insideWidth,
                    topCoordinate + outsideHeight,
                    outsideWidth,
                    insideHeight);

                mSprites[(int)NineSliceSections.BottomLeft].SourceRectangle = new Rectangle(
                    leftCoordinate + 0,
                    topCoordinate + outsideHeight + insideHeight,
                    outsideWidth,
                    outsideHeight);
                mSprites[(int)NineSliceSections.Bottom].SourceRectangle = new Rectangle(
                    leftCoordinate + outsideWidth,
                    topCoordinate + outsideHeight + insideHeight,
                    insideWidth,
                    outsideHeight);
                mSprites[(int)NineSliceSections.BottomRight].SourceRectangle = new Rectangle(
                    leftCoordinate + outsideWidth + insideWidth,
                    topCoordinate + outsideHeight + insideHeight,
                    outsideWidth,
                    outsideHeight);
            }

            //top
            var tempRect = mSprites[(int) NineSliceSections.Top].SourceRectangle;
            if (!mSprites[(int)NineSliceSections.Top].SourceRectangle.HasValue && useAtlas)
                tempRect = mSprites[(int) NineSliceSections.Top].AtlasedTexture.SourceRectangle;

            if (tempRect.HasValue)
            {
                mSprites[(int)NineSliceSections.Top].Height = tempRect.Value.Height;
                mSprites[(int)NineSliceSections.TopLeft].Height = mSprites[(int)NineSliceSections.Top].Height;
                mSprites[(int)NineSliceSections.TopRight].Height = mSprites[(int)NineSliceSections.Top].Height;
            }

            //bottom
            tempRect = mSprites[(int)NineSliceSections.Bottom].SourceRectangle;
            if (!mSprites[(int)NineSliceSections.Bottom].SourceRectangle.HasValue && useAtlas)
                tempRect = mSprites[(int)NineSliceSections.Bottom].AtlasedTexture.SourceRectangle;

            if (tempRect.HasValue)
            {
                mSprites[(int)NineSliceSections.Bottom].Height = tempRect.Value.Height;
                mSprites[(int)NineSliceSections.BottomRight].Height = mSprites[(int)NineSliceSections.Bottom].Height;
                mSprites[(int)NineSliceSections.BottomLeft].Height = mSprites[(int)NineSliceSections.Bottom].Height;
            }

            //left
            tempRect = mSprites[(int)NineSliceSections.Left].SourceRectangle;
            if (!mSprites[(int)NineSliceSections.Left].SourceRectangle.HasValue && useAtlas)
                tempRect = mSprites[(int)NineSliceSections.Left].AtlasedTexture.SourceRectangle;

            if (tempRect.HasValue)
            {
                mSprites[(int)NineSliceSections.Left].Width = tempRect.Value.Width;
                mSprites[(int)NineSliceSections.TopLeft].Width = mSprites[(int)NineSliceSections.Left].Width;
                mSprites[(int)NineSliceSections.BottomLeft].Width = mSprites[(int)NineSliceSections.Left].Width;
            }

            //right
            tempRect = mSprites[(int)NineSliceSections.Right].SourceRectangle;
            if (!mSprites[(int)NineSliceSections.Right].SourceRectangle.HasValue && useAtlas)
                tempRect = mSprites[(int)NineSliceSections.Right].AtlasedTexture.SourceRectangle;

            if (tempRect.HasValue)
            {
                mSprites[(int)NineSliceSections.Right].Width = tempRect.Value.Width;
                mSprites[(int)NineSliceSections.TopRight].Width = mSprites[(int)NineSliceSections.Right].Width;
                mSprites[(int)NineSliceSections.BottomRight].Width = mSprites[(int)NineSliceSections.Right].Width;
            }
        }