zkv1000 / Nasal / menu.nas /
Sébastien MARQUE add window management
b5524f6 7 years ago
1 contributor
155 lines | 5.275kb
var pageClass = {
    new : func (d) {
        var m = { parents : [pageClass] };
        m.page = d.display.screen;
        m.window = {};
        m.state = {};
        m.selected = '';
        return m;
    },

    del : func (id = nil) {
        if (id != nil and typeof(v) == 'scalar') {
            delete(me.state, id);
            id = [ id ];
        }
        else {
            foreach (var s; keys(me.state))
                delete(me.state, s);
            id = keys(me.window);
        }
        foreach (var w; id) {
            me.window[w]
                .hide()
                .del();
            delete(me.window, w);
        }
    },

    _selected_text : func (id, text, x, y) {
        me.selected = id;
        me.window[id]
            .setFontSize(16)
            .setFont('LiberationFonts/LiberationMono-Regular.ttf')
            .setTranslation(x, y)
            .setDrawMode(0x01 + 0x04)
            .setText(text)
            .setColorFill(0,1,1)
            .setColor(0,0,0);
    },

    _editable_text : func (id, text, x, y) {
        me.window[id]
            .setFontSize(16)
            .setFont('LiberationFonts/LiberationMono-Regular.ttf')
            .setTranslation(x, y)
            .setText(text)
            .setColorFill(0,0,0)
            .setColor(0,1,1);
    },

    _normal_text : func (id, text, x, y) {
        me.window[id]
            .setFontSize(16)
            .setFont('LiberationFonts/LiberationMono-Regular.ttf')
            .setTranslation(x, y)
            .setText(text)
            .setColorFill(0,0,0)
            .setColor(1,1,1);
    },

    _title_text : func (id, text, x, y) {
        me.window[id]
            .setFontSize(16)
            .setFont('LiberationFonts/LiberationMono-Regular.ttf')
            .setTranslation(x, y)
            .setAlignment('center-center')
            .setText(text)
            .setColorFill(0,0,0)
            .setColor(0,1,1);
    },

    fill : func (id) {
        var state = me.state[id];
        forindex (var line; state.lines) {
            if (find('separator', state.lines[line].type) > -1) {
                me.window[id ~ '-' ~ line] = me.page.createChild('path')
                    .setStrokeLineWidth(1)
                    .moveTo(state.x_base, state.geometry.y - 12)
                    .horiz(state.geometry.w - 20)
                    .setColor(1,1,1);
                state.geometry.x = state.x_base;
                state.geometry.y += 8;
            }
            else {
                me.window[id ~ '-' ~ line] = me.page.createChild('text');
                if (find('normal', state.lines[line].type) > -1)
                    me._normal_text(
                            id ~ '-' ~ line,
                            state.lines[line].text,
                            state.geometry.x,
                            state.geometry.y,
                            );

                elsif (find('selected', state.lines[line].type) > -1)
                    me._selected_text(
                            id ~ '-' ~ line,
                            state.lines[line].text,
                            state.geometry.x,
                            state.geometry.y,
                            );

                elsif (find('editable', state.lines[line].type) > -1)
                    me._editable_text(
                            id ~ '-' ~ line,
                            state.lines[line].text,
                            state.geometry.x,
                            state.geometry.y,
                            );

                elsif (find('title', state.lines[line].type) > -1)
                    me._title_text(
                            id ~ '-' ~ line,
                            state.lines[line].text,
                            state.x_base - 10 + state.geometry.w / 2,
                            state.geometry.y
                        );

                if (find('end-of-line', state.lines[line].type) > -1
                or  find('title', state.lines[line].type) > -1) {
                    state.geometry.x = state.x_base;
                    state.geometry.y += 24;
                }
                else
                    state.geometry.x += size(state.lines[line].text) * 10 + 8;
            }
        }
    },
    
    draw : func (id, geometry, lines) {
        if (contains(me.window, id ~ '-bg')) {
            printlog('debug', 'objet ' ~ id ~ ' already exists');
            return;
        }
        if (!contains(geometry, 'h') and !contains(geometry, 'l')) {
            printlog('debug', 'missing parameter l or h');
            return;
        }
        me.state[id] = {
            lines: lines, 
            geometry: geometry,
            x_base : geometry.x + 10,
            h_max : contains(geometry, 'h') ? h : geometry.l * 24 + 8 + geometry.sep * 16,
        };
        me.state[id].y_max = me.state[id].h_max + me.state[id].geometry.y;
        me.window[id ~ '-bg'] = me.page.createChild('path');
        me.window[id ~ '-bg']
            .rect(geometry.x, geometry.y,
                  geometry.w, me.state[id].h_max)
            .setColor(1,1,1)
            .setColorFill(0,0,0);
        me.state[id].geometry.x += 10;
        me.state[id].geometry.y += 16;
        me.fill(id);
    },
};