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

Mouse inputs not working with GLFW backend #7481

Open
Asti0s opened this issue Apr 10, 2024 · 4 comments
Open

Mouse inputs not working with GLFW backend #7481

Asti0s opened this issue Apr 10, 2024 · 4 comments
Labels

Comments

@Asti0s
Copy link

Asti0s commented Apr 10, 2024

Version/Branch of Dear ImGui:

last commit from docking

Back-ends:

imgui_impl_glfw.cpp + imgui_impl_vulkan.cpp

Compiler, OS:

Windows 10

Full config/build information:

No response

Details:

I cant use my mouse to interact with the menus, only my keyboard. When i do a left click, the imgui menu switch to grey and did not respond anymore.
I have another vulkan renderer that uses imgui without this issue but with an older version of the docking branch. I tried to copy this imgui version but the result is the same.
When i switch the "install_callbacks" bool from "ImGui_ImplGlfw_InitForVulkan()" to false, my cursor can hover the buttons but i cant click on them.

Screenshots/Video:

2024-04-10.23-45-57.mp4

Minimal, Complete and Verifiable Example code:

// ImGui setup
    // Setup Dear ImGui context
    IMGUI_CHECKVERSION();

    ImGui::CreateContext();
    m_deletionQueue.add([]() {
        ImGui::DestroyContext();
    });

    ImGuiIO& io = ImGui::GetIO(); (void)io;
    io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
    io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
    io.ConfigFlags |= ImGuiConfigFlags_NavEnableSetMousePos;
    ImGui::StyleColorsDark();


    {   // Vulkan init
        if (!ImGui_ImplGlfw_InitForVulkan(m_window->getHandle(), false))
            throw std::runtime_error("Failed to initialize ImGui for Vulkan");
        m_deletionQueue.add([]() {
            ImGui_ImplGlfw_Shutdown();
        });

        ImGui_ImplVulkan_InitInfo init_info = {};
        init_info.Instance = m_device->getInstance();
        init_info.PhysicalDevice = m_device->getPhysicalDevice();
        init_info.Device = m_device->getHandle();
        init_info.QueueFamily = m_device->getQueueFamilyIndices().graphicsFamilyIndex;
        init_info.Queue = m_device->getGraphicsQueue();
        init_info.PipelineCache = VK_NULL_HANDLE;
        init_info.DescriptorPool = m_bindlessManager->getDescriptorPool();
        init_info.Allocator = nullptr;
        init_info.MinImageCount = m_swapchain->getImageCount();
        init_info.ImageCount = m_swapchain->getImageCount();
        init_info.CheckVkResultFn = nullptr;
        init_info.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
        init_info.UseDynamicRendering = true;
        init_info.PipelineRenderingCreateInfo = pipelineRenderingCreateInfo;

        ImGui_ImplVulkan_Init(&init_info);
        m_deletionQueue.add([]() {
            ImGui_ImplVulkan_Shutdown();
        });
    }

    {   // Upload Fonts
        VkCommandBuffer commandBuffer = m_device->beginCommandBuffer();
        if (!ImGui_ImplVulkan_CreateFontsTexture())
            throw std::runtime_error("Failed to create ImGui font texture");
        m_device->submitCommandBuffer(commandBuffer, m_device->getTransferQueue());
    }
// Utulisation;
    ImGui_ImplVulkan_NewFrame();
    ImGui_ImplGlfw_NewFrame();
    ImGui::NewFrame();

    ImGui::ShowDemoWindow();

    ImGui::Render();
    ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), m_commandBuffers[m_currentFrameIndex]);
@ocornut
Copy link
Owner

ocornut commented Apr 11, 2024

The issue is likely in your code so it would be more adequate to post a complete project, and compare it to existing projects.
Are you rightfully calling glfwPollEvents() ?

@ocornut ocornut changed the title Mouse inputs not working Mouse inputs not working with GLFW backend Apr 11, 2024
@Asti0s
Copy link
Author

Asti0s commented Apr 11, 2024

Yes i call glfwPollEvents(),
My window creation process is the following:

    if (glfwInit() == GLFW_FALSE)
        throw std::runtime_error("Failed to initialize GLFW");
    m_deletionQueue.add([=]() {
        glfwTerminate();
    });

    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);

    m_window = glfwCreateWindow(width, height, title, nullptr, nullptr);
    if (m_window == nullptr)
        throw std::runtime_error("Failed to create GLFW window");
    m_deletionQueue.add([=]() {
        glfwDestroyWindow(m_window);
    });

    glfwSetWindowUserPointer(m_window, this);
    glfwSetFramebufferSizeCallback(m_window, framebufferResizeCallback);

For each frame, here is my code:

    glfwPollEvents();
    VkCommandBufferBeginInfo beginInfo{};
    beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
    beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;

    VK_CHECK(vkBeginCommandBuffer(m_commandBuffers[m_currentFrameIndex], &beginInfo));

    ImGui_ImplVulkan_NewFrame();
    ImGui_ImplGlfw_NewFrame();
    ImGui::NewFrame();

    {   // Begin Rendering
        Image::cmdTransitionImageLayout(
            m_commandBuffers[m_currentFrameIndex],
            m_depthImage->getHandle(),
            VK_IMAGE_LAYOUT_UNDEFINED,
            VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
        );

        Image::cmdTransitionImageLayout(
            m_commandBuffers[m_currentFrameIndex],
            m_swapchain->getImage(m_currentImageIndex),
            VK_IMAGE_LAYOUT_UNDEFINED,
            VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
        );

        VkRenderingAttachmentInfo swapchainImageAttachment{};
        swapchainImageAttachment.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO;
        swapchainImageAttachment.imageView = m_swapchain->getImageView(m_currentImageIndex);
        swapchainImageAttachment.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
        swapchainImageAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
        swapchainImageAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
        swapchainImageAttachment.clearValue = { 0.0f, 0.0f, 0.0f, 1.0f };

        VkRenderingAttachmentInfo depthAttachment{};
        depthAttachment.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO;
        depthAttachment.imageView = m_depthImage->getImageView();
        depthAttachment.imageLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
        depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
        depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
        depthAttachment.clearValue = { 1.0f, 0 };

        VkRenderingInfo renderingInfo{};
        renderingInfo.sType = VK_STRUCTURE_TYPE_RENDERING_INFO;
        renderingInfo.renderArea.offset = {0, 0};
        renderingInfo.renderArea.extent = m_swapchain->getSwapChainExtent();
        renderingInfo.layerCount = 1;
        renderingInfo.colorAttachmentCount = 1;
        renderingInfo.pColorAttachments = &swapchainImageAttachment;
        renderingInfo.pDepthAttachment = &depthAttachment;
        renderingInfo.pStencilAttachment = nullptr;

        vkCmdBeginRendering(m_commandBuffers[m_currentFrameIndex], &renderingInfo);
    }

   // Draw scene
       ImGui::ShowDemoWindow();
       ImGui::Render();
       ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), m_commandBuffers[m_currentFrameIndex]);

       vkCmdEndRendering(m_commandBuffers[m_currentFrameIndex]);

       Image::cmdTransitionImageLayout(
           m_commandBuffers[m_currentFrameIndex],
           m_swapchain->getImage(m_currentImageIndex),
           VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
           VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
       );

       VK_CHECK(vkEndCommandBuffer(m_commandBuffers[m_currentFrameIndex]));

I cant understand why the hovering works when the callbacks have not been installed in ImGui_ImplGlfw_InitForVulkan, but not with the callbacks installed.

@ocornut
Copy link
Owner

ocornut commented Apr 16, 2024

I cant understand why the hovering works when the callbacks have not been installed in ImGui_ImplGlfw_InitForVulkan, but not with the callbacks installed.

Because we have a fallback using glfwGetCursorPos() when window is focused, we need it to handle certain edge cases.

Callbacks should be installed or you should install them yourself.
Try to use a debugger to see if callbacks are being called yourself.

You can also use our debug log:

GImGui->DebugLogFlags |= ImGuiDebugLogFlags_EventIO; // force enable since you cannot click
ImGui::ShowDebugLogWindow();

@Asti0s
Copy link
Author

Asti0s commented Apr 16, 2024

when i have the callbacks automatically installed it says

[00426] [io] Processed: MouseButton 0 Down (Mouse)
[00496] [io] Processed: MouseButton 0 Up (Mouse)

it does not print the mouse pos

and when the callbacks are not installed it spams the mouse pos without detecting when i click

[05265] [io] Processed: MousePos (768.0, -9.0) (Mouse)
[05269] [io] Processed: MousePos (769.0, -9.0) (Mouse)
[05279] [io] Processed: MousePos (769.0, -10.0) (Mouse)
[05283] [io] Processed: MousePos (770.0, -10.0) (Mouse)

do i have to install the callbacks manualy ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants