MRPT  2.0.3
CDisplayWindowGUI.cpp
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 
10 #include "gui-precomp.h" // Precompiled headers
11 
12 #include <mrpt/core/exceptions.h>
15 
16 using namespace mrpt::gui;
17 
18 #if MRPT_HAS_NANOGUI
19 
21  const std::string& caption, unsigned int width, unsigned int height,
22  const CDisplayWindowGUI_Params& p)
23  : nanogui::Screen(
24  Eigen::Vector2i(width, height), caption, p.resizable, p.fullscreen,
25  p.colorBits, p.alphaBits, p.depthBits, p.stencilBits, p.nSamples,
26  p.glMajor, p.glMinor, p.maximized)
27 {
28  // Set MRPT icon:
29  GLFWimage images;
30  images.width = 64;
31  images.height = 64;
32  images.pixels = default_mrpt_glfw_icon();
33 
34 // glfwSetWindowIcon added in glfw 3.2
35 #if GLFW_VERSION_MAJOR > 3 || \
36  (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 2)
37  glfwSetWindowIcon(screen()->glfwWindow(), 1, &images);
38 #endif
39 }
40 
42 {
43  // Close window:
44  nanogui::Screen::setVisible(false);
45 }
46 
48 {
49  // If provided, call the user loop code:
51 
52  // Optional: render background scene.
53  std::lock_guard<std::mutex> lck(background_scene_mtx);
54  if (!background_scene) return;
55 
56  try
57  {
58  // We need the size of the viewport:
59  GLint win_dims[4];
60  glGetIntegerv(GL_VIEWPORT, win_dims);
61 
62  // Set the camera params in the scene:
64  background_scene->getViewport("main");
65  if (!view)
67  "Fatal error: there is no 'main' viewport in the 3D scene!");
68  mrpt::opengl::CCamera& cam = view->getCamera();
70 
71  for (const auto& m_viewport : background_scene->viewports())
72  m_viewport->render(
73  win_dims[2], win_dims[3], win_dims[0], win_dims[1]);
74  }
75  catch (const std::exception& e)
76  {
77  std::cerr << "[CDisplayWindowGUI::drawContents] Exception:\n"
79  }
80 }
81 
82 void CDisplayWindowGUI::resize(unsigned int width, unsigned int height)
83 {
84  Screen::setSize({width, height});
85 }
86 
87 void CDisplayWindowGUI::setPos(int x, int y) { Screen::setPosition({x, y}); }
88 
89 void CDisplayWindowGUI::setWindowTitle(const std::string& str)
90 {
91  Screen::setCaption(str);
92 }
93 
95  const nanogui::Vector2i& p, int button, bool down, int modifiers)
96 {
97  if (!Screen::mouseButtonEvent(p, button, down, modifiers))
98  m_background_canvas.mouseButtonEvent(p, button, down, modifiers);
99 
100  return true;
101 }
102 
104  const nanogui::Vector2i& p, const nanogui::Vector2i& rel, int button,
105  int modifiers)
106 {
107  if (!Screen::mouseMotionEvent(p, rel, button, modifiers))
108  m_background_canvas.mouseMotionEvent(p, rel, button, modifiers);
109 
110  return true;
111 }
112 
114  const nanogui::Vector2i& p, const nanogui::Vector2f& rel)
115 {
116  if (!Screen::scrollEvent(p, rel)) m_background_canvas.scrollEvent(p, rel);
117 
118  return true;
119 }
120 
121 bool CDisplayWindowGUI::dropEvent(const std::vector<std::string>& filenames)
122 {
124  return m_dropFilesCallback(filenames);
125  else
126  return false;
127 }
128 
130  int key, int scancode, int action, int modifiers)
131 {
132  if (m_keyboardCallback)
133  if (m_keyboardCallback(key, scancode, action, modifiers)) return true;
134 
135  if (Screen::keyboardEvent(key, scancode, action, modifiers)) return true;
136 
137  // Process special key events?
138  return false;
139 }
140 
141 #endif // MRPT_HAS_NANOGUI
Eigen
Definition: math_frwds.h:21
exceptions.h
mrpt::gui::CGlCanvasBase::updateCameraParams
mrpt::opengl::CCamera & updateCameraParams(mrpt::opengl::CCamera &cam) const
This function gets a reference to mrpt::opengl::CCamera and updates the camera parameters(pointing,...
Definition: CGlCanvasBase.cpp:191
mrpt::gui::internal::NanoGUICanvasHeadless::scrollEvent
void scrollEvent(const nanogui::Vector2i &p, const nanogui::Vector2f &rel)
Definition: NanoGUICanvasHeadless.cpp:61
mrpt::gui::CDisplayWindowGUI::scrollEvent
virtual bool scrollEvent(const nanogui::Vector2i &p, const nanogui::Vector2f &rel) override
Definition: CDisplayWindowGUI.cpp:113
images
mrpt::vision::TCalibrationStereoImageList images
Definition: chessboard_stereo_camera_calib_unittest.cpp:20
mrpt::gui::internal::NanoGUICanvasHeadless::mouseMotionEvent
void mouseMotionEvent(const nanogui::Vector2i &p, const nanogui::Vector2i &rel, int button, int modifiers)
Definition: NanoGUICanvasHeadless.cpp:20
mrpt::gui::CDisplayWindowGUI::background_scene_mtx
std::mutex background_scene_mtx
Definition: CDisplayWindowGUI.h:133
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
mrpt::opengl::CCamera::render
void render(const RenderContext &rc) const override
Render does nothing here.
Definition: CCamera.h:93
mrpt::gui::CDisplayWindowGUI::m_dropFilesCallback
std::function< bool(const std::vector< std::string > &)> m_dropFilesCallback
Definition: CDisplayWindowGUI.h:185
mrpt::opengl::COpenGLViewport::Ptr
std::shared_ptr< mrpt::opengl ::COpenGLViewport > Ptr
Definition: COpenGLViewport.h:65
mrpt::gui::CDisplayWindowGUI::keyboardEvent
virtual bool keyboardEvent(int key, int scancode, int action, int modifiers) override
Definition: CDisplayWindowGUI.cpp:129
mrpt::opengl::CCamera
A camera: if added to a scene, the viewpoint defined by this camera will be used instead of the camer...
Definition: CCamera.h:33
gui-precomp.h
mrpt::gui::CDisplayWindowGUI::~CDisplayWindowGUI
virtual ~CDisplayWindowGUI() override
Definition: CDisplayWindowGUI.cpp:41
mrpt::gui::default_mrpt_glfw_icon
unsigned char * default_mrpt_glfw_icon()
Returns the default MRPT icon data, as a 64x64 RGBA (32 bit/pixel) image, compatible with GLFW.
Definition: default_mrpt_glfw_icon.cpp:17
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
mrpt::gui::CDisplayWindowGUI::setWindowTitle
void setWindowTitle(const std::string &str)
Changes the window title.
Definition: CDisplayWindowGUI.cpp:89
mrpt::gui::CDisplayWindowGUI::m_background_canvas
internal::NanoGUICanvasHeadless m_background_canvas
Used to keep track of mouse events on the camera.
Definition: CDisplayWindowGUI.h:180
mrpt::gui::CDisplayWindowGUI::m_loopCallback
std::function< void(void)> m_loopCallback
Definition: CDisplayWindowGUI.h:182
mrpt::gui::CDisplayWindowGUI::drawContents
virtual void drawContents() override
Definition: CDisplayWindowGUI.cpp:47
mrpt::gui::CDisplayWindowGUI::background_scene
mrpt::opengl::COpenGLScene::Ptr background_scene
Definition: CDisplayWindowGUI.h:132
mrpt::gui::CDisplayWindowGUI::setPos
void setPos(int x, int y)
Changes the position of the window on the screen.
Definition: CDisplayWindowGUI.cpp:87
mrpt::gui::CDisplayWindowGUI::resize
void resize(unsigned int width, unsigned int height)
Resizes the window.
Definition: CDisplayWindowGUI.cpp:82
mrpt::gui::CDisplayWindowGUI::CDisplayWindowGUI
CDisplayWindowGUI(const std::string &caption=std::string(), unsigned int width=400, unsigned int height=300, const CDisplayWindowGUI_Params &p=CDisplayWindowGUI_Params())
Definition: CDisplayWindowGUI.cpp:20
mrpt::gui::internal::NanoGUICanvasHeadless::mouseButtonEvent
void mouseButtonEvent(const nanogui::Vector2i &p, int button, bool down, int modifiers)
Definition: NanoGUICanvasHeadless.cpp:55
mrpt::gui::CDisplayWindowGUI_Params
Additional parameters to change the window behavior and OpenGL context.
Definition: CDisplayWindowGUI.h:28
mrpt::gui::CDisplayWindowGUI::mouseButtonEvent
virtual bool mouseButtonEvent(const nanogui::Vector2i &p, int button, bool down, int modifiers) override
Definition: CDisplayWindowGUI.cpp:94
mrpt::gui::CDisplayWindowGUI::m_keyboardCallback
std::function< bool(int, int, int, int)> m_keyboardCallback
Definition: CDisplayWindowGUI.h:189
default_mrpt_glfw_icon.h
mrpt::exception_to_str
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Definition: exceptions.cpp:59
CDisplayWindowGUI.h
mrpt::gui::CDisplayWindowGUI::mouseMotionEvent
virtual bool mouseMotionEvent(const nanogui::Vector2i &p, const nanogui::Vector2i &rel, int button, int modifiers) override
Definition: CDisplayWindowGUI.cpp:103
mrpt::gui::CDisplayWindowGUI::dropEvent
virtual bool dropEvent(const std::vector< std::string > &filenames) override
Definition: CDisplayWindowGUI.cpp:121



Page generated by Doxygen 1.8.17 for MRPT 2.0.3 at Fri May 29 13:06:46 UTC 2020