how dumb, imgui has a repeat feature for shortcuts, removed a lot of nonsense

This commit is contained in:
2025-11-19 22:31:28 -05:00
parent ca719d7dfb
commit ab65eb7c58
6 changed files with 25 additions and 76 deletions
+3 -56
View File
@@ -3,7 +3,6 @@
#include <imgui/imgui_internal.h>
#include <cmath>
#include <set>
#include <sstream>
#include <unordered_map>
@@ -300,66 +299,14 @@ namespace anm2ed::imgui
return ImVec2(ImGui::GetTextLineHeightWithSpacing(), ImGui::GetTextLineHeightWithSpacing());
}
bool chord_held(ImGuiKeyChord chord)
{
auto& io = ImGui::GetIO();
for (constexpr ImGuiKey mods[] = {ImGuiMod_Ctrl, ImGuiMod_Shift, ImGuiMod_Alt, ImGuiMod_Super}; ImGuiKey mod : mods)
{
bool required = (chord & mod) != 0;
if (bool held = io.KeyMods & mod; required && !held) return false;
}
auto main_key = (ImGuiKey)(chord & ~ImGuiMod_Mask_);
if (main_key == ImGuiKey_None) return false;
return ImGui::IsKeyDown(main_key);
}
bool chord_repeating(ImGuiKeyChord chord, float delay, float rate)
{
struct State
{
float timeHeld = 0.f;
float nextRepeat = 0.f;
};
static std::unordered_map<ImGuiKeyChord, State> stateMap;
auto& io = ImGui::GetIO();
auto& state = stateMap[chord];
if (chord_held(chord))
{
state.timeHeld += io.DeltaTime;
if (state.timeHeld <= io.DeltaTime)
{
state.nextRepeat = delay;
return true;
}
if (state.timeHeld >= state.nextRepeat)
{
state.nextRepeat += rate;
return true;
}
}
else
{
state.timeHeld = 0.f;
state.nextRepeat = 0.f;
}
return false;
}
bool shortcut(ImGuiKeyChord chord, shortcut::Type type, bool isRepeat)
bool shortcut(ImGuiKeyChord chord, shortcut::Type type)
{
if (ImGui::GetTopMostPopupModal() != nullptr) return false;
if (isRepeat && !isRenaming) return chord_repeating(chord);
int flags = type == shortcut::GLOBAL || type == shortcut::GLOBAL_SET ? ImGuiInputFlags_RouteGlobal
: ImGuiInputFlags_RouteFocused;
flags |= ImGuiInputFlags_Repeat;
if (type == shortcut::GLOBAL_SET || type == shortcut::FOCUSED_SET)
{
ImGui::SetNextItemShortcut(chord, flags);