Skip to content

Commit

Permalink
Added touch support
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Dec 31, 2024
1 parent 355f931 commit 6ffdd3a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#if SDL2
using BUTR.CrashReport.ImGui.Enums;

using SDL2;

using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -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;
Expand All @@ -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; }
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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)
Expand Down

0 comments on commit 6ffdd3a

Please sign in to comment.