Esempio n. 1
0
        private void OnCapturedTouchReported(object sender, TouchReportedEventArgs e)
        {
            var parent = AssociatedObject.Parent as UIElement;

            if (parent == null)
            {
                return;
            }

            var root = TouchHelper.GetRootElement(parent);

            if (root == null)
            {
                return;
            }

            List <Manipulator2D> manipulators = null;

            if (e.TouchPoints.FirstOrDefault() != null)
            {
                // Get transformation to convert positions to the parent's coordinate system.
                var transform = root.TransformToVisual(parent);
                foreach (var touchPoint in e.TouchPoints)
                {
                    var position = touchPoint.Position;

                    // Convert to the parent's coordinate system.
                    position = transform.Transform(position);

                    // Create a manipulator.
                    var manipulator = new Manipulator2D(
                        touchPoint.TouchDevice.Id,
                        (float)(position.X),
                        (float)(position.Y));

                    if (manipulators == null)
                    {
                        // Lazy initialization.
                        manipulators = new List <Manipulator2D>();
                    }

                    manipulators.Add(manipulator);
                }
            }

            // Process manipulations.
            _manipulationProcessor.ProcessManipulators(DateTime.UtcNow.Ticks, manipulators);
        }
        private void OnCapturedTouchReported(object sender, TouchReportedEventArgs e)
        {
            UIElement parent = this.AssociatedObject.Parent as UIElement;

            if (parent == null)
            {
                return;
            }

            //Find the root element
            UIElement root = TouchHelper.GetRootElement(parent);

            if (root == null)
            {
                return;
            }

            // get transformation to convert positions to the parent's coordinate system
            GeneralTransform     transform    = root.TransformToVisual(parent);
            List <Manipulator2D> manipulators = null;

            foreach (TouchPoint touchPoint in e.TouchPoints)
            {
                Point position = touchPoint.Position;

                // convert to the parent's coordinate system
                position = transform.Transform(position);

                // create a manipulator
                Manipulator2D manipulator = new Manipulator2D(
                    touchPoint.TouchDevice.Id,
                    (float)(position.X),
                    (float)(position.Y));

                if (manipulators == null)
                {
                    // lazy initialization
                    manipulators = new List <Manipulator2D>();
                }
                manipulators.Add(manipulator);
            }

            // process manipulations
            this.manipulationProcessor.ProcessManipulators(
                Timestamp,
                manipulators);
        }
Esempio n. 3
0
        private void UpdateManipulators(ICollection <IManipulator> updatedManipulators)
        {
            // Clear out the old removed collection and use it to store
            // the new current collection. The old current collection
            // will be used to generate the new removed collection.
            _removedManipulators.Clear();
            var temp = _removedManipulators;

            _removedManipulators = _currentManipulators;
            _currentManipulators = temp;

            // End the manipulation if the element is not
            // visible anymore
            UIElement uie = _currentContainer as UIElement;

            if (uie != null)
            {
                if (!uie.IsVisible)
                {
                    return;
                }
            }
            else
            {
                UIElement3D uie3D = _currentContainer as UIElement3D;
                if (uie3D != null &&
                    !uie3D.IsVisible)
                {
                    return;
                }
            }

            // For each updated manipulator, convert it to the correct format in the
            // current collection and remove it from the removed collection. What is left
            // in the removed collection will be the manipulators that were removed.
            foreach (IManipulator updatedManipulator in updatedManipulators)
            {
                //
                int id = updatedManipulator.Id;
                _removedManipulators.Remove(id); // This manipulator was not removed
                Point position = updatedManipulator.GetPosition(_currentContainer);
                position = _manipulationDevice.GetTransformedManipulatorPosition(position);
                _currentManipulators[id] = new Manipulator2D(id, (float)position.X, (float)position.Y);
            }
        }
Esempio n. 4
0
        // </Snippet_GamePiece_ProcessInertia>

        // <Snippet_GamePiece_UpdateFromMouse>
        #region UpdateFromMouse
        public bool UpdateFromMouse(MouseState mouseState)
        {
            if (mouseState.LeftButton == ButtonState.Released)
            {
                if (isMouseCaptured)
                {
                    manipulationProcessor.CompleteManipulation(Timestamp);
                }
                isMouseCaptured = false;
            }

            if (isMouseCaptured ||
                (mouseState.LeftButton == ButtonState.Pressed &&
                 bounds.Contains(mouseState.X, mouseState.Y)))
            {
                isMouseCaptured = true;

                Manipulator2D[] manipulators = new Manipulator2D[]
                {
                    new Manipulator2D(0, mouseState.X, mouseState.Y)
                };

                dragPoint.X = mouseState.X;
                dragPoint.Y = mouseState.Y;
                manipulationProcessor.ProcessManipulators(Timestamp, manipulators);
            }

            // If the right button is pressed, stop the piece and move it to the center.
            if (mouseState.RightButton == ButtonState.Pressed)
            {
                processInertia = false;
                X        = viewport.Width / 2;
                Y        = viewport.Height / 2;
                rotation = 0;
            }
            return(isMouseCaptured);
        }
Esempio n. 5
0
        /// <summary>
        /// Updates manipulators to reflect the touch change.
        /// </summary>
        /// <param name="touchEvent">The new TouchTargetEvent</param>
        /// <param name="details">The contacEvent.HitTestDetails as ScrollViewerHitTestDetails</param>
        private void UpdateCurrentManipulators(TouchTargetEvent touchEvent, ScrollViewerHitTestDetails details)
        {
            // Try to find the changed touch in the manipulators list.
            bool foundManipulator = false;
            for (int i = 0; i < manipulations.Count; i++)
            {
                Manipulator2D manipulator = manipulations[i];

                if (manipulator.Id == touchEvent.Touch.Id)
                {
                    manipulations.Remove(manipulator);

                    Manipulator2D manipulatorToAdd = new Manipulator2D(touchEvent.Touch.Id,
                                                                   ConvertFromHorizontalValueToScreenSpace(details.HorizontalPosition),
                                                                   ConvertFromVerticalValueToScreenSpace(details.VerticalPosition));

                    // Performance: It doesn't matter where we insert, but if all the touches are being updated, 
                    // then putting the most recent change at the end will mean that there is one less touch
                    // to go through the next time this loop is executed.
                    if (manipulations.Count == 0)
                    {
                        manipulations.Add(manipulatorToAdd);
                    }
                    else
                    {
                        manipulations.Insert(manipulations.Count - 1, manipulatorToAdd);
                    }

                    foundManipulator = true;
                    break;
                }
            }

            // The manipulator isn't in the list so add it.
            if (!foundManipulator)
            {
                manipulations.Add(new Manipulator2D(touchEvent.Touch.Id,
                                                  ConvertFromHorizontalValueToScreenSpace(details.HorizontalPosition),
                                                  ConvertFromVerticalValueToScreenSpace(details.VerticalPosition)));
            }
        }
Esempio n. 6
0
        private void UpdateManipulators(ICollection<IManipulator> updatedManipulators)
        {
            // Clear out the old removed collection and use it to store
            // the new current collection. The old current collection 
            // will be used to generate the new removed collection.
            _removedManipulators.Clear(); 
            var temp = _removedManipulators; 
            _removedManipulators = _currentManipulators;
            _currentManipulators = temp; 

            // End the manipulation if the element is not
            // visible anymore
            UIElement uie = _currentContainer as UIElement; 
            if (uie != null)
            { 
                if (!uie.IsVisible) 
                {
                    return; 
                }
            }
            else
            { 
                UIElement3D uie3D = _currentContainer as UIElement3D;
                if (uie3D != null && 
                    !uie3D.IsVisible) 
                {
                    return; 
                }
            }

            // For each updated manipulator, convert it to the correct format in the 
            // current collection and remove it from the removed collection. What is left
            // in the removed collection will be the manipulators that were removed. 
            foreach (IManipulator updatedManipulator in updatedManipulators) 
            {
                // 
                int id = updatedManipulator.Id;
                _removedManipulators.Remove(id); // This manipulator was not removed
                Point position = updatedManipulator.GetPosition(_currentContainer);
                position = _manipulationDevice.GetTransformedManipulatorPosition(position); 
                _currentManipulators[id] = new Manipulator2D(id, (float)position.X, (float)position.Y);
            } 
        } 
        /// <summary>
        /// Occurs when Touch points are reported: handles manipulations
        /// </summary>
        private void OnCapturedTouchReported(object sender, TouchReportedEventArgs e)
        {
            var parent = AssociatedObject.Parent as UIElement;

            if (parent == null)
            {
                return;
            }

            //Find the root element
            var root = TouchHelper.GetRootElement(parent);

            if (root == null)
            {
                return;
            }

            //Multi-Page support: verify if the collection of Touch points is null
            var touchPoints = e.TouchPoints;
            List <Manipulator2D> manipulators = null;

            if (touchPoints.FirstOrDefault() != null)
            {
                // get transformation to convert positions to the parent's coordinate system
                var transform = root.TransformToVisual(parent);
                foreach (var touchPoint in touchPoints)
                {
                    var position = touchPoint.Position;

                    // convert to the parent's coordinate system
                    position = transform.Transform(position);

                    // create a manipulator
                    var manipulator = new Manipulator2D(
                        touchPoint.TouchDevice.Id,
                        (float)(position.X),
                        (float)(position.Y));

                    if (manipulators == null)
                    {
                        // lazy initialization
                        manipulators = new List <Manipulator2D>();
                    }
                    manipulators.Add(manipulator);

                    //Change the visualization of the touchPoint
                    if (_touchPointsMarkers.ContainsKey(touchPoint.TouchDevice.Id))
                    {
                        if (AreFingersVisible)
                        {
                            _touchPointsMarkers[touchPoint.TouchDevice.Id].HorizontalOffset =
                                touchPoint.Position.X - (EllipseWidth / 2);
                            _touchPointsMarkers[touchPoint.TouchDevice.Id].VerticalOffset =
                                touchPoint.Position.Y - (EllipseWidth / 2);
                        }
#if DEBUG
                        Debug.WriteLine("TouchPoint Reported: Id {0} at ({1} - Total Touch Points: {2})",
                                        touchPoint.TouchDevice.Id,
                                        touchPoint.Position, touchPoints.Count());
#endif
                    }
                }
            }

            // process manipulations
            _manipulationProcessor.ProcessManipulators(Timestamp, manipulators);
        }
        public void processTouchPoints(ReadOnlyTouchPointCollection touches, List<BlobPair> blobPairs, GameTime gameTime)
        {
            lastTouchPosition = touchPosition;
            int tagID = -1;
            int tagValue = -1;
            Boolean manipulatorControl = false;
            
            if (touches.Count == 2 && touches[0].IsFingerRecognized && touches[1].IsFingerRecognized)
            {
                manipulatorControl = true;
                Manipulator2D[] manipulators;
                manipulators = new Manipulator2D[] { 
                    new Manipulator2D(1, touches[1].X, touches[1].Y),
                    new Manipulator2D(3, touches[0].X, touches[0].Y)
                };

                manipulationProcessor.Pivot.X = touches[0].X;
                manipulationProcessor.Pivot.Y = touches[0].Y;

                manipulationProcessor.ProcessManipulators(Timestamp, manipulators);
            }
            else
            {
                manipulationProcessor.CompleteManipulation(Timestamp);
            }

            if (touches.Count >= 1)
            {
                for (int i = 0; i < touches.Count; i++)
                {
                    if (touches[i].IsTagRecognized)
                    {
                        tagID = touches[i].Id;
                        tagValue = (int)touches[i].Tag.Value;
                        break;
                    }
                }

                /*switch (tagValue)
                {
                    case 4:
                        viewManager.rotateToSide(4,t);
                        break;
                    case 5:
                        viewManager.rotateToSide(5,t);
                        break;
                    case 6:
                        viewManager.rotateToSide(6,t);
                        break;
                    case 7:
                        viewManager.rotateToSide(7,t);
                        break;
                    case 8:
                        viewManager.rotateToSide(8,t);
                        break;
                }
                */
                touchPosition = touches[0];
                //First time touch
                if (lastTouchPosition == null)
                {
                    Segment s;
                    s.P1 = game.GraphicsDevice.Viewport.Unproject(new Vector3(touchPosition.X, touchPosition.Y, 0f),
                        viewManager.Projection, viewManager.View, Matrix.Identity);
                    s.P2 = game.GraphicsDevice.Viewport.Unproject(new Vector3(touchPosition.X, touchPosition.Y, 1f),
                        viewManager.Projection, viewManager.View, Matrix.Identity);
                    float scalar;
                    Vector3 point;
                    var c = physics.BroadPhase.Intersect(ref s, out scalar, out point);

                    if (c != null && c is BodySkin && !(((SolidThing)((BodySkin)c).Owner).getThingType() == 1) && !(((SolidThing)((BodySkin)c).Owner).getThingType() == 2))
                    {
                        pickedObject = ((BodySkin)c).Owner;
                        orientation = pickedObject.Orientation;
                        pickedDistance = scalar;
                        pickedObject.IsActive = true;
                        pickedObjectOffset = pickedObject.Position - point;
                        pickedObject.IsWeightless = true;
                    }
                    //lastOrientation = touches.Count == 1 ? touches[0].Orientation : touches[1].Orientation;
                    lastOrientation = touches[0].Orientation;
                }
                else if (pickedObject != null)
                {
                    Segment s;
                    s.P1 = game.GraphicsDevice.Viewport.Unproject(new Vector3(touchPosition.X, touchPosition.Y, 0f),
                        viewManager.Projection, viewManager.View, Matrix.Identity);
                    s.P2 = game.GraphicsDevice.Viewport.Unproject(new Vector3(touchPosition.X, touchPosition.Y, 1f),
                        viewManager.Projection, viewManager.View, Matrix.Identity);

                    Vector3 diff, point;
                    Vector3.Subtract(ref s.P2, ref s.P1, out diff);
                    Vector3.Multiply(ref diff, pickedDistance, out diff);
                    Vector3.Add(ref s.P1, ref diff, out point);                    
                    pickedObject.SetVelocity(Vector3.Zero, Vector3.Zero);

                    if (!manipulatorControl)
                    {
                        Vector3 position = Vector3.Add(point, pickedObjectOffset);
                        pickedObject.SetWorld(position, orientation);
                    }
                    
                    pickedObject.IsActive = true;
                    SolidThing pickedObjectST = (SolidThing)pickedObject;

                    switch (tagValue)
                    {

                        //Pin a block
                        case 0:
                            pickedObject.Unfreeze();
                            pickedObject.Freeze();
                            break;
                        //unPin a block
                        case 1:
                            if (pickedObjectST.getThingType() == 1)
                            {
                        
                                break;
                            }
                            else
                            {
                                pickedObject.Unfreeze();
                                break;
                            }

                        //Rotate a block
                        case 2:
                            pickedObject.SetWorld(pickedObject.Position, Quaternion.CreateFromAxisAngle(new Vector3(0, 0, -1.0f), touchPosition.Orientation));
                            break;
                        //Move a block towards or away from camera
                        case 3:
                            TouchPoint tagPoint = touches.GetTouchPointFromId(tagID);
                            float deltaRotation = MathHelper.ToDegrees(lastOrientation) - MathHelper.ToDegrees(tagPoint.Orientation);

                            Vector3 direction = new Vector3(0, 0, 1.0f);
                            direction.Normalize();
                            pickedObject.SetWorld(Vector3.Add(pickedObject.Position, Vector3.Multiply(direction, deltaRotation * 0.03f)));
                            lastOrientation = tagPoint.Orientation;
                            break;
                            
                    }




                }
                else if (pickedObject != null)
                {
                    pickedObject.IsWeightless = false;
                    pickedObject = null;                    
                }
            }
            else if (pickedObject != null)
            {
                pickedObject.IsWeightless = false;
                pickedObject = null;
                touchPosition = null;
                lastTouchPosition = null;
                
            }
            else
            {
                touchPosition = null;
            }

        }
        public void processTouchPoints(ReadOnlyTouchPointCollection touches, List<BlobPair> blobPairs, GameTime gameTime)
        {
            bool corkScrewOnTable = false;
            bool fineCameraOnTable = false;
            try
            {
                foreach (BlobPair bp in blobPairs)
                {
                    switch (bp.thisBlobPairTangible.Name)
                    {
                        case ("Jenga Block"):
                            processBlockTangible(bp);
                            break;
                        case ("Cork Screw"):
                            processCorkScrewTangible(bp);
                            corkScrewOnTable = true;
                            break;
                        case ("Fine Camera"):
                            processFineCamera(bp);
                            fineCameraOnTable = true;
                            break;
                    }
                }
            }
            catch (NullReferenceException e)
            {
            }

            if (!corkScrewOnTable)
                this.lastCorkScrewOrientation = -1;
            if (!fineCameraOnTable)
                this.lastFineCameraInformation = null;

            //==========================================================================
            foreach (TouchPoint t in touches)
            {
                if (t.IsTagRecognized)
                {
                    switch (t.Tag.Value)
                    {
                        case JengaConstants.STACK_SIDE_0:
                            _viewManager.rotateToSide(0,t);
                            break;
                        case JengaConstants.STACK_SIDE_1:
                            _viewManager.rotateToSide(1, t);
                            break;
                        case JengaConstants.STACK_SIDE_2:
                            _viewManager.rotateToSide(2, t);
                            break;
                        case JengaConstants.STACK_SIDE_3:
                            _viewManager.rotateToSide(3, t);
                            break;
                        case JengaConstants.STACK_SIDE_4:
                            _viewManager.rotateToSide(4, t);
                            break;

                    }
                }
            }
            //==========================================================================
            //Get center positions of all finger touchpoints
            float x = 0, y = 0;
            int count = 0;
            int firstID = -1;

            foreach (TouchPoint activeTouchPoint in touches)
            {
                if (activeTouchPoint.IsFingerRecognized){
                    x += activeTouchPoint.X;
                    y += activeTouchPoint.Y;
                    firstID = activeTouchPoint.Id;
                    count++;
                }
            }

            x = x / count;
            y = y / count;

            Tuple<SolidThing, Quaternion, float, Vector3> middleBlock = null;
 
            if (count > 0)
                middleBlock = getTouchedBlock(x, y);
            
            //Rotation or zoom block
            if (selectedBrick != null && middleBlock != null && selectedBrick.Item1.Equals(middleBlock.Item1) && activeTouchPoints.Count > 1)
            {
                //holdingTouchPointID = -1;
                List<Manipulator2D> manipulatorList = new List<Manipulator2D>();
                foreach (TouchPoint t in touches)
                {   
                    if (t.IsFingerRecognized)
                        manipulatorList.Add(new Manipulator2D(t.Id, t.X, t.Y));
                }
                Manipulator2D[] manipulators = null;
                manipulators = manipulatorList.ToArray();

                try
                {
                    rotateOrZoom = true;
                    manipulationProcessor.Pivot.X = selectedBrick.Item1.Position.X;
                    manipulationProcessor.Pivot.Y = selectedBrick.Item1.Position.Y;
                    manipulationProcessor.ProcessManipulators(Timestamp, manipulators);         //TODO FIXED COLLECTION MODIFIED                   
                }
                catch (NullReferenceException e)
                {
                }
                catch (InvalidOperationException e) { }
            }
            //Otherwise if we arent moving the block, move camera
            else if (holdingTouchPointID == -1)
            {
                rotateOrZoom = false;
                if (activeTouchPoints.Count > 0 && count > 0)
                {
                    Manipulator2D[] manipulators = null;
                    manipulators = new Manipulator2D[]{new Manipulator2D(firstID, x, y)};
                    try
                    {
                        manipulationProcessor.ProcessManipulators(Timestamp, manipulators);
                    }
                    catch (NullReferenceException e) { }
                    catch (InvalidOperationException e) { }
                }
            }
        }
        /// <summary>
        /// Returns the average of the captured touches in the ThumbList.
        /// </summary>
        /// <returns></returns>
        private float AverageCapturedTouchesInThumbList()
        {
            float average = 0;
            int count = 0;

            if (manipulationProcessor == null)
            {
                manipulationProcessor = new ManipulationProcessor2D(Manipulations2D.TranslateX);  // The coordinate doesn't matter we always deal in 1 dimension.
                manipulationProcessor.Completed += OnAffine2DManipulationCompleted;
            }

            List<Manipulator2D> currentManipulators = new List<Manipulator2D>();

            // Go through the touches which are captured on the thumb and average them.
            for (int i = 0; i < thumbCapturedTouchesList.Count; i++)
            {
                int id = thumbCapturedTouchesList[i];

                // Make sure the touch is captured.
                if (TouchesCaptured.Contains(id))
                {
                    //  Make sure we have hit test details for this touch.
                    if (captureTouchesHitTestDetails.ContainsKey(id))
                    {
                        Debug.Assert(distanceOffset.ContainsKey(id), "Offset wasn't calculated for this touch.");

                        float offset = distanceOffset[id];

                        ScrollBarHitTestDetails details = captureTouchesHitTestDetails[id];

                        // The Manipulations should all run in screen space.
                        Manipulator2D manipulator = new Manipulator2D(id, ToScreenSpace(details.Position - offset), 0);

                        // Make sure the value of each touch accounts for offset.
                        average += captureTouchesHitTestDetails[id].Position - offset;
                        count++;

                        currentManipulators.Add(manipulator);
                    }
                    else
                    {
                        Debug.Fail("The touch was captured, but wasn't in the hit test details dictionary.");
                    }
                }
                else
                {
                    // The touch was released so we need to remove it.
                    thumbCapturedTouchesList.Remove(id);
                    i--;
                }
            }

            manipulationProcessor.ProcessManipulators(stopwatch.Elapsed100Nanoseconds(), currentManipulators);

            // Don't divide by zero.
            if (count != 0)
                average = average / count;

            return average;
        }