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

Help With Overlapped Widgets #5312

Closed
AlexvZyl opened this issue May 13, 2022 · 3 comments
Closed

Help With Overlapped Widgets #5312

AlexvZyl opened this issue May 13, 2022 · 3 comments
Labels

Comments

@AlexvZyl
Copy link

AlexvZyl commented May 13, 2022

Version/Branch of Dear ImGui:

Version: 1.88
Branch: docking

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_glfw.cpp + imgui_impl_opengl3.cpp
Operating System: Windows 10

My Issue/Question:

I am trying to create an overlay for my scene, as seen here. Notice the button in the top left and the child in the bottom right.

image

I want the button to block events to the scene. Basically, I just need to know if the texture is hovered and then I can respond accordingly. I have tried the following:

// The image.
ImGui::Image(m_textureID, m_contentRegionSize, { 0, 1 }, { 1, 0 });
ImGui::SetItemAllowOverlap();
if (ImGui::IsItemHovered()) m_engine->m_isHovered = true;
else         	            m_engine->m_isHovered = false;

// I have drag and drop items here, if that might have an effect.

// The button.
ImGui::SetCursorPos(ImGui::GetWindowContentRegionMin());
ImGui::Button("Test");
ImGui::SetItemAllowOverlap();

// Render child here.

The child seems to be working correctly, but I suspect this has nothing to do with overlapping and just how children work in Dear ImGui. m_engine->m_isHovered is being set to true, even if the button is being hovered. Am I missing something?

@rokups
Copy link
Contributor

rokups commented May 16, 2022

You could try this:

static ImDrawListSplitter splitter;
splitter.Split(ImGui::GetWindowDrawList(), 2);

// Render button first, however, put it to the top layer so it is rendered on top of viewport image.
splitter.SetCurrentChannel(ImGui::GetWindowDrawList(), 1);
if (ImGui::Button("Button"))
    printf("Button pressed\n");  // Interacting with the button will prevent interacting with the image.

// Render viewport image later, but it wont cover previously rendered button.
splitter.SetCurrentChannel(ImGui::GetWindowDrawList(), 0);
ImGui::SetCursorPos(ImGui::GetWindowContentRegionMin());
ImGui::Image(io.Fonts->TexID, ImVec2(200, 200));
if (ImGui::IsItemClicked())
    printf("Viewport pressed\n");

// Debug info: image is hovered when hovering "Button", but hovered state gets reset when "Button" is pressed down.
ImGui::Text("Viewport hovered: %d", ImGui::IsItemHovered());
splitter.Merge(ImGui::GetWindowDrawList());

@AlexvZyl
Copy link
Author

AlexvZyl commented Jun 22, 2022

I tried what you suggested, but it still does not give me the functionality I am looking for. The texture is still seen as hovered even if the button is on top of it.

However, I did learn about the splitter now, so thanks for that! Pretty cool.

@ocornut
Copy link
Owner

ocornut commented Jun 28, 2023

Hello,

This should now all be fixed by changes outlined here:

#6512 (comment)

  • SetNextItemAllowOverlap() works better.
  • IsItemHovered() previously was never aware of it. It is now aware of it and it can be opted out with ImGuiHoveredFlags_AllowWhenOverlappedByItem.

Thanks for your patience.

@ocornut ocornut closed this as completed Jun 28, 2023
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

3 participants