-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMapView.cs
More file actions
435 lines (358 loc) · 10.5 KB
/
MapView.cs
File metadata and controls
435 lines (358 loc) · 10.5 KB
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
using System.Globalization;
using System.Net.NetworkInformation;
namespace ValveServerPicker
{
public partial class MapView : Form
{
/// <summary>
/// Represents a GeoMenuItem that can toggle the blocked state of an SDR.
/// </summary>
private class GeoMenuItem : ToolStripMenuItem
{
private readonly SteamSDR sdr;
private readonly MapView parent;
public GeoMenuItem(SteamSDR sdr, MapView parent)
{
this.parent = parent;
this.sdr = sdr;
this.Text = sdr.ToString();
this.Checked = sdr.Blocked;
this.Click += this.GeoMenuItem_Click;
}
private void GeoMenuItem_Click(object? sender, EventArgs e)
{
this.sdr.Blocked = !this.sdr.Blocked;
this.Checked = this.sdr.Blocked;
this.parent.TriggerDataUpdate();
}
}
/// <summary>
/// Wrapper for a Geo point which may contain multiple SDRs, and a screen position.
/// </summary>
/// <param name="lat"></param>
/// <param name="lon"></param>
private class Geo(float lat, float lon)
{
public float lat = lat, lon = lon;
public Point scrPos;
public bool InRadius(Geo other, float radius)
{
return Math.Abs(this.lat - other.lat) < radius && Math.Abs(this.lon - other.lon) < radius;
}
}
private const float geoClickRadius = 5;
private const int dotDiameter = 5;
// If set, trigger data update for this control
private readonly Control? dataContainer;
private readonly Dictionary<Geo, List<SteamSDR>> pointToSDRs = [];
private readonly System.Windows.Forms.Timer pingRedrawTimer = new();
// Constructor
public MapView(List<SteamSDR> sdrs, Control? dataContainer = null)
{
this.InitializeComponent();
this.dataContainer = dataContainer;
pingRedrawTimer.Interval = (int)RelayPinger.PingTimeOut.TotalMilliseconds;
pingRedrawTimer.Tick += (sender, e) =>
{
this.pic_Map.Invalidate();
};
this.UpdateSDRs(sdrs);
this.pingRedrawTimer.Start();
}
public void UpdateSDRs(List<SteamSDR> sdrs)
{
if (this.IsDisposed)
{
return;
}
this.pointToSDRs.Clear();
foreach (SteamSDR sdr in sdrs)
{
Geo geo = new(sdr.Geo.lat, sdr.Geo.lon);
// Check if any of the existing geos are within radius
bool found = false;
foreach (KeyValuePair<Geo, List<SteamSDR>> kvp in this.pointToSDRs)
{
if (geo.InRadius(kvp.Key, 0.5f))
{
kvp.Value.Add(sdr);
found = true;
break;
}
}
if (!found)
{
this.pointToSDRs[geo] = [sdr];
}
}
this.pic_Map.Invalidate();
}
public void TriggerDataUpdate()
{
this.dataContainer?.Refresh();
this.pic_Map.Invalidate();
}
private static double PointDistance(Point a, Point b)
{
return Math.Sqrt(Math.Pow(a.X - b.X, 2) + Math.Pow(a.Y - b.Y, 2));
}
private void handleMapClick(Point mapPos)
{
// Get the latitude and longitude of the click
// Point mapPos = this.pic_Map.PointToClient(clickPoint);
// Offset by some pixels because this sucks
mapPos.X -= (int)(geoClickRadius / 2);
mapPos.Y -= (int)(geoClickRadius / 2);
// Check if the click is within the map
if (mapPos.X < 0 || mapPos.Y < 0 || mapPos.X >= this.pic_Map.Width || mapPos.Y >= this.pic_Map.Height)
{
return;
}
// Update the position label
{
float lat = 90.0f - (mapPos.Y / (float)this.pic_Map.Height) * 180.0f;
float lon = 180.0f - (mapPos.X / (float)this.pic_Map.Width) * 360.0f;
#if DEBUG
this.lbl_Pos.Text = $"pX: {mapPos.X}, pY: {mapPos.Y}, lat: {lat.ToString(CultureInfo.InvariantCulture)}, lon: {lon.ToString(CultureInfo.InvariantCulture)}";
#else
this.lbl_Pos.Text = $"{lat.ToString(CultureInfo.InvariantCulture)}, {lon.ToString(CultureInfo.InvariantCulture)}";
#endif
}
// SDR selection
List<SteamSDR> selectedSDRs = [];
foreach (var kvp in this.pointToSDRs)
{
Geo geo = kvp.Key;
List<SteamSDR> cSdr = kvp.Value;
if (PointDistance(geo.scrPos, mapPos) < geoClickRadius)
{
selectedSDRs.AddRange(cSdr);
#if DEBUG
foreach (SteamSDR sdr in cSdr)
{
this.lbl_Pos.Text = this.lbl_Pos.Text + $"\nSDR: {sdr.Aliases[0]} {geo.scrPos.X}, {geo.scrPos.Y}";
}
#endif
}
}
// Add them to the context menu
this.cms_SelectedServers.Items.Clear();
foreach (SteamSDR sdr in selectedSDRs)
{
GeoMenuItem item = new(sdr, this);
this.cms_SelectedServers.Items.Add(item);
}
// Show the context menu
this.cms_SelectedServers.Show(this, mapPos);
}
// Robinson projection constants
private static readonly float[] robX = [1.0f, 0.9986f, 0.9954f, 0.99f, 0.9822f, 0.973f, 0.96f, 0.9427f, 0.9216f, 0.8962f, 0.8679f, 0.835f, 0.7986f, 0.7597f, 0.7186f, 0.6732f, 0.6213f, 0.5722f, 0.5322f];
private static readonly float[] robY = [0.0f, 0.062f, 0.124f, 0.186f, 0.248f, 0.31f, 0.372f, 0.434f, 0.4958f, 0.5571f, 0.6176f, 0.6769f, 0.7346f, 0.7903f, 0.8435f, 0.8936f, 0.9394f, 0.9761f, 1.0f];
private Point GetXY(float lat, float lon)
{
// Shift coordinates because of map projection
lon += -10.2f;
lat += 0.7f;
// Get index of latitude
float absLat = Math.Abs(lat);
int index = (int)(absLat / 5.0f);
float latRatio = (absLat % 5.0f) / 5.0f;
// Interpolate X and Y
float x = (robX[index] + latRatio * (robX[index + 1] - robX[index])) * (lon / 180.0f);
float y = (robY[index] + latRatio * (robY[index + 1] - robY[index])) * (lat >= 0 ? 1 : -1);
// Convert to pixel coordinates
int mapWidth = this.pic_Map.Width;
int mapHeight = this.pic_Map.Height;
int pixelX = (int)((x + 1) * 0.5f * mapWidth);
int pixelY = (int)((1 - (y + 1) * 0.5f) * mapHeight);
return new Point(pixelX, pixelY);
}
#region resize handling
private bool inResize = false;
private void MapView_ResizeBegin(object sender, EventArgs e)
{
this.inResize = true;
}
private void MapView_ResizeEnd(object sender, EventArgs e)
{
this.inResize = false;
this.pic_Map.Invalidate();
}
#endregion
#region dragging functions
private Rectangle? dragRect;
private void pic_Map_MouseDown(object sender, MouseEventArgs e)
{
// Start a drag event
if (e.Button == MouseButtons.Left)
{
this.dragRect = new(e.Location, new Size(0, 0));
}
}
private void pic_Map_MouseMove(object sender, MouseEventArgs e)
{
if (this.dragRect.HasValue)
{
// Update the rectangle
Point point = e.Location;
if (this.dragRect.Value.X < point.X)
{
if (this.dragRect.Value.Y < point.Y)
{
this.dragRect = new(this.dragRect.Value.Location, new Size(point.X - this.dragRect.Value.X, point.Y - this.dragRect.Value.Y));
}
else
{
this.dragRect = new(this.dragRect.Value.X, point.Y, point.X - this.dragRect.Value.X, this.dragRect.Value.Y - point.Y);
}
}
else
{
if (this.dragRect.Value.Y < point.Y)
{
this.dragRect = new(point.X, this.dragRect.Value.Y, this.dragRect.Value.X - point.X, point.Y - this.dragRect.Value.Y);
}
else
{
this.dragRect = new(point, new Size(this.dragRect.Value.X - point.X, this.dragRect.Value.Y - point.Y));
}
}
this.pic_Map.Invalidate();
}
}
private void pic_Map_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// Check if the drag was just a click
Point point = e.Location;
if (this.dragRect == null || (
this.dragRect.HasValue &&
this.dragRect.Value.Size.Width < 5
&& this.dragRect.Value.Size.Height < 5
))
{
this.handleMapClick(point);
}
else
{
this.UseWaitCursor = true;
bool didUpdate = false;
foreach (KeyValuePair<Geo, List<SteamSDR>> kvp in this.pointToSDRs)
{
Geo geo = kvp.Key;
if (this.dragRect.Value.Contains(geo.scrPos))
{
kvp.Value.ForEach(sdr => sdr.Blocked = !sdr.Blocked);
didUpdate = true;
}
}
if (didUpdate)
{
GC.Collect();
this.TriggerDataUpdate();
}
this.UseWaitCursor = false;
}
this.dragRect = null;
}
}
#endregion
/// <summary>
/// Paints the map and the SDRs on it.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pic_Map_Paint(object sender, PaintEventArgs e)
{
if (this.inResize)
{
// Access the pings so they do not expire
foreach (List<SteamSDR> sdrList in this.pointToSDRs.Values)
{
sdrList.ForEach(sdr =>
{
// Access the ping
_ = sdr.AvgPing;
});
}
return;
}
this.pingRedrawTimer.Stop();
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// Draw the geo points
foreach (KeyValuePair<Geo, List<SteamSDR>> kvp in this.pointToSDRs)
{
Geo geo = kvp.Key;
List<SteamSDR> sdrs = kvp.Value;
// Calculate the x and y
Point pos = this.GetXY(geo.lat, geo.lon);
// Set the screen position
geo.scrPos.X = pos.X;
geo.scrPos.Y = pos.Y;
// If all are disabled, draw red, if one or more is disabled, draw yellow, if all are enabled, draw green
Brush brush;
int blockedCount = 0;
// NOTE: This is an average of the average ping.
int pingAvg = 0;
int enabledCount = 0;
foreach (SteamSDR sdr in sdrs)
{
if (sdr.Blocked)
{
blockedCount++;
}
else
{
enabledCount++;
pingAvg += (int)sdr.AvgPing;
}
}
if (pingAvg != 0)
{
pingAvg /= enabledCount;
}
if (blockedCount == 0)
{
brush = Brushes.Green;
}
else if (blockedCount == sdrs.Count)
{
brush = Brushes.Red;
}
else
{
brush = Brushes.Orange;
}
g.FillEllipse(brush, pos.X - dotDiameter / 2, pos.Y - dotDiameter / 2, dotDiameter, dotDiameter);
string text = pingAvg >= 0 ? $"{pingAvg}ms" : "N/A";
Brush pingBrush = pingAvg >= 0 ? Brushes.Black : Brushes.Red;
SizeF v = g.MeasureString(text, this.Font);
g.DrawString(text, this.Font, pingBrush, pos.X - v.Width / 2, pos.Y + (v.Height / 4));
}
// Draw the dragRect
if (this.dragRect.HasValue)
{
// Point point = this.PointToClient(MousePosition);
g.DrawRectangle(Pens.Black, this.dragRect.Value);
}
// Restart the ping timer
this.pingRedrawTimer.Start();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
this.pingRedrawTimer.Stop();
this.pingRedrawTimer.Dispose();
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
}
}