From 4a11cc35b901e7e49740cc60b5291dce72a5e9a5 Mon Sep 17 00:00:00 2001 From: ocornut Date: Tue, 19 Jul 2016 21:26:36 +0200 Subject: [PATCH] Updated code for repeat delay / repeat handling. GetKeyPressedAmount() now returns a count to support fast repeat rate (where DeltaTime > RepeatRate). Renamed from recently added IsKeyPressed() variant to GetKeyPressedAmount(). (no API breakage, added in branch, bbd3b7560922fe3ff68cf418ed7ada7370a31c79) (#323) --- imgui.cpp | 18 +++++++++--------- imgui.h | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 8f679470c..bba5c176d 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -3100,29 +3100,29 @@ bool ImGui::IsKeyDown(int key_index) return GImGui->IO.KeysDown[key_index]; } -bool ImGui::IsKeyPressed(int key_index, float repeat_delay, float repeat_rate) +int ImGui::GetKeyPressedAmount(int key_index, float repeat_delay, float repeat_rate) { ImGuiContext& g = *GImGui; if (key_index < 0) return false; IM_ASSERT(key_index >= 0 && key_index < IM_ARRAYSIZE(g.IO.KeysDown)); const float t = g.IO.KeysDownDuration[key_index]; if (t == 0.0f) - return true; - + return 1; 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; + { + int count = (int)((t - repeat_delay) / repeat_rate) - (int)((t - repeat_delay - g.IO.DeltaTime) / repeat_rate); + return (count > 0) ? count : 0; + } + return 0; } bool ImGui::IsKeyPressed(int key_index, bool repeat) { ImGuiContext& g = *GImGui; if (repeat) - return IsKeyPressed(key_index, g.IO.KeyRepeatDelay, g.IO.KeyRepeatRate); + return GetKeyPressedAmount(key_index, g.IO.KeyRepeatDelay, g.IO.KeyRepeatRate) > 0; else - return IsKeyPressed(key_index, 0.0f, 0.0f); + return GetKeyPressedAmount(key_index, 0.0f, 0.0f) > 0; } bool ImGui::IsKeyReleased(int key_index) diff --git a/imgui.h b/imgui.h index 2db1a3ead..994b663c7 100644 --- a/imgui.h +++ b/imgui.h @@ -433,8 +433,8 @@ 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 int GetKeyPressedAmount(int key_index, float repeat_delay, float rate); // uses provided repeat rate/delay. return a count, typically 0 or 1 but may be >1 if RepeatRate is small enough that DeltaTime > RepeatRate 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) IMGUI_API bool IsMouseDoubleClicked(int button); // did mouse button double-clicked. a double-click returns false in IsMouseClicked(). uses io.MouseDoubleClickTime.