Beispiel #1
0
        public SvgScale(float x, float y)
        {
            _scaleX = x;
            _scaleY = y;

            Matrix = Matrix2D.CreateScale(_scaleX, _scaleY);
        }
Beispiel #2
0
        internal override void RecalculateBounds(Collider collider)
        {
            // if we dont have rotation or dont care about TRS we use localOffset as the center so we'll start with that
            center = collider.LocalOffset;

            if (collider.ShouldColliderScaleAndRotateWithTransform)
            {
                var      hasUnitScale = true;
                Matrix2D tempMat;
                var      combinedMatrix = Matrix2D.CreateTranslation(-_polygonCenter);

                if (collider.Entity.Transform.Scale != Vector2.One)
                {
                    Matrix2D.CreateScale(collider.Entity.Transform.Scale.X, collider.Entity.Transform.Scale.Y,
                                         out tempMat);
                    Matrix2D.Multiply(ref combinedMatrix, ref tempMat, out combinedMatrix);

                    hasUnitScale = false;

                    // scale our offset and set it as center. If we have rotation also it will be reset below
                    var scaledOffset = collider.LocalOffset * collider.Entity.Transform.Scale;
                    center = scaledOffset;
                }

                if (collider.Entity.Transform.Rotation != 0)
                {
                    Matrix2D.CreateRotation(collider.Entity.Transform.Rotation, out tempMat);
                    Matrix2D.Multiply(ref combinedMatrix, ref tempMat, out combinedMatrix);

                    // to deal with rotation with an offset origin we just move our center in a circle around 0,0 with our offset making the 0 angle
                    // we have to deal with scale here as well so we scale our offset to get the proper length first.
                    var offsetAngle  = Mathf.Atan2(collider.LocalOffset.Y, collider.LocalOffset.X) * Mathf.Rad2Deg;
                    var offsetLength = hasUnitScale
                                                ? collider._localOffsetLength
                                                : (collider.LocalOffset * collider.Entity.Transform.Scale).Length();
                    center = Mathf.PointOnCircle(Vector2.Zero, offsetLength,
                                                 collider.Entity.Transform.RotationDegrees + offsetAngle);
                }

                Matrix2D.CreateTranslation(ref _polygonCenter, out tempMat);                 // translate back center
                Matrix2D.Multiply(ref combinedMatrix, ref tempMat, out combinedMatrix);

                // finaly transform our original points
                Vector2Ext.Transform(_originalPoints, ref combinedMatrix, Points);

                IsUnrotated = collider.Entity.Transform.Rotation == 0;

                // we only need to rebuild our edge normals if we rotated
                if (collider._isRotationDirty)
                {
                    _areEdgeNormalsDirty = true;
                }
            }

            position         = collider.Entity.Transform.Position + center;
            bounds           = RectangleF.RectEncompassingPoints(Points);
            bounds.Location += position;
        }
 private void RecalculateLocalMatrix(out Matrix2D matrix)
 {
     if (Parent != null)
     {
         var parentPosition = Parent.Position;
         matrix = Matrix2D.CreateTranslation(-parentPosition) * Matrix2D.CreateScale(_scale) *
                  Matrix2D.CreateRotationZ(-_rotation) * Matrix2D.CreateTranslation(parentPosition) *
                  Matrix2D.CreateTranslation(_position);
     }
     else
     {
         matrix = Matrix2D.CreateScale(_scale) * Matrix2D.CreateRotationZ(-_rotation) *
                  Matrix2D.CreateTranslation(_position);
     }
 }
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            Engines = new List <GameEngine>();

            Width  = GraphicsDevice.PresentationParameters.BackBufferWidth;
            Height = GraphicsDevice.PresentationParameters.BackBufferHeight;

            WidthScale  = Width / Constants.SampleWindowWidth;
            HeightScale = Height / Constants.SampleWindowHeight;

            // Scale = Matrix2D.CreateScale((float)WidthScale, (float)HeightScale);
            Scale = Matrix2D.CreateScale((float)ScaleFactor);

            int h = 64;
            int w = 64;
            int x = Constants.SampleWindowWidth / 2 - w / 2;

            StartButton    = new Button(x, 10, w, h);
            SettingsButton = new Button(x, 90, w, h);

            SettingsButton.GraphicsDevice = StartButton.GraphicsDevice = this.GraphicsDevice;

            LevelButtons = new List <Button>();
            w            = Constants.SampleWindowWidth / Levels - 15;

            for (int i = 0; i < Levels; i++)
            {
                LevelButtons.Add(new Button(15 + i * (w + 5), Constants.SampleWindowHeight / 2 - 15, w, 30));
                LevelButtons[i].GraphicsDevice = GraphicsDevice;
            }

            CurrentSettingsUI = new SettingsUI();

            FontManager.Initialize(Content);

            if (Settings.ShowTutorial)
            {
                TextBox        = new TutorialBox(Constants.SampleWindowWidth / 4, Constants.SampleWindowHeight / 4);
                TutorialEngine = new GameEngine(GraphicsDevice);
            }

            base.Initialize();
        }
Beispiel #5
0
        /// <summary>
        /// Returns the transform for this group's coordinate system
        /// </summary>
        /// <returns>The transform.</returns>
        protected Matrix2D ComputeTransform()
        {
            var mat = Matrix2D.Identity;

            if (originX != 0 || originY != 0)
            {
                mat = Matrix2D.Multiply(mat, Matrix2D.CreateTranslation(-originX, -originY));
            }

            if (rotation != 0)
            {
                mat = Matrix2D.Multiply(mat, Matrix2D.CreateRotation(MathHelper.ToRadians(rotation)));
            }

            if (scaleX != 1 || scaleY != 1)
            {
                mat = Matrix2D.Multiply(mat, Matrix2D.CreateScale(scaleX, scaleY));
            }

            mat = Matrix2D.Multiply(mat, Matrix2D.CreateTranslation(x + originX, y + originY));

            // Find the first parent that transforms
            Group parentGroup = parent;

            while (parentGroup != null)
            {
                if (parentGroup.transform)
                {
                    break;
                }

                parentGroup = parentGroup.parent;
            }

            if (parentGroup != null)
            {
                mat = Matrix2D.Multiply(mat, parentGroup.ComputeTransform());
            }

            return(mat);
        }
Beispiel #6
0
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            GraphicsDevice.SetRenderTarget(rend);
            GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin(transformMatrix: camera.GetViewMatrix());
            map.draw(spriteBatch, this);
            gui.draw(spriteBatch, this);


            spriteBatch.End();

            GraphicsDevice.SetRenderTarget(null);

            spriteBatch.Begin(transformMatrix: Matrix2D.CreateScale(1.0f / scale));
            spriteBatch.Draw(rend, new Vector2(0));
            spriteBatch.End();
            base.Draw(gameTime);
        }
Beispiel #7
0
        public void DrawInto(Batcher batcher, ref FontCharacterSource text, Vector2 position, Color color,
                             float rotation, Vector2 origin, Vector2 scale, SpriteEffects effect, float depth)
        {
            var flipAdjustment = Vector2.Zero;

            var flippedVert = (effect & SpriteEffects.FlipVertically) == SpriteEffects.FlipVertically;
            var flippedHorz = (effect & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally;

            if (flippedVert || flippedHorz)
            {
                var size = MeasureString(ref text);

                if (flippedHorz)
                {
                    origin.X        *= -1;
                    flipAdjustment.X = -size.X;
                }

                if (flippedVert)
                {
                    origin.Y        *= -1;
                    flipAdjustment.Y = LineHeight - size.Y;
                }
            }


            var requiresTransformation = flippedHorz || flippedVert || rotation != 0f || scale != new Vector2(1);

            if (requiresTransformation)
            {
                Matrix2D temp;
                Matrix2D.CreateTranslation(-origin.X, -origin.Y, out _transformationMatrix);
                Matrix2D.CreateScale((flippedHorz ? -scale.X : scale.X), (flippedVert ? -scale.Y : scale.Y), out temp);
                Matrix2D.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
                Matrix2D.CreateTranslation(flipAdjustment.X, flipAdjustment.Y, out temp);
                Matrix2D.Multiply(ref temp, ref _transformationMatrix, out _transformationMatrix);
                Matrix2D.CreateRotation(rotation, out temp);
                Matrix2D.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
                Matrix2D.CreateTranslation(position.X, position.Y, out temp);
                Matrix2D.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
            }

            var       previousCharacter = ' ';
            Character currentChar       = null;
            var       offset            = requiresTransformation ? Vector2.Zero : position - origin;

            for (var i = 0; i < text.Length; ++i)
            {
                var c = text[i];
                if (c == '\r')
                {
                    continue;
                }

                if (c == '\n')
                {
                    offset.X    = requiresTransformation ? 0f : position.X - origin.X;
                    offset.Y   += LineHeight;
                    currentChar = null;
                    continue;
                }

                if (currentChar != null)
                {
                    offset.X += Spacing.X + currentChar.XAdvance;
                }

                currentChar = ContainsCharacter(c) ? this[c] : DefaultCharacter;

                var p = offset;

                if (flippedHorz)
                {
                    p.X += currentChar.Bounds.Width;
                }
                p.X += currentChar.Offset.X + GetKerning(previousCharacter, currentChar.Char);

                if (flippedVert)
                {
                    p.Y += currentChar.Bounds.Height - LineHeight;
                }
                p.Y += currentChar.Offset.Y;

                // transform our point if we need to
                if (requiresTransformation)
                {
                    Vector2Ext.Transform(ref p, ref _transformationMatrix, out p);
                }

                var destRect = RectangleExt.FromFloats
                               (
                    p.X, p.Y,
                    currentChar.Bounds.Width * scale.X,
                    currentChar.Bounds.Height * scale.Y
                               );

                batcher.Draw(Textures[currentChar.TexturePage], destRect, currentChar.Bounds, color, rotation, Vector2.Zero, effect, depth);
                previousCharacter = c;
            }
        }
Beispiel #8
0
        /// <summary>
        /// old SpriteBatch drawing method. This should probably be removed since SpriteBatch was replaced by Batcher
        /// </summary>
        /// <param name="spriteBatch">Sprite batch.</param>
        /// <param name="text">Text.</param>
        /// <param name="position">Position.</param>
        /// <param name="color">Color.</param>
        /// <param name="rotation">Rotation.</param>
        /// <param name="origin">Origin.</param>
        /// <param name="scale">Scale.</param>
        /// <param name="effect">Effect.</param>
        /// <param name="depth">Depth.</param>
        internal void drawInto(SpriteBatch spriteBatch, ref FontCharacterSource text, Vector2 position, Color color,
                               float rotation, Vector2 origin, Vector2 scale, SpriteEffects effect, float depth)
        {
            var flipAdjustment = Vector2.Zero;

            var flippedVert = (effect & SpriteEffects.FlipVertically) == SpriteEffects.FlipVertically;
            var flippedHorz = (effect & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally;

            if (flippedVert || flippedHorz)
            {
                Vector2 size;
                measureString(ref text, out size);

                if (flippedHorz)
                {
                    origin.X        *= -1;
                    flipAdjustment.X = -size.X;
                }

                if (flippedVert)
                {
                    origin.Y        *= -1;
                    flipAdjustment.Y = lineHeight - size.Y;
                }
            }


            var requiresTransformation = flippedHorz || flippedVert || rotation != 0f || scale != Vector2.One;

            if (requiresTransformation)
            {
                Matrix2D temp;
                Matrix2D.CreateTranslation(-origin.X, -origin.Y, out _transformationMatrix);
                Matrix2D.CreateScale((flippedHorz ? -scale.X : scale.X), (flippedVert ? -scale.Y : scale.Y), out temp);
                Matrix2D.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
                Matrix2D.CreateTranslation(flipAdjustment.X, flipAdjustment.Y, out temp);
                Matrix2D.Multiply(ref temp, ref _transformationMatrix, out _transformationMatrix);
                Matrix2D.CreateRotationZ(rotation, out temp);
                Matrix2D.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
                Matrix2D.CreateTranslation(position.X, position.Y, out temp);
                Matrix2D.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
            }

            BitmapFontRegion currentFontRegion = null;
            var offset = requiresTransformation ? Vector2.Zero : position - origin;

            for (var i = 0; i < text.Length; ++i)
            {
                var c = text[i];
                if (c == '\r')
                {
                    continue;
                }

                if (c == '\n')
                {
                    offset.X          = requiresTransformation ? 0f : position.X - origin.X;
                    offset.Y         += lineHeight;
                    currentFontRegion = null;
                    continue;
                }

                if (currentFontRegion != null)
                {
                    offset.X += spacing + currentFontRegion.xAdvance;
                }

                if (!_characterMap.TryGetValue(c, out currentFontRegion))
                {
                    currentFontRegion = defaultCharacterRegion;
                }


                var p = offset;

                if (flippedHorz)
                {
                    p.X += currentFontRegion.width;
                }
                p.X += currentFontRegion.xOffset;

                if (flippedVert)
                {
                    p.Y += currentFontRegion.height - lineHeight;
                }
                p.Y += currentFontRegion.yOffset;

                // transform our point if we need to
                if (requiresTransformation)
                {
                    Vector2Ext.Transform(ref p, ref _transformationMatrix, out p);
                }

                var destRect = RectangleExt.fromFloats
                               (
                    p.X, p.Y,
                    currentFontRegion.width * scale.X,
                    currentFontRegion.height * scale.Y
                               );

                spriteBatch.Draw(currentFontRegion.subtexture, destRect, currentFontRegion.subtexture.sourceRect, color, rotation, Vector2.Zero, effect, depth);
            }
        }
 private void RecalculateLocalMatrix(out Matrix2D matrix)
 {
     matrix = Matrix2D.CreateScale(_scale) *
              Matrix2D.CreateRotationZ(_rotation) *
              Matrix2D.CreateTranslation(_position);
 }