Visual Computing Library
Loading...
Searching...
No Matches
imgui_drawer.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_IMGUI_IMGUI_DRAWER_H
24#define VCL_IMGUI_IMGUI_DRAWER_H
25
26#include <vclib/render/drawers/blocker_event_drawer.h>
27#include <vclib/render/window_managers.h>
28
29// Include the render backand imgui implementation
30#ifdef VCLIB_RENDER_BACKEND_OPENGL2
31#include <imgui_impl_opengl2.h>
32#elif defined(VCLIB_RENDER_BACKEND_BGFX)
33#include <vclib/bgfx_imgui/imgui_impl_bgfx.h>
34#endif
35
36// Include the event backend imgui implementations
37#ifdef VCLIB_WITH_GLFW
38#include <imgui_impl_glfw.h>
39#endif
40#ifdef VCLIB_WITH_QT
41#include <vclib/qt_imgui/imgui_impl_qt.h>
42#endif
43
44namespace vcl::imgui {
45
46template<typename DerivedRenderApp>
47class ImGuiDrawer : public BlockerEventDrawer<DerivedRenderApp>
48{
49protected:
50 using DRA = DerivedRenderApp;
51
52public:
54 {
55 static_assert(
56 DRA::WINDOW_MANAGER_ID == WindowManagerId::GLFW_WINDOW ||
57 DRA::WINDOW_MANAGER_ID == WindowManagerId::QT_WIDGET,
58 "ImGuiDrawer supports only GLFW or Qt window managers.");
59 }
60
61 ImGuiDrawer(uint, uint) : ImGuiDrawer() {}
62
64 {
65 // cleanup
66#ifdef VCLIB_RENDER_BACKEND_OPENGL2
68#elif defined(VCLIB_RENDER_BACKEND_BGFX)
69 ImGui_ImplBgfx_Shutdown();
70#endif // VCLIB_RENDER_BACKEND_*
71#ifdef VCLIB_WITH_GLFW
72 if constexpr (DRA::WINDOW_MANAGER_ID == WindowManagerId::GLFW_WINDOW) {
74 }
75#endif // VCLIB_WITH_GLFW
76#ifdef VCLIB_WITH_QT
77 if constexpr (DRA::WINDOW_MANAGER_ID == WindowManagerId::QT_WIDGET) {
78 ImGui_ImplQt_Shutdown();
79 }
80#endif // VCLIB_WITH
81 ImGui::DestroyContext();
82 }
83
84 virtual void onInit(uint viewId)
85 {
86 // setup ImGui context
88 ImGui::CreateContext();
89 ImGuiIO& io = ImGui::GetIO();
90 // Enable Keyboard Controls
91 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
92 // Enable Gamepad Controls
93 // io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
94 // setup ImGui style
95 ImGui::StyleColorsDark();
96
97#ifdef VCLIB_WITH_GLFW
98 if constexpr (DRA::WINDOW_MANAGER_ID == WindowManagerId::GLFW_WINDOW) {
99 GLFWwindow* mWindow =
100 reinterpret_cast<GLFWwindow*>(DRA::DRW::windowPtr(derived()));
101 // setup platform/RenderApp backends (GLFW and ImGui)
102#ifdef VCLIB_RENDER_BACKEND_OPENGL2
103 ImGui_ImplGlfw_InitForOpenGL(mWindow, true);
105#elif defined(VCLIB_RENDER_BACKEND_BGFX)
106 ImGui_ImplGlfw_InitForOther(mWindow, true);
107 ImGui_ImplBgfx_Init();
108#endif // VCLIB_RENDER_BACKEND_*
109 }
110#endif // VCLIB_WITH_GLFW
111#ifdef VCLIB_WITH_QT
112 if constexpr (DRA::WINDOW_MANAGER_ID == WindowManagerId::QT_WIDGET) {
113 QWidget* mWindow =
114 reinterpret_cast<QWidget*>(DRA::DRW::windowPtr(derived()));
115 // setup platform/RenderApp backends (Qt and ImGui)
116 ImGui_ImplQt_Init(mWindow);
117#ifdef VCLIB_RENDER_BACKEND_OPENGL2
119#elif defined(VCLIB_RENDER_BACKEND_BGFX)
120 ImGui_ImplBgfx_Init();
121#endif // VCLIB_RENDER_BACKEND_*
122 }
123#endif // VCLIB_WITH_QT
124 }
125
126 virtual void onDraw(uint viewId)
127 {
128 // imgui frame
129#ifdef VCLIB_RENDER_BACKEND_OPENGL2
131#elif defined(VCLIB_RENDER_BACKEND_BGFX)
132 ImGui_ImplBgfx_NewFrame();
133#endif // VCLIB_RENDER_BACKEND_*
134#ifdef VCLIB_WITH_GLFW
135 if constexpr (DRA::WINDOW_MANAGER_ID == WindowManagerId::GLFW_WINDOW) {
137 }
138#endif // VCLIB_WITH_GLFW
139#ifdef VCLIB_WITH_QT
140 if constexpr (DRA::WINDOW_MANAGER_ID == WindowManagerId::QT_WIDGET) {
141 ImGui_ImplQt_NewFrame();
142 }
143#endif // VCLIB_WITH_QT
144 ImGui::NewFrame();
145
146 this->onDrawContent(viewId);
147 }
148
149 virtual void onPostDraw()
150 {
151 // imgui rendering
152 ImGui::Render();
153#ifdef VCLIB_RENDER_BACKEND_OPENGL2
154 ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
155#elif defined(VCLIB_RENDER_BACKEND_BGFX)
156 ImGui_ImplBgfx_RenderDrawData(ImGui::GetDrawData());
157#endif // VCLIB_RENDER_BACKEND_*
158#ifndef __linux__
159 // FIXME #4: find a clean way to properly update the WindowManager
160 // as fast as possible (update inside the paintEvent is not allowed by
161 // qt)
162 derived()->update();
163#endif
164 }
165
166 virtual bool onKeyPress(Key::Enum key, const KeyModifiers& modifiers)
167 {
168 return wantCapture(false);
169 }
170
171 virtual bool onKeyRelease(Key::Enum key, const KeyModifiers& modifiers)
172 {
173 return wantCapture(false);
174 }
175
176 virtual bool onMouseMove(double x, double y, const KeyModifiers& modifiers)
177 {
178 return wantCapture();
179 }
180
181 virtual bool onMousePress(
182 MouseButton::Enum button,
183 double x,
184 double y,
185 const KeyModifiers& modifiers)
186 {
187 return wantCapture();
188 }
189
190 virtual bool onMouseRelease(
191 MouseButton::Enum button,
192 double x,
193 double y,
194 const KeyModifiers& modifiers)
195 {
196 return wantCapture();
197 }
198
199 virtual bool onMouseDoubleClick(
200 MouseButton::Enum button,
201 double x,
202 double y,
203 const KeyModifiers& modifiers)
204 {
205 return wantCapture();
206 }
207
208 virtual bool onMouseScroll(
209 double x,
210 double y,
211 const KeyModifiers& modifiers)
212 {
213 return wantCapture();
214 }
215
216protected:
217 bool isWindowMinimized() const { return derived()->isMinimized(); }
218
219private:
220 auto* derived() { return static_cast<DRA*>(this); }
221
222 const auto* derived() const { return static_cast<const DRA*>(this); }
223
224 bool wantCapture(bool mouse = true)
225 {
226 const auto& io = ImGui::GetIO();
227 return mouse ? io.WantCaptureMouse : io.WantCaptureKeyboard;
228 }
229};
230
231} // namespace vcl::imgui
232
233#endif // VCL_IMGUI_IMGUI_DRAWER_H
Special EventDrawer class for the case where the drawer can request to block the event propagation.
Definition blocker_event_drawer.h:48
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
Definition imgui_drawer.h:48