123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792 |
-
- using UnityEngine;
- using System.Collections;
- using System;
- using Valve.VR;
- using System.Runtime.InteropServices;
- using System.Collections.Generic;
- namespace Valve.VR
- {
- [Serializable]
-
-
-
-
- public abstract class SteamVR_Action<SourceMap, SourceElement> : SteamVR_Action, ISteamVR_Action where SourceMap : SteamVR_Action_Source_Map<SourceElement>, new() where SourceElement : SteamVR_Action_Source, new()
- {
-
-
-
- [NonSerialized]
- protected SourceMap sourceMap;
-
-
-
-
- public virtual SourceElement this[SteamVR_Input_Sources inputSource]
- {
- get
- {
- return sourceMap[inputSource];
- }
- }
-
- public override string fullPath
- {
- get
- {
- return sourceMap.fullPath;
- }
- }
-
- public override ulong handle { get { return sourceMap.handle; } }
-
- public override SteamVR_ActionSet actionSet
- {
- get
- {
- return sourceMap.actionSet;
- }
- }
-
- public override SteamVR_ActionDirections direction
- {
- get
- {
- return sourceMap.direction;
- }
- }
-
- public override bool active { get { return sourceMap[SteamVR_Input_Sources.Any].active; } }
-
- public override bool lastActive { get { return sourceMap[SteamVR_Input_Sources.Any].lastActive; } }
-
- public override bool activeBinding { get { return sourceMap[SteamVR_Input_Sources.Any].activeBinding; } }
-
- public override bool lastActiveBinding { get { return sourceMap[SteamVR_Input_Sources.Any].lastActiveBinding; } }
- [NonSerialized]
- protected bool initialized = false;
-
-
-
- public override void PreInitialize(string newActionPath)
- {
- actionPath = newActionPath;
- sourceMap = new SourceMap();
- sourceMap.PreInitialize(this, actionPath);
- initialized = true;
- }
- protected override void CreateUninitialized(string newActionPath, bool caseSensitive)
- {
- actionPath = newActionPath;
- sourceMap = new SourceMap();
- sourceMap.PreInitialize(this, actionPath, false);
- needsReinit = true;
- initialized = false;
- }
- protected override void CreateUninitialized(string newActionSet, SteamVR_ActionDirections direction, string newAction, bool caseSensitive)
- {
- actionPath = SteamVR_Input_ActionFile_Action.CreateNewName(newActionSet, direction, newAction);
- sourceMap = new SourceMap();
- sourceMap.PreInitialize(this, actionPath, false);
- needsReinit = true;
- initialized = false;
- }
-
-
-
- public override string TryNeedsInitData()
- {
- if (needsReinit && actionPath != null)
- {
- SteamVR_Action existingAction = FindExistingActionForPartialPath(actionPath);
- if (existingAction == null)
- {
- this.sourceMap = null;
- }
- else
- {
- this.actionPath = existingAction.fullPath;
- this.sourceMap = (SourceMap)existingAction.GetSourceMap();
- initialized = true;
- needsReinit = false;
- return actionPath;
- }
- }
- return null;
- }
-
-
-
-
- public override void Initialize(bool createNew = false, bool throwErrors = true)
- {
- if (needsReinit)
- {
- TryNeedsInitData();
- }
- if (createNew)
- {
- sourceMap.Initialize();
- }
- else
- {
- sourceMap = SteamVR_Input.GetActionDataFromPath<SourceMap>(actionPath);
- if (sourceMap == null)
- {
- #if UNITY_EDITOR
- if (throwErrors)
- {
- if (string.IsNullOrEmpty(actionPath))
- {
- Debug.LogError("<b>[SteamVR]</b> Action has not been assigned.");
- }
- else
- {
- Debug.LogError("<b>[SteamVR]</b> Could not find action with path: " + actionPath);
- }
- }
- #endif
- }
- }
- initialized = true;
- }
-
-
-
-
- public override SteamVR_Action_Source_Map GetSourceMap()
- {
- return sourceMap;
- }
- protected override void InitializeCopy(string newActionPath, SteamVR_Action_Source_Map newData)
- {
- this.actionPath = newActionPath;
- this.sourceMap = (SourceMap)newData;
- initialized = true;
- }
- protected void InitAfterDeserialize()
- {
- if (sourceMap != null)
- {
- if (sourceMap.fullPath != actionPath)
- {
- needsReinit = true;
- TryNeedsInitData();
- }
- if (string.IsNullOrEmpty(actionPath))
- sourceMap = null;
- }
- if (initialized == false)
- {
- Initialize(false, false);
- }
- }
-
-
-
-
- public override bool GetActive(SteamVR_Input_Sources inputSource)
- {
- return sourceMap[inputSource].active;
- }
-
-
-
-
- public override bool GetActiveBinding(SteamVR_Input_Sources inputSource)
- {
- return sourceMap[inputSource].activeBinding;
- }
-
-
-
-
- public override bool GetLastActive(SteamVR_Input_Sources inputSource)
- {
- return sourceMap[inputSource].lastActive;
- }
-
-
-
-
- public override bool GetLastActiveBinding(SteamVR_Input_Sources inputSource)
- {
- return sourceMap[inputSource].lastActiveBinding;
- }
- }
- [Serializable]
- public abstract class SteamVR_Action : IEquatable<SteamVR_Action>, ISteamVR_Action
- {
- public SteamVR_Action() { }
- [SerializeField]
- protected string actionPath;
- [SerializeField]
- protected bool needsReinit;
-
-
-
- public static bool startUpdatingSourceOnAccess = true;
-
-
-
- public static CreateType Create<CreateType>(string newActionPath) where CreateType : SteamVR_Action, new()
- {
- CreateType action = new CreateType();
- action.PreInitialize(newActionPath);
- return action;
- }
-
-
-
- public static CreateType CreateUninitialized<CreateType>(string setName, SteamVR_ActionDirections direction, string newActionName, bool caseSensitive) where CreateType : SteamVR_Action, new()
- {
- CreateType action = new CreateType();
- action.CreateUninitialized(setName, direction, newActionName, caseSensitive);
- return action;
- }
-
-
-
- public static CreateType CreateUninitialized<CreateType>(string actionPath, bool caseSensitive) where CreateType : SteamVR_Action, new()
- {
- CreateType action = new CreateType();
- action.CreateUninitialized(actionPath, caseSensitive);
- return action;
- }
-
-
-
- public CreateType GetCopy<CreateType>() where CreateType : SteamVR_Action, new()
- {
- if (SteamVR_Input.ShouldMakeCopy())
- {
- CreateType action = new CreateType();
- action.InitializeCopy(this.actionPath, this.GetSourceMap());
- return action;
- }
- else
- {
- return (CreateType)this;
- }
- }
- public abstract string TryNeedsInitData();
- protected abstract void InitializeCopy(string newActionPath, SteamVR_Action_Source_Map newData);
-
- public abstract string fullPath { get; }
-
- public abstract ulong handle { get; }
-
- public abstract SteamVR_ActionSet actionSet { get; }
-
- public abstract SteamVR_ActionDirections direction { get; }
-
- public bool setActive { get { return actionSet.IsActive(SteamVR_Input_Sources.Any); } }
-
- public abstract bool active { get; }
-
- public abstract bool activeBinding { get; }
-
- public abstract bool lastActive { get; }
-
-
-
- public abstract bool lastActiveBinding { get; }
-
-
-
- public abstract void PreInitialize(string newActionPath);
- protected abstract void CreateUninitialized(string newActionPath, bool caseSensitive);
- protected abstract void CreateUninitialized(string newActionSet, SteamVR_ActionDirections direction, string newAction, bool caseSensitive);
-
-
-
- public abstract void Initialize(bool createNew = false, bool throwNotSetError = true);
-
-
- public abstract float GetTimeLastChanged(SteamVR_Input_Sources inputSource);
- public abstract SteamVR_Action_Source_Map GetSourceMap();
-
-
-
-
- public abstract bool GetActive(SteamVR_Input_Sources inputSource);
-
-
-
-
- public bool GetSetActive(SteamVR_Input_Sources inputSource)
- {
- return actionSet.IsActive(inputSource);
- }
-
-
-
-
- public abstract bool GetActiveBinding(SteamVR_Input_Sources inputSource);
-
-
-
-
- public abstract bool GetLastActive(SteamVR_Input_Sources inputSource);
-
-
-
-
- public abstract bool GetLastActiveBinding(SteamVR_Input_Sources inputSource);
-
- public string GetPath()
- {
- return actionPath;
- }
-
-
-
- public abstract bool IsUpdating(SteamVR_Input_Sources inputSource);
-
-
-
- public override int GetHashCode()
- {
- if (actionPath == null)
- return 0;
- else
- return actionPath.GetHashCode();
- }
-
-
-
- public bool Equals(SteamVR_Action other)
- {
- if (ReferenceEquals(null, other))
- return false;
-
-
-
- return this.actionPath == other.actionPath;
- }
-
-
-
- public override bool Equals(object other)
- {
- if (ReferenceEquals(null, other))
- {
- if (string.IsNullOrEmpty(this.actionPath))
- return true;
- if (this.GetSourceMap() == null)
- return true;
- return false;
- }
- if (ReferenceEquals(this, other))
- return true;
- if (other is SteamVR_Action)
- return this.Equals((SteamVR_Action)other);
- return false;
- }
-
-
-
- public static bool operator !=(SteamVR_Action action1, SteamVR_Action action2)
- {
- return !(action1 == action2);
- }
-
-
-
- public static bool operator ==(SteamVR_Action action1, SteamVR_Action action2)
- {
- bool action1null = (ReferenceEquals(null, action1) || string.IsNullOrEmpty(action1.actionPath) || action1.GetSourceMap() == null);
- bool action2null = (ReferenceEquals(null, action2) || string.IsNullOrEmpty(action2.actionPath) || action2.GetSourceMap() == null);
- if (action1null && action2null)
- return true;
- else if (action1null != action2null)
- return false;
- return action1.Equals(action2);
- }
-
-
-
- public static SteamVR_Action FindExistingActionForPartialPath(string path)
- {
- if (string.IsNullOrEmpty(path) || path.IndexOf('/') == -1)
- return null;
-
-
- string[] pathParts = path.Split('/');
- SteamVR_Action existingAction;
- if (pathParts.Length >= 5 && string.IsNullOrEmpty(pathParts[2]))
- {
- string set = pathParts[2];
- string name = pathParts[4];
- existingAction = SteamVR_Input.GetBaseAction(set, name);
- }
- else
- {
- existingAction = SteamVR_Input.GetBaseActionFromPath(path);
- }
- return existingAction;
- }
- [NonSerialized]
- private string cachedShortName;
-
- public string GetShortName()
- {
- if (cachedShortName == null)
- {
- cachedShortName = SteamVR_Input_ActionFile.GetShortName(fullPath);
- }
- return cachedShortName;
- }
- public void ShowOrigins()
- {
- OpenVR.Input.ShowActionOrigins(actionSet.handle, handle);
- }
- public void HideOrigins()
- {
- OpenVR.Input.ShowActionOrigins(0,0);
- }
- }
- public abstract class SteamVR_Action_Source_Map<SourceElement> : SteamVR_Action_Source_Map where SourceElement : SteamVR_Action_Source, new()
- {
-
-
-
-
- public SourceElement this[SteamVR_Input_Sources inputSource]
- {
- get
- {
- return GetSourceElementForIndexer(inputSource);
- }
- }
- protected virtual void OnAccessSource(SteamVR_Input_Sources inputSource) { }
- protected SourceElement[] sources = new SourceElement[SteamVR_Input_Source.numSources];
-
-
-
- public override void Initialize()
- {
- base.Initialize();
- for (int sourceIndex = 0; sourceIndex < sources.Length; sourceIndex++)
- {
- if (sources[sourceIndex] != null)
- sources[sourceIndex].Initialize();
- }
- }
- protected override void PreinitializeMap(SteamVR_Input_Sources inputSource, SteamVR_Action wrappingAction)
- {
- int sourceIndex = (int)inputSource;
- sources[sourceIndex] = new SourceElement();
- sources[sourceIndex].Preinitialize(wrappingAction, inputSource);
- }
-
- protected virtual SourceElement GetSourceElementForIndexer(SteamVR_Input_Sources inputSource)
- {
- int sourceIndex = (int)inputSource;
- OnAccessSource(inputSource);
- return sources[sourceIndex];
- }
- }
- public abstract class SteamVR_Action_Source_Map
- {
-
- public string fullPath { get; protected set; }
-
- public ulong handle { get; protected set; }
-
- public SteamVR_ActionSet actionSet { get; protected set; }
-
- public SteamVR_ActionDirections direction { get; protected set; }
-
- public SteamVR_Action action;
- public virtual void PreInitialize(SteamVR_Action wrappingAction, string actionPath, bool throwErrors = true)
- {
- fullPath = actionPath;
- action = wrappingAction;
- actionSet = SteamVR_Input.GetActionSetFromPath(GetActionSetPath());
- direction = GetActionDirection();
- SteamVR_Input_Sources[] sources = SteamVR_Input_Source.GetAllSources();
- for (int sourceIndex = 0; sourceIndex < sources.Length; sourceIndex++)
- {
- PreinitializeMap(sources[sourceIndex], wrappingAction);
- }
- }
-
-
-
- protected abstract void PreinitializeMap(SteamVR_Input_Sources inputSource, SteamVR_Action wrappingAction);
-
-
-
- public virtual void Initialize()
- {
- ulong newHandle = 0;
- EVRInputError err = OpenVR.Input.GetActionHandle(fullPath.ToLowerInvariant(), ref newHandle);
- handle = newHandle;
- if (err != EVRInputError.None)
- Debug.LogError("<b>[SteamVR]</b> GetActionHandle (" + fullPath.ToLowerInvariant() + ") error: " + err.ToString());
- }
- private string GetActionSetPath()
- {
- int actionsEndIndex = fullPath.IndexOf('/', 1);
- int setStartIndex = actionsEndIndex + 1;
- int setEndIndex = fullPath.IndexOf('/', setStartIndex);
- int count = setEndIndex;
- return fullPath.Substring(0, count);
- }
- private static string inLowered = "IN".ToLower(System.Globalization.CultureInfo.CurrentCulture);
- private static string outLowered = "OUT".ToLower(System.Globalization.CultureInfo.CurrentCulture);
- private SteamVR_ActionDirections GetActionDirection()
- {
- int actionsEndIndex = fullPath.IndexOf('/', 1);
- int setStartIndex = actionsEndIndex + 1;
- int setEndIndex = fullPath.IndexOf('/', setStartIndex);
- int directionEndIndex = fullPath.IndexOf('/', setEndIndex + 1);
- int count = directionEndIndex - setEndIndex - 1;
- string direction = fullPath.Substring(setEndIndex + 1, count);
- if (direction == inLowered)
- return SteamVR_ActionDirections.In;
- else if (direction == outLowered)
- return SteamVR_ActionDirections.Out;
- else
- Debug.LogError("Could not find match for direction: " + direction);
- return SteamVR_ActionDirections.In;
- }
- }
- public abstract class SteamVR_Action_Source : ISteamVR_Action_Source
- {
-
- public string fullPath { get { return action.fullPath; } }
-
- public ulong handle { get { return action.handle; } }
-
- public SteamVR_ActionSet actionSet { get { return action.actionSet; } }
-
- public SteamVR_ActionDirections direction { get { return action.direction; } }
-
- public SteamVR_Input_Sources inputSource { get; protected set; }
-
- public bool setActive { get { return actionSet.IsActive(inputSource); } }
-
- public abstract bool active { get; }
-
- public abstract bool activeBinding { get; }
-
- public abstract bool lastActive { get; protected set; }
-
- public abstract bool lastActiveBinding { get; }
- protected ulong inputSourceHandle;
- protected SteamVR_Action action;
-
-
-
- public virtual void Preinitialize(SteamVR_Action wrappingAction, SteamVR_Input_Sources forInputSource)
- {
- action = wrappingAction;
- inputSource = forInputSource;
- }
- public SteamVR_Action_Source() { }
-
-
-
-
- public virtual void Initialize()
- {
- inputSourceHandle = SteamVR_Input_Source.GetHandle(inputSource);
- }
- }
- public interface ISteamVR_Action : ISteamVR_Action_Source
- {
-
-
- bool GetActive(SteamVR_Input_Sources inputSource);
-
- string GetShortName();
- }
- public interface ISteamVR_Action_Source
- {
-
- bool active { get; }
-
- bool activeBinding { get; }
-
- bool lastActive { get; }
-
- bool lastActiveBinding { get; }
-
- string fullPath { get; }
-
- ulong handle { get; }
-
- SteamVR_ActionSet actionSet { get; }
-
- SteamVR_ActionDirections direction { get; }
- }
- }
|