A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System;
5using System.Collections.Concurrent;
6using System.Collections.Generic;
7using osu.Framework.Bindables;
8using osu.Framework.Input.StateChanges;
9using osu.Framework.Platform;
10
11namespace osu.Framework.Input.Handlers
12{
13 public abstract class InputHandler : IDisposable, IHasDescription
14 {
15 private bool isInitialized;
16
17 /// <summary>
18 /// Used to initialize resources specific to this InputHandler. It gets called once.
19 /// </summary>
20 /// <returns>Success of the initialization.</returns>
21 public virtual bool Initialize(GameHost host)
22 {
23 if (isInitialized)
24 throw new InvalidOperationException($"{nameof(Initialize)} was run more than once");
25
26 isInitialized = true;
27 return true;
28 }
29
30 /// <summary>
31 /// 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.
32 /// </summary>
33 /// <remarks>
34 /// An example would be a user setting the sensitivity too high to turn it back down, or restricting the navigable screen area too small.
35 /// Calling this would attempt to return the user to a sane state so they could re-attempt configuration changes.
36 /// </remarks>
37 public virtual void Reset()
38 {
39 }
40
41 protected ConcurrentQueue<IInput> PendingInputs = new ConcurrentQueue<IInput>();
42
43 private readonly object pendingInputsRetrievalLock = new object();
44
45 /// <summary>
46 /// Add all pending states since the last call to this method to a provided list.
47 /// </summary>
48 /// <param name="inputs">The list for pending inputs to be added to.</param>
49 public virtual void CollectPendingInputs(List<IInput> inputs)
50 {
51 lock (pendingInputsRetrievalLock)
52 {
53 while (PendingInputs.TryDequeue(out IInput s))
54 inputs.Add(s);
55 }
56 }
57
58 /// <summary>
59 /// Indicates whether this InputHandler is currently delivering input by the user. When handling input the OsuGame uses the first InputHandler which is active.
60 /// </summary>
61 public abstract bool IsActive { get; }
62
63 /// <summary>
64 /// A user-readable description of this input handler, for display in settings.
65 /// </summary>
66 public virtual string Description => ToString().Replace("Handler", string.Empty);
67
68 /// <summary>
69 /// Whether this InputHandler should be collecting <see cref="IInput"/>s to return on the next <see cref="CollectPendingInputs"/> call
70 /// </summary>
71 public BindableBool Enabled { get; } = new BindableBool(true);
72
73 public override string ToString() => GetType().Name;
74
75 #region IDisposable Support
76
77 protected bool IsDisposed;
78
79 protected virtual void Dispose(bool disposing)
80 {
81 if (IsDisposed)
82 return;
83
84 Enabled.Value = false;
85 IsDisposed = true;
86 }
87
88 public void Dispose()
89 {
90 Dispose(true);
91 GC.SuppressFinalize(this);
92 }
93
94 #endregion
95 }
96}