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