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

Support ImGui 1.91.5 and greater #1872

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
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
146 changes: 114 additions & 32 deletions dart/gui/osg/ImGuiHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@ namespace dart {
namespace gui {
namespace osg {

//==============================================================================
#if IMGUI_VERSION_NUM < 19150

// Special keys that are usually greater than 512 in osgGA
//
// Imporant Note: Dear ImGui expects the control Keys indices not to be greater
// Important Note: Dear ImGui expects the control Keys indices not to be greater
// thant 511. It actually uses an array of 512 elements. However, OSG has
// indices greater than that. So here I do a conversion for special keys between
// ImGui and OSG.
enum ConvertedKey : int
{
ConvertedKey_None = -1,
ConvertedKey_Tab = 257,
ConvertedKey_Left,
ConvertedKey_Right,
Expand All @@ -83,14 +85,74 @@ enum ConvertedKey : int
ConvertedKey_RightSuper,
};

#endif

//==============================================================================
// Check for a special key and return the converted code (range [257, 511]) if
// so. Otherwise returns -1
int convertFromOSGKey(int key)
#if IMGUI_VERSION_NUM >= 19150
ImGuiKey convertFromOSGKey(int key)
#else
ConvertedKey convertFromOSGKey(int key)
#endif
{
using KeySymbol = osgGA::GUIEventAdapter::KeySymbol;

switch (key) {
#if IMGUI_VERSION_NUM >= 19150
case KeySymbol::KEY_Tab:
return ImGuiKey_Tab;
case KeySymbol::KEY_Left:
return ImGuiKey_LeftArrow;
case KeySymbol::KEY_Right:
return ImGuiKey_RightArrow;
case KeySymbol::KEY_Up:
return ImGuiKey_UpArrow;
case KeySymbol::KEY_Down:
return ImGuiKey_DownArrow;
case KeySymbol::KEY_Page_Up:
return ImGuiKey_PageUp;
case KeySymbol::KEY_Page_Down:
return ImGuiKey_PageDown;
case KeySymbol::KEY_Home:
return ImGuiKey_Home;
case KeySymbol::KEY_End:
return ImGuiKey_End;
case KeySymbol::KEY_Delete:
return ImGuiKey_Delete;
case KeySymbol::KEY_BackSpace:
return ImGuiKey_Backspace;
case KeySymbol::KEY_Return:
return ImGuiKey_Enter;
case KeySymbol::KEY_Escape:
return ImGuiKey_Escape;
case KeySymbol::KEY_Control_L:
case KeySymbol::KEY_Control_R:
return ImGuiKey_ModCtrl;
case KeySymbol::KEY_Shift_L:
case KeySymbol::KEY_Shift_R:
return ImGuiKey_ModShift;
case KeySymbol::KEY_Alt_L:
case KeySymbol::KEY_Alt_R:
return ImGuiKey_ModAlt;
case KeySymbol::KEY_Super_L:
case KeySymbol::KEY_Super_R:
return ImGuiKey_ModSuper;
case KeySymbol::KEY_A:
return ImGuiKey_A;
case KeySymbol::KEY_C:
return ImGuiKey_C;
case KeySymbol::KEY_V:
return ImGuiKey_V;
case KeySymbol::KEY_X:
return ImGuiKey_X;
case KeySymbol::KEY_Y:
return ImGuiKey_Y;
case KeySymbol::KEY_Z:
return ImGuiKey_Z;
default:
return ImGuiKey_None;
#else
case KeySymbol::KEY_Tab:
return ConvertedKey_Tab;
case KeySymbol::KEY_Left:
Expand Down Expand Up @@ -134,7 +196,8 @@ int convertFromOSGKey(int key)
case KeySymbol::KEY_Super_R:
return ConvertedKey_RightSuper;
default:
return -1;
return ConvertedKey_None;
#endif
}
}

Expand Down Expand Up @@ -182,6 +245,7 @@ ImGuiHandler::ImGuiHandler()

ImGui_ImplOpenGL2_Init();

#if IMGUI_VERSION_NUM < 19150
// Keyboard mapping. ImGui will use those indices to peek into the
// io.KeyDown[] array.
ImGuiIO& io = ImGui::GetIO();
Expand All @@ -204,6 +268,7 @@ ImGuiHandler::ImGuiHandler()
io.KeyMap[ImGuiKey_X] = osgGA::GUIEventAdapter::KeySymbol::KEY_X;
io.KeyMap[ImGuiKey_Y] = osgGA::GUIEventAdapter::KeySymbol::KEY_Y;
io.KeyMap[ImGuiKey_Z] = osgGA::GUIEventAdapter::KeySymbol::KEY_Z;
#endif
}

//==============================================================================
Expand Down Expand Up @@ -272,22 +337,31 @@ bool ImGuiHandler::handle(
::osg::Object* /*object*/,
::osg::NodeVisitor* /*nodeVisitor*/)
{
auto& io = ImGui::GetIO();
const auto wantCapureMouse = io.WantCaptureMouse;
const auto wantCapureKeyboard = io.WantCaptureKeyboard;
ImGuiIO& io = ImGui::GetIO();
const bool wantCaptureMouse = io.WantCaptureMouse;
const bool wantCaptureKeyboard = io.WantCaptureKeyboard;

switch (eventAdapter.getEventType()) {
case osgGA::GUIEventAdapter::KEYDOWN: {
const auto c = eventAdapter.getUnmodifiedKey();
const auto special_key = convertFromOSGKey(c);

if (special_key > 0) {
assert(special_key < 512 && "ImGui KeysDown is an array of 512");
const int key = eventAdapter.getUnmodifiedKey();

#if IMGUI_VERSION_NUM >= 19150
const ImGuiKey specialKey = convertFromOSGKey(key);
if (specialKey != ImGuiKey_None) {
io.AddKeyEvent(specialKey, true);
} else if (key != 0 && key < 0x10000) {
const ImWchar c = static_cast<ImWchar>(eventAdapter.getKey());
io.AddInputCharacter(c);
}
#else
const ConvertedKey specialKey = convertFromOSGKey(key);
if (specialKey != ConvertedKey_None) {
assert(specialKey < 512 && "ImGui KeysDown is an array of 512");
assert(
special_key > 256
specialKey > 256
&& "ASCII stop at 127, but we use the range [257, 511]");

io.KeysDown[special_key] = true;
io.KeysDown[specialKey] = true;

io.KeyCtrl = io.KeysDown[ConvertedKey_LeftControl]
|| io.KeysDown[ConvertedKey_RightControl];
Expand All @@ -297,24 +371,31 @@ bool ImGuiHandler::handle(
|| io.KeysDown[ConvertedKey_RightAlt];
io.KeySuper = io.KeysDown[ConvertedKey_LeftSuper]
|| io.KeysDown[ConvertedKey_RightSuper];
} else if (0 < c && c < 0x10000) {
io.KeysDown[c] = true;
io.AddInputCharacter(static_cast<ImWchar>(c));
} else if (0 < key && key < 0x10000) {
io.KeysDown[key] = true;
io.AddInputCharacter(static_cast<ImWchar>(key));
}
#endif

return wantCapureKeyboard;
return wantCaptureKeyboard;
}
case osgGA::GUIEventAdapter::KEYUP: {
const auto c = eventAdapter.getUnmodifiedKey();
const auto special_key = convertFromOSGKey(c);
const int key = eventAdapter.getUnmodifiedKey();

if (special_key > 0) {
assert(special_key < 512 && "ImGui KeysDown is an array of 512");
#if IMGUI_VERSION_NUM >= 19150
const ImGuiKey specialKey = convertFromOSGKey(key);
if (specialKey != ImGuiKey_None) {
io.AddKeyEvent(specialKey, false);
}
#else
const ConvertedKey specialKey = convertFromOSGKey(key);
if (specialKey != ConvertedKey_None) {
assert(specialKey < 512 && "ImGui KeysDown is an array of 512");
assert(
special_key > 256
specialKey > 256
&& "ASCII stop at 127, but we use the range [257, 511]");

io.KeysDown[special_key] = false;
io.KeysDown[specialKey] = false;

io.KeyCtrl = io.KeysDown[ConvertedKey_LeftControl]
|| io.KeysDown[ConvertedKey_RightControl];
Expand All @@ -324,12 +405,13 @@ bool ImGuiHandler::handle(
|| io.KeysDown[ConvertedKey_RightAlt];
io.KeySuper = io.KeysDown[ConvertedKey_LeftSuper]
|| io.KeysDown[ConvertedKey_RightSuper];
} else if (0 < c && c < 0x10000) {
io.KeysDown[c] = false;
io.AddInputCharacter(static_cast<ImWchar>(c));
} else if (0 < key && key < 0x10000) {
io.KeysDown[key] = false;
io.AddInputCharacter(static_cast<ImWchar>(key));
}
#endif

return wantCapureKeyboard;
return wantCaptureKeyboard;
}
case osgGA::GUIEventAdapter::PUSH: {
io.MousePos
Expand All @@ -351,14 +433,14 @@ bool ImGuiHandler::handle(
mMousePressed[0] = true;
}

return wantCapureMouse;
return wantCaptureMouse;
}
case osgGA::GUIEventAdapter::DRAG:
case osgGA::GUIEventAdapter::MOVE: {
io.MousePos
= ImVec2(eventAdapter.getX(), io.DisplaySize.y - eventAdapter.getY());

return wantCapureMouse;
return wantCaptureMouse;
}
case osgGA::GUIEventAdapter::RELEASE: {
// When a mouse button is released no button mask is set. So we mark all
Expand All @@ -367,7 +449,7 @@ bool ImGuiHandler::handle(
mMousePressed[1] = false;
mMousePressed[2] = false;

return wantCapureMouse;
return wantCaptureMouse;
}
case osgGA::GUIEventAdapter::SCROLL: {
constexpr float increment = 0.1f;
Expand All @@ -390,7 +472,7 @@ bool ImGuiHandler::handle(
break;
}

return wantCapureMouse;
return wantCaptureMouse;
}
default: {
return false;
Expand Down
Loading
Loading