-
Notifications
You must be signed in to change notification settings - Fork 22
/
HistoryTrailRenderer.cpp
415 lines (366 loc) · 16.6 KB
/
HistoryTrailRenderer.cpp
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
#include "AircraftHistoryTrail.h"
#include "HistoryTrailData.h"
#include "HistoryTrailRenderer.h"
#include "HistoryTrailRepository.h"
#include "dialog/DialogManager.h"
#include "euroscope/EuroScopeCRadarTargetInterface.h"
#include "euroscope/EuroscopePluginLoopbackInterface.h"
#include "euroscope/EuroscopeRadarLoopbackInterface.h"
#include "euroscope/UserSetting.h"
#include "graphics/GdiGraphicsInterface.h"
using UKControllerPlugin::Dialog::DialogManager;
using UKControllerPlugin::Euroscope::EuroScopeCRadarTargetInterface;
using UKControllerPlugin::Euroscope::EuroscopePluginLoopbackInterface;
using UKControllerPlugin::Euroscope::EuroscopeRadarLoopbackInterface;
using UKControllerPlugin::Euroscope::UserSetting;
using UKControllerPlugin::HistoryTrail::HistoryTrailData;
using UKControllerPlugin::HistoryTrail::HistoryTrailRepository;
using UKControllerPlugin::Plugin::PopupMenuItem;
using UKControllerPlugin::Windows::GdiGraphicsInterface;
namespace UKControllerPlugin::HistoryTrail {
HistoryTrailRenderer::HistoryTrailRenderer(
const HistoryTrailRepository& trails,
EuroscopePluginLoopbackInterface& plugin,
const DialogManager& dialogManager,
int toggleCallbackFunctionId)
: toggleCallbackFunctionId(toggleCallbackFunctionId), dialogManager(dialogManager), plugin(plugin),
trails(trails)
{
this->pen = std::make_unique<Gdiplus::Pen>(Gdiplus::Color(255, 255, 255, 255));
this->brush = std::make_unique<Gdiplus::SolidBrush>(Gdiplus::Color(255, 255, 255, 255));
}
/*
Load settings from the ASR.
*/
void HistoryTrailRenderer::AsrLoadedEvent(UserSetting& userSetting)
{
this->visible = userSetting.GetBooleanEntry(this->visibleUserSettingKey, true);
this->historyTrailType = userSetting.GetIntegerEntry(this->trailTypeUserSettingKey, this->trailTypeDiamond);
this->historyTrailDotSize = userSetting.GetIntegerEntry(this->dotSizeUserSettingKey, 4);
this->historyTrailDotSizeFloat = static_cast<float>(this->historyTrailDotSize);
this->degradingTrails = userSetting.GetBooleanEntry(this->degradingUserSettingKey, true);
this->fadingTrails = userSetting.GetBooleanEntry(this->fadingUserSettingKey, true);
this->historyTrailLength = userSetting.GetIntegerEntry(this->trailLengthUserSettingKey, 15);
this->antialiasedTrails = userSetting.GetBooleanEntry(this->antialiasUserSettingKey, true);
this->startColour = std::make_unique<Gdiplus::Color>(255, 255, 130, 20);
this->startColour->SetFromCOLORREF(
userSetting.GetColourEntry(this->trailColourUserSettingKey, RGB(255, 130, 20)));
this->alphaPerDot = 255 / this->historyTrailLength;
this->minimumDisplayAltitude =
userSetting.GetIntegerEntry(this->minAltitudeFilterUserSettingKey, this->defaultMinAltitude);
this->maximumDisplayAltitude =
userSetting.GetIntegerEntry(this->maxAltitudeFilterUserSettingKey, this->defaultMaxAltitude);
this->filledDots = userSetting.GetBooleanEntry(this->dotFillUserSettingKey, false);
this->rotatedDots = userSetting.GetBooleanEntry(this->dotRotateUserSettingKey, false);
this->reducePerDot = (this->historyTrailDotSizeFloat / this->historyTrailLength) / 2;
// Load the dot drawing function
this->drawDot = this->GetDoDotFunction();
}
/*
Save settings to the ASR.
*/
void HistoryTrailRenderer::AsrClosingEvent(UserSetting& userSetting)
{
userSetting.Save(this->visibleUserSettingKey, this->visibleUserSettingDescription, this->visible);
userSetting.Save(this->trailTypeUserSettingKey, this->trailTypeUserSettingDescription, this->historyTrailType);
userSetting.Save(this->dotSizeUserSettingKey, this->dotSizeUserSettingDescription, this->historyTrailDotSize);
userSetting.Save(this->degradingUserSettingKey, this->degradingUserSettingDescription, this->degradingTrails);
userSetting.Save(this->fadingUserSettingKey, this->fadingUserSettingDescription, this->fadingTrails);
userSetting.Save(
this->trailLengthUserSettingKey, this->trailLengthUserSettingDescription, this->historyTrailLength);
userSetting.Save(
this->trailColourUserSettingKey, this->trailColourUserSettingDescription, this->startColour->ToCOLORREF());
userSetting.Save(this->antialiasUserSettingKey, this->antialiasUserSettingDescription, this->antialiasedTrails);
userSetting.Save(
this->minAltitudeFilterUserSettingKey,
this->minAltitudeFilterUserSettingDescription,
this->minimumDisplayAltitude);
userSetting.Save(
this->maxAltitudeFilterUserSettingKey,
this->maxAltitudeFilterUserSettingDescription,
this->maximumDisplayAltitude);
userSetting.Save(this->dotFillUserSettingKey, this->dotFillUserSettingDescription, this->filledDots);
userSetting.Save(this->dotRotateUserSettingKey, this->dotRotateUserSettingDescription, this->rotatedDots);
}
/*
Called when the configuration menu item is selected. Create dialog.
*/
void HistoryTrailRenderer::Configure(int functionId, std::string subject, RECT screenObjectArea)
{
COLORREF newColour =
RGB(this->startColour->GetRed(), this->startColour->GetGreen(), this->startColour->GetBlue());
HistoryTrailData data;
data.fade = &this->fadingTrails;
data.degrade = &this->degradingTrails;
data.antiAlias = &this->antialiasedTrails;
data.type = &this->historyTrailType;
data.length = &this->historyTrailLength;
data.colour = &newColour;
data.dotSize = &this->historyTrailDotSize;
data.maxAltitude = &this->maximumDisplayAltitude;
data.minAltitude = &this->minimumDisplayAltitude;
data.filledDots = &this->filledDots;
data.rotate = &this->rotatedDots;
this->dialogManager.OpenDialog(IDD_HISTORY_TRAIL, reinterpret_cast<LPARAM>(&data));
this->historyTrailDotSizeFloat = static_cast<float>(this->historyTrailDotSize);
this->startColour->SetFromCOLORREF(newColour);
this->alphaPerDot = 255 / this->historyTrailLength;
this->reducePerDot = (this->historyTrailDotSizeFloat / this->historyTrailLength) / 2;
// Change the rendering function
this->drawDot = this->GetDoDotFunction();
}
/*
Returns the alpha decrease per dot.
*/
auto HistoryTrailRenderer::GetAlphaPerDot() const -> int
{
return this->alphaPerDot;
}
/*
Returns antialiasing mode.
*/
auto HistoryTrailRenderer::GetAntiAliasedTrails() const -> bool
{
return this->antialiasedTrails;
}
/*
Returns the configuration menu item.
*/
auto HistoryTrailRenderer::GetConfigurationMenuItem() const -> PopupMenuItem
{
PopupMenuItem returnVal;
returnVal.firstValue = this->menuItemDescription;
returnVal.secondValue = "";
returnVal.callbackFunctionId = this->toggleCallbackFunctionId;
returnVal.checked = EuroScopePlugIn::POPUP_ELEMENT_NO_CHECKBOX;
returnVal.disabled = false;
returnVal.fixedPosition = false;
return returnVal;
}
/*
Return whether to degrade trails.
*/
auto HistoryTrailRenderer::GetDegradingTrails() const -> bool
{
return this->degradingTrails;
}
/*
Return whether to fade trails.
*/
auto HistoryTrailRenderer::GetFadingTrails() const -> bool
{
return this->fadingTrails;
}
/*
Returns the length of trail to be rendered.
*/
auto HistoryTrailRenderer::GetHistoryTrailLength() const -> int
{
return this->historyTrailLength;
}
/*
Returns the trail type.
*/
auto HistoryTrailRenderer::GetHistoryTrailType() const -> int
{
return this->historyTrailType;
}
/*
Returns the dot size.
*/
auto HistoryTrailRenderer::GetHistoryTrailDotSize() const -> int
{
return this->historyTrailDotSize;
}
/*
Get the maximum display altitude
*/
auto HistoryTrailRenderer::GetMaximumAltitudeFilter() const -> int
{
return this->maximumDisplayAltitude;
}
/*
Get the minimum display altitude
*/
auto HistoryTrailRenderer::GetMinimumAltitudeFilter() const -> int
{
return this->minimumDisplayAltitude;
}
auto HistoryTrailRenderer::GetFilledDots() const -> bool
{
return this->filledDots;
}
auto HistoryTrailRenderer::GetRotatedDots() const -> bool
{
return this->rotatedDots;
}
auto HistoryTrailRenderer::GetTrailColour() const -> Gdiplus::Color&
{
return *this->startColour;
}
/*
Returns whether or not history trails should be rendered.
*/
auto HistoryTrailRenderer::IsVisible() const -> bool
{
return this->visible;
}
/*
These don't get moved.
*/
void HistoryTrailRenderer::Move(RECT position, std::string objectDescription)
{
}
/*
Processes Euroscope dot commands.
*/
auto HistoryTrailRenderer::ProcessCommand(std::string command) -> bool
{
if (command == this->dotCommand) {
this->Configure(0, command, {});
return true;
}
return false;
}
void HistoryTrailRenderer::Render(GdiGraphicsInterface& graphics, EuroscopeRadarLoopbackInterface& radarScreen)
{
Gdiplus::Color currentColourArgb = *this->startColour;
this->pen->SetColor(currentColourArgb);
this->brush->SetColor(currentColourArgb);
// The dot we are to make.
Gdiplus::RectF dot;
int roundNumber;
// Loop through the history trails.
std::shared_ptr<EuroScopeCRadarTargetInterface> radarTarget;
for (const auto& aircraft : trails.trailData) {
// Check the radar target exists
const auto& callsign = aircraft->GetCallsign();
radarTarget = this->plugin.GetRadarTargetForCallsign(callsign);
if (!radarTarget) {
continue;
}
const auto& trail = aircraft->GetTrail();
// If there's one or fewer dots, they're not going fast enough or are off the screen, don't display the
// trail.
if (trail.size() < 2 || radarScreen.GetGroundspeedForCallsign(callsign) < this->minimumSpeed ||
radarScreen.PositionOffScreen(aircraft->GetTrail().rbegin()->position) ||
radarTarget->GetFlightLevel() < this->minimumDisplayAltitude ||
radarTarget->GetFlightLevel() > this->maximumDisplayAltitude) {
continue;
}
// Round number used to govern fade and degrade, reset the colour
roundNumber = 0;
currentColourArgb = *this->startColour;
this->pen->SetColor(currentColourArgb);
this->brush->SetColor(currentColourArgb);
// Reset the dot height and width
dot.Width = this->historyTrailDotSizeFloat;
dot.Height = this->historyTrailDotSizeFloat;
// Loop through the points and display. The points are in reverse order, so we need to start at the end.
for (auto position = ++trail.rbegin(); position != trail.rend(); ++position) {
// Translate to screen location
POINT dotCoordinates = radarScreen.ConvertCoordinateToScreenPoint(position->position);
graphics.Translated(
static_cast<Gdiplus::REAL>(dotCoordinates.x),
static_cast<Gdiplus::REAL>(dotCoordinates.y),
[&graphics, this, &dot, &roundNumber, &position] {
// Adjust the dot size and position as required
if (this->degradingTrails) {
dot.X = -(this->historyTrailDotSizeFloat / 2) + (roundNumber * reducePerDot);
dot.Y = -(this->historyTrailDotSizeFloat / 2) + (roundNumber * reducePerDot);
dot.Width = dot.Width - reducePerDot;
dot.Height = dot.Height - reducePerDot;
} else {
dot.X = -(this->historyTrailDotSizeFloat / 2);
dot.Y = -(this->historyTrailDotSizeFloat / 2);
}
if (this->rotatedDots) {
graphics.Rotated(static_cast<Gdiplus::REAL>(position->heading), [&graphics, &dot, this]() {
graphics.SetAntialias(this->antialiasedTrails);
this->drawDot(graphics, dot);
});
} else {
graphics.SetAntialias(this->antialiasedTrails);
this->drawDot(graphics, dot);
}
});
// If the trails are set to fade, reduce the alpha value for the next run
if (this->fadingTrails) {
currentColourArgb = Gdiplus::Color(
255 - (roundNumber * this->alphaPerDot),
currentColourArgb.GetRed(),
currentColourArgb.GetGreen(),
currentColourArgb.GetBlue());
this->pen->SetColor(currentColourArgb);
this->brush->SetColor(currentColourArgb);
}
// If we've done enough dots, we stop.
if (roundNumber == this->historyTrailLength) {
break;
}
roundNumber++;
}
}
}
/*
History Trails cannot have their position reset. Do nothing.
*/
void HistoryTrailRenderer::ResetPosition()
{
}
auto HistoryTrailRenderer::GetDoDotFunction() const
-> std::function<void(GdiGraphicsInterface&, const Gdiplus::RectF&)>
{
return this->filledDots && this->historyTrailType != this->trailTypeLine ? this->GetFillDotFunction()
: this->GetDrawDotFunction();
}
auto HistoryTrailRenderer::GetDrawDotFunction() const
-> std::function<void(Windows::GdiGraphicsInterface&, const Gdiplus::RectF&)>
{
// Diamonds
if (this->historyTrailType == this->trailTypeDiamond) {
return [this](Windows::GdiGraphicsInterface& graphics, const Gdiplus::RectF& area) {
graphics.DrawDiamond(area, *this->pen);
};
}
// Circles
if (this->historyTrailType == this->trailTypeCircle) {
return [this](Windows::GdiGraphicsInterface& graphics, const Gdiplus::RectF& area) {
graphics.DrawCircle(area, *this->pen);
};
}
// Lines
if (this->historyTrailType == this->trailTypeLine) {
return [this](Windows::GdiGraphicsInterface& graphics, const Gdiplus::RectF& area) {
graphics.DrawLine(
*this->pen,
Gdiplus::PointF{area.GetLeft(), area.GetBottom()},
Gdiplus::PointF{area.GetRight(), area.GetTop()});
};
}
// Rectangles
return [this](Windows::GdiGraphicsInterface& graphics, const Gdiplus::RectF& area) {
graphics.DrawRect(area, *this->pen);
};
}
auto HistoryTrailRenderer::GetFillDotFunction() const
-> std::function<void(Windows::GdiGraphicsInterface&, const Gdiplus::RectF&)>
{
// Diamonds
if (this->historyTrailType == this->trailTypeDiamond) {
return [this](Windows::GdiGraphicsInterface& graphics, const Gdiplus::RectF& area) {
graphics.FillDiamond(area, *this->brush);
};
}
// Circles
if (this->historyTrailType == this->trailTypeCircle) {
return [this](Windows::GdiGraphicsInterface& graphics, const Gdiplus::RectF& area) {
graphics.FillCircle(area, *this->brush);
};
}
// Rects
return [this](Windows::GdiGraphicsInterface& graphics, const Gdiplus::RectF& area) {
graphics.FillRect(area, *this->brush);
};
}
} // namespace UKControllerPlugin::HistoryTrail