|
@@ -24,6 +24,8 @@
|
|
|
// 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-07-08: *BREAKING* Renamed ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback to ImGui_ImplGlfw_InstallEmscriptenCallbacks(), added GLFWWindow* parameter.
|
|
|
+// 2024-07-08: Emscripten: Added support for GLFW3 contrib port (GLFW 3.4.0 features + bug fixes): to enable, replace -sUSE_GLFW=3 with --use-port=contrib.glfw3 (requires emscripten 3.1.59+) (https://github.com/pongasoft/emscripten-glfw)
|
|
|
// 2024-07-02: Emscripten: Added io.PlatformOpenInShellFn() handler for Emscripten versions.
|
|
|
// 2023-12-19: Emscripten: Added ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback() to register canvas selector and auto-resize GLFW window.
|
|
|
// 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys.
|
|
@@ -106,6 +108,11 @@
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
#include <emscripten.h>
|
|
|
#include <emscripten/html5.h>
|
|
|
+#ifdef EMSCRIPTEN_USE_PORT_CONTRIB_GLFW3
|
|
|
+#include <GLFW/emscripten_glfw3.h>
|
|
|
+#else
|
|
|
+#define EMSCRIPTEN_USE_EMBEDDED_GLFW3
|
|
|
+#endif
|
|
|
#endif
|
|
|
|
|
|
// We gather version tests as define in order to easily see which features are version-dependent.
|
|
@@ -157,7 +164,7 @@ struct ImGui_ImplGlfw_Data
|
|
|
bool InstalledCallbacks;
|
|
|
bool CallbacksChainForAllWindows;
|
|
|
bool WantUpdateMonitors;
|
|
|
-#ifdef __EMSCRIPTEN__
|
|
|
+#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3
|
|
|
const char* CanvasSelector;
|
|
|
#endif
|
|
|
|
|
@@ -366,7 +373,7 @@ void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yo
|
|
|
if (bd->PrevUserCallbackScroll != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window))
|
|
|
bd->PrevUserCallbackScroll(window, xoffset, yoffset);
|
|
|
|
|
|
-#ifdef __EMSCRIPTEN__
|
|
|
+#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3
|
|
|
// Ignore GLFW events: will be processed in ImGui_ImplEmscripten_WheelCallback().
|
|
|
return;
|
|
|
#endif
|
|
@@ -377,7 +384,7 @@ void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yo
|
|
|
|
|
|
static int ImGui_ImplGlfw_TranslateUntranslatedKey(int key, int scancode)
|
|
|
{
|
|
|
-#if GLFW_HAS_GETKEYNAME && !defined(__EMSCRIPTEN__)
|
|
|
+#if GLFW_HAS_GETKEYNAME && !defined(EMSCRIPTEN_USE_EMBEDDED_GLFW3)
|
|
|
// GLFW 3.1+ attempts to "untranslate" keys, which goes the opposite of what every other framework does, making using lettered shortcuts difficult.
|
|
|
// (It had reasons to do so: namely GLFW is/was more likely to be used for WASD-type game controls rather than lettered shortcuts, but IHMO the 3.1 change could have been done differently)
|
|
|
// See https://github.com/glfw/glfw/issues/1502 for details.
|
|
@@ -388,7 +395,7 @@ static int ImGui_ImplGlfw_TranslateUntranslatedKey(int key, int scancode)
|
|
|
GLFWerrorfun prev_error_callback = glfwSetErrorCallback(nullptr);
|
|
|
const char* key_name = glfwGetKeyName(key, scancode);
|
|
|
glfwSetErrorCallback(prev_error_callback);
|
|
|
-#if GLFW_HAS_GETERROR && !defined(__EMSCRIPTEN__) // Eat errors (see #5908)
|
|
|
+#if GLFW_HAS_GETERROR && !defined(EMSCRIPTEN_USE_EMBEDDED_GLFW3) // Eat errors (see #5908)
|
|
|
(void)glfwGetError(nullptr);
|
|
|
#endif
|
|
|
if (key_name && key_name[0] != 0 && key_name[1] == 0)
|
|
@@ -496,7 +503,7 @@ void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor*, int)
|
|
|
bd->WantUpdateMonitors = true;
|
|
|
}
|
|
|
|
|
|
-#ifdef __EMSCRIPTEN__
|
|
|
+#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3
|
|
|
static EM_BOOL ImGui_ImplEmscripten_WheelCallback(int, const EmscriptenWheelEvent* ev, void*)
|
|
|
{
|
|
|
// Mimic Emscripten_HandleWheel() in SDL.
|
|
@@ -634,12 +641,6 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw
|
|
|
// Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any.
|
|
|
if (install_callbacks)
|
|
|
ImGui_ImplGlfw_InstallCallbacks(window);
|
|
|
- // Register Emscripten Wheel callback to workaround issue in Emscripten GLFW Emulation (#6096)
|
|
|
- // We intentionally do not check 'if (install_callbacks)' here, as some users may set it to false and call GLFW callback themselves.
|
|
|
- // FIXME: May break chaining in case user registered their own Emscripten callback?
|
|
|
-#ifdef __EMSCRIPTEN__
|
|
|
- emscripten_set_wheel_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, nullptr, false, ImGui_ImplEmscripten_WheelCallback);
|
|
|
-#endif
|
|
|
|
|
|
// Update monitors the first time (note: monitor callback are broken in GLFW 3.2 and earlier, see github.com/glfw/glfw/issues/784)
|
|
|
ImGui_ImplGlfw_UpdateMonitors();
|
|
@@ -694,8 +695,9 @@ void ImGui_ImplGlfw_Shutdown()
|
|
|
|
|
|
if (bd->InstalledCallbacks)
|
|
|
ImGui_ImplGlfw_RestoreCallbacks(bd->Window);
|
|
|
-#ifdef __EMSCRIPTEN__
|
|
|
- emscripten_set_wheel_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, nullptr, false, nullptr);
|
|
|
+#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3
|
|
|
+ if (bd->CanvasSelector)
|
|
|
+ emscripten_set_wheel_callback(bd->CanvasSelector, nullptr, false, nullptr);
|
|
|
#endif
|
|
|
|
|
|
for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
|
|
@@ -727,7 +729,7 @@ static void ImGui_ImplGlfw_UpdateMouseData()
|
|
|
ImGuiViewport* viewport = platform_io.Viewports[n];
|
|
|
GLFWwindow* window = (GLFWwindow*)viewport->PlatformHandle;
|
|
|
|
|
|
-#ifdef __EMSCRIPTEN__
|
|
|
+#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3
|
|
|
const bool is_window_focused = true;
|
|
|
#else
|
|
|
const bool is_window_focused = glfwGetWindowAttrib(window, GLFW_FOCUSED) != 0;
|
|
@@ -821,7 +823,7 @@ static void ImGui_ImplGlfw_UpdateGamepads()
|
|
|
return;
|
|
|
|
|
|
io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
|
|
|
-#if GLFW_HAS_GAMEPAD_API && !defined(__EMSCRIPTEN__)
|
|
|
+#if GLFW_HAS_GAMEPAD_API && !defined(EMSCRIPTEN_USE_EMBEDDED_GLFW3)
|
|
|
GLFWgamepadstate gamepad;
|
|
|
if (!glfwGetGamepadState(GLFW_JOYSTICK_1, &gamepad))
|
|
|
return;
|
|
@@ -939,7 +941,7 @@ void ImGui_ImplGlfw_NewFrame()
|
|
|
ImGui_ImplGlfw_UpdateGamepads();
|
|
|
}
|
|
|
|
|
|
-#ifdef __EMSCRIPTEN__
|
|
|
+#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3
|
|
|
static EM_BOOL ImGui_ImplGlfw_OnCanvasSizeChange(int event_type, const EmscriptenUiEvent* event, void* user_data)
|
|
|
{
|
|
|
ImGui_ImplGlfw_Data* bd = (ImGui_ImplGlfw_Data*)user_data;
|
|
@@ -960,7 +962,7 @@ static EM_BOOL ImGui_ImplEmscripten_FullscreenChangeCallback(int event_type, con
|
|
|
|
|
|
// 'canvas_selector' is a CSS selector. The event listener is applied to the first element that matches the query.
|
|
|
// STRING MUST PERSIST FOR THE APPLICATION DURATION. PLEASE USE A STRING LITERAL OR ENSURE POINTER WILL STAY VALID.
|
|
|
-void ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback(const char* canvas_selector)
|
|
|
+void ImGui_ImplGlfw_InstallEmscriptenCallbacks(GLFWwindow*, const char* canvas_selector)
|
|
|
{
|
|
|
IM_ASSERT(canvas_selector != nullptr);
|
|
|
ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
|
|
@@ -972,8 +974,24 @@ void ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback(const char* canvas_sel
|
|
|
|
|
|
// Change the size of the GLFW window according to the size of the canvas
|
|
|
ImGui_ImplGlfw_OnCanvasSizeChange(EMSCRIPTEN_EVENT_RESIZE, {}, bd);
|
|
|
+
|
|
|
+ // Register Emscripten Wheel callback to workaround issue in Emscripten GLFW Emulation (#6096)
|
|
|
+ // We intentionally do not check 'if (install_callbacks)' here, as some users may set it to false and call GLFW callback themselves.
|
|
|
+ // FIXME: May break chaining in case user registered their own Emscripten callback?
|
|
|
+ emscripten_set_wheel_callback(bd->CanvasSelector, nullptr, false, ImGui_ImplEmscripten_WheelCallback);
|
|
|
}
|
|
|
-#endif
|
|
|
+#elif defined(EMSCRIPTEN_USE_PORT_CONTRIB_GLFW3)
|
|
|
+// When using --use-port=contrib.glfw3 for the GLFW implementation, you can override the behavior of this call
|
|
|
+// by invoking emscripten_glfw_make_canvas_resizable afterward.
|
|
|
+// See https://github.com/pongasoft/emscripten-glfw/blob/master/docs/Usage.md#how-to-make-the-canvas-resizable-by-the-user for an explanation
|
|
|
+void ImGui_ImplGlfw_InstallEmscriptenCallbacks(GLFWwindow* window, const char* canvas_selector)
|
|
|
+{
|
|
|
+ GLFWwindow* w = (GLFWwindow*)(EM_ASM_INT({ return Module.glfwGetWindow(UTF8ToString($0)); }, canvas_selector));
|
|
|
+ IM_ASSERT(window == w); // Sanity check
|
|
|
+ IM_UNUSED(w);
|
|
|
+ emscripten_glfw_make_canvas_resizable(window, "window", nullptr);
|
|
|
+}
|
|
|
+#endif // #ifdef EMSCRIPTEN_USE_PORT_CONTRIB_GLFW3
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------------
|