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

Add a user-accessible BackgroundDrawList #545

Closed
esquellington opened this issue Mar 4, 2016 · 4 comments
Closed

Add a user-accessible BackgroundDrawList #545

esquellington opened this issue Mar 4, 2016 · 4 comments

Comments

@esquellington
Copy link

A simple feature request: having an API method ImGui::GetBackgroundDrawList() that returns an internal ImDrawList that is always rendered first at full display size, without any window overhead (context, scrollbars, alpha, etc...) just a list of primitives guaranteed to be rendered before any ImGui window.

The BackgroundDrawList would act as a global overlay on the user-drawn scene, but always remain behind ImGui windows and mouse cursor (the internal OverlayDrawList). There's a few use cases for this:

  • Draw text labels anywhere in the window using existing ImGui font load/render functionality, avoiding the need for a different font load/render code path in simple applications.
  • Draw primitives anywhere in the window using existing ImGui functionality, for example, to display rubber-banding selection rectangles overlayed on a 3D scene, or even using ImGui as a simple 2D immediate rendering API.

It was pointed out to me by @ocornut that the same effect can be achieved with a "naked window" (code below), however, this requires draw commands to be issued inside the specific Begin/End block, or switching to/from the "naked window" in the middle of user code that may want to issue both global background text/primitives and normal ImGui windows.
bool bOpened(true);
ImGui::PushStyleColor( ImGuiCol_WindowBg, ImVec4(0,0,0,0) );
ImGui::Begin("BCKGND",&bOpened, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoBringToFrontOnFocus );
ImGui::SetWindowPos( ImVec2(0,0), ImGuiSetCond_FirstUseEver );
ImGui::SetWindowSize( ImGui::GetIO().DisplaySize );
ImGui::SetWindowCollapsed( false );
ImGui::GetWindowDrawList()->AddText( ImVec2(400,400), ImColor(1.0f,1.0f,1.0f,1.0f), "Text in Background Layer" );
ImGui::End();
ImGui::PopStyleColor();

As a generalization, instead of a single BackgroundDrawList, it might be better to have an array of ordered background layers 0...N, retrieved with ImGui::GetLayerDrawList( int layer_id ), to which draw commands can be directly sent regardless of the current window stack state.

I have implemented basic ImGui::GetBackgroundDrawList() feature successfully in my local branch, and could work on the ImGui::GetLayerDrawList() generalization if it's considered better, but first I'd like to know if there's any interest/drawbacks on this becoming part of the ImGui API.

@ocornut
Copy link
Owner

ocornut commented Mar 4, 2016

Not answering the full question, FYI your code above for a naked window can be shortened:

ImGui::SetNextWindowPos( ImVec2(0,0) );
ImGui::Begin("BCKGND", NULL, ImGui::GetIO().DisplaySize, 0,0f, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoBringToFrontOnFocus );
ImGui::GetWindowDrawList()->AddText( ImVec2(400,400), ImColor(1.0f,1.0f,1.0f,1.0f), "Text in Background Layer" );
ImGui::End();

I wouldn't mind some helper combination flag, e.g ImGuiWindowFlags_Naked the problem is to decide on a set of flags that makes definitive sense under a given label. And they are actually harder to find than you'd expect.

@esquellington
Copy link
Author

Thanks, the (...)_Naked flags combination would be helpful but could just be done on the user side I guess.
My main suggestion/question is if it would make sense to have naked layers with no window context at all, outside Begin/End blocks. I understand it may not fit as a "core" feature of ImGui or may break some existing or future abstractions, but would allow users to reuse existing immediate 2D renderer, specially for text.

@ocornut
Copy link
Owner

ocornut commented Mar 13, 2019

@esquellington GetBackgroundDrawList() has been added now, see #2391
Closing this.

Also note that 1.66 also introduced helper flags ImGuiWindowFlags_NoDecoration to facilitate custom versions of this.

@ocornut ocornut closed this as completed Mar 13, 2019
@esquellington
Copy link
Author

That's awesome, thanks!

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

No branches or pull requests

2 participants