feat: implement better keybind system

This commit is contained in:
2025-10-15 21:06:13 -04:00
parent c92517af5c
commit 5832f92edc
4 changed files with 109 additions and 61 deletions
+19 -2
View File
@@ -330,6 +330,12 @@ namespace Spectrum
GDI, GDI,
DirectX DirectX
} }
public enum KeybindType
{
Hold,
Toggle,
Always
}
#endregion #endregion
public class ConfigData public class ConfigData
{ {
@@ -353,7 +359,7 @@ namespace Spectrum
#region Aim Settings #region Aim Settings
public bool EnableAim { get; set; } = true; public bool EnableAim { get; set; } = true;
public bool ClosestToMouse { get; set; } = true; public bool ClosestToMouse { get; set; } = true;
public Keys Keybind { get; set; } = Keys.XButton2; public Keybind Keybind = new(Keys.XButton2, KeybindType.Hold);
public double Sensitivity { get; set; } = 0.5; public double Sensitivity { get; set; } = 0.5;
public MovementType AimMovementType { get; set; } = MovementType.Adaptive; public MovementType AimMovementType { get; set; } = MovementType.Adaptive;
public bool EmaSmoothening { get; set; } = true; public bool EmaSmoothening { get; set; } = true;
@@ -364,7 +370,7 @@ namespace Spectrum
#region Trigger Settings #region Trigger Settings
public bool TriggerBot { get; set; } = false; public bool TriggerBot { get; set; } = false;
public Keys TriggerKey { get; set; } = Keys.XButton2; public Keybind TriggerKeybind = new(Keys.XButton1, KeybindType.Hold);
public int TriggerDelay { get; set; } = 50; // milliseconds public int TriggerDelay { get; set; } = 50; // milliseconds
public int TriggerFov { get; set; } = 15; // pixels public int TriggerFov { get; set; } = 15; // pixels
public int TriggerDuration { get; set; } = 100; // milliseconds public int TriggerDuration { get; set; } = 100; // milliseconds
@@ -413,4 +419,15 @@ namespace Spectrum
public Scalar Upper { get; set; } = upper; public Scalar Upper { get; set; } = upper;
public Scalar Lower { get; set; } = lower; public Scalar Lower { get; set; } = lower;
} }
public class Keybind
{
public Keys Key { get; set; }
public KeybindType Type { get; set; }
public Keybind(Keys key, KeybindType type)
{
Key = key;
Type = type;
}
}
} }
+8 -8
View File
@@ -40,7 +40,7 @@ namespace Spectrum.Detection
{ {
try try
{ {
if (InputManager.IsKeyOrMouseDown(mainConfig.Data.Keybind) || InputManager.IsKeyOrMouseDown(mainConfig.Data.TriggerKey)) if (InputManager.IsKeyOrMouseDown(mainConfig.Data.Keybind) || InputManager.IsKeyOrMouseDown(mainConfig.Data.TriggerKeybind))
{ {
var config = mainConfig.Data; var config = mainConfig.Data;
if (config.DebugMode) if (config.DebugMode)
@@ -231,7 +231,13 @@ namespace Spectrum.Detection
} }
} }
} }
else if (InputManager.IsKeyOrMouseDown(mainConfig.Data.MenuKey)) else
{
renderer?.ClearDetectionDrawCommands();
renderer?.CommitDetectionDrawCommands();
Thread.Sleep(5);
}
if (InputManager.IsKeyOrMouseDown(mainConfig.Data.MenuKey))
{ {
var now = DateTime.Now; var now = DateTime.Now;
if ((now - lastMenuToggle).TotalMilliseconds > 175) if ((now - lastMenuToggle).TotalMilliseconds > 175)
@@ -240,12 +246,6 @@ namespace Spectrum.Detection
lastMenuToggle = now; lastMenuToggle = now;
} }
} }
else
{
renderer?.ClearDetectionDrawCommands();
renderer?.CommitDetectionDrawCommands();
Thread.Sleep(5);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
+79 -1
View File
@@ -313,7 +313,11 @@ namespace Spectrum
if (parseStr.EndsWith("%")) parseStr = parseStr.TrimEnd('%'); if (parseStr.EndsWith("%")) parseStr = parseStr.TrimEnd('%');
if (float.TryParse(parseStr, NumberStyles.Float, CultureInfo.InvariantCulture, out float typed)) if (float.TryParse(parseStr, NumberStyles.Float, CultureInfo.InvariantCulture, out float typed))
{ {
float clamped = Math.Clamp(typed, min, max); float clamped;
if (enforceRange)
clamped = Math.Clamp(typed, min, max);
else
clamped = typed;
if (Math.Abs(clamped - value) > float.Epsilon) if (Math.Abs(clamped - value) > float.Epsilon)
{ {
value = clamped; value = clamped;
@@ -340,6 +344,7 @@ namespace Spectrum
drawList.AddRectFilled(p0, p1, ImGui.GetColorU32(ImGuiCol.FrameBg), 4f); drawList.AddRectFilled(p0, p1, ImGui.GetColorU32(ImGuiCol.FrameBg), 4f);
float fillWidth = ((value - min) / (max - min)) * (p1.X - p0.X); float fillWidth = ((value - min) / (max - min)) * (p1.X - p0.X);
fillWidth = Math.Clamp(fillWidth, 0f, p1.X - p0.X);
drawList.AddRectFilled(p0, new Vector2(p0.X + fillWidth, p1.Y), ImGui.GetColorU32(ImGuiCol.SliderGrabActive), 4f); drawList.AddRectFilled(p0, new Vector2(p0.X + fillWidth, p1.Y), ImGui.GetColorU32(ImGuiCol.SliderGrabActive), 4f);
ImGui.PopStyleVar(); ImGui.PopStyleVar();
@@ -466,5 +471,78 @@ namespace Spectrum
var g = _paneGroups.Pop(); var g = _paneGroups.Pop();
g.Index = g.Count; g.Index = g.Count;
} }
private static readonly Dictionary<string, bool> _keybindListening = [];
private static readonly Dictionary<string, Keys> _keybindPending = [];
private static readonly Dictionary<string, Task?> _keybindTasks = [];
public static bool KeybindInput(string label, ref Keybind keybind, bool inline = false)
{
bool changed = false;
ImGui.PushID(label);
if (!inline)
ImGui.TextUnformatted(label);
string buttonText = _keybindListening.GetValueOrDefault(label)
? "Listening..."
: keybind.Key.ToString();
ImGui.SameLine(ImGui.GetContentRegionAvail().X - ImGui.CalcTextSize(buttonText).X);
if (!_keybindListening.GetValueOrDefault(label, false))
{
if (ImGui.Button($"{buttonText}###{label}_Button"))
{
_keybindListening[label] = true;
_keybindPending[label] = Keys.None;
_keybindTasks[label] = Task.Run(async () =>
{
var key = await InputManager.ListenForNextKeyOrMouseAsync();
_keybindPending[label] = key;
});
}
if (ImGui.BeginPopupContextItem($"###{label}_Context"))
{
bool isHold = keybind.Type == KeybindType.Hold;
if (ImGui.MenuItem("Hold", null, isHold))
{
keybind.Type = KeybindType.Hold;
changed = true;
}
bool isToggle = keybind.Type == KeybindType.Toggle;
if (ImGui.MenuItem("Toggle", null, isToggle))
{
keybind.Type = KeybindType.Toggle;
changed = true;
}
bool isAlways = keybind.Type == KeybindType.Always;
if (ImGui.MenuItem("Always", null, isAlways))
{
keybind.Type = KeybindType.Always;
changed = true;
}
ImGui.EndPopup();
}
}
else
{
ImGui.Button($"{buttonText}###{label}_Listening");
if (_keybindPending.GetValueOrDefault(label, Keys.None) != Keys.None)
{
keybind.Key = _keybindPending[label];
_keybindPending[label] = Keys.None;
_keybindListening[label] = false;
changed = true;
}
}
ImGui.PopID();
return changed;
}
} }
} }
+2 -49
View File
@@ -81,31 +81,7 @@ namespace Spectrum
ImGui.SameLine(); ImGui.SameLine();
ImGui.TextUnformatted("Enable Aiming"); ImGui.TextUnformatted("Enable Aiming");
ImGuiExtensions.KeybindInput("Aim Keybind", ref config.Keybind, true);
ImGui.SameLine(ImGui.GetContentRegionAvail().X - ImGui.CalcTextSize(_waitingForKeybind.GetValueOrDefault("Aiming") ? "Listening..." : config.Keybind.ToString()).X);
if (!_waitingForKeybind.GetValueOrDefault("Aiming", false))
{
if (ImGui.Button($"{config.Keybind.ToString()}###AimKeybind"))
{
_waitingForKeybind["Aiming"] = true;
_pendingKeybind["Aiming"] = Keys.None;
_keybindTask["Aiming"] = Task.Run(async () =>
{
var key = await InputManager.ListenForNextKeyOrMouseAsync();
_pendingKeybind["Aiming"] = key;
});
}
}
else
{
ImGui.Button("Listening... ###AimListening");
if (_pendingKeybind.GetValueOrDefault("Aiming", Keys.None) != Keys.None)
{
config.Keybind = _pendingKeybind["Aiming"];
_pendingKeybind["Aiming"] = Keys.None;
_waitingForKeybind["Aiming"] = false;
}
}
bool closestToMouse = config.ClosestToMouse; bool closestToMouse = config.ClosestToMouse;
if (ImGui.Checkbox(" Closest to Mouse", ref closestToMouse)) if (ImGui.Checkbox(" Closest to Mouse", ref closestToMouse))
@@ -249,30 +225,7 @@ namespace Spectrum
bool triggerBot = config.TriggerBot; bool triggerBot = config.TriggerBot;
if (ImGui.Checkbox(" Enabled", ref triggerBot)) if (ImGui.Checkbox(" Enabled", ref triggerBot))
config.TriggerBot = triggerBot; config.TriggerBot = triggerBot;
ImGui.SameLine(ImGui.GetContentRegionAvail().X - ImGui.CalcTextSize(_waitingForKeybind.GetValueOrDefault("Trigger Key") ? "Listening..." : config.TriggerKey.ToString()).X); ImGuiExtensions.KeybindInput("Trigger Keybind", ref config.TriggerKeybind, true);
if (!_waitingForKeybind.GetValueOrDefault("Trigger Key", false))
{
if (ImGui.Button($"{config.TriggerKey}###TriggerKeybind"))
{
_waitingForKeybind["Trigger Key"] = true;
_pendingKeybind["Trigger Key"] = Keys.None;
_keybindTask["Trigger Key"] = Task.Run(async () =>
{
var key = await InputManager.ListenForNextKeyOrMouseAsync();
_pendingKeybind["Trigger Key"] = key;
});
}
}
else
{
ImGui.Button("Listening... ###TriggerListening");
if (_pendingKeybind.GetValueOrDefault("Trigger Key", Keys.None) != Keys.None)
{
config.TriggerKey = _pendingKeybind["Trigger Key"];
_pendingKeybind["Trigger Key"] = Keys.None;
_waitingForKeybind["Trigger Key"] = false;
}
}
if (triggerBot) if (triggerBot)
{ {