Esempio n. 1
0
        void UpdateDrag()
        {
            bool isDragging = buttonSelectPressed && IsManipulating();

            if (isDragging)
            {
                if (!prevDragging)
                {
                    dragControl.ApplyPos(false, Vector2.zero);
                }
                else
                {
                    Vector3 posDelta  = position - prevPosition;
                    float   pdDot     = Vector3.Dot(posDelta.normalized, rotation * Vector3.forward);
                    float   distDelta = pdDot * posDelta.magnitude;

                    dragControl.ApplyDelta(true, new Vector2(0, distDelta));
                }
            }
            else
            {
                dragControl.ApplyPos(false, Vector2.zero);
            }

            // Debug sphere
            if (debugSphere != null)
            {
                debugSphere.transform.position = position;// + rotation * Vector3.forward;
            }

            prevPosition = position;
            prevDragging = isDragging;
        }
        /// <summary>
        /// Updates all control states.  See InputControlState.cs.
        /// </summary>
        void UpdateStates()
        {
            // Hand zoom gesture
            stateHandZoom.ApplyState(inputSources.hands.dragControl);

            // Mouse wheel zoom
            stateMouseWheelZoom.AddState(false, new Vector2(0, inputSources.editor.mouseSource.ScrollWheelDelta));
            stateMouseWheelZoom.AddState(false, new Vector2(0, inputSources.worldCursorMouse.mouseSource.ScrollWheelDelta));
            stateMouseWheelZoom.FinalizeState();

            // SixDOF zoom gesture
            stateSixDOFZoom.ApplyDelta(false, new Vector2(0, inputSources.touch6D.dragControl.delta.y));

            // Controller input maps to scrolling, zooming, and freecam input
            stateLeftJoyScroll.ApplyDelta(false, inputSources.gamepad.leftJoyVector);
            stateLeftJoyTranslate.ApplyDelta(false, inputSources.gamepad.leftJoyVector);
            stateTrigZoom.ApplyDelta(false, new Vector2(0, -inputSources.gamepad.trigVector.x + inputSources.gamepad.trigVector.y));
            stateRightJoyRotate.ApplyDelta(false, inputSources.gamepad.rightJoyVector);
            statePadTranslate.ApplyDelta(false, inputSources.gamepad.padVector);
            statePadCardinal.ApplyDelta(false, inputSources.gamepad.padVector);

            // Joystick cardinal state
            Vector2 joyPos = Vector2.zero;

            if (inputSources.gamepadCardinal.IsActiveTargetingSource())
            {
                float mag = 0.7f;
                if (inputSources.gamepad.leftJoyVector.sqrMagnitude > mag * mag)
                {
                    joyPos = inputSources.gamepad.leftJoyVector;
                    // Quantize
                    joyPos.x = (float)Mathf.RoundToInt(5f * joyPos.x) / 5f;
                    joyPos.y = (float)Mathf.RoundToInt(5f * joyPos.y) / 5f;
                }
            }
            stateLeftJoyCardinal.ApplyDelta(false, joyPos);

            // Update drag gesture
            InputSourceBase curSource = inputSwitchLogic.CurrentTargetingSource as InputSourceBase;

            if (curSource != null && curSource.IsManipulating())
            {
                stateTargetScroll.ApplyPos(true, curSource.GetManipulationPlaneProjection());
                //Debug.Log("scroll state delta: " + stateTargetScroll.delta);
            }
            else
            {
                stateTargetScroll.ApplyPos(false, Vector2.zero);
                //Debug.Log("scroll (done) state delta: " + stateTargetScroll.delta);
            }
        }
        void UpdateHandDrag()
        {
            bool    isDragging = NumFingersPressed > 0 && IsManipulating();
            Vector3 handPos    = GetWorldPosition(0);

            if (isDragging)
            {
                if (!prevDragging)
                {
                    dragStartHeadPosition = HeadTransform.position;

                    if (handPos.y < dragStartHeadPosition.y)
                    {
                        dragStartHeadPosition.y = handPos.y;
                    }

                    dragStartHeadRotation = HeadTransform.rotation;

                    dragLocalHandStart = Quaternion.Inverse(dragStartHeadRotation) * (handPos - dragStartHeadPosition);

                    dragControl.ApplyPos(false, Vector2.zero);

                    prevHandPosition = handPos;
                    prevDragging     = isDragging;
                    UpdateHandDrag();
                }
                else
                {
                    // Use the head position pivot, but at the starting height
                    Vector3 pivotPos = HeadTransform.position;
                    pivotPos.y = dragStartHeadPosition.y;

                    // Find where the hand has moved relative to the starting pose (of head and hand)
                    Vector3 localOffsetFromStart = Quaternion.Inverse(dragStartHeadRotation) * (handPos - pivotPos);

                    // Get the difference in rotation
                    Quaternion handRotation = Quaternion.FromToRotation(dragLocalHandStart, localOffsetFromStart);

                    // Apply to original head direction
                    adjustedHandTargetRot = dragStartHeadRotation * handRotation;

                    // Update drag distance
                    Vector3 posDelta  = handPos - prevHandPosition;
                    float   pdDot     = Vector3.Dot(posDelta.normalized, HeadTransform.forward);
                    float   distDelta = pdDot * posDelta.magnitude;

                    // Deadzone?
                    //distDelta = Mathf.Sign(distDelta) * Mathf.Max(0, Mathf.Abs(distDelta) - dragDistanceDeadzone);

                    // Update the drag control
                    dragControl.ApplyDelta(true, new Vector2(0, distDelta));
                }
            }
            else
            {
                dragControl.ApplyPos(false, Vector2.zero);
            }

            prevHandPosition = handPos;
            prevDragging     = isDragging;
        }