// Subscribe to events here.
 private void OnEnable()
 {
     instance = this;
     input    = new CMInput();
     input.Enable();
     InputInstance             = input;
     SceneManager.sceneLoaded += SceneLoaded;
     Application.wantsToQuit  += WantsToQuit;
 }
 public void InputObjectCreated(object obj)
 {
     input = obj as CMInput;
     if (File.Exists(path))
     {
         JSONNode keybindObject = JSON.Parse(File.ReadAllText(path));
         foreach (string key in keybindObject.Keys)
         {
             input.asset[key].ApplyBindingOverride(0, keybindObject[key]);
         }
     }
 }
Exemple #3
0
    // Start is called before the first frame update
    void OnTabSelected()
    {
        if (isInit)
        {
            return;
        }
        CMInput input = CMInputCallbackInstaller.InputInstance;

        //Grab each Action Map and create our Action Map Controller, which will loop through each Input Action and create Keybinds.
        foreach (InputActionMap actionMap in input.asset.actionMaps)
        {
            if (actionMap.name.StartsWith("+"))
            {
                continue;                                 //Filter keybinds that should not be modified (Designated with + prefix)
            }
            OptionsActionMapController map = Instantiate(prefab.gameObject, parent).GetComponent <OptionsActionMapController>();
            map.Init(actionMap.name, actionMap);
            map.gameObject.name = $"{actionMap.name} Action Map";
            allActionMaps.Add(map);
        }
        StartCoroutine(FuckingSetThisShitDirty());
        prefab.gameObject.SetActive(false);
        isInit = true;
    }
Exemple #4
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);
    }