zkv1000 / Nasal / map.nas /
Newer Older
237 lines | 8.817kb
adds Map on MFD
Sébastien MARQUE authored on 2017-03-20
1
# vim: set foldmethod=marker foldmarker={{{,}}} :
2
var MapTiles = {
3
# code from http://wiki.flightgear.org/Canvas_Snippets#A_simple_tile_map
4
    new : func (display) {
5
        var m = { parents: [MapTiles] };
6
        m.display = display;
7
        m.group = m.display.createGroup();
8
        m.tile_size = 256;
9
        m.zoom = 10;
10
        m.maps_base = getprop("/sim/fg-home") ~ '/cache/maps';
moves some map infos in data...
Sébastien MARQUE authored on 2017-04-11
11
        m.makeUrl = string.compileTemplate('https://{server}/{type}/{z}/{x}/{y}.png{apikey}');
12
        m.makePath = string.compileTemplate(m.maps_base ~ '/{server}/{type}/{z}/{x}/{y}.png');
adds Map on MFD
Sébastien MARQUE authored on 2017-03-20
13
        m.num_tiles = [
14
            math.ceil( m.display.get('view[0]') / m.tile_size ) + 1, 
15
            math.ceil( m.display.get('view[1]') / m.tile_size ) + 1
16
        ];
17
        m.center_tile_offset = [
18
            (m.num_tiles[0] - 1) / 2,
19
            (m.num_tiles[1] - 1) / 2
20
        ];
21
        m.tiles = setsize([], m.num_tiles[0]);
22
        m.last_tile = [-1,-1];
moves some map infos in data...
Sébastien MARQUE authored on 2017-04-11
23
        m.last_type = data['tiles-type'];
adds Map on MFD
Sébastien MARQUE authored on 2017-03-20
24
        m.update_timer = maketimer(2, m, m.updateTiles);
25
        return m;
26
    },
27

            
28
# Simple user interface (Buttons for zoom and label for displaying it)
29
    changeZoom : func(d) {
30
        me.zoom = math.max(2, math.min(19, me.zoom + d));
31
        call(me.updateTiles, [], me);
32
    },
33

            
34
# initialize the map by setting up a grid of raster images  
35
    initialize_grid : func {
36
        for(var x = 0; x < me.num_tiles[0]; x += 1) {
37
            me.tiles[x] = setsize([], me.num_tiles[1]);
38
            for(var y = 0; y < me.num_tiles[1]; y += 1)
39
                me.tiles[x][y] = me.group.createChild("image", "map-tile");
40
        }
41
    },
42

            
43
# this is the callback that will be regularly called by the timer to update the map
44
    updateTiles : func() {
45
        var lat = getprop('/position/latitude-deg');
46
        var lon = getprop('/position/longitude-deg');
47

            
48
        var n = math.pow(2, me.zoom);
49
        var offset = [
50
            n * ((lon + 180) / 360) - me.center_tile_offset[0],
51
            (1 - math.ln(math.tan(lat * math.pi/180) + 1 / math.cos(lat * math.pi/180)) / math.pi) / 2 * n - me.center_tile_offset[1]
52
        ];
53
        var tile_index = [int(offset[0]), int(offset[1])];
54

            
55
        var ox = tile_index[0] - offset[0];
56
        var oy = tile_index[1] - offset[1];
57

            
58
        for(var x = 0; x < me.num_tiles[0]; x += 1)
59
            for(var y = 0; y < me.num_tiles[1]; y += 1)
60
                me.tiles[x][y]
61
                    .setTranslation(int((ox + x) * me.tile_size + 0.5), int((oy + y) * me.tile_size + 0.5));
62

            
63
        if(    tile_index[0] != me.last_tile[0]
64
                or tile_index[1] != me.last_tile[1]
moves some map infos in data...
Sébastien MARQUE authored on 2017-04-11
65
                or data['tiles-type'] != me.last_type ) {
adds Map on MFD
Sébastien MARQUE authored on 2017-03-20
66
            for(var x = 0; x < me.num_tiles[0]; x += 1)
67
                for(var y = 0; y < me.num_tiles[1]; y += 1) {
68
                    var pos = {
69
                        z: me.zoom,
70
                        x: int(offset[0] + x),
71
                        y: int(offset[1] + y),
moves some map infos in data...
Sébastien MARQUE authored on 2017-04-11
72
                        type: data['tiles-type'],
73
                        server : data['tiles-server'],
74
                        apikey: data['tiles-apikey'],
adds Map on MFD
Sébastien MARQUE authored on 2017-03-20
75
                    };
76

            
77
                    (func {
78
                        var img_path = me.makePath(pos);
79
                        printlog('debug', 'img_path: ', img_path);
80
                        var tile = me.tiles[x][y];
81

            
82
                        if( io.stat(img_path) == nil ) { # image not found, save in $FG_HOME
83
                            var img_url = me.makeUrl(pos);
84
                            printlog('debug', 'requesting ' ~ img_url);
85
                            http.save(img_url, img_path)
86
                                .done(func {printlog('info', 'received image ' ~ img_path); tile.set("src", img_path);})
87
                                .fail(func (r) printlog('warn', 'Failed to get image ' ~ img_path ~ ' ' ~ r.status ~ ': ' ~ r.reason));
88
                        }
89
                        else { # cached image found, reusing
90
                            printlog('debug', 'loading ' ~ img_path);
91
                            tile.set("src", img_path);
92
                        }
93
                    })();
94
                }
95
            me.last_tile = tile_index;
moves some map infos in data...
Sébastien MARQUE authored on 2017-04-11
96
            me.last_type = data['tiles-type'];
adds Map on MFD
Sébastien MARQUE authored on 2017-03-20
97
        }
98
    },
99

            
100
    del : func() {
101
        me.update_timer.stop();
102
        call(canvas.Window.del, [], me);
103
    },
104
};
105

            
106
var MapNavDisplay = {
107
    new : func (display) {
108
        var m = { parents: [MapNavDisplay] };
109
        m.display = display;
110
        m.map = m.display.createGroup().createChild('map');
111
        m.ctrl_ns = canvas.Map.Controller.get("Aircraft position");
112
        m.ctrl_ns.SOURCES["map-dialog"] = {
113
            getPosition: func subvec(geo.aircraft_position().latlon(), 0, 2),
114
            getAltitude: func getprop('/position/altitude-ft'),
115
            getHeading:  func {
116
                if (me.aircraft_heading)
117
                    getprop('/orientation/heading-deg')
118
                else 0
119
            },
120
            aircraft_heading: 1,
121
        };
122

            
123
        m.Styles = {
124
            get : func(type) return m.Styles[type],
125
        };
126

            
127
        m.Options = {
128
            get : func(type) return m.Options[type],
129
        };
130

            
131
        m.listeners = [];
132

            
133
        return m;
134
    },
135

            
136
    showMap : func {
137
        me.setMap();
138
        me.setStyles();
139
        me.setOptions();
140
        me.refresh();
141
    },
142

            
143
    setMap : func {
144
        var source = me.ctrl_ns.SOURCES["map-dialog"];
145
        me.map.setController("Aircraft position", "map-dialog");
146
        me.map.setRange(40);
147
        me.map.setTranslation(
148
            me.display.get("view[0]")/2,
149
            me.display.get("view[1]")/2
150
        );
151
    },
152

            
153
    setStyles : func {
154
## set up a few keys supported by the DME.symbol file to customize appearance: {{{
155
        me.Styles.DME = {};
156
        me.Styles.DME.debug = 1; # HACK for benchmarking/debugging purposes
157
        me.Styles.DME.animation_test = 0; # for prototyping animated symbols
158

            
159
        me.Styles.DME.scale_factor = 0.4; # 40% (applied to whole group)
160
        me.Styles.DME.line_width = 3.0;
161
        me.Styles.DME.color_tuned = [0,1,0]; #rgb
162
        me.Styles.DME.color_default = [1,1,0];  #rgb
163

            
164
        me.Styles.APT = {};
165
        me.Styles.APT.scale_factor = 0.4; # 40% (applied to whole group)
166
        me.Styles.APT.line_width = 3.0;
167
        me.Styles.APT.color_default = [0,0.6,0.85];  #rgb
168
        me.Styles.APT.label_font_color = me.Styles.APT.color_default;
169
        me.Styles.APT.label_font_size=16;
170

            
171
        me.Styles.TFC = {};
172
        me.Styles.TFC.scale_factor = 0.4; # 40% (applied to whole group)
173

            
174
        me.Styles.WPT = {};
175
        me.Styles.WPT.scale_factor = 0.5; # 50% (applied to whole group)
176

            
177
        me.Styles.RTE = {};
178
        me.Styles.RTE.line_width = 2;
179

            
180
        me.Styles.FLT = {};
181
        me.Styles.FLT.line_width = 3;
182

            
183
        me.Styles.FIX = {};
184
        me.Styles.FIX.color = [1,0,0];
185
        me.Styles.FIX.scale_factor = 0.4; # 40%
186

            
187
        me.Styles.VOR = {};
188
        me.Styles.VOR.range_line_width = 2;
189
        me.Styles.VOR.radial_line_width = 1;
190
        me.Styles.VOR.scale_factor = 0.6; # 60%
191

            
192
        me.Styles.APS = {};
193
        me.Styles.APS.scale_factor = 0.5;
194
    },
195
#}}}
196

            
197
    setOptions : func {
198
        me.Options.FLT = {};
199
    },
200

            
201
    make_update_wrapper : func(name) {
202
        if (!contains(me.Options, name)) me.Options[name] = {};
203
        me.Options[name].update_wrapper = func(layer, fn) {
204
            fn();
commit initial
Sébastien MARQUE authored on 2017-03-07
205
        }
adds Map on MFD
Sébastien MARQUE authored on 2017-03-20
206
    },
207

            
208
    ToggleLayerVisible : func(name) {
209
        (var l = me.map.getLayer(name)).setVisible(l.getVisible());
210
    },
211

            
212
    SetLayerVisible : func(name,n=1) {
213
        me.map.getLayer(name).setVisible(n);
214
    },
215

            
216
    refresh : func {
217
        var r = func(name,vis=1,zindex=nil) return caller(0)[0];
218
        # TODO: we'll need some z-indexing here, right now it's just random
219
        foreach(var type; [r('TFC',0),r('APT'),r('DME'),r('VOR'),r('NDB'),r('FIX',0),r('RTE'),r('WPT'),r('FLT'),r('WXR',0),r('APS'), ] ) {
220
            if (1 and type.name != 'APS' and type.name != 'FLT') me.make_update_wrapper(type.name);
221
            me.map.addLayer(factory: canvas.SymbolLayer, type_arg: type.name,
222
                    visible: type.vis, priority: type.zindex,
223
                    style: me.Styles.get(type.name),
224
                    options: me.Options.get(type.name) 
225
                    );
226
            (func {
227
                 # Notify MapStructure about layer visibility changes:
228
                 var name = type.name;
229
                 props.globals.initNode("/sim/gui/dialogs/map-canvas/draw-"~name, type.vis, "BOOL");
230
                 append(me.listeners,
231
                         setlistener("/sim/gui/dialogs/map-canvas/draw-"~name,
232
                             func(n) me.SetLayerVisible(name,n.getValue()), 1, 2)
233
                       );
234
             })();
commit initial
Sébastien MARQUE authored on 2017-03-07
235
        }
adds Map on MFD
Sébastien MARQUE authored on 2017-03-20
236
    },
237
};