Exemple #1
0
        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);
        }
Exemple #2
0
        public void CopyFrom(RenderState renderState)
        {
            if (OnDrawRequired != null)
            {
                uint currentTarget   = _renderTarget != null ? _renderTarget.Base : 0;
                uint nextTarget      = renderState._renderTarget != null ? renderState._renderTarget.Base : 0;
                bool clipRectChanges = _clipRect != null || renderState._clipRect != null ?
                                       !Rectangle.Compare(_clipRect, renderState._clipRect) : false;

                if (_blendMode != renderState._blendMode || _culling != renderState._culling ||
                    currentTarget != nextTarget || clipRectChanges)
                {
                    OnDrawRequired();
                }
            }

            Alpha                = renderState.Alpha;
            _blendMode           = renderState._blendMode;
            _renderTarget        = renderState._renderTarget;
            _renderTargetOptions = renderState._renderTargetOptions;
            _culling             = renderState._culling;
            _modelviewMatrix.CopyFromMatrix(renderState._modelviewMatrix);

            _projectionMatrix3D.CopyFrom(renderState._projectionMatrix3D);

            if (_clipRect != null || renderState._clipRect != null)
            {
                ClipRect = renderState._clipRect;
            }
        }
Exemple #3
0
        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");
        }
Exemple #4
0
        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);
        }
Exemple #5
0
 /// <summary>
 /// Sets this RenderState by taking the given render state as a base and applying the given
 /// DisplayObject's properties.
 /// This is used to calculate the absolute values for the given object (position, scale, rotation,..)
 /// </summary>
 /// <param name="parentState">The state of its parent object.</param>
 /// <param name="displayObject">The DisplayObject whose state needs to be calculated.</param>
 public void SetValues(RenderState parentState, DisplayObject displayObject)
 {
     _alpha  = parentState._alpha * displayObject.Alpha;
     _scaleX = parentState._scaleX * displayObject.ScaleX;
     _scaleY = parentState._scaleY * displayObject.ScaleY;
     _modelviewMatrix.CopyFromMatrix(parentState._modelviewMatrix).PrependMatrix(displayObject.TransformationMatrix);
     _originalWidth    = displayObject.Width;
     _originalHeight   = displayObject.Height;
     _textureSubRegion = displayObject.TextureSubRegionNormalized;
     ResSet            = displayObject.ResSet;
 }
Exemple #6
0
        /// <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);
        }
Exemple #7
0
 /// <summary>
 /// Figures out if the mask can be represented by a scissor rectangle; this is possible
 /// if it's just a simple (untextured) quad that is parallel to the stage axes. The 'out'
 /// parameter will be filled with the transformation matrix required to move the mask into
 /// stage coordinates.
 /// </summary>
 private bool IsRectangularMask(DisplayObject mask, Matrix2D outMatrix)
 {
     if (mask is Quad quad && quad.Texture == null)
     {
         if (mask.Stage != null)
         {
             outMatrix = mask.GetTransformationMatrix(null);
         }
         else
         {
             outMatrix.CopyFromMatrix(mask.TransformationMatrix);
             outMatrix.AppendMatrix(_state.ModelviewMatrix);
         }
         return((MathUtil.Equals(outMatrix.A, 0f) && MathUtil.Equals(outMatrix.D, 0f)) ||
                (MathUtil.Equals(outMatrix.B, 0f) && MathUtil.Equals(outMatrix.C, 0f)));
     }
     return(false);
 }
        /// <summary>
        /// Checks if a certain point is inside the display object's mask. If there is no mask,
        /// this method always returns <code>true</code> (because having no mask is equivalent
        /// to having one that's infinitely big).
        /// </summary>
        public bool HitTestMask(Point localPoint)
        {
            if (_mask == null)
            {
                return(true);
            }
            if (_mask.Stage != null)
            {
                _sHelperMatrixAlt = GetTransformationMatrix(_mask);
            }
            else
            {
                _sHelperMatrixAlt.CopyFromMatrix(_mask.TransformationMatrix);
                _sHelperMatrixAlt.Invert();
            }

            var helperPoint = _sHelperMatrixAlt.TransformPoint(localPoint);

            return(_mask.HitTest(helperPoint) != null);
        }
        /// <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);
        }