WingMan – Diff between revs 6 and 7

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 6 Rev 7
Line 1... Line 1...
1 using System; 1 using System;
2 using System.Collections.Generic; 2 using System.Collections.Generic;
3 using System.Drawing; 3 using System.Drawing;
4 using System.Linq; 4 using System.Linq;
5 using System.Net; 5 using System.Net;
-   6 using System.Threading;
6 using System.Threading.Tasks; 7 using System.Threading.Tasks;
7 using System.Windows.Forms; 8 using System.Windows.Forms;
8 using Gma.System.MouseKeyHook; 9 using Gma.System.MouseKeyHook;
9 using WingMan.Communication; 10 using WingMan.Communication;
-   11 using WingMan.Lobby;
10 using WingMan.MouseKey; 12 using WingMan.MouseKey;
11 using WingMan.Properties; 13 using WingMan.Properties;
Line 12... Line 14...
12   14  
13 namespace WingMan 15 namespace WingMan
Line 17... Line 19...
17 public WingManForm() 19 public WingManForm()
18 { 20 {
19 InitializeComponent(); 21 InitializeComponent();
Line 20... Line 22...
20   22  
-   23 FormTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
Line 21... Line 24...
21 FormTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); 24 FormCancellationTokenSource = new CancellationTokenSource();
Line 22... Line 25...
22   25  
Line 23... Line 26...
23 MQTTCommunication = new MQTTCommunication(FormTaskScheduler); 26 MQTTCommunication = new MQTTCommunication(FormTaskScheduler, FormCancellationTokenSource.Token);
24   27  
25 MouseKeyBindings = new MouseKeyBindings(new List<MouseKeyBinding>()); 28 MouseKeyBindings = new MouseKeyBindings(new List<MouseKeyBinding>());
26   29  
27 HelmBindingSource = new BindingSource 30 HelmListBoxBindingSource = new BindingSource
28 { 31 {
29 DataSource = MouseKeyBindings.Bindings 32 DataSource = MouseKeyBindings.Bindings
Line 30... Line 33...
30 }; 33 };
-   34 HelmBindingsListBox.DisplayMember = "DisplayName";
-   35 HelmBindingsListBox.ValueMember = "Keys";
-   36 HelmBindingsListBox.DataSource = HelmListBoxBindingSource;
-   37  
31 HelmBindingsListBox.DisplayMember = "DisplayName"; 38 MouseKeyBindingsExchange = new MouseKeyBindingsExchange
32 HelmBindingsListBox.ValueMember = "Keys"; 39 {
33 HelmBindingsListBox.DataSource = HelmBindingSource; 40 ExchangeBindings = new List<MouseKeyBindingExchange>()
34   41 };
35 MouseKeyBindingsExchange = new MouseKeyBindingsExchange(); 42  
36 WingBindingSource = new BindingSource 43 WingBindingsComboBoxSource = new BindingSource
37 { 44 {
Line 38... Line 45...
38 DataSource = MouseKeyBindingsExchange.ExchangeBindings 45 DataSource = MouseKeyBindingsExchange.ExchangeBindings
39 }; 46 };
-   47 WingBindingsComboBox.DisplayMember = "Nick";
40 WingBindingsComboBox.DisplayMember = "Nick"; 48 WingBindingsComboBox.ValueMember = "MouseKeyBindings";
Line 41... Line 49...
41 WingBindingsComboBox.ValueMember = "Names"; 49 WingBindingsComboBox.DataSource = WingBindingsComboBoxSource;
42 WingBindingsComboBox.DataSource = WingBindingSource; 50  
-   51 // Start lobby message synchronizer.
43   52 LobbyMessageSynchronizer = new LobbyMessageSynchronizer(MQTTCommunication, FormTaskScheduler,
44 // Start lobby message synchronizer. 53 FormCancellationTokenSource.Token);
Line 45... Line 54...
45 LobbyMessageSynchronizer = new LobbyMessageSynchronizer(MQTTCommunication); 54 LobbyMessageSynchronizer.OnLobbyMessageReceived += OnLobbyMessageReceived;
46 LobbyMessageSynchronizer.OnLobbyMessageReceived += OnLobbyMessageReceived; -  
47   -  
48 // Start mouse key bindings synchronizer. -  
49 MouseKeyBindingsSynchronizer = new MouseKeyBindingsSynchronizer(MouseKeyBindings, MQTTCommunication); -  
50 MouseKeyBindingsSynchronizer.OnMouseKeyBindingsSynchronized += OnMouseKeyBindingsSynchronized; -  
51 } -  
52   -  
53 private void OnMouseKeyBindingsSynchronized(object sender, MouseKeyBindingsSynchronizedEventArgs e) -  
54 { -  
55 foreach (var binding in e.ExchangeBindings) -  
56 { -  
57 ActivityTextBox.AppendText( -  
58 $"{Strings.Synchronized_bindings_with_client} : {binding.Nick} : {binding.Names.Count}{Environment.NewLine}"); -  
59   -  
60   -  
61 var exchangeBindings = MouseKeyBindingsExchange.ExchangeBindings.FirstOrDefault(exchangeBinding => -  
62 string.Equals(exchangeBinding.Nick, binding.Nick, StringComparison.Ordinal)); -  
63   -  
64 // If the nick has not been registered add it. -  
65 if (exchangeBindings == null) -  
66 { -  
67 MouseKeyBindingsExchange.ExchangeBindings.Add(binding); -  
68 continue; -  
69 } -  
70   -  
71 // If the nick has been added, then update the binding names. -  
72 exchangeBindings.Names = binding.Names; -  
73   -  
74 // Update wing list box. -  
75 var selectedExchangeBinding = (MouseKeyBindingExchange) WingBindingsComboBox.SelectedItem; -  
76 if (selectedExchangeBinding == null || !string.Equals(selectedExchangeBinding.Nick, binding.Nick)) -  
77 continue; -  
78   -  
79 WingBindingsListBox.Items.Clear(); -  
Line 80... Line 55...
80 WingBindingsListBox.DisplayMember = "Name"; 55  
Line 81... Line 56...
81 WingBindingsListBox.ValueMember = "Name"; 56 // Start mouse key bindings synchronizer.
Line 82... Line 57...
82 WingBindingsListBox.Items.AddRange(exchangeBindings.Names.Select(name => (object) name).ToArray()); 57 MouseKeyBindingsSynchronizer = new MouseKeyBindingsSynchronizer(MouseKeyBindings, MQTTCommunication,
Line 83... Line 58...
83   58 FormTaskScheduler, FormCancellationTokenSource.Token);
Line 84... Line 59...
84 } 59 MouseKeyBindingsSynchronizer.OnMouseKeyBindingsSynchronized += OnMouseKeyBindingsSynchronized;
Line 85... Line 60...
85   60 }
Line 86... Line 61...
86 WingBindingSource.ResetBindings(false); 61  
Line 87... Line 62...
87 } 62 private static CancellationTokenSource FormCancellationTokenSource { get; set; }
Line 88... Line 63...
88   63  
Line 89... Line 64...
89 private static TaskScheduler FormTaskScheduler { get; set; } 64 private static TaskScheduler FormTaskScheduler { get; set; }
Line -... Line 65...
-   65  
-   66 private static IKeyboardMouseEvents MouseKeyApplicationHook { get; set; }
-   67  
-   68 private List<string> MouseKeyCombo { get; set; }
-   69  
-   70 private MouseKeyBindings MouseKeyBindings { get; }
-   71  
-   72 private BindingSource HelmListBoxBindingSource { get; }
-   73  
-   74 private BindingSource WingBindingsComboBoxSource { get; }
-   75  
-   76 private MouseKeyBindingsExchange MouseKeyBindingsExchange { get; }
-   77  
-   78 public MQTTCommunication MQTTCommunication { get; set; }
-   79  
-   80 public LobbyMessageSynchronizer LobbyMessageSynchronizer { get; set; }
-   81  
-   82 public MouseKeyBindingsSynchronizer MouseKeyBindingsSynchronizer { get; set; }
-   83  
-   84 /// <summary>
-   85 /// Clean up any resources being used.
-   86 /// </summary>
-   87 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
-   88 protected override void Dispose(bool disposing)
-   89 {
-   90 if (disposing && components != null)
-   91 {
-   92 FormCancellationTokenSource.Dispose();
-   93 FormCancellationTokenSource = null;
-   94  
-   95 components.Dispose();
-   96 }
-   97  
-   98 base.Dispose(disposing);
-   99 }
-   100  
-   101 private void OnMouseKeyBindingsSynchronized(object sender, MouseKeyBindingsSynchronizedEventArgs e)
-   102 {
-   103 ActivityTextBox.AppendText(
-   104 $"{Strings.Synchronized_bindings_with_client} : {e.Bindings.Nick} : {e.Bindings.MouseKeyBindings.Count}{Environment.NewLine}");
-   105  
-   106 var exchangeBindings = MouseKeyBindingsExchange.ExchangeBindings.FirstOrDefault(exchangeBinding =>
-   107 string.Equals(exchangeBinding.Nick, e.Bindings.Nick, StringComparison.Ordinal));
-   108  
-   109 // If the nick does not exist then add it.
-   110 if (exchangeBindings == null)
-   111 {
-   112 MouseKeyBindingsExchange.ExchangeBindings.Add(e.Bindings);
-   113 WingBindingsComboBoxSource.ResetBindings(false);
-   114 UpdateWingListBoxItems();
-   115 return;
-   116 }
-   117  
-   118 // If the bindings for the nick have not changed then do not update.
-   119 if (exchangeBindings.MouseKeyBindings.SequenceEqual(e.Bindings.MouseKeyBindings))
-   120 {
-   121 WingBindingsComboBoxSource.ResetBindings(false);
-   122 UpdateWingListBoxItems();
-   123 return;
-   124 }
-   125  
-   126 // Update the bindings.
-   127 exchangeBindings.MouseKeyBindings = e.Bindings.MouseKeyBindings;
-   128 WingBindingsComboBoxSource.ResetBindings(false);
90   129 UpdateWingListBoxItems();
91 private static IKeyboardMouseEvents MouseKeyApplicationHook { get; set; } 130 }
92   131  
93 private List<string> MouseKeyCombo { get; set; } 132 private void UpdateWingListBoxItems()
94   133 {
Line 263... Line 302...
263   302  
264 private void MouseKeyHookOnKeyUp(object sender, KeyEventArgs e) 303 private void MouseKeyHookOnKeyUp(object sender, KeyEventArgs e)
265 { 304 {
Line 266... Line 305...
266 MouseKeyBindings.Bindings.Add(new MouseKeyBinding(HelmNameTextBox.Text, MouseKeyCombo)); 305 MouseKeyBindings.Bindings.Add(new MouseKeyBinding(HelmNameTextBox.Text, MouseKeyCombo));
Line 267... Line 306...
267   306  
268 HelmBindingSource.ResetBindings(false); 307 HelmListBoxBindingSource.ResetBindings(false);
269   308  
270 MouseKeyApplicationHook.KeyDown -= MouseKeyHookOnKeyDown; 309 MouseKeyApplicationHook.KeyDown -= MouseKeyHookOnKeyDown;
Line 280... Line 319...
280   319  
281 private void MouseKeyHookOnMouseUp(object sender, MouseEventArgs e) 320 private void MouseKeyHookOnMouseUp(object sender, MouseEventArgs e)
282 { 321 {
Line 283... Line 322...
283 MouseKeyBindings.Bindings.Add(new MouseKeyBinding(HelmNameTextBox.Text, MouseKeyCombo)); 322 MouseKeyBindings.Bindings.Add(new MouseKeyBinding(HelmNameTextBox.Text, MouseKeyCombo));
Line 284... Line 323...
284   323  
285 HelmBindingSource.ResetBindings(false); 324 HelmListBoxBindingSource.ResetBindings(false);
286   325  
287 MouseKeyApplicationHook.KeyDown -= MouseKeyHookOnKeyDown; 326 MouseKeyApplicationHook.KeyDown -= MouseKeyHookOnKeyDown;
Line 318... Line 357...
318 var helmBinding = (MouseKeyBinding) HelmBindingsListBox.SelectedItem; 357 var helmBinding = (MouseKeyBinding) HelmBindingsListBox.SelectedItem;
319 if (helmBinding == null) 358 if (helmBinding == null)
320 return; 359 return;
Line 321... Line 360...
321   360  
322 MouseKeyBindings.Bindings.Remove(helmBinding); 361 MouseKeyBindings.Bindings.Remove(helmBinding);
323 HelmBindingSource.ResetBindings(false); 362 HelmListBoxBindingSource.ResetBindings(false);
Line 324... Line 363...
324 } 363 }
325   364  
326 private async void LobbySayButtonClick(object sender, EventArgs e) 365 private async void LobbySayButtonClick(object sender, EventArgs e)
Line 330... Line 369...
330 LobbySayTextBox.Text = string.Empty; 369 LobbySayTextBox.Text = string.Empty;
331 } 370 }
Line 332... Line 371...
332   371  
333 private void WingBindingsComboBoxSelectionChangeCompleted(object sender, EventArgs e) 372 private void WingBindingsComboBoxSelectionChangeCompleted(object sender, EventArgs e)
334 { -  
335 var exchangeBinding = (MouseKeyBindingExchange)WingBindingsComboBox.SelectedItem; -  
336 if (exchangeBinding == null) -  
337 return; -  
338   373 {
339 WingBindingsListBox.Items.Clear(); -  
340 WingBindingsListBox.DisplayMember = "Name"; -  
341 WingBindingsListBox.ValueMember = "Name"; -  
342 WingBindingsListBox.Items.AddRange(exchangeBinding.Names.Select(name => (object)name).ToArray()); 374 UpdateWingListBoxItems();
343 } 375 }
344 } 376 }