zkv1000 / Nasal / menu.nas /
Newer Older
229 lines | 9.166kb
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)
41
            .setColorFill(0,1,1)
42
            .setColor(0,0,0);
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
    
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
166
    draw : func (id, geometry, objects, scroll = nil) {
add window management
Sébastien MARQUE authored on 2017-03-28
167
        if (contains(me.window, id ~ '-bg')) {
printlog deprecated + consta...
Sébastien MARQUE authored on 2020-04-24
168
            logprint(LOG_DEBUG, 'objet ' ~ id ~ ' already exists');
add window management
Sébastien MARQUE authored on 2017-03-28
169
            return;
170
        }
add auto config geometry for...
Sébastien MARQUE authored on 2020-04-30
171
        if (!contains(geometry, 'sep'))
172
            geometry.sep = 0;
173
        if (contains(geometry, 'autogeom') and geometry.autogeom) {
174
            # 1024x768 display
175
            # - let 10 from the border
176
            # - plus other 10 from the window border and the text
177
            # - font size tends to be 10x24
178
            # - let 8+8 around the separator
179
            var textWidth = 200;
180
            var lines = 0;
181
            forindex (var o; objects) {
182
                if (contains(objects[o], 'text')) {
183
                    var l = size(objects[o].text) * 10;
184
                    if (l > textWidth)
185
                        textWidth = l;
186
                    lines += 1;
187
                }
188
                if (contains(objects[o], 'type') and objects[o].type == 'separator')
189
                    geometry.sep += 1;
190
            }
191
            geometry.l = lines;
192
            if (contains(geometry, 'x'))  geometry.x -= textWidth;
193
            else                          geometry.x  = 1014 - textWidth;
194
            if (contains(geometry, 'y'))  geometry.y -= lines * 24 + 72;         # 72 = offset from bottom to let...
195
            else                          geometry.y  = 758 - (lines * 24) - 72; # ... softkeys display and margin
196
            if (!contains(geometry, 'w')) geometry.w = textWidth;
197
        }
add window management
Sébastien MARQUE authored on 2017-03-28
198
        if (!contains(geometry, 'h') and !contains(geometry, 'l')) {
printlog deprecated + consta...
Sébastien MARQUE authored on 2020-04-24
199
            logprint(LOG_DEBUG, 'missing parameter l or h');
add window management
Sébastien MARQUE authored on 2017-03-28
200
            return;
201
        }
add auto config geometry for...
Sébastien MARQUE authored on 2020-04-30
202

            
fixes some bugs in windows
Sébastien MARQUE authored on 2017-04-12
203
        var save_x = geometry.x;
204
        var save_y = geometry.y;
add auto config geometry for...
Sébastien MARQUE authored on 2020-04-30
205
        if (!geometry.sep)
206
            geometry.sep = 1;
add window management
Sébastien MARQUE authored on 2017-03-28
207
        me.state[id] = {
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
208
            objects: objects,
add window management
Sébastien MARQUE authored on 2017-03-28
209
            geometry: geometry,
210
            x_base : geometry.x + 10,
211
            h_max : contains(geometry, 'h') ? h : geometry.l * 24 + 8 + geometry.sep * 16,
212
        };
add auto config geometry for...
Sébastien MARQUE authored on 2020-04-30
213

            
214
        logprint(LOG_DEBUG, sprintf('geom id: %s, x: %d, y: %d, w: %d, h: %d, l: %d, sep: %d',
215
                 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
216
        me.state[id].y_max = me.state[id].h_max + me.state[id].geometry.y;
217
        me.window[id ~ '-bg'] = me.page.createChild('path');
218
        me.window[id ~ '-bg']
219
            .rect(geometry.x, geometry.y,
220
                  geometry.w, me.state[id].h_max)
221
            .setColor(1,1,1)
222
            .setColorFill(0,0,0);
223
        me.state[id].geometry.x += 10;
224
        me.state[id].geometry.y += 16;
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
225
        me.fill(id, scroll);
fixes some bugs in windows
Sébastien MARQUE authored on 2017-04-12
226
        geometry.x = save_x;
227
        geometry.y = save_y;
add window management
Sébastien MARQUE authored on 2017-03-28
228
    },
229
};