12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652 |
-
- using UnityEngine;
- using Valve.VR;
- using System.IO;
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Linq;
- using Valve.Newtonsoft.Json;
- using System.Text;
- namespace Valve.VR
- {
- public partial class SteamVR_Input
- {
- public const string defaultInputGameObjectName = "[SteamVR Input]";
- private const string localizationKeyName = "localization";
-
- public static bool fileInitialized = false;
-
- public static bool initialized = false;
-
- public static bool preInitialized = false;
-
- public static SteamVR_Input_ActionFile actionFile;
-
- public static string actionFileHash;
-
- public static event Action onNonVisualActionsUpdated;
-
- public static event PosesUpdatedHandler onPosesUpdated;
- public delegate void PosesUpdatedHandler(bool skipSendingEvents);
-
- public static event SkeletonsUpdatedHandler onSkeletonsUpdated;
- public delegate void SkeletonsUpdatedHandler(bool skipSendingEvents);
- protected static bool initializing = false;
- protected static int startupFrame = 0;
- public static bool isStartupFrame
- {
- get
- {
- return Time.frameCount >= (startupFrame - 1) && Time.frameCount <= (startupFrame + 1);
- }
- }
- #region array accessors
-
- public static SteamVR_ActionSet[] actionSets;
-
- public static SteamVR_Action[] actions;
-
- public static ISteamVR_Action_In[] actionsIn;
-
- public static ISteamVR_Action_Out[] actionsOut;
-
- public static SteamVR_Action_Boolean[] actionsBoolean;
-
- public static SteamVR_Action_Single[] actionsSingle;
-
- public static SteamVR_Action_Vector2[] actionsVector2;
-
- public static SteamVR_Action_Vector3[] actionsVector3;
-
- public static SteamVR_Action_Pose[] actionsPose;
-
- public static SteamVR_Action_Skeleton[] actionsSkeleton;
-
- public static SteamVR_Action_Vibration[] actionsVibration;
-
- public static ISteamVR_Action_In[] actionsNonPoseNonSkeletonIn;
- protected static Dictionary<string, SteamVR_ActionSet> actionSetsByPath = new Dictionary<string, SteamVR_ActionSet>();
- protected static Dictionary<string, SteamVR_ActionSet> actionSetsByPathLowered = new Dictionary<string, SteamVR_ActionSet>();
- protected static Dictionary<string, SteamVR_Action> actionsByPath = new Dictionary<string, SteamVR_Action>();
- protected static Dictionary<string, SteamVR_Action> actionsByPathLowered = new Dictionary<string, SteamVR_Action>();
- protected static Dictionary<string, SteamVR_ActionSet> actionSetsByPathCache = new Dictionary<string, SteamVR_ActionSet>();
- protected static Dictionary<string, SteamVR_Action> actionsByPathCache = new Dictionary<string, SteamVR_Action>();
- protected static Dictionary<string, SteamVR_Action> actionsByNameCache = new Dictionary<string, SteamVR_Action>();
- protected static Dictionary<string, SteamVR_ActionSet> actionSetsByNameCache = new Dictionary<string, SteamVR_ActionSet>();
- #endregion
- static SteamVR_Input()
- {
- #if !UNITY_EDITOR
-
-
-
- #endif
- FindPreinitializeMethod();
- }
- public static void ForcePreinitialize()
- {
- FindPreinitializeMethod();
- }
- private static void FindPreinitializeMethod()
- {
- Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
- for (int assemblyIndex = 0; assemblyIndex < assemblies.Length; assemblyIndex++)
- {
- Assembly assembly = assemblies[assemblyIndex];
- Type type = assembly.GetType(SteamVR_Input_Generator_Names.fullActionsClassName);
- if (type != null)
- {
- MethodInfo preinitMethodInfo = type.GetMethod(SteamVR_Input_Generator_Names.preinitializeMethodName);
- if (preinitMethodInfo != null)
- {
- preinitMethodInfo.Invoke(null, null);
- return;
- }
- }
- }
- }
-
-
-
-
-
- public static void Initialize(bool force = false)
- {
- if (initialized == true && force == false)
- return;
- #if UNITY_EDITOR
- CheckSetup();
- if (IsOpeningSetup())
- return;
- #endif
-
- initializing = true;
- startupFrame = Time.frameCount;
- SteamVR_ActionSet_Manager.Initialize();
- SteamVR_Input_Source.Initialize();
- for (int actionIndex = 0; actionIndex < actions.Length; actionIndex++)
- {
- SteamVR_Action action = actions[actionIndex];
- action.Initialize(true);
- }
- for (int actionSetIndex = 0; actionSetIndex < actionSets.Length; actionSetIndex++)
- {
- SteamVR_ActionSet set = actionSets[actionSetIndex];
- set.Initialize(true);
- }
- if (SteamVR_Settings.instance.activateFirstActionSetOnStart)
- {
- if (actionSets.Length > 0)
- actionSets[0].Activate();
- else
- {
- Debug.LogError("<b>[SteamVR]</b> No action sets to activate.");
- }
- }
- SteamVR_Action_Pose.SetTrackingUniverseOrigin(SteamVR_Settings.instance.trackingSpace);
- initialized = true;
- initializing = false;
-
- }
- public static void PreinitializeFinishActionSets()
- {
- for (int actionSetIndex = 0; actionSetIndex < actionSets.Length; actionSetIndex++)
- {
- SteamVR_ActionSet actionSet = actionSets[actionSetIndex];
- actionSet.FinishPreInitialize();
- }
- }
- public static void PreinitializeActionSetDictionaries()
- {
- actionSetsByPath.Clear();
- actionSetsByPathLowered.Clear();
- actionSetsByPathCache.Clear();
- for (int actionSetIndex = 0; actionSetIndex < actionSets.Length; actionSetIndex++)
- {
- SteamVR_ActionSet actionSet = actionSets[actionSetIndex];
- actionSetsByPath.Add(actionSet.fullPath, actionSet);
- actionSetsByPathLowered.Add(actionSet.fullPath.ToLower(), actionSet);
- }
- }
- public static void PreinitializeActionDictionaries()
- {
- actionsByPath.Clear();
- actionsByPathLowered.Clear();
- actionsByPathCache.Clear();
- for (int actionIndex = 0; actionIndex < actions.Length; actionIndex++)
- {
- SteamVR_Action action = actions[actionIndex];
- actionsByPath.Add(action.fullPath, action);
- actionsByPathLowered.Add(action.fullPath.ToLower(), action);
- }
- }
-
- public static void Update()
- {
- if (initialized == false || isStartupFrame)
- return;
- if (SteamVR.settings.IsInputUpdateMode(SteamVR_UpdateModes.OnUpdate))
- {
- UpdateNonVisualActions();
- }
- if (SteamVR.settings.IsPoseUpdateMode(SteamVR_UpdateModes.OnUpdate))
- {
- UpdateVisualActions();
- }
- }
-
-
-
-
- public static void LateUpdate()
- {
- if (initialized == false || isStartupFrame)
- return;
- if (SteamVR.settings.IsInputUpdateMode(SteamVR_UpdateModes.OnLateUpdate))
- {
- UpdateNonVisualActions();
- }
- if (SteamVR.settings.IsPoseUpdateMode(SteamVR_UpdateModes.OnLateUpdate))
- {
-
- UpdateVisualActions();
- }
- else
- {
-
- UpdateSkeletonActions(true);
- }
- }
-
- public static void FixedUpdate()
- {
- if (initialized == false || isStartupFrame)
- return;
- if (SteamVR.settings.IsInputUpdateMode(SteamVR_UpdateModes.OnFixedUpdate))
- {
- UpdateNonVisualActions();
- }
- if (SteamVR.settings.IsPoseUpdateMode(SteamVR_UpdateModes.OnFixedUpdate))
- {
- UpdateVisualActions();
- }
- }
-
- public static void OnPreCull()
- {
- if (initialized == false || isStartupFrame)
- return;
- if (SteamVR.settings.IsInputUpdateMode(SteamVR_UpdateModes.OnPreCull))
- {
- UpdateNonVisualActions();
- }
- if (SteamVR.settings.IsPoseUpdateMode(SteamVR_UpdateModes.OnPreCull))
- {
- UpdateVisualActions();
- }
- }
-
-
-
-
- public static void UpdateVisualActions(bool skipStateAndEventUpdates = false)
- {
- if (initialized == false)
- return;
- SteamVR_ActionSet_Manager.UpdateActionStates();
- UpdatePoseActions(skipStateAndEventUpdates);
- UpdateSkeletonActions(skipStateAndEventUpdates);
- }
-
-
-
-
- public static void UpdatePoseActions(bool skipSendingEvents = false)
- {
- if (initialized == false)
- return;
- for (int actionIndex = 0; actionIndex < actionsPose.Length; actionIndex++)
- {
- SteamVR_Action_Pose action = actionsPose[actionIndex];
- action.UpdateValues(skipSendingEvents);
- }
- if (onPosesUpdated != null)
- onPosesUpdated(false);
- }
-
-
-
-
- public static void UpdateSkeletonActions(bool skipSendingEvents = false)
- {
- if (initialized == false)
- return;
- for (int actionIndex = 0; actionIndex < actionsSkeleton.Length; actionIndex++)
- {
- SteamVR_Action_Skeleton action = actionsSkeleton[actionIndex];
- action.UpdateValue(skipSendingEvents);
- }
- if (onSkeletonsUpdated != null)
- onSkeletonsUpdated(skipSendingEvents);
- }
-
-
-
- public static void UpdateNonVisualActions()
- {
- if (initialized == false)
- return;
- SteamVR_ActionSet_Manager.UpdateActionStates();
- for (int actionIndex = 0; actionIndex < actionsNonPoseNonSkeletonIn.Length; actionIndex++)
- {
- ISteamVR_Action_In action = actionsNonPoseNonSkeletonIn[actionIndex];
- action.UpdateValues();
- }
- if (onNonVisualActionsUpdated != null)
- onNonVisualActionsUpdated();
- }
- private static uint sizeVRActiveActionSet_t = 0;
- protected static void ShowBindingHintsForSets(VRActiveActionSet_t[] sets, ulong highlightAction = 0)
- {
- if (sizeVRActiveActionSet_t == 0)
- sizeVRActiveActionSet_t = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VRActiveActionSet_t));
- OpenVR.Input.ShowBindingsForActionSet(sets, sizeVRActiveActionSet_t, highlightAction);
- }
- private static VRActiveActionSet_t[] setCache = new VRActiveActionSet_t[1];
-
-
-
-
- public static bool ShowBindingHints(ISteamVR_Action_In originToHighlight)
- {
- if (originToHighlight != null)
- {
- setCache[0].ulActionSet = originToHighlight.actionSet.handle;
- ShowBindingHintsForSets(setCache, originToHighlight.activeOrigin);
- return true;
- }
- return false;
- }
-
-
-
- public static bool ShowBindingHints(ISteamVR_ActionSet setToShow)
- {
- if (setToShow != null)
- {
- setCache[0].ulActionSet = setToShow.handle;
- ShowBindingHintsForSets(setCache, 0);
- return true;
- }
- return false;
- }
-
-
-
-
- public static void ShowBindingHintsForActiveActionSets(ulong highlightAction = 0)
- {
- if (sizeVRActiveActionSet_t == 0)
- sizeVRActiveActionSet_t = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VRActiveActionSet_t));
- OpenVR.Input.ShowBindingsForActionSet(SteamVR_ActionSet_Manager.rawActiveActionSetArray, sizeVRActiveActionSet_t, highlightAction);
- }
- #region String accessor helpers
- #region action accessors
-
-
-
-
-
-
- public static T GetActionDataFromPath<T>(string path, bool caseSensitive = false) where T : SteamVR_Action_Source_Map
- {
- SteamVR_Action action = GetBaseActionFromPath(path, caseSensitive);
- if (action != null)
- {
- T actionData = (T)action.GetSourceMap();
- return actionData;
- }
- return null;
- }
-
-
-
-
-
- public static SteamVR_ActionSet_Data GetActionSetDataFromPath(string path, bool caseSensitive = false)
- {
- SteamVR_ActionSet actionSet = GetActionSetFromPath(path, caseSensitive);
- if (actionSet != null)
- {
- return actionSet.GetActionSetData();
- }
- return null;
- }
-
-
-
-
-
-
- public static T GetActionFromPath<T>(string path, bool caseSensitive = false, bool returnNulls = false) where T : SteamVR_Action, new()
- {
- SteamVR_Action foundAction = GetBaseActionFromPath(path, caseSensitive);
- if (foundAction != null)
- return foundAction.GetCopy<T>();
- if (returnNulls)
- return null;
- return CreateFakeAction<T>(path, caseSensitive);
- }
-
- public static SteamVR_Action GetBaseActionFromPath(string path, bool caseSensitive = false)
- {
- if (string.IsNullOrEmpty(path))
- return null;
- if (caseSensitive)
- {
- if (actionsByPath.ContainsKey(path))
- {
- return actionsByPath[path];
- }
- }
- else
- {
- if (actionsByPathCache.ContainsKey(path))
- {
- return actionsByPathCache[path];
- }
- else if (actionsByPath.ContainsKey(path))
- {
- actionsByPathCache.Add(path, actionsByPath[path]);
- return actionsByPath[path];
- }
- else
- {
- string loweredPath = path.ToLower();
- if (actionsByPathLowered.ContainsKey(loweredPath))
- {
- actionsByPathCache.Add(path, actionsByPathLowered[loweredPath]);
- return actionsByPathLowered[loweredPath];
- }
- else
- {
- actionsByPathCache.Add(path, null);
- }
- }
- }
- return null;
- }
- public static bool HasActionPath(string path, bool caseSensitive = false)
- {
- SteamVR_Action action = GetBaseActionFromPath(path, caseSensitive);
- return action != null;
- }
- public static bool HasAction(string actionName, bool caseSensitive = false)
- {
- SteamVR_Action action = GetBaseAction(null, actionName, caseSensitive);
- return action != null;
- }
- public static bool HasAction(string actionSetName, string actionName, bool caseSensitive = false)
- {
- SteamVR_Action action = GetBaseAction(actionSetName, actionName, caseSensitive);
- return action != null;
- }
-
-
-
-
-
- public static SteamVR_Action_Boolean GetBooleanActionFromPath(string path, bool caseSensitive = false)
- {
- return GetActionFromPath<SteamVR_Action_Boolean>(path, caseSensitive);
- }
-
-
-
-
-
- public static SteamVR_Action_Single GetSingleActionFromPath(string path, bool caseSensitive = false)
- {
- return GetActionFromPath<SteamVR_Action_Single>(path, caseSensitive);
- }
-
-
-
-
-
- public static SteamVR_Action_Vector2 GetVector2ActionFromPath(string path, bool caseSensitive = false)
- {
- return GetActionFromPath<SteamVR_Action_Vector2>(path, caseSensitive);
- }
-
-
-
-
-
- public static SteamVR_Action_Vector3 GetVector3ActionFromPath(string path, bool caseSensitive = false)
- {
- return GetActionFromPath<SteamVR_Action_Vector3>(path, caseSensitive);
- }
-
-
-
-
-
- public static SteamVR_Action_Vibration GetVibrationActionFromPath(string path, bool caseSensitive = false)
- {
- return GetActionFromPath<SteamVR_Action_Vibration>(path, caseSensitive);
- }
-
-
-
-
-
- public static SteamVR_Action_Pose GetPoseActionFromPath(string path, bool caseSensitive = false)
- {
- return GetActionFromPath<SteamVR_Action_Pose>(path, caseSensitive);
- }
-
-
-
-
-
- public static SteamVR_Action_Skeleton GetSkeletonActionFromPath(string path, bool caseSensitive = false)
- {
- return GetActionFromPath<SteamVR_Action_Skeleton>(path, caseSensitive);
- }
-
-
-
-
-
-
-
- public static T GetAction<T>(string actionSetName, string actionName, bool caseSensitive = false, bool returnNulls = false) where T : SteamVR_Action, new()
- {
- SteamVR_Action action = GetBaseAction(actionSetName, actionName, caseSensitive);
- if (action != null)
- return (T)action.GetCopy<T>();
- if (returnNulls)
- return null;
- return CreateFakeAction<T>(actionSetName, actionName, caseSensitive);
- }
- public static SteamVR_Action GetBaseAction(string actionSetName, string actionName, bool caseSensitive = false)
- {
- if (actions == null)
- {
- return null;
- }
- if (string.IsNullOrEmpty(actionSetName))
- {
- for (int actionIndex = 0; actionIndex < actions.Length; actionIndex++)
- {
- if (caseSensitive)
- {
- if (actions[actionIndex].GetShortName() == actionName)
- return actions[actionIndex];
- }
- else
- {
- if (string.Equals(actions[actionIndex].GetShortName(), actionName, StringComparison.CurrentCultureIgnoreCase))
- return actions[actionIndex];
- }
- }
- }
- else
- {
- SteamVR_ActionSet actionSet = GetActionSet(actionSetName, caseSensitive, true);
- if (actionSet != null)
- {
- for (int actionIndex = 0; actionIndex < actionSet.allActions.Length; actionIndex++)
- {
- if (caseSensitive)
- {
- if (actionSet.allActions[actionIndex].GetShortName() == actionName)
- return actionSet.allActions[actionIndex];
- }
- else
- {
- if (string.Equals(actionSet.allActions[actionIndex].GetShortName(), actionName, StringComparison.CurrentCultureIgnoreCase))
- return actionSet.allActions[actionIndex];
- }
- }
- }
- }
- return null;
- }
- private static T CreateFakeAction<T>(string actionSetName, string actionName, bool caseSensitive) where T : SteamVR_Action, new()
- {
- if (typeof(T) == typeof(SteamVR_Action_Vibration))
- {
- return SteamVR_Action.CreateUninitialized<T>(actionSetName, SteamVR_ActionDirections.Out, actionName, caseSensitive);
- }
- else
- {
- return SteamVR_Action.CreateUninitialized<T>(actionSetName, SteamVR_ActionDirections.In, actionName, caseSensitive);
- }
- }
- private static T CreateFakeAction<T>(string actionPath, bool caseSensitive) where T : SteamVR_Action, new()
- {
- return SteamVR_Action.CreateUninitialized<T>(actionPath, caseSensitive);
- }
-
-
-
-
-
-
- public static T GetAction<T>(string actionName, bool caseSensitive = false) where T : SteamVR_Action, new()
- {
- return GetAction<T>(null, actionName, caseSensitive);
- }
-
-
-
-
-
-
- public static SteamVR_Action_Boolean GetBooleanAction(string actionSetName, string actionName, bool caseSensitive = false)
- {
- return GetAction<SteamVR_Action_Boolean>(actionSetName, actionName, caseSensitive);
- }
-
-
-
-
-
-
- public static SteamVR_Action_Boolean GetBooleanAction(string actionName, bool caseSensitive = false)
- {
- return GetAction<SteamVR_Action_Boolean>(null, actionName, caseSensitive);
- }
-
-
-
-
-
-
- public static SteamVR_Action_Single GetSingleAction(string actionSetName, string actionName, bool caseSensitive = false)
- {
- return GetAction<SteamVR_Action_Single>(actionSetName, actionName, caseSensitive);
- }
-
-
-
-
-
-
- public static SteamVR_Action_Single GetSingleAction(string actionName, bool caseSensitive = false)
- {
- return GetAction<SteamVR_Action_Single>(null, actionName, caseSensitive);
- }
-
-
-
-
-
-
- public static SteamVR_Action_Vector2 GetVector2Action(string actionSetName, string actionName, bool caseSensitive = false)
- {
- return GetAction<SteamVR_Action_Vector2>(actionSetName, actionName, caseSensitive);
- }
-
-
-
-
-
-
- public static SteamVR_Action_Vector2 GetVector2Action(string actionName, bool caseSensitive = false)
- {
- return GetAction<SteamVR_Action_Vector2>(null, actionName, caseSensitive);
- }
-
-
-
-
-
-
- public static SteamVR_Action_Vector3 GetVector3Action(string actionSetName, string actionName, bool caseSensitive = false)
- {
- return GetAction<SteamVR_Action_Vector3>(actionSetName, actionName, caseSensitive);
- }
-
-
-
-
-
-
- public static SteamVR_Action_Vector3 GetVector3Action(string actionName, bool caseSensitive = false)
- {
- return GetAction<SteamVR_Action_Vector3>(null, actionName, caseSensitive);
- }
-
-
-
-
-
-
- public static SteamVR_Action_Pose GetPoseAction(string actionSetName, string actionName, bool caseSensitive = false)
- {
- return GetAction<SteamVR_Action_Pose>(actionSetName, actionName, caseSensitive);
- }
-
-
-
-
-
-
- public static SteamVR_Action_Pose GetPoseAction(string actionName, bool caseSensitive = false)
- {
- return GetAction<SteamVR_Action_Pose>(null, actionName, caseSensitive);
- }
-
-
-
-
-
-
- public static SteamVR_Action_Skeleton GetSkeletonAction(string actionSetName, string actionName, bool caseSensitive = false)
- {
- return GetAction<SteamVR_Action_Skeleton>(actionSetName, actionName, caseSensitive);
- }
-
-
-
-
-
-
- public static SteamVR_Action_Skeleton GetSkeletonAction(string actionName, bool caseSensitive = false)
- {
- return GetAction<SteamVR_Action_Skeleton>(null, actionName, caseSensitive);
- }
-
-
-
-
-
-
- public static SteamVR_Action_Vibration GetVibrationAction(string actionSetName, string actionName, bool caseSensitive = false)
- {
- return GetAction<SteamVR_Action_Vibration>(actionSetName, actionName, caseSensitive);
- }
-
-
-
-
-
-
- public static SteamVR_Action_Vibration GetVibrationAction(string actionName, bool caseSensitive = false)
- {
- return GetAction<SteamVR_Action_Vibration>(null, actionName, caseSensitive);
- }
-
-
-
-
-
-
-
- public static T GetActionSet<T>(string actionSetName, bool caseSensitive = false, bool returnNulls = false) where T : SteamVR_ActionSet, new()
- {
- if (actionSets == null)
- {
- if (returnNulls)
- return null;
- return SteamVR_ActionSet.CreateFromName<T>(actionSetName);
- }
- for (int actionSetIndex = 0; actionSetIndex < actionSets.Length; actionSetIndex++)
- {
- if (caseSensitive)
- {
- if (actionSets[actionSetIndex].GetShortName() == actionSetName)
- return actionSets[actionSetIndex].GetCopy<T>();
- }
- else
- {
- if (string.Equals(actionSets[actionSetIndex].GetShortName(), actionSetName, StringComparison.CurrentCultureIgnoreCase))
- return actionSets[actionSetIndex].GetCopy<T>();
- }
- }
- if (returnNulls)
- return null;
- return SteamVR_ActionSet.CreateFromName<T>(actionSetName);
- }
-
-
-
-
-
-
- public static SteamVR_ActionSet GetActionSet(string actionSetName, bool caseSensitive = false, bool returnsNulls = false)
- {
- return GetActionSet<SteamVR_ActionSet>(actionSetName, caseSensitive, returnsNulls);
- }
- protected static bool HasActionSet(string name, bool caseSensitive = false)
- {
- SteamVR_ActionSet actionSet = GetActionSet(name, caseSensitive, true);
- return actionSet != null;
- }
-
-
-
-
-
-
- public static T GetActionSetFromPath<T>(string path, bool caseSensitive = false, bool returnsNulls = false) where T : SteamVR_ActionSet, new()
- {
- if (actionSets == null || actionSets[0] == null || string.IsNullOrEmpty(path))
- {
- if (returnsNulls)
- return null;
- return SteamVR_ActionSet.Create<T>(path);
- }
- if (caseSensitive)
- {
- if (actionSetsByPath.ContainsKey(path))
- {
- return actionSetsByPath[path].GetCopy<T>();
- }
- }
- else
- {
- if (actionSetsByPathCache.ContainsKey(path))
- {
- SteamVR_ActionSet set = actionSetsByPathCache[path];
- if (set == null)
- return null;
- else
- return set.GetCopy<T>();
- }
- else if (actionSetsByPath.ContainsKey(path))
- {
- actionSetsByPathCache.Add(path, actionSetsByPath[path]);
- return actionSetsByPath[path].GetCopy<T>();
- }
- else
- {
- string loweredPath = path.ToLower();
- if (actionSetsByPathLowered.ContainsKey(loweredPath))
- {
- actionSetsByPathCache.Add(path, actionSetsByPathLowered[loweredPath]);
- return actionSetsByPath[loweredPath].GetCopy<T>();
- }
- else
- {
- actionSetsByPathCache.Add(path, null);
- }
- }
- }
- if (returnsNulls)
- return null;
- return SteamVR_ActionSet.Create<T>(path);
- }
-
-
-
-
-
- public static SteamVR_ActionSet GetActionSetFromPath(string path, bool caseSensitive = false)
- {
- return GetActionSetFromPath<SteamVR_ActionSet>(path, caseSensitive);
- }
- #endregion
- #region digital string accessors
-
-
-
-
-
-
-
- public static bool GetState(string actionSet, string action, SteamVR_Input_Sources inputSource, bool caseSensitive = false)
- {
- SteamVR_Action_Boolean booleanAction = GetAction<SteamVR_Action_Boolean>(actionSet, action, caseSensitive);
- if (booleanAction != null)
- {
- return booleanAction.GetState(inputSource);
- }
- return false;
- }
-
-
-
-
-
-
- public static bool GetState(string action, SteamVR_Input_Sources inputSource, bool caseSensitive = false)
- {
- return GetState(null, action, inputSource, caseSensitive);
- }
-
-
-
-
-
-
-
-
- public static bool GetStateDown(string actionSet, string action, SteamVR_Input_Sources inputSource, bool caseSensitive = false)
- {
- SteamVR_Action_Boolean booleanAction = GetAction<SteamVR_Action_Boolean>(actionSet, action, caseSensitive);
- if (booleanAction != null)
- {
- return booleanAction.GetStateDown(inputSource);
- }
- return false;
- }
-
-
-
-
-
-
-
- public static bool GetStateDown(string action, SteamVR_Input_Sources inputSource, bool caseSensitive = false)
- {
- return GetStateDown(null, action, inputSource, caseSensitive);
- }
-
-
-
-
-
-
-
-
- public static bool GetStateUp(string actionSet, string action, SteamVR_Input_Sources inputSource, bool caseSensitive = false)
- {
- SteamVR_Action_Boolean booleanAction = GetAction<SteamVR_Action_Boolean>(actionSet, action, caseSensitive);
- if (booleanAction != null)
- {
- return booleanAction.GetStateUp(inputSource);
- }
- return false;
- }
-
-
-
-
-
-
-
- public static bool GetStateUp(string action, SteamVR_Input_Sources inputSource, bool caseSensitive = false)
- {
- return GetStateUp(null, action, inputSource, caseSensitive);
- }
- #endregion
- #region analog string accessors
-
-
-
-
-
-
-
- public static float GetFloat(string actionSet, string action, SteamVR_Input_Sources inputSource, bool caseSensitive = false)
- {
- SteamVR_Action_Single singleAction = GetAction<SteamVR_Action_Single>(actionSet, action, caseSensitive);
- if (singleAction != null)
- {
- return singleAction.GetAxis(inputSource);
- }
- return 0;
- }
-
-
-
-
-
-
- public static float GetFloat(string action, SteamVR_Input_Sources inputSource, bool caseSensitive = false)
- {
- return GetFloat(null, action, inputSource, caseSensitive);
- }
-
-
-
-
-
-
-
- public static float GetSingle(string actionSet, string action, SteamVR_Input_Sources inputSource, bool caseSensitive = false)
- {
- SteamVR_Action_Single singleAction = GetAction<SteamVR_Action_Single>(actionSet, action, caseSensitive);
- if (singleAction != null)
- {
- return singleAction.GetAxis(inputSource);
- }
- return 0;
- }
-
-
-
-
-
-
- public static float GetSingle(string action, SteamVR_Input_Sources inputSource, bool caseSensitive = false)
- {
- return GetFloat(null, action, inputSource, caseSensitive);
- }
-
-
-
-
-
-
-
- public static Vector2 GetVector2(string actionSet, string action, SteamVR_Input_Sources inputSource, bool caseSensitive = false)
- {
- SteamVR_Action_Vector2 vectorAction = GetAction<SteamVR_Action_Vector2>(actionSet, action, caseSensitive);
- if (vectorAction != null)
- {
- return vectorAction.GetAxis(inputSource);
- }
- return Vector2.zero;
- }
-
-
-
-
-
-
- public static Vector2 GetVector2(string action, SteamVR_Input_Sources inputSource, bool caseSensitive = false)
- {
- return GetVector2(null, action, inputSource, caseSensitive);
- }
-
-
-
-
-
-
-
- public static Vector3 GetVector3(string actionSet, string action, SteamVR_Input_Sources inputSource, bool caseSensitive = false)
- {
- SteamVR_Action_Vector3 vectorAction = GetAction<SteamVR_Action_Vector3>(actionSet, action, caseSensitive);
- if (vectorAction != null)
- {
- return vectorAction.GetAxis(inputSource);
- }
- return Vector3.zero;
- }
-
-
-
-
-
-
- public static Vector3 GetVector3(string action, SteamVR_Input_Sources inputSource, bool caseSensitive = false)
- {
- return GetVector3(null, action, inputSource, caseSensitive);
- }
- #endregion
- #endregion
-
-
-
- public static SteamVR_ActionSet[] GetActionSets()
- {
- return actionSets;
- }
-
-
-
-
- public static T[] GetActions<T>() where T : SteamVR_Action
- {
- Type type = typeof(T);
- if (type == typeof(SteamVR_Action))
- {
- return actions as T[];
- }
- else if (type == typeof(ISteamVR_Action_In))
- {
- return actionsIn as T[];
- }
- else if (type == typeof(ISteamVR_Action_Out))
- {
- return actionsOut as T[];
- }
- else if (type == typeof(SteamVR_Action_Boolean))
- {
- return actionsBoolean as T[];
- }
- else if (type == typeof(SteamVR_Action_Single))
- {
- return actionsSingle as T[];
- }
- else if (type == typeof(SteamVR_Action_Vector2))
- {
- return actionsVector2 as T[];
- }
- else if (type == typeof(SteamVR_Action_Vector3))
- {
- return actionsVector3 as T[];
- }
- else if (type == typeof(SteamVR_Action_Pose))
- {
- return actionsPose as T[];
- }
- else if (type == typeof(SteamVR_Action_Skeleton))
- {
- return actionsSkeleton as T[];
- }
- else if (type == typeof(SteamVR_Action_Vibration))
- {
- return actionsVibration as T[];
- }
- else
- {
- Debug.Log("<b>[SteamVR]</b> Wrong type.");
- }
- return null;
- }
- internal static bool ShouldMakeCopy()
- {
- bool shouldMakeCopy = SteamVR_Behaviour.isPlaying == false;
- return shouldMakeCopy;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public static string GetLocalizedName(ulong originHandle, params EVRInputStringBits[] localizedParts)
- {
- int localizedPartsMask = 0;
- for (int partIndex = 0; partIndex < localizedParts.Length; partIndex++)
- localizedPartsMask |= (int)localizedParts[partIndex];
- StringBuilder stringBuilder = new StringBuilder(500);
- OpenVR.Input.GetOriginLocalizedName(originHandle, stringBuilder, 500, localizedPartsMask);
- return stringBuilder.ToString();
- }
- public static bool CheckOldLocation()
- {
- #if UNITY_EDITOR
- DirectoryInfo dataPath = new DirectoryInfo(Application.dataPath);
- string projectRoot = dataPath.Parent.FullName;
- string fullOldActionsPath = Path.Combine(projectRoot, SteamVR_Settings.instance.actionsFilePath);
- if (File.Exists(fullOldActionsPath))
- {
- SteamVR_Input_ActionFile oldActionsFile = SteamVR_Input_ActionFile.Open(fullOldActionsPath);
- string[] actionAndBindingFiles = oldActionsFile.GetFilesToCopy(true);
- string newActionsFilePath = GetActionsFilePath(true);
- bool shouldCopy = true;
- bool verified = false;
- if (File.Exists(newActionsFilePath))
- {
- shouldCopy = UnityEditor.EditorUtility.DisplayDialog("SteamVR", "SteamVR Unity Plugin detected an Action Manifest file in the legacy location (project root). You also have an Action Manifest File in the new location (streaming assets). Would you like to overwrite the files in streaming assets?", "Yes", "No");
- verified = true;
- }
- if (shouldCopy)
- {
- string newFolderPath = GetActionsFileFolder();
- foreach (string filePath in actionAndBindingFiles)
- {
- FileInfo oldFile = new FileInfo(filePath);
- string newFilePath = Path.Combine(newFolderPath, oldFile.Name);
- if (File.Exists(newFilePath))
- {
- FileInfo newFile = new FileInfo(newFilePath);
- newFile.IsReadOnly = false;
- newFile.Delete();
- }
- oldFile.IsReadOnly = false;
- oldFile.MoveTo(newFilePath);
- }
- if (verified == false)
- {
- UnityEditor.EditorUtility.DisplayDialog("SteamVR", "SteamVR Unity Plugin detected an Action Manifest file in the legacy location (project root). We've automatically moved the files to the new location (" + GetActionsFileFolder() + ").", "Ok");
- }
- else
- {
- UnityEditor.EditorUtility.DisplayDialog("SteamVR", "Moving files to the new location (" + GetActionsFileFolder() + ") is complete.", "Ok");
- }
- UnityEditor.AssetDatabase.Refresh();
- return true;
- }
- }
- #endif
- return false;
- }
-
- public static void IdentifyActionsFile(bool showLogs = true)
- {
- string fullPath = GetActionsFilePath();
- if (File.Exists(fullPath))
- {
- if (OpenVR.Input == null)
- {
- Debug.LogError("<b>[SteamVR]</b> Could not instantiate OpenVR Input interface.");
- return;
- }
- EVRInputError err = OpenVR.Input.SetActionManifestPath(fullPath);
- if (err != EVRInputError.None)
- Debug.LogError("<b>[SteamVR]</b> Error loading action manifest into SteamVR: " + err.ToString());
- else
- {
- int numActions = 0;
- if (SteamVR_Input.actions != null)
- {
- numActions = SteamVR_Input.actions.Length;
- if (showLogs)
- Debug.Log(string.Format("<b>[SteamVR]</b> Successfully loaded {0} actions from action manifest into SteamVR ({1})", numActions, fullPath));
- }
- else
- {
- if (showLogs)
- Debug.LogWarning("<b>[SteamVR]</b> No actions found, but the action manifest was loaded. This usually means you haven't generated actions. Window -> SteamVR Input -> Save and Generate.");
- }
- }
- }
- else
- {
- if (showLogs)
- Debug.LogError("<b>[SteamVR]</b> Could not find actions file at: " + fullPath);
- }
- }
-
-
-
- public static bool HasFileInMemoryBeenModified()
- {
- string fullPath = GetActionsFilePath();
- string jsonText = null;
- if (File.Exists(fullPath))
- {
- jsonText = System.IO.File.ReadAllText(fullPath);
- }
- else
- {
- return true;
- }
- string newHashFromFile = SteamVR_Utils.GetBadMD5Hash(jsonText);
- string newJSON = JsonConvert.SerializeObject(SteamVR_Input.actionFile, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
- string newHashFromMemory = SteamVR_Utils.GetBadMD5Hash(newJSON);
- return newHashFromFile != newHashFromMemory;
- }
- public static bool CreateEmptyActionsFile(bool completelyEmpty = false)
- {
- string actionsFilePath = GetActionsFilePath();
- if (File.Exists(actionsFilePath))
- {
- Debug.LogErrorFormat("<b>[SteamVR]</b> Actions file already exists in project root: {0}", actionsFilePath);
- return false;
- }
- actionFile = new SteamVR_Input_ActionFile();
- if (completelyEmpty == false)
- {
- actionFile.action_sets.Add(SteamVR_Input_ActionFile_ActionSet.CreateNew());
- actionFile.actions.Add(SteamVR_Input_ActionFile_Action.CreateNew(actionFile.action_sets[0].shortName,
- SteamVR_ActionDirections.In, SteamVR_Input_ActionFile_ActionTypes.boolean));
- }
- string newJSON = JsonConvert.SerializeObject(actionFile, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
- File.WriteAllText(actionsFilePath, newJSON);
- actionFile.InitializeHelperLists();
- fileInitialized = true;
- return true;
- }
- public static bool DoesActionsFileExist()
- {
- return File.Exists(GetActionsFilePath());
- }
-
-
-
-
- public static bool InitializeFile(bool force = false, bool showErrors = true)
- {
- bool actionsFileExists = DoesActionsFileExist();
- string actionsFilePath = GetActionsFilePath();
- string jsonText = null;
- if (actionsFileExists)
- {
- jsonText = System.IO.File.ReadAllText(actionsFilePath);
- }
- else
- {
- if (showErrors)
- Debug.LogErrorFormat("<b>[SteamVR]</b> Actions file does not exist in project root: {0}", actionsFilePath);
- return false;
- }
- if (fileInitialized == true || (fileInitialized == true && force == false))
- {
- string newHash = SteamVR_Utils.GetBadMD5Hash(jsonText);
- if (newHash == actionFileHash)
- {
- return true;
- }
- actionFileHash = newHash;
- }
- actionFile = SteamVR_Input_ActionFile.Open(GetActionsFilePath());
- fileInitialized = true;
- return true;
- }
- public static string GetActionsFileFolder(bool fullPath = true)
- {
- string streamingAssets = Application.streamingAssetsPath;
- if (Directory.Exists(streamingAssets) == false)
- Directory.CreateDirectory(streamingAssets);
- string streamingAssets_SteamVR = Path.Combine(streamingAssets, "SteamVR");
- if (Directory.Exists(streamingAssets_SteamVR) == false)
- Directory.CreateDirectory(streamingAssets_SteamVR);
- return streamingAssets_SteamVR;
- }
- public static string GetActionsFilePath(bool fullPath = true)
- {
- string streamingAssets_SteamVR = GetActionsFileFolder(fullPath);
- string path = Path.Combine(streamingAssets_SteamVR, SteamVR_Settings.instance.actionsFilePath);
- return SteamVR_Utils.SanitizePath(path);
- }
- public static string GetActionsFileName()
- {
- return SteamVR_Settings.instance.actionsFilePath;
- }
-
-
-
-
- public static bool DeleteManifestAndBindings()
- {
- if (DoesActionsFileExist() == false)
- return false;
- InitializeFile();
- string[] filesToDelete = actionFile.GetFilesToCopy();
- foreach (string bindingFilePath in filesToDelete)
- {
- FileInfo bindingFileInfo = new FileInfo(bindingFilePath);
- bindingFileInfo.IsReadOnly = false;
- File.Delete(bindingFilePath);
- }
- string actionsFilePath = GetActionsFilePath();
- if (File.Exists(actionsFilePath))
- {
- FileInfo actionFileInfo = new FileInfo(actionsFilePath);
- actionFileInfo.IsReadOnly = false;
- File.Delete(actionsFilePath);
- actionFile = null;
- fileInitialized = false;
- return true;
- }
- return false;
- }
-
-
-
-
-
- public static void OpenBindingUI(SteamVR_ActionSet actionSetToEdit = null, SteamVR_Input_Sources deviceBindingToEdit = SteamVR_Input_Sources.Any)
- {
- ulong deviceHandle = SteamVR_Input_Source.GetHandle(deviceBindingToEdit);
- ulong actionSetHandle = 0;
- if (actionSetToEdit != null)
- actionSetHandle = actionSetToEdit.handle;
- OpenVR.Input.OpenBindingUI(null, actionSetHandle, deviceHandle, false);
- }
- #if UNITY_EDITOR
- public static string GetResourcesFolderPath(bool fromAssetsDirectory = false)
- {
- string inputFolder = string.Format("Assets/{0}", SteamVR_Settings.instance.steamVRInputPath);
- string path = Path.Combine(inputFolder, "Resources");
- bool createdDirectory = false;
- if (Directory.Exists(inputFolder) == false)
- {
- Directory.CreateDirectory(inputFolder);
- createdDirectory = true;
- }
- if (Directory.Exists(path) == false)
- {
- Directory.CreateDirectory(path);
- createdDirectory = true;
- }
- if (createdDirectory)
- UnityEditor.AssetDatabase.Refresh();
- if (fromAssetsDirectory == false)
- return path.Replace("Assets/", "");
- else
- return path;
- }
- private static bool checkingSetup = false;
- private static bool openingSetup = false;
- public static bool IsOpeningSetup() { return openingSetup; }
- private static void CheckSetup()
- {
- if (checkingSetup == false && openingSetup == false && (SteamVR_Input.actions == null || SteamVR_Input.actions.Length == 0))
- {
- checkingSetup = true;
- Debug.Break();
- bool open = UnityEditor.EditorUtility.DisplayDialog("[SteamVR]", "It looks like you haven't generated actions for SteamVR Input yet. Would you like to open the SteamVR Input window?", "Yes", "No");
- if (open)
- {
- openingSetup = true;
- UnityEditor.EditorApplication.isPlaying = false;
- Type editorWindowType = SteamVR_Utils.FindType("Valve.VR.SteamVR_Input_EditorWindow");
- if (editorWindowType != null)
- {
- var window = UnityEditor.EditorWindow.GetWindow(editorWindowType, false, "SteamVR Input", true);
- if (window != null)
- window.Show();
- }
- }
- else
- {
- Debug.LogError("<b>[SteamVR]</b> This version of SteamVR will not work if you do not create and generate actions. Please open the SteamVR Input window or downgrade to version 1.2.3 (on github)");
- }
- checkingSetup = false;
- }
- }
- public static string GetEditorAppKey()
- {
- return SteamVR_Settings.instance.editorAppKey;
- }
- #endif
- }
- }
|