| 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 |
# Checklist Controller |
| 18 |
var ChecklistController = |
| 19 |
{ |
| 20 |
UIGROUP : { |
| 21 |
GROUP : 0, |
| 22 |
CHECKLIST: 1, |
| 23 |
ITEMS : 2, |
| 24 |
NEXT : 3, |
| 25 |
}, |
| 26 |
|
| 27 |
new : func (page, svg) |
| 28 |
{ |
| 29 |
var obj = { |
| 30 |
parents : [ ChecklistController, MFDPageController.new(page) ], |
| 31 |
_crsrToggle : 0, |
| 32 |
_recipient : nil, |
| 33 |
_page : page, |
| 34 |
_currentGroup : -1, |
| 35 |
_group_selected : nil, |
| 36 |
_list_selected : nil, |
| 37 |
_checklists : nil, |
| 38 |
}; |
| 39 |
|
| 40 |
obj.selectGroup(-1); |
| 41 |
return obj; |
| 42 |
}, |
| 43 |
|
| 44 |
selectGroup : func() { |
| 45 |
me.selectGroup(ChecklistController.UIGROUP.GROUP) |
| 46 |
}, |
| 47 |
selectChecklist : func() { |
| 48 |
me.selectGroup(ChecklistController.UIGROUP.CHECKLIST); |
| 49 |
}, |
| 50 |
selectItems : func() { |
| 51 |
me.selectGroup(ChecklistController.UIGROUP.ITEMS); |
| 52 |
}, |
| 53 |
selectNext : func() { |
| 54 |
me.selectGroup(ChecklistController.UIGROUP.NEXT); |
| 55 |
}, |
| 56 |
|
| 57 |
|
| 58 |
getSelectedGroup : func() { |
| 59 |
return me._currentGroup; |
| 60 |
}, |
| 61 |
|
| 62 |
selectGroup : func(grp) { |
| 63 |
me._page.hideGroupSelect(); |
| 64 |
me._page.hideChecklistSelect(); |
| 65 |
|
| 66 |
me._currentGroup = grp; |
| 67 |
if (grp == ChecklistController.UIGROUP.GROUP) { |
| 68 |
me._page.highlightTextElement("GroupName"); |
| 69 |
} else { |
| 70 |
me._page.unhighlightTextElement("GroupName"); |
| 71 |
} |
| 72 |
|
| 73 |
if (grp == ChecklistController.UIGROUP.CHECKLIST) { |
| 74 |
me._page.highlightTextElement("Name"); |
| 75 |
} else { |
| 76 |
me._page.unhighlightTextElement("Name"); |
| 77 |
} |
| 78 |
|
| 79 |
if (grp == ChecklistController.UIGROUP.ITEMS) { |
| 80 |
me._page.checklistDisplay.showCRSR(); |
| 81 |
} else { |
| 82 |
me._page.checklistDisplay.hideCRSR(); |
| 83 |
} |
| 84 |
|
| 85 |
if (grp == ChecklistController.UIGROUP.NEXT) { |
| 86 |
me._page.highlightTextElement("Next"); |
| 87 |
} else { |
| 88 |
me._page.unhighlightTextElement("Next"); |
| 89 |
} |
| 90 |
}, |
| 91 |
|
| 92 |
selectEmergencyChecklist : func() { |
| 93 |
if (me._checklists == nil) return; |
| 94 |
# Select the EMERGENCY checklist group, if available. |
| 95 |
var emergency_labels = ["EMERGENCY", "Emergency", "emergency"]; |
| 96 |
var group = nil; |
| 97 |
foreach (var l; emergency_labels) { |
| 98 |
if (me._checklists[l] != nil) { |
| 99 |
group = l; |
| 100 |
break; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if (group != nil) { |
| 105 |
me._group_selected = group; |
| 106 |
me._list_selected = keys(me._checklists[me._group_selected])[0]; |
| 107 |
me._page.hideGroupSelect(); |
| 108 |
me._page.displayChecklist(me._group_selected, me._list_selected, me._checklists); |
| 109 |
me.selectChecklist(); |
| 110 |
} |
| 111 |
}, |
| 112 |
|
| 113 |
# Input Handling |
| 114 |
handleCRSR : func() { |
| 115 |
# If there are no checklists then we don't allow the CRSR to be enabled as |
| 116 |
# there's nothing to do. |
| 117 |
if (me._checklists == nil) return emesary.Transmitter.ReceiptStatus_Finished; |
| 118 |
me._crsrToggle = (! me._crsrToggle); |
| 119 |
if (me._crsrToggle) { |
| 120 |
me.selectGroup(0); |
| 121 |
} else { |
| 122 |
me.selectGroup(-1); |
| 123 |
} |
| 124 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 125 |
}, |
| 126 |
handleFMSInner : func(value) { |
| 127 |
if (me._crsrToggle == 1) { |
| 128 |
# Either display the select group or scroll through whatever is the current list |
| 129 |
|
| 130 |
if (me._currentGroup == ChecklistController.UIGROUP.GROUP) { |
| 131 |
if (me._page.isGroupSelectVisible()) { |
| 132 |
me._page.checklistGroupSelect.incrSmall(value); |
| 133 |
} else { |
| 134 |
me._page.displayGroupSelect(); |
| 135 |
me._page.unhighlightTextElement("GroupName"); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if (me._currentGroup == ChecklistController.UIGROUP.CHECKLIST) { |
| 140 |
if (me._page.isChecklistSelectVisible()) { |
| 141 |
me._page.checklistSelect.incrSmall(value); |
| 142 |
} else { |
| 143 |
me._page.displayChecklistSelect(); |
| 144 |
me._page.unhighlightTextElement("Name"); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
if (me._currentGroup == ChecklistController.UIGROUP.ITEMS) { |
| 149 |
me._page.checklistDisplay.incrSmall(value); |
| 150 |
} |
| 151 |
|
| 152 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 153 |
} else { |
| 154 |
# Pass to the page group controller to display and scroll through the page group menu |
| 155 |
return me._page.mfd.SurroundController.handleFMSInner(value); |
| 156 |
} |
| 157 |
}, |
| 158 |
handleFMSOuter : func(value) { |
| 159 |
if (me._crsrToggle == 1) { |
| 160 |
# Manual explicitly documents that _either_ FMS knob may be used to scroll through the checklist. |
| 161 |
# However, that means that there is no way to navigate from the checklist itself other |
| 162 |
# than to disable and then re-enable the CRSR. Odd. |
| 163 |
if (me._currentGroup == ChecklistController.UIGROUP.ITEMS) return me.handleFMSInner(value); |
| 164 |
|
| 165 |
var incr_or_decr = (value > 0) ? 1 : -1; |
| 166 |
var idx = me._currentGroup + incr_or_decr; |
| 167 |
if (idx < 0) idx = 0; |
| 168 |
if (idx > (size(ChecklistController.UIGROUP) -1)) idx = size(ChecklistController.UIGROUP) -1; |
| 169 |
me.selectGroup(idx); |
| 170 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 171 |
} else { |
| 172 |
# Pass to the page group controller to display and scroll through the page group menu |
| 173 |
return me._page.mfd.SurroundController.handleFMSOuter(value); |
| 174 |
} |
| 175 |
}, |
| 176 |
handleEnter : func(value) { |
| 177 |
if (me._crsrToggle == 1) { |
| 178 |
if (me._checklists ==nil) return emesary.Transmitter.ReceiptStatus_Finished; |
| 179 |
if (me._currentGroup == ChecklistController.UIGROUP.GROUP) { |
| 180 |
# Load the new group, selecting the first checklist in the group |
| 181 |
me._group_selected = me._page.checklistGroupSelect.getValue(); |
| 182 |
me._list_selected = keys(me._checklists[me._group_selected])[0]; |
| 183 |
me._page.hideGroupSelect(); |
| 184 |
me._page.displayChecklist(me._group_selected, me._list_selected, me._checklists); |
| 185 |
me._page.checklistDisplay.setCRSR(0); |
| 186 |
me.selectChecklist(); |
| 187 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 188 |
} |
| 189 |
|
| 190 |
if (me._currentGroup == ChecklistController.UIGROUP.CHECKLIST) { |
| 191 |
# Load the selected checking from the group |
| 192 |
me._list_selected = me._page.checklistSelect.getValue(); |
| 193 |
me._page.hideChecklistSelect(); |
| 194 |
me._page.displayChecklist(me._group_selected, me._list_selected, me._checklists); |
| 195 |
me._page.checklistDisplay.setCRSR(0); |
| 196 |
me.selectItems(); |
| 197 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 198 |
} |
| 199 |
|
| 200 |
if (me._currentGroup == ChecklistController.UIGROUP.ITEMS) { |
| 201 |
# Check the selected Checklist item |
| 202 |
me.checkCurrentItem(); |
| 203 |
var idx = me._page.checklistDisplay.getCRSR(); |
| 204 |
|
| 205 |
if ((idx == (size(me._checklists[me._group_selected][me._list_selected]) -1)) and |
| 206 |
me._page.checklistDisplay.isComplete()) { |
| 207 |
# If we're right at the end of this checklist then move onto the "Next Checklist" |
| 208 |
# button. Manual isn't clear on whether this is only if the checklist is complete, |
| 209 |
# but we will assume that is the case. |
| 210 |
me.selectNext(); |
| 211 |
} else { |
| 212 |
# Automatically go to the next item. |
| 213 |
me.handleFMSInner(1); |
| 214 |
} |
| 215 |
|
| 216 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 217 |
} |
| 218 |
|
| 219 |
if (me._currentGroup == ChecklistController.UIGROUP.NEXT) { |
| 220 |
# Select the next checklist if there's one available |
| 221 |
var lists = keys(me._checklists[me._group_selected]); |
| 222 |
var idx = 0; |
| 223 |
for (i = 0; i < size(lists); i = i +1) { |
| 224 |
if (lists[i] == me._list_selected) idx = i + 1; |
| 225 |
} |
| 226 |
|
| 227 |
if (idx < size(lists)) { |
| 228 |
me._list_selected = lists[idx]; |
| 229 |
me._page.checklistDisplay.setCRSR(0); |
| 230 |
me._page.displayChecklist(me._group_selected, me._list_selected, me._checklists); |
| 231 |
me.selectItems(); |
| 232 |
} |
| 233 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 234 |
} |
| 235 |
|
| 236 |
return emesary.Transmitter.ReceiptStatus_NotProcessed; |
| 237 |
} else { |
| 238 |
return emesary.Transmitter.ReceiptStatus_NotProcessed; |
| 239 |
} |
| 240 |
}, |
| 241 |
|
| 242 |
handleClear : func() { |
| 243 |
if ((me._crsrToggle == 1) and |
| 244 |
(me._currentGroup == ChecklistController.UIGROUP.ITEMS)) { |
| 245 |
# Uncheck the selected Checklist item |
| 246 |
me.clearCurrentItem(); |
| 247 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 248 |
} |
| 249 |
|
| 250 |
return emesary.Transmitter.ReceiptStatus_NotProcessed; |
| 251 |
}, |
| 252 |
|
| 253 |
checkCurrentItem : func() { |
| 254 |
me._page.checklistDisplay.enterElement(); |
| 255 |
var idx = me._page.checklistDisplay.getCRSR(); |
| 256 |
me._checklists[me._group_selected][me._list_selected][idx]["Checked"] = 1; |
| 257 |
me._page.displayChecklist(me._group_selected, me._list_selected, me._checklists); |
| 258 |
}, |
| 259 |
|
| 260 |
clearCurrentItem : func() { |
| 261 |
me._page.checklistDisplay.clearElement(); |
| 262 |
var idx = me._page.checklistDisplay.getCRSR(); |
| 263 |
me._checklists[me._group_selected][me._list_selected][idx]["Checked"] = 0; |
| 264 |
me._page.displayChecklist(me._group_selected, me._list_selected, me._checklists); |
| 265 |
}, |
| 266 |
|
| 267 |
toggleCurrentItem : func() { |
| 268 |
if (me._page.checklistDisplay.getValue()) { |
| 269 |
me.clearCurrentItem(); |
| 270 |
} else { |
| 271 |
me.checkCurrentItem(); |
| 272 |
} |
| 273 |
}, |
| 274 |
|
| 275 |
# Retrieve the current set of checklists from the system. |
| 276 |
getChecklists : func() { |
| 277 |
var notification = notifications.PFDEventNotification.new( |
| 278 |
"MFD", |
| 279 |
me.getDeviceID(), |
| 280 |
notifications.PFDEventNotification.NavData, |
| 281 |
{Id: "GetChecklists", Value: nil}); |
| 282 |
|
| 283 |
var response = me._transmitter.NotifyAll(notification); |
| 284 |
if (! me._transmitter.IsFailed(response)) { |
| 285 |
me._checklists = notification.EventParameter.Value; |
| 286 |
} |
| 287 |
|
| 288 |
me._page.displayChecklist("", "", nil); |
| 289 |
|
| 290 |
# Find the first checklist of the first group to display; |
| 291 |
if ((me._checklists != nil) and size(keys(me._checklists)) != 0) { |
| 292 |
me._group_selected = keys(me._checklists)[0]; |
| 293 |
if (size(keys(me._checklists[me._group_selected])) != 0) { |
| 294 |
me._list_selected = keys(me._checklists[me._group_selected])[0]; |
| 295 |
me._page.displayChecklist(me._group_selected, me._list_selected, me._checklists); |
| 296 |
} |
| 297 |
} |
| 298 |
}, |
| 299 |
|
| 300 |
# Reset controller if required when the page is displayed or hidden |
| 301 |
ondisplay : func() { |
| 302 |
me.RegisterWithEmesary(); |
| 303 |
|
| 304 |
# Get the list of checklists if we don't already have it. This has to |
| 305 |
# be done here rather than in the Constructor because the appropriate |
| 306 |
# Emesary interface may not have been initialized in time. |
| 307 |
if (me._checklists == nil) me.getChecklists(); |
| 308 |
}, |
| 309 |
offdisplay : func() { |
| 310 |
me.DeRegisterWithEmesary(); |
| 311 |
}, |
| 312 |
|
| 313 |
}; |