WingMan – Blame information for rev 5

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