Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/keymap-config'
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczy93 committed May 23, 2024
2 parents a9a89bb + 187eb91 commit 7e4c41b
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 52 deletions.
11 changes: 9 additions & 2 deletions CentrED/Config.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.Json;
using CentrED.IO.Models;
using Microsoft.Xna.Framework.Input;

namespace CentrED;

Expand All @@ -11,6 +12,7 @@ public class ConfigRoot
public bool LegacyMouseScroll;
public string GraphicsDriver = "D3D11";
public Dictionary<string, WindowState> Layout = new();
public Dictionary<string, (Keys[], Keys[])> Keymap = new();
}

public static class Config
Expand Down Expand Up @@ -43,8 +45,13 @@ public static void AutoSave()
{
if (DateTime.Now > LastConfigSave + ConfigSaveRate)
{
File.WriteAllText(_configFilePath, JsonSerializer.Serialize(Instance, SerializerOptions));
LastConfigSave = DateTime.Now;
Save();
}
}

public static void Save()
{
File.WriteAllText(_configFilePath, JsonSerializer.Serialize(Instance, SerializerOptions));
LastConfigSave = DateTime.Now;
}
}
96 changes: 96 additions & 0 deletions CentrED/Keymap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using Microsoft.Xna.Framework.Input;

namespace CentrED;

public static class Keymap
{
private static KeyboardState currentState;
private static KeyboardState previousState;

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 static void Update(KeyboardState newState)
{
previousState = currentState;
currentState = newState;
}

public static bool IsKeyDown(string action)
{
var assignedKeys = GetKeys(action);
return assignedKeys.Item1.All(currentState.IsKeyDown) || assignedKeys.Item2.All(currentState.IsKeyDown);
}

public static bool IsKeyUp(Keys key)
{
return currentState.IsKeyUp(key);
}

public static bool IsKeyUp(string action)
{
var assignedKeys = GetKeys(action);
return assignedKeys.Item1.All(currentState.IsKeyUp) || assignedKeys.Item2.All(currentState.IsKeyUp);
}

public static bool IsKeyPressed(string action)
{
var assignedKeys = GetKeys(action);
return (assignedKeys.Item1.All(currentState.IsKeyDown) && assignedKeys.Item1.Any(previousState.IsKeyUp)) ||
(assignedKeys.Item2.All(currentState.IsKeyDown) && assignedKeys.Item2.Any(previousState.IsKeyUp));
}

public static (Keys[], Keys[]) GetKeys(string action)
{
InitAction(action);
return Config.Instance.Keymap[action];
}

public static Keys[] GetKeysPressed()
{
return currentState.GetPressedKeys();
}

public static Keys AnyKeyPressed()
{
return currentState.GetPressedKeys().Except(previousState.GetPressedKeys()).FirstOrDefault();
}

public static Keys AnyKeyReleased()
{
return previousState.GetPressedKeys().Except(currentState.GetPressedKeys()).FirstOrDefault();
}

private static void InitAction(string action)
{
if (Config.Instance.Keymap.ContainsKey(action))
{
return;
}
var defaultKey = GetDefault(action);
if (defaultKey != (NotAssigned, NotAssigned))
{
Config.Instance.Keymap[action] = defaultKey;
}
Config.Save();
}

private static (Keys[],Keys[]) GetDefault(string action)
{
return action switch
{
MoveUp => ([Keys.W], [Keys.Up]),
MoveDown => ([Keys.S], [Keys.Down]),
MoveLeft => ([Keys.A], [Keys.Left]),
MoveRight => ([Keys.D], [Keys.Right]),
ToggleAnimatedStatics => ([Keys.LeftControl, Keys.A], NotAssigned),
_ => (NotAssigned, NotAssigned)
};
}
}
99 changes: 52 additions & 47 deletions CentrED/Map/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ public void Update(GameTime gameTime, bool isActive, bool processMouse, bool pro
return;
Metrics.Start("UpdateMap");
var mouseState = Mouse.GetState();
Keymap.Update(Keyboard.GetState());
var keyState = Keyboard.GetState();
if (isActive && processMouse)
{
Expand Down Expand Up @@ -595,60 +596,64 @@ public void Update(GameTime gameTime, bool isActive, bool processMouse, bool pro
}
}
}
if (keyState.IsKeyDown(Keys.LeftControl) || keyState.IsKeyDown(Keys.RightControl))
if (Keymap.IsKeyPressed(Keymap.ToggleAnimatedStatics))
{
if(IsKeyPressed(keyState, Keys.Z))
{
Client.Undo();
}
if(IsKeyPressed(keyState, Keys.R))
{
Reset();
}
if(IsKeyPressed(keyState, Keys.W))
{
WalkableSurfaces = !WalkableSurfaces;
}
if(IsKeyPressed(keyState, Keys.F))
{
FlatView = !FlatView;
UpdateAllTiles();
}
if(IsKeyPressed(keyState, Keys.S))
{
FlatStatics = !FlatStatics;
UpdateAllTiles();
}
if (IsKeyPressed(keyState, Keys.A))
{
AnimatedStatics = !AnimatedStatics;
}
if (IsKeyPressed(keyState, Keys.G))
{
ShowGrid = !ShowGrid;
}
AnimatedStatics = !AnimatedStatics;
}
else
{
if(IsKeyPressed(keyState, Keys.Escape))
{
Camera.ResetZoom();
}
if(keyState.IsKeyDown(Keys.A))
{
Camera.Move(-delta, delta);
}
if(keyState.IsKeyDown(Keys.D))
{
Camera.Move(delta, -delta);
}
if(keyState.IsKeyDown(Keys.W))

if (keyState.IsKeyDown(Keys.LeftControl) || keyState.IsKeyDown(Keys.RightControl))
{
Camera.Move(-delta, -delta);
if (IsKeyPressed(keyState, Keys.Z))
{
Client.Undo();
}
if (IsKeyPressed(keyState, Keys.R))
{
Reset();
}
if (IsKeyPressed(keyState, Keys.W))
{
WalkableSurfaces = !WalkableSurfaces;
}
if (IsKeyPressed(keyState, Keys.F))
{
FlatView = !FlatView;
UpdateAllTiles();
}
if (IsKeyPressed(keyState, Keys.S))
{
FlatStatics = !FlatStatics;
UpdateAllTiles();
}
if (IsKeyPressed(keyState, Keys.G))
{
ShowGrid = !ShowGrid;
}
}
if(keyState.IsKeyDown(Keys.S))
else
{
Camera.Move(delta, delta);
if (IsKeyPressed(keyState, Keys.Escape))
{
Camera.ResetZoom();
}
if(Keymap.IsKeyDown(Keymap.MoveLeft))
{
Camera.Move(-delta, delta);
}
if(Keymap.IsKeyDown(Keymap.MoveRight))
{
Camera.Move(delta, -delta);
}
if(Keymap.IsKeyDown(Keymap.MoveUp))
{
Camera.Move(-delta, -delta);
}
if(Keymap.IsKeyDown(Keymap.MoveDown))
{
Camera.Move(delta, delta);
}
}
}
_prevKeyState = keyState;
Expand Down
101 changes: 98 additions & 3 deletions CentrED/UI/Windows/OptionsWindow.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Numerics;
using ImGuiNET;
using ImGuiNET;
using Microsoft.Xna.Framework.Input;
using static CentrED.Application;
using Vector4 = System.Numerics.Vector4;

namespace CentrED.UI.Windows;

public class OptionsWindow : Window
{
public override string Name => "Options";
public override ImGuiWindowFlags WindowFlags => ImGuiWindowFlags.AlwaysAutoResize | ImGuiWindowFlags.NoResize;

private int _lightLevel = 30;
private Vector4 _virtualLayerFillColor = new(0.2f, 0.2f, 0.2f, 0.1f);
private Vector4 _virtualLayerBorderColor = new(1.0f, 1.0f, 1.0f, 1.0f);
Expand All @@ -34,6 +35,7 @@ protected override void InternalDraw()
UIManager.Tooltip("Mouse scroll up/down: elevate tile\nCtrl + Mouse scroll up/down: Zoom in/out");
ImGui.EndTabItem();
}
DrawKeymapOptions();
if (ImGui.BeginTabItem("Virtual Layer"))
{
if (ImGui.ColorPicker4("Virtual Layer Fill Color", ref _virtualLayerFillColor))
Expand Down Expand Up @@ -85,4 +87,97 @@ protected override void InternalDraw()
ImGui.EndTabBar();
}
}

private string assigningActionName = "";
private byte assignedKeyNumber = 0;

private void DrawKeymapOptions()
{
if (ImGui.BeginTabItem("Keymap"))
{
DrawSingleKey(Keymap.MoveUp);
DrawSingleKey(Keymap.MoveDown);
DrawSingleKey(Keymap.MoveLeft);
DrawSingleKey(Keymap.MoveRight);
ImGui.Separator();
DrawSingleKey(Keymap.ToggleAnimatedStatics);
}
}


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

private void DrawSingleKey(string action)
{
var keys = Keymap.GetKeys(action);
ImGui.Text(action);
ImGui.SameLine();
if (assigningActionName != "")
{
ImGui.BeginDisabled();
}
var label1 = (assigningActionName == action && assignedKeyNumber == 1) ?
"Assign new key" :
string.Join(" + ", keys.Item1.Select(x => x.ToString()));
if (ImGui.Button($"{label1}##{action}1"))
{
assigningActionName = action;
assignedKeyNumber = 1;
ImGui.OpenPopup("NewKey");
_showNewKeyPopup = true;
}
ImGui.SameLine();
var label2 = (assigningActionName == action && assignedKeyNumber == 2) ?
"Assign new key" :
string.Join(" + ", keys.Item2.Select(x => x.ToString()));
if (ImGui.Button($"{label2}##{action}2"))
{
assigningActionName = action;
assignedKeyNumber = 2;
ImGui.OpenPopup("NewKey");
_showNewKeyPopup = true;
}
if (assigningActionName != "")
{
ImGui.EndDisabled();
}
if (assigningActionName == action && ImGui.BeginPopupModal
("NewKey", ref _showNewKeyPopup, ImGuiWindowFlags.AlwaysAutoResize | ImGuiWindowFlags.NoTitleBar))
{
var pressedKeys = Keymap.GetKeysPressed();
ImGui.Text($"Enter new key for {assigningActionName}");
ImGui.Text(string.Join("+", pressedKeys));
ImGui.Text("Press ESCAPE to cancel");


foreach (var pressedKey in pressedKeys)
{
if (pressedKey == Keys.Escape)
{
assigningActionName = "";
assignedKeyNumber = 0;
break;
}
if (pressedKey is >= Keys.A and <= Keys.Z)
{
_tempNewKey = pressedKeys.ToList();
}
}
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();
}
ImGui.EndPopup();
}
}
}

0 comments on commit 7e4c41b

Please sign in to comment.