-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyConfigForm.cs
300 lines (268 loc) · 10.1 KB
/
KeyConfigForm.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using System;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
namespace D4Automator
{
public partial class KeyConfigForm : Form
{
private Settings settings;
private Dictionary<string, TextBox> actionControls;
private bool isListening = false;
private bool isFirstClick = true;
private static readonly Keys[] RestrictedKeys = new Keys[]
{
Keys.Alt, Keys.LMenu, Keys.RMenu,
Keys.Shift, Keys.LShiftKey, Keys.RShiftKey,
Keys.LWin, Keys.RWin,
Keys.Control, Keys.LControlKey, Keys.RControlKey,
Keys.Tab, Keys.CapsLock, Keys.NumLock, Keys.Scroll,
Keys.PrintScreen, Keys.Pause, Keys.Enter,
Keys.ShiftKey, Keys.ControlKey, Keys.Menu
};
public KeyConfigForm(Settings settings)
{
InitializeComponent();
this.settings = settings;
InitializeActionControls();
LoadCurrentActionMappings();
ApplyDarkMode();
btnSave.Focus();
}
private void InitializeActionControls()
{
actionControls = new Dictionary<string, TextBox>
{
{"Skill1Action", txtSkill1},
{"Skill2Action", txtSkill2},
{"Skill3Action", txtSkill3},
{"Skill4Action", txtSkill4},
{"PrimaryAttackAction", txtPrimaryAttack},
{"SecondaryAttackAction", txtSecondaryAttack},
{"PotionAction", txtPotion},
{"DodgeAction", txtDodge},
{"ToggleAutomationAction", txtToggleAutomation}
};
foreach (var textBox in actionControls.Values)
{
textBox.ReadOnly = true;
textBox.Enter += TextBox_Enter;
textBox.Leave += TextBox_Leave;
textBox.KeyDown += TextBox_KeyDown;
textBox.MouseDown += TextBox_MouseDown;
}
}
private void TextBox_Enter(object sender, EventArgs e)
{
isFirstClick = true;
isListening = false;
}
private void TextBox_Leave(object sender, EventArgs e)
{
isListening = false;
isFirstClick = true;
}
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (isListening)
{
var textBox = (TextBox)sender;
if (!IsValidKey(e.KeyCode, textBox))
{
e.Handled = true;
e.SuppressKeyPress = true;
return;
}
string displayValue = KeyToString(e.KeyCode);
if (IsUnique(displayValue, textBox))
{
textBox.Text = displayValue;
textBox.Tag = e.KeyCode; // Set the Tag for keyboard inputs
isListening = false;
isFirstClick = true;
e.Handled = true;
e.SuppressKeyPress = true;
}
}
}
private string GetDisplayTextForKey(Keys key)
{
if (key >= Keys.D0 && key <= Keys.D9)
{
return key.ToString().Substring(1); // Remove the 'D' prefix for display
}
if (key >= Keys.NumPad0 && key <= Keys.NumPad9)
{
return $"NumPad {key.ToString().Substring(6)}";
}
return key.ToString();
}
private void LoadCurrentActionMappings()
{
foreach (var kvp in actionControls)
{
var property = settings.GetType().GetProperty(kvp.Key);
if (property != null)
{
var value = property.GetValue(settings);
if (Enum.TryParse(value.ToString(), out Keys key))
{
kvp.Value.Text = GetDisplayTextForKey(key);
kvp.Value.Tag = key;
}
else
{
kvp.Value.Text = value.ToString();
}
}
}
}
private void TextBox_MouseDown(object sender, MouseEventArgs e)
{
var textBox = (TextBox)sender;
if (isFirstClick)
{
textBox.Text = "Press key or mouse click...";
isFirstClick = false;
isListening = true;
}
else if (isListening && e.Button != MouseButtons.None)
{
string clickAction = GetMouseClickAction(e.Button);
if (!string.IsNullOrEmpty(clickAction) && IsUnique(clickAction, textBox))
{
textBox.Text = clickAction;
textBox.Tag = null; // Clear the Tag for mouse clicks
isListening = false;
isFirstClick = true;
}
}
}
private bool IsValidKey(Keys key, TextBox currentTextBox)
{
if (RestrictedKeys.Contains(key) || key == Keys.None)
{
MessageBox.Show("This key is not allowed.", "Invalid Key", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
string keyString = KeyToString(key);
return IsUnique(keyString, currentTextBox);
}
private bool IsUnique(string actionString, TextBox currentTextBox)
{
// Get all current values from other textboxes
var existingValues = actionControls
.Where(kvp => kvp.Value != currentTextBox)
.Select(kvp => kvp.Value.Text)
.ToList();
if (existingValues.Contains(actionString))
{
MessageBox.Show("This action is already in use for another binding.",
"Duplicate Action",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
return false;
}
return true;
}
private string GetMouseClickAction(MouseButtons button)
{
switch (button)
{
case MouseButtons.Left:
return "LeftClick";
case MouseButtons.Right:
return "RightClick";
case MouseButtons.Middle:
return "MiddleClick";
case MouseButtons.XButton1:
return "MouseBack"; // Usually "Back" button
case MouseButtons.XButton2:
return "MouseForward"; // Usually "Forward" button
default:
return string.Empty;
}
}
private string KeyToString(Keys key)
{
if (key >= Keys.D0 && key <= Keys.D9)
{
return key.ToString().Substring(1); // Remove the 'D' prefix for display
}
if (key >= Keys.NumPad0 && key <= Keys.NumPad9)
{
return $"NumPad {key.ToString().Substring(6)}";
}
return key.ToString();
}
private void btnSave_Click(object sender, EventArgs e)
{
const string placeholderText = "Press key or mouse click...";
var invalidActions = actionControls.Where(kvp => string.IsNullOrWhiteSpace(kvp.Value.Text) || kvp.Value.Text == placeholderText)
.Select(kvp => kvp.Key.Replace("Action", ""))
.ToList();
if (invalidActions.Any())
{
string actionList = string.Join(", ", invalidActions);
MessageBox.Show($"The following actions do not have valid keys assigned: {actionList}. Please assign keys to all actions before saving.",
"Incomplete Configuration",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
foreach (var kvp in actionControls)
{
var property = settings.GetType().GetProperty(kvp.Key);
if (property != null)
{
string value;
if (kvp.Value.Tag != null)
{
// For keyboard inputs, use the Keys enum value
value = ((Keys)kvp.Value.Tag).ToString();
}
else
{
// For mouse clicks, use the text directly
value = kvp.Value.Text;
}
property.SetValue(settings, value);
}
}
this.DialogResult = DialogResult.OK;
this.Close();
}
private void ApplyDarkMode()
{
this.BackColor = Color.FromArgb(30, 30, 30);
this.ForeColor = Color.White;
foreach (Control control in this.Controls)
{
ApplyDarkModeToControl(control);
}
btnSave.BackColor = Color.FromArgb(60, 60, 60);
btnSave.FlatStyle = FlatStyle.Flat;
btnSave.FlatAppearance.BorderColor = Color.Gray;
bntCancel.BackColor = Color.FromArgb(60, 60, 60);
bntCancel.FlatStyle = FlatStyle.Flat;
bntCancel.FlatAppearance.BorderColor = Color.Gray;
}
private void ApplyDarkModeToControl(Control control)
{
if (control is TextBox)
{
control.BackColor = Color.FromArgb(45, 45, 45);
control.ForeColor = Color.White;
}
else if (control is Label)
{
control.ForeColor = Color.White;
}
}
private void bntCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}