-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEKeyBinding.cs
105 lines (95 loc) · 5.39 KB
/
EKeyBinding.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
using ColossalFramework;
using ColossalFramework.UI;
using UnityEngine;
namespace EManagersLib {
internal sealed class EKeyBinding : UICustomControl {
private const string thisCategory = "EManagersLib";
private SavedInputKey m_EditingBinding;
[RebindableKey("EManagersLib")]
private static readonly string toggleStatsPanelVisibility = "toggleStatsPanelVisibility";
private static readonly InputKey toggleStatsPanelVisiblityKey = SavedInputKey.Encode(KeyCode.L, true, false, false);
internal SavedInputKey m_toggleStatsPanel;
internal void Awake() {
m_toggleStatsPanel = new SavedInputKey(toggleStatsPanelVisibility, EModule.m_settingsFile, toggleStatsPanelVisiblityKey, true);
AddKeymapping("Toggle Stats Panel Visibility", m_toggleStatsPanel);
}
internal void OnGUI() {
if (m_toggleStatsPanel.IsPressed(Event.current)) {
EStatsPanel.m_isVisible = !EStatsPanel.m_isVisible;
if (EStatsPanel.m_isVisible) {
StartCoroutine(EStatsPanel.UpdateStats());
} else {
StopCoroutine(EStatsPanel.UpdateStats());
}
}
EStatsPanel.OnGUI();
}
private int listCount = 0;
private void AddKeymapping(string key, SavedInputKey savedInputKey) {
UIPanel uIPanel = component.AttachUIComponent(UITemplateManager.GetAsGameObject("KeyBindingTemplate")) as UIPanel;
if (listCount++ % 2 == 1) uIPanel.backgroundSprite = null;
UILabel uILabel = uIPanel.Find<UILabel>("Name");
UIButton uIButton = uIPanel.Find<UIButton>("Binding");
uIButton.eventKeyDown += new KeyPressHandler(OnBindingKeyDown);
uIButton.eventMouseDown += new MouseEventHandler(OnBindingMouseDown);
uILabel.stringUserData = key;
uILabel.text = key;
uIButton.text = savedInputKey.ToLocalizedString("KEYNAME");
uIButton.objectUserData = savedInputKey;
uIButton.stringUserData = thisCategory; // used for localization TODO:
}
private void OnBindingKeyDown(UIComponent comp, UIKeyEventParameter p) {
if (!(m_EditingBinding is null) && !IsModifierKey(p.keycode)) {
p.Use();
UIView.PopModal();
KeyCode keycode = p.keycode;
InputKey inputKey = (p.keycode == KeyCode.Escape) ? m_EditingBinding.value : SavedInputKey.Encode(keycode, p.control, p.shift, p.alt);
if (p.keycode == KeyCode.Backspace) {
inputKey = SavedInputKey.Empty;
}
m_EditingBinding.value = inputKey;
UITextComponent uITextComponent = p.source as UITextComponent;
uITextComponent.text = m_EditingBinding.ToLocalizedString("KEYNAME");
m_EditingBinding = null;
}
}
private void OnBindingMouseDown(UIComponent comp, UIMouseEventParameter p) {
if (m_EditingBinding is null) {
p.Use();
m_EditingBinding = (SavedInputKey)p.source.objectUserData;
UIButton uIButton = p.source as UIButton;
uIButton.buttonsMask = (UIMouseButton.Left | UIMouseButton.Right | UIMouseButton.Middle | UIMouseButton.Special0 | UIMouseButton.Special1 | UIMouseButton.Special2 | UIMouseButton.Special3);
uIButton.text = "Press Any Key";
p.source.Focus();
UIView.PushModal(p.source);
} else if (!IsUnbindableMouseButton(p.buttons)) {
p.Use();
UIView.PopModal();
InputKey inputKey = SavedInputKey.Encode(ButtonToKeycode(p.buttons), IsControlDown(), IsShiftDown(), IsAltDown());
m_EditingBinding.value = inputKey;
UIButton uIButton2 = p.source as UIButton;
uIButton2.text = m_EditingBinding.ToLocalizedString("KEYNAME");
uIButton2.buttonsMask = UIMouseButton.Left;
m_EditingBinding = null;
}
}
private KeyCode ButtonToKeycode(UIMouseButton button) {
switch (button) {
case UIMouseButton.Left: return KeyCode.Mouse0;
case UIMouseButton.Right: return KeyCode.Mouse1;
case UIMouseButton.Middle: return KeyCode.Mouse2;
case UIMouseButton.Special0: return KeyCode.Mouse3;
case UIMouseButton.Special1: return KeyCode.Mouse4;
case UIMouseButton.Special2: return KeyCode.Mouse5;
case UIMouseButton.Special3: return KeyCode.Mouse6;
default: return KeyCode.None;
}
}
private bool IsUnbindableMouseButton(UIMouseButton code) => (code == UIMouseButton.Left || code == UIMouseButton.Right);
private bool IsModifierKey(KeyCode code) => code == KeyCode.LeftControl || code == KeyCode.RightControl || code == KeyCode.LeftShift ||
code == KeyCode.RightShift || code == KeyCode.LeftAlt || code == KeyCode.RightAlt;
private bool IsControlDown() => (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl));
private bool IsShiftDown() => (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift));
private bool IsAltDown() => (Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt));
}
}