Skip to content

Commit a02300f

Browse files
committed
Add demo
1 parent 4a6f472 commit a02300f

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

imgui_demo.cpp

+92
Original file line numberDiff line numberDiff line change
@@ -3506,6 +3506,98 @@ static void ShowDemoWindowMisc()
35063506
ImGui::TreePop();
35073507
}
35083508

3509+
if (ImGui::TreeNode("Untranslated Key Input"))
3510+
{
3511+
static bool use_untranslated_keys = true;
3512+
3513+
ImGui::Text("HasUntranslatedKeys: %d", (io.BackendFlags & ImGuiBackednFlags_HasUntranslatedKeys) ? 1 : 0);
3514+
3515+
ImGui::Checkbox("Use untranslated keys", &use_untranslated_keys);
3516+
3517+
static const ImVec2 key_size = ImVec2(35.0f, 35.0f);
3518+
static const float key_rounding = 3.0f;
3519+
static const ImVec2 key_face_size = ImVec2(25.0f, 25.0f);
3520+
static const ImVec2 key_face_pos = ImVec2(5.0f, 3.0f);
3521+
static const float key_face_rounding = 2.0f;
3522+
static const ImVec2 key_label_pos = ImVec2(7.0f, 4.0f);
3523+
static const ImVec2 key_advance = ImVec2(key_size.x - 1.0f, key_size.y - 1.0f);
3524+
static const float key_row_offset = 9.0f;
3525+
3526+
struct Funcs
3527+
{
3528+
static void DrawKey(const ImVec2& pos, const char* label, int key)
3529+
{
3530+
ImDrawList* draw_list = ImGui::GetWindowDrawList();
3531+
ImVec2 key_min = pos;
3532+
ImVec2 key_max = ImVec2(key_min.x + key_size.x, key_min.y + key_size.y);
3533+
draw_list->AddRectFilled(key_min, key_max, IM_COL32(204, 204, 204, 255), key_rounding);
3534+
draw_list->AddRect(key_min, key_max, IM_COL32(24, 24, 24, 255), key_rounding);
3535+
ImVec2 face_min = ImVec2(key_min.x + key_face_pos.x, key_min.y + key_face_pos.y);
3536+
ImVec2 face_max = ImVec2(face_min.x + key_face_size.x, face_min.y + key_face_size.y);
3537+
draw_list->AddRect(face_min, face_max, IM_COL32(193, 193, 193, 255), key_face_rounding, ImDrawCornerFlags_All, 2.0f);
3538+
draw_list->AddRectFilled(face_min, face_max, IM_COL32(252, 252, 252, 255), key_face_rounding);
3539+
ImVec2 label_min = ImVec2(key_min.x + key_label_pos.x, key_min.y + key_label_pos.y);
3540+
draw_list->AddText(label_min, IM_COL32(64, 64, 64, 255), label);
3541+
if (key >= 0)
3542+
{
3543+
int key_index = use_untranslated_keys ? ImGui::GetUntranslatedKeyIndex(key) : ImGui::GetKeyIndex(key);
3544+
if (ImGui::IsKeyDown(key_index))
3545+
draw_list->AddRectFilled(key_min, key_max, IM_COL32(255, 0, 0, 128), key_rounding);
3546+
}
3547+
}
3548+
};
3549+
3550+
ImVec2 cursor_pos = ImGui::GetCursorScreenPos();
3551+
ImVec2 board_min = cursor_pos;
3552+
ImVec2 board_max = ImVec2(cursor_pos.x + 3 * key_advance.x + 2 * key_row_offset + 10.0f, cursor_pos.y + 3 * key_advance.y + 10.0f);
3553+
ImVec2 start_pos = ImVec2(board_min.x + 5.0f, board_min.y);
3554+
3555+
ImDrawList* draw_list = ImGui::GetWindowDrawList();
3556+
draw_list->PushClipRect(board_min, board_max, true);
3557+
3558+
ImVec2 key_pos = start_pos;
3559+
key_pos.x -= key_advance.x;
3560+
Funcs::DrawKey(key_pos, "", ImGuiKey_Tab);
3561+
key_pos.x += key_advance.x;
3562+
Funcs::DrawKey(key_pos, "Q", ImGuiKey_Q);
3563+
key_pos.x += key_advance.x;
3564+
Funcs::DrawKey(key_pos, "W", ImGuiKey_W);
3565+
key_pos.x += key_advance.x;
3566+
Funcs::DrawKey(key_pos, "E", ImGuiKey_E);
3567+
key_pos.x += key_advance.x;
3568+
Funcs::DrawKey(key_pos, "R", ImGuiKey_R);
3569+
key_pos.x = start_pos.x + key_row_offset;
3570+
key_pos.y += key_advance.y;
3571+
key_pos.x -= key_advance.x;
3572+
Funcs::DrawKey(key_pos, "", -1);
3573+
key_pos.x += key_advance.x;
3574+
Funcs::DrawKey(key_pos, "A", ImGuiKey_A);
3575+
key_pos.x += key_advance.x;
3576+
Funcs::DrawKey(key_pos, "S", ImGuiKey_S);
3577+
key_pos.x += key_advance.x;
3578+
Funcs::DrawKey(key_pos, "D", ImGuiKey_D);
3579+
key_pos.x += key_advance.x;
3580+
Funcs::DrawKey(key_pos, "F", ImGuiKey_F);
3581+
key_pos.x = start_pos.x + key_row_offset * 2;
3582+
key_pos.y += key_advance.y;
3583+
key_pos.x -= key_advance.x;
3584+
Funcs::DrawKey(key_pos, "", -1);
3585+
key_pos.x += key_advance.x;
3586+
Funcs::DrawKey(key_pos, "Z", ImGuiKey_Z);
3587+
key_pos.x += key_advance.x;
3588+
Funcs::DrawKey(key_pos, "X", ImGuiKey_X);
3589+
key_pos.x += key_advance.x;
3590+
Funcs::DrawKey(key_pos, "C", ImGuiKey_C);
3591+
key_pos.x += key_advance.x;
3592+
Funcs::DrawKey(key_pos, "V", ImGuiKey_V);
3593+
3594+
draw_list->PopClipRect();
3595+
3596+
ImGui::Dummy(ImVec2(board_max.x - board_min.x, board_max.y - board_min.y));
3597+
3598+
ImGui::TreePop();
3599+
}
3600+
35093601
if (ImGui::TreeNode("Tabbing"))
35103602
{
35113603
ImGui::Text("Use TAB/SHIFT+TAB to cycle through keyboard editable fields.");

0 commit comments

Comments
 (0)