Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TouchHistory.cs 3.6KB

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