Example #1
0
    private void Update()
    {
        if (inputLock > 0)
        {
            return;
        }

        //if (Input.GetKeyDown(KeyCode.W))
        //{
        //    gameManager.GameWon();
        //}

        //if (Input.GetKeyUp(KeyCode.R))
        //{
        //    gameManager.RandomRotation();
        //}

        //if (Input.GetKeyDown(KeyCode.U))
        //{
        //    gameManager.UndoLastRotation();
        //}

//#if !UNITY_ANDROID
        //If no touches were detected there's a good chance we want to check what the mouse is doing
        //I could make some platform checking but these days pretty much anything has touch and/or mouse support
        if (Input.touchCount == 0)
        {
            //Mouse zoom is a separate thing
            orbitingCamera.AddZoomMouseScrollInput(-Input.GetAxis("Mouse ScrollWheel"));

            if (Input.GetMouseButton(0) && !this.startedSwipeOnCube)
            {
                Vector2 mouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
                orbitingCamera.AddMouseInput(mouseDelta);
            }

            if (allowRotations)
            {
                if (startedSwipeOnCube == true && Input.GetMouseButtonUp(0))
                {
                    gameManager.CubeSwipePerformed(this.mouseSwipeStart, Input.mousePosition);
                    this.startedSwipeOnCube = false;
                }

                if (Input.GetMouseButtonDown(0))
                {
                    if (Physics.Raycast(orbitingCamera.GetComponent <Camera>().ScreenPointToRay(Input.mousePosition), out RaycastHit hit))
                    {
                        startedSwipeOnCube   = true;
                        this.mouseSwipeStart = Input.mousePosition;
                        gameManager.CubeSwipeStarted(hit.transform.position, hit.normal);
                    }
                }
            }
        }

//#endif

        //if (UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject != null)
        //{
        //    return;
        //}

        // Handle touch input
        if (Input.touchCount == 1)
        {
            Touch touch = Input.GetTouch(0);

            if (touch.phase == TouchPhase.Began && touch.phase == TouchPhase.Began)
            {
                if (allowRotations)
                {
                    if (Physics.Raycast(orbitingCamera.GetComponent <Camera>().ScreenPointToRay(Input.mousePosition), out RaycastHit hit))
                    {
                        startedSwipeOnCube   = true;
                        this.mouseSwipeStart = Input.mousePosition;
                        gameManager.CubeSwipeStarted(hit.transform.position, hit.normal);
                    }
                }
            }
            else if (!this.startedSwipeOnCube && touch.phase == TouchPhase.Moved)
            {
                orbitingCamera.AddTouchInput(touch.deltaPosition);
            }
            else if (this.startedSwipeOnCube && touch.phase == TouchPhase.Ended)
            {
                gameManager.CubeSwipePerformed(this.mouseSwipeStart, Input.mousePosition);
                this.startedSwipeOnCube = false;
            }
        }
        //Pinch to zoom
        else if (Input.touchCount == 2)
        {
            Touch touchZero = Input.GetTouch(0);
            Touch touchOne  = Input.GetTouch(1);

            if (touchZero.phase != TouchPhase.Moved && touchOne.phase != TouchPhase.Moved)
            {
                return;
            }

            // Find the position in the previous frame of each touch.
            Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
            Vector2 touchOnePrevPos  = touchOne.position - touchOne.deltaPosition;

            // Find the magnitude of the vector (the distance) between the touches in each frame.
            float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
            float touchDeltaMag     = (touchZero.position - touchOne.position).magnitude;

            // Find the difference in the distances between each frame.
            float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
            orbitingCamera.AddZoomInputPinch(deltaMagnitudeDiff);
        }
    }