/// <summary>
        /// Get or create an application service
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        private static T GetService <T>() where T : IMixedRealityService
        {
            Type serviceType = typeof(T);

            // See if we already have a WeakReference entry for this service type
            if (_serviceCache.ContainsKey(serviceType))
            {
                IMixedRealityService svc;
                // If our reference object is still alive, return it
                if (_serviceCache[serviceType].TryGetTarget(out svc))
                {
                    return((T)svc);
                }

                // Our reference object has been collected by the GC. Try to get the latest service if available
                _serviceCache.Remove(serviceType);
            }

            // This is the first request for the given service type. See if it is available and if so, add entry
            T service;

            if (!MixedRealityServiceRegistry.TryGetService(out service))
            {
                return(default(T));
            }

            _serviceCache.Add(typeof(T), new WeakReference <IMixedRealityService>(service, false));
            return(service);
        }
Esempio n. 2
0
        public void Setup()
        {
            PlayModeTestUtilities.Setup();
            PlayModeTestUtilities.EnsureInputModule();

            inputSim = PlayModeTestUtilities.GetInputSimulationService();
            Assert.NotNull(inputSim);

            Assert.IsTrue(MixedRealityServiceRegistry.TryGetService <IMixedRealityInputSystem>(out inputSystem));
            Assert.NotNull(inputSystem);

            GameObject lightObj = new GameObject("Light");

            lightObj.transform.rotation = Quaternion.FromToRotation(Vector3.forward, new Vector3(1, -4, 2));
            Light light = lightObj.AddComponent <Light>();

            light.type = LightType.Directional;

            var shader = StandardShaderUtility.MrtkStandardShader;

            idleMaterial       = new Material(shader);
            idleMaterial.color = Color.yellow;

            pokeMaterial       = new Material(shader);
            pokeMaterial.color = Color.green;
        }
        public IEnumerator TestPointerEventCallsForGlobalHandlers()
        {
            // We need Gaze Cursor in this test to use it as source to emit events.
            IMixedRealityInputSystem inputSystem = null;

            MixedRealityServiceRegistry.TryGetService(out inputSystem);

            var object1 = new GameObject("Object 1");

            // Second handler depends on the first one as they sit on the same object
            var objectBasedListener  = object1.AddComponent <TestInputGlobalListenerObjectBased>();
            var handlerBasedListener = object1.AddComponent <TestInputGlobalListenerHandlerBasedSpeechHandler>();

            var object2 = new GameObject("Object 2");

            // These 2 handlers are independent
            // 1st is Pointer + Speech
            // 2nd is Speech only
            var handlerBasedListener1 = object2.AddComponent <TestInputGlobalListenerHandlerBasedAllHandlers>();
            var handlerBasedListener2 = object2.AddComponent <TestInputGlobalListenerHandlerBasedSpeechHandler>();

            yield return(null);

            LogAssert.Expect(LogType.Error, new Regex("Detected simultaneous usage of IMixedRealityEventSystem.Register and IMixedRealityEventSystem.RegisterHandler"));

            // Emit pointer event, which should be received by global handler, poor speech handler, which is on the same object as global,
            // and a handler listening to all events.
            inputSystem.RaisePointerClicked(inputSystem.GazeProvider.GazePointer, MixedRealityInputAction.None, 1);

            Assert.AreEqual(objectBasedListener.pointerClickedCount, 1, "Pointer clicked event is not received by old API handler.");
            Assert.Zero(objectBasedListener.pointerDownCount, "Pointer down event is received by old API handler.");
            Assert.Zero(objectBasedListener.pointerUpCount, "Pointer up event is received by old API handler.");
            Assert.Zero(objectBasedListener.pointerDraggedCount, "Pointer dragged event is received by old API handler.");
            Assert.Zero(objectBasedListener.speechCommandsReceived.Count(), "Speech event is received by old API handler.");

            // Wrong behavior, preserved for backward compatibility
            Assert.AreEqual(handlerBasedListener.pointerClickedCount, 1, "Pointer clicked event is not received by new API handler.");
            Assert.Zero(handlerBasedListener.pointerDownCount, "Pointer down event is received by new  API handler.");
            Assert.Zero(handlerBasedListener.pointerUpCount, "Pointer up event is received by new API handler.");
            Assert.Zero(handlerBasedListener.pointerDraggedCount, "Pointer dragged event is received by new API handler.");
            Assert.Zero(handlerBasedListener.speechCommandsReceived.Count(), "Speech event is received by new API handler.");

            Assert.AreEqual(handlerBasedListener1.pointerClickedCount, 1, "Pointer clicked event is not received by all-handlers component.");
            Assert.Zero(handlerBasedListener1.pointerDownCount, "Pointer down event is received by all-handlers component.");
            Assert.Zero(handlerBasedListener1.pointerUpCount, "Pointer up event is received by all-handlers component.");
            Assert.Zero(handlerBasedListener1.pointerDraggedCount, "Pointer dragged event is received by all-handlers component.");
            Assert.Zero(handlerBasedListener1.speechCommandsReceived.Count(), "Speech event is received by all-handlers component.");

            // No pointer clicked event:
            Assert.Zero(handlerBasedListener2.pointerClickedCount, "Pointer clicked event is received by speech-handler component.");
            Assert.Zero(handlerBasedListener2.pointerDownCount, "Pointer down event is received by speech-handler component.");
            Assert.Zero(handlerBasedListener2.pointerUpCount, "Pointer up event is received by speech-handler component.");
            Assert.Zero(handlerBasedListener2.pointerDraggedCount, "Pointer dragged event is received by speech-handler component.");
            Assert.Zero(handlerBasedListener2.speechCommandsReceived.Count(), "Speech event is received by speech-handler component.");

            Object.Destroy(object1);
            Object.Destroy(object2);

            yield return(null);
        }
        public static IMixedRealityInputSystem GetInputSystem()
        {
            IMixedRealityInputSystem inputSystem;

            MixedRealityServiceRegistry.TryGetService(out inputSystem);
            Assert.IsNotNull(inputSystem, "MixedRealityInputSystem is null!");
            return(inputSystem);
        }
        public IEnumerator TestSpeechEventCallsForGlobalHandlers()
        {
            // We need Gaze Cursor in this test to use it as source to emit events.
            IMixedRealityInputSystem inputSystem = null;

            MixedRealityServiceRegistry.TryGetService(out inputSystem);

            var object1 = new GameObject("Object 1");

            // These 2 handlers are independent
            var objectBasedListener  = object1.AddComponent <TestInputGlobalListenerObjectBased>();
            var handlerBasedListener = object1.AddComponent <TestInputGlobalListenerHandlerBasedSpeechHandler>();

            var object2 = new GameObject("Object 2");

            // These 2 handlers are independent
            var handlerBasedListener1 = object2.AddComponent <TestInputGlobalListenerHandlerBasedAllHandlers>();
            var handlerBasedListener2 = object2.AddComponent <TestInputGlobalListenerHandlerBasedSpeechHandler>();

            yield return(null);

            LogAssert.Expect(LogType.Error, new Regex("Detected simultaneous usage of IMixedRealityEventSystem.Register and IMixedRealityEventSystem.RegisterHandler"));

            // Emit speech event, which should be received by all handlers.
            var gazeInputSource = inputSystem.DetectedInputSources.Where(x => x.SourceName.Equals("Gaze")).First();

            inputSystem.RaiseSpeechCommandRecognized(gazeInputSource, RecognitionConfidenceLevel.High, new System.TimeSpan(), System.DateTime.Now, new SpeechCommands("menu", KeyCode.Alpha1, MixedRealityInputAction.None));

            Assert.Zero(objectBasedListener.pointerClickedCount, "Pointer clicked event is received by old API handler.");
            Assert.Zero(objectBasedListener.pointerDownCount, "Pointer down event is received by old API handler.");
            Assert.Zero(objectBasedListener.pointerUpCount, "Pointer up event is received by old API handler.");
            Assert.Zero(objectBasedListener.pointerDraggedCount, "Pointer dragged event is received by old API handler.");
            Assert.AreEqual(objectBasedListener.speechCount, 1, "Speech event is not received by old API handler.");

            Assert.Zero(handlerBasedListener.pointerClickedCount, "Pointer clicked event is received by new API handler.");
            Assert.Zero(handlerBasedListener.pointerDownCount, "Pointer down event is received by new  API handler.");
            Assert.Zero(handlerBasedListener.pointerUpCount, "Pointer up event is received by new API handler.");
            Assert.Zero(handlerBasedListener.pointerDraggedCount, "Pointer dragged event is received by new API handler.");
            Assert.AreEqual(handlerBasedListener.speechCount, 1, "Speech event is not received by new API handler.");

            Assert.Zero(handlerBasedListener1.pointerClickedCount, "Pointer clicked event is received by all-handlers component.");
            Assert.Zero(handlerBasedListener1.pointerDownCount, "Pointer down event is received by all-handlers component.");
            Assert.Zero(handlerBasedListener1.pointerUpCount, "Pointer up event is received by all-handlers component.");
            Assert.Zero(handlerBasedListener1.pointerDraggedCount, "Pointer dragged event is received by all-handlers component.");
            Assert.AreEqual(handlerBasedListener1.speechCount, 1, "Speech event is not received by all-handlers component.");

            // No pointer clicked event:
            Assert.Zero(handlerBasedListener2.pointerClickedCount, "Pointer clicked event is received by speech-handler component.");
            Assert.Zero(handlerBasedListener2.pointerDownCount, "Pointer down event is received by speech-handler component.");
            Assert.Zero(handlerBasedListener2.pointerUpCount, "Pointer up event is received by speech-handler component.");
            Assert.Zero(handlerBasedListener2.pointerDraggedCount, "Pointer dragged event is received by speech-handler component.");
            Assert.AreEqual(handlerBasedListener2.speechCount, 1, "Speech event is not received by speech-handler component.");

            Object.Destroy(object1);
            Object.Destroy(object2);

            yield return(null);
        }
        public void Destroy()
        {
            IMixedRealityInputSystem inputSystem;

            if (MixedRealityServiceRegistry.TryGetService <IMixedRealityInputSystem>(out inputSystem))
            {
                inputSystem.UnregisterHandler <IMixedRealityPointerHandler>(this);
            }
        }
        /// <summary>
        /// Initializes the MRTK such that there are no other input system listeners
        /// (global or per-interface).
        /// </summary>
        internal static IEnumerator SetupMrtkWithoutGlobalInputHandlers()
        {
            if (!MixedRealityToolkit.IsInitialized)
            {
                Debug.LogError("MixedRealityToolkit must be initialized before it can be configured.");
                yield break;
            }

            IMixedRealityInputSystem inputSystem = null;

            MixedRealityServiceRegistry.TryGetService(out inputSystem);

            Assert.IsNotNull(inputSystem, "Input system must be initialized");

            // Let input system to register all cursors and managers.
            yield return(null);

            // Switch off / Destroy all input components, which listen to global events
            UnityEngine.Object.Destroy(inputSystem.GazeProvider.GazeCursor as Behaviour);
            inputSystem.GazeProvider.Enabled = false;

            var diagnosticsVoiceControls = UnityEngine.Object.FindObjectsOfType <DiagnosticsSystemVoiceControls>();

            foreach (var diagnosticsComponent in diagnosticsVoiceControls)
            {
                diagnosticsComponent.enabled = false;
            }

            // Let objects be destroyed
            yield return(null);

            // Forcibly unregister all other input event listeners.
            BaseEventSystem baseEventSystem   = inputSystem as BaseEventSystem;
            MethodInfo      unregisterHandler = baseEventSystem.GetType().GetMethod("UnregisterHandler");

            // Since we are iterating over and removing these values, we need to snapshot them
            // before calling UnregisterHandler on each handler.
            var eventHandlersByType = new Dictionary <System.Type, List <BaseEventSystem.EventHandlerEntry> >(((BaseEventSystem)inputSystem).EventHandlersByType);

            foreach (var typeToEventHandlers in eventHandlersByType)
            {
                var handlerEntries = new List <BaseEventSystem.EventHandlerEntry>(typeToEventHandlers.Value);
                foreach (var handlerEntry in handlerEntries)
                {
                    unregisterHandler.MakeGenericMethod(typeToEventHandlers.Key)
                    .Invoke(baseEventSystem,
                            new object[] { handlerEntry.handler });
                }
            }

            // Check that input system is clean
            CollectionAssert.IsEmpty(((BaseEventSystem)inputSystem).EventListeners, "Input event system handler registry is not empty in the beginning of the test.");
            CollectionAssert.IsEmpty(((BaseEventSystem)inputSystem).EventHandlersByType, "Input event system handler registry is not empty in the beginning of the test.");

            yield return(null);
        }
Esempio n. 8
0
        public void Test02_TestGetMixedRealityInputSystem()
        {
            TestUtilities.InitializeMixedRealityToolkitAndCreateScenes(true);

            // Retrieve Input System
            IMixedRealityInputSystem inputSystem = null;

            MixedRealityServiceRegistry.TryGetService <IMixedRealityInputSystem>(out inputSystem);

            // Tests
            Assert.IsNotNull(inputSystem);
        }
        public void TestGetSpatialAwarenessSystem()
        {
            TestUtilities.InitializeMixedRealityToolkitAndCreateScenes(true);

            // Retrieve Spatial Awareness System
            IMixedRealitySpatialAwarenessSystem spatialAwarenessSystem = null;

            MixedRealityServiceRegistry.TryGetService(out spatialAwarenessSystem);

            // Tests
            Assert.IsNotNull(spatialAwarenessSystem);
        }
    private void OnDisable()
    {
        Camera.onPostRender -= PostRender;

        ISceneTransitionService transitionService;

        if (MixedRealityServiceRegistry.TryGetService <ISceneTransitionService>(out transitionService))
        {
            transitionService.OnTransitionStarted -= OnTransitionStarted;
        }

        updateWisps = false;
    }
Esempio n. 11
0
        public IEnumerator LoadHandInteractionExamplesScene()
        {
            AsyncOperation loadOp = SceneManager.LoadSceneAsync(HandInteractionExamplesSceneName);

            loadOp.allowSceneActivation = true;
            while (!loadOp.isDone)
            {
                yield return(null);
            }

            yield return(new WaitForSeconds(ScenePlayDuration));

            IMixedRealityInputSystem inputSystem = null;

            MixedRealityServiceRegistry.TryGetService(out inputSystem);

            Assert.NotNull(inputSystem);
        }
        public IEnumerator TestGazeCursorArticulated()
        {
            TestUtilities.InitializeMixedRealityToolkitAndCreateScenes(true);
            TestUtilities.InitializePlayspace();

            RenderSettings.skybox = null;

            IMixedRealityInputSystem inputSystem;

            MixedRealityServiceRegistry.TryGetService(out inputSystem);
            Assert.IsNotNull(inputSystem, "MixedRealityInputSystem is null!");

            yield return(null);

            // Verify that the gaze cursor is visible at the start
            Assert.IsTrue(inputSystem.GazeProvider.GazePointer.IsInteractionEnabled, "Gaze cursor should be visible at start");

            // raise hand up -- gaze cursor should no longer be visible
            // disable user input
            InputSimulationService inputSimulationService = (inputSystem as IMixedRealityDataProviderAccess).GetDataProvider <InputSimulationService>();

            Assert.IsNotNull(inputSimulationService, "InputSimulationService is null!");

            inputSimulationService.UserInputEnabled = false;
            ArticulatedHandPose gesturePose = ArticulatedHandPose.GetGesturePose(ArticulatedHandPose.GestureId.Open);
            var handOpenPose = PlayModeTestUtilities.GenerateHandPose(ArticulatedHandPose.GestureId.Open, Handedness.Right, Vector3.forward * 0.1f);

            inputSimulationService.HandDataRight.Update(true, false, handOpenPose);
            yield return(null);

            // Gaze cursor should not be visible
            Assert.IsFalse(inputSystem.GazeProvider.GazePointer.IsInteractionEnabled, "Gaze cursor should not be visible when one articulated hand is up");
            inputSimulationService.HandDataRight.Update(false, false, handOpenPose);
            yield return(null);

            // Say "select" to make gaze cursor active again
            // Really we need to tear down the scene and create it again but MRTK doesn't support that yet
            var gazeInputSource = inputSystem.DetectedInputSources.Where(x => x.SourceName.Equals("Gaze")).First();

            inputSystem.RaiseSpeechCommandRecognized(gazeInputSource, RecognitionConfidenceLevel.High, new System.TimeSpan(), System.DateTime.Now, new SpeechCommands("select", KeyCode.Alpha1, MixedRealityInputAction.None));
            yield return(null);

            Assert.IsTrue(inputSystem.GazeProvider.GazePointer.IsInteractionEnabled, "Gaze cursor should be visible after select command");
        }
Esempio n. 13
0
        public IEnumerator TestToggleProfilerCommand()
        {
            // Confirm that the diagnostics system is enabled.
            IMixedRealityDiagnosticsSystem diagnosticsSystem = null;

            MixedRealityServiceRegistry.TryGetService <IMixedRealityDiagnosticsSystem>(out diagnosticsSystem);
            Assert.IsNotNull(diagnosticsSystem, "The diagnostics system is not enabled in the scene.");
            yield return(null);

            // This test uses the input system to simulate speech commands.
            IMixedRealityInputSystem inputSystem = null;

            MixedRealityServiceRegistry.TryGetService <IMixedRealityInputSystem>(out inputSystem);
            Assert.IsNotNull(inputSystem, "The input system is not enabled in the scene.");
            yield return(null);

            // Verify that the VisualProfiler is enabled.
            Assert.IsTrue(diagnosticsSystem.ShowProfiler, "The VisualProfiler is not active.");
            yield return(null);

            int frameDelay = 10;

            // Toggle the profiler visualization off.
            var gazeInputSource = inputSystem.DetectedInputSources.Where(x => x.SourceName.Equals("Gaze")).First();

            inputSystem.RaiseSpeechCommandRecognized(
                gazeInputSource,
                RecognitionConfidenceLevel.High,
                new TimeSpan(),
                DateTime.Now,
                new SpeechCommands("toggle profiler", KeyCode.Alpha9, MixedRealityInputAction.None));
            // It may take a few frames before the event is handled and the system responds to the state change.
            for (int i = 0; i < frameDelay; i++)
            {
                yield return(null);
            }

            // Verify that the VisualProfiler is disabled.
            Assert.IsFalse(diagnosticsSystem.ShowProfiler, "The VisualProfiler is active (should be inactive).");
            yield return(null);
        }
Esempio n. 14
0
        /// <summary>
        /// Find the first detected hand controller of the given type with matching handedness.
        /// </summary>
        public static T FindHand <T>(Handedness handedness) where T : class, IMixedRealityHand
        {
            IMixedRealityInputSystem inputSystem = null;

            if (!MixedRealityServiceRegistry.TryGetService <IMixedRealityInputSystem>(out inputSystem))
            {
                return(null);
            }

            foreach (var detectedController in inputSystem.DetectedControllers)
            {
                var hand = detectedController as T;
                if (hand != null)
                {
                    if ((detectedController.ControllerHandedness & handedness) != 0)
                    {
                        return(hand);
                    }
                }
            }
            return(null);
        }
        protected virtual bool DoesContextProhibitMenu()
        {
            bool contextProhibited = false;

            // See if we're close to the active surface
            if (FingerSurface.ActiveSurface != null)
            {
                float distToSurface = Vector3.Distance(FingerSurface.ActiveSurface.SurfacePosition, transform.position);
                if (distToSurface - FingerSurface.ActiveSurface.SurfaceRadius < disableDistance)
                {
                    contextProhibited = true;
                }
            }

            // See if we're doing a transition
            ISceneTransitionService transitionService;

            if (MixedRealityServiceRegistry.TryGetService <ISceneTransitionService>(out transitionService) && transitionService.TransitionInProgress)
            {
                contextProhibited = true;
            }

            return(contextProhibited);
        }
        public IEnumerator TestObjectBasedEventRegistration()
        {
            // Need to remove cursors and other global event handlers
            yield return(PlayModeTestUtilities.SetupMrtkWithoutGlobalInputHandlers());

            IMixedRealityInputSystem iInputSystem = null;

            MixedRealityServiceRegistry.TryGetService(out iInputSystem);

            BaseEventSystem inputSystem = (BaseEventSystem)iInputSystem;

            var object1 = new GameObject("Object");

            // Second handler weirdly depends on the first one due to event registration working on the entire object
            var objectBasedListener  = object1.AddComponent <TestInputGlobalListenerObjectBased>();
            var handlerBasedListener = object1.AddComponent <TestInputGlobalListenerHandlerBasedSpeechHandler>();

            yield return(null);

            LogAssert.Expect(LogType.Error, new Regex("Detected simultaneous usage of IMixedRealityEventSystem.Register and IMixedRealityEventSystem.RegisterHandler"));

            // Event listener collection is filled for backward compatibility
            CollectionAssert.AreEquivalent(
                new List <GameObject> {
                objectBasedListener.gameObject
            },
                inputSystem.EventListeners,
                "Event listener for old event system API hasn't been registered correctly.");

            CollectionAssert.AreEquivalent(
                new List <System.Type> {
                typeof(IMixedRealitySpeechHandler), typeof(IMixedRealityBaseInputHandler)
            },
                inputSystem.EventHandlersByType.Keys,
                "Input event system doesn't contain expected event handler types.");

            CollectionAssert.AreEquivalent(
                new HandleList {
                new Handle(handlerBasedListener, true)
            },
                inputSystem.EventHandlersByType[typeof(IMixedRealitySpeechHandler)],
                "Input event system doesn't contain expected IMixedRealitySpeechHandler handlers.");

            CollectionAssert.AreEquivalent(
                new HandleList {
                new Handle(handlerBasedListener, true)
            },
                inputSystem.EventHandlersByType[typeof(IMixedRealityBaseInputHandler)],
                "Input event system doesn't contain expected IMixedRealityBaseInputHandler handlers.");

            // Make sure that disabling global listener doesn't remove the new API one.
            objectBasedListener.enabled = false;

            CollectionAssert.IsEmpty(inputSystem.EventListeners, "Event listener for old event system API shouldn't be registered");

            CollectionAssert.AreEquivalent(
                new List <System.Type> {
                typeof(IMixedRealitySpeechHandler), typeof(IMixedRealityBaseInputHandler)
            },
                inputSystem.EventHandlersByType.Keys,
                "Input event system doesn't contain expected event handler types.");

            CollectionAssert.AreEquivalent(
                new HandleList {
                new Handle(handlerBasedListener, false)
            },
                inputSystem.EventHandlersByType[typeof(IMixedRealitySpeechHandler)],
                "Input event system doesn't contain expected IMixedRealitySpeechHandler handlers.");

            CollectionAssert.AreEquivalent(
                new HandleList {
                new Handle(handlerBasedListener, false)
            },
                inputSystem.EventHandlersByType[typeof(IMixedRealityBaseInputHandler)],
                "Input event system doesn't contain expected IMixedRealityBaseInputHandler handlers.");

            handlerBasedListener.enabled = false;
            CollectionAssert.IsEmpty(inputSystem.EventHandlersByType, "Input event system contains unexpected event handlers.");

            Object.Destroy(object1);
            yield return(null);
        }
        public IEnumerator TestHandlerBasedEventRegistration()
        {
            // Need to remove cursors and other global event handlers
            yield return(PlayModeTestUtilities.SetupMrtkWithoutGlobalInputHandlers());

            IMixedRealityInputSystem iInputSystem = null;

            MixedRealityServiceRegistry.TryGetService(out iInputSystem);

            BaseEventSystem inputSystem = (BaseEventSystem)iInputSystem;

            var object1 = new GameObject("Object");

            // These 2 handlers are independent
            // 1st is Pointer + Speech
            // 2nd is Speech only
            var handlerBasedListener1 = object1.AddComponent <TestInputGlobalListenerHandlerBasedAllHandlers>();
            var handlerBasedListener2 = object1.AddComponent <TestInputGlobalListenerHandlerBasedSpeechHandler>();

            yield return(null);

            // No event listener registration in this test
            CollectionAssert.IsEmpty(inputSystem.EventListeners, "Event listener for old event system API shouldn't be registered");

            CollectionAssert.AreEquivalent(
                new List <System.Type> {
                typeof(IMixedRealityPointerHandler), typeof(IMixedRealitySpeechHandler), typeof(IMixedRealityBaseInputHandler)
            },
                inputSystem.EventHandlersByType.Keys,
                "Input event system doesn't contain expected event handler types.");

            CollectionAssert.AreEquivalent(
                new HandleList {
                new Handle(handlerBasedListener1)
            },
                inputSystem.EventHandlersByType[typeof(IMixedRealityPointerHandler)],
                "Input event system doesn't contain expected IMixedRealityPointerHandler handlers.");

            CollectionAssert.AreEquivalent(
                new HandleList {
                new Handle(handlerBasedListener1), new Handle(handlerBasedListener2)
            },
                inputSystem.EventHandlersByType[typeof(IMixedRealitySpeechHandler)],
                "Input event system doesn't contain expected IMixedRealitySpeechHandler handlers.");

            CollectionAssert.AreEquivalent(
                new HandleList {
                new Handle(handlerBasedListener1), new Handle(handlerBasedListener2)
            },
                inputSystem.EventHandlersByType[typeof(IMixedRealityBaseInputHandler)],
                "Input event system doesn't contain expected IMixedRealityBaseInputHandler handlers.");

            // Disabling one component doesn't influence another one.
            handlerBasedListener1.enabled = false;

            CollectionAssert.AreEquivalent(
                new List <System.Type> {
                typeof(IMixedRealitySpeechHandler), typeof(IMixedRealityBaseInputHandler)
            },
                inputSystem.EventHandlersByType.Keys,
                "Input event system doesn't contain expected event handler types.");

            CollectionAssert.AreEquivalent(
                new HandleList {
                new Handle(handlerBasedListener2)
            },
                inputSystem.EventHandlersByType[typeof(IMixedRealitySpeechHandler)],
                "Input event system doesn't contain expected IMixedRealitySpeechHandler handlers.");

            CollectionAssert.AreEquivalent(
                new HandleList {
                new Handle(handlerBasedListener2)
            },
                inputSystem.EventHandlersByType[typeof(IMixedRealityBaseInputHandler)],
                "Input event system doesn't contain expected IMixedRealityBaseInputHandler handlers.");

            Object.Destroy(object1);
            yield return(null);
        }
Esempio n. 18
0
        public async Task PlaceNewSurface()
        {
            await Task.Yield();

            // Find our finger surface
            FingerSurface fingerSurface       = GameObject.FindObjectOfType <FingerSurface>();
            float         fingerSurfaceRadius = fingerSurface.SurfaceRadius;

            // Use our camera to place the surface
            placementTransform.position = CameraCache.Main.transform.position;
            placementTransform.rotation = CameraCache.Main.transform.rotation;
            // Only rotate on y axis
            Vector3 eulerAngles = placementTransform.eulerAngles;

            eulerAngles.x = 0;
            eulerAngles.z = 0;
            placementTransform.eulerAngles = eulerAngles;

            Vector3 targetSurfacePosition = placementTransform.TransformPoint(defaultOffset);

            if (fingerSurface == null)
            {
                Debug.LogError("No surface found in surface placement.");
                return;
            }

            if (placedOnce)
            {   // If we've placed it once already, the current placement might still be valid.
                if (Vector3.Distance(CameraCache.Main.transform.position, currentSurfacePosition) < maxRepositionDistance)
                {
                    targetSurfacePosition = currentSurfacePosition;
                }
            }
            else if (useSpatialUnderstanding)
            {
                IMixedRealitySpatialAwarenessSystem spatial;
                if (!MixedRealityServiceRegistry.TryGetService <IMixedRealitySpatialAwarenessSystem>(out spatial))
                {
                    Debug.LogError("This component requires a IMixedRealitySpatialAwarenessSystem to be enabled.");
                    return;
                }

                // Start the observers creating meshes again
                spatial.ResumeObservers();

                // Wait until our spatial understanding has delivered some meshes
                timeStarted = Time.time;
                while (spatial.SpatialAwarenessObjectParent.transform.childCount == 0)
                {
                    if ((Time.time - timeStarted > timeOut))
                    {
                        break;
                    }

                    await Task.Yield();
                }

                // While our placement sphere overlaps with spatial awareness objects, move it progressively closer to the user
                bool collidesWithWalls = true;
                timeStarted = Time.time;
                while (collidesWithWalls)
                {
                    if (Time.time - timeStarted > timeOut)
                    {
                        break;
                    }

                    Collider[] colliders = Physics.OverlapSphere(targetSurfacePosition, fingerSurfaceRadius, spatialAwarenessMask);
                    if (colliders.Length == 0)
                    {
                        collidesWithWalls = false;
                    }
                    else
                    {
                        targetSurfacePosition = Vector3.MoveTowards(targetSurfacePosition, CameraCache.Main.transform.position, moveIncrement);
                    }

                    await Task.Yield();
                }

                // Suspeend observers now that we're all set
                spatial.SuspendObservers();
            }

            currentSurfacePosition = targetSurfacePosition;
            placedOnce             = true;

            // Tell the surface to initialize
            fingerSurface.Initialize(targetSurfacePosition);
        }
        public IEnumerator TestInputSystemGlobalHandlerListener()
        {
            // Need to remove cursors and other global event handlers
            yield return(PlayModeTestUtilities.SetupMrtkWithoutGlobalInputHandlers());

            IMixedRealityInputSystem iInputSystem = null;

            MixedRealityServiceRegistry.TryGetService(out iInputSystem);

            BaseEventSystem inputSystem = (BaseEventSystem)iInputSystem;

            var object1 = new GameObject("Object");

            var listener = object1.AddComponent <TestInputGlobalHandlerListener>();

            yield return(null);

            // No event listener registration in this test
            CollectionAssert.IsEmpty(inputSystem.EventListeners, "Event listener for old event system API shouldn't be registered");

            CollectionAssert.AreEquivalent(
                new List <System.Type> {
                typeof(IMixedRealityHandJointHandler),
                typeof(IMixedRealitySpeechHandler),
                typeof(IMixedRealityBaseInputHandler),
                typeof(IMixedRealityInputHandler <float>)
            },
                inputSystem.EventHandlersByType.Keys,
                "Input event system doesn't contain expected event handler types.");

            CollectionAssert.AreEquivalent(
                new HandleList {
                new Handle(listener)
            },
                inputSystem.EventHandlersByType[typeof(IMixedRealityHandJointHandler)],
                "Input event system doesn't contain expected IMixedRealityHandJointHandler handlers.");

            CollectionAssert.AreEquivalent(
                new HandleList {
                new Handle(listener)
            },
                inputSystem.EventHandlersByType[typeof(IMixedRealitySpeechHandler)],
                "Input event system doesn't contain expected IMixedRealitySpeechHandler handlers.");

            CollectionAssert.AreEquivalent(
                new HandleList {
                new Handle(listener)
            },
                inputSystem.EventHandlersByType[typeof(IMixedRealityBaseInputHandler)],
                "Input event system doesn't contain expected IMixedRealityBaseInputHandler handlers.");

            CollectionAssert.AreEquivalent(
                new HandleList {
                new Handle(listener)
            },
                inputSystem.EventHandlersByType[typeof(IMixedRealityInputHandler <float>)],
                "Input event system doesn't contain expected IMixedRealityInputHandler<float> handlers.");

            Object.Destroy(object1);
            yield return(null);
        }
    public override void Initialize(Vector3 surfacePosition)
    {
        base.Initialize(surfacePosition);

        ISceneTransitionService transitionService;

        if (MixedRealityServiceRegistry.TryGetService <ISceneTransitionService>(out transitionService))
        {
            transitionService.OnTransitionStarted += OnTransitionStarted;
        }

        tileScale   = 1f / numColumns;
        tileOffsets = new Vector2[numTiles];

        int tileNum = 0;

        for (int row = 0; row < numRows; row++)
        {
            for (int column = 0; column < numColumns; column++)
            {
                Vector2 uv = Vector2.zero;
                uv.x = tileScale * column;
                uv.y = (tileScale * -row) - tileScale;
                tileOffsets[tileNum] = uv;

                tileNum++;
                if (tileNum >= numTiles)
                {
                    break;
                }
            }

            if (tileNum >= numTiles)
            {
                break;
            }
        }

        noise = new FastSimplexNoise();

        transitionColor = Color.black;

        forces      = new Force[fingers.Length];
        fingertips  = new Fingertip[fingers.Length];
        wisps       = new Wisp[numWisps];
        quads       = new Quad[numWisps];
        finalQuads  = new Quad[numWisps];
        renderOrder = new List <RenderOrder>(numWisps);

        for (int i = 0; i < numWisps; i++)
        {
            Wisp wisp = new Wisp();
            wisp.TargetPoint  = SurfacePosition + (Random.insideUnitSphere * SurfaceRadius);
            wisp.Point        = wisp.TargetPoint;
            wisp.Velocity     = Random.insideUnitSphere * initialVelocity;
            wisp.TargetRadius = Mathf.Lerp(wispSize.x, wispSize.y, (float)noise.Evaluate(wisp.Point.x, wisp.Point.y, wisp.Point.z));
            wisp.Radius       = wisp.TargetRadius;
            wisp.TileOffset   = Random.Range(0, numTiles);
            wisps[i]          = wisp;

            Quad quad = Quad.FromWisp(baseWispColor.Evaluate(Random.Range(0f, 1f)), wisp.Point, wisp.Radius, Vector3.up, Vector3.right, tileOffsets[wisp.TileOffset], tileScale);
            quad.Color    = transitionColor;
            quads[i]      = quad;
            finalQuads[i] = quads[i];

            RenderOrder r = new RenderOrder();
            renderOrder.Add(r);
        }

        for (int i = 0; i < fingers.Length; i++)
        {
            Transform finger    = fingers[i];
            Fingertip fingertip = fingertips[i];
            fingertip.Block     = new MaterialPropertyBlock();
            fingertip.Trail     = finger.GetComponentInChildren <TrailRenderer>();
            fingertip.Mesh      = finger.GetComponentInChildren <MeshRenderer>();
            fingertip.PingAudio = finger.GetComponentInChildren <AudioSource>();
            fingertips[i]       = fingertip;
        }

        Camera.onPostRender += PostRender;

        updateWisps = true;
        Task task = UpdateWispsTask();
    }
        private static void UpdateServiceFacades()
        {
            if (!MixedRealityToolkit.IsInitialized)
            {   // Nothing to do here.
                return;
            }

            if (EditorApplication.isCompiling)
            {   // Wait for compilation to complete before creating or destroying facades
                return;
            }

            if (Application.isPlaying && Time.frameCount == previousFrameCount)
            {   // Only update once per frame (SceneGUI + Update may result in multiple calls)
                return;
            }

            previousFrameCount = Time.frameCount;

            if (previousActiveInstance != null && MixedRealityToolkit.Instance != previousActiveInstance)
            {   // We've changed active instances. Destroy all children in the previous instance.
                DestroyAllChildren(previousActiveInstance);
            }

            if (MixedRealityToolkit.Instance.HasActiveProfile && !MixedRealityToolkit.Instance.ActiveProfile.UseServiceInspectors)
            {   // If we're not using inspectors, destroy them all now
                DestroyAllChildren(MixedRealityToolkit.Instance);
                return;
            }

            servicesToSort.Clear();
            servicesToSort.AddRange(MixedRealityServiceRegistry.GetAllServices());

            IMixedRealityInputSystem inputSystem;

            MixedRealityServiceRegistry.TryGetService(out inputSystem);
            if (inputSystem != null)
            {
                servicesToSort.AddRange((inputSystem as IMixedRealityDataProviderAccess).GetDataProviders());
            }

            servicesToSort.Sort(
                delegate(IMixedRealityService s1, IMixedRealityService s2)
            {
                string s1Name = s1.GetType().Name;
                string s2Name = s2.GetType().Name;

                if (s1Name == s2Name)
                {
                    return(s1.Priority.CompareTo(s2.Priority));
                }

                return(s1Name.CompareTo(s2Name));
            });

            for (int i = 0; i < servicesToSort.Count; i++)
            {
                CreateFacade(MixedRealityToolkit.Instance.transform, servicesToSort[i], i);
            }

            // Delete any stragglers
            childrenToDelete.Clear();
            for (int i = servicesToSort.Count; i < MixedRealityToolkit.Instance.transform.childCount; i++)
            {
                childrenToDelete.Add(MixedRealityToolkit.Instance.transform.GetChild(i));
            }

            foreach (Transform childToDelete in childrenToDelete)
            {
                if (Application.isPlaying)
                {
                    GameObject.Destroy(childToDelete.gameObject);
                }
                else
                {
                    GameObject.DestroyImmediate(childToDelete.gameObject);
                }
            }

            try
            {
                // Update all self-registered facades
                foreach (ServiceFacade facade in ServiceFacade.ActiveFacadeObjects)
                {
                    if (facade == null)
                    {
                        continue;
                    }

                    facade.CheckIfStillValid();
                }
            }
            catch (Exception)
            {
                Debug.LogWarning("Service Facades should remain parented under the MixedRealityToolkit instance.");
            }

            previousActiveInstance = MixedRealityToolkit.Instance;
        }