public void ReadState(ControllerState outState) { lock (state) { state.connectionState = EmulatorManager.Instance.Connected ? GvrConnectionState.Connected : GvrConnectionState.Connecting; outState.CopyFrom(state); } state.ClearTransientState(); }
public void ReadState(ControllerState outState) { lock (state) { state.connectionState = EmulatorManager.Instance.Connected ? GvrConnectionState.Connected : GvrConnectionState.Connecting; state.apiStatus = EmulatorManager.Instance.Connected ? GvrControllerApiStatus.Ok : GvrControllerApiStatus.Unavailable; outState.CopyFrom(state); } state.ClearTransientState(); }
public void CopyFrom(ControllerState other) { connectionState = other.connectionState; orientation = other.orientation; gyro = other.gyro; accel = other.accel; isTouching = other.isTouching; touchPos = other.touchPos; touchDown = other.touchDown; touchUp = other.touchUp; recentering = other.recentering; recentered = other.recentered; clickButtonState = other.clickButtonState; clickButtonDown = other.clickButtonDown; clickButtonUp = other.clickButtonUp; appButtonState = other.appButtonState; appButtonDown = other.appButtonDown; appButtonUp = other.appButtonUp; errorDetails = other.errorDetails; }
public void ReadState(ControllerState outState) { if (error) { outState.connectionState = GvrConnectionState.Error; outState.errorDetails = errorDetails; return; } gvr_controller_state_update(api, statePtr); outState.connectionState = ConvertConnectionState( gvr_controller_state_get_connection_state(statePtr)); gvr_quat rawOri = gvr_controller_state_get_orientation(statePtr); gvr_vec3 rawAccel = gvr_controller_state_get_accel(statePtr); gvr_vec3 rawGyro = gvr_controller_state_get_gyro(statePtr); // Convert GVR API orientation (right-handed) into Unity axis system (left-handed). pose3d.Set(Vector3.zero, new Quaternion(rawOri.x, rawOri.y, rawOri.z, rawOri.w)); pose3d.SetRightHanded(pose3d.Matrix); outState.orientation = pose3d.Orientation; // For accelerometer, we have to flip Z because the GVR API has Z pointing backwards // and Unity has Z pointing forward. outState.accel = new Vector3(rawAccel.x, rawAccel.y, -rawAccel.z); // Gyro in GVR represents a right-handed angular velocity about each axis (positive means // clockwise when sighting along axis). Since Unity uses a left-handed system, we flip the // signs to adjust the sign of the rotational velocity (so that positive means // counter-clockwise). In addition, since in Unity the Z axis points forward while GVR // has Z pointing backwards, we flip the Z axis sign again. So the result is that // we should use -X, -Y, +Z: outState.gyro = new Vector3(-rawGyro.x, -rawGyro.y, rawGyro.z); outState.isTouching = 0 != gvr_controller_state_is_touching(statePtr); gvr_vec2 touchPos = gvr_controller_state_get_touch_pos(statePtr); outState.touchPos = new Vector2(touchPos.x, touchPos.y); outState.touchDown = 0 != gvr_controller_state_get_touch_down(statePtr); outState.touchUp = 0 != gvr_controller_state_get_touch_up(statePtr); outState.appButtonDown = 0 != gvr_controller_state_get_button_down(statePtr, GVR_CONTROLLER_BUTTON_APP); outState.appButtonState = 0 != gvr_controller_state_get_button_state(statePtr, GVR_CONTROLLER_BUTTON_APP); outState.appButtonUp = 0 != gvr_controller_state_get_button_up(statePtr, GVR_CONTROLLER_BUTTON_APP); outState.clickButtonDown = 0 != gvr_controller_state_get_button_down(statePtr, GVR_CONTROLLER_BUTTON_CLICK); outState.clickButtonState = 0 != gvr_controller_state_get_button_state(statePtr, GVR_CONTROLLER_BUTTON_CLICK); outState.clickButtonUp = 0 != gvr_controller_state_get_button_up(statePtr, GVR_CONTROLLER_BUTTON_CLICK); outState.recentering = 0 != gvr_controller_state_get_recentering(statePtr); outState.recentered = 0 != gvr_controller_state_get_recentered(statePtr); }
public void ReadState(ControllerState outState) { if (error) { outState.connectionState = GvrConnectionState.Error; outState.errorDetails = errorDetails; return; } gvr_controller_read_state(api, ref state); outState.connectionState = ConvertConnectionState(state.connection_state); // Note: for accelerometer, gyro and orientation, we have to convert from the space used by // the GVR API to Unity space. They are different. // GVR API: X = right, Y = up, Z = back, right-handed. // Unity: X = right, Y = up, Z = forward, left-handed // // So for orientation and gyro, we must invert the signs of X, Y, and Z due to chiral // conversion, and then must flip Z because of the difference in the Z axis direction. // So, in the end, the conversion is: -x, -y, z. // // For the accelerometer, there is no chirality conversion because it doesn't express // a rotation. But we still need to flip Z. outState.accel = new Vector3(state.accel.x, state.accel.y, -state.accel.z); outState.gyro = new Vector3(-state.gyro.x, -state.gyro.y, state.gyro.z); outState.orientation = new Quaternion( -state.orientation.x, -state.orientation.y, state.orientation.z, state.orientation.w); outState.isTouching = 0 != state.is_touching; outState.touchPos = new Vector2(state.touch_pos.x, state.touch_pos.y); outState.touchDown = 0 != state.touch_down; outState.touchUp = 0 != state.touch_up; outState.appButtonDown = 0 != state.button_down[GVR_CONTROLLER_BUTTON_APP]; outState.appButtonState = 0 != state.button_state[GVR_CONTROLLER_BUTTON_APP]; outState.appButtonUp = 0 != state.button_up[GVR_CONTROLLER_BUTTON_APP]; outState.clickButtonDown = 0 != state.button_down[GVR_CONTROLLER_BUTTON_CLICK]; outState.clickButtonState = 0 != state.button_state[GVR_CONTROLLER_BUTTON_CLICK]; outState.clickButtonUp = 0 != state.button_up[GVR_CONTROLLER_BUTTON_CLICK]; outState.recentering = 0 != state.recentering; outState.recentered = 0 != state.recentered; }
public void ReadState(ControllerState outState) { outState.CopyFrom(dummyState); }
/// <summary>Reads a single controller's state for this frame.</summary> /// <param name="outState">The controller to write data to.</param> /// <param name="controller_id">The controller id to fetch data for.</param> public void ReadState(ControllerState outState, int controller_id) { if (controller_id >= MAX_NUM_CONTROLLERS) { return; } ReadControllerState(out nativeControllerState, controller_id); outState.connectionState = nativeControllerState.connectionState; outState.gyro = new Vector3(-nativeControllerState.gyro.x, -nativeControllerState.gyro.y, nativeControllerState.gyro.z); outState.accel = new Vector3(nativeControllerState.accel.x, nativeControllerState.accel.y, -nativeControllerState.accel.z); outState.touchPos = nativeControllerState.touchPos; outState.batteryLevel = (GvrControllerBatteryLevel)nativeControllerState.batteryLevel; outState.isCharging = nativeControllerState.isCharging; outState.recentered = nativeControllerState.isRecentered; outState.orientation = new Quaternion( -nativeControllerState.orientation.y, -nativeControllerState.orientation.z, nativeControllerState.orientation.w, nativeControllerState.orientation.x); outState.position = new Vector3(nativeControllerState.position.x, nativeControllerState.position.y, -nativeControllerState.position.z); outState.is6DoF = outState.position != Vector3.zero; outState.buttonsState = 0; if (nativeControllerState.appButtonState) { outState.buttonsState |= GvrControllerButton.App; } if (nativeControllerState.clickButtonState) { outState.buttonsState |= GvrControllerButton.TouchPadButton; } if (nativeControllerState.homeButtonState) { outState.buttonsState |= GvrControllerButton.System; } if (nativeControllerState.isTouching) { outState.buttonsState |= GvrControllerButton.TouchPadTouch; } if (nativeControllerState.triggerButtonState) { outState.buttonsState |= GvrControllerButton.Trigger; } if (nativeControllerState.gripButtonState) { outState.buttonsState |= GvrControllerButton.Grip; } outState.SetButtonsUpDownFromPrevious(prevButtonsState[controller_id]); prevButtonsState[controller_id] = outState.buttonsState; }
public void ReadState(ControllerState outState, int controller_id) { if (error) { outState.connectionState = GvrConnectionState.Error; outState.apiStatus = GvrControllerApiStatus.Error; outState.errorDetails = errorDetails; return; } if (api == IntPtr.Zero || statePtr == IntPtr.Zero) { Debug.LogError("AndroidNativeControllerProvider used after dispose."); return; } gvr_controller_state_update(api, controller_id, statePtr); outState.connectionState = ConvertConnectionState( gvr_controller_state_get_connection_state(statePtr)); outState.apiStatus = ConvertControllerApiStatus( gvr_controller_state_get_api_status(statePtr)); gvr_quat rawOri = gvr_controller_state_get_orientation(statePtr); gvr_vec3 rawAccel = gvr_controller_state_get_accel(statePtr); gvr_vec3 rawGyro = gvr_controller_state_get_gyro(statePtr); gvr_vec3 rawPos = gvr_controller_state_get_position(statePtr); // Convert GVR API orientation (right-handed) into Unity axis system (left-handed). pose3d.Set(new Vector3(rawPos.x, rawPos.y, rawPos.z), new Quaternion(rawOri.x, rawOri.y, rawOri.z, rawOri.w)); pose3d.SetRightHanded(pose3d.Matrix); outState.orientation = pose3d.Orientation; outState.position = pose3d.Position; // For accelerometer, we have to flip Z because the GVR API has Z pointing backwards // and Unity has Z pointing forward. outState.accel = new Vector3(rawAccel.x, rawAccel.y, -rawAccel.z); // Gyro in GVR represents a right-handed angular velocity about each axis (positive means // clockwise when sighting along axis). Since Unity uses a left-handed system, we flip the // signs to adjust the sign of the rotational velocity (so that positive means // counter-clockwise). In addition, since in Unity the Z axis points forward while GVR // has Z pointing backwards, we flip the Z axis sign again. So the result is that // we should use -X, -Y, +Z: outState.gyro = new Vector3(-rawGyro.x, -rawGyro.y, rawGyro.z); gvr_vec2 touchPos = gvr_controller_state_get_touch_pos(statePtr); outState.touchPos = new Vector2(touchPos.x, touchPos.y); outState.buttonsState = 0; for (int i = 0; i < GVR_BUTTONS.Length; i++) { if (0 != gvr_controller_state_get_button_state(statePtr, GVR_BUTTONS[i])) { outState.buttonsState |= GVR_UNITY_BUTTONS[i]; } } if (0 != gvr_controller_state_is_touching(statePtr)) { outState.buttonsState |= GvrControllerButton.TouchPadTouch; } outState.SetButtonsUpDownFromPrevious(lastButtonsState[controller_id]); lastButtonsState[controller_id] = outState.buttonsState; outState.recentered = 0 != gvr_controller_state_get_recentered(statePtr); outState.gvrPtr = statePtr; if (hasBatteryMethods) { outState.isCharging = 0 != gvr_controller_state_get_battery_charging(statePtr); outState.batteryLevel = (GvrControllerBatteryLevel)gvr_controller_state_get_battery_level(statePtr); } }
public void ReadState(ControllerState outState, int controller_id) { if (error) { outState.connectionState = GvrConnectionState.Error; outState.apiStatus = GvrControllerApiStatus.Error; outState.errorDetails = errorDetails; return; } IntPtr statePtr = statePtr_arr[controller_id]; gvr_controller_state_update(api, controller_id, statePtr); //Debug.Log("Reading state for controller<<>> " + controller_id.ToString()); //Debug.Log("state pointer<<>> " +statePtr.ToString()); int controller_count = gvr_controller_get_count(api); //Debug.Log("controller_count<<>> " +controller_count.ToString()); outState.connectionState = ConvertConnectionState( gvr_controller_state_get_connection_state(statePtr)); outState.apiStatus = ConvertControllerApiStatus( gvr_controller_state_get_api_status(statePtr)); gvr_quat rawOri = gvr_controller_state_get_orientation(statePtr); gvr_vec3 rawAccel = gvr_controller_state_get_accel(statePtr); gvr_vec3 rawGyro = gvr_controller_state_get_gyro(statePtr); // saketp gvr_vec3 rawPos = gvr_controller_state_get_position(statePtr); // Convert GVR API orientation (right-handed) into Unity axis system (left-handed). // saketp pose3d.Set(new Vector3(rawPos.x, rawPos.y, rawPos.z), new Quaternion(rawOri.x, rawOri.y, rawOri.z, rawOri.w)); pose3d.SetRightHanded(pose3d.Matrix); outState.orientation = pose3d.Orientation; // saketp outState.position = pose3d.Position; //Debug.Log("PositionGVR<<>> " + outState.position.ToString()); //Debug.Log("PositionGVR<<>> " + outState.position.ToString()); // Debug.Log("OrientationGVR<<>> " + outState.orientation.ToString()); // For accelerometer, we have to flip Z because the GVR API has Z pointing backwards // and Unity has Z pointing forward. outState.accel = new Vector3(rawAccel.x, rawAccel.y, -rawAccel.z); // Gyro in GVR represents a right-handed angular velocity about each axis (positive means // clockwise when sighting along axis). Since Unity uses a left-handed system, we flip the // signs to adjust the sign of the rotational velocity (so that positive means // counter-clockwise). In addition, since in Unity the Z axis points forward while GVR // has Z pointing backwards, we flip the Z axis sign again. So the result is that // we should use -X, -Y, +Z: outState.gyro = new Vector3(-rawGyro.x, -rawGyro.y, rawGyro.z); // statePtr = statePtr_arr[0]; outState.isTouching = 0 != gvr_controller_state_is_touching(statePtr); gvr_vec2 touchPos = gvr_controller_state_get_touch_pos(statePtr); outState.touchPos = new Vector2(touchPos.x, touchPos.y); outState.touchDown = 0 != gvr_controller_state_get_touch_down(statePtr); outState.touchUp = 0 != gvr_controller_state_get_touch_up(statePtr); outState.appButtonDown = 0 != gvr_controller_state_get_button_down(statePtr, GVR_CONTROLLER_BUTTON_APP); outState.appButtonState = 0 != gvr_controller_state_get_button_state(statePtr, GVR_CONTROLLER_BUTTON_APP); outState.appButtonUp = 0 != gvr_controller_state_get_button_up(statePtr, GVR_CONTROLLER_BUTTON_APP); outState.homeButtonDown = 0 != gvr_controller_state_get_button_down(statePtr, GVR_CONTROLLER_BUTTON_HOME); outState.homeButtonState = 0 != gvr_controller_state_get_button_state(statePtr, GVR_CONTROLLER_BUTTON_HOME); outState.clickButtonDown = 0 != gvr_controller_state_get_button_down(statePtr, GVR_CONTROLLER_BUTTON_CLICK); outState.clickButtonState = 0 != gvr_controller_state_get_button_state(statePtr, GVR_CONTROLLER_BUTTON_CLICK); outState.clickButtonUp = 0 != gvr_controller_state_get_button_up(statePtr, GVR_CONTROLLER_BUTTON_CLICK); outState.triggerButtonDown = 0 != gvr_controller_state_get_button_down(statePtr, GVR_CONTROLLER_BUTTON_TRIGGER); outState.triggerButtonState = 0 != gvr_controller_state_get_button_state(statePtr, GVR_CONTROLLER_BUTTON_TRIGGER); outState.triggerButtonUp = 0 != gvr_controller_state_get_button_up(statePtr, GVR_CONTROLLER_BUTTON_TRIGGER); outState.recentering = 0 != gvr_controller_state_get_recentering(statePtr); outState.recentered = 0 != gvr_controller_state_get_recentered(statePtr); outState.gvrPtr = statePtr; if (hasBatteryMethods) { outState.isCharging = 0 != gvr_controller_state_get_battery_charging(statePtr); outState.batteryLevel = (GvrControllerBatteryLevel)gvr_controller_state_get_battery_level(statePtr); } }
// this is called every frame void IControllerProvider.ReadState(ControllerState outState) { lock (state) { if (ctrl != null) { XDevicePlugin.UpdateInputState(handle); XDevicePlugin.GetInputState(handle, ref m_leftControllerState); state.orientation = new Quaternion( -m_leftControllerState.rotation[0], -m_leftControllerState.rotation[1], m_leftControllerState.rotation[2], m_leftControllerState.rotation[3] ); state.gyro = new Vector3( -m_leftControllerState.gyroscope[0], -m_leftControllerState.gyroscope[1], m_leftControllerState.gyroscope[2] ); state.accel = new Vector3( m_leftControllerState.accelerometer[0], m_leftControllerState.accelerometer[1], -m_leftControllerState.accelerometer[2] ); state.touchPos = ctrl.touchPos; // GVR Hack Detection Controller if (ctrl.connectionState == DeviceConnectionState.Connected) { state.connectionState = GvrConnectionState.Connected; } else if (ctrl.connectionState == DeviceConnectionState.Connecting) { state.connectionState = GvrConnectionState.Connecting; } else { state.connectionState = GvrConnectionState.Disconnected; } // GVR Input Mapping state.apiStatus = GvrControllerApiStatus.Ok; state.appButtonState = ctrl.GetButton(XimmerseButton.App); state.appButtonDown = ctrl.GetButtonDown(XimmerseButton.App); state.appButtonUp = ctrl.GetButtonUp(XimmerseButton.App); state.homeButtonDown = ctrl.GetButtonDown(XimmerseButton.Home); state.homeButtonState = ctrl.GetButton(XimmerseButton.Home); state.clickButtonDown = ctrl.GetButtonDown(XimmerseButton.Click) || ctrl.GetButtonDown(XimmerseButton.Trigger); state.clickButtonState = ctrl.GetButton(XimmerseButton.Click) || ctrl.GetButton(XimmerseButton.Trigger); state.clickButtonUp = ctrl.GetButtonUp(XimmerseButton.Click) || ctrl.GetButtonUp(XimmerseButton.Trigger); // GVR Battery Indicator if (ctrl.batteryLevel > 80) { state.batteryLevel = GvrControllerBatteryLevel.Full; } if (ctrl.batteryLevel > 60 && ctrl.batteryLevel <= 80) { state.batteryLevel = GvrControllerBatteryLevel.AlmostFull; } if (ctrl.batteryLevel > 40 && ctrl.batteryLevel <= 60) { state.batteryLevel = GvrControllerBatteryLevel.Medium; } if (ctrl.batteryLevel > 20 && ctrl.batteryLevel <= 40) { state.batteryLevel = GvrControllerBatteryLevel.Low; } if (ctrl.batteryLevel >= 0 && ctrl.batteryLevel <= 20) { state.batteryLevel = GvrControllerBatteryLevel.CriticalLow; } // GVR Recenter Touchpad Detection if (ctrl.GetButtonDown(ControllerButton.PrimaryThumbMove) || ctrl.GetButtonDown(XimmerseButton.Click)) { state.touchDown = true; state.isTouching = true; } if (ctrl.GetButton(ControllerButton.PrimaryThumbMove) || ctrl.GetButton(XimmerseButton.Click)) { state.isTouching = true; } if (ctrl.GetButtonUp(ControllerButton.PrimaryThumbMove) || ctrl.GetButtonUp(XimmerseButton.Click)) { state.touchUp = true; state.isTouching = false; } // GVR Recenter Interactions state.gvrPtr = IntPtr.Zero; if (ctrl.GetButtonUp(XimmerseButton.Home)) { GvrCardboardHelpers.Recenter(); ctrl.Recenter(); state.recentered = true; } } else { if (EnableXdevice == false && FlipAppInstalled() == true) { EnableXdevice = true; XDevicePlugin.Init(); handle = XDevicePlugin.GetInputDeviceHandle("XCobra-0"); ctrl = new ControllerInput(handle); } state.connectionState = GvrConnectionState.Disconnected; state.clickButtonState = false; state.clickButtonDown = false; state.clickButtonUp = false; state.appButtonState = false; state.appButtonDown = false; state.appButtonUp = false; } outState.CopyFrom(state); } state.ClearTransientState(); }