zkv1000 / Nasal / maps / route.nas /
Newer Older
381 lines | 14.28kb
adds route display on map
Sébastien MARQUE authored on 2017-05-11
1
# The following is largely inspired from the Extra500 Avidyne Entegra 9 
2
# https://gitlab.com/extra500/extra500.git
3
# Many thanks to authors: Dirk Dittmann and Eric van den Berg
4
var RouteItemClass = {
5
    new : func (canvasGroup, index) {
6
        var m = {parents:[RouteItemClass]};
7
        m.index = index;
8
        m.group = canvasGroup.createChild('group', 'Waypoint-' ~ index).setVisible(0);
9
        
10
        m.can = {
11
            Waypoint : m.group.createChild('path','icon-' ~ index)
12
                .setStrokeLineWidth(3)
13
                .setScale(1)
14
                .setColor(1,1,1)
15
                .setColorFill(1,1,1)
16
                .moveTo(-20, 0)
17
                .lineTo(-5, 5)
18
                .lineTo(0, 20)
19
                .lineTo(5, 5)
20
                .lineTo(20, 0)
21
                .lineTo(5, -5)
22
                .lineTo(0, -20)
23
                .lineTo(-5, -5)
24
                .close(),
25
            Label : m.group.createChild('text', 'wptLabel-' ~ index)
26
                .setFont('LiberationFonts/LiberationMono-Bold.ttf')
27
                .setTranslation(25,-25)
28
                .setFontSize(20, 1)
29
                .setColor(1,1,1)
30
                .setColorFill(1,1,1),
31
            track : canvasGroup.createChild('path','track-' ~ index)
32
                .setStrokeLineWidth(3)
33
                .setScale(1)
34
                .setColor(1,1,1)
35
                .setVisible(1),
36
        };
37
        return m;
38
    },
39
    setVisible : func (v) {
40
        me.group.setVisible(v);
41
        me.can.track.setVisible(v);
42
    },
43
    draw : func (wpt) {
44
        me.can.Label.setText(wpt[0].name);
45
        me.setColor(1,1,1);
46
        me.group.setGeoPosition(wpt[0].lat, wpt[0].lon);
47
        me.group.setVisible(1);
48
    },
49
    drawTrack : func (wpt) {
50
        var cmds = [];
51
        var coords = [];
52
        var cmd = canvas.Path.VG_MOVE_TO;
53
        me.can.track.setVisible(1);
54
        
55
        foreach (var pt; wpt) {
56
            append(coords, 'N' ~ pt.lat);
57
            append(coords, 'E' ~ pt.lon);
58
            append(cmds, cmd);
59
            cmd = canvas.Path.VG_LINE_TO;
60
        }
61
        me.can.track.setDataGeo(cmds, coords);
62
    },
63
    setColor : func (color) {
64
        me.can.Label.setColor(color).setColorFill(color);
65
        me.can.Waypoint.setColor(color).setColorFill(color);
66
    },
67
    del : func {
deletes route on display on ...
Sébastien MARQUE authored on 2017-05-20
68
        me.can.track.del();
adds route display on map
Sébastien MARQUE authored on 2017-05-11
69
        me.group.del();
70
        me = nil;
71
    },
72
};
73

            
74
var FMSIcon = {
75
    new : func (canvasGroup, text) {
76
        var m = {parents:[ FMSIcon ]};
77
        m.group = canvasGroup.createChild('group', 'FMS-' ~ text).setVisible(0);   
78
        m.can = {
79
            icon : m.group.createChild('path','FMS-icon' ~ text)
80
                .setStrokeLineWidth(3)
81
                .setScale(1)
82
                .setColor(0,1,0)
83
                .setColorFill(0,1,0)
84
                .moveTo(-15, 0)
85
                .lineTo(0, 15)
86
                .lineTo(15, 0)
87
                .lineTo(0, -15)
88
                .close(),
89
            label : m.group.createChild('text', 'FMS-label-' ~ text)
90
                .setFont('LiberationFonts/LiberationMono-Bold.ttf')
91
                .setTranslation(20,12)
92
                .setFontSize(32, 1)
93
                .setColor(0,1,0)
94
                .setColorFill(0,1,0)
95
                .setText(text),
96
        };
97
        return m;
98
    },
99
    setVisible : func (v)
100
        me.group.setVisible(v),
101
    setGeoPosition : func (lat, lon)
102
        me.group.setGeoPosition(lat, lon),
103
};
104

            
105
var FMSIconRTA = {
106
    new : func (canvasGroup, text) {
107
        var m = {parents:[FMSIconRTA]};
108
        m.group = canvasGroup.createChild('group', 'FMS-' ~ text).setVisible(0);   
109
        m.can = {
110
            icon : m.group.createChild('path','FMS-icon' ~ text)
111
                .setStrokeLineWidth(3)
112
                .setScale(1)
113
                .setColor(0,1,0)
114
                .setColorFill(0,1,0)
115
                .moveTo(-15, 0)
116
                .lineTo(0, 15)
117
                .lineTo(15, 0)
118
                .lineTo(0, -15)
119
                .close(),
120
            label : m.group.createChild('text', 'FMS-label-' ~ text)
121
                .setFont('LiberationFonts/LiberationMono-Bold.ttf')
122
                .setTranslation(-80,12)
123
                .setFontSize(32, 1)
124
                .setColor(0,1,0)
125
                .setColorFill(0,1,0)
126
                .setText(text),
127
        };
128
        return m;
129
    },
130
    setVisible : func (v)
131
        me.group.setVisible(v),
132
    setGeoPosition : func (lat, lon)
133
        me.group.setGeoPosition(lat, lon),
134
};
135

            
136
var MapRoute = {
137
    new : func (device, group) {
138
        var m = {parents:[ MapRoute ]}; 
139
        m.item       = [];
140
        m.itemIndex  = 0;
141
        m.visibility = 0;
142
        m.device = device;
143
        m.group = group.createChild('map', 'route map')
144
            .setTranslation(
145
                m.device.role == 'MFD' ? (m.device.data.mapview[0] + m.device.data.mapclip.left)/2 : 120,
146
                m.device.role == 'MFD' ? 400 : 600)
147
            .setVisible(m.visibility);
148
        m.group._node
149
            .getNode('range', 1).setDoubleValue(m.device.data['range-factor']);
150
        m.group._node
151
            .getNode('ref-lat', 1).setDoubleValue(data.lat);
152
        m.group._node
153
            .getNode('ref-lon', 1).setDoubleValue(data.lon);
154

            
155
        m.groupTrack = m.group.createChild('group', 'Track')
156
            .setVisible(1);
157
        m.groupOBS   = m.group.createChild('group', 'OBS')
158
            .setVisible(0);
159
        m.groupFMS   = m.group.createChild('group', 'FMS')
160
            .setVisible(1);
161
        
162
        m.can = {
163
            track : m.group.createChild('path', 'track')
164
                .setStrokeLineWidth(3)
165
                .setScale(1)
166
                .setColor(1,1,1),
167
            currentLeg : m.groupFMS.createChild('path', 'currentLeg')
168
                .setStrokeLineWidth(5)
169
                .setScale(1)
170
                .setColor(1,0,1),
171
            nextLeg : m.groupFMS.createChild('path', 'nextLeg')
172
                .setStrokeLineWidth(5)
173
                .setScale(1)
174
                .setStrokeDashArray([25,25])
175
                .setColor(1,0,1),
176
            obsCourse : m.groupOBS.createChild('path', 'obsCourse')
177
                .setStrokeLineWidth(5)
178
                .setScale(1)
179
                .setColor(1,0,1),
180
        };
181
        
182
        m.TOD = FMSIcon.new(m.groupFMS, 'TOD');
183
        m.TOC = FMSIcon.new(m.groupFMS, 'TOC');
184
        m.RTA = FMSIconRTA.new(m.groupFMS, 'RTA');
185
        
186
        m.track = {
187
            cmds  : [],
188
            coords: [],
189
        };
190
        m.currentLeg = {
191
            cmds: [ canvas.Path.VG_MOVE_TO, canvas.Path.VG_LINE_TO ],
192
            coords: [0, 0, 0, 0],
193
            index : -1,
194
        };
195
        m.nextLeg = {
196
            cmds: [ canvas.Path.VG_MOVE_TO, canvas.Path.VG_LINE_TO ],
197
            coords: [0, 0, 0, 0],
198
            index : -1,
199
        };
200
        
201
        m.mapOptions = {
202
            orientation : 0,
203
        };
204
        
205
        m.obsMode = 0;
206
        m.obsCourse = 0;
207
        m.obsWaypoint = RouteItemClass.new(m.groupOBS, 'obs_wp');
208
        m.obsCourseData = {
209
            cmds: [ canvas.Path.VG_MOVE_TO, canvas.Path.VG_LINE_TO ],
210
            coords: [0, 0, 0, 0]};
211

            
212
        m.flightPlan = [];
213
        m.currentWpIndex = getprop('/autopilot/route-manager/current-wp');
214
        
215
        return m;
216
    },
217
    update: func {
218
        me.visibility != 0 or return;
219
        me.group._node
220
            .getNode('range', 1).setDoubleValue(me.device.data['range-factor']);
221
        me.group._node.getNode('ref-lat', 1).setDoubleValue(data.lat);
222
        me.group._node.getNode('ref-lon', 1).setDoubleValue(data.lon);
223
    },
224
    setVisible : func (v) {
225
        if (me.visibility != v) {
226
            me.visibility = v;
227
            me.group.setVisible(v);
228
        }
229
    },
230
    updateOrientation : func (value) {
231
        me.mapOptions.orientation = value;
232
        me.can.obsCourse.setRotation((me.obsCourse - me.mapOptions.orientation) * D2R);
233
    },
234
    onFlightPlanChange : func {
235
        for (var i = size(me.item) - 1; i >= 0; i -= 1) {
236
            me.item[i].del();
237
            pop(me.item);
238
        }
239
        me.itemIndex = 0;
240
        me.flightPlan = [];
241
        var route = props.globals.getNode('/autopilot/route-manager/route');
242
        var planSize = getprop('/autopilot/route-manager/route/num');
243
        for (var i=0; i < planSize - 1; i+=1) {
244
            var wp0  = route.getNode('wp[' ~   i   ~ ']');
245
            var wp1  = route.getNode('wp[' ~ (i+1) ~ ']');
246
            append(me.flightPlan, [
247
                {
248
                    lat : wp0.getNode('latitude-deg').getValue(),
249
                    lon : wp0.getNode('longitude-deg').getValue(),
250
                    name: wp0.getNode('id').getValue(),
251
                },
252
                {
253
                    lat : wp1.getNode('latitude-deg').getValue(),
254
                    lon : wp1.getNode('longitude-deg').getValue(),
255
                    name: wp1.getNode('id').getValue(),
256
                }
257
            ]);
258
            append(me.item, RouteItemClass.new(me.groupTrack, i));
259
        }
260
        me.setVisible(me.device.map.visibility);
261
        me.drawWaypoints();
262
    },
263
    onCurrentWaypointChange : func (n) {
264
        me.currentWpIndex = n.getValue();
265
        me.currentLeg.index = me.currentWpIndex - 1;
266
        me.nextLeg.index = me.currentWpIndex;
267
        if (me.currentWpIndex == 0) {
268
            n.setIntValue(1);
269
            me.currentWpIndex = 1;
270
        }
271
        me.drawLegs();
272
    },
273
    drawWaypoints : func {
274
        me.visibility != 0 or return;
275
        var cmd = canvas.Path.VG_MOVE_TO;
276

            
277
        for (var i=0; i < size(me.flightPlan); i+=1) {
278
#            me.item[me.itemIndex].draw(me.flightPlan[i]);
279
            me.item[me.itemIndex].drawTrack(me.flightPlan[i]);
280
            me.itemIndex +=1;
281
        }
282
        me.groupTrack.setVisible(me.visibility and (me.obsMode == 0));
283
    },
284
    drawLegs : func {
285
        me.visibility != 0 or return;
286
        if (me.currentLeg.index >= 0 and me.currentLeg.index < size(me.flightPlan)) {
287
            var cmd = canvas.Path.VG_MOVE_TO;
288
            me.currentLeg.coords = [];
289
            me.currentLeg.cmds = [];
290
            foreach (var pt; me.flightPlan[me.currentLeg.index]) {
291
                append(me.currentLeg.coords, 'N' ~ pt.lat);
292
                append(me.currentLeg.coords, 'E' ~ pt.lon);
293
                append(me.currentLeg.cmds, cmd);
294
                cmd = canvas.Path.VG_LINE_TO;
295
            }
296
            me.can.currentLeg.setDataGeo(me.currentLeg.cmds, me.currentLeg.coords);
297
            me.can.currentLeg.setVisible(1);
298
        }
299
        else
300
            me.can.currentLeg.setVisible(0);
301
        
302
        if (me.nextLeg.index >= 1 and me.nextLeg.index < size(me.flightPlan)) {
303
            var cmd = canvas.Path.VG_MOVE_TO;
304
            me.nextLeg.coords = [];
305
            me.nextLeg.cmds = [];
306
            foreach (var pt; me.flightPlan[me.nextLeg.index]) {
307
                append(me.nextLeg.coords,'N' ~ pt.lat);
308
                append(me.nextLeg.coords,'E' ~ pt.lon);
309
                append(me.nextLeg.cmds,cmd);
310
                cmd = canvas.Path.VG_LINE_TO;
311
            }
312
            me.can.nextLeg.setDataGeo(me.nextLeg.cmds, me.nextLeg.coords);
313
            me.can.nextLeg.setVisible(1);
314
        }
315
        else
316
            me.can.nextLeg.setVisible(0);
317
    },
318
#    _onVisibilityChange : func {
319
#        me.group.setVisible(me.visibility and (me.obsMode == 0));
320
#        me.groupTrack.setVisible(me.visibility and (me.obsMode == 0));
321
#        me.groupFMS.setVisible(me.visibility and (me.obsMode == 0));
322
#        me.groupOBS.setVisible(me.visibility and (me.obsMode == 1));
323
#        me.TOD.setVisible(fms._dynamicPoint.TOD.visible and me.visibility);
324
#        me.TOC.setVisible(fms._dynamicPoint.TOC.visible and me.visibility);
325
#        me.RTA.setVisible(fms._dynamicPoint.RTA.visible and me.visibility);
326
#    },
327
#    _onFplReadyChange : func (n) {
328
#        me.TOD.setVisible(fms._dynamicPoint.TOD.visible and me.visibility);
329
#        me.TOC.setVisible(fms._dynamicPoint.TOC.visible and me.visibility);
330
#        me.RTA.setVisible(fms._dynamicPoint.RTA.visible and me.visibility);
331
#    },
332
#    _onFplUpdatedChange : func (n) {
333
#        if (fms._dynamicPoint.TOD.visible)
334
#            me.TOD.setGeoPosition(fms._dynamicPoint.TOD.position.lat, fms._dynamicPoint.TOD.position.lon);
335
#        if (fms._dynamicPoint.TOC.visible)
336
#            me.TOC.setGeoPosition(fms._dynamicPoint.TOC.position.lat, fms._dynamicPoint.TOC.position.lon);
337
#        if (fms._dynamicPoint.RTA.visible)
338
#            me.RTA.setGeoPosition(fms._dynamicPoint.RTA.position.lat, fms._dynamicPoint.RTA.position.lon);  
339
#        
340
#        me.TOD.setVisible(fms._dynamicPoint.TOD.visible and me.visibility);
341
#        me.TOC.setVisible(fms._dynamicPoint.TOC.visible and me.visibility);
342
#        me.RTA.setVisible(fms._dynamicPoint.RTA.visible and me.visibility);
343
#    },
344
#    _onObsModeChange : func (n) {
345
#        me.obsMode = n.getValue();
346
#        
347
#        if (me.obsMode==1) {
348
#            var wp = {
349
#                wp_lat : getprop('/instrumentation/gps[0]/scratch/latitude-deg'),
350
#                wp_lon : getprop('/instrumentation/gps[0]/scratch/longitude-deg'),
351
#                wp_name : getprop('/instrumentation/gps[0]/scratch/ident'),
352
#            };
353
#            var acLat = getprop('/position/latitude-deg');
354
#            var acLon = getprop('/position/longitude-deg');
355
#            
356
#            me.groupOBS.setGeoPosition(wp.wp_lat, wp.wp_lon);
357
#            
358
#            me.obsCourseData.coords = [
359
#                0,
360
#                0,
361
#                0,
362
#                5000,
363
#            ];
364
#            
365
#            me.obsWaypoint._can.Label.setText(wp.wp_name);
366
#            me.obsWaypoint.setColor(1,0,1);
367
#            me.obsWaypoint._group.setVisible(1);
368
#            
369
#            me.can.obsCourse.setData(me.obsCourseData.cmds,me.obsCourseData.coords);
370
#        }
371
#        
372
#        me.groupTrack.setVisible(me.visibility and (me.obsMode == 0));
373
#        me.groupFMS.setVisible(me.visibility and (me.obsMode == 0));
374
#        me.groupOBS.setVisible(me.visibility and (me.obsMode == 1));
375
#    },
376
#    _onObsCourseChange : func (n) {
377
#        me.obsCourse = n.getValue();
378
#        me.can.obsCourse.setRotation((me.obsCourse - me.mapOptions.orientation) * global.CONST.DEG2RAD);
379
#    },
380
};
381