Exemple #1
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();
        }