Skip to content
Permalink
Browse files

Initial SDL Finger Multitouch support

Working but certain parts are unfinished and hacky. See TODOs
  • Loading branch information
dominikjancik authored and xen2 committed Aug 30, 2019
1 parent 21399ae commit 73dc9ae2f6550de4cc642291f562ae3396e9a74c
@@ -123,6 +123,9 @@ public static void ProcessEvents()
}
}

// Used for FINGER* events that do not provide their own windowID
static Window lastCtrl = null;

/// <summary>
/// Process a single event and dispatch it to the right window.
/// </summary>
@@ -160,6 +163,12 @@ public static void ProcessEvent(SDL.SDL_Event e)
ctrl = WindowFromSdlHandle(SDL.SDL_GetWindowFromID(e.text.windowID));
break;

case SDL.SDL_EventType.SDL_FINGERMOTION:
case SDL.SDL_EventType.SDL_FINGERDOWN:
case SDL.SDL_EventType.SDL_FINGERUP:
ctrl = lastCtrl;
break;

case SDL.SDL_EventType.SDL_WINDOWEVENT:
{
ctrl = WindowFromSdlHandle(SDL.SDL_GetWindowFromID(e.window.windowID));
@@ -182,6 +191,9 @@ public static void ProcessEvent(SDL.SDL_Event e)
break;
}
ctrl?.ProcessEvent(e);

// TODO this is a bit of a mess but FINGER* events do not provide a proper windowID - how to handle?
if (ctrl != null) lastCtrl = ctrl;
}

/// <summary>
@@ -399,6 +399,7 @@ public FormBorderStyle FormBorderStyle
public delegate void WindowEventDelegate(SDL.SDL_WindowEvent e);
public delegate void KeyDelegate(SDL.SDL_KeyboardEvent e);
public delegate void JoystickDeviceChangedDelegate(int which);
public delegate void TouchFingerDelegate(SDL.SDL_TouchFingerEvent e);
public delegate void NotificationDelegate();

public event MouseButtonDelegate PointerButtonPressActions;
@@ -412,6 +413,9 @@ public FormBorderStyle FormBorderStyle
public event NotificationDelegate CloseActions;
public event JoystickDeviceChangedDelegate JoystickDeviceAdded;
public event JoystickDeviceChangedDelegate JoystickDeviceRemoved;
public event TouchFingerDelegate FingerMoveActions;
public event TouchFingerDelegate FingerPressActions;
public event TouchFingerDelegate FingerReleaseActions;
public event WindowEventDelegate ResizeBeginActions;
public event WindowEventDelegate ResizeEndActions;
public event WindowEventDelegate ActivateActions;
@@ -476,6 +480,18 @@ public virtual void ProcessEvent(SDL.SDL_Event e)
JoystickDeviceRemoved?.Invoke(e.jdevice.which);
break;

case SDL.SDL_EventType.SDL_FINGERMOTION:
FingerMoveActions?.Invoke(e.tfinger);
break;

case SDL.SDL_EventType.SDL_FINGERDOWN:
FingerPressActions?.Invoke(e.tfinger);
break;

case SDL.SDL_EventType.SDL_FINGERUP:
FingerReleaseActions?.Invoke(e.tfinger);
break;

case SDL.SDL_EventType.SDL_WINDOWEVENT:
{
switch (e.window.windowEvent)
@@ -0,0 +1,78 @@
// Copyright (c) Xenko contributors (https://xenko.com) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.

#if XENKO_UI_SDL
using System;
using SDL2;
using Xenko.Core.Mathematics;
using Xenko.Games;
using Xenko.Graphics.SDL;

namespace Xenko.Input
{
// TODO FingerSDL or Touch(screen)SDL?
internal class FingerSDL : PointerDeviceBase, IDisposable
{
private readonly Window uiControl;

public FingerSDL(InputSourceSDL source, Window uiControl)
{
Source = source;
this.uiControl = uiControl;

uiControl.FingerMoveActions += OnFingerMoveEvent;
uiControl.FingerPressActions += OnFingerPressEvent;
uiControl.FingerReleaseActions += OnFingerReleaseEvent;

uiControl.ResizeEndActions += OnSizeChanged;
OnSizeChanged(new SDL.SDL_WindowEvent());
}

public override string Name => "SDL Finger";

// TODO Should there be another Guid for this Input device? (This one is copied from MouseSDL + 1)
public override Guid Id => new Guid("0ccaf48e-e371-4b34-b6bb-a3720f6742a9");

public override IInputSource Source { get; }

public void Dispose()
{
uiControl.FingerMoveActions -= OnFingerMoveEvent;
uiControl.FingerPressActions -= OnFingerPressEvent;
uiControl.FingerReleaseActions -= OnFingerReleaseEvent;

uiControl.ResizeEndActions -= OnSizeChanged;
}

private void OnSizeChanged(SDL.SDL_WindowEvent eventArgs)
{
SetSurfaceSize(new Vector2(uiControl.ClientSize.Width, uiControl.ClientSize.Height));
}

private void HandleFingerEvent(SDL.SDL_TouchFingerEvent e, PointerEventType type)
{
var newPosition = new Vector2(e.x, e.y);

// TODO own ID counter ala iOS/Android implementations
var id = (int)e.fingerId;

PointerState.PointerInputEvents.Add(new PointerDeviceState.InputEvent { Type = type, Position = newPosition, Id = id });
}

private void OnFingerMoveEvent(SDL.SDL_TouchFingerEvent e)
{
HandleFingerEvent(e, PointerEventType.Moved);
}

private void OnFingerPressEvent(SDL.SDL_TouchFingerEvent e)
{
HandleFingerEvent(e, PointerEventType.Pressed);
}

private void OnFingerReleaseEvent(SDL.SDL_TouchFingerEvent e)
{
HandleFingerEvent(e, PointerEventType.Released);
}
}
}
#endif
@@ -21,6 +21,7 @@ internal class InputSourceSDL : InputSourceBase
private Window uiControl;
private MouseSDL mouse;
private KeyboardSDL keyboard;
private FingerSDL finger;
private InputManager inputManager;

public override void Initialize(InputManager inputManager)
@@ -33,9 +34,11 @@ public override void Initialize(InputManager inputManager)

mouse = new MouseSDL(this, inputManager.Game, uiControl);
keyboard = new KeyboardSDL(this, uiControl);
finger = new FingerSDL(this, uiControl);

RegisterDevice(mouse);
RegisterDevice(keyboard);
RegisterDevice(finger);

// Scan for gamepads
Scan();

0 comments on commit 73dc9ae

Please sign in to comment.
You can’t perform that action at this time.