//======= Copyright (c) Valve Corporation, All rights reserved. =============== // Action implementation overview: // Actions are split into three parts: // * Action: The user-accessible class that is the interface for accessing action data. // There may be many Action instances per Actual SteamVR Action, but these instances are just interfaces to the data and should have virtually no overhead. // * Action Map: This is basically a wrapper for a list of Action_Source instances. // The idea being there is one Map per Actual SteamVR Action. // These maps can be retrieved from a static store in SteamVR_Input so we're not duplicating data. // * Action Source: This is a collection of cached data retrieved by calls to the underlying SteamVR Input system. // Each Action Source has an inputSource that it is associated with. using UnityEngine; using System.Collections; using System; using Valve.VR; using System.Runtime.InteropServices; using System.Collections.Generic; namespace Valve.VR { [Serializable] /// /// This is the base level action for SteamVR Input Actions. All SteamVR_Action_In and SteamVR_Action_Out inherit from this. /// Initializes the ulong handle for the action, has some helper references that all actions will have. /// public abstract class SteamVR_Action : SteamVR_Action, ISteamVR_Action where SourceMap : SteamVR_Action_Source_Map, new() where SourceElement : SteamVR_Action_Source, new() { /// /// The map to the source elements, a dictionary of source elements. Should be accessed through the action indexer /// [NonSerialized] protected SourceMap sourceMap; /// /// Access this action restricted to individual input sources. /// /// The input source to access for this action public virtual SourceElement this[SteamVR_Input_Sources inputSource] { get { return sourceMap[inputSource]; } } /// The full string path for this action public override string fullPath { get { return sourceMap.fullPath; } } /// The underlying handle for this action used for native SteamVR Input calls public override ulong handle { get { return sourceMap.handle; } } /// The actionset this action is contained within public override SteamVR_ActionSet actionSet { get { return sourceMap.actionSet; } } /// The action direction of this action (in for input - most actions, out for output - mainly haptics) public override SteamVR_ActionDirections direction { get { return sourceMap.direction; } } /// [Shortcut to: SteamVR_Input_Sources.Any] Returns true if the action is bound and the actionset is active public override bool active { get { return sourceMap[SteamVR_Input_Sources.Any].active; } } /// [Shortcut to: SteamVR_Input_Sources.Any] Returns true if the action was bound and the ActionSet was active during the previous update public override bool lastActive { get { return sourceMap[SteamVR_Input_Sources.Any].lastActive; } } /// [Shortcut to: SteamVR_Input_Sources.Any] Returns true if the action is bound public override bool activeBinding { get { return sourceMap[SteamVR_Input_Sources.Any].activeBinding; } } /// [Shortcut to: SteamVR_Input_Sources.Any] Returns true if the action was bound at the previous update public override bool lastActiveBinding { get { return sourceMap[SteamVR_Input_Sources.Any].lastActiveBinding; } } [NonSerialized] protected bool initialized = false; /// /// Prepares the action to be initialized. Creating dictionaries, finding the right existing action, etc. /// 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; } /// /// [Should not be called by user code] If it looks like we aren't attached to a real action then try and find the existing action for our given path. /// 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; } /// /// [Should not be called by user code] Initializes the individual sources as well as the base map itself. /// Gets the handle for the action from SteamVR and does any other SteamVR related setup that needs to be done /// public override void Initialize(bool createNew = false, bool throwErrors = true) { if (needsReinit) { TryNeedsInitData(); } if (createNew) { sourceMap.Initialize(); } else { sourceMap = SteamVR_Input.GetActionDataFromPath(actionPath); if (sourceMap == null) { #if UNITY_EDITOR if (throwErrors) { if (string.IsNullOrEmpty(actionPath)) { Debug.LogError("[SteamVR] Action has not been assigned."); } else { Debug.LogError("[SteamVR] Could not find action with path: " + actionPath); } } #endif } } initialized = true; } /// /// [Should not be called by user code] Returns the underlying source map for the action. /// [Should not be called by user code] Returns the underlying source map for the action. /// 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); } } /// /// Gets a value indicating whether or not the action is currently bound and if the containing action set is active /// /// The device you would like to get data from. Any if the action is not device specific. public override bool GetActive(SteamVR_Input_Sources inputSource) { return sourceMap[inputSource].active; } /// /// Gets a value indicating whether or not the action is currently bound /// /// The device you would like to get data from. Any if the action is not device specific. public override bool GetActiveBinding(SteamVR_Input_Sources inputSource) { return sourceMap[inputSource].activeBinding; } /// /// Gets the value from the previous update indicating whether or not the action was currently bound and if the containing action set was active /// /// The device you would like to get data from. Any if the action is not device specific. public override bool GetLastActive(SteamVR_Input_Sources inputSource) { return sourceMap[inputSource].lastActive; } /// /// Gets the value from the previous update indicating whether or not the action is currently bound /// /// The device you would like to get data from. Any if the action is not device specific. public override bool GetLastActiveBinding(SteamVR_Input_Sources inputSource) { return sourceMap[inputSource].lastActiveBinding; } } [Serializable] public abstract class SteamVR_Action : IEquatable, ISteamVR_Action { public SteamVR_Action() { } [SerializeField] protected string actionPath; [SerializeField] protected bool needsReinit; /// /// Not recommended. Determines if we should do a lazy-loading style of updating actions where we don't check for their data until the code asks for it. Note: You will have to manually activate actions otherwise. Not recommended. /// public static bool startUpdatingSourceOnAccess = true; /// /// [Should not be called by user code] Creates an actual action that will later be called by user code. /// public static CreateType Create(string newActionPath) where CreateType : SteamVR_Action, new() { CreateType action = new CreateType(); action.PreInitialize(newActionPath); return action; } /// /// [Should not be called by user code] Creates an uninitialized action that can be saved without being attached to a real action /// public static CreateType CreateUninitialized(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; } /// /// [Should not be called by user code] Creates an uninitialized action that can be saved without being attached to a real action /// public static CreateType CreateUninitialized(string actionPath, bool caseSensitive) where CreateType : SteamVR_Action, new() { CreateType action = new CreateType(); action.CreateUninitialized(actionPath, caseSensitive); return action; } /// /// [Should not be called by user code] Gets a copy of the underlying source map so we're always using the same underlying event data /// public CreateType GetCopy() where CreateType : SteamVR_Action, new() { if (SteamVR_Input.ShouldMakeCopy()) //no need to make copies at runtime { 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); /// The full string path for this action public abstract string fullPath { get; } /// The underlying handle for this action used for native SteamVR Input calls public abstract ulong handle { get; } /// The actionset this action is contained within public abstract SteamVR_ActionSet actionSet { get; } /// The action direction of this action (in for input - most actions, out for output - mainly haptics) public abstract SteamVR_ActionDirections direction { get; } /// [Shortcut to: SteamVR_Input_Sources.Any] Returns true if the action set that contains this action is active for Any input source. public bool setActive { get { return actionSet.IsActive(SteamVR_Input_Sources.Any); } } /// [Shortcut to: SteamVR_Input_Sources.Any] Returns true if the action is bound and the actionset is active public abstract bool active { get; } /// [Shortcut to: SteamVR_Input_Sources.Any] Returns true if the action is bound public abstract bool activeBinding { get; } /// [Shortcut to: SteamVR_Input_Sources.Any] Returns true if the action was bound and the actionset was active at the previous update public abstract bool lastActive { get; } /// /// /// public abstract bool lastActiveBinding { get; } /// /// Prepares the action to be initialized. Creating dictionaries, finding the right existing action, etc. /// 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); /// /// Initializes the individual sources as well as the base map itself. Gets the handle for the action from SteamVR and does any other SteamVR related setup that needs to be done /// public abstract void Initialize(bool createNew = false, bool throwNotSetError = true); /// Gets the last timestamp this action was changed. (by Time.realtimeSinceStartup) /// The input source to use to select the last changed time public abstract float GetTimeLastChanged(SteamVR_Input_Sources inputSource); public abstract SteamVR_Action_Source_Map GetSourceMap(); /// /// Gets a value indicating whether or not the action is currently bound and if the containing action set is active /// /// The device you would like to get data from. Any if the action is not device specific. public abstract bool GetActive(SteamVR_Input_Sources inputSource); /// /// Gets a value indicating whether or not the containing action set is active /// /// The device you would like to get data from. Any if the action is not device specific. public bool GetSetActive(SteamVR_Input_Sources inputSource) { return actionSet.IsActive(inputSource); } /// /// Gets a value indicating whether or not the action is currently bound /// /// The device you would like to get data from. Any if the action is not device specific. public abstract bool GetActiveBinding(SteamVR_Input_Sources inputSource); /// /// Gets the value from the previous update indicating whether or not the action is currently bound and if the containing action set is active /// /// The device you would like to get data from. Any if the action is not device specific. public abstract bool GetLastActive(SteamVR_Input_Sources inputSource); /// /// Gets the value from the previous update indicating whether or not the action is currently bound /// /// The device you would like to get data from. Any if the action is not device specific. public abstract bool GetLastActiveBinding(SteamVR_Input_Sources inputSource); /// Returns the full action path for this action. public string GetPath() { return actionPath; } /// /// Returns true if the data for this action is being updated for the specified input source. This can be triggered by querying the data /// public abstract bool IsUpdating(SteamVR_Input_Sources inputSource); /// /// Creates a hashcode from the full action path of this action /// public override int GetHashCode() { if (actionPath == null) return 0; else return actionPath.GetHashCode(); } /// /// Compares two SteamVR_Actions by their action path instead of references /// public bool Equals(SteamVR_Action other) { if (ReferenceEquals(null, other)) return false; //SteamVR_Action_Source_Map thisMap = this.GetSourceMap(); //SteamVR_Action_Source_Map otherMap = other.GetSourceMap(); //return this.actionPath == other.actionPath && thisMap.fullPath == otherMap.fullPath; return this.actionPath == other.actionPath; } /// /// Compares two SteamVR_Actions by their action path instead of references /// public override bool Equals(object other) { if (ReferenceEquals(null, other)) { if (string.IsNullOrEmpty(this.actionPath)) //if we haven't set a path, say this action is equal to null 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; } /// /// Compares two SteamVR_Actions by their action path. /// public static bool operator !=(SteamVR_Action action1, SteamVR_Action action2) { return !(action1 == action2); } /// /// Compares two SteamVR_Actions by their action path. /// 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); } /// /// Tries to find an existing action matching some subsection of an action path. More useful functions in SteamVR_Input. /// public static SteamVR_Action FindExistingActionForPartialPath(string path) { if (string.IsNullOrEmpty(path) || path.IndexOf('/') == -1) return null; // 0 1 2 3 4 // /actions/default/in/foobar 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; /// Gets just the name of this action. The last part of the path for this action. Removes action set, and direction. 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 : SteamVR_Action_Source_Map where SourceElement : SteamVR_Action_Source, new() { /// /// Gets a reference to the action restricted to a certain input source. LeftHand or RightHand for example. /// /// The device you would like data from 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]; /// /// [Should not be called by user code] Initializes the individual sources as well as the base map itself. Gets the handle for the action from SteamVR and does any other SteamVR related setup that needs to be done /// 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); } // Normally I'd just make the indexer virtual and override that but some unity versions don't like that protected virtual SourceElement GetSourceElementForIndexer(SteamVR_Input_Sources inputSource) { int sourceIndex = (int)inputSource; OnAccessSource(inputSource); return sources[sourceIndex]; } } public abstract class SteamVR_Action_Source_Map { /// The full string path for this action (from the action manifest) public string fullPath { get; protected set; } /// The underlying handle for this action used for native SteamVR Input calls. Retrieved on Initialization from SteamVR. public ulong handle { get; protected set; } /// The ActionSet this action is contained within public SteamVR_ActionSet actionSet { get; protected set; } /// The action direction of this action (in for input - most actions, out for output - haptics) public SteamVR_ActionDirections direction { get; protected set; } /// The base SteamVR_Action this map corresponds to 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); } } /// /// [Should not be called by user code] Sets up the internals of the action source before SteamVR has been initialized. /// protected abstract void PreinitializeMap(SteamVR_Input_Sources inputSource, SteamVR_Action wrappingAction); /// /// [Should not be called by user code] Initializes the handle for the action and any other related SteamVR data. /// public virtual void Initialize() { ulong newHandle = 0; EVRInputError err = OpenVR.Input.GetActionHandle(fullPath.ToLowerInvariant(), ref newHandle); handle = newHandle; if (err != EVRInputError.None) Debug.LogError("[SteamVR] 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 { /// The full string path for this action (from the action manifest) public string fullPath { get { return action.fullPath; } } /// The underlying handle for this action used for native SteamVR Input calls. Retrieved on Initialization from SteamVR. public ulong handle { get { return action.handle; } } /// The ActionSet this action is contained within public SteamVR_ActionSet actionSet { get { return action.actionSet; } } /// The action direction of this action (in for input - most actions, out for output - haptics) public SteamVR_ActionDirections direction { get { return action.direction; } } /// The input source that this instance corresponds to. ex. LeftHand, RightHand public SteamVR_Input_Sources inputSource { get; protected set; } /// Returns true if the action set this is contained in is active for this input source (or Any) public bool setActive { get { return actionSet.IsActive(inputSource); } } /// Returns true if this action is bound and the ActionSet is active public abstract bool active { get; } /// Returns true if the action is bound public abstract bool activeBinding { get; } /// Returns true if the action was bound and the ActionSet was active during the previous update public abstract bool lastActive { get; protected set; } /// Returns true if the action was bound during the previous update public abstract bool lastActiveBinding { get; } protected ulong inputSourceHandle; protected SteamVR_Action action; /// /// [Should not be called by user code] Sets up the internals of the action source before SteamVR has been initialized. /// public virtual void Preinitialize(SteamVR_Action wrappingAction, SteamVR_Input_Sources forInputSource) { action = wrappingAction; inputSource = forInputSource; } public SteamVR_Action_Source() { } /// /// [Should not be called by user code] /// Initializes the handle for the inputSource, and any other related SteamVR data. /// public virtual void Initialize() { inputSourceHandle = SteamVR_Input_Source.GetHandle(inputSource); } } public interface ISteamVR_Action : ISteamVR_Action_Source { /// Returns the active state of the action for the specified Input Source /// The input source to check bool GetActive(SteamVR_Input_Sources inputSource); /// Returns the name of the action without the action set or direction string GetShortName(); } public interface ISteamVR_Action_Source { /// Returns true if this action is bound and the ActionSet is active bool active { get; } /// Returns true if the action is bound bool activeBinding { get; } /// Returns true if the action was bound and the ActionSet was active during the previous update bool lastActive { get; } /// Returns true if the action was bound last update bool lastActiveBinding { get; } /// The full string path for this action (from the action manifest) string fullPath { get; } /// The underlying handle for this action used for native SteamVR Input calls. Retrieved on Initialization from SteamVR. ulong handle { get; } /// The ActionSet this action is contained within SteamVR_ActionSet actionSet { get; } /// The action direction of this action (in for input, out for output) SteamVR_ActionDirections direction { get; } } }