glfwimgui

How do I handle mouse events in general in Imgui with glfw?


I used the glfw callback function to move the camera with the mouse.

mouse callback function is:

void mouse_callback(GLFWwindow *window, double xposIn, double yposIn)
{
    if (is_pressed)
    {
        camera.ProcessMouseMovement((static_cast<float>(yposIn) - prev_mouse.y) / 3.6f, (static_cast<float>(xposIn) - prev_mouse.x) / 3.6f);
        prev_mouse.x = xposIn;
        prev_mouse.y = yposIn;
    }
    cur_mouse.x = xposIn;
    cur_mouse.y = yposIn;
}

void mouse_btn_callback(GLFWwindow *window, int button, int action, int mods)
{
    if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
    {
        prev_mouse.x = cur_mouse.x;
        prev_mouse.y = cur_mouse.y;
        is_pressed = true;
    }
    else
    {
        is_pressed = false;
    }
}

However, in this case, the camera will move even when operated in other imgui windows as shown below.

I don't know how to handle this.

Should I put this logic between begin and end of IMGUI, using something like ImGui::IsWindowHovered()?

like this:

ImGui::Begin("scene");
{
    if(ImGui::IsWindowHovered())
    {
        //camera setting    
    }
}
ImGui::End()

Solution

  • Answer above are wrong. This is answered in the Dear ImGui FAQ: https://github.com/ocornut/imgui/blob/master/docs/FAQ.md#q-how-can-i-tell-whether-to-dispatch-mousekeyboard-to-dear-imgui-or-my-application TL;DR check the io.WantCaptureMouse flag for mouse.