public override bool processInput(GestureProfile profile)
    {
        if (Input.touchCount > 0)
        {
            //
            //Check for adding new touches
            //
            for (int i = 0; i < Input.touchCount; i++)
            {
                Touch touch = Input.touches[i];
                if (touch.phase == TouchPhase.Began)
                {
                    touchDatas.Add(touch.fingerId, new TouchData(touch));
                    origTouchCenter           = TouchCenter;
                    origCameraZoom            = Managers.Camera.ZoomLevel;
                    origAvgDistanceFromCenter = AverageDistanceFromCenter;
                }
            }
            maxTouchCount = Mathf.Max(maxTouchCount, Input.touchCount);

            //
            // Gesture Identification
            //

            if (touchEvent == TouchEvent.UNKNOWN)
            {
                if (maxTouchCount == 1)
                {
                    Touch     touch = Input.touches[0];
                    TouchData data  = touchDatas[touch.fingerId];
                    //Drag Gesture
                    if (Vector2.Distance(data.origPosScreen, touch.position) >= dragThreshold)
                    {
                        touchEvent = TouchEvent.DRAG;
                    }
                    //Hold Gesture
                    if (Time.time - data.origTime >= holdThreshold)
                    {
                        touchEvent = TouchEvent.HOLD;
                    }
                }
                //If more than one touch
                else if (maxTouchCount > 1)
                {
                    touchEvent = TouchEvent.CAMERA;
                }
            }
            //If converting from a player gesture to a camera gesture,
            else if (touchEvent != TouchEvent.CAMERA && maxTouchCount > 1)
            {
                //End the current player gesture
                //(No need to process tap gesture,
                //because it requires that all input stops to activate)
                Touch     touch = Input.touches[0];
                TouchData data  = touchDatas[touch.fingerId];
                switch (touchEvent)
                {
                //DRAG
                case TouchEvent.DRAG:
                    profile.processDragGesture(
                        data.origPosWorld,
                        Utility.ScreenToWorldPoint(touch.position),
                        DragType.DRAG_PLAYER,
                        true
                        );
                    break;

                //HOLD
                case TouchEvent.HOLD:
                    profile.processHoldGesture(
                        Utility.ScreenToWorldPoint(touch.position),
                        Time.time - data.origTime,
                        true
                        );
                    break;
                }
                //Convert to camera gesture
                touchEvent = TouchEvent.CAMERA;
            }

            //
            //Main Processing
            //

            if (maxTouchCount == 1)
            {
                Touch     touch = Input.touches[0];
                TouchData data  = touchDatas[touch.fingerId];
                switch (touchEvent)
                {
                //DRAG
                case TouchEvent.DRAG:
                    profile.processDragGesture(
                        data.origPosWorld,
                        Utility.ScreenToWorldPoint(touch.position),
                        DragType.DRAG_PLAYER,
                        touch.phase == TouchPhase.Ended
                        );
                    break;

                //HOLD
                case TouchEvent.HOLD:
                    profile.processHoldGesture(
                        Utility.ScreenToWorldPoint(touch.position),
                        Time.time - data.origTime,
                        touch.phase == TouchPhase.Ended
                        );
                    break;
                }

                //
                // Check for tap end
                //
                if (touch.phase == TouchPhase.Ended)
                {
                    //If it's unknown,
                    if (touchEvent == TouchEvent.UNKNOWN)
                    {
                        //Then it's a tap
                        profile.processTapGesture(Utility.ScreenToWorldPoint(touch.position));
                    }
                }
            }
            else if (maxTouchCount > 1)
            {
                //Get the center and drag the camera to it
                profile.processDragGesture(
                    origTouchCenterWorld,
                    Utility.ScreenToWorldPoint(TouchCenter),
                    DragType.DRAG_CAMERA,
                    Input.touches
                    .Where(t =>
                           t.phase != TouchPhase.Ended &&
                           t.phase != TouchPhase.Canceled
                           ).ToArray()
                    .Length == 0
                    );
                //Get the change in scale and zoom the camera
                float adfc = AverageDistanceFromCenter;
                if (adfc > 0)
                {
                    float scaleFactor = origAvgDistanceFromCenter / adfc;
                    Managers.Camera.ZoomLevel = origCameraZoom * scaleFactor;
                }
            }

            //
            //Check for removing touches
            //
            for (int i = 0; i < Input.touchCount; i++)
            {
                Touch touch = Input.touches[i];
                if (touch.phase == TouchPhase.Ended)
                {
                    touchDatas.Remove(touch.fingerId);
                    origTouchCenter           = TouchCenter;
                    origCameraZoom            = Managers.Camera.ZoomLevel;
                    origAvgDistanceFromCenter = AverageDistanceFromCenter;
                }
            }
            return(true);
        }
        //If there is no input,
        else
        {
            //Reset gesture variables
            touchEvent                = TouchEvent.UNKNOWN;
            maxTouchCount             = 0;
            origTouchCenter           = Vector2.zero;
            origCameraZoom            = Managers.Camera.ZoomLevel;
            origAvgDistanceFromCenter = 0;
            touchDatas.Clear();
            return(false);
        }
    }
Esempio n. 2
0
    public override bool processInput(GestureProfile profile)
    {
        if (InputOngoing)
        {
            //
            //Check for click start
            //
            if (mouseEvent == MouseEvent.UNKNOWN)
            {
                //Click beginning
                if (Input.GetMouseButtonDown(mouseButton))
                {
                    origPosScreen = Input.mousePosition;
                    origTime      = Time.time;
                    dragType      = DragType.DRAG_PLAYER;
                }
                else if (Input.GetMouseButtonDown(mouseButton2))
                {
                    origPosScreen = Input.mousePosition;
                    origTime      = Time.time;
                    dragType      = DragType.DRAG_CAMERA;
                }
                else if (Input.GetAxis("Mouse ScrollWheel") != 0)
                {
                    mouseEvent = MouseEvent.SCROLL;
                }
                //Click middle
                else
                {
                    //Check Drag
                    float dragDistance = Vector2.Distance(origPosScreen, Input.mousePosition);
                    if (dragDistance >= dragThreshold)
                    {
                        mouseEvent = MouseEvent.DRAG;
                    }
                    //Check Hold
                    else if (Time.time - origTime >= holdThreshold)
                    {
                        mouseEvent = MouseEvent.HOLD;
                    }
                }
            }

            //
            //Main Processing
            //

            switch (mouseEvent)
            {
            case MouseEvent.DRAG:
                profile.processDragGesture(
                    OrigPosWorld,
                    Utility.ScreenToWorldPoint(Input.mousePosition),
                    dragType,
                    Input.GetMouseButtonUp(mouseButton) || Input.GetMouseButtonUp(mouseButton2)
                    );
                break;

            case MouseEvent.HOLD:
                profile.processHoldGesture(
                    Utility.ScreenToWorldPoint(Input.mousePosition),
                    Time.time - origTime,
                    Input.GetMouseButtonUp(mouseButton) || Input.GetMouseButtonUp(mouseButton2)
                    );
                break;

            case MouseEvent.SCROLL:
                if (Input.GetAxis("Mouse ScrollWheel") < 0)
                {
                    Managers.Camera.ZoomLevel *= 1.2f;
                }
                else if (Input.GetAxis("Mouse ScrollWheel") > 0)
                {
                    Managers.Camera.ZoomLevel /= 1.2f;
                }
                break;
            }

            //
            //Check for click end
            //
            if (Input.GetMouseButtonUp(mouseButton))
            {
                //If it's unknown,
                if (mouseEvent == MouseEvent.UNKNOWN)
                {
                    //Then it's a click.
                    mouseEvent = MouseEvent.CLICK;
                    profile.processTapGesture(Utility.ScreenToWorldPoint(Input.mousePosition));
                }
            }
            if (Input.GetMouseButtonUp(mouseButton2))
            {
                //If it's unknown,
                if (mouseEvent == MouseEvent.UNKNOWN)
                {
                    //Then it's a camera drag
                    mouseEvent = MouseEvent.DRAG;
                    profile.processDragGesture(
                        OrigPosWorld,
                        Utility.ScreenToWorldPoint(Input.mousePosition),
                        dragType,
                        true
                        );
                }
            }
            return(true);
        }
        //If there's no input,
        else
        {
            //Reset gesture variables
            mouseEvent = MouseEvent.UNKNOWN;
            dragType   = DragType.UNKNOWN;
            //Hover gesture
            profile.processHoverGesture(
                Utility.ScreenToWorldPoint(Input.mousePosition)
                );
            return(false);
        }
    }