-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTAKeyBinding.cs
More file actions
207 lines (192 loc) · 10.6 KB
/
TAKeyBinding.cs
File metadata and controls
207 lines (192 loc) · 10.6 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using ColossalFramework;
using ColossalFramework.UI;
using UnityEngine;
using static TreeAnarchy.TAMod;
namespace TreeAnarchy {
internal class TAKeyBinding : UICustomControl {
private const string thisCategory = @"TreeAnarchy";
private SavedInputKey m_EditingBinding;
[RebindableKey(@"TreeAnarchy")]
private static readonly string toggleTreeSnapping = @"toggleTreeSnapping";
[RebindableKey(@"TreeAnarchy")]
private static readonly string toggleLockForestry = "toggleLockForestry";
[RebindableKey(@"TreeAnarchy")]
private static readonly string toggleTreeAnarchy = @"toggleTreeAnarchy";
#if ENABLETREEGROUP
[RebindableKey(@"TreeAnarchy")]
private static readonly string groupTrees = @"groupTrees";
[RebindableKey(@"TreeAnarchy")]
private static readonly string ungroupTrees = @"ungroupTrees";
#endif
#if ENABLETERRAINCONFORM
[RebindableKey(@"TreeAnarchy")]
private static readonly string terrainConformTrees = @"terrainConformTrees";
#endif
[RebindableKey(@"TreeAnarchy")]
private static readonly string incrementTreeSize = @"incrTreeVariation";
[RebindableKey(@"TreeAnarchy")]
private static readonly string decrementTreeSize = @"decrTreeVariation";
private static readonly InputKey defaultToggleTreeSnappingKey = SavedInputKey.Encode(KeyCode.S, false, false, true);
private static readonly InputKey defaultToggleLockForestryKey = SavedInputKey.Encode(KeyCode.F, false, false, true);
#if ENABLETREEANARCHY
private static readonly InputKey defaultToggleTreeAnarchyKey = SavedInputKey.Encode(KeyCode.A, false, false, true);
#endif
#if ENABLETREEGROUP
private static readonly InputKey defaultGroupTreeKey = SavedInputKey.Encode(KeyCode.G, true, false, false);
private static readonly InputKey defaultUngroupTreeKey = SavedInputKey.Encode(KeyCode.U, true, false, false);
#endif
#if ENABLETERRAINCONFORM
private static readonly InputKey defaultTerrainConformTrees = SavedInputKey.Encode(KeyCode.T, false, false, true);
#endif
private static readonly InputKey defaultIncrementTreeSizeKey = SavedInputKey.Encode(KeyCode.Period, false, false, false);
private static readonly InputKey defaultDecrementTreeSizeKey = SavedInputKey.Encode(KeyCode.Comma, false, false, false);
private SavedInputKey m_treeSnapping;
private SavedInputKey m_lockForestry;
private SavedInputKey m_treeAnarchy;
#if ENABLETREEGROUP
private SavedInputKey m_groupTrees;
private SavedInputKey m_ungroupTrees;
#endif
#if ENABLETERRAINCONFORM
private SavedInputKey m_terrainConformTrees;
#endif
private SavedInputKey m_incrTreeVariation;
private SavedInputKey m_decrTreeVariation;
protected void Update() {
if (!UIView.HasModalInput() && !UIView.HasInputFocus()) {
Event e = Event.current;
if (m_treeSnapping.IsPressed(e)) {
UseTreeSnapping = !UseTreeSnapping;
} else if (m_lockForestry.IsPressed(e)) {
UseLockForestry = !UseLockForestry;
} else if (m_treeAnarchy.IsPressed(e)) {
UseTreeAnarchy = !UseTreeAnarchy;
}
#if ENABLETREEGROUP
else if (m_groupTrees.IsPressed(e)) {
SingletonLite<TAManager>.instance.GroupTrees();
} else if (m_ungroupTrees.IsPressed(e)) {
SingletonLite<TAManager>.instance.UngroupTrees();
}
#endif
#if ENABLETERRAINCONFORM
else if (m_terrainConformTrees.IsPressed(e)) {
SingletonLite<TAManager>.instance.TerrainConformTrees();
}
#endif
else if (IsCustomPressed(m_incrTreeVariation, e)) {
TAManager.IncrementTreeSize();
} else if (IsCustomPressed(m_decrTreeVariation, e)) {
TAManager.DecrementTreeSize();
}
}
}
protected void Awake() {
m_treeSnapping = new SavedInputKey(toggleTreeSnapping, KeybindingConfigFile, defaultToggleTreeSnappingKey, true);
m_lockForestry = new SavedInputKey(toggleLockForestry, KeybindingConfigFile, defaultToggleLockForestryKey, true);
m_treeAnarchy = new SavedInputKey(toggleTreeAnarchy, KeybindingConfigFile, defaultToggleTreeAnarchyKey, true);
#if ENABLETREEGROUP
m_groupTrees = new SavedInputKey(groupTrees, KeybindingConfigFile, defaultGroupTreeKey, true);
m_ungroupTrees = new SavedInputKey(ungroupTrees, KeybindingConfigFile, defaultUngroupTreeKey, true);
#endif
#if ENABLETERRAINCONFORM
m_terrainConformTrees = new SavedInputKey(terrainConformTrees, KeybindingConfigFile, defaultTerrainConformTrees, true);
#endif
m_incrTreeVariation = new SavedInputKey(incrementTreeSize, KeybindingConfigFile, defaultIncrementTreeSizeKey, true);
m_decrTreeVariation = new SavedInputKey(decrementTreeSize, KeybindingConfigFile, defaultDecrementTreeSizeKey, true);
UILabel desc = component.AddUIComponent<UILabel>();
desc.padding.top = 10;
desc.width = component.width - 50;
desc.autoHeight = true;
desc.wordWrap = true;
desc.textScale = TAOptionPanel.SmallFontScale;
desc.text = TALocale.GetLocale(@"KeyBindDescription");
AddKeymapping(@"TreeSnap", m_treeSnapping);
AddKeymapping(@"LockForestry", m_lockForestry);
AddKeymapping(@"TreeAnarchy", m_treeAnarchy);
#if ENABLETERRAINCONFORM
AddKeymapping(@"TerrainConformTrees", m_terrainConformTrees);
#endif
#if ENABLETREEGROUP
AddKeymapping(@"GroupTrees", m_groupTrees);
AddKeymapping(@"UngroupTrees", m_ungroupTrees);
#endif
AddKeymapping(@"IncreaseTreeSize", m_incrTreeVariation);
AddKeymapping(@"DecreaseTreeSize", m_decrTreeVariation);
}
private bool IsCustomPressed(SavedInputKey inputKey, Event e) {
if (e.type != EventType.KeyDown) return false;
return Input.GetKey(inputKey.Key) &&
(e.modifiers & EventModifiers.Control) == EventModifiers.Control == inputKey.Control &&
(e.modifiers & EventModifiers.Shift) == EventModifiers.Shift == inputKey.Shift &&
(e.modifiers & EventModifiers.Alt) == EventModifiers.Alt == inputKey.Alt;
}
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 = TALocale.GetLocale(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 = TALocale.GetLocale(@"PressAnyKey");
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));
}
}