// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Concurrent; using System.Collections.Generic; using osu.Framework.Bindables; using osu.Framework.Input.StateChanges; using osu.Framework.Platform; namespace osu.Framework.Input.Handlers { public abstract class InputHandler : IDisposable, IHasDescription { private bool isInitialized; /// /// Used to initialize resources specific to this InputHandler. It gets called once. /// /// Success of the initialization. public virtual bool Initialize(GameHost host) { if (isInitialized) throw new InvalidOperationException($"{nameof(Initialize)} was run more than once"); isInitialized = true; return true; } /// /// Reset this handler to a sane default state. This should reset any settings a consumer or user may have changed in order to attempt to make the handler usable again. /// /// /// An example would be a user setting the sensitivity too high to turn it back down, or restricting the navigable screen area too small. /// Calling this would attempt to return the user to a sane state so they could re-attempt configuration changes. /// public virtual void Reset() { } protected ConcurrentQueue PendingInputs = new ConcurrentQueue(); private readonly object pendingInputsRetrievalLock = new object(); /// /// Add all pending states since the last call to this method to a provided list. /// /// The list for pending inputs to be added to. public virtual void CollectPendingInputs(List inputs) { lock (pendingInputsRetrievalLock) { while (PendingInputs.TryDequeue(out IInput s)) inputs.Add(s); } } /// /// Indicates whether this InputHandler is currently delivering input by the user. When handling input the OsuGame uses the first InputHandler which is active. /// public abstract bool IsActive { get; } /// /// A user-readable description of this input handler, for display in settings. /// public virtual string Description => ToString().Replace("Handler", string.Empty); /// /// Whether this InputHandler should be collecting s to return on the next call /// public BindableBool Enabled { get; } = new BindableBool(true); public override string ToString() => GetType().Name; #region IDisposable Support protected bool IsDisposed; protected virtual void Dispose(bool disposing) { if (IsDisposed) return; Enabled.Value = false; IsDisposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } #endregion } }