zkv1000 / Nasal / MFD.pages.nas /
83d4e14 4 years ago
1 contributor
328 lines | 14.47kb
# vim: set foldmethod=marker foldmarker={{{,}}} :
displayClass.MFD = {
    AUX: {
        'SYSTEM SETUP' : func {
            var obj_infos = [
                {text: 'NAV BOX DATA FIELDS', type: 'title'},
                {type: 'separator'},
                {text: 'DATA FIELD NUMBER', type: 'normal'},
                {text: '  1 >', type: 'selected|end-of-line', choices: ['  1 >', '< 2 >', '< 3 >', '< 4  ']},
            ];
            var column = 0;
            var colmax = 5;
            foreach (var text; me.device.display.updateNavigationBox(1)) {
                append(obj_infos, {
                    text: sprintf('%-5s', text),
                    type: 'editable' ~ ((column == colmax) ? '|end-of-line' : ''),
                    callback: func (id, selected) {
                        var field = string.trim(me.device.windows.state[id].objects[3].text,
                                                0,
                                                func (c) c == `<` or c == `>` or c == ` `);
                        me.device.display.screenElements['DATA-FIELD' ~ field ~ '-ID-text']
                            .setText(string.trim(me.device.windows.state[id].objects[selected].text, 1));
                    }
                });
                column += (column < colmax) ? 1 : -column;
            }
            var windowId = 'SYSTEM SETUP';
            me.device.windows.draw( windowId, obj_infos );
            me.device.buttons.CLR = func {
                me.device.windows.del(windowId);
                me.device.buttons.ENT = func;
                me.device.buttons.CLR = func;
                me.device.knobs.FmsInner = func;
                me.device.knobs.FmsOuter = me.device.knobs.MFD_select_page_group;
            }
            me.device.knobs.FmsInner = me.device.knobs.MenuSettings;
            me.device.knobs.FmsOuter = me.device.knobs.NavigateMenu;
            me.device.buttons.ENT = me.device.buttons.ValidateTMRREF;
        },
    },
    NRST : {
        'NEAREST AIRPORTS': func {
            var airports = findAirportsWithinRange(99.99);
            var obj_infos = [
                {text: size(airports) ~ ' NEAREST AIRPORTS', type: 'title'},
                {type: 'separator'},
            ];
            var name_max_length = 0;
            forindex (var idx; airports) {
                var info = airportinfo(airports[idx].id);
                var (course, distance) = courseAndDistance(info);
                var name = sprintf('%s (%s)', airports[idx].id, airports[idx].name);
                if (size(name) > name_max_length)
                    name_max_length = size(name);

                append(obj_infos, {
                        text: name,
                        type: (idx ? 'editable' : 'selected') ~ '|immediate|end-of-line',
                        scrollgroup: idx,
                    }
                );
                append(obj_infos, {
                        text: sprintf(' %s DST %2dNM CRS %03d', utf8.chstr(9658), distance, course),
                        type: 'normal|end-of-line',
                        scrollgroup: idx
                    }
                );
            }
            append(obj_infos, {type: 'separator'});
            var windowId = 'NEAREST AIRPORTS';
            me.device.windows.draw( windowId, obj_infos, {lines: 4, columns: 1} );
            me.device.buttons.CLR = func {
                me.device.windows.del(windowId);
                me.device.buttons.ENT = func;
                me.device.buttons.CLR = func;
                me.device.knobs.FmsInner = func;
                me.device.knobs.FmsOuter = me.device.knobs.MFD_select_page_group;
            }
            me.device.knobs.FmsInner = me.device.knobs.NavigateMenu;
            me.device.knobs.FmsOuter = me.device.knobs.NavigateMenu;
            me.device.buttons.ENT = func (a = 0) {
                if (a) return;
                me.device.windows.del(windowId);
                var (_, selected) = split('-', me.device.windows.selected);
                var (airport_id, _) = split(" ", obj_infos[selected].text);
                call(me.device.display.MFD._ShowAirportInfo, [airport_id], me);
            };
        },
        'NEAREST INTERSECTIONS': func {
            call(me.device.display.MFD._NearestNavaids, ['fix'], me);
        },
        'NEAREST NDB': func {
            call(me.device.display.MFD._NearestNavaids, ['ndb'], me);
        },
        'NEAREST VOR': func {
            call(me.device.display.MFD._NearestNavaids, ['vor'], me);
        },
    },
    _NearestNavaids: func (navaid_type) {
        var navaids = findNavaidsWithinRange(99.99, navaid_type);
        var obj_infos = [
            {text: size(navaids) ~ ' NEAREST ' ~ string.uc(navaid_type), type: 'title'},
            {type: 'separator'},
        ];
        var idx = 0;
        foreach (var navaid; navaids) {
            var (course, distance) = courseAndDistance(navaid);
            var name = navaid.id ~ ' (' ~ navaid.name ~ ')';

            append(obj_infos, {
                    text: navaid.id ~ ' (' ~ navaid.name ~ ')',
                    type: (idx ? 'editable' : 'selected') ~ '|immediate|end-of-line',
                    scrollgroup: idx,
                }
            );
            append(obj_infos, {
                    text: sprintf(' %s DST %2dNM CRS %03d', utf8.chstr(9658), distance, course),
                    type: 'normal|end-of-line',
                    scrollgroup: idx
                }
            );
            idx += 1;
        }
        append(obj_infos, {type: 'separator'});
        var windowId = obj_infos[0].text;
        me.device.windows.draw( windowId, obj_infos, {lines: 4, columns: 1} );
        me.device.buttons.CLR = func {
            me.device.windows.del(windowId);
            me.device.buttons.ENT = func;
            me.device.buttons.CLR = func;
            me.device.knobs.FmsInner = func;
            me.device.knobs.FmsOuter = me.device.knobs.MFD_select_page_group;
        }
        me.device.knobs.FmsInner = me.device.knobs.NavigateMenu;
        me.device.knobs.FmsOuter = me.device.knobs.NavigateMenu;
        me.device.buttons.ENT = func (a = 0) {
            if (a) return;
            me.device.windows.del(windowId);
            var (_, selected) = split('-', me.device.windows.selected);
            var (navaid_id, _) = split(" ", obj_infos[selected].text);
            if (navaid_type == 'airport')
                call(me.device.display.MFD._ShowAirportInfo, [navaid_id], me);
            else
                call(me.device.display.MFD._ShowNavaidInfo, [navaid_id, navaid_type], me);
        };
    },
    _ShowNavaidInfo: func (navaid_id, navaid_type) {
        var info = findNavaidsByID(navaid_id, navaid_type);
        if (size(info) == 0) return;
        me.device.knobs.FmsInner = func;
        me.device.knobs.FmsOuter = func;
        me.device.buttons.ENT    = func;
        print(typeof(info[0]));
        var obj_infos = [
            {text: info[0].id ~ ' INFORMATION', type: 'title'},
            {type: 'separator'},
            {text: 'ID    ' ~ info[0].id, type: 'normal|end-of-line'},
            {text: 'NAME  ' ~ info[0].name, type: 'normal|end-of-line'},
            {text: 'TYPE  ' ~ info[0].type, type: 'normal|end-of-line'},
            {text: sprintf('LON   %.3f %s', abs(info[0].lon), info[0].lon > 0 ? 'E' : 'W'), type: 'normal|end-of-line'},
            {text: sprintf('LAT   %.3f %s', abs(info[0].lat), info[0].lat > 0 ? 'N' : 'S'), type: 'normal|end-of-line'},
        ];
        call(func {return info[0].range }, [], nil, nil, var errors = []);
        if (!size(errors))
            append(obj_infos, {text: sprintf('RANGE %i NM', info[0].range * 1.852), type: 'normal|end-of-line'});
        call(func {return info[0].frequency }, [], nil, nil, var errors = []);
        if (!size(errors))
            append(obj_infos, {text: sprintf('FREQ  %.2f kHz', info[0].frequency / 100), type: 'normal|end-of-line'});
        var lines = size(obj_infos);
        var windowId = 'NAVAID INFORMATIONS';
        me.device.windows.draw( windowId, obj_infos );
        me.device.buttons.CLR = func {
            me.device.windows.del(windowId);
            me.device.buttons.ENT = func;
            me.device.buttons.CLR = func;
            me.device.knobs.FmsInner = func;
            me.device.knobs.FmsOuter = me.device.knobs.MFD_select_page_group;
        }
    },
    _ShowAirportInfo: func (airport_id) {
        me.device.knobs.FmsInner = func;
        me.device.knobs.FmsOuter = func;
        me.device.buttons.ENT    = func;
        var info = airportinfo(airport_id);
        var obj_infos = [
            {text: airport_id ~ ' INFORMATION', type: 'title'},
            {type: 'separator'},
            {text: sprintf('ELEVATION %.0d FT', info.elevation * 3.28), type: 'normal|end-of-line'},
            {text: sprintf('LON: %.3f %s', abs(info.lon), info.lon > 0 ? 'E' : 'W'), type: 'normal|end-of-line'},
            {text: sprintf('LAT: %.3f %s', abs(info.lat), info.lat > 0 ? 'N' : 'S'), type: 'normal|end-of-line'},
            {type: 'separator'},
            {text: 'RUNWAYS', type: 'title'},
        ];
        foreach (var rwy; sort(keys(info.runways), string.icmp)) {
            var rwyInfo = sprintf("%-3s %4.0dm %3.0d°",
                                  rwy,
                                  info.runways[rwy].length,
                                  info.runways[rwy].heading);
            if (info.runways[rwy].ils != nil) {
                rwyInfo = sprintf("%s %3.3f Mhz", rwyInfo, info.runways[rwy].ils.frequency / 100);
            }
            append(obj_infos, {text: rwyInfo, type: 'normal|end-of-line'});
        }
        var sep = 2; # to count separators, already two printed
        if (size(info.comms()) > 0) {
# TODO: find nearby freqs if none found for airport
            append(obj_infos, {type: 'separator'}); sep += 1;
            append(obj_infos, {text: 'COMMS FREQUENCIES', type: 'title'});
            var freqs = {};
            var comms = info.comms();
            foreach (var c; comms)
                freqs[c.ident] = sprintf("%.3f", c.frequency);

            foreach (var f; sort(keys(freqs), string.icmp))
                append(obj_infos, {text: sprintf('%-15s %.3f', f, freqs[f]), type: 'normal|end-of-line'});
        }
        var lines = size(obj_infos) - sep; # minus the separators
        var windowId = 'AIRPORT INFORMATIONS';
        me.device.windows.draw( windowId, obj_infos );
        me.device.buttons.CLR = func {
            me.device.windows.del(windowId);
            me.device.buttons.ENT = func;
            me.device.buttons.CLR = func;
            me.device.knobs.FmsInner = func;
            me.device.knobs.FmsOuter = me.device.knobs.MFD_select_page_group;
        }
    },
};

displayClass.setMFDPages = func {
    me.device.data['page selection'] = [
# list of pages, by group {{{
        {
            name: 'MAP',
            objects: [
                {text: 'NAVIGATION MAP'},
                {text: 'TRAFFIC MAP'},
                {text: 'STORMSCOPE'},
                {text: 'WEATHER DATA LINK'},
                {text: 'TAWS-B'},
            ],
        },
        {
            name: 'WPT',
            objects: [
                {text: 'AIRPORT INFORMATION'},
                {text: 'AIRPORT DIRECTORY'},
                {text: 'DEPARTURE INFORMATION'},
                {text: 'ARRIVAL INFORMATION'},
                {text: 'APPROACH INFORMATION'},
                {text: 'WEATHER INFORMATION'},
                {text: 'INTERSECTION INFORMATION'},
                {text: 'NDB INFORMATION'},
                {text: 'VOR INFORMATION'},
                {text: 'USER WAYPOINT INFORMATION'},
            ],
        },
        {
            name: 'AUX',
            objects: [
                {text: 'TRIP PLANNING'},
                {text: 'UTILITY'},
                {text: 'GPS STATUS'},
                {text: 'SYSTEM SETUP'},
            ],
        },
        {
            name: 'FPL',
            objects: [
                {text: 'ACTIVE FLIGHT PLAN'},
                {text: 'WIDE VIEW, NARROW VIEW'},
                {text: 'FLIGHT PLAN CATALOG'},
            ],
        },
        {
            name: 'PROC',
            objects: [
                {text: 'DEPARTURE LOADING'},
                {text: 'ARRIVAL LOADING'},
                {text: 'APPROACH LOADING'},
            ],
        },
        {
            name: 'NRST',
            objects: [
                {text: 'NEAREST AIRPORTS'},
                {text: 'NEAREST INTERSECTIONS'},
                {text: 'NEAREST NDB'},
                {text: 'NEAREST VOR'},
                {text: 'NEAREST USER WAYPOINTS'},
                {text: 'NEAREST FREQUENCIES'},
                {text: 'NEAREST AIRSPACES'},
            ],
        },
# }}}
    ];

    foreach (var g; me.device.data['page selection']) {
        var obj_s = size(g.objects);
        # build specific geometry per page, depending of number of sub-pages
        g.geometry = {x: 720, y: 758 - ((obj_s + 3) * 24), w: 300, l: obj_s + 1, sep: 1};
        # complete the hash with reccurrent elements type and callback
        var firstEntry = 1;
        foreach (var o; g.objects) {
            if (contains(me.MFD, g.name) and contains(me.MFD[g.name], o.text)) {
                o.type = firstEntry ? 'selected' : 'editable';
                o.type ~= '|end-of-line';
                o.callback = me.device.buttons.MFD_page_wrapper;
                firstEntry = 0;
            }
            else
                o.type = 'normal|end-of-line';
        }
    }
    # build the available groups line, at the bottom
    forindex (var g; me.device.data['page selection']) {
        append(me.device.data['page selection'][g].objects, {type: 'separator'});
        for (var i = 0; i < g; i+=1)
            append(me.device.data['page selection'][g].objects,
                {text: me.device.data['page selection'][i].name, type: 'normal'});
        append(me.device.data['page selection'][g].objects,
            {text: me.device.data['page selection'][i].name, type: 'highlighted'});
        for (var i = g+1; i < size(me.device.data['page selection']); i+=1)
            append(me.device.data['page selection'][g].objects,
                {text: me.device.data['page selection'][i].name, type: 'normal'});
    }
    me.device.knobs.FmsOuter = me.device.knobs.MFD_select_page_group;
};