Skip to content

Commit

Permalink
Basic configurable keymap
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczy93 committed May 23, 2024
1 parent 7e4c41b commit 1aed1f6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
40 changes: 33 additions & 7 deletions CentrED/Keymap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ public static class Keymap

public static readonly Keys[] NotAssigned = Array.Empty<Keys>();

public const string MoveUp = "Move Up";
public const string MoveDown = "Move Down";
public const string MoveLeft = "Move Left";
public const string MoveRight = "Move Right";
public const string ToggleAnimatedStatics = "Toggle Animated Statics";
public const string MoveUp = "move_up";
public const string MoveDown = "move_down";
public const string MoveLeft = "move_left";
public const string MoveRight = "move_right";
public const string ToggleAnimatedStatics = "toggle_animated_statics";


public static void Update(KeyboardState newState)
{
previousState = currentState;
currentState = newState;
}

public static bool IsKeyDown(string action)
{
var assignedKeys = GetKeys(action);
Expand Down Expand Up @@ -52,6 +52,16 @@ public static (Keys[], Keys[]) GetKeys(string action)
return Config.Instance.Keymap[action];
}

public static string GetShortcut(string action)
{
return string.Join('+', GetKeys(action).Item1);
}

public static string PrettyName(string action)
{
return string.Join(' ', action.Split('_').Select(s => char.ToUpper(s[0]) + s[1..]));
}

public static Keys[] GetKeysPressed()
{
return currentState.GetPressedKeys();
Expand Down Expand Up @@ -92,5 +102,21 @@ private static (Keys[],Keys[]) GetDefault(string action)
ToggleAnimatedStatics => ([Keys.LeftControl, Keys.A], NotAssigned),
_ => (NotAssigned, NotAssigned)
};
}
}

public class LetterLastComparer : IComparer<Keys>
{
public int Compare(Keys k1, Keys k2)
{
if (k1 is >= Keys.A and <= Keys.Z or >= Keys.D0 and <= Keys.D9)
{
return 1;
}
if (k2 is >= Keys.A and <= Keys.Z or >= Keys.D0 and <= Keys.D9)
{
return -1;
}
return 0;
}
}
}
1 change: 0 additions & 1 deletion CentrED/Map/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,6 @@ public void Update(GameTime gameTime, bool isActive, bool processMouse, bool pro
}
else
{

if (keyState.IsKeyDown(Keys.LeftControl) || keyState.IsKeyDown(Keys.RightControl))
{
if (IsKeyPressed(keyState, Keys.Z))
Expand Down
2 changes: 1 addition & 1 deletion CentrED/UI/UIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ private void DrawMainMenu()
}
ImGui.EndMenu();
}
ImGui.MenuItem("Animated Statics", "Ctrl + A", ref CEDGame.MapManager.AnimatedStatics);
ImGui.MenuItem("Animated Statics", Keymap.GetShortcut(Keymap.ToggleAnimatedStatics), ref CEDGame.MapManager.AnimatedStatics);
ImGui.MenuItem("Show Grid", "Ctrl + G", ref CEDGame.MapManager.ShowGrid);
ImGui.EndMenu();
}
Expand Down
2 changes: 2 additions & 0 deletions CentrED/UI/Windows/LargeScaleOperationsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class LSOWindow : Window

protected override void InternalDraw()
{
ImGui.Text("Work in progress :)");
return;
if (!CEDClient.Initialized)
{
ImGui.Text("Not connected");
Expand Down
19 changes: 7 additions & 12 deletions CentrED/UI/Windows/OptionsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,11 @@ private void DrawKeymapOptions()


private bool _showNewKeyPopup;
private List<Keys> _tempNewKey = [];

private void DrawSingleKey(string action)
{
var keys = Keymap.GetKeys(action);
ImGui.Text(action);
ImGui.Text(Keymap.PrettyName(action));
ImGui.SameLine();
if (assigningActionName != "")
{
Expand Down Expand Up @@ -161,18 +160,14 @@ private void DrawSingleKey(string action)
}
if (pressedKey is >= Keys.A and <= Keys.Z)
{
_tempNewKey = pressedKeys.ToList();
var sortedKeys = pressedKeys.Order(new Keymap.LetterLastComparer()).ToArray();
var oldKeys = Config.Instance.Keymap[action];
var newKeys = assignedKeyNumber == 1 ? (sortedKeys, oldKeys.Item2) : (oldKeys.Item1, sortedKeys);
Config.Instance.Keymap[action] = newKeys;
assigningActionName = "";
assignedKeyNumber = 0;
}
}
if (_tempNewKey.Count > 0 /*&& Keymap.AnyKeyReleased() != Keys.None*/)
{
var oldKeys = Config.Instance.Keymap[action];
var newKeys = assignedKeyNumber == 1 ? (pressedKeys, oldKeys.Item2) : (oldKeys.Item1, pressedKeys);
Config.Instance.Keymap[action] = newKeys;
assigningActionName = "";
assignedKeyNumber = 0;
_tempNewKey.Clear();
}
if (assigningActionName == "")
{
ImGui.CloseCurrentPopup();
Expand Down

0 comments on commit 1aed1f6

Please sign in to comment.