| 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 |
# ActiveFlightPlanNarrow Controller |
| 18 |
var ActiveFlightPlanNarrowController = |
| 19 |
{ |
| 20 |
new : func (page, svg) |
| 21 |
{ |
| 22 |
var obj = { |
| 23 |
parents : [ ActiveFlightPlanNarrowController, MFDPageController.new(page) ], |
| 24 |
_crsrToggle : 0, |
| 25 |
_recipient : nil, |
| 26 |
_page : page, |
| 27 |
_fp_current_wp : 0, |
| 28 |
_fp_active : 0, |
| 29 |
_current_flightplan : nil, |
| 30 |
_fprecipient : nil, |
| 31 |
transmitter : nil, |
| 32 |
_waypointSubmenuVisible : 0, |
| 33 |
}; |
| 34 |
|
| 35 |
obj._current_flightplan = obj.getNavData("Flightplan"); |
| 36 |
if (obj._current_flightplan != nil) { |
| 37 |
obj._fp_current_wp = obj._current_flightplan.current; |
| 38 |
obj._page.setFlightPlan(obj._current_flightplan, obj._fp_current_wp); |
| 39 |
} else { |
| 40 |
obj._page.setFlightPlan(nil, nil); |
| 41 |
} |
| 42 |
|
| 43 |
return obj; |
| 44 |
}, |
| 45 |
|
| 46 |
# Input Handling |
| 47 |
handleCRSR : func() { |
| 48 |
me._crsrToggle = (! me._crsrToggle); |
| 49 |
if (me._crsrToggle) { |
| 50 |
me._page.flightplanList.showCRSR(); |
| 51 |
} else { |
| 52 |
me._page.flightplanList.hideCRSR(); |
| 53 |
} |
| 54 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 55 |
}, |
| 56 |
handleFMSInner : func(value) { |
| 57 |
if (me._crsrToggle == 1) { |
| 58 |
# Enable the WaypointEntry window |
| 59 |
me._page.mfd._WaypointEntry.ondisplay(); |
| 60 |
|
| 61 |
# Also directly pass in the message. This is because the WaypointEntry page |
| 62 |
# is above this in the Emesary stack, and as it was not displayed, it won't |
| 63 |
# have picked up the message to display either an entry box or the submenu. |
| 64 |
me._page.mfd._WaypointEntry.getController().handleFMSInner(value); |
| 65 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 66 |
} else { |
| 67 |
# Pass to the page group controller to display and scroll through the page group menu |
| 68 |
return me._page.mfd.SurroundController.handleFMSInner(value); |
| 69 |
} |
| 70 |
}, |
| 71 |
handleFMSOuter : func(value) { |
| 72 |
if (me._crsrToggle == 1) { |
| 73 |
me._page.flightplanList.incrLarge(value); |
| 74 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 75 |
} else { |
| 76 |
# Pass to the page group controller to display and scroll through the page group menu |
| 77 |
return me._page.mfd.SurroundController.handleFMSOuter(value); |
| 78 |
} |
| 79 |
}, |
| 80 |
handleEnter : func(value) { |
| 81 |
if (me._crsrToggle == 1) { |
| 82 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 83 |
} else { |
| 84 |
return emesary.Transmitter.ReceiptStatus_NotProcessed; |
| 85 |
} |
| 86 |
}, |
| 87 |
|
| 88 |
handleRange : func(val) |
| 89 |
{ |
| 90 |
# Pass any range entries to the NavMapController |
| 91 |
me._page.Map.handleRange(val); |
| 92 |
}, |
| 93 |
|
| 94 |
# Handle the user entry of a waypoint. We now need to insert it into the |
| 95 |
# flightplan at the point after the selected waypoint, then update the |
| 96 |
# flightplan. This should cause a cascade of Emesary updates, which will |
| 97 |
# subsequently update this display (and any others). |
| 98 |
handleWaypointEntry : func(data) { |
| 99 |
assert(data.id != nil, "handleWaypointEntry called with invalid hash"); |
| 100 |
# Place this after the current index |
| 101 |
var params = { |
| 102 |
index : me._page.flightplanList.getCRSR() + 1, |
| 103 |
wp : data |
| 104 |
}; |
| 105 |
|
| 106 |
# Update the FMS with the new flightplan via Emesary |
| 107 |
var notification = notifications.PFDEventNotification.new( |
| 108 |
"MFD", |
| 109 |
me.getDeviceID(), |
| 110 |
notifications.PFDEventNotification.NavData, |
| 111 |
{Id: "InsertWaypoint", Value: params}); |
| 112 |
|
| 113 |
var response = me._transmitter.NotifyAll(notification); |
| 114 |
|
| 115 |
if (me._transmitter.IsFailed(response)) { |
| 116 |
print("ActiveFlightPlanNarrowController.handleWaypointEntry() : Failed to set FMS Data " ~ params); |
| 117 |
debug.dump(params); |
| 118 |
} else { |
| 119 |
# The flightplan has changed. For some reason this isn't triggering an |
| 120 |
# update from the flightplan delegate, so we'll just trigger an update |
| 121 |
# ourselves. |
| 122 |
var notification = notifications.PFDEventNotification.new( |
| 123 |
"MFD", |
| 124 |
1, |
| 125 |
notifications.PFDEventNotification.FMSData, |
| 126 |
{"FMSFlightPlanEdited" : 1}); |
| 127 |
|
| 128 |
var response = me._transmitter.NotifyAll(notification); |
| 129 |
|
| 130 |
if (me._transmitter.IsFailed(response)) { |
| 131 |
print("ActiveFlightPlanNarrowController.handleWaypointEntry() : Failed to set FMS Data " ~ params); |
| 132 |
debug.dump(params); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
# Critically, only this page should handle the waypoint entry. |
| 137 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 138 |
}, |
| 139 |
|
| 140 |
# Handle update to the FMS information. Note that there is no guarantee |
| 141 |
# that the entire set of FMS data will be available. |
| 142 |
handleFMSData : func(data) { |
| 143 |
var update_fp = 0; |
| 144 |
var reload_fp = 0; |
| 145 |
|
| 146 |
if (data["FMSLegID"] != nil) me._leg_id = data["FMSLegID"]; |
| 147 |
|
| 148 |
if ((data["FMSFlightPlanSequenced"] != nil) and (data["FMSFlightPlanSequenced"] != me._fp_current_wp)) { |
| 149 |
me._fp_current_wp = data["FMSFlightPlanSequenced"]; |
| 150 |
update_fp = 1; |
| 151 |
} |
| 152 |
|
| 153 |
if (data["FMSFlightPlanEdited"] != nil) { |
| 154 |
reload_fp = 1; |
| 155 |
} |
| 156 |
|
| 157 |
if ((data["FMSFlightPlanActive"] != nil) and (data["FMSFlightPlanActive"] != me._fp_active)) { |
| 158 |
me._fp_active = data["FMSFlightPlanActive"]; |
| 159 |
if (me._fp_active) { |
| 160 |
reload_fp = 1; |
| 161 |
} else { |
| 162 |
# No flightplan active, so we will display nothing. |
| 163 |
me._current_flightplan = nil; |
| 164 |
me._fp_current_wp = -1; |
| 165 |
update_fp = 1; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
if ((data["FMSFlightPlanCurrentWP"] != nil) and (data["FMSFlightPlanCurrentWP"] != me._fp_current_wp)) { |
| 170 |
me._fp_current_wp = data["FMSFlightPlanCurrentWP"]; |
| 171 |
update_fp = 1; |
| 172 |
} |
| 173 |
|
| 174 |
if (reload_fp) { |
| 175 |
# The flightplan has changed in some way, so reload it. |
| 176 |
me._current_flightplan = me.getNavData("Flightplan"); |
| 177 |
if (me._current_flightplan != nil) { |
| 178 |
me._fp_current_wp = me._current_flightplan.current; |
| 179 |
update_fp = 1; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
if (update_fp) { |
| 184 |
#me._current_flightplan = me.getNavData("Flightplan"); |
| 185 |
me._page.setFlightPlan(me._current_flightplan, me._fp_current_wp); |
| 186 |
} |
| 187 |
|
| 188 |
return emesary.Transmitter.ReceiptStatus_OK; |
| 189 |
}, |
| 190 |
|
| 191 |
# Reset controller if required when the page is displayed or hidden |
| 192 |
ondisplay : func() { |
| 193 |
me.RegisterWithEmesary(); |
| 194 |
me.FPRegisterWithEmesary(); |
| 195 |
}, |
| 196 |
offdisplay : func() { |
| 197 |
me.DeRegisterWithEmesary(); |
| 198 |
me.FPDeRegisterWithEmesary(); |
| 199 |
}, |
| 200 |
|
| 201 |
FPRegisterWithEmesary : func(transmitter = nil){ |
| 202 |
if (transmitter == nil) |
| 203 |
transmitter = emesary.GlobalTransmitter; |
| 204 |
|
| 205 |
if (me._fprecipient == nil){ |
| 206 |
me._fprecipient = emesary.Recipient.new("ActiveFlightPlanNarrowController_" ~ me._page.device.designation); |
| 207 |
var pfd_obj = me._page.device; |
| 208 |
var controller = me; |
| 209 |
me._fprecipient.Receive = func(notification) |
| 210 |
{ |
| 211 |
|
| 212 |
if (notification.Device_Id == pfd_obj.device_id and |
| 213 |
notification.NotificationType == notifications.PFDEventNotification.DefaultType and |
| 214 |
notification.Event_Id == notifications.PFDEventNotification.FMSData and |
| 215 |
notification.EventParameter != nil and |
| 216 |
notification.EventParameter["Id"] == "SetWaypointEntry") |
| 217 |
{ |
| 218 |
# Special case where THIS DEVICE has displayed the WaypointEntry page and |
| 219 |
# we are now receiving the entered waypoint. In this case we need to |
| 220 |
# determine where to enter it in the flightplan and update it. |
| 221 |
return controller.handleWaypointEntry(notification.EventParameter.Value); |
| 222 |
} |
| 223 |
|
| 224 |
if (notification.NotificationType == notifications.PFDEventNotification.DefaultType and |
| 225 |
notification.Event_Id == notifications.PFDEventNotification.FMSData and |
| 226 |
notification.EventParameter != nil) |
| 227 |
{ |
| 228 |
return controller.handleFMSData(notification.EventParameter); |
| 229 |
} |
| 230 |
|
| 231 |
return emesary.Transmitter.ReceiptStatus_NotProcessed; |
| 232 |
}; |
| 233 |
} |
| 234 |
transmitter.Register(me._fprecipient); |
| 235 |
me.transmitter = transmitter; |
| 236 |
}, |
| 237 |
FPDeRegisterWithEmesary : func(transmitter = nil){ |
| 238 |
# remove registration from transmitter; but keep the recipient once it is created. |
| 239 |
if (me.transmitter != nil) |
| 240 |
me.transmitter.DeRegister(me._fprecipient); |
| 241 |
me.transmitter = nil; |
| 242 |
}, |
| 243 |
|
| 244 |
getNavData : func(type, value=nil) { |
| 245 |
# Use Emesary to get a piece from the NavData system, using the provided |
| 246 |
# type and value; |
| 247 |
var notification = notifications.PFDEventNotification.new( |
| 248 |
"MFD", |
| 249 |
me._page.mfd.getDeviceID(), |
| 250 |
notifications.PFDEventNotification.NavData, |
| 251 |
{Id: type, Value: value}); |
| 252 |
|
| 253 |
var response = me._transmitter.NotifyAll(notification); |
| 254 |
|
| 255 |
if (! me._transmitter.IsFailed(response)) { |
| 256 |
return notification.EventParameter.Value; |
| 257 |
} else { |
| 258 |
return nil; |
| 259 |
} |
| 260 |
}, |
| 261 |
}; |