Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to imgui IO event API #2726

Merged
merged 1 commit into from
Feb 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 118 additions & 47 deletions examples/common/imgui/imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,49 +215,116 @@ struct OcornutImguiContext
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset;

#if USE_ENTRY
io.KeyMap[ImGuiKey_Tab] = (int)entry::Key::Tab;
io.KeyMap[ImGuiKey_LeftArrow] = (int)entry::Key::Left;
io.KeyMap[ImGuiKey_RightArrow] = (int)entry::Key::Right;
io.KeyMap[ImGuiKey_UpArrow] = (int)entry::Key::Up;
io.KeyMap[ImGuiKey_DownArrow] = (int)entry::Key::Down;
io.KeyMap[ImGuiKey_PageUp] = (int)entry::Key::PageUp;
io.KeyMap[ImGuiKey_PageDown] = (int)entry::Key::PageDown;
io.KeyMap[ImGuiKey_Home] = (int)entry::Key::Home;
io.KeyMap[ImGuiKey_End] = (int)entry::Key::End;
io.KeyMap[ImGuiKey_Insert] = (int)entry::Key::Insert;
io.KeyMap[ImGuiKey_Delete] = (int)entry::Key::Delete;
io.KeyMap[ImGuiKey_Backspace] = (int)entry::Key::Backspace;
io.KeyMap[ImGuiKey_Space] = (int)entry::Key::Space;
io.KeyMap[ImGuiKey_Enter] = (int)entry::Key::Return;
io.KeyMap[ImGuiKey_Escape] = (int)entry::Key::Esc;
io.KeyMap[ImGuiKey_A] = (int)entry::Key::KeyA;
io.KeyMap[ImGuiKey_C] = (int)entry::Key::KeyC;
io.KeyMap[ImGuiKey_V] = (int)entry::Key::KeyV;
io.KeyMap[ImGuiKey_X] = (int)entry::Key::KeyX;
io.KeyMap[ImGuiKey_Y] = (int)entry::Key::KeyY;
io.KeyMap[ImGuiKey_Z] = (int)entry::Key::KeyZ;
for (int32_t ii = 0; ii < (int32_t)entry::Key::Count; ++ii)
{
m_keyMap[ii] = ImGuiKey_None;
}

m_keyMap[(int)entry::Key::Esc] = ImGuiKey_Escape;
m_keyMap[(int)entry::Key::Return] = ImGuiKey_Enter;
m_keyMap[(int)entry::Key::Tab] = ImGuiKey_Tab;
m_keyMap[(int)entry::Key::Space] = ImGuiKey_Space;
m_keyMap[(int)entry::Key::Backspace] = ImGuiKey_Backspace;
m_keyMap[(int)entry::Key::Up] = ImGuiKey_UpArrow;
m_keyMap[(int)entry::Key::Down] = ImGuiKey_DownArrow;
m_keyMap[(int)entry::Key::Left] = ImGuiKey_LeftArrow;
m_keyMap[(int)entry::Key::Right] = ImGuiKey_RightArrow;
m_keyMap[(int)entry::Key::Insert] = ImGuiKey_Insert;
m_keyMap[(int)entry::Key::Delete] = ImGuiKey_Delete;
m_keyMap[(int)entry::Key::Home] = ImGuiKey_Home;
m_keyMap[(int)entry::Key::End] = ImGuiKey_End;
m_keyMap[(int)entry::Key::PageUp] = ImGuiKey_PageUp;
m_keyMap[(int)entry::Key::PageDown] = ImGuiKey_PageDown;
m_keyMap[(int)entry::Key::Print] = ImGuiKey_PrintScreen;
m_keyMap[(int)entry::Key::Plus] = ImGuiKey_Equal;
m_keyMap[(int)entry::Key::Minus] = ImGuiKey_Minus;
m_keyMap[(int)entry::Key::LeftBracket] = ImGuiKey_LeftBracket;
m_keyMap[(int)entry::Key::RightBracket] = ImGuiKey_RightBracket;
m_keyMap[(int)entry::Key::Semicolon] = ImGuiKey_Semicolon;
m_keyMap[(int)entry::Key::Quote] = ImGuiKey_Apostrophe;
m_keyMap[(int)entry::Key::Comma] = ImGuiKey_Comma;
m_keyMap[(int)entry::Key::Period] = ImGuiKey_Period;
m_keyMap[(int)entry::Key::Slash] = ImGuiKey_Slash;
m_keyMap[(int)entry::Key::Backslash] = ImGuiKey_Backslash;
m_keyMap[(int)entry::Key::Tilde] = ImGuiKey_GraveAccent;
m_keyMap[(int)entry::Key::F1] = ImGuiKey_F1;
m_keyMap[(int)entry::Key::F2] = ImGuiKey_F2;
m_keyMap[(int)entry::Key::F3] = ImGuiKey_F3;
m_keyMap[(int)entry::Key::F4] = ImGuiKey_F4;
m_keyMap[(int)entry::Key::F5] = ImGuiKey_F5;
m_keyMap[(int)entry::Key::F6] = ImGuiKey_F6;
m_keyMap[(int)entry::Key::F7] = ImGuiKey_F7;
m_keyMap[(int)entry::Key::F8] = ImGuiKey_F8;
m_keyMap[(int)entry::Key::F9] = ImGuiKey_F9;
m_keyMap[(int)entry::Key::F10] = ImGuiKey_F10;
m_keyMap[(int)entry::Key::F11] = ImGuiKey_F11;
m_keyMap[(int)entry::Key::F12] = ImGuiKey_F12;
m_keyMap[(int)entry::Key::NumPad0] = ImGuiKey_Keypad0;
m_keyMap[(int)entry::Key::NumPad1] = ImGuiKey_Keypad1;
m_keyMap[(int)entry::Key::NumPad2] = ImGuiKey_Keypad2;
m_keyMap[(int)entry::Key::NumPad3] = ImGuiKey_Keypad3;
m_keyMap[(int)entry::Key::NumPad4] = ImGuiKey_Keypad4;
m_keyMap[(int)entry::Key::NumPad5] = ImGuiKey_Keypad5;
m_keyMap[(int)entry::Key::NumPad6] = ImGuiKey_Keypad6;
m_keyMap[(int)entry::Key::NumPad7] = ImGuiKey_Keypad7;
m_keyMap[(int)entry::Key::NumPad8] = ImGuiKey_Keypad8;
m_keyMap[(int)entry::Key::NumPad9] = ImGuiKey_Keypad9;
m_keyMap[(int)entry::Key::Key0] = ImGuiKey_0;
m_keyMap[(int)entry::Key::Key1] = ImGuiKey_1;
m_keyMap[(int)entry::Key::Key2] = ImGuiKey_2;
m_keyMap[(int)entry::Key::Key3] = ImGuiKey_3;
m_keyMap[(int)entry::Key::Key4] = ImGuiKey_4;
m_keyMap[(int)entry::Key::Key5] = ImGuiKey_5;
m_keyMap[(int)entry::Key::Key6] = ImGuiKey_6;
m_keyMap[(int)entry::Key::Key7] = ImGuiKey_7;
m_keyMap[(int)entry::Key::Key8] = ImGuiKey_8;
m_keyMap[(int)entry::Key::Key9] = ImGuiKey_9;
m_keyMap[(int)entry::Key::KeyA] = ImGuiKey_A;
m_keyMap[(int)entry::Key::KeyB] = ImGuiKey_B;
m_keyMap[(int)entry::Key::KeyC] = ImGuiKey_C;
m_keyMap[(int)entry::Key::KeyD] = ImGuiKey_D;
m_keyMap[(int)entry::Key::KeyE] = ImGuiKey_E;
m_keyMap[(int)entry::Key::KeyF] = ImGuiKey_F;
m_keyMap[(int)entry::Key::KeyG] = ImGuiKey_G;
m_keyMap[(int)entry::Key::KeyH] = ImGuiKey_H;
m_keyMap[(int)entry::Key::KeyI] = ImGuiKey_I;
m_keyMap[(int)entry::Key::KeyJ] = ImGuiKey_J;
m_keyMap[(int)entry::Key::KeyK] = ImGuiKey_K;
m_keyMap[(int)entry::Key::KeyL] = ImGuiKey_L;
m_keyMap[(int)entry::Key::KeyM] = ImGuiKey_M;
m_keyMap[(int)entry::Key::KeyN] = ImGuiKey_N;
m_keyMap[(int)entry::Key::KeyO] = ImGuiKey_O;
m_keyMap[(int)entry::Key::KeyP] = ImGuiKey_P;
m_keyMap[(int)entry::Key::KeyQ] = ImGuiKey_Q;
m_keyMap[(int)entry::Key::KeyR] = ImGuiKey_R;
m_keyMap[(int)entry::Key::KeyS] = ImGuiKey_S;
m_keyMap[(int)entry::Key::KeyT] = ImGuiKey_T;
m_keyMap[(int)entry::Key::KeyU] = ImGuiKey_U;
m_keyMap[(int)entry::Key::KeyV] = ImGuiKey_V;
m_keyMap[(int)entry::Key::KeyW] = ImGuiKey_W;
m_keyMap[(int)entry::Key::KeyX] = ImGuiKey_X;
m_keyMap[(int)entry::Key::KeyY] = ImGuiKey_Y;
m_keyMap[(int)entry::Key::KeyZ] = ImGuiKey_Z;

io.ConfigFlags |= 0
| ImGuiConfigFlags_NavEnableGamepad
| ImGuiConfigFlags_NavEnableKeyboard
;

io.NavInputs[ImGuiNavInput_Activate] = (int)entry::Key::GamepadA;
io.NavInputs[ImGuiNavInput_Cancel] = (int)entry::Key::GamepadB;
// io.NavInputs[ImGuiNavInput_Input] = (int)entry::Key::;
// io.NavInputs[ImGuiNavInput_Menu] = (int)entry::Key::;
io.NavInputs[ImGuiNavInput_DpadLeft] = (int)entry::Key::GamepadLeft;
io.NavInputs[ImGuiNavInput_DpadRight] = (int)entry::Key::GamepadRight;
io.NavInputs[ImGuiNavInput_DpadUp] = (int)entry::Key::GamepadUp;
io.NavInputs[ImGuiNavInput_DpadDown] = (int)entry::Key::GamepadDown;
// io.NavInputs[ImGuiNavInput_LStickLeft] = (int)entry::Key::;
// io.NavInputs[ImGuiNavInput_LStickRight] = (int)entry::Key::;
// io.NavInputs[ImGuiNavInput_LStickUp] = (int)entry::Key::;
// io.NavInputs[ImGuiNavInput_LStickDown] = (int)entry::Key::;
// io.NavInputs[ImGuiNavInput_FocusPrev] = (int)entry::Key::;
// io.NavInputs[ImGuiNavInput_FocusNext] = (int)entry::Key::;
// io.NavInputs[ImGuiNavInput_TweakSlow] = (int)entry::Key::;
// io.NavInputs[ImGuiNavInput_TweakFast] = (int)entry::Key::;
m_keyMap[(int)entry::Key::GamepadStart] = ImGuiKey_GamepadStart;
m_keyMap[(int)entry::Key::GamepadBack] = ImGuiKey_GamepadBack;
m_keyMap[(int)entry::Key::GamepadY] = ImGuiKey_GamepadFaceUp;
m_keyMap[(int)entry::Key::GamepadA] = ImGuiKey_GamepadFaceDown;
m_keyMap[(int)entry::Key::GamepadX] = ImGuiKey_GamepadFaceLeft;
m_keyMap[(int)entry::Key::GamepadB] = ImGuiKey_GamepadFaceRight;
m_keyMap[(int)entry::Key::GamepadUp] = ImGuiKey_GamepadDpadUp;
m_keyMap[(int)entry::Key::GamepadDown] = ImGuiKey_GamepadDpadDown;
m_keyMap[(int)entry::Key::GamepadLeft] = ImGuiKey_GamepadDpadLeft;
m_keyMap[(int)entry::Key::GamepadRight] = ImGuiKey_GamepadDpadRight;
m_keyMap[(int)entry::Key::GamepadShoulderL] = ImGuiKey_GamepadL1;
m_keyMap[(int)entry::Key::GamepadShoulderR] = ImGuiKey_GamepadR1;
m_keyMap[(int)entry::Key::GamepadThumbL] = ImGuiKey_GamepadL3;
m_keyMap[(int)entry::Key::GamepadThumbR] = ImGuiKey_GamepadR3;
#endif // USE_ENTRY

bgfx::RendererType::Enum type = bgfx::getRendererType();
Expand Down Expand Up @@ -387,22 +454,23 @@ struct OcornutImguiContext
const double freq = double(bx::getHPFrequency() );
io.DeltaTime = float(frameTime/freq);

io.MousePos = ImVec2( (float)_mx, (float)_my);
io.MouseDown[0] = 0 != (_button & IMGUI_MBUT_LEFT);
io.MouseDown[1] = 0 != (_button & IMGUI_MBUT_RIGHT);
io.MouseDown[2] = 0 != (_button & IMGUI_MBUT_MIDDLE);
io.MouseWheel = (float)(_scroll - m_lastScroll);
io.AddMousePosEvent( (float)_mx, (float)_my);
io.AddMouseButtonEvent(ImGuiMouseButton_Left, 0 != (_button & IMGUI_MBUT_LEFT ) );
io.AddMouseButtonEvent(ImGuiMouseButton_Right, 0 != (_button & IMGUI_MBUT_RIGHT ) );
io.AddMouseButtonEvent(ImGuiMouseButton_Middle, 0 != (_button & IMGUI_MBUT_MIDDLE) );
io.AddMouseWheelEvent(0.0f, (float)(_scroll - m_lastScroll) );
m_lastScroll = _scroll;

#if USE_ENTRY
uint8_t modifiers = inputGetModifiersState();
io.KeyShift = 0 != (modifiers & (entry::Modifier::LeftShift | entry::Modifier::RightShift) );
io.KeyCtrl = 0 != (modifiers & (entry::Modifier::LeftCtrl | entry::Modifier::RightCtrl ) );
io.KeyAlt = 0 != (modifiers & (entry::Modifier::LeftAlt | entry::Modifier::RightAlt ) );
io.KeySuper = 0 != (modifiers & (entry::Modifier::LeftMeta | entry::Modifier::RightMeta ) );
io.AddKeyEvent(ImGuiKey_ModShift, 0 != (modifiers & (entry::Modifier::LeftShift | entry::Modifier::RightShift) ) );
io.AddKeyEvent(ImGuiKey_ModCtrl, 0 != (modifiers & (entry::Modifier::LeftCtrl | entry::Modifier::RightCtrl ) ) );
io.AddKeyEvent(ImGuiKey_ModAlt, 0 != (modifiers & (entry::Modifier::LeftAlt | entry::Modifier::RightAlt ) ) );
io.AddKeyEvent(ImGuiKey_ModSuper, 0 != (modifiers & (entry::Modifier::LeftMeta | entry::Modifier::RightMeta ) ) );
for (int32_t ii = 0; ii < (int32_t)entry::Key::Count; ++ii)
{
io.KeysDown[ii] = inputGetKeyState(entry::Key::Enum(ii) );
io.AddKeyEvent(m_keyMap[ii], inputGetKeyState(entry::Key::Enum(ii) ) );
io.SetKeyEventNativeData(m_keyMap[ii], 0, 0, ii);
}
#endif // USE_ENTRY

Expand All @@ -429,6 +497,9 @@ struct OcornutImguiContext
int64_t m_last;
int32_t m_lastScroll;
bgfx::ViewId m_viewId;
#if USE_ENTRY
ImGuiKey m_keyMap[(int)entry::Key::Count];
#endif // USE_ENTRY
};

static OcornutImguiContext s_ctx;
Expand Down