imgui

FORK: Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
git clone https://git.neptards.moe/neptards/imgui.git
Log | Files | Refs

imgui_impl_glut.cpp (7547B)


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