Esempio n. 1
0
 private void Update()
 {
     if (UnityEngine.Input.mouseScrollDelta.y != 0)
     {
         Zoom?.Invoke(UnityEngine.Input.mouseScrollDelta.y / 10);
     }
 }
Esempio n. 2
0
        private void computeZoom(EventLayer eventLayer, ref bool didGesture, Finger finger1, Finger finger2, ref Vector2 finger1Vec, ref Vector2 finger2Vec)
        {
            Vector2 finger1Pos           = new Vector2(finger1.NrmlX, finger1.NrmlY);
            Vector2 finger2Pos           = new Vector2(finger2.NrmlX, finger2.NrmlY);
            float   currentPinchDistance = (finger1Pos - finger2Pos).length2();

            Vector2 vectorSum = finger1Vec - finger2Vec;

            momentumDirection = 1.0f;
            zoomDelta         = vectorSum.length();
            momentum          = zoomDelta;
            deceleration      = momentum / decelerationTime;

            if (currentPinchDistance > lastPinchDistance)
            {
                zoomDelta         = -zoomDelta;
                momentumDirection = -1.0f;
            }
            didGesture        = true;
            lastPinchDistance = currentPinchDistance;

            if (!gestureStarted)
            {
                gestureStarted = true;
                if (GestureStarted != null)
                {
                    GestureStarted.Invoke(eventLayer, this);
                }
            }

            if (Zoom != null)
            {
                Zoom.Invoke(eventLayer, this);
            }
        }
Esempio n. 3
0
        protected override void OnMouseWheel(MouseWheelEventArgs e)
        {
            base.OnMouseWheel(e);
            var delta = e.Delta;

            if (_wheelOffset <= MIN_WHEEL_OFFSET && delta < 0 ||
                _wheelOffset >= MAX_WHEEL_OFFSET && delta > 0)
            {
                return;
            }
            _wheelOffset += e.Delta / 120;

            if (_wheelOffset < MIN_WHEEL_OFFSET)
            {
                _wheelOffset = MIN_WHEEL_OFFSET;
            }
            if (_wheelOffset > MAX_WHEEL_OFFSET)
            {
                _wheelOffset = MAX_WHEEL_OFFSET;
            }


            double oldScale = _currentScale.ScaleX;
            double newScale = Math.Log(1 + ((_wheelOffset) / 10d)) * 2d;

            var point1 = TranslatePoint(new Point(0, 0), Children[0]);
            var point2 = TranslatePoint(new Point(ActualWidth, ActualHeight), Children[0]);

            Rect  rect              = new Rect(point1, point2);
            Point viewMousePos      = e.GetPosition(this);
            Point relativeZoomPoint = new Point(viewMousePos.X / ActualWidth, viewMousePos.Y / ActualHeight);

            var zoomModifier = oldScale / newScale;
            var newSize      = new Size(rect.Width * zoomModifier, rect.Height * zoomModifier);

            var zoomCenterX = rect.X + (rect.Width * relativeZoomPoint.X);
            var zoomCenterY = rect.Y + (rect.Height * relativeZoomPoint.Y);
            var newX        = zoomCenterX - (relativeZoomPoint.X * newSize.Width);
            var newY        = zoomCenterY - (relativeZoomPoint.Y * newSize.Height);
            var newView     = new Rect(newX, newY, newSize.Width, newSize.Height);

            Point newOffset = new Point(-newView.X * newScale, -newView.Y * newScale);

            //Calculate new viewing window scale
            ScaleTransform newScaleTransform = new ScaleTransform
            {
                ScaleX = newScale,
                ScaleY = newScale
            };
            var zoomEvent = new ZoomEventArgs(e, _currentScale, newScaleTransform, newOffset);

            Zoom?.Invoke(this, zoomEvent);

            AdjustChildrenScale(newScaleTransform, newOffset);

            _currentScale = newScaleTransform;
        }
Esempio n. 4
0
        private void MoveTouch(TouchState touch)
        {
            var index = IndexOfTouch(touch);

            if (index < 0)
            {
                return;
            }
            if (_activeTouches.Count == 1)
            {
                var dist    = touch.ScreenPosition - _activeTouches[index].ScreenPosition;
                var contact = _environment.Raycast(touch.ScreenPosition);
                if (_potentialClick)
                {
                    if (dist.LengthSquared < ClickThresholdSquared)
                    {
                        return;
                    }
                    _potentialClick = false;
                    ClickCanceled?.Invoke(this, new SimpleInteractionEventArgs(contact));
                }

                Pan?.Invoke(this, new PanInteractionEventArgs(Move(contact, _activeTouches[index].ScreenPosition), contact));
            }
            else if (_activeTouches.Count >= 2)
            {
                var center = _multitouchCenter;
                var dist   = _panningDistance;
                var angle  = _panningAngle;
                CalculatePanning(out center, out dist, ref angle);
                if (center != _multitouchCenter)
                {
                    var oldContact = _environment.Raycast(_multitouchCenter);
                    var newContact = _environment.Raycast(center);
                    Pan?.Invoke(this, new PanInteractionEventArgs(oldContact, newContact));
                    _multitouchCenter = center;
                }

                if (angle != _panningAngle)
                {
                    var oldContact = _environment.Raycast(_multitouchCenter);
                    Rotate?.Invoke(this, new RotateInteractionEventArgs(oldContact, angle - _panningAngle));
                    _panningAngle = angle;
                }

                if (dist != _panningDistance)
                {
                    var oldContact = _environment.Raycast(_multitouchCenter);
                    Zoom?.Invoke(this, new ZoomInteractionEventArgs(oldContact, dist - _panningDistance));
                    _panningDistance = dist;
                }
            }

            _activeTouches[index].Update(touch);
        }
Esempio n. 5
0
        private void Pinch()
        {
            Touch firstTouch  = UnityEngine.Input.GetTouch(0);
            Touch secondTouch = UnityEngine.Input.GetTouch(1);

            Vector2 firstTouchPrevPos  = firstTouch.position - firstTouch.deltaPosition;
            Vector2 secondTouchPrevPos = secondTouch.position - secondTouch.deltaPosition;

            float prevTouchDelta = (firstTouchPrevPos - secondTouchPrevPos).magnitude;
            float touchDelta     = (firstTouch.position - secondTouch.position).magnitude;

            Zoom?.Invoke((touchDelta - prevTouchDelta) / 100);
        }
Esempio n. 6
0
        private void CameraInput()
        {
            if (UnityEngine.Input.touchCount == 2)
            {
                Touch touch1 = UnityEngine.Input.GetTouch(0);
                Touch touch2 = UnityEngine.Input.GetTouch(1);

                Vector2 t1Prev  = touch1.position - touch1.deltaPosition;
                Vector2 t2Prev  = touch2.position - touch2.deltaPosition;
                float   prevMag = (t1Prev - t2Prev).magnitude;
                float   current = (touch1.position - touch2.position).magnitude;

                float diff = current - prevMag;
                Zoom?.Invoke(diff);
            }
        }
Esempio n. 7
0
        protected override void OnMouseWheel(MouseWheelEventArgs e)
        {
            e.Handled = true;

            //Calculate new scaling factor
            if ((_wheelOffset == MinWheelOffset && e.Delta < 0) || (_wheelOffset == MaxWheelOffset && e.Delta > 0))
            {
                return;
            }

            _wheelOffset += e.Delta / 120;
            if (_wheelOffset < MinWheelOffset)
            {
                _wheelOffset = MinWheelOffset;
            }
            else if (_wheelOffset > MaxWheelOffset)
            {
                _wheelOffset = MaxWheelOffset;
            }

            double oldScale = _curScaleTransform.ScaleX;
            double newScale = Math.Log(1 + ((_wheelOffset) / 10d)) * 2d;

            //Calculate current viewing window onto the content
            Point topLeftContentSpace     = TranslatePoint(new Point(0, 0), Children[0]);
            Point bottomRightContentSpace = TranslatePoint(new Point(ActualWidth, ActualHeight), Children[0]);
            Rect  curView = new Rect
            {
                Location = topLeftContentSpace,
                Size     = new Size(bottomRightContentSpace.X - topLeftContentSpace.X, bottomRightContentSpace.Y - topLeftContentSpace.Y)
            };

            //Mouse position as a fraction of the view size
            Point viewSpaceMousePos = e.GetPosition(this);
            Point relZoomPoint      = new Point
            {
                X = viewSpaceMousePos.X / this.ActualWidth,
                Y = viewSpaceMousePos.Y / this.ActualHeight
            };

            //Calculate new viewing window
            Rect newView = ZoomView(curView, oldScale, newScale, relZoomPoint);

            //Calculate new content offset based on the new view
            Point newOffset = new Point(-newView.X * newScale, -newView.Y * newScale);

            //Calculate new viewing window scale
            ScaleTransform newScaleTransform = new ScaleTransform
            {
                ScaleX = newScale,
                ScaleY = newScale
            };

            var zoomEvent = new ZoomEventArgs(e, _curScaleTransform, newScaleTransform, newOffset);

            Zoom?.Invoke(this, zoomEvent);

            ApplyZoomToChildren(zoomEvent);

            _curScaleTransform = newScaleTransform;
        }
Esempio n. 8
0
        private void HandleMouseWheel(MouseWheelEventArgs args)
        {
            var contact = _environment.Raycast(Input.MousePosition);

            Zoom?.Invoke(this, new ZoomInteractionEventArgs(contact, args.Wheel * 120));
        }
Esempio n. 9
0
        protected internal override void update(EventLayer eventLayer, bool allowProcessing, Clock clock)
        {
            var touches = eventLayer.EventManager.Touches;
            var fingers = touches.Fingers;

            if (fingers.Count == 2)
            {
                Finger  finger1    = fingers[0];
                Finger  finger2    = fingers[1];
                Vector2 finger1Vec = new Vector2(finger1.NrmlDeltaX, finger1.NrmlDeltaY);
                Vector2 finger2Vec = new Vector2(finger2.NrmlDeltaX, finger2.NrmlDeltaY);
                float   finger1Len = finger1Vec.length2();
                float   finger2Len = finger2Vec.length2();
                if (finger1Len > 0 || finger2Len > 0)
                {
                    if (finger1Len > 0 && finger2Len > 0)
                    {
                        float cosTheta = finger1Vec.dot(ref finger2Vec) / (finger1Vec.length() * finger2Vec.length());
                        if (cosTheta < -0.5f && Zoom != null)
                        {
                            computeZoom(eventLayer, ref didGesture, finger1, finger2, ref finger1Vec, ref finger2Vec);
                        }
                    }
                    else if (finger1Len == 0 && finger2Len > 0)
                    {
                        computeZoom(eventLayer, ref didGesture, finger1, finger2, ref finger1Vec, ref finger2Vec);
                    }
                    else if (finger2Len == 0 && finger1Len > 0)
                    {
                        computeZoom(eventLayer, ref didGesture, finger1, finger2, ref finger1Vec, ref finger2Vec);
                    }
                }
            }

            //Momentum
            if (!didGesture)
            {
                if (momentum > 0.0f)
                {
                    zoomDelta = momentum * momentumDirection;

                    if (!momentumStarted)
                    {
                        momentumStarted = true;
                        if (MomentumStarted != null)
                        {
                            MomentumStarted.Invoke(eventLayer, this);
                        }
                    }

                    momentum -= deceleration * clock.DeltaSeconds;
                    if (momentum < 0.0f)
                    {
                        momentum = 0.0f;
                    }
                    if (Zoom != null)
                    {
                        Zoom.Invoke(eventLayer, this);
                    }
                }
                else if (momentumStarted)
                {
                    zoomDelta = 0.0f;

                    momentumStarted = false;
                    gestureStarted  = false;
                    if (MomentumEnded != null)
                    {
                        MomentumEnded.Invoke(eventLayer, this);
                    }
                }
            }
            didGesture = false;
        }