Example #1
0
        public Event()
        {
            __position = Vector2.zero;

            target2D     = null;
            target3D     = null;
            targetButton = null;
            firstFrame   = true;
            action       = ACTION.VOID;
            touch        = TOUCH.NONE;
            direction    = DIRECTION.FREE;
            callback     = "";

            isInert     = false;
            isSpringing = false;
            springIndex = -1;
            tapCount    = 0;
        }
Example #2
0
        public void GetUserActivity()
        {
            touch = TOUCH.NONE;

#if UNITY_EDITOR || UNITY_STANDALONE
            // if we're on macos or windows
            px = x;
            py = y;

            Vector2 abs = Input.mousePosition;



#if UNITY_STANDALONE_WIN
            // on windows standalone we have a decent way of finding out which display we're on
            Vector2 absolute = Input.mousePosition;
            Vector3 relative = Display.RelativeMouseAt(abs);
            //       GameObject.Find("Environment").GetComponent<Text>().text = "" + absolute.ToString()+" "+relative.ToString();

            // relative = abs;
#else
            Vector3 relative = abs;
#endif

#if UNITY_EDITOR
            relative = abs;
#endif

            x = relative.x;
            y = relative.y;

            dx = x - px;
            dy = y - py;


            if (Input.GetButton("Fire1"))
            {
                // First frame doesn't produce delta values yet.
                if (firstFrame)
                {
                    firstFrame = false;
                    touch      = TOUCH.BEGAN;
                    dx         = 0;
                    dy         = 0;
                    dd         = 0;
                }
                else
                {
                    action = ACTION.SINGLEDRAG;
                    touch  = TOUCH.TOUCHING;

                    if (Input.GetKey(KeyCode.Space))
                    {
                        // equivalent to doubletouch dragging only
                        action = ACTION.DOUBLEDRAG;
                        dd     = 0;
                    }
                    if (Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt))
                    {
                        // equivalent to doubletouch pinching only
                        action = ACTION.DOUBLEDRAG;
                        dd     = dx;
                        dx     = 0;
                        dy     = 0;
                    }
                }

                trackTap();
            }

            if (Input.GetButtonUp("Fire1"))
            {
                touch = TOUCH.ENDED;

                // Catch double drag simulation to get the correct inertia, a dd value rather than dx/dy

                if (Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt))
                {
                    // equivalent to doubletouch pinching only
                    action = ACTION.DOUBLEDRAG;
                    dd     = dx;
                    dx     = 0;
                    dy     = 0;
                }


                if (wasTap())
                {
                    action = ACTION.TAP;
                }
                else
                {
                }

                firstFrame = true;
            }
#endif


#if UNITY_IOS
            // if we're on ios

            if (Input.touchCount == 1)
            {
                // review single touch
                Vector2 tp = Input.GetTouch(0).position;

                switch (Input.GetTouch(0).phase)
                {
                case TouchPhase.Began:
                    // Set x and y for target raycast.
                    x  = tp.x;
                    y  = tp.y;
                    dx = 0;
                    dy = 0;
                    dd = 0;
                    trackTap();
                    touch = TOUCH.BEGAN;
                    break;

                case TouchPhase.Moved:

                    Vector2 touchDelta = Input.GetTouch(0).deltaPosition;
                    dx     = touchDelta.x;
                    dy     = touchDelta.y;
                    dd     = 0;
                    action = ACTION.SINGLEDRAG;
                    touch  = TOUCH.TOUCHING;
                    trackTap();
                    break;

                case TouchPhase.Stationary:
                    dx     = 0;
                    dy     = 0;
                    dd     = 0;
                    action = ACTION.SINGLEDRAG;
                    touch  = TOUCH.TOUCHING;
                    trackTap();
                    break;

                case TouchPhase.Ended:

                    touch = TOUCH.ENDED;

                    if (wasTap())
                    {
                        action = ACTION.TAP;
                    }
                    else
                    {
                    }
                    break;

                default:
                    break;
                }
            }

            if (Input.touchCount == 2)
            {
                // review double touch
                TouchPhase phase0 = Input.GetTouch(0).phase;
                TouchPhase phase1 = Input.GetTouch(1).phase;

                Vector2 tp0 = Input.GetTouch(0).position;
                Vector2 tp1 = Input.GetTouch(1).position;

                if ((phase0 == TouchPhase.Moved || phase0 == TouchPhase.Stationary) && phase1 == TouchPhase.Began)
                {
                    // One finger was touching, second was added.
                    // Leave x and y at the position of the first touch
                    // initialise the d value so we can get delta d in the next frame
                    d  = Vector2.Distance(tp0, tp1);
                    dd = dx = dy = 0;

                    touch = TOUCH.TOUCHING;
                }
                else if (phase0 == TouchPhase.Began && phase1 == TouchPhase.Began)
                {
                    // if both start at the same time, aim in between to set targets and initialise the d value
                    x  = (tp0.x + tp1.x) / 2f;
                    y  = (tp0.y + tp1.y) / 2f;
                    d  = Vector2.Distance(tp0, tp1);
                    dd = dx = dy = 0;

                    touch = TOUCH.BEGAN;
                }
                else if (phase0 == TouchPhase.Ended && phase1 == TouchPhase.Began)
                {
                    // unlikely but could happen: flicking fingers in a single frame. catch the start of a new single touch.
                    x = tp1.x;
                    y = tp1.y;

                    dd    = dx = dy = 0;
                    touch = TOUCH.BEGAN;
                }
                else if ((phase0 == TouchPhase.Moved || phase0 == TouchPhase.Stationary) && (phase0 == TouchPhase.Moved || phase0 == TouchPhase.Stationary))
                {
                    // dragging
                    Vector2 touchDelta0 = Input.GetTouch(0).deltaPosition;
                    Vector2 touchDelta1 = Input.GetTouch(1).deltaPosition;

                    dx = (touchDelta0.x + touchDelta1.x) / 2f;
                    dy = (touchDelta0.y + touchDelta1.y) / 2f;

                    pd = d;
                    d  = Vector2.Distance(tp0, tp1);
                    dd = d - pd;

                    action = ACTION.DOUBLEDRAG;
                    touch  = TOUCH.TOUCHING;
                }
            }
#endif
        }