private void AddAction(string Action) { if (!InputMap.HasAction(Action)) { InputMap.AddAction(Action); } }
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 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); } }
/// <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); }
/// <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); }
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); }
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); }
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); }
public static void SetActionFromInput(string key, InputEventKey code) { InputMap.EraseAction(key); InputMap.AddAction(key); InputMap.ActionAddEvent(key, code); }
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); }
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); } } }
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); } }