Draw() private method

private Draw ( Microsoft.Xna.Framework.Graphics.Texture2D textureToUse, Rectangle destinationRectangle, Rectangle sourceRectangle, Color color, float rotation, Vector2 vector2, SpriteEffects effects, int layerDepth, object objectRequestingChange ) : void
textureToUse Microsoft.Xna.Framework.Graphics.Texture2D
destinationRectangle Microsoft.Xna.Framework.Rectangle
sourceRectangle Microsoft.Xna.Framework.Rectangle
color Color
rotation float
vector2 Vector2
effects SpriteEffects
layerDepth int
objectRequestingChange object
return void
        public void DrawTextLines(IEnumerable <string> lines, HorizontalAlignment horizontalAlignment, object objectRequestingChange, int requiredWidth, List <int> widths, SpriteRenderer spriteRenderer,
                                  Color color,
                                  float xOffset = 0, float yOffset = 0, float rotation = 0, float scaleX = 1, float scaleY = 1)
        {
            Point point = new Point();

            int lineNumber = 0;

            int xOffsetAsInt = MathFunctions.RoundToInt(xOffset);
            int yOffsetAsInt = MathFunctions.RoundToInt(yOffset);

            if (Renderer.NormalBlendState == BlendState.AlphaBlend)
            {
                // this is premultiplied, so premulitply the color value
                float multiple = color.A / 255.0f;

                color.R = (byte)(color.R * multiple);
                color.G = (byte)(color.G * multiple);
                color.B = (byte)(color.B * multiple);
            }

            foreach (string line in lines)
            {
                // scoot over to leave room for the outline
                point.X = mOutlineThickness;

                if (horizontalAlignment == HorizontalAlignment.Right)
                {
                    point.X = requiredWidth - widths[lineNumber];
                }
                else if (horizontalAlignment == HorizontalAlignment.Center)
                {
                    point.X = (requiredWidth - widths[lineNumber]) / 2;
                }

                foreach (char c in line)
                {
                    Rectangle destRect;
                    int       pageIndex;
                    var       sourceRect = GetCharacterRect(c, lineNumber, ref point, out destRect, out pageIndex, scaleX);



                    // todo: rotation, because that will impact destination rectangle too
                    if (Text.TextRenderingPositionMode == TextRenderingPositionMode.FreeFloating)
                    {
                        spriteRenderer.Draw(mTextures[pageIndex], new Vector2(destRect.X + xOffset, destRect.Y + yOffset), sourceRect, color, 0, Vector2.Zero, Vector2.One, SpriteEffects.None, 0, this);
                    }
                    else
                    {
                        // position:
                        destRect.X += xOffsetAsInt;
                        destRect.Y += yOffsetAsInt;
                        spriteRenderer.Draw(mTextures[pageIndex], destRect, sourceRect, color, this);
                    }
                }
                point.X = 0;
                lineNumber++;
            }
        }
Beispiel #2
0
        private Point DrawLines(IEnumerable <string> lines, HorizontalAlignment horizontalAlignment, object objectRequestingChange, int requiredWidth, List <int> widths, SpriteRenderer spriteRenderer)
        {
            Point point = new Point();

            int lineNumber = 0;

            foreach (string line in lines)
            {
                // scoot over to leave room for the outline
                point.X = mOutlineThickness;

                if (horizontalAlignment == HorizontalAlignment.Right)
                {
                    point.X = requiredWidth - widths[lineNumber];
                }
                else if (horizontalAlignment == HorizontalAlignment.Center)
                {
                    point.X = (requiredWidth - widths[lineNumber]) / 2;
                }

                foreach (char c in line)
                {
                    Rectangle destRect;
                    int       pageIndex;
                    var       sourceRect = GetCharacterRect(c, lineNumber, ref point, out destRect, out pageIndex);

                    spriteRenderer.Draw(mTextures[pageIndex], destRect, sourceRect, Color.White, objectRequestingChange);
                }
                point.X = 0;
                lineNumber++;
            }

            return(point);
        }
Beispiel #3
0
        public static void Render(SystemManagers managers, SpriteRenderer spriteRenderer,
                                  IRenderableIpso ipso, Texture2D texture, Color color,
                                  Rectangle?sourceRectangle   = null,
                                  bool flipVertical           = false,
                                  float rotationInDegrees     = 0,
                                  bool treat0AsFullDimensions = false,
                                  // In the case of Text objects, we send in a line rectangle, but we want the Text object to be the owner of any resulting render states
                                  object objectCausingRenering = null
                                  )
        {
            if (objectCausingRenering == null)
            {
                objectCausingRenering = ipso;
            }

            Renderer renderer = null;

            if (managers == null)
            {
                renderer = Renderer.Self;
            }
            else
            {
                renderer = managers.Renderer;
            }

            Texture2D textureToUse = texture;

            if (textureToUse == null)
            {
                textureToUse = LoaderManager.Self.InvalidTexture;

                if (textureToUse == null)
                {
                    return;
                }
            }

            SpriteEffects effects = SpriteEffects.None;

            var flipHorizontal = ipso.GetAbsoluteFlipHorizontal();
            var effectiveParentFlipHorizontal = ipso.Parent?.GetAbsoluteFlipHorizontal() ?? false;

            if (flipHorizontal)
            {
                effects |= SpriteEffects.FlipHorizontally;
                //rotationInDegrees *= -1;
            }

            var rotationInRadians = MathHelper.ToRadians(rotationInDegrees);


            float leftAbsolute = ipso.GetAbsoluteX();
            float topAbsolute  = ipso.GetAbsoluteY();

            Vector2 origin = Vector2.Zero;

            //if(flipHorizontal)
            //{
            //    var offsetX = (float)System.Math.Cos(rotationInRadians);
            //    var offsetY = (float)System.Math.Sin(rotationInRadians);
            //    origin.X = 1;



            //}

            if (flipVertical)
            {
                effects |= SpriteEffects.FlipVertically;
            }

            var modifiedColor = color;

            if (Renderer.NormalBlendState == BlendState.AlphaBlend)
            {
                // we are using premult textures, so we need to premult the color:
                var alphaRatio = color.A / 255.0f;

                modifiedColor.R = (byte)(color.R * alphaRatio);
                modifiedColor.G = (byte)(color.G * alphaRatio);
                modifiedColor.B = (byte)(color.B * alphaRatio);
            }

            if ((ipso.Width > 0 && ipso.Height > 0) || treat0AsFullDimensions == false)
            {
                Vector2 scale = Vector2.One;

                if (textureToUse == null)
                {
                    scale = new Vector2(ipso.Width, ipso.Height);
                }
                else
                {
                    float ratioWidth  = 1;
                    float ratioHeight = 1;
                    if (sourceRectangle.HasValue)
                    {
                        ratioWidth  = sourceRectangle.Value.Width / (float)textureToUse.Width;
                        ratioHeight = sourceRectangle.Value.Height / (float)textureToUse.Height;
                    }

                    scale = new Vector2(ipso.Width / (ratioWidth * textureToUse.Width),
                                        ipso.Height / (ratioHeight * textureToUse.Height));
                }

#if DEBUG
                if (textureToUse != null && textureToUse.IsDisposed)
                {
                    throw new ObjectDisposedException($"Texture is disposed.  Texture name: {textureToUse.Name}, sprite scale: {scale}, Sprite name: {ipso.Name}");
                }
#endif

                spriteRenderer.Draw(textureToUse,
                                    new Vector2(leftAbsolute, topAbsolute),
                                    sourceRectangle,
                                    modifiedColor,
                                    -rotationInRadians,
                                    origin,
                                    scale,
                                    effects,
                                    0,
                                    objectCausingRenering);
            }
            else
            {
                int width  = textureToUse.Width;
                int height = textureToUse.Height;

                if (sourceRectangle != null && sourceRectangle.HasValue)
                {
                    width  = sourceRectangle.Value.Width;
                    height = sourceRectangle.Value.Height;
                }

                Rectangle destinationRectangle = new Rectangle(
                    (int)(leftAbsolute),
                    (int)(topAbsolute),
                    width,
                    height);


                spriteRenderer.Draw(textureToUse,
                                    destinationRectangle,
                                    sourceRectangle,
                                    modifiedColor,
                                    rotationInRadians,
                                    origin,
                                    effects,
                                    0,
                                    objectCausingRenering
                                    );
            }
        }
Beispiel #4
0
        private Point DrawLines(IEnumerable<string> lines, HorizontalAlignment horizontalAlignment, object objectRequestingChange, int requiredWidth, List<int> widths, SpriteRenderer spriteRenderer)
        {
            Point point = new Point();

            int lineNumber = 0;

            foreach (string line in lines)
            {
                // scoot over to leave room for the outline
                point.X = mOutlineThickness;

                if (horizontalAlignment == HorizontalAlignment.Right)
                {
                    point.X = requiredWidth - widths[lineNumber];
                }
                else if (horizontalAlignment == HorizontalAlignment.Center)
                {
                    point.X = (requiredWidth - widths[lineNumber]) / 2;
                }

                foreach (char c in line)
                {
                    Rectangle destRect;
                    int pageIndex;
                    var sourceRect = GetCharacterRect(c, lineNumber, ref point, out destRect, out pageIndex);

                    spriteRenderer.Draw(mTextures[pageIndex], destRect, sourceRect, Color.White, objectRequestingChange);
                }
                point.X = 0;
                lineNumber++;
            }

            return point;
        }
Beispiel #5
0
        public static void Render(SystemManagers managers, SpriteRenderer spriteRenderer,
            IRenderableIpso ipso, Texture2D texture, Color color,
            Rectangle? sourceRectangle = null,
            bool flipHorizontal = false,
            bool flipVertical = false,
            float rotationInDegrees = 0,
            bool treat0AsFullDimensions = false,
            // In the case of Text objects, we send in a line rectangle, but we want the Text object to be the owner of any resulting render states
            object objectCausingRenering = null
            )
        {
            if (objectCausingRenering == null)
            {
                objectCausingRenering = ipso;
            }

            Renderer renderer = null;
            if (managers == null)
            {
                renderer = Renderer.Self;
            }
            else
            {
                renderer = managers.Renderer;
            }

            Texture2D textureToUse = texture;

            if (textureToUse == null)
            {
                textureToUse = LoaderManager.Self.InvalidTexture;

                if (textureToUse == null)
                {
                    return;
                }
            }

            SpriteEffects effects = SpriteEffects.None;
            if (flipHorizontal)
            {
                effects |= SpriteEffects.FlipHorizontally;
            }
            if (flipVertical)
            {
                effects |= SpriteEffects.FlipVertically;
            }

            var modifiedColor = color;

            if (Renderer.NormalBlendState == BlendState.AlphaBlend)
            {
                // we are using premult textures, so we need to premult the color:
                var alphaRatio = color.A / 255.0f;

                modifiedColor.R = (byte)(color.R * alphaRatio);
                modifiedColor.G = (byte)(color.G * alphaRatio);
                modifiedColor.B = (byte)(color.B * alphaRatio);
            }

            if ((ipso.Width > 0 && ipso.Height > 0) || treat0AsFullDimensions == false)
            {
                Vector2 scale = Vector2.One;

                if (textureToUse == null)
                {
                    scale = new Vector2(ipso.Width, ipso.Height);
                }
                else
                {
                    float ratioWidth = 1;
                    float ratioHeight = 1;
                    if (sourceRectangle.HasValue)
                    {
                        ratioWidth = sourceRectangle.Value.Width / (float)textureToUse.Width;
                        ratioHeight = sourceRectangle.Value.Height / (float)textureToUse.Height;
                    }

                    scale = new Vector2(ipso.Width / (ratioWidth * textureToUse.Width),
                        ipso.Height / (ratioHeight * textureToUse.Height));
                }

                if (textureToUse != null && textureToUse.IsDisposed)
                {
                    throw new ObjectDisposedException("Texture is disposed.  Texture name: " + textureToUse.Name + ", sprite scale: " + scale);
                }

                spriteRenderer.Draw(textureToUse,
                    new Vector2(ipso.GetAbsoluteX(), ipso.GetAbsoluteY()),
                    sourceRectangle,
                    modifiedColor,
                    Microsoft.Xna.Framework.MathHelper.TwoPi * -rotationInDegrees / 360.0f,
                    Vector2.Zero,
                    scale,
                    effects,
                    0,
                    objectCausingRenering);
            }
            else
            {
                int width = textureToUse.Width;
                int height = textureToUse.Height;

                if (sourceRectangle != null && sourceRectangle.HasValue)
                {
                    width = sourceRectangle.Value.Width;
                    height = sourceRectangle.Value.Height;
                }

                Rectangle destinationRectangle = new Rectangle(
                    (int)(ipso.GetAbsoluteX()),
                    (int)(ipso.GetAbsoluteY()),
                    width,
                    height);


                spriteRenderer.Draw(textureToUse,
                    destinationRectangle,
                    sourceRectangle,
                    modifiedColor,
                    rotationInDegrees / 360.0f,
                    Vector2.Zero,
                    effects,
                    0,
                    objectCausingRenering
                    );
            }
        }
Beispiel #6
0
        public static void Render(SystemManagers managers, SpriteRenderer spriteRenderer,
                                  IRenderableIpso ipso, Texture2D texture, Color color,
                                  Rectangle?sourceRectangle   = null,
                                  bool flipHorizontal         = false,
                                  bool flipVertical           = false,
                                  float rotationInDegrees     = 0,
                                  bool treat0AsFullDimensions = false,
                                  // In the case of Text objects, we send in a line rectangle, but we want the Text object to be the owner of any resulting render states
                                  object objectCausingRenering = null
                                  )
        {
            if (objectCausingRenering == null)
            {
                objectCausingRenering = ipso;
            }

            Renderer renderer = null;

            if (managers == null)
            {
                renderer = Renderer.Self;
            }
            else
            {
                renderer = managers.Renderer;
            }

            Texture2D textureToUse = texture;

            if (textureToUse == null)
            {
                textureToUse = LoaderManager.Self.InvalidTexture;

                if (textureToUse == null)
                {
                    return;
                }
            }

            SpriteEffects effects = SpriteEffects.None;

            if (flipHorizontal)
            {
                effects |= SpriteEffects.FlipHorizontally;
            }
            if (flipVertical)
            {
                effects |= SpriteEffects.FlipVertically;
            }

            var modifiedColor = color;

            if (Renderer.NormalBlendState == BlendState.AlphaBlend)
            {
                // we are using premult textures, so we need to premult the color:
                var alphaRatio = color.A / 255.0f;

                modifiedColor.R = (byte)(color.R * alphaRatio);
                modifiedColor.G = (byte)(color.G * alphaRatio);
                modifiedColor.B = (byte)(color.B * alphaRatio);
            }

            if ((ipso.Width > 0 && ipso.Height > 0) || treat0AsFullDimensions == false)
            {
                Vector2 scale = Vector2.One;

                if (textureToUse == null)
                {
                    scale = new Vector2(ipso.Width, ipso.Height);
                }
                else
                {
                    float ratioWidth  = 1;
                    float ratioHeight = 1;
                    if (sourceRectangle.HasValue)
                    {
                        ratioWidth  = sourceRectangle.Value.Width / (float)textureToUse.Width;
                        ratioHeight = sourceRectangle.Value.Height / (float)textureToUse.Height;
                    }

                    scale = new Vector2(ipso.Width / (ratioWidth * textureToUse.Width),
                                        ipso.Height / (ratioHeight * textureToUse.Height));
                }

                if (textureToUse != null && textureToUse.IsDisposed)
                {
                    throw new ObjectDisposedException("Texture is disposed.  Texture name: " + textureToUse.Name + ", sprite scale: " + scale);
                }

                spriteRenderer.Draw(textureToUse,
                                    new Vector2(ipso.GetAbsoluteX(), ipso.GetAbsoluteY()),
                                    sourceRectangle,
                                    modifiedColor,
                                    Microsoft.Xna.Framework.MathHelper.TwoPi * -rotationInDegrees / 360.0f,
                                    Vector2.Zero,
                                    scale,
                                    effects,
                                    0,
                                    objectCausingRenering);
            }
            else
            {
                int width  = textureToUse.Width;
                int height = textureToUse.Height;

                if (sourceRectangle != null && sourceRectangle.HasValue)
                {
                    width  = sourceRectangle.Value.Width;
                    height = sourceRectangle.Value.Height;
                }

                Rectangle destinationRectangle = new Rectangle(
                    (int)(ipso.GetAbsoluteX()),
                    (int)(ipso.GetAbsoluteY()),
                    width,
                    height);


                spriteRenderer.Draw(textureToUse,
                                    destinationRectangle,
                                    sourceRectangle,
                                    modifiedColor,
                                    rotationInDegrees / 360.0f,
                                    Vector2.Zero,
                                    effects,
                                    0,
                                    objectCausingRenering
                                    );
            }
        }
Beispiel #7
0
        public void DrawTextLines(List <string> lines, HorizontalAlignment horizontalAlignment, object objectRequestingChange, int requiredWidth, List <int> widths, SpriteRenderer spriteRenderer,
                                  Color color,
                                  float xOffset = 0, float yOffset = 0, float rotation = 0, float scaleX = 1, float scaleY = 1)
        {
            var point = new Vector2();

            int lineNumber = 0;

            int xOffsetAsInt = MathFunctions.RoundToInt(xOffset);
            int yOffsetAsInt = MathFunctions.RoundToInt(yOffset);

            if (Renderer.NormalBlendState == BlendState.AlphaBlend)
            {
                // this is premultiplied, so premulitply the color value
                float multiple = color.A / 255.0f;

                color.R = (byte)(color.R * multiple);
                color.G = (byte)(color.G * multiple);
                color.B = (byte)(color.B * multiple);
            }

            var rotationRadians = MathHelper.ToRadians(rotation);

            Vector2 xAxis = Vector2.UnitX;
            Vector2 yAxis = Vector2.UnitY;

            if (rotation != 0)
            {
                xAxis.X = (float)System.Math.Cos(-rotationRadians);
                xAxis.Y = (float)System.Math.Sin(-rotationRadians);

                yAxis.X = (float)System.Math.Cos(-rotationRadians + MathHelper.PiOver2);
                yAxis.Y = (float)System.Math.Sin(-rotationRadians + MathHelper.PiOver2);
            }

            foreach (string line in lines)
            {
                // scoot over to leave room for the outline
                point.X = mOutlineThickness;

                if (horizontalAlignment == HorizontalAlignment.Right)
                {
                    point.X = requiredWidth - widths[lineNumber];
                }
                else if (horizontalAlignment == HorizontalAlignment.Center)
                {
                    point.X = (requiredWidth - widths[lineNumber]) / 2;
                }


                foreach (char c in line)
                {
                    FloatRectangle destRect;
                    int            pageIndex;
                    var            sourceRect = GetCharacterRect(c, lineNumber, ref point, out destRect, out pageIndex, scaleX);

                    var finalPosition = destRect.X * xAxis + destRect.Y * yAxis;

                    finalPosition.X += xOffset;
                    finalPosition.Y += yOffset;

                    if (Text.TextRenderingPositionMode == TextRenderingPositionMode.FreeFloating ||
                        // If rotated, need free floating positions since sprite positions will likely not line up with pixels
                        rotation != 0 ||
                        // If scaled up/down, don't use free floating
                        scaleX != 1)
                    {
                        var scale = new Vector2(scaleX, scaleY);
                        spriteRenderer.Draw(mTextures[pageIndex], finalPosition, sourceRect, color, -rotationRadians, Vector2.Zero, scale, SpriteEffects.None, 0, this);
                    }
                    else
                    {
                        // position:
                        destRect.X += xOffsetAsInt;
                        destRect.Y += yOffsetAsInt;

                        var position = new Vector2(destRect.X, destRect.Y);

                        spriteRenderer.Draw(mTextures[pageIndex], position, sourceRect, color, 0, Vector2.Zero, new Vector2(scaleX, scaleY), SpriteEffects.None, 0, this);
                    }
                }
                point.X = 0;
                lineNumber++;
            }
        }
Beispiel #8
0
        public void Render(SpriteRenderer spriteRenderer, SystemManagers managers, Texture2D textureToUse, float repetitionsPerLength)
        {
            if (mVectors.Count < 2)
                return;

            Renderer renderer;
            if (managers == null)
            {
                renderer = Renderer.Self;
            }
            else
            {
                renderer = managers.Renderer;
            }

            Vector2 offset = new Vector2(renderer.Camera.RenderingXOffset, renderer.Camera.RenderingYOffset);

            int extraStep = 0;
            if (BreakIntoSegments)
            {
                extraStep = 1;
            }
            for (int i = 1; i < mVectors.Count; i++)
            {
                Vector2 vector1 = mVectors[i - 1];
                Vector2 vector2 = mVectors[i];

                // calculate the distance between the two vectors
                float distance = Vector2.Distance(vector1, vector2);

                int repetitions = (int)(distance * repetitionsPerLength);

                if (repetitions < 1)
                {
                    repetitions = 1;
                }

                //repetitions = 128;

                // calculate the angle between the two vectors
                float angle = (float)System.Math.Atan2((double)(vector2.Y - vector1.Y),
                    (double)(vector2.X - vector1.X));

                Rectangle sourceRectangle = new Rectangle(
                    0, 
                    0, 
                    textureToUse.Width * repetitions, 
                    textureToUse.Height);

                // stretch the pixel between the two vectors
                spriteRenderer.Draw(textureToUse,
                    offset + Position + vector1,
                    sourceRectangle,
                    Color,
                    angle,
                    Vector2.Zero,
                    new Vector2(distance / ((float)repetitions * textureToUse.Width), 1/renderer.CurrentZoom),
                    SpriteEffects.None,
                    Depth,
                    this);

                i += extraStep;
            }
        }