zkv1000 / Nasal / menu.nas /
Newer Older
263 lines | 10.698kb
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

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

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

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

            
56
    _normal_text : func (id, text, x, y) {
57
        me.window[id]
58
            .setFontSize(16)
59
            .setFont('LiberationFonts/LiberationMono-Regular.ttf')
60
            .setTranslation(x, y)
61
            .setText(text)
62
            .setColorFill(0,0,0)
63
            .setColor(1,1,1);
64
    },
65

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

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

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

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

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

            
153

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

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

            
add auto config geometry for...
Sébastien MARQUE authored on 2020-04-30
224
                if (contains(objects[o], 'type') and objects[o].type == 'separator')
225
                    geometry.sep += 1;
226
            }
fix count of lines
Sébastien MARQUE authored on 2020-05-03
227
            if (!contains(geometry, 'l')) geometry.l = lines;
228
            if (!contains(geometry, 'x')) geometry.x = 1014 - textWidth;
229
            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
230
            if (!contains(geometry, 'w')) geometry.w = textWidth;
231
        }
add window management
Sébastien MARQUE authored on 2017-03-28
232
        if (!contains(geometry, 'h') and !contains(geometry, 'l')) {
printlog deprecated + consta...
Sébastien MARQUE authored on 2020-04-24
233
            logprint(LOG_DEBUG, 'missing parameter l or h');
add window management
Sébastien MARQUE authored on 2017-03-28
234
            return;
235
        }
add auto config geometry for...
Sébastien MARQUE authored on 2020-04-30
236

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

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