Exemplo n.º 1
0
        private void PanGestureUpdated(DigitalRubyShared.GestureRecognizer r)
        {
            if (!AllowPan)
            {
                r.Reset();
                return;
            }

            Camera     camera;
            GameObject obj = FingersScript.StartOrResetGesture(r, BringToFront, Cameras, gameObject, spriteRenderer, Mode, out camera);

            if (camera == null)
            {
                r.Reset();
                return;
            }
            else if (r.State == GestureRecognizerState.Began)
            {
                SetStartState(r, obj, false);
            }
            else if (r.State == GestureRecognizerState.Executing && _transform != null)
            {
                if (PanGesture.ReceivedAdditionalTouches)
                {
                    panZ      = camera.WorldToScreenPoint(_transform.position).z;
                    panOffset = _transform.position - camera.ScreenToWorldPoint(new Vector3(r.FocusX, r.FocusY, panZ));
                }
                Vector3 gestureScreenPoint = new Vector3(r.FocusX, r.FocusY, panZ);
                Vector3 gestureWorldPoint  = camera.ScreenToWorldPoint(gestureScreenPoint) + panOffset;
                if (rigidBody != null)
                {
                    rigidBody.MovePosition(gestureWorldPoint);
                }
                else if (rigidBody2D != null)
                {
                    rigidBody2D.MovePosition(gestureWorldPoint);
                }
                else if (canvasRenderer != null)
                {
                    _transform.position = gestureScreenPoint;
                }
                else
                {
                    _transform.position = gestureWorldPoint;
                }
            }
            else if (r.State == GestureRecognizerState.Ended)
            {
                if (spriteRenderer != null && BringToFront)
                {
                    spriteRenderer.sortingOrder = startSortOrder;
                }
                ClearStartState();
            }
        }
        private void ScaleGestureUpdated(DigitalRubyShared.GestureRecognizer scaleGesture)
        {
            if (!AllowScale)
            {
                scaleGesture.Reset();
                return;
            }
            else if (MinMaxScale.x < 0.0f || MinMaxScale.y < 0.0f || startScale == null)
            {
                // no scaling
                return;
            }

            Camera     camera;
            GameObject obj = FingersScript.StartOrResetGesture(scaleGesture, BringToFront, Cameras, gameObject, spriteRenderer, Mode, out camera);

            if (camera == null)
            {
                scaleGesture.Reset();
                return;
            }

            StateUpdated.Invoke(PanGesture);
            if (scaleGesture.State == GestureRecognizerState.Began)
            {
                SetStartState(scaleGesture, obj, false);
            }
            else if (scaleGesture.State == GestureRecognizerState.Executing && _transform != null)
            {
                // assume uniform scale
                Vector3 scale = new Vector3
                                (
                    (_transform.localScale.x * ScaleGesture.ScaleMultiplier),
                    (_transform.localScale.y * ScaleGesture.ScaleMultiplier),
                    (_transform.localScale.z * ScaleGesture.ScaleMultiplier)
                                );
                if (MinMaxScale.x > 0.0f || MinMaxScale.y > 0.0f)
                {
                    float minValue = Mathf.Min(MinMaxScale.x, MinMaxScale.y);
                    float maxValue = Mathf.Max(MinMaxScale.x, MinMaxScale.y);
                    scale.x = Mathf.Clamp(scale.x, startScale.Value.x * minValue, startScale.Value.x * maxValue);
                    scale.y = Mathf.Clamp(scale.y, startScale.Value.y * minValue, startScale.Value.y * maxValue);
                    scale.z = Mathf.Clamp(scale.z, startScale.Value.z * minValue, startScale.Value.z * maxValue);
                }

                // don't mess with z scale for 2D
                scale.z = (rigidBody2D == null && spriteRenderer == null && canvasRenderer == null ? scale.z : _transform.localScale.z);
                _transform.localScale = scale;
            }
            else if (scaleGesture.State == GestureRecognizerState.Ended)
            {
                ClearStartState();
            }
        }
        private void RotateGestureUpdated(DigitalRubyShared.GestureRecognizer rotateGesture)
        {
            if (!AllowRotate)
            {
                rotateGesture.Reset();
                return;
            }

            Camera     camera;
            GameObject obj = FingersScript.StartOrResetGesture(rotateGesture, BringToFront, Cameras, gameObject, spriteRenderer, Mode, out camera);

            if (camera == null)
            {
                rotateGesture.Reset();
                return;
            }

            StateUpdated.Invoke(rotateGesture);
            if (rotateGesture.State == GestureRecognizerState.Began)
            {
                SetStartState(rotateGesture, obj, false);
            }
            else if (rotateGesture.State == GestureRecognizerState.Executing && _transform != null)
            {
                if (rigidBody != null)
                {
                    float      angle    = RotateGesture.RotationDegreesDelta;
                    Quaternion rotation = Quaternion.AngleAxis(angle, camera.transform.forward);
                    rigidBody.MoveRotation(rigidBody.rotation * rotation);
                }
                else if (rigidBody2D != null)
                {
                    rigidBody2D.MoveRotation(rigidBody2D.rotation + RotateGesture.RotationDegreesDelta);
                }
                else if (canvasRenderer != null)
                {
                    _transform.Rotate(Vector3.forward, RotateGesture.RotationDegreesDelta, Space.Self);
                }
                else
                {
                    _transform.Rotate(camera.transform.forward, RotateGesture.RotationDegreesDelta, Space.Self);
                }
            }
            else if (rotateGesture.State == GestureRecognizerState.Ended)
            {
                ClearStartState();
            }
        }
Exemplo n.º 4
0
        private void ScaleGestureUpdated(DigitalRubyShared.GestureRecognizer r)
        {
            if (!AllowScale)
            {
                r.Reset();
                return;
            }
            else if (MinMaxScale.x == MinMaxScale.y && MinMaxScale.y == 1.0f)
            {
                return;
            }

            Camera     camera;
            GameObject obj = FingersScript.StartOrResetGesture(r, BringToFront, Cameras, gameObject, spriteRenderer, Mode, out camera);

            if (camera == null)
            {
                r.Reset();
                return;
            }
            else if (r.State == GestureRecognizerState.Began)
            {
                SetStartState(r, obj, false);
            }
            else if (r.State == GestureRecognizerState.Executing && _transform != null)
            {
                // assume uniform scale
                float scale = _transform.localScale.x * ScaleGesture.ScaleMultiplier;

                if (MinMaxScale.x > 0.0f && MinMaxScale.y >= MinMaxScale.x)
                {
                    scale = Mathf.Clamp(scale, MinMaxScale.x, MinMaxScale.y);
                }

                // don't mess with z scale for 2D
                float zScale = (rigidBody2D == null && spriteRenderer == null && canvasRenderer == null ? scale : _transform.localScale.z);
                _transform.localScale = new Vector3(scale, scale, zScale);
            }
            else if (r.State == GestureRecognizerState.Ended)
            {
                ClearStartState();
            }
        }
Exemplo n.º 5
0
 private void ScaleGestureUpdated(DigitalRubyShared.GestureRecognizer gesture)
 {
     if (gesture.State == GestureRecognizerState.Began && !GestureIntersectsSprite(gesture, ScaleIcon))
     {
         gesture.Reset();
     }
     else if (gesture.State == GestureRecognizerState.Executing)
     {
         Earth.transform.localScale *= scaleGesture.ScaleMultiplier;
     }
 }
Exemplo n.º 6
0
 private void RotationGestureUpdated(DigitalRubyShared.GestureRecognizer gesture)
 {
     if (gesture.State == GestureRecognizerState.Began && !GestureIntersectsSprite(gesture, RotateIcon))
     {
         gesture.Reset();
     }
     else if (gesture.State == GestureRecognizerState.Executing)
     {
         Earth.transform.Rotate(0.0f, 0.0f, rotationGesture.RotationDegreesDelta);
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// Callback for image gesture
        /// </summary>
        /// <param name="gesture">Image gesture</param>
        public void ImageGestureExecuted(DigitalRubyShared.GestureRecognizer gesture)
        {
            ImageGestureRecognizer imgGesture = gesture as ImageGestureRecognizer;

            if (gesture.State == GestureRecognizerState.Ended)
            {
                if (imgGesture.MatchedGestureImage == null)
                {
                    Debug.Log("Image gesture failed to match.");
                }
                else
                {
                    Debug.Log("Image gesture matched!");
                }
                gesture.Reset();
            }
        }
 private bool SetStartState(DigitalRubyShared.GestureRecognizer gesture, GameObject obj, bool force)
 {
     if (!force && Mode != GestureRecognizerComponentScriptBase.GestureObjectMode.AllowOnAnyGameObjectViaRaycast)
     {
         return(false);
     }
     else if (obj == null)
     {
         ClearStartState();
         return(false);
     }
     else if (_transform == null)
     {
         rigidBody2D    = obj.GetComponent <Rigidbody2D>();
         rigidBody      = obj.GetComponent <Rigidbody>();
         spriteRenderer = obj.GetComponent <SpriteRenderer>();
         canvasRenderer = obj.GetComponent <CanvasRenderer>();
         if (spriteRenderer != null)
         {
             startSortOrder = spriteRenderer.sortingOrder;
         }
         _transform = (rigidBody == null ? (rigidBody2D == null ? obj.transform : rigidBody2D.transform) : rigidBody.transform);
         if (DoubleTapResetMode != _DoubleTapResetMode.Off && !savedStates.ContainsKey(_transform))
         {
             savedStates[_transform] = new SavedState {
                 Rotation = _transform.rotation, Scale = _transform.localScale, Position = _transform.position
             };
         }
         if (startScale == null)
         {
             startScale = _transform.localScale;
         }
     }
     else if (_transform != obj.transform)
     {
         if (gesture != null)
         {
             gesture.Reset();
         }
         return(false);
     }
     return(true);
 }
        public static GameObject StartOrResetGesture(DigitalRubyShared.GestureRecognizer r, bool bringToFront, Camera camera, GameObject obj, SpriteRenderer spriteRenderer, GestureRecognizerComponentScriptBase.GestureObjectMode mode)
        {
            GameObject result = null;

            if (r.State == GestureRecognizerState.Began)
            {
                if ((result = GestureIntersectsObject(r, camera, obj, mode)) != null)
                {
                    SpriteRenderer _spriteRenderer;
                    if (bringToFront && (_spriteRenderer = result.GetComponent <SpriteRenderer>()) != null)
                    {
                        _spriteRenderer.sortingOrder = 1000;
                    }
                }
                else
                {
                    r.Reset();
                }
            }
            return(result);
        }
 private void DoubleTapGestureUpdated(DigitalRubyShared.GestureRecognizer r)
 {
     if (DoubleTapResetMode == _DoubleTapResetMode.Off)
     {
         r.Reset();
         return;
     }
     else if (r.State == GestureRecognizerState.Ended)
     {
         GameObject obj = GestureIntersectsObject(r, Camera, gameObject, Mode);
         SavedState state;
         if (obj != null && savedStates.TryGetValue(obj.transform, out state))
         {
             obj.transform.rotation   = state.Rotation;
             obj.transform.localScale = state.Scale;
             if (DoubleTapResetMode == _DoubleTapResetMode.ResetScaleRotationPosition)
             {
                 obj.transform.position = state.Position;
             }
             savedStates.Remove(obj.transform);
         }
     }
 }
Exemplo n.º 11
0
        private void LongPress_StateUpdated(DigitalRubyShared.GestureRecognizer gesture)
        {
            if (gesture.State == GestureRecognizerState.Began)
            {
                // raycast out for the card - this let's us have multiple cards
                Vector3    gestureWorldPos = Camera.main.ScreenToWorldPoint(new Vector3(gesture.FocusX, gesture.FocusY, 0.0f));
                Collider2D hit             = Physics2D.OverlapPoint(gestureWorldPos);

                // see if we found a card
                if (hit != null && !swipedCards.Contains(hit.transform))
                {
                    // set the draggingCard variable to the card transform, this let's us move it around in the "Executing" state
                    Debug.Log("Found card, beginning drag...");
                    draggingCard            = hit.GetComponent <Transform>();
                    draggingCard.localScale = Vector3.one * 1.2f;
                    draggingCard.transform.SetSiblingIndex(draggingCard.transform.parent.childCount - 1);
                    SpriteRenderer renderer = draggingCard.GetComponent <SpriteRenderer>();
                    if (renderer != null)
                    {
                        // put the picked up card on top of everything else
                        renderer.transform.SetSiblingIndex(renderer.transform.parent.childCount - 1);
                        for (int i = 0; i < renderer.transform.parent.childCount; i++)
                        {
                            SpriteRenderer other = renderer.transform.parent.GetChild(i).GetComponent <SpriteRenderer>();
                            if (other != null)
                            {
                                // make sure sort order and z position is set for the other cards to ensure they are in the right
                                // spot in the pile
                                other.sortingOrder = i;
                                Vector3 pos = other.transform.position;
                                pos.z = ((float)(renderer.transform.parent.childCount - i) * 0.01f);
                                other.transform.position = pos;
                            }
                        }
                    }
                }
                else
                {
                    // no card, reset the gesture and they must lift the touch and try again
                    Debug.Log("No card under gesture, resetting...");
                    gesture.Reset();
                }

                // apply an offset from the center of the card so it drags from wherever it was touched on the card
                dragOffset   = draggingCard.position - Camera.main.ScreenToWorldPoint(new Vector3(gesture.FocusX, gesture.FocusY, 0.0f));
                dragOffset.z = 0.0f;
            }
            else if (gesture.State == GestureRecognizerState.Executing)
            {
                // if the gesture velocity is high enough, fling the card off screen
                float speed = longPress.Distance(gesture.VelocityX, gesture.VelocityY);
                if (speed >= SwipeAwaySpeed)
                {
                    // convert the screen units velocity to world velocity and apply to the card
                    draggingCard.localScale = Vector3.one;
                    Vector3 worldVelocityZero    = Camera.main.ScreenToWorldPoint(Vector3.zero);
                    Vector3 worldVelocityGesture = Camera.main.ScreenToWorldPoint(new Vector3(gesture.VelocityX, gesture.VelocityY, 0.0f));
                    Vector3 worldVelocity        = (worldVelocityGesture - worldVelocityZero) * 0.5f;
                    worldVelocity.z = 0.0f;
                    Rigidbody2D rb = draggingCard.GetComponent <Rigidbody2D>();
                    rb.velocity = worldVelocity;

                    // apply some random spin for fun
                    rb.angularVelocity = Random.Range(-1000.0f, 1000.0f);

                    // don't allow the card to be re-dragged while it flings away
                    swipedCards.Add(draggingCard);
                    draggingCard = null;

                    // reset gesture, the swipe away finishes the gesture
                    gesture.Reset();

                    Debug.LogFormat("Swiping card away at world velocity {0} (screen velocity units {1})", (Vector2)worldVelocity, new Vector2(gesture.VelocityX, gesture.VelocityY));
                }
                else
                {
                    // drag the card
                    Vector3 dragCurrent = Camera.main.ScreenToWorldPoint(new Vector3(gesture.FocusX, gesture.FocusY, 0.0f));
                    dragCurrent.z         = draggingCard.transform.position.z;
                    draggingCard.position = dragCurrent + dragOffset;
                }
            }
            else
            {
                // if not begin or execute state, null out the dragging card
                if (draggingCard != null)
                {
                    draggingCard.localScale = Vector3.one;
                    SpriteRenderer renderer = draggingCard.GetComponent <SpriteRenderer>();
                    if (renderer != null)
                    {
                        //renderer.sortingOrder = savedSortOrder;
                    }
                }
                draggingCard = null;
            }
        }