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