WingMan – Blame information for rev 6

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Collections.Generic;
3 using System.Drawing;
4 using System.Linq;
5 using System.Net;
6 office 6 using System.Threading.Tasks;
1 office 7 using System.Windows.Forms;
4 office 8 using Gma.System.MouseKeyHook;
1 office 9 using WingMan.Communication;
5 office 10 using WingMan.MouseKey;
11 using WingMan.Properties;
1 office 12  
13 namespace WingMan
14 {
2 office 15 public partial class WingManForm : Form
1 office 16 {
5 office 17 public WingManForm()
18 {
19 InitializeComponent();
1 office 20  
6 office 21 FormTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
1 office 22  
6 office 23 MQTTCommunication = new MQTTCommunication(FormTaskScheduler);
24  
5 office 25 MouseKeyBindings = new MouseKeyBindings(new List<MouseKeyBinding>());
1 office 26  
5 office 27 HelmBindingSource = new BindingSource
28 {
29 DataSource = MouseKeyBindings.Bindings
30 };
31 HelmBindingsListBox.DisplayMember = "DisplayName";
32 HelmBindingsListBox.ValueMember = "Keys";
33 HelmBindingsListBox.DataSource = HelmBindingSource;
1 office 34  
5 office 35 MouseKeyBindingsExchange = new MouseKeyBindingsExchange();
36 WingBindingSource = new BindingSource
37 {
38 DataSource = MouseKeyBindingsExchange.ExchangeBindings
39 };
40 WingBindingsComboBox.DisplayMember = "Nick";
41 WingBindingsComboBox.ValueMember = "Names";
42 WingBindingsComboBox.DataSource = WingBindingSource;
4 office 43  
5 office 44 // Start lobby message synchronizer.
45 LobbyMessageSynchronizer = new LobbyMessageSynchronizer(MQTTCommunication);
46 LobbyMessageSynchronizer.OnLobbyMessageReceived += OnLobbyMessageReceived;
4 office 47  
5 office 48 // Start mouse key bindings synchronizer.
49 MouseKeyBindingsSynchronizer = new MouseKeyBindingsSynchronizer(MouseKeyBindings, MQTTCommunication);
50 MouseKeyBindingsSynchronizer.OnMouseKeyBindingsSynchronized += OnMouseKeyBindingsSynchronized;
51 }
4 office 52  
5 office 53 private void OnMouseKeyBindingsSynchronized(object sender, MouseKeyBindingsSynchronizedEventArgs e)
54 {
6 office 55 foreach (var binding in e.ExchangeBindings)
5 office 56 {
6 office 57 ActivityTextBox.AppendText(
58 $"{Strings.Synchronized_bindings_with_client} : {binding.Nick} : {binding.Names.Count}{Environment.NewLine}");
4 office 59  
60  
6 office 61 var exchangeBindings = MouseKeyBindingsExchange.ExchangeBindings.FirstOrDefault(exchangeBinding =>
62 string.Equals(exchangeBinding.Nick, binding.Nick, StringComparison.Ordinal));
4 office 63  
6 office 64 // If the nick has not been registered add it.
65 if (exchangeBindings == null)
66 {
67 MouseKeyBindingsExchange.ExchangeBindings.Add(binding);
68 continue;
69 }
2 office 70  
6 office 71 // If the nick has been added, then update the binding names.
72 exchangeBindings.Names = binding.Names;
4 office 73  
6 office 74 // Update wing list box.
75 var selectedExchangeBinding = (MouseKeyBindingExchange) WingBindingsComboBox.SelectedItem;
76 if (selectedExchangeBinding == null || !string.Equals(selectedExchangeBinding.Nick, binding.Nick))
77 continue;
4 office 78  
6 office 79 WingBindingsListBox.Items.Clear();
80 WingBindingsListBox.DisplayMember = "Name";
81 WingBindingsListBox.ValueMember = "Name";
82 WingBindingsListBox.Items.AddRange(exchangeBindings.Names.Select(name => (object) name).ToArray());
4 office 83  
6 office 84 }
4 office 85  
6 office 86 WingBindingSource.ResetBindings(false);
1 office 87 }
88  
6 office 89 private static TaskScheduler FormTaskScheduler { get; set; }
90  
5 office 91 private static IKeyboardMouseEvents MouseKeyApplicationHook { get; set; }
92  
93 private List<string> MouseKeyCombo { get; set; }
94  
95 private MouseKeyBindings MouseKeyBindings { get; }
96  
97 private BindingSource HelmBindingSource { get; }
98  
99 private BindingSource WingBindingSource { get; set; }
100  
101 private MouseKeyBindingsExchange MouseKeyBindingsExchange { get; set; }
102  
103 public MQTTCommunication MQTTCommunication { get; set; }
104  
105 public LobbyMessageSynchronizer LobbyMessageSynchronizer { get; set; }
106  
107 public MouseKeyBindingsSynchronizer MouseKeyBindingsSynchronizer { get; set; }
108  
109 private void OnLobbyMessageReceived(object sender, LobbyMessageReceivedEventArgs e)
110 {
111 LobbyTextBox.Invoke((MethodInvoker) delegate
112 {
113 LobbyTextBox.AppendText($"{e.Nick} : {e.Message}{Environment.NewLine}");
114 });
115 }
116  
1 office 117 private void AddressTextBoxClick(object sender, EventArgs e)
118 {
119 Address.BackColor = Color.Empty;
120 }
121  
122 private void PortTextBoxClick(object sender, EventArgs e)
123 {
124 Port.BackColor = Color.Empty;
125 }
126  
127 private async void HostButtonClickAsync(object sender, EventArgs e)
128 {
129 // Stop the MQTT server if it is running.
5 office 130 if (MQTTCommunication.Running)
1 office 131 {
5 office 132 await MQTTCommunication.Stop().ConfigureAwait(false);
133 toolStripStatusLabel.Text = Strings.Server_stopped;
1 office 134 HostButton.BackColor = Color.Empty;
135  
3 office 136 // Enable controls.
1 office 137 ConnectButton.Enabled = true;
5 office 138 Address.Enabled = true;
139 Port.Enabled = true;
140 Nick.Enabled = true;
141  
1 office 142 return;
143 }
144  
2 office 145 if (!ValidateAddressPort(out var ipAddress, out var port, out var nick))
1 office 146 return;
147  
148 // Start the MQTT server.
5 office 149 await MQTTCommunication.Start(MQTTCommunicationType.Server, ipAddress, port, nick).ConfigureAwait(false);
150 toolStripStatusLabel.Text = Strings.Server_started;
1 office 151 HostButton.BackColor = Color.Aquamarine;
152  
3 office 153 // Disable controls
1 office 154 ConnectButton.Enabled = false;
3 office 155 Address.Enabled = false;
156 Port.Enabled = false;
157 Nick.Enabled = false;
158 }
159  
2 office 160 private bool ValidateAddressPort(out IPAddress address, out int port, out string nick)
1 office 161 {
162 address = IPAddress.Any;
163 port = 0;
2 office 164 nick = string.Empty;
1 office 165  
166 if (string.IsNullOrEmpty(Address.Text) &&
2 office 167 string.IsNullOrEmpty(Port.Text) &&
168 string.IsNullOrEmpty(Nick.Text))
1 office 169 {
170 Address.BackColor = Color.LightPink;
171 Port.BackColor = Color.LightPink;
2 office 172 Nick.BackColor = Color.LightPink;
1 office 173 return false;
174 }
175  
176 if (!IPAddress.TryParse(Address.Text, out address))
177 {
178 Address.BackColor = Color.LightPink;
179 return false;
180 }
181  
182 if (!uint.TryParse(Port.Text, out var uPort))
183 {
184 Port.BackColor = Color.LightPink;
185 return false;
186 }
187  
188 port = (int) uPort;
189  
2 office 190 if (string.IsNullOrEmpty(Nick.Text))
191 {
192 Nick.BackColor = Color.LightPink;
193 return false;
194 }
195  
196 nick = Nick.Text;
197  
1 office 198 Address.BackColor = Color.Empty;
199 Port.BackColor = Color.Empty;
2 office 200 Nick.BackColor = Color.Empty;
1 office 201  
202 return true;
203 }
204  
205 private async void ConnectButtonClickAsync(object sender, EventArgs e)
206 {
5 office 207 if (MQTTCommunication.Running)
1 office 208 {
5 office 209 await MQTTCommunication.Stop().ConfigureAwait(false);
210 ConnectButton.Text = Strings.Connect;
1 office 211 ConnectButton.BackColor = Color.Empty;
212  
5 office 213 Address.Enabled = true;
214 Port.Enabled = true;
215 Nick.Enabled = true;
1 office 216 HostButton.Enabled = true;
217 return;
218 }
219  
2 office 220 if (!ValidateAddressPort(out var ipAddress, out var port, out var nick))
1 office 221 return;
222  
5 office 223 await MQTTCommunication.Start(MQTTCommunicationType.Client, ipAddress, port, nick).ConfigureAwait(false);
1 office 224  
5 office 225 toolStripStatusLabel.Text = Strings.Client_started;
226 ConnectButton.Text = Strings.Disconnect;
1 office 227 ConnectButton.BackColor = Color.Aquamarine;
228  
229 HostButton.Enabled = false;
5 office 230 Address.Enabled = false;
231 Port.Enabled = false;
232 Nick.Enabled = false;
1 office 233 }
234  
2 office 235 private async void LobbySayTextBoxKeyDown(object sender, KeyEventArgs e)
236 {
237 if (e.KeyCode != Keys.Enter)
238 return;
239  
5 office 240 await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text).ConfigureAwait(false);
2 office 241  
5 office 242 LobbySayTextBox.Text = string.Empty;
2 office 243 }
4 office 244  
245 private void HelmAddButtonClick(object sender, EventArgs e)
246 {
247 if (string.IsNullOrEmpty(HelmNameTextBox.Text))
248 {
249 HelmNameTextBox.BackColor = Color.LightPink;
250 return;
251 }
252  
253 HelmAddButton.Enabled = false;
254  
255 MouseKeyCombo = new List<string>();
256  
257 MouseKeyApplicationHook = Hook.AppEvents();
258 MouseKeyApplicationHook.MouseDown += MouseKeyHookOnMouseDown;
259 MouseKeyApplicationHook.KeyUp += MouseKeyHookOnKeyUp;
260 MouseKeyApplicationHook.KeyDown += MouseKeyHookOnKeyDown;
261 MouseKeyApplicationHook.MouseUp += MouseKeyHookOnMouseUp;
262 }
263  
264 private void MouseKeyHookOnKeyUp(object sender, KeyEventArgs e)
265 {
5 office 266 MouseKeyBindings.Bindings.Add(new MouseKeyBinding(HelmNameTextBox.Text, MouseKeyCombo));
4 office 267  
268 HelmBindingSource.ResetBindings(false);
269  
270 MouseKeyApplicationHook.KeyDown -= MouseKeyHookOnKeyDown;
271 MouseKeyApplicationHook.KeyUp -= MouseKeyHookOnKeyUp;
5 office 272 MouseKeyApplicationHook.KeyDown -= MouseKeyHookOnKeyDown;
273 MouseKeyApplicationHook.KeyUp -= MouseKeyHookOnKeyUp;
4 office 274  
275 MouseKeyApplicationHook.Dispose();
276  
277 HelmNameTextBox.Text = string.Empty;
278 HelmAddButton.Enabled = true;
279 }
280  
281 private void MouseKeyHookOnMouseUp(object sender, MouseEventArgs e)
282 {
5 office 283 MouseKeyBindings.Bindings.Add(new MouseKeyBinding(HelmNameTextBox.Text, MouseKeyCombo));
4 office 284  
285 HelmBindingSource.ResetBindings(false);
286  
287 MouseKeyApplicationHook.KeyDown -= MouseKeyHookOnKeyDown;
288 MouseKeyApplicationHook.KeyUp -= MouseKeyHookOnKeyUp;
5 office 289 MouseKeyApplicationHook.KeyDown -= MouseKeyHookOnKeyDown;
290 MouseKeyApplicationHook.KeyUp -= MouseKeyHookOnKeyUp;
4 office 291  
292 MouseKeyApplicationHook.Dispose();
293  
294 HelmNameTextBox.Text = string.Empty;
295 HelmAddButton.Enabled = true;
296 }
297  
298  
299 private void MouseKeyHookOnMouseDown(object sender, MouseEventArgs e)
300 {
5 office 301 MouseKeyCombo.Add(e.Button.ToDisplayName());
4 office 302 }
303  
304 private void MouseKeyHookOnKeyDown(object sender, KeyEventArgs e)
305 {
306 e.SuppressKeyPress = true;
307  
5 office 308 MouseKeyCombo.Add(e.KeyCode.ToDisplayName());
4 office 309 }
310  
311 private void HelmNameTextBoxClick(object sender, EventArgs e)
312 {
313 HelmNameTextBox.BackColor = Color.Empty;
314 }
315  
5 office 316 private void HelmRemoveButtonClick(object sender, EventArgs e)
4 office 317 {
5 office 318 var helmBinding = (MouseKeyBinding) HelmBindingsListBox.SelectedItem;
319 if (helmBinding == null)
320 return;
4 office 321  
5 office 322 MouseKeyBindings.Bindings.Remove(helmBinding);
323 HelmBindingSource.ResetBindings(false);
4 office 324 }
325  
5 office 326 private async void LobbySayButtonClick(object sender, EventArgs e)
4 office 327 {
5 office 328 await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text).ConfigureAwait(false);
4 office 329  
5 office 330 LobbySayTextBox.Text = string.Empty;
4 office 331 }
332  
6 office 333 private void WingBindingsComboBoxSelectionChangeCompleted(object sender, EventArgs e)
4 office 334 {
6 office 335 var exchangeBinding = (MouseKeyBindingExchange)WingBindingsComboBox.SelectedItem;
5 office 336 if (exchangeBinding == null)
4 office 337 return;
338  
5 office 339 WingBindingsListBox.Items.Clear();
340 WingBindingsListBox.DisplayMember = "Name";
341 WingBindingsListBox.ValueMember = "Name";
342 WingBindingsListBox.Items.AddRange(exchangeBinding.Names.Select(name => (object)name).ToArray());
4 office 343 }
1 office 344 }
345 }