暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IntegerControl.cs 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using UnityEngine.InputSystem.LowLevel;
  2. using UnityEngine.InputSystem.Utilities;
  3. ////TODO: this or the layout system needs to detect when the format isn't supported by the control
  4. namespace UnityEngine.InputSystem.Controls
  5. {
  6. /// <summary>
  7. /// A generic input control reading integer values.
  8. /// </summary>
  9. public class IntegerControl : InputControl<int>
  10. {
  11. /// <summary>
  12. /// Default-initialize an integer control.
  13. /// </summary>
  14. public IntegerControl()
  15. {
  16. m_StateBlock.format = InputStateBlock.FormatInt;
  17. }
  18. /// <inheritdoc/>
  19. public override unsafe int ReadUnprocessedValueFromState(void* statePtr)
  20. {
  21. switch (m_OptimizedControlDataType)
  22. {
  23. case InputStateBlock.kFormatInt:
  24. return *(int*)((byte*)statePtr + (int)m_StateBlock.byteOffset);
  25. default:
  26. return m_StateBlock.ReadInt(statePtr);
  27. }
  28. }
  29. /// <inheritdoc/>
  30. public override unsafe void WriteValueIntoState(int value, void* statePtr)
  31. {
  32. switch (m_OptimizedControlDataType)
  33. {
  34. case InputStateBlock.kFormatInt:
  35. *(int*)((byte*)statePtr + (int)m_StateBlock.byteOffset) = value;
  36. break;
  37. default:
  38. m_StateBlock.WriteInt(statePtr, value);
  39. break;
  40. }
  41. }
  42. protected override FourCC CalculateOptimizedControlDataType()
  43. {
  44. if (m_StateBlock.format == InputStateBlock.FormatInt &&
  45. m_StateBlock.sizeInBits == 32 &&
  46. m_StateBlock.bitOffset == 0)
  47. return InputStateBlock.FormatInt;
  48. return InputStateBlock.FormatInvalid;
  49. }
  50. }
  51. }