From 6ffdd3a34789b73418da54233e3e09d5f434f91b Mon Sep 17 00:00:00 2001 From: Vitalii Mikhailov Date: Tue, 31 Dec 2024 16:03:43 +0200 Subject: [PATCH] Added touch support --- .../Controller/ImGuiController.Emscripten.cs | 11 ++++++++ .../Controller/ImGuiController.SDL2.cs | 27 +++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/BUTR.CrashReport.Renderer.ImGui.WASM/Controller/ImGuiController.Emscripten.cs b/src/BUTR.CrashReport.Renderer.ImGui.WASM/Controller/ImGuiController.Emscripten.cs index 5eb83e9..d4a5ded 100644 --- a/src/BUTR.CrashReport.Renderer.ImGui.WASM/Controller/ImGuiController.Emscripten.cs +++ b/src/BUTR.CrashReport.Renderer.ImGui.WASM/Controller/ImGuiController.Emscripten.cs @@ -33,7 +33,18 @@ private static void ScaleWindowSize(ref int width, ref int height, Vector2 scale private unsafe void SetupEmscripten(IntPtr windowHandle) { _imgui.GetIO(out var io); + io.BackendFlags |= ImGuiNET.ImGuiBackendFlags.HasMouseCursors; + io.BackendFlags |= ImGuiNET.ImGuiBackendFlags.HasSetMousePos; + io.BackendFlags |= ImGuiNET.ImGuiBackendFlags.RendererHasVtxOffset; + io.BackendFlags |= ImGuiNET.ImGuiBackendFlags.HasGamepad; + + io.ConfigFlags |= ImGuiNET.ImGuiConfigFlags.NavEnableKeyboard; + io.ConfigFlags |= ImGuiNET.ImGuiConfigFlags.NavEnableGamepad; + io.ConfigFlags |= ImGuiNET.ImGuiConfigFlags.NavEnableSetMousePos; + io.ConfigFlags |= ImGuiNET.ImGuiConfigFlags.DpiEnableScaleFonts; + io.ConfigFlags |= ImGuiNET.ImGuiConfigFlags.DpiEnableScaleViewports; + io.ConfigFlags |= ImGuiNET.ImGuiConfigFlags.IsTouchScreen; int windowsWidth, windowsHeight; _emscripten.custom_emscripten_get_display_usable_bounds(&windowsWidth, &windowsHeight); diff --git a/src/BUTR.CrashReport.Renderer.ImGui.WASM/Controller/ImGuiController.SDL2.cs b/src/BUTR.CrashReport.Renderer.ImGui.WASM/Controller/ImGuiController.SDL2.cs index 00e0d76..578e762 100644 --- a/src/BUTR.CrashReport.Renderer.ImGui.WASM/Controller/ImGuiController.SDL2.cs +++ b/src/BUTR.CrashReport.Renderer.ImGui.WASM/Controller/ImGuiController.SDL2.cs @@ -1,6 +1,8 @@ #if SDL2 using BUTR.CrashReport.ImGui.Enums; +using SDL2; + using System.Numerics; using System.Runtime.InteropServices; using System.Text; @@ -96,7 +98,7 @@ public unsafe bool ProcessEvent(SDL_Event evt) } case SDL_EventType.SDL_MOUSEWHEEL: { - if (evt.wheel.windowID != SDL_GetWindowID(_window)) return false; ; + if (evt.wheel.windowID != SDL_GetWindowID(_window)) return false; var wheel_x = -evt.wheel.preciseX; var wheel_y = evt.wheel.preciseY; @@ -110,7 +112,7 @@ public unsafe bool ProcessEvent(SDL_Event evt) case SDL_EventType.SDL_MOUSEBUTTONDOWN: case SDL_EventType.SDL_MOUSEBUTTONUP: { - if (evt.button.windowID != SDL_GetWindowID(_window)) return false; ; + if (evt.button.windowID != SDL_GetWindowID(_window)) return false; var mouse_button = ImGuiNET.ImGuiMouseButton.COUNT; if (evt.button.button == SDL_BUTTON_LEFT) { mouse_button = ImGuiNET.ImGuiMouseButton.Left; } @@ -124,9 +126,24 @@ public unsafe bool ProcessEvent(SDL_Event evt) io.AddMouseButtonEvent(mouse_button, evt.type == SDL_EventType.SDL_MOUSEBUTTONDOWN); return true; } + case SDL_EventType.SDL_FINGERMOTION: + { + if (evt.tfinger.windowID != SDL_GetWindowID(_window)) return false; + + const float touchScrollMultiplier = 5.0f; + + var scale = GetWindowDevicePixelRatio(); + + var wheel_x = -evt.tfinger.dx * scale.X * touchScrollMultiplier; + var wheel_y = evt.tfinger.dy * scale.Y * touchScrollMultiplier; + + io.MouseWheelH += wheel_x; + io.MouseWheel += wheel_y; + return true; + } case SDL_EventType.SDL_TEXTINPUT: { - if (evt.text.windowID != SDL_GetWindowID(_window)) return false; ; + if (evt.text.windowID != SDL_GetWindowID(_window)) return false; var utf8 = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(evt.text.text); var utf16Length = Encoding.UTF8.GetCharCount(utf8); @@ -140,7 +157,7 @@ public unsafe bool ProcessEvent(SDL_Event evt) case SDL_EventType.SDL_KEYDOWN: case SDL_EventType.SDL_KEYUP: { - if (evt.key.windowID != SDL_GetWindowID(_window)) return false; ; + if (evt.key.windowID != SDL_GetWindowID(_window)) return false; UpdateKeyModifiers(evt.key.keysym.mod); @@ -152,7 +169,7 @@ public unsafe bool ProcessEvent(SDL_Event evt) } case SDL_EventType.SDL_WINDOWEVENT: { - if (evt.window.windowID != SDL_GetWindowID(_window)) return false; ; + if (evt.window.windowID != SDL_GetWindowID(_window)) return false; /* if (evt.window.windowEvent == SDL_WindowEventID.SDL_WINDOWEVENT_ENTER)