zkv1000 / Nasal / menu.nas /
Newer Older
272 lines | 10.916kb
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
        }
more complete windows deleti...
Sébastien MARQUE authored on 2020-06-11
36
        me.selected = '';
add window management
Sébastien MARQUE authored on 2017-03-28
37
    },
38

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

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

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

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

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

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

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

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

            
160

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

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

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

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

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