SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLGLImGui.h
Go to the documentation of this file.
1 /**
2  * \file gl/SLGLImGui.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 #ifndef SLGLIMGUI_H
13 #define SLGLIMGUI_H
14 
15 #include <SL.h>
16 #include <SLEnums.h>
17 #include <SLVec2.h>
18 #include <math/SLRect.h>
19 #define IMGUI_DEFINE_MATH_OPERATORS
20 #include <imgui.h>
21 #include <SLUiInterface.h>
22 #include "SLFileStorage.h"
23 
24 class SLScene;
25 class SLSceneView;
26 
27 //! Callback function typedef for ImGui build function
28 typedef void(SL_STDCALL* cbOnImGuiBuild)(SLScene* s, SLSceneView* sv);
29 
30 //! Callback function typedef for ImGui load config function
31 typedef void(SL_STDCALL* cbOnImGuiLoadConfig)(int dpi);
32 
33 //! Callback function typedef for ImGui save config function
34 typedef void(SL_STDCALL* cbOnImGuiSaveConfig)();
35 
36 //-----------------------------------------------------------------------------
37 //! ImGui Interface class for forwarding all events to the ImGui Handlers
38 /*! ImGui is a super easy GUI library for the rendering of a UI with OpenGL.
39 For more information see: https://github.com/ocornut/imgui\n
40 \n
41 This class provides only the interface into ImGui. In the event handlers of
42 SLSceneView the according callback in ImGui is called.\n
43 There is no UI drawn with this class. It must be defined in another class
44 that provides the build function. For the Demo apps this is done in the class
45 SLDemoGui and the build function is passed e.g. in glfwMain function of the
46 app-demo project.\n
47 \n
48 The full call stack for rendering one frame is:\n
49 - The top-level onPaint of the app (Win, Linux, MacOS, Android or iOS)
50  - slUpdateAndPaint: C-Interface function of SLProject
51  - SLSceneView::onPaint: Main onPaint function of a sceneview
52  - SLGLImGui::onInitNewFrame: Initializes a new GUI frame
53  - ImGui::NewFrame()
54  - SLGLImGui::build: The UI build function
55  - ... normal scene rendering of SLProject
56  - SLSceneView::draw2DGL:
57  - ImGui::Render
58  - SLGLImGui::onPaint(ImGui::GetDrawData())
59  - SLDemoGui::buildDemoGui: Builds the full UI
60 */
61 
62 class SLGLImGui : public SLUiInterface
63 {
64 public:
65  SLGLImGui(cbOnImGuiBuild buildCB,
66  cbOnImGuiLoadConfig loadConfigCB,
67  cbOnImGuiSaveConfig saveConfigCB,
68  int dpi,
69  SLIOBuffer fontDataProp,
70  SLIOBuffer fontDataFixed);
71  ~SLGLImGui() override;
72  void init(const string& configPath) override;
73 
74  void onInitNewFrame(SLScene* s, SLSceneView* sv) override;
75  void onResize(const SLRecti& viewportRect) override;
76  void onPaint(const SLRecti& viewport) override;
77  void onMouseDown(SLMouseButton button, SLint x, SLint y) override;
78  void onMouseUp(SLMouseButton button, SLint x, SLint y) override;
79  // returns true if it wants to capture mouse
80  void onMouseMove(SLint xPos, SLint yPos) override;
81  void onMouseWheel(SLfloat yoffset) override;
82  void onKeyPress(SLKey key, SLKey mod) override;
83  void onKeyRelease(SLKey key, SLKey mod) override;
84  void onCharInput(SLuint c) override;
85  void onClose() override;
87  bool doNotDispatchKeyboard() override { return ImGui::GetIO().WantCaptureKeyboard; }
88  bool doNotDispatchMouse() override { return ImGui::GetIO().WantCaptureMouse; }
90  void drawMouseCursor(bool doDraw) override { ImGui::GetIO().MouseDrawCursor = doDraw; }
91 
92  // Default font dots
93  static SLfloat fontPropDots; //!< Default font size of proportional font
94  static SLfloat fontFixedDots; //!< Default font size of fixed size font
95 
96 private:
97  void deleteOpenGLObjects();
98  void createOpenGLObjects();
99  void printCompileErrors(SLint shaderHandle,
100  const SLchar* src);
101 
102  // gui build function pattern
103  cbOnImGuiBuild _build = nullptr;
104 
105  // save config callback
107 
108  SLfloat _timeSec; //!< Time in seconds
109  SLVec2f _mousePosPX; //!< Mouse cursor position
110  SLfloat _mouseWheel; //!< Mouse wheel position
111  SLbool _mousePressed[3]; //!< Mouse button press state
112  SLuint _fontTexture; //!< OpenGL texture id for font
113  SLint _progHandle; //!< OpenGL handle for shader program
114  SLint _vertHandle; //!< OpenGL handle for vertex shader
115  SLint _fragHandle; //!< OpenGL handle for fragment shader
116  SLint _attribLocTex; //!< OpenGL attribute location for texture
117  SLint _attribLocProjMtx; //!< OpenGL attribute location for ???
118  SLint _attribLocPosition; //!< OpenGL attribute location for vertex pos.
119  SLint _attribLocUV; //!< OpenGL attribute location for texture coords
120  SLint _attribLocColor; //!< OpenGL attribute location for color
121  SLuint _vboHandle; //!< OpenGL handle for vertex buffer object
122  SLuint _vaoHandle; //!< OpenGL vertex array object handle
123  SLuint _elementsHandle; //!< OpenGL handle for vertex indexes
124  SLfloat _fontPropDots; //!< Active font size of proportional font
125  SLfloat _fontFixedDots; //!< Active font size of fixed size font
126  SLstring _configPath; //!< Path to config files
127  SLIOBuffer _fontDataProp; //!< Raw data of proportional font file
128  SLIOBuffer _fontDataFixed; //!< Raw data of fixed size font file
129 };
130 //-----------------------------------------------------------------------------
131 #endif
static SLint mouseX
Last mouse position x in pixels.
static SLint mouseY
Last mouse position y in pixels.
static SLint dpi
Dot per inch resolution of screen.
Definition: AppGLFW.cpp:41
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
char SLchar
Definition: SL.h:162
bool SLbool
Definition: SL.h:175
string SLstring
Definition: SL.h:158
int SLint
Definition: SL.h:170
SLMouseButton
Mouse button codes.
Definition: SLEnums.h:98
SLKey
Keyboard key codes enumeration.
Definition: SLEnums.h:16
void(SL_STDCALL * cbOnImGuiSaveConfig)()
Callback function typedef for ImGui save config function.
Definition: SLGLImGui.h:34
void(SL_STDCALL * cbOnImGuiLoadConfig)(int dpi)
Callback function typedef for ImGui load config function.
Definition: SLGLImGui.h:31
typedef void(SL_STDCALL *cbOnImGuiBuild)(SLScene *s
Callback function typedef for ImGui build function.
SLSceneView * sv
Definition: SLGLImGui.h:28
SLScene * s
Definition: SLScene.h:31
ImGui Interface class for forwarding all events to the ImGui Handlers.
Definition: SLGLImGui.h:63
void loadFonts(SLfloat fontPropDots, SLfloat fontFixedDots)
Loads the proportional and fixed size font depending on the passed DPI.
Definition: SLGLImGui.cpp:160
void printCompileErrors(SLint shaderHandle, const SLchar *src)
Prints the compile errors in case of a GLSL compile failure.
Definition: SLGLImGui.cpp:380
void onInitNewFrame(SLScene *s, SLSceneView *sv) override
Inits a new frame for the ImGui system.
Definition: SLGLImGui.cpp:399
void onMouseDown(SLMouseButton button, SLint x, SLint y) override
Callback on mouse button down event.
Definition: SLGLImGui.cpp:633
static SLfloat fontFixedDots
Default font size of fixed size font.
Definition: SLGLImGui.h:94
void onPaint(const SLRecti &viewport) override
Callback for main rendering for the ImGui GUI system.
Definition: SLGLImGui.cpp:453
SLint _attribLocProjMtx
OpenGL attribute location for ???
Definition: SLGLImGui.h:117
static SLfloat fontPropDots
Default font size of proportional font.
Definition: SLGLImGui.h:93
void init(const string &configPath) override
Initializes OpenGL handles to zero and sets the ImGui key map.
Definition: SLGLImGui.cpp:89
SLint _attribLocUV
OpenGL attribute location for texture coords.
Definition: SLGLImGui.h:119
void onClose() override
Callback on closing the application.
Definition: SLGLImGui.cpp:697
~SLGLImGui() override
Definition: SLGLImGui.cpp:79
SLuint _elementsHandle
OpenGL handle for vertex indexes.
Definition: SLGLImGui.h:123
void onCharInput(SLuint c) override
Callback on character input.
Definition: SLGLImGui.cpp:689
void renderExtraFrame(SLScene *s, SLSceneView *sv, SLint mouseX, SLint mouseY) override
Renders an extra frame with the current mouse position.
Definition: SLGLImGui.cpp:703
SLint _progHandle
OpenGL handle for shader program.
Definition: SLGLImGui.h:113
SLIOBuffer _fontDataProp
Raw data of proportional font file.
Definition: SLGLImGui.h:127
SLuint _vboHandle
OpenGL handle for vertex buffer object.
Definition: SLGLImGui.h:121
SLVec2f _mousePosPX
Mouse cursor position.
Definition: SLGLImGui.h:109
SLfloat _fontFixedDots
Active font size of fixed size font.
Definition: SLGLImGui.h:125
void onMouseWheel(SLfloat yoffset) override
Callback for the mouse scroll movement.
Definition: SLGLImGui.cpp:662
SLIOBuffer _fontDataFixed
Raw data of fixed size font file.
Definition: SLGLImGui.h:128
void onResize(const SLRecti &viewportRect) override
Callback if window got resized.
Definition: SLGLImGui.cpp:445
SLint _attribLocPosition
OpenGL attribute location for vertex pos.
Definition: SLGLImGui.h:118
SLint _fragHandle
OpenGL handle for fragment shader.
Definition: SLGLImGui.h:115
SLbool _mousePressed[3]
Mouse button press state.
Definition: SLGLImGui.h:111
SLstring _configPath
Path to config files.
Definition: SLGLImGui.h:126
void onMouseMove(SLint xPos, SLint yPos) override
Updates the mouse cursor position.
Definition: SLGLImGui.cpp:655
SLint _vertHandle
OpenGL handle for vertex shader.
Definition: SLGLImGui.h:114
void drawMouseCursor(bool doDraw) override
Turns on or off the mouse cursor drawing.
Definition: SLGLImGui.h:90
void onKeyRelease(SLKey key, SLKey mod) override
Callback on key release event.
Definition: SLGLImGui.cpp:679
SLfloat _mouseWheel
Mouse wheel position.
Definition: SLGLImGui.h:110
bool doNotDispatchMouse() override
inform if user mouse input was consumed by the ui
Definition: SLGLImGui.h:88
SLfloat _fontPropDots
Active font size of proportional font.
Definition: SLGLImGui.h:124
void onMouseUp(SLMouseButton button, SLint x, SLint y) override
Callback on mouse button up event.
Definition: SLGLImGui.cpp:644
SLint _attribLocColor
OpenGL attribute location for color.
Definition: SLGLImGui.h:120
cbOnImGuiSaveConfig _saveConfig
Definition: SLGLImGui.h:106
bool doNotDispatchKeyboard() override
inform if user keyboard input was consumed by the ui
Definition: SLGLImGui.h:87
SLfloat _timeSec
Time in seconds.
Definition: SLGLImGui.h:108
void deleteOpenGLObjects()
Deletes all OpenGL objects for drawing the imGui.
Definition: SLGLImGui.cpp:331
void onKeyPress(SLKey key, SLKey mod) override
Callback on key press event.
Definition: SLGLImGui.cpp:669
void createOpenGLObjects()
Creates all OpenGL objects for drawing the imGui.
Definition: SLGLImGui.cpp:188
SLGLImGui(cbOnImGuiBuild buildCB, cbOnImGuiLoadConfig loadConfigCB, cbOnImGuiSaveConfig saveConfigCB, int dpi, SLIOBuffer fontDataProp, SLIOBuffer fontDataFixed)
Definition: SLGLImGui.cpp:24
SLint _attribLocTex
OpenGL attribute location for texture.
Definition: SLGLImGui.h:116
SLuint _vaoHandle
OpenGL vertex array object handle.
Definition: SLGLImGui.h:122
cbOnImGuiBuild _build
Definition: SLGLImGui.h:103
SLuint _fontTexture
OpenGL texture id for font.
Definition: SLGLImGui.h:112
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
Interface for ui integration in SLSceneView.
Definition: SLUiInterface.h:24
T mod(T a, T b)
Definition: Utils.h:250
Utility struct that holds a pointer and its length.
Definition: SLFileStorage.h:28