예제 #1
0
    public void UpdateUIElements()
    {
        InputActionMap map;
        InputActionMap mapActions = inputs.FindActionMap("Actions");
        InputActionMap mapMenu    = inputs.FindActionMap("MainMenu");

        for (int j = 0; j < groupNames.Length; j++)
        {
            for (int i = 0; i < actionNames.Length; i++)
            {
                if (texts[j].Length > i)
                {
                    if (i < mapSwitchIndex)
                    {
                        map = mapActions;
                    }
                    else
                    {
                        map = mapMenu;
                    }
                    InputAction action       = map.FindAction(actionNames[i]);
                    int         bindingIndex = action.GetBindingIndex(InputBinding.MaskByGroup(groupNames[j]));
                    texts[j][i].text = action.GetBindingDisplayString(bindingIndex);
                }
            }
        }
    }
예제 #2
0
    public void RebindActionStart(InputAction inputAction, string group)
    {
        InputActionRebindingExtensions.RebindingOperation rebindOperation = null;

        void CleanUp()
        {
            rebindOperation?.Dispose();
            rebindOperation = null;
        }

        int    bindingIndex = inputAction.GetBindingIndex(InputBinding.MaskByGroup(group));
        string controlType;
        string cancelType;
        bool   keyboard;

        if (bindingIndex == 3)
        {
            controlType = "<Gamepad>";
            cancelType  = "<Gamepad>/start";
            keyboard    = false;
        }
        else
        {
            controlType = "<keyboard>";
            cancelType  = "<Keyboard>/escape";
            keyboard    = true;
        }

        rebindOperation = inputAction.PerformInteractiveRebinding(bindingIndex).WithCancelingThrough(cancelType)
                          .WithCancelingThrough("<Keyboard>/escape").WithControlsHavingToMatchPath(controlType).OnCancel(operation =>
        {
            CleanUp();
            ui.SetInstructionOverlay(false);
        })
                          .OnComplete(operation =>
        {
            CleanUp();
            ui.UpdateSingleElement(inputAction, bindingIndex, inputAction.actionMap.name);
            ui.SetInstructionOverlay(false);
        });

        ui.SetInstructionOverlay(true, keyboard);
        rebindOperation.Start();
    }
예제 #3
0
    /// <summary>
    /// Adds a new <see cref="KeybindOverride"/> to the list of overrides. This will remove any already existing overrides.
    /// </summary>
    /// <param name="keybindOverride"></param>
    public static void AddKeybindOverride(KeybindOverride keybindOverride)
    {
        // Do not override keybinds if paths are not in acceptable bounds
        if (keybindOverride.OverrideKeybindPaths.Count <= 0 || keybindOverride.OverrideKeybindPaths.Count > 4)
        {
            return;
        }
        // Remove anyexisting override to prevent duplicates
        AllOverrides.RemoveAll(x => x.InputActionName == keybindOverride.InputActionName && x.CompositeKeybindName == keybindOverride.CompositeKeybindName);

        // Grab our CMInput object and the map our action map is in.
        CMInput        input = CMInputCallbackInstaller.InputInstance;
        InputActionMap map   = input.asset.actionMaps.Where(x => x.actions.Any(y => y.name == keybindOverride.InputActionName)).FirstOrDefault();

        if (map is null)
        {
            return;
        }

        InputAction action = map.FindAction(keybindOverride.InputActionName);

        // Determine what existing bindings we need to erase
        List <InputBinding> toErase = new List <InputBinding>();
        // Grab our composite keybind
        InputBinding bindingToOverride = action.bindings.Where(x => x.name == keybindOverride.CompositeKeybindName).FirstOrDefault();

        if (bindingToOverride == null) // This is not a composite keybind, just grab the first one
        {
            bindingToOverride = action.bindings.First();
        }
        toErase.Add(bindingToOverride);
        // Grab all composite pieces
        for (int i = action.GetBindingIndex(bindingToOverride) + 1; i < action.bindings.Count; i++)
        {
            if (action.bindings[i].isPartOfComposite)
            {
                toErase.Add(action.bindings[i]);
            }
            else
            {
                break;
            }
        }
        // Reverse them so that the Composite keybind is erased last, and prevents errors.
        toErase.Reverse();
        // Erase the bindings
        foreach (InputBinding binding in toErase)
        {
            Debug.Log($"Deleting {binding.name} from {action.name}");
            action.ChangeBinding(action.GetBindingIndex(binding)).Erase();
        }

        // Add a new binding depending on some conditions
        switch (keybindOverride.OverrideKeybindPaths.Count)
        {
        case 1:     // With one override path, make a regular binding
            action.AddBinding(keybindOverride.OverrideKeybindPaths[0]);
            break;

        case 2 when keybindOverride.IsAxisComposite:     // Create a 1D Axis if we need to.
            action.AddCompositeBinding("1DAxis")
            .With("positive", keybindOverride.OverrideKeybindPaths[0])
            .With("negative", keybindOverride.OverrideKeybindPaths[1]);
            RenameCompositeBinding(action, keybindOverride);
            break;

        case 2 when !keybindOverride.IsAxisComposite:     // Else, create a composite.
            action.AddCompositeBinding("ButtonWithOneModifier")
            .With("modifier", keybindOverride.OverrideKeybindPaths[0])
            .With("button", keybindOverride.OverrideKeybindPaths[1]);
            RenameCompositeBinding(action, keybindOverride);
            break;

        case 3:     // No 1.5D Axis, so just a composite with two modifiers.
            action.AddCompositeBinding("ButtonWithTwoModifiers")
            .With("modifier2", keybindOverride.OverrideKeybindPaths[0])
            .With("modifier1", keybindOverride.OverrideKeybindPaths[1])
            .With("button", keybindOverride.OverrideKeybindPaths[2]);
            RenameCompositeBinding(action, keybindOverride);
            break;

        case 4 when keybindOverride.IsAxisComposite:     // 4 paths means a 2D Axis composite
            action.AddCompositeBinding("2DVector(mode=2)")
            .With("up", keybindOverride.OverrideKeybindPaths[0])
            .With("left", keybindOverride.OverrideKeybindPaths[1])
            .With("down", keybindOverride.OverrideKeybindPaths[2])
            .With("right", keybindOverride.OverrideKeybindPaths[3]);
            RenameCompositeBinding(action, keybindOverride);
            break;

        default: break;
        }

        Debug.Log($"Added keybind override for {keybindOverride.InputActionName}.");
        AllOverrides.Add(keybindOverride);
    }