WingMan – Blame information for rev 10
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.Drawing; |
||
9 | office | 4 | using System.IO; |
1 | office | 5 | using System.Linq; |
6 | using System.Net; |
||
7 | office | 7 | using System.Threading; |
6 | office | 8 | using System.Threading.Tasks; |
1 | office | 9 | using System.Windows.Forms; |
4 | office | 10 | using Gma.System.MouseKeyHook; |
9 | office | 11 | using MQTTnet.Extensions.ManagedClient; |
12 | using MQTTnet.Server; |
||
1 | office | 13 | using WingMan.Communication; |
7 | office | 14 | using WingMan.Lobby; |
5 | office | 15 | using WingMan.MouseKey; |
16 | using WingMan.Properties; |
||
8 | office | 17 | using WingMan.Utilities; |
1 | office | 18 | |
19 | namespace WingMan |
||
20 | { |
||
2 | office | 21 | public partial class WingManForm : Form |
1 | office | 22 | { |
5 | office | 23 | public WingManForm() |
24 | { |
||
25 | InitializeComponent(); |
||
1 | office | 26 | |
6 | office | 27 | FormTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); |
7 | office | 28 | FormCancellationTokenSource = new CancellationTokenSource(); |
1 | office | 29 | |
9 | office | 30 | MqttCommunication = new MqttCommunication(FormTaskScheduler, FormCancellationTokenSource.Token); |
31 | MqttCommunication.OnClientAuthenticationFailed += OnMqttClientAuthenticationFailed; |
||
32 | MqttCommunication.OnClientConnectionFailed += OnMqttClientConnectionFailed; |
||
33 | MqttCommunication.OnClientDisconnected += OnMqttClientDisconnected; |
||
34 | MqttCommunication.OnClientConnected += OnMqttClientConnected; |
||
35 | MqttCommunication.OnServerAuthenticationFailed += OnMqttServerAuthenticationFailed; |
||
36 | MqttCommunication.OnServerStopped += OnMqttServerStopped; |
||
37 | MqttCommunication.OnServerStarted += OnMqttServerStarted; |
||
38 | MqttCommunication.OnServerClientConnected += OnMqttServerClientConnected; |
||
39 | MqttCommunication.OnServerClientDisconnected += OnMqttServerClientDisconnected; |
||
6 | office | 40 | |
10 | office | 41 | LocalKeyBindings = new LocalKeyBindings(new List<KeyBinding>()); |
42 | RemoteKeyBindings = new RemoteKeyBindings(new List<RemoteKeyBinding>()); |
||
1 | office | 43 | |
9 | office | 44 | LocalListBoxBindingSource = new BindingSource |
5 | office | 45 | { |
10 | office | 46 | DataSource = LocalKeyBindings.Bindings |
5 | office | 47 | }; |
9 | office | 48 | LocalBindingsListBox.DisplayMember = "DisplayName"; |
49 | LocalBindingsListBox.ValueMember = "Keys"; |
||
50 | LocalBindingsListBox.DataSource = LocalListBoxBindingSource; |
||
1 | office | 51 | |
10 | office | 52 | KeyBindingsExchange = new KeyBindingsExchange |
5 | office | 53 | { |
10 | office | 54 | ExchangeBindings = new List<KeyBindingExchange>() |
7 | office | 55 | }; |
56 | |||
9 | office | 57 | RemoteBindingsComboBoxSource = new BindingSource |
7 | office | 58 | { |
10 | office | 59 | DataSource = KeyBindingsExchange.ExchangeBindings |
5 | office | 60 | }; |
9 | office | 61 | RemoteBindingsComboBox.DisplayMember = "Nick"; |
10 | office | 62 | RemoteBindingsComboBox.ValueMember = "KeyBindings"; |
9 | office | 63 | RemoteBindingsComboBox.DataSource = RemoteBindingsComboBoxSource; |
4 | office | 64 | |
5 | office | 65 | // Start lobby message synchronizer. |
9 | office | 66 | LobbyMessageSynchronizer = new LobbyMessageSynchronizer(MqttCommunication, FormTaskScheduler, |
7 | office | 67 | FormCancellationTokenSource.Token); |
5 | office | 68 | LobbyMessageSynchronizer.OnLobbyMessageReceived += OnLobbyMessageReceived; |
4 | office | 69 | |
5 | office | 70 | // Start mouse key bindings synchronizer. |
10 | office | 71 | KeyBindingsSynchronizer = new KeyBindingsSynchronizer(LocalKeyBindings, MqttCommunication, |
7 | office | 72 | FormTaskScheduler, FormCancellationTokenSource.Token); |
10 | office | 73 | KeyBindingsSynchronizer.OnMouseKeyBindingsSynchronized += OnMouseKeyBindingsSynchronized; |
9 | office | 74 | |
10 | office | 75 | // Start mouse key interceptor. |
76 | KeyInterceptor = new KeyInterceptor(RemoteKeyBindings, MqttCommunication, FormTaskScheduler, |
||
77 | FormCancellationTokenSource.Token); |
||
78 | KeyInterceptor.OnMouseKeyBindingMatched += OnMouseKeyBindingMatched; |
||
79 | |||
80 | // Start mouse key simulator. |
||
81 | KeySimulator = new KeySimulator(LocalKeyBindings, MqttCommunication, FormTaskScheduler, |
||
82 | FormCancellationTokenSource.Token); |
||
83 | KeySimulator.OnMouseKeyBindingExecuting += OnMouseKeyBindingExecuting; |
||
5 | office | 84 | } |
4 | office | 85 | |
7 | office | 86 | private static CancellationTokenSource FormCancellationTokenSource { get; set; } |
4 | office | 87 | |
7 | office | 88 | private static TaskScheduler FormTaskScheduler { get; set; } |
4 | office | 89 | |
7 | office | 90 | private static IKeyboardMouseEvents MouseKeyApplicationHook { get; set; } |
4 | office | 91 | |
7 | office | 92 | private List<string> MouseKeyCombo { get; set; } |
2 | office | 93 | |
10 | office | 94 | private LocalKeyBindings LocalKeyBindings { get; } |
4 | office | 95 | |
10 | office | 96 | private RemoteKeyBindings RemoteKeyBindings { get; } |
4 | office | 97 | |
9 | office | 98 | private BindingSource LocalListBoxBindingSource { get; } |
4 | office | 99 | |
9 | office | 100 | private BindingSource RemoteBindingsComboBoxSource { get; } |
101 | |||
10 | office | 102 | private KeyBindingsExchange KeyBindingsExchange { get; } |
7 | office | 103 | |
9 | office | 104 | public MqttCommunication MqttCommunication { get; set; } |
7 | office | 105 | |
106 | public LobbyMessageSynchronizer LobbyMessageSynchronizer { get; set; } |
||
107 | |||
10 | office | 108 | public KeyBindingsSynchronizer KeyBindingsSynchronizer { get; set; } |
7 | office | 109 | |
10 | office | 110 | public KeyInterceptor KeyInterceptor { get; set; } |
111 | |||
112 | public KeySimulator KeySimulator { get; set; } |
||
113 | |||
114 | /// <summary> |
||
115 | /// Clean up any resources being used. |
||
116 | /// </summary> |
||
117 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
||
118 | protected override void Dispose(bool disposing) |
||
8 | office | 119 | { |
10 | office | 120 | FormCancellationTokenSource?.Dispose(); |
121 | FormCancellationTokenSource = null; |
||
9 | office | 122 | |
10 | office | 123 | if (disposing && components != null) |
9 | office | 124 | { |
10 | office | 125 | components.Dispose(); |
126 | components = null; |
||
9 | office | 127 | } |
10 | office | 128 | |
129 | base.Dispose(disposing); |
||
9 | office | 130 | } |
131 | |||
10 | office | 132 | private void OnMouseKeyBindingExecuting(object sender, KeyBindingExecutingEventArgs args) |
133 | { |
||
134 | ActivityTextBox.AppendText( |
||
135 | $"{Strings.Executing_binding_from_remote_client} : {args.Nick} : {args.Name}{Environment.NewLine}"); |
||
136 | } |
||
137 | |||
138 | private void OnMouseKeyBindingMatched(object sender, KeyBindingMatchedEventArgs args) |
||
139 | { |
||
140 | ActivityTextBox.AppendText( |
||
141 | $"{Strings.Matched_remote_key_binding} : {args.Nick} : {args.Name} : {string.Join(" + ", args.KeyCombo)}{Environment.NewLine}"); |
||
142 | } |
||
143 | |||
9 | office | 144 | private void OnMqttServerClientDisconnected(object sender, MqttClientDisconnectedEventArgs e) |
145 | { |
||
8 | office | 146 | ActivityTextBox.AppendText( |
9 | office | 147 | $"{Strings.Client_disconnected}{Environment.NewLine}"); |
148 | } |
||
149 | |||
150 | private void OnMqttServerClientConnected(object sender, MqttClientConnectedEventArgs e) |
||
151 | { |
||
152 | ActivityTextBox.AppendText( |
||
153 | $"{Strings.Client_connected}{Environment.NewLine}"); |
||
154 | } |
||
155 | |||
156 | private void OnMqttServerStarted(object sender, EventArgs e) |
||
157 | { |
||
158 | ActivityTextBox.AppendText( |
||
159 | $"{Strings.Server_started}{Environment.NewLine}"); |
||
160 | } |
||
161 | |||
162 | private void OnMqttServerStopped(object sender, EventArgs e) |
||
163 | { |
||
164 | ActivityTextBox.AppendText( |
||
165 | $"{Strings.Server_stopped}{Environment.NewLine}"); |
||
166 | } |
||
167 | |||
168 | private void OnMqttClientConnected(object sender, MQTTnet.Client.MqttClientConnectedEventArgs e) |
||
169 | { |
||
170 | ActivityTextBox.AppendText( |
||
171 | $"{Strings.Client_connected}{Environment.NewLine}"); |
||
172 | } |
||
173 | |||
174 | private void OnMqttClientDisconnected(object sender, MQTTnet.Client.MqttClientDisconnectedEventArgs e) |
||
175 | { |
||
176 | ActivityTextBox.AppendText( |
||
177 | $"{Strings.Client_disconnected}{Environment.NewLine}"); |
||
178 | } |
||
179 | |||
180 | private void OnMqttClientConnectionFailed(object sender, MqttManagedProcessFailedEventArgs e) |
||
181 | { |
||
182 | ActivityTextBox.AppendText( |
||
183 | $"{Strings.Client_connection_failed}{Environment.NewLine}"); |
||
184 | } |
||
185 | |||
10 | office | 186 | private void OnMqttServerAuthenticationFailed(object sender, MqttAuthenticationFailureEventArgs e) |
9 | office | 187 | { |
188 | ActivityTextBox.AppendText( |
||
10 | office | 189 | $"{Strings.Failed_to_authenticate_client} : {e.Exception}{Environment.NewLine}"); |
8 | office | 190 | } |
191 | |||
10 | office | 192 | private void OnMqttClientAuthenticationFailed(object sender, MqttAuthenticationFailureEventArgs e) |
8 | office | 193 | { |
194 | ActivityTextBox.AppendText( |
||
10 | office | 195 | $"{Strings.Failed_to_authenticate_with_server} : {e.Exception}{Environment.NewLine}"); |
8 | office | 196 | } |
197 | |||
10 | office | 198 | private void OnMouseKeyBindingsSynchronized(object sender, KeyBindingsSynchronizerEventArgs e) |
7 | office | 199 | { |
200 | ActivityTextBox.AppendText( |
||
10 | office | 201 | $"{Strings.Synchronized_bindings_with_client} : {e.Bindings.Nick} : {e.Bindings.KeyBindings.Count}{Environment.NewLine}"); |
6 | office | 202 | |
10 | office | 203 | var exchangeBindings = KeyBindingsExchange.ExchangeBindings.FirstOrDefault(exchangeBinding => |
7 | office | 204 | string.Equals(exchangeBinding.Nick, e.Bindings.Nick, StringComparison.Ordinal)); |
5 | office | 205 | |
7 | office | 206 | // If the nick does not exist then add it. |
207 | if (exchangeBindings == null) |
||
208 | { |
||
10 | office | 209 | KeyBindingsExchange.ExchangeBindings.Add(e.Bindings); |
9 | office | 210 | RemoteBindingsComboBoxSource.ResetBindings(false); |
10 | office | 211 | UpdateRemoteItems(); |
7 | office | 212 | return; |
213 | } |
||
5 | office | 214 | |
7 | office | 215 | // If the bindings for the nick have not changed then do not update. |
10 | office | 216 | if (exchangeBindings.KeyBindings.SequenceEqual(e.Bindings.KeyBindings)) |
7 | office | 217 | { |
9 | office | 218 | RemoteBindingsComboBoxSource.ResetBindings(false); |
10 | office | 219 | UpdateRemoteItems(); |
7 | office | 220 | return; |
221 | } |
||
5 | office | 222 | |
7 | office | 223 | // Update the bindings. |
10 | office | 224 | exchangeBindings.KeyBindings = e.Bindings.KeyBindings; |
9 | office | 225 | RemoteBindingsComboBoxSource.ResetBindings(false); |
10 | office | 226 | UpdateRemoteItems(); |
7 | office | 227 | } |
5 | office | 228 | |
10 | office | 229 | private void UpdateRemoteItems() |
7 | office | 230 | { |
10 | office | 231 | var exchangeBindings = (List<KeyBinding>) RemoteBindingsComboBox.SelectedValue; |
232 | if (exchangeBindings == null) |
||
7 | office | 233 | return; |
5 | office | 234 | |
10 | office | 235 | var replaceMouseBindings = new List<RemoteKeyBinding>(); |
236 | foreach (var remoteBinding in RemoteKeyBindings.Bindings) |
||
237 | { |
||
238 | if (!exchangeBindings.Any(binding => |
||
239 | string.Equals(binding.Name, remoteBinding.Name, StringComparison.Ordinal))) |
||
240 | continue; |
||
241 | |||
242 | replaceMouseBindings.Add(remoteBinding); |
||
243 | } |
||
244 | |||
245 | RemoteKeyBindings.Bindings = replaceMouseBindings; |
||
246 | |||
9 | office | 247 | RemoteBindingsListBox.Items.Clear(); |
248 | RemoteBindingsListBox.DisplayMember = "Name"; |
||
249 | RemoteBindingsListBox.ValueMember = "Name"; |
||
10 | office | 250 | var bindings = exchangeBindings.Select(binding => (object) binding.Name).ToArray(); |
251 | if (bindings.Length == 0) |
||
7 | office | 252 | return; |
5 | office | 253 | |
10 | office | 254 | RemoteBindingsListBox.Items.AddRange(bindings); |
7 | office | 255 | } |
5 | office | 256 | |
257 | private void OnLobbyMessageReceived(object sender, LobbyMessageReceivedEventArgs e) |
||
258 | { |
||
9 | office | 259 | LobbyTextBox.AppendText($"{e.Nick} : {e.Message}{Environment.NewLine}"); |
5 | office | 260 | } |
261 | |||
1 | office | 262 | private void AddressTextBoxClick(object sender, EventArgs e) |
263 | { |
||
264 | Address.BackColor = Color.Empty; |
||
265 | } |
||
266 | |||
267 | private void PortTextBoxClick(object sender, EventArgs e) |
||
268 | { |
||
269 | Port.BackColor = Color.Empty; |
||
270 | } |
||
271 | |||
272 | private async void HostButtonClickAsync(object sender, EventArgs e) |
||
273 | { |
||
274 | // Stop the MQTT server if it is running. |
||
9 | office | 275 | if (MqttCommunication.Running) |
1 | office | 276 | { |
10 | office | 277 | await MqttCommunication.Stop(); |
1 | office | 278 | HostButton.BackColor = Color.Empty; |
279 | |||
3 | office | 280 | // Enable controls. |
1 | office | 281 | ConnectButton.Enabled = true; |
5 | office | 282 | Address.Enabled = true; |
283 | Port.Enabled = true; |
||
284 | Nick.Enabled = true; |
||
10 | office | 285 | Password.Enabled = true; |
1 | office | 286 | return; |
287 | } |
||
288 | |||
8 | office | 289 | if (!ValidateAddressPort(out var ipAddress, out var port, out var nick, out var password)) |
1 | office | 290 | return; |
291 | |||
292 | // Start the MQTT server. |
||
10 | office | 293 | if (!await MqttCommunication |
294 | .Start(MqttCommunicationType.Server, ipAddress, port, nick, password)) |
||
9 | office | 295 | { |
296 | ActivityTextBox.AppendText( |
||
297 | $"{Strings.Failed_starting_server}{Environment.NewLine}"); |
||
298 | return; |
||
299 | } |
||
300 | |||
1 | office | 301 | HostButton.BackColor = Color.Aquamarine; |
302 | |||
3 | office | 303 | // Disable controls |
1 | office | 304 | ConnectButton.Enabled = false; |
3 | office | 305 | Address.Enabled = false; |
306 | Port.Enabled = false; |
||
307 | Nick.Enabled = false; |
||
10 | office | 308 | Password.Enabled = false; |
3 | office | 309 | } |
310 | |||
8 | office | 311 | private bool ValidateAddressPort(out IPAddress address, out int port, out string nick, out string password) |
1 | office | 312 | { |
313 | address = IPAddress.Any; |
||
314 | port = 0; |
||
2 | office | 315 | nick = string.Empty; |
8 | office | 316 | password = string.Empty; |
1 | office | 317 | |
318 | if (string.IsNullOrEmpty(Address.Text) && |
||
2 | office | 319 | string.IsNullOrEmpty(Port.Text) && |
8 | office | 320 | string.IsNullOrEmpty(Nick.Text) && |
321 | string.IsNullOrEmpty(Password.Text)) |
||
1 | office | 322 | { |
323 | Address.BackColor = Color.LightPink; |
||
324 | Port.BackColor = Color.LightPink; |
||
2 | office | 325 | Nick.BackColor = Color.LightPink; |
8 | office | 326 | Password.BackColor = Color.LightPink; |
1 | office | 327 | return false; |
328 | } |
||
329 | |||
330 | if (!IPAddress.TryParse(Address.Text, out address)) |
||
331 | { |
||
332 | Address.BackColor = Color.LightPink; |
||
333 | return false; |
||
334 | } |
||
335 | |||
336 | if (!uint.TryParse(Port.Text, out var uPort)) |
||
337 | { |
||
338 | Port.BackColor = Color.LightPink; |
||
339 | return false; |
||
340 | } |
||
341 | |||
342 | port = (int) uPort; |
||
343 | |||
2 | office | 344 | if (string.IsNullOrEmpty(Nick.Text)) |
345 | { |
||
346 | Nick.BackColor = Color.LightPink; |
||
347 | return false; |
||
348 | } |
||
349 | |||
350 | nick = Nick.Text; |
||
351 | |||
8 | office | 352 | if (string.IsNullOrEmpty(Password.Text)) |
353 | { |
||
354 | Password.BackColor = Color.LightPink; |
||
355 | return false; |
||
356 | } |
||
357 | |||
10 | office | 358 | password = AES.ExpandKey(Password.Text); |
8 | office | 359 | |
1 | office | 360 | Address.BackColor = Color.Empty; |
361 | Port.BackColor = Color.Empty; |
||
2 | office | 362 | Nick.BackColor = Color.Empty; |
8 | office | 363 | Password.BackColor = Color.Empty; |
1 | office | 364 | |
365 | return true; |
||
366 | } |
||
367 | |||
368 | private async void ConnectButtonClickAsync(object sender, EventArgs e) |
||
369 | { |
||
9 | office | 370 | if (MqttCommunication.Running) |
1 | office | 371 | { |
10 | office | 372 | await MqttCommunication.Stop(); |
5 | office | 373 | ConnectButton.Text = Strings.Connect; |
1 | office | 374 | ConnectButton.BackColor = Color.Empty; |
375 | |||
5 | office | 376 | Address.Enabled = true; |
377 | Port.Enabled = true; |
||
378 | Nick.Enabled = true; |
||
10 | office | 379 | Password.Enabled = true; |
1 | office | 380 | HostButton.Enabled = true; |
381 | return; |
||
382 | } |
||
383 | |||
8 | office | 384 | if (!ValidateAddressPort(out var ipAddress, out var port, out var nick, out var password)) |
1 | office | 385 | return; |
386 | |||
10 | office | 387 | if (!await MqttCommunication |
388 | .Start(MqttCommunicationType.Client, ipAddress, port, nick, password)) |
||
9 | office | 389 | { |
390 | ActivityTextBox.AppendText( |
||
391 | $"{Strings.Failed_starting_client}{Environment.NewLine}"); |
||
392 | return; |
||
393 | } |
||
1 | office | 394 | |
5 | office | 395 | ConnectButton.Text = Strings.Disconnect; |
1 | office | 396 | ConnectButton.BackColor = Color.Aquamarine; |
397 | |||
398 | HostButton.Enabled = false; |
||
5 | office | 399 | Address.Enabled = false; |
400 | Port.Enabled = false; |
||
401 | Nick.Enabled = false; |
||
10 | office | 402 | Password.Enabled = false; |
1 | office | 403 | } |
404 | |||
2 | office | 405 | private async void LobbySayTextBoxKeyDown(object sender, KeyEventArgs e) |
406 | { |
||
407 | if (e.KeyCode != Keys.Enter) |
||
408 | return; |
||
409 | |||
10 | office | 410 | await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text); |
2 | office | 411 | |
5 | office | 412 | LobbySayTextBox.Text = string.Empty; |
2 | office | 413 | } |
4 | office | 414 | |
10 | office | 415 | private void LocalAddBindingButtonClick(object sender, EventArgs e) |
4 | office | 416 | { |
9 | office | 417 | if (string.IsNullOrEmpty(LocalNameTextBox.Text)) |
4 | office | 418 | { |
9 | office | 419 | LocalNameTextBox.BackColor = Color.LightPink; |
4 | office | 420 | return; |
421 | } |
||
422 | |||
10 | office | 423 | // Only unique names allowed. |
424 | if (LocalKeyBindings.Bindings.Any(binding => |
||
425 | string.Equals(binding.Name, LocalNameTextBox.Text, StringComparison.Ordinal))) |
||
426 | { |
||
427 | LocalNameTextBox.BackColor = Color.LightPink; |
||
428 | LocalBindingsListBox.BackColor = Color.LightPink; |
||
429 | return; |
||
430 | } |
||
431 | |||
432 | LocalNameTextBox.BackColor = Color.Empty; |
||
433 | LocalBindingsListBox.BackColor = Color.Empty; |
||
434 | |||
8 | office | 435 | ShowOverlayPanel(); |
4 | office | 436 | |
437 | MouseKeyCombo = new List<string>(); |
||
438 | |||
10 | office | 439 | MouseKeyApplicationHook = Hook.GlobalEvents(); |
9 | office | 440 | MouseKeyApplicationHook.KeyUp += LocalMouseKeyHookOnKeyUp; |
441 | MouseKeyApplicationHook.KeyDown += LocalMouseKeyHookOnKeyDown; |
||
4 | office | 442 | } |
443 | |||
8 | office | 444 | private void ShowOverlayPanel() |
445 | { |
||
446 | OverlayPanel.BringToFront(); |
||
447 | OverlayPanel.Visible = true; |
||
448 | OverlayPanel.Invalidate(); |
||
449 | } |
||
450 | |||
10 | office | 451 | private void LocalMouseKeyHookOnKeyUp(object sender, KeyEventArgs e) |
4 | office | 452 | { |
10 | office | 453 | LocalKeyBindings.Bindings.Add(new KeyBinding(LocalNameTextBox.Text, MouseKeyCombo)); |
4 | office | 454 | |
9 | office | 455 | LocalListBoxBindingSource.ResetBindings(false); |
4 | office | 456 | |
9 | office | 457 | MouseKeyApplicationHook.KeyDown -= LocalMouseKeyHookOnKeyDown; |
458 | MouseKeyApplicationHook.KeyUp -= LocalMouseKeyHookOnKeyUp; |
||
4 | office | 459 | |
460 | MouseKeyApplicationHook.Dispose(); |
||
461 | |||
9 | office | 462 | LocalNameTextBox.Text = string.Empty; |
8 | office | 463 | HideOverlayPanel(); |
9 | office | 464 | |
10 | office | 465 | //await SaveLocalMouseKeyBindings(); |
4 | office | 466 | } |
467 | |||
8 | office | 468 | private void HideOverlayPanel() |
469 | { |
||
470 | OverlayPanel.SendToBack(); |
||
471 | OverlayPanel.Visible = false; |
||
472 | OverlayPanel.Invalidate(); |
||
473 | } |
||
474 | |||
9 | office | 475 | private void LocalMouseKeyHookOnKeyDown(object sender, KeyEventArgs e) |
4 | office | 476 | { |
477 | e.SuppressKeyPress = true; |
||
478 | |||
10 | office | 479 | KeyConversion.KeysToString.TryGetValue((byte) e.KeyCode, out var key); |
480 | |||
481 | MouseKeyCombo.Add(key); |
||
4 | office | 482 | } |
483 | |||
10 | office | 484 | private void LocalNameTextBoxClick(object sender, EventArgs e) |
4 | office | 485 | { |
9 | office | 486 | LocalNameTextBox.BackColor = Color.Empty; |
4 | office | 487 | } |
488 | |||
10 | office | 489 | private void LocalBindingsRemoveButtonClick(object sender, EventArgs e) |
4 | office | 490 | { |
10 | office | 491 | var helmBinding = (KeyBinding) LocalBindingsListBox.SelectedItem; |
5 | office | 492 | if (helmBinding == null) |
493 | return; |
||
4 | office | 494 | |
10 | office | 495 | LocalKeyBindings.Bindings.Remove(helmBinding); |
9 | office | 496 | LocalListBoxBindingSource.ResetBindings(false); |
497 | |||
10 | office | 498 | // await SaveLocalMouseKeyBindings(); |
4 | office | 499 | } |
500 | |||
5 | office | 501 | private async void LobbySayButtonClick(object sender, EventArgs e) |
4 | office | 502 | { |
10 | office | 503 | await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text); |
4 | office | 504 | |
5 | office | 505 | LobbySayTextBox.Text = string.Empty; |
4 | office | 506 | } |
507 | |||
9 | office | 508 | private void RemoteBindingsComboBoxSelectionChangeCompleted(object sender, EventArgs e) |
4 | office | 509 | { |
10 | office | 510 | UpdateRemoteItems(); |
4 | office | 511 | } |
8 | office | 512 | |
10 | office | 513 | private void WingManFormOnLoad(object sender, EventArgs e) |
8 | office | 514 | { |
10 | office | 515 | // await LoadLocalMouseKeyBindings(); |
516 | |||
517 | // await LoadRemoteMouseKeyBindings(); |
||
8 | office | 518 | } |
9 | office | 519 | |
10 | office | 520 | private void RemoteBindingsBindButtonClicked(object sender, EventArgs e) |
9 | office | 521 | { |
10 | office | 522 | if (string.IsNullOrEmpty((string) RemoteBindingsListBox.SelectedItem)) |
9 | office | 523 | { |
10 | office | 524 | RemoteBindingsListBox.BackColor = Color.LightPink; |
525 | return; |
||
526 | } |
||
9 | office | 527 | |
10 | office | 528 | RemoteBindingsListBox.BackColor = Color.Empty; |
9 | office | 529 | |
10 | office | 530 | ShowOverlayPanel(); |
9 | office | 531 | |
10 | office | 532 | MouseKeyCombo = new List<string>(); |
9 | office | 533 | |
10 | office | 534 | MouseKeyApplicationHook = Hook.GlobalEvents(); |
535 | MouseKeyApplicationHook.KeyUp += RemoteMouseKeyHookOnKeyUp; |
||
536 | MouseKeyApplicationHook.KeyDown += RemoteMouseKeyHookOnKeyDown; |
||
9 | office | 537 | } |
538 | |||
10 | office | 539 | private void RemoteBindingsUnbindButtonClicked(object sender, EventArgs e) |
9 | office | 540 | { |
10 | office | 541 | var item = (string) RemoteBindingsListBox.SelectedItem; |
542 | if (string.IsNullOrEmpty(item)) |
||
543 | { |
||
544 | RemoteBindingsListBox.BackColor = Color.LightPink; |
||
545 | return; |
||
546 | } |
||
9 | office | 547 | |
10 | office | 548 | RemoteBindingsListBox.BackColor = Color.Empty; |
9 | office | 549 | |
10 | office | 550 | var remoteKeyBinding = RemoteKeyBindings.Bindings.FirstOrDefault(binding => |
551 | string.Equals(binding.Name, item, StringComparison.Ordinal)); |
||
552 | |||
553 | if (remoteKeyBinding == null) |
||
554 | return; |
||
555 | |||
556 | RemoteKeyBindings.Bindings.Remove(remoteKeyBinding); |
||
557 | RemoteBindingsBindToBox.Text = string.Empty; |
||
9 | office | 558 | } |
559 | |||
560 | private void RemoteMouseKeyHookOnKeyDown(object sender, KeyEventArgs e) |
||
561 | { |
||
562 | e.SuppressKeyPress = true; |
||
563 | |||
10 | office | 564 | KeyConversion.KeysToString.TryGetValue((byte) e.KeyCode, out var key); |
9 | office | 565 | |
10 | office | 566 | MouseKeyCombo.Add(key); |
9 | office | 567 | } |
568 | |||
10 | office | 569 | private void RemoteMouseKeyHookOnKeyUp(object sender, KeyEventArgs e) |
9 | office | 570 | { |
10 | office | 571 | RemoteKeyBindings.Bindings.Add(new RemoteKeyBinding(RemoteBindingsComboBox.Text, |
9 | office | 572 | (string) RemoteBindingsListBox.SelectedItem, MouseKeyCombo)); |
573 | |||
574 | MouseKeyApplicationHook.KeyDown -= RemoteMouseKeyHookOnKeyDown; |
||
575 | MouseKeyApplicationHook.KeyUp -= RemoteMouseKeyHookOnKeyUp; |
||
576 | |||
577 | MouseKeyApplicationHook.Dispose(); |
||
578 | |||
579 | RemoteBindingsBindToBox.Text = string.Join(" + ", MouseKeyCombo); |
||
580 | HideOverlayPanel(); |
||
581 | |||
10 | office | 582 | // await SaveRemoteMouseKeyBindings(); |
9 | office | 583 | } |
584 | |||
10 | office | 585 | private void RemoteBindingsListBoxSelectedValueChanged(object sender, EventArgs e) |
9 | office | 586 | { |
10 | office | 587 | RemoteBindingsBindToBox.Text = ""; |
9 | office | 588 | |
10 | office | 589 | var name = (string) RemoteBindingsListBox.SelectedItem; |
590 | if (string.IsNullOrEmpty(name)) |
||
591 | return; |
||
9 | office | 592 | |
10 | office | 593 | foreach (var binding in RemoteKeyBindings.Bindings) |
594 | { |
||
595 | if (!string.Equals(binding.Name, name)) |
||
596 | continue; |
||
9 | office | 597 | |
10 | office | 598 | RemoteBindingsBindToBox.Text = string.Join(" + ", binding.Keys); |
599 | break; |
||
600 | } |
||
601 | } |
||
9 | office | 602 | |
10 | office | 603 | #region Saving and loading |
604 | |||
605 | private async Task SaveLocalMouseKeyBindings() |
||
606 | { |
||
607 | try |
||
608 | { |
||
609 | using (var memoryStream = new MemoryStream()) |
||
610 | { |
||
611 | LocalKeyBindings.XmlSerializer.Serialize(memoryStream, LocalKeyBindings); |
||
612 | |||
613 | memoryStream.Position = 0L; |
||
614 | |||
615 | using (var fileStream = new FileStream("LocalKeyBindings.xml", FileMode.Create)) |
||
616 | { |
||
617 | await memoryStream.CopyToAsync(fileStream); |
||
618 | } |
||
619 | } |
||
620 | } |
||
621 | catch (Exception) |
||
622 | { |
||
623 | ActivityTextBox.AppendText( |
||
624 | $"{Strings.Failed_saving_local_bindings}{Environment.NewLine}"); |
||
625 | } |
||
9 | office | 626 | } |
627 | |||
10 | office | 628 | private async Task LoadLocalMouseKeyBindings() |
629 | { |
||
630 | try |
||
631 | { |
||
632 | using (var fileStream = new FileStream("LocalKeyBindings.xml", FileMode.Open)) |
||
633 | { |
||
634 | using (var memoryStream = new MemoryStream()) |
||
635 | { |
||
636 | await fileStream.CopyToAsync(memoryStream); |
||
637 | |||
638 | memoryStream.Position = 0L; |
||
639 | |||
640 | var loadedBindings = |
||
641 | (LocalKeyBindings) LocalKeyBindings.XmlSerializer.Deserialize(memoryStream); |
||
642 | |||
643 | foreach (var binding in loadedBindings.Bindings) LocalKeyBindings.Bindings.Add(binding); |
||
644 | |||
645 | LocalListBoxBindingSource.ResetBindings(false); |
||
646 | } |
||
647 | } |
||
648 | } |
||
649 | catch (Exception) |
||
650 | { |
||
651 | ActivityTextBox.AppendText( |
||
652 | $"{Strings.Failed_loading_local_bindings}{Environment.NewLine}"); |
||
653 | } |
||
654 | } |
||
655 | |||
9 | office | 656 | private async Task SaveRemoteMouseKeyBindings() |
657 | { |
||
658 | try |
||
659 | { |
||
660 | using (var memoryStream = new MemoryStream()) |
||
661 | { |
||
10 | office | 662 | RemoteKeyBindings.XmlSerializer.Serialize(memoryStream, RemoteKeyBindings); |
9 | office | 663 | |
664 | memoryStream.Position = 0L; |
||
665 | |||
10 | office | 666 | using (var fileStream = new FileStream("RemoteKeyBindings.xml", FileMode.Create)) |
9 | office | 667 | { |
10 | office | 668 | await memoryStream.CopyToAsync(fileStream); |
9 | office | 669 | } |
670 | } |
||
671 | } |
||
672 | catch (Exception) |
||
673 | { |
||
674 | ActivityTextBox.AppendText( |
||
675 | $"{Strings.Failed_saving_remote_bindings}{Environment.NewLine}"); |
||
676 | } |
||
677 | } |
||
678 | |||
679 | private async Task LoadRemoteMouseKeyBindings() |
||
680 | { |
||
681 | try |
||
682 | { |
||
10 | office | 683 | using (var fileStream = new FileStream("RemoteKeyBindings.xml", FileMode.Open)) |
9 | office | 684 | { |
685 | using (var memoryStream = new MemoryStream()) |
||
686 | { |
||
10 | office | 687 | await fileStream.CopyToAsync(memoryStream); |
9 | office | 688 | |
689 | memoryStream.Position = 0L; |
||
690 | |||
691 | var loadedBindings = |
||
10 | office | 692 | (RemoteKeyBindings) RemoteKeyBindings.XmlSerializer.Deserialize(memoryStream); |
9 | office | 693 | |
10 | office | 694 | foreach (var binding in loadedBindings.Bindings) RemoteKeyBindings.Bindings.Add(binding); |
9 | office | 695 | } |
696 | } |
||
697 | } |
||
698 | catch (Exception) |
||
699 | { |
||
700 | ActivityTextBox.AppendText( |
||
701 | $"{Strings.Failed_loading_remote_bindings}{Environment.NewLine}"); |
||
702 | } |
||
703 | } |
||
10 | office | 704 | |
705 | #endregion |
||
1 | office | 706 | } |
707 | } |