Skip to main content

ImGuiOverlay.ixx File

Manages a collection of ImGui widgets and renders them using a backend. More...

Included Headers

#include <vector> #include "imgui.h" #include <helios.ext.imgui.ImGuiWidget> #include <helios.ext.imgui.ImGuiBackend>

Namespaces Index

namespacehelios
namespaceext

Platform-specific extensions and backend implementations. More...

namespaceimgui

Classes Index

classImGuiOverlay

Central manager for ImGui widgets rendered via a specific backend. More...

Description

Manages a collection of ImGui widgets and renders them using a backend.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file ImGuiOverlay.ixx
3 * @brief Manages a collection of ImGui widgets and renders them using a backend.
4 */
5module;
6
7#include <vector>
8
9#include "imgui.h"
10
11export module helios.ext.imgui.ImGuiOverlay;
12
13import helios.ext.imgui.ImGuiBackend;
14import helios.ext.imgui.ImGuiWidget;
15
16
17export namespace helios::ext::imgui {
18
19 /**
20 * @class ImGuiOverlay
21 * @brief Central manager for ImGui widgets rendered via a specific backend.
22 *
23 * The overlay is a singleton per backend. Widgets can be registered dynamically,
24 * and all widgets are rendered in the order they were added.
25 *
26 * @note This class is implemented as a singleton using `forBackend()`. Direct
27 * construction is not allowed.
28 */
29 class ImGuiOverlay {
30
31
32 private:
33 /**
34 * @brief Container for dynamically registered ImGui widgets.
35 *
36 * Stores pointers to widgets that are managed and rendered by the `ImGuiOverlay`.
37 * Widgets are added in the order they are registered and must remain valid for the
38 * lifetime of the overlay. The container does not take ownership of the widgets.
39 *
40 * @note This is a private member of `ImGuiOverlay` and is utilized for managing the
41 * rendering lifecycle of widgets.
42 */
43 std::vector<helios::ext::imgui::ImGuiWidget*> widgets_;
44
45 /**
46 * @brief Pointer to the backend used for rendering ImGui widgets.
47 *
48 * Represents the platform-specific implementation of ImGui functionality
49 * (e.g., OpenGL, Vulkan, etc.). The backend facilitates frame management,
50 * new frame initialization, and rendering draw data.
51 *
52 * @note This pointer is initialized via the `ImGuiOverlay` constructor
53 * and must remain a valid instance for the lifetime of the `ImGuiOverlay`.
54 */
55 ImGuiBackend* backend_;
56
57 /**
58 * @brief Whether to enable a full-viewport DockSpace for docking widgets.
59 */
60 bool enableDockSpace_ = true;
61
62 /**
63 * @brief Constructs an ImGuiOverlay instance with a specified backend.
64 *
65 * This constructor initializes the overlay with the provided ImGui backend.
66 * It is private to enforce the singleton pattern of the overlay.
67 *
68 * @param backend Pointer to the ImGuiBackend implementation. Must remain valid
69 * for the lifetime of the overlay.
70 */
71 explicit ImGuiOverlay(ImGuiBackend* backend) :
72 backend_(backend)
73 {
74 }
75
76 public:
77
78 /**
79 * @brief Retrieves the singleton overlay instance for a given backend.
80 *
81 * @param backend Pointer to the ImGui backend (e.g., GLFW+OpenGL). Must remain valid.
82 * @return Reference to the singleton overlay instance.
83 */
84 static ImGuiOverlay& forBackend(ImGuiBackend* backend) {
85
86 static auto overlay = ImGuiOverlay(backend);
87
88 return overlay;
89 }
90
91 /**
92 * @brief Adds a widget to the overlay.
93 *
94 * Widgets are rendered in the order they were added. The overlay does not
95 * take ownership; the caller must ensure the widget remains valid.
96 *
97 * @param widget Pointer to the widget. Must remain valid for the lifetime of the overlay.
98 */
99 void addWidget(ImGuiWidget* widget) {
100
101 widgets_.emplace_back(widget);
102 }
103
104 /**
105 * @brief Enables or disables the full-viewport DockSpace.
106 *
107 * When enabled, widgets can be docked to the edges of the main viewport.
108 *
109 * @param enabled True to enable docking, false to disable.
110 */
111 void setDockSpaceEnabled(bool enabled) noexcept {
112 enableDockSpace_ = enabled;
113 }
114
115 /**
116 * @brief Renders all registered widgets using the backend.
117 *
118 * Calls `newFrame()` on the backend, optionally creates a DockSpace,
119 * invokes `draw()` on each widget, then finalizes rendering via
120 * `ImGui::Render()` and `renderDrawData()`.
121 */
122 void render() {
123 backend_->newFrame();
124
125 // Create a full-viewport DockSpace so widgets can be docked to window edges
126 if (enableDockSpace_) {
127 ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport(),
128 ImGuiDockNodeFlags_PassthruCentralNode);
129 }
130
131 for (const auto it : widgets_) {
132 it->draw();
133 }
134
135 ImGui::Render();
136 backend_->renderDrawData(ImGui::GetDrawData());
137 }
138
139 };
140
141
142}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.