MRPT  2.0.0
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 auto& loopCallback() const { return m_loopCallback; }
108 
109  /** Sets a handle for file drop events */
111  const std::function<
112  bool(const std::vector<std::string>& /* filenames */)>& callback)
113  {
114  m_dropFilesCallback = callback;
115  }
116  const auto& dropFilesCallback() const { return m_dropFilesCallback; }
117 
118  void setKeyboardCallback(const std::function<bool(
119  int /*key*/, int /*scancode*/, int /*action*/,
120  int /*modifiers*/)>& callback)
121  {
122  m_keyboardCallback = callback;
123  }
124  const auto& keyboardCallback() const { return m_keyboardCallback; }
125 
126  /** @} */
127 
128  /** @name Access to full-window (background) GL scene
129  * @{ */
130 
133 
135  const CGlCanvasBase& camera() const { return m_background_canvas; }
136 
137  /** @} */
138 
139  /** @name Direct access to underlying nanogui API
140  * @{ */
141 
142  nanogui::Window* nanogui_win()
143  {
144  ASSERT_(m_window);
145  return m_window;
146  }
147 
148  /** @} */
149 
150  protected:
151  CDisplayWindowGUI(const CDisplayWindowGUI&) = delete;
153 
156 
157  /** the pointer is owned by the parent class Screen, no need to delete
158  * it */
159  nanogui::Window* m_window = nullptr;
160 
161  virtual bool keyboardEvent(
162  int key, int scancode, int action, int modifiers) override;
163  virtual void drawContents() override;
164 
165  /** @name Internal virtual functions to handle GUI events
166  * @{ */
167  virtual bool mouseMotionEvent(
168  const nanogui::Vector2i& p, const nanogui::Vector2i& rel, int button,
169  int modifiers) override;
170  virtual bool mouseButtonEvent(
171  const nanogui::Vector2i& p, int button, bool down,
172  int modifiers) override;
173  virtual bool scrollEvent(
174  const nanogui::Vector2i& p, const nanogui::Vector2f& rel) override;
175  virtual bool dropEvent(const std::vector<std::string>& filenames) override;
176  /** @} */
177 
178  /** Used to keep track of mouse events on the camera */
180 
181  std::function<void(void)> m_loopCallback;
182  // Returns true if handled
183  std::function<bool(const std::vector<std::string>& /* filenames */)>
185  // Returns true if handled
186  std::function<bool(
187  int /*key*/, int /*scancode*/, int /*action*/, int /*modifiers*/)>
189 };
190 
191 } // namespace mrpt::gui
192 
193 #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.0 Git: b38439d21 Tue Mar 31 19:58:06 2020 +0200 at miƩ abr 1 00:50:30 CEST 2020