2020-10-12 15:34:22 +00:00
// dear imgui: Platform Backend for GLUT/FreeGLUT
2018-06-11 17:27:11 +00:00
// This needs to be used along with a Renderer (e.g. OpenGL2)
2019-03-26 11:14:17 +00:00
2021-01-03 13:59:32 +00: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 14:15:08 +00:00
// !!! Nowadays, prefer using GLFW or SDL instead!
2018-06-11 17:27:11 +00:00
2021-12-12 11:02:20 +00:00
// Implemented features:
// [X] Platform: Partial keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy GLUT values will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set]
2018-06-11 17:27:11 +00:00
// Issues:
// [ ] Platform: GLUT is unable to distinguish e.g. Backspace from CTRL+H or TAB from CTRL+I
2019-04-25 10:01:01 +00:00
// [ ] Platform: Missing mouse cursor shape/visibility support.
2019-03-16 14:48:21 +00:00
// [ ] Platform: Missing clipboard support (not supported by Glut).
2018-09-20 08:37:55 +00:00
// [ ] Platform: Missing gamepad support.
2018-06-11 17:27:11 +00:00
2021-12-12 11:02:20 +00:00
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
2021-05-27 11:59:35 +00:00
// 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 10:22:53 +00: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 17:27:11 +00:00
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
2022-01-10 15:59:31 +00:00
// 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range.
2019-04-03 14:23:50 +00:00
// 2019-04-03: Misc: Renamed imgui_impl_freeglut.cpp/.h to imgui_impl_glut.cpp/.h.
2019-03-26 11:14:17 +00:00
// 2019-03-25: Misc: Made io.DeltaTime always above zero.
2018-11-30 17:18:15 +00:00
// 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
2019-04-03 13:53:11 +00:00
// 2018-03-22: Added GLUT Platform binding.
2018-06-11 17:27:11 +00:00
# include "imgui.h"
2019-04-03 13:53:11 +00:00
# include "imgui_impl_glut.h"
2019-04-02 11:47:14 +00:00
# ifdef __APPLE__
2020-06-30 13:31:54 +00:00
# include <GLUT/glut.h>
2019-04-02 11:47:14 +00:00
# else
2020-06-30 13:31:54 +00:00
# include <GL/freeglut.h>
2019-04-02 11:47:14 +00:00
# endif
2018-06-11 17:27:11 +00: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
2021-12-12 11:02:20 +00:00
// Glut has 1 function for characters and one for "special keys". We map the characters in the 0..255 range and the keys above.
static ImGuiKey ImGui_ImplGLUT_KeyToImGuiKey ( int key )
{
switch ( key )
{
case ' \t ' : return ImGuiKey_Tab ;
case 256 + GLUT_KEY_LEFT : return ImGuiKey_LeftArrow ;
case 256 + GLUT_KEY_RIGHT : return ImGuiKey_RightArrow ;
case 256 + GLUT_KEY_UP : return ImGuiKey_UpArrow ;
case 256 + GLUT_KEY_DOWN : return ImGuiKey_DownArrow ;
case 256 + GLUT_KEY_PAGE_UP : return ImGuiKey_PageUp ;
case 256 + GLUT_KEY_PAGE_DOWN : return ImGuiKey_PageDown ;
case 256 + GLUT_KEY_HOME : return ImGuiKey_Home ;
case 256 + GLUT_KEY_END : return ImGuiKey_End ;
case 256 + GLUT_KEY_INSERT : return ImGuiKey_Insert ;
case 127 : return ImGuiKey_Delete ;
case 8 : return ImGuiKey_Backspace ;
case ' ' : return ImGuiKey_Space ;
case 13 : return ImGuiKey_Enter ;
case 27 : return ImGuiKey_Escape ;
case 39 : return ImGuiKey_Apostrophe ;
case 44 : return ImGuiKey_Comma ;
case 45 : return ImGuiKey_Minus ;
case 46 : return ImGuiKey_Period ;
case 47 : return ImGuiKey_Slash ;
case 59 : return ImGuiKey_Semicolon ;
case 61 : return ImGuiKey_Equal ;
case 91 : return ImGuiKey_LeftBracket ;
case 92 : return ImGuiKey_Backslash ;
case 93 : return ImGuiKey_RightBracket ;
case 96 : return ImGuiKey_GraveAccent ;
//case 0: return ImGuiKey_CapsLock;
//case 0: return ImGuiKey_ScrollLock;
case 256 + 0x006D : return ImGuiKey_NumLock ;
//case 0: return ImGuiKey_PrintScreen;
//case 0: return ImGuiKey_Pause;
//case '0': return ImGuiKey_Keypad0;
//case '1': return ImGuiKey_Keypad1;
//case '2': return ImGuiKey_Keypad2;
//case '3': return ImGuiKey_Keypad3;
//case '4': return ImGuiKey_Keypad4;
//case '5': return ImGuiKey_Keypad5;
//case '6': return ImGuiKey_Keypad6;
//case '7': return ImGuiKey_Keypad7;
//case '8': return ImGuiKey_Keypad8;
//case '9': return ImGuiKey_Keypad9;
//case 46: return ImGuiKey_KeypadDecimal;
//case 47: return ImGuiKey_KeypadDivide;
case 42 : return ImGuiKey_KeypadMultiply ;
//case 45: return ImGuiKey_KeypadSubtract;
case 43 : return ImGuiKey_KeypadAdd ;
//case 13: return ImGuiKey_KeypadEnter;
//case 0: return ImGuiKey_KeypadEqual;
2022-01-17 14:41:04 +00:00
case 256 + 0x0072 : return ImGuiKey_LeftCtrl ;
2021-12-12 11:02:20 +00:00
case 256 + 0x0070 : return ImGuiKey_LeftShift ;
case 256 + 0x0074 : return ImGuiKey_LeftAlt ;
//case 0: return ImGuiKey_LeftSuper;
2022-01-17 14:41:04 +00:00
case 256 + 0x0073 : return ImGuiKey_RightCtrl ;
2021-12-12 11:02:20 +00:00
case 256 + 0x0071 : return ImGuiKey_RightShift ;
case 256 + 0x0075 : return ImGuiKey_RightAlt ;
//case 0: return ImGuiKey_RightSuper;
//case 0: return ImGuiKey_Menu;
case ' 0 ' : return ImGuiKey_0 ;
case ' 1 ' : return ImGuiKey_1 ;
case ' 2 ' : return ImGuiKey_2 ;
case ' 3 ' : return ImGuiKey_3 ;
case ' 4 ' : return ImGuiKey_4 ;
case ' 5 ' : return ImGuiKey_5 ;
case ' 6 ' : return ImGuiKey_6 ;
case ' 7 ' : return ImGuiKey_7 ;
case ' 8 ' : return ImGuiKey_8 ;
case ' 9 ' : return ImGuiKey_9 ;
case ' A ' : case ' a ' : return ImGuiKey_A ;
case ' B ' : case ' b ' : return ImGuiKey_B ;
case ' C ' : case ' c ' : return ImGuiKey_C ;
case ' D ' : case ' d ' : return ImGuiKey_D ;
case ' E ' : case ' e ' : return ImGuiKey_E ;
case ' F ' : case ' f ' : return ImGuiKey_F ;
case ' G ' : case ' g ' : return ImGuiKey_G ;
case ' H ' : case ' h ' : return ImGuiKey_H ;
case ' I ' : case ' i ' : return ImGuiKey_I ;
case ' J ' : case ' j ' : return ImGuiKey_J ;
case ' K ' : case ' k ' : return ImGuiKey_K ;
case ' L ' : case ' l ' : return ImGuiKey_L ;
case ' M ' : case ' m ' : return ImGuiKey_M ;
case ' N ' : case ' n ' : return ImGuiKey_N ;
case ' O ' : case ' o ' : return ImGuiKey_O ;
case ' P ' : case ' p ' : return ImGuiKey_P ;
case ' Q ' : case ' q ' : return ImGuiKey_Q ;
case ' R ' : case ' r ' : return ImGuiKey_R ;
case ' S ' : case ' s ' : return ImGuiKey_S ;
case ' T ' : case ' t ' : return ImGuiKey_T ;
case ' U ' : case ' u ' : return ImGuiKey_U ;
case ' V ' : case ' v ' : return ImGuiKey_V ;
case ' W ' : case ' w ' : return ImGuiKey_W ;
case ' X ' : case ' x ' : return ImGuiKey_X ;
case ' Y ' : case ' y ' : return ImGuiKey_Y ;
case ' Z ' : case ' z ' : return ImGuiKey_Z ;
case 256 + GLUT_KEY_F1 : return ImGuiKey_F1 ;
case 256 + GLUT_KEY_F2 : return ImGuiKey_F2 ;
case 256 + GLUT_KEY_F3 : return ImGuiKey_F3 ;
case 256 + GLUT_KEY_F4 : return ImGuiKey_F4 ;
case 256 + GLUT_KEY_F5 : return ImGuiKey_F5 ;
case 256 + GLUT_KEY_F6 : return ImGuiKey_F6 ;
case 256 + GLUT_KEY_F7 : return ImGuiKey_F7 ;
case 256 + GLUT_KEY_F8 : return ImGuiKey_F8 ;
case 256 + GLUT_KEY_F9 : return ImGuiKey_F9 ;
case 256 + GLUT_KEY_F10 : return ImGuiKey_F10 ;
case 256 + GLUT_KEY_F11 : return ImGuiKey_F11 ;
case 256 + GLUT_KEY_F12 : return ImGuiKey_F12 ;
default : return ImGuiKey_None ;
}
}
2019-04-03 13:53:11 +00:00
bool ImGui_ImplGLUT_Init ( )
2018-06-11 17:27:11 +00:00
{
ImGuiIO & io = ImGui : : GetIO ( ) ;
2020-01-27 11:11:29 +00:00
# ifdef FREEGLUT
2020-06-30 13:31:54 +00:00
io . BackendPlatformName = " imgui_impl_glut (freeglut) " ;
2020-01-27 11:11:29 +00:00
# else
2020-06-30 13:31:54 +00:00
io . BackendPlatformName = " imgui_impl_glut " ;
2020-01-27 11:11:29 +00:00
# endif
2018-06-11 17:27:11 +00:00
g_Time = 0 ;
return true ;
}
2019-04-03 13:53:11 +00:00
void ImGui_ImplGLUT_InstallFuncs ( )
2018-06-11 17:27:11 +00:00
{
2019-04-03 13:53:11 +00:00
glutReshapeFunc ( ImGui_ImplGLUT_ReshapeFunc ) ;
glutMotionFunc ( ImGui_ImplGLUT_MotionFunc ) ;
glutPassiveMotionFunc ( ImGui_ImplGLUT_MotionFunc ) ;
glutMouseFunc ( ImGui_ImplGLUT_MouseFunc ) ;
2019-04-02 15:07:28 +00:00
# ifdef __FREEGLUT_EXT_H__
2019-04-03 13:53:11 +00:00
glutMouseWheelFunc ( ImGui_ImplGLUT_MouseWheelFunc ) ;
2019-04-02 11:47:14 +00:00
# endif
2019-04-03 13:53:11 +00:00
glutKeyboardFunc ( ImGui_ImplGLUT_KeyboardFunc ) ;
glutKeyboardUpFunc ( ImGui_ImplGLUT_KeyboardUpFunc ) ;
glutSpecialFunc ( ImGui_ImplGLUT_SpecialFunc ) ;
glutSpecialUpFunc ( ImGui_ImplGLUT_SpecialUpFunc ) ;
2018-06-11 17:27:11 +00:00
}
2019-04-03 13:53:11 +00:00
void ImGui_ImplGLUT_Shutdown ( )
2018-06-11 17:27:11 +00:00
{
}
2019-04-03 13:53:11 +00:00
void ImGui_ImplGLUT_NewFrame ( )
2018-06-11 17:27:11 +00:00
{
// Setup time step
ImGuiIO & io = ImGui : : GetIO ( ) ;
int current_time = glutGet ( GLUT_ELAPSED_TIME ) ;
2019-03-26 11:14:17 +00: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 17:27:11 +00:00
g_Time = current_time ;
// Start the frame
ImGui : : NewFrame ( ) ;
}
2022-01-17 13:57:55 +00:00
static void ImGui_ImplGLUT_UpdateKeyModifiers ( )
2018-06-11 17:27:11 +00:00
{
ImGuiIO & io = ImGui : : GetIO ( ) ;
2022-01-10 11:39:01 +00:00
int glut_key_mods = glutGetModifiers ( ) ;
ImGuiKeyModFlags key_mods =
( ( glut_key_mods & GLUT_ACTIVE_CTRL ) ? ImGuiKeyModFlags_Ctrl : 0 ) |
( ( glut_key_mods & GLUT_ACTIVE_SHIFT ) ? ImGuiKeyModFlags_Shift : 0 ) |
( ( glut_key_mods & GLUT_ACTIVE_ALT ) ? ImGuiKeyModFlags_Alt : 0 ) ;
2022-01-10 15:59:31 +00:00
io . AddKeyModsEvent ( key_mods ) ;
2021-12-12 11:02:20 +00:00
}
static void ImGui_ImplGLUT_AddKeyEvent ( ImGuiKey key , bool down , int native_keycode )
{
ImGuiIO & io = ImGui : : GetIO ( ) ;
io . AddKeyEvent ( key , down ) ;
io . SetKeyEventNativeData ( key , native_keycode , - 1 ) ; // To support legacy indexing (<1.87 user code)
2018-06-11 17:27:11 +00:00
}
2019-04-03 13:53:11 +00:00
void ImGui_ImplGLUT_KeyboardFunc ( unsigned char c , int x , int y )
2018-06-11 17:27:11 +00:00
{
// Send character to imgui
//printf("char_down_func %d '%c'\n", c, c);
ImGuiIO & io = ImGui : : GetIO ( ) ;
if ( c > = 32 )
2019-05-11 08:33:56 +00:00
io . AddInputCharacter ( ( unsigned int ) c ) ;
2018-06-11 17:27:11 +00:00
2021-12-12 11:02:20 +00:00
ImGuiKey key = ImGui_ImplGLUT_KeyToImGuiKey ( c ) ;
ImGui_ImplGLUT_AddKeyEvent ( key , true , c ) ;
2022-01-17 13:57:55 +00:00
ImGui_ImplGLUT_UpdateKeyModifiers ( ) ;
2018-06-11 17:27:11 +00:00
( void ) x ; ( void ) y ; // Unused
}
2019-04-03 13:53:11 +00:00
void ImGui_ImplGLUT_KeyboardUpFunc ( unsigned char c , int x , int y )
2018-06-11 17:27:11 +00:00
{
//printf("char_up_func %d '%c'\n", c, c);
2021-12-12 11:02:20 +00:00
ImGuiKey key = ImGui_ImplGLUT_KeyToImGuiKey ( c ) ;
ImGui_ImplGLUT_AddKeyEvent ( key , false , c ) ;
2022-01-17 13:57:55 +00:00
ImGui_ImplGLUT_UpdateKeyModifiers ( ) ;
2018-06-11 17:27:11 +00:00
( void ) x ; ( void ) y ; // Unused
}
2019-04-03 13:53:11 +00:00
void ImGui_ImplGLUT_SpecialFunc ( int key , int x , int y )
2018-06-11 17:27:11 +00:00
{
//printf("key_down_func %d\n", key);
2021-12-12 11:02:20 +00:00
ImGuiKey imgui_key = ImGui_ImplGLUT_KeyToImGuiKey ( key + 256 ) ;
ImGui_ImplGLUT_AddKeyEvent ( imgui_key , true , key + 256 ) ;
2022-01-17 13:57:55 +00:00
ImGui_ImplGLUT_UpdateKeyModifiers ( ) ;
2018-06-11 17:27:11 +00:00
( void ) x ; ( void ) y ; // Unused
}
2019-04-03 13:53:11 +00:00
void ImGui_ImplGLUT_SpecialUpFunc ( int key , int x , int y )
2018-06-11 17:27:11 +00:00
{
//printf("key_up_func %d\n", key);
2021-12-12 11:02:20 +00:00
ImGuiKey imgui_key = ImGui_ImplGLUT_KeyToImGuiKey ( key + 256 ) ;
ImGui_ImplGLUT_AddKeyEvent ( imgui_key , false , key + 256 ) ;
2022-01-17 13:57:55 +00:00
ImGui_ImplGLUT_UpdateKeyModifiers ( ) ;
2018-06-11 17:27:11 +00:00
( void ) x ; ( void ) y ; // Unused
}
2019-04-03 13:53:11 +00:00
void ImGui_ImplGLUT_MouseFunc ( int glut_button , int state , int x , int y )
2018-06-11 17:27:11 +00: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 15:07:28 +00:00
# ifdef __FREEGLUT_EXT_H__
2019-04-03 13:53:11 +00:00
void ImGui_ImplGLUT_MouseWheelFunc ( int button , int dir , int x , int y )
2018-06-11 17:27:11 +00: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 15:07:28 +00:00
# endif
2018-06-11 17:27:11 +00:00
2019-04-03 13:53:11 +00:00
void ImGui_ImplGLUT_ReshapeFunc ( int w , int h )
2018-06-11 17:27:11 +00:00
{
ImGuiIO & io = ImGui : : GetIO ( ) ;
io . DisplaySize = ImVec2 ( ( float ) w , ( float ) h ) ;
}
2019-04-03 13:53:11 +00:00
void ImGui_ImplGLUT_MotionFunc ( int x , int y )
2018-06-11 17:27:11 +00:00
{
ImGuiIO & io = ImGui : : GetIO ( ) ;
io . MousePos = ImVec2 ( ( float ) x , ( float ) y ) ;
}