zkv1000 / Nasal / maps / navaids.nas /
Newer Older
531 lines | 18.596kb
add vim foldmarks
Sébastien MARQUE authored on 2017-12-21
1
# vim: set foldmethod=marker foldmarker={{{,}}} :
separates maps code
Sébastien MARQUE authored on 2017-05-11
2
# The following is largely inspired from the Extra500 Avidyne Entegra 9 
3
# https://gitlab.com/extra500/extra500.git
4
# Many thanks to authors: Dirk Dittmann and Eric van den Berg
5
var MapIconCache = {
6
# creates at init an icons cache for navaids, airports and airplane {{{
7
    new : func (svgFile) {
8
        var m = { parents:[MapIconCache] };
9

            
10
        m._canvas = canvas.new( {
11
            'name': 'MapIconCache',
12
            'size': [512, 512],
13
            'view': [512, 512],
14
            'mipmapping': 1
15
        });
16
        m._canvas.addPlacement( {'type': 'ref'} );
17
        m._canvas.setColorBackground(1,1,1,0);
18
        m._group = m._canvas.createGroup('MapIcons');
19

            
20
        canvas.parsesvg(m._group, data.zkv1000_reldir ~ svgFile);
21

            
22
        m._sourceRectMap = {};
23

            
24
        var icons = [ 'airplane' ];
25

            
26
        foreach (var near; [0, 1])
27
            foreach (var surface; [0, 1])
28
                foreach (var tower; [0, 1])
29
                    foreach (var center; tower ? [0, 1] : [ 0 ])
30
                        append(icons, 'Airport_' ~ near ~ surface ~ tower ~ center);
31

            
32
        foreach (var type; ['VOR', 'DME', 'TACAN', 'NDB'])
33
            append(icons, 'Navaid_' ~ type);
34

            
35
        foreach (var i; icons)
36
            m.registerIcon(i);
37

            
38
        return m;
39
    },
40
    registerIcon : func (id) {
41
        me._sourceRectMap[id] = {
42
            'bound' : [],
43
            'size'  : [],
44
        };
45
        var element = me._group.getElementById(id);
46
        if (element != nil) {
47
            me._sourceRectMap[id].bound = element.getTransformedBounds();
48
            # TODO ugly hack ? check for reason!
49
            var top     = 512 - me._sourceRectMap[id].bound[3];
50
            var bottom  = 512 - me._sourceRectMap[id].bound[1];
51
            me._sourceRectMap[id].bound[1] = top;
52
            me._sourceRectMap[id].bound[3] = bottom;
53

            
54
            me._sourceRectMap[id].size = [
55
                me._sourceRectMap[id].bound[2] - me._sourceRectMap[id].bound[0],
56
                me._sourceRectMap[id].bound[3] - me._sourceRectMap[id].bound[1]
57
            ];
58
        }
59
        else {
60
            print('MapIconCache.registerIcon(' ~ id ~ ') fail');
61
        }
62
    },
63
    getBounds : func (id) {
64
        return me._sourceRectMap[id].bound;
65
    },
66
    getSize : func (id) {
67
        return me._sourceRectMap[id].size;
68
    },
69
    boundIconToImage : func (id, image, center=1) {
70
        if (!contains(me._sourceRectMap, id)) {
71
            print('MapIconCache.boundIconToImage('~id~') ... no available.');
72
            id = 'Airport_0001';
73
        }
74
        image.setSourceRect(
75
                me._sourceRectMap[id].bound[0],
76
                me._sourceRectMap[id].bound[1],
77
                me._sourceRectMap[id].bound[2],
78
                me._sourceRectMap[id].bound[3],
79
                0);
80
        image.setSize(
81
                me._sourceRectMap[id].size[0],
82
                me._sourceRectMap[id].size[1]);
83
        if (center) {
84
            image.setTranslation(
85
                    -me._sourceRectMap[id].size[0]/2,
86
                    -me._sourceRectMap[id].size[1]/2);
87
        }
88
    },
89
};
90

            
91
var mapIconCache = MapIconCache.new('Models/MapIcons.svg');
92
# }}}
93

            
94
var MapAirportItem = {
95
# manage airports items by adding the ID and runways on associated icon {{{
96
    new : func (id) {
97
        var m = {parents:[MapAirportItem]};
98
        m._id = id;
99
        m._can = {
100
            'group' : nil,
101
            'label' : nil,
102
            'image' : nil,
103
            'layout': nil,
104
            'runway': [],
105
        };
106
        m._mapAirportIcon = {
107
            'near'      : 0,
108
            'surface'   : 0,
109
            'tower'     : 0,
110
            'center'    : 0,
111
            'displayed' : 0,
112
            'icon'      : '',
113
        };
114
        return m;
115
    },
116
    create : func (group) {
117
        me._can.group = group
118
            .createChild('group', 'airport_' ~ me._id);
119

            
120
        me._can.image = me._can.group.createChild('image', 'airport-image_' ~ me._id)
121
            .setFile(mapIconCache._canvas.getPath())
122
            .setSourceRect(0,0,0,0,0);
123

            
124
        me._can.label = me._can.group.createChild('text', 'airport-label_' ~ me._id)
125
            .setDrawMode( canvas.Text.TEXT )
126
            .setTranslation(0, 37)
127
            .setAlignment('center-bottom-baseline')
128
            .setFont('LiberationFonts/LiberationSans-Regular.ttf')
129
            .setFontSize(24);
130

            
131
        me._can.label.set('fill','#BACBFB');
132
        me._can.label.set('stroke','#000000');
133

            
134
        me._can.layout = group.createChild('group','airport_layout' ~ me._id);
135
        me._can.layoutIcon = group.createChild('group','airport_layout_Icon' ~ me._id);
136
        return me._can.group;
137
    },
138
    draw : func (apt, mapOptions) {
139
        me._mapAirportIcon.near = mapOptions.range > 32 ? 0 : 1;
140
        me._mapAirportIcon.surface  = 0;
141
        me._mapAirportIcon.tower    = 0;
142
        me._mapAirportIcon.center   = 0;
143
        me._mapAirportIcon.displayed    = 0;
144

            
145
        # TODO make departure and destination airports specific
146
        var aptInfo = airportinfo(apt.id);
147

            
148
        me._can.layout.removeAllChildren();
149
        me._can.layoutIcon.removeAllChildren();
150

            
151
        me._mapAirportIcon.tower = (size(aptInfo.comms('tower')) > 0);
152
        me._mapAirportIcon.center = me._mapAirportIcon.tower and (size(aptInfo.comms('approach')) > 0);
153

            
154
        foreach (var rwy; keys(aptInfo.runways)) {
155
            var runway = aptInfo.runways[rwy];
156
            me._mapAirportIcon.surface = MAP_RUNWAY_SURFACE[runway.surface] ? 1 : me._mapAirportIcon.surface;
157
            me._mapAirportIcon.displayed = runway.length > mapOptions.runwayLength ? 1 : me._mapAirportIcon.displayed;
158

            
159
            if (mapOptions.range <= 10) {    # drawing real runways
160
                me._can.layout.createChild('path', 'airport-runway-' ~ me._id ~ '-' ~ runway.id)
161
                    .setStrokeLineWidth(7)
162
                    .setColor(1,1,1)
163
                    .setColorFill(1,1,1)
164
                    .setDataGeo([
165
                        canvas.Path.VG_MOVE_TO,
166
                        canvas.Path.VG_LINE_TO,
167
                        canvas.Path.VG_CLOSE_PATH
168
                    ],[
169
                        'N' ~ runway.lat, 'E' ~ runway.lon,
170
                        'N' ~ runway.reciprocal.lat, 'E' ~ runway.reciprocal.lon,
171
                    ]);
172
            }
173
            elsif (mapOptions.range <= 32) {     #draw icon runways
174
                me._can.layoutIcon.setGeoPosition(apt.lat, apt.lon);
175
                me._can.layoutIcon.createChild('path', 'airport-runway-' ~ me._id ~ '-' ~ runway.id)
176
                    .setStrokeLineWidth(7)
177
                    .setColor(1,1,1)
178
                    .setColorFill(1,1,1)
179
                    .setData([
180
                        canvas.Path.VG_MOVE_TO,
181
                        canvas.Path.VG_LINE_TO,
182
                        canvas.Path.VG_CLOSE_PATH
183
                    ],[
184
                        0, -20,
185
                        0, 20,
186
                    ])
187
                    .setRotation((runway.heading)* D2R);
188
            }
189
        }
190
        me._mapAirportIcon.icon = 'Airport_'
191
            ~ me._mapAirportIcon.near
192
            ~ me._mapAirportIcon.surface
193
            ~ me._mapAirportIcon.tower
194
            ~ me._mapAirportIcon.center;
195

            
196
        if (me._mapAirportIcon.displayed) {
197
            me._can.label
198
                .setText(apt.id)
199
                .setRotation(-mapOptions.orientation * D2R);
200
            me._can.group.setGeoPosition(apt.lat, apt.lon);
201
            if (mapOptions.range <= 10) {
202
                me._can.image.setVisible(0);
203
                me._can.layout.setVisible(1);
204
            }
205
            elsif (mapOptions.range <= 32) {
206
                mapIconCache.boundIconToImage(me._mapAirportIcon.icon, me._can.image);
207
                me._can.image.setVisible(1);
208
                me._can.layout.setVisible(1);
209
            }
210
            else {
211
                mapIconCache.boundIconToImage(me._mapAirportIcon.icon, me._can.image);
212
                me._can.layout.setVisible(0);
213
                me._can.image.setVisible(1);
214
            }
215
            me._can.group.setVisible(1);
216
        }
217
        return me._mapAirportIcon.displayed;
218
    },
219
    update : func (mapOptions) {
220
        if (mapOptions.range <= 10) { }
221
        elsif (mapOptions.range <= 32)
222
            me._can.layoutIcon.setRotation(-mapOptions.orientation * D2R);
223
        else { }
224
    },
225
    setVisible : func (visibility) {
226
        me._can.group.setVisible(visibility);
227
        me._can.layout.setVisible(visibility);
228
        me._can.image.setVisible(visibility);
229
        me._can.layoutIcon.setVisible(visibility);
230
    },
231
};
232
# }}}
233

            
234
var MapNavaidItem = {
235
# manage navaids items by adding ID in the icon {{{
236
    new : func (id, type) {
237
        var m = {parents:[MapNavaidItem]};
238
        m._id = id;
239
        m._type = type;
240
        m._can = {
241
            'group' : nil,
242
            'label' : nil,
243
            'image' : nil,
244
        };
245
        return m;
246
    },
247
    create : func (group) {
248
        me._can.group = group
249
            .createChild('group', me._type ~ '_' ~ me._id);
250

            
251
        me._can.image = me._can.group.createChild('image', me._type ~ '-image_' ~ me._id)
252
            .setFile(mapIconCache._canvas.getPath())
253
            .setSourceRect(0,0,0,0,0);
254

            
255
        me._can.label = me._can.group.createChild('text', me._type ~ '-label_' ~ me._id)
256
            .setDrawMode( canvas.Text.TEXT )
257
            .setTranslation(0,42)
258
            .setAlignment('center-bottom-baseline')
259
            .setFont('LiberationFonts/LiberationSans-Regular.ttf')
lower text size for map nava...
Sébastien MARQUE authored on 2020-05-02
260
            .setFontSize(16);
separates maps code
Sébastien MARQUE authored on 2017-05-11
261

            
262
        me._can.label.set('fill','#BACBFB');
263
        me._can.label.set('stroke','#000000');
264

            
265
        return me._can.group;
266
    },
267
    setData : func (navaid, type, mapOptions) {
268
        mapIconCache.boundIconToImage('Navaid_' ~ type, me._can.image);
269
        me._can.label
270
            .setText(navaid.id)
271
            .setRotation(-mapOptions.orientation * D2R);
272
        me._can.group.setGeoPosition(navaid.lat, navaid.lon);
273
    },
274
    setVisible : func (visibility) {
275
        me._can.group.setVisible(visibility);
276
    },
277
};
278
# }}}
279

            
280
var MapAirplaneItem = {
281
# set airplane on ND {{{
282
    new : func {
283
        var m = {parents:[MapAirplaneItem]};
284
        m._can = {
285
            'group' : nil,
286
            'image' : nil,
287
        };
288
        return m;
289
    },
290
    create : func (group) {
291
        me._can.group = group
292
            .createChild('group', 'airplane');
293
        me._can.image = me._can.group.createChild('image', 'airplane-image')
294
            .setFile(mapIconCache._canvas.getPath())
295
            .setSourceRect(0,0,0,0,0);
296
        return me._can.group;
297
    },
298
    setData : func (orientation) {
299
        mapIconCache.boundIconToImage('airplane', me._can.image);
300
        me._can.group
301
            .setGeoPosition(data.lat, data.lon)
302
            .setRotation(orientation * D2R);
303
    },
304
    setVisible : func (visibility) {
305
        me._can.group.setVisible(visibility);
306
    },
307
};
308
# }}}
309

            
310
var MAP_RUNWAY_SURFACE =  {0:0, 1:1, 2:1, 3:0, 4:0, 5:0, 6:1, 7:1, 8:0, 9:0, 10:0, 11:0, 12:0};
311
var MAP_RUNWAY_AT_RANGE = func (range) {
312
    if (range < 40) return 0;
313
    if (range < 50) return 250;
314
    if (range < 80) return 500;
315
    if (range < 160) return 1000;
316
    if (range < 240) return 3000;
317
    return 3000;
318
}
319
var MAP_TXRANGE_VOR = func (range) {
320
    if (range < 40) return 0;
321
    if (range < 50) return 20;
322
    if (range < 80) return 25;
323
    if (range < 160) return 30;
324
    if (range < 240) return 50;
325
    return 100;
326
}
327
####
328
# Declutter
329
#   land
330
#       0 : 'Terrain'
331
#       1 : 'Political boundaries'
332
#       2 : 'River/Lakes/Oceans'
333
#       3 : 'Roads'
334
#   Nav
335
#       0 : 'Airspace'
336
#       1 : 'Victor/Jet airways'
337
#       2 : 'Obstacles'
338
#       3 : 'Navaids'
339

            
340
var MapNavaids = {
341
# the layer to show navaids, airports and airplane symbol {{{
342
    new : func (device, group) {
343
        var m = {parents : [MapNavaids]};
344

            
345
        m._model = nil;
346
        m.device = device;
347
        m._visibility = m.device.role == 'MFD';
348

            
349
        m._group = group.createChild('map', 'MFD map')
350
            .setTranslation(
351
                m.device.role == 'MFD' ? (m.device.data.mapview[0] + m.device.data.mapclip.left)/2 : 120,
352
                m.device.role == 'MFD' ? 400 : 600)
close issue with misplacing ...
Sébastien MARQUE authored on 2020-06-22
353
            .setRange(m.device.data['range-nm']/2)
separates maps code
Sébastien MARQUE authored on 2017-05-11
354
            .setVisible(m._visibility);
355

            
356
        m._can = {};
357
        m._cache = {};
358
        foreach (var n; ['airport', 'VOR', 'TACAN', 'NDB', 'DME']) {
359
            m._can[n] = m._group.createChild('group', n);
360
            m._cache[n] = {
361
                'data' : [],
362
                'index' : 0,
363
                'max' : 100,
364
            };
365
        }
366
        m._can['airplane'] = m._group.createChild('group', 'airplane');
367
        m._cache['airplane'] = {
368
            displayed : 0,
369
            item : nil,
370
        };
371

            
372
        m._mapOptions = {
373
            declutterLand : 3,
374
            declutterNAV  : 3,
375
            lightning     : 0,
376
            reports       : 0,
377
            overlay       : 0,
close issue with misplacing ...
Sébastien MARQUE authored on 2020-06-22
378
            range         : m.device.data['range-nm'] / 2,
separates maps code
Sébastien MARQUE authored on 2017-05-11
379
            runwayLength  : -1,
380
            orientation   : m.device.data.orientation.map,
381
        };
382

            
383
        m._results = nil;
384

            
improve the selection of dis...
Sébastien MARQUE authored on 2017-12-30
385
        if (m.device.role == 'PFD') {
386
            m.device.softkeys.colored.INSETNAVAIDSALL = 1;
387
            m.device.softkeys.colored.INSETNAVAIDSTACAN = 1;
388
            m.device.softkeys.colored.INSETNAVAIDSVOR = 1;
389
            m.device.softkeys.colored.INSETNAVAIDSNDB = 1;
390
            m.device.softkeys.colored.INSETNAVAIDSDME = 1;
391
            m.device.softkeys.colored.INSETNAVAIDSAPT = 1;
392
        }
393
        if (m.device.role == 'MFD') {
394
            m.device.softkeys.colored.MAPNAVAIDSALL = 1;
395
            m.device.softkeys.colored.MAPNAVAIDSTACAN = 1;
396
            m.device.softkeys.colored.MAPNAVAIDSVOR = 1;
397
            m.device.softkeys.colored.MAPNAVAIDSNDB = 1;
398
            m.device.softkeys.colored.MAPNAVAIDSDME = 1;
399
            m.device.softkeys.colored.MAPNAVAIDSAPT = 1;
400
        }
401

            
separates maps code
Sébastien MARQUE authored on 2017-05-11
402
        return m;
403
    },
poweroff improved
Sébastien MARQUE authored on 2020-05-16
404
    off: func {
405
        me._group.setVisible(0);
406
        me._group.removeAllChildren();
407
    },
separates maps code
Sébastien MARQUE authored on 2017-05-11
408
    update : func {
409
        me._group._node.getNode('ref-lat', 1).setDoubleValue(data.lat);
410
        me._group._node.getNode('ref-lon', 1).setDoubleValue(data.lon);
411

            
close issue with misplacing ...
Sébastien MARQUE authored on 2020-06-22
412
        me._group.setRange(me.device.data['range-nm']/2);
separates maps code
Sébastien MARQUE authored on 2017-05-11
413
        me._mapOptions.orientation = me.device.data.orientation.map;
414

            
415
        if (me._visibility == 1) {
416
            me.loadAirport();
417
            foreach (var n; ['VOR', 'TACAN', 'NDB', 'DME'])
418
                me.loadNavaid(n);
419
            me.loadAirplane();
420
        }
421
    },
422
    _onVisibilityChange : func {
423
        me._group.setVisible(me._visibility);
424
    },
425
    setMapOptions : func (mapOptions) {
426
        me._mapOptions = mapOptions;
427
        me.update();
428
    },
429
    updateOrientation : func (value) {
430
        me._mapOptions.orientation = value;
431
        for (var i = 0 ; i < me._cache.airport.index ; i +=1) {
432
            item = me._cache.airport.data[i];
433
            item.update(me._mapOptions);
434
        }
435
    },
436
    setRotation : func (deg) {
437
        me._group.setRotation(deg * D2R);
438
    },
439
    setVisible : func (v) {
440
        if (me._visibility != v) {
441
            me._visibility = v;
442
            me._onVisibilityChange();
443
        }
444
    },
445
    _onVisibilityChange : func {
446
        me._group.setVisible(me._visibility);
447
    },
448
    # positioned.findWithinRange : any, fix, vor, ndb, ils, dme, tacan
449

            
450
    loadAirport : func {
451
        me._cache.airport.index = 0;
452
        var results = positioned.findWithinRange(me._mapOptions.range * 2.5, 'airport');
453
        var item = nil;
454

            
455
        if (me._mapOptions.declutterNAV >= 2)
456
            me._mapOptions.runwayLength = MAP_RUNWAY_AT_RANGE(me._mapOptions.range);
457
        elsif (me._mapOptions.declutterNAV >= 1)
458
            me._mapOptions.runwayLength = 2000;
459
        else
460
            me._mapOptions.runwayLength = 3000;
461

            
462
        if (me._mapOptions.runwayLength >= 0) {
463
            foreach (var apt; results) {
464
                if (me._cache.airport.index >= me._cache.airport.max )
465
                    break;
466

            
467
                if (size(me._cache.airport.data) > me._cache.airport.index)
468
                    item = me._cache.airport.data[me._cache.airport.index];
469
                else {
470
                    item = MapAirportItem.new(me._cache.airport.index);
471
                    item.create(me._can.airport);
472
                    append(me._cache.airport.data, item);
473
                }
474

            
475
                if (item.draw(apt, me._mapOptions)) {
476
                    item.setVisible(1);
477
                    me._cache.airport.index += 1;
478
                }
479
            }
480
        }
481

            
482
        for (var i = me._cache.airport.index ; i < size(me._cache.airport.data) ; i +=1) {
483
            item = me._cache.airport.data[i];
484
            item.setVisible(0);
485
        }
486
    },
487
    loadNavaid : func (type) {
488
        me._cache[type].index = 0;
489
        if (me._mapOptions.declutterNAV >= 3) { # TODO test for DME and NDB range < 100nm
490
            var range = me._mapOptions.range * 2.5;
491
            var txRange = MAP_TXRANGE_VOR(me._mapOptions.range);
492
            var results = positioned.findWithinRange(range, type);
493
            var item = nil;
494
            foreach (var n; results) {
495
                if (n.range_nm < txRange)
496
                    break;
497

            
498
                if (me._cache[type].index >= me._cache[type].max )
499
                    break;
500

            
501
                if (size(me._cache[type].data) > me._cache[type].index) {
502
                    item = me._cache[type].data[me._cache[type].index];
503
                    item.setData(n, type, me._mapOptions);
504
                }
505
                else {
506
                    item = MapNavaidItem.new(me._cache[type].index, type);
507
                    item.create(me._can[type]);
508
                    item.setData(n, type, me._mapOptions);
509
                    append(me._cache[type].data, item);
510
                }
511
                item.setVisible(1);
512
                me._cache[type].index += 1;
513
            }
514
        }
515
        for (var i = me._cache[type].index ; i < size(me._cache[type].data) ; i +=1) {
516
            item = me._cache[type].data[i];
517
            item.setVisible(0);
518
        }
519
    },
520
    loadAirplane : func {
521
        if (!me._cache.airplane.displayed) {
522
            me._cache.airplane.item = MapAirplaneItem.new();
523
            me._cache.airplane.item.create(me._can['airplane']);
524
            me._cache.airplane.displayed = 1;
525
        }
526
        me._cache.airplane.item.setData(me.device.data.orientation.airplane);
527
        me._cache.airplane.item.setVisible(1);
528
    },
529
};
530
# }}}
531