Skip to content

Commit

Permalink
imgui: support multi-screen config, and add an onStartup hook so the …
Browse files Browse the repository at this point in the history
…user can make imgui calls on startup
  • Loading branch information
gwaldron committed Nov 27, 2024
1 parent e2cd526 commit c6b4e56
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/applications/osgearth_imgui/osgearth_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ main(int argc, char** argv)
ui->add("Procedural", new osgEarth::Procedural::VegetationLayerGUI());
#endif

ui->onStartup = []()
{
ImGui::GetIO().FontAllowUserScaling = true;
};

// Put it on the front of the list so events don't filter through to other handlers.
viewer.getEventHandlers().push_front(ui);

Expand Down
6 changes: 6 additions & 0 deletions src/osgEarthImGui/ImGuiEventHandler
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,15 @@ namespace osgEarth
virtual void draw(osg::RenderInfo& renderInfo) = 0;

void newFrame(osg::RenderInfo& renderInfo);

void render(osg::RenderInfo& renderInfo);

bool _show = true;

// called by the handler upon ImGui initialization - you can access
// ImGui::GetIO() safely from here.
std::function<void()> onStartup = nullptr;

protected:
bool _dirtySettings = false;

Expand Down
31 changes: 23 additions & 8 deletions src/osgEarthImGui/ImGuiEventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ ImGuiEventHandler::newFrame(osg::RenderInfo& renderInfo)
#ifdef IMGUI_HAS_DOCK
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
#endif
// call a user startup function if set
if (onStartup)
onStartup();
}

ImGui_ImplOpenGL3_NewFrame();
Expand Down Expand Up @@ -214,15 +217,27 @@ void ImGuiEventHandler::render(osg::RenderInfo& ri)
}
}

namespace
{
void applyModifiers(const osgGA::GUIEventAdapter& ea, ImGuiIO& io)
{
io.AddKeyEvent(ImGuiMod_Ctrl, (ea.getModKeyMask() & ea.MODKEY_CTRL) != 0);
io.AddKeyEvent(ImGuiMod_Shift, (ea.getModKeyMask() & ea.MODKEY_SHIFT) != 0);
io.AddKeyEvent(ImGuiMod_Alt, (ea.getModKeyMask() & ea.MODKEY_ALT) != 0);
io.AddKeyEvent(ImGuiMod_Super, (ea.getModKeyMask() & ea.MODKEY_SUPER) != 0);
}
}

bool ImGuiEventHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
if (!_initialized)
{
auto view = aa.asView();
if (view)
{
view->getCamera()->setPreDrawCallback(new PreDrawOp(*this));
view->getCamera()->setPostDrawCallback(new PostDrawOp(*this));
osg::Camera* camera = view->getNumSlaves() > 0 ? view->getSlave(0)._camera : view->getCamera();
camera->setPreDrawCallback(new PreDrawOp(*this));
camera->setPostDrawCallback(new PostDrawOp(*this));
_initialized = true;
return false;
}
Expand Down Expand Up @@ -250,11 +265,7 @@ bool ImGuiEventHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActio

if (io.WantCaptureKeyboard || c == ea.KEY_Return)
{
// first apply any modifiers:
io.AddKeyEvent(ImGuiMod_Ctrl, (ea.getModKeyMask() & ea.MODKEY_CTRL) != 0);
io.AddKeyEvent(ImGuiMod_Shift, (ea.getModKeyMask() & ea.MODKEY_SHIFT) != 0);
io.AddKeyEvent(ImGuiMod_Alt, (ea.getModKeyMask() & ea.MODKEY_ALT) != 0);
io.AddKeyEvent(ImGuiMod_Super, (ea.getModKeyMask() & ea.MODKEY_SUPER) != 0);
applyModifiers(ea, io);

// map the OSG key code to the ImGui key code and send to imgui:
auto imgui_key = convertKey(c);
Expand Down Expand Up @@ -283,6 +294,8 @@ bool ImGuiEventHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActio
{
if (io.WantCaptureMouse)
{
applyModifiers(ea, io);

auto imgui_button = convertMouseButton(ea.getButtonMask());
io.AddMousePosEvent(ea.getX(), io.DisplaySize.y - ea.getY());
io.AddMouseButtonEvent(imgui_button, true); // true = press
Expand All @@ -305,17 +318,19 @@ bool ImGuiEventHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActio
case (osgGA::GUIEventAdapter::DRAG):
case (osgGA::GUIEventAdapter::MOVE):
{
applyModifiers(ea, io);
io.AddMousePosEvent(ea.getX(), io.DisplaySize.y - ea.getY());
return io.WantCaptureMouse;
}

case (osgGA::GUIEventAdapter::SCROLL):
{
applyModifiers(ea, io);
auto scrolling = ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_UP ? 1.0 : -1.0;
io.AddMouseWheelEvent(0.0, io.MouseWheel += scrolling);
return io.WantCaptureMouse;
}
}
}

return false;
}
Expand Down

0 comments on commit c6b4e56

Please sign in to comment.