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