private void SetMotionParameterAndConsumeInput(ref float parameter, InputEventJoypadMotion inputEvent) { GetTree().SetInputAsHandled(); if (Mathf.Abs(inputEvent.AxisValue) > 0.1f) { parameter = inputEvent.AxisValue; } else { parameter = 0; } }
private void ConfigureInputJoypadMotion(InputEventJoypadMotion inputEventJoyMotion) { if (inputEventJoyMotion != null && inputEventJoyMotion.AxisValue != 0 && currentInputName != null && !keyboardMappingActive) { string inputcode = ConvertJoystickAxisInputcode(inputEventJoyMotion.Axis, inputEventJoyMotion.AxisValue); AddInput(currentInputName, inputcode); AddInput("deviceId", inputEventJoyMotion.Device); SetButtonText(currentInputName); currentInputName = null; } }
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); } } } } } }
public override void _Input(InputEvent @event) { if (@event is InputEventKey) { //Downcast to key InputEventKey eventKey = (InputEventKey)@event; //Update Motion Input if (eventKey.IsAction("move_forward")) { SetMotionParameterAndConsumeInput(ref Vinput, false, eventKey, 0); } else if (eventKey.IsAction("move_backward")) { SetMotionParameterAndConsumeInput(ref Vinput, true, eventKey, 1); } else if (eventKey.IsAction("move_left")) { SetMotionParameterAndConsumeInput(ref Hinput, false, eventKey, 2); } else if (eventKey.IsAction("move_right")) { SetMotionParameterAndConsumeInput(ref Hinput, true, eventKey, 3); } else if (eventKey.IsAction("move_up")) { SetMotionParameterAndConsumeInput(ref Linput, true, eventKey, 4); } else if (eventKey.IsAction("move_down")) { SetMotionParameterAndConsumeInput(ref Linput, false, eventKey, 5); } if (eventKey.IsAction("release_mouse") && eventKey.Pressed) { Input.SetMouseMode(Input.GetMouseMode() == Input.MouseMode.Captured ? Input.MouseMode.Visible : Input.MouseMode.Captured); } } else if (@event is InputEventJoypadMotion) { //Downcast to motion InputEventJoypadMotion eventMotion = (InputEventJoypadMotion)@event; if (eventMotion.Axis == (int)JoystickList.Axis0 || eventMotion.Axis == (int)JoystickList.Axis1) { //Update Motion Input if (!Imobile) { if (eventMotion.IsAction("move_forward")) { SetMotionParameterAndConsumeInput(ref Vinput, eventMotion); } else if (eventMotion.IsAction("move_backward")) { SetMotionParameterAndConsumeInput(ref Vinput, eventMotion); } else if (eventMotion.IsAction("move_left")) { SetMotionParameterAndConsumeInput(ref Hinput, eventMotion); } else if (eventMotion.IsAction("move_right")) { SetMotionParameterAndConsumeInput(ref Hinput, eventMotion); } else if (eventMotion.IsAction("move_up")) { SetMotionParameterAndConsumeInput(ref Linput, eventMotion); } else if (eventMotion.IsAction("move_down")) { SetMotionParameterAndConsumeInput(ref Linput, eventMotion); } } } else if (eventMotion.Axis == (int)JoystickList.Axis2) { SetMotionParameterAndConsumeInput(ref Xinput, eventMotion); } else if (eventMotion.Axis == (int)JoystickList.Axis3) { SetMotionParameterAndConsumeInput(ref Yinput, eventMotion); } } else if (@event is InputEventMouseMotion && Input.GetMouseMode() == Input.MouseMode.Captured) { //Downcast to mouse motion InputEventMouseMotion eventMouseMotion = (InputEventMouseMotion)@event; //Update camera angle MoveCamera(eventMouseMotion.Relative, SensitivityX, SensitivityY); //Apply rotation ApplyCameraAngle(); //Tell event was handled GetTree().SetInputAsHandled(); } }
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 override void _PhysicsProcess(float Delta) { if (!Game.BindsEnabled) { return; } foreach (BindingObject Binding in BindingsWithArg) { if (Binding.Type == TYPE.SCANCODE || Binding.Type == TYPE.MOUSEBUTTON || Binding.Type == TYPE.CONTROLLERBUTTON) { if (Input.IsActionJustPressed(Binding.Name)) { Binding.FuncWithArg.Invoke(1); } else if (Input.IsActionJustReleased(Binding.Name)) { Binding.FuncWithArg.Invoke(0); } } else if (Binding.Type == TYPE.MOUSEWHEEL) { if (Input.IsActionJustReleased(Binding.Name)) { Binding.FuncWithArg.Invoke(1); } } else if (Binding.Type == TYPE.CONTROLLERAXIS) { int VerticalAxis = 0; int HorizontalAxis = 0; foreach (InputEvent Option in InputMap.GetActionList(Binding.Name)) { if (Option is InputEventJoypadMotion JoyEvent) { InputEventJoypadMotion StickEvent = JoyEvent; if (StickEvent.Axis == 0 || StickEvent.Axis == 1) { // We are using Left stick VerticalAxis = 1; HorizontalAxis = 0; } else if (StickEvent.Axis == 2 || StickEvent.Axis == 3) { // We are using Right stick VerticalAxis = 3; HorizontalAxis = 2; } else { Console.ThrowPrint("This joystick doesn't seem to exist"); return; } } } if (Math.Abs(Input.GetJoyAxis(0, HorizontalAxis)) >= Game.Deadzone || Math.Abs(Input.GetJoyAxis(0, VerticalAxis)) >= Game.Deadzone) { float HorizontalMovement = Input.GetJoyAxis(0, HorizontalAxis); float VerticalMovement = Input.GetJoyAxis(0, VerticalAxis); switch (Binding.AxisDirection) { case (DIRECTION.UP): Binding.FuncWithArg.Invoke(-VerticalMovement); break; case (DIRECTION.DOWN): Binding.FuncWithArg.Invoke(VerticalMovement); break; case (DIRECTION.RIGHT): Binding.FuncWithArg.Invoke(HorizontalMovement); break; case (DIRECTION.LEFT): Binding.FuncWithArg.Invoke(-HorizontalMovement); break; } Binding.JoyWasInDeadzone = false; } else // Set sens to zero to simulate key release { if (Binding.JoyWasInDeadzone == false) // Only do this if the Binding wasn't zero last time { Binding.FuncWithArg.Invoke(0); Binding.JoyWasInDeadzone = true; } } } } foreach (BindingObject Binding in BindingsWithoutArg) { if (Binding.Type == TYPE.SCANCODE || Binding.Type == TYPE.MOUSEBUTTON || Binding.Type == TYPE.CONTROLLERBUTTON) { if (Input.IsActionJustPressed(Binding.Name)) { Binding.FuncWithoutArg.Invoke(); } } else if (Binding.Type == TYPE.MOUSEWHEEL) { if (Input.IsActionJustReleased(Binding.Name)) { Binding.FuncWithoutArg.Invoke(); } } } }
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 override void _PhysicsProcess(float Delta) { // FIXME - change this to use binds system, at least for toggle console? //UI handling where action only exists for UI interaction if (UIManager.UIOpen()) { if (Input.IsActionJustPressed("ui_accept")) { UIManager.UI_Accept(); } else if (Input.IsActionJustPressed("ui_up")) { UIManager.UI_Up(); } else if (Input.IsActionJustPressed("ui_down")) { UIManager.UI_Down(); } else if (Input.IsActionJustPressed("ui_cancel")) { UIManager.UI_Cancel(); } else if (Input.IsActionJustPressed("ui_toggleconsole")) { UIManager.UI_ConsoleToggle(); } return; } // FIXME - needs to use distinct based on binding type foreach (BindingObject Binding in BindingsWithArg) { if (Binding.CommandWithArg != null) { if (Input.IsActionJustPressed(Binding.Name)) { string arg = Binding.Name.Replace(Binding.CommandWithArg.Method.Name.ToLower(), "").Trim(); List <string> args = new List <string> { arg }; Binding.CommandWithArg.Invoke(args); } } else if (Binding.Type == ButtonInfo.TYPE.SCANCODE || Binding.Type == ButtonInfo.TYPE.MOUSEBUTTON || Binding.Type == ButtonInfo.TYPE.CONTROLLERBUTTON) { if (Input.IsActionJustPressed(Binding.Name)) { Binding.FuncWithArg.Invoke(1); } else if (Input.IsActionJustReleased(Binding.Name)) { Binding.FuncWithArg.Invoke(-1); } } else if (Binding.Type == ButtonInfo.TYPE.MOUSEWHEEL) { if (Input.IsActionJustReleased(Binding.Name)) { Binding.FuncWithArg.Invoke(1); } } else if (Binding.Type == ButtonInfo.TYPE.CONTROLLERAXIS) { int VerticalAxis = 0; int HorizontalAxis = 0; foreach (InputEvent Option in InputMap.GetActionList(Binding.Name)) { if (Option is InputEventJoypadMotion JoyEvent) { InputEventJoypadMotion StickEvent = JoyEvent; if (StickEvent.Axis == 0 || StickEvent.Axis == 1) { // We are using Left stick VerticalAxis = 1; HorizontalAxis = 0; } else if (StickEvent.Axis == 2 || StickEvent.Axis == 3) { // We are using Right stick VerticalAxis = 3; HorizontalAxis = 2; } else { Console.ThrowPrint("This joystick doesn't seem to exist"); return; } } } if (Math.Abs(Input.GetJoyAxis(0, HorizontalAxis)) >= Settings.Deadzone || Math.Abs(Input.GetJoyAxis(0, VerticalAxis)) >= Settings.Deadzone) { float HorizontalMovement = Input.GetJoyAxis(0, HorizontalAxis); float VerticalMovement = Input.GetJoyAxis(0, VerticalAxis); switch (Binding.AxisDirection) { case (ButtonInfo.DIRECTION.UP): Binding.FuncWithArg.Invoke(-VerticalMovement); break; case (ButtonInfo.DIRECTION.DOWN): Binding.FuncWithArg.Invoke(VerticalMovement); break; case (ButtonInfo.DIRECTION.RIGHT): Binding.FuncWithArg.Invoke(HorizontalMovement); break; case (ButtonInfo.DIRECTION.LEFT): Binding.FuncWithArg.Invoke(-HorizontalMovement); break; } Binding.JoyWasInDeadzone = false; } else // Set sens to zero to simulate key release { if (Binding.JoyWasInDeadzone == false) // Only do this if the Binding wasn't zero last time { Binding.FuncWithArg.Invoke(0); Binding.JoyWasInDeadzone = true; } } } } foreach (BindingObject Binding in BindingsWithoutArgDistinct) { if (Binding.Type == ButtonInfo.TYPE.SCANCODE || Binding.Type == ButtonInfo.TYPE.MOUSEBUTTON || Binding.Type == ButtonInfo.TYPE.CONTROLLERBUTTON) { if (Input.IsActionJustPressed(Binding.Name)) { Binding.FuncWithoutArg.Invoke(); } } else if (Binding.Type == ButtonInfo.TYPE.MOUSEWHEEL) { if (Input.IsActionJustReleased(Binding.Name)) { Binding.FuncWithoutArg.Invoke(); } } } }
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); }