-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathInputHandler.cs
122 lines (99 loc) · 4.61 KB
/
InputHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable disable
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using osu.Framework.Bindables;
using osu.Framework.Extensions.TypeExtensions;
using osu.Framework.Input.StateChanges;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Framework.Statistics;
namespace osu.Framework.Input.Handlers
{
public abstract class InputHandler : IDisposable, IHasDescription
{
/// <summary>
/// Base category to use for input-related <see cref="GlobalStatistics"/>.
/// </summary>
public const string STATISTIC_GROUP = "Input";
/// <summary>
/// Gets the appropriate statistic group for use in <see cref="GlobalStatistics.Get{T}"/>.
/// </summary>
/// <typeparam name="T">Calling class</typeparam>
protected static string StatisticGroupFor<T>() where T : InputHandler => $"{STATISTIC_GROUP} - {typeof(T).ReadableName()}";
private static readonly Logger logger = Logger.GetLogger(LoggingTarget.Input);
private bool isInitialized;
/// <summary>
/// Used to initialize resources specific to this InputHandler. It gets called once.
/// </summary>
/// <returns>Success of the initialization.</returns>
public virtual bool Initialize(GameHost host)
{
if (isInitialized)
throw new InvalidOperationException($"{nameof(Initialize)} was run more than once");
isInitialized = true;
return true;
}
/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
public virtual void Reset()
{
}
protected ConcurrentQueue<IInput> PendingInputs = new ConcurrentQueue<IInput>();
private readonly object pendingInputsRetrievalLock = new object();
/// <summary>
/// Add all pending states since the last call to this method to a provided list.
/// </summary>
/// <param name="inputs">The list for pending inputs to be added to.</param>
public virtual void CollectPendingInputs(List<IInput> inputs)
{
lock (pendingInputsRetrievalLock)
{
while (PendingInputs.TryDequeue(out IInput s))
inputs.Add(s);
}
}
/// <summary>
/// Indicates whether this InputHandler is currently delivering input by the user. When handling input the OsuGame uses the first InputHandler which is active.
/// </summary>
public abstract bool IsActive { get; }
/// <summary>
/// A user-readable description of this input handler, for display in settings and logs.
/// </summary>
public virtual string Description => ToString().Replace("Handler", string.Empty);
/// <summary>
/// Whether this InputHandler should be collecting <see cref="IInput"/>s to return on the next <see cref="CollectPendingInputs"/> call
/// </summary>
public BindableBool Enabled { get; } = new BindableBool(true);
/// <summary>
/// Logs an arbitrary string prefixed by the name of this input handler.
/// </summary>
/// <param name="message">The message to log. Can include newline (\n) characters to split into multiple lines.</param>
/// <param name="level">The verbosity level.</param>
/// <param name="exception">An optional related exception.</param>
protected void Log(string message, LogLevel level = LogLevel.Verbose, Exception exception = null) => logger.Add($"[{Description}] {message}", level, exception);
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
}
}