Visual Computing Library  devel
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/context.h>
34#include <vclib/bgfx_imgui/imgui_impl_bgfx.h>
35#endif
36
37// Include the event backend imgui implementations
38#ifdef VCLIB_WITH_GLFW
39#include <imgui_impl_glfw.h>
40#endif
41#ifdef VCLIB_WITH_QT
42#include <vclib/qt_imgui/imgui_impl_qt.h>
43#endif
44
45namespace vcl::imgui {
46
47template<typename DerivedRenderApp>
48class ImGuiDrawer : public BlockerEventDrawer<DerivedRenderApp>
49{
50#ifdef VCLIB_RENDER_BACKEND_BGFX
51 bgfx::ViewId mImguiViewId = BGFX_INVALID_VIEW;
52#endif
53
54protected:
55 using DRA = DerivedRenderApp;
56
57public:
59 {
60 static_assert(
61 DRA::WINDOW_MANAGER_ID == WindowManagerId::GLFW_WINDOW ||
62 DRA::WINDOW_MANAGER_ID == WindowManagerId::QT_WIDGET,
63 "ImGuiDrawer supports only GLFW or Qt window managers.");
64
65#ifdef VCLIB_RENDER_BACKEND_BGFX
66 mImguiViewId = vcl::Context::instance().requestViewId();
68#endif
69 }
70
71 ImGuiDrawer(uint, uint) : ImGuiDrawer() {}
72
74 {
75 // cleanup
76#ifdef VCLIB_RENDER_BACKEND_OPENGL2
78#elif defined(VCLIB_RENDER_BACKEND_BGFX)
79 ImGui_ImplBgfx_Shutdown();
80 vcl::Context::instance().releaseViewId(mImguiViewId);
81 mImguiViewId = BGFX_INVALID_VIEW;
82#endif // VCLIB_RENDER_BACKEND_*
83#ifdef VCLIB_WITH_GLFW
84 if constexpr (DRA::WINDOW_MANAGER_ID == WindowManagerId::GLFW_WINDOW) {
86 }
87#endif // VCLIB_WITH_GLFW
88#ifdef VCLIB_WITH_QT
89 if constexpr (DRA::WINDOW_MANAGER_ID == WindowManagerId::QT_WIDGET) {
90 ImGui_ImplQt_Shutdown();
91 }
92#endif // VCLIB_WITH
93 ImGui::DestroyContext();
94 }
95
96 virtual void onInit(uint)
97 {
98 // setup ImGui context
100 ImGui::CreateContext();
101 ImGuiIO& io = ImGui::GetIO();
102 // Enable Keyboard Controls
103 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
104 // Enable Gamepad Controls
105 // io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
106 // setup ImGui style
107 ImGui::StyleColorsDark();
108
109#ifdef VCLIB_WITH_GLFW
110 if constexpr (DRA::WINDOW_MANAGER_ID == WindowManagerId::GLFW_WINDOW) {
111 GLFWwindow* mWindow =
112 reinterpret_cast<GLFWwindow*>(DRA::DRW::windowPtr(derived()));
113 // setup platform/RenderApp backends (GLFW and ImGui)
114#ifdef VCLIB_RENDER_BACKEND_OPENGL2
115 ImGui_ImplGlfw_InitForOpenGL(mWindow, true);
117#elif defined(VCLIB_RENDER_BACKEND_BGFX)
118 ImGui_ImplGlfw_InitForOther(mWindow, true);
119 ImGui_ImplBgfx_Init();
120#endif // VCLIB_RENDER_BACKEND_*
121 }
122#endif // VCLIB_WITH_GLFW
123#ifdef VCLIB_WITH_QT
124 if constexpr (DRA::WINDOW_MANAGER_ID == WindowManagerId::QT_WIDGET) {
125 QWidget* mWindow =
126 reinterpret_cast<QWidget*>(DRA::DRW::windowPtr(derived()));
127 // setup platform/RenderApp backends (Qt and ImGui)
128 ImGui_ImplQt_Init(mWindow);
129#ifdef VCLIB_RENDER_BACKEND_OPENGL2
131#elif defined(VCLIB_RENDER_BACKEND_BGFX)
132 ImGui_ImplBgfx_Init();
133#endif // VCLIB_RENDER_BACKEND_*
134 }
135#endif // VCLIB_WITH_QT
136 }
137
138 virtual void onDraw(uint viewId)
139 {
140 // imgui frame
141#ifdef VCLIB_RENDER_BACKEND_OPENGL2
143#elif defined(VCLIB_RENDER_BACKEND_BGFX)
144 (void) viewId;
145 ImGui_ImplBgfx_NewFrame(mImguiViewId);
146#endif // VCLIB_RENDER_BACKEND_*
147#ifdef VCLIB_WITH_GLFW
148 if constexpr (DRA::WINDOW_MANAGER_ID == WindowManagerId::GLFW_WINDOW) {
150 }
151#endif // VCLIB_WITH_GLFW
152#ifdef VCLIB_WITH_QT
153 if constexpr (DRA::WINDOW_MANAGER_ID == WindowManagerId::QT_WIDGET) {
154 ImGui_ImplQt_NewFrame();
155 }
156#endif // VCLIB_WITH_QT
157 ImGui::NewFrame();
158
159#ifdef VCLIB_RENDER_BACKEND_OPENGL2
160 this->onDrawContent(viewId);
161#elif defined(VCLIB_RENDER_BACKEND_BGFX)
162 this->onDrawContent(mImguiViewId);
163#endif // VCLIB_RENDER_BACKEND_*
164 }
165
166 virtual void onPostDraw()
167 {
168 // imgui rendering
169 ImGui::Render();
170#ifdef VCLIB_RENDER_BACKEND_OPENGL2
171 ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
172#elif defined(VCLIB_RENDER_BACKEND_BGFX)
173 ImGui_ImplBgfx_RenderDrawData(ImGui::GetDrawData());
174#endif // VCLIB_RENDER_BACKEND_*
175#ifndef __linux__
176 // FIXME #4: find a clean way to properly update the WindowManager
177 // as fast as possible (update inside the paintEvent is not allowed by
178 // qt)
179 derived()->update();
180#endif
181 }
182
183 virtual bool onKeyPress(Key::Enum key, const KeyModifiers& modifiers)
184 {
185 return wantCapture(false);
186 }
187
188 virtual bool onKeyRelease(Key::Enum key, const KeyModifiers& modifiers)
189 {
190 return wantCapture(false);
191 }
192
193 virtual bool onMouseMove(double x, double y, const KeyModifiers& modifiers)
194 {
195 return wantCapture();
196 }
197
198 virtual bool onMousePress(
199 MouseButton::Enum button,
200 double x,
201 double y,
202 const KeyModifiers& modifiers)
203 {
204 return wantCapture();
205 }
206
207 virtual bool onMouseRelease(
208 MouseButton::Enum button,
209 double x,
210 double y,
211 const KeyModifiers& modifiers)
212 {
213 return wantCapture();
214 }
215
216 virtual bool onMouseDoubleClick(
217 MouseButton::Enum button,
218 double x,
219 double y,
220 const KeyModifiers& modifiers)
221 {
222 return wantCapture();
223 }
224
225 virtual bool onMouseScroll(
226 double x,
227 double y,
228 const KeyModifiers& modifiers)
229 {
230 return wantCapture();
231 }
232
233protected:
234 bool isWindowMinimized() const { return derived()->isMinimized(); }
235
236private:
237 auto* derived() { return static_cast<DRA*>(this); }
238
239 const auto* derived() const { return static_cast<const DRA*>(this); }
240
241 bool wantCapture(bool mouse = true)
242 {
243 const auto& io = ImGui::GetIO();
244 return mouse ? io.WantCaptureMouse : io.WantCaptureKeyboard;
245 }
246};
247
248} // namespace vcl::imgui
249
250#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 box in N-dimensional space.
Definition box.h:46
static Context & instance(void *windowHandle=nullptr, void *displayHandle=nullptr)
Return the context instance.
Definition context.cpp:371
Definition imgui_drawer.h:49