| 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 |
// Implemented features: |
| 9 |
// [X] Platform: Partial keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy GLUT values are obsolete since 1.87 and not supported since 1.91.5] |
| 10 |
// Missing features or Issues: |
| 11 |
// [ ] Platform: GLUT is unable to distinguish e.g. Backspace from CTRL+H or TAB from CTRL+I |
| 12 |
// [ ] Platform: Missing horizontal mouse wheel support. |
| 13 |
// [ ] Platform: Missing mouse cursor shape/visibility support. |
| 14 |
// [ ] Platform: Missing clipboard support (not supported by Glut). |
| 15 |
// [ ] Platform: Missing gamepad support. |
| 16 |
|
| 17 |
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. |
| 18 |
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. |
| 19 |
// Learn about Dear ImGui: |
| 20 |
// - FAQ https://dearimgui.com/faq |
| 21 |
// - Getting Started https://dearimgui.com/getting-started |
| 22 |
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). |
| 23 |
// - Introduction, links and more at the top of imgui.cpp |
| 24 |
|
| 25 |
// CHANGELOG |
| 26 |
// (minor and older changes stripped away, please see git history for details) |
| 27 |
// 2023-04-17: BREAKING: Removed call to ImGui::NewFrame() from ImGui_ImplGLUT_NewFrame(). Needs to be called from the main application loop, like with every other backends. |
| 28 |
// 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported). |
| 29 |
// 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion. |
| 30 |
// 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+). |
| 31 |
// 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range. |
| 32 |
// 2019-04-03: Misc: Renamed imgui_impl_freeglut.cpp/.h to imgui_impl_glut.cpp/.h. |
| 33 |
// 2019-03-25: Misc: Made io.DeltaTime always above zero. |
| 34 |
// 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window. |
| 35 |
// 2018-03-22: Added GLUT Platform binding. |
| 36 |
|
| 37 |
#include "imgui.h" |
| 38 |
#ifndef IMGUI_DISABLE |
| 39 |
#include "imgui_impl_glut.h" |
| 40 |
#define GL_SILENCE_DEPRECATION |
| 41 |
#ifdef __APPLE__ |
| 42 |
#include <GLUT/glut.h> |
| 43 |
#else |
| 44 |
#include <GL/freeglut.h> |
| 45 |
#endif |
| 46 |
|
| 47 |
#ifdef _MSC_VER |
| 48 |
#pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff) |
| 49 |
#endif |
| 50 |
|
| 51 |
static int g_Time = 0; // Current time, in milliseconds |
| 52 |
|
| 53 |
// Glut has 1 function for characters and one for "special keys". We map the characters in the 0..255 range and the keys above. |
| 54 |
static ImGuiKey ImGui_ImplGLUT_KeyToImGuiKey(int key) |
| 55 |
{ |
| 56 |
switch (key) |
| 57 |
{ |
| 58 |
case '\t': return ImGuiKey_Tab; |
| 59 |
case 256 + GLUT_KEY_LEFT: return ImGuiKey_LeftArrow; |
| 60 |
case 256 + GLUT_KEY_RIGHT: return ImGuiKey_RightArrow; |
| 61 |
case 256 + GLUT_KEY_UP: return ImGuiKey_UpArrow; |
| 62 |
case 256 + GLUT_KEY_DOWN: return ImGuiKey_DownArrow; |
| 63 |
case 256 + GLUT_KEY_PAGE_UP: return ImGuiKey_PageUp; |
| 64 |
case 256 + GLUT_KEY_PAGE_DOWN: return ImGuiKey_PageDown; |
| 65 |
case 256 + GLUT_KEY_HOME: return ImGuiKey_Home; |
| 66 |
case 256 + GLUT_KEY_END: return ImGuiKey_End; |
| 67 |
case 256 + GLUT_KEY_INSERT: return ImGuiKey_Insert; |
| 68 |
case 127: return ImGuiKey_Delete; |
| 69 |
case 8: return ImGuiKey_Backspace; |
| 70 |
case ' ': return ImGuiKey_Space; |
| 71 |
case 13: return ImGuiKey_Enter; |
| 72 |
case 27: return ImGuiKey_Escape; |
| 73 |
case 39: return ImGuiKey_Apostrophe; |
| 74 |
case 44: return ImGuiKey_Comma; |
| 75 |
case 45: return ImGuiKey_Minus; |
| 76 |
case 46: return ImGuiKey_Period; |
| 77 |
case 47: return ImGuiKey_Slash; |
| 78 |
case 59: return ImGuiKey_Semicolon; |
| 79 |
case 61: return ImGuiKey_Equal; |
| 80 |
case 91: return ImGuiKey_LeftBracket; |
| 81 |
case 92: return ImGuiKey_Backslash; |
| 82 |
case 93: return ImGuiKey_RightBracket; |
| 83 |
case 96: return ImGuiKey_GraveAccent; |
| 84 |
//case 0: return ImGuiKey_CapsLock; |
| 85 |
//case 0: return ImGuiKey_ScrollLock; |
| 86 |
case 256 + 0x006D: return ImGuiKey_NumLock; |
| 87 |
//case 0: return ImGuiKey_PrintScreen; |
| 88 |
//case 0: return ImGuiKey_Pause; |
| 89 |
//case '0': return ImGuiKey_Keypad0; |
| 90 |
//case '1': return ImGuiKey_Keypad1; |
| 91 |
//case '2': return ImGuiKey_Keypad2; |
| 92 |
//case '3': return ImGuiKey_Keypad3; |
| 93 |
//case '4': return ImGuiKey_Keypad4; |
| 94 |
//case '5': return ImGuiKey_Keypad5; |
| 95 |
//case '6': return ImGuiKey_Keypad6; |
| 96 |
//case '7': return ImGuiKey_Keypad7; |
| 97 |
//case '8': return ImGuiKey_Keypad8; |
| 98 |
//case '9': return ImGuiKey_Keypad9; |
| 99 |
//case 46: return ImGuiKey_KeypadDecimal; |
| 100 |
//case 47: return ImGuiKey_KeypadDivide; |
| 101 |
case 42: return ImGuiKey_KeypadMultiply; |
| 102 |
//case 45: return ImGuiKey_KeypadSubtract; |
| 103 |
case 43: return ImGuiKey_KeypadAdd; |
| 104 |
//case 13: return ImGuiKey_KeypadEnter; |
| 105 |
//case 0: return ImGuiKey_KeypadEqual; |
| 106 |
case 256 + 0x0072: return ImGuiKey_LeftCtrl; |
| 107 |
case 256 + 0x0070: return ImGuiKey_LeftShift; |
| 108 |
case 256 + 0x0074: return ImGuiKey_LeftAlt; |
| 109 |
//case 0: return ImGuiKey_LeftSuper; |
| 110 |
case 256 + 0x0073: return ImGuiKey_RightCtrl; |
| 111 |
case 256 + 0x0071: return ImGuiKey_RightShift; |
| 112 |
case 256 + 0x0075: return ImGuiKey_RightAlt; |
| 113 |
//case 0: return ImGuiKey_RightSuper; |
| 114 |
//case 0: return ImGuiKey_Menu; |
| 115 |
case '0': return ImGuiKey_0; |
| 116 |
case '1': return ImGuiKey_1; |
| 117 |
case '2': return ImGuiKey_2; |
| 118 |
case '3': return ImGuiKey_3; |
| 119 |
case '4': return ImGuiKey_4; |
| 120 |
case '5': return ImGuiKey_5; |
| 121 |
case '6': return ImGuiKey_6; |
| 122 |
case '7': return ImGuiKey_7; |
| 123 |
case '8': return ImGuiKey_8; |
| 124 |
case '9': return ImGuiKey_9; |
| 125 |
case 'A': case 'a': return ImGuiKey_A; |
| 126 |
case 'B': case 'b': return ImGuiKey_B; |
| 127 |
case 'C': case 'c': return ImGuiKey_C; |
| 128 |
case 'D': case 'd': return ImGuiKey_D; |
| 129 |
case 'E': case 'e': return ImGuiKey_E; |
| 130 |
case 'F': case 'f': return ImGuiKey_F; |
| 131 |
case 'G': case 'g': return ImGuiKey_G; |
| 132 |
case 'H': case 'h': return ImGuiKey_H; |
| 133 |
case 'I': case 'i': return ImGuiKey_I; |
| 134 |
case 'J': case 'j': return ImGuiKey_J; |
| 135 |
case 'K': case 'k': return ImGuiKey_K; |
| 136 |
case 'L': case 'l': return ImGuiKey_L; |
| 137 |
case 'M': case 'm': return ImGuiKey_M; |
| 138 |
case 'N': case 'n': return ImGuiKey_N; |
| 139 |
case 'O': case 'o': return ImGuiKey_O; |
| 140 |
case 'P': case 'p': return ImGuiKey_P; |
| 141 |
case 'Q': case 'q': return ImGuiKey_Q; |
| 142 |
case 'R': case 'r': return ImGuiKey_R; |
| 143 |
case 'S': case 's': return ImGuiKey_S; |
| 144 |
case 'T': case 't': return ImGuiKey_T; |
| 145 |
case 'U': case 'u': return ImGuiKey_U; |
| 146 |
case 'V': case 'v': return ImGuiKey_V; |
| 147 |
case 'W': case 'w': return ImGuiKey_W; |
| 148 |
case 'X': case 'x': return ImGuiKey_X; |
| 149 |
case 'Y': case 'y': return ImGuiKey_Y; |
| 150 |
case 'Z': case 'z': return ImGuiKey_Z; |
| 151 |
case 256 + GLUT_KEY_F1: return ImGuiKey_F1; |
| 152 |
case 256 + GLUT_KEY_F2: return ImGuiKey_F2; |
| 153 |
case 256 + GLUT_KEY_F3: return ImGuiKey_F3; |
| 154 |
case 256 + GLUT_KEY_F4: return ImGuiKey_F4; |
| 155 |
case 256 + GLUT_KEY_F5: return ImGuiKey_F5; |
| 156 |
case 256 + GLUT_KEY_F6: return ImGuiKey_F6; |
| 157 |
case 256 + GLUT_KEY_F7: return ImGuiKey_F7; |
| 158 |
case 256 + GLUT_KEY_F8: return ImGuiKey_F8; |
| 159 |
case 256 + GLUT_KEY_F9: return ImGuiKey_F9; |
| 160 |
case 256 + GLUT_KEY_F10: return ImGuiKey_F10; |
| 161 |
case 256 + GLUT_KEY_F11: return ImGuiKey_F11; |
| 162 |
case 256 + GLUT_KEY_F12: return ImGuiKey_F12; |
| 163 |
default: return ImGuiKey_None; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
bool ImGui_ImplGLUT_Init() |
| 168 |
{ |
| 169 |
ImGuiIO& io = ImGui::GetIO(); |
| 170 |
IMGUI_CHECKVERSION(); |
| 171 |
|
| 172 |
#ifdef FREEGLUT |
| 173 |
io.BackendPlatformName = "imgui_impl_glut (freeglut)"; |
| 174 |
#else |
| 175 |
io.BackendPlatformName = "imgui_impl_glut"; |
| 176 |
#endif |
| 177 |
g_Time = 0; |
| 178 |
|
| 179 |
return true; |
| 180 |
} |
| 181 |
|
| 182 |
void ImGui_ImplGLUT_InstallFuncs() |
| 183 |
{ |
| 184 |
glutReshapeFunc(ImGui_ImplGLUT_ReshapeFunc); |
| 185 |
glutMotionFunc(ImGui_ImplGLUT_MotionFunc); |
| 186 |
glutPassiveMotionFunc(ImGui_ImplGLUT_MotionFunc); |
| 187 |
glutMouseFunc(ImGui_ImplGLUT_MouseFunc); |
| 188 |
#ifdef __FREEGLUT_EXT_H__ |
| 189 |
glutMouseWheelFunc(ImGui_ImplGLUT_MouseWheelFunc); |
| 190 |
#endif |
| 191 |
glutKeyboardFunc(ImGui_ImplGLUT_KeyboardFunc); |
| 192 |
glutKeyboardUpFunc(ImGui_ImplGLUT_KeyboardUpFunc); |
| 193 |
glutSpecialFunc(ImGui_ImplGLUT_SpecialFunc); |
| 194 |
glutSpecialUpFunc(ImGui_ImplGLUT_SpecialUpFunc); |
| 195 |
} |
| 196 |
|
| 197 |
void ImGui_ImplGLUT_Shutdown() |
| 198 |
{ |
| 199 |
ImGuiIO& io = ImGui::GetIO(); |
| 200 |
io.BackendPlatformName = nullptr; |
| 201 |
} |
| 202 |
|
| 203 |
void ImGui_ImplGLUT_NewFrame() |
| 204 |
{ |
| 205 |
// Setup time step |
| 206 |
ImGuiIO& io = ImGui::GetIO(); |
| 207 |
int current_time = glutGet(GLUT_ELAPSED_TIME); |
| 208 |
int delta_time_ms = (current_time - g_Time); |
| 209 |
if (delta_time_ms <= 0) |
| 210 |
delta_time_ms = 1; |
| 211 |
io.DeltaTime = delta_time_ms / 1000.0f; |
| 212 |
g_Time = current_time; |
| 213 |
} |
| 214 |
|
| 215 |
static void ImGui_ImplGLUT_UpdateKeyModifiers() |
| 216 |
{ |
| 217 |
ImGuiIO& io = ImGui::GetIO(); |
| 218 |
int glut_key_mods = glutGetModifiers(); |
| 219 |
io.AddKeyEvent(ImGuiMod_Ctrl, (glut_key_mods & GLUT_ACTIVE_CTRL) != 0); |
| 220 |
io.AddKeyEvent(ImGuiMod_Shift, (glut_key_mods & GLUT_ACTIVE_SHIFT) != 0); |
| 221 |
io.AddKeyEvent(ImGuiMod_Alt, (glut_key_mods & GLUT_ACTIVE_ALT) != 0); |
| 222 |
} |
| 223 |
|
| 224 |
static void ImGui_ImplGLUT_AddKeyEvent(ImGuiKey key, bool down, int native_keycode) |
| 225 |
{ |
| 226 |
ImGuiIO& io = ImGui::GetIO(); |
| 227 |
io.AddKeyEvent(key, down); |
| 228 |
io.SetKeyEventNativeData(key, native_keycode, -1); // To support legacy indexing (<1.87 user code) |
| 229 |
} |
| 230 |
|
| 231 |
void ImGui_ImplGLUT_KeyboardFunc(unsigned char c, int x, int y) |
| 232 |
{ |
| 233 |
// Send character to imgui |
| 234 |
//printf("char_down_func %d '%c'\n", c, c); |
| 235 |
ImGuiIO& io = ImGui::GetIO(); |
| 236 |
if (c >= 32) |
| 237 |
io.AddInputCharacter((unsigned int)c); |
| 238 |
|
| 239 |
ImGuiKey key = ImGui_ImplGLUT_KeyToImGuiKey(c); |
| 240 |
ImGui_ImplGLUT_AddKeyEvent(key, true, c); |
| 241 |
ImGui_ImplGLUT_UpdateKeyModifiers(); |
| 242 |
(void)x; (void)y; // Unused |
| 243 |
} |
| 244 |
|
| 245 |
void ImGui_ImplGLUT_KeyboardUpFunc(unsigned char c, int x, int y) |
| 246 |
{ |
| 247 |
//printf("char_up_func %d '%c'\n", c, c); |
| 248 |
ImGuiKey key = ImGui_ImplGLUT_KeyToImGuiKey(c); |
| 249 |
ImGui_ImplGLUT_AddKeyEvent(key, false, c); |
| 250 |
ImGui_ImplGLUT_UpdateKeyModifiers(); |
| 251 |
(void)x; (void)y; // Unused |
| 252 |
} |
| 253 |
|
| 254 |
void ImGui_ImplGLUT_SpecialFunc(int key, int x, int y) |
| 255 |
{ |
| 256 |
//printf("key_down_func %d\n", key); |
| 257 |
ImGuiKey imgui_key = ImGui_ImplGLUT_KeyToImGuiKey(key + 256); |
| 258 |
ImGui_ImplGLUT_AddKeyEvent(imgui_key, true, key + 256); |
| 259 |
ImGui_ImplGLUT_UpdateKeyModifiers(); |
| 260 |
(void)x; (void)y; // Unused |
| 261 |
} |
| 262 |
|
| 263 |
void ImGui_ImplGLUT_SpecialUpFunc(int key, int x, int y) |
| 264 |
{ |
| 265 |
//printf("key_up_func %d\n", key); |
| 266 |
ImGuiKey imgui_key = ImGui_ImplGLUT_KeyToImGuiKey(key + 256); |
| 267 |
ImGui_ImplGLUT_AddKeyEvent(imgui_key, false, key + 256); |
| 268 |
ImGui_ImplGLUT_UpdateKeyModifiers(); |
| 269 |
(void)x; (void)y; // Unused |
| 270 |
} |
| 271 |
|
| 272 |
void ImGui_ImplGLUT_MouseFunc(int glut_button, int state, int x, int y) |
| 273 |
{ |
| 274 |
ImGuiIO& io = ImGui::GetIO(); |
| 275 |
io.AddMousePosEvent((float)x, (float)y); |
| 276 |
int button = -1; |
| 277 |
if (glut_button == GLUT_LEFT_BUTTON) button = 0; |
| 278 |
if (glut_button == GLUT_RIGHT_BUTTON) button = 1; |
| 279 |
if (glut_button == GLUT_MIDDLE_BUTTON) button = 2; |
| 280 |
if (button != -1 && (state == GLUT_DOWN || state == GLUT_UP)) |
| 281 |
io.AddMouseButtonEvent(button, state == GLUT_DOWN); |
| 282 |
} |
| 283 |
|
| 284 |
#ifdef __FREEGLUT_EXT_H__ |
| 285 |
void ImGui_ImplGLUT_MouseWheelFunc(int button, int dir, int x, int y) |
| 286 |
{ |
| 287 |
ImGuiIO& io = ImGui::GetIO(); |
| 288 |
io.AddMousePosEvent((float)x, (float)y); |
| 289 |
if (dir != 0) |
| 290 |
io.AddMouseWheelEvent(0.0f, dir > 0 ? 1.0f : -1.0f); |
| 291 |
(void)button; // Unused |
| 292 |
} |
| 293 |
#endif |
| 294 |
|
| 295 |
void ImGui_ImplGLUT_ReshapeFunc(int w, int h) |
| 296 |
{ |
| 297 |
ImGuiIO& io = ImGui::GetIO(); |
| 298 |
io.DisplaySize = ImVec2((float)w, (float)h); |
| 299 |
} |
| 300 |
|
| 301 |
void ImGui_ImplGLUT_MotionFunc(int x, int y) |
| 302 |
{ |
| 303 |
ImGuiIO& io = ImGui::GetIO(); |
| 304 |
io.AddMousePosEvent((float)x, (float)y); |
| 305 |
} |
| 306 |
|
| 307 |
//----------------------------------------------------------------------------- |
| 308 |
|
| 309 |
#endif // #ifndef IMGUI_DISABLE |