Debian Codegraph @

Aircraft/Instruments-3d/FG1000/Nasal/MFDPages/DirectTo/DirectToController.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 # DirectTo Controller
18 var DirectToController =
19 {
20 new : func (page, svg)
21 {
22 var obj = { parents : [ DirectToController, MFDPageController.new(page)] };
23 obj.id = "";
24 obj.page = page;
25 obj.dto_displayed = 0;
26
27 obj._cursorElements = [
28 obj.page.IDEntry,
29 obj.page.VNVAltEntry,
30 obj.page.VNVOffsetEntry,
31 obj.page.CourseEntry,
32 obj.page.Activate
33 ];
34
35 obj._activateIndex = size(obj._cursorElements) - 1;
36
37 obj._selectedElement = 0;
38
39 # Whether the WaypointSubmenuGroup is enabled
40 obj._waypointSubmenuVisible = 0;
41
42 return obj;
43 },
44
45 setCursorElement : func(value) {
46
47 for (var i = 0; i < size(me._cursorElements); i = i+1) {
48 me._cursorElements[i].unhighlightElement();
49 }
50
51 if (value < 0) value = 0;
52 if (value > (size(me._cursorElements) -1)) value = size(me._cursorElements) -1;
53 me._selectedElement = value;
54 me._cursorElements[me._selectedElement].highlightElement();
55 },
56
57 nextCursorElement : func(value) {
58 var incr_or_decr = (value > 0) ? 1 : -1;
59 me.setCursorElement(me._selectedElement + incr_or_decr);
60 },
61
62 handleCRSR : func() {
63 if (! me.dto_displayed) return emesary.Transmitter.ReceiptStatus_NotProcessed;
64
65 # No effect, but shouldn't be passed to underlying page?
66 return emesary.Transmitter.ReceiptStatus_Finished;
67 },
68 updateWaypointSubmenu : func() {
69 var type = me.page.WaypointSubmenuSelect.getValue();
70
71 var items = [];
72
73 if (type == "FPL") {
74 # Get the contents of the flightplan and display the list of waypoints.
75 var fp = me.getNavData("Flightplan");
76
77 for (var i = 0; i < fp.getPlanSize(); i = i + 1) {
78 var wp = fp.getWP(i);
79 if (wp.wp_name != nil) append(items, wp.wp_name);
80 }
81 }
82
83 if (type == "NRST") {
84 # Get the nearest airports
85 var apts = me.getNavData("NearestAirports");
86
87 for (var i = 0; i < size(apts); i = i + 1) {
88 var apt = apts[i];
89 if (apt.id != nil) append(items, apt.id);
90 }
91 }
92
93 if (type == "RECENT") {
94 # Get the set of recent waypoints
95 items = me.getNavData("RecentWaypoints");
96 }
97
98 if (type == "USER") {
99 items = me.getNavData("UserWaypoints");
100 }
101
102 if (type == "AIRWAY") {
103 var fp = me.getNavData("AirwayWaypoints");
104 if (fp != nil) {
105 for (var i = 0; i < fp.getPlanSize(); i = i + 1) {
106 var wp = fp.getWP(i);
107 if (wp.wp_name != nil) append(items, wp.wp_name);
108 }
109 }
110 }
111
112 if ((items != nil) and (size(items) > 0)) {
113 # At this point we have a vector of waypoint names. We need to convert
114 # this into a vector of { "WaypointSubmenuScroll" : [name] } hashes for consumption by the
115 # list of waypoints
116 var groupitems = [];
117 foreach (var item; items) {
118 append(groupitems, { "WaypointSubmenuScroll" : item } );
119 }
120
121 # Now display them!
122 me.page.WaypointSubmenuScroll.setValues(groupitems);
123 } else {
124 # Nothing to display
125 me.page.WaypointSubmenuScroll.setValues([]);
126 }
127 },
128
129 getNavData : func(type, value=nil) {
130 # Use Emesary to get a piece from the NavData system, using the provided
131 # type and value;
132 var notification = notifications.PFDEventNotification.new(
133 "MFD",
134 me.getDeviceID(),
135 notifications.PFDEventNotification.NavData,
136 {Id: type, Value: value});
137
138 var response = me._transmitter.NotifyAll(notification);
139
140 if (! me._transmitter.IsFailed(response)) {
141 return notification.EventParameter.Value;
142 } else {
143 return nil;
144 }
145 },
146
147 setNavData : func(type, value=nil) {
148 # Use Emesary to set a piece of data in the NavData system, using the provided
149 # type and value;
150 var notification = notifications.PFDEventNotification.new(
151 "MFD",
152 me.getDeviceID(),
153 notifications.PFDEventNotification.NavData,
154 {Id: type, Value: value});
155
156 var response = me._transmitter.NotifyAll(notification);
157
158 if (me._transmitter.IsFailed(response)) {
159 print("DirectToController.setNavData() : Failed to set Nav Data " ~ value);
160 debug.dump(value);
161 }
162 },
163
164 handleDTO : func(value) {
165 if (me.dto_displayed == 0) {
166 # Display the DTO page.
167 me.page.ondisplay();
168 me.dto_displayed = 1;
169 } else {
170 # Hide the DTO page.
171 me.page.offdisplay();
172 me.dto_displayed = 0;
173 }
174
175 return emesary.Transmitter.ReceiptStatus_Finished;
176 },
177
178 handleRange : func(val)
179 {
180 if (! me.dto_displayed) return emesary.Transmitter.ReceiptStatus_NotProcessed;
181
182 if (me.page.DirectToChart != nil) {
183 return me.page.DirectToChart.handleRange(val);
184 } else {
185 return emesary.Transmitter.ReceiptStatus_NotProcessed;
186 }
187 },
188
189 handleFMSInner : func(value) {
190 if (! me.dto_displayed) return emesary.Transmitter.ReceiptStatus_NotProcessed;
191
192 if (me._waypointSubmenuVisible) {
193 # We're in the Waypoint Submenu, in which case the inner FMS knob
194 # selects between the different waypoint types.
195 me.page.WaypointSubmenuSelect.highlightElement();
196 me.page.WaypointSubmenuSelect.incrSmall(value);
197 # Now update the Scroll group with the new type of waypoints
198 me.updateWaypointSubmenu();
199 } else if ((me._selectedElement == 0) and (! me.page.IDEntry.isInEdit()) and (value == -1)) {
200 # The WaypointSubmenuGroup group is displayed if the small FMS knob is rotated
201 # anti-clockwise as an initial rotation where the ID Entry is not being editted.
202 me._cursorElements[0].unhighlightElement();
203
204 me.page.WaypointSubmenuGroup.setVisible(1);
205 me.page.WaypointSubmenuSelect.highlightElement();
206 me._waypointSubmenuVisible = 1;
207 me.updateWaypointSubmenu();
208 } else {
209 # We've already got something selected, and we're not in the
210 # WaypointSubmenuGroup, so increment it.
211 me._cursorElements[me._selectedElement].incrSmall(value);
212 }
213
214 return emesary.Transmitter.ReceiptStatus_Finished;
215 },
216
217 handleFMSOuter : func(value) {
218 if (! me.dto_displayed) return emesary.Transmitter.ReceiptStatus_NotProcessed;
219
220 if (me._waypointSubmenuVisible) {
221 # We're in the Waypoint Submenu, in which case the outer FMS knob
222 # selects between the different waypoints in the Waypoint Submenu.
223 me.page.WaypointSubmenuSelect.unhighlightElement();
224 me.page.WaypointSubmenuScroll.showCRSR();
225 me.page.WaypointSubmenuScroll.incrLarge(value);
226 } else if (me._cursorElements[me._selectedElement].isInEdit()) {
227 # If we're editing an element, then get on with it!
228 me._cursorElements[me._selectedElement].incrLarge(value);
229 } else {
230 me.nextCursorElement(value);
231 }
232
233 return emesary.Transmitter.ReceiptStatus_Finished;
234 },
235
236 handleEnter : func(value) {
237 if (! me.dto_displayed) return emesary.Transmitter.ReceiptStatus_NotProcessed;
238
239 if (me._waypointSubmenuVisible) {
240 # If we're in the Waypoint Submenu, then take whatever is highlighted
241 # in the scroll list, load it and hide the Waypoint submenu
242 var id = me.page.WaypointSubmenuScroll.getValue();
243 if (id != nil) me.loadDestination(id);
244 me.page.WaypointSubmenuGroup.setVisible(0);
245 me._waypointSubmenuVisible = 0;
246
247 # Select the activate ACTIVATE item.
248 me.setCursorElement(me._activateIndex);
249 } else {
250 if (me._selectedElement == me._activateIndex) {
251 # If we're on the Activate button, then set up the DirectTo and hide the
252 # page. We're finished
253 var params = {};
254 params.id = me.page.IDEntry.getValue();
255 params.alt_ft = me.page.VNVAltEntry.getValue();
256 params.offset_nm = me.page.VNVOffsetEntry.getValue();
257 me.setNavData("SetDirectTo", params);
258
259 me.page.offdisplay();
260 me.dto_displayed = 0;
261
262 return emesary.Transmitter.ReceiptStatus_Finished;
263 }
264
265 # If we're editing an element, complete the data entry.
266 if (me._cursorElements[me._selectedElement].isInEdit()) {
267 me._cursorElements[me._selectedElement].enterElement();
268 }
269
270 if (me._cursorElements[me._selectedElement] == me.page.IDEntry) {
271 # We've finished entering an ID, so load it.
272 me.loadDestination(me.page.IDEntry.getValue());
273 }
274
275 # Determine where to highlight next. In most cases, we go straight to ACTIVATE.
276 # The exception is the VNV Alt field which goes to the VNV Distance field;
277 if (me._cursorElements[me._selectedElement] == me.page.VNVAltEntry) {
278 # VNV DIS entry is the next element
279 me.nextCursorElement(1);
280 } else {
281 # ACTIVATE is the last element of the group
282 me.setCursorElement(me._activateIndex);
283 }
284 }
285
286 return emesary.Transmitter.ReceiptStatus_Finished;
287 },
288
289 handleClear : func(value) {
290 if (! me.dto_displayed) return emesary.Transmitter.ReceiptStatus_NotProcessed;
291
292 if (me._waypointSubmenuVisible) {
293 # If we're in the Waypoint Submenu, then this clears it.
294 me.page.WaypointSubmenuGroup.setVisible(0);
295 me._waypointSubmenuVisible = 0;
296 } else if (me._cursorElements[me._selectedElement].isInEdit()) {
297 me._cursorElements[me._selectedElement].clearElement();
298 } else {
299 # Cancel the entire Direct To page.
300 me.page.offdisplay();
301 me.dto_displayed = 0;
302 }
303 return emesary.Transmitter.ReceiptStatus_Finished;
304 },
305
306 handleStringInput : func(value) {
307 if (! me.dto_displayed) return emesary.Transmitter.ReceiptStatus_NotProcessed;
308 if (me._waypointSubmenuVisible) return emesary.Transmitter.ReceiptStatus_NotProcessed;
309
310 if (me._cursorElements[me._selectedElement].isEditable()) {
311 # Cancel any editing
312 me._cursorElements[me._selectedElement].clearElement();
313
314 # Set the new value
315 me._cursorElements[me._selectedElement].setValue(value);
316
317 # Also cause any enter-handling
318 me.handleEnter(1);
319 }
320
321 return emesary.Transmitter.ReceiptStatus_Finished;
322 },
323 handleKeyInput : func (value) {
324 if (! me.dto_displayed) return emesary.Transmitter.ReceiptStatus_NotProcessed;
325 if (me._waypointSubmenuVisible) return emesary.Transmitter.ReceiptStatus_NotProcessed;
326
327 if (me._cursorElements[me._selectedElement].isEditable()) {
328 me._cursorElements[me._selectedElement].keyPress(value);
329 }
330
331 return emesary.Transmitter.ReceiptStatus_Finished;
332 },
333
334
335 # Reset controller if required when the page is displayed or hidden
336 # Note that we explicitly do NOT RegisterWithEmesary/DeRegisterWithEmesary!
337 # This page should RegisterWithEmesary at start of day instead.
338 ondisplay : func() {
339 # Find the current DTO target, which may have been set by the page the
340 # use was on.
341 var id = me.getNavData("CurrentDTO");
342 me.page.IDEntry.setValue(id);
343 me.loadDestination(id);
344 me.setCursorElement(0);
345 },
346
347 offdisplay : func() {
348 },
349
350 loadDestination : func(id) {
351 if ((id == nil) or (id == "")) {
352 me.page.displayDestination(nil);
353 } else {
354 # Use Emesary to get the destination
355 var notification = notifications.PFDEventNotification.new(
356 "MFD",
357 me.getDeviceID(),
358 notifications.PFDEventNotification.NavData,
359 {Id: "NavDataByID", Value: id});
360
361 var response = me._transmitter.NotifyAll(notification);
362 var retval = notification.EventParameter.Value;
363
364 if ((! me._transmitter.IsFailed(response)) and (size(retval) > 0)) {
365 var destination = retval[0];
366 # set the course and distance to the destination if required
367
368 # Some elements don't have names
369 var name = destination.id;
370 if (defined("destination.name")) name = destination.name;
371
372 var point = { lat: destination.lat, lon: destination.lon };
373 var (course, dist) = courseAndDistance(point);
374
375 var d = {
376 id: destination.id,
377 name: name,
378 lat: destination.lat,
379 lon: destination.lon,
380 course : course,
381 range_nm : dist,
382 };
383
384 me.page.displayDestination(d);
385 }
386 }
387 },
388 };