Skip to content

Commit

Permalink
Automatically prevent multiple panels from overlapping.
Browse files Browse the repository at this point in the history
The implementation is alot more arkward than I would have liked, but will suffice.
  • Loading branch information
Robadob committed Aug 30, 2022
1 parent 1a15241 commit 730fe3f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/flamegpu/visualiser/ui/ImGuiPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,26 @@ ImGuiPanel::ImGuiPanel(const std::map<std::string, std::shared_ptr<PanelConfig>>
ImGui_ImplSDL2_NewFrame(); // This can't be called in the render thread, as it tries to grab the window dimensions via SDL
}
void ImGuiPanel::reload() {
// Do nothing
first_render = 0;
}
void ImGuiPanel::drawPanel() const {
void ImGuiPanel::drawPanel() {
const ImGuiViewport* main_viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(ImVec2(main_viewport->WorkPos.x + 5, main_viewport->WorkPos.y + 5), ImGuiCond_Once);
ImVec2 prev_window_size(0, 0), prev_window_pos(main_viewport->WorkPos.x, main_viewport->WorkPos.y);
for (const auto &config : configs) {
// Translucent background
ImGui::SetNextWindowBgAlpha(150.0f / 255.0f);
// Size panel to fit it's content
ImGui::SetNextWindowSize(ImVec2(0, 0), ImGuiCond_Always);
if (first_render < 2) {
// Manual set once condition, as I can't get ImGuiCond to play nicely here for multiple panels
// Position multiple panels so that they don't overlap
ImGui::SetNextWindowPos(ImVec2(prev_window_pos.x + prev_window_size.x + 5, main_viewport->WorkPos.y + 5), ImGuiCond_Always);
}

// Actually start creating the panel
if (!ImGui::Begin(config.title.c_str(), nullptr, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize)) {
prev_window_size = ImGui::GetWindowSize();
prev_window_pos = ImGui::GetWindowPos();
// Early out if the window is collapsed, as an optimization.
ImGui::End();
return;
Expand All @@ -57,8 +64,11 @@ void ImGuiPanel::drawPanel() const {

// Finalise the panel
ImGui::PopItemWidth();
prev_window_size = ImGui::GetWindowSize();
prev_window_pos = ImGui::GetWindowPos();
ImGui::End();
}
first_render += first_render < 2 ? 1: 0; // Auto size is not calculated until it's been drawn, so wait a frame to fix it
}
void ImGuiPanel::drawDebugPanel() const {
const ImGuiViewport* main_viewport = ImGui::GetMainViewport();
Expand Down Expand Up @@ -132,6 +142,7 @@ void ImGuiPanel::registerProperty(const std::string& name, void* ptr, bool is_co
}
}
}
first_render = 0; // As these can change window width
}

} // namespace visualiser
Expand Down
9 changes: 7 additions & 2 deletions src/flamegpu/visualiser/ui/ImGuiPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ImGuiPanel : public Overlay {
*/
explicit ImGuiPanel(const std::map<std::string, std::shared_ptr<PanelConfig>> &cfgs, const Visualiser& _vis);
/**
* Does nothing, as ImGui runs in immediate mode
* Only resets first_render flag, as ImGui runs in immediate mode
*/
void reload() override;
/**
Expand Down Expand Up @@ -68,11 +68,16 @@ class ImGuiPanel : public Overlay {
/**
* Calls ImGui to draw the user specified panels from configs
*/
void drawPanel() const;
void drawPanel();
/**
* Calls ImGui to draw the debug panel
*/
void drawDebugPanel() const;
/**
* Causes panels to be automatically positioned
* Takes 2 frames to process, due to auto sizing
*/
unsigned char first_render;
/**
* A copy of the panel configurations passed to the constructor
* These are maintaned as they must be passed to ImGui prior to each frame being rendered
Expand Down

0 comments on commit 730fe3f

Please # to comment.