Debian Codegraph @

Aircraft/Instruments-3d/FG1000/Nasal/MFDPages/Checklist/Checklist.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 # Checklist
18 var Checklist =
19 {
20 new : func (mfd, myCanvas, device, svg)
21 {
22 var obj = {
23 parents : [
24 Checklist,
25 MFDPage.new(mfd, myCanvas, device, svg, "Checklist", "LST - CHECKLIST")
26 ],
27 _groupSelectVisible : 0,
28 _checklistSelectVisible : 0,
29 _checklistDisplayVisible : 0,
30 };
31
32 obj.topMenu(device, obj, nil);
33
34 obj.checklistGroupSelect = PFD.GroupElement.new(
35 obj.pageName,
36 svg,
37 ["GroupItem"],
38 7,
39 "GroupItem",
40 0,
41 "GroupSelectScrollTrough",
42 "GroupSelectScrollThumb",
43 (165 - 120)
44 );
45
46 obj.checklistSelect = PFD.GroupElement.new(
47 obj.pageName,
48 svg,
49 ["ChecklistItem"],
50 7,
51 "ChecklistItem",
52 0,
53 "SelectScrollTrough",
54 "SelectScrollThumb",
55 (165 - 120)
56 );
57
58 obj.checklistDisplay = ChecklistGroupElement.new(
59 obj,
60 svg,
61 16,
62 "ScrollTrough",
63 "ScrollThumb",
64 (525 - 180)
65 );
66
67 # Other dynamic text elements
68 obj.addTextElements(["GroupName", "Name", "Next", "Finished", "NotFinished"]);
69
70 # The "Next" element isn't technically dynamic, though we want it to be
71 # highlighted as a text element. We need to set a value for it explicitly,
72 # as it'll be set to an empty string otherwise.
73 obj.setTextElement("Next", "GO TO NEXT CHECKLIST?");
74 obj.setTextElement("Finished", "* Checklist Finished *");
75 obj.setTextElement("NotFinished", "* CHECKLIST NOT FINISHED *");
76
77 # Hide the various groups
78 obj.hideChecklistSelect();
79 obj.hideGroupSelect();
80
81 obj.setController(fg1000.ChecklistController.new(obj, svg));
82
83 return obj;
84 },
85
86 displayChecklist : func(group, name, checklist_data) {
87 me.setTextElement("GroupName", group);
88 me.setTextElement("Name", name);
89
90 if (checklist_data == nil) {
91 me.checklistGroupSelect.setValues([]);
92 me.checklistGroupSelect.setValues([]);
93 me.checklistDisplay.setValues([]);
94 return;
95 }
96
97 # Populate the list of groups
98 var grouplist = [];
99 foreach (var grp; keys(checklist_data)) {
100 append(grouplist, { GroupItem : substr(grp, 0, 20) } );
101 }
102
103 me.checklistGroupSelect.setValues(grouplist);
104
105 # Populate the list of checklists for this group
106 var checklist_group = checklist_data[group];
107 var checklistlist = [];
108 foreach (var checklist; keys(checklist_group)) {
109 append(checklistlist, { ChecklistItem : checklist } );
110 }
111
112 me.checklistSelect.setValues(checklistlist);
113
114 # Finally, populate the checklist itself!
115 var checklist = checklist_group[name];
116 var checklistitems = [];
117 foreach (var item; checklist) {
118 append(checklistitems, {
119 "ItemName" : item.Name,
120 "ItemValue" : item.Value,
121 "ItemBox" : 1,
122 "ItemDots" : 1,
123 "ItemTick" : item.Checked,
124 "ItemSelect" : size(checklistitems),
125 });
126 }
127
128 me.checklistDisplay.setValues(checklistitems);
129 },
130
131 displayChecklistSelect : func () {
132 me.getElement("Select").setVisible(1);
133 me.checklistSelect.displayGroup();
134 me.checklistSelect.showCRSR();
135 },
136
137 hideChecklistSelect : func () { me.getElement("Select").setVisible(0); },
138 isChecklistSelectVisible : func() { return me.getElement("Select").getVisible(); },
139
140 displayGroupSelect : func () {
141 me.getElement("GroupSelect").setVisible(1);
142 me.checklistGroupSelect.displayGroup();
143 me.checklistGroupSelect.showCRSR();
144 },
145
146 hideGroupSelect : func () { me.getElement("GroupSelect").setVisible(0); },
147 isGroupSelectVisible : func() { return me.getElement("GroupSelect").getVisible(); },
148
149 displayItemSelect : func () {
150 me.checklistDisplay.displayGroup();
151 me.checklistDisplay.showCRSR();
152 },
153
154 hideItemSelect : func () {
155 me.checklistDisplay.hideCRSR();
156 },
157
158 offdisplay : func() {
159 me._group.setVisible(0);
160
161 # Reset the menu colours. Shouldn't have to do this here, but
162 # there's not currently an obvious other location to do so.
163 for(var i = 0; i < 12; i +=1) {
164 var name = sprintf("SoftKey%d",i);
165 me.device.svg.getElementById(name ~ "-bg").setColorFill(0.0,0.0,0.0);
166 me.device.svg.getElementById(name).setColor(1.0,1.0,1.0);
167 }
168 me.getController().offdisplay();
169 },
170 ondisplay : func() {
171 me._group.setVisible(1);
172 me.mfd.setPageTitle(me.title);
173 me.getController().ondisplay();
174 },
175 topMenu : func(device, pg, menuitem) {
176 pg.clearMenu();
177 pg.resetMenuColors();
178 # Manual is inconsistent. Checklist page has SYSTEM, menu structure shows ENGINE
179 pg.addMenuItem(0, "SYSTEM", pg, pg.mfd.EIS.systemMenu);
180 pg.addMenuItem(5, "CHECK", pg,
181 func(dev, pg, mi) { pg.getController().toggleCurrentItem(); dev.updateMenus(); }, # callback
182 func(svg, mi) { pg.displayCheckUncheck(svg); } # Display function
183 );
184 pg.addMenuItem(10, "EXIT", pg,
185 # This should return to the previous page...
186 func(dev, pg, mi) { dev.selectPage(pg.getMFD().getPage("NavigationMap")); },
187 );
188
189 pg.addMenuItem(11, "EMERGCY", pg,
190 func(dev, pg, mi) { pg.getController().selectEmergencyChecklist(); }, # callback
191 );
192
193 device.updateMenus();
194 },
195
196 # Display function for the CHECK/UNCHECK softkey
197 displayCheckUncheck : func (svg) {
198 if (me.checklistDisplay.getValue()) {
199 svg.setText("UNCHECK");
200 } else {
201 svg.setText("CHECK");
202 }
203 svg.setVisible(1);
204 },
205 };
206
207
208 # A modified GroupElement for specific use by the checklist function.
209 #
210 # Key differences:
211 # - Current selected item is shown in white.
212 # - Checked items are shown in green
213 # - Unchecked items are shown in blue.
214 var ChecklistGroupElement =
215 {
216
217 new : func (page, svg, displaysize, scrollTroughElement=nil, scrollThumbElement=nil, scrollHeight=0, style=nil)
218 {
219 var obj = {
220 parents : [ ChecklistGroupElement ],
221 _page : page,
222 _pageName : page.pageName,
223 _svg : svg,
224 _style : style,
225 _scrollTroughElement : nil,
226 _scrollThumbElement : nil,
227 _scrollBaseTransform : nil,
228
229 # A hash mapping keys to the element name prefix of an SVG element
230 _textElementNames : ["ItemName", "ItemValue"],
231 _highlightElementNames : ["ItemBox", "ItemTick", "ItemDots", "ItemSelect"],
232
233 # The size of the group. For each of the ._textElementNames hash values there
234 # must be an SVG Element [pageName][elementName]{0...pageSize}
235 _size : displaysize,
236
237 # Length of the scroll bar.
238 _scrollHeight : scrollHeight,
239
240 # List of values to display
241 _values : [],
242
243 # List of SVG elements to display the values
244 _elements : [],
245
246 # Cursor index into the _values array
247 _crsrIndex : 0,
248
249 # Whether the CRSR is enabled
250 _crsrEnabled : 0,
251
252 # Page index - which _values index element[0] refers to. The currently
253 # selected _element has index (_crsrIndex - _pageIndex)
254 _pageIndex : 0,
255 };
256
257 # Optional scroll bar elements, consisting of the Thumb and the Trough *,
258 # which will be used to display the scroll position.
259 # * Yes, these are the terms of art for the elements.
260 assert(((scrollTroughElement == nil) and (scrollThumbElement == nil)) or
261 ((scrollTroughElement != nil) and (scrollThumbElement != nil)),
262 "Both the scroll trough element and the scroll thumb element must be defined, or neither");
263
264 if (scrollTroughElement != nil) {
265 obj._scrollTroughElement = svg.getElementById(obj._pageName ~ scrollTroughElement);
266 assert(obj._scrollTroughElement != nil, "Unable to find scroll element " ~ obj._pageName ~ scrollTroughElement);
267 }
268 if (scrollThumbElement != nil) {
269 obj._scrollThumbElement = svg.getElementById(obj._pageName ~ scrollThumbElement);
270 assert(obj._scrollThumbElement != nil, "Unable to find scroll element " ~ obj._pageName ~ scrollThumbElement);
271 obj._scrollBaseTransform = obj._scrollThumbElement.getTranslation();
272 }
273
274 if (style == nil) obj._style = PFD.DefaultStyle;
275
276 for (var i = 0; i < displaysize; i = i + 1) {
277 append(obj._elements, PFD.HighlightElement.new(obj._pageName, svg, "ItemSelect" ~ i, i, obj._style));
278 }
279
280 return obj;
281 },
282
283 # Set the values of the group. values_array is an array of hashes, each of which
284 # has keys that match those of ._textElementNames
285 setValues : func (values_array) {
286 me._values = values_array;
287 #me._pageIndex = 0;
288 #me._crsrIndex = 0;
289
290 if (size(me._values) > me._size) {
291 # Number of elements exceeds our ability to display them, so enable
292 # the scroll bar.
293 if (me._scrollThumbElement != nil) me._scrollThumbElement.setVisible(1);
294 if (me._scrollTroughElement != nil) me._scrollTroughElement.setVisible(1);
295 } else {
296 # There is no scrolling to do, so hide the scrollbar.
297 if (me._scrollThumbElement != nil) me._scrollThumbElement.setVisible(0);
298 if (me._scrollTroughElement != nil) me._scrollTroughElement.setVisible(0);
299 }
300
301 me.displayGroup();
302 },
303
304 displayGroup : func () {
305
306 # The _crsrIndex element should be displayed as close to the middle of the
307 # group as possible. So as the user scrolls the list appears to move around
308 # a static cursor position.
309 #
310 # The exceptions to this is as the _crsrIndex approaches the ends of the list.
311 # In these cases, we let the cursor move to the top or bottom of the list.
312
313 # Check the CRSR index is valid
314 if (me._crsrIndex > (size(me._values) -1)) me._crsrIndex = 0;
315
316 # Determine the middle element
317 var middle_element_index = math.ceil(me._size / 2);
318 me._pageIndex = me._crsrIndex - middle_element_index;
319
320 if ((size(me._values) <= me._size) or (me._crsrIndex < middle_element_index)) {
321 # Start of list or the list is too short to require scrolling
322 me._pageIndex = 0;
323 } else if (me._crsrIndex > (size(me._values) - middle_element_index - 1)) {
324 # End of list
325 me._pageIndex = size(me._values) - me._size;
326 }
327
328 for (var i = 0; i < me._size; i = i + 1) {
329 if (me._pageIndex + i < size(me._values)) {
330
331 var value = me._values[me._pageIndex + i];
332 var checked = 0;
333 var crsr = 0;
334
335 if (value["ItemTick"] == 1) checked = 1;
336 if (me._crsrEnabled and (i + me._pageIndex == me._crsrIndex)) crsr = 1;
337
338 foreach (var k; keys(value)) {
339
340 var name = me._pageName ~ k ~ i;
341 var element = me._svg.getElementById(name);
342 assert(element != nil, "Unable to find element " ~ name);
343
344 if (k == "ItemSelect") {
345 # Display if this is the cursor element
346 element.setVisible(crsr);
347 } else if (k == "ItemTick") {
348 # Check the box if appropriate
349 element.setVisible(checked);
350 } else if (k == "ItemBox") {
351 # Always display the box, but don't colour it.
352 element.setVisible(1);
353 } else if (k == "ItemDots") {
354 # Always display the dots
355 element.setVisible(1);
356
357 if (crsr) {
358 # White - current cursor
359 element.setColor("#FFFFFF");
360 } else if (checked) {
361 # Green - checked
362 element.setColor("#00FF00");
363 } else {
364 # Cyan - unchecked
365 element.setColor("#00FFFF");
366 }
367 } else if ((k == "ItemName") or (k == "ItemValue")) {
368 element.setVisible(1);
369 # We need to fill the bounding box with black so that we don't
370 # see the underlying dots.
371 element.setText(value[k])
372 .setDrawMode(canvas.Text.TEXT + canvas.Text.FILLEDBOUNDINGBOX)
373 .setPadding(2)
374 .setColorFill("#000000");
375
376 if (crsr) {
377 # White - current cursor
378 element.setColor("#FFFFFF");
379 } else if (checked) {
380 # Green - checked
381 element.setColor("#00FF00");
382 } else {
383 # Cyan - unchecked
384 element.setColor("#00FFFF");
385 }
386 } else {
387 print("Unknown value " ~ k);
388 }
389 }
390 } else {
391 # We've gone off the end of the values list, so hide any further values.
392 foreach (var k; me._textElementNames) {
393 var name = me._pageName ~ k ~ i;
394 var element = me._svg.getElementById(name);
395 assert(element != nil, "Unable to find element " ~ name);
396 element.setVisible(0);
397 }
398 foreach (var k; me._highlightElementNames) {
399 var name = me._pageName ~ k ~ i;
400 var element = me._svg.getElementById(name);
401 assert(element != nil, "Unable to find element " ~ name);
402 element.setVisible(0);
403 }
404 }
405 }
406
407 if ((me._scrollThumbElement != nil) and (me._size < size(me._values))) {
408 # Shift the scrollbar if it's relevant
409 me._scrollThumbElement.setTranslation([
410 me._scrollBaseTransform[0],
411 me._scrollBaseTransform[1] + me._scrollHeight * (me._crsrIndex / (size(me._values) -1))
412 ]);
413 }
414
415 # Indicate whether we're finished with this checklist or not
416 var finished = me.isComplete();
417 me._page.getTextElement("Finished").setVisible(finished);
418 me._page.getTextElement("NotFinished").setVisible(! finished);
419
420 # Update the softkeys, which will in particular change the CHECK/UNCHECK softkeys
421 # appropriately.
422 me._page.device.updateMenus();
423 },
424
425 isComplete : func() {
426 var finished = 1;
427 foreach (var entry; me._values) {
428 if (entry["ItemTick"] == 0) {
429 finished = 0;
430 break;
431 }
432 }
433
434 return finished;
435 },
436
437 # Methods to add dynamic elements to the group. Must be called in the
438 # scroll order, as they are simply appended to the end of the list of elements!
439 addHighlightElement : func(name, value) {
440 append(me._elements, HighlightElement.new(me._pageName, me._svg, name, value));
441 },
442 addTextElement : func(name, value) {
443 append(me._elements, TextElement.new(me._pageName, me._svg, name, value));
444 },
445
446 showCRSR : func() {
447 if (size(me._values) == 0) return;
448 me._crsrEnabled = 1;
449 me.displayGroup();
450 },
451 hideCRSR : func() {
452 if (me._crsrEnabled == 0) return;
453 me._crsrEnabled = 0;
454 me.displayGroup();
455 },
456 setCRSR : func(index) {
457 me._crsrIndex = math.min(index, size(me._values) -1);
458 me._crsrIndex = math.max(0, me._crsrIndex);
459 },
460 getCRSR : func() {
461 return me._crsrIndex;
462 },
463 getCursorElementName : func() {
464 if (me._crsrEnabled == -1) return nil;
465 return me._elements[me._crsrIndex - me._pageIndex].name;
466 },
467 isCursorOnDataEntryElement : func() {
468 if (me._crsrEnabled == -1) return 0;
469 return isa(me._elements[me._crsrIndex - me._pageIndex], DataEntryElement);
470 },
471 enterElement : func() {
472 if (me._crsrEnabled == 0) return;
473
474 # ENT on an element of the checklist checks the box,
475 # indicated by whether the check mark is visible or not.
476 var name = me._pageName ~ "ItemTick" ~ (me._crsrIndex - me._pageIndex);
477 var element = me._svg.getElementById(name);
478 element.setVisible(1);
479 return element.getVisible();
480 },
481 getValue : func() {
482 if (me._crsrEnabled == -1) return nil;
483
484 # In this case, all we care about is whether this particular value is
485 # checked or not.
486 var name = me._pageName ~ "ItemTick" ~ (me._crsrIndex - me._pageIndex);
487 var element = me._svg.getElementById(name);
488 return element.getVisible();
489 },
490 setValue : func(idx, key, value) {
491 me._values[idx][key] = value;
492 },
493 clearElement : func() {
494 if (me._crsrEnabled == 0) return;
495
496 # CLR on an element of the checklist unchecks the box,
497 # indicated by whether the check mark is visible or not.
498 var name = me._pageName ~ "ItemTick" ~ (me._crsrIndex - me._pageIndex);
499 var element = me._svg.getElementById(name);
500 element.setVisible(0);
501 return element.getVisible();
502 },
503 incrSmall : func(value) {
504 if (me._crsrEnabled == 0) return;
505
506 var incr_or_decr = (value > 0) ? 1 : -1;
507 if (me._elements[me._crsrIndex - me._pageIndex].isInEdit()) {
508 # We're editing, so pass to the element.
509 me._elements[me._crsrIndex - me._pageIndex].incrSmall(val);
510 } else {
511 # Move to next selection element
512 me._crsrIndex = me._crsrIndex + incr_or_decr;
513 if (me._crsrIndex < 0 ) me._crsrIndex = 0;
514 if (me._crsrIndex == size(me._values)) me._crsrIndex = size(me._values) -1;
515 me.displayGroup();
516 }
517 },
518 incrLarge : func(val) {
519 if (me._crsrEnabled == 0) return;
520 var incr_or_decr = (val > 0) ? 1 : -1;
521 if (me._elements[me._crsrIndex - me._pageIndex].isInEdit()) {
522 # We're editing, so pass to the element.
523 me._elements[me._crsrIndex - me._pageIndex].incrLarge(val);
524 } else {
525 # Move to next selection element
526 me._crsrIndex = me._crsrIndex + incr_or_decr;
527 if (me._crsrIndex < 0 ) me._crsrIndex = 0;
528 if (me._crsrIndex == size(me._values)) me._crsrIndex = size(me._values) -1;
529 me.displayGroup();
530 }
531 },
532 };