Radium Engine  1.5.0
Viewer.hpp
1 #pragma once
2 
3 #include <Gui/RaGui.hpp>
4 
5 #include <Core/CoreMacros.hpp>
6 #include <Core/Types.hpp>
7 #include <Engine/RadiumEngine.hpp>
8 #include <Engine/Rendering/Renderer.hpp>
9 #include <Engine/Scene/CameraComponent.hpp>
10 #include <Gui/Utils/KeyMappingManager.hpp>
11 #include <Gui/Viewer/WindowQt.hpp>
12 
13 #include <atomic>
14 #include <memory>
15 
16 #include <QThread>
17 #include <QWindow>
18 
19 // Forward declarations
20 class QOpenGLContext;
21 class QSurfaceFormat;
22 
23 namespace Ra {
24 namespace Gui {
25 class CameraManipulator;
26 class GizmoManager;
27 class PickingManager;
28 class RadiumHelpDialog;
29 } // namespace Gui
30 } // namespace Ra
31 
32 namespace Ra {
33 namespace Gui {
34 
53 class RA_GUI_API Viewer : public WindowQt, public KeyMappingManageable<Viewer>
54 {
55  Q_OBJECT
56  friend class KeyMappingManageable<Viewer>;
57 
58  public:
60  explicit Viewer( QScreen* screen = nullptr );
61 
63  ~Viewer() override;
64 
66  virtual void setupKeyMappingCallbacks();
67 
73  virtual bool prepareDisplay();
74 
75  //
76  // Accessors
77  //
79  QOpenGLContext* getContext() const { return m_context.get(); }
80 
82  void setCameraManipulator( CameraManipulator* ci );
83 
85  CameraManipulator* getCameraManipulator();
86 
87  void resetToDefaultCamera();
88 
90  GizmoManager* getGizmoManager();
91 
93  const Engine::Rendering::Renderer* getRenderer() const;
94 
96  Engine::Rendering::Renderer* getRenderer();
97 
109  int addRenderer( const std::shared_ptr<Engine::Rendering::Renderer>& e );
110 
112  PickingManager* getPickingManager();
113 
114  //
115  // Time dependent state management
116  //
118  virtual void update( const Scalar dt );
119  //
120  // Rendering management
121  //
122 
124  void startRendering( const Scalar dt );
125 
127  void swapBuffers();
128 
132 
134  void processPicking();
135 
137  void fitCameraToScene( const Core::Aabb& sceneAabb );
138  void fitCamera();
140  std::vector<std::string> getRenderersName() const;
141 
143  void grabFrame( const std::string& filename );
144 
145  void enableDebug();
146 
148  const Core::Utils::Color& getBackgroundColor() const { return m_backgroundColor; }
149 
152  addCustomAction( const std::string& actionName,
153  const KeyMappingManager::EventBinding& binding,
154  std::function<void( QEvent* )> callback );
156 
157  Scalar getDepthUnderMouse() const { return m_depthUnderMouse; }
158 
165  Core::Vector2 toDevice( const Core::Vector2& logicCoordinates ) {
166  return static_cast<Scalar>( devicePixelRatio() ) * logicCoordinates;
167  }
168 
169  signals:
173  bool glInitialized();
174  void rendererReady();
175 
178 
180  void toggleBrushPicking( bool on );
181 
182  void needUpdate();
183 
185  void onMousePress( QMouseEvent* event );
187  void onMouseRelease( QMouseEvent* event );
189  void onMouseMove( QMouseEvent* event );
190 
191  public slots:
193  void reloadShaders();
194 
196  void displayTexture( const QString& tex );
197 
199  bool changeRenderer( int index );
200 
202  void enablePostProcess( int enabled );
203 
205  void enableDebugDraw( int enabled );
206  void setBackgroundColor( const Core::Utils::Color& background );
207 
208  // Display help dialog about Viewer key-bindings
209  void displayHelpDialog();
210 
211  private slots:
214  void onAboutToCompose();
215  void onAboutToResize();
216  void onResized();
217  void onFrameSwapped();
218 
219  protected:
221  void createGizmoManager();
222 
224  void initializeRenderer( Engine::Rendering::Renderer* renderer );
225 
230  bool initializeGL() override;
231 
233  void resizeGL( QResizeEvent* event ) override;
234 
236  getPickingMode( const Ra::Gui::KeyMappingManager::KeyMappingAction& action ) const;
237 
243  void keyPressEvent( QKeyEvent* event ) override;
244  void keyReleaseEvent( QKeyEvent* event ) override;
245 
248  void mousePressEvent( QMouseEvent* event ) override;
250  void mouseReleaseEvent( QMouseEvent* event ) override;
252  void mouseMoveEvent( QMouseEvent* event ) override;
254  void wheelEvent( QWheelEvent* event ) override;
256 
257  void showEvent( QShowEvent* ev ) override;
258 
260  void focusOutEvent( QFocusEvent* event ) override;
261 
266  virtual bool handleKeyPressEvent( QKeyEvent* event );
267  virtual bool handleKeyReleaseEvent( QKeyEvent* event );
268  virtual void handleMousePressEvent( QMouseEvent* event,
270  virtual void handleMouseReleaseEvent( QMouseEvent* event );
271  virtual void handleMouseMoveEvent( QMouseEvent* event,
273  virtual void handleWheelEvent( QWheelEvent* event );
275  private:
280  static void configureKeyMapping_impl();
281 
282  Ra::Engine::Rendering::Renderer::PickingResult pickAtPosition( Core::Vector2 position );
283 
284  void propagateEventToParent( QEvent* event );
285 
289  getComponentActions( const Qt::MouseButtons& buttons,
290  const Qt::KeyboardModifiers& modifiers,
291  int key,
292  bool wheel );
293 
294  Scalar m_depthUnderMouse;
295  std::unique_ptr<RadiumHelpDialog> m_helpDialog { nullptr };
296 
297  protected:
300  std::vector<std::shared_ptr<Engine::Rendering::Renderer>> m_renderers;
301  std::vector<std::shared_ptr<Engine::Rendering::Renderer>> m_pendingRenderers;
302  Engine::Rendering::Renderer* m_currentRenderer;
303 
305  PickingManager* m_pickingManager;
306 
307  bool m_isBrushPickingEnabled;
308  Scalar m_brushRadius;
309 
311  std::unique_ptr<CameraManipulator> m_camera;
312 
315 
316  Core::Utils::Color m_backgroundColor { Core::Utils::Color::Grey( 0.0392_ra, 0_ra ) };
317 
319  enum KeyEventType { KeyPressed = 0, KeyReleased, KeyEventTypeCount };
322  std::array<std::map<Core::Utils::Index, std::function<void( QKeyEvent* )>>, KeyEventTypeCount>
324 
325  KeyMappingCallbackManager m_keyMappingCallbackManager;
326 
327  KeyMappingManager::Context m_activeContext {};
328 #define KeyMappingViewer \
329  KMA_VALUE( VIEWER_PICKING ) \
330  KMA_VALUE( VIEWER_PICKING_VERTEX ) \
331  KMA_VALUE( VIEWER_PICKING_EDGE ) \
332  KMA_VALUE( VIEWER_PICKING_TRIANGLE ) \
333  KMA_VALUE( VIEWER_PICKING_MULTI_CIRCLE ) \
334  KMA_VALUE( VIEWER_RAYCAST ) \
335  KMA_VALUE( VIEWER_SCALE_BRUSH ) \
336  KMA_VALUE( VIEWER_RELOAD_SHADERS ) \
337  KMA_VALUE( VIEWER_TOGGLE_WIREFRAME ) \
338  KMA_VALUE( VIEWER_SWITCH_CAMERA ) \
339  KMA_VALUE( VIEWER_CAMERA_FIT_SCENE ) \
340  KMA_VALUE( VIEWER_HELP )
341 
342 #define KMA_VALUE( x ) static KeyMappingManager::KeyMappingAction x;
343  KeyMappingViewer
344 #undef KMA_VALUE
345 };
346 
347 } // namespace Gui
348 } // namespace Ra
The CameraManipulator class is the generic class for camera manipulators.
This class manage a collection of binding/callback associated with a context.
KeyMappingManageable decorator to use as CRTP.
Inner class to store event binding.
Ra::Core::Utils::Index KeyMappingAction
handle to an action
Ra::Core::Utils::Index Context
handle to a Context
std::unique_ptr< CameraManipulator > m_camera
Owning pointer to the camera.
Definition: Viewer.hpp:311
QOpenGLContext * getContext() const
Access to the OpenGL context of the Viewer.
Definition: Viewer.hpp:79
const Core::Utils::Color & getBackgroundColor() const
get the currently used background color
Definition: Viewer.hpp:148
KeyEventType
Name of the customisable key actions.
Definition: Viewer.hpp:319
PickingManager * m_pickingManager
Owning Pointer to the feature picking manager.
Definition: Viewer.hpp:305
void rendererReady()
Emitted when GL context is ready. We except call to addRenderer here.
void requestEngineOpenGLInitialization()
void onMousePress(QMouseEvent *event)
Event sent after a mouse press event has been processed, but before emitting needUpdate()
void toggleBrushPicking(bool on)
Emitted when the corresponding key is released (see keyReleaseEvent)
std::vector< std::shared_ptr< Engine::Rendering::Renderer > > m_renderers
Definition: Viewer.hpp:300
Core::Vector2 toDevice(const Core::Vector2 &logicCoordinates)
Definition: Viewer.hpp:165
void onMouseRelease(QMouseEvent *event)
Event sent after a mouse release event has been processed, but before emitting needUpdate()
std::array< std::map< Core::Utils::Index, std::function< void(QKeyEvent *)> >, KeyEventTypeCount > m_customKeyActions
Definition: Viewer.hpp:323
GizmoManager * m_gizmoManager
Owning (QObject child) pointer to gizmo manager.
Definition: Viewer.hpp:314
void rightClickPicking(const Ra::Engine::Rendering::Renderer::PickingResult &result)
Emitted when the rendered is correctly initialized.
void onMouseMove(QMouseEvent *event)
Event sent after a mouse move event has been processed, but before emitting needUpdate()
Base class for OpenGL widgets, compatble with Qt and globjects/glbindings.
Definition: WindowQt.hpp:28
Definition: Cage.cpp:3