Example #1
0
    //
    // UPDATE: For input
    //

    void Update()
    {
        if (cursor.isOnHUD() && !isPanning && !isRotating && !isZooming)
        {
            return;
        }

        bool twoHands = false;

        if (cursor.mode != cursor_handle.MODE.BUILD)
        {
            // == Getting Input ==
            HandRenderer hr = cursor.GetComponentInChildren <HandRenderer> ();
            bool         panmode = false, rotatemode = false, zoommode = false;
            twoHands = (hr.getNumHandsDetected() == 2);
            // panmode  = (hr.getLeftHandGesture () == "fist" && hr.getRightHandGesture () == null) || (hr.getLeftHandGesture () == null && hr.getRightHandGesture () == "fist");
            panmode    = (hr.getNumHandsDetected() == 1 && (hr.getLeftHandGesture() == "fist" || hr.getRightHandGesture() == "fist"));
            rotatemode = (hr.getNumHandsDetected() == 2 && ((hr.getLeftHandGesture() == "v_sign" && hr.getRightHandGesture() == "v_sign")));                                     // || (hr.getLeftHandGesture () == "v_sign" && hr.getRightHandGesture () != null )) );
            //rotatemode    = (hr.getLeftHandGesture () == "v_sign" && hr.getRightHandGesture () == "v_sign");
            zoommode = (hr.getNumHandsDetected() == 2 && ((hr.getLeftHandGesture() == "v_sign" && hr.getRightHandGesture() == "v_sign")));                                       // || (hr.getLeftHandGesture () == "v_sign" && hr.getRightHandGesture () != null )) );
            // hr.getLeftHandGesture () == "v_sign" && hr.getRightHandGesture () == "v_sign");

            //if (!rotatemode)
            //		Debug.Log (hr.leftPresent + " <-> " + hr.rightPresent + " == " + hr.getLeftHandGesture () + " <-> " + hr.getRightHandGesture ());

            handmode = panmode || rotatemode || zoommode;
            //handmode = true;

            if (handmode)
            {
                //Debug.Log ("Pan: " + isPanning.ToString () + ", Rot: " + isRotating.ToString () + ", " + panmode.ToString () + ", " + rotatemode.ToString ());
                if (!isPanning && rotatemode)
                {
                    //		Debug.Log (hr.leftPresent +" <-> " +hr.rightPresent+" == "+hr.getLeftHandGesture () + " <-> " + hr.getRightHandGesture ());

                    isRotating = true;
                }
                else if (!isRotating && !isZooming && panmode)
                {
                    //Debug.Log (hr.leftPresent + " <-> " + hr.rightPresent + " == " + hr.getLeftHandGesture () + " <-> " + hr.getRightHandGesture ());

                    if (!isPanning)
                    {
                        bool lho = hr.queryLeftHand3DCoordinates(out lefthandorigin);
                        if (!lho)
                        {
                            lho = hr.queryRightHand3DCoordinates(out righthandorigin);
                        }
                        if (!lho)
                        {
//						isPanning = false;
                        }
                        else
                        {
                            isPanning = true;
                        }
                    }
                }

                if (!isPanning && zoommode)
                {
                    if (!isZooming)
                    {
                        bool lho = hr.queryLeftHand3DCoordinates(out lefthandorigin);
                        bool rho = hr.queryRightHand3DCoordinates(out righthandorigin);
                        if (!lho || !rho)
                        {
//						isZooming = false;
                        }
                        else
                        {
                            isZooming = true;
                        }
                    }
                }
                if (hr.getLeftHandGesture() == "spreadfingers" || hr.getRightHandGesture() == "spreadfingers")
                {
                    isPanning  = false;
                    isRotating = false;
                    isZooming  = false;
                }
                //Debug.Log ("ispanning " + isPanning + " isR " + isRotating);
            }
            else
            {
                // Get the left mouse button
                if (Input.GetMouseButtonDown(0))
                {
                    // Get mouse origin
                    mouseOrigin = Input.mousePosition;
                    isPanning   = true;
                }

                // Get the right mouse button
                if (Input.GetMouseButtonDown(1))
                {
                    // Get mouse origin
                    mouseOrigin = Input.mousePosition;
                    isRotating  = true;
                }

                // Get the middle mouse button
                if (Input.GetMouseButtonDown(2))
                {
                    // Get mouse origin
                    mouseOrigin = Input.mousePosition;
                    isZooming   = true;
                }

                // == Disable movements on Input Release ==

                if (!Input.GetMouseButton(0))
                {
                    isPanning = false;
                }
                if (!Input.GetMouseButton(1))
                {
                    isRotating = false;
                }
                if (!Input.GetMouseButton(2))
                {
                    isZooming = false;
                }

                // Get the mouse scroll
                // Debug.Log(Input.mouseScrollDelta.ToString());
                if (!zoommode && (Input.mouseScrollDelta.magnitude == 0 || ((transform.position.y <= 5.0f && Input.mouseScrollDelta.y >= 0) || (transform.position.y >= zoomOut && Input.mouseScrollDelta.y <= 0))))
                {
                    isZooming = false;
                }
                else if (!zoommode)
                {
                    isZooming = true;
                }
            }
        }

        Vector3    screenCenter = new Vector3(Screen.width / 2, Screen.height / 2);
        Ray        ray          = Camera.main.ScreenPointToRay(screenCenter);
        float      zoomIn       = 5.0f;
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 10.0f))
        {
            zoomIn = hit.point.y + 5.0f;
            //Debug.Log("HIT");
        }

        Vector3 cameraPosition = transform.position;

        // Debug.Log ("Camera: " + cameraPosition.ToString ()+" zin: " + zoomIn.ToString ()+ ", zout: " + zoomOut.ToString() + " clamp: " + Mathf.Clamp (transform.position.y, zoomIn, zoomOut).ToString() );
//		cameraPosition.y = Mathf.Clamp (transform.position.y, zoomIn, zoomOut);
//		transform.position = cameraPosition;

        prevRealTime = thisRealTime;
        thisRealTime = Time.realtimeSinceStartup;



        if (isRotating || isZooming || twoHands)
        {
            transform.GetChild(0).GetComponent <TrackingAction> ().Constraints.Rotation.X = true;
            transform.GetChild(0).GetComponent <TrackingAction> ().Constraints.Rotation.Y = true;
        }
        else
        {
            transform.GetChild(0).GetComponent <TrackingAction> ().Constraints.Rotation.X = false;
            transform.GetChild(0).GetComponent <TrackingAction> ().Constraints.Rotation.Y = false;
        }

        // Check the mode and display on the GUI
        if (isRotating || isZooming)
        {
            modeGUILabel.GetComponent <Text>().text = "Rotate/Zoom Mode";
        }
        else if (isPanning)
        {
            modeGUILabel.GetComponent <Text>().text = "Pan Mode";
        }
        else if (cursor.mode == cursor_handle.MODE.DEFAULT)
        {
            // idle
            modeGUILabel.GetComponent <Text>().text = "Idle";
        }
        else if (cursor.mode == cursor_handle.MODE.BUILD)
        {
            modeGUILabel.GetComponent <Text>().text = "Build Mode";
        }
        else if (cursor.mode == cursor_handle.MODE.REACH)
        {
            modeGUILabel.GetComponent <Text>().text = "Reach Mode";
        }
    }