public override unsafe float ReadUnprocessedValueFromState(void *statePtr) { var valuePtr = (byte *)statePtr + (int)m_StateBlock.byteOffset; var intValue = MemoryHelpers.ReadIntFromMultipleBits(valuePtr, m_StateBlock.bitOffset, m_StateBlock.sizeInBits); var value = 0.0f; if (minValue > maxValue) { // If no wrapping point is set, default to wrapping around exactly // at the point of minValue. if (wrapAtValue == nullValue) { wrapAtValue = minValue; } if ((intValue >= minValue && intValue <= wrapAtValue) || (intValue != nullValue && intValue <= maxValue)) { value = 1.0f; } } else { value = intValue >= minValue && intValue <= maxValue ? 1.0f : 0.0f; } return(Preprocess(value)); }
public unsafe int ReadInt(IntPtr statePtr) { Debug.Assert(sizeInBits != 0); var valuePtr = (byte *)statePtr.ToPointer() + (int)byteOffset; int value; if (format == kTypeInt || format == kTypeUInt) { Debug.Assert(sizeInBits == 32, "INT and UINT state must have sizeInBits=32"); Debug.Assert(bitOffset == 0, "INT and UINT state must be byte-aligned"); value = *(int *)valuePtr; } else if (format == kTypeBit) { if (sizeInBits == 1) { value = MemoryHelpers.ReadSingleBit(new IntPtr(valuePtr), bitOffset) ? 1 : 0; } else { value = MemoryHelpers.ReadIntFromMultipleBits(new IntPtr(valuePtr), bitOffset, sizeInBits); } } else if (format == kTypeByte) { Debug.Assert(sizeInBits == 8, "BYTE state must have sizeInBits=8"); Debug.Assert(bitOffset == 0, "BYTE state must be byte-aligned"); value = *valuePtr; } else if (format == kTypeSByte) { Debug.Assert(sizeInBits == 8, "SBYT state must have sizeInBits=8"); Debug.Assert(bitOffset == 0, "SBYT state must be byte-aligned"); value = *(sbyte *)valuePtr; } else if (format == kTypeShort) { Debug.Assert(sizeInBits == 16, "SHRT state must have sizeInBits=16"); Debug.Assert(bitOffset == 0, "SHRT state must be byte-aligned"); value = *(short *)valuePtr; } else if (format == kTypeUShort) { Debug.Assert(sizeInBits == 16, "USHT state must have sizeInBits=16"); Debug.Assert(bitOffset == 0, "USHT state must be byte-aligned"); value = *(ushort *)valuePtr; } else { throw new Exception(string.Format("State format '{0}' is not supported as integer format", format)); } return(value); }
public override unsafe float ReadUnprocessedValueFromState(void* statePtr) { var valuePtr = (byte*)statePtr + (int)m_StateBlock.byteOffset; var intValue = MemoryHelpers.ReadIntFromMultipleBits(valuePtr, m_StateBlock.bitOffset, m_StateBlock.sizeInBits); var phaseValue = (TouchPhase)intValue; var value = 0.0f; if (phaseValue == TouchPhase.Began || phaseValue == TouchPhase.Stationary || phaseValue == TouchPhase.Moved) value = 1; return Preprocess(value); }
public unsafe int ReadInt(IntPtr statePtr) { var valuePtr = (byte *)statePtr.ToPointer() + (int)byteOffset; int value; if (format == kTypeInt || format == kTypeUInt) { value = *(int *)valuePtr; } else if (format == kTypeBit) { if (sizeInBits == 0) { value = MemoryHelpers.ReadSingleBit(new IntPtr(valuePtr), bitOffset) ? 1 : 0; } else { value = MemoryHelpers.ReadIntFromMultipleBits(new IntPtr(valuePtr), bitOffset, sizeInBits); } } else if (format == kTypeByte) { value = *valuePtr; } else if (format == kTypeSByte) { value = *(sbyte *)valuePtr; } else if (format == kTypeShort) { value = *(short *)valuePtr; } else if (format == kTypeUShort) { value = *(ushort *)valuePtr; } else { throw new Exception(string.Format("State format '{0}' is not supported as integer format", format)); } return(value); }
////REVIEW: This is some bad code duplication here between Read/WriteFloat&Double but given that there's no //// way to use a type argument here, not sure how to get rid of it. public double ReadDouble(void *statePtr) { Debug.Assert(sizeInBits != 0); var valuePtr = (byte *)statePtr + (int)byteOffset; double value; if (format == FormatFloat) { Debug.Assert(sizeInBits == 32, "FLT state must have sizeInBits=32"); Debug.Assert(bitOffset == 0, "FLT state must be byte-aligned"); value = *(float *)valuePtr; } else if (format == FormatBit || format == FormatSBit) { if (sizeInBits == 1) { value = MemoryHelpers.ReadSingleBit(valuePtr, bitOffset) ? 1.0f : (format == FormatSBit ? -1.0f : 0.0f); } else if (sizeInBits != 31) { var maxValue = (float)(1 << (int)sizeInBits); var rawValue = (float)(MemoryHelpers.ReadIntFromMultipleBits(valuePtr, bitOffset, sizeInBits)); if (format == FormatSBit) { var unclampedValue = (((rawValue / maxValue) * 2.0f) - 1.0f); value = Mathf.Clamp(unclampedValue, -1.0f, 1.0f); } else { value = Mathf.Clamp(rawValue / maxValue, 0.0f, 1.0f); } } else { throw new NotImplementedException("Cannot yet convert multi-bit fields greater than 31 bits to floats"); } } // If a control with an integer-based representation does not use the full range // of its integer size (e.g. only goes from [0..128]), processors or the parameters // above have to be used to re-process the resulting float values. else if (format == FormatShort) { Debug.Assert(sizeInBits == 16, "SHRT state must have sizeInBits=16"); Debug.Assert(bitOffset == 0, "SHRT state must be byte-aligned"); ////REVIEW: What's better here? This code reaches a clean -1 but doesn't reach a clean +1 as the range is [-32768..32767]. //// Should we cut off at -32767? Or just live with the fact that 0.999 is as high as it gets? value = *(short *)valuePtr / 32768.0f; } else if (format == FormatUShort) { Debug.Assert(sizeInBits == 16, "USHT state must have sizeInBits=16"); Debug.Assert(bitOffset == 0, "USHT state must be byte-aligned"); value = *(ushort *)valuePtr / 65535.0f; } else if (format == FormatByte) { Debug.Assert(sizeInBits == 8, "BYTE state must have sizeInBits=8"); Debug.Assert(bitOffset == 0, "BYTE state must be byte-aligned"); value = *valuePtr / 255.0f; } else if (format == FormatSByte) { Debug.Assert(sizeInBits == 8, "SBYT state must have sizeInBits=8"); Debug.Assert(bitOffset == 0, "SBYT state must be byte-aligned"); ////REVIEW: Same problem here as with 'short' value = *(sbyte *)valuePtr / 128.0f; } else if (format == FormatInt) { Debug.Assert(sizeInBits == 32, "INT state must have sizeInBits=32"); Debug.Assert(bitOffset == 0, "INT state must be byte-aligned"); value = *(Int32 *)valuePtr / 2147483647.0f; } else if (format == FormatUInt) { Debug.Assert(sizeInBits == 32, "UINT state must have sizeInBits=32"); Debug.Assert(bitOffset == 0, "UINT state must be byte-aligned"); value = *(UInt32 *)valuePtr / 4294967295.0f; } else if (format == FormatDouble) { Debug.Assert(sizeInBits == 64, "DBL state must have sizeInBits=64"); Debug.Assert(bitOffset == 0, "DBL state must be byte-aligned"); value = *(double *)valuePtr; } else { throw new Exception($"State format '{format}' is not supported as floating-point format"); } return(value); }
public int ReadInt(void *statePtr) { Debug.Assert(sizeInBits != 0); var valuePtr = (byte *)statePtr + (int)byteOffset; int value; if (format == FormatInt || format == FormatUInt) { Debug.Assert(sizeInBits == 32, "INT and UINT state must have sizeInBits=32"); Debug.Assert(bitOffset == 0, "INT and UINT state must be byte-aligned"); value = *(int *)valuePtr; } else if (format == FormatBit) { if (sizeInBits == 1) { value = MemoryHelpers.ReadSingleBit(valuePtr, bitOffset) ? 1 : 0; } else { value = MemoryHelpers.ReadIntFromMultipleBits(valuePtr, bitOffset, sizeInBits); } } else if (format == FormatSBit) { if (sizeInBits == 1) { value = MemoryHelpers.ReadSingleBit(valuePtr, bitOffset) ? 1 : -1; } else { var halfMax = (1 << (int)sizeInBits) / 2; var unsignedValue = MemoryHelpers.ReadIntFromMultipleBits(valuePtr, bitOffset, sizeInBits); value = unsignedValue - halfMax; } } else if (format == FormatByte) { Debug.Assert(sizeInBits == 8, "BYTE state must have sizeInBits=8"); Debug.Assert(bitOffset == 0, "BYTE state must be byte-aligned"); value = *valuePtr; } else if (format == FormatSByte) { Debug.Assert(sizeInBits == 8, "SBYT state must have sizeInBits=8"); Debug.Assert(bitOffset == 0, "SBYT state must be byte-aligned"); value = *(sbyte *)valuePtr; } else if (format == FormatShort) { Debug.Assert(sizeInBits == 16, "SHRT state must have sizeInBits=16"); Debug.Assert(bitOffset == 0, "SHRT state must be byte-aligned"); value = *(short *)valuePtr; } else if (format == FormatUShort) { Debug.Assert(sizeInBits == 16, "USHT state must have sizeInBits=16"); Debug.Assert(bitOffset == 0, "USHT state must be byte-aligned"); value = *(ushort *)valuePtr; } else { throw new InvalidOperationException($"State format '{format}' is not supported as integer format"); } return(value); }
private unsafe string ReadRawValueAsString(InputControl control, byte[] state) { fixed(byte *statePtr = state) { var ptr = statePtr + control.m_StateBlock.byteOffset - m_RootControl.m_StateBlock.byteOffset; var format = control.m_StateBlock.format; object value = null; if (format == InputStateBlock.kTypeBit) { if (control.valueSizeInBytes == 1) { value = MemoryHelpers.ReadSingleBit(ptr, control.m_StateBlock.bitOffset) ? "1" : "0"; } else { value = MemoryHelpers.ReadIntFromMultipleBits(ptr, control.m_StateBlock.bitOffset, control.m_StateBlock.sizeInBits); } } else if (format == InputStateBlock.kTypeSBit) { if (control.valueSizeInBytes == 1) { value = MemoryHelpers.ReadSingleBit(ptr, control.m_StateBlock.bitOffset) ? "1" : "-1"; } else { var halfMaxValue = ((1 << (int)control.m_StateBlock.sizeInBits) - 1) / 2; var fullValue = (MemoryHelpers.ReadIntFromMultipleBits(ptr, control.m_StateBlock.bitOffset, control.m_StateBlock.sizeInBits)); value = fullValue - halfMaxValue; } } else if (format == InputStateBlock.kTypeByte || format == InputStateBlock.kTypeSByte) { value = *ptr; } else if (format == InputStateBlock.kTypeShort) { value = *(short *)ptr; } else if (format == InputStateBlock.kTypeUShort) { value = *(ushort *)ptr; } else if (format == InputStateBlock.kTypeInt) { value = *(int *)ptr; } else if (format == InputStateBlock.kTypeUInt) { value = *(uint *)ptr; } else if (format == InputStateBlock.kTypeFloat) { value = *(float *)ptr; } else if (format == InputStateBlock.kTypeDouble) { value = *(double *)ptr; } // Stringify enum values, for. ex., PointerPhase if (value != null && control.valueType.IsEnum) { var intValue = Convert.ToInt32(value); value = Enum.ToObject(control.valueType, intValue); } return(value?.ToString()); } }