Skip to content

Commit 3e5849a

Browse files
committed
Change logic to be based on state events
The previous logic was based on changing state and didn't apply if we queued events. Also, this commit avoids processing the queued events for the simulated Touchscreen.
1 parent 4203ed1 commit 3e5849a

File tree

1 file changed

+13
-37
lines changed

1 file changed

+13
-37
lines changed

Packages/com.unity.inputsystem/InputSystem/Plugins/EnhancedTouch/TouchSimulation.cs

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ protected void RemovePointer(Pointer pointer)
122122

123123
private unsafe void OnEvent(InputEventPtr eventPtr, InputDevice device)
124124
{
125+
if (device == simulatedTouchscreen)
126+
{
127+
// Avoid processing events queued by this simulation device
128+
return;
129+
}
130+
125131
var pointerIndex = m_Pointers.IndexOfReference(device, m_NumPointers);
126132
if (pointerIndex < 0)
127133
return;
@@ -290,63 +296,33 @@ private unsafe void UpdateTouch(int touchIndex, int pointerIndex, TouchPhase pha
290296
Debug.Assert(m_CurrentDisplayIndices[pointerIndex] <= byte.MaxValue, "Display index was larger than expected");
291297
byte displayIndex = (byte)m_CurrentDisplayIndices[pointerIndex];
292298

299+
// We need to partially set TouchState in a similar way that the Native side would do, but deriving that
300+
// data from the Pointer events.
301+
// The handling of the remaining fields is done by the Touchscreen.OnStateEvent() callback.
293302
var touch = new TouchState
294303
{
295304
phase = phase,
296305
position = position,
297-
displayIndex = displayIndex
306+
displayIndex = displayIndex,
298307
};
299-
var time = eventPtr.valid ? eventPtr.time : InputState.currentTime;
300-
301-
var oldTouchState = (TouchState*)((byte*)simulatedTouchscreen.currentStatePtr +
302-
simulatedTouchscreen.touches[touchIndex].stateBlock.byteOffset);
303308

304309
if (phase == TouchPhase.Began)
305310
{
306-
touch.isPrimaryTouch = m_PrimaryTouchIndex < 0;
307-
touch.startTime = time;
311+
touch.startTime = eventPtr.valid ? eventPtr.time : InputState.currentTime;
308312
touch.startPosition = position;
309313
touch.touchId = ++m_LastTouchId;
310-
touch.tapCount = oldTouchState->tapCount; // Get reset automatically by Touchscreen.
311-
312-
if (touch.isPrimaryTouch)
313-
m_PrimaryTouchIndex = touchIndex;
314314
}
315315
else
316316
{
317-
touch.touchId = oldTouchState->touchId;
318-
touch.isPrimaryTouch = m_PrimaryTouchIndex == touchIndex;
319-
touch.delta = position - oldTouchState->position;
320-
touch.startPosition = oldTouchState->startPosition;
321-
touch.startTime = oldTouchState->startTime;
322-
touch.tapCount = oldTouchState->tapCount;
323-
324-
if (phase == TouchPhase.Ended)
325-
{
326-
touch.isTap = time - oldTouchState->startTime <= Touchscreen.s_TapTime &&
327-
(position - oldTouchState->startPosition).sqrMagnitude <= Touchscreen.s_TapRadiusSquared;
328-
if (touch.isTap)
329-
++touch.tapCount;
330-
}
317+
touch.touchId = m_LastTouchId;
331318
}
332319

333320
//NOTE: Processing these events still happen in the current frame.
334-
using (StateEvent.From(simulatedTouchscreen, out var touchscreenEventPtr))
335-
{
336-
if (touch.isPrimaryTouch)
337-
{
338-
simulatedTouchscreen.primaryTouch.WriteValueIntoEvent(touch, touchscreenEventPtr);
339-
InputSystem.QueueEvent(touchscreenEventPtr);
340-
}
341-
simulatedTouchscreen.touches[touchIndex].WriteValueIntoEvent(touch, touchscreenEventPtr);
342-
InputSystem.QueueEvent(touchscreenEventPtr);
343-
}
321+
InputSystem.QueueStateEvent(simulatedTouchscreen, touch);
344322

345323
if (phase.IsEndedOrCanceled())
346324
{
347325
m_Touches[touchIndex] = null;
348-
if (m_PrimaryTouchIndex == touchIndex)
349-
m_PrimaryTouchIndex = -1;
350326
}
351327
}
352328

0 commit comments

Comments
 (0)