/// <summary> /// Resets the RenderState to the default settings. /// (Check each property documentation for its default value.) /// </summary> public void Reset() { Alpha = 1.0f; BlendMode = Display.BlendMode.NORMAL; RenderTarget = null; ClipRect = null; if (_modelviewMatrix != null) { _modelviewMatrix.Identity(); } else { _modelviewMatrix = Matrix2D.Create(); } if (_projectionMatrix3D != null) { _projectionMatrix3D.Identity(); } else { _projectionMatrix3D = Matrix3D.Create(); } if (_mvpMatrix3D == null) { _mvpMatrix3D = Matrix3D.Create(); } }
public void TestTransformVertices() { VertexData vertexData = new VertexData(3, true); Vertex defaultVertex = DefaultVertex(); Vertex secondVertex = DefaultVertex(); secondVertex.Position.X = 1.0f; secondVertex.Position.Y = 2.0f; vertexData.Vertices[0] = defaultVertex; vertexData.Vertices[1] = secondVertex; vertexData.Vertices[2] = defaultVertex; Matrix2D matrix = Matrix2D.Create(); matrix.Rotate((float)Math.PI); vertexData.TransformVertices(matrix, 1, 1); Vertex expected = DefaultVertex(); expected.Position.X = -1.0f; expected.Position.Y = -2.0f; CompareVertex(vertexData.Vertices[0], DefaultVertex()); CompareVertex(vertexData.Vertices[1], expected); CompareVertex(vertexData.Vertices[2], DefaultVertex()); }
public override DisplayObject HitTest(Point localPoint) { if (!Visible || !Touchable || !HitTestMask(localPoint)) { return(null); } for (int i = _children.Count - 1; i >= 0; --i) { // front to back! DisplayObject child = _children[i]; if (child.IsMask) { continue; } Matrix2D transformationMatrix = Matrix2D.Create(); transformationMatrix.CopyFromMatrix(child.TransformationMatrix); transformationMatrix.Invert(); Point transformedPoint = transformationMatrix.TransformPoint(localPoint); DisplayObject target = child.HitTest(transformedPoint); if (target != null) { return(TouchGroup ? this : target); } } return(null); }
protected DisplayObject() { _x = _y = _pivotX = _pivotY = _rotation = _skewX = _skewY = 0.0f; _scaleX = _scaleY = _alpha = 1.0f; _visible = _touchable = _hasVisibleArea = true; BlendMode = Display.BlendMode.AUTO; _transformationMatrix = Matrix2D.Create(); }
public void TestCopy() { Matrix2D copy = Matrix2D.Create(); copy.CopyFromMatrix(countMatrix); Assert.IsTrue(countMatrix.IsEqual(copy), "copy not equal " + copy); Assert.IsFalse(countMatrix == copy, "its not a copy, but the same object"); }
public void TestInvert() { countMatrix.Invert(); Assert.IsTrue(CheckMatrixValues(countMatrix, -2, 1, 3.0f / 2.0f, -0.5f, 1, -2), "invert produced wrong result: " + countMatrix); Matrix2D translateMatrix = Matrix2D.Create(); translateMatrix.Translate(20.0f, 40.0f); translateMatrix.Invert(); Assert.IsTrue(CheckMatrixValues(translateMatrix, 1, 0, 0, 1, -20, -40), "invert produced wrong result: " + translateMatrix); }
/// <summary> /// Resets the RenderState to the default settings. /// </summary> private void Reset() { _scaleX = _scaleY = 1.0f; _alpha = 1.0f; if (_modelviewMatrix != null) { _modelviewMatrix.Identity(); } else { _modelviewMatrix = Matrix2D.Create(); } }
public void TestSetTransformationMatrixWithZeroValues() { Matrix2D matrix = Matrix2D.Create(0, 0, 0, 0, 0, 0); Sprite sprite = new Sprite(); sprite.TransformationMatrix = matrix; Assert.AreEqual(0.0f, sprite.X, "wrong x"); Assert.AreEqual(0.0f, sprite.Y, "wrong y"); Assert.AreEqual(0.0f, sprite.ScaleX, "wrong scaleX"); Assert.AreEqual(0.0f, sprite.ScaleY, "wrong scaleY"); Assert.AreEqual(0.0f, sprite.Rotation, "wrong rotation"); Assert.AreEqual(0.0f, sprite.SkewX, "wrong skewX"); Assert.AreEqual(0.0f, sprite.SkewY, "wrong skewY"); }
public void TestTransformationMatrix() { Sprite sprite = new Sprite(); sprite.X = 50; sprite.Y = 100; sprite.Rotation = (float)(Math.PI / 4); sprite.ScaleX = 0.5f; sprite.ScaleY = 1.5f; Matrix2D matrix = Matrix2D.Create(); matrix.Scale(sprite.ScaleX, sprite.ScaleY); matrix.Rotate(sprite.Rotation); matrix.Translate(sprite.X, sprite.Y); Assert.IsTrue(sprite.TransformationMatrix.IsEqual(matrix)); }
/// <summary> /// Transforms the given texture coordinates from the root texture's coordinate system /// to the local coordinate system. /// </summary> public Point GlobalToLocal(float u, float v) { Point outP = Point.Create(u, v); if (this == Root) { outP.X = u; outP.Y = v; } else { Matrix2D sMatrix = Matrix2D.Create(); sMatrix.CopyFromMatrix(TransformationMatrixToRoot); sMatrix.Invert(); outP = sMatrix.TransformPoint(u, v); } return(outP); }
public void TestAppendMatrix() { Matrix2D copy = Matrix2D.Create(); copy.CopyFromMatrix(countMatrix); copy.AppendMatrix(identMatrix); Assert.IsTrue(countMatrix.IsEqual(copy), "multiplication with identity modified matrix"); copy.CopyFromMatrix(identMatrix); copy.AppendMatrix(countMatrix); Assert.IsTrue(countMatrix.IsEqual(copy), "multiplication with identity modified matrix"); Matrix2D countdownMatrix = Matrix2D.Create(9, 8, 7, 6, 5, 4); copy.AppendMatrix(countdownMatrix); Assert.IsTrue(CheckMatrixValues(copy, 23, 20, 55, 48, 92, 80), "wrong matrix " + copy); countdownMatrix.AppendMatrix(countMatrix); Assert.IsTrue(CheckMatrixValues(countdownMatrix, 33, 50, 25, 38, 22, 32), "wrong matrix " + copy); }
private void UpdateMatrices() { if (_transformationMatrix != null) { _transformationMatrix.Identity(); } else { _transformationMatrix = Matrix2D.Create(); } if (_transformationMatrixToRoot != null) { _transformationMatrixToRoot.Identity(); } else { _transformationMatrixToRoot = Matrix2D.Create(); } if (_rotated) { _transformationMatrix.Translate(0, -1); _transformationMatrix.Rotate((float)Math.PI / 2.0f); } _transformationMatrix.Scale(_region.Width / _parent.Width, _region.Height / _parent.Height); _transformationMatrix.Translate(_region.X / _parent.Width, _region.Y / _parent.Height); SubTexture texture = this; while (texture != null) { _transformationMatrixToRoot.AppendMatrix(texture._transformationMatrix); texture = texture.Parent as SubTexture; } }
/** Sets up a VertexData instance with the correct positions for 4 vertices so that * the texture can be mapped onto it unscaled. If the texture has a <code>frame</code>, * the vertices will be offset accordingly. * * @param vertexData the VertexData instance to which the positions will be written. * @param vertexID the start position within the VertexData instance. * @param bounds useful only for textures with a frame. This will position the * vertices at the correct position within the given bounds, * distorted appropriately. */ public void SetupVertexPositions(VertexData vertexData, int vertexId = 0, Rectangle bounds = null) { Rectangle frame = Frame; float width = Width; float height = Height; Rectangle sRectangle = Rectangle.Create(); if (frame != null) { sRectangle.SetTo(-frame.X, -frame.Y, width, height); } else { sRectangle.SetTo(0, 0, width, height); } vertexData.SetPoint(vertexId, sRectangle.Left, sRectangle.Top); vertexData.SetPoint(vertexId + 1, sRectangle.Right, sRectangle.Top); vertexData.SetPoint(vertexId + 2, sRectangle.Left, sRectangle.Bottom); vertexData.SetPoint(vertexId + 3, sRectangle.Right, sRectangle.Bottom); if (bounds != null) { float scaleX = bounds.Width / FrameWidth; float scaleY = bounds.Height / FrameHeight; if (scaleX != 1.0 || scaleY != 1.0 || bounds.X != 0 || bounds.Y != 0) { Matrix2D sMatrix = Matrix2D.Create(); sMatrix.Identity(); sMatrix.Scale(scaleX, scaleY); sMatrix.Translate(bounds.X, bounds.Y); vertexData.TransformVertices(sMatrix, vertexId, 4); } } }
public void TestSetTransformationMatrixWithRightAngle() { Sprite sprite = new Sprite(); float[] angles = { (float)(Math.PI / 2.0f), (float)(-Math.PI / 2.0f) }; Matrix2D[] matrices = { Matrix2D.Create(0, 1, -1, 0), Matrix2D.Create(0, -1, 1, 0) }; for (int i = 0; i < 2; ++i) { float angle = angles[i]; Matrix2D matrix = matrices[i]; sprite.TransformationMatrix = matrix; AssertAreEqualWithSmallError(0.0f, sprite.X, "wrong x coord"); AssertAreEqualWithSmallError(0.0f, sprite.Y, "wrong y coord"); AssertAreEqualWithSmallError(1.0f, sprite.ScaleX, "wrong scaleX"); AssertAreEqualWithSmallError(1.0f, sprite.ScaleY, "wrong scaleY"); AssertAreEqualWithSmallError(angle, sprite.Rotation, "wrong rotation"); } }
public void TestSetTransformationMatrix() { const float x = 50; const float y = 100; const float scaleX = 0.5f; const float scaleY = 1.5f; const float rotation = (float)(Math.PI / 4.0f); Matrix2D matrix = Matrix2D.Create(); matrix.Scale(scaleX, scaleY); matrix.Rotate(rotation); matrix.Translate(x, y); Sprite sprite = new Sprite(); sprite.TransformationMatrix = matrix; AssertAreEqualWithSmallError(x, sprite.X); AssertAreEqualWithSmallError(y, sprite.Y); AssertAreEqualWithSmallError(scaleX, sprite.ScaleX); AssertAreEqualWithSmallError(scaleY, sprite.ScaleY); AssertAreEqualWithSmallError(rotation, sprite.Rotation); }
protected void SetUp() { countMatrix = Matrix2D.Create(1, 2, 3, 4, 5, 6); identMatrix = Matrix2D.Create(); }
/// <summary> /// Creates a matrix that represents the transformation from the local coordinate system to another. /// </summary> public Matrix2D GetTransformationMatrix(DisplayObject targetSpace) { DisplayObject commonParent = null; DisplayObject currentObject; Matrix2D outMatrix = Matrix2D.Create(); outMatrix.Identity(); if (targetSpace == this) { return(outMatrix); } if (targetSpace == _parent || (targetSpace == null && _parent == null)) { outMatrix.CopyFromMatrix(TransformationMatrix); return(outMatrix); } if (targetSpace == null || targetSpace == Base) { // targetSpace 'null' represents the target coordinate of the base object. // -> move up from this to base currentObject = this; while (currentObject != targetSpace) { outMatrix.AppendMatrix(currentObject.TransformationMatrix); currentObject = currentObject._parent; } return(outMatrix); } if (targetSpace.Parent == this) { outMatrix = targetSpace.GetTransformationMatrix(this); outMatrix.Invert(); return(outMatrix); } // 1.: Find a common parent of this and the target coordinate space. commonParent = FindCommonParent(this, targetSpace); // 2.: Move up from this to common parent currentObject = this; while (currentObject != commonParent) { outMatrix.AppendMatrix(currentObject.TransformationMatrix); currentObject = currentObject._parent; } if (commonParent == targetSpace) { return(outMatrix); } // 3.: Now move up from target until we reach the common parent var sHelperMatrix = Matrix2D.Create(); sHelperMatrix.Identity(); currentObject = targetSpace; while (currentObject != commonParent) { sHelperMatrix.AppendMatrix(currentObject.TransformationMatrix); currentObject = currentObject._parent; } // 4.: Combine the two matrices sHelperMatrix.Invert(); outMatrix.AppendMatrix(sHelperMatrix); return(outMatrix); }
protected DisplayObject() { _transformationMatrix = Matrix2D.Create(); Alpha = 1; }