using Chickensoft.AutoInject; using Chickensoft.Introspection; using Godot; using NathanHoad; using SimpleInjector.Lifestyles; using System.Collections.Generic; using System.IO.Abstractions; using System.Linq; using Zennysoft.Game.Abstractions; using Zennysoft.Game.Implementation; namespace Zennysoft.Game.Ma; [Meta(typeof(IAutoNode))] public partial class InputMapper : PanelContainer { public override void _Notification(int what) => this.Notify(what); [Node] public VBoxContainer ActionList { get; set; } [Node] public KeyboardRemapButton MoveForwardKeyboard { get; set; } [Node] public JoypadRemapButton MoveForwardController { get; set; } [Node] public KeyboardRemapButton MoveLeftKeyboard { get; set; } [Node] public JoypadRemapButton MoveLeftController { get; set; } [Node] public KeyboardRemapButton MoveRightKeyboard { get; set; } [Node] public JoypadRemapButton MoveRightController { get; set; } [Node] public KeyboardRemapButton MoveBackwardKeyboard { get; set; } [Node] public JoypadRemapButton MoveBackwardController { get; set; } [Node] public KeyboardRemapButton StrafeLeftKeyboard { get; set; } [Node] public JoypadRemapButton StrafeLeftController { get; set; } [Node] public KeyboardRemapButton StrafeRightKeyboard { get; set; } [Node] public JoypadRemapButton StrafeRightController { get; set; } [Node] public KeyboardRemapButton AttackKeyboard { get; set; } [Node] public JoypadRemapButton AttackController { get; set; } [Node] public KeyboardRemapButton InteractKeyboard { get; set; } [Node] public JoypadRemapButton InteractController { get; set; } [Node] public KeyboardRemapButton InventoryKeyboard { get; set; } [Node] public JoypadRemapButton InventoryController { get; set; } [Node] public KeyboardRemapButton SortKeyboard { get; set; } [Node] public JoypadRemapButton SortController { get; set; } private Button _remappingButton = null; private InputEvent _remappingAction = null; private List _actionJoyMap = []; private List _actionKeyMap = []; private SimpleInjector.Container _container; private ISaveFileManager _saveFileManager; [Signal] public delegate void SaveControllerInputEventHandler(); public void OnReady() { _container = new SimpleInjector.Container(); _container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle(); _container.RegisterSingleton(); _container.RegisterSingleton(); _saveFileManager = _container.GetInstance(); MoveForwardController.Action = GameInputs.MoveUp; MoveForwardKeyboard.Action = GameInputs.MoveUp; MoveLeftController.Action = GameInputs.MoveLeft; MoveLeftKeyboard.Action = GameInputs.MoveLeft; MoveRightController.Action = GameInputs.MoveRight; MoveRightKeyboard.Action = GameInputs.MoveRight; MoveBackwardController.Action = GameInputs.MoveDown; MoveBackwardKeyboard.Action = GameInputs.MoveDown; StrafeLeftController.Action = GameInputs.StrafeLeft; StrafeLeftKeyboard.Action = GameInputs.StrafeLeft; StrafeRightController.Action = GameInputs.StrafeRight; StrafeRightKeyboard.Action = GameInputs.StrafeRight; AttackController.Action = GameInputs.Attack; AttackKeyboard.Action = GameInputs.Attack; InteractController.Action = GameInputs.Interact; InteractKeyboard.Action = GameInputs.Interact; InventoryController.Action = GameInputs.Inventory; InventoryKeyboard.Action = GameInputs.Inventory; SortController.Action = GameInputs.InventorySort; SortKeyboard.Action = GameInputs.InventorySort; _actionJoyMap.Add(MoveForwardController); _actionJoyMap.Add(MoveLeftController); _actionJoyMap.Add(MoveRightController); _actionJoyMap.Add(MoveBackwardController); _actionJoyMap.Add(StrafeLeftController); _actionJoyMap.Add(StrafeRightController); _actionJoyMap.Add(AttackController); _actionJoyMap.Add(InteractController); _actionJoyMap.Add(InventoryController); _actionJoyMap.Add(SortController); _actionKeyMap.Add(MoveForwardKeyboard); _actionKeyMap.Add(MoveLeftKeyboard); _actionKeyMap.Add(MoveRightKeyboard); _actionKeyMap.Add(MoveBackwardKeyboard); _actionKeyMap.Add(StrafeLeftKeyboard); _actionKeyMap.Add(StrafeRightKeyboard); _actionKeyMap.Add(AttackKeyboard); _actionKeyMap.Add(InteractKeyboard); _actionKeyMap.Add(InventoryKeyboard); _actionKeyMap.Add(SortKeyboard); MoveForwardKeyboard.Remap += OnRemap; MoveForwardController.Remap += OnRemap; MoveLeftKeyboard.Remap += OnRemap; MoveLeftController.Remap += OnRemap; MoveRightKeyboard.Remap += OnRemap; MoveRightController.Remap += OnRemap; MoveBackwardKeyboard.Remap += OnRemap; MoveBackwardController.Remap += OnRemap; StrafeLeftKeyboard.Remap += OnRemap; StrafeLeftController.Remap += OnRemap; StrafeRightKeyboard.Remap += OnRemap; StrafeRightController.Remap += OnRemap; AttackKeyboard.Remap += OnRemap; AttackController.Remap += OnRemap; InteractKeyboard.Remap += OnRemap; InteractController.Remap += OnRemap; InventoryKeyboard.Remap += OnRemap; InventoryController.Remap += OnRemap; SortKeyboard.Remap += OnRemap; SortController.Remap += OnRemap; InputHelper.JoypadInputChanged += (string action, InputEvent input) => { var buttonChanged = _actionJoyMap.SingleOrDefault(x => x.Action == action); if (buttonChanged != null) { buttonChanged.SetProcessInput(false); buttonChanged.Text = InputHelper.GetLabelForInput(input); } var allButtons = _actionKeyMap.Concat(_actionJoyMap); foreach (var button in allButtons) button.Disabled = false; }; InputHelper.KeyboardInputChanged += (string action, InputEvent input) => { var buttonChanged = _actionKeyMap.SingleOrDefault(x => x.Action == action); if (buttonChanged != null) { buttonChanged.SetProcessInput(false); buttonChanged.Text = InputHelper.GetLabelForInput(input); } var allButtons = _actionKeyMap.Concat(_actionJoyMap); foreach (var button in allButtons) button.Disabled = false; }; } public void LoadControllerInput(string jsonData) { InputHelper.DeserializeInputsForActions(jsonData); InitializeButtonText(); } public void InitializeButtonText() { MoveForwardKeyboard.Text = InputHelper.GetLabelForInput(InputHelper.GetKeyboardInputForAction(GameInputs.MoveUp)); MoveForwardController.Text = InputHelper.GetLabelForInput(InputHelper.GetJoypadInputForAction(GameInputs.MoveUp)); MoveLeftKeyboard.Text = InputHelper.GetLabelForInput(InputHelper.GetKeyboardInputForAction(GameInputs.MoveLeft)); MoveLeftController.Text = InputHelper.GetLabelForInput(InputHelper.GetJoypadInputForAction(GameInputs.MoveLeft)); MoveRightKeyboard.Text = InputHelper.GetLabelForInput(InputHelper.GetKeyboardInputForAction(GameInputs.MoveRight)); MoveRightController.Text = InputHelper.GetLabelForInput(InputHelper.GetJoypadInputForAction(GameInputs.MoveRight)); MoveBackwardKeyboard.Text = InputHelper.GetLabelForInput(InputHelper.GetKeyboardInputForAction(GameInputs.MoveDown)); MoveBackwardController.Text = InputHelper.GetLabelForInput(InputHelper.GetJoypadInputForAction(GameInputs.MoveDown)); StrafeLeftKeyboard.Text = InputHelper.GetLabelForInput(InputHelper.GetKeyboardInputForAction(GameInputs.StrafeLeft)); StrafeLeftController.Text = InputHelper.GetLabelForInput(InputHelper.GetJoypadInputForAction(GameInputs.StrafeLeft)); StrafeRightKeyboard.Text = InputHelper.GetLabelForInput(InputHelper.GetKeyboardInputForAction(GameInputs.StrafeRight)); StrafeRightController.Text = InputHelper.GetLabelForInput(InputHelper.GetJoypadInputForAction(GameInputs.StrafeRight)); AttackKeyboard.Text = InputHelper.GetLabelForInput(InputHelper.GetKeyboardInputForAction(GameInputs.Attack)); AttackController.Text = InputHelper.GetLabelForInput(InputHelper.GetJoypadInputForAction(GameInputs.Attack)); InteractKeyboard.Text = InputHelper.GetLabelForInput(InputHelper.GetKeyboardInputForAction(GameInputs.Interact)); InteractController.Text = InputHelper.GetLabelForInput(InputHelper.GetJoypadInputForAction(GameInputs.Interact)); InventoryKeyboard.Text = InputHelper.GetLabelForInput(InputHelper.GetKeyboardInputForAction(GameInputs.Inventory)); InventoryController.Text = InputHelper.GetLabelForInput(InputHelper.GetJoypadInputForAction(GameInputs.Inventory)); SortKeyboard.Text = InputHelper.GetLabelForInput(InputHelper.GetKeyboardInputForAction(GameInputs.InventorySort)); SortController.Text = InputHelper.GetLabelForInput(InputHelper.GetJoypadInputForAction(GameInputs.InventorySort)); } private void OnRemap(InputMapButton inputButton) { inputButton.Text = "..."; inputButton.SetProcessInput(true); var allButtons = _actionKeyMap.Concat(_actionJoyMap); foreach (var button in allButtons) button.Disabled = true; ReleaseFocus(); } }