Exemple #1
0
        /// <summary>
        /// Set the control to the given value by sending a state event with the value to the
        /// control's device.
        /// </summary>
        /// <param name="control">An input control on a device that has been added to the system.</param>
        /// <param name="state">New value for the input control.</param>
        /// <typeparam name="TValue">Value type of the given control.</typeparam>
        /// <example>
        /// <code>
        /// var gamepad = InputSystem.AddDevice&lt;Gamepad&gt;();
        /// Set(gamepad.leftButton, 1);
        /// </code>
        /// </example>
        public void Set <TValue>(InputControl <TValue> control, TValue state, double absoluteTime = -1, double timeOffset = 0)
            where TValue : struct
        {
            if (control == null)
            {
                throw new ArgumentNullException(nameof(control));
            }
            if (!control.device.added)
            {
                throw new ArgumentException(
                          $"Device of control '{control}' has not been added to the system", nameof(control));
            }

            using (StateEvent.From(control.device, out var eventPtr))
            {
                ////REVIEW: should we by default take the time from the device here?
                if (absoluteTime >= 0)
                {
                    eventPtr.time = absoluteTime;
                }
                eventPtr.time += timeOffset;
                control.WriteValueIntoEvent(state, eventPtr);
                InputSystem.QueueEvent(eventPtr);
            }

            InputSystem.Update();
        }
        public static void SendValueToControl <TValue>(InputControl <TValue> control, TValue value)
        {
            TValue currentValue = control.value;

            if (value.Equals(currentValue))
            {
                return;
            }

            var inputEvent = InputSystem.CreateEvent <GenericControlEvent <TValue> >();

            inputEvent.device          = (InputDevice)control.provider;
            inputEvent.controlIndex    = control.index;
            inputEvent.value           = value;
            inputEvent.alreadyRemapped = true;
            InputSystem.QueueEvent(inputEvent);
        }
Exemple #3
0
        /// <summary>
        /// Set the control to the given value by sending a state event with the value to the
        /// control's device.
        /// </summary>
        /// <param name="control">An input control on a device that has been added to the system.</param>
        /// <param name="state">New value for the input control.</param>
        /// <typeparam name="TValue">Value type of the given control.</typeparam>
        /// <example>
        /// <code>
        /// var gamepad = InputSystem.AddDevice&lt;Gamepad&gt;();
        /// Set(gamepad.leftButton, 1);
        /// </code>
        /// </example>
        public void Set <TValue>(InputControl <TValue> control, TValue state)
            where TValue : struct
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            if (!control.device.added)
            {
                throw new ArgumentException(
                          string.Format("Device of control '{0}' has not been added to the system", control), "control");
            }

            InputEventPtr eventPtr;

            using (StateEvent.From(control.device, out eventPtr))
            {
                control.WriteValueInto(eventPtr, state);
                InputSystem.QueueEvent(eventPtr);
            }

            InputSystem.Update();
        }
Exemple #4
0
        /// <summary>
        /// Perform the input action without having to know what it is bound to.
        /// </summary>
        /// <param name="action">An input action that is currently enabled and has controls it is bound to.</param>
        /// <remarks>
        /// Blindly triggering an action requires making a few assumptions. Actions are not built to be able to trigger
        /// without any input. This means that this method has to generate input on a control that the action is bound to.
        ///
        /// Note that this method has no understanding of the interactions that may be present on the action and thus
        /// does not know how they may affect the triggering of the action.
        /// </remarks>
        public void Trigger(InputAction action)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            if (!action.enabled)
            {
                throw new ArgumentException(
                          string.Format("Action '{0}' must be enabled in order to be able to trigger it", action), "action");
            }

            var controls = action.controls;

            if (controls.Count == 0)
            {
                throw new ArgumentException(
                          string.Format("Action '{0}' must be bound to controls in order to be able to trigger it", action), "action");
            }

            // See if we have a button we can trigger.
            for (var i = 0; i < controls.Count; ++i)
            {
                var button = controls[i] as ButtonControl;
                if (button == null)
                {
                    continue;
                }

                // We do, so flip its state and we're done.
                var           device = button.device;
                InputEventPtr inputEvent;
                using (StateEvent.From(device, out inputEvent))
                {
                    button.WriteValueInto(inputEvent, button.isPressed ? 0 : 1);
                    InputSystem.QueueEvent(inputEvent);
                    InputSystem.Update();
                }

                return;
            }

            // See if we have an axis we can slide a bit.
            for (var i = 0; i < controls.Count; ++i)
            {
                var axis = controls[i] as AxisControl;
                if (axis == null)
                {
                    continue;
                }

                // We do, so nudge its value a bit.
                var           device = axis.device;
                InputEventPtr inputEvent;
                using (StateEvent.From(device, out inputEvent))
                {
                    var currentValue = axis.ReadValue();
                    var newValue     = currentValue + 0.01f;

                    if (axis.clamp && newValue > axis.clampMax)
                    {
                        newValue = axis.clampMin;
                    }

                    axis.WriteValueInto(inputEvent, newValue);
                    InputSystem.QueueEvent(inputEvent);
                    InputSystem.Update();
                }

                return;
            }

            ////TODO: support a wider range of controls
            throw new NotImplementedException();
        }