-
Notifications
You must be signed in to change notification settings - Fork 57
/
Hotkey.cs
95 lines (84 loc) · 3.11 KB
/
Hotkey.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
using KSP.IO;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace TweakScale
{
public class Hotkey
{
private readonly Dictionary<KeyCode, bool> _modifiers = new Dictionary<KeyCode, bool>();
private KeyCode _trigger = KeyCode.None;
private readonly string _name;
private readonly PluginConfiguration _config;
public Hotkey(string name, ICollection<KeyCode> defaultKey)
{
_config = HotkeyManager.Instance.Config;
_name = name;
if (defaultKey.Count == 0)
{
Tools.LogWf("No keys for hotkey {0}. Need at least 1 key in defaultKey parameter, got none.", _name);
}
else
{
_trigger = defaultKey.Last();
SetModifiers(defaultKey.SkipLast().ToList());
}
Load();
}
public Hotkey(string name, string defaultKey)
{
_config = HotkeyManager.Instance.Config;
_name = name;
ParseString(defaultKey);
Load();
}
public void Load()
{
_config.load();
var rawNames = _config.GetValue(_name, "");
if (!string.IsNullOrEmpty(rawNames))
{
ParseString(rawNames);
}
Save();
}
private void ParseString(string s)
{
var names = s.Split('+');
var keys = names.Select(Enums.Parse<KeyCode>).ToList();
_trigger = keys.Last();
SetModifiers(keys.SkipLast().ToList());
}
private void Save()
{
var result = "";
foreach (var kv in _modifiers)
if (kv.Value)
result += kv.Key + "+";
_config.SetValue(_name, result + _trigger);
_config.save();
}
private void SetModifiers(ICollection<KeyCode> mods)
{
_modifiers[KeyCode.RightShift] = mods.Contains(KeyCode.RightShift);
_modifiers[KeyCode.LeftShift] = mods.Contains(KeyCode.LeftShift);
_modifiers[KeyCode.RightControl] = mods.Contains(KeyCode.RightControl);
_modifiers[KeyCode.LeftControl] = mods.Contains(KeyCode.LeftControl);
_modifiers[KeyCode.RightAlt] = mods.Contains(KeyCode.RightAlt);
_modifiers[KeyCode.LeftAlt] = mods.Contains(KeyCode.LeftAlt);
_modifiers[KeyCode.RightApple] = mods.Contains(KeyCode.RightApple);
_modifiers[KeyCode.RightCommand] = mods.Contains(KeyCode.RightCommand);
_modifiers[KeyCode.LeftApple] = mods.Contains(KeyCode.LeftApple);
_modifiers[KeyCode.LeftCommand] = mods.Contains(KeyCode.LeftCommand);
_modifiers[KeyCode.LeftWindows] = mods.Contains(KeyCode.LeftWindows);
_modifiers[KeyCode.RightWindows] = mods.Contains(KeyCode.RightWindows);
}
public bool IsTriggered
{
get
{
return _modifiers.All(a => Input.GetKey(a.Key) == a.Value) && Input.GetKeyDown(_trigger);
}
}
}
}