zkv1000 / Nasal / menu.nas /
Newer Older
270 lines | 10.809kb
add window management
Sébastien MARQUE authored on 2017-03-28
1
var pageClass = {
2
    new : func (d) {
3
        var m = { parents : [pageClass] };
fix issue #2
Sébastien MARQUE authored on 2017-04-23
4
        m.page = d.display.display.createGroup().set('z-index', 100);
add window management
Sébastien MARQUE authored on 2017-03-28
5
        m.window = {};
6
        m.state = {};
7
        m.selected = '';
8
        return m;
9
    },
10

            
poweroff improved
Sébastien MARQUE authored on 2020-05-16
11
    off: func {
12
        me.del();
13
        me.page.removeAllChildren();
14
    },
15

            
add window management
Sébastien MARQUE authored on 2017-03-28
16
    del : func (id = nil) {
fixes some bugs in windows
Sébastien MARQUE authored on 2017-04-12
17
        if (id != nil and typeof(id) == 'scalar') {
add window management
Sébastien MARQUE authored on 2017-03-28
18
            delete(me.state, id);
fix deletion of specific win...
Sébastien MARQUE authored on 2017-04-24
19
            var _id = id ~ '-';
20
            id = [];
21
            foreach (var w; keys(me.window))
22
                if (find(_id, w) == 0)
23
                    append(id, w);
add window management
Sébastien MARQUE authored on 2017-03-28
24
        }
25
        else {
26
            foreach (var s; keys(me.state))
27
                delete(me.state, s);
28
            id = keys(me.window);
29
        }
30
        foreach (var w; id) {
31
            me.window[w]
32
                .hide()
33
                .del();
34
            delete(me.window, w);
35
        }
36
    },
37

            
38
    _selected_text : func (id, text, x, y) {
39
        me.selected = id;
40
        me.window[id]
41
            .setFontSize(16)
42
            .setFont('LiberationFonts/LiberationMono-Regular.ttf')
43
            .setTranslation(x, y)
inverted color... the come b...
Sébastien MARQUE authored on 2020-05-15
44
            .setDrawMode(canvas.Text.TEXT + canvas.Text.FILLEDBOUNDINGBOX)
add window management
Sébastien MARQUE authored on 2017-03-28
45
            .setText(text)
inverted color... the come b...
Sébastien MARQUE authored on 2020-05-15
46
            .setColorFill(0,1,1)
47
            .setColor(0,0,0);
add window management
Sébastien MARQUE authored on 2017-03-28
48
    },
49

            
50
    _editable_text : func (id, text, x, y) {
51
        me.window[id]
52
            .setFontSize(16)
53
            .setFont('LiberationFonts/LiberationMono-Regular.ttf')
54
            .setTranslation(x, y)
inverted color... the come b...
Sébastien MARQUE authored on 2020-05-15
55
            .setDrawMode(canvas.Text.TEXT + canvas.Text.FILLEDBOUNDINGBOX)
add window management
Sébastien MARQUE authored on 2017-03-28
56
            .setText(text)
57
            .setColorFill(0,0,0)
58
            .setColor(0,1,1);
59
    },
60

            
61
    _normal_text : func (id, text, x, y) {
62
        me.window[id]
63
            .setFontSize(16)
64
            .setFont('LiberationFonts/LiberationMono-Regular.ttf')
65
            .setTranslation(x, y)
66
            .setText(text)
67
            .setColorFill(0,0,0)
68
            .setColor(1,1,1);
69
    },
70

            
71
    _title_text : func (id, text, x, y) {
72
        me.window[id]
73
            .setFontSize(16)
74
            .setFont('LiberationFonts/LiberationMono-Regular.ttf')
75
            .setTranslation(x, y)
76
            .setAlignment('center-center')
77
            .setText(text)
78
            .setColorFill(0,0,0)
79
            .setColor(0,1,1);
80
    },
81

            
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
82
    fill : func (id, scroll = nil) {
add window management
Sébastien MARQUE authored on 2017-03-28
83
        var state = me.state[id];
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
84
        state.scroll = {
85
            offset : 0,           # offset between canvas element and state element
86
            last : 9999,          # last scrollgroup
87
            begin: -9999,         # first canvas element of the scrolling area
88
            end: 9999,            # last canvas element of the scrolling area
89
            upper: -9999,         # group printed on the top of the scrolling area
90
            lower: 9999,          # group printed at the bottom of the scrolling area
fixes some bugs in windows
Sébastien MARQUE authored on 2017-04-12
91
            lines : scroll != nil ? scroll.lines : 0, # number of lines for the scrolling area
92
            columns : scroll != nil ? scroll.columns : 0, # number of objects on each scrolling lines
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
93
        };
94
        var scrollgroup = {};
95
        forindex (var line; state.objects) {
96
            if (find('separator', state.objects[line].type) > -1) {
97
                me.window[id ~ '-' ~ (line - state.scroll.offset)] = me.page.createChild('path')
add window management
Sébastien MARQUE authored on 2017-03-28
98
                    .setStrokeLineWidth(1)
99
                    .moveTo(state.x_base, state.geometry.y - 12)
100
                    .horiz(state.geometry.w - 20)
101
                    .setColor(1,1,1);
102
                state.geometry.x = state.x_base;
103
                state.geometry.y += 8;
104
            }
105
            else {
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
106
                if (contains(state.objects[line], 'scrollgroup')) {
107
                    state.scroll.last = state.objects[line].scrollgroup;
108
                    scrollgroup[state.objects[line].scrollgroup] = 1;
109
                    if (state.scroll.begin == -9999) {
110
                        state.scroll.begin = line;
111
                        state.scroll.upper = state.objects[line].scrollgroup;
112
                    }
113
                    if (size(keys(scrollgroup)) > state.scroll.lines) {
114
                        if (state.scroll.end == 9999) {
115
                            state.scroll.end = line - 1;
116
                            state.scroll.lower = state.objects[line - 1].scrollgroup;
117
                        }
118
                        else
119
                            state.scroll.last = state.objects[line].scrollgroup;
120
                        state.scroll.offset += 1;
121
                        continue;
122
                    }
123
                }
124
                me.window[id ~ '-' ~ (line - state.scroll.offset)] = me.page.createChild('text');
type normal for text is defa...
Sébastien MARQUE authored on 2017-04-12
125
                if (find('selected', state.objects[line].type) > -1)
add window management
Sébastien MARQUE authored on 2017-03-28
126
                    me._selected_text(
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
127
                            id ~ '-' ~ (line - state.scroll.offset),
128
                            state.objects[line].text,
add window management
Sébastien MARQUE authored on 2017-03-28
129
                            state.geometry.x,
130
                            state.geometry.y,
131
                            );
132

            
adds type highlighted
Sébastien MARQUE authored on 2017-04-12
133
                elsif (find('editable', state.objects[line].type) > -1
134
                   or  find('highlighted', state.objects[line].type) > -1)
add window management
Sébastien MARQUE authored on 2017-03-28
135
                    me._editable_text(
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
136
                            id ~ '-' ~ (line - state.scroll.offset),
137
                            state.objects[line].text,
add window management
Sébastien MARQUE authored on 2017-03-28
138
                            state.geometry.x,
139
                            state.geometry.y,
140
                            );
141

            
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
142
                elsif (find('title', state.objects[line].type) > -1)
add window management
Sébastien MARQUE authored on 2017-03-28
143
                    me._title_text(
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
144
                            id ~ '-' ~ (line - state.scroll.offset),
145
                            state.objects[line].text,
add window management
Sébastien MARQUE authored on 2017-03-28
146
                            state.x_base - 10 + state.geometry.w / 2,
147
                            state.geometry.y
148
                        );
149

            
type normal for text is defa...
Sébastien MARQUE authored on 2017-04-12
150
                else
151
                    me._normal_text(
152
                            id ~ '-' ~ (line - state.scroll.offset),
153
                            state.objects[line].text,
154
                            state.geometry.x,
155
                            state.geometry.y,
156
                            );
157

            
158

            
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
159
                if (find('end-of-line', state.objects[line].type) > -1
160
                or  find('title', state.objects[line].type) > -1) {
add window management
Sébastien MARQUE authored on 2017-03-28
161
                    state.geometry.x = state.x_base;
162
                    state.geometry.y += 24;
163
                }
164
                else
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
165
                    state.geometry.x += size(state.objects[line].text) * 10 + 8;
add window management
Sébastien MARQUE authored on 2017-03-28
166
            }
167
        }
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
168
        # reset the scrolling offset before the first move
169
        state.scroll.offset = 0;
add window management
Sébastien MARQUE authored on 2017-03-28
170
    },
171
    
simplifies draw call
Sébastien MARQUE authored on 2020-05-03
172
    draw : func (id, geometry, objects = nil, scroll = nil) {
173
        if (typeof(geometry) == 'vector') {
174
            if (typeof(objects) == 'hash')
175
                scroll = objects;
176
            objects  = geometry;
177
            geometry = {autogeom: 1};
178
        }
179

            
add window management
Sébastien MARQUE authored on 2017-03-28
180
        if (contains(me.window, id ~ '-bg')) {
printlog deprecated + consta...
Sébastien MARQUE authored on 2020-04-24
181
            logprint(LOG_DEBUG, 'objet ' ~ id ~ ' already exists');
add window management
Sébastien MARQUE authored on 2017-03-28
182
            return;
183
        }
add auto config geometry for...
Sébastien MARQUE authored on 2020-04-30
184
        if (!contains(geometry, 'sep'))
185
            geometry.sep = 0;
186
        if (contains(geometry, 'autogeom') and geometry.autogeom) {
187
            # 1024x768 display
188
            # - let 10 from the border
189
            # - plus other 10 from the window border and the text
190
            # - font size tends to be 10x24
191
            # - let 8+8 around the separator
fix count of lines
Sébastien MARQUE authored on 2020-05-03
192
            var textWidth = 0;
add auto config geometry for...
Sébastien MARQUE authored on 2020-04-30
193
            var lines = 0;
fix count of lines
Sébastien MARQUE authored on 2020-05-03
194
            var _textWidth = 0;
add auto config geometry for...
Sébastien MARQUE authored on 2020-04-30
195
            forindex (var o; objects) {
fix count of lines
Sébastien MARQUE authored on 2020-05-03
196
                if (find('end-of-line', objects[o].type) > -1) {
improve autogeom
Sébastien MARQUE authored on 2020-05-02
197
                    if (scroll != nil and contains(scroll, 'lines')) {
fix count of lines
Sébastien MARQUE authored on 2020-05-03
198
                        if (contains(objects[o], 'scrollgroup')) {
improve autogeom
Sébastien MARQUE authored on 2020-05-02
199
                            if (objects[o].scrollgroup < scroll.lines)
200
                                lines += 1;
improve windows auto size
Sébastien MARQUE authored on 2020-05-16
201
                            _textWidth += size(objects[o].text);
fix count of lines
Sébastien MARQUE authored on 2020-05-03
202
                            if (_textWidth > textWidth) textWidth = _textWidth;
203
                            _textWidth = 0;
204
                        }
205
                        else {
206
                            lines += 1;
improve windows auto size
Sébastien MARQUE authored on 2020-05-16
207
                            _textWidth += size(objects[o].text);
fix count of lines
Sébastien MARQUE authored on 2020-05-03
208
                            if (_textWidth > textWidth) textWidth = _textWidth;
209
                            _textWidth = 0;
210
                        }
improve autogeom
Sébastien MARQUE authored on 2020-05-02
211
                    }
fix count of lines
Sébastien MARQUE authored on 2020-05-03
212
                    else {
improve autogeom
Sébastien MARQUE authored on 2020-05-02
213
                        lines += 1;
improve windows auto size
Sébastien MARQUE authored on 2020-05-16
214
                        _textWidth += size(objects[o].text);
fix count of lines
Sébastien MARQUE authored on 2020-05-03
215
                        if (_textWidth > textWidth) textWidth = _textWidth;
216
                        _textWidth = 0;
217
                    }
add auto config geometry for...
Sébastien MARQUE authored on 2020-04-30
218
                }
fix count of lines
Sébastien MARQUE authored on 2020-05-03
219
                elsif (objects[o].type == 'title') {
220
                    lines += 1;
improve windows auto size
Sébastien MARQUE authored on 2020-05-16
221
                    _textWidth = size(objects[o].text);
fix count of lines
Sébastien MARQUE authored on 2020-05-03
222
                    if (_textWidth > textWidth) textWidth = _textWidth;
223
                    _textWidth = 0;
224
                }
225
                elsif (objects[o].type != 'separator') {
improve windows auto size
Sébastien MARQUE authored on 2020-05-16
226
                    _textWidth += size(objects[o].text);
fix count of lines
Sébastien MARQUE authored on 2020-05-03
227
                }
228

            
add auto config geometry for...
Sébastien MARQUE authored on 2020-04-30
229
                if (contains(objects[o], 'type') and objects[o].type == 'separator')
230
                    geometry.sep += 1;
231
            }
improve windows auto size
Sébastien MARQUE authored on 2020-05-16
232
            textWidth += 1;
233
            textWidth *= 10;
fix count of lines
Sébastien MARQUE authored on 2020-05-03
234
            if (!contains(geometry, 'l')) geometry.l = lines;
235
            if (!contains(geometry, 'x')) geometry.x = 1014 - textWidth;
236
            if (!contains(geometry, 'y')) geometry.y = 758 - (lines * 24) - 72; # 72 = offset from bottom to let softkeys display and margin
add auto config geometry for...
Sébastien MARQUE authored on 2020-04-30
237
            if (!contains(geometry, 'w')) geometry.w = textWidth;
238
        }
add window management
Sébastien MARQUE authored on 2017-03-28
239
        if (!contains(geometry, 'h') and !contains(geometry, 'l')) {
printlog deprecated + consta...
Sébastien MARQUE authored on 2020-04-24
240
            logprint(LOG_DEBUG, 'missing parameter l or h');
add window management
Sébastien MARQUE authored on 2017-03-28
241
            return;
242
        }
add auto config geometry for...
Sébastien MARQUE authored on 2020-04-30
243

            
fixes some bugs in windows
Sébastien MARQUE authored on 2017-04-12
244
        var save_x = geometry.x;
245
        var save_y = geometry.y;
add auto config geometry for...
Sébastien MARQUE authored on 2020-04-30
246
        if (!geometry.sep)
247
            geometry.sep = 1;
add window management
Sébastien MARQUE authored on 2017-03-28
248
        me.state[id] = {
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
249
            objects: objects,
add window management
Sébastien MARQUE authored on 2017-03-28
250
            geometry: geometry,
251
            x_base : geometry.x + 10,
252
            h_max : contains(geometry, 'h') ? h : geometry.l * 24 + 8 + geometry.sep * 16,
253
        };
add auto config geometry for...
Sébastien MARQUE authored on 2020-04-30
254

            
255
        logprint(LOG_DEBUG, sprintf('geom id: %s, x: %d, y: %d, w: %d, h: %d, l: %d, sep: %d',
256
                 id, geometry.x, geometry.y, geometry.w, me.state[id].h_max, geometry.l, geometry.sep));
add window management
Sébastien MARQUE authored on 2017-03-28
257
        me.state[id].y_max = me.state[id].h_max + me.state[id].geometry.y;
258
        me.window[id ~ '-bg'] = me.page.createChild('path');
259
        me.window[id ~ '-bg']
260
            .rect(geometry.x, geometry.y,
261
                  geometry.w, me.state[id].h_max)
262
            .setColor(1,1,1)
263
            .setColorFill(0,0,0);
264
        me.state[id].geometry.x += 10;
265
        me.state[id].geometry.y += 16;
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
266
        me.fill(id, scroll);
fixes some bugs in windows
Sébastien MARQUE authored on 2017-04-12
267
        geometry.x = save_x;
268
        geometry.y = save_y;
add window management
Sébastien MARQUE authored on 2017-03-28
269
    },
270
};