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, bbd3b75609) (#323)
This commit is contained in:
ocornut 2016-07-19 21:26:36 +02:00
parent 88c1966629
commit 4a11cc35b9
2 changed files with 10 additions and 10 deletions

View File

@ -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)

View File

@ -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.