adds Map on MFD
|
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...
|
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
|
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...
|
23 |
m.last_type = data['tiles-type']; |
adds Map on MFD
|
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() { |
|
put lat/lon in global data s...
|
45 |
# me.group.setRotation(-data.hdg * D2R); |
adds Map on MFD
|
46 | |
47 |
var n = math.pow(2, me.zoom); |
|
48 |
var offset = [ |
|
put lat/lon in global data s...
|
49 |
n * ((data.lon + 180) / 360) - me.center_tile_offset[0], |
50 |
(1 - math.ln(math.tan(data.lat * math.pi/180) + 1 / math.cos(data.lat * math.pi/180)) / math.pi) / 2 * n - me.center_tile_offset[1] |
|
adds Map on MFD
|
51 |
]; |
52 |
var tile_index = [int(offset[0]), int(offset[1])]; |
|
53 | ||
54 |
var ox = tile_index[0] - offset[0]; |
|
55 |
var oy = tile_index[1] - offset[1]; |
|
56 | ||
57 |
for(var x = 0; x < me.num_tiles[0]; x += 1) |
|
58 |
for(var y = 0; y < me.num_tiles[1]; y += 1) |
|
59 |
me.tiles[x][y] |
|
60 |
.setTranslation(int((ox + x) * me.tile_size + 0.5), int((oy + y) * me.tile_size + 0.5)); |
|
61 | ||
62 |
if( tile_index[0] != me.last_tile[0] |
|
63 |
or tile_index[1] != me.last_tile[1] |
|
moves some map infos in data...
|
64 |
or data['tiles-type'] != me.last_type ) { |
adds Map on MFD
|
65 |
for(var x = 0; x < me.num_tiles[0]; x += 1) |
66 |
for(var y = 0; y < me.num_tiles[1]; y += 1) { |
|
67 |
var pos = { |
|
68 |
z: me.zoom, |
|
69 |
x: int(offset[0] + x), |
|
70 |
y: int(offset[1] + y), |
|
moves some map infos in data...
|
71 |
type: data['tiles-type'], |
72 |
server : data['tiles-server'], |
|
73 |
apikey: data['tiles-apikey'], |
|
adds Map on MFD
|
74 |
}; |
75 | ||
76 |
(func { |
|
77 |
var img_path = me.makePath(pos); |
|
78 |
printlog('debug', 'img_path: ', img_path); |
|
79 |
var tile = me.tiles[x][y]; |
|
80 | ||
81 |
if( io.stat(img_path) == nil ) { # image not found, save in $FG_HOME |
|
82 |
var img_url = me.makeUrl(pos); |
|
83 |
printlog('debug', 'requesting ' ~ img_url); |
|
84 |
http.save(img_url, img_path) |
|
85 |
.done(func {printlog('info', 'received image ' ~ img_path); tile.set("src", img_path);}) |
|
86 |
.fail(func (r) printlog('warn', 'Failed to get image ' ~ img_path ~ ' ' ~ r.status ~ ': ' ~ r.reason)); |
|
87 |
} |
|
88 |
else { # cached image found, reusing |
|
89 |
printlog('debug', 'loading ' ~ img_path); |
|
90 |
tile.set("src", img_path); |
|
91 |
} |
|
92 |
})(); |
|
93 |
} |
|
94 |
me.last_tile = tile_index; |
|
moves some map infos in data...
|
95 |
me.last_type = data['tiles-type']; |
adds Map on MFD
|
96 |
} |
97 |
}, |
|
98 | ||
99 |
del : func() { |
|
100 |
me.update_timer.stop(); |
|
101 |
call(canvas.Window.del, [], me); |
|
102 |
}, |
|
103 |
}; |
|
104 | ||
105 |
var MapNavDisplay = { |
|
106 |
new : func (display) { |
|
107 |
var m = { parents: [MapNavDisplay] }; |
|
108 |
m.display = display; |
|
109 |
m.map = m.display.createGroup().createChild('map'); |
|
110 |
m.ctrl_ns = canvas.Map.Controller.get("Aircraft position"); |
|
111 |
m.ctrl_ns.SOURCES["map-dialog"] = { |
|
112 |
getPosition: func subvec(geo.aircraft_position().latlon(), 0, 2), |
|
113 |
getAltitude: func getprop('/position/altitude-ft'), |
|
114 |
getHeading: func { |
|
115 |
if (me.aircraft_heading) |
|
116 |
getprop('/orientation/heading-deg') |
|
117 |
else 0 |
|
118 |
}, |
|
119 |
aircraft_heading: 1, |
|
120 |
}; |
|
121 | ||
122 |
m.Styles = { |
|
123 |
get : func(type) return m.Styles[type], |
|
124 |
}; |
|
125 | ||
126 |
m.Options = { |
|
127 |
get : func(type) return m.Options[type], |
|
128 |
}; |
|
129 | ||
130 |
m.listeners = []; |
|
131 | ||
132 |
return m; |
|
133 |
}, |
|
134 | ||
135 |
showMap : func { |
|
136 |
me.setMap(); |
|
137 |
me.setStyles(); |
|
138 |
me.setOptions(); |
|
139 |
me.refresh(); |
|
140 |
}, |
|
141 | ||
142 |
setMap : func { |
|
143 |
var source = me.ctrl_ns.SOURCES["map-dialog"]; |
|
144 |
me.map.setController("Aircraft position", "map-dialog"); |
|
145 |
me.map.setRange(40); |
|
146 |
me.map.setTranslation( |
|
147 |
me.display.get("view[0]")/2, |
|
148 |
me.display.get("view[1]")/2 |
|
149 |
); |
|
150 |
}, |
|
151 | ||
152 |
setStyles : func { |
|
153 |
## set up a few keys supported by the DME.symbol file to customize appearance: {{{ |
|
154 |
me.Styles.DME = {}; |
|
155 |
me.Styles.DME.debug = 1; # HACK for benchmarking/debugging purposes |
|
156 |
me.Styles.DME.animation_test = 0; # for prototyping animated symbols |
|
157 | ||
158 |
me.Styles.DME.scale_factor = 0.4; # 40% (applied to whole group) |
|
159 |
me.Styles.DME.line_width = 3.0; |
|
160 |
me.Styles.DME.color_tuned = [0,1,0]; #rgb |
|
161 |
me.Styles.DME.color_default = [1,1,0]; #rgb |
|
162 | ||
163 |
me.Styles.APT = {}; |
|
164 |
me.Styles.APT.scale_factor = 0.4; # 40% (applied to whole group) |
|
165 |
me.Styles.APT.line_width = 3.0; |
|
166 |
me.Styles.APT.color_default = [0,0.6,0.85]; #rgb |
|
167 |
me.Styles.APT.label_font_color = me.Styles.APT.color_default; |
|
168 |
me.Styles.APT.label_font_size=16; |
|
169 | ||
170 |
me.Styles.TFC = {}; |
|
171 |
me.Styles.TFC.scale_factor = 0.4; # 40% (applied to whole group) |
|
172 | ||
173 |
me.Styles.WPT = {}; |
|
174 |
me.Styles.WPT.scale_factor = 0.5; # 50% (applied to whole group) |
|
175 | ||
176 |
me.Styles.RTE = {}; |
|
177 |
me.Styles.RTE.line_width = 2; |
|
178 | ||
179 |
me.Styles.FLT = {}; |
|
180 |
me.Styles.FLT.line_width = 3; |
|
181 | ||
182 |
me.Styles.FIX = {}; |
|
183 |
me.Styles.FIX.color = [1,0,0]; |
|
184 |
me.Styles.FIX.scale_factor = 0.4; # 40% |
|
185 | ||
186 |
me.Styles.VOR = {}; |
|
187 |
me.Styles.VOR.range_line_width = 2; |
|
188 |
me.Styles.VOR.radial_line_width = 1; |
|
189 |
me.Styles.VOR.scale_factor = 0.6; # 60% |
|
190 | ||
191 |
me.Styles.APS = {}; |
|
192 |
me.Styles.APS.scale_factor = 0.5; |
|
193 |
}, |
|
194 |
#}}} |
|
195 | ||
196 |
setOptions : func { |
|
197 |
me.Options.FLT = {}; |
|
198 |
}, |
|
199 | ||
200 |
make_update_wrapper : func(name) { |
|
201 |
if (!contains(me.Options, name)) me.Options[name] = {}; |
|
202 |
me.Options[name].update_wrapper = func(layer, fn) { |
|
203 |
fn(); |
|
commit initial
|
204 |
} |
adds Map on MFD
|
205 |
}, |
206 | ||
207 |
ToggleLayerVisible : func(name) { |
|
208 |
(var l = me.map.getLayer(name)).setVisible(l.getVisible()); |
|
209 |
}, |
|
210 | ||
211 |
SetLayerVisible : func(name,n=1) { |
|
212 |
me.map.getLayer(name).setVisible(n); |
|
213 |
}, |
|
214 | ||
215 |
refresh : func { |
|
216 |
var r = func(name,vis=1,zindex=nil) return caller(0)[0]; |
|
217 |
# TODO: we'll need some z-indexing here, right now it's just random |
|
218 |
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'), ] ) { |
|
219 |
if (1 and type.name != 'APS' and type.name != 'FLT') me.make_update_wrapper(type.name); |
|
220 |
me.map.addLayer(factory: canvas.SymbolLayer, type_arg: type.name, |
|
221 |
visible: type.vis, priority: type.zindex, |
|
222 |
style: me.Styles.get(type.name), |
|
223 |
options: me.Options.get(type.name) |
|
224 |
); |
|
225 |
(func { |
|
226 |
# Notify MapStructure about layer visibility changes: |
|
227 |
var name = type.name; |
|
228 |
props.globals.initNode("/sim/gui/dialogs/map-canvas/draw-"~name, type.vis, "BOOL"); |
|
229 |
append(me.listeners, |
|
230 |
setlistener("/sim/gui/dialogs/map-canvas/draw-"~name, |
|
231 |
func(n) me.SetLayerVisible(name,n.getValue()), 1, 2) |
|
232 |
); |
|
233 |
})(); |
|
commit initial
|
234 |
} |
adds Map on MFD
|
235 |
}, |
236 |
}; |