Main MRPT website > C++ reference for MRPT 1.9.9
CWindowObserver.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 // Implementation file for CWindowObserver class
11 #include "graphslam-precomp.h" // Precompiled headers
12 
15 
16 using namespace mrpt::graphslam;
17 
18 CWindowObserver::CWindowObserver() : m_showing_help(false), m_hiding_help(false)
19 {
20  m_help_msg =
21  "User options:\n"
22  " - h/H: Toggle help message\n"
23  " - Alt+Enter: Toggle fullscreen\n"
24  " - Mouse click: Set camera manually\n"
25  " - Ctrl+c: Halt program execution";
26 
27  // register the default keystrokes
28  m_key_codes_to_pressed["h"] = false;
29  m_key_codes_to_pressed["Alt+Enter"] = false;
30  m_key_codes_to_pressed["Ctrl+c"] = false;
31  m_key_codes_to_pressed["mouse_clicked"] = false;
32 
33  std::cout << "WindowObserver initialized." << std::endl;
34 }
35 
37  std::map<std::string, bool>* codes_to_pressed,
38  bool reset_keypresses /*= true */)
39 {
40  *codes_to_pressed = m_key_codes_to_pressed;
41 
42  // reset the code flags
43  if (reset_keypresses)
44  {
46  m_key_codes_to_pressed.begin();
47  map_it != m_key_codes_to_pressed.end(); ++map_it)
48  {
49  map_it->second = false;
50  }
51  }
52 }
53 
55  const std::string key_str, const std::string key_desc)
56 {
57  m_help_msg += std::string("\n") + " - " + key_str + "/" +
58  mrpt::system::upperCase(key_str) + ": " + key_desc;
59 
60  m_key_codes_to_pressed[key_str] = false;
61 }
62 
64 {
66  {
68  static_cast<const mrpt::utils::mrptEventOnDestroy&>(e);
70  std::cout << "Event received: mrptEventOnDestroy" << std::endl;
71  }
73  {
75  static_cast<const mrpt::gui::mrptEventWindowResize&>(e);
76  std::cout << "Resize event received from: " << ev.source_object
77  << ", new size: " << ev.new_width << " x " << ev.new_height
78  << std::endl;
79  }
81  {
83  static_cast<const mrpt::gui::mrptEventWindowChar&>(e);
84  std::cout << "Char event received from: " << ev.source_object
85  << ". Char code: " << ev.char_code
86  << " modif: " << ev.key_modifiers << std::endl;
87  ;
88 
89  switch (ev.char_code)
90  {
91  case 'h':
92  case 'H':
93  if (!m_showing_help)
94  {
95  m_showing_help = true;
96  std::cout << "h/H was pressed!" << std::endl;
97  }
98  else
99  {
100  m_showing_help = false;
101  m_hiding_help = true;
102  }
103  m_key_codes_to_pressed["h"] = true;
104  break;
105  case 'c':
106  case 'C':
107  // case 3: // <C-c>
108  if (ev.key_modifiers == 8192)
109  {
110  std::cout << "Pressed C-c inside CDisplayWindow3D"
111  << std::endl;
112  m_key_codes_to_pressed["Ctrl+c"] = true;
113  }
114  break;
115 
116  default:
117  // just raise the corresponding flag. Let the class which
118  // actually
119  // cares translate the character to its corresponding meaning.
120  // Pressed letter is stored in lower case form only to make it
121  // easier
122  // to check afterwards
124  std::string(1, ev.char_code))] = true;
125  break;
126  }
127 
128  } // end of e.isOftype<mrptEventWindowChar>
130  {
132  static_cast<const mrpt::gui::mrptEventWindowClosed&>(e);
133  std::cout << "Window closed event received from: " << ev.source_object
134  << "\n";
135  }
137  {
139  static_cast<const mrpt::gui::mrptEventMouseDown&>(e);
140  m_key_codes_to_pressed["mouse_clicked"] = true;
141 
142  std::cout << "Mouse down event received from: " << ev.source_object
143  << "pt: " << ev.coords.x << "," << ev.coords.y << "\n";
144  }
146  {
147  /*
148  * An event sent by an mrpt::opengl::COpenGLViewport AFTER calling
149  * the
150  * SCENE OPENGL DRAWING PRIMITIVES and before doing a glSwapBuffers.
151  */
152 
153  // std::cout << "mrpt::opengl::mrpptEventGLPostRender received." <<
154  // std::endl;
155 
157  0.70f, 0.05f, // x,y (in screen "ratios")
158  0.25f, 0.09f, // width, height (in screen "ratios")
159  "Press 'h' for help",
160  0.02f // text size
161  );
162 
163  // Also showing help?
165  {
166  // std::cout << "In the m_showing_help ... if-clause" << std::endl;
167  static const double TRANSP_ANIMATION_TIME_SEC = 0.5;
168 
169  const double show_tim = m_tim_show_start.Tac();
170  const double hide_tim = m_tim_show_end.Tac();
171 
172  const double tranparency =
174  ? 1.0 - std::min(1.0, hide_tim / TRANSP_ANIMATION_TIME_SEC)
175  : std::min(1.0, show_tim / TRANSP_ANIMATION_TIME_SEC);
176 
178  0.25f, 0.25f, // x,y (in screen "ratios")
179  0.50f, 0.50f, // width, height (in screen "ratios")
180  m_help_msg.c_str(),
181  0.02f, // text size
183  190, 190, 190, 200 * tranparency), // background
184  mrpt::utils::TColor(0, 0, 0, 200 * tranparency), // border
185  mrpt::utils::TColor(200, 0, 0, 150 * tranparency), // text
186  6.0f, // border width
187  "serif", // text font
188  mrpt::opengl::NICE // text style
189  );
190 
191  if (hide_tim > TRANSP_ANIMATION_TIME_SEC && m_hiding_help)
192  {
193  m_hiding_help = false;
194  }
195  }
196  }
197  else
198  {
199  // std::cout << "Unregistered mrptEvent received\n";
200  }
201 }
An event sent by a window upon resize.
void renderMessageBox(const float msg_x, const float msg_y, const float msg_w, const float msg_h, const std::string &text, float text_scale, const mrpt::utils::TColor &back_col=mrpt::utils::TColor(0, 0, 50, 150), const mrpt::utils::TColor &border_col=mrpt::utils::TColor(0, 0, 0, 140), const mrpt::utils::TColor &text_col=mrpt::utils::TColor(255, 255, 255, 220), const float border_width=4.0f, const std::string &text_font=std::string("sans"), mrpt::opengl::TOpenGLFontStyle text_style=mrpt::opengl::FILL, const double text_spacing=1.5, const double text_kerning=0.1)
Draws a message box with a centered (possibly multi-lined) text.
Definition: gl_utils.cpp:384
An event sent by a window upon a mouse click, giving the (x,y) pixel coordinates. ...
void OnEvent(const mrpt::utils::mrptEvent &e)
This virtual function will be called upon receive of any event after starting listening at any CObser...
#define min(a, b)
The basic event type for the observer-observable pattern in MRPT.
Definition: mrptEvent.h:34
Scalar * iterator
Definition: eigen_plugins.h:26
void returnEventsStruct(std::map< std::string, bool > *codes_to_pressed, bool reset_keypresses=true)
Return a map of key code to a boolean indicating whether it was pressed since the previous time the c...
void registerKeystroke(const std::string key_str, const std::string key_desc)
Make new keystrokes available in the help message box.
An event sent by a window upon when it&#39;s about to be closed, either manually by the user or programma...
SLAM methods related to graphs of pose constraints.
mrpt::utils::TPixelCoord coords
bool isOfType() const
Definition: mrptEvent.h:43
std::string lowerCase(const std::string &str)
Returns an lower-case version of a string.
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
A RGB color - 8bit.
Definition: TColor.h:25
mrpt::utils::CTicTac m_tim_show_start
An event sent by a window upon a char pressed by the user.
GLsizei const GLchar ** string
Definition: glext.h:4101
An event sent by any CObservable object (automatically) just before being destroyed and telling its o...
Definition: mrptEvent.h:68
mrpt::utils::CTicTac m_tim_show_end
mrptKeyModifier key_modifiers
Modifiers (Shift, Control, etc...)
An event sent by an mrpt::opengl::COpenGLViewport after calling the scene OpenGL drawing primitives a...
std::string upperCase(const std::string &str)
Returns a upper-case version of a string.
std::map< std::string, bool > m_key_codes_to_pressed
Map from registered char_code (std::string to support <C-c>) to boolean true/false indicating whether...
renders glyphs filled with antialiased outlines
Definition: opengl_fonts.h:40
double Tac()
Stops the stopwatch.
Definition: CTicTac.cpp:97
int char_code
The virtual key code, as defined in <mrpt/gui/keycodes.h> (a replication of wxWidgets key codes)...



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019