You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

193 lines
9.1 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using gaemstone.ECS;
using ImGuiNET;
using Silk.NET.GLFW;
using Silk.NET.Input;
using static gaemstone.Client.Components.InputComponents;
using static gaemstone.Client.Systems.ImGuiManager;
namespace gaemstone.Client.Systems;
[Module]
[DependsOn<gaemstone.Client.Components.InputComponents>]
[DependsOn<gaemstone.Client.Systems.ImGuiManager>]
public class ImGuiInputDebug
{
[System]
public static void ShowInputDebugWindow(Universe universe, ImGuiData _)
{
var input = universe.Lookup<Input>();
if (input == null) return;
ImGui.Begin("Input Information", ImGuiWindowFlags.NoResize);
if (universe.Lookup<Keyboard>() is EntityRef keyboard)
DrawKeyboard(keyboard);
if (universe.Lookup<Mouse>() is EntityRef mouse) {
ImGui.BeginChild("Mouse Info", new(160, 180), true);
ImGui.Text("Position: " + (Vector2?)mouse.Lookup("Position")?.MaybeGet<RawValue2D>());
ImGui.Text("Delta: " + (Vector2?)mouse.Lookup("Delta" )?.MaybeGet<RawValue2D>());
ImGui.Text("Wheel: " + (float?) mouse.Lookup("Wheel" )?.MaybeGet<RawValue1D>());
ImGui.Spacing();
var buttons = mouse.Lookup("Buttons")?.GetChildren().ToArray() ?? Array.Empty<EntityRef>();
ImGui.Text("Buttons: " + string.Join(" ", buttons
.Where (button => button.Has<Active>())
.Select(button => $"{button.Name} ({button.Get<Active>().Duration.TotalSeconds:f2}s)")));
ImGui.Text(" Pressed: " + string.Join(" ", buttons
.Where (button => button.Has<Activated>())
.Select(button => button.Name)));
ImGui.Text(" Released: " + string.Join(" ", buttons
.Where (button => button.Has<Deactivated>())
.Select(button => button.Name)));
ImGui.EndChild();
}
for (var index = 0; input.Lookup("Gamepad" + index) is EntityRef gamepad; index++) {
ImGui.SameLine();
ImGui.BeginChild($"{gamepad.Name} Info", new(160, 180), true);
var buttons = gamepad.Lookup("Buttons")?.GetChildren().ToArray() ?? Array.Empty<EntityRef>();
ImGui.Text("Buttons: " + string.Join(" ", buttons.Where(b => b.Has<Active>())
.Select(b => $"{b.Name} ({b.Get<Active>().Duration.TotalSeconds:f2}s)")));
ImGui.Text(" Pressed: " + string.Join(" ", buttons.Where(b => b.Has<Activated>()).Select(b => b.Name)));
ImGui.Text(" Released: " + string.Join(" ", buttons.Where(b => b.Has<Deactivated>()).Select(b => b.Name)));
ImGui.Spacing();
ImGui.Text("Triggers:");
for (var i = 0; gamepad.Lookup("Trigger" + i) is EntityRef trigger; i++) {
var text = $" {i}: {(float?)trigger.MaybeGet<RawValue1D>() ?? default:f2}";
if (trigger.Has<Activated>()) text += " pressed!";
else if (trigger.Has<Deactivated>()) text += " released!";
else if (trigger.MaybeGet<Active>() is Active active)
text += $" ({active.Duration.TotalSeconds:f2}s)";
ImGui.Text(text);
}
ImGui.Text("Thumbsticks:");
for (var i = 0; gamepad.Lookup("Thumbstick" + i) is EntityRef thumbstick; i++)
ImGui.Text($" {i}: {(Vector2?)thumbstick.MaybeGet<RawValue2D>() ?? default:f2}");
ImGui.EndChild();
}
ImGui.End();
}
private const float U = 1.00F;
private const float SM = 1.25F;
// Spacing (invisible)
private const float I = -0.50F;
private const float _ = -1.00F;
private const float ER = -1.25F;
// Special
private const float T = -11.00F;
private const float ENT = -11.50F;
private static readonly float[][] KeyboardLayout = {
new[] { U, _, U, U, U, U,I,U, U, U, U,I, U, U, U, U, I,U, U, U },
new[] { U, U, U, U, U, U, U, U, U, U, U, U, U, 2.0F, I,U, U, U,I, U, U, U, U },
new[] { 1.5F, U, U, U, U, U, U, U, U, U, U, U, U, ENT, I,U, U, U,I, U, U, U, T },
new[] { 1.75F, U, U, U, U, U, U, U, U, U, U, U, U, ER, I,_, _, _,I, U, U, U, _ },
new[] { SM, U, U, U, U, U, U, U, U, U, U, U, 2.75F, I,_, U, _,I, U, U, U, T },
new[] { SM, SM, SM, 6.25F, SM, SM, SM, SM, I,U, U, U,I, 2.0F, U, _ },
};
private static readonly Key?[][] KeyboardKeys = {
new Key?[] { Key.Escape, null, Key.F1, Key.F2, Key.F3, Key.F4, null, Key.F5, Key.F6, Key.F7, Key.F8, null, Key.F9, Key.F10, Key.F11, Key.F12, null, Key.PrintScreen, Key.ScrollLock, Key.Pause },
new Key?[] { Key.GraveAccent, Key.Number1, Key.Number2, Key.Number3, Key.Number4, Key.Number5, Key.Number6, Key.Number7, Key.Number8, Key.Number9, Key.Number0, Key.Minus, Key.Equal, Key.Backspace, null, Key.Insert, Key.Home, Key.PageUp, null, Key.NumLock, Key.KeypadDivide, Key.KeypadMultiply, Key.KeypadSubtract },
new Key?[] { Key.Tab, Key.Q, Key.W, Key.E, Key.R, Key.T, Key.Y, Key.U, Key.I, Key.O, Key.P, Key.LeftBracket, Key.RightBracket, Key.Enter, null, Key.Delete, Key.End, Key.PageDown, null, Key.Keypad7, Key.Keypad8, Key.Keypad9, Key.KeypadAdd },
new Key?[] { Key.CapsLock, Key.A, Key.S, Key.D, Key.F, Key.G, Key.H, Key.J, Key.K, Key.L, Key.Semicolon, Key.Apostrophe, Key.BackSlash, null, null, null, null, null, null, Key.Keypad4, Key.Keypad5, Key.Keypad6, null },
new Key?[] { Key.ShiftLeft, Key.World1, Key.Z, Key.X, Key.C, Key.V, Key.B, Key.N, Key.M, Key.Comma, Key.Period, Key.Slash, Key.ShiftRight, null, null, Key.Up, null, null, Key.Keypad1, Key.Keypad2, Key.Keypad3, Key.KeypadEnter },
new Key?[] { Key.ControlLeft, Key.SuperLeft, Key.AltLeft, Key.Space, Key.AltRight, Key.SuperRight, Key.Menu, Key.ControlRight, null, Key.Left, Key.Down, Key.Right, null, Key.Keypad0, Key.KeypadDecimal, null },
};
private static readonly Dictionary<Key, string> KeyToNameMapping = new() {
[Key.Escape] = "Ecs", [Key.PrintScreen] = "Prn\nScr", [Key.ScrollLock] = "Scr\nLck", [Key.Pause] = "Pause",
[Key.F1] = "F1", [Key.F2] = "F2", [Key.F3] = "F3", [Key.F4] = "F4", [Key.F5] = "F5", [Key.F6] = "F6",
[Key.F7] = "F7", [Key.F8] = "F8", [Key.F9] = "F9", [Key.F10] = "F10", [Key.F11] = "F11", [Key.F12] = "F12",
[Key.Tab] = "Tab", [Key.CapsLock] = "Caps\nLock", [Key.Menu] = "Menu",
[Key.Backspace] = "Backspace", [Key.Enter] = "Enter",
[Key.ControlLeft] = "Ctrl", [Key.ControlRight] = "Ctrl",
[Key.ShiftLeft] = "Shift", [Key.ShiftRight] = "Shift",
[Key.AltLeft] = "Alt", [Key.AltRight] = "Alt",
[Key.SuperLeft] = "Super", [Key.SuperRight] = "Super",
[Key.Insert] = "Ins", [Key.Delete] = "Del",
[Key.Home] = "Home", [Key.End] = "End",
[Key.PageUp] = "PgUp", [Key.PageDown] = "PgDn",
[Key.NumLock] = "Num\nLck", [Key.KeypadEnter] = "Enter",
};
public static void DrawKeyboard(EntityRef keyboard)
{
var GLFW = Glfw.GetApi();
const float UnitKeySize = 32.0F;
Vector2 Size = new Vector2(23, 6.5F) * UnitKeySize;
Vector2 Border = new(1, 1);
Vector2 LabelOffset = new(7, 3);
Vector2 FaceStartOffset = new(5, 3);
Vector2 FaceEndOffset = new(5, 6);
uint BorderColor = Color.FromGrayscale(24).RGBA;
uint LabelColor = Color.FromGrayscale(64).RGBA;
var draw = ImGui.GetWindowDrawList();
var offset = ImGui.GetCursorScreenPos();
var current = Vector2.Zero;
foreach (var (widths, keys) in KeyboardLayout.Zip(KeyboardKeys)) {
foreach (var (width, key) in widths.Zip(keys)) {
uint KeyColor(byte lightness)
=> (key != null) && (keyboard.Lookup(key.Value.ToString())?.Has<Active>() == true)
? Color.FromRGB(lightness, (byte)(lightness / 2), (byte)(lightness / 2)).RGBA
: Color.FromGrayscale(lightness).RGBA;
var start = offset + current;
var keySize = new Vector2(width, 1.0F);
if (width == T) keySize = new Vector2((-keySize.X - 10), 2.0F);
else if (width < -10) keySize = new Vector2((-keySize.X - 10), 1.0F);
else if (width < 0) { current += new Vector2(-keySize.X * UnitKeySize, 0); continue; }
var label = (key != null) ? KeyToNameMapping.GetValueOrDefault(key.Value)
?? GLFW.GetKeyName((int)key.Value, 0)?.ToUpper() : null;
var end = start + keySize * UnitKeySize;
if (width == ENT) {
var start2 = start + new Vector2(0.25F * UnitKeySize, 0);
var end2 = end + new Vector2(0, 1 * UnitKeySize);
draw.AddRectFilled(start , end , BorderColor, 3);
draw.AddRectFilled(start2 , end2 , BorderColor, 3);
draw.AddRectFilled(start + Border, end - Border, KeyColor(204), 3);
draw.AddRectFilled(start2 + Border, end2 - Border, KeyColor(204), 3);
var faceStart = start + FaceStartOffset;
var faceEnd = end - FaceEndOffset;
draw.AddRectFilled(faceStart , faceEnd , KeyColor(252), 2);
var faceStart2 = start2 + FaceStartOffset;
var faceEnd2 = end2 - FaceEndOffset;
draw.AddRectFilled(faceStart2, faceEnd2, KeyColor(252), 2);
if (label != null) draw.AddText(start + LabelOffset, LabelColor, label);
} else {
draw.AddRectFilled(start , end , BorderColor, 3);
draw.AddRectFilled(start + Border, end - Border, KeyColor(204), 3);
var faceStart = start + FaceStartOffset;
var faceEnd = end - FaceEndOffset;
draw.AddRectFilled(faceStart, faceEnd, KeyColor(252), 2);
if (label != null) draw.AddText(start + LabelOffset, LabelColor, label);
}
current += new Vector2(keySize.X * UnitKeySize, 0);
}
current = new Vector2(0, current.Y + UnitKeySize);
if (widths == KeyboardLayout[0]) current += new Vector2(0, UnitKeySize / 2);
}
ImGui.Dummy(Size);
}
}