예제 #1
0
        // Apply the given override to the action.
        //
        // NOTE: Ignores the action name in the override.
        // NOTE: Action must be disabled while applying overrides.
        // NOTE: If there's already an override on the respective binding, replaces the override.

        /// <summary>
        ///
        /// </summary>
        /// <param name="action"></param>
        /// <param name="bindingOverride"></param>
        /// <remarks>
        /// Binding overrides are non-destructive. They do not change the bindings set up for an action
        /// but rather apply non-destructive modifications that change the paths of existing bindings.
        /// However, this also means that for overrides to work, there have to be existing bindings that
        /// can be modified.
        /// </remarks>
        public static void ApplyBindingOverride(this InputAction action, InputBinding bindingOverride)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            bindingOverride.action = action.name;
            var actionMap = action.GetOrCreateActionMap();

            ApplyBindingOverride(actionMap, bindingOverride);
        }
예제 #2
0
        public static void ApplyBindingOverride(this InputAction action, int bindingIndex, InputBinding bindingOverride)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var indexOnMap = action.BindingIndexOnActionToBindingIndexOnMap(bindingIndex);

            bindingOverride.action = action.name;
            ApplyBindingOverride(action.GetOrCreateActionMap(), indexOnMap, bindingOverride);
        }
        public static BindingSyntax ChangeBinding(this InputAction action, InputBinding match)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var actionMap = action.GetOrCreateActionMap();

            actionMap.ThrowIfModifyingBindingsIsNotAllowed();
            var bindingIndex = actionMap.FindBinding(match);

            if (bindingIndex == -1)
            {
                throw new ArgumentException($"Cannot find binding matching '{match}' in '{action}'", nameof(match));
            }

            return(new BindingSyntax(actionMap, action, bindingIndex));
        }
예제 #4
0
        public static CompositeSyntax AddCompositeBinding(this InputAction action, string composite, string interactions = null)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }
            if (string.IsNullOrEmpty(composite))
            {
                throw new ArgumentException("Composite name cannot be null or empty", "composite");
            }

            var actionMap = action.GetOrCreateActionMap();
            ////REVIEW: use 'name' instead of 'path' field here?
            var binding = new InputBinding {
                path = composite, interactions = interactions, isComposite = true, action = action.name
            };
            var bindingIndex = AddBindingInternal(actionMap, binding);

            return(new CompositeSyntax(actionMap, action, bindingIndex));
        }
예제 #5
0
        /// <summary>
        /// Add a new binding to the action.
        /// </summary>
        /// <param name="action">A disabled action to add the binding to.</param>
        /// <param name="binding"></param>
        /// <returns>
        /// Returns a fluent-style syntax structure that allows performing additional modifications
        /// based on the new binding.
        /// </returns>
        /// <remarks>
        /// This works both with actions that are part of an action set as well as with actions that aren't.
        ///
        /// Note that actions must be disabled while altering their binding sets. Also, if the action belongs
        /// to a set, all actions in the set must be disabled.
        /// </remarks>
        public static BindingSyntax AddBinding(this InputAction action, InputBinding binding)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }
            if (string.IsNullOrEmpty(binding.path))
            {
                throw new ArgumentException("Binding path cannot be null or empty", "binding");
            }
            action.ThrowIfModifyingBindingsIsNotAllowed();

            Debug.Assert(action.m_Name != null || action.isSingletonAction);
            binding.action = action.m_Name;

            var actionMap    = action.GetOrCreateActionMap();
            var bindingIndex = AddBindingInternal(actionMap, binding);

            return(new BindingSyntax(actionMap, action, bindingIndex));
        }
예제 #6
0
        public static void ApplyBindingOverride(this InputAction action, int bindingIndex, InputBinding bindingOverride)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            // We don't want to hit InputAction.bindings here as this requires setting up per-action
            // binding info which we then nuke as part of the override process. Calling ApplyBindingOverride
            // repeatedly with an index would thus cause the same data to be computed and thrown away
            // over and over.
            // Instead we manually search through the map's bindings to find the right binding index
            // in the map.

            var actionMap         = action.GetOrCreateActionMap();
            var bindingsInMap     = actionMap.m_Bindings;
            var bindingCountInMap = bindingsInMap != null ? bindingsInMap.Length : 0;
            var actionName        = action.name;

            var currentBindingIndexOnAction = -1;

            for (var i = 0; i < bindingCountInMap; ++i)
            {
                if (string.Compare(bindingsInMap[i].action, actionName, StringComparison.InvariantCultureIgnoreCase) != 0)
                {
                    continue;
                }

                ++currentBindingIndexOnAction;
                if (currentBindingIndexOnAction == bindingIndex)
                {
                    bindingOverride.action = actionName;
                    ApplyBindingOverride(actionMap, i, bindingOverride);
                    return;
                }
            }

            throw new ArgumentOutOfRangeException(
                      string.Format("Binding index {0} is out of range for action '{1}' with {2} bindings", bindingIndex,
                                    action, currentBindingIndexOnAction), "bindingIndex");
        }