MRPT  1.9.9
CDisplayWindowGUI.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
14 
15 #include <mutex>
16 #include <string>
17 
18 // Expose nanogui API to mrpt users, for direct use of nanogui classes.
19 #include <mrpt/config.h>
20 #if MRPT_HAS_NANOGUI
21 #include <nanogui/nanogui.h>
22 
23 namespace mrpt::gui
24 {
25 /** Additional parameters to change the window behavior and OpenGL context
26  */
28 {
29  CDisplayWindowGUI_Params() = default;
30 
31  bool resizable = true;
32  bool fullscreen = false;
33  int colorBits = 8;
34  int alphaBits = 8;
35  int depthBits = 24;
36  int stencilBits = 8;
37  int nSamples = 0;
38  unsigned int glMajor = 3;
39  unsigned int glMinor = 3;
40  bool maximized = false;
41 };
42 
43 /** A window with powerful GUI capabilities, via the nanogui library.
44  *
45  * You can add a background mrpt::opengl::COpenGLScene object rendered on the
46  * background of the entire window by setting an object in field
47  * `background_scene`, locking its mutex `background_scene_mtx`.
48  *
49  * Refer to nanogui API docs or MRPT examples for further usage examples.
50  * A typical lifecycle of a GUI app with this class might look like:
51  *
52  * \rst
53  * .. code-block:: cpp
54  *
55  * nanogui::init();
56  * {
57  * mrpt::gui::CDisplayWindowGUI win;
58  * win.drawAll();
59  * win.setVisible(true);
60  * nanogui::mainloop();
61  * }
62  * nanogui::shutdown();
63  * \endrst
64  *
65  *
66  * ![mrpt::gui::CDisplayWindowGUI screenshot](preview_CDisplayWindowGUI.png)
67  *
68  * \ingroup mrpt_gui_grp
69  */
70 class CDisplayWindowGUI : public nanogui::Screen
71 {
72  public:
73  /** @name Ctor and basic window set up
74  * @{ */
77 
79  const std::string& caption = std::string(), unsigned int width = 400,
80  unsigned int height = 300,
82 
83  virtual ~CDisplayWindowGUI() override;
84 
85  /** Class factory returning a smart pointer */
86  template <typename... Args>
87  static Ptr Create(Args&&... args)
88  {
89  return std::make_shared<CDisplayWindowGUI>(std::forward<Args>(args)...);
90  }
91 
92  /** Resizes the window */
93  void resize(unsigned int width, unsigned int height);
94 
95  /** Changes the position of the window on the screen. */
96  void setPos(int x, int y);
97 
98  /** Changes the window title. */
99  void setWindowTitle(const std::string& str);
100 
101  /** Every time the window is about to be repainted, an optional callback can
102  * be called, if provided via this method. */
103  void setLoopCallback(const std::function<void(void)>& callback)
104  {
105  m_loopCallback = callback;
106  }
107  const std::function<void(void)>& loopCallback() const
108  {
109  return m_loopCallback;
110  }
111 
112  /** @} */
113 
114  /** @name Access to full-window (background) GL scene
115  * @{ */
116 
119 
121  const CGlCanvasBase& camera() const { return m_background_canvas; }
122 
123  /** @} */
124 
125  /** @name Direct access to underlying nanogui API
126  * @{ */
127 
128  nanogui::Window* nanogui_win()
129  {
130  ASSERT_(m_window);
131  return m_window;
132  }
133 
134  /** @} */
135 
136  protected:
137  CDisplayWindowGUI(const CDisplayWindowGUI&) = delete;
139 
142 
143  /** the pointer is owned by the parent class Screen, no need to delete
144  * it */
145  nanogui::Window* m_window = nullptr;
146 
147  virtual bool keyboardEvent(
148  int key, int scancode, int action, int modifiers) override;
149  virtual void drawContents() override;
150 
151  /** @name Internal virtual functions to handle GUI events
152  * @{ */
153  virtual bool mouseMotionEvent(
154  const nanogui::Vector2i& p, const nanogui::Vector2i& rel, int button,
155  int modifiers) override;
156  virtual bool mouseButtonEvent(
157  const nanogui::Vector2i& p, int button, bool down,
158  int modifiers) override;
159  virtual bool scrollEvent(
160  const nanogui::Vector2i& p, const nanogui::Vector2f& rel) override;
161  /** @} */
162 
163  /** Used to keep track of mouse events on the camera */
165 
166  std::function<void(void)> m_loopCallback;
167 };
168 
169 } // namespace mrpt::gui
170 
171 #endif // MRPT_HAS_NANOGUI
virtual bool mouseMotionEvent(const nanogui::Vector2i &p, const nanogui::Vector2i &rel, int button, int modifiers) override
std::function< void(void)> m_loopCallback
void resize(unsigned int width, unsigned int height)
Resizes the window.
CDisplayWindowGUI & operator=(const CDisplayWindowGUI &)=delete
virtual ~CDisplayWindowGUI() override
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
nanogui::Window * m_window
the pointer is owned by the parent class Screen, no need to delete it
void setWindowTitle(const std::string &str)
Changes the window title.
CDisplayWindowGUI(const std::string &caption=std::string(), unsigned int width=400, unsigned int height=300, const CDisplayWindowGUI_Params &p=CDisplayWindowGUI_Params())
const CGlCanvasBase & camera() const
static Ptr Create(Args &&... args)
Class factory returning a smart pointer.
virtual bool scrollEvent(const nanogui::Vector2i &p, const nanogui::Vector2f &rel) override
const std::function< void(void)> & loopCallback() const
internal::NanoGUICanvasHeadless m_background_canvas
Used to keep track of mouse events on the camera.
void setPos(int x, int y)
Changes the position of the window on the screen.
mrpt::opengl::COpenGLScene::Ptr background_scene
virtual void drawContents() override
Additional parameters to change the window behavior and OpenGL context.
virtual bool keyboardEvent(int key, int scancode, int action, int modifiers) override
void setLoopCallback(const std::function< void(void)> &callback)
Every time the window is about to be repainted, an optional callback can be called, if provided via this method.
virtual bool mouseButtonEvent(const nanogui::Vector2i &p, int button, bool down, int modifiers) override
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
Specialization of CGlCanvasBaseHeadless for nanogui events.
This base class implements a working with opengl::Camera and a OpenGL canvas, and it&#39;s used in gui::C...
Definition: CGlCanvasBase.h:24
A window with powerful GUI capabilities, via the nanogui library.
nanogui::Window * nanogui_win()



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 3a26b90fd Wed Mar 25 20:17:03 2020 +0100 at miƩ mar 25 23:05:41 CET 2020