Skip to content

Commit 4b5b6a8

Browse files
authored
Support ImGui 1.91.5 and greater (#1872)
1 parent ad4e80e commit 4b5b6a8

File tree

3 files changed

+696
-589
lines changed

3 files changed

+696
-589
lines changed

dart/gui/osg/ImGuiHandler.cpp

Lines changed: 114 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,17 @@ namespace dart {
5151
namespace gui {
5252
namespace osg {
5353

54-
//==============================================================================
54+
#if IMGUI_VERSION_NUM < 19150
55+
5556
// Special keys that are usually greater than 512 in osgGA
5657
//
57-
// Imporant Note: Dear ImGui expects the control Keys indices not to be greater
58+
// Important Note: Dear ImGui expects the control Keys indices not to be greater
5859
// thant 511. It actually uses an array of 512 elements. However, OSG has
5960
// indices greater than that. So here I do a conversion for special keys between
6061
// ImGui and OSG.
6162
enum ConvertedKey : int
6263
{
64+
ConvertedKey_None = -1,
6365
ConvertedKey_Tab = 257,
6466
ConvertedKey_Left,
6567
ConvertedKey_Right,
@@ -83,14 +85,74 @@ enum ConvertedKey : int
8385
ConvertedKey_RightSuper,
8486
};
8587

88+
#endif
89+
8690
//==============================================================================
8791
// Check for a special key and return the converted code (range [257, 511]) if
8892
// so. Otherwise returns -1
89-
int convertFromOSGKey(int key)
93+
#if IMGUI_VERSION_NUM >= 19150
94+
ImGuiKey convertFromOSGKey(int key)
95+
#else
96+
ConvertedKey convertFromOSGKey(int key)
97+
#endif
9098
{
9199
using KeySymbol = osgGA::GUIEventAdapter::KeySymbol;
92100

93101
switch (key) {
102+
#if IMGUI_VERSION_NUM >= 19150
103+
case KeySymbol::KEY_Tab:
104+
return ImGuiKey_Tab;
105+
case KeySymbol::KEY_Left:
106+
return ImGuiKey_LeftArrow;
107+
case KeySymbol::KEY_Right:
108+
return ImGuiKey_RightArrow;
109+
case KeySymbol::KEY_Up:
110+
return ImGuiKey_UpArrow;
111+
case KeySymbol::KEY_Down:
112+
return ImGuiKey_DownArrow;
113+
case KeySymbol::KEY_Page_Up:
114+
return ImGuiKey_PageUp;
115+
case KeySymbol::KEY_Page_Down:
116+
return ImGuiKey_PageDown;
117+
case KeySymbol::KEY_Home:
118+
return ImGuiKey_Home;
119+
case KeySymbol::KEY_End:
120+
return ImGuiKey_End;
121+
case KeySymbol::KEY_Delete:
122+
return ImGuiKey_Delete;
123+
case KeySymbol::KEY_BackSpace:
124+
return ImGuiKey_Backspace;
125+
case KeySymbol::KEY_Return:
126+
return ImGuiKey_Enter;
127+
case KeySymbol::KEY_Escape:
128+
return ImGuiKey_Escape;
129+
case KeySymbol::KEY_Control_L:
130+
case KeySymbol::KEY_Control_R:
131+
return ImGuiKey_ModCtrl;
132+
case KeySymbol::KEY_Shift_L:
133+
case KeySymbol::KEY_Shift_R:
134+
return ImGuiKey_ModShift;
135+
case KeySymbol::KEY_Alt_L:
136+
case KeySymbol::KEY_Alt_R:
137+
return ImGuiKey_ModAlt;
138+
case KeySymbol::KEY_Super_L:
139+
case KeySymbol::KEY_Super_R:
140+
return ImGuiKey_ModSuper;
141+
case KeySymbol::KEY_A:
142+
return ImGuiKey_A;
143+
case KeySymbol::KEY_C:
144+
return ImGuiKey_C;
145+
case KeySymbol::KEY_V:
146+
return ImGuiKey_V;
147+
case KeySymbol::KEY_X:
148+
return ImGuiKey_X;
149+
case KeySymbol::KEY_Y:
150+
return ImGuiKey_Y;
151+
case KeySymbol::KEY_Z:
152+
return ImGuiKey_Z;
153+
default:
154+
return ImGuiKey_None;
155+
#else
94156
case KeySymbol::KEY_Tab:
95157
return ConvertedKey_Tab;
96158
case KeySymbol::KEY_Left:
@@ -134,7 +196,8 @@ int convertFromOSGKey(int key)
134196
case KeySymbol::KEY_Super_R:
135197
return ConvertedKey_RightSuper;
136198
default:
137-
return -1;
199+
return ConvertedKey_None;
200+
#endif
138201
}
139202
}
140203

@@ -182,6 +245,7 @@ ImGuiHandler::ImGuiHandler()
182245

183246
ImGui_ImplOpenGL2_Init();
184247

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

209274
//==============================================================================
@@ -272,22 +337,31 @@ bool ImGuiHandler::handle(
272337
::osg::Object* /*object*/,
273338
::osg::NodeVisitor* /*nodeVisitor*/)
274339
{
275-
auto& io = ImGui::GetIO();
276-
const auto wantCapureMouse = io.WantCaptureMouse;
277-
const auto wantCapureKeyboard = io.WantCaptureKeyboard;
340+
ImGuiIO& io = ImGui::GetIO();
341+
const bool wantCaptureMouse = io.WantCaptureMouse;
342+
const bool wantCaptureKeyboard = io.WantCaptureKeyboard;
278343

279344
switch (eventAdapter.getEventType()) {
280345
case osgGA::GUIEventAdapter::KEYDOWN: {
281-
const auto c = eventAdapter.getUnmodifiedKey();
282-
const auto special_key = convertFromOSGKey(c);
283-
284-
if (special_key > 0) {
285-
assert(special_key < 512 && "ImGui KeysDown is an array of 512");
346+
const int key = eventAdapter.getUnmodifiedKey();
347+
348+
#if IMGUI_VERSION_NUM >= 19150
349+
const ImGuiKey specialKey = convertFromOSGKey(key);
350+
if (specialKey != ImGuiKey_None) {
351+
io.AddKeyEvent(specialKey, true);
352+
} else if (key != 0 && key < 0x10000) {
353+
const ImWchar c = static_cast<ImWchar>(eventAdapter.getKey());
354+
io.AddInputCharacter(c);
355+
}
356+
#else
357+
const ConvertedKey specialKey = convertFromOSGKey(key);
358+
if (specialKey != ConvertedKey_None) {
359+
assert(specialKey < 512 && "ImGui KeysDown is an array of 512");
286360
assert(
287-
special_key > 256
361+
specialKey > 256
288362
&& "ASCII stop at 127, but we use the range [257, 511]");
289363

290-
io.KeysDown[special_key] = true;
364+
io.KeysDown[specialKey] = true;
291365

292366
io.KeyCtrl = io.KeysDown[ConvertedKey_LeftControl]
293367
|| io.KeysDown[ConvertedKey_RightControl];
@@ -297,24 +371,31 @@ bool ImGuiHandler::handle(
297371
|| io.KeysDown[ConvertedKey_RightAlt];
298372
io.KeySuper = io.KeysDown[ConvertedKey_LeftSuper]
299373
|| io.KeysDown[ConvertedKey_RightSuper];
300-
} else if (0 < c && c < 0x10000) {
301-
io.KeysDown[c] = true;
302-
io.AddInputCharacter(static_cast<ImWchar>(c));
374+
} else if (0 < key && key < 0x10000) {
375+
io.KeysDown[key] = true;
376+
io.AddInputCharacter(static_cast<ImWchar>(key));
303377
}
378+
#endif
304379

305-
return wantCapureKeyboard;
380+
return wantCaptureKeyboard;
306381
}
307382
case osgGA::GUIEventAdapter::KEYUP: {
308-
const auto c = eventAdapter.getUnmodifiedKey();
309-
const auto special_key = convertFromOSGKey(c);
383+
const int key = eventAdapter.getUnmodifiedKey();
310384

311-
if (special_key > 0) {
312-
assert(special_key < 512 && "ImGui KeysDown is an array of 512");
385+
#if IMGUI_VERSION_NUM >= 19150
386+
const ImGuiKey specialKey = convertFromOSGKey(key);
387+
if (specialKey != ImGuiKey_None) {
388+
io.AddKeyEvent(specialKey, false);
389+
}
390+
#else
391+
const ConvertedKey specialKey = convertFromOSGKey(key);
392+
if (specialKey != ConvertedKey_None) {
393+
assert(specialKey < 512 && "ImGui KeysDown is an array of 512");
313394
assert(
314-
special_key > 256
395+
specialKey > 256
315396
&& "ASCII stop at 127, but we use the range [257, 511]");
316397

317-
io.KeysDown[special_key] = false;
398+
io.KeysDown[specialKey] = false;
318399

319400
io.KeyCtrl = io.KeysDown[ConvertedKey_LeftControl]
320401
|| io.KeysDown[ConvertedKey_RightControl];
@@ -324,12 +405,13 @@ bool ImGuiHandler::handle(
324405
|| io.KeysDown[ConvertedKey_RightAlt];
325406
io.KeySuper = io.KeysDown[ConvertedKey_LeftSuper]
326407
|| io.KeysDown[ConvertedKey_RightSuper];
327-
} else if (0 < c && c < 0x10000) {
328-
io.KeysDown[c] = false;
329-
io.AddInputCharacter(static_cast<ImWchar>(c));
408+
} else if (0 < key && key < 0x10000) {
409+
io.KeysDown[key] = false;
410+
io.AddInputCharacter(static_cast<ImWchar>(key));
330411
}
412+
#endif
331413

332-
return wantCapureKeyboard;
414+
return wantCaptureKeyboard;
333415
}
334416
case osgGA::GUIEventAdapter::PUSH: {
335417
io.MousePos
@@ -351,14 +433,14 @@ bool ImGuiHandler::handle(
351433
mMousePressed[0] = true;
352434
}
353435

354-
return wantCapureMouse;
436+
return wantCaptureMouse;
355437
}
356438
case osgGA::GUIEventAdapter::DRAG:
357439
case osgGA::GUIEventAdapter::MOVE: {
358440
io.MousePos
359441
= ImVec2(eventAdapter.getX(), io.DisplaySize.y - eventAdapter.getY());
360442

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

370-
return wantCapureMouse;
452+
return wantCaptureMouse;
371453
}
372454
case osgGA::GUIEventAdapter::SCROLL: {
373455
constexpr float increment = 0.1f;
@@ -390,7 +472,7 @@ bool ImGuiHandler::handle(
390472
break;
391473
}
392474

393-
return wantCapureMouse;
475+
return wantCaptureMouse;
394476
}
395477
default: {
396478
return false;

0 commit comments

Comments
 (0)