PeriDyno 1.0.0
Loading...
Searching...
No Matches
ImGuiRenderer.cpp
Go to the documentation of this file.
1#include "ImGuiRenderer.h"
2#include <QDateTime>
3#include <QGuiApplication>
4#include <QMouseEvent>
5#include <QClipboard>
6#include <QCursor>
7
8#ifdef ANDROID
9#define GL_VERTEX_ARRAY_BINDING 0x85B5 // Missing in android as of May 2020
10#define USE_GLSL_ES
11#endif
12
13#ifdef USE_GLSL_ES
14#define IMGUIRENDERER_GLSL_VERSION "#version 300 es\n"
15#else
16#define IMGUIRENDERER_GLSL_VERSION "#version 330\n"
17#endif
18
19namespace QtImGui {
20
21namespace {
22
23// Keyboard mapping. Dear ImGui use those indices to peek into the io.KeysDown[] array.
24const QHash<int, ImGuiKey> keyMap = {
25 { Qt::Key_Tab, ImGuiKey_Tab },
26 { Qt::Key_Left, ImGuiKey_LeftArrow },
27 { Qt::Key_Right, ImGuiKey_RightArrow },
28 { Qt::Key_Up, ImGuiKey_UpArrow },
29 { Qt::Key_Down, ImGuiKey_DownArrow },
30 { Qt::Key_PageUp, ImGuiKey_PageUp },
31 { Qt::Key_PageDown, ImGuiKey_PageDown },
32 { Qt::Key_Home, ImGuiKey_Home },
33 { Qt::Key_End, ImGuiKey_End },
34 { Qt::Key_Insert, ImGuiKey_Insert },
35 { Qt::Key_Delete, ImGuiKey_Delete },
36 { Qt::Key_Backspace, ImGuiKey_Backspace },
37 { Qt::Key_Space, ImGuiKey_Space },
38 { Qt::Key_Enter, ImGuiKey_Enter },
39 { Qt::Key_Return, ImGuiKey_Enter },
40 { Qt::Key_Escape, ImGuiKey_Escape },
41 { Qt::Key_A, ImGuiKey_A },
42 { Qt::Key_C, ImGuiKey_C },
43 { Qt::Key_V, ImGuiKey_V },
44 { Qt::Key_X, ImGuiKey_X },
45 { Qt::Key_Y, ImGuiKey_Y },
46 { Qt::Key_Z, ImGuiKey_Z }
47 //{ Qt::MiddleButton, ImGuiMouseButton_Middle }
48};
49
50#ifndef QT_NO_CURSOR
51const QHash<ImGuiMouseCursor, Qt::CursorShape> cursorMap = {
52 { ImGuiMouseCursor_Arrow, Qt::CursorShape::ArrowCursor },
53 { ImGuiMouseCursor_TextInput, Qt::CursorShape::IBeamCursor },
54 { ImGuiMouseCursor_ResizeAll, Qt::CursorShape::SizeAllCursor },
55 { ImGuiMouseCursor_ResizeNS, Qt::CursorShape::SizeVerCursor },
56 { ImGuiMouseCursor_ResizeEW, Qt::CursorShape::SizeHorCursor },
57 { ImGuiMouseCursor_ResizeNESW, Qt::CursorShape::SizeBDiagCursor },
58 { ImGuiMouseCursor_ResizeNWSE, Qt::CursorShape::SizeFDiagCursor },
59 { ImGuiMouseCursor_Hand, Qt::CursorShape::PointingHandCursor },
60 { ImGuiMouseCursor_NotAllowed, Qt::CursorShape::ForbiddenCursor },
61};
62#endif
63
64QByteArray g_currentClipboardText;
65
66} // namespace
67
69 m_window.reset(window);
70 initializeOpenGLFunctions();
71
72 g_ctx = ImGui::CreateContext();
73 ImGui::SetCurrentContext(g_ctx);
74
75 // Setup backend capabilities flags
76 ImGuiIO &io = ImGui::GetIO();
77 #ifndef QT_NO_CURSOR
78 io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
79 io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
80 #endif
81 io.BackendPlatformName = "qtimgui";
82
83#if IMGUI_VERSION_NUM < 18700
84 // Setup keyboard mapping
85 for (ImGuiKey key : keyMap.values()) {
86 io.KeyMap[key] = key;
87 }
88#endif
89
90 // io.RenderDrawListsFn = [](ImDrawData *drawData) {
91 // instance()->renderDrawList(drawData);
92 // };
93 io.SetClipboardTextFn = [](void *user_data, const char *text) {
94 Q_UNUSED(user_data);
95 QGuiApplication::clipboard()->setText(text);
96 };
97 io.GetClipboardTextFn = [](void *user_data) {
98 Q_UNUSED(user_data);
99 g_currentClipboardText = QGuiApplication::clipboard()->text().toUtf8();
100 return (const char *)g_currentClipboardText.data();
101 };
102
103 window->installEventFilter(this);
104}
105
106void ImGuiRenderer::renderDrawList(ImDrawData *draw_data)
107{
108 // Select current context
109 ImGui::SetCurrentContext(g_ctx);
110
111 // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
112 const ImGuiIO& io = ImGui::GetIO();
113 int fb_width = (int)(io.DisplaySize.x * io.DisplayFramebufferScale.x);
114 int fb_height = (int)(io.DisplaySize.y * io.DisplayFramebufferScale.y);
115 if (fb_width == 0 || fb_height == 0)
116 return;
117 draw_data->ScaleClipRects(io.DisplayFramebufferScale);
118
119 // Backup GL state
120 GLint last_active_texture; glGetIntegerv(GL_ACTIVE_TEXTURE, &last_active_texture);
121 glActiveTexture(GL_TEXTURE0);
122 GLint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
123 GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
124 GLint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
125 GLint last_element_array_buffer; glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &last_element_array_buffer);
126 GLint last_vertex_array; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
127 GLint last_blend_src_rgb; glGetIntegerv(GL_BLEND_SRC_RGB, &last_blend_src_rgb);
128 GLint last_blend_dst_rgb; glGetIntegerv(GL_BLEND_DST_RGB, &last_blend_dst_rgb);
129 GLint last_blend_src_alpha; glGetIntegerv(GL_BLEND_SRC_ALPHA, &last_blend_src_alpha);
130 GLint last_blend_dst_alpha; glGetIntegerv(GL_BLEND_DST_ALPHA, &last_blend_dst_alpha);
131 GLint last_blend_equation_rgb; glGetIntegerv(GL_BLEND_EQUATION_RGB, &last_blend_equation_rgb);
132 GLint last_blend_equation_alpha; glGetIntegerv(GL_BLEND_EQUATION_ALPHA, &last_blend_equation_alpha);
133 GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport);
134 GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
135 GLboolean last_enable_blend = glIsEnabled(GL_BLEND);
136 GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE);
137 GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST);
138 GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
139
140 // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled
141 glEnable(GL_BLEND);
142 glBlendEquation(GL_FUNC_ADD);
143 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
144 glDisable(GL_CULL_FACE);
145 glDisable(GL_DEPTH_TEST);
146 glEnable(GL_SCISSOR_TEST);
147
148 // Setup viewport, orthographic projection matrix
149 glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height);
150 const float ortho_projection[4][4] =
151 {
152 { 2.0f/io.DisplaySize.x, 0.0f, 0.0f, 0.0f },
153 { 0.0f, 2.0f/-io.DisplaySize.y, 0.0f, 0.0f },
154 { 0.0f, 0.0f, -1.0f, 0.0f },
155 {-1.0f, 1.0f, 0.0f, 1.0f },
156 };
157 glUseProgram(g_ShaderHandle);
158 glUniform1i(g_AttribLocationTex, 0);
159 glUniformMatrix4fv(g_AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]);
160 glBindVertexArray(g_VaoHandle);
161
162 for (int n = 0; n < draw_data->CmdListsCount; n++)
163 {
164 const ImDrawList* cmd_list = draw_data->CmdLists[n];
165 const ImDrawIdx* idx_buffer_offset = 0;
166
167 glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle);
168 glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)cmd_list->VtxBuffer.Size * sizeof(ImDrawVert), (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW);
169
170 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_ElementsHandle);
171 glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx), (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW);
172
173 for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
174 {
175 const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
176 if (pcmd->UserCallback)
177 {
178 pcmd->UserCallback(cmd_list, pcmd);
179 }
180 else
181 {
182 glBindTexture(GL_TEXTURE_2D, (GLuint)(size_t)pcmd->TextureId);
183 glScissor((int)pcmd->ClipRect.x, (int)(fb_height - pcmd->ClipRect.w), (int)(pcmd->ClipRect.z - pcmd->ClipRect.x), (int)(pcmd->ClipRect.w - pcmd->ClipRect.y));
184 glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset);
185 }
186 idx_buffer_offset += pcmd->ElemCount;
187 }
188 }
189
190 // Restore modified GL state
191 glUseProgram(last_program);
192 glBindTexture(GL_TEXTURE_2D, last_texture);
193 glActiveTexture(last_active_texture);
194 glBindVertexArray(last_vertex_array);
195 glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
196 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer);
197 glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha);
198 glBlendFuncSeparate(last_blend_src_rgb, last_blend_dst_rgb, last_blend_src_alpha, last_blend_dst_alpha);
199 if (last_enable_blend) glEnable(GL_BLEND); else glDisable(GL_BLEND);
200 if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE);
201 if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
202 if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST);
203 glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
204 glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]);
205}
206
208{
209 // Select current context
210 ImGui::SetCurrentContext(g_ctx);
211
212 // Build texture atlas
213 ImGuiIO& io = ImGui::GetIO();
214 unsigned char* pixels;
215 int width, height;
216 io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bits (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory.
217
218 // Upload texture to graphics system
219 GLint last_texture;
220 glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
221 glGenTextures(1, &g_FontTexture);
222 glBindTexture(GL_TEXTURE_2D, g_FontTexture);
223 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
224 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
225 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
226
227 // Store our identifier
228 io.Fonts->TexID = (void *)(size_t)g_FontTexture;
229
230 // Restore state
231 glBindTexture(GL_TEXTURE_2D, last_texture);
232
233 return true;
234}
235
237{
238 // Select current context
239 ImGui::SetCurrentContext(g_ctx);
240
241 // Backup GL state
242 GLint last_texture, last_array_buffer, last_vertex_array;
243 glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
244 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
245 glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
246
247 const GLchar *vertex_shader =
249 "uniform mat4 ProjMtx;\n"
250 "in vec2 Position;\n"
251 "in vec2 UV;\n"
252 "in vec4 Color;\n"
253 "out vec2 Frag_UV;\n"
254 "out vec4 Frag_Color;\n"
255 "void main()\n"
256 "{\n"
257 " Frag_UV = UV;\n"
258 " Frag_Color = Color;\n"
259 " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
260 "}\n";
261
262 const GLchar* fragment_shader =
264 "precision mediump float;"
265 "uniform sampler2D Texture;\n"
266 "in vec2 Frag_UV;\n"
267 "in vec4 Frag_Color;\n"
268 "out vec4 Out_Color;\n"
269 "void main()\n"
270 "{\n"
271 " Out_Color = Frag_Color * texture( Texture, Frag_UV.st);\n"
272 "}\n";
273
274 g_ShaderHandle = glCreateProgram();
275 g_VertHandle = glCreateShader(GL_VERTEX_SHADER);
276 g_FragHandle = glCreateShader(GL_FRAGMENT_SHADER);
277 glShaderSource(g_VertHandle, 1, &vertex_shader, 0);
278 glShaderSource(g_FragHandle, 1, &fragment_shader, 0);
279 glCompileShader(g_VertHandle);
280 glCompileShader(g_FragHandle);
281 glAttachShader(g_ShaderHandle, g_VertHandle);
282 glAttachShader(g_ShaderHandle, g_FragHandle);
283 glLinkProgram(g_ShaderHandle);
284
285 g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "Texture");
286 g_AttribLocationProjMtx = glGetUniformLocation(g_ShaderHandle, "ProjMtx");
287 g_AttribLocationPosition = glGetAttribLocation(g_ShaderHandle, "Position");
288 g_AttribLocationUV = glGetAttribLocation(g_ShaderHandle, "UV");
289 g_AttribLocationColor = glGetAttribLocation(g_ShaderHandle, "Color");
290
291 glGenBuffers(1, &g_VboHandle);
292 glGenBuffers(1, &g_ElementsHandle);
293
294 glGenVertexArrays(1, &g_VaoHandle);
295 glBindVertexArray(g_VaoHandle);
296 glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle);
297 glEnableVertexAttribArray(g_AttribLocationPosition);
298 glEnableVertexAttribArray(g_AttribLocationUV);
299 glEnableVertexAttribArray(g_AttribLocationColor);
300
301#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
302 glVertexAttribPointer(g_AttribLocationPosition, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, pos));
303 glVertexAttribPointer(g_AttribLocationUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, uv));
304 glVertexAttribPointer(g_AttribLocationColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, col));
305#undef OFFSETOF
306
308
309 // Restore modified GL state
310 glBindTexture(GL_TEXTURE_2D, last_texture);
311 glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
312 glBindVertexArray(last_vertex_array);
313
314 return true;
315}
316
318{
319 // Select current context
320 ImGui::SetCurrentContext(g_ctx);
321
322 if (!g_FontTexture)
324
325 ImGuiIO& io = ImGui::GetIO();
326
327 // Setup display size (every frame to accommodate for window resizing)
328 io.DisplaySize = ImVec2(m_window->size().width(), m_window->size().height());
329 io.DisplayFramebufferScale = ImVec2(m_window->devicePixelRatio(), m_window->devicePixelRatio());
330
331 // Setup time step
332 double current_time = QDateTime::currentMSecsSinceEpoch() / double(1000);
333 io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
334 io.DeltaTime = io.DeltaTime < 0.001f ? 0.001f : io.DeltaTime;
335 g_Time = current_time;
336
337
338 // If ImGui wants to set cursor position (for example, during navigation by using keyboard)
339 // we need to do it here (before getting `QCursor::pos()` below).
340 setCursorPos(io);
341
342 // Setup inputs
343 // (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
344 if (m_window->isActive())
345 {
346 const QPoint pos = m_window->mapFromGlobal(QCursor::pos());
347 io.MousePos = ImVec2(pos.x(), pos.y()); // Mouse position in screen coordinates (set to -1,-1 if no mouse / on another screen, etc.)
348 }
349 else
350 {
351 io.MousePos = ImVec2(-1,-1);
352 }
353
354 for (int i = 0; i < 3; i++)
355 {
356 io.MouseDown[i] = g_MousePressed[i];
357 }
358
359 io.MouseWheelH = g_MouseWheelH;
360 io.MouseWheel = g_MouseWheel;
361 g_MouseWheelH = 0;
362 g_MouseWheel = 0;
363
364
366
367
368 // Start the frame
369 ImGui::NewFrame();
370}
371
373{
374 // Select current context
375 ImGui::SetCurrentContext(g_ctx);
376
377 auto drawData = ImGui::GetDrawData();
378 renderDrawList(drawData);
379}
380
382 : g_ctx(nullptr)
383{
384}
385
387{
388 // remove this context
389 ImGui::DestroyContext(g_ctx);
390}
391
393{
394 g_MousePressed[0] = event->buttons() & Qt::LeftButton;
395 g_MousePressed[1] = event->buttons() & Qt::RightButton;
396 g_MousePressed[2] = event->buttons() & Qt::MiddleButton;
397}
398
399void ImGuiRenderer::onWheel(QWheelEvent *event)
400{
401 // Select current context
402 ImGui::SetCurrentContext(g_ctx);
403
404 // Handle horizontal component
405 if(event->pixelDelta().x() != 0)
406 {
407 g_MouseWheelH += event->pixelDelta().x() / (ImGui::GetTextLineHeight());
408 } else {
409 // Magic number of 120 comes from Qt doc on QWheelEvent::pixelDelta()
410 g_MouseWheelH += event->angleDelta().x() / 120;
411 }
412
413 // Handle vertical component
414 if(event->pixelDelta().y() != 0)
415 {
416 // 5 lines per unit
417 g_MouseWheel += event->pixelDelta().y() / (5.0 * ImGui::GetTextLineHeight());
418 } else {
419 // Magic number of 120 comes from Qt doc on QWheelEvent::pixelDelta()
420 g_MouseWheel += event->angleDelta().y() / 120;
421 }
422}
423
425{
426 // Select current context
427 ImGui::SetCurrentContext(g_ctx);
428
429 ImGuiIO& io = ImGui::GetIO();
430
431 const bool key_pressed = (event->type() == QEvent::KeyPress);
432
433 // Translate `Qt::Key` into `ImGuiKey`, and apply 'pressed' state for that key
434 const auto key_it = keyMap.constFind( event->key() );
435 if (key_it != keyMap.constEnd()) { // Qt's key found in keyMap
436 const auto imgui_key = *(key_it);
437#if IMGUI_VERSION_NUM < 18700
438 io.KeysDown[imgui_key] = key_pressed;
439#else
440 io.AddKeyEvent(imgui_key, key_pressed);
441#endif
442 }
443
444 if (key_pressed) {
445 const QString text = event->text();
446 if (text.size() == 1) {
447 io.AddInputCharacter( text.at(0).unicode() );
448 }
449 }
450
451#ifdef Q_OS_MAC
452 io.KeyCtrl = event->modifiers() & Qt::MetaModifier;
453 io.KeyShift = event->modifiers() & Qt::ShiftModifier;
454 io.KeyAlt = event->modifiers() & Qt::AltModifier;
455 io.KeySuper = event->modifiers() & Qt::ControlModifier; // Comamnd key
456#else
457 io.KeyCtrl = event->modifiers() & Qt::ControlModifier;
458 io.KeyShift = event->modifiers() & Qt::ShiftModifier;
459 io.KeyAlt = event->modifiers() & Qt::AltModifier;
460 io.KeySuper = event->modifiers() & Qt::MetaModifier;
461#endif
462}
463
464void ImGuiRenderer::updateCursorShape(const ImGuiIO& io)
465{
466 // NOTE: This code will be executed, only if the following flags have been set:
467 // - backend flag: `ImGuiBackendFlags_HasMouseCursors` - enabled
468 // - config flag: `ImGuiConfigFlags_NoMouseCursorChange` - disabled
469
470#ifndef QT_NO_CURSOR
471 if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
472 return;
473
474 const ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
475 if (io.MouseDrawCursor || (imgui_cursor == ImGuiMouseCursor_None))
476 {
477 // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
478 m_window->setCursorShape(Qt::CursorShape::BlankCursor);
479 }
480 else
481 {
482 // Show OS mouse cursor
483
484 // Translate `ImGuiMouseCursor` into `Qt::CursorShape` and show it, if we can
485 const auto cursor_it = cursorMap.constFind( imgui_cursor );
486 if(cursor_it != cursorMap.constEnd()) // `Qt::CursorShape` found for `ImGuiMouseCursor`
487 {
488 const Qt::CursorShape qt_cursor_shape = *(cursor_it);
489 m_window->setCursorShape(qt_cursor_shape);
490 }
491 else // shape NOT found - use default
492 {
493 m_window->setCursorShape(Qt::CursorShape::ArrowCursor);
494 }
495 }
496#else
497 Q_UNUSED(io);
498#endif
499}
500
501void ImGuiRenderer::setCursorPos(const ImGuiIO& io)
502{
503 // NOTE: This code will be executed, only if the following flags have been set:
504 // - backend flag: `ImGuiBackendFlags_HasSetMousePos` - enabled
505 // - config flag: `ImGuiConfigFlags_NavEnableSetMousePos` - enabled
506
507#ifndef QT_NO_CURSOR
508 if(io.WantSetMousePos) {
509 m_window->setCursorPos({(int)io.MousePos.x, (int)io.MousePos.y});
510 }
511#else
512 Q_UNUSED(io);
513#endif
514}
515
516bool ImGuiRenderer::eventFilter(QObject *watched, QEvent *event)
517{
518 if (watched == m_window->object()) {
519 switch (event->type()) {
520 case QEvent::MouseButtonDblClick:
521 case QEvent::MouseButtonPress:
522 case QEvent::MouseButtonRelease:
523 this->onMousePressedChange(static_cast<QMouseEvent*>(event));
524 break;
525 case QEvent::Wheel:
526 this->onWheel(static_cast<QWheelEvent*>(event));
527 break;
528 case QEvent::KeyPress:
529 case QEvent::KeyRelease:
530 this->onKeyPressRelease(static_cast<QKeyEvent*>(event));
531 break;
532 default:
533 break;
534 }
535 }
536 return QObject::eventFilter(watched, event);
537}
538
540 static ImGuiRenderer* instance = nullptr;
541 if (!instance) {
542 instance = new ImGuiRenderer();
543 }
544 return instance;
545}
546
547} // namespace QtImGui
#define OFFSETOF(TYPE, ELEMENT)
#define IMGUIRENDERER_GLSL_VERSION
unsigned int g_ElementsHandle
void renderDrawList(ImDrawData *draw_data)
static ImGuiRenderer * instance()
void initialize(WindowWrapper *window)
void updateCursorShape(const ImGuiIO &io)
void onWheel(QWheelEvent *event)
std::unique_ptr< WindowWrapper > m_window
bool eventFilter(QObject *watched, QEvent *event)
void onKeyPressRelease(QKeyEvent *event)
void onMousePressedChange(QMouseEvent *event)
void setCursorPos(const ImGuiIO &io)
virtual void installEventFilter(QObject *object)=0