| 1 |
# Class things: |
| 2 |
var parents = [Map.Controller]; |
| 3 |
var __self__ = caller(0)[0]; |
| 4 |
Map.Controller.add("Static position", __self__); |
| 5 |
#Map.df_controller = __self__; |
| 6 |
|
| 7 |
## |
| 8 |
# A controller to handle static updates of position, for example an airport |
| 9 |
# diagram or non-moving map. |
| 10 |
# |
| 11 |
# Note that in contrast to the Aircraft Controllers, update_pos() or |
| 12 |
# update_layers() must be called explicitly to trigger an update of the map. |
| 13 |
## |
| 14 |
|
| 15 |
var new = func(map, source='main') { |
| 16 |
var m = { |
| 17 |
parents: [__self__], |
| 18 |
map: map, |
| 19 |
_pos: nil, _time: nil, _range: nil, |
| 20 |
_alt: 0, _hdg: 0, |
| 21 |
}; |
| 22 |
m._pos = geo.Coord.new(); |
| 23 |
m._pos.set_latlon(0.0,0.0); |
| 24 |
m.update_pos(); |
| 25 |
return m; |
| 26 |
}; |
| 27 |
|
| 28 |
var del = func(map) { |
| 29 |
if (map != me.map) die(); |
| 30 |
}; |
| 31 |
|
| 32 |
var setHeading = func(hdg) { me._hdg = hdg; }; |
| 33 |
var setAltitude = func(alt) { me._alt = alt; }; |
| 34 |
var getHeading = func() { return me._hdg; }; |
| 35 |
var getAltitude = func() { return me._alt; }; |
| 36 |
var setPosition = func(lat, lon) { |
| 37 |
me._pos.set_latlon(lat, lon); |
| 38 |
me.update_pos(); |
| 39 |
}; |
| 40 |
|
| 41 |
var applyOffset = func(x, y) { |
| 42 |
# Apply an offset in screen coordinates, e.g. from a mouse event |
| 43 |
var crs = 0.0; |
| 44 |
if (x != 0.0) { |
| 45 |
# Calculate course in degrees |
| 46 |
crs = 90.0 + 180.0 / math.pi * math.atan(y/x); |
| 47 |
if (x < 0.0 ) crs = crs + 180.0; |
| 48 |
} else { |
| 49 |
if (y < 0.0) crs = 0.0; |
| 50 |
if (y > 0.0) crs = 180.0 |
| 51 |
} |
| 52 |
|
| 53 |
# Screen resolution m/pixel is range/screen_range |
| 54 |
var screen_range = me.map.getScreenRange() or 200; |
| 55 |
var screen_resolution = me.map.getRange() * globals.NM2M / screen_range; |
| 56 |
me._pos.apply_course_distance(crs, math.sqrt(x*x + y*y) * screen_resolution); |
| 57 |
me.update_pos(); |
| 58 |
}; |
| 59 |
|
| 60 |
# Controller methods |
| 61 |
var update_pos = func { |
| 62 |
me.map.setPos(lat: me._pos.lat(), lon: me._pos.lon(), |
| 63 |
hdg: me._hdg,, |
| 64 |
alt: me._alt,); |
| 65 |
me.map.update(); |
| 66 |
}; |
| 67 |
|
| 68 |
var update_layers = func { |
| 69 |
me.map.update(); |
| 70 |
}; |
| 71 |
|
| 72 |
var get_position = func { |
| 73 |
return [ me._pos.lat(), me._pos.lon() ]; |
| 74 |
} |