Пример #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();
        }
Пример #2
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 timeOffset = 0)
            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))
            {
                eventPtr.time += timeOffset;
                control.WriteValueIntoEvent(state, eventPtr);
                InputSystem.QueueEvent(eventPtr);
            }

            InputSystem.Update();
        }