zkv1000 / Nasal / buttons.nas /
Sébastien MARQUE settings now have effect
989f952 4 years ago
1 contributor
371 lines | 16.931kb
var buttonsClass = {
    new : func (device) {
        var m = { parents: [ buttonsClass ] };
        m.device = device;
        return m;
    },

    PAN : func (xdir = 0, ydir = 0) {
    },

    AsSwitchNAV : func {
        var n = radios.getValue('nav-tune');
        var tmp = getprop('/instrumentation/nav[' ~ n ~ ']/frequencies/selected-mhz');
        setprop('/instrumentation/nav[' ~ n ~ ']/frequencies/selected-mhz', getprop('/instrumentation/nav[' ~ n ~ ']/frequencies/standby-mhz'));
        setprop('/instrumentation/nav[' ~ n ~ ']/frequencies/standby-mhz', tmp);
        foreach (var c; keys(flightdeck))
            if (contains(flightdeck[c], 'display'))
                flightdeck[c].display.updateNAV({refresh: n+1});
    },

    AsSwitchCOM : func (x) {
        if (x) {
            me.AsSwitchCOM_pushed = getprop('/sim/time/elapsed-sec');
        }
        else {
            var pressed = getprop('/sim/time/elapsed-sec') - me.AsSwitchCOM_pushed;
            if (pressed > 2) {
                setprop('/instrumentation/comm/frequencies/selected-mhz', 121.500);
                radios.setValue('comm1-selected', 1);
                radios.setValue('comm2-selected', 0);
                foreach (var d; keys(flightdeck))
                    if (contains(flightdeck[d], 'display')) {
                        flightdeck[d].display.updateCOMM({refresh: 1});
                        flightdeck[d].display.updateCOMM({refresh: 2});
                    }
            }
            else {
                var c = radios.getValue('comm-tune');
                var tmp = getprop('/instrumentation/comm[' ~ c ~ ']/frequencies/selected-mhz');
                setprop('/instrumentation/comm[' ~ c ~ ']/frequencies/selected-mhz', getprop('/instrumentation/comm[' ~ c ~ ']/frequencies/standby-mhz'));
                setprop('/instrumentation/comm[' ~ c ~ ']/frequencies/standby-mhz', tmp);
                foreach (var d; keys(flightdeck))
                    if (contains(flightdeck[d], 'display'))
                        flightdeck[d].display.updateCOMM({refresh: c+1});
            }
        }
    },

    ALT : func () {
        var alt = getprop('instrumentation/altimeter/indicated-altitude-ft');
        afcs.setIntValue('selected-alt-ft', math.round(alt, 10));
    },

    ValidateTMRREF : func (a = 0) {
        if (a)
            return;
        var (id, selected) = split('-', me.device.windows.selected);
        var state = me.device.windows.state[id];
        selected += state.scroll.offset;
        if (contains(state.objects[selected], 'callback'))
            call(state.objects[selected].callback, [id, selected], me);
    },

    ClearTMRREF : func (a = 0) {
        if (a)
            return;
        me.device.windows.del();
        me.device.data.TMRtimer = nil;
        me.device.knobs.FmsInner = func;
        me.device.knobs.FmsOuter = func;
        me.device.buttons.ENT = func;
        me.device.buttons.FMS = func;
        me.device.buttons.CLR = func;
    },

    MFD_page_wrapper : func (id, selected) {
        var s = me.device.data[id][me.device.display['page selected']];
        var group = s.name;
        var subpage = s.objects[selected].text;

        foreach (var k; keys(me.device.windows.window))
            if (find(id, k) == 0) {
                me.device.windows.del(id);
                break;
            }
        call(me.device.display.MFD[group][subpage], [], me);
    },

    MapMenu : func (a = 0) {
        if (a == 1)
            return;
        var menu_label = 'MAP MENU';
        if (!contains(me.device.windows.window, menu_label ~ '-bg')) {
            var level_min = 13;
            var level_max = 7;
            var levels = [ sprintf('  % 2u >', level_max) ];
            for (var i = level_max + 1; i <= level_min; i += 1)
                append(levels, sprintf(i < level_min ? '< % 2u >' : '< % 2u  ', i));
            var orientation = [ '  NORTH UP >', '<  TRK UP  >' ];
            if (getprop('/instrumentation/gps/route-distance-nm') != nil)
                append(orientation, '<  DTK UP  >');
            append(orientation, '<  HDG UP   ');
            me.device.windows.draw(
                    menu_label,
                    {x: 720, y: 100, w: 300, l:3, sep: 1},
                    [
                        {text: menu_label, type: 'title'},
                        {type: 'separator'},
                        {text: 'ZOOM LEVEL', type: 'normal'},
                        {text: sprintf('%s % 2u %s',
                                me.device.data.zoom > level_max ? '<' : ' ',
                                me.device.data.zoom,
                                me.device.data.zoom < level_min ? '>' : ' '),
                         type: 'selected|end-of-line',
                         choices: levels,
                         callback: func (id, selected) {
                             var z = me.device.windows.state[id].objects[selected].text;
                             foreach (var c; [' ', '<', '>'])
                                 z = string.replace(z, c, '');
                             me.device.data.zoom = z;
                             me.device.map.changeZoom();
                             me.device.map.update();
                         }
                        },
                        {text: 'ORIENTATION', type: 'normal'},
                        {text: (func foreach (var o; orientation) if (find(me.device.data.orientation.text, o) > -1) return o;)(),
                         type: 'editable|end-of-line',
                         choices: orientation,
                         callback: func (id, selected) {
                             var o = me.device.windows.state[id].objects[selected].text;
                             o = substr(o, 2);
                             o = substr(o, 0, size(o) - 2);
                             o = string.trim(o);
                             me.device.data.orientation.text = o;
                             me.device.map.update();
                         }
                        },
                    ]
                );
            me.device.knobs.FmsOuter = me.device.knobs.NavigateMenu;
            me.device.knobs.FmsInner = me.device.knobs.MenuSettings;
            me.device.buttons.CLR = me.device.buttons.MapMenu;
            me.device.buttons.ENT = me.device.buttons.ValidateTMRREF;
        }
        else {
            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.windows.del(menu_label);
        }
    },

    GlobalParams: func (a) {
        if (a == 1)
            return;
        var windowId = 'GLOBAL SETTINGS';
        var obj_infos = [
            {text: 'DISPLAY', type: 'title'},
            {type: 'separator'},
            {text: 'Brightness: ', type: 'normal'},
            {text: sprintf( '% 3u %%', zkv.getValue('display-brightness-norm') * 100 / 0.7),
                type: 'selected immediate end-of-line',
                format: ' % 3u %% ',
                range: {max: 100, min: 0},
                callback: func (id, selected) {
                    var b = num(string.trim(me.device.windows.state[id].objects[selected].text, 0, func (c) c == `%` or c == ` `));
                    zkv.setDoubleValue('display-brightness-norm', b * 0.7 / 100);
                },
            },
            {text: 'Light    : ', type: 'normal'},
            {text: '  0 >',
                type: 'editable|immediate|end-of-line',
                choices: ['  0 >', '< 1 >', '< 2 >', '< 3  '],
                callback: func (id, selected) {
                    var l = num(string.trim(me.device.windows.state[id].objects[selected].text, 0, func (c) c == ` ` or c == `<` or c == `>`));
                    zkv.setDoubleValue('lightmap', l * 3);
                }
            },
            {text: 'UNITS', type: 'title'},
            {type: 'separator'},
            {text: 'Pressure  :', type: 'normal'},
            {text: data.settings.units.pressure == 'inhg' ? '  inHg >' : '<  hPa  ',
                type: 'editable|end-of-line',
                choices: [ '  inHg >', '<  hPa  '],
                callback: func (id, selected) {
                    var u = string.trim(me.device.windows.state[id].objects[selected].text, 0, func (c) c == ` ` or c == `<` or c == `>`);
                    if (u == 'inHg')
                        data.settings.units.pressure = 'inhg';
                    else
                        data.settings.units.pressure = 'hpa';
                    me.device.display.updateBARO();
                }
            },
            {text: 'Altitude  :', type: 'normal'},
            {text: units.altitude.from_ft == 1 ? '   feet  >' : '< meters  ',
                type: 'editable|end-of-line',
                choices: [ '   feet  >', '< meters  ' ],
                callback: func (id, selected) {
                    var u = string.trim(me.device.windows.state[id].objects[selected].text, 0, func (c) c == ` ` or c == `<` or c == `>`);
                    if (u == 'feet') {
                        data.settings.units.altitude = 'ft';
                        units.altitude.from_ft = 1;
                        units.altitude.from_m  = M2FT;
                    }
                    else {
                        data.settings.units.altitude = 'm';
                        units.altitude.from_ft = FT2M;
                        units.altitude.from_m  = 1;
                    }
                }
            },
            {text: 'Distance  :', type: 'normal'},
            {text: units.distance.from_nm == 1 ? '  NM >' : '< km  ',
                type: 'editable|end-of-line',
                choices: ['  NM >', '< km  '],
                callback: func (id, selected) {
                    var u = string.trim(me.device.windows.state[id].objects[selected].text, 0, func (c) c == ` ` or c == `<` or c == `>`);
                    if (u == 'NM') {
                        data.settings.units.distance = 'nm';
                        units.distance.from_nm = 1;
                        units.distance.from_m  = M2NM * 1000;
                    }
                    else {
                        data.settings.units.distance = 'km';
                        units.distance.from_nm = NM2M / 1000;
                        units.distance.from_m  = 0.001;
                    }
                }
            },
            {text: 'Speed     :', type: 'normal'},
            {text: units.speed.from_kt == 1 ? '  knots  >' : '<  km/h   ',
                type: 'editable|end-of-line',
                choices: [ '  knots  >', '<  km/h   ' ],
                callback: func (id, selected) {
                    var u = string.trim(me.device.windows.state[id].objects[selected].text, 0, func (c) c == ` ` or c == `<` or c == `>`);
                    if (u == 'knots') {
                        data.settings.units.altitude = 'kt';
                        units.speed.from_kt  = 1;
                        units.speed.from_kmh = MPS2KT / 3.6;
                    }
                    else {
                        data.settings.units.altitude = 'kmh';
                        units.speed.from_kt  = KT2MPS * 3.6;
                        units.speed.from_kmh = 1;
                    }
                    foreach (var v; ['Vx', 'Vy', 'Vr', 'Vglide', 'Vne']) {
                        var speed = alerts.getValue(v) * units.speed.from_kt;
                        data[v] = speed != nil ? speed : 9999;
                    }
                }
            },
            {text: 'Vert. Spd :', type: 'normal'},
            {text: units.vspeed.from_fpm == 1 ? '  ft/min >' : '<  m/min  ',
                type: 'editable|end-of-line',
                choices: [ '  ft/min >', '<  m/min  ' ],
                callback: func (id, selected) {
                    var u = string.trim(me.device.windows.state[id].objects[selected].text, 0, func (c) c == ` ` or c == `<` or c == `>`);
                    if (u == 'ft/min') {
                        data.settings.units.altitude = 'fpm';
                        units.vspeed.from_fpm = 1;
                        units.vspeed.from_mpm = M2FT;
                    }
                    else {
                        data.settings.units.altitude = 'mpm';
                        units.vspeed.from_fpm = FT2M;
                        units.vspeed.from_mpm = 1;
                    }
                }
            },
            {type: 'separator'},
            {text: 'TIME REF: ', type: 'normal'},
            {text: data.settings.time.actual,
                type: 'editable|end-of-line',
                choices: [ '  GMT >', '< LCL >', '< UTC >', '< RL   ' ],
                callback: func (id, selected) {
                    data.settings.time.actual = me.device.windows.state[id].objects[selected].text;
                    data.settings.time.label = string.trim(me.device.windows.state[id].objects[selected].text, 0, func (c) c == ` ` or c == `<` or c == `>`);
                }
            },
        ];
        me.device.windows.draw( windowId, {autogeom: 1}, obj_infos);
        me.device.knobs.FmsOuter = me.device.knobs.NavigateMenu;
        me.device.knobs.FmsInner = me.device.knobs.MenuSettings;
        me.device.buttons.ENT = me.device.buttons.ValidateTMRREF;
        me.device.buttons.CLR = func {
            me.device.windows.del(windowId);
            me.device.knobs.FmsOuter = func;
            me.device.knobs.FmsInner = func;
            foreach (var b; ['ENT', 'CLR'])
                me.device.buttons[b] = func;
            me.device.buttons.MENU = me.device.buttons.GlobalParams;
        }
        me.device.buttons.MENU = me.device.buttons.CLR;
    },

    DirectTo : func (a) {
        if (a) return;
        if (me.device.windows.selected == nil) return;
        var (id, selected) = split('-', me.device.windows.selected);
        var state = me.device.windows.state[id];
        selected += state.scroll.offset;
        var scratch = props.globals.getNode('/instrumentation/gps/scratch');
        if (contains(state.objects[selected], 'dto')) {
            scratch.setValue('longitude-deg', state.objects[selected].dto.lon);
            scratch.setValue('latitude-deg', state.objects[selected].dto.lat);
            scratch.setValue('ident', state.objects[selected].dto.id);
            scratch.setValue('altitude-ft', data.alt);
            setprop('/instrumentation/gps/command', 'direct');
        }
    },

    FPL : func (a) {
        if (a or data.fpSize == 0 or me.device.windows.selected != '')
            return;
        var windowId = 'FLIGHTPLAN LIST';
        var flightplan = props.globals.getNode('/autopilot/route-manager');
        var route = flightplan.getNode('route');
        var obj_infos = [];
        var firstEntry = 1;
        var current = flightplan.getValue('current-wp');
        for (var i = current; i <= data.fpSize; i += 1) {
            var wp = route.getChild('wp', i);
            append(obj_infos, {
                type: (firstEntry ? 'selected' : 'editable'),
                text: sprintf('#%2d %-10s', data.fpSize - i, wp.getValue('id')),
                scrollgroup: i - current,
                dto: {
                    lon: wp.getValue('lon'),
                    lat: wp.getValue('lat'),
                    id:  wp.getValue('id')
                },
            });
            append(obj_infos, {
                type: 'normal|end-of-line',
                scrollgroup: i - current,
                text: sprintf(' %3dNM %3d°',
                              math.round(wp.getValue('leg-distance-nm')),
                              math.round(wp.getValue('leg-bearing-true-deg')))
            });
            firstEntry = 0;
        }
        me.device.windows.draw(windowId, obj_infos, {lines: 6, columns: 2});
        me.device.knobs.FmsOuter = me.device.knobs.NavigateMenu;
        me.device.knobs.FmsInner = me.device.knobs.NavigateMenu;
        me.device.buttons.ENT = func;
        me.device.buttons.MENU = func;
        me.device.buttons.CLR = func {
            me.device.windows.del(windowId);
            if (me.device.role == 'PFD') {
                me.device.knobs.FmsOuter = func;
                me.device.knobs.FmsInner = func;
                me.device.buttons.MENU   = me.device.buttons.GlobalParams;
            }
            else {
                me.device.knobs.FmsInner = func;
                me.device.knobs.FmsOuter = me.device.knobs.MFD_select_page_group;
                me.device.buttons.MENU   = me.device.buttons.MapMenu;
            }
            foreach (var b; ['ENT', 'CLR'])
                me.device.buttons[b] = func;
        }
        me.device.buttons.MENU = me.device.buttons.CLR;
    },

    MENU : void,
    PROC : void,
    CLR : void,
    ENT : void,
    FMS : void,
};