diff --git a/src/oscar/Graphics/GraphicsContext.h b/src/oscar/Graphics/GraphicsContext.h index 7c1b1c359..f420f7cf5 100644 --- a/src/oscar/Graphics/GraphicsContext.h +++ b/src/oscar/Graphics/GraphicsContext.h @@ -33,9 +33,6 @@ namespace osc void clear_screen(const Color&); - // HACK: this is needed by ImGui's SDL backend, because it uses OpenGL "in the raw" - void* upd_raw_opengl_context_handle_HACK(); - // returns a future that asynchronously yields a complete screenshot of the next complete frame std::future request_screenshot(); diff --git a/src/oscar/Graphics/GraphicsImplementation.cpp b/src/oscar/Graphics/GraphicsImplementation.cpp index c93e89173..f728388bd 100644 --- a/src/oscar/Graphics/GraphicsImplementation.cpp +++ b/src/oscar/Graphics/GraphicsImplementation.cpp @@ -6170,11 +6170,6 @@ class osc::GraphicsContext::Impl final { gl::clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } - void* upd_raw_opengl_context_handle_HACK() - { - return opengl_context_.get(); - } - std::future request_screenshot() { return screenshot_request_queue_.emplace_back().get_future(); @@ -6333,11 +6328,6 @@ void osc::GraphicsContext::clear_screen(const Color& color) g_graphics_context_impl->clear_screen(color); } -void* osc::GraphicsContext::upd_raw_opengl_context_handle_HACK() -{ - return g_graphics_context_impl->upd_raw_opengl_context_handle_HACK(); -} - void osc::GraphicsContext::swap_buffers(SDL_Window& window) { g_graphics_context_impl->swap_buffers(window); diff --git a/src/oscar/Platform/App.cpp b/src/oscar/Platform/App.cpp index 100aafe07..3d4fc2953 100644 --- a/src/oscar/Platform/App.cpp +++ b/src/oscar/Platform/App.cpp @@ -617,11 +617,6 @@ class osc::App::Impl final { GraphicsContext& upd_graphics_context() { return graphics_context_; } - void* upd_raw_opengl_context_handle_HACK() - { - return graphics_context_.upd_raw_opengl_context_handle_HACK(); - } - private: bool is_window_focused() const { @@ -1094,8 +1089,3 @@ SDL_Window* osc::App::upd_underlying_window() { return impl_->upd_window().get(); } - -void* osc::App::upd_underlying_opengl_context() -{ - return impl_->upd_graphics_context().upd_raw_opengl_context_handle_HACK(); -} diff --git a/src/oscar/Platform/App.h b/src/oscar/Platform/App.h index 7402de141..3fb578d2a 100644 --- a/src/oscar/Platform/App.h +++ b/src/oscar/Platform/App.h @@ -281,10 +281,9 @@ namespace osc // try and retrieve a singleton that has the same lifetime as the app std::shared_ptr upd_singleton(const std::type_info&, const std::function()>&); - // HACK: the 2D ui currently needs access to these + // HACK: the 2D ui currently needs to access this friend void ui::context::init(); SDL_Window* upd_underlying_window(); - void* upd_underlying_opengl_context(); class Impl; std::unique_ptr impl_; diff --git a/src/oscar/UI/imgui_impl_sdl2.cpp b/src/oscar/UI/imgui_impl_sdl2.cpp index 0fcc5f16f..101d57a31 100644 --- a/src/oscar/UI/imgui_impl_sdl2.cpp +++ b/src/oscar/UI/imgui_impl_sdl2.cpp @@ -1,92 +1,10 @@ -// dear imgui: Platform Backend for SDL2 -// This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..) -// (Info: SDL2 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.) -// (Prefer SDL 2.0.5+ for full feature support.) - -// Implemented features: -// [X] Platform: Clipboard support. -// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen. -// [X] Platform: 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 SDL_SCANCODE_* values will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set] -// [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. -// [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. -// [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'. -// Missing features: -// [ ] Platform: Multi-viewport + Minimized windows seems to break mouse wheel events (at least under Windows). -// [x] Platform: Basic IME support. App needs to call 'SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");' before SDL_CreateWindow()!. +#include "imgui_impl_sdl2.h" -// 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. -// Learn about Dear ImGui: -// - FAQ https://dearimgui.com/faq -// - Getting Started https://dearimgui.com/getting-started -// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). -// - Introduction, links and more at the top of imgui.cpp +#include -// CHANGELOG -// (minor and older changes stripped away, please see git history for details) -// 2024-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface. -// 2024-02-14: Inputs: Handle gamepad disconnection. Added ImGui_ImplSDL2_SetGamepadMode(). -// 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys. -// 2023-04-06: Inputs: Avoid calling SDL_StartTextInput()/SDL_StopTextInput() as they don't only pertain to IME. It's unclear exactly what their relation is to IME. (#6306) -// 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen. (#2702) -// 2023-02-23: Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. (#6189, #6114, #3644) -// 2023-02-07: Implement IME handler (io.SetPlatformImeDataFn will call SDL_SetTextInputRect()/SDL_StartTextInput()). -// 2023-02-07: *BREAKING CHANGE* Renamed this backend file from imgui_impl_sdl.cpp/.h to imgui_impl_sdl2.cpp/.h in prevision for the future release of SDL3. -// 2023-02-02: Avoid calling SDL_SetCursor() when cursor has not changed, as the function is surprisingly costly on Mac with latest SDL (may be fixed in next SDL version). -// 2023-02-02: Added support for SDL 2.0.18+ preciseX/preciseY mouse wheel data for smooth scrolling + Scaling X value on Emscripten (bug?). (#4019, #6096) -// 2023-02-02: Removed SDL_MOUSEWHEEL value clamping, as values seem correct in latest Emscripten. (#4019) -// 2023-02-01: Flipping SDL_MOUSEWHEEL 'wheel.x' value to match other backends and offer consistent horizontal scrolling direction. (#4019, #6096, #1463) -// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11. -// 2022-09-26: Inputs: Disable SDL 2.0.22 new "auto capture" (SDL_HINT_MOUSE_AUTO_CAPTURE) which prevents drag and drop across windows for multi-viewport support + don't capture when drag and dropping. (#5710) -// 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported). -// 2022-03-22: Inputs: Fix mouse position issues when dragging outside of boundaries. SDL_CaptureMouse() erroneously still gives out LEAVE events when hovering OS decorations. -// 2022-03-22: Inputs: Added support for extra mouse buttons (SDL_BUTTON_X1/SDL_BUTTON_X2). -// 2022-02-04: Added SDL_Renderer* parameter to ImGui_ImplSDL2_InitForSDLRenderer(), so we can use SDL_GetRendererOutputSize() instead of SDL_GL_GetDrawableSize() when bound to a SDL_Renderer. -// 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion. -// 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[]. -// 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+). -// 2022-01-17: Inputs: always update key mods next and before key event (not in NewFrame) to fix input queue with very low framerates. -// 2022-01-12: Update mouse inputs using SDL_MOUSEMOTION/SDL_WINDOWEVENT_LEAVE + fallback to provide it when focused but not hovered/captured. More standard and will allow us to pass it to future input queue API. -// 2022-01-12: Maintain our own copy of MouseButtonsDown mask instead of using ImGui::IsAnyMouseDown() which will be obsoleted. -// 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range. -// 2021-08-17: Calling io.AddFocusEvent() on SDL_WINDOWEVENT_FOCUS_GAINED/SDL_WINDOWEVENT_FOCUS_LOST. -// 2021-07-29: Inputs: MousePos is correctly reported when the host platform window is hovered but not focused (using SDL_GetMouseFocus() + SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, requires SDL 2.0.5+) -// 2021-06:29: *BREAKING CHANGE* Removed 'SDL_Window* window' parameter to ImGui_ImplSDL2_NewFrame() which was unnecessary. -// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX). -// 2021-03-22: Rework global mouse pos availability check listing supported platforms explicitly, effectively fixing mouse access on Raspberry Pi. (#2837, #3950) -// 2020-05-25: Misc: Report a zero display-size when window is minimized, to be consistent with other backends. -// 2020-02-20: Inputs: Fixed mapping for ImGuiKey_KeyPadEnter (using SDL_SCANCODE_KP_ENTER instead of SDL_SCANCODE_RETURN2). -// 2019-12-17: Inputs: On Wayland, use SDL_GetMouseState (because there is no global mouse state). -// 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor. -// 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter. -// 2019-04-23: Inputs: Added support for SDL_GameController (if ImGuiConfigFlags_NavEnableGamepad is set by user application). -// 2019-03-12: Misc: Preserve DisplayFramebufferScale when main window is minimized. -// 2018-12-21: Inputs: Workaround for Android/iOS which don't seem to handle focus related calls. -// 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window. -// 2018-11-14: Changed the signature of ImGui_ImplSDL2_ProcessEvent() to take a 'const SDL_Event*'. -// 2018-08-01: Inputs: Workaround for Emscripten which doesn't seem to handle focus related calls. -// 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor. -// 2018-06-08: Misc: Extracted imgui_impl_sdl.cpp/.h away from the old combined SDL2+OpenGL/Vulkan examples. -// 2018-06-08: Misc: ImGui_ImplSDL2_InitForOpenGL() now takes a SDL_GLContext parameter. -// 2018-05-09: Misc: Fixed clipboard paste memory leak (we didn't call SDL_FreeMemory on the data returned by SDL_GetClipboardText). -// 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag. -// 2018-02-16: Inputs: Added support for mouse cursors, honoring ImGui::GetMouseCursor() value. -// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves. -// 2018-02-06: Inputs: Added mapping for ImGuiKey_Space. -// 2018-02-05: Misc: Using SDL_GetPerformanceCounter() instead of SDL_GetTicks() to be able to handle very high framerate (1000+ FPS). -// 2018-02-05: Inputs: Keyboard mapping is using scancodes everywhere instead of a confusing mixture of keycodes and scancodes. -// 2018-01-20: Inputs: Added Horizontal Mouse Wheel support. -// 2018-01-19: Inputs: When available (SDL 2.0.4+) using SDL_CaptureMouse() to retrieve coordinates outside of client area when dragging. Otherwise (SDL 2.0.3 and before) testing for SDL_WINDOW_INPUT_FOCUS instead of SDL_WINDOW_MOUSE_FOCUS. -// 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert. -// 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1). -// 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers. +// this was refactored from https://github.com/ocornut/imgui/blob/v1.91.1-docking/backends/imgui_impl_sdl2.cpp // NOLINTBEGIN - -#include "imgui.h" -#ifndef IMGUI_DISABLE -#include "imgui_impl_sdl2.h" - // Clang warnings with -Weverything #if defined(__clang__) #pragma clang diagnostic push @@ -109,15 +27,7 @@ #else #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0 #endif -#define SDL_HAS_WINDOW_ALPHA SDL_VERSION_ATLEAST(2,0,5) -#define SDL_HAS_ALWAYS_ON_TOP SDL_VERSION_ATLEAST(2,0,5) -#define SDL_HAS_USABLE_DISPLAY_BOUNDS SDL_VERSION_ATLEAST(2,0,5) -#define SDL_HAS_PER_MONITOR_DPI SDL_VERSION_ATLEAST(2,0,4) -#define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6) #define SDL_HAS_DISPLAY_EVENT SDL_VERSION_ATLEAST(2,0,9) -#if !SDL_HAS_VULKAN -static const Uint32 SDL_WINDOW_VULKAN = 0x10000000; -#endif // SDL Data struct ImGui_ImplSDL2_Data @@ -138,11 +48,6 @@ struct ImGui_ImplSDL2_Data bool MouseCanUseGlobalState; bool MouseCanReportHoveredViewport; // This is hard to use/unreliable on SDL so we'll set ImGuiBackendFlags_HasMouseHoveredViewport dynamically based on state. - // Gamepad handling - ImVector Gamepads; - ImGui_ImplSDL2_GamepadMode GamepadMode; - bool WantUpdateGamepadsList; - ImGui_ImplSDL2_Data() { memset((void*)this, 0, sizeof(*this)); } }; @@ -157,8 +62,6 @@ static ImGui_ImplSDL2_Data* ImGui_ImplSDL2_GetBackendData() // Forward Declarations static void ImGui_ImplSDL2_UpdateMonitors(); -static void ImGui_ImplSDL2_InitPlatformInterface(SDL_Window* window, void* sdl_gl_context); -static void ImGui_ImplSDL2_ShutdownPlatformInterface(); // Functions static const char* ImGui_ImplSDL2_GetClipboardText(void*) @@ -439,12 +342,6 @@ bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event) } return true; } - case SDL_CONTROLLERDEVICEADDED: - case SDL_CONTROLLERDEVICEREMOVED: - { - bd->WantUpdateGamepadsList = true; - return true; - } } return false; } @@ -453,7 +350,7 @@ bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event) EM_JS(void, ImGui_ImplSDL2_EmscriptenOpenURL, (char const* url), { url = url ? UTF8ToString(url) : null; if (url) window.open(url, '_blank'); }); #endif -static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer, void* sdl_gl_context) +bool ImGui_ImplSDL2_Init(SDL_Window* window) { ImGuiIO& io = ImGui::GetIO(); IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!"); @@ -479,7 +376,7 @@ static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer, void io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional) bd->Window = window; - bd->Renderer = renderer; + bd->Renderer = nullptr; // SDL on Linux/OSX doesn't report events for unfocused windows (see https://github.com/ocornut/imgui/issues/4960) // We will use 'MouseCanReportHoveredViewport' to set 'ImGuiBackendFlags_HasMouseHoveredViewport' dynamically each frame. @@ -499,10 +396,6 @@ static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer, void io.PlatformOpenInShellFn = [](ImGuiContext*, const char* url) { ImGui_ImplSDL2_EmscriptenOpenURL(url); return true; }; #endif - // Gamepad handling - bd->GamepadMode = ImGui_ImplSDL2_GamepadMode_AutoFirst; - bd->WantUpdateGamepadsList = true; - // Load mouse cursors bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW); bd->MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM); @@ -551,69 +444,19 @@ static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer, void SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0"); #endif - // We need SDL_CaptureMouse(), SDL_GetGlobalMouseState() from SDL 2.0.4+ to support multiple viewports. - // We left the call to ImGui_ImplSDL2_InitPlatformInterface() outside of #ifdef to avoid unused-function warnings. - if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) && (io.BackendFlags & ImGuiBackendFlags_PlatformHasViewports)) - ImGui_ImplSDL2_InitPlatformInterface(window, sdl_gl_context); - return true; } -bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context) -{ - return ImGui_ImplSDL2_Init(window, nullptr, sdl_gl_context); -} - -bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window) -{ -#if !SDL_HAS_VULKAN - IM_ASSERT(0 && "Unsupported"); -#endif - if (!ImGui_ImplSDL2_Init(window, nullptr, nullptr)) - return false; - ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); - bd->UseVulkan = true; - return true; -} - -bool ImGui_ImplSDL2_InitForD3D(SDL_Window* window) -{ -#if !defined(_WIN32) - IM_ASSERT(0 && "Unsupported"); -#endif - return ImGui_ImplSDL2_Init(window, nullptr, nullptr); -} - -bool ImGui_ImplSDL2_InitForMetal(SDL_Window* window) -{ - return ImGui_ImplSDL2_Init(window, nullptr, nullptr); -} - -bool ImGui_ImplSDL2_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer) -{ - return ImGui_ImplSDL2_Init(window, renderer, nullptr); -} - -bool ImGui_ImplSDL2_InitForOther(SDL_Window* window) -{ - return ImGui_ImplSDL2_Init(window, nullptr, nullptr); -} - -static void ImGui_ImplSDL2_CloseGamepads(); - void ImGui_ImplSDL2_Shutdown() { ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?"); ImGuiIO& io = ImGui::GetIO(); - ImGui_ImplSDL2_ShutdownPlatformInterface(); - if (bd->ClipboardTextData) SDL_free(bd->ClipboardTextData); for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++) SDL_FreeCursor(bd->MouseCursors[cursor_n]); - ImGui_ImplSDL2_CloseGamepads(); io.BackendPlatformName = nullptr; io.BackendPlatformUserData = nullptr; @@ -711,111 +554,6 @@ static void ImGui_ImplSDL2_UpdateMouseCursor() } } -static void ImGui_ImplSDL2_CloseGamepads() -{ - ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); - if (bd->GamepadMode != ImGui_ImplSDL2_GamepadMode_Manual) - for (SDL_GameController* gamepad : bd->Gamepads) - SDL_GameControllerClose(gamepad); - bd->Gamepads.resize(0); -} - -void ImGui_ImplSDL2_SetGamepadMode(ImGui_ImplSDL2_GamepadMode mode, struct _SDL_GameController** manual_gamepads_array, int manual_gamepads_count) -{ - ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); - ImGui_ImplSDL2_CloseGamepads(); - if (mode == ImGui_ImplSDL2_GamepadMode_Manual) - { - IM_ASSERT(manual_gamepads_array != nullptr && manual_gamepads_count > 0); - for (int n = 0; n < manual_gamepads_count; n++) - bd->Gamepads.push_back(manual_gamepads_array[n]); - } - else - { - IM_ASSERT(manual_gamepads_array == nullptr && manual_gamepads_count <= 0); - bd->WantUpdateGamepadsList = true; - } - bd->GamepadMode = mode; -} - -static void ImGui_ImplSDL2_UpdateGamepadButton(ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerButton button_no) -{ - bool merged_value = false; - for (SDL_GameController* gamepad : bd->Gamepads) - merged_value |= SDL_GameControllerGetButton(gamepad, button_no) != 0; - io.AddKeyEvent(key, merged_value); -} - -static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; } -static void ImGui_ImplSDL2_UpdateGamepadAnalog(ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerAxis axis_no, float v0, float v1) -{ - float merged_value = 0.0f; - for (SDL_GameController* gamepad : bd->Gamepads) - { - float vn = Saturate((float)(SDL_GameControllerGetAxis(gamepad, axis_no) - v0) / (float)(v1 - v0)); - if (merged_value < vn) - merged_value = vn; - } - io.AddKeyAnalogEvent(key, merged_value > 0.1f, merged_value); -} - -static void ImGui_ImplSDL2_UpdateGamepads() -{ - ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); - ImGuiIO& io = ImGui::GetIO(); - - // Update list of controller(s) to use - if (bd->WantUpdateGamepadsList && bd->GamepadMode != ImGui_ImplSDL2_GamepadMode_Manual) - { - ImGui_ImplSDL2_CloseGamepads(); - int joystick_count = SDL_NumJoysticks(); - for (int n = 0; n < joystick_count; n++) - if (SDL_IsGameController(n)) - if (SDL_GameController* gamepad = SDL_GameControllerOpen(n)) - { - bd->Gamepads.push_back(gamepad); - if (bd->GamepadMode == ImGui_ImplSDL2_GamepadMode_AutoFirst) - break; - } - bd->WantUpdateGamepadsList = false; - } - - // FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs. - if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0) - return; - io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; - if (bd->Gamepads.Size == 0) - return; - io.BackendFlags |= ImGuiBackendFlags_HasGamepad; - - // Update gamepad inputs - const int thumb_dead_zone = 8000; // SDL_gamecontroller.h suggests using this value. - ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadStart, SDL_CONTROLLER_BUTTON_START); - ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadBack, SDL_CONTROLLER_BUTTON_BACK); - ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceLeft, SDL_CONTROLLER_BUTTON_X); // Xbox X, PS Square - ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceRight, SDL_CONTROLLER_BUTTON_B); // Xbox B, PS Circle - ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceUp, SDL_CONTROLLER_BUTTON_Y); // Xbox Y, PS Triangle - ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceDown, SDL_CONTROLLER_BUTTON_A); // Xbox A, PS Cross - ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT); - ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT); - ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadUp, SDL_CONTROLLER_BUTTON_DPAD_UP); - ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN); - ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL1, SDL_CONTROLLER_BUTTON_LEFTSHOULDER); - ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR1, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); - ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadL2, SDL_CONTROLLER_AXIS_TRIGGERLEFT, 0.0f, 32767); - ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadR2, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, 0.0f, 32767); - ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL3, SDL_CONTROLLER_BUTTON_LEFTSTICK); - ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR3, SDL_CONTROLLER_BUTTON_RIGHTSTICK); - ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickLeft, SDL_CONTROLLER_AXIS_LEFTX, -thumb_dead_zone, -32768); - ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickRight, SDL_CONTROLLER_AXIS_LEFTX, +thumb_dead_zone, +32767); - ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickUp, SDL_CONTROLLER_AXIS_LEFTY, -thumb_dead_zone, -32768); - ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickDown, SDL_CONTROLLER_AXIS_LEFTY, +thumb_dead_zone, +32767); - ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickLeft, SDL_CONTROLLER_AXIS_RIGHTX, -thumb_dead_zone, -32768); - ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickRight, SDL_CONTROLLER_AXIS_RIGHTX, +thumb_dead_zone, +32767); - ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickUp, SDL_CONTROLLER_AXIS_RIGHTY, -thumb_dead_zone, -32768); - ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickDown, SDL_CONTROLLER_AXIS_RIGHTY, +thumb_dead_zone, +32767); -} - // FIXME: Note that doesn't update with DPI/Scaling change only as SDL2 doesn't have an event for it (SDL3 has). static void ImGui_ImplSDL2_UpdateMonitors() { @@ -898,264 +636,10 @@ void ImGui_ImplSDL2_NewFrame() ImGui_ImplSDL2_UpdateMouseData(); ImGui_ImplSDL2_UpdateMouseCursor(); - - // Update game controllers (if enabled and available) - ImGui_ImplSDL2_UpdateGamepads(); -} - -//-------------------------------------------------------------------------------------------------------- -// MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT -// This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously. -// If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first.. -//-------------------------------------------------------------------------------------------------------- - -// Helper structure we store in the void* RendererUserData field of each ImGuiViewport to easily retrieve our backend data. -struct ImGui_ImplSDL2_ViewportData -{ - SDL_Window* Window; - Uint32 WindowID; - bool WindowOwned; - SDL_GLContext GLContext; - - ImGui_ImplSDL2_ViewportData() { Window = nullptr; WindowID = 0; WindowOwned = false; GLContext = nullptr; } - ~ImGui_ImplSDL2_ViewportData() { IM_ASSERT(Window == nullptr && GLContext == nullptr); } -}; - -static void ImGui_ImplSDL2_CreateWindow(ImGuiViewport* viewport) -{ - ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); - ImGui_ImplSDL2_ViewportData* vd = IM_NEW(ImGui_ImplSDL2_ViewportData)(); - viewport->PlatformUserData = vd; - - ImGuiViewport* main_viewport = ImGui::GetMainViewport(); - ImGui_ImplSDL2_ViewportData* main_viewport_data = (ImGui_ImplSDL2_ViewportData*)main_viewport->PlatformUserData; - - // Share GL resources with main context - bool use_opengl = (main_viewport_data->GLContext != nullptr); - SDL_GLContext backup_context = nullptr; - if (use_opengl) - { - backup_context = SDL_GL_GetCurrentContext(); - SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); - SDL_GL_MakeCurrent(main_viewport_data->Window, main_viewport_data->GLContext); - } - - Uint32 sdl_flags = 0; - sdl_flags |= use_opengl ? SDL_WINDOW_OPENGL : (bd->UseVulkan ? SDL_WINDOW_VULKAN : 0); - sdl_flags |= SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_ALLOW_HIGHDPI; - sdl_flags |= SDL_WINDOW_HIDDEN; - sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? SDL_WINDOW_BORDERLESS : 0; - sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? 0 : SDL_WINDOW_RESIZABLE; -#if !defined(_WIN32) - // See SDL hack in ImGui_ImplSDL2_ShowWindow(). - sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon) ? SDL_WINDOW_SKIP_TASKBAR : 0; -#endif -#if SDL_HAS_ALWAYS_ON_TOP - sdl_flags |= (viewport->Flags & ImGuiViewportFlags_TopMost) ? SDL_WINDOW_ALWAYS_ON_TOP : 0; -#endif - vd->Window = SDL_CreateWindow("No Title Yet", (int)viewport->Pos.x, (int)viewport->Pos.y, (int)viewport->Size.x, (int)viewport->Size.y, sdl_flags); - vd->WindowOwned = true; - if (use_opengl) - { - vd->GLContext = SDL_GL_CreateContext(vd->Window); - SDL_GL_SetSwapInterval(0); - } - if (use_opengl && backup_context) - SDL_GL_MakeCurrent(vd->Window, backup_context); - - viewport->PlatformHandle = (void*)vd->Window; - viewport->PlatformHandleRaw = nullptr; - SDL_SysWMinfo info; - SDL_VERSION(&info.version); - if (SDL_GetWindowWMInfo(vd->Window, &info)) - { -#if defined(SDL_VIDEO_DRIVER_WINDOWS) - viewport->PlatformHandleRaw = info.info.win.window; -#elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA) - viewport->PlatformHandleRaw = (void*)info.info.cocoa.window; -#endif - } -} - -static void ImGui_ImplSDL2_DestroyWindow(ImGuiViewport* viewport) -{ - if (ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData) - { - if (vd->GLContext && vd->WindowOwned) - SDL_GL_DeleteContext(vd->GLContext); - if (vd->Window && vd->WindowOwned) - SDL_DestroyWindow(vd->Window); - vd->GLContext = nullptr; - vd->Window = nullptr; - IM_DELETE(vd); - } - viewport->PlatformUserData = viewport->PlatformHandle = nullptr; -} - -static void ImGui_ImplSDL2_ShowWindow(ImGuiViewport* viewport) -{ - ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; -#if defined(_WIN32) - HWND hwnd = (HWND)viewport->PlatformHandleRaw; - - // SDL hack: Hide icon from task bar - // Note: SDL 2.0.6+ has a SDL_WINDOW_SKIP_TASKBAR flag which is supported under Windows but the way it create the window breaks our seamless transition. - if (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon) - { - LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE); - ex_style &= ~WS_EX_APPWINDOW; - ex_style |= WS_EX_TOOLWINDOW; - ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style); - } - - // SDL hack: SDL always activate/focus windows :/ - if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing) - { - ::ShowWindow(hwnd, SW_SHOWNA); - return; - } -#endif - - SDL_ShowWindow(vd->Window); -} - -static ImVec2 ImGui_ImplSDL2_GetWindowPos(ImGuiViewport* viewport) -{ - ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; - int x = 0, y = 0; - SDL_GetWindowPosition(vd->Window, &x, &y); - return ImVec2((float)x, (float)y); -} - -static void ImGui_ImplSDL2_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos) -{ - ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; - SDL_SetWindowPosition(vd->Window, (int)pos.x, (int)pos.y); -} - -static ImVec2 ImGui_ImplSDL2_GetWindowSize(ImGuiViewport* viewport) -{ - ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; - int w = 0, h = 0; - SDL_GetWindowSize(vd->Window, &w, &h); - return ImVec2((float)w, (float)h); -} - -static void ImGui_ImplSDL2_SetWindowSize(ImGuiViewport* viewport, ImVec2 size) -{ - ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; - SDL_SetWindowSize(vd->Window, (int)size.x, (int)size.y); } -static void ImGui_ImplSDL2_SetWindowTitle(ImGuiViewport* viewport, const char* title) -{ - ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; - SDL_SetWindowTitle(vd->Window, title); -} - -#if SDL_HAS_WINDOW_ALPHA -static void ImGui_ImplSDL2_SetWindowAlpha(ImGuiViewport* viewport, float alpha) -{ - ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; - SDL_SetWindowOpacity(vd->Window, alpha); -} -#endif - -static void ImGui_ImplSDL2_SetWindowFocus(ImGuiViewport* viewport) -{ - ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; - SDL_RaiseWindow(vd->Window); -} - -static bool ImGui_ImplSDL2_GetWindowFocus(ImGuiViewport* viewport) -{ - ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; - return (SDL_GetWindowFlags(vd->Window) & SDL_WINDOW_INPUT_FOCUS) != 0; -} - -static bool ImGui_ImplSDL2_GetWindowMinimized(ImGuiViewport* viewport) -{ - ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; - return (SDL_GetWindowFlags(vd->Window) & SDL_WINDOW_MINIMIZED) != 0; -} - -static void ImGui_ImplSDL2_RenderWindow(ImGuiViewport* viewport, void*) -{ - ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; - if (vd->GLContext) - SDL_GL_MakeCurrent(vd->Window, vd->GLContext); -} - -static void ImGui_ImplSDL2_SwapBuffers(ImGuiViewport* viewport, void*) -{ - ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; - if (vd->GLContext) - { - SDL_GL_MakeCurrent(vd->Window, vd->GLContext); - SDL_GL_SwapWindow(vd->Window); - } -} - -// Vulkan support (the Vulkan renderer needs to call a platform-side support function to create the surface) -// SDL is graceful enough to _not_ need so we can safely include this. -#if SDL_HAS_VULKAN -#include -static int ImGui_ImplSDL2_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance, const void* vk_allocator, ImU64* out_vk_surface) -{ - ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; - (void)vk_allocator; - SDL_bool ret = SDL_Vulkan_CreateSurface(vd->Window, (VkInstance)vk_instance, (VkSurfaceKHR*)out_vk_surface); - return ret ? 0 : 1; // ret ? VK_SUCCESS : VK_NOT_READY -} -#endif // SDL_HAS_VULKAN - -static void ImGui_ImplSDL2_InitPlatformInterface(SDL_Window* window, void* sdl_gl_context) -{ - // Register platform interface (will be coupled with a renderer interface) - ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); - platform_io.Platform_CreateWindow = ImGui_ImplSDL2_CreateWindow; - platform_io.Platform_DestroyWindow = ImGui_ImplSDL2_DestroyWindow; - platform_io.Platform_ShowWindow = ImGui_ImplSDL2_ShowWindow; - platform_io.Platform_SetWindowPos = ImGui_ImplSDL2_SetWindowPos; - platform_io.Platform_GetWindowPos = ImGui_ImplSDL2_GetWindowPos; - platform_io.Platform_SetWindowSize = ImGui_ImplSDL2_SetWindowSize; - platform_io.Platform_GetWindowSize = ImGui_ImplSDL2_GetWindowSize; - platform_io.Platform_SetWindowFocus = ImGui_ImplSDL2_SetWindowFocus; - platform_io.Platform_GetWindowFocus = ImGui_ImplSDL2_GetWindowFocus; - platform_io.Platform_GetWindowMinimized = ImGui_ImplSDL2_GetWindowMinimized; - platform_io.Platform_SetWindowTitle = ImGui_ImplSDL2_SetWindowTitle; - platform_io.Platform_RenderWindow = ImGui_ImplSDL2_RenderWindow; - platform_io.Platform_SwapBuffers = ImGui_ImplSDL2_SwapBuffers; -#if SDL_HAS_WINDOW_ALPHA - platform_io.Platform_SetWindowAlpha = ImGui_ImplSDL2_SetWindowAlpha; -#endif -#if SDL_HAS_VULKAN - platform_io.Platform_CreateVkSurface = ImGui_ImplSDL2_CreateVkSurface; -#endif - - // Register main window handle (which is owned by the main application, not by us) - // This is mostly for simplicity and consistency, so that our code (e.g. mouse handling etc.) can use same logic for main and secondary viewports. - ImGuiViewport* main_viewport = ImGui::GetMainViewport(); - ImGui_ImplSDL2_ViewportData* vd = IM_NEW(ImGui_ImplSDL2_ViewportData)(); - vd->Window = window; - vd->WindowID = SDL_GetWindowID(window); - vd->WindowOwned = false; - vd->GLContext = sdl_gl_context; - main_viewport->PlatformUserData = vd; - main_viewport->PlatformHandle = vd->Window; -} - -static void ImGui_ImplSDL2_ShutdownPlatformInterface() -{ - ImGui::DestroyPlatformWindows(); -} - -// NOLINTEND - -//----------------------------------------------------------------------------- - #if defined(__clang__) #pragma clang diagnostic pop #endif -#endif // #ifndef IMGUI_DISABLE +// NOLINTEND diff --git a/src/oscar/UI/imgui_impl_sdl2.h b/src/oscar/UI/imgui_impl_sdl2.h index 2970772ef..ac6a50ec8 100644 --- a/src/oscar/UI/imgui_impl_sdl2.h +++ b/src/oscar/UI/imgui_impl_sdl2.h @@ -1,48 +1,11 @@ -// dear imgui: Platform Backend for SDL2 -// This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..) -// (Info: SDL2 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.) - -// Implemented features: -// [X] Platform: Clipboard support. -// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen. -// [X] Platform: 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 SDL_SCANCODE_* values will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set] -// [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. -// [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. -// [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'. -// Missing features: -// [ ] Platform: Multi-viewport + Minimized windows seems to break mouse wheel events (at least under Windows). -// [x] Platform: Basic IME support. App needs to call 'SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");' before SDL_CreateWindow()!. - -// 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. -// Learn about Dear ImGui: -// - FAQ https://dearimgui.com/faq -// - Getting Started https://dearimgui.com/getting-started -// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). -// - Introduction, links and more at the top of imgui.cpp +// this was refactored from https://github.com/ocornut/imgui/blob/v1.91.1-docking/backends/imgui_impl_sdl2.h #pragma once -#include "imgui.h" // IMGUI_IMPL_API -#ifndef IMGUI_DISABLE struct SDL_Window; -struct SDL_Renderer; -struct _SDL_GameController; typedef union SDL_Event SDL_Event; -IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context); -IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window); -IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForD3D(SDL_Window* window); -IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForMetal(SDL_Window* window); -IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer); -IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForOther(SDL_Window* window); -IMGUI_IMPL_API void ImGui_ImplSDL2_Shutdown(); -IMGUI_IMPL_API void ImGui_ImplSDL2_NewFrame(); -IMGUI_IMPL_API bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event); - -// Gamepad selection automatically starts in AutoFirst mode, picking first available SDL_Gamepad. You may override this. -// When using manual mode, caller is responsible for opening/closing gamepad. -enum ImGui_ImplSDL2_GamepadMode { ImGui_ImplSDL2_GamepadMode_AutoFirst, ImGui_ImplSDL2_GamepadMode_AutoAll, ImGui_ImplSDL2_GamepadMode_Manual }; -IMGUI_IMPL_API void ImGui_ImplSDL2_SetGamepadMode(ImGui_ImplSDL2_GamepadMode mode, struct _SDL_GameController** manual_gamepads_array = NULL, int manual_gamepads_count = -1); - -#endif // #ifndef IMGUI_DISABLE +bool ImGui_ImplSDL2_Init(SDL_Window*); +void ImGui_ImplSDL2_Shutdown(); +void ImGui_ImplSDL2_NewFrame(); +bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event); diff --git a/src/oscar/UI/ui_context.cpp b/src/oscar/UI/ui_context.cpp index 33de52999..51c19a73b 100644 --- a/src/oscar/UI/ui_context.cpp +++ b/src/oscar/UI/ui_context.cpp @@ -123,9 +123,8 @@ void osc::ui::context::init() #endif // init ImGui for SDL2 /w OpenGL - ImGui_ImplSDL2_InitForOpenGL( - App::upd().upd_underlying_window(), - App::upd().upd_underlying_opengl_context() + ImGui_ImplSDL2_Init( + App::upd().upd_underlying_window() ); // init ImGui for OpenGL