using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.InputSystem.LowLevel;
namespace UnityEngine.InputSystem.EnhancedTouch
{
///
/// A fixed-size buffer of records used to trace the history of touches.
///
///
/// This struct provides access to a recorded list of touches.
///
public struct TouchHistory : IReadOnlyList
{
private readonly InputStateHistory m_History;
private readonly Finger m_Finger;
private readonly int m_Count;
private readonly int m_StartIndex;
private readonly uint m_Version;
internal TouchHistory(Finger finger, InputStateHistory history, int startIndex = -1, int count = -1)
{
m_Finger = finger;
m_History = history;
m_Version = history.version;
m_Count = count >= 0 ? count : m_History.Count;
m_StartIndex = startIndex >= 0 ? startIndex : m_History.Count - 1;
}
///
/// Enumerate touches in the history. Goes from newest records to oldest.
///
/// Enumerator over the touches in the history.
public IEnumerator GetEnumerator()
{
return new Enumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
///
/// Number of history records available.
///
public int Count => m_Count;
///
/// Return a history record by index. Indexing starts at 0 == newest to - 1 == oldest.
///
/// Index of history record.
/// is less than 0 or >= .
public Touch this[int index]
{
get
{
CheckValid();
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException(
$"Index {index} is out of range for history with {Count} entries", nameof(index));
// History records oldest-first but we index newest-first.
return new Touch(m_Finger, m_History[m_StartIndex - index]);
}
}
internal void CheckValid()
{
if (m_Finger == null || m_History == null)
throw new InvalidOperationException("Touch history not initialized");
if (m_History.version != m_Version)
throw new InvalidOperationException(
"Touch history is no longer valid; the recorded history has been changed");
}
private class Enumerator : IEnumerator
{
private readonly TouchHistory m_Owner;
private int m_Index;
internal Enumerator(TouchHistory owner)
{
m_Owner = owner;
m_Index = -1;
}
public bool MoveNext()
{
if (m_Index >= m_Owner.Count - 1)
return false;
++m_Index;
return true;
}
public void Reset()
{
m_Index = -1;
}
public Touch Current => m_Owner[m_Index];
object IEnumerator.Current => Current;
public void Dispose()
{
}
}
}
}