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