SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLImGui.h
Go to the documentation of this file.
1 /**
2  * \file SLImGui.h
3  * \brief Wrapper Class around the external ImGui GUI-framework
4  * \date October 2015, refresh in 2024
5  * \authors Marcus Hudritsch
6  * \copyright http://opensource.org/licenses/GPL-3.0
7  * \remarks Please use clangformat to format the code. See more code style on
8  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
9 */
10 
11 #ifndef SLIMGUI_H
12 #define SLIMGUI_H
13 
14 #include <SL.h>
15 #include <SLEnums.h>
16 #include <SLVec2.h>
17 #include <math/SLRect.h>
18 #define IMGUI_DEFINE_MATH_OPERATORS
19 #include <imgui.h>
20 #include <SLUiInterface.h>
21 #include "SLFileStorage.h"
22 
23 class SLScene;
24 class SLSceneView;
25 
26 //! Callback function typedef for ImGui build function
27 typedef void(SL_STDCALL* cbOnImGuiBuild)(SLScene* s, SLSceneView* sv);
28 
29 //! Callback function typedef for ImGui load config function
30 typedef void(SL_STDCALL* cbOnImGuiLoadConfig)(int dpi);
31 
32 //! Callback function typedef for ImGui save config function
33 typedef void(SL_STDCALL* cbOnImGuiSaveConfig)();
34 
35 //-----------------------------------------------------------------------------
36 //! ImGui Interface class for forwarding all events to the original ImGui Handlers
37 /*! ImGui is a super easy GUI library for the rendering of a UI with OpenGL.
38 For more information see: https://github.com/ocornut/imgui\n
39 \n
40 This class provides only the interface into ImGui. In the event handlers of
41 SLSceneView the according callback in ImGui is called.\n
42 Compared to the older SLGLImGui class this class contains no OpenGL code and
43 only calls the original backend functions in ImGui_impl_OpenGL3.cpp provided
44 by the ImGui framework.\n
45 There is no UI drawn with this class. It must be defined in another class
46 that provides the build function. For the Demo apps this is done in the file
47 AppDemoGui and the build function is passed e.g. in glfwMain function of the
48 app-demo project.\n
49 \n
50 The full call stack for rendering one frame is:\n
51 - The top-level onPaint of the app (Win, Linux, MacOS, Android or iOS)
52  - slUpdateAndPaint: C-Interface function of SLProject
53  - SLSceneView::onPaint: Main onPaint function of a sceneview
54  - SLImGui::onInitNewFrame: Initializes a new GUI frame
55  - ImGui::NewFrame()
56  - ... normal scene rendering of SLProject
57  - SLSceneView::draw2DGL:
58  - SLImGui::onPaint: Draws the UI with ImGUI
59 */
60 
61 class SLImGui : public SLUiInterface
62 {
63 public:
64  SLImGui(cbOnImGuiBuild buildCB,
65  cbOnImGuiLoadConfig loadConfigCB,
66  cbOnImGuiSaveConfig saveConfigCB,
67  int dpi,
68  SLIOBuffer fontDataProp,
69  SLIOBuffer fontDataFixed);
70  ~SLImGui() override;
71  void init(const string& configPath) override;
72 
73  void onInitNewFrame(SLScene* s, SLSceneView* sv) override;
74  void onResize(const SLRecti& viewport) override;
75  void onPaint(const SLRecti& viewport) override;
76  void onMouseDown(SLMouseButton button, SLint x, SLint y) override;
77  void onMouseUp(SLMouseButton button, SLint x, SLint y) override;
78  void onMouseMove(SLint xPos, SLint yPos) override;
79  void onMouseWheel(SLfloat yoffset) override;
80  void onKeyPress(SLKey key, SLKey mod) override;
81  void onKeyRelease(SLKey key, SLKey mod) override;
82  void onCharInput(SLuint c) override;
83  void onClose() override;
85  bool doNotDispatchKeyboard() override { return ImGui::GetIO().WantCaptureKeyboard; }
86  bool doNotDispatchMouse() override { return ImGui::GetIO().WantCaptureMouse; }
88  void drawMouseCursor(bool doDraw) override { ImGui::GetIO().MouseDrawCursor = doDraw; }
89 
90  // Default font dots
91  static SLfloat fontPropDots; //!< Default font size of proportional font
92  static SLfloat fontFixedDots; //!< Default font size of fixed size font
93 
94 private:
97  void printCompileErrors(SLint shaderHandle,
98  const SLchar* src);
99 
100  // gui build function pattern
101  cbOnImGuiBuild _build = nullptr;
102 
103  // save config callback
105 
106  SLfloat _timeSec; //!< Time in seconds
107  SLVec2f _mousePosPX; //!< Mouse cursor position
108  SLfloat _mouseWheel; //!< Mouse wheel position
109  SLbool _mousePressed[3]; //!< Mouse button press state
110  SLfloat _fontPropDots; //!< Active font size of proportional font
111  SLfloat _fontFixedDots; //!< Active font size of fixed size font
112  SLstring _configPath; //!< Path to config files
113  SLIOBuffer _fontDataProp; //!< Raw data of proportional font file
114  SLIOBuffer _fontDataFixed; //!< Raw data of fixed size font file
115 };
116 //-----------------------------------------------------------------------------
117 #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: SLImGui.h:33
void(SL_STDCALL * cbOnImGuiLoadConfig)(int dpi)
Callback function typedef for ImGui load config function.
Definition: SLImGui.h:30
typedef void(SL_STDCALL *cbOnImGuiBuild)(SLScene *s
Callback function typedef for ImGui build function.
SLSceneView * sv
Definition: SLImGui.h:27
SLScene * s
Definition: SLScene.h:31
ImGui Interface class for forwarding all events to the original ImGui Handlers.
Definition: SLImGui.h:62
void onKeyRelease(SLKey key, SLKey mod) override
Callback on key release event.
Definition: SLImGui.cpp:288
bool doNotDispatchKeyboard() override
inform if user keyboard input was consumed by the ui
Definition: SLImGui.h:85
void onMouseDown(SLMouseButton button, SLint x, SLint y) override
Callback on mouse button down event.
Definition: SLImGui.cpp:242
void deleteOpenGLObjects()
SLIOBuffer _fontDataFixed
Raw data of fixed size font file.
Definition: SLImGui.h:114
void loadFonts(SLfloat fontPropDots, SLfloat fontFixedDots)
Loads the proportional and fixed size font depending on the passed DPI.
Definition: SLImGui.cpp:151
void onMouseWheel(SLfloat yoffset) override
Callback for the mouse scroll movement.
Definition: SLImGui.cpp:271
static SLfloat fontPropDots
Default font size of proportional font.
Definition: SLImGui.h:91
~SLImGui() override
Definition: SLImGui.cpp:75
SLfloat _timeSec
Time in seconds.
Definition: SLImGui.h:106
void onPaint(const SLRecti &viewport) override
Callback for main rendering for the ImGui GUI system.
Definition: SLImGui.cpp:234
SLbool _mousePressed[3]
Mouse button press state.
Definition: SLImGui.h:109
void onCharInput(SLuint c) override
Callback on character input.
Definition: SLImGui.cpp:298
void onMouseUp(SLMouseButton button, SLint x, SLint y) override
Callback on mouse button up event.
Definition: SLImGui.cpp:253
void printCompileErrors(SLint shaderHandle, const SLchar *src)
Prints the compile errors in case of a GLSL compile failure.
Definition: SLImGui.cpp:178
void onMouseMove(SLint xPos, SLint yPos) override
Updates the mouse cursor position.
Definition: SLImGui.cpp:264
bool doNotDispatchMouse() override
inform if user mouse input was consumed by the ui
Definition: SLImGui.h:86
void onKeyPress(SLKey key, SLKey mod) override
Callback on key press event.
Definition: SLImGui.cpp:278
SLfloat _fontFixedDots
Active font size of fixed size font.
Definition: SLImGui.h:111
cbOnImGuiBuild _build
Definition: SLImGui.h:101
SLfloat _mouseWheel
Mouse wheel position.
Definition: SLImGui.h:108
SLstring _configPath
Path to config files.
Definition: SLImGui.h:112
void createOpenGLObjects()
void init(const string &configPath) override
Initializes OpenGL handles to zero and sets the ImGui key map.
Definition: SLImGui.cpp:81
void onClose() override
Callback on closing the application.
Definition: SLImGui.cpp:141
void onResize(const SLRecti &viewport) override
Callback if window got resized.
Definition: SLImGui.cpp:225
void drawMouseCursor(bool doDraw) override
Turns on or off the mouse cursor drawing.
Definition: SLImGui.h:88
SLfloat _fontPropDots
Active font size of proportional font.
Definition: SLImGui.h:110
SLIOBuffer _fontDataProp
Raw data of proportional font file.
Definition: SLImGui.h:113
SLVec2f _mousePosPX
Mouse cursor position.
Definition: SLImGui.h:107
static SLfloat fontFixedDots
Default font size of fixed size font.
Definition: SLImGui.h:92
void onInitNewFrame(SLScene *s, SLSceneView *sv) override
Inits a new frame for the ImGui system.
Definition: SLImGui.cpp:183
void renderExtraFrame(SLScene *s, SLSceneView *sv, SLint mouseX, SLint mouseY) override
Renders an extra frame with the current mouse position.
Definition: SLImGui.cpp:306
cbOnImGuiSaveConfig _saveConfig
Definition: SLImGui.h:104
SLImGui(cbOnImGuiBuild buildCB, cbOnImGuiLoadConfig loadConfigCB, cbOnImGuiSaveConfig saveConfigCB, int dpi, SLIOBuffer fontDataProp, SLIOBuffer fontDataFixed)
Definition: SLImGui.cpp:27
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