Skip to content

Commit

Permalink
Work on AddDockableWindow: handle dockSpaceIdFromName failures
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Sep 19, 2024
1 parent c3d46c7 commit 9480a37
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
45 changes: 28 additions & 17 deletions src/hello_imgui/internal/docking_details.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ namespace AddDockableWindowHelper
};

std::vector<DockableWindowWaitingForAddition> gDockableWindowsToAdd;
std::vector<std::string> gDockableWindowsToRemove;

void AddDockableWindow(const DockableWindow& dockableWindow)
{
Expand All @@ -646,21 +647,24 @@ namespace AddDockableWindowHelper
{
if (dockableWindow.state == DockableWindowAdditionState::Waiting)
{
ImGui::Begin(dockableWindow.dockableWindow.label.c_str());
ImGui::Dummy(ImVec2(10, 10));
ImGui::End();

auto dockId = HelloImGui::GetRunnerParams()->dockingParams.dockSpaceIdFromName(dockableWindow.dockableWindow.dockSpaceName);
if (dockId.has_value())
ImGui::DockBuilderDockWindow(dockableWindow.dockableWindow.label.c_str(), dockId.value());
{
ImGui::Begin(dockableWindow.dockableWindow.label.c_str());
ImGui::Dummy(ImVec2(10, 10));
ImGui::End();

ImGui::DockBuilderDockWindow(dockableWindow.dockableWindow.label.c_str(), 21423345);
//ImGui::DockBuilderDockWindow(dockableWindow.dockableWindow.label.c_str(), dockId.value());
}
dockableWindow.state = DockableWindowAdditionState::AddedAsDummyToImGui;
}
}
}

void Callback_2_PreNewFrame()
{
// Add the dockable windows that have been added as dummy to ImGui to HelloImGui
for (auto & dockableWindow: gDockableWindowsToAdd)
{
if (dockableWindow.state == DockableWindowAdditionState::AddedAsDummyToImGui)
Expand All @@ -670,7 +674,7 @@ namespace AddDockableWindowHelper
}
}

// Remove the dockable windows that have been added to HelloImGui
// Forget about the dockable windows that have been added to HelloImGui
gDockableWindowsToAdd.erase( // typical C++ shenanigans
std::remove_if(
gDockableWindowsToAdd.begin(),
Expand All @@ -681,6 +685,23 @@ namespace AddDockableWindowHelper
),
gDockableWindowsToAdd.end()
);

// Remove the dockable windows that have been requested to be removed
auto& dockableWindows = HelloImGui::GetRunnerParams()->dockingParams.dockableWindows;
for (const auto& dockableWindowName: gDockableWindowsToRemove)
{
dockableWindows.erase(
std::remove_if(
dockableWindows.begin(),
dockableWindows.end(),
[&dockableWindowName](const DockableWindow& dockableWindow) {
return dockableWindow.label == dockableWindowName;
}
),
dockableWindows.end()
);
}
gDockableWindowsToRemove.clear();
}

} // namespace AddDockableWindowHelper
Expand All @@ -693,17 +714,7 @@ void AddDockableWindow(const DockableWindow& dockableWindow)

void RemoveDockableWindow(const std::string& dockableWindowName)
{
auto& dockableWindows = HelloImGui::GetRunnerParams()->dockingParams.dockableWindows;
dockableWindows.erase(
std::remove_if(
dockableWindows.begin(),
dockableWindows.end(),
[&dockableWindowName](const DockableWindow& dockableWindow) {
return dockableWindow.label == dockableWindowName;
}
),
dockableWindows.end()
);
AddDockableWindowHelper::gDockableWindowsToRemove.push_back(dockableWindowName);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ void DemoShowAdditionalWindow(AppState& appState)
{
// In order to add a dockable window during execution, you should use
// HelloImGui::AddDockableWindow()

// Note: you should not modify manually the content of the vector runnerParams.dockingParams.dockableWindows
// Note: you should not modify manually the content of runnerParams.dockingParams.dockableWindows
// (since HelloImGui is constantly looping on it)
const char* windowName = "Additional Window";

ImGui::PushFont(appState.TitleFont->font); ImGui::Text("Dynamically add window"); ImGui::PopFont();

const char* windowName = "Additional Window";
if (ImGui::Button("Show additional window"))
{
HelloImGui::DockableWindow additionalWindow;
Expand Down

0 comments on commit 9480a37

Please sign in to comment.