Beispiel #1
0
        private void LoadInputMapConfiguration(string configPath, string defaultPath)
        {
            ConfigFile configFile = new ConfigFile();
            Directory  directory  = new Directory();
            Dictionary <string, Dictionary <string, object> > config = this.LoadConfig(configPath, defaultPath, configFile, directory);

            foreach (var item in config)
            {
                var action   = item.Key;
                var value    = item.Value;
                var deadzone = value.ContainsKey("deadzone") ? (float)value["deadzone"] : 0.5f;
                var gamepad  = value.ContainsKey("GamePad") ? (int)value["GamePad"] : -1;
                var keyboard = value.ContainsKey("Keyboard") ? value["Keyboard"] as string : "";
                if (action != "" && gamepad != -1 && keyboard != "")
                {
                    if (!InputMap.HasAction(action))
                    {
                        InputMap.AddAction(action);
                    }
                    var inputKey = new InputEventKey();
                    inputKey.Scancode = (uint)OS.FindScancodeFromString(keyboard);
                    InputMap.ActionAddEvent(action, inputKey);
                    var inputjoy = new InputEventJoypadButton();
                    inputjoy.ButtonIndex = gamepad;
                    InputMap.ActionAddEvent(action, inputjoy);
                    InputMap.ActionSetDeadzone(action, deadzone);
                }
            }
        }
    public override void _Input(InputEvent _event)
    {
        var method = _event.InputMethod();

        if (method == InputMethod.None)
        {
            return;
        }

        if (method != InputMethod.None && method != CurrentInputMethod)
        {
            CurrentInputMethod = method;
            if (IsVisibleInTree())
            {
                InitializeKeybindings();
            }
        }
        if (ActionWaitingForInput != null)
        {
            GetNode <Control>(keybindingPopupPath).Hide();
            KeybindingElement element = GetKeybingingElement(ActionWaitingForInput);
            InputMap.ActionEraseEvent(ActionWaitingForInput, element.InputEvent);
            InputMap.ActionAddEvent(ActionWaitingForInput, _event);
            element.InputEvent    = _event;
            ActionWaitingForInput = null;
        }
    }
Beispiel #3
0
    public static void CheckAndAddAction(string action, KeyList key, bool ctrl = false, bool alt = false, bool shift = false, bool cmd = false)
    {
        if (!InputMap.HasAction(action))
        {
            //Tell user that input was added
            string message = action + " not found in Input Map. Action was added and set to ";
            if (ctrl || alt || shift || cmd)
            {
                message += (ctrl ? "ctrl + " : "") + (alt ? "alt + " : "") + (shift ? "shift + " : "") + (cmd ? "cmd + " : "");
            }
            message += key.ToString();
            GD.Print(message);

            //Add the action
            InputMap.AddAction(action);

            //Create the event
            InputEventWithModifiers inputEventWithModifiers = new InputEventKey
            {
                Scancode = (uint)key
            };
            inputEventWithModifiers.Control = ctrl;
            inputEventWithModifiers.Alt     = alt;
            inputEventWithModifiers.Shift   = shift;
            inputEventWithModifiers.Command = cmd;

            //Add event to action
            InputMap.ActionAddEvent(action, inputEventWithModifiers);
        }
    }
Beispiel #4
0
    public void ScrambleControls()
    {
        // Add rotate inputs to the scramble if the player has unlocked it.
        if (PlayerEvolution > 3 && !_actions.Contains("rotate_left") && !_actions.Contains("rotate_right"))
        {
            _actions.Add("rotate_left");
            _actions.Add("rotate_right");
        }

        var activeInputs = new ArrayList();

        foreach (string action in _actions)
        {
            activeInputs.Add(InputMap.GetActionList(action)[0]);
            InputMap.ActionEraseEvents(action);
        }

        // Randomly assign inputs to said events.
        var random = new Random();

        foreach (string action in _actions)
        {
            int index = random.Next(activeInputs.Count);
            InputMap.ActionAddEvent(action, (InputEvent)activeInputs[index]);
            activeInputs.RemoveAt(index);
        }
    }
Beispiel #5
0
    private void AddKeyboardInputsToGameMapping()
    {
        string        playerPrefix;
        string        action;
        InputEventKey iek;

        SC.IDictionaryEnumerator it;
        Dictionary mapping;

        for (int i = 1; i <= playerAmount; i++)
        {
            playerPrefix = this.CreateString("p", i, "_");
            mapping      = keyboardInputMapping[i.ToString()] as Dictionary;

            if (mapping != null)
            {
                it = mapping.GetEnumerator();

                while (it.MoveNext())
                {
                    action = this.CreateString(playerPrefix, it.Entry.Key);
                    InputMap.ActionEraseEvents(action);
                    iek          = new InputEventKey();
                    iek.Scancode = uint.Parse(it.Entry.Value as string);
                    InputMap.ActionAddEvent(action, iek);
                }
            }
        }
    }
Beispiel #6
0
    private static void applyJoy(string action_name, string key)
    {
        var e = new InputEventJoypadButton();

        //e.Device
        e.ButtonIndex = int.Parse(key);
        InputMap.ActionAddEvent(action_name, e);
    }
Beispiel #7
0
    private static void applyKey(string action_name, string key)
    {
        int scancode = OS.FindScancodeFromString(key);
        var e        = new InputEventKey();

        e.Scancode = (uint)scancode;
        InputMap.ActionAddEvent(action_name, e);
    }
Beispiel #8
0
    private void OnResetButtonPressed()
    {
        InputHelper.EraseActionEvents(_inputMapKey);
        var ev = InputHelper.GetDefaultInputEvent(_inputMapKey);

        InputMap.ActionAddEvent(_inputMapKey, ev);
        _lineEditNode.Text = InputHelper.GetKeyText(ev as InputEventKey);
    }
 private void ApplyControls()
 {
     InputMap.ActionAddEvent("Move-Left", _lKey);
     InputMap.ActionAddEvent("Move-Right", _rKey);
     InputMap.ActionAddEvent("Jump", _jKey);
     InputMap.ActionAddEvent("Action", _aKey);
     InputMap.ActionAddEvent("Pause", _pKey);
     InputMap.ActionAddEvent("Okay", _oKey);
 }
Beispiel #10
0
 private void OnGuiInput(InputEvent ev)
 {
     if (ev is InputEventKey e)
     {
         InputHelper.EraseActionEvents(_inputMapKey);
         InputMap.ActionAddEvent(_inputMapKey, ev);
         _lineEditNode.Text = InputHelper.GetKeyText(e);
         AcceptEvent();
     }
 }
Beispiel #11
0
    /// <summary> Bind a mouse button to fire an action. </summary>
    public static void BindMouseButton(ButtonList mouseButton, string actionName)
    {
        InputEventMouseButton inputEvent = new InputEventMouseButton();

        inputEvent.ButtonIndex = (int)mouseButton;
        if (!InputMap.HasAction(actionName))
        {
            InputMap.AddAction(actionName);
        }
        InputMap.ActionAddEvent(actionName, inputEvent);
    }
Beispiel #12
0
    /// <summary> Bind a key to fire an action. </summary>
    public static void BindKey(KeyList key, string actionName)
    {
        InputEventKey inputEvent = new InputEventKey();

        inputEvent.Scancode = (uint)key;
        if (!InputMap.HasAction(actionName))
        {
            InputMap.AddAction(actionName);
        }
        InputMap.ActionAddEvent(actionName, inputEvent);
    }
 public static void SetActionScancodes(Dictionary <string, List <int> > actionScancodes)
 {
     foreach (var key in actionScancodes.Keys)
     {
         EraseActionEvents(key);
         var scancodes = actionScancodes[key];
         foreach (var scancode in scancodes)
         {
             var keyEvent = new InputEventKey();
             keyEvent.Scancode = scancode;
             InputMap.ActionAddEvent(key, keyEvent);
         }
     }
 }
Beispiel #14
0
        private void AddKey(string Key, string Action)
        {
            InputEventKey EventKey = new InputEventKey();

            EventKey.Scancode = (uint)EnumUtil.GetKeyFromString <int, KeyList>(Key);

            if (InputMap.HasAction(Action))
            {
                if (!InputMap.ActionHasEvent(Action, EventKey))
                {
                    InputMap.ActionAddEvent(Action, EventKey);
                }
            }
        }
Beispiel #15
0
        public void SetInputMap()
        {
            var actions = Enum.GetValues(typeof(ActionType)).Cast <ActionType>();

            foreach (var action in actions)
            {
                var name = action.ToString();
                InputMap.ActionEraseEvents(name);
                foreach (var bind in Settings.ActionControls[action])
                {
                    var inputEvent = bind.ToInputEvent();
                    InputMap.ActionAddEvent(name, inputEvent);
                }
            }
        }
Beispiel #16
0
 private void on_add_input_map()
 {
     if (GamePad == null && Keyboard == null && action == "")
     {
         return;
     }
     GD.Print("add");
     if (!InputMap.HasAction(action))
     {
         InputMap.AddAction(action, deadzone);
     }
     InputMap.ActionEraseEvents(action);
     InputMap.ActionAddEvent(action, GamePad);
     InputMap.ActionAddEvent(action, Keyboard);
 }
Beispiel #17
0
    private void AddControllerInputsToGameMapping()
    {
        string playerPrefix;
        string action;
        InputEventJoypadMotion iejm;
        InputEventJoypadButton iejb;

        SC.IDictionaryEnumerator it = controllerInputMapping.GetEnumerator();
        Dictionary mapping;

        for (int i = 1; i <= playerAmount; i++)
        {
            mapping = controllerInputMapping[i.ToString()] as Dictionary;

            if (mapping != null && mapping.Contains("deviceId"))
            {
                playerPrefix = this.CreateString("p", i, "_");
                int deviceId = int.Parse(mapping["deviceId"] as string);
                it = mapping.GetEnumerator();

                while (it.MoveNext())
                {
                    if (!it.Entry.Key.Equals("deviceId"))
                    {
                        action = this.CreateString(playerPrefix, it.Entry.Key);
                        string inputValue = it.Entry.Value as string;

                        if (IsDirectionInput(inputValue))
                        {
                            iejm = new InputEventJoypadMotion();
                            float[] axisData = ConvertInputcodeToJoystickAxis(inputValue);
                            iejm.Axis      = System.Convert.ToInt32(axisData[0]);
                            iejm.AxisValue = axisData[1];
                            iejm.Device    = deviceId;
                            InputMap.ActionAddEvent(action, iejm);
                        }
                        else
                        {
                            iejb             = new InputEventJoypadButton();
                            iejb.ButtonIndex = int.Parse(it.Entry.Value as string);
                            iejb.Device      = deviceId;
                            InputMap.ActionAddEvent(action, iejb);
                        }
                    }
                }
            }
        }
    }
Beispiel #18
0
        public async void AddControl(ActionType action)
        {
            _popup.Popup_();

            await ToSignal(_popup, "NewControl");

            if (_popup.NewEvent == null)
            {
                return;
            }

            var inputEvent = _popup.NewEvent;
            var bind       = inputEvent.ToInputBind();

            Settings.ActionControls[action].Add(bind);

            InputMap.ActionAddEvent(action.ToString(), inputEvent);
            NewBind(action, bind);
        }
Beispiel #19
0
 public override void _Input(InputEvent @event)
 {
     if (changing)
     {
         if (globale.controller_mode == "keyboard")
         {
             if (@event is InputEventKey)
             {
                 Godot.Collections.Array old_button = InputMap.GetActionList(liste_strings[input_changing]);
                 foreach (InputEvent e in old_button)
                 {
                     if (e is InputEventKey)
                     {
                         InputMap.ActionEraseEvent(liste_strings[input_changing], e);
                     }
                 }
                 InputMap.ActionAddEvent(liste_strings[input_changing], @event);
                 changing          = false;
                 changinge.Visible = changing;
                 update();
             }
         }
         else if (globale.controller_mode == "controller")
         {
             if (@event is InputEventJoypadButton)
             {
                 Godot.Collections.Array old_button = InputMap.GetActionList(liste_strings[input_changing]);
                 foreach (InputEvent e in old_button)
                 {
                     if (@event is InputEventJoypadButton)
                     {
                         InputMap.ActionEraseEvent(liste_strings[input_changing], e);
                     }
                 }
                 InputMap.ActionAddEvent(liste_strings[input_changing], @event);
                 changing          = false;
                 changinge.Visible = changing;
                 update();
             }
         }
     }
 }
Beispiel #20
0
    /// <summary>
    ///   Applies the current controls (from Data) to the global InputMap.
    /// </summary>
    internal void ApplyToGodotInputMap()
    {
        foreach (var action in Data)
        {
            // Clear all old input keys
            InputMap.ActionEraseEvents(action.Key);

            // Register the new input keys
            foreach (var inputEvent in action.Value)
            {
                // If the game is waiting for an input for this thing, skip trying to apply it
                if (inputEvent == null)
                    return;

                InputMap.ActionAddEvent(action.Key, inputEvent.ToInputEvent());
            }
        }

        InputsRemapped?.Invoke(this, EventArgs.Empty);
    }
Beispiel #21
0
    public static void Bind(string FunctionName, string InputString)
    {
        BIND_TYPE Type = BIND_TYPE.SCANCODE;

        if (System.Array.IndexOf(MouseButtonList, InputString) >= 0)
        {
            Type = BIND_TYPE.MOUSEBUTTON;
        }
        if (System.Array.IndexOf(MouseWheelList, InputString) >= 0)
        {
            Type = BIND_TYPE.MOUSEWHEEL;
        }
        if (System.Array.IndexOf(AxisList, InputString) >= 0)
        {
            Type = BIND_TYPE.AXIS;
        }

        if (InputMap.HasAction(FunctionName))
        {
            InputMap.EraseAction(FunctionName);
            foreach (BindingObject Bind in BindingList)
            {
                if (Bind.Name == FunctionName)
                {
                    BindingList.Remove(Bind);
                    break;
                }
            }
        }

        if (Type == BIND_TYPE.SCANCODE)
        {
            InputMap.AddAction(FunctionName);
            InputEventKey Event = new InputEventKey();
            Event.Scancode = OS.FindScancodeFromString(InputString);
            InputMap.ActionAddEvent(FunctionName, Event);
            BindingList.Add(new BindingObject(FunctionName, Type));
        }
        else if (Type == BIND_TYPE.MOUSEBUTTON)
        {
            InputMap.AddAction(FunctionName);
            InputEventMouseButton Event = new InputEventMouseButton();
            switch (InputString)
            {
            case ("MouseOne"):
                Event.ButtonIndex = (int)ButtonList.Left;
                break;

            case ("MouseTwo"):
                Event.ButtonIndex = (int)ButtonList.Right;
                break;

            case ("MouseThree"):
                Event.ButtonIndex = (int)ButtonList.Middle;
                break;
                //No default as this else if will not run unless one of these string will match anyway
            }
            InputMap.ActionAddEvent(FunctionName, Event);
            BindingList.Add(new BindingObject(FunctionName, Type));
        }
        else if (Type == BIND_TYPE.MOUSEWHEEL)
        {
            InputMap.AddAction(FunctionName);
            InputEventMouseButton Event = new InputEventMouseButton();
            switch (InputString)
            {
            case ("WheelUp"):
                Event.ButtonIndex = (int)ButtonList.WheelUp;
                break;

            case ("WheelDown"):
                Event.ButtonIndex = (int)ButtonList.WheelDown;
                break;
            }
            InputMap.ActionAddEvent(FunctionName, Event);
            BindingList.Add(new BindingObject(FunctionName, Type));
        }
        else if (Type == BIND_TYPE.AXIS)
        {
            InputMap.AddAction(FunctionName);
            InputEventMouseMotion Event = new InputEventMouseMotion();
            InputMap.ActionAddEvent(FunctionName, Event);
            BindingObject Bind = new BindingObject(FunctionName, Type);
            switch (InputString)
            {
            case ("MouseUp"):
                Bind.AxisDirection = BindingObject.DIRECTION.UP;
                break;

            case ("MouseDown"):
                Bind.AxisDirection = BindingObject.DIRECTION.DOWN;
                break;

            case ("MouseRight"):
                Bind.AxisDirection = BindingObject.DIRECTION.RIGHT;
                break;

            case ("MouseLeft"):
                Bind.AxisDirection = BindingObject.DIRECTION.LEFT;
                break;
            }
            BindingList.Add(Bind);
        }
    }
Beispiel #22
0
    public static bool Bind(string KeyName, string FunctionName)
    {
        BindingObject NewBind = new BindingObject(KeyName);

        bool Found = false;

        if (WithArgMethods == null)
        {
            var Methods = Assembly.GetExecutingAssembly().GetTypes()
                          .SelectMany(t => t.GetMethods())
                          .Where(m => m.GetCustomAttributes(typeof(SteelInputWithArg), false).Length > 0);

            WithArgMethods = new List <WithArgInfo>();
            foreach (MethodInfo Method in Methods)
            {
                WithArgMethods.Add(
                    new WithArgInfo(
                        Method.Name,
                        Attribute.GetCustomAttribute(Method, typeof(SteelInputWithArg)) as SteelInputWithArg
                        )
                    );
            }
        }
        foreach (WithArgInfo Method in WithArgMethods)
        {
            if (Method.Name == FunctionName)
            {
                Found = true;
                NewBind.FuncWithArg = Method.Tag.Function;
                break;
            }
        }

        if (WithoutArgMethods == null)
        {
            var Methods = Assembly.GetExecutingAssembly().GetTypes()
                          .SelectMany(t => t.GetMethods())
                          .Where(m => m.GetCustomAttributes(typeof(SteelInputWithoutArg), false).Length > 0);

            WithoutArgMethods = new List <WithoutArgInfo>();
            foreach (MethodInfo Method in Methods)
            {
                WithoutArgMethods.Add(
                    new WithoutArgInfo(
                        Method.Name,
                        Attribute.GetCustomAttribute(Method, typeof(SteelInputWithoutArg)) as SteelInputWithoutArg
                        )
                    );
            }
        }
        foreach (WithoutArgInfo Method in WithoutArgMethods)
        {
            if (Method.Name == FunctionName)
            {
                Found = true;
                NewBind.FuncWithoutArg = Method.Tag.Function;
                break;
            }
        }

        if (!Found)
        {
            Console.ThrowPrint($"The specified function '{FunctionName}' does not exist as a bindable function");
            return(false);
        }

        var  ButtonValue           = ButtonList.Left;
        var  AxisDirection         = DIRECTION.UP;
        var  ControllerButtonValue = JoystickList.Axis0;
        uint Scancode = 0;

        switch (KeyName)        //Checks custom string literals first then assumes Scancode
        {
        case ("MouseOne"): {
            NewBind.Type = TYPE.MOUSEBUTTON;
            ButtonValue  = ButtonList.Left;
            break;
        }

        case ("MouseTwo"): {
            NewBind.Type = TYPE.MOUSEBUTTON;
            ButtonValue  = ButtonList.Right;
            break;
        }

        case ("MouseThree"): {
            NewBind.Type = TYPE.MOUSEBUTTON;
            ButtonValue  = ButtonList.Middle;
            break;
        }

        case ("WheelUp"): {
            NewBind.Type = TYPE.MOUSEWHEEL;
            ButtonValue  = ButtonList.WheelUp;
            break;
        }

        case ("WheelDown"): {
            NewBind.Type = TYPE.MOUSEWHEEL;
            ButtonValue  = ButtonList.WheelDown;
            break;
        }

        case ("MouseUp"): {
            NewBind.Type  = TYPE.MOUSEAXIS;
            AxisDirection = DIRECTION.UP;
            break;
        }

        case ("MouseDown"): {
            NewBind.Type  = TYPE.MOUSEAXIS;
            AxisDirection = DIRECTION.DOWN;
            break;
        }

        case ("MouseRight"): {
            NewBind.Type  = TYPE.MOUSEAXIS;
            AxisDirection = DIRECTION.RIGHT;
            break;
        }

        case ("MouseLeft"): {
            NewBind.Type  = TYPE.MOUSEAXIS;
            AxisDirection = DIRECTION.LEFT;
            break;
        }

        case ("LeftStickUp"): {
            NewBind.Type          = TYPE.CONTROLLERAXIS;
            AxisDirection         = DIRECTION.UP;
            ControllerButtonValue = JoystickList.AnalogLy;
            break;
        }

        case ("LeftStickDown"): {
            NewBind.Type          = TYPE.CONTROLLERAXIS;
            AxisDirection         = DIRECTION.DOWN;
            ControllerButtonValue = JoystickList.AnalogLy;
            break;
        }

        case ("LeftStickLeft"): {
            NewBind.Type          = TYPE.CONTROLLERAXIS;
            AxisDirection         = DIRECTION.LEFT;
            ControllerButtonValue = JoystickList.AnalogLx;
            break;
        }

        case ("LeftStickRight"): {
            NewBind.Type          = TYPE.CONTROLLERAXIS;
            AxisDirection         = DIRECTION.RIGHT;
            ControllerButtonValue = JoystickList.AnalogLx;
            break;
        }

        case ("RightStickUp"): {
            NewBind.Type          = TYPE.CONTROLLERAXIS;
            AxisDirection         = DIRECTION.UP;
            ControllerButtonValue = JoystickList.AnalogRy;
            break;
        }

        case ("RightStickDown"): {
            NewBind.Type          = TYPE.CONTROLLERAXIS;
            AxisDirection         = DIRECTION.DOWN;
            ControllerButtonValue = JoystickList.AnalogRy;
            break;
        }

        case ("RightStickLeft"): {
            NewBind.Type          = TYPE.CONTROLLERAXIS;
            AxisDirection         = DIRECTION.LEFT;
            ControllerButtonValue = JoystickList.AnalogRx;
            break;
        }

        case ("RightStickRight"): {
            NewBind.Type          = TYPE.CONTROLLERAXIS;
            AxisDirection         = DIRECTION.RIGHT;
            ControllerButtonValue = JoystickList.AnalogRx;
            break;
        }

        case ("XboxA"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.XboxA;
            break;
        }

        case ("XboxB"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.XboxB;
            break;
        }

        case ("XboxX"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.XboxX;
            break;
        }

        case ("XboxY"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.XboxY;
            break;
        }

        case ("XboxLB"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.L;
            break;
        }

        case ("XboxRB"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.R;
            break;
        }

        case ("XboxLT"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.L2;
            break;
        }

        case ("XboxRT"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.R2;
            break;
        }

        case ("RightStickClick"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.R3;
            break;
        }

        case ("LeftStickClick"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.L3;
            break;
        }

        case ("DPadUp"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.DpadUp;
            break;
        }

        case ("DPadDown"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.DpadDown;
            break;
        }

        case ("DPadLeft"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.DpadLeft;
            break;
        }

        case ("DPadRight"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.DpadRight;
            break;
        }

        case ("XboxStart"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.Start;
            break;
        }

        case ("XboxSelect"): {
            // Or Select. Or Share. Or The big thing in the middle of ps4 remotes. Or -.
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.Select;
            break;
        }

        default: {
            //Does not match any custom string literal must either be a Scancode or is invalid
            uint LocalScancode = (uint)OS.FindScancodeFromString(KeyName);
            if (LocalScancode != 0)
            {
                //Is a valid Scancode
                NewBind.Type = TYPE.SCANCODE;
                Scancode     = LocalScancode;
            }
            else
            {
                //If not a valid Scancode then the provided key must not be a valid key
                Console.ThrowPrint($"The supplied key '{KeyName}' is not a valid key");
                return(false);
            }
            break;
        }
        }
        //Now we have everything we need to setup the bind with Godot's input system

        //First clear any bind with the same key
        UnBind(KeyName);

        //Then add new bind
        InputMap.AddAction(KeyName);
        switch (NewBind.Type)
        {
        case (TYPE.SCANCODE): {
            InputEventKey Event = new InputEventKey {
                Scancode = Scancode
            };
            InputMap.ActionAddEvent(KeyName, Event);
            break;
        }

        case (TYPE.MOUSEBUTTON):
        case (TYPE.MOUSEWHEEL): {
            InputEventMouseButton Event = new InputEventMouseButton {
                ButtonIndex = (int)ButtonValue
            };
            InputMap.ActionAddEvent(KeyName, Event);
            break;
        }

        case (TYPE.MOUSEAXIS): {
            InputEventMouseMotion Event = new InputEventMouseMotion();
            InputMap.ActionAddEvent(KeyName, Event);
            NewBind.AxisDirection = (DIRECTION)AxisDirection;                     //Has to cast as it is Nullable
            break;
        }

        case (TYPE.CONTROLLERAXIS): {
            InputEventJoypadMotion Event = new InputEventJoypadMotion {
                Axis = (int)ControllerButtonValue
            };
            // Set which Joystick axis we're using
            switch (AxisDirection)                       // Set which direction on the axis we need to trigger the event
            {
            case (DIRECTION.UP): {
                Event.AxisValue = -1;                                 // -1, on the Vertical axis is up
                break;
            }

            case (DIRECTION.LEFT): {
                Event.AxisValue = -1;                                 // -1, on the Horizontal axis is left
                break;
            }

            case (DIRECTION.DOWN): {
                Event.AxisValue = 1;                                 // 1, on the Vertical axis is down
                break;
            }

            case (DIRECTION.RIGHT): {
                Event.AxisValue = 1;                                 // 1, on the Horizontal axis is right
                break;
            }
            }

            InputMap.ActionAddEvent(KeyName, Event);
            NewBind.AxisDirection = (DIRECTION)AxisDirection;                     //Has to cast as it is Nullable
            break;
        }

        case (TYPE.CONTROLLERBUTTON): {
            InputEventJoypadButton Event = new InputEventJoypadButton {
                ButtonIndex = (int)ControllerButtonValue
            };
            InputMap.ActionAddEvent(KeyName, Event);
            break;
        }
        }

        if (NewBind.FuncWithArg != null)
        {
            BindingsWithArg.Add(NewBind);
        }
        else if (NewBind.FuncWithoutArg != null)
        {
            BindingsWithoutArg.Add(NewBind);
        }

        return(true);
    }
Beispiel #23
0
    public static bool Bind(string actionName, string KeyName)
    {
        string FunctionName;

        actionName = actionName.ToLower();
        // TODO - support multiple commands with semicolon
        string searchname = actionName.Split(" ")[0];

        KeyName = KeyName.ToLower();

        var         kvp = _game.Commands.List.Where(e => e.Key.ToLower() == searchname).FirstOrDefault();
        CommandInfo ci  = kvp.Value;

        FunctionName = ci.FunctionName;

        BindingObject NewBind = new BindingObject(actionName, KeyName);

        bool Found = false;

        if (WithArgMethods == null)
        {
            var Methods = Assembly.GetExecutingAssembly().GetTypes()
                          .SelectMany(t => t.GetMethods())
                          .Where(m => m.GetCustomAttributes(typeof(InputWithArg), false).Length > 0);

            WithArgMethods = new List <WithArgInfo>();
            foreach (MethodInfo Method in Methods)
            {
                WithArgMethods.Add(
                    new WithArgInfo(
                        Method.Name,
                        Attribute.GetCustomAttribute(Method, typeof(InputWithArg)) as InputWithArg
                        )
                    );
            }
        }
        foreach (WithArgInfo Method in WithArgMethods)
        {
            if (Method.Name == FunctionName)
            {
                Found = true;
                NewBind.FuncWithArg = Method.Tag.Function;
                break;
            }
        }

        if (WithoutArgMethods == null)
        {
            var Methods = Assembly.GetExecutingAssembly().GetTypes()
                          .SelectMany(t => t.GetMethods())
                          .Where(m => m.GetCustomAttributes(typeof(InputWithoutArg), false).Length > 0);

            WithoutArgMethods = new List <WithoutArgInfo>();
            foreach (MethodInfo Method in Methods)
            {
                WithoutArgMethods.Add(
                    new WithoutArgInfo(
                        Method.Name,
                        Attribute.GetCustomAttribute(Method, typeof(InputWithoutArg)) as InputWithoutArg
                        )
                    );
            }
        }
        foreach (WithoutArgInfo Method in WithoutArgMethods)
        {
            if (Method.Name == FunctionName)
            {
                Found = true;
                NewBind.FuncWithoutArg = Method.Tag.Function;
                break;
            }
        }
        if (WithArgCommands == null)
        {
            var Methods = Assembly.GetExecutingAssembly().GetTypes()
                          .SelectMany(t => t.GetMethods())
                          .Where(m => m.GetCustomAttributes(typeof(CommandWithArg), false).Length > 0);

            WithArgCommands = new List <WithArgCommandInfo>();
            foreach (MethodInfo Method in Methods)
            {
                WithArgCommands.Add(
                    new WithArgCommandInfo(
                        Method.Name,
                        Attribute.GetCustomAttribute(Method, typeof(CommandWithArg)) as CommandWithArg
                        )
                    );
            }
        }
        foreach (WithArgCommandInfo Method in WithArgCommands)
        {
            if (Method.Name == FunctionName)
            {
                Found = true;
                NewBind.CommandWithArg = Method.Tag.Function;
                break;
            }
        }

        if (!Found)
        {
            Console.ThrowPrint($"The specified function '{FunctionName}' does not exist as a bindable function");
            return(false);
        }

        var  ButtonValue           = ButtonList.Left;
        var  AxisDirection         = ButtonInfo.DIRECTION.UP;
        var  ControllerButtonValue = JoystickList.Axis0;
        uint Scancode = 0;

        //Checks custom string literals first then assumes Scancode
        KeyType kt = null;

        KeyTypes.List.TryGetValue(KeyName, out kt);

        if (kt == null)
        {
            // scancodes
            uint LocalScancode = (uint)OS.FindScancodeFromString(KeyName);
            if (LocalScancode != 0)
            {
                //Is a valid Scancode
                NewBind.Type = ButtonInfo.TYPE.SCANCODE;
                Scancode     = LocalScancode;
            }
            else if (KeyName == "`")             // this fails on ubuntu 18.04 (scancode of 0 given back)
            {
                NewBind.Type = ButtonInfo.TYPE.SCANCODE;
                Scancode     = 96;
            }
            else
            {
                //If not a valid Scancode then the provided key must not be a valid key
                Console.ThrowPrint($"The supplied key '{KeyName}' is not a valid key");
                return(false);
            }
        }
        else
        {
            NewBind.Type          = kt.Type;
            ButtonValue           = kt.ButtonValue;
            AxisDirection         = kt.Direction;
            ControllerButtonValue = kt.ControllerButtonValue;
        }

        //Now we have everything we need to setup the bind with Godot's input system

        //First clear any bind with the same key
        UnBind(KeyName);

        //Then add new bind
        if (!InputMap.HasAction(actionName))
        {
            InputMap.AddAction(actionName);
        }

        switch (NewBind.Type)
        {
        case (ButtonInfo.TYPE.SCANCODE): {
            InputEventKey Event = new InputEventKey {
                Scancode = Scancode
            };
            InputMap.ActionAddEvent(actionName, Event);
            break;
        }

        case (ButtonInfo.TYPE.MOUSEBUTTON):
        case (ButtonInfo.TYPE.MOUSEWHEEL): {
            InputEventMouseButton Event = new InputEventMouseButton {
                ButtonIndex = (int)ButtonValue
            };
            InputMap.ActionAddEvent(actionName, Event);
            break;
        }

        case (ButtonInfo.TYPE.MOUSEAXIS): {
            InputEventMouseMotion Event = new InputEventMouseMotion();
            InputMap.ActionAddEvent(actionName, Event);
            NewBind.AxisDirection = (ButtonInfo.DIRECTION)AxisDirection;                     //Has to cast as it is Nullable
            break;
        }

        case (ButtonInfo.TYPE.CONTROLLERAXIS): {
            InputEventJoypadMotion Event = new InputEventJoypadMotion {
                Axis = (int)ControllerButtonValue
            };
            // Set which Joystick axis we're using
            switch (AxisDirection)                       // Set which direction on the axis we need to trigger the event
            {
            case (ButtonInfo.DIRECTION.UP): {
                Event.AxisValue = -1;                                 // -1, on the Vertical axis is up
                break;
            }

            case (ButtonInfo.DIRECTION.LEFT): {
                Event.AxisValue = -1;                                 // -1, on the Horizontal axis is left
                break;
            }

            case (ButtonInfo.DIRECTION.DOWN): {
                Event.AxisValue = 1;                                 // 1, on the Vertical axis is down
                break;
            }

            case (ButtonInfo.DIRECTION.RIGHT): {
                Event.AxisValue = 1;                                 // 1, on the Horizontal axis is right
                break;
            }
            }

            InputMap.ActionAddEvent(actionName, Event);
            NewBind.AxisDirection = (ButtonInfo.DIRECTION)AxisDirection;                     //Has to cast as it is Nullable
            break;
        }

        case (ButtonInfo.TYPE.CONTROLLERBUTTON): {
            InputEventJoypadButton Event = new InputEventJoypadButton {
                ButtonIndex = (int)ControllerButtonValue
            };
            InputMap.ActionAddEvent(actionName, Event);
            break;
        }
        }

        if (NewBind.FuncWithArg != null || NewBind.CommandWithArg != null)
        {
            BindingsWithArg.Add(NewBind);
            AddDistinctBind(BindingsWithArgDistinct, NewBind);
        }
        else if (NewBind.FuncWithoutArg != null)
        {
            BindingsWithoutArg.Add(NewBind);
            AddDistinctBind(BindingsWithoutArgDistinct, NewBind);
        }

        return(true);
    }
Beispiel #24
0
 public static void SetActionFromInput(string key, InputEventKey code)
 {
     InputMap.EraseAction(key);
     InputMap.AddAction(key);
     InputMap.ActionAddEvent(key, code);
 }
    private void PopulateNameAction()
    {
        // Initialize the dictionary
        nameAction = new Dictionary <string, InputEvent[]>();

        // TODO: Load from file if already exists

        nameAction[ActionNames.LEFT.ToString()] = new InputEvent[]
        {
            new InputEventKey()
            {
                Scancode = (int)Godot.KeyList.A
            },
            new InputEventJoypadButton()
            {
                ButtonIndex = (int)Godot.JoystickList.DpadLeft
            }
        };

        nameAction[ActionNames.RIGHT.ToString()] = new InputEvent[]
        {
            new InputEventKey()
            {
                Scancode = (int)Godot.KeyList.D
            },
            new InputEventJoypadButton()
            {
                ButtonIndex = (int)Godot.JoystickList.DpadRight
            }
        };

        nameAction[ActionNames.DOWN.ToString()] = new InputEvent[]
        {
            new InputEventKey()
            {
                Scancode = (int)Godot.KeyList.W
            },
            new InputEventJoypadButton()
            {
                ButtonIndex = (int)Godot.JoystickList.DpadUp
            }
        };

        nameAction[ActionNames.UP.ToString()] = new InputEvent[]
        {
            new InputEventKey()
            {
                Scancode = (int)Godot.KeyList.S
            },
            new InputEventJoypadButton()
            {
                ButtonIndex = (int)Godot.JoystickList.DpadDown
            }
        };

        // Add to the built-in input map itself
        foreach (string key in nameAction.Keys)
        {
            InputMap.AddAction(key);
            foreach (InputEvent ev in nameAction[key])
            {
                InputMap.ActionAddEvent(key, ev);
            }
        }
    }
Beispiel #26
0
    public static bool Bind(string KeyName, string FunctionName)
    {
        BindingObject NewBind = new BindingObject(KeyName);

        //We need to check that the function exitst and either takes no args or one float arg and get the Action
        try         //First assume it takes a float argument
        {
            Sc.ScriptState State = Scripting.ConsoleState.ContinueWithAsync($"return new Action<float>(delegate(float x) {{ {FunctionName}(x); }} );").Result;
            NewBind.FuncWithArg = State.ReturnValue as Action <float>;
        }
        catch         //Must either not exist or has different argument requirements
        {
            try       //Next we assume that it exists but without an argument
            {
                Sc.ScriptState State = Scripting.ConsoleState.ContinueWithAsync($"return new Action(delegate() {{ {FunctionName}(); }} );").Result;
                NewBind.FuncWithoutArg = State.ReturnValue as Action;
            }
            catch             //At this point we know it either does not exist or has incompatible argument requirements
            {
                Console.ThrowPrint($"The supplied function '{FunctionName}' does not exist, does not take a single float argument, or does not take zero arguments");
                return(false);
            }
        }

        Nullable <ButtonList>   ButtonValue           = null; //Making it null by default prevents a compile warning further down
        Nullable <DIRECTION>    AxisDirection         = null; //Making it null by default prevents a compile warning further down
        Nullable <JoystickList> ControllerButtonValue = null; // Making a new variable for Controller buttons because
        int Scancode = 0;

        switch (KeyName)        //Checks custom string literals first then assumes Scancode
        {
        case ("MouseOne"): {
            NewBind.Type = TYPE.MOUSEBUTTON;
            ButtonValue  = ButtonList.Left;
            break;
        }

        case ("MouseTwo"): {
            NewBind.Type = TYPE.MOUSEBUTTON;
            ButtonValue  = ButtonList.Right;
            break;
        }

        case ("MouseThree"): {
            NewBind.Type = TYPE.MOUSEBUTTON;
            ButtonValue  = ButtonList.Middle;
            break;
        }

        case ("WheelUp"): {
            NewBind.Type = TYPE.MOUSEWHEEL;
            ButtonValue  = ButtonList.WheelUp;
            break;
        }

        case ("WheelDown"): {
            NewBind.Type = TYPE.MOUSEWHEEL;
            ButtonValue  = ButtonList.WheelDown;
            break;
        }

        case ("MouseUp"): {
            NewBind.Type  = TYPE.MOUSEAXIS;
            AxisDirection = DIRECTION.UP;
            break;
        }

        case ("MouseDown"): {
            NewBind.Type  = TYPE.MOUSEAXIS;
            AxisDirection = DIRECTION.DOWN;
            break;
        }

        case ("MouseRight"): {
            NewBind.Type  = TYPE.MOUSEAXIS;
            AxisDirection = DIRECTION.RIGHT;
            break;
        }

        case ("MouseLeft"): {
            NewBind.Type  = TYPE.MOUSEAXIS;
            AxisDirection = DIRECTION.LEFT;
            break;
        }

        case ("LeftStickUp"): {
            NewBind.Type          = TYPE.CONTROLLERAXIS;
            AxisDirection         = DIRECTION.UP;
            ControllerButtonValue = JoystickList.AnalogLy;
            break;
        }

        case ("LeftStickDown"): {
            NewBind.Type          = TYPE.CONTROLLERAXIS;
            AxisDirection         = DIRECTION.DOWN;
            ControllerButtonValue = JoystickList.AnalogLy;
            break;
        }

        case ("LeftStickLeft"): {
            NewBind.Type          = TYPE.CONTROLLERAXIS;
            AxisDirection         = DIRECTION.LEFT;
            ControllerButtonValue = JoystickList.AnalogLx;
            break;
        }

        case ("LeftStickRight"): {
            NewBind.Type          = TYPE.CONTROLLERAXIS;
            AxisDirection         = DIRECTION.RIGHT;
            ControllerButtonValue = JoystickList.AnalogLx;
            break;
        }

        case ("RightStickUp"): {
            NewBind.Type          = TYPE.CONTROLLERAXIS;
            AxisDirection         = DIRECTION.UP;
            ControllerButtonValue = JoystickList.AnalogRy;
            break;
        }

        case ("RightStickDown"): {
            NewBind.Type          = TYPE.CONTROLLERAXIS;
            AxisDirection         = DIRECTION.DOWN;
            ControllerButtonValue = JoystickList.AnalogRy;
            break;
        }

        case ("RightStickLeft"): {
            NewBind.Type          = TYPE.CONTROLLERAXIS;
            AxisDirection         = DIRECTION.LEFT;
            ControllerButtonValue = JoystickList.AnalogRx;
            break;
        }

        case ("RightStickRight"): {
            NewBind.Type          = TYPE.CONTROLLERAXIS;
            AxisDirection         = DIRECTION.RIGHT;
            ControllerButtonValue = JoystickList.AnalogRx;
            break;
        }

        case ("XboxA"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.XboxA;
            break;
        }

        case ("XboxB"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.XboxB;
            break;
        }

        case ("XboxX"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.XboxX;
            break;
        }

        case ("XboxY"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.XboxY;
            break;
        }

        case ("XboxLB"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.L;
            break;
        }

        case ("XboxRB"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.R;
            break;
        }

        case ("XboxLT"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.L2;
            break;
        }

        case ("XboxRT"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.R2;
            break;
        }

        case ("RightStickClick"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.R3;
            break;
        }

        case ("LeftStickClick"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.L3;
            break;
        }

        case ("DPadUp"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.DpadUp;
            break;
        }

        case ("DPadDown"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.DpadDown;
            break;
        }

        case ("DPadLeft"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.DpadLeft;
            break;
        }

        case ("DPadRight"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.DpadRight;
            break;
        }

        case ("XboxStart"): {
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.Start;
            break;
        }

        case ("XboxSelect"): {
            // Or Select. Or Share. Or The big thing in the middle of ps4 remotes. Or -.
            NewBind.Type          = TYPE.CONTROLLERBUTTON;
            ControllerButtonValue = JoystickList.Select;
            break;
        }

        default: {
            //Does not match any custom string literal must either be a Scancode or is invalid
            int LocalScancode = OS.FindScancodeFromString(KeyName);
            if (LocalScancode != 0)
            {
                //Is a valid Scancode
                NewBind.Type = TYPE.SCANCODE;
                Scancode     = LocalScancode;
            }
            else
            {
                //If not a valid Scancode then the provided key must not be a valid key
                Console.ThrowPrint($"The supplied key '{KeyName}' is not a valid key");
                return(false);
            }
            break;
        }
        }
        //Now we have everything we need to setup the bind with Godot's input system

        //First clear any bind with the same key
        UnBind(KeyName);

        //Then add new bind
        InputMap.AddAction(KeyName);
        switch (NewBind.Type)
        {
        case (TYPE.SCANCODE): {
            InputEventKey Event = new InputEventKey();
            Event.Scancode = Scancode;
            InputMap.ActionAddEvent(KeyName, Event);
            break;
        }

        case (TYPE.MOUSEBUTTON):
        case (TYPE.MOUSEWHEEL): {
            InputEventMouseButton Event = new InputEventMouseButton();
            Event.ButtonIndex = (int)ButtonValue;
            InputMap.ActionAddEvent(KeyName, Event);
            break;
        }

        case (TYPE.MOUSEAXIS): {
            InputEventMouseMotion Event = new InputEventMouseMotion();
            InputMap.ActionAddEvent(KeyName, Event);
            NewBind.AxisDirection = (DIRECTION)AxisDirection;                     //Has to cast as it is Nullable
            break;
        }

        case (TYPE.CONTROLLERAXIS): {
            InputEventJoypadMotion Event = new InputEventJoypadMotion();
            Event.Axis = (int)ControllerButtonValue;     // Set which Joystick axis we're using
            switch (AxisDirection)                       // Set which direction on the axis we need to trigger the event
            {
            case (DIRECTION.UP): {
                Event.AxisValue = -1;                                 // -1, on the Vertical axis is up
                break;
            }

            case (DIRECTION.LEFT): {
                Event.AxisValue = -1;                                 // -1, on the Horizontal axis is left
                break;
            }

            case (DIRECTION.DOWN): {
                Event.AxisValue = 1;                                 // 1, on the Vertical axis is down
                break;
            }

            case (DIRECTION.RIGHT): {
                Event.AxisValue = 1;                                 // 1, on the Horizontal axis is right
                break;
            }
            }

            InputMap.ActionAddEvent(KeyName, Event);
            NewBind.AxisDirection = (DIRECTION)AxisDirection;                     //Has to cast as it is Nullable
            break;
        }

        case (TYPE.CONTROLLERBUTTON): {
            InputEventJoypadButton Event = new InputEventJoypadButton();
            Event.SetButtonIndex((int)ControllerButtonValue);
            InputMap.ActionAddEvent(KeyName, Event);
            break;
        }
        }

        if (NewBind.FuncWithArg != null)
        {
            BindingsWithArg.Add(NewBind);
        }
        else if (NewBind.FuncWithoutArg != null)
        {
            BindingsWithoutArg.Add(NewBind);
        }

        return(true);
    }