2020-10-12 17:34:22 +02:00
|
|
|
// dear imgui: Platform Backend for GLUT/FreeGLUT
|
2018-06-11 19:27:11 +02:00
|
|
|
// This needs to be used along with a Renderer (e.g. OpenGL2)
|
2019-03-26 12:14:17 +01:00
|
|
|
|
2021-01-03 14:59:32 +01:00
|
|
|
// !!! GLUT/FreeGLUT IS OBSOLETE PREHISTORIC SOFTWARE. Using GLUT is not recommended unless you really miss the 90's. !!!
|
|
|
|
// !!! If someone or something is teaching you GLUT today, you are being abused. Please show some resistance. !!!
|
2019-06-12 16:15:08 +02:00
|
|
|
// !!! Nowadays, prefer using GLFW or SDL instead!
|
2018-06-11 19:27:11 +02:00
|
|
|
|
|
|
|
// Issues:
|
|
|
|
// [ ] Platform: GLUT is unable to distinguish e.g. Backspace from CTRL+H or TAB from CTRL+I
|
2019-04-25 12:01:01 +02:00
|
|
|
// [ ] Platform: Missing mouse cursor shape/visibility support.
|
2019-03-16 15:48:21 +01:00
|
|
|
// [ ] Platform: Missing clipboard support (not supported by Glut).
|
2018-09-20 10:37:55 +02:00
|
|
|
// [ ] Platform: Missing gamepad support.
|
2018-06-11 19:27:11 +02:00
|
|
|
|
2021-05-27 13:59:35 +02:00
|
|
|
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
|
|
|
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
2020-10-14 12:22:53 +02:00
|
|
|
// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
|
|
|
|
// Read online: https://github.com/ocornut/imgui/tree/master/docs
|
2018-06-11 19:27:11 +02:00
|
|
|
|
|
|
|
// CHANGELOG
|
|
|
|
// (minor and older changes stripped away, please see git history for details)
|
2019-04-03 16:23:50 +02:00
|
|
|
// 2019-04-03: Misc: Renamed imgui_impl_freeglut.cpp/.h to imgui_impl_glut.cpp/.h.
|
2019-03-26 12:14:17 +01:00
|
|
|
// 2019-03-25: Misc: Made io.DeltaTime always above zero.
|
2018-11-30 18:18:15 +01:00
|
|
|
// 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
|
2019-04-03 15:53:11 +02:00
|
|
|
// 2018-03-22: Added GLUT Platform binding.
|
2018-06-11 19:27:11 +02:00
|
|
|
|
|
|
|
#include "imgui.h"
|
2019-04-03 15:53:11 +02:00
|
|
|
#include "imgui_impl_glut.h"
|
2019-04-02 13:47:14 +02:00
|
|
|
#ifdef __APPLE__
|
2020-06-30 15:31:54 +02:00
|
|
|
#include <GLUT/glut.h>
|
2019-04-02 13:47:14 +02:00
|
|
|
#else
|
2020-06-30 15:31:54 +02:00
|
|
|
#include <GL/freeglut.h>
|
2019-04-02 13:47:14 +02:00
|
|
|
#endif
|
2018-06-11 19:27:11 +02:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int g_Time = 0; // Current time, in milliseconds
|
|
|
|
|
2019-04-03 15:53:11 +02:00
|
|
|
bool ImGui_ImplGLUT_Init()
|
2018-06-11 19:27:11 +02:00
|
|
|
{
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
2020-01-27 12:11:29 +01:00
|
|
|
|
|
|
|
#ifdef FREEGLUT
|
2020-06-30 15:31:54 +02:00
|
|
|
io.BackendPlatformName = "imgui_impl_glut (freeglut)";
|
2020-01-27 12:11:29 +01:00
|
|
|
#else
|
2020-06-30 15:31:54 +02:00
|
|
|
io.BackendPlatformName = "imgui_impl_glut";
|
2020-01-27 12:11:29 +01:00
|
|
|
#endif
|
2018-11-30 18:18:15 +01:00
|
|
|
|
2018-06-11 19:27:11 +02:00
|
|
|
g_Time = 0;
|
|
|
|
|
|
|
|
// Glut has 1 function for characters and one for "special keys". We map the characters in the 0..255 range and the keys above.
|
|
|
|
io.KeyMap[ImGuiKey_Tab] = '\t'; // == 9 == CTRL+I
|
|
|
|
io.KeyMap[ImGuiKey_LeftArrow] = 256 + GLUT_KEY_LEFT;
|
|
|
|
io.KeyMap[ImGuiKey_RightArrow] = 256 + GLUT_KEY_RIGHT;
|
|
|
|
io.KeyMap[ImGuiKey_UpArrow] = 256 + GLUT_KEY_UP;
|
|
|
|
io.KeyMap[ImGuiKey_DownArrow] = 256 + GLUT_KEY_DOWN;
|
|
|
|
io.KeyMap[ImGuiKey_PageUp] = 256 + GLUT_KEY_PAGE_UP;
|
|
|
|
io.KeyMap[ImGuiKey_PageDown] = 256 + GLUT_KEY_PAGE_DOWN;
|
|
|
|
io.KeyMap[ImGuiKey_Home] = 256 + GLUT_KEY_HOME;
|
|
|
|
io.KeyMap[ImGuiKey_End] = 256 + GLUT_KEY_END;
|
2018-06-11 20:03:35 +02:00
|
|
|
io.KeyMap[ImGuiKey_Insert] = 256 + GLUT_KEY_INSERT;
|
2018-06-11 19:27:11 +02:00
|
|
|
io.KeyMap[ImGuiKey_Delete] = 127;
|
|
|
|
io.KeyMap[ImGuiKey_Backspace] = 8; // == CTRL+H
|
2018-06-11 20:03:35 +02:00
|
|
|
io.KeyMap[ImGuiKey_Space] = ' ';
|
2018-06-11 19:27:11 +02:00
|
|
|
io.KeyMap[ImGuiKey_Enter] = 13; // == CTRL+M
|
|
|
|
io.KeyMap[ImGuiKey_Escape] = 27;
|
2021-12-21 18:25:46 +01:00
|
|
|
io.KeyMap[ImGuiKey_KeypadEnter] = 13; // == CTRL+M
|
2018-06-11 19:27:11 +02:00
|
|
|
io.KeyMap[ImGuiKey_A] = 'A';
|
|
|
|
io.KeyMap[ImGuiKey_C] = 'C';
|
|
|
|
io.KeyMap[ImGuiKey_V] = 'V';
|
|
|
|
io.KeyMap[ImGuiKey_X] = 'X';
|
|
|
|
io.KeyMap[ImGuiKey_Y] = 'Y';
|
|
|
|
io.KeyMap[ImGuiKey_Z] = 'Z';
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:53:11 +02:00
|
|
|
void ImGui_ImplGLUT_InstallFuncs()
|
2018-06-11 19:27:11 +02:00
|
|
|
{
|
2019-04-03 15:53:11 +02:00
|
|
|
glutReshapeFunc(ImGui_ImplGLUT_ReshapeFunc);
|
|
|
|
glutMotionFunc(ImGui_ImplGLUT_MotionFunc);
|
|
|
|
glutPassiveMotionFunc(ImGui_ImplGLUT_MotionFunc);
|
|
|
|
glutMouseFunc(ImGui_ImplGLUT_MouseFunc);
|
2019-04-02 17:07:28 +02:00
|
|
|
#ifdef __FREEGLUT_EXT_H__
|
2019-04-03 15:53:11 +02:00
|
|
|
glutMouseWheelFunc(ImGui_ImplGLUT_MouseWheelFunc);
|
2019-04-02 13:47:14 +02:00
|
|
|
#endif
|
2019-04-03 15:53:11 +02:00
|
|
|
glutKeyboardFunc(ImGui_ImplGLUT_KeyboardFunc);
|
|
|
|
glutKeyboardUpFunc(ImGui_ImplGLUT_KeyboardUpFunc);
|
|
|
|
glutSpecialFunc(ImGui_ImplGLUT_SpecialFunc);
|
|
|
|
glutSpecialUpFunc(ImGui_ImplGLUT_SpecialUpFunc);
|
2018-06-11 19:27:11 +02:00
|
|
|
}
|
|
|
|
|
2019-04-03 15:53:11 +02:00
|
|
|
void ImGui_ImplGLUT_Shutdown()
|
2018-06-11 19:27:11 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:53:11 +02:00
|
|
|
void ImGui_ImplGLUT_NewFrame()
|
2018-06-11 19:27:11 +02:00
|
|
|
{
|
|
|
|
// Setup time step
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
int current_time = glutGet(GLUT_ELAPSED_TIME);
|
2019-03-26 12:14:17 +01:00
|
|
|
int delta_time_ms = (current_time - g_Time);
|
|
|
|
if (delta_time_ms <= 0)
|
|
|
|
delta_time_ms = 1;
|
|
|
|
io.DeltaTime = delta_time_ms / 1000.0f;
|
2018-06-11 19:27:11 +02:00
|
|
|
g_Time = current_time;
|
|
|
|
|
|
|
|
// Start the frame
|
|
|
|
ImGui::NewFrame();
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:53:11 +02:00
|
|
|
static void ImGui_ImplGLUT_UpdateKeyboardMods()
|
2018-06-11 19:27:11 +02:00
|
|
|
{
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
int mods = glutGetModifiers();
|
|
|
|
io.KeyCtrl = (mods & GLUT_ACTIVE_CTRL) != 0;
|
|
|
|
io.KeyShift = (mods & GLUT_ACTIVE_SHIFT) != 0;
|
|
|
|
io.KeyAlt = (mods & GLUT_ACTIVE_ALT) != 0;
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:53:11 +02:00
|
|
|
void ImGui_ImplGLUT_KeyboardFunc(unsigned char c, int x, int y)
|
2018-06-11 19:27:11 +02:00
|
|
|
{
|
|
|
|
// Send character to imgui
|
|
|
|
//printf("char_down_func %d '%c'\n", c, c);
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
if (c >= 32)
|
2019-05-11 10:33:56 +02:00
|
|
|
io.AddInputCharacter((unsigned int)c);
|
2018-06-11 19:27:11 +02:00
|
|
|
|
2019-01-20 17:56:17 +01:00
|
|
|
// Store letters in KeysDown[] array as both uppercase and lowercase + Handle GLUT translating CTRL+A..CTRL+Z as 1..26.
|
2018-06-11 19:27:11 +02:00
|
|
|
// 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.
|
|
|
|
if (c >= 1 && c <= 26)
|
|
|
|
io.KeysDown[c] = io.KeysDown[c - 1 + 'a'] = io.KeysDown[c - 1 + 'A'] = true;
|
|
|
|
else if (c >= 'a' && c <= 'z')
|
|
|
|
io.KeysDown[c] = io.KeysDown[c - 'a' + 'A'] = true;
|
|
|
|
else if (c >= 'A' && c <= 'Z')
|
|
|
|
io.KeysDown[c] = io.KeysDown[c - 'A' + 'a'] = true;
|
|
|
|
else
|
|
|
|
io.KeysDown[c] = true;
|
2019-04-03 15:53:11 +02:00
|
|
|
ImGui_ImplGLUT_UpdateKeyboardMods();
|
2018-06-11 19:27:11 +02:00
|
|
|
(void)x; (void)y; // Unused
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:53:11 +02:00
|
|
|
void ImGui_ImplGLUT_KeyboardUpFunc(unsigned char c, int x, int y)
|
2018-06-11 19:27:11 +02:00
|
|
|
{
|
|
|
|
//printf("char_up_func %d '%c'\n", c, c);
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
if (c >= 1 && c <= 26)
|
|
|
|
io.KeysDown[c] = io.KeysDown[c - 1 + 'a'] = io.KeysDown[c - 1 + 'A'] = false;
|
|
|
|
else if (c >= 'a' && c <= 'z')
|
|
|
|
io.KeysDown[c] = io.KeysDown[c - 'a' + 'A'] = false;
|
|
|
|
else if (c >= 'A' && c <= 'Z')
|
|
|
|
io.KeysDown[c] = io.KeysDown[c - 'A' + 'a'] = false;
|
|
|
|
else
|
|
|
|
io.KeysDown[c] = false;
|
2019-04-03 15:53:11 +02:00
|
|
|
ImGui_ImplGLUT_UpdateKeyboardMods();
|
2018-06-11 19:27:11 +02:00
|
|
|
(void)x; (void)y; // Unused
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:53:11 +02:00
|
|
|
void ImGui_ImplGLUT_SpecialFunc(int key, int x, int y)
|
2018-06-11 19:27:11 +02:00
|
|
|
{
|
|
|
|
//printf("key_down_func %d\n", key);
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
if (key + 256 < IM_ARRAYSIZE(io.KeysDown))
|
|
|
|
io.KeysDown[key + 256] = true;
|
2019-04-03 15:53:11 +02:00
|
|
|
ImGui_ImplGLUT_UpdateKeyboardMods();
|
2018-06-11 19:27:11 +02:00
|
|
|
(void)x; (void)y; // Unused
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:53:11 +02:00
|
|
|
void ImGui_ImplGLUT_SpecialUpFunc(int key, int x, int y)
|
2018-06-11 19:27:11 +02:00
|
|
|
{
|
|
|
|
//printf("key_up_func %d\n", key);
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
if (key + 256 < IM_ARRAYSIZE(io.KeysDown))
|
|
|
|
io.KeysDown[key + 256] = false;
|
2019-04-03 15:53:11 +02:00
|
|
|
ImGui_ImplGLUT_UpdateKeyboardMods();
|
2018-06-11 19:27:11 +02:00
|
|
|
(void)x; (void)y; // Unused
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:53:11 +02:00
|
|
|
void ImGui_ImplGLUT_MouseFunc(int glut_button, int state, int x, int y)
|
2018-06-11 19:27:11 +02:00
|
|
|
{
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
io.MousePos = ImVec2((float)x, (float)y);
|
|
|
|
int button = -1;
|
|
|
|
if (glut_button == GLUT_LEFT_BUTTON) button = 0;
|
|
|
|
if (glut_button == GLUT_RIGHT_BUTTON) button = 1;
|
|
|
|
if (glut_button == GLUT_MIDDLE_BUTTON) button = 2;
|
|
|
|
if (button != -1 && state == GLUT_DOWN)
|
|
|
|
io.MouseDown[button] = true;
|
|
|
|
if (button != -1 && state == GLUT_UP)
|
|
|
|
io.MouseDown[button] = false;
|
|
|
|
}
|
|
|
|
|
2019-04-02 17:07:28 +02:00
|
|
|
#ifdef __FREEGLUT_EXT_H__
|
2019-04-03 15:53:11 +02:00
|
|
|
void ImGui_ImplGLUT_MouseWheelFunc(int button, int dir, int x, int y)
|
2018-06-11 19:27:11 +02:00
|
|
|
{
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
io.MousePos = ImVec2((float)x, (float)y);
|
|
|
|
if (dir > 0)
|
|
|
|
io.MouseWheel += 1.0;
|
|
|
|
else if (dir < 0)
|
|
|
|
io.MouseWheel -= 1.0;
|
|
|
|
(void)button; // Unused
|
|
|
|
}
|
2019-04-02 17:07:28 +02:00
|
|
|
#endif
|
2018-06-11 19:27:11 +02:00
|
|
|
|
2019-04-03 15:53:11 +02:00
|
|
|
void ImGui_ImplGLUT_ReshapeFunc(int w, int h)
|
2018-06-11 19:27:11 +02:00
|
|
|
{
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
io.DisplaySize = ImVec2((float)w, (float)h);
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:53:11 +02:00
|
|
|
void ImGui_ImplGLUT_MotionFunc(int x, int y)
|
2018-06-11 19:27:11 +02:00
|
|
|
{
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
io.MousePos = ImVec2((float)x, (float)y);
|
|
|
|
}
|