1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26
27 #include "VisualizerWithKeyCallback.h"
28
29 namespace three{
30
VisualizerWithKeyCallback()31 VisualizerWithKeyCallback::VisualizerWithKeyCallback()
32 {
33 }
34
~VisualizerWithKeyCallback()35 VisualizerWithKeyCallback::~VisualizerWithKeyCallback()
36 {
37 }
38
PrintVisualizerHelp()39 void VisualizerWithKeyCallback::PrintVisualizerHelp()
40 {
41 Visualizer::PrintVisualizerHelp();
42 PrintInfo(" -- Keys registered for callback functions --\n");
43 PrintInfo(" ");
44 for (auto &key_callback_pair : key_to_callback_) {
45 PrintInfo("[%s] ", PrintKeyToString(key_callback_pair.first).c_str());
46 }
47 PrintInfo("\n");
48 PrintInfo(" The default functions of these keys will be overridden.\n");
49 PrintInfo("\n");
50 }
51
RegisterKeyCallback(int key,std::function<bool (Visualizer *)> callback)52 void VisualizerWithKeyCallback::RegisterKeyCallback(int key,
53 std::function<bool(Visualizer *)> callback)
54 {
55 key_to_callback_[key] = callback;
56 }
57
KeyPressCallback(GLFWwindow * window,int key,int scancode,int action,int mods)58 void VisualizerWithKeyCallback::KeyPressCallback(GLFWwindow *window,
59 int key, int scancode, int action, int mods)
60 {
61 if (action == GLFW_RELEASE) {
62 return;
63 }
64 auto callback = key_to_callback_.find(key);
65 if (callback != key_to_callback_.end()) {
66 if (callback->second(this)) {
67 UpdateGeometry();
68 }
69 UpdateRender();
70 } else {
71 Visualizer::KeyPressCallback(window, key, scancode, action, mods);
72 }
73 }
74
PrintKeyToString(int key)75 std::string VisualizerWithKeyCallback::PrintKeyToString(int key)
76 {
77 if (key == GLFW_KEY_SPACE) { // 32
78 return std::string("Space");
79 } else if (key >= 39 && key <= 96) { // 39 - 96
80 return std::string(1, char(key));
81 } else if (key == GLFW_KEY_ESCAPE) { // 256
82 return std::string("Esc");
83 } else if (key == GLFW_KEY_ENTER) { // 257
84 return std::string("Enter");
85 } else if (key == GLFW_KEY_TAB) { // 258
86 return std::string("Tab");
87 } else if (key == GLFW_KEY_BACKSPACE) { // 259
88 return std::string("Backspace");
89 } else if (key == GLFW_KEY_INSERT) { // 260
90 return std::string("Insert");
91 } else if (key == GLFW_KEY_DELETE) { // 261
92 return std::string("Delete");
93 } else if (key == GLFW_KEY_RIGHT) { // 262
94 return std::string("Right arrow");
95 } else if (key == GLFW_KEY_LEFT) { // 263
96 return std::string("Left arrow");
97 } else if (key == GLFW_KEY_DOWN) { // 264
98 return std::string("Down arrow");
99 } else if (key == GLFW_KEY_UP) { // 265
100 return std::string("Up arrow");
101 } else if (key == GLFW_KEY_PAGE_UP) { // 266
102 return std::string("Page up");
103 } else if (key == GLFW_KEY_PAGE_DOWN) { // 267
104 return std::string("Page down");
105 } else if (key == GLFW_KEY_HOME) { // 268
106 return std::string("Home");
107 } else if (key == GLFW_KEY_END) { // 269
108 return std::string("End");
109 } else if (key == GLFW_KEY_CAPS_LOCK) { // 280
110 return std::string("Caps lock");
111 } else if (key == GLFW_KEY_SCROLL_LOCK) { // 281
112 return std::string("Scroll lock");
113 } else if (key == GLFW_KEY_NUM_LOCK) { // 282
114 return std::string("Num lock");
115 } else if (key == GLFW_KEY_PRINT_SCREEN) { // 283
116 return std::string("PrtScn");
117 } else if (key == GLFW_KEY_PAUSE) { // 284
118 return std::string("Pause");
119 } else if (key >= 290 && key <= 314) { // 290 - 314
120 return std::string("F") + std::to_string(key - 289);
121 }
122 return std::string("Unknown");
123 }
124
125 } // namespace three
126