/// <summary> /// Registers the given view on this thread. /// </summary> internal void RegisterView(IInputEnabledView view) { view.EnsureNotNull(nameof(view)); m_commandQueue.Enqueue(() => { List <IInputHandler> inputHandlers = InputHandlerFactory.CreateInputHandlersForView(view); if (inputHandlers == null) { return; } if (inputHandlers.Count == 0) { return; } // Deregister old input handlers if necessary if (m_viewInputHandlers.ContainsKey(view)) { List <IInputHandler> oldList = m_viewInputHandlers[view]; foreach (IInputHandler actOldInputHanlder in oldList) { actOldInputHanlder.Stop(); } m_viewInputHandlers.Remove(view); } // Register new ones m_viewInputHandlers[view] = inputHandlers; foreach (IInputHandler actInputHandler in inputHandlers) { actInputHandler.Start(view); } }); }
protected override void OnTick(EventArgs eArgs) { base.OnTick(eArgs); if (!GraphicsCore.IsInitialized) { return; } // Query for all input handlers on first tick if (m_globalInputHandlers == null) { m_globalInputHandlers = InputHandlerFactory.CreateInputHandlersForGlobal(); foreach (IInputHandler actInputHandler in m_globalInputHandlers) { actInputHandler.Start(null); } } // Execute all commands within the command queue if (m_commandQueue.Count > 0) { Action actCommand = null; while (m_commandQueue.Dequeue(out actCommand)) { actCommand(); } } // Gather all input data int expectedStateCount = m_lastInputFrame != null ? m_lastInputFrame.CountStates : 6; // Create new InputFrame object or reuse an old one InputFrame newInputFrame = null; if (m_recoveredInputFrames.Dequeue(out newInputFrame)) { newInputFrame.Reset(expectedStateCount, SINGLE_FRAME_DURATION); } else { newInputFrame = new InputFrame(expectedStateCount, SINGLE_FRAME_DURATION); } // Gather all input states foreach (IInputHandler actInputHandler in m_globalInputHandlers) { foreach (InputStateBase actInputState in actInputHandler.GetInputStates()) { actInputState.EnsureNotNull(nameof(actInputState)); newInputFrame.AddCopyOfState(actInputState, null); } } foreach (KeyValuePair <IInputEnabledView, List <IInputHandler> > actViewSpecificHandlers in m_viewInputHandlers) { RenderLoop renderLoop = actViewSpecificHandlers.Key.RenderLoop; if (renderLoop == null) { continue; } foreach (IInputHandler actInputHandler in actViewSpecificHandlers.Value) { foreach (InputStateBase actInputState in actInputHandler.GetInputStates()) { actInputState.EnsureNotNull(nameof(actInputState)); newInputFrame.AddCopyOfState(actInputState, renderLoop.ViewInformation); } } } // Store the generated InputFrame m_lastInputFrame = newInputFrame; m_gatheredInputFrames.Enqueue(newInputFrame); // Ensure that we hold input frames for a maximum time range of a second // (older input is obsolete) while (m_gatheredInputFrames.Count > Constants.INPUT_FRAMES_PER_SECOND) { InputFrame dummyFrame = null; m_gatheredInputFrames.Dequeue(out dummyFrame); } }