Debian Codegraph @

Aircraft/Instruments-3d/FG1000/Nasal/MFDPages/NearestAirports/NearestAirports.nas

raw
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 # NearestAirports
18 var NearestAirports =
19 {
20 new : func (mfd, myCanvas, device, svg)
21 {
22 var obj = {
23 parents : [
24 NearestAirports,
25 MFDPage.new(mfd, myCanvas, device, svg, "NearestAirports", "NRST - NEAREST AIRPORTS")
26 ],
27 };
28
29 obj.setController(fg1000.NearestAirportsController.new(obj, svg));
30
31 # Dynamic elements. There are 4 different sets of dynamic elements:
32 #
33 # Nearest Airports - this is a scrolling list of up to 25 airports within 200nm, shown 5 at a time.
34 # Runways - just a single scroll element
35 # Frequencies - 3 displayed in a scrolling list
36 # Approaches - 3 displayed in a scrolling list
37 #
38 # Selection is via softkeys, the FMS knob, or via the page menu.
39
40 obj.airportSelect = PFD.GroupElement.new(
41 obj.pageName,
42 svg,
43 [ "Arrow", "ID", "CRS", "DST"],
44 5,
45 "Arrow",
46 1,
47 "AirportScrollBar",
48 "AirportScroll",
49 100
50 );
51
52 obj.runwaySelect = PFD.ScrollElement.new(obj.pageName, svg, "RunwayID", [36,18]); # Dummy values
53
54 obj.freqSelect = PFD.GroupElement.new(
55 obj.pageName,
56 svg,
57 ["FreqLabel", "Freq"],
58 3,
59 "Freq",
60 0,
61 "FreqScrollBar",
62 "FreqScroll",
63 75
64 );
65
66 obj.approachSelect = PFD.GroupElement.new(
67 obj.pageName,
68 svg,
69 ["Approach"],
70 3,
71 "Approach",
72 0,
73 "ApproachScrollBar",
74 "ApproachScroll",
75 75
76 );
77
78 # Other dynamic text elements
79 obj.addTextElements(["Name", "Alt", "RunwaySurface", "RunwayDimensions"]);
80
81 obj.topMenu(device, obj, nil);
82
83 return obj;
84 },
85
86 # Indicate which group is selected by colour of the softkeys
87 display_toggle : func(device, svg, mi, group) {
88 var bg_name = sprintf("SoftKey%d-bg",mi.menu_id);
89 if (me.getController().getSelectedGroup() == group) {
90 device.svg.getElementById(bg_name).setColorFill(0.5,0.5,0.5);
91 svg.setColor(0.0,0.0,0.0);
92 } else {
93 device.svg.getElementById(bg_name).setColorFill(0.0,0.0,0.0);
94 svg.setColor(1.0,1.0,1.0);
95 }
96 svg.setText(mi.title);
97 svg.setVisible(1); # display function
98 },
99
100 # Function to highlight the APT softkey - used when CRSR is pressed to indicate
101 # that we're editing the airports selection.
102 selectAirports : func() {
103 me.resetMenuColors();
104 var bg_name = sprintf("SoftKey%d-bg",4);
105 var tname = sprintf("SoftKey%d",4);
106 me.device.svg.getElementById(bg_name).setColorFill(0.5,0.5,0.5);
107 me.device.svg.getElementById(tname).setColor(0.0,0.0,0.0);
108 },
109
110 # Clear any cursor, highlights. Used when exiting from CRSR mode
111 resetCRSR : func() {
112 me.airportSelect.hideCRSR();
113 me.runwaySelect.unhighlightElement();
114 me.freqSelect.hideCRSR();
115 me.approachSelect.hideCRSR();
116 me.resetMenuColors();
117 },
118
119 offdisplay : func() {
120 # The Nearest... pages use the underlying navigation map.
121 me.mfd.NavigationMap.offdisplayPartial();
122
123 # Reset the menu colours. Shouldn't have to do this here, but
124 # there's not currently an obvious other location to do so.
125 me.resetMenuColors();
126
127 me.getController().offdisplay();
128 },
129 ondisplay : func() {
130 me.getController().ondisplay();
131
132 # The Nearest... pages use the underlying navigation map.
133 me.mfd.NavigationMap.ondisplayPartial();
134
135 me.mfd.setPageTitle(me.title);
136 },
137 updateAirports : func(apts) {
138
139 if (apts == nil) return;
140
141 var airportlist = [];
142 for (var i = 0; i < size(apts); i = i + 1) {
143 var apt = apts[i];
144 var crsAndDst = courseAndDistance(apt);
145
146 # Display the course and distance in NM .
147 var crs = sprintf("%i°", crsAndDst[0]);
148 var dst = sprintf("%.1fnm", crsAndDst[1]);
149
150 # Convert into something we can pass straight to the UIGroup.
151 append(airportlist, {
152 Arrow : apt.id,
153 ID: apt.id,
154 CRS: crs,
155 DST: dst,
156 });
157 }
158
159 me.airportSelect.setValues(airportlist);
160
161 if (size(airportlist) > 0) {
162 me.updateAirportData(apts[0]);
163 #me.airportSelect.showCRSR();
164 } else {
165 #me.airportSelect.hideCRSR();
166 me.setTextElement("Name", "NONE WITHIN 200NM");
167 me.setTextElement("Alt", "");
168 }
169 },
170 updateAirportData : func(apt) {
171
172 if (apt == nil) return;
173
174 me.setTextElement("Name", apt.name);
175 me.setTextElement("Alt", sprintf("%ift", M2FT * apt.elevation));
176
177 # Set up the runways list, but ignoring reciprocals so we don't get
178 # runways displayed twice.
179 var rwys = [];
180 var recips = {};
181 foreach(var rwy; sort(keys(apt.runways), string.icmp)) {
182 var rwy_info = apt.runways[rwy];
183 if (recips[rwy_info.id] == nil) {
184 var lbl = rwy_info.id ~ "-" ~ rwy_info.reciprocal.id;
185 append(rwys, lbl);
186 recips[rwy_info.reciprocal.id] = 1;
187 }
188 }
189
190 if (size(rwys) > 0) {
191 me.runwaySelect.setValues(rwys);
192 me.updateRunwayInfo(apt.runways[keys(apt.runways)[0]]);
193 } else {
194 me.runwaySelect.setValues([""]);
195 me.updateRunwayInfo(nil);
196 }
197
198 var freqarray = [];
199
200 # Add Comm Frequencies
201 var apt_comms = apt.comms();
202 if (size(apt_comms) > 0) {
203 # Airport has one or more frequencies assigned to it.
204 var freqs = {};
205 foreach (var c; apt_comms) {
206 freqs[c.ident] = sprintf("%.3f", c.frequency);
207 }
208
209 foreach (var c; sort(keys(freqs), string.icmp)) {
210 append(freqarray, {FreqLabel: c, Freq: freqs[c]});
211 }
212 }
213
214 # Add any ILS frequencies as well
215 foreach(var rwy; sort(keys(apt.runways), string.icmp)) {
216 var rwy_info = apt.runways[rwy];
217 if (rwy_info.ils_frequency_mhz != nil) {
218 var label = "ILS " ~ rwy_info.id;
219 var freq = sprintf("%.3f", rwy_info.ils_frequency_mhz);
220 append(freqarray, {FreqLabel: label, Freq: freq});
221 }
222 }
223
224 me.freqSelect.setValues(freqarray);
225
226 # Approaches
227 var approachList = apt.getApproachList();
228 me.approachSelect.setValues(approachList);
229
230 # Display the DTO line to the airport
231 me.mfd.NavigationMap.getController().setDTOLineTarget(apt.lat, apt.lon);
232 },
233 updateRunwayInfo : func(rwy_info) {
234 if (rwy_info != nil ) {
235 var dim = sprintf("%ift x %ift", 3.28 * rwy_info.length, 3.28 * rwy_info.width);
236 me.setTextElement("RunwayDimensions", dim);
237 me.setTextElement("RunwaySurface", get_SURFACE_TYPES(rwy_info.surface));
238 } else {
239 me.setTextElement("RunwayDimensions", "");
240 me.setTextElement("RunwaySurface", "");
241 }
242 },
243 getSelectedAirportID : func() {
244 return me.airportSelect.getValue();
245 },
246 getSelectedRunway : func() {
247 return me.runwaySelect.getValue();
248 },
249 getSelectedFreq : func() {
250 return me.freqSelect.getValue();
251 },
252 getSelectedApproach : func() {
253 return me.approachSelect.getValue();
254 },
255
256 topMenu : func(device, pg, menuitem) {
257 pg.clearMenu();
258 pg.resetMenuColors();
259 pg.addMenuItem(0, "ENGINE", pg, pg.mfd.EIS.engineMenu);
260 pg.addMenuItem(2, "MAP", pg, pg.mfd.NavigationMap.mapMenu);
261 pg.addMenuItem(4, "APT", pg,
262 func(dev, pg, mi) { pg.getController().selectAirports(); device.updateMenus(); }, # callback
263 func(svg, mi) { pg.display_toggle(device, svg, mi, NearestAirportsController.UIGROUP.APT); }
264 );
265
266 pg.addMenuItem(5, "RNWY", pg,
267 func(dev, pg, mi) { pg.getController().selectRunways(); device.updateMenus(); }, # callback
268 func(svg, mi) { pg.display_toggle(device, svg, mi, NearestAirportsController.UIGROUP.RNWY); }
269 );
270
271 pg.addMenuItem(6, "FREQ", pg,
272 func(dev, pg, mi) { pg.getController().selectFrequencies(); device.updateMenus(); }, # callback
273 func(svg, mi) { pg.display_toggle(device, svg, mi, NearestAirportsController.UIGROUP.FREQ); }
274 );
275
276 pg.addMenuItem(7, "APR", pg,
277 func(dev, pg, mi) { pg.getController().selectApproaches(); device.updateMenus(); }, # callback
278 func(svg, mi) { pg.display_toggle(device, svg, mi, NearestAirportsController.UIGROUP.APR); }
279 );
280
281 device.updateMenus();
282 },
283 };