////TODO: pass state ptr *NOT* value ptr (it's confusing) // We don't allow custom default values for state so all zeros indicates // default states for us. // NOTE: The given argument should point directly to the value *not* to the // base state to which the state block offset has to be added. internal unsafe bool CheckStateIsAllZeros(IntPtr valuePtr = new IntPtr()) { if (valuePtr == IntPtr.Zero) { valuePtr = new IntPtr(currentStatePtr.ToInt64() + (int)m_StateBlock.byteOffset); } // Bitfield value. if (m_StateBlock.sizeInBits % 8 != 0 || m_StateBlock.bitOffset != 0) { if (m_StateBlock.sizeInBits > 1) { throw new NotImplementedException("multi-bit zero check"); } return(BitfieldHelpers.ReadSingleBit(valuePtr, m_StateBlock.bitOffset) == false); } // Multi-byte value. var ptr = (byte *)valuePtr; var numBytes = m_StateBlock.alignedSizeInBytes; for (var i = 0; i < numBytes; ++i, ++ptr) { if (*ptr != 0) { return(false); } } return(true); }
private unsafe string ReadRawValueAsString(InputControl control) { fixed(byte *state = stateBuffer) { var ptr = state + control.m_StateBlock.byteOffset - m_RootControl.m_StateBlock.byteOffset; var format = control.m_StateBlock.format; if (format == InputStateBlock.kTypeBit) { if (BitfieldHelpers.ReadSingleBit(new IntPtr(ptr), control.m_StateBlock.bitOffset)) { return("1"); } return("0"); } if (format == InputStateBlock.kTypeByte) { return((*ptr).ToString()); } if (format == InputStateBlock.kTypeShort) { return((*((short *)ptr)).ToString()); } if (format == InputStateBlock.kTypeInt) { return((*((int *)ptr)).ToString()); } if (format == InputStateBlock.kTypeFloat) { return((*((float *)ptr)).ToString()); } if (format == InputStateBlock.kTypeDouble) { return((*((double *)ptr)).ToString()); } return(null); } }
// Read a floating-point value from the given state. Automatically checks // the state format of the control and performs conversions. // NOTE: Throws if the format set on 'stateBlock' is not of integer, floating-point, // or bitfield type. protected override unsafe float ReadRawValueFrom(IntPtr statePtr) { float value; var valuePtr = new IntPtr(statePtr.ToInt64() + (int)m_StateBlock.byteOffset); var format = m_StateBlock.format; if (format == InputStateBlock.kTypeFloat) { value = *(float *)valuePtr; } else if (format == InputStateBlock.kTypeBit) { if (m_StateBlock.sizeInBits != 1) { throw new NotImplementedException("Cannot yet convert multi-bit fields to floats"); } value = BitfieldHelpers.ReadSingleBit(valuePtr, m_StateBlock.bitOffset) ? 1.0f : 0.0f; } // 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 == InputStateBlock.kTypeShort) { value = *((short *)valuePtr) / 65535.0f; } else if (format == InputStateBlock.kTypeByte) { value = *((byte *)valuePtr) / 255.0f; } else { throw new Exception(string.Format("State format '{0}' is not supported as state for {1}", m_StateBlock.format, GetType().Name)); } return(Preprocess(value)); }