1 // dear imgui: Platform Backend for GLUT/FreeGLUT
2 // This needs to be used along with a Renderer (e.g. OpenGL2)
3
4 // !!! GLUT/FreeGLUT IS OBSOLETE PREHISTORIC SOFTWARE. Using GLUT is not recommended unless you really miss the 90's. !!!
5 // !!! If someone or something is teaching you GLUT today, you are being abused. Please show some resistance. !!!
6 // !!! Nowadays, prefer using GLFW or SDL instead!
7
8 // Issues:
9 // [ ] Platform: GLUT is unable to distinguish e.g. Backspace from CTRL+H or TAB from CTRL+I
10 // [ ] Platform: Missing mouse cursor shape/visibility support.
11 // [ ] Platform: Missing clipboard support (not supported by Glut).
12 // [ ] Platform: Missing gamepad support.
13
14 // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
15 // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
16 // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
17 // Read online: https://github.com/ocornut/imgui/tree/master/docs
18
19 // CHANGELOG
20 // (minor and older changes stripped away, please see git history for details)
21 // 2019-04-03: Misc: Renamed imgui_impl_freeglut.cpp/.h to imgui_impl_glut.cpp/.h.
22 // 2019-03-25: Misc: Made io.DeltaTime always above zero.
23 // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
24 // 2018-03-22: Added GLUT Platform binding.
25
26 #include "imgui.h"
27 #include "imgui_impl_glut.h"
28 #ifdef __APPLE__
29 #include <GLUT/glut.h>
30 #else
31 #include <GL/freeglut.h>
32 #endif
33
34 #ifdef _MSC_VER
35 #pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff)
36 #endif
37
38 static int g_Time = 0; // Current time, in milliseconds
39
ImGui_ImplGLUT_Init()40 bool ImGui_ImplGLUT_Init()
41 {
42 ImGuiIO& io = ImGui::GetIO();
43
44 #ifdef FREEGLUT
45 io.BackendPlatformName = "imgui_impl_glut (freeglut)";
46 #else
47 io.BackendPlatformName = "imgui_impl_glut";
48 #endif
49
50 g_Time = 0;
51
52 // Glut has 1 function for characters and one for "special keys". We map the characters in the 0..255 range and the keys above.
53 io.KeyMap[ImGuiKey_Tab] = '\t'; // == 9 == CTRL+I
54 io.KeyMap[ImGuiKey_LeftArrow] = 256 + GLUT_KEY_LEFT;
55 io.KeyMap[ImGuiKey_RightArrow] = 256 + GLUT_KEY_RIGHT;
56 io.KeyMap[ImGuiKey_UpArrow] = 256 + GLUT_KEY_UP;
57 io.KeyMap[ImGuiKey_DownArrow] = 256 + GLUT_KEY_DOWN;
58 io.KeyMap[ImGuiKey_PageUp] = 256 + GLUT_KEY_PAGE_UP;
59 io.KeyMap[ImGuiKey_PageDown] = 256 + GLUT_KEY_PAGE_DOWN;
60 io.KeyMap[ImGuiKey_Home] = 256 + GLUT_KEY_HOME;
61 io.KeyMap[ImGuiKey_End] = 256 + GLUT_KEY_END;
62 io.KeyMap[ImGuiKey_Insert] = 256 + GLUT_KEY_INSERT;
63 io.KeyMap[ImGuiKey_Delete] = 127;
64 io.KeyMap[ImGuiKey_Backspace] = 8; // == CTRL+H
65 io.KeyMap[ImGuiKey_Space] = ' ';
66 io.KeyMap[ImGuiKey_Enter] = 13; // == CTRL+M
67 io.KeyMap[ImGuiKey_Escape] = 27;
68 io.KeyMap[ImGuiKey_KeyPadEnter] = 13; // == CTRL+M
69 io.KeyMap[ImGuiKey_A] = 'A';
70 io.KeyMap[ImGuiKey_C] = 'C';
71 io.KeyMap[ImGuiKey_V] = 'V';
72 io.KeyMap[ImGuiKey_X] = 'X';
73 io.KeyMap[ImGuiKey_Y] = 'Y';
74 io.KeyMap[ImGuiKey_Z] = 'Z';
75
76 return true;
77 }
78
ImGui_ImplGLUT_InstallFuncs()79 void ImGui_ImplGLUT_InstallFuncs()
80 {
81 glutReshapeFunc(ImGui_ImplGLUT_ReshapeFunc);
82 glutMotionFunc(ImGui_ImplGLUT_MotionFunc);
83 glutPassiveMotionFunc(ImGui_ImplGLUT_MotionFunc);
84 glutMouseFunc(ImGui_ImplGLUT_MouseFunc);
85 #ifdef __FREEGLUT_EXT_H__
86 glutMouseWheelFunc(ImGui_ImplGLUT_MouseWheelFunc);
87 #endif
88 glutKeyboardFunc(ImGui_ImplGLUT_KeyboardFunc);
89 glutKeyboardUpFunc(ImGui_ImplGLUT_KeyboardUpFunc);
90 glutSpecialFunc(ImGui_ImplGLUT_SpecialFunc);
91 glutSpecialUpFunc(ImGui_ImplGLUT_SpecialUpFunc);
92 }
93
ImGui_ImplGLUT_Shutdown()94 void ImGui_ImplGLUT_Shutdown()
95 {
96 }
97
ImGui_ImplGLUT_NewFrame()98 void ImGui_ImplGLUT_NewFrame()
99 {
100 // Setup time step
101 ImGuiIO& io = ImGui::GetIO();
102 int current_time = glutGet(GLUT_ELAPSED_TIME);
103 int delta_time_ms = (current_time - g_Time);
104 if (delta_time_ms <= 0)
105 delta_time_ms = 1;
106 io.DeltaTime = delta_time_ms / 1000.0f;
107 g_Time = current_time;
108
109 // Start the frame
110 ImGui::NewFrame();
111 }
112
ImGui_ImplGLUT_UpdateKeyboardMods()113 static void ImGui_ImplGLUT_UpdateKeyboardMods()
114 {
115 ImGuiIO& io = ImGui::GetIO();
116 int mods = glutGetModifiers();
117 io.KeyCtrl = (mods & GLUT_ACTIVE_CTRL) != 0;
118 io.KeyShift = (mods & GLUT_ACTIVE_SHIFT) != 0;
119 io.KeyAlt = (mods & GLUT_ACTIVE_ALT) != 0;
120 }
121
ImGui_ImplGLUT_KeyboardFunc(unsigned char c,int x,int y)122 void ImGui_ImplGLUT_KeyboardFunc(unsigned char c, int x, int y)
123 {
124 // Send character to imgui
125 //printf("char_down_func %d '%c'\n", c, c);
126 ImGuiIO& io = ImGui::GetIO();
127 if (c >= 32)
128 io.AddInputCharacter((unsigned int)c);
129
130 // Store letters in KeysDown[] array as both uppercase and lowercase + Handle GLUT translating CTRL+A..CTRL+Z as 1..26.
131 // This is a hacky mess but GLUT is unable to distinguish e.g. a TAB key from CTRL+I so this is probably the best we can do here.
132 if (c >= 1 && c <= 26)
133 io.KeysDown[c] = io.KeysDown[c - 1 + 'a'] = io.KeysDown[c - 1 + 'A'] = true;
134 else if (c >= 'a' && c <= 'z')
135 io.KeysDown[c] = io.KeysDown[c - 'a' + 'A'] = true;
136 else if (c >= 'A' && c <= 'Z')
137 io.KeysDown[c] = io.KeysDown[c - 'A' + 'a'] = true;
138 else
139 io.KeysDown[c] = true;
140 ImGui_ImplGLUT_UpdateKeyboardMods();
141 (void)x; (void)y; // Unused
142 }
143
ImGui_ImplGLUT_KeyboardUpFunc(unsigned char c,int x,int y)144 void ImGui_ImplGLUT_KeyboardUpFunc(unsigned char c, int x, int y)
145 {
146 //printf("char_up_func %d '%c'\n", c, c);
147 ImGuiIO& io = ImGui::GetIO();
148 if (c >= 1 && c <= 26)
149 io.KeysDown[c] = io.KeysDown[c - 1 + 'a'] = io.KeysDown[c - 1 + 'A'] = false;
150 else if (c >= 'a' && c <= 'z')
151 io.KeysDown[c] = io.KeysDown[c - 'a' + 'A'] = false;
152 else if (c >= 'A' && c <= 'Z')
153 io.KeysDown[c] = io.KeysDown[c - 'A' + 'a'] = false;
154 else
155 io.KeysDown[c] = false;
156 ImGui_ImplGLUT_UpdateKeyboardMods();
157 (void)x; (void)y; // Unused
158 }
159
ImGui_ImplGLUT_SpecialFunc(int key,int x,int y)160 void ImGui_ImplGLUT_SpecialFunc(int key, int x, int y)
161 {
162 //printf("key_down_func %d\n", key);
163 ImGuiIO& io = ImGui::GetIO();
164 if (key + 256 < IM_ARRAYSIZE(io.KeysDown))
165 io.KeysDown[key + 256] = true;
166 ImGui_ImplGLUT_UpdateKeyboardMods();
167 (void)x; (void)y; // Unused
168 }
169
ImGui_ImplGLUT_SpecialUpFunc(int key,int x,int y)170 void ImGui_ImplGLUT_SpecialUpFunc(int key, int x, int y)
171 {
172 //printf("key_up_func %d\n", key);
173 ImGuiIO& io = ImGui::GetIO();
174 if (key + 256 < IM_ARRAYSIZE(io.KeysDown))
175 io.KeysDown[key + 256] = false;
176 ImGui_ImplGLUT_UpdateKeyboardMods();
177 (void)x; (void)y; // Unused
178 }
179
ImGui_ImplGLUT_MouseFunc(int glut_button,int state,int x,int y)180 void ImGui_ImplGLUT_MouseFunc(int glut_button, int state, int x, int y)
181 {
182 ImGuiIO& io = ImGui::GetIO();
183 io.MousePos = ImVec2((float)x, (float)y);
184 int button = -1;
185 if (glut_button == GLUT_LEFT_BUTTON) button = 0;
186 if (glut_button == GLUT_RIGHT_BUTTON) button = 1;
187 if (glut_button == GLUT_MIDDLE_BUTTON) button = 2;
188 if (button != -1 && state == GLUT_DOWN)
189 io.MouseDown[button] = true;
190 if (button != -1 && state == GLUT_UP)
191 io.MouseDown[button] = false;
192 }
193
194 #ifdef __FREEGLUT_EXT_H__
ImGui_ImplGLUT_MouseWheelFunc(int button,int dir,int x,int y)195 void ImGui_ImplGLUT_MouseWheelFunc(int button, int dir, int x, int y)
196 {
197 ImGuiIO& io = ImGui::GetIO();
198 io.MousePos = ImVec2((float)x, (float)y);
199 if (dir > 0)
200 io.MouseWheel += 1.0;
201 else if (dir < 0)
202 io.MouseWheel -= 1.0;
203 (void)button; // Unused
204 }
205 #endif
206
ImGui_ImplGLUT_ReshapeFunc(int w,int h)207 void ImGui_ImplGLUT_ReshapeFunc(int w, int h)
208 {
209 ImGuiIO& io = ImGui::GetIO();
210 io.DisplaySize = ImVec2((float)w, (float)h);
211 }
212
ImGui_ImplGLUT_MotionFunc(int x,int y)213 void ImGui_ImplGLUT_MotionFunc(int x, int y)
214 {
215 ImGuiIO& io = ImGui::GetIO();
216 io.MousePos = ImVec2((float)x, (float)y);
217 }
218