Added IsKeyPressed() with explicit repeat delay and repeat rate (for usage by nav) (#323)

This commit is contained in:
ocornut 2016-07-16 10:46:22 +02:00
parent 9f92cc255b
commit bbd3b75609
2 changed files with 25 additions and 10 deletions

View File

@ -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 void PushMultiItemsWidths(int components, float w_full = 0.0f);
static float GetDraggedColumnOffset(int column_index); static float GetDraggedColumnOffset(int column_index);
static bool IsKeyDownMap(ImGuiKey key);
static bool IsKeyPressedMap(ImGuiKey key, bool repeat = true); static bool IsKeyPressedMap(ImGuiKey key, bool repeat = true);
static void SetCurrentFont(ImFont* font); static void SetCurrentFont(ImFont* font);
@ -3065,10 +3066,16 @@ bool ImGui::IsPosHoveringAnyWindow(const ImVec2& pos)
return FindHoveredWindow(pos, false) != NULL; 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) static bool IsKeyPressedMap(ImGuiKey key, bool repeat)
{ {
const int key_index = GImGui->IO.KeyMap[key]; 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) int ImGui::GetKeyIndex(ImGuiKey key)
@ -3084,7 +3091,7 @@ bool ImGui::IsKeyDown(int key_index)
return GImGui->IO.KeysDown[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; ImGuiContext& g = *GImGui;
if (key_index < 0) return false; 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]; const float t = g.IO.KeysDownDuration[key_index];
if (t == 0.0f) if (t == 0.0f)
return true; return true;
if (repeat && t > g.IO.KeyRepeatDelay) 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))
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))
return true; return true;
}
return false; 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) bool ImGui::IsKeyReleased(int key_index)
{ {
ImGuiContext& g = *GImGui; 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("FocusedWindow: '%s'", g.FocusedWindow ? g.FocusedWindow->Name : "NULL");
ImGui::Text("HoveredWindow: '%s'", g.HoveredWindow ? g.HoveredWindow->Name : "NULL"); ImGui::Text("HoveredWindow: '%s'", g.HoveredWindow ? g.HoveredWindow->Name : "NULL");
ImGui::Text("HoveredRootWindow: '%s'", g.HoveredRootWindow ? g.HoveredRootWindow->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("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("ActiveId: 0x%08X/0x%08X", g.ActiveId, g.ActiveIdPreviousFrame);
ImGui::TreePop(); ImGui::TreePop();
} }
} }

View File

@ -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 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 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, 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 IsKeyReleased(int key_index); // "
IMGUI_API bool IsMouseDown(int button); // is mouse button held 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 IsMouseClicked(int button, bool repeat = false); // did mouse button clicked (went from !Down to Down)