SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
ImGuiWrapper Class Referenceabstract

ImGui Interface class for forwarding all events to the ImGui Handlers. More...

#include <ImGuiWrapper.h>

Inheritance diagram for ImGuiWrapper:
[legend]

Public Member Functions

 ImGuiWrapper (ImGuiContext *context, ImGuiRenderer *renderer)
 
 ~ImGuiWrapper () override
 
void init (const std::string &configPath) override
 Initializes OpenGL handles to zero and sets the ImGui key map. More...
 
void onInitNewFrame (SLScene *s, SLSceneView *sv) override
 Inits a new frame for the ImGui system. More...
 
void onResize (SLint scrW, SLint scrH) override
 Callback if window got resized. More...
 
void onPaint (const SLRecti &viewport) override
 Callback for main rendering for the ImGui GUI system. More...
 
void onMouseDown (SLMouseButton button, SLint x, SLint y) override
 Callback on mouse button down event. More...
 
void onMouseUp (SLMouseButton button, SLint x, SLint y) override
 Callback on mouse button up event. More...
 
void onMouseMove (SLint xPos, SLint yPos) override
 Updates the mouse cursor position. More...
 
void onMouseWheel (SLfloat yoffset) override
 Callback for the mouse scroll movement. More...
 
void onKeyPress (SLKey key, SLKey mod) override
 Callback on key press event. More...
 
void onKeyRelease (SLKey key, SLKey mod) override
 Callback on key release event. More...
 
void onCharInput (SLuint c) override
 Callback on character input. More...
 
void onClose () override
 Callback on closing the application. More...
 
void renderExtraFrame (SLScene *s, SLSceneView *sv, SLint mouseX, SLint mouseY) override
 Renders an extra frame with the current mouse position. More...
 
bool doNotDispatchKeyboard () override
 inform if user keyboard input was consumed by the ui More...
 
bool doNotDispatchMouse () override
 inform if user mouse input was consumed by the ui More...
 
virtual void build (SLScene *s, SLSceneView *sv)=0
 
- Public Member Functions inherited from SLUiInterface
virtual ~SLUiInterface ()
 
virtual void init (const string &configPath)
 initialization (called by SLSceneView init) More...
 
virtual void onResize (const SLRecti &viewportRect)
 inform the ui about scene view size change More...
 
virtual void drawMouseCursor (bool doDraw)
 Turns on or off the mouse cursor drawing. More...
 

Protected Attributes

PanScrolling _panScroll
 
ImGuiContext * _context {nullptr}
 

Private Attributes

SLfloat _timeSec
 Time in seconds. More...
 
SLfloat _mouseWheel {0}
 Mouse wheel position. More...
 
ImGuiRenderer_renderer {nullptr}
 

Detailed Description

ImGui Interface class for forwarding all events to the ImGui Handlers.

ImGui is a super easy GUI library for the rendering of a UI with OpenGL. For more information see: https://github.com/ocornut/imgui

This class provides only the interface into ImGui. In the event handlers of SLSceneView the according callback in ImGui is called.
There is no UI drawn with this class. It must be defined in another class that provides the build function. For the Demo apps this is done in the class SLDemoGui and the build function is passed e.g. in glfwMain function of the app-demo project.

The full call stack for rendering one frame is:

  • The top-level onPaint of the app (Win, Linux, MacOS, Android or iOS)
    • slUpdateAndPaint: C-Interface function of SLProject
      • SLSceneView::onPaint: Main onPaint function of a sceneview
        • SLImGui::newFrame: Initializes a new GUI frame
          • ImGui::NewFrame()
          • SLImGui::build: The UI build function
        • ... normal scene rendering of SLProject
        • SLSceneView::draw2DGL:
          • ImGui::Render
            • SLImGui::render(ImGui::GetDrawData())
            • SLDemoGui::buildDemoGui: Builds the full UI ImGuiWrapper implements SLUiInterface for ImGui.

The interface function are called by a SLSceneView instance

Definition at line 250 of file ImGuiWrapper.h.

Constructor & Destructor Documentation

◆ ImGuiWrapper()

ImGuiWrapper::ImGuiWrapper ( ImGuiContext *  context,
ImGuiRenderer renderer 
)

Definition at line 431 of file ImGuiWrapper.cpp.

432  : _context(context),
433  _renderer(renderer)
434 {
435  assert(_context);
436  assert(_renderer);
437 }
ImGuiContext * _context
Definition: ImGuiWrapper.h:278
ImGuiRenderer * _renderer
Definition: ImGuiWrapper.h:284

◆ ~ImGuiWrapper()

ImGuiWrapper::~ImGuiWrapper ( )
override

Definition at line 439 of file ImGuiWrapper.cpp.

440 {
441 }

Member Function Documentation

◆ build()

virtual void ImGuiWrapper::build ( SLScene s,
SLSceneView sv 
)
pure virtual

◆ doNotDispatchKeyboard()

bool ImGuiWrapper::doNotDispatchKeyboard ( )
overridevirtual

inform if user keyboard input was consumed by the ui

Reimplemented from SLUiInterface.

Definition at line 592 of file ImGuiWrapper.cpp.

593 {
594  return _context->IO.WantCaptureKeyboard;
595 }

◆ doNotDispatchMouse()

bool ImGuiWrapper::doNotDispatchMouse ( )
overridevirtual

inform if user mouse input was consumed by the ui

(e.g. the ui was hit by a mouse click. In this case the user input would not be forwarded to 3D scene graph)

Reimplemented from SLUiInterface.

Definition at line 597 of file ImGuiWrapper.cpp.

598 {
599  return _context->IO.WantCaptureMouse;
600 }

◆ init()

void ImGuiWrapper::init ( const std::string &  configPath)
override

Initializes OpenGL handles to zero and sets the ImGui key map.

Definition at line 444 of file ImGuiWrapper.cpp.

445 {
446  _mouseWheel = 0.0f;
447 }
SLfloat _mouseWheel
Mouse wheel position.
Definition: ImGuiWrapper.h:282

◆ onCharInput()

void ImGuiWrapper::onCharInput ( SLuint  c)
overridevirtual

Callback on character input.

Reimplemented from SLUiInterface.

Definition at line 571 of file ImGuiWrapper.cpp.

572 {
573  ImGuiIO& io = _context->IO;
574  if (c > 0 && c < 0x10000)
575  io.AddInputCharacter((unsigned short)c);
576 }

◆ onClose()

void ImGuiWrapper::onClose ( )
overridevirtual

Callback on closing the application.

Reimplemented from SLUiInterface.

Definition at line 579 of file ImGuiWrapper.cpp.

580 {
581 }

◆ onInitNewFrame()

void ImGuiWrapper::onInitNewFrame ( SLScene s,
SLSceneView sv 
)
overridevirtual

Inits a new frame for the ImGui system.

Reimplemented from SLUiInterface.

Definition at line 450 of file ImGuiWrapper.cpp.

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 }
float SLfloat
Definition: SL.h:173
static float timeS()
Definition: GlobalTimer.cpp:20
virtual void build(SLScene *s, SLSceneView *sv)=0
PanScrolling _panScroll
Definition: ImGuiWrapper.h:277
SLfloat _timeSec
Time in seconds.
Definition: ImGuiWrapper.h:281
bool enabled() const
Definition: ImGuiWrapper.h:210
float getScrollInMouseWheelCoords(const bool mouseDown, const float fontSize, const float t)
Definition: ImGuiWrapper.h:165
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

◆ onKeyPress()

void ImGuiWrapper::onKeyPress ( SLKey  key,
SLKey  mod 
)
overridevirtual

Callback on key press event.

Reimplemented from SLUiInterface.

Definition at line 551 of file ImGuiWrapper.cpp.

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 }
@ K_shift
Definition: SLEnums.h:62
@ K_ctrl
Definition: SLEnums.h:63
@ K_alt
Definition: SLEnums.h:64
T mod(T a, T b)
Definition: Utils.h:250

◆ onKeyRelease()

void ImGuiWrapper::onKeyRelease ( SLKey  key,
SLKey  mod 
)
overridevirtual

Callback on key release event.

Reimplemented from SLUiInterface.

Definition at line 561 of file ImGuiWrapper.cpp.

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 }

◆ onMouseDown()

void ImGuiWrapper::onMouseDown ( SLMouseButton  button,
SLint  x,
SLint  y 
)
overridevirtual

Callback on mouse button down event.

Reimplemented from SLUiInterface.

Definition at line 507 of file ImGuiWrapper.cpp.

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 }
@ MB_left
Definition: SLEnums.h:100
@ MB_right
Definition: SLEnums.h:102
@ MB_middle
Definition: SLEnums.h:101
void start(const float yPos)
Definition: ImGuiWrapper.h:154

◆ onMouseMove()

void ImGuiWrapper::onMouseMove ( SLint  xPos,
SLint  yPos 
)
overridevirtual

Updates the mouse cursor position.

Reimplemented from SLUiInterface.

Definition at line 534 of file ImGuiWrapper.cpp.

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 }
void moveTo(const float yPos)
Definition: ImGuiWrapper.h:147

◆ onMouseUp()

void ImGuiWrapper::onMouseUp ( SLMouseButton  button,
SLint  x,
SLint  y 
)
overridevirtual

Callback on mouse button up event.

Reimplemented from SLUiInterface.

Definition at line 523 of file ImGuiWrapper.cpp.

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 }

◆ onMouseWheel()

void ImGuiWrapper::onMouseWheel ( SLfloat  yoffset)
overridevirtual

Callback for the mouse scroll movement.

Reimplemented from SLUiInterface.

Definition at line 544 of file ImGuiWrapper.cpp.

545 {
546  // Use fractional mouse wheel, 1.0 unit 5 lines.
547  _mouseWheel += yoffset;
548 }

◆ onPaint()

void ImGuiWrapper::onPaint ( const SLRecti viewport)
overridevirtual

Callback for main rendering for the ImGui GUI system.

Reimplemented from SLUiInterface.

Definition at line 500 of file ImGuiWrapper.cpp.

501 {
502  // do the opengl rendering
503  _renderer->render(viewportRect);
504 }
virtual void render(const SLRecti &viewportRect)
Definition: ImGuiWrapper.h:40

◆ onResize()

void ImGuiWrapper::onResize ( SLint  scrW,
SLint  scrH 
)
override

Callback if window got resized.

Definition at line 491 of file ImGuiWrapper.cpp.

493 {
494  ImGuiIO& io = _context->IO;
495  io.DisplaySize = ImVec2((SLfloat)scrW, (SLfloat)scrH);
496  io.DisplayFramebufferScale = ImVec2(1, 1);
497 }

◆ renderExtraFrame()

void ImGuiWrapper::renderExtraFrame ( SLScene s,
SLSceneView sv,
SLint  mouseX,
SLint  mouseY 
)
overridevirtual

Renders an extra frame with the current mouse position.

Reimplemented from SLUiInterface.

Definition at line 584 of file ImGuiWrapper.cpp.

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 }
static SLint mouseX
Last mouse position x in pixels.
static SLint mouseY
Last mouse position y in pixels.
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.
SLRecti viewportRect() const
Definition: SLSceneView.h:176

Member Data Documentation

◆ _context

ImGuiContext* ImGuiWrapper::_context {nullptr}
protected

Definition at line 278 of file ImGuiWrapper.h.

◆ _mouseWheel

SLfloat ImGuiWrapper::_mouseWheel {0}
private

Mouse wheel position.

Definition at line 282 of file ImGuiWrapper.h.

◆ _panScroll

PanScrolling ImGuiWrapper::_panScroll
protected

Definition at line 277 of file ImGuiWrapper.h.

◆ _renderer

ImGuiRenderer* ImGuiWrapper::_renderer {nullptr}
private

Definition at line 284 of file ImGuiWrapper.h.

◆ _timeSec

SLfloat ImGuiWrapper::_timeSec
private

Time in seconds.

Definition at line 281 of file ImGuiWrapper.h.


The documentation for this class was generated from the following files: