Пример #1
0
        public void ReadState(ControllerState outState, int controller_id)
        {
            if (controller_id != 0)
            {
                return;
            }
            lock (state) {
                state.connectionState = GvrConnectionState.Connected;
                if (!EmulatorManager.Instance.Connected)
                {
                    state.connectionState = EmulatorManager.Instance.Connecting ?
                                            GvrConnectionState.Connecting : GvrConnectionState.Disconnected;
                }
                state.apiStatus = EmulatorManager.Instance.Connected ? GvrControllerApiStatus.Ok :
                                  GvrControllerApiStatus.Unavailable;

                // During emulation, just assume the controller is fully charged
                state.isCharging   = false;
                state.batteryLevel = GvrControllerBatteryLevel.Full;

                state.SetButtonsUpDownFromPrevious(lastButtonsState);
                lastButtonsState = state.buttonsState;

                outState.CopyFrom(state);
            }
            state.ClearTransientState();
        }
Пример #2
0
        private void UpdateButtonStates()
        {
            state.buttonsState = 0;
            if (IsClickButtonPressed)
            {
                state.buttonsState |= GvrControllerButton.TouchPadButton;
            }
            if (IsAppButtonPressed)
            {
                state.buttonsState |= GvrControllerButton.App;
            }
            if (IsHomeButtonPressed)
            {
                state.buttonsState |= GvrControllerButton.System;
            }
            if (IsTouching)
            {
                state.buttonsState |= GvrControllerButton.TouchPadTouch;
            }

            state.SetButtonsUpDownFromPrevious(lastButtonsState);
            lastButtonsState = state.buttonsState;

            if (0 != (state.buttonsUp & GvrControllerButton.TouchPadTouch))
            {
                ClearTouchPos();
            }

            if (0 != (state.buttonsUp & GvrControllerButton.System))
            {
                Recenter();
            }
        }
Пример #3
0
        private void UpdateButtonStates()
        {
            state.buttonsState = 0;
            if (IsTouchPadButtonPressed)
            {
                state.buttonsState |= GvrControllerButton.TouchPadButton;
            }

            if (IsGripButtonPressed)
            {
                state.buttonsState |= GvrControllerButton.Grip;
            }

            if (IsTriggerButtonPressed)
            {
                state.buttonsState |= GvrControllerButton.Trigger;
            }

            if (IsAppButtonPressed)
            {
                state.buttonsState |= GvrControllerButton.App;
            }

            if (IsSystemButtonPressed)
            {
                state.buttonsState |= GvrControllerButton.System;
            }

            if (IsTouching & !scrollPause)
            {
                state.buttonsState |= GvrControllerButton.TouchPadTouch;
            }

            state.SetButtonsUpDownFromPrevious(lastButtonsState);
            lastButtonsState = state.buttonsState;

            if (0 != (state.buttonsUp & GvrControllerButton.TouchPadTouch))
            {
                ClearTouchPos();
            }

            if (0 != (state.buttonsUp & GvrControllerButton.System))
            {
                Recenter();
            }
        }
Пример #4
0
        public void ReadState(ControllerState outState)
        {
            var nativeControllerState = new NativeControllerState();

            ReadControllerState(out nativeControllerState);

            outState.connectionState = nativeControllerState.connectionState;
            outState.orientation     = new Quaternion(
                -nativeControllerState.orientation.y,
                -nativeControllerState.orientation.z,
                nativeControllerState.orientation.w,
                nativeControllerState.orientation.x);

            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.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;
            }

            outState.SetButtonsUpDownFromPrevious(prevButtonsState);
            prevButtonsState = outState.buttonsState;
        }
Пример #5
0
        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);

            int[] gvr_buttons = new int[] {
                GVR_CONTROLLER_BUTTON_APP,
                GVR_CONTROLLER_BUTTON_HOME,
                GVR_CONTROLLER_BUTTON_CLICK,
                GVR_CONTROLLER_BUTTON_RESERVED0,
                GVR_CONTROLLER_BUTTON_RESERVED1,
                GVR_CONTROLLER_BUTTON_RESERVED2
            };
            GvrControllerButton[] gvrUnityButtons = new GvrControllerButton[] {
                GvrControllerButton.App,
                GvrControllerButton.System,
                GvrControllerButton.TouchPadButton,
                GvrControllerButton.Reserved0,
                GvrControllerButton.Reserved1,
                GvrControllerButton.Reserved2
            };

            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 |= gvrUnityButtons[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);
            }
        }
Пример #6
0
        /// <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;
        }
        /// Reads the controller's current state and stores it in outState.
        public void ReadState(ControllerState outState, int controller_id)
        {
            int retval = 0;

            if (lastUpdateFrame != Time.frameCount)
            {
                lastUpdateFrame = Time.frameCount;

                retval = GvrShimUnity_updateState();
                if (retval != GVR_SHIM_OK)
                {
                    Debug.LogError("GvrShimUnity_updateState returned error.");
                    return;
                }
            }

            int connStatus = 0;

            retval = GvrShimUnity_getControllerConnectionStatus(controller_id, ref connStatus);
            if (retval != GVR_SHIM_OK)
            {
                Debug.LogError("GvrShimUnity_getControllerConnectionStatus returned error.");
                return;
            }
            outState.connectionState = ConvertConnectionState(connStatus);

            // Early out if not connected.  No other state is relevant.
            if (outState.connectionState != GvrConnectionState.Connected)
            {
                return;
            }

            GvrShimUnityControllerState aState = new GvrShimUnityControllerState();

            retval = GvrShimUnity_getControllerState(controller_id, ref aState);
            if (retval != GVR_SHIM_OK)
            {
                Debug.LogError("GvrShimUnity_getControllerState returned error.");
                return;
            }

            // Convert GVR API orientation (right-handed) into Unity axis system (left-handed).
            pose3d.Set(aState.position, aState.orientation);
            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    = aState.acceleration;
            outState.accel.z *= -1;

            // 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 = aState.gyro;
            outState.gyro.Scale(new Vector3(-1, -1, 1));

            outState.touchPos = aState.touchPos;
            // Shim outputs centered touchpos coordinates, but the ControllerState struct is
            // top-left coordinates. Convert back to top-left.
            outState.touchPos.x = (outState.touchPos.x / 2.0f) + 0.5f;
            outState.touchPos.y = (-outState.touchPos.y / 2.0f) + 0.5f;

            outState.is6DoF = (aState.trackedDataAvailable &
                               (int)GvrShimTrackedDataAvailableFlags.PositionAvailable) != 0;
            outState.buttonsState = (GvrControllerButton)aState.buttonState;
            // Derive button up/down state from Unity perspective, as it can miss
            // up/downs from platform, like on pause/resume.
            if (controller_id >= 0 && controller_id < lastButtonStates.Length)
            {
                outState.SetButtonsUpDownFromPrevious(lastButtonStates[controller_id]);
                lastButtonStates[controller_id] = outState.buttonsState;
            }
            outState.recentered   = (aState.recentered != 0);
            outState.isCharging   = (aState.batteryCharging != 0);
            outState.batteryLevel = (GvrControllerBatteryLevel)aState.batteryLevel;
        }