示例#1
0
        public override unsafe float EvaluateMagnitude(void *statePtr)
        {
            if (m_MinValue.isEmpty || m_MaxValue.isEmpty)
            {
                return(-1);
            }

            var value = ReadValueFromState(statePtr);
            var min   = m_MinValue.ToFloat();
            var max   = m_MaxValue.ToFloat();

            value = Mathf.Clamp(value, min, max);

            // If part of our range is in negative space, evaluate magnitude as two
            // separate subspaces.
            if (min < 0)
            {
                if (value < 0)
                {
                    return(NormalizeProcessor.Normalize(Mathf.Abs(value), 0, Mathf.Abs(min), 0));
                }
                return(NormalizeProcessor.Normalize(value, 0, max, 0));
            }

            return(NormalizeProcessor.Normalize(value, min, max, 0));
        }
示例#2
0
 protected float Preprocess(float value)
 {
     if (scale)
     {
         value *= scaleFactor;
     }
     if (clampToConstant)
     {
         if (value < clampMin || value > clampMax)
         {
             value = clampConstant;
         }
     }
     else if (clamp)
     {
         value = Mathf.Clamp(value, clampMin, clampMax);
     }
     if (normalize)
     {
         value = NormalizeProcessor.Normalize(value, normalizeMin, normalizeMax, normalizeZero);
     }
     if (invert)
     {
         value *= -1.0f;
     }
     return(value);
 }
示例#3
0
    public void Devices_SupportsDualShockAsHID()
    {
        var gamepad = InputSystem.AddDevice <DualShockGamepadHID>();

        // Dpad has default state value so make sure that one is coming through.
        Assert.That(gamepad.dpad.ReadValue(), Is.EqualTo(Vector2.zero).Using(Vector2EqualityComparer.Instance));

        InputSystem.QueueStateEvent(gamepad,
                                    new DualShockHIDInputReport
        {
            leftStickX   = 32,
            leftStickY   = 64,
            rightStickX  = 128,
            rightStickY  = 255,
            leftTrigger  = 20,
            rightTrigger = 40,
            buttons1     = 0xf7, // Low order 4 bits is Dpad but effectively uses only 3 bits.
            buttons2     = 0xff,
            buttons3     = 0xff
        });
        InputSystem.Update();

        var leftStickDeadzone  = gamepad.leftStick.TryGetProcessor <StickDeadzoneProcessor>();
        var rightStickDeadzone = gamepad.leftStick.TryGetProcessor <StickDeadzoneProcessor>();

        Assert.That(gamepad.leftStick.ReadValue(),
                    Is.EqualTo(leftStickDeadzone.Process(
                                   new Vector2(NormalizeProcessor.Normalize(32 / 255.0f, 0f, 1f, 0.5f),
                                               -NormalizeProcessor.Normalize(64 / 255.0f, 0f, 1f, 0.5f)))));

        Assert.That(gamepad.rightStick.ReadValue(), Is.EqualTo(rightStickDeadzone.Process(
                                                                   new Vector2(NormalizeProcessor.Normalize(128 / 255.0f, 0f, 1f, 0.5f),
                                                                               -NormalizeProcessor.Normalize(255 / 255.0f, 0f, 1f, 0.5f)))));

        Assert.That(gamepad.leftTrigger.ReadValue(), Is.EqualTo(NormalizeProcessor.Normalize(20 / 255.0f, 0f, 1f, 0f)).Within(0.00001));
        Assert.That(gamepad.rightTrigger.ReadValue(), Is.EqualTo(NormalizeProcessor.Normalize(40 / 255.0f, 0f, 1f, 0f)).Within(0.00001));
        ////TODO: test button presses individually
        Assert.That(gamepad.buttonSouth.isPressed);
        Assert.That(gamepad.buttonEast.isPressed);
        Assert.That(gamepad.buttonWest.isPressed);
        Assert.That(gamepad.buttonNorth.isPressed);
        Assert.That(gamepad.squareButton.isPressed);
        Assert.That(gamepad.triangleButton.isPressed);
        Assert.That(gamepad.circleButton.isPressed);
        Assert.That(gamepad.crossButton.isPressed);
        Assert.That(gamepad.startButton.isPressed);
        Assert.That(gamepad.selectButton.isPressed);
        Assert.That(gamepad.dpad.up.isPressed); // Dpad uses enumerated values, not a bitfield; 0x7 is up left.
        Assert.That(gamepad.dpad.down.isPressed, Is.False);
        Assert.That(gamepad.dpad.left.isPressed);
        Assert.That(gamepad.dpad.right.isPressed, Is.False);
        Assert.That(gamepad.leftShoulder.isPressed);
        Assert.That(gamepad.rightShoulder.isPressed);
        Assert.That(gamepad.leftStickButton.isPressed);
        Assert.That(gamepad.rightStickButton.isPressed);
        Assert.That(gamepad.touchpadButton.isPressed);

        // Sensors not (yet?) supported. Needs figuring out how to interpret the HID data.
    }
示例#4
0
    public DualShockGamepad Devices_SupportsDualShockAsHID <TDevice, TState>(TState state)
        where TDevice : DualShockGamepad
        where TState : struct, IInputStateTypeInfo
    {
        var gamepad = InputSystem.AddDevice <TDevice>();

        // Dpad has default state value so make sure that one is coming through.
        Assert.That(gamepad.dpad.ReadValue(), Is.EqualTo(Vector2.zero).Using(Vector2EqualityComparer.Instance));

        InputSystem.QueueStateEvent(gamepad, state);

        InputSystem.Update();

        var leftStickDeadzone  = gamepad.leftStick.TryGetProcessor <StickDeadzoneProcessor>();
        var rightStickDeadzone = gamepad.leftStick.TryGetProcessor <StickDeadzoneProcessor>();

        Assert.That(gamepad.leftStick.ReadValue(),
                    Is.EqualTo(leftStickDeadzone.Process(
                                   new Vector2(NormalizeProcessor.Normalize(32 / 255.0f, 0f, 1f, 0.5f),
                                               -NormalizeProcessor.Normalize(64 / 255.0f, 0f, 1f, 0.5f)))));

        Assert.That(gamepad.rightStick.ReadValue(), Is.EqualTo(rightStickDeadzone.Process(
                                                                   new Vector2(NormalizeProcessor.Normalize(128 / 255.0f, 0f, 1f, 0.5f),
                                                                               -NormalizeProcessor.Normalize(255 / 255.0f, 0f, 1f, 0.5f)))));

        Assert.That(gamepad.leftTrigger.ReadValue(), Is.EqualTo(NormalizeProcessor.Normalize(20 / 255.0f, 0f, 1f, 0f)).Within(0.00001));
        Assert.That(gamepad.rightTrigger.ReadValue(), Is.EqualTo(NormalizeProcessor.Normalize(40 / 255.0f, 0f, 1f, 0f)).Within(0.00001));
        ////TODO: test button presses individually
        Assert.That(gamepad.buttonSouth.isPressed);
        Assert.That(gamepad.buttonEast.isPressed);
        Assert.That(gamepad.buttonWest.isPressed);
        Assert.That(gamepad.buttonNorth.isPressed);
        Assert.That(gamepad.squareButton.isPressed);
        Assert.That(gamepad.triangleButton.isPressed);
        Assert.That(gamepad.circleButton.isPressed);
        Assert.That(gamepad.crossButton.isPressed);
        Assert.That(gamepad.startButton.isPressed);
        Assert.That(gamepad.selectButton.isPressed);
        Assert.That(gamepad.dpad.up.isPressed); // Dpad uses enumerated values, not a bitfield; 0x7 is up left.
        Assert.That(gamepad.dpad.down.isPressed, Is.False);
        Assert.That(gamepad.dpad.left.isPressed);
        Assert.That(gamepad.dpad.right.isPressed, Is.False);
        Assert.That(gamepad.leftShoulder.isPressed);
        Assert.That(gamepad.rightShoulder.isPressed);
        Assert.That(gamepad.leftStickButton.isPressed);
        Assert.That(gamepad.rightStickButton.isPressed);

        ////REVIEW: Should we just kill these buttons? Do they provide any value?
        // PS controller adds buttons for the left and right trigger. Make sure these are marked as
        // synthetic so they don't get picked up as double input.
        // https://fogbugz.unity3d.com/f/cases/1293734
        Assert.That(gamepad["leftTriggerButton"].synthetic, Is.True);
        Assert.That(gamepad["rightTriggerButton"].synthetic, Is.True);

        return(gamepad);
        // Sensors not (yet?) supported. Needs figuring out how to interpret the HID data.
    }
示例#5
0
    public void Devices_SupportsDualShockAsHID()
    {
        var device = InputSystem.AddDevice(new InputDeviceDescription
        {
            product       = "Wireless Controller",
            manufacturer  = "Sony Interactive Entertainment",
            interfaceName = "HID"
        });

        Assert.That(device, Is.AssignableTo <DualShockGamepad>());
        var gamepad = (DualShockGamepad)device;

        InputSystem.QueueStateEvent(gamepad,
                                    new DualShockHIDInputReport
        {
            leftStickX   = 32,
            leftStickY   = 64,
            rightStickX  = 128,
            rightStickY  = 255,
            leftTrigger  = 20,
            rightTrigger = 40,
            buttons1     = 0xf7, // Low order 4 bits is Dpad but effectively uses only 3 bits.
            buttons2     = 0xff,
            buttons3     = 0xff
        });
        InputSystem.Update();

        Assert.That(gamepad.leftStick.x.ReadValue(), Is.EqualTo(NormalizeProcessor.Normalize(32 / 255.0f, 0f, 1f, 0.5f)).Within(0.00001));
        Assert.That(gamepad.leftStick.y.ReadValue(), Is.EqualTo(-NormalizeProcessor.Normalize(64 / 255.0f, 0f, 1f, 0.5f)).Within(0.00001));
        Assert.That(gamepad.rightStick.x.ReadValue(), Is.EqualTo(NormalizeProcessor.Normalize(128 / 255.0f, 0f, 1f, 0.5f)).Within(0.00001));
        Assert.That(gamepad.rightStick.y.ReadValue(), Is.EqualTo(-NormalizeProcessor.Normalize(255 / 255.0f, 0f, 1f, 0.5f)).Within(0.00001));
        Assert.That(gamepad.leftTrigger.ReadValue(), Is.EqualTo(NormalizeProcessor.Normalize(20 / 255.0f, 0f, 1f, 0f)).Within(0.00001));
        Assert.That(gamepad.rightTrigger.ReadValue(), Is.EqualTo(NormalizeProcessor.Normalize(40 / 255.0f, 0f, 1f, 0f)).Within(0.00001));
        ////TODO: test button presses individually
        Assert.That(gamepad.buttonSouth.isPressed);
        Assert.That(gamepad.buttonEast.isPressed);
        Assert.That(gamepad.buttonWest.isPressed);
        Assert.That(gamepad.buttonNorth.isPressed);
        Assert.That(gamepad.squareButton.isPressed);
        Assert.That(gamepad.triangleButton.isPressed);
        Assert.That(gamepad.circleButton.isPressed);
        Assert.That(gamepad.crossButton.isPressed);
        Assert.That(gamepad.startButton.isPressed);
        Assert.That(gamepad.selectButton.isPressed);
        Assert.That(gamepad.dpad.up.isPressed); // Dpad uses enumerated values, not a bitfield; 0x7 is up left.
        Assert.That(gamepad.dpad.down.isPressed, Is.False);
        Assert.That(gamepad.dpad.left.isPressed);
        Assert.That(gamepad.dpad.right.isPressed, Is.False);
        Assert.That(gamepad.leftShoulder.isPressed);
        Assert.That(gamepad.rightShoulder.isPressed);
        Assert.That(gamepad.leftStickButton.isPressed);
        Assert.That(gamepad.rightStickButton.isPressed);
        Assert.That(gamepad.touchpadButton.isPressed);

        // Sensors not (yet?) supported. Needs figuring out how to interpret the HID data.
    }
示例#6
0
        /// <inheritdoc />
        public override float EvaluateMagnitude(ref InputBindingCompositeContext context)
        {
            var value = ReadValue(ref context);

            if (value < midPoint)
            {
                value = Mathf.Abs(value - midPoint);
                return(NormalizeProcessor.Normalize(value, 0, Mathf.Abs(minValue), 0));
            }

            value = Mathf.Abs(value - midPoint);
            return(NormalizeProcessor.Normalize(value, 0, Mathf.Abs(maxValue), 0));
        }
示例#7
0
    public DualShockGamepad Devices_SupportsDualShockAsHID <TDevice, TState>(TState state)
        where TDevice : DualShockGamepad
        where TState : struct, IInputStateTypeInfo
    {
        var gamepad = InputSystem.AddDevice <TDevice>();

        // Dpad has default state value so make sure that one is coming through.
        Assert.That(gamepad.dpad.ReadValue(), Is.EqualTo(Vector2.zero).Using(Vector2EqualityComparer.Instance));

        InputSystem.QueueStateEvent(gamepad, state);

        InputSystem.Update();

        var leftStickDeadzone  = gamepad.leftStick.TryGetProcessor <StickDeadzoneProcessor>();
        var rightStickDeadzone = gamepad.leftStick.TryGetProcessor <StickDeadzoneProcessor>();

        Assert.That(gamepad.leftStick.ReadValue(),
                    Is.EqualTo(leftStickDeadzone.Process(
                                   new Vector2(NormalizeProcessor.Normalize(32 / 255.0f, 0f, 1f, 0.5f),
                                               -NormalizeProcessor.Normalize(64 / 255.0f, 0f, 1f, 0.5f)))));

        Assert.That(gamepad.rightStick.ReadValue(), Is.EqualTo(rightStickDeadzone.Process(
                                                                   new Vector2(NormalizeProcessor.Normalize(128 / 255.0f, 0f, 1f, 0.5f),
                                                                               -NormalizeProcessor.Normalize(255 / 255.0f, 0f, 1f, 0.5f)))));

        Assert.That(gamepad.leftTrigger.ReadValue(), Is.EqualTo(NormalizeProcessor.Normalize(20 / 255.0f, 0f, 1f, 0f)).Within(0.00001));
        Assert.That(gamepad.rightTrigger.ReadValue(), Is.EqualTo(NormalizeProcessor.Normalize(40 / 255.0f, 0f, 1f, 0f)).Within(0.00001));
        ////TODO: test button presses individually
        Assert.That(gamepad.buttonSouth.isPressed);
        Assert.That(gamepad.buttonEast.isPressed);
        Assert.That(gamepad.buttonWest.isPressed);
        Assert.That(gamepad.buttonNorth.isPressed);
        Assert.That(gamepad.squareButton.isPressed);
        Assert.That(gamepad.triangleButton.isPressed);
        Assert.That(gamepad.circleButton.isPressed);
        Assert.That(gamepad.crossButton.isPressed);
        Assert.That(gamepad.startButton.isPressed);
        Assert.That(gamepad.selectButton.isPressed);
        Assert.That(gamepad.dpad.up.isPressed); // Dpad uses enumerated values, not a bitfield; 0x7 is up left.
        Assert.That(gamepad.dpad.down.isPressed, Is.False);
        Assert.That(gamepad.dpad.left.isPressed);
        Assert.That(gamepad.dpad.right.isPressed, Is.False);
        Assert.That(gamepad.leftShoulder.isPressed);
        Assert.That(gamepad.rightShoulder.isPressed);
        Assert.That(gamepad.leftStickButton.isPressed);
        Assert.That(gamepad.rightStickButton.isPressed);

        return(gamepad);
        // Sensors not (yet?) supported. Needs figuring out how to interpret the HID data.
    }
示例#8
0
 private float Preprocess(float value)
 {
     if (clamp)
     {
         value = Mathf.Clamp(value, clampMin, clampMax);
     }
     if (normalize)
     {
         value = NormalizeProcessor.Normalize(value, normalizeMin, normalizeMax);
     }
     if (invert)
     {
         value *= -1.0f;
     }
     return(value);
 }
示例#9
0
        private float Unpreprocess(float value)
        {
            // Does not reverse the effect of clamping (we don't know what the unclamped value should be).

            if (invert)
            {
                value *= -1f;
            }
            if (normalize)
            {
                value = NormalizeProcessor.Denormalize(value, normalizeMin, normalizeMax, normalizeZero);
            }
            if (scale)
            {
                value /= scaleFactor;
            }
            return(value);
        }
    public void Devices_SupportsDualShockAsHID()
    {
#if !UNITY_WSA
        var device = InputSystem.AddDevice(new InputDeviceDescription
        {
            product       = "Wireless Controller",
            manufacturer  = "Sony Interactive Entertainment",
            interfaceName = "HID",
        });
#else // UWP requires different query logic (manufacture not available)
        var device = InputSystem.AddDevice(new InputDeviceDescription
        {
            product       = "Wireless Controller",
            interfaceName = "HID",
            capabilities  = new HID.HIDDeviceDescriptor
            {
                vendorId = 0x054C, // Sony
            }.ToJson()
        });
#endif

        Assert.That(device, Is.AssignableTo <DualShockGamepad>());
        var gamepad = (DualShockGamepad)device;

        // Dpad has default state value so make sure that one is coming through.
        Assert.That(gamepad.dpad.ReadValue(), Is.EqualTo(Vector2.zero).Using(vector2Comparer));

        InputSystem.QueueStateEvent(gamepad,
                                    new DualShockHIDInputReport
        {
            leftStickX   = 32,
            leftStickY   = 64,
            rightStickX  = 128,
            rightStickY  = 255,
            leftTrigger  = 20,
            rightTrigger = 40,
            buttons1     = 0xf7, // Low order 4 bits is Dpad but effectively uses only 3 bits.
            buttons2     = 0xff,
            buttons3     = 0xff
        });
        InputSystem.Update();

        Assert.That(gamepad.leftStick.x.ReadValue(), Is.EqualTo(NormalizeProcessor.Normalize(32 / 255.0f, 0f, 1f, 0.5f)).Within(0.00001));
        Assert.That(gamepad.leftStick.y.ReadValue(), Is.EqualTo(-NormalizeProcessor.Normalize(64 / 255.0f, 0f, 1f, 0.5f)).Within(0.00001));
        Assert.That(gamepad.rightStick.x.ReadValue(), Is.EqualTo(NormalizeProcessor.Normalize(128 / 255.0f, 0f, 1f, 0.5f)).Within(0.00001));
        Assert.That(gamepad.rightStick.y.ReadValue(), Is.EqualTo(-NormalizeProcessor.Normalize(255 / 255.0f, 0f, 1f, 0.5f)).Within(0.00001));
        Assert.That(gamepad.leftTrigger.ReadValue(), Is.EqualTo(NormalizeProcessor.Normalize(20 / 255.0f, 0f, 1f, 0f)).Within(0.00001));
        Assert.That(gamepad.rightTrigger.ReadValue(), Is.EqualTo(NormalizeProcessor.Normalize(40 / 255.0f, 0f, 1f, 0f)).Within(0.00001));
        ////TODO: test button presses individually
        Assert.That(gamepad.buttonSouth.isPressed);
        Assert.That(gamepad.buttonEast.isPressed);
        Assert.That(gamepad.buttonWest.isPressed);
        Assert.That(gamepad.buttonNorth.isPressed);
        Assert.That(gamepad.squareButton.isPressed);
        Assert.That(gamepad.triangleButton.isPressed);
        Assert.That(gamepad.circleButton.isPressed);
        Assert.That(gamepad.crossButton.isPressed);
        Assert.That(gamepad.startButton.isPressed);
        Assert.That(gamepad.selectButton.isPressed);
        Assert.That(gamepad.dpad.up.isPressed); // Dpad uses enumerated values, not a bitfield; 0x7 is up left.
        Assert.That(gamepad.dpad.down.isPressed, Is.False);
        Assert.That(gamepad.dpad.left.isPressed);
        Assert.That(gamepad.dpad.right.isPressed, Is.False);
        Assert.That(gamepad.leftShoulder.isPressed);
        Assert.That(gamepad.rightShoulder.isPressed);
        Assert.That(gamepad.leftStickButton.isPressed);
        Assert.That(gamepad.rightStickButton.isPressed);
        Assert.That(gamepad.touchpadButton.isPressed);

        // Sensors not (yet?) supported. Needs figuring out how to interpret the HID data.
    }