zkv1000 / Nasal / display.nas /
Sébastien MARQUE AI disponible
3649b6f 7 years ago
1 contributor
131 lines | 4.382kb
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();
            me.loadGroup({ show : [
                    'SoftKeysTexts', 
                    'COMM', 
                    'NAV', 
                    'XPDR-TIME', 
                    'FlightInstruments',
                    'Horizon',
            ]});
            me.updateAi(getprop('/orientation/roll-deg'),getprop('orientation/pitch-deg'));
            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);
            }
            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)",
    },

    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);
        settimer(func me.updateAi(getprop('/orientation/roll-deg'),getprop('orientation/pitch-deg')), 0.1);
    },
};