From 6826ab3ffdd881693ff653c30bac3fca355363d4 Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 2 Jul 2015 21:32:29 -0600 Subject: [PATCH] Added GetKeyIndex() helper. --- imgui.cpp | 20 ++++++++++++++------ imgui.h | 5 +++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index aacb80c2..002cea48 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -682,6 +682,8 @@ ImGuiIO::ImGuiIO() MouseDoubleClickTime = 0.30f; MouseDoubleClickMaxDist = 6.0f; MouseDragThreshold = 6.0f; + for (int i = 0; i < ImGuiKey_COUNT; i++) + KeyMap[i] = -1; KeyRepeatDelay = 0.250f; KeyRepeatRate = 0.050f; UserData = NULL; @@ -2888,21 +2890,27 @@ bool ImGui::IsPosHoveringAnyWindow(const ImVec2& pos) static bool IsKeyPressedMap(ImGuiKey key, bool repeat) { - ImGuiState& g = *GImGui; - const int key_index = g.IO.KeyMap[key]; + const int key_index = GImGui->IO.KeyMap[key]; return ImGui::IsKeyPressed(key_index, repeat); } +int ImGui::GetKeyIndex(ImGuiKey key) +{ + IM_ASSERT(key >= 0 && key < ImGuiKey_COUNT); + return GImGui->IO.KeyMap[key]; +} + bool ImGui::IsKeyDown(int key_index) { - ImGuiState& g = *GImGui; - IM_ASSERT(key_index >= 0 && key_index < IM_ARRAYSIZE(g.IO.KeysDown)); - return g.IO.KeysDown[key_index]; + if (key_index < 0) return false; + IM_ASSERT(key_index >= 0 && key_index < IM_ARRAYSIZE(GImGui->IO.KeysDown)); + return GImGui->IO.KeysDown[key_index]; } bool ImGui::IsKeyPressed(int key_index, bool repeat) { ImGuiState& 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) @@ -2920,10 +2928,10 @@ bool ImGui::IsKeyPressed(int key_index, bool repeat) bool ImGui::IsKeyReleased(int key_index) { ImGuiState& g = *GImGui; + if (key_index < 0) return false; IM_ASSERT(key_index >= 0 && key_index < IM_ARRAYSIZE(g.IO.KeysDown)); if (g.IO.KeysDownDurationPrev[key_index] >= 0.0f && !g.IO.KeysDown[key_index]) return true; - return false; } diff --git a/imgui.h b/imgui.h index 4e12c4ec..d7495dfa 100644 --- a/imgui.h +++ b/imgui.h @@ -371,8 +371,9 @@ namespace ImGui IMGUI_API void ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float& out_g, float& out_b); // Inputs - IMGUI_API bool IsKeyDown(int key_index); // key_index into the keys_down[512] array, imgui doesn't know the semantic of each entry, uses your own indices! - IMGUI_API bool IsKeyPressed(int key_index, bool repeat = true); // if repeat=true. uses io.KeyRepeatDelay / KeyRepeatRate + 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 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)