123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620 |
-
- namespace NRKernal
- {
- using System;
- using System.Runtime.InteropServices;
- using UnityEngine;
-
- internal partial class NativeController
- {
-
- private UInt64 m_ControllerHandle = 0;
-
- private UInt64[] m_StateHandles = new UInt64[NRInput.MAX_CONTROLLER_STATE_COUNT] { 0, 0 };
-
-
- public bool Init()
- {
- NRDebugger.Debug("[NativeController] Init");
- NativeResult result = NativeApi.NRControllerCreate(ref m_ControllerHandle);
- if (result == NativeResult.Success)
- {
-
- NativeApi.NRControllerStart(m_ControllerHandle);
- int count = GetControllerCount();
- NRDebugger.Debug("[NativeController] Get controller count:" + count);
- for (int i = 0; i < count; i++)
- {
- result = NativeApi.NRControllerStateCreate(m_ControllerHandle, i, ref m_StateHandles[i]);
- if (result != NativeResult.Success)
- {
- NRDebugger.Error("[NativeController] Create Failed!" + result.ToString());
- return false;
- }
- }
- NRDebugger.Debug("[NativeController] Created Successed");
- return true;
- }
- NRDebugger.Error("[NativeController] Create Failed!");
- m_ControllerHandle = 0;
- return false;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public int GetControllerCount()
- {
- if (m_ControllerHandle == 0)
- {
- return 0;
- }
- int count = 0;
- if (NativeApi.NRControllerGetCount(m_ControllerHandle, ref count) != NativeResult.Success)
- NRDebugger.Error("Get Controller Count Failed!");
- return Mathf.Min(count, m_StateHandles.Length);
- }
-
- public void Pause()
- {
- if (m_ControllerHandle == 0)
- {
- return;
- }
- NativeApi.NRControllerPause(m_ControllerHandle);
- }
-
- public void Resume()
- {
- if (m_ControllerHandle == 0)
- {
- return;
- }
- NativeApi.NRControllerResume(m_ControllerHandle);
- }
-
- public void Stop()
- {
- if (m_ControllerHandle == 0)
- {
- return;
- }
- NativeApi.NRControllerStop(m_ControllerHandle);
- }
-
- public void Destroy()
- {
- if (m_ControllerHandle == 0)
- {
- return;
- }
- Stop();
- NativeApi.NRControllerDestroy(m_ControllerHandle);
- }
-
-
-
- public uint GetAvailableFeatures(int controllerIndex)
- {
- if (m_ControllerHandle == 0)
- {
- return 0;
- }
- uint availableFeature = 0;
- NativeApi.NRControllerGetAvailableFeatures(m_ControllerHandle, controllerIndex, ref availableFeature);
- return availableFeature;
- }
-
-
-
- public ControllerType GetControllerType(int controllerIndex)
- {
- if (m_ControllerHandle == 0)
- {
- return ControllerType.CONTROLLER_TYPE_UNKNOWN;
- }
- ControllerType controllerType = ControllerType.CONTROLLER_TYPE_UNKNOWN;
- NativeApi.NRControllerGetType(m_ControllerHandle, controllerIndex, ref controllerType);
- return controllerType;
- }
-
-
- public void RecenterController(int controllerIndex)
- {
- if (m_ControllerHandle == 0)
- {
- return;
- }
- NativeApi.NRControllerRecenter(m_ControllerHandle, controllerIndex);
- }
-
-
-
-
-
- public void TriggerHapticVibrate(int controllerIndex, Int64 duration, float frequency, float amplitude)
- {
- if (m_ControllerHandle == 0)
- {
- return;
- }
- NativeApi.NRControllerHapticVibrate(m_ControllerHandle, controllerIndex, duration, frequency, amplitude);
- }
-
-
-
- public bool UpdateState(int controllerIndex)
- {
- if (m_ControllerHandle == 0)
- {
- return false;
- }
- if (m_StateHandles[controllerIndex] == 0)
- NativeApi.NRControllerStateCreate(m_ControllerHandle, controllerIndex, ref m_StateHandles[controllerIndex]);
- if (m_StateHandles[controllerIndex] == 0)
- return false;
- NativeResult result = NativeApi.NRControllerStateUpdate(m_StateHandles[controllerIndex]);
- return result == NativeResult.Success;
- }
-
-
- public void DestroyState(int controllerIndex)
- {
- NativeApi.NRControllerStateDestroy(m_StateHandles[controllerIndex]);
- }
-
-
-
- public ControllerConnectionState GetConnectionState(int controllerIndex)
- {
- ControllerConnectionState state = ControllerConnectionState.CONTROLLER_CONNECTION_STATE_NOT_INITIALIZED;
- NativeApi.NRControllerStateGetConnectionState(m_StateHandles[controllerIndex], ref state);
- return state;
- }
-
-
-
- public int GetBatteryLevel(int controllerIndex)
- {
- int batteryLevel = -1;
- NativeApi.NRControllerStateGetBatteryLevel(m_StateHandles[controllerIndex], ref batteryLevel);
- return batteryLevel;
- }
-
-
-
- public bool IsCharging(int controllerIndex)
- {
- int isCharging = 0;
- NativeApi.NRControllerStateGetCharging(m_StateHandles[controllerIndex], ref isCharging);
- return isCharging == 1;
- }
-
-
-
- public Pose GetPose(int controllerIndex)
- {
- Pose controllerPos = Pose.identity;
- NativeMat4f mat4f = new NativeMat4f(Matrix4x4.identity);
- NativeResult result = NativeApi.NRControllerStateGetPose(m_StateHandles[controllerIndex], ref mat4f);
- if (result == NativeResult.Success)
- ConversionUtility.ApiPoseToUnityPose(mat4f, out controllerPos);
- return controllerPos;
- }
-
-
-
- public Vector3 GetGyro(int controllerIndex)
- {
- NativeVector3f vec3f = new NativeVector3f();
- NativeResult result = NativeApi.NRControllerStateGetGyro(m_StateHandles[controllerIndex], ref vec3f);
- if (result == NativeResult.Success)
- return vec3f.ToUnityVector3();
- return Vector3.zero;
- }
-
-
-
- public Vector3 GetAccel(int controllerIndex)
- {
- NativeVector3f vec3f = new NativeVector3f();
- NativeResult result = NativeApi.NRControllerStateGetAccel(m_StateHandles[controllerIndex], ref vec3f);
- if (result == NativeResult.Success)
- return vec3f.ToUnityVector3();
- return Vector3.zero;
- }
-
-
-
- public Vector3 GetMag(int controllerIndex)
- {
- NativeVector3f vec3f = new NativeVector3f();
- NativeResult result = NativeApi.NRControllerStateGetMag(m_StateHandles[controllerIndex], ref vec3f);
- if (result == NativeResult.Success)
- return vec3f.ToUnityVector3();
- return Vector3.zero;
- }
-
-
-
- public uint GetButtonState(int controllerIndex)
- {
- uint buttonPress = 0;
- NativeApi.NRControllerStateGetButtonState(m_StateHandles[controllerIndex], ref buttonPress);
- return buttonPress;
- }
-
-
-
- public uint GetButtonUp(int controllerIndex)
- {
- uint buttonUp = 0;
- NativeApi.NRControllerStateGetButtonUp(m_StateHandles[controllerIndex], ref buttonUp);
- return buttonUp;
- }
-
-
-
- public uint GetButtonDown(int controllerIndex)
- {
- uint buttonDown = 0;
- NativeApi.NRControllerStateGetButtonDown(m_StateHandles[controllerIndex], ref buttonDown);
- return buttonDown;
- }
-
-
-
- public bool IsTouching(int controllerIndex)
- {
- uint touchState = 0;
- NativeApi.NRControllerStateTouchState(m_StateHandles[controllerIndex], ref touchState);
- return touchState == 1;
- }
-
-
-
- public bool GetTouchUp(int controllerIndex)
- {
- uint touchUp = 0;
- NativeApi.NRControllerStateGetTouchUp(m_StateHandles[controllerIndex], ref touchUp);
- return touchUp == 1;
- }
-
-
-
- public bool GetTouchDown(int controllerIndex)
- {
- uint touchDown = 0;
- NativeApi.NRControllerStateGetTouchDown(m_StateHandles[controllerIndex], ref touchDown);
- return touchDown == 1;
- }
-
-
-
- public Vector2 GetTouch(int controllerIndex)
- {
- NativeVector2f touchPos = new NativeVector2f();
- NativeResult result = NativeApi.NRControllerStateGetTouchPose(m_StateHandles[controllerIndex], ref touchPos);
- if (result == NativeResult.Success)
- return touchPos.ToUnityVector2();
- return Vector3.zero;
- }
-
-
- public void UpdateHeadPose(Pose hmdPose)
- {
- NativeMat4f apiPose;
- ConversionUtility.UnityPoseToApiPose(hmdPose, out apiPose);
- NativeApi.NRControllerSetHeadPose(m_ControllerHandle, ref apiPose);
- }
-
-
-
- public string GetVersion(int controllerIndex)
- {
- if (m_ControllerHandle == 0)
- {
- return "";
- }
- byte[] bytes = new byte[128];
- var result = NativeApi.NRControllerGetVersion(m_ControllerHandle, controllerIndex, bytes, bytes.Length);
- if (result == NativeResult.Success)
- {
- return System.Text.Encoding.ASCII.GetString(bytes, 0, bytes.Length);
- }
- else
- {
- return "";
- }
- }
- public HandednessType GetHandednessType()
- {
- HandednessType handedness_type = HandednessType.RIGHT_HANDEDNESS;
- var result = NativeApi.NRControllerGetHandednessType(m_ControllerHandle, ref handedness_type);
- NativeErrorListener.Check(result, this, "GetHandednessType");
- return handedness_type;
- }
- private partial struct NativeApi
- {
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerCreate(ref UInt64 out_controller_handle);
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStart(UInt64 controller_handle);
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerPause(UInt64 controller_handle);
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerResume(UInt64 controller_handle);
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStop(UInt64 controller_handle);
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerDestroy(UInt64 controller_handle);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerGetCount(UInt64 controller_handle, ref int out_controller_count);
-
-
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerGetAvailableFeatures(UInt64 controller_handle, int controller_index, ref uint out_controller_available_features);
-
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerGetType(UInt64 controller_handle, int controller_index, ref ControllerType out_controller_type);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerRecenter(UInt64 controller_handle, int controller_index);
-
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStateCreate(UInt64 controller_handle, int controller_index, ref UInt64 out_controller_state_handle);
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStateUpdate(UInt64 controller_state_handle);
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStateDestroy(UInt64 controller_state_handle);
-
-
-
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerHapticVibrate(UInt64 controller_handle, int controller_index, Int64 duration, float frequency, float amplitude);
-
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStateGetConnectionState(UInt64 controller_state_handle, ref ControllerConnectionState out_controller_connection_state);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStateGetBatteryLevel(UInt64 controller_state_handle, ref int out_controller_battery_level);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStateGetCharging(UInt64 controller_state_handle, ref int out_controller_charging);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStateGetPose(UInt64 controller_state_handle, ref NativeMat4f out_controller_pose);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStateGetGyro(UInt64 controller_state_handle, ref NativeVector3f out_controller_gyro);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStateGetAccel(UInt64 controller_state_handle, ref NativeVector3f out_controller_accel);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStateGetMag(UInt64 controller_state_handle, ref NativeVector3f out_controller_mag);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStateGetButtonState(UInt64 controller_state_handle, ref uint out_controller_button_state);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStateGetButtonUp(UInt64 controller_state_handle, ref uint out_controller_button_up);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStateGetButtonDown(UInt64 controller_state_handle, ref uint out_controller_button_down);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStateTouchState(UInt64 controller_state_handle, ref uint out_controller_touch_state);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStateGetTouchUp(UInt64 controller_state_handle, ref uint out_controller_touch_up);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStateGetTouchDown(UInt64 controller_state_handle, ref uint out_controller_touch_down);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerStateGetTouchPose(UInt64 controller_state_handle, ref NativeVector2f out_controller_touch_pose);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerSetHeadPose(UInt64 controller_handle, ref NativeMat4f out_controller_pose);
-
-
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerGetVersion(UInt64 controller_handle, int controller_index, byte[] out_version, int len);
-
-
-
-
- [DllImport(NativeConstants.NRNativeLibrary)]
- public static extern NativeResult NRControllerGetHandednessType(UInt64 controller_handle, ref HandednessType handedness_type);
- };
- }
- }
|