Ejemplo n.º 1
0
    // per-frame mouse input; called from Update
    unsafe void ProcessMouse()
    {
        // get modifiers
        bool alt     = Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt);
        bool shift   = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
        bool control = Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);

        // get button pressed, swap left-right on alt
        int buttonpressed = 0;

        if (Input.GetMouseButton(0))           // left
        {
            buttonpressed = (alt ? 2 : 1);
        }
        if (Input.GetMouseButton(1))           // right
        {
            buttonpressed = (alt ? 1 : 2);
        }
        if (Input.GetMouseButton(2))           // middle
        {
            buttonpressed = 3;
        }

        // get button click, swap left-right on alt
        int buttonclick = 0;

        if (Input.GetMouseButtonDown(0))       // left
        {
            buttonclick = (alt ? 2 : 1);
        }
        if (Input.GetMouseButtonDown(1))       // right
        {
            buttonclick = (alt ? 1 : 2);
        }
        if (Input.GetMouseButtonDown(2))       // middle
        {
            buttonclick = 3;
        }

        // click
        if (buttonclick > 0)
        {
            // set perturbation state
            int newstate = 0;
            if (control)
            {
                // determine new perturbation state
                if (buttonclick == 1)
                {
                    newstate = 2;              // rotate
                }
                else if (buttonclick == 2)
                {
                    newstate = 1;              // move
                }
                // get old perturbation state
                MJP.TPerturb current;
                MJP.GetPerturb(&current);

                // syncronize if starting perturbation now
                if (newstate > 0 && current.active == 0)
                {
                    MJP.PerturbSynchronize();
                }
            }
            MJP.PerturbActive(newstate);

            // process double-click
            if (buttonclick == lastbutton && Time.fixedUnscaledTime - lasttime < 0.25)
            {
                // relative screen position and aspect ratio
                float relx   = Input.mousePosition.x / Screen.width;
                float rely   = Input.mousePosition.y / Screen.height;
                float aspect = (float)Screen.width / (float)Screen.height;

                // left: select body
                if (buttonclick == 1)
                {
                    MJP.PerturbSelect(relx, rely, aspect);
                }

                // right: set lookat
                else if (buttonclick == 2)
                {
                    MJP.CameraLookAt(relx, rely, aspect);
                }
            }

            // save mouse state
            lastx      = Input.mousePosition.x;
            lasty      = Input.mousePosition.y;
            lasttime   = Time.fixedUnscaledTime;
            lastbutton = buttonclick;
        }

        // left or right drag: manipulate camera or perturb
        if (buttonpressed == 1 || buttonpressed == 2)
        {
            // compute relative displacement and modifier
            float reldx    = (Input.mousePosition.x - lastx) / Screen.height;
            float reldy    = (Input.mousePosition.y - lasty) / Screen.height;
            int   modifier = (shift ? 1 : 0);

            // perturb
            if (control)
            {
                if (buttonpressed == 1)
                {
                    MJP.PerturbRotate(reldx, -reldy, modifier);
                }
                else
                {
                    MJP.PerturbMove(reldx, -reldy, modifier);
                }
            }

            // camera
            else
            {
                if (buttonpressed == 1)
                {
                    MJP.CameraRotate(reldx, -reldy);
                }
                else
                {
                    MJP.CameraMove(reldx, -reldy, modifier);
                }
            }
        }

        // middle drag: zoom camera
        if (buttonpressed == 3)
        {
            float reldy = (Input.mousePosition.y - lasty) / Screen.height;
            MJP.CameraZoom(-reldy);
        }

        // scroll: zoom camera
        if (Input.mouseScrollDelta.y != 0)
        {
            MJP.CameraZoom(-0.05f * Input.mouseScrollDelta.y);
        }

        // save position
        lastx = Input.mousePosition.x;
        lasty = Input.mousePosition.y;

        // release left or right: stop perturb
        if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1))
        {
            MJP.PerturbActive(0);
        }
    }