ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
VisualizerWithKeyCallback.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <Logging.h>
11 
12 namespace cloudViewer {
13 
14 namespace visualization {
15 using namespace cloudViewer;
16 
18 
20 
23  utility::LogInfo(" -- Keys registered for callback functions --");
24  utility::LogInfo(" ");
25  for (auto &key_callback_pair : key_to_callback_) {
26  utility::LogInfo("[{}] ", PrintKeyToString(key_callback_pair.first));
27  }
28  utility::LogInfo("");
30  " The default functions of these keys will be overridden.");
31  utility::LogInfo("");
32 
33  std::string mouse_callbacks = (mouse_move_callback_ ? "MouseMove, " : "");
34  mouse_callbacks += (mouse_scroll_callback_ ? "MouseScroll, " : "");
35  mouse_callbacks += (mouse_button_callback_ ? "MouseButton, " : "");
36  utility::LogInfo(" Custom mouse callbacks registered for: {}",
37  mouse_callbacks.substr(0, mouse_callbacks.size() - 2));
38  utility::LogInfo("");
39 }
40 
42  int key, std::function<bool(Visualizer *)> callback) {
44 }
45 
47  int key, std::function<bool(Visualizer *, int, int)> callback) {
49 }
50 
52  std::function<bool(Visualizer *, double, double)> callback) {
54 }
55 
57  std::function<bool(Visualizer *, double, double)> callback) {
59 }
60 
62  std::function<bool(Visualizer *, int, int, int)> callback) {
64 }
65 
67  GLFWwindow *window, int key, int scancode, int action, int mods) {
68  auto action_callback = key_action_to_callback_.find(key);
69  if (action_callback != key_action_to_callback_.end()) {
70  if (action_callback->second(this, action, mods)) {
72  }
73  UpdateRender();
74  return;
75  }
76 
77  if (action == GLFW_RELEASE) {
78  return;
79  }
80  auto callback = key_to_callback_.find(key);
81  if (callback != key_to_callback_.end()) {
82  if (callback->second(this)) {
84  }
85  UpdateRender();
86  } else {
87  Visualizer::KeyPressCallback(window, key, scancode, action, mods);
88  }
89 }
90 
92  double x,
93  double y) {
95  if (mouse_move_callback_(this, x, y)) {
97  }
98  UpdateRender();
99  } else {
100  Visualizer::MouseMoveCallback(window, x, y);
101  }
102 }
103 
105  double x,
106  double y) {
108  if (mouse_scroll_callback_(this, x, y)) {
109  UpdateGeometry();
110  }
111  UpdateRender();
112  } else {
113  Visualizer::MouseScrollCallback(window, x, y);
114  }
115 }
116 
118  int button,
119  int action,
120  int mods) {
122  if (mouse_button_callback_(this, button, action, mods)) {
123  UpdateGeometry();
124  }
125  UpdateRender();
126  } else {
127  Visualizer::MouseButtonCallback(window, button, action, mods);
128  }
129 }
130 
132  if (key == GLFW_KEY_SPACE) { // 32
133  return std::string("Space");
134  } else if (key >= 39 && key <= 96) { // 39 - 96
135  return std::string(1, char(key));
136  } else if (key == GLFW_KEY_ESCAPE) { // 256
137  return std::string("Esc");
138  } else if (key == GLFW_KEY_ENTER) { // 257
139  return std::string("Enter");
140  } else if (key == GLFW_KEY_TAB) { // 258
141  return std::string("Tab");
142  } else if (key == GLFW_KEY_BACKSPACE) { // 259
143  return std::string("Backspace");
144  } else if (key == GLFW_KEY_INSERT) { // 260
145  return std::string("Insert");
146  } else if (key == GLFW_KEY_DELETE) { // 261
147  return std::string("Delete");
148  } else if (key == GLFW_KEY_RIGHT) { // 262
149  return std::string("Right arrow");
150  } else if (key == GLFW_KEY_LEFT) { // 263
151  return std::string("Left arrow");
152  } else if (key == GLFW_KEY_DOWN) { // 264
153  return std::string("Down arrow");
154  } else if (key == GLFW_KEY_UP) { // 265
155  return std::string("Up arrow");
156  } else if (key == GLFW_KEY_PAGE_UP) { // 266
157  return std::string("Page up");
158  } else if (key == GLFW_KEY_PAGE_DOWN) { // 267
159  return std::string("Page down");
160  } else if (key == GLFW_KEY_HOME) { // 268
161  return std::string("Home");
162  } else if (key == GLFW_KEY_END) { // 269
163  return std::string("End");
164  } else if (key == GLFW_KEY_CAPS_LOCK) { // 280
165  return std::string("Caps lock");
166  } else if (key == GLFW_KEY_SCROLL_LOCK) { // 281
167  return std::string("Scroll lock");
168  } else if (key == GLFW_KEY_NUM_LOCK) { // 282
169  return std::string("Num lock");
170  } else if (key == GLFW_KEY_PRINT_SCREEN) { // 283
171  return std::string("PrtScn");
172  } else if (key == GLFW_KEY_PAUSE) { // 284
173  return std::string("Pause");
174  } else if (key >= 290 && key <= 314) { // 290 - 314
175  return std::string("F") + std::to_string(key - 289);
176  }
177  return std::string("Unknown");
178 }
179 
180 } // namespace visualization
181 } // namespace cloudViewer
std::function< void(std::shared_ptr< core::Tensor >)> callback
void RegisterMouseScrollCallback(std::function< bool(Visualizer *, double, double)> callback)
void RegisterMouseMoveCallback(std::function< bool(Visualizer *, double, double)> callback)
std::map< int, std::function< bool(Visualizer *, int, int)> > key_action_to_callback_
void MouseScrollCallback(GLFWwindow *window, double x, double y) override
std::function< bool(Visualizer *, int, int, int)> mouse_button_callback_
std::function< bool(Visualizer *, double, double)> mouse_scroll_callback_
void KeyPressCallback(GLFWwindow *window, int key, int scancode, int action, int mods) override
void RegisterKeyActionCallback(int key, std::function< bool(Visualizer *, int, int)> callback)
void MouseMoveCallback(GLFWwindow *window, double x, double y) override
std::function< bool(Visualizer *, double, double)> mouse_move_callback_
void RegisterMouseButtonCallback(std::function< bool(Visualizer *, int, int, int)> callback)
void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods) override
std::map< int, std::function< bool(Visualizer *)> > key_to_callback_
void RegisterKeyCallback(int key, std::function< bool(Visualizer *)> callback)
The main Visualizer class.
Definition: Visualizer.h:45
virtual bool UpdateGeometry(std::shared_ptr< const ccHObject > geometry_ptr=nullptr)
Function to update geometry.
Definition: Visualizer.cpp:466
virtual void KeyPressCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
virtual void UpdateRender()
Function to inform render needed to be updated.
Definition: Visualizer.cpp:479
virtual void MouseScrollCallback(GLFWwindow *window, double x, double y)
virtual void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
virtual void MouseMoveCallback(GLFWwindow *window, double x, double y)
#define LogInfo(...)
Definition: Logging.h:81
Generic file read and write utility for python interface.
std::string to_string(const T &n)
Definition: Common.h:20