zkv1000 / Nasal / mud.nas /
Sébastien MARQUE commit initial
56c0030 7 years ago
1 contributor
166 lines | 4.985kb
var mudClass = {
    new: func (node, lines=10) {
        var m = { parents: [ mudClass ] };
        m.node = node.getNode('mud', 1);
        m.content = []; # displayed
        m.data = []; # not displayed
        m.callback = []; # callback functions for selected items
        m.node.getNode('displayed', 1).setBoolValue(0);
        m.node.getNode('title', 1).setValue('');
        m.lines = lines;
        for (var i = 0; i < me.lines; i += 1) {
            m.node.getNode('line[' ~ i ~ ']', 1).setValue('');
            m.node.getNode('sel-line[' ~ i ~ ']', 1).setBoolValue(0);
        }
        return m;
    },

    lines: 10,

    total_lines: 0,

    wrap: 0,

    selected: {
        block: 0,
        first_line: 0,
        last_line: 0,
    },

    first: 0,

    last: 0,

    wipe: func {
        me.content = [];
        me.data = [];
        me.callback = [];
        me.selected.block = 0;
        me.selected.first_line = -1;
        me.selected.last_line = -1;
        foreach (var p; me.node.getChildren('line')) {
            p.setValue('');
        }
        me.node.getNode('title').setValue('');
    },

    setTitle: func (t) {
        me.node.getNode('title').setValue(t);
    },

    add: func (content, callback = void, data = nil) {
        append(me.content, content);
        append(me.callback, callback);
        append(me.data, data);
        me.total_lines += size(content);
    },

    open: func (wrap = 0) {
        me.fill();
        me.wrap = wrap;
        me.node.getNode('displayed').setBoolValue(1);
    },

    close: func () {
        me.node.getNode('displayed').setBoolValue(0);
        me.wipe();
        me.wrap = 0;
    },

    fill: func (starting_block = 0) {
        var f = 0;
        var b = 0;
        for (b = starting_block; b < size(me.content); b += 1) {
            var s = size(me.content[b]);
            f + s <= me.lines or break;
            for (var i = 0; i < s; i += 1) {
                var c = f + i;
                me.node.getNode('line[' ~ c ~ ']').setValue(me.content[b][i]);
            }
            f += s;
        }
        me.first = starting_block;
        me.last = b - 1;
    },

    invfill: func (block) {
        var f = me.lines;
        me.last = block;
        for (me.first = block; me.first >= 0; me.first -= 1) {
            var s = size(me.content[me.first]);
            for (var i = size(me.content[me.first]) - 1; i >=0; i -= 1) {
                f -= 1;
                if (f < 0) {
                    me.first += 1;
                    return;
                }
                me.node.getNode('line[' ~ f ~ ']').setValue(me.content[me.first][i]);
            }
        }
    },

    select: func (dir = 0) {
        foreach (var c; me.node.getChildren('sel-line')) c.setBoolValue(0);
        var next = me.selected.block + dir;
        if (next > (size(me.content) - 1)) {
            if (me.wrap) {
                next = 0;
                me.fill(0);
                me.selected.first_line = 0;
                me.selected.last_line = size(me.content[0]) - 1;
            }
            else
                next = size(me.content) - 1;
        }
        elsif (next < 0) {
            if (me.wrap) {
                next = size(me.content) - 1;
                if (me.total_lines > me.lines) {
                    me.invfill(next);
                    me.selected.last_line = me.lines - 1;
                }
                else {
                    me.fill(0);
                    me.selected.last_line = me.total_lines - 1;
                }
                me.selected.first_line = me.selected.last_line - (size(me.content[next]) - 1);
            }
            else
                next = 0;
        }
        elsif (next > me.last) {
            me.invfill(next);
            me.selected.last_line = me.lines - 1;
            me.selected.first_line = me.selected.last_line - (size(me.content[next]) - 1);
        }
        elsif (next < me.first) {
            me.fill(next);
            me.selected.first_line = 0;
            me.selected.last_line = size(me.content[0]) - 1;
        }
        elsif (dir > 0) {
            me.selected.first_line += size(me.content[me.selected.block]);
            me.selected.last_line = me.selected.first_line + size(me.content[next]) - 1;
        }
        elsif (dir < 0) {
            me.selected.last_line = me.selected.first_line - 1;
            me.selected.first_line = me.selected.last_line - (size(me.content[next]) - 1);
        }
        else {
            me.selected.first_line = 0;
            me.selected.last_line = size(me.content[next]) - 1;
        }
        me.selected.block = next;
        for (var i = me.selected.first_line; i <= me.selected.last_line; i += 1) {
            me.node.getNode('sel-line[' ~ i ~ ']').setBoolValue(1);
        }
    },

    getdata: func {
        return me.data[me.selected.block];
    },

    apply: func {
        me.callback[me.selected.block](me.data[me.selected.block]);
    }
};