separates maps code
|
1 |
var MapTiles = { |
2 |
# displays maps background from web tiles |
|
3 |
# code from http://wiki.flightgear.org/Canvas_Snippets#A_simple_tile_map |
|
4 |
new : func (device, group) { |
|
5 |
var m = { parents: [MapTiles] }; |
|
6 |
m.device = device; |
|
7 |
m.display = m.device.display.display; |
|
8 |
m.tile_size = 256; |
|
9 |
m.maps_base = getprop("/sim/fg-home") ~ '/cache/maps'; |
|
add template option for tile...
|
10 |
m.makeUrl = string.compileTemplate(data['tiles-template']); |
separates maps code
|
11 |
m.makePath = string.compileTemplate(m.maps_base ~ '/{server}/{type}/{z}/{x}/{y}.png'); |
12 |
m.num_tiles = [ |
|
13 |
math.ceil( m.device.data.mapsize[0] / m.tile_size ) + 1, |
|
14 |
math.ceil( m.device.data.mapsize[1] / m.tile_size ) + 1 |
|
15 |
]; |
|
16 |
m.center_tile_offset = [ |
|
17 |
(m.num_tiles[0] - 1) / 2, |
|
18 |
(m.num_tiles[1] - 1) / 2 |
|
19 |
]; |
|
20 |
m.visibility = m.device.role == 'MFD'; |
|
21 |
m.group = group.createChild('group', 'tiles') |
|
22 |
.setTranslation( |
|
23 |
m.device.role == 'MFD' ? (m.device.data.mapview[0] - m.device.data.mapsize[0] + m.device.data.mapclip.left)/2 : -520, |
|
24 |
m.device.role == 'MFD' ? -250 : -45) |
|
25 |
.setVisible(m.visibility); |
|
26 |
m.tiles = setsize([], m.num_tiles[0]); |
|
27 |
m.last_tile = [-1,-1]; |
|
28 |
m.last_type = data['tiles-type']; |
|
29 |
m.initialize_grid(); |
|
30 |
return m; |
|
31 |
}, |
|
32 | ||
33 |
setVisible : func (v) { |
|
34 |
if (v != me.visibility) { |
|
35 |
me.visibility = v; |
|
36 |
me.group.setVisible(v); |
|
37 |
} |
|
38 |
}, |
|
39 | ||
40 |
# initialize the map by setting up a grid of raster images |
|
41 |
initialize_grid : func { |
|
42 |
for(var x = 0; x < me.num_tiles[0]; x += 1) { |
|
43 |
me.tiles[x] = setsize([], me.num_tiles[1]); |
|
44 |
for(var y = 0; y < me.num_tiles[1]; y += 1) |
|
45 |
me.tiles[x][y] = me.group.createChild('image', 'tile ' ~ x ~ ',' ~ y); |
|
46 |
} |
|
47 |
}, |
|
48 | ||
49 |
# this is the callback that will be regularly called by the timer to update the map |
|
50 |
update : func { |
|
51 |
if (! me.visibility) |
|
52 |
return; |
|
53 | ||
54 |
var n = math.pow(2, me.device.data.zoom); |
|
55 |
var offset = [ |
|
56 |
n * ((data.lon + 180) / 360) - me.center_tile_offset[0], |
|
57 |
(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] |
|
58 |
]; |
|
59 |
var tile_index = [int(offset[0]), int(offset[1])]; |
|
60 | ||
61 |
var ox = tile_index[0] - offset[0]; |
|
62 |
var oy = tile_index[1] - offset[1]; |
|
63 | ||
64 |
for (var x = 0; x < me.num_tiles[0]; x += 1) |
|
65 |
for(var y = 0; y < me.num_tiles[1]; y += 1) |
|
66 |
me.tiles[x][y] |
|
67 |
.setTranslation( |
|
68 |
int((ox + x) * me.tile_size + 0.5), |
|
69 |
int((oy + y) * me.tile_size + 0.5)); |
|
70 | ||
71 |
if (tile_index[0] != me.last_tile[0] |
|
72 |
or tile_index[1] != me.last_tile[1] |
|
73 |
or data['tiles-type'] != me.last_type) { |
|
74 |
for(var x = 0; x < me.num_tiles[0]; x += 1) |
|
75 |
for(var y = 0; y < me.num_tiles[1]; y += 1) { |
|
76 |
var pos = { |
|
77 |
z: me.device.data.zoom, |
|
78 |
x: int(offset[0] + x), |
|
79 |
y: int(offset[1] + y), |
|
80 |
type: data['tiles-type'], |
|
81 |
server : data['tiles-server'], |
|
82 |
apikey: data['tiles-apikey'], |
|
83 |
}; |
|
84 | ||
85 |
(func { |
|
86 |
var img_path = me.makePath(pos); |
|
87 |
printlog('debug', 'img_path: ', img_path); |
|
88 |
var tile = me.tiles[x][y]; |
|
89 | ||
90 |
if (io.stat(img_path) == nil) { # image not found, save in $FG_HOME |
|
91 |
var img_url = me.makeUrl(pos); |
|
92 |
printlog('debug', 'requesting ' ~ img_url); |
|
93 |
http.save(img_url, img_path) |
|
94 |
.done(func {printlog('info', 'received image ' ~ img_path); tile.set("src", img_path);}) |
|
95 |
.fail(func (r) printlog('warn', 'Failed to get image ' ~ img_path ~ ' ' ~ r.status ~ ': ' ~ r.reason)); |
|
96 |
} |
|
97 |
else { # cached image found, reusing |
|
98 |
printlog('debug', 'loading ' ~ img_path); |
|
99 |
tile.set("src", img_path); |
|
100 |
} |
|
101 |
})(); |
|
102 |
} |
|
103 |
me.last_tile = tile_index; |
|
104 |
me.last_type = data['tiles-type']; |
|
105 |
} |
|
106 |
}, |
|
107 |
}; |