mirror of
https://github.com/whoswhip/Spectrum.git
synced 2026-12-06 01:28:42 +00:00
feat: implement better keybind system
This commit is contained in:
+19
-2
@@ -330,6 +330,12 @@ namespace Spectrum
|
||||
GDI,
|
||||
DirectX
|
||||
}
|
||||
public enum KeybindType
|
||||
{
|
||||
Hold,
|
||||
Toggle,
|
||||
Always
|
||||
}
|
||||
#endregion
|
||||
public class ConfigData
|
||||
{
|
||||
@@ -353,7 +359,7 @@ namespace Spectrum
|
||||
#region Aim Settings
|
||||
public bool EnableAim { 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 MovementType AimMovementType { get; set; } = MovementType.Adaptive;
|
||||
public bool EmaSmoothening { get; set; } = true;
|
||||
@@ -364,7 +370,7 @@ namespace Spectrum
|
||||
|
||||
#region Trigger Settings
|
||||
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 TriggerFov { get; set; } = 15; // pixels
|
||||
public int TriggerDuration { get; set; } = 100; // milliseconds
|
||||
@@ -413,4 +419,15 @@ namespace Spectrum
|
||||
public Scalar Upper { get; set; } = upper;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,7 +40,7 @@ namespace Spectrum.Detection
|
||||
{
|
||||
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;
|
||||
if (config.DebugMode)
|
||||
@@ -195,7 +195,7 @@ namespace Spectrum.Detection
|
||||
{
|
||||
AutoLabeling.AddBackgroundImage(drawing, false);
|
||||
}
|
||||
|
||||
|
||||
renderer?.CommitDetectionDrawCommands();
|
||||
|
||||
mat.Dispose();
|
||||
@@ -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;
|
||||
if ((now - lastMenuToggle).TotalMilliseconds > 175)
|
||||
@@ -240,12 +246,6 @@ namespace Spectrum.Detection
|
||||
lastMenuToggle = now;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
renderer?.ClearDetectionDrawCommands();
|
||||
renderer?.CommitDetectionDrawCommands();
|
||||
Thread.Sleep(5);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -313,7 +313,11 @@ namespace Spectrum
|
||||
if (parseStr.EndsWith("%")) parseStr = parseStr.TrimEnd('%');
|
||||
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)
|
||||
{
|
||||
value = clamped;
|
||||
@@ -340,6 +344,7 @@ namespace Spectrum
|
||||
drawList.AddRectFilled(p0, p1, ImGui.GetColorU32(ImGuiCol.FrameBg), 4f);
|
||||
|
||||
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);
|
||||
|
||||
ImGui.PopStyleVar();
|
||||
@@ -466,5 +471,78 @@ namespace Spectrum
|
||||
var g = _paneGroups.Pop();
|
||||
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
@@ -81,31 +81,7 @@ namespace Spectrum
|
||||
|
||||
ImGui.SameLine();
|
||||
ImGui.TextUnformatted("Enable Aiming");
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
ImGuiExtensions.KeybindInput("Aim Keybind", ref config.Keybind, true);
|
||||
|
||||
bool closestToMouse = config.ClosestToMouse;
|
||||
if (ImGui.Checkbox(" Closest to Mouse", ref closestToMouse))
|
||||
@@ -249,30 +225,7 @@ namespace Spectrum
|
||||
bool triggerBot = config.TriggerBot;
|
||||
if (ImGui.Checkbox(" Enabled", ref triggerBot))
|
||||
config.TriggerBot = triggerBot;
|
||||
ImGui.SameLine(ImGui.GetContentRegionAvail().X - ImGui.CalcTextSize(_waitingForKeybind.GetValueOrDefault("Trigger Key") ? "Listening..." : config.TriggerKey.ToString()).X);
|
||||
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;
|
||||
}
|
||||
}
|
||||
ImGuiExtensions.KeybindInput("Trigger Keybind", ref config.TriggerKeybind, true);
|
||||
|
||||
if (triggerBot)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user