zkv1000 / Nasal / mud.nas /
Newer Older
166 lines | 4.985kb
commit initial
Sébastien MARQUE authored on 2017-03-07
1
var mudClass = {
2
    new: func (node, lines=10) {
3
        var m = { parents: [ mudClass ] };
4
        m.node = node.getNode('mud', 1);
5
        m.content = []; # displayed
6
        m.data = []; # not displayed
7
        m.callback = []; # callback functions for selected items
8
        m.node.getNode('displayed', 1).setBoolValue(0);
9
        m.node.getNode('title', 1).setValue('');
10
        m.lines = lines;
11
        for (var i = 0; i < me.lines; i += 1) {
12
            m.node.getNode('line[' ~ i ~ ']', 1).setValue('');
13
            m.node.getNode('sel-line[' ~ i ~ ']', 1).setBoolValue(0);
14
        }
15
        return m;
16
    },
17

            
18
    lines: 10,
19

            
20
    total_lines: 0,
21

            
22
    wrap: 0,
23

            
24
    selected: {
25
        block: 0,
26
        first_line: 0,
27
        last_line: 0,
28
    },
29

            
30
    first: 0,
31

            
32
    last: 0,
33

            
34
    wipe: func {
35
        me.content = [];
36
        me.data = [];
37
        me.callback = [];
38
        me.selected.block = 0;
39
        me.selected.first_line = -1;
40
        me.selected.last_line = -1;
41
        foreach (var p; me.node.getChildren('line')) {
42
            p.setValue('');
43
        }
44
        me.node.getNode('title').setValue('');
45
    },
46

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

            
51
    add: func (content, callback = void, data = nil) {
52
        append(me.content, content);
53
        append(me.callback, callback);
54
        append(me.data, data);
55
        me.total_lines += size(content);
56
    },
57

            
58
    open: func (wrap = 0) {
59
        me.fill();
60
        me.wrap = wrap;
61
        me.node.getNode('displayed').setBoolValue(1);
62
    },
63

            
64
    close: func () {
65
        me.node.getNode('displayed').setBoolValue(0);
66
        me.wipe();
67
        me.wrap = 0;
68
    },
69

            
70
    fill: func (starting_block = 0) {
71
        var f = 0;
72
        var b = 0;
73
        for (b = starting_block; b < size(me.content); b += 1) {
74
            var s = size(me.content[b]);
75
            f + s <= me.lines or break;
76
            for (var i = 0; i < s; i += 1) {
77
                var c = f + i;
78
                me.node.getNode('line[' ~ c ~ ']').setValue(me.content[b][i]);
79
            }
80
            f += s;
81
        }
82
        me.first = starting_block;
83
        me.last = b - 1;
84
    },
85

            
86
    invfill: func (block) {
87
        var f = me.lines;
88
        me.last = block;
89
        for (me.first = block; me.first >= 0; me.first -= 1) {
90
            var s = size(me.content[me.first]);
91
            for (var i = size(me.content[me.first]) - 1; i >=0; i -= 1) {
92
                f -= 1;
93
                if (f < 0) {
94
                    me.first += 1;
95
                    return;
96
                }
97
                me.node.getNode('line[' ~ f ~ ']').setValue(me.content[me.first][i]);
98
            }
99
        }
100
    },
101

            
102
    select: func (dir = 0) {
103
        foreach (var c; me.node.getChildren('sel-line')) c.setBoolValue(0);
104
        var next = me.selected.block + dir;
105
        if (next > (size(me.content) - 1)) {
106
            if (me.wrap) {
107
                next = 0;
108
                me.fill(0);
109
                me.selected.first_line = 0;
110
                me.selected.last_line = size(me.content[0]) - 1;
111
            }
112
            else
113
                next = size(me.content) - 1;
114
        }
115
        elsif (next < 0) {
116
            if (me.wrap) {
117
                next = size(me.content) - 1;
118
                if (me.total_lines > me.lines) {
119
                    me.invfill(next);
120
                    me.selected.last_line = me.lines - 1;
121
                }
122
                else {
123
                    me.fill(0);
124
                    me.selected.last_line = me.total_lines - 1;
125
                }
126
                me.selected.first_line = me.selected.last_line - (size(me.content[next]) - 1);
127
            }
128
            else
129
                next = 0;
130
        }
131
        elsif (next > me.last) {
132
            me.invfill(next);
133
            me.selected.last_line = me.lines - 1;
134
            me.selected.first_line = me.selected.last_line - (size(me.content[next]) - 1);
135
        }
136
        elsif (next < me.first) {
137
            me.fill(next);
138
            me.selected.first_line = 0;
139
            me.selected.last_line = size(me.content[0]) - 1;
140
        }
141
        elsif (dir > 0) {
142
            me.selected.first_line += size(me.content[me.selected.block]);
143
            me.selected.last_line = me.selected.first_line + size(me.content[next]) - 1;
144
        }
145
        elsif (dir < 0) {
146
            me.selected.last_line = me.selected.first_line - 1;
147
            me.selected.first_line = me.selected.last_line - (size(me.content[next]) - 1);
148
        }
149
        else {
150
            me.selected.first_line = 0;
151
            me.selected.last_line = size(me.content[next]) - 1;
152
        }
153
        me.selected.block = next;
154
        for (var i = me.selected.first_line; i <= me.selected.last_line; i += 1) {
155
            me.node.getNode('sel-line[' ~ i ~ ']').setBoolValue(1);
156
        }
157
    },
158

            
159
    getdata: func {
160
        return me.data[me.selected.block];
161
    },
162

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