| 1 |
# Copyright 2018 Stuart Buchanan |
| 2 |
# This file is part of FlightGear. |
| 3 |
# |
| 4 |
# FlightGear is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 2 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# FlightGear is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with FlightGear. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
# |
| 17 |
# Common Navigation map functions |
| 18 |
var NavMap = { |
| 19 |
|
| 20 |
# Declutter levels. |
| 21 |
DCLTR : [ "DCLTR", "DCLTR-1", "DCLTR-2", "DCLTR-3"], |
| 22 |
|
| 23 |
# Airways levels. |
| 24 |
AIRWAYS : [ "AIRWAYS", "AIRWY ON", "AIRWY LO", "AIRWY HI"], |
| 25 |
|
| 26 |
# Lazy-loading - only create the map element when the page becomes visible, |
| 27 |
# and delete afterwards. |
| 28 |
LAZY_LOADING : 1, |
| 29 |
|
| 30 |
new : func(page, element, center, clip="", zindex=0, vis_shift=0, static=0 ) |
| 31 |
{ |
| 32 |
var obj = { |
| 33 |
parents : [ NavMap ], |
| 34 |
_group : page.getGroup(), |
| 35 |
_svg : page.getSVG(), |
| 36 |
_page : page, |
| 37 |
_pageName : page.getPageName(), |
| 38 |
_element : element, |
| 39 |
_center : center, |
| 40 |
_clip : clip, |
| 41 |
_zindex : zindex, |
| 42 |
_vis_shift : vis_shift, |
| 43 |
_static : static, |
| 44 |
_current_zoom : 13, |
| 45 |
_declutter : 0, |
| 46 |
_airways : 0, |
| 47 |
_map : nil, |
| 48 |
}; |
| 49 |
|
| 50 |
element.setTranslation(center[0], center[1]); |
| 51 |
|
| 52 |
obj.Styles = fg1000.NavigationMapStyles.new(); |
| 53 |
obj.Options = fg1000.NavigationMapOptions.new(); |
| 54 |
|
| 55 |
obj._rangeDisplay = obj._svg.getElementById(obj._pageName ~ "RangeDisplay"); |
| 56 |
if (obj._rangeDisplay == nil) die("Unable to find element " ~ obj._pageName ~ "RangeDisplay"); |
| 57 |
|
| 58 |
obj._orientationDisplay = obj._svg.getElementById(obj._pageName ~ "OrientationDisplay"); |
| 59 |
if (obj._orientationDisplay == nil) die("Unable to find element " ~ obj._pageName ~ "OrientationDisplay"); |
| 60 |
|
| 61 |
if (NavMap.LAZY_LOADING == 0) { |
| 62 |
obj.createMapElement(); |
| 63 |
obj._map.setVisible(0); |
| 64 |
} |
| 65 |
|
| 66 |
return obj; |
| 67 |
}, |
| 68 |
|
| 69 |
# Create the map element itself. Depending on whether we are doing lazy loading |
| 70 |
# or not, this may be called by the constructor, or when the NavMap is made visible. |
| 71 |
createMapElement : func() { |
| 72 |
|
| 73 |
if (me._map != nil) return; |
| 74 |
|
| 75 |
me._map = me._element.createChild("map"); |
| 76 |
me._map.setScreenRange(689/2.0); |
| 77 |
|
| 78 |
# Initialize the controllers: |
| 79 |
if (me._static) { |
| 80 |
me._map.setController("Static position", "main"); |
| 81 |
} else { |
| 82 |
var ctrl_ns = canvas.Map.Controller.get("Aircraft position"); |
| 83 |
var source = ctrl_ns.SOURCES["current-pos"]; |
| 84 |
if (source == nil) { |
| 85 |
# TODO: amend |
| 86 |
var source = ctrl_ns.SOURCES["current-pos"] = { |
| 87 |
getPosition: func subvec(geo.aircraft_position().latlon(), 0, 2), |
| 88 |
getAltitude: func getprop('/position/altitude-ft'), |
| 89 |
getHeading: func { |
| 90 |
if (me.aircraft_heading) |
| 91 |
getprop('/orientation/heading-deg') |
| 92 |
else 0 |
| 93 |
}, |
| 94 |
aircraft_heading: 1, |
| 95 |
}; |
| 96 |
setlistener("/sim/gui/dialogs/map-canvas/aircraft-heading-up", func(n) { |
| 97 |
source.aircraft_heading = n.getBoolValue(); |
| 98 |
}, 1); |
| 99 |
} |
| 100 |
# Make it move with our aircraft: |
| 101 |
me._map.setController("Aircraft position", "current-pos"); # from aircraftpos.controller |
| 102 |
} |
| 103 |
|
| 104 |
if (me._clip != "") { |
| 105 |
me._map.set("clip-frame", canvas.Element.LOCAL); |
| 106 |
me._map.set("clip", me._clip); |
| 107 |
} |
| 108 |
|
| 109 |
if (me._zindex != 0) { |
| 110 |
me._element.setInt("z-index", me._zindex); |
| 111 |
} |
| 112 |
|
| 113 |
var r = func(name,on_static=1, vis=1,zindex=nil) return caller(0)[0]; |
| 114 |
# TODO: we'll need some z-indexing here, right now it's just random |
| 115 |
foreach (var layer_name; me._page.mfd.ConfigStore.getLayerNames()) { |
| 116 |
var layer = me._page.mfd.ConfigStore.getLayer(layer_name); |
| 117 |
|
| 118 |
if ((me._static == 0) or (layer.static == 1)) { |
| 119 |
# Not all layers are displayed for all map types. Specifically, |
| 120 |
# some layers are not displayed on static maps - e.g. DirectTo |
| 121 |
me._map.addLayer( |
| 122 |
factory: layer.factory, |
| 123 |
type_arg: layer_name, |
| 124 |
priority: layer.priority, |
| 125 |
style: me.Styles.getStyle(layer_name), |
| 126 |
options: me.Options.getOption(layer_name), |
| 127 |
visible: 0); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
me.setZoom(me._current_zoom); |
| 132 |
me.setOrientation(0); |
| 133 |
}, |
| 134 |
|
| 135 |
setController : func(type, controller ) { |
| 136 |
if (NavMap.LAZY_LOADING) me.createMapElement(); |
| 137 |
me._map.setController(type, controller); |
| 138 |
}, |
| 139 |
|
| 140 |
getController : func() { |
| 141 |
if (NavMap.LAZY_LOADING) me.createMapElement(); |
| 142 |
return me._map.getController(); |
| 143 |
}, |
| 144 |
|
| 145 |
toggleLayerVisible : func(name) { |
| 146 |
if (NavMap.LAZY_LOADING) me.createMapElement(); |
| 147 |
(var l = me._map.getLayer(name)).setVisible(l.getVisible()); |
| 148 |
}, |
| 149 |
|
| 150 |
setLayerVisible : func(name,n=1) { |
| 151 |
if (NavMap.LAZY_LOADING) me.createMapElement(); |
| 152 |
me._map.getLayer(name).setVisible(n); |
| 153 |
}, |
| 154 |
|
| 155 |
setOrientation : func(orientation) { |
| 156 |
# TODO - implment this |
| 157 |
me._orientationDisplay.setText(fg1000.ORIENTATIONS[orientation].label); |
| 158 |
}, |
| 159 |
|
| 160 |
setScreenRange : func(range) { |
| 161 |
if (NavMap.LAZY_LOADING) me.createMapElement(); |
| 162 |
me._map.setScreenRange(range); |
| 163 |
}, |
| 164 |
|
| 165 |
zoomIn : func() { |
| 166 |
me.setZoom(me._current_zoom -1); |
| 167 |
}, |
| 168 |
|
| 169 |
zoomOut : func() { |
| 170 |
me.setZoom(me._current_zoom +1); |
| 171 |
}, |
| 172 |
|
| 173 |
setZoom : func(zoom) { |
| 174 |
if (NavMap.LAZY_LOADING) me.createMapElement(); |
| 175 |
if ((zoom < 0) or (zoom > (size(fg1000.RANGES) - 1))) return; |
| 176 |
me._current_zoom = zoom; |
| 177 |
me._rangeDisplay.setText(fg1000.RANGES[zoom].label); |
| 178 |
me._map.setRange(fg1000.RANGES[zoom].range); |
| 179 |
me.updateVisibility(); |
| 180 |
}, |
| 181 |
|
| 182 |
updateVisibility : func() { |
| 183 |
if (NavMap.LAZY_LOADING) me.createMapElement(); |
| 184 |
# Determine which layers should be visible. |
| 185 |
foreach (var layer_name; me._page.mfd.ConfigStore.getLayerNames()) { |
| 186 |
var layer = me._page.mfd.ConfigStore.getLayer(layer_name); |
| 187 |
|
| 188 |
if (me._map.getLayer(layer_name) == nil) continue; |
| 189 |
|
| 190 |
# Layers are only displayed if: |
| 191 |
# 1) the user has enabled them. |
| 192 |
# 2) The current zoom level is _less_ than the maximum range for the layer |
| 193 |
# (i.e. as the range gets larger, we remove layers). Note that for |
| 194 |
# inset maps, the range that items are removed is lower. |
| 195 |
# 3) They haven't been removed due to the declutter level. |
| 196 |
var effective_zoom = math.clamp(me._current_zoom + me._vis_shift, 0, size(fg1000.RANGES) -1); |
| 197 |
var effective_range = fg1000.RANGES[effective_zoom].range; |
| 198 |
if (layer.enabled and |
| 199 |
(effective_range <= layer.range) and |
| 200 |
(me._declutter <= layer.declutter) ) |
| 201 |
{ |
| 202 |
me._map.getLayer(layer_name).setVisible(1); |
| 203 |
} else { |
| 204 |
me._map.getLayer(layer_name).setVisible(0); |
| 205 |
} |
| 206 |
} |
| 207 |
}, |
| 208 |
|
| 209 |
isEnabled : func(layer) { |
| 210 |
return me._page.mfd.ConfigStore.isLayerEnabled(layer); |
| 211 |
}, |
| 212 |
|
| 213 |
toggleLayer : func(layer) { |
| 214 |
me._page.mfd.ConfigStore.toggleLayerEnabled(layer); |
| 215 |
me.updateVisibility(); |
| 216 |
}, |
| 217 |
|
| 218 |
# Increment through the de-clutter levels, which impact what layers are |
| 219 |
# displayed. We also need to update the declutter menu item. |
| 220 |
incrDCLTR : func(device, menuItem) { |
| 221 |
me._declutter = math.mod(me._declutter +1, 4); |
| 222 |
me.updateVisibility(); |
| 223 |
return me.DCLTR[me._declutter]; |
| 224 |
}, |
| 225 |
|
| 226 |
getDCLTRTitle : func() { |
| 227 |
return me.DCLTR[me._declutter]; |
| 228 |
}, |
| 229 |
|
| 230 |
# Increment through the AIRWAYS levels. At present this doesn't do anything |
| 231 |
# except change the label. It should enable/disable different airways |
| 232 |
# information. |
| 233 |
incrAIRWAYS : func(device, menuItem) { |
| 234 |
me._airways = math.mod(me._airways +1, 4); |
| 235 |
me.updateVisibility(); |
| 236 |
return me.AIRWAYS[me._airways]; |
| 237 |
}, |
| 238 |
|
| 239 |
getAIRWAYSTitle : func() { |
| 240 |
return me.AIRWAYS[me._airways]; |
| 241 |
}, |
| 242 |
|
| 243 |
# Set the DTO line target |
| 244 |
setDTOLineTarget : func(lat, lon) { |
| 245 |
if (NavMap.LAZY_LOADING) me.createMapElement(); |
| 246 |
me._map.getLayer("DTO").controller.setTarget(lat,lon); |
| 247 |
}, |
| 248 |
enableDTO : func(enable) { |
| 249 |
me._page.mfd.ConfigStore.setLayerEnabled("DTO", enable); |
| 250 |
me.updateVisibility(); |
| 251 |
}, |
| 252 |
|
| 253 |
handleRange : func(val) |
| 254 |
{ |
| 255 |
if (val >0) { |
| 256 |
me.zoomOut(); |
| 257 |
} else { |
| 258 |
me.zoomIn(); |
| 259 |
} |
| 260 |
return emesary.Transmitter.ReceiptStatus_OK; |
| 261 |
}, |
| 262 |
|
| 263 |
getMap : func() { |
| 264 |
if (NavMap.LAZY_LOADING) me.createMapElement(); |
| 265 |
return me._map; |
| 266 |
}, |
| 267 |
show : func() { |
| 268 |
if (NavMap.LAZY_LOADING) me.createMapElement(); |
| 269 |
me._map.show(); |
| 270 |
}, |
| 271 |
hide : func() { |
| 272 |
if (me._map != nil) me._map.hide(); |
| 273 |
if (NavMap.LAZY_LOADING) me._map = nil; |
| 274 |
}, |
| 275 |
setVisible : func(visible) { |
| 276 |
if (visible) { |
| 277 |
if (NavMap.LAZY_LOADING) me.createMapElement(); |
| 278 |
me._map.setVisible(visible); |
| 279 |
} else { |
| 280 |
if (me._map != nil) me._map.setVisible(visible); |
| 281 |
if (NavMap.LAZY_LOADING) me._map = nil; |
| 282 |
} |
| 283 |
}, |
| 284 |
}; |