Skip to content

Commit

Permalink
Manually building docking nodes for Editor and setting default positi…
Browse files Browse the repository at this point in the history
…ons for windows.

Windows like Console and Entity Tree now default show at the bottom and left respectively. Requires manual construction of our dockspace via imgui_internal.h to ascertain the correct dock_ids.
  • Loading branch information
MStachowicz committed Mar 17, 2024
1 parent e905255 commit d36500a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
55 changes: 39 additions & 16 deletions source/Platform/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "backends/imgui_impl_glfw.h"
#include "backends/imgui_impl_opengl3.h"
#include "imgui.h"
#include "imgui_internal.h"
#include "ImGuizmo.h"

#include <iostream>
Expand Down Expand Up @@ -161,32 +162,54 @@ namespace Platform
ImGui::NewFrame();
ImGuizmo::BeginFrame();

{ // At the start of an ImGui frame, push a window the size of viewport to allow docking other ImGui windows to.
ImGui::SetNextWindowSize(size());
ImGui::SetNextWindowPos(ImVec2(0, 0));
ImGui::SetNextWindowViewport(ImGui::GetMainViewport()->ID);
// At the start of an ImGui frame, push a window the size of viewport to allow docking other ImGui windows to.
ImGuiViewport* viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowSize(viewport->Size);
ImGui::SetNextWindowPos(viewport->Pos);
ImGui::SetNextWindowViewport(viewport->ID);

ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));

auto imgui_window_flags = ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoTitleBar
| ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove
| ImGuiWindowFlags_NoNavFocus | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoBringToFrontOnFocus;
auto imgui_window_flags = ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoTitleBar
| ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove
| ImGuiWindowFlags_NoNavFocus | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoBringToFrontOnFocus;

if (m_show_menu_bar) imgui_window_flags |= ImGuiWindowFlags_MenuBar;
if (m_show_menu_bar) imgui_window_flags |= ImGuiWindowFlags_MenuBar;

ImGui::Begin("Dockspace window", nullptr, imgui_window_flags);
ImGui::Begin("root_dock", nullptr, imgui_window_flags);
ImGui::PopStyleVar(3);

ImGui::DockSpace(ImGui::GetID("Dockspace window"), ImVec2(0.f, 0.f), ImGuiDockNodeFlags_None
| ImGuiDockNodeFlags_PassthruCentralNode | ImGuiDockNodeFlags_NoDockingInCentralNode);

ImGui::PopStyleVar(3);
ImGuiID root_dock_ID = ImGui::GetID("root_dock");
ImGui::DockSpace(root_dock_ID, ImVec2(0.f, 0.f), ImGuiDockNodeFlags_PassthruCentralNode | ImGuiDockNodeFlags_NoDockingInCentralNode);

static bool first_time = true;
if (first_time)
{
first_time = false;
ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_PassthruCentralNode;

// Clear the previous layout and add a root node the size of the viewport.
ImGui::DockBuilderRemoveNode(root_dock_ID);
ImGui::DockBuilderAddNode(root_dock_ID, dockspace_flags | ImGuiDockNodeFlags_DockSpace | ImGuiDockNodeFlags_NoResize | ImGuiDockNodeFlags_KeepAliveOnly);
ImGui::DockBuilderSetNodeSize(root_dock_ID, viewport->Size);

// Split the dockspace into 4 nodes by splitting root_dock_ID horizontally and then splitting the right node vertically.
ImGuiID dock_id_left, dock_id_right;
ImGui::DockBuilderSplitNode(root_dock_ID, ImGuiDir_Left, 0.2f, &dock_id_left, &dock_id_right);
ImGuiID dock_id_down, dock_id_up;
ImGui::DockBuilderSplitNode(dock_id_right, ImGuiDir_Down, 0.15f, &dock_id_down, &dock_id_up);
ImGui::DockBuilderFinish(root_dock_ID);

ASSERT_THROW(dock_id_left == 1 && dock_id_right == 2 && dock_id_up == 3 && dock_id_down == 4,
"Dock direction IDs are not as expected. We use these hard coded numbers for the layout of the editor with SetNextWindowDockID()! Can create a mapping if the order changes.");
}
}
void Window::end_ImGui_frame()
{
ImGui::End();
ImGui::End(); // root_dock
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
Expand Down
3 changes: 1 addition & 2 deletions source/UI/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ namespace UI
void Console::draw(const char* title, bool* p_open)
{
static ImGuiTextFilter m_filter; // Declared statically to avoid having to include ImGui in the header.
ImGui::SetNextWindowSize(ImVec2(520, 600), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowDockID(0, ImGuiCond_FirstUseEver); // Dock to the bottom of the view.

ImGui::SetNextWindowDockID(4, ImGuiCond_Once);
if (ImGui::Begin(title, p_open))
{
bool copy_to_clipboard = false;
Expand Down
1 change: 1 addition & 0 deletions source/UI/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ namespace UI
}
void Editor::draw_entity_tree_window()
{
ImGui::SetNextWindowDockID(1, ImGuiCond_Once);
if (ImGui::Begin("Entities", &m_windows_to_display.Entity))
{
auto& scene = m_scene_system.get_current_scene_entities();
Expand Down

0 comments on commit d36500a

Please sign in to comment.