-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.cs
460 lines (392 loc) · 17 KB
/
common.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using GMap.NET;
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.Net.Sockets;
using System.Xml; // config file
using System.Runtime.InteropServices; // dll imports
using ZedGraph; // Graphs
using System.Reflection;
using System.IO;
using System.Drawing.Drawing2D;
using System.Net.NetworkInformation;
namespace MultiWiiWinGUI
{
public class GMapMarkerRect : GMapMarker
{
public Pen Pen = new Pen(Brushes.White, 2);
public Color Color { get { return Pen.Color; } set { Pen.Color = value; } }
public GMapMarker InnerMarker;
public int wprad = 0;
public GMapControl MainMap;
public GMapMarkerRect(PointLatLng p)
: base(p)
{
Pen.DashStyle = DashStyle.Dot;
Size = new System.Drawing.Size(50, 50);
Offset = new System.Drawing.Point(-Size.Width / 2, -Size.Height / 2 - 20);
}
public override void OnRender(Graphics g)
{
base.OnRender(g);
if (wprad == 0 || MainMap == null)
return;
// undo autochange in mouse over
if (Pen.Color == Color.Blue)
Pen.Color = Color.White;
double width = (MainMap.MapProvider.Projection.GetDistance(MainMap.FromLocalToLatLng(0, 0), MainMap.FromLocalToLatLng(MainMap.Width, 0)) * 1000.0);
double height = (MainMap.MapProvider.Projection.GetDistance(MainMap.FromLocalToLatLng(0, 0), MainMap.FromLocalToLatLng(MainMap.Height, 0)) * 1000.0);
double m2pixelwidth = MainMap.Width / width;
double m2pixelheight = MainMap.Height / height;
GPoint loc = new GPoint((int)(LocalPosition.X - (m2pixelwidth * wprad * 2)), LocalPosition.Y);// MainMap.FromLatLngToLocal(wpradposition);
try
{
g.DrawArc(Pen, new System.Drawing.Rectangle((int)(LocalPosition.X - Offset.X - (Math.Abs(loc.X - LocalPosition.X) / 2)), (int)(LocalPosition.Y - Offset.Y - Math.Abs(loc.X - LocalPosition.X) / 2), (int)(Math.Abs(loc.X - LocalPosition.X)), (int)(Math.Abs(loc.X - LocalPosition.X))), 0, 360);
}
catch { }
}
}
public class GMapMarkerCopter : GMapMarker
{
const float rad2deg = (float)(180 / Math.PI);
const float deg2rad = (float)(1.0 / rad2deg);
static readonly System.Drawing.Size SizeSt = new System.Drawing.Size(global::MultiWiiWinGUI.Properties.Resources.quadicon.Width, global::MultiWiiWinGUI.Properties.Resources.quadicon.Height);
float heading = 0;
float cog = -1;
float target = -1;
byte coptertype;
public GMapMarkerCopter(PointLatLng p, float heading, float cog, float target, byte coptertype)
: base(p)
{
this.heading = heading;
this.cog = cog;
this.target = target;
this.coptertype = coptertype;
Size = SizeSt;
}
public override void OnRender(Graphics g)
{
Matrix temp = g.Transform;
g.TranslateTransform(LocalPosition.X, LocalPosition.Y);
Image pic = global::MultiWiiWinGUI.Properties.Resources.marker_tri;
switch (coptertype)
{
case 1:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_tri;
break;
case 2:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_quadp;
break;
case 3:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_quadx;
break;
case 4:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_bi;
break;
case 5:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_tri;
break;
case 6:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_y6;
break;
case 7:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_hex6p;
break;
case 8:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_fwing;
break;
case 9:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_y4;
break;
case 10:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_hex6x;
break;
case 11:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_octox8;
break;
case 12:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_octoflatx;
break;
case 13:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_oktoflatp;
break;
case 14:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_airplane;
break;
case 15:
case 16:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_heli;
break;
case 17:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_vtail4;
break;
case 18:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_hex6x;
break;
default:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_tri;
break;
}
int length = 100;
// anti NaN
g.DrawLine(new Pen(Color.Red, 2), 0.0f, 0.0f, (float)Math.Cos((heading - 90) * deg2rad) * length, (float)Math.Sin((heading - 90) * deg2rad) * length);
//g.DrawLine(new Pen(Color.Black, 2), 0.0f, 0.0f, (float)Math.Cos((cog - 90) * deg2rad) * length, (float)Math.Sin((cog - 90) * deg2rad) * length);
g.DrawLine(new Pen(Color.Orange, 2), 0.0f, 0.0f, (float)Math.Cos((target - 90) * deg2rad) * length, (float)Math.Sin((target - 90) * deg2rad) * length);
// anti NaN
g.RotateTransform(heading);
g.DrawImageUnscaled(pic,pic.Width /-2, pic.Height/-2);
g.Transform = temp;
}
}
public class GMapMarkerFlyHere : GMapMarker
{
static readonly System.Drawing.Size SizeSt = new System.Drawing.Size(global::MultiWiiWinGUI.Properties.Resources.home_marker.Width, global::MultiWiiWinGUI.Properties.Resources.home_marker.Height);
public GMapMarkerFlyHere(PointLatLng p)
: base(p)
{
Size = SizeSt;
}
public override void OnRender(Graphics g)
{
Matrix temp = g.Transform;
g.TranslateTransform(LocalPosition.X, LocalPosition.Y);
Image pic = global::MultiWiiWinGUI.Properties.Resources.flight_here;
g.DrawImageUnscaled(pic, pic.Width / -2 - 4, -pic.Height - 14);
g.Transform = temp;
}
}
public class GMapMarkerHome : GMapMarker
{
static readonly System.Drawing.Size SizeSt = new System.Drawing.Size(global::MultiWiiWinGUI.Properties.Resources.home_marker.Width, global::MultiWiiWinGUI.Properties.Resources.home_marker.Height);
public GMapMarkerHome(PointLatLng p)
: base(p)
{
Size = SizeSt;
}
public override void OnRender(Graphics g)
{
Matrix temp = g.Transform;
g.TranslateTransform(LocalPosition.X, LocalPosition.Y);
Image pic = global::MultiWiiWinGUI.Properties.Resources.home_marker;
g.DrawImageUnscaled(pic, pic.Width / -2 - 4, -pic.Height - 14);
g.Transform = temp;
}
}
public class GMapMarkerMissionStep : GMapMarker
{
static readonly System.Drawing.Size SizeSt = new System.Drawing.Size(global::MultiWiiWinGUI.Properties.Resources.marker_poi.Width, global::MultiWiiWinGUI.Properties.Resources.marker_poi.Height);
private byte number;
private byte markertype;
public GMapMarkerMissionStep(PointLatLng p, byte id, byte type)
: base(p)
{
Size = SizeSt;
number = id;
markertype = type;
}
public override void OnRender(Graphics g)
{
Image pic;
Matrix temp = g.Transform;
g.TranslateTransform(LocalPosition.X, LocalPosition.Y);
// Set graphics
switch (markertype)
{
case WP_ACTION.WAYPOINT:
pic = global::MultiWiiWinGUI.Properties.Resources.map_point1;
break;
case WP_ACTION.HOLD_UNLIM:
pic = global::MultiWiiWinGUI.Properties.Resources.poshold_markert;
break;
case WP_ACTION.HOLD_TIME:
pic = global::MultiWiiWinGUI.Properties.Resources.timed_marker;
break;
case WP_ACTION.SET_POI:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_poi;
break;
case WP_ACTION.LAND:
pic = global::MultiWiiWinGUI.Properties.Resources.marker_land;
break;
default:
pic = global::MultiWiiWinGUI.Properties.Resources.map_point1;
break;
}
g.DrawImageUnscaled(pic, pic.Width / -2 - 4, -pic.Height - 14);
System.Drawing.Font drawFont = new System.Drawing.Font(FontFamily.GenericMonospace, 9.0F, FontStyle.Bold);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White);
if (markertype == WP_ACTION.WAYPOINT)
{
drawBrush.Color = Color.Yellow;
g.DrawString("WP", drawFont, drawBrush, -8, -51);
drawBrush.Color = Color.White;
}
if (number < 10) g.DrawString(String.Format("{0:0}", number), drawFont, drawBrush, -5, -40);
if (number < 100 && number > 9) g.DrawString(String.Format("{0:0}", number), drawFont, drawBrush, -8, -40);
if (number > 100) g.DrawString(String.Format("{0:0}", number), drawFont, drawBrush, -12, -40);
g.Transform = temp;
}
}
public class Stuff
{
public static string GetIpByHostName(string hostName)
{
hostName = hostName.Trim();
if (hostName == string.Empty)
return string.Empty;
try
{
System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry(hostName);
return host.AddressList.GetValue(0).ToString();
}
catch (Exception)
{
return string.Empty;
}
}
public static bool PingNetwork(string hostNameOrAddress)
{
bool pingStatus = false;
hostNameOrAddress = hostNameOrAddress.Trim();
if (hostNameOrAddress == string.Empty)
return false;
try
{
string ip = string.Empty;
IPAddress ipadd;
if (IPAddress.TryParse(hostNameOrAddress, out ipadd))
{
ip = hostNameOrAddress;
}
else
{
ip = GetIpByHostName(hostNameOrAddress);
}
//string ip = GetIpByHostName(hostNameOrAddress);
using (Ping p = new Ping())
{
PingOptions options = new PingOptions();
options.DontFragment = true;
string data = "Test Data!";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 1000; // Timeout 时间,单位:毫秒
System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);
pingStatus = reply.Status == System.Net.NetworkInformation.IPStatus.Success;
}
}
catch (Exception ex)
{
Debug.Print(ex.Message);
pingStatus = false;
}
return pingStatus;
//using (Ping p = new Ping())
//{
// byte[] buffer = Encoding.ASCII.GetBytes("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
// int timeout = 4444; // 4s
// try
// {
// PingReply reply = p.Send(hostNameOrAddress, timeout, buffer);
// pingStatus = (reply.Status == IPStatus.Success);
// }
// catch (Exception)
// {
// pingStatus = false;
// }
//}
//return pingStatus;
}
}
public class InputBox
{
public delegate void ThemeManager(Control ctl);
public static event ThemeManager ApplyTheme;
public static string value = "";
//from http://www.csharp-examples.net/inputbox/
public static DialogResult Show(string title, string promptText, ref string value, bool password = false)
{
DialogResult answer = DialogResult.Cancel;
InputBox.value = value;
string passin = value;
// ensure we run this on the right thread - mono - mac
if (Application.OpenForms.Count > 0 && Application.OpenForms[0].InvokeRequired)
{
Application.OpenForms[0].Invoke((MethodInvoker)delegate
{
answer = ShowUI(title, promptText, passin, password);
});
}
else
{
answer = ShowUI(title, promptText, passin, password);
}
value = InputBox.value;
return answer;
}
static DialogResult ShowUI(string title, string promptText, string value, bool password = false)
{
Form form = new Form();
System.Windows.Forms.Label label = new System.Windows.Forms.Label();
TextBox textBox = new TextBox();
if (password)
textBox.UseSystemPasswordChar = true;
System.Windows.Forms.Button buttonOk = new System.Windows.Forms.Button();
System.Windows.Forms.Button buttonCancel = new System.Windows.Forms.Button();
//System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainV2));
//form.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
form.TopMost = true;
form.TopLevel = true;
form.Text = title;
label.Text = promptText;
textBox.Text = value;
buttonOk.Text = "OK";
buttonCancel.Text = "Cancel";
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;
label.SetBounds(9, 20, 372, 13);
textBox.SetBounds(12, 36, 372, 20);
buttonOk.SetBounds(228, 72, 75, 23);
buttonCancel.SetBounds(309, 72, 75, 23);
label.AutoSize = true;
textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
form.ClientSize = new Size(396, 107);
form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
form.FormBorderStyle = FormBorderStyle.FixedSingle;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
if (ApplyTheme != null)
ApplyTheme(form);
DialogResult dialogResult = DialogResult.Cancel;
Console.WriteLine("Input Box " + System.Threading.Thread.CurrentThread.Name);
Application.DoEvents();
form.ShowDialog();
Console.WriteLine("Input Box 2 " + System.Threading.Thread.CurrentThread.Name);
dialogResult = form.DialogResult;
if (dialogResult == DialogResult.OK)
{
value = textBox.Text;
InputBox.value = value;
}
form.Dispose();
form = null;
return dialogResult;
}
}
}