Created
August 30, 2020 14:49
-
-
Save Pikachuxxxx/a3796bb193ca0aaed4ad4f591b2dab07 to your computer and use it in GitHub Desktop.
A snippet to enable ImGui Docking Feature with the necessary constrains
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ImGuiEnableDocking(bool* p_open) | |
{ | |
static bool opt_fullscreen = true; | |
static bool opt_padding = false; | |
static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_None; | |
// We are using the ImGuiWindowFlags_NoDocking flag to make the parent window not dockable into, | |
// because it would be confusing to have two docking targets within each others. | |
ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking; | |
if (opt_fullscreen) | |
{ | |
ImGuiViewport* viewport = ImGui::GetMainViewport(); | |
ImGui::SetNextWindowPos(viewport->GetWorkPos()); | |
ImGui::SetNextWindowSize(viewport->GetWorkSize()); | |
ImGui::SetNextWindowViewport(viewport->ID); | |
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); | |
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); | |
window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove; | |
window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus; | |
} | |
else | |
{ | |
dockspace_flags &= ~ImGuiDockNodeFlags_PassthruCentralNode; | |
} | |
// When using ImGuiDockNodeFlags_PassthruCentralNode, DockSpace() will render our background | |
// and handle the pass-thru hole, so we ask Begin() to not render a background. | |
if (dockspace_flags & ImGuiDockNodeFlags_PassthruCentralNode) | |
window_flags |= ImGuiWindowFlags_NoBackground; | |
// Important: note that we proceed even if Begin() returns false (aka window is collapsed). | |
// This is because we want to keep our DockSpace() active. If a DockSpace() is inactive, | |
// all active windows docked into it will lose their parent and become undocked. | |
// We cannot preserve the docking relationship between an active window and an inactive docking, otherwise | |
// any change of dockspace/settings would lead to windows being stuck in limbo and never being visible. | |
if (!opt_padding) | |
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); | |
ImGui::Begin("DockSpace Demo", p_open, window_flags); | |
if (!opt_padding) | |
ImGui::PopStyleVar(); | |
if (opt_fullscreen) | |
ImGui::PopStyleVar(2); | |
// DockSpace | |
ImGuiIO& io = ImGui::GetIO(); | |
if (io.ConfigFlags & ImGuiConfigFlags_DockingEnable) | |
{ | |
ImGuiID dockspace_id = ImGui::GetID("MyDockSpace"); | |
ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags); | |
} | |
else | |
{ | |
std::cerr << "ERROR::ImGui::Cannot Dock Windows" << '\n'; | |
} | |
ImGui::End(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment