TouchPhaseControl.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using UnityEngine.InputSystem.Layouts;
  2. using UnityEngine.InputSystem.LowLevel;
  3. // Unfortunately, C# (at least up to version 6) does not support enum type constraints. There's
  4. // ways to work around it in some situations (https://stackoverflow.com/questions/79126/create-generic-method-constraining-t-to-an-enum)
  5. // but not in a way that will allow us to convert an int to the enum type.
  6. ////TODO: allow this to be stored in less than 32bits
  7. namespace UnityEngine.InputSystem.Controls
  8. {
  9. /// <summary>
  10. /// A control reading a <see cref="TouchPhase"/> value.
  11. /// </summary>
  12. /// <remarks>
  13. /// This is used mainly by <see cref="Touchscreen"/> to read <see cref="TouchState.phase"/>.
  14. /// </remarks>
  15. /// <seealso cref="Touchscreen"/>
  16. [InputControlLayout(hideInUI = true)]
  17. [Scripting.Preserve]
  18. public class TouchPhaseControl : InputControl<TouchPhase>
  19. {
  20. /// <summary>
  21. /// Default-initialize the control.
  22. /// </summary>
  23. /// <remarks>
  24. /// Format of the control is <see cref="InputStateBlock.FormatInt"/>
  25. /// by default.
  26. /// </remarks>
  27. public TouchPhaseControl()
  28. {
  29. m_StateBlock.format = InputStateBlock.FormatInt;
  30. }
  31. /// <inheritdoc />
  32. public override unsafe TouchPhase ReadUnprocessedValueFromState(void* statePtr)
  33. {
  34. var intValue = stateBlock.ReadInt(statePtr);
  35. return (TouchPhase)intValue;
  36. }
  37. /// <inheritdoc />
  38. public override unsafe void WriteValueIntoState(TouchPhase value, void* statePtr)
  39. {
  40. var valuePtr = (byte*)statePtr + (int)m_StateBlock.byteOffset;
  41. *(TouchPhase*)valuePtr = value;
  42. }
  43. }
  44. }