Draw an arbitrary US keyboard layout to visualize translated keys
| 15845 | |
| 15846 | // Draw an arbitrary US keyboard layout to visualize translated keys |
| 15847 | void ImGui::DebugRenderKeyboardPreview(ImDrawList* draw_list) |
| 15848 | { |
| 15849 | const float scale = ImGui::GetFontSize() / 13.0f; |
| 15850 | const ImVec2 key_size = ImVec2(35.0f, 35.0f) * scale; |
| 15851 | const float key_rounding = 3.0f * scale; |
| 15852 | const ImVec2 key_face_size = ImVec2(25.0f, 25.0f) * scale; |
| 15853 | const ImVec2 key_face_pos = ImVec2(5.0f, 3.0f) * scale; |
| 15854 | const float key_face_rounding = 2.0f * scale; |
| 15855 | const ImVec2 key_label_pos = ImVec2(7.0f, 4.0f) * scale; |
| 15856 | const ImVec2 key_step = ImVec2(key_size.x - 1.0f, key_size.y - 1.0f); |
| 15857 | const float key_row_offset = 9.0f * scale; |
| 15858 | |
| 15859 | ImVec2 board_min = GetCursorScreenPos(); |
| 15860 | ImVec2 board_max = ImVec2(board_min.x + 3 * key_step.x + 2 * key_row_offset + 10.0f, board_min.y + 3 * key_step.y + 10.0f); |
| 15861 | ImVec2 start_pos = ImVec2(board_min.x + 5.0f - key_step.x, board_min.y); |
| 15862 | |
| 15863 | struct KeyLayoutData { int Row, Col; const char* Label; ImGuiKey Key; }; |
| 15864 | const KeyLayoutData keys_to_display[] = |
| 15865 | { |
| 15866 | { 0, 0, "", ImGuiKey_Tab }, { 0, 1, "Q", ImGuiKey_Q }, { 0, 2, "W", ImGuiKey_W }, { 0, 3, "E", ImGuiKey_E }, { 0, 4, "R", ImGuiKey_R }, |
| 15867 | { 1, 0, "", ImGuiKey_CapsLock }, { 1, 1, "A", ImGuiKey_A }, { 1, 2, "S", ImGuiKey_S }, { 1, 3, "D", ImGuiKey_D }, { 1, 4, "F", ImGuiKey_F }, |
| 15868 | { 2, 0, "", ImGuiKey_LeftShift },{ 2, 1, "Z", ImGuiKey_Z }, { 2, 2, "X", ImGuiKey_X }, { 2, 3, "C", ImGuiKey_C }, { 2, 4, "V", ImGuiKey_V } |
| 15869 | }; |
| 15870 | |
| 15871 | // Elements rendered manually via ImDrawList API are not clipped automatically. |
| 15872 | // While not strictly necessary, here IsItemVisible() is used to avoid rendering these shapes when they are out of view. |
| 15873 | Dummy(board_max - board_min); |
| 15874 | if (!IsItemVisible()) |
| 15875 | return; |
| 15876 | draw_list->PushClipRect(board_min, board_max, true); |
| 15877 | for (int n = 0; n < IM_ARRAYSIZE(keys_to_display); n++) |
| 15878 | { |
| 15879 | const KeyLayoutData* key_data = &keys_to_display[n]; |
| 15880 | ImVec2 key_min = ImVec2(start_pos.x + key_data->Col * key_step.x + key_data->Row * key_row_offset, start_pos.y + key_data->Row * key_step.y); |
| 15881 | ImVec2 key_max = key_min + key_size; |
| 15882 | draw_list->AddRectFilled(key_min, key_max, IM_COL32(204, 204, 204, 255), key_rounding); |
| 15883 | draw_list->AddRect(key_min, key_max, IM_COL32(24, 24, 24, 255), key_rounding); |
| 15884 | ImVec2 face_min = ImVec2(key_min.x + key_face_pos.x, key_min.y + key_face_pos.y); |
| 15885 | ImVec2 face_max = ImVec2(face_min.x + key_face_size.x, face_min.y + key_face_size.y); |
| 15886 | draw_list->AddRect(face_min, face_max, IM_COL32(193, 193, 193, 255), key_face_rounding, ImDrawFlags_None, 2.0f); |
| 15887 | draw_list->AddRectFilled(face_min, face_max, IM_COL32(252, 252, 252, 255), key_face_rounding); |
| 15888 | ImVec2 label_min = ImVec2(key_min.x + key_label_pos.x, key_min.y + key_label_pos.y); |
| 15889 | draw_list->AddText(label_min, IM_COL32(64, 64, 64, 255), key_data->Label); |
| 15890 | if (IsKeyDown(key_data->Key)) |
| 15891 | draw_list->AddRectFilled(key_min, key_max, IM_COL32(255, 0, 0, 128), key_rounding); |
| 15892 | } |
| 15893 | draw_list->PopClipRect(); |
| 15894 | } |
| 15895 | |
| 15896 | // Helper tool to diagnose between text encoding issues and font loading issues. Pass your UTF-8 string and verify that there are correct. |
| 15897 | void ImGui::DebugTextEncoding(const char* str) |
nothing calls this directly
no test coverage detected