zkv1000 / Nasal / menu.nas /
Newer Older
271 lines | 10.89kb
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
fix scrolling with multiline...
Sébastien MARQUE authored on 2020-05-22
93
            rows : scroll != nil and contains(scroll, 'rows') ? scroll.rows : 1,
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
94
        };
95
        var scrollgroup = {};
96
        forindex (var line; state.objects) {
97
            if (find('separator', state.objects[line].type) > -1) {
98
                me.window[id ~ '-' ~ (line - state.scroll.offset)] = me.page.createChild('path')
add window management
Sébastien MARQUE authored on 2017-03-28
99
                    .setStrokeLineWidth(1)
100
                    .moveTo(state.x_base, state.geometry.y - 12)
101
                    .horiz(state.geometry.w - 20)
102
                    .setColor(1,1,1);
103
                state.geometry.x = state.x_base;
104
                state.geometry.y += 8;
105
            }
106
            else {
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
107
                if (contains(state.objects[line], 'scrollgroup')) {
108
                    state.scroll.last = state.objects[line].scrollgroup;
109
                    scrollgroup[state.objects[line].scrollgroup] = 1;
110
                    if (state.scroll.begin == -9999) {
111
                        state.scroll.begin = line;
112
                        state.scroll.upper = state.objects[line].scrollgroup;
113
                    }
114
                    if (size(keys(scrollgroup)) > state.scroll.lines) {
115
                        if (state.scroll.end == 9999) {
116
                            state.scroll.end = line - 1;
117
                            state.scroll.lower = state.objects[line - 1].scrollgroup;
118
                        }
119
                        else
120
                            state.scroll.last = state.objects[line].scrollgroup;
121
                        state.scroll.offset += 1;
122
                        continue;
123
                    }
124
                }
125
                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
126
                if (find('selected', state.objects[line].type) > -1)
add window management
Sébastien MARQUE authored on 2017-03-28
127
                    me._selected_text(
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
128
                            id ~ '-' ~ (line - state.scroll.offset),
129
                            state.objects[line].text,
add window management
Sébastien MARQUE authored on 2017-03-28
130
                            state.geometry.x,
131
                            state.geometry.y,
132
                            );
133

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

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

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

            
159

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

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

            
add auto config geometry for...
Sébastien MARQUE authored on 2020-04-30
230
                if (contains(objects[o], 'type') and objects[o].type == 'separator')
231
                    geometry.sep += 1;
232
            }
improve windows auto size
Sébastien MARQUE authored on 2020-05-16
233
            textWidth += 1;
234
            textWidth *= 10;
fix count of lines
Sébastien MARQUE authored on 2020-05-03
235
            if (!contains(geometry, 'l')) geometry.l = lines;
236
            if (!contains(geometry, 'x')) geometry.x = 1014 - textWidth;
237
            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
238
            if (!contains(geometry, 'w')) geometry.w = textWidth;
239
        }
add window management
Sébastien MARQUE authored on 2017-03-28
240
        if (!contains(geometry, 'h') and !contains(geometry, 'l')) {
printlog deprecated + consta...
Sébastien MARQUE authored on 2020-04-24
241
            logprint(LOG_DEBUG, 'missing parameter l or h');
add window management
Sébastien MARQUE authored on 2017-03-28
242
            return;
243
        }
add auto config geometry for...
Sébastien MARQUE authored on 2020-04-30
244

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

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