A game about forced loneliness, made by TACStudios
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using UnityEngine.InputSystem.LowLevel;
5
6namespace UnityEngine.InputSystem.EnhancedTouch
7{
8 /// <summary>
9 /// A fixed-size buffer of <see cref="Touch"/> records used to trace the history of touches.
10 /// </summary>
11 /// <remarks>
12 /// This struct provides access to a recorded list of touches.
13 /// </remarks>
14 public struct TouchHistory : IReadOnlyList<Touch>
15 {
16 private readonly InputStateHistory<TouchState> m_History;
17 private readonly Finger m_Finger;
18 private readonly int m_Count;
19 private readonly int m_StartIndex;
20 private readonly uint m_Version;
21
22 internal TouchHistory(Finger finger, InputStateHistory<TouchState> history, int startIndex = -1, int count = -1)
23 {
24 m_Finger = finger;
25 m_History = history;
26 m_Version = history.version;
27 m_Count = count >= 0 ? count : m_History.Count;
28 m_StartIndex = startIndex >= 0 ? startIndex : m_History.Count - 1;
29 }
30
31 /// <summary>
32 /// Enumerate touches in the history. Goes from newest records to oldest.
33 /// </summary>
34 /// <returns>Enumerator over the touches in the history.</returns>
35 public IEnumerator<Touch> GetEnumerator()
36 {
37 return new Enumerator(this);
38 }
39
40 IEnumerator IEnumerable.GetEnumerator()
41 {
42 return GetEnumerator();
43 }
44
45 /// <summary>
46 /// Number of history records available.
47 /// </summary>
48 public int Count => m_Count;
49
50 /// <summary>
51 /// Return a history record by index. Indexing starts at 0 == newest to <see cref="Count"/> - 1 == oldest.
52 /// </summary>
53 /// <param name="index">Index of history record.</param>
54 /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is less than 0 or >= <see cref="Count"/>.</exception>
55 public Touch this[int index]
56 {
57 get
58 {
59 CheckValid();
60 if (index < 0 || index >= Count)
61 throw new ArgumentOutOfRangeException(
62 $"Index {index} is out of range for history with {Count} entries", nameof(index));
63
64 // History records oldest-first but we index newest-first.
65 return new Touch(m_Finger, m_History[m_StartIndex - index]);
66 }
67 }
68
69 internal void CheckValid()
70 {
71 if (m_Finger == null || m_History == null)
72 throw new InvalidOperationException("Touch history not initialized");
73 if (m_History.version != m_Version)
74 throw new InvalidOperationException(
75 "Touch history is no longer valid; the recorded history has been changed");
76 }
77
78 private class Enumerator : IEnumerator<Touch>
79 {
80 private readonly TouchHistory m_Owner;
81 private int m_Index;
82
83 internal Enumerator(TouchHistory owner)
84 {
85 m_Owner = owner;
86 m_Index = -1;
87 }
88
89 public bool MoveNext()
90 {
91 if (m_Index >= m_Owner.Count - 1)
92 return false;
93 ++m_Index;
94 return true;
95 }
96
97 public void Reset()
98 {
99 m_Index = -1;
100 }
101
102 public Touch Current => m_Owner[m_Index];
103
104 object IEnumerator.Current => Current;
105
106 public void Dispose()
107 {
108 }
109 }
110 }
111}