zkv1000 / Nasal / display.nas /
0798b5c 7 years ago
1 contributor
482 lines | 18.39kb
var displayClass = {
    new: func(node, role) {
        var m = { parents: [ displayClass ] };
        
        m.display = canvas.new({
                "name"      : role,
                "size"      : [1024, 768],
                "view"      : [1024, 768],
                "mipmapping": 1
        });
        m.display.addPlacement({
                "node": "Screen",
                "parent": role
        });
        m.display.setColorBackground(0,0,0);
        m.role = role;
        m.screenElements = {};

        return m;
    },

    loadsvg : func () {
        me.screen = me.display.createGroup();
        me.screen.hide();
        canvas.parsesvg(me.screen, "Aircraft/Instruments-3d/zkv1000/Systems/screen.svg");
    },

    _showInitProgress : func (p,t) {
        p.setText(t);
        if (zkv.getNode(me.role ~ 'init').getValue() != 0) {
            if (size(t) >= 10) t = '';
            settimer(func { me._showInitProgress(p, t ~ '.'); }, 0.1);
        }
        else {
            me.progress.hide();
            me.screen.show();
            var groups = {
                show : [
                    'SoftKeysTexts', 
                    'COMM', 
                    'NAV', 
                ],
                hide : [ ],
                text: [
                    'nav1-standby-freq', 'nav1-selected-freq', 'nav1-id',
                    'nav2-standby-freq', 'nav2-selected-freq', 'nav2-id',
                    'nav-freq-switch',
                ],
                clip: [ ],
            };
            if (me.role == 'PFD') {
                append(groups.show,
                    'XPDR-TIME', 
                    'FlightInstruments',
                    'Horizon',
                    'bankPointer',
                    'VSI',
                    'Rose',
                );
                append(groups.hide,
                    'CDI',
                    'NAV1-pointer',
                    'NAV2-pointer',
                    'GPS-pointer',
                    'Bearing1',
                    'Bearing2',
                );
                append(groups.clip,
                    'SpeedLint1',
                    'SpeedTape',
                    'LintAlt',
                    'AltLint00011'
                );
                append(groups.text,
                    'VSIText',
                    'Speed110',
                    'Alt11100',
                    'HDG-text',
                    'AltBigC', 'AltSmallC'
                );
                for (var place = 1; place <= 6; place +=1) {
                    append(groups.text,
                        'AltBigU' ~ place,
                        'AltSmallU' ~ place,
                        'AltBigD' ~ place,
                        'AltSmallD' ~ place
                    );
                }
            }
            else
                append(groups.show, 'Header');

            me.loadGroup(groups);

            if (me.role == 'PFD') {
                me.updateAI(getprop('/orientation/roll-deg'),getprop('orientation/pitch-deg'));
                me.updateVSI(getprop('/instrumentation/vertical-speed-indicator/indicated-speed-fpm'));
                me.updateIAS(getprop('/velocities/airspeed-kt'));
                me.updateALT(getprop('instrumentation/altimeter/indicated-altitude-ft'));
                me.updateHSI(getprop('orientation/heading-deg'));
            }
            me.updateNAV({refresh:1, auto:1});
            me.progress.removeAllChildren();
            me.progress = nil;
            me.showInitProgress = nil;
            me._showInitProgress = nil;
            zkv.removeChild(me.role ~ 'init');
        }
    },

    showInitProgress : func (role) {
        me.progress = me.display.createGroup();
        me.progress.show();
        me.progress.createChild("text", role ~ " title")
            .setTranslation(512, 384)
            .setAlignment("center-center")
            .setFont("LiberationFonts/LiberationSans-Italic.ttf")
            .setFontSize(64, 1)
            .setColor(1,1,1)
            .setText("ZKV1000 " ~ role ~ " init");

        zkv.getNode(role ~ 'init',1).setIntValue(1);

        me._showInitProgress(me.progress.createChild("text", role ~ "progress")
            .setTranslation(512, 484)
            .setAlignment("center-center")
            .setFont("LiberationFonts/LiberationSans-Bold.ttf")
            .setFontSize(128, 1)
            .setColor(1,0,0), '.');
    },

    loadGroup : func (h) {
        if (typeof(h) != 'hash') {
            msg_dbg(sprintf("%s need a hash, but get a %s from %s",
                    caller(0)[0],
                    typeof(h),
                    caller(1)[0]));
            return;
        }
        var setMethod = func (e, t) {
            if (t == 'hide')
                me.screenElements[e].hide();
            elsif (t == 'show')
                me.screenElements[e].show();
            elsif (t == 'rot' or t == 'trans') {
                if (! contains(me.screenElements[e], t))
                    me.screenElements[e][t] = me.screenElements[e].createTransform();
            }
            elsif (t == 'clip') {
                if (contains(me.clips, e))
                    me.screenElements[e].set("clip", me.clips[e]);
                else
                    print('no defined clip for ' ~ e);
            }
            elsif (t == 'text') {
                if (contains(me.texts, e)) {
                    if (contains(me.texts[e], 'alignment'))
                        me.screenElements[e].setAlignment(me.texts[e].alignment);
                    if (contains(me.texts[e], 'default'))
                        me.screenElements[e].setText(me.texts[e].default);
                    if (contains(me.texts[e], 'color'))
                        me.screenElements[e].setColor(me.texts[e].color);
                }
#                else
#                    print('no text format for ' ~ e);
            }
            else
                print('unknown method ' ~ t);
        };
        foreach (var todo; keys(h)) {
            if (typeof(h[todo]) != 'vector') h[todo] = [ h[todo] ];
            foreach (var id; h[todo]) {
                if (! contains(me.screenElements, id)) {
                    me.screenElements[id] = me.screen.getElementById(id);
                    if (me.screenElements[id] != nil)
                        setMethod(id, todo);
                    else
                        print('SVG ID ' ~ id ~ ' not found');
                }
                else
                    setMethod(id, todo);
            }
        }
    },

    clips : {
        PitchScale   : "rect(70,664,370,256)",
        SpeedLint1   : "rect(252,226,318,204)",
        SpeedTape    : "rect(115,239,455,156)",
        LintAlt      : "rect(115,808,455,704)",
        AltLint00011 : "rect(252,804,318,771)",
    },

    texts : {
        VSIText : {
            alignment: "right-bottom", default : num('0'),
        },
        Speed110 : {
            alignment : 'left-bottom'
        },
        Alt11100 : {
            alignment:'left-bottom'
        },
        "HDG-text" : {
            default: '---°'
        },
        'nav1-standby-freq' : {
            color: [0, 1, 1],
        },
        'nav1-id' : {
            default: ''
        },
        'nav2-id' : {
            default: ''
        },
    },

    updateAI: func(roll,pitch){
        if (pitch > 80)
            pitch = 80;
        elsif (pitch < -80)
            pitch = -80;
        me.screenElements.Horizon
            .setRotation(-roll * D2R)
            .setTranslation(0, pitch * 6.8571428);
        me.screenElements.bankPointer
            .setRotation(-roll * D2R);
        settimer(func me.updateAI(getprop('/orientation/roll-deg'),getprop('orientation/pitch-deg')), 0.1);
    },

    updateVSI: func (vsi) {
        me.screenElements.VSIText
            .setText(num(math.round(vsi, 10)));
        if (vsi > 4500)
            vsi = 4500;
        elsif (vsi < -4500)
            vsi = -4500;
        me.screenElements.VSI
            .setTranslation(0, vsi * -0.03465);
        settimer(func me.updateVSI(getprop('/instrumentation/vertical-speed-indicator/indicated-speed-fpm')), 0.1);
    },

    updateIAS: func (ias) {
        if (ias >= 10)
            me.screenElements.Speed110
                .setText(sprintf("% 2u",num(math.floor(ias/10))));
        else
            me.screenElements.Speed110
                .setText('');
        me.screenElements.SpeedLint1
            .setTranslation(0,(math.mod(ias,10) + (ias >= 10)*10) * 36);
        me.screenElements.SpeedTape
            .setTranslation(0,ias * 5.711);
        settimer(func me.updateIAS(getprop('/velocities/airspeed-kt')), 0.1);
    },

    updateALT: func (alt) {
        if (alt < 0)
            me.screenElements.Alt11100
                .setText(sprintf("% 3i",math.ceil(alt/100)));
        elsif (alt < 100)
            me.screenElements.Alt11100
                .setText('');
        else
            me.screenElements.Alt11100
                .setText(sprintf("% 3i",math.floor(alt/100)));
        me.screenElements.AltLint00011
            .setTranslation(0,math.fmod(alt,100) * 1.24);

        # From Farmin/G1000 http://wiki.flightgear.org/Project_Farmin/FG1000
        if (alt> -1000 and alt< 1000000) {
            var Offset10 = 0;
            var Offset100 = 0;
            var Offset1000 = 0;
            if (alt< 0) {
                var Ne = 1;
                var alt= -alt;
            }
            else
                var Ne = 0;

            var Alt10       = math.mod(alt,100);
            var Alt100      = int(math.mod(alt/100,10));
            var Alt1000     = int(math.mod(alt/1000,10));
            var Alt10000    = int(math.mod(alt/10000,10));
            var Alt20       = math.mod(Alt10,20)/20;
            if (Alt10 >= 80)
                var Alt100 += Alt20;

            if (Alt10 >= 80 and Alt100 >= 9)
                var Alt1000 += Alt20;

            if (Alt10 >= 80 and Alt100 >= 9 and Alt1000 >= 9)
                var Alt10000 += Alt20;

            if (alt> 100)
                var Offset10 = 100;

            if (alt> 1000)
                var Offset100 = 10;

            if (alt> 10000)
                var Offset1000 = 10;

            if (!Ne) {
                me.screenElements.LintAlt.setTranslation(0,(math.mod(alt,100))*0.57375);
                var altCentral = (int(alt/100)*100);
            }
            elsif (Ne) {
                me.screenElements.LintAlt.setTranslation(0,(math.mod(alt,100))*-0.57375);
                var altCentral = -(int(alt/100)*100);
            }
            me.screenElements["AltBigC"].setText("");
            me.screenElements["AltSmallC"].setText("");
            for (var place = 1; place <= 6; place += 1) {
                var altUP = altCentral + (place*100);
                var offset = -30.078;
                if (altUP < 0) {
                    var altUP = -altUP;
                    var prefix = "-";
                    var offset += 15.039;
                }
                else
                    var prefix = "";

                if (altUP == 0) {
                    var AltBigUP    = "";
                    var AltSmallUP  = "0";

                }
                elsif (math.mod(altUP,500) == 0 and altUP != 0) {
                    var AltBigUP    = sprintf(prefix~"%1d", altUP);
                    var AltSmallUP  = "";
                }
                elsif (altUP < 1000 and (math.mod(altUP,500))) {
                    var AltBigUP    = "";
                    var AltSmallUP  = sprintf(prefix~"%1d", int(math.mod(altUP,1000)));
                    var offset = -30.078;
                }
                elsif ((altUP < 10000) and (altUP >= 1000) and (math.mod(altUP,500))) {
                    var AltBigUP    = sprintf(prefix~"%1d", int(altUP/1000));
                    var AltSmallUP  = sprintf("%1d", int(math.mod(altUP,1000)));
                    var offset += 15.039;
                }
                else {
                    var AltBigUP    = sprintf(prefix~"%1d", int(altUP/1000));
                    var mod = int(math.mod(altUP,1000));
                    var AltSmallUP  = sprintf("%1d", mod);
                    var offset += 30.078;
                }

                me.screenElements["AltBigU"~place].setText(AltBigUP);
                me.screenElements["AltSmallU"~place].setText(AltSmallUP);
                me.screenElements["AltSmallU"~place].setTranslation(offset,0);
                var altDOWN = altCentral - (place*100);
                var offset = -30.078;
                if (altDOWN < 0) {
                    var altDOWN = -altDOWN;
                    var prefix = "-";
                    var offset += 15.039;
                }
                else
                    var prefix = "";

                if (altDOWN == 0) {
                    var AltBigDOWN  = "";
                    var AltSmallDOWN    = "0";
                }
                elsif (math.mod(altDOWN,500) == 0 and altDOWN != 0) {
                    var AltBigDOWN  = sprintf(prefix~"%1d", altDOWN);
                    var AltSmallDOWN    = "";
                }
                elsif (altDOWN < 1000 and (math.mod(altDOWN,500))) {
                    var AltBigDOWN  = "";
                    var AltSmallDOWN    = sprintf(prefix~"%1d", int(math.mod(altDOWN,1000)));
                    var offset = -30.078;
                }
                elsif ((altDOWN < 10000) and (altDOWN >= 1000) and (math.mod(altDOWN,500))) {
                    var AltBigDOWN  = sprintf(prefix~"%1d", int(altDOWN/1000));
                    var AltSmallDOWN    = sprintf("%1d", int(math.mod(altDOWN,1000)));
                    var offset += 15.039;
                }
                else {
                    var AltBigDOWN  = sprintf(prefix~"%1d", int(altDOWN/1000));
                    var mod = int(math.mod(altDOWN,1000));
                    var AltSmallDOWN    = sprintf("%1d", mod);
                    var offset += 30.078;
                }
                me.screenElements["AltBigD"~place].setText(AltBigDOWN);
                me.screenElements["AltSmallD"~place].setText(AltSmallDOWN);
                me.screenElements["AltSmallD"~place].setTranslation(offset,0);
            }
        }
        settimer(func me.updateALT(getprop('instrumentation/altimeter/indicated-altitude-ft')), 0.2);
    },

    updateHSI : func (hdg) {
        me.screenElements.Rose
            .setRotation(-hdg * D2R);
        me.screenElements['HDG-text']
            .setText(sprintf("%03u°", hdg));
        settimer(func me.updateHSI(getprop('orientation/heading-deg')), 0.1);
    },

    updateNAV : func {
        # made active via menu
        if (contains(arg[0], "active")) {
            if (arg[0]['active'] == 'none') {
                me.screenElements['nav1-id']
                    .setColor(1,1,1);
                me.screenElements['nav1-selected-freq']
                    .setColor(1,1,1);
                me.screenElements['nav2-id']
                    .setColor(1,1,1);
                me.screenElements['nav2-selected-freq']
                    .setColor(1,1,1);
                me.screenElements['NAV1-pointer']
                    .hide();
                me.screenElements['NAV2-pointer']
                    .hide();
            }
            else {
                var inactive = (arg[0]['active'] == 1) + 1;
                me.screenElements['nav' ~ arg[0]['active'] ~ '-id']
                    .setColor(0,1,0);
                me.screenElements['nav' ~ arg[0]['active'] ~ '-selected-freq']
                    .setColor(0,1,0);
                me.screenElements['NAV' ~ arg[0]['active'] ~ '-pointer']
                    .show();
#                me.screenElements['HDI']
#                    .setRotation();
                me.screenElements['nav' ~ inactive ~ '-id']
                    .setColor(1,1,1);
                me.screenElements['nav' ~ inactive ~ '-selected-freq']
                    .setColor(1,1,1);
#                me.screenElements['NAV' ~ inactive ~ '-pointer']
#                    .hide();
#                foreach (var e; [ 'FROM', 'TO', 'CDI' ])
#                    me.screenElements['NAV' ~ inactive ~ '-' ~ e]
#                        .hide();
            }
        }
        if (contains(arg[0], 'tune')) {
            var n = getprop('/instrumentation/zkv1000/radios/nav-tune');
            # n = 0 -> NAV1
            # n = 1 -> NAV2
            me.screenElements['nav-freq-switch']
                .setTranslation(0, n * 25);
            me.screenElements['nav' ~ (n + 1) ~ '-standby-freq']
                .setColor(0,1,1);
            me.screenElements['nav' ~ ((n == 0) + 1) ~ '-standby-freq']
                .setColor(1,1,1);
        }
        if (contains(arg[0], 'nav-id')) {
            # TODO: récupérer la valeur via les paramètres transmis du listener
            var navid = getprop('/instrumentation/nav[' ~ (arg[0]['nav-id'] - 1) ~ ']/nav-id');
            if (navid == nil)
                navid = '';
            me.screenElements["nav" ~ arg[0]['nav-id'] ~ "-id"]
                    .setText(navid); # veut pas exister au début...
        }
        if (contains(arg[0], 'refresh')) {
            # rafraichi une seule ligne NAV1 ou NAV2
            me.screenElements['nav' ~ arg[0].refresh ~ '-selected-freq']
                .setText(getprop('/instrumentation/nav[' ~ (arg[0].refresh - 1) ~ ']/frequencies/selected-mhz-fmt'));
            me.screenElements['nav' ~ arg[0].refresh ~ '-standby-freq']
                .setText(getprop('/instrumentation/nav[' ~ (arg[0].refresh - 1) ~ ']/frequencies/standby-mhz-fmt'));
        }
        if (contains(arg[0], 'set')) {
            # positionne la valeur modifiée, les listeners "trigguent" en permanence ces propriétés, donc exit
            var n = getprop('/instrumentation/zkv1000/radios/nav-tune');
            me.screenElements['nav' ~ (n + 1) ~ '-standby-freq']
                .setText(getprop('/instrumentation/nav[' ~ n ~ ']/frequencies/standby-mhz-fmt'));
        }
        if (contains(arg[0], 'auto')) {
            # pour rafraichir automagiquement, toutes les deux secondes un refresh pour un NAV
            me.updateNAV({refresh: 1});
            settimer(func me.updateNAV({refresh: 2}), 1);
            settimer(func me.updateNAV({auto:1}),     2);
        }
    },
};