| 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 Controller |
| 18 |
var NearestAirportsController = |
| 19 |
{ |
| 20 |
UIGROUP : { |
| 21 |
NONE : 0, # No group currently selected, |
| 22 |
APT : 1, |
| 23 |
RNWY : 2, |
| 24 |
FREQ : 3, |
| 25 |
APR : 4, |
| 26 |
}, |
| 27 |
|
| 28 |
new : func (page, svg) |
| 29 |
{ |
| 30 |
var obj = { parents : [ NearestAirportsController, MFDPageController.new(page) ] }; |
| 31 |
|
| 32 |
# Current active UI group. |
| 33 |
obj.page = page; |
| 34 |
obj._currentGroup = NearestAirportsController.UIGROUP.NONE; |
| 35 |
obj._crsrToggle = 0; |
| 36 |
|
| 37 |
return obj; |
| 38 |
}, |
| 39 |
|
| 40 |
selectAirports : func() { |
| 41 |
me.selectGroup(NearestAirportsController.UIGROUP.APT) |
| 42 |
}, |
| 43 |
selectRunways : func() { |
| 44 |
me.selectGroup(NearestAirportsController.UIGROUP.RNWY); |
| 45 |
}, |
| 46 |
selectFrequencies : func() { |
| 47 |
me.selectGroup(NearestAirportsController.UIGROUP.FREQ); |
| 48 |
}, |
| 49 |
selectApproaches : func() { |
| 50 |
me.selectGroup(NearestAirportsController.UIGROUP.APR); |
| 51 |
}, |
| 52 |
getSelectedGroup : func() { |
| 53 |
return me._currentGroup; |
| 54 |
}, |
| 55 |
selectGroup : func(grp) { |
| 56 |
me._currentGroup = grp; |
| 57 |
if (grp == NearestAirportsController.UIGROUP.APT) me.page.airportSelect.showCRSR() else me.page.airportSelect.hideCRSR(); |
| 58 |
if (grp == NearestAirportsController.UIGROUP.RNWY) me.page.runwaySelect.highlightElement() else me.page.runwaySelect.unhighlightElement(); |
| 59 |
if (grp == NearestAirportsController.UIGROUP.FREQ) me.page.freqSelect.showCRSR() else me.page.freqSelect.hideCRSR(); |
| 60 |
if (grp == NearestAirportsController.UIGROUP.APR) me.page.approachSelect.showCRSR() else me.page.approachSelect.hideCRSR(); |
| 61 |
me._crsrToggle = 1; |
| 62 |
}, |
| 63 |
|
| 64 |
# Input Handling |
| 65 |
handleCRSR : func() { |
| 66 |
me._crsrToggle = (! me._crsrToggle); |
| 67 |
if (me._crsrToggle) { |
| 68 |
me.page.topMenu(me.page.device, me.page, nil); |
| 69 |
me.page.selectAirports(); |
| 70 |
me.selectAirports(); |
| 71 |
} else { |
| 72 |
# Hide the cursor and reset any highlighting |
| 73 |
me.page.resetCRSR(); |
| 74 |
} |
| 75 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 76 |
}, |
| 77 |
handleFMSInner : func(value) { |
| 78 |
if (me._crsrToggle == 1) { |
| 79 |
# Scroll through whatever is the current list |
| 80 |
|
| 81 |
if (me._currentGroup == NearestAirportsController.UIGROUP.APT) { |
| 82 |
me.page.airportSelect.incrSmall(value); |
| 83 |
var apt_id = me.page.getSelectedAirportID(); |
| 84 |
|
| 85 |
var aptdata = me.getAirport(apt_id); |
| 86 |
me.page.updateAirportData(aptdata); |
| 87 |
} |
| 88 |
|
| 89 |
if (me._currentGroup == NearestAirportsController.UIGROUP.RNWY) { |
| 90 |
me.page.runwaySelect.incrSmall(value); |
| 91 |
# Need to manually update the runway information |
| 92 |
var apt_id = me.page.airportSelect.getValue(); |
| 93 |
var rwy = me.page.runwaySelect.getValue(); |
| 94 |
|
| 95 |
if ((rwy != nil) and (rwy != "")) { |
| 96 |
var apt_info = me.getAirport(apt_id); |
| 97 |
|
| 98 |
if (apt_info != nil) { |
| 99 |
# Names in the runway selection are of the form "NNN-MMM", e.g. 11R-29L |
| 100 |
# We just want the first of these. |
| 101 |
var idx = find("-", rwy); |
| 102 |
if (idx != -1) { |
| 103 |
rwy = substr(rwy, 0, idx); |
| 104 |
var rwy_info = apt_info.runways[rwy]; |
| 105 |
me.page.updateRunwayInfo(rwy_info); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
if (me._currentGroup == NearestAirportsController.UIGROUP.FREQ) me.page.freqSelect.incrSmall(value); |
| 112 |
if (me._currentGroup == NearestAirportsController.UIGROUP.APR) me.page.approachSelect.incrSmall(value); |
| 113 |
|
| 114 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 115 |
} else { |
| 116 |
return me.page.mfd.SurroundController.handleFMSInner(value); |
| 117 |
} |
| 118 |
}, |
| 119 |
handleFMSOuter : func(value) { |
| 120 |
if (me._crsrToggle == 1) { |
| 121 |
|
| 122 |
# The large knob only affects the Airport selection in cursor mode. |
| 123 |
# Question-mark over whether it does this when other groups |
| 124 |
# are selected. Assumption now is that it doesn't - otherwise it would |
| 125 |
# be too easy to nudge it while trying to scroll through runways/approaches etc. |
| 126 |
|
| 127 |
if (me._currentGroup == NearestAirportsController.UIGROUP.APT) { |
| 128 |
me.page.airportSelect.incrLarge(value); |
| 129 |
var apt_id = me.page.getSelectedAirportID(); |
| 130 |
var apt_info = me.getAirport(apt_id); |
| 131 |
me.page.updateAirportData(apt_info); |
| 132 |
} |
| 133 |
|
| 134 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 135 |
} else { |
| 136 |
return me.page.mfd.SurroundController.handleFMSOuter(value); |
| 137 |
} |
| 138 |
}, |
| 139 |
handleEnter : func(value) { |
| 140 |
if (me._crsrToggle == 1) { |
| 141 |
if (me._currentGroup == NearestAirportsController.UIGROUP.APT) { |
| 142 |
# If the airport group is selected, the ENT key selects the next airport |
| 143 |
me.page.airportSelect.incrLarge(value); |
| 144 |
var apt_id = me.page.getSelectedAirportID(); |
| 145 |
var apt_info = me.getAirport(apt_id); |
| 146 |
me.page.updateAirportData(apt_info); |
| 147 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 148 |
} |
| 149 |
|
| 150 |
if (me._currentGroup == NearestAirportsController.UIGROUP.RNWY) { |
| 151 |
# No effect if runways are selected |
| 152 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 153 |
} |
| 154 |
|
| 155 |
if (me._currentGroup == NearestAirportsController.UIGROUP.FREQ) { |
| 156 |
var freq = me.page.getSelectedFreq(); |
| 157 |
if (freq != nil) { |
| 158 |
me.page.mfd.SurroundController.setStandbyNavComFreq(freq); |
| 159 |
} |
| 160 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 161 |
} |
| 162 |
|
| 163 |
if (me._currentGroup == NearestAirportsController.UIGROUP.APR) { |
| 164 |
# TODO Select the current Approach |
| 165 |
var appr = me.page.getSelectedApproach(); |
| 166 |
if (appr != nil) print("NearestAirportController.handleEnter Approach selection " ~ appr); |
| 167 |
} |
| 168 |
} else { |
| 169 |
return emesary.Transmitter.ReceiptStatus_NotProcessed; |
| 170 |
} |
| 171 |
}, |
| 172 |
|
| 173 |
handleRange : func(val) |
| 174 |
{ |
| 175 |
# Pass any range entries to the NavMapController |
| 176 |
me.page.mfd.NavigationMap.getController().handleRange(val); |
| 177 |
}, |
| 178 |
|
| 179 |
|
| 180 |
# Reset controller if required when the page is displayed or hidden |
| 181 |
ondisplay : func() { |
| 182 |
me._currentGroup = NearestAirportsController.UIGROUP.NONE; |
| 183 |
me.RegisterWithEmesary(); |
| 184 |
me.getNearestAirports(); |
| 185 |
me.page.mfd.NavigationMap.getController().enableDTO(1); |
| 186 |
}, |
| 187 |
offdisplay : func() { |
| 188 |
me.page.mfd.NavigationMap.getController().enableDTO(0); |
| 189 |
me.DeRegisterWithEmesary(); |
| 190 |
}, |
| 191 |
|
| 192 |
getNearestAirports : func() { |
| 193 |
|
| 194 |
var notification = notifications.PFDEventNotification.new( |
| 195 |
"MFD", |
| 196 |
me.getDeviceID(), |
| 197 |
notifications.PFDEventNotification.NavData, |
| 198 |
{Id: "NearestAirports", Value: nil}); |
| 199 |
|
| 200 |
var response = me._transmitter.NotifyAll(notification); |
| 201 |
|
| 202 |
if (! me._transmitter.IsFailed(response)) { |
| 203 |
me.page.updateAirports(notification.EventParameter.Value); |
| 204 |
} else { |
| 205 |
return nil; |
| 206 |
} |
| 207 |
}, |
| 208 |
|
| 209 |
getAirport : func(id) { |
| 210 |
# Use Emesary to get the airport |
| 211 |
var notification = notifications.PFDEventNotification.new( |
| 212 |
"MFD", |
| 213 |
me.getDeviceID(), |
| 214 |
notifications.PFDEventNotification.NavData, |
| 215 |
{Id: "AirportByID", Value: id}); |
| 216 |
|
| 217 |
var response = me._transmitter.NotifyAll(notification); |
| 218 |
|
| 219 |
if (! me._transmitter.IsFailed(response)) { |
| 220 |
var apt_list = notification.EventParameter.Value; |
| 221 |
if ((apt_list != nil) and (size(apt_list) > 0)) { |
| 222 |
return apt_list[0]; |
| 223 |
} else { |
| 224 |
return nil; |
| 225 |
} |
| 226 |
} else { |
| 227 |
return nil; |
| 228 |
} |
| 229 |
}, |
| 230 |
}; |