-
Notifications
You must be signed in to change notification settings - Fork 5
/
HotKeyBase.cs
114 lines (98 loc) · 4.07 KB
/
HotKeyBase.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
using System;
using TaleWorlds.InputSystem;
namespace BannerLib.Input
{
/// <summary>
/// Base type for all HotKey definitions to derive from.
/// </summary>
public abstract class HotKeyBase
{
internal int ID { get; set; }
internal GameKey GameKey { get; set; }
/// <summary>
/// The unique (to your mod) Id for this hotkey.
/// </summary>
protected internal string IdName { get; }
/// <summary>
/// The default key for your HotKey, if this is not set in your constructor it will default to `Invalid`
/// </summary>
protected internal InputKey DefaultKey { get; protected set; } = InputKey.Invalid;
/// <summary>
/// The display name for your hotkey that will appear in the options menu.
/// </summary>
protected internal string DisplayName { get; protected set; }
/// <summary>
/// The description text that will appear in the options menu next to your hotkey.
/// </summary>
protected internal string Description { get; protected set; } = "No Description Set.";
/// <summary>
/// The Category in the options menu under which this hotkey will appear.
/// <see cref="HotKeyManager.Categories"/>
/// </summary>
protected internal string Category { get; protected set; } =
HotKeyManager.Categories[HotKeyCategory.Action];
/// <summary>
/// Called once on the frame a key was pressed.
/// </summary>
public event Action OnPressedEvent;
/// <summary>
/// Called once on the frame a key was released.
/// </summary>
public event Action OnReleasedEvent;
/// <summary>
/// Called once every frame a key remains down.
/// </summary>
public event Action IsDownEvent;
/// <summary>
/// Provide none, one or many functions which all must evaluate to true in order for the key to process input.
/// This does not need to be set, and can be reset with <see cref="Predicate"/> = null;
/// </summary>
public Func<bool> Predicate { get; set; }
/// <summary>
/// Tells the input manager whether or not to process input for this key.
/// Setting this infrequently is cheaper than using <see cref="Predicate"/> but it is less convenient.
/// </summary>
public bool IsEnabled { get; private set; } = true;
/// <summary>
/// The required constructor which has the bare minimum needed to register a key.
/// </summary>
/// <param name="idName">The (unique to your mod) id for your hotkey.</param>
protected internal HotKeyBase(string idName)
{
IdName = idName;
DisplayName = IdName;
}
/// <summary>
/// Allows you to supply a HotKeyBase derived class wherever a GameKey might normally be used.
/// </summary>
/// <param name="hotkey"><see cref="HotKeyBase"/> to convert.</param>
/// <returns>The <see cref="GameKey"/> stored internally.</returns>
public static implicit operator GameKey(HotKeyBase hotkey) => hotkey.GameKey;
/// <inheritdoc cref="OnPressedEvent"/>
protected virtual void OnPressed() { }
/// <inheritdoc cref="OnReleasedEvent"/>
protected virtual void OnReleased() { }
/// <inheritdoc cref="IsDownEvent"/>
protected virtual void IsDown() { }
internal bool ShouldExecute()
{
if (Predicate is null && IsEnabled) return true;
return !(Predicate is null) && Predicate() && IsEnabled;
}
internal void OnPressedInternal()
{
OnPressedEvent?.Invoke();
OnPressed();
}
internal void OnReleasedInternal()
{
OnReleasedEvent?.Invoke();
OnReleased();
}
internal void IsDownInternal()
{
IsDownEvent?.Invoke();
IsDown();
}
}
}