SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
ImGuiWrapper.cpp
Go to the documentation of this file.
1 /**
2  * \file ImGuiWrapper.cpp
3  * \brief Wrapper Class around the external ImGui GUI-framework
4 // See also: https://github.com/ocornut/imgui
5  * \date October 2015
6  * \authors Marcus Hudritsch
7  * \copyright http://opensource.org/licenses/GPL-3.0
8  * \remarks Please use clangformat to format the code. See more code style on
9  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
10 */
11 
12 #include <ImGuiWrapper.h>
13 #include <SLSceneView.h>
14 #include <SLScene.h>
15 #include <GlobalTimer.h>
16 
17 #include <imgui_internal.h>
18 
19 //-----------------------------------------------------------------------------
20 //! Prints the compile errors in case of a GLSL compile failure
22 {
23  // Check compiler log
24  SLint compileSuccess = 0;
25  glGetShaderiv((SLuint)shaderHandle, GL_COMPILE_STATUS, &compileSuccess);
26  if (compileSuccess == GL_FALSE)
27  {
28  GLchar log[512];
29  glGetShaderInfoLog((SLuint)shaderHandle,
30  sizeof(log),
31  nullptr,
32  &log[0]);
33  SL_LOG("*** COMPILER ERROR ***");
34  SL_LOG("%s\n---", log);
35  SL_LOG("%s", src);
36  }
37 }
38 //-----------------------------------------------------------------------------
39 //! Creates all OpenGL objects for drawing the imGui
41 {
42  // Backup GL state
43  GLint last_texture, last_array_buffer, last_vertex_array;
44  glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
45  glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
46  glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
47 
48  // Build version string as the first statement
49  SLGLState* state = SLGLState::instance();
50  SLstring verGLSL = state->glSLVersionNO();
51  SLstring vertex_shader = "#version " + verGLSL;
52  if (state->glIsES3()) vertex_shader += " es";
53  vertex_shader +=
54  "\n"
55  "#ifdef GL_ES\n"
56  "precision mediump float;\n"
57  "#endif\n"
58  "\n"
59  "uniform mat4 ProjMtx;\n"
60  "in vec2 Position;\n"
61  "in vec2 UV;\n"
62  "in vec4 Color;\n"
63  "out vec2 Frag_UV;\n"
64  "out vec4 Frag_Color;\n"
65  "void main()\n"
66  "{\n"
67  " Frag_UV = UV;\n"
68  " Frag_Color = Color;\n"
69  " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
70  "}\n";
71 
72  SLstring fragment_shader = "#version " + verGLSL;
73  if (state->glIsES3()) fragment_shader += " es";
74  fragment_shader +=
75  "\n"
76  "#ifdef GL_ES\n"
77  "precision mediump float;\n"
78  "#endif\n"
79  "\n"
80  "uniform sampler2D Texture;\n"
81  "in vec2 Frag_UV;\n"
82  "in vec4 Frag_Color;\n"
83  "out vec4 Out_Color;\n"
84  "void main()\n"
85  "{\n"
86  " Out_Color = Frag_Color * texture( Texture, Frag_UV.st);\n"
87  "}\n";
88 
89  _vertHandle = (SLint)glCreateShader(GL_VERTEX_SHADER);
90  _fragHandle = (SLint)glCreateShader(GL_FRAGMENT_SHADER);
91  const char* srcVert = vertex_shader.c_str();
92  const char* srcFrag = fragment_shader.c_str();
93  glShaderSource((SLuint)_vertHandle, 1, &srcVert, nullptr);
94  glShaderSource((SLuint)_fragHandle, 1, &srcFrag, nullptr);
95  glCompileShader((SLuint)_vertHandle);
97  glCompileShader((SLuint)_fragHandle);
99 
100  _progHandle = (SLint)glCreateProgram();
101  glAttachShader((SLuint)_progHandle, (SLuint)_vertHandle);
102  glAttachShader((SLuint)_progHandle, (SLuint)_fragHandle);
103  glLinkProgram((SLuint)_progHandle);
104 
105  GET_GL_ERROR;
106 
107  _attribLocTex = glGetUniformLocation((SLuint)_progHandle, "Texture");
108  _attribLocProjMtx = glGetUniformLocation((SLuint)_progHandle, "ProjMtx");
109  _attribLocPosition = glGetAttribLocation((SLuint)_progHandle, "Position");
110  _attribLocUV = glGetAttribLocation((SLuint)_progHandle, "UV");
111  _attribLocColor = glGetAttribLocation((SLuint)_progHandle, "Color");
112 
113  GET_GL_ERROR;
114 
115  glGenBuffers(1, &_vboHandle);
116  glGenBuffers(1, &_elementsHandle);
117 
118  glGenVertexArrays(1, &_vaoHandle);
119  glBindVertexArray(_vaoHandle);
120  glBindBuffer(GL_ARRAY_BUFFER, _vboHandle);
121  glEnableVertexAttribArray((SLuint)_attribLocPosition);
122  glEnableVertexAttribArray((SLuint)_attribLocUV);
123  glEnableVertexAttribArray((SLuint)_attribLocColor);
124 
125  GET_GL_ERROR;
126 
127 #define OFFSETOF(TYPE, ELEMENT) ((size_t) & (((TYPE*)nullptr)->ELEMENT))
128  glVertexAttribPointer((SLuint)_attribLocPosition,
129  2,
130  GL_FLOAT,
131  GL_FALSE,
132  sizeof(ImDrawVert),
133  (GLvoid*)OFFSETOF(ImDrawVert, pos));
134  glVertexAttribPointer((SLuint)_attribLocUV,
135  2,
136  GL_FLOAT,
137  GL_FALSE,
138  sizeof(ImDrawVert),
139  (GLvoid*)OFFSETOF(ImDrawVert, uv));
140  glVertexAttribPointer((SLuint)_attribLocColor,
141  4,
142  GL_UNSIGNED_BYTE,
143  GL_TRUE,
144  sizeof(ImDrawVert),
145  (GLvoid*)OFFSETOF(ImDrawVert, col));
146 #undef OFFSETOF
147 
148  GET_GL_ERROR;
149 
150  // Build texture atlas
151  ImGuiIO& io = _context->IO;
152  SLuchar* pixels;
153  int width, height;
154 
155  // Load as RGBA 32-bits (75% of the memory is wasted, but default font is
156  // so small) because it is more likely to be compatible with user's
157  // existing shaders. If your ImTextureId represent a higher-level concept
158  // than just a GL texture id, consider calling GetTexDataAsAlpha8()
159  // instead to save on GPU memory.
160  io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
161 
162  // Upload texture to graphics system
163  glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
164  glGenTextures(1, &_fontTexture);
165  glBindTexture(GL_TEXTURE_2D, _fontTexture);
166  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
167  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
168  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
169 
170  GET_GL_ERROR;
171 
172  // Store our identifier
173  io.Fonts->TexID = (void*)(intptr_t)_fontTexture;
174 
175  // Restore state
176  glBindTexture(GL_TEXTURE_2D, (SLuint)last_texture);
177 
178  // Restore modified GL state
179  glBindTexture(GL_TEXTURE_2D, (SLuint)last_texture);
180  glBindBuffer(GL_ARRAY_BUFFER, (SLuint)last_array_buffer);
181  glBindVertexArray((SLuint)last_vertex_array);
182 
183  GET_GL_ERROR;
184 }
185 //-----------------------------------------------------------------------------
186 //! Deletes all OpenGL objects for drawing the imGui
188 {
189  if (_vaoHandle) glDeleteVertexArrays(1, &_vaoHandle);
190  if (_vboHandle) glDeleteBuffers(1, &_vboHandle);
191  if (_elementsHandle) glDeleteBuffers(1, &_elementsHandle);
193 
194  if (_progHandle && _vertHandle) glDetachShader((SLuint)_progHandle,
196  if (_vertHandle) glDeleteShader((SLuint)_vertHandle);
197  _vertHandle = 0;
198 
199  if (_progHandle && _fragHandle) glDetachShader((SLuint)_progHandle,
201  if (_fragHandle) glDeleteShader((SLuint)_fragHandle);
202  _fragHandle = 0;
203 
204  if (_progHandle) glDeleteProgram((SLuint)_progHandle);
205  _progHandle = 0;
206 
207  if (_fontTexture)
208  {
209  glDeleteTextures(1, &_fontTexture);
210  _context->IO.Fonts->TexID = nullptr;
211  _fontTexture = 0;
212  }
213 }
214 //-----------------------------------------------------------------------------
215 void ImGuiRendererOpenGL::render(const SLRecti& viewportRect)
216 {
217  ImGui::SetCurrentContext(_context);
218  ImGui::Render();
219  ImDrawData* draw_data = ImGui::GetDrawData();
220 
221  ImGuiIO& io = _context->IO;
222 
223  // Avoid rendering when minimized, scale coordinates for retina displays
224  // (screen coordinates != framebuffer coordinates)
225  int fbWidth = (int)(io.DisplaySize.x * io.DisplayFramebufferScale.x);
226  int fbHeight = (int)(io.DisplaySize.y * io.DisplayFramebufferScale.y);
227  if (fbWidth == 0 || fbHeight == 0)
228  return;
229  draw_data->ScaleClipRects(io.DisplayFramebufferScale);
230 
231  // Backup GL state
232  GLint last_active_texture;
233  glGetIntegerv(GL_ACTIVE_TEXTURE, &last_active_texture);
234  glActiveTexture(GL_TEXTURE0);
235 
236  GLint last_program;
237  glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
238  GLint last_texture;
239  glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
240  GLint last_array_buffer;
241  glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
242  GLint last_element_array_buffer;
243  glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &last_element_array_buffer);
244  GLint last_vertex_array;
245  glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
246  GLint last_blend_src_rgb;
247  glGetIntegerv(GL_BLEND_SRC_RGB, &last_blend_src_rgb);
248  GLint last_blend_dst_rgb;
249  glGetIntegerv(GL_BLEND_DST_RGB, &last_blend_dst_rgb);
250  GLint last_blend_src_alpha;
251  glGetIntegerv(GL_BLEND_SRC_ALPHA, &last_blend_src_alpha);
252  GLint last_blend_dst_alpha;
253  glGetIntegerv(GL_BLEND_DST_ALPHA, &last_blend_dst_alpha);
254  GLint last_blend_equation_rgb;
255  glGetIntegerv(GL_BLEND_EQUATION_RGB, &last_blend_equation_rgb);
256  GLint last_blend_equation_alpha;
257  glGetIntegerv(GL_BLEND_EQUATION_ALPHA, &last_blend_equation_alpha);
258  GLint last_viewport[4];
259  glGetIntegerv(GL_VIEWPORT, last_viewport);
260  GLint last_scissor_box[4];
261  glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
262 
263  GLboolean last_enable_blend = glIsEnabled(GL_BLEND);
264  GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE);
265  GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST);
266  GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
267 
268  // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled
269  glEnable(GL_BLEND);
270  glBlendEquation(GL_FUNC_ADD);
271  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
272  glDisable(GL_CULL_FACE);
273  glDisable(GL_DEPTH_TEST);
274  glEnable(GL_SCISSOR_TEST);
275 
276  // Setup viewport
277  if (viewportRect.isEmpty())
278  glViewport(0,
279  0,
280  (GLsizei)fbWidth,
281  (GLsizei)fbHeight);
282  else
283  glViewport((GLsizei)(viewportRect.x * io.DisplayFramebufferScale.x),
284  (GLsizei)(viewportRect.y * io.DisplayFramebufferScale.y),
285  (GLsizei)(viewportRect.width * io.DisplayFramebufferScale.x),
286  (GLsizei)(viewportRect.height * io.DisplayFramebufferScale.y));
287 
288  // Setup orthographic projection matrix
289  // clang-format off
290  const float ortho_projection[4][4] =
291  {
292  {2.0f / io.DisplaySize.x, 0.0f, 0.0f, 0.0f},
293  {0.0f, 2.0f / -io.DisplaySize.y, 0.0f, 0.0f},
294  {0.0f, 0.0f, -1.0f, 0.0f},
295  {-1.0f, 1.0f, 0.0f, 1.0f},
296  };
297  // clang-format on
298 
299  glUseProgram((SLuint)_progHandle);
300  glUniform1i(_attribLocTex, 0);
301  glUniformMatrix4fv(_attribLocProjMtx, 1, GL_FALSE, &ortho_projection[0][0]);
302  glBindVertexArray((SLuint)_vaoHandle);
303 
304  for (int n = 0; n < draw_data->CmdListsCount; n++)
305  {
306  const ImDrawList* cmd_list = draw_data->CmdLists[n];
307  const ImDrawIdx* idx_buffer_offset = nullptr;
308 
309  glBindBuffer(GL_ARRAY_BUFFER, _vboHandle);
310  glBufferData(GL_ARRAY_BUFFER,
311  (GLsizeiptr)cmd_list->VtxBuffer.Size * (GLsizeiptr)sizeof(ImDrawVert),
312  (const GLvoid*)cmd_list->VtxBuffer.Data,
313  GL_STREAM_DRAW);
314 
315  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _elementsHandle);
316  glBufferData(GL_ELEMENT_ARRAY_BUFFER,
317  (GLsizeiptr)cmd_list->IdxBuffer.Size * (GLsizeiptr)sizeof(ImDrawIdx),
318  (const GLvoid*)cmd_list->IdxBuffer.Data,
319  GL_STREAM_DRAW);
320 
321  for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
322  {
323  const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
324  if (pcmd->UserCallback)
325  {
326  pcmd->UserCallback(cmd_list, pcmd);
327  }
328  else
329  {
330  glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId);
331 
332  if (viewportRect.isEmpty())
333  glScissor((int)pcmd->ClipRect.x,
334  (int)(fbHeight - pcmd->ClipRect.w),
335  (int)(pcmd->ClipRect.z - pcmd->ClipRect.x),
336  (int)(pcmd->ClipRect.w - pcmd->ClipRect.y));
337  else
338  glScissor((GLsizei)(viewportRect.x * io.DisplayFramebufferScale.x),
339  (GLsizei)(viewportRect.y * io.DisplayFramebufferScale.y),
340  (GLsizei)(viewportRect.width * io.DisplayFramebufferScale.x),
341  (GLsizei)(viewportRect.height * io.DisplayFramebufferScale.y));
342 
343  glDrawElements(GL_TRIANGLES,
344  (GLsizei)pcmd->ElemCount,
345  sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
346  idx_buffer_offset);
347  }
348  idx_buffer_offset += pcmd->ElemCount;
349  }
350  }
351 
352  // Restore modified GL state
353  glUseProgram((SLuint)last_program);
354  glBindTexture(GL_TEXTURE_2D, (SLuint)last_texture);
355  glActiveTexture((SLuint)last_active_texture);
356  glBindVertexArray((SLuint)last_vertex_array);
357  glBindBuffer(GL_ARRAY_BUFFER, (SLuint)last_array_buffer);
358  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (SLuint)last_element_array_buffer);
359  glBlendEquationSeparate((SLuint)last_blend_equation_rgb,
360  (SLuint)last_blend_equation_alpha);
361  glBlendFuncSeparate((SLuint)last_blend_src_rgb,
362  (SLuint)last_blend_dst_rgb,
363  (SLuint)last_blend_src_alpha,
364  (SLuint)last_blend_dst_alpha);
365  if (last_enable_blend)
366  glEnable(GL_BLEND);
367  else
368  glDisable(GL_BLEND);
369 
370  if (last_enable_cull_face)
371  glEnable(GL_CULL_FACE);
372  else
373  glDisable(GL_CULL_FACE);
374 
375  if (last_enable_depth_test)
376  glEnable(GL_DEPTH_TEST);
377  else
378  glDisable(GL_DEPTH_TEST);
379 
380  if (last_enable_scissor_test)
381  glEnable(GL_SCISSOR_TEST);
382  else
383  glDisable(GL_SCISSOR_TEST);
384 
385  glViewport(last_viewport[0],
386  last_viewport[1],
387  (GLsizei)last_viewport[2],
388  (GLsizei)last_viewport[3]);
389 
390  glScissor(last_scissor_box[0],
391  last_scissor_box[1],
392  (GLsizei)last_scissor_box[2],
393  (GLsizei)last_scissor_box[3]);
394 
395  ImGui::SetCurrentContext(nullptr);
396 }
397 //-----------------------------------------------------------------------------
398 void ImGuiEngine::init(const std::string& configPath)
399 {
400  ImGuiIO& io = _context->IO;
401  _iniFilename = configPath + "imgui.ini";
402  io.IniFilename = _iniFilename.c_str();
403 
404  io.KeyMap[ImGuiKey_Tab] = K_tab;
405  io.KeyMap[ImGuiKey_LeftArrow] = K_left;
406  io.KeyMap[ImGuiKey_RightArrow] = K_right;
407  io.KeyMap[ImGuiKey_UpArrow] = K_up;
408  io.KeyMap[ImGuiKey_DownArrow] = K_down;
409  io.KeyMap[ImGuiKey_PageUp] = K_pageUp;
410  io.KeyMap[ImGuiKey_PageDown] = K_pageUp;
411  io.KeyMap[ImGuiKey_Home] = K_home;
412  io.KeyMap[ImGuiKey_End] = K_end;
413  io.KeyMap[ImGuiKey_Delete] = K_delete;
414  io.KeyMap[ImGuiKey_Backspace] = K_backspace;
415  io.KeyMap[ImGuiKey_Enter] = K_enter;
416  io.KeyMap[ImGuiKey_Escape] = K_esc;
417  io.KeyMap[ImGuiKey_A] = 'A';
418  io.KeyMap[ImGuiKey_C] = 'C';
419  io.KeyMap[ImGuiKey_V] = 'V';
420  io.KeyMap[ImGuiKey_X] = 'X';
421  io.KeyMap[ImGuiKey_Y] = 'Y';
422  io.KeyMap[ImGuiKey_Z] = 'Z';
423 
424  // The screen size is set again in onResize
425  io.DisplaySize = ImVec2(0, 0);
426  io.DisplayFramebufferScale = ImVec2(1, 1);
427 }
428 //-----------------------------------------------------------------------------
429 
430 //-----------------------------------------------------------------------------
431 ImGuiWrapper::ImGuiWrapper(ImGuiContext* context, ImGuiRenderer* renderer)
432  : _context(context),
433  _renderer(renderer)
434 {
435  assert(_context);
436  assert(_renderer);
437 }
438 //-----------------------------------------------------------------------------
440 {
441 }
442 //-----------------------------------------------------------------------------
443 //! Initializes OpenGL handles to zero and sets the ImGui key map
444 void ImGuiWrapper::init(const std::string& configPath)
445 {
446  _mouseWheel = 0.0f;
447 }
448 //-----------------------------------------------------------------------------
449 //! Inits a new frame for the ImGui system
451 {
452  // If no build function is provided there is no ImGui
453  // if (!build) return;
454 
455  // if ((SLint)ImGuiWrapper::fontPropDots != (SLint)_fontPropDots ||
456  // (SLint)ImGuiWrapper::fontFixedDots != (SLint)_fontFixedDots)
457  // loadFonts(ImGuiWrapper::fontPropDots, ImGuiWrapper::fontFixedDots);
458 
459  // if (!_fontTexture)
460  // createOpenGLObjects();
461 
462  ImGuiIO& io = _context->IO;
463 
464  // Setup time step
465  SLfloat nowSec = GlobalTimer::timeS();
466  io.DeltaTime = _timeSec > 0.0 ? nowSec - _timeSec : 1.0f / 60.0f;
467  if (io.DeltaTime < 0) io.DeltaTime = 1.0f / 60.0f;
468  _timeSec = nowSec;
469 
470  if (_panScroll.enabled())
471  {
472  io.MouseWheel = _panScroll.getScrollInMouseWheelCoords(io.MouseDown[0], _context->FontSize, nowSec);
473  }
474  else
475  {
476  io.MouseWheel = _mouseWheel;
477  _mouseWheel = 0.0f;
478  }
479 
480  // Start the frame
481  ImGui::SetCurrentContext(_context);
482  ImGui::NewFrame();
483 
484  // Call the build function to construct the ui
485  build(s, sv);
486  ImGui::SetCurrentContext(nullptr);
487  // SL_LOG(".");
488 }
489 //-----------------------------------------------------------------------------
490 //! Callback if window got resized
492  SLint scrH)
493 {
494  ImGuiIO& io = _context->IO;
495  io.DisplaySize = ImVec2((SLfloat)scrW, (SLfloat)scrH);
496  io.DisplayFramebufferScale = ImVec2(1, 1);
497 }
498 //-----------------------------------------------------------------------------
499 //! Callback for main rendering for the ImGui GUI system
500 void ImGuiWrapper::onPaint(const SLRecti& viewportRect)
501 {
502  // do the opengl rendering
503  _renderer->render(viewportRect);
504 }
505 //-----------------------------------------------------------------------------
506 //! Callback on mouse button down event
508 {
509  ImGuiIO& io = _context->IO;
510  io.MousePos = ImVec2((SLfloat)x, (SLfloat)y);
511  if (button == MB_left)
512  {
513  io.MouseDown[0] = true;
514  if (_panScroll.enabled())
516  }
517  if (button == MB_right) io.MouseDown[1] = true;
518  if (button == MB_middle) io.MouseDown[2] = true;
519  // SL_LOG("D");
520 }
521 //-----------------------------------------------------------------------------
522 //! Callback on mouse button up event
524 {
525  ImGuiIO& io = _context->IO;
526  io.MousePos = ImVec2((SLfloat)x, (SLfloat)y);
527  if (button == MB_left) io.MouseDown[0] = false;
528  if (button == MB_right) io.MouseDown[1] = false;
529  if (button == MB_middle) io.MouseDown[2] = false;
530  // SL_LOG("U");
531 }
532 //-----------------------------------------------------------------------------
533 //! Updates the mouse cursor position
535 {
536  _context->IO.MousePos = ImVec2((SLfloat)xPos, (SLfloat)yPos);
537  ImGuiIO& io = _context->IO;
538  if (io.MouseDown[0] && _panScroll.enabled())
539  _panScroll.moveTo((float)yPos);
540  // SL_LOG("M");
541 }
542 //-----------------------------------------------------------------------------
543 //! Callback for the mouse scroll movement
545 {
546  // Use fractional mouse wheel, 1.0 unit 5 lines.
547  _mouseWheel += yoffset;
548 }
549 //-----------------------------------------------------------------------------
550 //! Callback on key press event
552 {
553  ImGuiIO& io = _context->IO;
554  io.KeysDown[key] = true;
555  io.KeyCtrl = mod & K_ctrl ? true : false;
556  io.KeyShift = mod & K_shift ? true : false;
557  io.KeyAlt = mod & K_alt ? true : false;
558 }
559 //-----------------------------------------------------------------------------
560 //! Callback on key release event
562 {
563  ImGuiIO& io = _context->IO;
564  io.KeysDown[key] = false;
565  io.KeyCtrl = mod & K_ctrl ? true : false;
566  io.KeyShift = mod & K_shift ? true : false;
567  io.KeyAlt = mod & K_alt ? true : false;
568 }
569 //-----------------------------------------------------------------------------
570 //! Callback on character input
572 {
573  ImGuiIO& io = _context->IO;
574  if (c > 0 && c < 0x10000)
575  io.AddInputCharacter((unsigned short)c);
576 }
577 //-----------------------------------------------------------------------------
578 //! Callback on closing the application
580 {
581 }
582 //-----------------------------------------------------------------------------
583 //! Renders an extra frame with the current mouse position
585 {
586  // If ImGui build function exists render the ImGui
587  _context->IO.MousePos = ImVec2((SLfloat)mouseX, (SLfloat)mouseY);
588  onInitNewFrame(s, sv);
589  onPaint(sv->viewportRect());
590 }
591 //-----------------------------------------------------------------------------
593 {
594  return _context->IO.WantCaptureKeyboard;
595 }
596 //-----------------------------------------------------------------------------
598 {
599  return _context->IO.WantCaptureMouse;
600 }
601 //-----------------------------------------------------------------------------
static SLint mouseX
Last mouse position x in pixels.
static SLint mouseY
Last mouse position y in pixels.
#define OFFSETOF(TYPE, ELEMENT)
float SLfloat
Definition: SL.h:173
#define SL_LOG(...)
Definition: SL.h:233
unsigned int SLuint
Definition: SL.h:171
char SLchar
Definition: SL.h:162
unsigned char SLuchar
Definition: SL.h:163
string SLstring
Definition: SL.h:158
int SLint
Definition: SL.h:170
SLMouseButton
Mouse button codes.
Definition: SLEnums.h:98
@ MB_left
Definition: SLEnums.h:100
@ MB_right
Definition: SLEnums.h:102
@ MB_middle
Definition: SLEnums.h:101
SLKey
Keyboard key codes enumeration.
Definition: SLEnums.h:16
@ K_down
Definition: SLEnums.h:25
@ K_delete
Definition: SLEnums.h:23
@ K_up
Definition: SLEnums.h:24
@ K_enter
Definition: SLEnums.h:20
@ K_esc
Definition: SLEnums.h:21
@ K_tab
Definition: SLEnums.h:19
@ K_shift
Definition: SLEnums.h:62
@ K_end
Definition: SLEnums.h:29
@ K_right
Definition: SLEnums.h:26
@ K_pageUp
Definition: SLEnums.h:31
@ K_ctrl
Definition: SLEnums.h:63
@ K_alt
Definition: SLEnums.h:64
@ K_left
Definition: SLEnums.h:27
@ K_backspace
Definition: SLEnums.h:22
@ K_home
Definition: SLEnums.h:28
#define GET_GL_ERROR
Definition: SLGLState.h:56
static float timeS()
Definition: GlobalTimer.cpp:20
ImGuiContext * _context
Definition: ImGuiWrapper.h:122
void init(const std::string &configPath)
std::string _iniFilename
Definition: ImGuiWrapper.h:123
virtual void render(const SLRecti &viewportRect)
Definition: ImGuiWrapper.h:40
ImGuiContext * _context
Definition: ImGuiWrapper.h:43
int _fragHandle
OpenGL handle for fragment shader.
Definition: ImGuiWrapper.h:75
int _attribLocPosition
OpenGL attribute location for vertex pos.
Definition: ImGuiWrapper.h:78
void printCompileErrors(SLint shaderHandle, const SLchar *src)
Prints the compile errors in case of a GLSL compile failure.
int _vertHandle
OpenGL handle for vertex shader.
Definition: ImGuiWrapper.h:74
int _attribLocColor
OpenGL attribute location for color.
Definition: ImGuiWrapper.h:80
int _attribLocUV
OpenGL attribute location for texture coords.
Definition: ImGuiWrapper.h:79
int _attribLocTex
OpenGL attribute location for texture.
Definition: ImGuiWrapper.h:76
unsigned int _elementsHandle
OpenGL handle for vertex indexes.
Definition: ImGuiWrapper.h:83
int _attribLocProjMtx
OpenGL attribute location for ???
Definition: ImGuiWrapper.h:77
unsigned int _fontTexture
OpenGL texture id for font.
Definition: ImGuiWrapper.h:72
void deleteOpenGLObjects()
Deletes all OpenGL objects for drawing the imGui.
int _progHandle
OpenGL handle for shader program.
Definition: ImGuiWrapper.h:73
unsigned int _vboHandle
OpenGL handle for vertex buffer object.
Definition: ImGuiWrapper.h:81
void render(const SLRecti &viewportRect) override
void createOpenGLObjects()
Creates all OpenGL objects for drawing the imGui.
unsigned int _vaoHandle
OpenGL vertex array object handle.
Definition: ImGuiWrapper.h:82
void onMouseDown(SLMouseButton button, SLint x, SLint y) override
Callback on mouse button down event.
ImGuiWrapper(ImGuiContext *context, ImGuiRenderer *renderer)
virtual void build(SLScene *s, SLSceneView *sv)=0
PanScrolling _panScroll
Definition: ImGuiWrapper.h:277
void init(const std::string &configPath) override
Initializes OpenGL handles to zero and sets the ImGui key map.
void onMouseWheel(SLfloat yoffset) override
Callback for the mouse scroll movement.
ImGuiContext * _context
Definition: ImGuiWrapper.h:278
void onMouseUp(SLMouseButton button, SLint x, SLint y) override
Callback on mouse button up event.
void onKeyPress(SLKey key, SLKey mod) override
Callback on key press event.
~ImGuiWrapper() override
void onResize(SLint scrW, SLint scrH) override
Callback if window got resized.
void onClose() override
Callback on closing the application.
void onMouseMove(SLint xPos, SLint yPos) override
Updates the mouse cursor position.
SLfloat _timeSec
Time in seconds.
Definition: ImGuiWrapper.h:281
bool doNotDispatchKeyboard() override
inform if user keyboard input was consumed by the ui
SLfloat _mouseWheel
Mouse wheel position.
Definition: ImGuiWrapper.h:282
void onPaint(const SLRecti &viewport) override
Callback for main rendering for the ImGui GUI system.
void onInitNewFrame(SLScene *s, SLSceneView *sv) override
Inits a new frame for the ImGui system.
void onCharInput(SLuint c) override
Callback on character input.
void renderExtraFrame(SLScene *s, SLSceneView *sv, SLint mouseX, SLint mouseY) override
Renders an extra frame with the current mouse position.
ImGuiRenderer * _renderer
Definition: ImGuiWrapper.h:284
void onKeyRelease(SLKey key, SLKey mod) override
Callback on key release event.
bool doNotDispatchMouse() override
inform if user mouse input was consumed by the ui
void start(const float yPos)
Definition: ImGuiWrapper.h:154
void moveTo(const float yPos)
Definition: ImGuiWrapper.h:147
bool enabled() const
Definition: ImGuiWrapper.h:210
float getScrollInMouseWheelCoords(const bool mouseDown, const float fontSize, const float t)
Definition: ImGuiWrapper.h:165
Singleton class holding all OpenGL states.
Definition: SLGLState.h:71
static SLGLState * instance()
Public static instance getter for singleton pattern.
Definition: SLGLState.h:74
SLbool glIsES3() const
Definition: SLGLState.h:136
SLstring glSLVersionNO()
Definition: SLGLState.h:133
T width
Definition: SLRect.h:29
T y
Definition: SLRect.h:29
SLbool isEmpty() const
Definition: SLRect.h:74
T x
Definition: SLRect.h:29
T height
Definition: SLRect.h:29
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47
SceneView class represents a dynamic real time 3D view onto the scene.
Definition: SLSceneView.h:69
SLRecti viewportRect() const
Definition: SLSceneView.h:176
T mod(T a, T b)
Definition: Utils.h:250
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
Definition: Utils.cpp:1103