MRPT  2.0.4
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 
11 #include <mrpt/core/exceptions.h>
15 
16 #include <mutex>
17 #include <string>
18 
19 // Expose nanogui API to mrpt users, for direct use of nanogui classes.
20 #include <mrpt/config.h>
21 #if MRPT_HAS_NANOGUI
22 #include <nanogui/nanogui.h>
23 
24 namespace mrpt::gui
25 {
26 /** Additional parameters to change the window behavior and OpenGL context
27  */
29 {
30  CDisplayWindowGUI_Params() = default;
31 
32  bool resizable = true;
33  bool fullscreen = false;
34  int colorBits = 8;
35  int alphaBits = 8;
36  int depthBits = 24;
37  int stencilBits = 8;
38  int nSamples = 0;
39  unsigned int glMajor = 3;
40  unsigned int glMinor = 3;
41  bool maximized = false;
42 };
43 
44 /** A window with powerful GUI capabilities, via the nanogui library.
45  *
46  * You can add a background mrpt::opengl::COpenGLScene object rendered on the
47  * background of the entire window by setting an object in field
48  * `background_scene`, locking its mutex `background_scene_mtx`.
49  *
50  * Refer to nanogui API docs or MRPT examples for further usage examples.
51  * A typical lifecycle of a GUI app with this class might look like:
52  *
53  * \rst
54  * .. code-block:: cpp
55  *
56  * nanogui::init();
57  * {
58  * mrpt::gui::CDisplayWindowGUI win;
59  * win.drawAll();
60  * win.setVisible(true);
61  * nanogui::mainloop();
62  * }
63  * nanogui::shutdown();
64  * \endrst
65  *
66  *
67  * ![mrpt::gui::CDisplayWindowGUI screenshot](preview_CDisplayWindowGUI.png)
68  *
69  * \ingroup mrpt_gui_grp
70  */
71 class CDisplayWindowGUI : public nanogui::Screen
72 {
73  public:
74  /** @name Ctor and basic window set up
75  * @{ */
78 
80  const std::string& caption = std::string(), unsigned int width = 400,
81  unsigned int height = 300,
83 
84  virtual ~CDisplayWindowGUI() override;
85 
86  /** Class factory returning a smart pointer */
87  template <typename... Args>
88  static Ptr Create(Args&&... args)
89  {
90  return std::make_shared<CDisplayWindowGUI>(std::forward<Args>(args)...);
91  }
92 
93  /** Resizes the window */
94  void resize(unsigned int width, unsigned int height);
95 
96  /** Changes the position of the window on the screen. */
97  void setPos(int x, int y);
98 
99  /** Changes the window title. */
100  void setWindowTitle(const std::string& str);
101 
102  /** Every time the window is about to be repainted, an optional callback can
103  * be called, if provided via this method. */
104  void setLoopCallback(const std::function<void(void)>& callback)
105  {
106  m_loopCallback = callback;
107  }
108  const auto& loopCallback() const { return m_loopCallback; }
109 
110  /** Sets a handle for file drop events */
112  const std::function<
113  bool(const std::vector<std::string>& /* filenames */)>& callback)
114  {
115  m_dropFilesCallback = callback;
116  }
117  const auto& dropFilesCallback() const { return m_dropFilesCallback; }
118 
119  void setKeyboardCallback(const std::function<bool(
120  int /*key*/, int /*scancode*/, int /*action*/,
121  int /*modifiers*/)>& callback)
122  {
123  m_keyboardCallback = callback;
124  }
125  const auto& keyboardCallback() const { return m_keyboardCallback; }
126 
127  /** @} */
128 
129  /** @name Access to full-window (background) GL scene
130  * @{ */
131 
134 
136  const CGlCanvasBase& camera() const { return m_background_canvas; }
137 
138  /** @} */
139 
140  /** @name Direct access to underlying nanogui API
141  * @{ */
142 
143  nanogui::Window* nanogui_win()
144  {
145  ASSERT_(m_window);
146  return m_window;
147  }
148 
149  /** @} */
150 
151  protected:
152  CDisplayWindowGUI(const CDisplayWindowGUI&) = delete;
154 
157 
158  /** the pointer is owned by the parent class Screen, no need to delete
159  * it */
160  nanogui::Window* m_window = nullptr;
161 
162  virtual bool keyboardEvent(
163  int key, int scancode, int action, int modifiers) override;
164  virtual void drawContents() override;
165 
166  /** @name Internal virtual functions to handle GUI events
167  * @{ */
168  virtual bool mouseMotionEvent(
169  const nanogui::Vector2i& p, const nanogui::Vector2i& rel, int button,
170  int modifiers) override;
171  virtual bool mouseButtonEvent(
172  const nanogui::Vector2i& p, int button, bool down,
173  int modifiers) override;
174  virtual bool scrollEvent(
175  const nanogui::Vector2i& p, const nanogui::Vector2f& rel) override;
176  virtual bool dropEvent(const std::vector<std::string>& filenames) override;
177  /** @} */
178 
179  /** Used to keep track of mouse events on the camera */
181 
182  std::function<void(void)> m_loopCallback;
183  // Returns true if handled
184  std::function<bool(const std::vector<std::string>& /* filenames */)>
186  // Returns true if handled
187  std::function<bool(
188  int /*key*/, int /*scancode*/, int /*action*/, int /*modifiers*/)>
190 };
191 
192 #define NANOGUI_START_TRY \
193  try \
194  {
195 #define NANOGUI_END_TRY(_parentWindowRef_) \
196  } \
197  catch (const std::exception& e) \
198  { \
199  const auto sErr = mrpt::exception_to_str(e); \
200  auto dlg = new nanogui::MessageDialog( \
201  &_parentWindowRef_, nanogui::MessageDialog::Type::Warning, \
202  "Exception", sErr); \
203  dlg->setCallback([](int /*result*/) {}); \
204  }
205 
206 } // namespace mrpt::gui
207 #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
const auto & keyboardCallback() const
std::function< bool(const std::vector< std::string > &)> m_dropFilesCallback
std::function< bool(int, int, int, int)> m_keyboardCallback
void resize(unsigned int width, unsigned int height)
Resizes the window.
virtual bool dropEvent(const std::vector< std::string > &filenames) override
CDisplayWindowGUI & operator=(const CDisplayWindowGUI &)=delete
virtual ~CDisplayWindowGUI() override
void setDropFilesCallback(const std::function< bool(const std::vector< std::string > &)> &callback)
Sets a handle for file drop events.
#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 setKeyboardCallback(const std::function< bool(int, int, int, int)> &callback)
const auto & dropFilesCallback() const
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
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
const auto & loopCallback() const
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 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020