zkv1000 / Nasal / buttons.nas /
0d37dc2 4 years ago
1 contributor
211 lines | 8.986kb
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 = me.device.windows.state[id].objects[selected].text;
                    foreach (var c; [' ', '<', '>'])
                        l = string.replace(l, c, '');
                    zkv.setDoubleValue('lightmap', num(l) * 3);
                }
            }
        ];
        var lines = size(obj_infos);
        var name_max_length = 20;
        me.device.windows.draw(
            windowId,
            {x: 1014 - (name_max_length * 10), y: 758 - ((lines + 3) * 24), w: name_max_length * 10, l: lines, sep: 1},
            obj_infos
        );
        me.device.knobs.FmsOuter = me.device.knobs.NavigateMenu;
        me.device.knobs.FmsInner = me.device.knobs.MenuSettings;
        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.ENT  = me.device.buttons.CLR;
        me.device.buttons.MENU = me.device.buttons.CLR;
    },

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