TouchPressControl.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using UnityEngine.InputSystem.Layouts;
  3. using UnityEngine.InputSystem.LowLevel;
  4. using UnityEngine.InputSystem.Utilities;
  5. namespace UnityEngine.InputSystem.Controls
  6. {
  7. /// <summary>
  8. /// A button that reads its pressed state from <see cref="TouchControl.phase"/>.
  9. /// </summary>
  10. /// <remarks>
  11. /// This control is used by <see cref="TouchControl"/> to link <see cref="TouchControl.press"/>
  12. /// to <see cref="TouchControl.phase"/>. It will return 1 as long as the value of
  13. /// phase is <see cref="TouchPhase.Began"/>, <see cref="TouchPhase.Stationary"/>, or
  14. /// <see cref="TouchPhase.Moved"/>, i.e. as long as the touch is in progress. For
  15. /// all other phases, it will return 0.
  16. /// </remarks>
  17. /// <seealso cref="TouchControl"/>
  18. [InputControlLayout(hideInUI = true)]
  19. [Scripting.Preserve]
  20. public class TouchPressControl : ButtonControl
  21. {
  22. /// <inheritdoc />
  23. protected override void FinishSetup()
  24. {
  25. base.FinishSetup();
  26. if (!stateBlock.format.IsIntegerFormat())
  27. throw new NotSupportedException(
  28. $"Non-integer format '{stateBlock.format}' is not supported for TouchButtonControl '{this}'");
  29. }
  30. /// <inheritdoc />
  31. public override unsafe float ReadUnprocessedValueFromState(void* statePtr)
  32. {
  33. var valuePtr = (byte*)statePtr + (int)m_StateBlock.byteOffset;
  34. var intValue = MemoryHelpers.ReadIntFromMultipleBits(valuePtr, m_StateBlock.bitOffset, m_StateBlock.sizeInBits);
  35. var phaseValue = (TouchPhase)intValue;
  36. var value = 0.0f;
  37. if (phaseValue == TouchPhase.Began || phaseValue == TouchPhase.Stationary ||
  38. phaseValue == TouchPhase.Moved)
  39. value = 1;
  40. return Preprocess(value);
  41. }
  42. }
  43. }