From bbd3b7560922fe3ff68cf418ed7ada7370a31c79 Mon Sep 17 00:00:00 2001 From: ocornut Date: Sat, 16 Jul 2016 10:46:22 +0200 Subject: [PATCH] Added IsKeyPressed() with explicit repeat delay and repeat rate (for usage by nav) (#323) --- imgui.cpp | 34 ++++++++++++++++++++++++---------- imgui.h | 1 + 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index b8a6fe9c9..a8f70f32e 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -652,6 +652,7 @@ static void LogRenderedText(const ImVec2& ref_pos, const char* text, static void PushMultiItemsWidths(int components, float w_full = 0.0f); static float GetDraggedColumnOffset(int column_index); +static bool IsKeyDownMap(ImGuiKey key); static bool IsKeyPressedMap(ImGuiKey key, bool repeat = true); static void SetCurrentFont(ImFont* font); @@ -3065,10 +3066,16 @@ bool ImGui::IsPosHoveringAnyWindow(const ImVec2& pos) return FindHoveredWindow(pos, false) != NULL; } +static bool IsKeyDownMap(ImGuiKey key) +{ + const int key_index = GImGui->IO.KeyMap[key]; + return (key_index >= 0) ? ImGui::IsKeyDown(key_index) : false; +} + static bool IsKeyPressedMap(ImGuiKey key, bool repeat) { const int key_index = GImGui->IO.KeyMap[key]; - return ImGui::IsKeyPressed(key_index, repeat); + return (key_index >= 0) ? ImGui::IsKeyPressed(key_index, repeat) : false; } int ImGui::GetKeyIndex(ImGuiKey key) @@ -3084,7 +3091,7 @@ bool ImGui::IsKeyDown(int key_index) return GImGui->IO.KeysDown[key_index]; } -bool ImGui::IsKeyPressed(int key_index, bool repeat) +bool ImGui::IsKeyPressed(int key_index, float repeat_delay, float repeat_rate) { ImGuiContext& g = *GImGui; if (key_index < 0) return false; @@ -3092,16 +3099,23 @@ bool ImGui::IsKeyPressed(int key_index, bool repeat) const float t = g.IO.KeysDownDuration[key_index]; if (t == 0.0f) return true; - - if (repeat && t > g.IO.KeyRepeatDelay) - { - float delay = g.IO.KeyRepeatDelay, rate = g.IO.KeyRepeatRate; - if ((fmodf(t - delay, rate) > rate*0.5f) != (fmodf(t - delay - g.IO.DeltaTime, rate) > rate*0.5f)) + + if (t > repeat_delay && repeat_rate > 0.0f) + if ((fmodf(t - repeat_delay, repeat_rate) > repeat_rate*0.5f) != (fmodf(t - repeat_delay - g.IO.DeltaTime, repeat_rate) > repeat_rate*0.5f)) return true; - } + return false; } +bool ImGui::IsKeyPressed(int key_index, bool repeat) +{ + ImGuiContext& g = *GImGui; + if (repeat) + return IsKeyPressed(key_index, g.IO.KeyRepeatDelay, g.IO.KeyRepeatRate); + else + return IsKeyPressed(key_index, 0.0f, 0.0f); +} + bool ImGui::IsKeyReleased(int key_index) { ImGuiContext& g = *GImGui; @@ -9725,8 +9739,8 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImGui::Text("FocusedWindow: '%s'", g.FocusedWindow ? g.FocusedWindow->Name : "NULL"); ImGui::Text("HoveredWindow: '%s'", g.HoveredWindow ? g.HoveredWindow->Name : "NULL"); ImGui::Text("HoveredRootWindow: '%s'", g.HoveredRootWindow ? g.HoveredRootWindow->Name : "NULL"); - ImGui::Text("HoveredID: 0x%08X/0x%08X", g.HoveredId, g.HoveredIdPreviousFrame); // Data is "in-flight" so depending on when the Metrics window is called we may see current frame information or not - ImGui::Text("ActiveID: 0x%08X/0x%08X", g.ActiveId, g.ActiveIdPreviousFrame); + ImGui::Text("HoveredId: 0x%08X/0x%08X", g.HoveredId, g.HoveredIdPreviousFrame); // Data is "in-flight" so depending on when the Metrics window is called we may see current frame information or not + ImGui::Text("ActiveId: 0x%08X/0x%08X", g.ActiveId, g.ActiveIdPreviousFrame); ImGui::TreePop(); } } diff --git a/imgui.h b/imgui.h index dffb50604..c4b8c35fa 100644 --- a/imgui.h +++ b/imgui.h @@ -430,6 +430,7 @@ namespace ImGui IMGUI_API int GetKeyIndex(ImGuiKey key); // map ImGuiKey_* values into user's key index. == io.KeyMap[key] IMGUI_API bool IsKeyDown(int key_index); // key_index into the keys_down[] array, imgui doesn't know the semantic of each entry, uses your own indices! IMGUI_API bool IsKeyPressed(int key_index, bool repeat = true); // uses user's key indices as stored in the keys_down[] array. if repeat=true. uses io.KeyRepeatDelay / KeyRepeatRate + IMGUI_API bool IsKeyPressed(int key_index, float repeat_delay, float repeat_rate); // uses user's key indices as stored in the keys_down[] array. uses provided repeat rate/delay IMGUI_API bool IsKeyReleased(int key_index); // " IMGUI_API bool IsMouseDown(int button); // is mouse button held IMGUI_API bool IsMouseClicked(int button, bool repeat = false); // did mouse button clicked (went from !Down to Down)