zkv1000 / Nasal / softkeys.nas /
c33293b 7 years ago
1 contributor
104 lines | 2.838kb
var softkeysClass = {
    new : func (device, node, role) {
        var m = { parents: [ softkeysClass ] };
        m.node = node;
        m.device = device;
        m.role = role;
        m.path = [];
        return m;
    },

    clean : func {
        # deletes unsed bindings
        foreach (var b; keys(me.bindings))
            if (b != me.role)
                delete(me.bindings, b);
    },

    SoftKey : func (n, a) {
        # released key not yet managed
        if (a == 1)
            return;

        var key = me.device.display.screenElements[sprintf("SoftKey%02i-text",n)].get('text');
        if (key == '' or key == nil)
            return;

        var path = keyMap[me.role];
        foreach(var p; me.path) {
            if (contains(path, p))
                path = path[p];
            else
                break;
        }

        var bindings = me.bindings[me.role];
        foreach(var p; me.path) {
            if (contains(bindings, p))
                bindings = bindings[p];
            else
                break;
        }

        if (contains(path, key)) {
            append(me.path, key);
            me.device.display.updateSoftKeys();
        }
        elsif (contains(bindings, key)) {
            call(bindings[key], [], me);
        }
        elsif (key == 'BACK') {
            pop(me.path);
            me.device.display.updateSoftKeys();
        }
        else
            print(me.device.role ~ '/' ~ key ~ ' : not yet implemented');
    },

    bindings : {
        PFD : {
            INSET: {
                OFF: func {
                    pop(me.path);
                    me.device.display.updateSoftKeys();
                },
            },
            PFD: {
                'STD BARO': func {
                    pop(me.path);
                    me.device.display.updateSoftKeys();
                },
            },
            XPDR: {
                CODE: {
                    IDENT: func {
                        me.path = [];
                        me.device.display.updateSoftKeys();
                    },
                    BACK: func {
                        me.path = [];
                        me.device.display.updateSoftKeys();
                    },
                },
            },
        },
        MFD : {
            ENGINE: {
                FUEL: {
                    UNDO: func {
                        pop(me.path);
                        me.device.display.updateSoftKeys();
                    },
                    ENTER: func {
                        pop(me.path);
                        me.device.display.updateSoftKeys();
                    },
                },
                ENGINE: func {
                    me.path = [];
                    me.device.display.updateSoftKeys();
                },
            },
        },
    },
};