//======= Copyright (c) Valve Corporation, All rights reserved. =============== // // Purpose: Simple event system for SteamVR. // // Example usage: // // void OnDeviceConnected(int i, bool connected) { ... } // SteamVR_Events.DeviceConnected.Listen(OnDeviceConnected); // Usually in OnEnable // SteamVR_Events.DeviceConnected.Remove(OnDeviceConnected); // Usually in OnDisable // // Alternatively, if Listening/Removing often these can be cached as follows: // // SteamVR_Event.Action deviceConnectedAction; // void OnAwake() { deviceConnectedAction = SteamVR_Event.DeviceConnectedAction(OnDeviceConnected); } // void OnEnable() { deviceConnectedAction.enabled = true; } // void OnDisable() { deviceConnectedAction.enabled = false; } // //============================================================================= using UnityEngine; using UnityEngine.Events; using Valve.VR; namespace Valve.VR { public static class SteamVR_Events { public abstract class Action { public abstract void Enable(bool enabled); public bool enabled { set { Enable(value); } } } [System.Serializable] public class ActionNoArgs : Action { public ActionNoArgs(Event _event, UnityAction action) { this._event = _event; this.action = action; } public override void Enable(bool enabled) { if (enabled) _event.Listen(action); else _event.Remove(action); } Event _event; UnityAction action; } [System.Serializable] public class Action : Action { public Action(Event _event, UnityAction action) { this._event = _event; this.action = action; } public override void Enable(bool enabled) { if (enabled) _event.Listen(action); else _event.Remove(action); } Event _event; UnityAction action; } [System.Serializable] public class Action : Action { public Action(Event _event, UnityAction action) { this._event = _event; this.action = action; } public override void Enable(bool enabled) { if (enabled) _event.Listen(action); else _event.Remove(action); } Event _event; UnityAction action; } [System.Serializable] public class Action : Action { public Action(Event _event, UnityAction action) { this._event = _event; this.action = action; } public override void Enable(bool enabled) { if (enabled) _event.Listen(action); else _event.Remove(action); } Event _event; UnityAction action; } public class Event : UnityEvent { public void Listen(UnityAction action) { this.AddListener(action); } public void Remove(UnityAction action) { this.RemoveListener(action); } public void Send() { this.Invoke(); } } public class Event : UnityEvent { public void Listen(UnityAction action) { this.AddListener(action); } public void Remove(UnityAction action) { this.RemoveListener(action); } public void Send(T arg0) { this.Invoke(arg0); } } public class Event : UnityEvent { public void Listen(UnityAction action) { this.AddListener(action); } public void Remove(UnityAction action) { this.RemoveListener(action); } public void Send(T0 arg0, T1 arg1) { this.Invoke(arg0, arg1); } } public class Event : UnityEvent { public void Listen(UnityAction action) { this.AddListener(action); } public void Remove(UnityAction action) { this.RemoveListener(action); } public void Send(T0 arg0, T1 arg1, T2 arg2) { this.Invoke(arg0, arg1, arg2); } } public static Event Calibrating = new Event(); public static Action CalibratingAction(UnityAction action) { return new Action(Calibrating, action); } public static Event DeviceConnected = new Event(); public static Action DeviceConnectedAction(UnityAction action) { return new Action(DeviceConnected, action); } public static Event Fade = new Event(); public static Action FadeAction(UnityAction action) { return new Action(Fade, action); } public static Event FadeReady = new Event(); public static Action FadeReadyAction(UnityAction action) { return new ActionNoArgs(FadeReady, action); } public static Event HideRenderModels = new Event(); public static Action HideRenderModelsAction(UnityAction action) { return new Action(HideRenderModels, action); } public static Event Initializing = new Event(); public static Action InitializingAction(UnityAction action) { return new Action(Initializing, action); } public static Event InputFocus = new Event(); public static Action InputFocusAction(UnityAction action) { return new Action(InputFocus, action); } public static Event Loading = new Event(); public static Action LoadingAction(UnityAction action) { return new Action(Loading, action); } public static Event LoadingFadeIn = new Event(); public static Action LoadingFadeInAction(UnityAction action) { return new Action(LoadingFadeIn, action); } public static Event LoadingFadeOut = new Event(); public static Action LoadingFadeOutAction(UnityAction action) { return new Action(LoadingFadeOut, action); } public static Event NewPoses = new Event(); public static Action NewPosesAction(UnityAction action) { return new Action(NewPoses, action); } public static Event NewPosesApplied = new Event(); public static Action NewPosesAppliedAction(UnityAction action) { return new ActionNoArgs(NewPosesApplied, action); } public static Event Initialized = new Event(); public static Action InitializedAction(UnityAction action) { return new Action(Initialized, action); } public static Event OutOfRange = new Event(); public static Action OutOfRangeAction(UnityAction action) { return new Action(OutOfRange, action); } public static Event RenderModelLoaded = new Event(); public static Action RenderModelLoadedAction(UnityAction action) { return new Action(RenderModelLoaded, action); } static System.Collections.Generic.Dictionary> systemEvents = new System.Collections.Generic.Dictionary>(); public static Event System(EVREventType eventType) { Event e; if (!systemEvents.TryGetValue(eventType, out e)) { e = new Event(); systemEvents.Add(eventType, e); } return e; } public static Action SystemAction(EVREventType eventType, UnityAction action) { return new Action(System(eventType), action); } } }