-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathKeybindToggle.cs
More file actions
82 lines (76 loc) · 3.01 KB
/
KeybindToggle.cs
File metadata and controls
82 lines (76 loc) · 3.01 KB
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
using GadgetCore.API;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
namespace GadgetCore
{
[RequireComponent(typeof(Toggle))]
internal class KeybindToggle : MonoBehaviour
{
/// <summary>
/// Whether a binding operation is currently in progress.
/// </summary>
public static bool Binding { get; private set; }
private Toggle toggle;
private List<string> keyList;
private Action<string> keybindSetter;
private bool allowMultiBind;
private bool clickSkip;
public void Init(Toggle toggle, Action<string> keybindSetter, bool allowMultiBind)
{
this.toggle = toggle;
this.keybindSetter = keybindSetter;
this.allowMultiBind = allowMultiBind;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Code Quality", "IDE0051:Remove unused private members", Justification = "Unity Event")]
private void Update()
{
if (toggle != null && toggle.isOn)
{
if (keyList == null)
{
keyList = new List<string>();
clickSkip = false;
GadgetCoreAPI.FreezeInput("Keybinding");
}
toggle.graphic.color = new Color(toggle.graphic.color.r, toggle.graphic.color.g, toggle.graphic.color.b, Mathf.RoundToInt(Time.time * 8) % 2);
foreach (KeyCode key in Enum.GetValues(typeof(KeyCode)))
{
if (key != KeyCode.Escape && Input.GetKeyDown(key))
{
if (!allowMultiBind)
{
keyList = new List<string>();
}
keyList.Add(key.ToString());
keybindSetter(keyList.Aggregate(new StringBuilder(), (a, b) => { if (a.Length > 0) a.Append('+'); a.Append(b); return a; }).ToString());
}
else if (Input.GetKeyUp(key))
{
if (!clickSkip && key == KeyCode.Mouse0)
{
clickSkip = true;
continue;
}
keyList = null;
toggle.isOn = false;
GadgetCoreAPI.UnfreezeInput("Keybinding");
}
}
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Code Quality", "IDE0051:Remove unused private members", Justification = "Unity Event")]
private void OnDisable()
{
GadgetCoreAPI.UnfreezeInput("Keybinding");
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Code Quality", "IDE0051:Remove unused private members", Justification = "Unity Event")]
private void OnDestroy()
{
GadgetCoreAPI.UnfreezeInput("Keybinding");
}
}
}