zkv1000 / Nasal / menu.nas /
Newer Older
262 lines | 10.586kb
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)
39
            .setDrawMode(0x01 + 0x04)
40
            .setText(text)
withrawal of highlight selec...
Sébastien MARQUE authored on 2020-05-02
41
            .setColorFill(0,0,0)
42
            .setColor(1,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)
50
            .setText(text)
51
            .setColorFill(0,0,0)
52
            .setColor(0,1,1);
53
    },
54

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

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

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

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

            
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
136
                elsif (find('title', state.objects[line].type) > -1)
add window management
Sébastien MARQUE authored on 2017-03-28
137
                    me._title_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.x_base - 10 + state.geometry.w / 2,
141
                            state.geometry.y
142
                        );
143

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

            
152

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

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

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

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

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