Input monitor.

This commit is contained in:
Pieter-Jan Briers
2019-11-17 17:25:52 +01:00
parent d3e7e12adf
commit 07b0548532
6 changed files with 110 additions and 36 deletions

View File

@@ -103,38 +103,48 @@ namespace Robust.Client.Console.Commands
internal class ToggleMonitorCommand : IConsoleCommand
{
public string Command => "monitor";
public string Help => "Usage: monitor <name>\nPossible monitors are: fps, net, coord, time, frames, mem";
public string Help =>
"Usage: monitor <name>\nPossible monitors are: fps, net, coord, time, frames, mem, clyde, input";
public string Description => "Toggles a debug monitor in the F3 menu.";
public bool Execute(IDebugConsole console, params string[] args)
{
var monitor = IoCManager.Resolve<IUserInterfaceManager>().DebugMonitors;
if (args.Length != 1)
{
throw new InvalidOperationException("Must have exactly 1 argument.");
console.AddLine($"Must provide exactly 1 argument!");
return false;
}
var monitor = IoCManager.Resolve<IUserInterfaceManager>().DebugMonitors;
switch (args[0])
{
case "fps":
monitor.ShowFPS = !monitor.ShowFPS;
monitor.ShowFPS ^= true;
break;
case "net":
monitor.ShowNet = !monitor.ShowNet;
monitor.ShowNet ^= true;
break;
case "coord":
monitor.ShowCoords = !monitor.ShowCoords;
monitor.ShowCoords ^= true;
break;
case "time":
monitor.ShowTime = !monitor.ShowTime;
monitor.ShowTime ^= true;
break;
case "frames":
monitor.ShowFrameGraph = !monitor.ShowFrameGraph;
monitor.ShowFrameGraph ^= true;
break;
case "mem":
monitor.ShowMemory ^= true;
break;
case "clyde":
monitor.ShowClyde ^= true;
break;
case "input":
monitor.ShowInput ^= true;
break;
default:
console.AddLine($"Invalid key: {args[0]}");
break;

View File

@@ -30,7 +30,9 @@ namespace Robust.Client.Input
[Dependency] private readonly IReflectionManager _reflectionManager;
#pragma warning restore 649
private readonly Dictionary<BoundKeyFunction, InputCmdHandler> _commands = new Dictionary<BoundKeyFunction, InputCmdHandler>();
private readonly Dictionary<BoundKeyFunction, InputCmdHandler> _commands =
new Dictionary<BoundKeyFunction, InputCmdHandler>();
private readonly List<KeyBinding> _bindings = new List<KeyBinding>();
private readonly bool[] _keysPressed = new bool[256];
@@ -46,6 +48,11 @@ namespace Robust.Client.Input
/// <inheritdoc />
public event Action<BoundKeyEventArgs> KeyBindStateChanged;
public IEnumerable<BoundKeyFunction> DownKeyFunctions => _bindings
.Where(x => x.State == BoundKeyState.Down)
.Select(x => x.Function)
.ToList();
/// <inheritdoc />
public void Initialize()
{
@@ -76,6 +83,7 @@ namespace Robust.Client.Input
{
continue;
}
SetBindState(bind, BoundKeyState.Up);
}
}
@@ -97,7 +105,7 @@ namespace Robust.Client.Input
foreach (var binding in _bindings)
{
// check if our binding is even in the active context
if(!Contexts.ActiveContext.FunctionExistsHierarchy(binding.Function))
if (!Contexts.ActiveContext.FunctionExistsHierarchy(binding.Function))
continue;
if (PackedMatchesPressedState(binding.PackedKeyCombo))
@@ -128,6 +136,7 @@ namespace Robust.Client.Input
{
return;
}
var internalKey = KeyToInternal(args.Key);
foreach (var binding in _bindings)
{
@@ -135,7 +144,8 @@ namespace Robust.Client.Input
if (!Contexts.ActiveContext.FunctionExistsHierarchy(binding.Function))
continue;
if (PackedContainsKey(binding.PackedKeyCombo, internalKey) && PackedMatchesPressedState(binding.PackedKeyCombo))
if (PackedContainsKey(binding.PackedKeyCombo, internalKey) &&
PackedMatchesPressedState(binding.PackedKeyCombo))
{
UpBind(binding);
}
@@ -161,6 +171,7 @@ namespace Robust.Client.Input
{
return SetBindState(binding, BoundKeyState.Down);
}
return false;
}
@@ -178,7 +189,8 @@ namespace Robust.Client.Input
{
binding.State = state;
var eventArgs = new BoundKeyEventArgs(binding.Function, binding.State, new ScreenCoordinates(MouseScreenPosition), binding.CanFocus);
var eventArgs = new BoundKeyEventArgs(binding.Function, binding.State,
new ScreenCoordinates(MouseScreenPosition), binding.CanFocus);
UIKeyBindStateChanged?.Invoke(eventArgs);
if (state == BoundKeyState.Up || !eventArgs.Handled)
@@ -195,21 +207,22 @@ namespace Robust.Client.Input
{
cmd?.Enabled(null);
}
return (eventArgs.Handled);
}
private bool PackedMatchesPressedState(int packedKeyCombo)
{
var key = (byte)(packedKeyCombo & 0x000000FF);
var key = (byte) (packedKeyCombo & 0x000000FF);
if (!_keysPressed[key]) return false;
key = (byte)((packedKeyCombo & 0x0000FF00) >> 8);
key = (byte) ((packedKeyCombo & 0x0000FF00) >> 8);
if (key != 0x00 && !_keysPressed[key]) return false;
key = (byte)((packedKeyCombo & 0x00FF0000) >> 16);
key = (byte) ((packedKeyCombo & 0x00FF0000) >> 16);
if (key != 0x00 && !_keysPressed[key]) return false;
key = (byte)((packedKeyCombo & 0xFF000000) >> 24);
key = (byte) ((packedKeyCombo & 0xFF000000) >> 24);
if (key != 0x00 && !_keysPressed[key]) return false;
return true;
@@ -217,16 +230,16 @@ namespace Robust.Client.Input
private static bool PackedContainsKey(int packedKeyCombo, byte key)
{
var cKey = (byte)(packedKeyCombo & 0x000000FF);
var cKey = (byte) (packedKeyCombo & 0x000000FF);
if (cKey == key) return true;
cKey = (byte)((packedKeyCombo & 0x0000FF00) >> 8);
cKey = (byte) ((packedKeyCombo & 0x0000FF00) >> 8);
if (cKey != 0x00 && cKey == key) return true;
cKey = (byte)((packedKeyCombo & 0x00FF0000) >> 16);
cKey = (byte) ((packedKeyCombo & 0x00FF0000) >> 16);
if (cKey != 0x00 && cKey == key) return true;
cKey = (byte)((packedKeyCombo & 0xFF000000) >> 24);
cKey = (byte) ((packedKeyCombo & 0xFF000000) >> 24);
if (cKey != 0x00 && cKey == key) return true;
return false;
@@ -236,18 +249,19 @@ namespace Robust.Client.Input
{
for (var i = 0; i < 32; i += 8)
{
var key = (byte)(subPackedCombo >> i);
var key = (byte) (subPackedCombo >> i);
if (!PackedContainsKey(packedCombo, key))
{
return false;
}
}
return true;
}
private static byte KeyToInternal(Keyboard.Key key)
{
return (byte)key;
return (byte) key;
}
private void LoadKeyFile(ResourcePath yamlFile)
@@ -262,7 +276,7 @@ namespace Robust.Client.Input
document = yamlStream.Documents[0];
}
var mapping = (YamlMappingNode)document.RootNode;
var mapping = (YamlMappingNode) document.RootNode;
foreach (var keyMapping in mapping.GetNode<YamlSequenceNode>("binds").Cast<YamlMappingNode>())
{
var function = keyMapping.GetNode("function").AsString();
@@ -271,6 +285,7 @@ namespace Robust.Client.Input
Logger.ErrorS("input", "Key function in {0} does not exist: '{1}'", yamlFile, function);
continue;
}
var key = keyMapping.GetNode("key").AsEnum<Keyboard.Key>();
var canFocus = false;
@@ -312,7 +327,8 @@ namespace Robust.Client.Input
public void AddClickBind()
{
RegisterBinding(new KeyBinding(EngineKeyFunctions.Use, KeyBindingType.State, Keyboard.Key.MouseLeft, true, false));
RegisterBinding(new KeyBinding(EngineKeyFunctions.Use, KeyBindingType.State, Keyboard.Key.MouseLeft, true,
false));
}
private void RegisterBinding(KeyBinding binding)
@@ -332,6 +348,7 @@ namespace Robust.Client.Input
{
return binding;
}
throw new KeyNotFoundException($"No keys are bound for function '{function}'");
}
@@ -377,12 +394,12 @@ namespace Robust.Client.Input
public bool CanRepeat { get; internal set; }
public KeyBinding(BoundKeyFunction function,
KeyBindingType bindingType,
Keyboard.Key baseKey,
bool canFocus, bool canRepeat,
Keyboard.Key mod1 = Keyboard.Key.Unknown,
Keyboard.Key mod2 = Keyboard.Key.Unknown,
Keyboard.Key mod3 = Keyboard.Key.Unknown)
KeyBindingType bindingType,
Keyboard.Key baseKey,
bool canFocus, bool canRepeat,
Keyboard.Key mod1 = Keyboard.Key.Unknown,
Keyboard.Key mod2 = Keyboard.Key.Unknown,
Keyboard.Key mod3 = Keyboard.Key.Unknown)
{
Function = function;
BindingType = bindingType;
@@ -393,9 +410,9 @@ namespace Robust.Client.Input
}
private static int PackKeyCombo(Keyboard.Key baseKey,
Keyboard.Key mod1 = Keyboard.Key.Unknown,
Keyboard.Key mod2 = Keyboard.Key.Unknown,
Keyboard.Key mod3 = Keyboard.Key.Unknown)
Keyboard.Key mod1 = Keyboard.Key.Unknown,
Keyboard.Key mod2 = Keyboard.Key.Unknown,
Keyboard.Key mod3 = Keyboard.Key.Unknown)
{
if (baseKey == Keyboard.Key.Unknown)
throw new ArgumentOutOfRangeException(nameof(baseKey), baseKey, "Cannot bind Unknown key.");
@@ -424,7 +441,6 @@ namespace Robust.Client.Input
return combo;
}
}
}
public enum KeyBindingType

View File

@@ -1,6 +1,7 @@
using Robust.Shared;
using Robust.Shared.IoC;
using System;
using System.Collections.Generic;
using Robust.Client.Input;
using Robust.Shared.Maths;
using Robust.Shared.Input;
@@ -56,5 +57,7 @@ namespace Robust.Client.Interfaces.Input
/// If UIKeyBindStateChanged did not handle the BoundKeyEvent, KeyBindStateChanged is called.
/// </summary>
event Action<BoundKeyEventArgs> KeyBindStateChanged;
IEnumerable<BoundKeyFunction> DownKeyFunctions { get; }
}
}

View File

@@ -10,5 +10,6 @@ namespace Robust.Client.Interfaces.UserInterface
bool ShowFrameGraph { get; set; }
bool ShowMemory { get; set; }
bool ShowClyde { get; set; }
bool ShowInput { get; set; }
}
}

View File

@@ -0,0 +1,37 @@
using Robust.Client.Graphics.Drawing;
using Robust.Client.Interfaces.Input;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Timing;
namespace Robust.Client.UserInterface.CustomControls
{
internal class DebugInputPanel : PanelContainer
{
[Dependency] private readonly IInputManager _inputManager;
private readonly Label _label;
public DebugInputPanel()
{
IoCManager.InjectDependencies(this);
PanelOverride = new StyleBoxFlat
{
BackgroundColor = new Color(67, 105, 255, 138),
};
PanelOverride.SetContentMarginOverride(StyleBox.Margin.All, 5);
AddChild(_label = new Label());
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
_label.Text = string.Join("\n", _inputManager.DownKeyFunctions);
}
}
}

View File

@@ -19,7 +19,8 @@ namespace Robust.Client.UserInterface.CustomControls
public bool ShowTime { get => _timeDebug.Visible; set => _timeDebug.Visible = value; }
public bool ShowFrameGraph { get => _frameGraph.Visible; set => _frameGraph.Visible = value; }
public bool ShowMemory { get => _debugMemoryPanel.Visible; set => _debugMemoryPanel.Visible = value; }
public bool ShowClyde { get => _debugMemoryPanel.Visible; set => _debugMemoryPanel.Visible = value; }
public bool ShowClyde { get => _debugClydePanel.Visible; set => _debugClydePanel.Visible = value; }
public bool ShowInput { get => _debugInputPanel.Visible; set => _debugInputPanel.Visible = value; }
private readonly FpsCounter _fpsCounter;
private readonly DebugCoordsPanel _debugCoordsPanel;
@@ -28,6 +29,7 @@ namespace Robust.Client.UserInterface.CustomControls
private readonly FrameGraph _frameGraph;
private readonly DebugMemoryPanel _debugMemoryPanel;
private readonly DebugClydePanel _debugClydePanel;
private readonly DebugInputPanel _debugInputPanel;
//TODO: Think about a factory for this
public DebugMonitors(IGameTiming gameTiming, IPlayerManager playerManager, IEyeManager eyeManager, IInputManager inputManager, IStateManager stateManager, IClyde displayManager, IClientNetManager netManager, IMapManager mapManager)
@@ -66,6 +68,11 @@ namespace Robust.Client.UserInterface.CustomControls
{
SizeFlagsHorizontal = SizeFlags.None
});
AddChild(_debugInputPanel = new DebugInputPanel
{
SizeFlagsHorizontal = SizeFlags.None
});
}
}
}