WingMan – Diff between revs 33 and 36
?pathlinks?
Rev 33 | Rev 36 | |||
---|---|---|---|---|
Line 22... | Line 22... | |||
22 | |
22 | |
|
23 | namespace WingMan |
23 | namespace WingMan |
|
24 | { |
24 | { |
|
25 | public partial class WingManForm : Form |
25 | public partial class WingManForm : Form |
|
26 | { |
26 | { |
|
Line 27... | Line 27... | |||
27 | private const int tabControlDetachPixelOffset = 20; |
27 | private const int TabControlDetachPixelOffset = 20; |
|
28 | |
28 | |
|
29 | public WingManForm() |
29 | public WingManForm() |
|
Line 43... | Line 43... | |||
43 | AutoCompletion.OnLoadFailed += AutoCompletionOnLoadFailed; |
43 | AutoCompletion.OnLoadFailed += AutoCompletionOnLoadFailed; |
|
Line 44... | Line 44... | |||
44 | |
44 | |
|
45 | Task.Run(() => AutoCompletion.Load(Address.Name, Address.AutoCompleteCustomSource)); |
45 | Task.Run(() => AutoCompletion.Load(Address.Name, Address.AutoCompleteCustomSource)); |
|
46 | Task.Run(() => AutoCompletion.Load(Port.Name, Address.AutoCompleteCustomSource)); |
46 | Task.Run(() => AutoCompletion.Load(Port.Name, Address.AutoCompleteCustomSource)); |
|
- | 47 | Task.Run(() => AutoCompletion.Load(Nick.Name, Nick.AutoCompleteCustomSource)); |
||
Line 47... | Line 48... | |||
47 | Task.Run(() => AutoCompletion.Load(Nick.Name, Nick.AutoCompleteCustomSource)); |
48 | Task.Run(() => AutoCompletion.Load(LocalNameTextBox.Name, LocalNameTextBox.AutoCompleteCustomSource)); |
|
48 | |
49 | |
|
49 | MqttCommunication = new MqttCommunication(FormTaskScheduler, FormCancellationTokenSource.Token); |
50 | MqttCommunication = new MqttCommunication(FormTaskScheduler, FormCancellationTokenSource.Token); |
|
50 | MqttCommunication.OnClientAuthenticationFailed += OnMqttClientAuthenticationFailed; |
51 | MqttCommunication.OnClientAuthenticationFailed += OnMqttClientAuthenticationFailed; |
|
Line 63... | Line 64... | |||
63 | LocalCheckedListBoxBindingSource = new BindingSource |
64 | LocalCheckedListBoxBindingSource = new BindingSource |
|
64 | { |
65 | { |
|
65 | DataSource = LocalKeyBindings.Bindings |
66 | DataSource = LocalKeyBindings.Bindings |
|
66 | }; |
67 | }; |
|
67 | LocalCheckedListBoxBindingSource.ListChanged += LocalCheckedListBoxBindingSourceOnListChanged; |
68 | LocalCheckedListBoxBindingSource.ListChanged += LocalCheckedListBoxBindingSourceOnListChanged; |
|
- | 69 | |
||
68 | LocalBindingsCheckedListBox.DisplayMember = "DisplayName"; |
70 | LocalBindingsCheckedListBox.DisplayMember = "DisplayName"; |
|
69 | LocalBindingsCheckedListBox.ValueMember = "Keys"; |
71 | LocalBindingsCheckedListBox.ValueMember = "Keys"; |
|
70 | LocalBindingsCheckedListBox.DataSource = LocalCheckedListBoxBindingSource; |
72 | LocalBindingsCheckedListBox.DataSource = LocalCheckedListBoxBindingSource; |
|
Line 71... | Line 73... | |||
71 | |
73 | |
|
Line 76... | Line 78... | |||
76 | |
78 | |
|
77 | RemoteBindingsComboBoxSource = new BindingSource |
79 | RemoteBindingsComboBoxSource = new BindingSource |
|
78 | { |
80 | { |
|
79 | DataSource = KeyBindingsExchange.ExchangeBindings |
81 | DataSource = KeyBindingsExchange.ExchangeBindings |
|
- | 82 | }; |
||
- | 83 | RemoteBindingsComboBoxSource.ListChanged += RemoteBindingsComboBoxSourceOnListChanged; |
||
80 | }; |
84 | |
|
81 | RemoteBindingsComboBox.DisplayMember = "Nick"; |
85 | RemoteBindingsComboBox.DisplayMember = "Nick"; |
|
82 | RemoteBindingsComboBox.ValueMember = "KeyBindings"; |
86 | RemoteBindingsComboBox.ValueMember = "KeyBindings"; |
|
Line -... | Line 87... | |||
- | 87 | RemoteBindingsComboBox.DataSource = RemoteBindingsComboBoxSource; |
||
83 | RemoteBindingsComboBox.DataSource = RemoteBindingsComboBoxSource; |
88 | |
|
84 | |
89 | |
|
85 | // Start lobby message synchronizer. |
90 | // Start lobby message synchronizer. |
|
86 | LobbyMessageSynchronizer = new LobbyMessageSynchronizer(MqttCommunication, FormTaskScheduler, |
91 | LobbyMessageSynchronizer = new LobbyMessageSynchronizer(MqttCommunication, FormTaskScheduler, |
|
Line 111... | Line 116... | |||
111 | |
116 | |
|
Line 112... | Line 117... | |||
112 | private static IKeyboardMouseEvents MouseKeyApplicationHook { get; set; } |
117 | private static IKeyboardMouseEvents MouseKeyApplicationHook { get; set; } |
|
Line 113... | Line 118... | |||
113 | |
118 | |
|
Line 114... | Line 119... | |||
114 | private List<string> MouseKeyCombo { get; set; } |
119 | private List<string> MouseKeyCombo { get; set; } |
|
Line 115... | Line 120... | |||
115 | |
120 | |
|
Line 131... | Line 136... | |||
131 | |
136 | |
|
Line 132... | Line 137... | |||
132 | public KeyInterceptor KeyInterceptor { get; set; } |
137 | public KeyInterceptor KeyInterceptor { get; set; } |
|
Line 133... | Line 138... | |||
133 | |
138 | |
|
134 | public KeySimulator KeySimulator { get; set; } |
139 | public KeySimulator KeySimulator { get; set; } |
|
135 | |
140 | |
|
Line -... | Line 141... | |||
- | 141 | private static Point TabControlClickStartPosition { get; set; } |
||
- | 142 | private static TabControl DetachedTabControl { get; set; } |
||
- | 143 | private static Form DetachedForm { get; set; } |
||
- | 144 | |
||
- | 145 | private void RemoteBindingsComboBoxSourceOnListChanged(object sender, ListChangedEventArgs e) |
||
136 | private static Point tabControlClickStartPosition { get; set; } |
146 | { |
|
137 | private static TabControl DetachedTabControl { get; set; } |
147 | UpdateRemoteBindingsListBox(); |
|
138 | private static Form DetachedForm { get; set; } |
148 | } |
|
139 | |
149 | |
|
140 | public void OnDiscoveryPortMapFailed(object sender, DiscoveryFailedEventArgs args) |
150 | public void OnDiscoveryPortMapFailed(object sender, DiscoveryFailedEventArgs args) |
|
141 | { |
151 | { |
|
142 | switch (args.Type) |
152 | switch (args.Type) |
|
143 | { |
153 | { |
|
144 | case DiscoveryType.UPnP: |
154 | case DiscoveryType.Upnp: |
|
145 | ActivityTextBox.AppendText( |
155 | ActivityTextBox.AppendText( |
|
146 | $"{Strings.Failed_to_create_UPnP_port_mapping}{Environment.NewLine}"); |
156 | $"{Strings.Failed_to_create_UPnP_port_mapping}{Environment.NewLine}"); |
|
147 | break; |
157 | break; |
|
148 | case DiscoveryType.PMP: |
158 | case DiscoveryType.Pmp: |
|
149 | ActivityTextBox.AppendText( |
159 | ActivityTextBox.AppendText( |
|
Line 279... | Line 289... | |||
279 | // If the nick does not exist then add it. |
289 | // If the nick does not exist then add it. |
|
280 | if (exchangeBindings == null) |
290 | if (exchangeBindings == null) |
|
281 | { |
291 | { |
|
282 | KeyBindingsExchange.ExchangeBindings.Add(e.Bindings); |
292 | KeyBindingsExchange.ExchangeBindings.Add(e.Bindings); |
|
283 | RemoteBindingsComboBoxSource.ResetBindings(false); |
293 | RemoteBindingsComboBoxSource.ResetBindings(false); |
|
284 | UpdateRemoteItems(); |
- | ||
285 | return; |
294 | return; |
|
286 | } |
295 | } |
|
Line 287... | Line 296... | |||
287 | |
296 | |
|
288 | // If the bindings for the nick have not changed then do not update. |
297 | // If the bindings for the nick have not changed then do not update. |
|
- | 298 | if (e.Bindings.KeyBindings.Count == exchangeBindings.KeyBindings.Count && |
||
289 | if (exchangeBindings.KeyBindings.SequenceEqual(e.Bindings.KeyBindings)) |
299 | e.Bindings.KeyBindings.All(exchangeBindings.KeyBindings.Contains)) |
|
290 | { |
300 | { |
|
291 | RemoteBindingsComboBoxSource.ResetBindings(false); |
- | ||
292 | UpdateRemoteItems(); |
301 | RemoteBindingsComboBoxSource.ResetBindings(false); |
|
293 | return; |
302 | return; |
|
Line 294... | Line 303... | |||
294 | } |
303 | } |
|
- | 304 | |
||
295 | |
305 | // Update the bindings. |
|
- | 306 | exchangeBindings.KeyBindings.RemoveAll(binding => !e.Bindings.KeyBindings.Contains(binding)); |
||
296 | // Update the bindings. |
307 | exchangeBindings.KeyBindings.AddRange( |
|
297 | exchangeBindings.KeyBindings = e.Bindings.KeyBindings; |
- | ||
298 | RemoteBindingsComboBoxSource.ResetBindings(false); |
308 | e.Bindings.KeyBindings.Where(binding => !exchangeBindings.KeyBindings.Contains(binding))); |
|
Line 299... | Line 309... | |||
299 | UpdateRemoteItems(); |
309 | RemoteBindingsComboBoxSource.ResetBindings(false); |
|
300 | } |
310 | } |
|
301 | |
311 | |
|
302 | private void UpdateRemoteItems() |
312 | private void UpdateRemoteBindingsListBox() |
|
303 | { |
313 | { |
|
Line 304... | Line -... | |||
304 | var exchangeBindings = (List<KeyBinding>) RemoteBindingsComboBox.SelectedValue; |
- | ||
305 | if (exchangeBindings == null) |
- | ||
306 | return; |
- | ||
307 | |
- | ||
308 | var replaceMouseBindings = new ObservableCollection<RemoteKeyBinding>(); |
- | ||
309 | foreach (var remoteBinding in RemoteKeyBindings.Bindings) |
- | ||
310 | { |
- | ||
311 | if (!exchangeBindings.Any(binding => |
- | ||
312 | string.Equals(binding.Name, remoteBinding.Name, StringComparison.Ordinal))) |
- | ||
313 | continue; |
- | ||
314 | |
- | ||
315 | replaceMouseBindings.Add(remoteBinding); |
- | ||
316 | } |
- | ||
317 | |
314 | var keyBindingExchange = (KeyBindingExchange) RemoteBindingsComboBox.SelectedItem; |
|
318 | RemoteKeyBindings.Bindings.Clear(); |
315 | if (keyBindingExchange == null) |
|
319 | foreach (var binding in replaceMouseBindings) RemoteKeyBindings.Bindings.Add(binding); |
316 | return; |
|
- | 317 | |
||
320 | |
318 | RemoteBindingsListBox.Items.Clear(); |
|
- | 319 | var bindings = KeyBindingsExchange.ExchangeBindings |
||
321 | RemoteBindingsListBox.Items.Clear(); |
320 | .Where(binding => binding.Nick == keyBindingExchange.Nick) |
|
322 | RemoteBindingsListBox.DisplayMember = "Name"; |
321 | .SelectMany(binding => binding.KeyBindings.Select(keyBinding => keyBinding)) |
|
Line 323... | Line 322... | |||
323 | RemoteBindingsListBox.ValueMember = "Name"; |
322 | .Select(binding => (object) binding).ToArray(); |
|
324 | var bindings = exchangeBindings.Select(binding => (object) binding.Name).ToArray(); |
323 | |
|
Line 325... | Line 324... | |||
325 | if (bindings.Length == 0) |
324 | if (bindings.Length == 0) |
|
326 | return; |
325 | return; |
|
327 | |
326 | |
|
328 | RemoteBindingsListBox.Items.AddRange(bindings); |
327 | RemoteBindingsListBox.Items.AddRange(bindings); |
|
329 | } |
328 | } |
|
Line 330... | Line 329... | |||
330 | |
329 | |
|
331 | private void OnLobbyMessageReceived(object sender, LobbyMessageReceivedEventArgs e) |
330 | private void OnLobbyMessageReceived(object sender, LobbyMessageReceivedEventArgs e) |
|
Line 332... | Line 331... | |||
332 | { |
331 | { |
|
Line 350... | Line 349... | |||
350 | private async void HostButtonClickAsync(object sender, EventArgs e) |
349 | private async void HostButtonClickAsync(object sender, EventArgs e) |
|
351 | { |
350 | { |
|
352 | // Stop the MQTT server if it is running. |
351 | // Stop the MQTT server if it is running. |
|
353 | if (MqttCommunication.Running) |
352 | if (MqttCommunication.Running) |
|
354 | { |
353 | { |
|
- | 354 | // Remote UPnP and Pmp mappings. |
||
- | 355 | await Task.Delay(0, FormCancellationTokenSource.Token).ContinueWith(async _ => |
||
- | 356 | { |
||
- | 357 | await Discovery.DeleteMapping(DiscoveryType.Upnp, MqttCommunication.Port); |
||
- | 358 | await Discovery.DeleteMapping(DiscoveryType.Pmp, MqttCommunication.Port); |
||
- | 359 | }, |
||
- | 360 | FormCancellationTokenSource.Token, TaskContinuationOptions.LongRunning, FormTaskScheduler); |
||
- | 361 | |
||
355 | await MqttCommunication.Stop(); |
362 | await MqttCommunication.Stop(); |
|
- | 363 | |
||
356 | HostButton.BackColor = Color.Empty; |
364 | HostButton.BackColor = Color.Empty; |
|
Line 357... | Line 365... | |||
357 | |
365 | |
|
358 | // Enable controls. |
366 | // Enable controls. |
|
359 | ConnectButton.Enabled = true; |
367 | ConnectButton.Enabled = true; |
|
Line 368... | Line 376... | |||
368 | return; |
376 | return; |
|
Line 369... | Line 377... | |||
369 | |
377 | |
|
Line 370... | Line 378... | |||
370 | StoreConnectionAutocomplete(); |
378 | StoreConnectionAutocomplete(); |
|
- | 379 | |
||
- | 380 | // Try to reserve port: try UPnP followed by PMP. |
||
371 | |
381 | await Task.Delay(0, FormCancellationTokenSource.Token).ContinueWith(async _ => |
|
372 | // Try to reserve port: try UPnP followed by PMP. |
382 | { |
|
373 | if (!await Discovery.CreateMapping(DiscoveryType.UPnP, port) && |
383 | if (!await Discovery.CreateMapping(DiscoveryType.Upnp, port) && |
|
374 | !await Discovery.CreateMapping(DiscoveryType.PMP, port)) |
384 | !await Discovery.CreateMapping(DiscoveryType.Pmp, port)) |
|
- | 385 | ActivityTextBox.AppendText( |
||
- | 386 | $"{Strings.Failed_creating_automatic_port_mapping_please_make_sure_the_port_is_routed_properly}{Environment.NewLine}"); |
||
Line 375... | Line 387... | |||
375 | ActivityTextBox.AppendText( |
387 | }, |
|
376 | $"{Strings.Failed_creating_automatic_port_mapping_please_make_sure_the_port_is_routed_properly}{Environment.NewLine}"); |
388 | FormCancellationTokenSource.Token, TaskContinuationOptions.LongRunning, FormTaskScheduler); |
|
377 | |
389 | |
|
378 | // Start the MQTT server. |
390 | // Start the MQTT server. |
|
Line 466... | Line 478... | |||
466 | { |
478 | { |
|
467 | Password.BackColor = Color.LightPink; |
479 | Password.BackColor = Color.LightPink; |
|
468 | return false; |
480 | return false; |
|
469 | } |
481 | } |
|
Line 470... | Line 482... | |||
470 | |
482 | |
|
Line 471... | Line 483... | |||
471 | password = AES.ExpandKey(Password.Text); |
483 | password = Aes.ExpandKey(Password.Text); |
|
472 | |
484 | |
|
473 | Address.BackColor = Color.Empty; |
485 | Address.BackColor = Color.Empty; |
|
474 | Port.BackColor = Color.Empty; |
486 | Port.BackColor = Color.Empty; |
|
Line 529... | Line 541... | |||
529 | await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text); |
541 | await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text); |
|
Line 530... | Line 542... | |||
530 | |
542 | |
|
531 | LobbySayTextBox.Text = string.Empty; |
543 | LobbySayTextBox.Text = string.Empty; |
|
Line 532... | Line 544... | |||
532 | } |
544 | } |
|
533 | |
545 | |
|
534 | private void LocalAddBindingButtonClick(object sender, EventArgs e) |
546 | private async void LocalAddBindingButtonClick(object sender, EventArgs e) |
|
535 | { |
547 | { |
|
536 | if (string.IsNullOrEmpty(LocalNameTextBox.Text)) |
548 | if (string.IsNullOrEmpty(LocalNameTextBox.Text)) |
|
537 | { |
549 | { |
|
Line 549... | Line 561... | |||
549 | } |
561 | } |
|
Line 550... | Line 562... | |||
550 | |
562 | |
|
551 | LocalNameTextBox.BackColor = Color.Empty; |
563 | LocalNameTextBox.BackColor = Color.Empty; |
|
Line -... | Line 564... | |||
- | 564 | LocalBindingsCheckedListBox.BackColor = Color.Empty; |
||
- | 565 | |
||
- | 566 | LocalNameTextBox.AutoCompleteCustomSource.Add(LocalNameTextBox.Text); |
||
- | 567 | |
||
552 | LocalBindingsCheckedListBox.BackColor = Color.Empty; |
568 | await AutoCompletion.Save(LocalNameTextBox.Name, LocalNameTextBox.AutoCompleteCustomSource); |
|
Line 553... | Line 569... | |||
553 | |
569 | |
|
Line 554... | Line 570... | |||
554 | ShowOverlayPanel(); |
570 | ShowOverlayPanel(); |
|
Line 605... | Line 621... | |||
605 | LocalNameTextBox.BackColor = Color.Empty; |
621 | LocalNameTextBox.BackColor = Color.Empty; |
|
606 | } |
622 | } |
|
Line 607... | Line 623... | |||
607 | |
623 | |
|
608 | private async void LocalBindingsRemoveButtonClick(object sender, EventArgs e) |
624 | private async void LocalBindingsRemoveButtonClick(object sender, EventArgs e) |
|
609 | { |
625 | { |
|
610 | var helmBinding = (KeyBinding) LocalBindingsCheckedListBox.SelectedItem; |
626 | var localBinding = (KeyBinding) LocalBindingsCheckedListBox.SelectedItem; |
|
611 | if (helmBinding == null) |
627 | if (localBinding == null) |
|
Line 612... | Line 628... | |||
612 | return; |
628 | return; |
|
613 | |
629 | |
|
Line 614... | Line 630... | |||
614 | LocalKeyBindings.Bindings.Remove(helmBinding); |
630 | LocalKeyBindings.Bindings.Remove(localBinding); |
|
615 | LocalCheckedListBoxBindingSource.ResetBindings(false); |
631 | LocalCheckedListBoxBindingSource.ResetBindings(false); |
|
Line 628... | Line 644... | |||
628 | LobbySayTextBox.Text = string.Empty; |
644 | LobbySayTextBox.Text = string.Empty; |
|
629 | } |
645 | } |
|
Line 630... | Line 646... | |||
630 | |
646 | |
|
631 | private void RemoteBindingsComboBoxSelectionChangeCompleted(object sender, EventArgs e) |
647 | private void RemoteBindingsComboBoxSelectionChangeCompleted(object sender, EventArgs e) |
|
632 | { |
648 | { |
|
633 | UpdateRemoteItems(); |
649 | UpdateRemoteBindingsListBox(); |
|
Line 634... | Line 650... | |||
634 | } |
650 | } |
|
635 | |
651 | |
|
636 | private async void WingManFormOnLoad(object sender, EventArgs e) |
652 | private async void WingManFormOnLoad(object sender, EventArgs e) |
|
Line 760... | Line 776... | |||
760 | |
776 | |
|
761 | private void WingManTabControlMouseDown(object sender, MouseEventArgs e) |
777 | private void WingManTabControlMouseDown(object sender, MouseEventArgs e) |
|
762 | { |
778 | { |
|
Line 763... | Line 779... | |||
763 | if (e.Button != MouseButtons.Left) return; |
779 | if (e.Button != MouseButtons.Left) return; |
|
764 | |
780 | |
|
Line 765... | Line 781... | |||
765 | tabControlClickStartPosition = e.Location; |
781 | TabControlClickStartPosition = e.Location; |
|
766 | } |
782 | } |
|
767 | |
783 | |
|
768 | private void WingManTabControlMouseMove(object sender, MouseEventArgs e) |
784 | private void WingManTabControlMouseMove(object sender, MouseEventArgs e) |
|
769 | { |
785 | { |
|
770 | if (e.Button == MouseButtons.Left) |
786 | if (e.Button == MouseButtons.Left) |
|
Line 771... | Line 787... | |||
771 | { |
787 | { |
|
772 | var mouseOffsetX = tabControlClickStartPosition.X - e.X; |
788 | var mouseOffsetX = TabControlClickStartPosition.X - e.X; |
|
Line 773... | Line 789... | |||
773 | var mouseOffsetY = tabControlClickStartPosition.Y - e.Y; |
789 | var mouseOffsetY = TabControlClickStartPosition.Y - e.Y; |
|
774 | |
790 | |
|
Line 775... | Line 791... | |||
775 | if (mouseOffsetX <= tabControlDetachPixelOffset && mouseOffsetY <= tabControlDetachPixelOffset) |
791 | if (mouseOffsetX <= TabControlDetachPixelOffset && mouseOffsetY <= TabControlDetachPixelOffset) |
|
776 | return; |
792 | return; |
|
Line 777... | Line 793... | |||
777 | |
793 | |
|
778 | tabControl1.DoDragDrop(tabControl1.SelectedTab, DragDropEffects.Move); |
794 | tabControl1.DoDragDrop(tabControl1.SelectedTab, DragDropEffects.Move); |
|
Line 779... | Line 795... | |||
779 | tabControlClickStartPosition = new Point(); |
795 | TabControlClickStartPosition = new Point(); |
|
780 | |
796 | |
|
781 | return; |
797 | return; |
|
Line 812... | Line 828... | |||
812 | tabControl1.Padding.Y), |
828 | tabControl1.Padding.Y), |
|
813 | StartPosition = FormStartPosition.Manual, |
829 | StartPosition = FormStartPosition.Manual, |
|
814 | Location = MousePosition, |
830 | Location = MousePosition, |
|
815 | MaximizeBox = false, |
831 | MaximizeBox = false, |
|
816 | SizeGripStyle = SizeGripStyle.Hide, |
832 | SizeGripStyle = SizeGripStyle.Hide, |
|
817 | FormBorderStyle = FormBorderStyle.FixedSingle |
833 | FormBorderStyle = FormBorderStyle.FixedSingle, |
|
- | 834 | Name = Name, |
||
- | 835 | Text = Text, |
||
- | 836 | Icon = Icon |
||
818 | }; |
837 | }; |
|
Line 819... | Line 838... | |||
819 | |
838 | |
|
820 | DetachedTabControl = new TabControl |
839 | DetachedTabControl = new TabControl |
|
821 | { |
840 | { |