| 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 |
# NearestAirportsPFD |
| 18 |
var NearestAirportsPFD = |
| 19 |
{ |
| 20 |
new : func (mfd, myCanvas, device, svg) |
| 21 |
{ |
| 22 |
var obj = { |
| 23 |
parents : [ |
| 24 |
NearestAirportsPFD, |
| 25 |
MFDPage.new(mfd, myCanvas, device, svg, "NearestAirportsPFD", "NRST - NEAREST AIRPORTS") |
| 26 |
], |
| 27 |
}; |
| 28 |
|
| 29 |
obj.setController(fg1000.NearestAirportsPFDController.new(obj, svg)); |
| 30 |
|
| 31 |
# Dynamic elements. There are 2 different sets of dynamic elements: |
| 32 |
# |
| 33 |
# Nearest Airports - this is a scrolling list of up to 25 airports within 200nm, shown 3 at a time. |
| 34 |
# Airport Information - A page of more detailed information displaying details of the selected airport |
| 35 |
# |
| 36 |
# Selection is via the ENT key or the FMS knob |
| 37 |
|
| 38 |
obj.airportSelect = PFD.GroupElement.new( |
| 39 |
obj.pageName, |
| 40 |
svg, |
| 41 |
[ "ID", "BRG", "DST", "APP", "CommsType", "CommsFreq", "RWY"], |
| 42 |
3, |
| 43 |
"ID", |
| 44 |
0, |
| 45 |
"ScrollBar", |
| 46 |
"Scroll", |
| 47 |
140 |
| 48 |
); |
| 49 |
|
| 50 |
# Dynamic text elements for the Airport Info pane. |
| 51 |
obj.addTextElements(["InfoID", "InfoName", |
| 52 |
"InfoFacility", |
| 53 |
"InfoUsage", "InfoTime", "InfoAlt", |
| 54 |
"InfoRegion", |
| 55 |
"InfoLat", "InfoLon", "InfoBack"]); |
| 56 |
|
| 57 |
obj.setTextElement("InfoBack", "BACK"); |
| 58 |
obj._visible = 0; |
| 59 |
obj._NO_AIRPORTS = "NONE WITHIN 200NM"; |
| 60 |
obj.getElement("Group").setVisible(0); |
| 61 |
obj.getElement("Info").setVisible(0); |
| 62 |
|
| 63 |
return obj; |
| 64 |
}, |
| 65 |
|
| 66 |
visible : func() { |
| 67 |
return me._visible; |
| 68 |
}, |
| 69 |
toggleDisplay : func() { |
| 70 |
if (me.visible()) { |
| 71 |
me.offdisplay(); |
| 72 |
} else { |
| 73 |
me.ondisplay(); |
| 74 |
} |
| 75 |
}, |
| 76 |
offdisplay : func() { |
| 77 |
me.getElement("Group").setVisible(0); |
| 78 |
me.getElement("Info").setVisible(0); |
| 79 |
me._visible = 0; |
| 80 |
me.getController().offdisplay(); |
| 81 |
}, |
| 82 |
ondisplay : func() { |
| 83 |
me._visible = 1; |
| 84 |
me.getController().ondisplay(); |
| 85 |
me.displayNearest(); |
| 86 |
}, |
| 87 |
displayNearest : func() { |
| 88 |
me.getElement("Group").setVisible(1); |
| 89 |
me.getElement("Info").setVisible(0); |
| 90 |
}, |
| 91 |
displayInfo : func() { |
| 92 |
me.getElement("Group").setVisible(0); |
| 93 |
me.getElement("Info").setVisible(1); |
| 94 |
me.highlightTextElement("InfoBack"); |
| 95 |
}, |
| 96 |
updateAirports : func(apts) { |
| 97 |
|
| 98 |
var airportlist = []; |
| 99 |
for (var i = 0; i < size(apts); i = i + 1) { |
| 100 |
var apt = apts[i]; |
| 101 |
var crsAndDst = courseAndDistance(apt); |
| 102 |
|
| 103 |
# Display the course and distance in NM . |
| 104 |
var crs = sprintf("%i°", crsAndDst[0]); |
| 105 |
var dst = sprintf("%.1fnm", crsAndDst[1]); |
| 106 |
|
| 107 |
# We need to derive various non-trivial pieces of information: |
| 108 |
# - Maximum runway Length |
| 109 |
# - Approach type - VFR, ILS, NDB |
| 110 |
# - Approach, Tower or Unicom frequency |
| 111 |
|
| 112 |
var max_rwy = 0; |
| 113 |
var app_type = "VFR"; |
| 114 |
var freq_type = ""; |
| 115 |
var freq = ""; |
| 116 |
|
| 117 |
foreach(var rwy; keys(apt.runways)) { |
| 118 |
var rwy_info = apt.runways[rwy]; |
| 119 |
max_rwy = math.max(max_rwy, rwy_info.length); |
| 120 |
|
| 121 |
# This is the best we can do at present for approach types. |
| 122 |
if (rwy_info.ils_frequency_mhz != nil) app_type = "ILS"; |
| 123 |
} |
| 124 |
|
| 125 |
var apt_comms = apt.comms(); |
| 126 |
foreach (var c; apt_comms) { |
| 127 |
if (string.icmp(c.ident, "Approach") or |
| 128 |
string.icmp(c.ident, "APP") or |
| 129 |
string.icmp(c.ident, "APPROACH") ) { |
| 130 |
|
| 131 |
freq_type = "APPROACH"; |
| 132 |
freq = sprintf("%.3f", c.frequency); |
| 133 |
|
| 134 |
# Fine - we've got the best possible frequency, so break out |
| 135 |
# to stop any Tower frequencies from over-writing. |
| 136 |
break; |
| 137 |
} |
| 138 |
|
| 139 |
if (string.icmp(c.ident, "Tower") or |
| 140 |
string.icmp(c.ident, "TWR") or |
| 141 |
string.icmp(c.ident, "Tower") ) { |
| 142 |
freq_type = "TOWER"; |
| 143 |
freq = sprintf("%.3f", c.frequency); |
| 144 |
} |
| 145 |
|
| 146 |
# Only select a Unicom / Traffic if there's nothing found already |
| 147 |
if ((freq_type == "") and |
| 148 |
(string.icmp(c.ident, "Unicom") or |
| 149 |
string.icmp(c.ident, "UNICOM") )) { |
| 150 |
freq_type = "UNICOM"; |
| 151 |
freq = sprintf("%.3f", c.frequency); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
# Convert into something we can pass straight to the UIGroup. |
| 156 |
append(airportlist, { |
| 157 |
ID: apt.id, |
| 158 |
BRG: crs, |
| 159 |
DST: dst, |
| 160 |
APP: app_type, |
| 161 |
CommsType : freq_type, |
| 162 |
CommsFreq : freq, |
| 163 |
RWY : sprintf("%ift", 3.28 * max_rwy) |
| 164 |
}); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
if (size(airportlist) == 0) { |
| 169 |
# Blank value if in the middle of nowhere |
| 170 |
append(airportlist, { |
| 171 |
ID: me._NO_AIRPORTS, |
| 172 |
BRG: "", |
| 173 |
DST: "", |
| 174 |
APP: "", |
| 175 |
CommsType : "", |
| 176 |
CommsFreq : "", |
| 177 |
RWY : "" |
| 178 |
}); |
| 179 |
} |
| 180 |
|
| 181 |
me.airportSelect.setValues(airportlist); |
| 182 |
}, |
| 183 |
updateAirportData : func(apt) { |
| 184 |
|
| 185 |
if (apt == nil) return; |
| 186 |
|
| 187 |
me.setTextElement("InfoID", apt.id); |
| 188 |
me.setTextElement("InfoName", string.uc(apt.name)); |
| 189 |
me.setTextElement("InfoFacility", ""); |
| 190 |
|
| 191 |
if (string.imatch(apt.name, "private") or string.imatch(apt.name, "pvt")) { |
| 192 |
me.setTextElement("InfoUsage", "PRIVATE"); |
| 193 |
} else { |
| 194 |
me.setTextElement("InfoUsage", "PUBLIC"); |
| 195 |
} |
| 196 |
|
| 197 |
me.setTextElement("InfoTime", ""); |
| 198 |
me.setTextElement("InfoAlt", sprintf("%ift", 3.28 * apt.elevation)); |
| 199 |
me.setTextElementLat("InfoLat", apt.lat); |
| 200 |
me.setTextElementLon("InfoLon", apt.lon); |
| 201 |
|
| 202 |
}, |
| 203 |
getSelectedAirportID : func() { |
| 204 |
var id = me.airportSelect.getValue(); |
| 205 |
if (id == me._NO_AIRPORTS) id = nil; |
| 206 |
return id; |
| 207 |
}, |
| 208 |
}; |