Skip to content

Instantly share code, notes, and snippets.

@marpe
Created August 16, 2022 17:52
Show Gist options
  • Save marpe/b1b67b4be05bdf418a35c325edf3c088 to your computer and use it in GitHub Desktop.
Save marpe/b1b67b4be05bdf418a35c325edf3c088 to your computer and use it in GitHub Desktop.
namespace GunSmoker.Input;
public class InputBinds
{
[CVar("p1_gp_index", "Player 1 controller index")]
public static int P1ControllerIndex = 0;
[CVar("p2_gp_index", "Player 2 controller index")]
public static int P2ControllerIndex = 1;
public static Dictionary<Binds, HashSet<int>>[] MouseBinds =
{
new()
{
{ Binds.Primary, new HashSet<int> { InputState.MOUSE_LEFT } },
{ Binds.Secondary, new HashSet<int> { InputState.MOUSE_RIGHT } },
{ Binds.Action, new HashSet<int> { InputState.MOUSE_X1 } },
{ Binds.Use, new HashSet<int> { InputState.MOUSE_X2 } },
},
new()
{
}
};
public static Dictionary<Binds, HashSet<Keys>>[] KeyboardBinds =
{
new()
{
{ Binds.Left, new HashSet<Keys> { Keys.A } },
{ Binds.Up, new HashSet<Keys> { Keys.W } },
{ Binds.Right, new HashSet<Keys> { Keys.D } },
{ Binds.Down, new HashSet<Keys> { Keys.S } },
},
new()
{
{ Binds.Primary, new HashSet<Keys> { Keys.U } },
{ Binds.Secondary, new HashSet<Keys> { Keys.I } },
{ Binds.Action, new HashSet<Keys> { Keys.O } },
{ Binds.Use, new HashSet<Keys> { Keys.P } },
{ Binds.Left, new HashSet<Keys> { Keys.Left } },
{ Binds.Up, new HashSet<Keys> { Keys.Up } },
{ Binds.Right, new HashSet<Keys> { Keys.Right } },
{ Binds.Down, new HashSet<Keys> { Keys.Down } },
},
};
public static Dictionary<Binds, HashSet<Buttons>>[] GamePadBinds =
{
new()
{
{ Binds.Primary, new HashSet<Buttons> { Buttons.B } },
{ Binds.Secondary, new HashSet<Buttons> { Buttons.Y } },
{ Binds.Action, new HashSet<Buttons> { Buttons.A } },
{ Binds.Use, new HashSet<Buttons> { Buttons.X } },
},
new()
{
{ Binds.Primary, new HashSet<Buttons> { Buttons.B } },
{ Binds.Secondary, new HashSet<Buttons> { Buttons.Y } },
{ Binds.Action, new HashSet<Buttons> { Buttons.A } },
{ Binds.Use, new HashSet<Buttons> { Buttons.X } },
}
};
public static Dictionary<Binds, HashSet<Keys>>[] DefaultKeyboardBinds;
public static Dictionary<Binds, HashSet<Buttons>>[] DefaultGamePadBinds;
public static Dictionary<Binds, HashSet<int>>[] DefaultMouseBinds;
public const int NumPlayers = 2;
static InputBinds()
{
// copy the binds to defaults
DefaultKeyboardBinds = new Dictionary<Binds, HashSet<Keys>>[NumPlayers];
DefaultGamePadBinds = new Dictionary<Binds, HashSet<Buttons>>[NumPlayers];
DefaultMouseBinds = new Dictionary<Binds, HashSet<int>>[NumPlayers];
for (var i = 0; i < NumPlayers; i++)
{
DefaultKeyboardBinds[i] = new Dictionary<Binds, HashSet<Keys>>();
foreach (var (bind, keys) in KeyboardBinds[i])
{
DefaultKeyboardBinds[i].Add(bind, new HashSet<Keys>(keys));
}
DefaultGamePadBinds[i] = new Dictionary<Binds, HashSet<Buttons>>();
foreach (var (bind, buttons) in GamePadBinds[i])
{
DefaultGamePadBinds[i].Add(bind, new HashSet<Buttons>(buttons));
}
DefaultMouseBinds[i] = new Dictionary<Binds, HashSet<int>>();
foreach (var (bind, mouseButtons) in MouseBinds[i])
{
DefaultMouseBinds[i].Add(bind, new HashSet<int>(mouseButtons));
}
}
TWConsole.OnCfgSave += builder =>
{
builder.AppendLine("unbindall");
var sb = GetBindsAsText();
builder.Append(sb);
};
}
[ConsoleHandler("unbindall", "Removes all binds")]
public static void UnbindAll()
{
for (var i = 0; i < NumPlayers; i++)
{
KeyboardBinds[i].Clear();
GamePadBinds[i].Clear();
MouseBinds[i].Clear();
}
}
[ConsoleHandler("list_binds", "Prints all binds to console")]
public static void ListBinds(string filter = "")
{
var sb = GetBindsAsText();
var formattedSb = new StringBuilder();
var lines = sb.ToString().Split(Environment.NewLine);
for (var i = 0; i < lines.Length - 1; i++)
{
if (filter != "" && !lines[i].Contains(filter, StringComparison.InvariantCultureIgnoreCase))
continue;
var parts = lines[i].Split(' ');
formattedSb.AppendLine($"^3{parts[0]} ^6{parts[1]} ^8{parts[2]}");
}
GameCore.TwConsole.Print(formattedSb.ToString());
}
public static StringBuilder GetBindsAsText()
{
var sb = new StringBuilder();
for (var i = 0; i < NumPlayers; i++)
{
List<(string key, string action)> binds = new();
foreach (var (bind, keys) in KeyboardBinds[i])
{
binds.AddRange(keys.Select(key => ($"{keyboardPrefix}{key}", bind.ToString())));
}
foreach (var (bind, buttons) in GamePadBinds[i])
{
binds.AddRange(buttons.Select(button => ($"{gamePadPrefix}{button}", bind.ToString())));
}
foreach (var (bind, mouseButtons) in MouseBinds[i])
{
binds.AddRange(mouseButtons.Select(button => ($"{mousePrefix}{button + 1}", bind.ToString())));
}
foreach (var (key, action) in binds)
{
sb.AppendLine($"bind {key.ToLowerInvariant()} p{i + 1}_{action.ToLowerInvariant()}");
}
}
return sb;
}
public const string keyboardPrefix = "kb_";
public const string gamePadPrefix = "gp_";
public const string mousePrefix = "mb_";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment