MRPT  1.9.9
CWindowObserver.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-2019, 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 // Implementation file for CWindowObserver class
11 #include "graphslam-precomp.h" // Precompiled headers
12 
16 #include <mrpt/opengl/gl_utils.h>
17 
18 using namespace mrpt::graphslam;
19 
21 {
22  m_help_msg =
23  "User options:\n"
24  " - h/H: Toggle help message\n"
25  " - Alt+Enter: Toggle fullscreen\n"
26  " - Mouse click: Set camera manually\n"
27  " - Ctrl+c: Halt program execution";
28 
29  // register the default keystrokes
30  m_key_codes_to_pressed["h"] = false;
31  m_key_codes_to_pressed["Alt+Enter"] = false;
32  m_key_codes_to_pressed["Ctrl+c"] = false;
33  m_key_codes_to_pressed["mouse_clicked"] = false;
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  {
45  for (auto& map_it : m_key_codes_to_pressed)
46  {
47  map_it.second = false;
48  }
49  }
50 }
51 
53  const std::string key_str, const std::string key_desc)
54 {
55  m_help_msg += std::string("\n") + " - " + key_str + "/" +
56  mrpt::system::upperCase(key_str) + ": " + key_desc;
57 
58  m_key_codes_to_pressed[key_str] = false;
59 }
60 
62 {
64  {
65  const auto& ev =
66  static_cast<const mrpt::system::mrptEventOnDestroy&>(e);
68  std::cout << "Event received: mrptEventOnDestroy" << std::endl;
69  }
71  {
72  const auto& ev =
73  static_cast<const mrpt::gui::mrptEventWindowResize&>(e);
74  std::cout << "Resize event received from: " << ev.source_object
75  << ", new size: " << ev.new_width << " x " << ev.new_height
76  << std::endl;
77  }
79  {
80  const auto& ev = static_cast<const mrpt::gui::mrptEventWindowChar&>(e);
81  std::cout << "Char event received from: " << ev.source_object
82  << ". Char code: " << ev.char_code
83  << " modif: " << ev.key_modifiers << std::endl;
84  ;
85 
86  switch (ev.char_code)
87  {
88  case 'h':
89  case 'H':
90  if (!m_showing_help)
91  {
92  m_showing_help = true;
93  std::cout << "h/H was pressed!" << std::endl;
94  }
95  else
96  {
97  m_showing_help = false;
98  m_hiding_help = true;
99  }
100  m_key_codes_to_pressed["h"] = true;
101  break;
102  case 'c':
103  case 'C':
104  // case 3: // <C-c>
105  if (ev.key_modifiers == 8192)
106  {
107  std::cout << "Pressed C-c inside CDisplayWindow3D"
108  << std::endl;
109  m_key_codes_to_pressed["Ctrl+c"] = true;
110  }
111  break;
112 
113  default:
114  // just raise the corresponding flag. Let the class which
115  // actually
116  // cares translate the character to its corresponding meaning.
117  // Pressed letter is stored in lower case form only to make it
118  // easier
119  // to check afterwards
121  std::string(1, ev.char_code))] = true;
122  break;
123  }
124 
125  } // end of e.isOftype<mrptEventWindowChar>
127  {
128  const auto& ev =
129  static_cast<const mrpt::gui::mrptEventWindowClosed&>(e);
130  std::cout << "Window closed event received from: " << ev.source_object
131  << "\n";
132  }
134  {
135  const auto& ev = static_cast<const mrpt::gui::mrptEventMouseDown&>(e);
136  m_key_codes_to_pressed["mouse_clicked"] = true;
137 
138  std::cout << "Mouse down event received from: " << ev.source_object
139  << "pt: " << ev.coords.x << "," << ev.coords.y << "\n";
140  }
142  {
143  /*
144  * An event sent by an mrpt::opengl::COpenGLViewport AFTER calling
145  * the
146  * SCENE OPENGL DRAWING PRIMITIVES and before doing a glSwapBuffers.
147  */
148 
149  // std::cout << "mrpt::opengl::mrpptEventGLPostRender received." <<
150  // std::endl;
151 
153  0.70f, 0.05f, // x,y (in screen "ratios")
154  0.25f, 0.09f, // width, height (in screen "ratios")
155  "Press 'h' for help",
156  0.02f // text size
157  );
158 
159  // Also showing help?
161  {
162  // std::cout << "In the m_showing_help ... if-clause" << std::endl;
163  static const double TRANSP_ANIMATION_TIME_SEC = 0.5;
164 
165  const double show_tim = m_tim_show_start.Tac();
166  const double hide_tim = m_tim_show_end.Tac();
167 
168  const double tranparency =
170  ? 1.0 - std::min(1.0, hide_tim / TRANSP_ANIMATION_TIME_SEC)
171  : std::min(1.0, show_tim / TRANSP_ANIMATION_TIME_SEC);
172 
174  0.25f, 0.25f, // x,y (in screen "ratios")
175  0.50f, 0.50f, // width, height (in screen "ratios")
176  m_help_msg.c_str(),
177  0.02f, // text size
179  190, 190, 190, 200 * tranparency), // background
180  mrpt::img::TColor(0, 0, 0, 200 * tranparency), // border
181  mrpt::img::TColor(200, 0, 0, 150 * tranparency), // text
182  6.0f, // border width
183  "serif", // text font
184  mrpt::opengl::NICE // text style
185  );
186 
187  if (hide_tim > TRANSP_ANIMATION_TIME_SEC && m_hiding_help)
188  {
189  m_hiding_help = false;
190  }
191  }
192  }
193 }
An event sent by a window upon resize.
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:86
An event sent by a window upon a mouse click, giving the (x,y) pixel coordinates. ...
#define min(a, b)
void OnEvent(const mrpt::system::mrptEvent &e) override
This virtual function will be called upon receive of any event after starting listening at any CObser...
mrpt::system::CTicTac m_tim_show_end
The basic event type for the observer-observable pattern in MRPT.
Definition: mrptEvent.h:31
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.
std::string lowerCase(const std::string &str)
Returns an lower-case version of a string.
An event sent by any CObservable object (automatically) just before being destroyed and telling its o...
Definition: mrptEvent.h:66
An event sent by a window upon a char pressed by the user.
mrpt::system::CTicTac m_tim_show_start
GLsizei const GLchar ** string
Definition: glext.h:4116
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::img::TColor &back_col=mrpt::img::TColor(0, 0, 50, 150), const mrpt::img::TColor &border_col=mrpt::img::TColor(0, 0, 0, 140), const mrpt::img::TColor &text_col=mrpt::img::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:400
bool isOfType() const
Definition: mrptEvent.h:41
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:37
A RGB color - 8bit.
Definition: TColor.h:20
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019