예제 #1
0
        public static Vector2 DrawDecimal(this SpriteBatch spriteBatch, SpriteFont spriteFont, float value, uint decimalPlaces, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth)
        {
            if (spriteBatch == null)
            {
                throw new ArgumentNullException("spriteBatch");
            }
            if (spriteFont == null)
            {
                throw new ArgumentNullException("spriteFont");
            }

            // Draw the integer value
            Vector2 nextPosition = spriteBatch.DrawInteger(spriteFont, XenMath.RoundFloatToInt(value), position, color, rotation, origin, scale, effects, layerDepth);

            if (decimalPlaces > 0)
            {
                spriteBatch.DrawString(spriteFont, ".", nextPosition, color, rotation, origin, scale, effects, layerDepth);
                nextPosition += spriteFont.MeasureString(".") * Vector2.UnitX;
                // Draw each decimal digit afterwards one-by-one because otherwise the leading-0's disappear
                for (int i = 0; i < decimalPlaces; i++)
                {
                    value        = (value - (int)value) * 10;
                    nextPosition = spriteBatch.DrawInteger(spriteFont, (int)value, nextPosition, color, rotation, origin, scale, effects, layerDepth);
                }
            }
            return(nextPosition);
        }
예제 #2
0
        internal static Vector2 MeasureDecimalString(SpriteFont spriteFont, float value, uint decimalPlaces)
        {
            if (spriteFont == null)
            {
                throw new ArgumentNullException("spriteFont");
            }

            Vector2 size = SpriteBatchEx.MeasureIntegerString(spriteFont, XenMath.RoundFloatToInt(value));

            if (decimalPlaces > 0)
            {
                Vector2 strVec = spriteFont.MeasureString(".");
                size.X += strVec.X;
                size.Y  = Math.Max(size.Y, strVec.Y);

                // Draw each decimal digit afterwards one-by-one because otherwise the leading-0's disappear
                for (int i = 0; i < decimalPlaces; i++)
                {
                    value   = (value - (int)value) * 10;
                    strVec  = SpriteBatchEx.MeasureIntegerString(spriteFont, (int)value);
                    size.X += strVec.X;
                    size.Y  = Math.Max(size.Y, strVec.Y);
                }
            }
            return(size);
        }
예제 #3
0
        public static void Draw(this SpriteBatch spriteBatch, Texture2D texture, Vector2 position, Rectangle?sourceRectangle, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth, Matrix transformFromWorldToCamera, RenderParams renderParams)
        {
            position = Vector2.Transform(position, transformFromWorldToCamera);

            Vector3    vecScale, translate;
            Quaternion rotate;

            transformFromWorldToCamera.Decompose(out vecScale, out rotate, out translate);
            transformFromWorldToCamera.Translation = Vector3.Zero;

            float angle = rotation + XenMath.GetAngleFloat(Vector2.Transform(Vector2.UnitX, transformFromWorldToCamera));

            spriteBatch.Draw(
                texture,                                     // The sprite texture.
                position,                                    // The location, in screen coordinates, where the sprite will be drawn.
                sourceRectangle,                             // A rectangle specifying, in texels, which section of the rectangle to draw.  Use null to draw the entire texture.
                color,                                       // The color channel modulation to use. Use Color.White for full color with no tinting.
                angle,                                       // The angle, in radians, to rotate the sprite around the origin.
                origin,                                      // The origin of the sprite. Specify (0,0) for the upper-left corner.
                new Vector2(vecScale.X, vecScale.Y) * scale, // Vector containing separate scalar multiples for the x- and y-axes of the sprite.
                effects,                                     // Rotations to apply before rendering.
                layerDepth);                                 // The sorting depth of the sprite, between 0 (front) and 1 (back). You must
                                                             // specify either SpriteSortMode.FrontToBack or SpriteSortMode.BackToFront for
                                                             // this parameter to affect sprite drawing.
        }
예제 #4
0
        private static void DrawSpriteInternal(this SpriteBatch spriteBatch, ISprite sprite, Matrix transformFromWorldToCamera, RenderParams renderParams)
        {
            Matrix  transformFromSpriteToWorld = sprite.RenderingExtent.TranslateFrom;
            Matrix  fromSpriteToCamera         = transformFromSpriteToWorld * transformFromWorldToCamera;
            Vector2 position = Vector2.Transform(sprite.RenderingExtent.ReferenceTopLeft, fromSpriteToCamera);

            Vector3    scale, translate;
            Quaternion rotate;

            fromSpriteToCamera.Decompose(out scale, out rotate, out translate);
            fromSpriteToCamera.Translation = Vector3.Zero;

            float angle = XenMath.GetAngleFloat(Vector2.Transform(Vector2.UnitX, fromSpriteToCamera));

            spriteBatch.Draw(
                sprite.TextureInfo.Asset,               // The sprite texture.
                position,                               // The location, in screen coordinates, where the sprite will be drawn.
                sprite.TextureInfo.SourceRectangle,     // A rectangle specifying, in texels, which section of the rectangle to draw.  Use null to draw the entire texture.
                sprite.ModulationColorWithOpacity,      // The color channel modulation to use. Use Color.White for full color with no tinting.
                angle,                                  // The angle, in radians, to rotate the sprite around the origin.
                Vector2.Zero,                           // The origin of the sprite. Specify (0,0) for the upper-left corner.
                new Vector2(scale.X, scale.Y),          // Vector containing separate scalar multiples for the x- and y-axes of the sprite.
                sprite.SpriteEffects,                   // Rotations to apply before rendering.
                sprite.LayerDepth);                     // The sorting depth of the sprite, between 0 (front) and 1 (back). You must
                                                        // specify either SpriteSortMode.FrontToBack or SpriteSortMode.BackToFront for
                                                        // this parameter to affect sprite drawing.
        }
예제 #5
0
        public static void DrawLine(this SpriteBatch spriteBatch, Color color, Vector2 start, Vector2 stop, int thickness, I2DDisplayModifiers attributes, Matrix transformFromWorldToCamera)
        {
            //float scale = Camera.GetScale( 1, Camera.Zoom, attributes.ParallaxDepth );
            //Vector2 position = Camera.TranslateAbsoluteVectorToCamera( start, attributes.ParallaxDepth );
            float originalLength = Vector2.Distance(start, stop);

            start = Vector2.Transform(start, transformFromWorldToCamera);
            stop  = Vector2.Transform(stop, transformFromWorldToCamera);
            float newLength = Vector2.Distance(start, stop);
            float scale     = newLength / originalLength; //originalLength might be 0

            thickness = (int)Math.Max(1, thickness * scale);
            Vector2 position = start;

            _destinationRect.X     = (int)position.X;
            _destinationRect.Y     = (int)(position.Y + (float)thickness / 2.0f);
            _destinationRect.Width = (int)(Math.Ceiling(Vector2.Distance(start, stop)));
            if (_destinationRect.Width == 0)
            {
                _destinationRect.Width = 1;
            }
            _destinationRect.Height = thickness;

            SinglePixel pixel = SinglePixel.WhitePixel;
            float       angle = XenMath.GetAngleFloat(stop - start);

            spriteBatch.Draw(
                pixel.Asset,
                _destinationRect,
                pixel.SourceRectangle,
#if SILVERLIGHT
                new Color(color, attributes.OpacityFinal),
#else
                color *attributes.OpacityFinal,
#endif
                angle,
                Vector2.UnitY / 2.0f,
                attributes.SpriteEffects,
                attributes.LayerDepth);
        }
예제 #6
0
        /// <summary>
        /// Extension method for SpriteBatch that draws a string without allocating
        /// any memory. This function avoids garbage collections that are normally caused
        /// by creating strings
        /// </summary>
        /// <param name="spriteBatch">The SpriteBatch instance whose DrawString method will be invoked.</param>
        /// <param name="spriteFont">The SpriteFont to draw the text with.</param>
        /// <param name="text">The text to draw.</param>
        /// <param name="transformFromWorldToCamera">The matrix transformation to bring the world to camera-space.</param>
        public static void DrawString(
            this SpriteBatch spriteBatch,
            SpriteFont spriteFont,
            XenString text,
            Matrix transformFromWorldToCamera
            )
        {
            SpriteFont font = (spriteFont != null ? spriteFont : text.SpriteFont);

            if (font == null)
            {
                throw new ArgumentNullException("spriteFont");
            }

            Matrix  transformFromTextToWorld = text.RenderingExtent.TranslateFrom;
            Matrix  fromTextToCamera         = transformFromTextToWorld * transformFromWorldToCamera;
            Vector2 position = Vector2.Transform(text.RenderingExtent.ReferenceTopLeft, fromTextToCamera);

            Vector3    scale, translate;
            Quaternion rotate;

            fromTextToCamera.Decompose(out scale, out rotate, out translate);
            fromTextToCamera.Translation = Vector3.Zero;

            float angle = XenMath.GetAngleFloat(Vector2.Transform(Vector2.UnitX, fromTextToCamera));

            Vector2 nextPosition = position;

            switch (text._type)
            {
            case XenString.XenStringType.String:
                spriteBatch.DrawString(
                    font,
                    text.Text,
                    position,
                    text.ModulationColorWithOpacity,
                    angle,
                    Vector2.Zero,
                    new Vector2(scale.X, scale.Y),
                    text.SpriteEffects,
                    text.LayerDepth);
                nextPosition += font.MeasureString(text.Text) * Vector2.Transform(Vector2.UnitX, Matrix.CreateRotationZ(angle));
                break;

            case XenString.XenStringType.Integer:
                nextPosition = spriteBatch.DrawInteger(
                    font,
                    text.ValueInt,
                    position,
                    text.ModulationColorWithOpacity,
                    angle,
                    Vector2.Zero,
                    new Vector2(scale.X, scale.Y),
                    text.SpriteEffects,
                    text.LayerDepth);
                break;

            case XenString.XenStringType.Float:
                nextPosition = spriteBatch.DrawDecimal(
                    font,
                    text.ValueFloat,
                    text.FloatDecimalPlaces,
                    position,
                    text.ModulationColorWithOpacity,
                    angle,
                    Vector2.Zero,
                    new Vector2(scale.X, scale.Y),
                    text.SpriteEffects,
                    text.LayerDepth);
                break;
            }
            if (text._nextString != null)
            {
                spriteBatch.DrawString(
                    spriteFont,
                    text._nextString,
                    nextPosition,
                    text.ModulationColorWithOpacity,
                    angle,
                    Vector2.Zero,
                    new Vector2(scale.X, scale.Y),
                    text.SpriteEffects, text.LayerDepth);
            }
            if (text.IsTemporary)
            {
                text.Release();
            }
        }
예제 #7
0
 public void MoveVertical(float amount)
 {
     Anchor = Anchor + XenMath.Rotate(new Vector2(0, amount), Angle);
 }
예제 #8
0
 public void MoveHorizontal(float amount)
 {
     Anchor = Anchor + XenMath.Rotate(new Vector2(amount, 0), Angle);
 }
예제 #9
0
 public static Vector2 TransformAndRound(Vector2 vectorToTransform, Matrix transformMatrix)
 {
     return(XenMath.RoundVector(Vector2.Transform(vectorToTransform, transformMatrix)));
 }