1 // dear imgui: Platform Binding for FreeGLUT
2 // This needs to be used along with a Renderer (e.g. OpenGL2)
3 
4 // Issues:
5 //  [ ] Platform: GLUT is unable to distinguish e.g. Backspace from CTRL+H or TAB from CTRL+I
6 //  [ ] Platform: Missing gamepad support.
7 
8 // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
9 // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
10 // https://github.com/ocornut/imgui
11 
12 // CHANGELOG
13 // (minor and older changes stripped away, please see git history for details)
14 //  2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
15 //  2018-03-22: Added FreeGLUT Platform binding.
16 
17 #include "imgui.h"
18 #include "imgui_impl_freeglut.h"
19 #include <GL/freeglut.h>
20 
21 #ifdef _MSC_VER
22 #pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff)
23 #endif
24 
25 static int g_Time = 0;          // Current time, in milliseconds
26 
ImGui_ImplFreeGLUT_Init()27 bool ImGui_ImplFreeGLUT_Init()
28 {
29     ImGuiIO& io = ImGui::GetIO();
30     io.BackendPlatformName ="imgui_impl_freeglut";
31 
32     g_Time = 0;
33 
34     // Glut has 1 function for characters and one for "special keys". We map the characters in the 0..255 range and the keys above.
35     io.KeyMap[ImGuiKey_Tab]         = '\t'; // == 9 == CTRL+I
36     io.KeyMap[ImGuiKey_LeftArrow]   = 256 + GLUT_KEY_LEFT;
37     io.KeyMap[ImGuiKey_RightArrow]  = 256 + GLUT_KEY_RIGHT;
38     io.KeyMap[ImGuiKey_UpArrow]     = 256 + GLUT_KEY_UP;
39     io.KeyMap[ImGuiKey_DownArrow]   = 256 + GLUT_KEY_DOWN;
40     io.KeyMap[ImGuiKey_PageUp]      = 256 + GLUT_KEY_PAGE_UP;
41     io.KeyMap[ImGuiKey_PageDown]    = 256 + GLUT_KEY_PAGE_DOWN;
42     io.KeyMap[ImGuiKey_Home]        = 256 + GLUT_KEY_HOME;
43     io.KeyMap[ImGuiKey_End]         = 256 + GLUT_KEY_END;
44     io.KeyMap[ImGuiKey_Insert]      = 256 + GLUT_KEY_INSERT;
45     io.KeyMap[ImGuiKey_Delete]      = 127;
46     io.KeyMap[ImGuiKey_Backspace]   = 8;  // == CTRL+H
47     io.KeyMap[ImGuiKey_Space]       = ' ';
48     io.KeyMap[ImGuiKey_Enter]       = 13; // == CTRL+M
49     io.KeyMap[ImGuiKey_Escape]      = 27;
50     io.KeyMap[ImGuiKey_A]           = 'A';
51     io.KeyMap[ImGuiKey_C]           = 'C';
52     io.KeyMap[ImGuiKey_V]           = 'V';
53     io.KeyMap[ImGuiKey_X]           = 'X';
54     io.KeyMap[ImGuiKey_Y]           = 'Y';
55     io.KeyMap[ImGuiKey_Z]           = 'Z';
56 
57     return true;
58 }
59 
ImGui_ImplFreeGLUT_InstallFuncs()60 void ImGui_ImplFreeGLUT_InstallFuncs()
61 {
62     glutReshapeFunc(ImGui_ImplFreeGLUT_ReshapeFunc);
63     glutMotionFunc(ImGui_ImplFreeGLUT_MotionFunc);
64     glutPassiveMotionFunc(ImGui_ImplFreeGLUT_MotionFunc);
65     glutMouseFunc(ImGui_ImplFreeGLUT_MouseFunc);
66     glutMouseWheelFunc(ImGui_ImplFreeGLUT_MouseWheelFunc);
67     glutKeyboardFunc(ImGui_ImplFreeGLUT_KeyboardFunc);
68     glutKeyboardUpFunc(ImGui_ImplFreeGLUT_KeyboardUpFunc);
69     glutSpecialFunc(ImGui_ImplFreeGLUT_SpecialFunc);
70     glutSpecialUpFunc(ImGui_ImplFreeGLUT_SpecialUpFunc);
71 }
72 
ImGui_ImplFreeGLUT_Shutdown()73 void ImGui_ImplFreeGLUT_Shutdown()
74 {
75 }
76 
ImGui_ImplFreeGLUT_NewFrame()77 void ImGui_ImplFreeGLUT_NewFrame()
78 {
79     // Setup time step
80     ImGuiIO& io = ImGui::GetIO();
81     int current_time = glutGet(GLUT_ELAPSED_TIME);
82     io.DeltaTime = (current_time - g_Time) / 1000.0f;
83     g_Time = current_time;
84 
85     // Start the frame
86     ImGui::NewFrame();
87 }
88 
ImGui_ImplFreeGLUT_UpdateKeyboardMods()89 static void ImGui_ImplFreeGLUT_UpdateKeyboardMods()
90 {
91     ImGuiIO& io = ImGui::GetIO();
92     int mods = glutGetModifiers();
93     io.KeyCtrl = (mods & GLUT_ACTIVE_CTRL) != 0;
94     io.KeyShift = (mods & GLUT_ACTIVE_SHIFT) != 0;
95     io.KeyAlt = (mods & GLUT_ACTIVE_ALT) != 0;
96 }
97 
ImGui_ImplFreeGLUT_KeyboardFunc(unsigned char c,int x,int y)98 void ImGui_ImplFreeGLUT_KeyboardFunc(unsigned char c, int x, int y)
99 {
100     // Send character to imgui
101     //printf("char_down_func %d '%c'\n", c, c);
102     ImGuiIO& io = ImGui::GetIO();
103     if (c >= 32)
104         io.AddInputCharacter((unsigned short)c);
105 
106     // Store letters in KeysDown[] array as both uppercase and lowercase + Handle GLUT translating CTRL+A..CTRL+Z as 1..26.
107     // 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.
108     if (c >= 1 && c <= 26)
109         io.KeysDown[c] = io.KeysDown[c - 1 + 'a'] = io.KeysDown[c - 1 + 'A'] = true;
110     else if (c >= 'a' && c <= 'z')
111         io.KeysDown[c] = io.KeysDown[c - 'a' + 'A'] = true;
112     else if (c >= 'A' && c <= 'Z')
113         io.KeysDown[c] = io.KeysDown[c - 'A' + 'a'] = true;
114     else
115         io.KeysDown[c] = true;
116     ImGui_ImplFreeGLUT_UpdateKeyboardMods();
117     (void)x; (void)y; // Unused
118 }
119 
ImGui_ImplFreeGLUT_KeyboardUpFunc(unsigned char c,int x,int y)120 void ImGui_ImplFreeGLUT_KeyboardUpFunc(unsigned char c, int x, int y)
121 {
122     //printf("char_up_func %d '%c'\n", c, c);
123     ImGuiIO& io = ImGui::GetIO();
124     if (c >= 1 && c <= 26)
125         io.KeysDown[c] = io.KeysDown[c - 1 + 'a'] = io.KeysDown[c - 1 + 'A'] = false;
126     else if (c >= 'a' && c <= 'z')
127         io.KeysDown[c] = io.KeysDown[c - 'a' + 'A'] = false;
128     else if (c >= 'A' && c <= 'Z')
129         io.KeysDown[c] = io.KeysDown[c - 'A' + 'a'] = false;
130     else
131         io.KeysDown[c] = false;
132     ImGui_ImplFreeGLUT_UpdateKeyboardMods();
133     (void)x; (void)y; // Unused
134 }
135 
ImGui_ImplFreeGLUT_SpecialFunc(int key,int x,int y)136 void ImGui_ImplFreeGLUT_SpecialFunc(int key, int x, int y)
137 {
138     //printf("key_down_func %d\n", key);
139     ImGuiIO& io = ImGui::GetIO();
140     if (key + 256 < IM_ARRAYSIZE(io.KeysDown))
141         io.KeysDown[key + 256] = true;
142     ImGui_ImplFreeGLUT_UpdateKeyboardMods();
143     (void)x; (void)y; // Unused
144 }
145 
ImGui_ImplFreeGLUT_SpecialUpFunc(int key,int x,int y)146 void ImGui_ImplFreeGLUT_SpecialUpFunc(int key, int x, int y)
147 {
148     //printf("key_up_func %d\n", key);
149     ImGuiIO& io = ImGui::GetIO();
150     if (key + 256 < IM_ARRAYSIZE(io.KeysDown))
151         io.KeysDown[key + 256] = false;
152     ImGui_ImplFreeGLUT_UpdateKeyboardMods();
153     (void)x; (void)y; // Unused
154 }
155 
ImGui_ImplFreeGLUT_MouseFunc(int glut_button,int state,int x,int y)156 void ImGui_ImplFreeGLUT_MouseFunc(int glut_button, int state, int x, int y)
157 {
158     ImGuiIO& io = ImGui::GetIO();
159     io.MousePos = ImVec2((float)x, (float)y);
160     int button = -1;
161     if (glut_button == GLUT_LEFT_BUTTON) button = 0;
162     if (glut_button == GLUT_RIGHT_BUTTON) button = 1;
163     if (glut_button == GLUT_MIDDLE_BUTTON) button = 2;
164     if (button != -1 && state == GLUT_DOWN)
165         io.MouseDown[button] = true;
166     if (button != -1 && state == GLUT_UP)
167         io.MouseDown[button] = false;
168 }
169 
ImGui_ImplFreeGLUT_MouseWheelFunc(int button,int dir,int x,int y)170 void ImGui_ImplFreeGLUT_MouseWheelFunc(int button, int dir, int x, int y)
171 {
172     ImGuiIO& io = ImGui::GetIO();
173     io.MousePos = ImVec2((float)x, (float)y);
174     if (dir > 0)
175         io.MouseWheel += 1.0;
176     else if (dir < 0)
177         io.MouseWheel -= 1.0;
178     (void)button; // Unused
179 }
180 
ImGui_ImplFreeGLUT_ReshapeFunc(int w,int h)181 void ImGui_ImplFreeGLUT_ReshapeFunc(int w, int h)
182 {
183     ImGuiIO& io = ImGui::GetIO();
184     io.DisplaySize = ImVec2((float)w, (float)h);
185 }
186 
ImGui_ImplFreeGLUT_MotionFunc(int x,int y)187 void ImGui_ImplFreeGLUT_MotionFunc(int x, int y)
188 {
189     ImGuiIO& io = ImGui::GetIO();
190     io.MousePos = ImVec2((float)x, (float)y);
191 }
192