zkv1000 / Nasal / menu.nas /
Newer Older
192 lines | 7.403kb
add window management
Sébastien MARQUE authored on 2017-03-28
1
var pageClass = {
2
    new : func (d) {
3
        var m = { parents : [pageClass] };
4
        m.page = d.display.screen;
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);
14
            id = [ id ];
15
        }
16
        else {
17
            foreach (var s; keys(me.state))
18
                delete(me.state, s);
19
            id = keys(me.window);
20
        }
21
        foreach (var w; id) {
22
            me.window[w]
23
                .hide()
24
                .del();
25
            delete(me.window, w);
26
        }
27
    },
28

            
29
    _selected_text : func (id, text, x, y) {
30
        me.selected = id;
31
        me.window[id]
32
            .setFontSize(16)
33
            .setFont('LiberationFonts/LiberationMono-Regular.ttf')
34
            .setTranslation(x, y)
35
            .setDrawMode(0x01 + 0x04)
36
            .setText(text)
37
            .setColorFill(0,1,1)
38
            .setColor(0,0,0);
39
    },
40

            
41
    _editable_text : func (id, text, x, y) {
42
        me.window[id]
43
            .setFontSize(16)
44
            .setFont('LiberationFonts/LiberationMono-Regular.ttf')
45
            .setTranslation(x, y)
46
            .setText(text)
47
            .setColorFill(0,0,0)
48
            .setColor(0,1,1);
49
    },
50

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

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

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

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

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

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

            
148

            
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
149
                if (find('end-of-line', state.objects[line].type) > -1
150
                or  find('title', state.objects[line].type) > -1) {
add window management
Sébastien MARQUE authored on 2017-03-28
151
                    state.geometry.x = state.x_base;
152
                    state.geometry.y += 24;
153
                }
154
                else
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
155
                    state.geometry.x += size(state.objects[line].text) * 10 + 8;
add window management
Sébastien MARQUE authored on 2017-03-28
156
            }
157
        }
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
158
        # reset the scrolling offset before the first move
159
        state.scroll.offset = 0;
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
    draw : func (id, geometry, objects, scroll = nil) {
add window management
Sébastien MARQUE authored on 2017-03-28
163
        if (contains(me.window, id ~ '-bg')) {
164
            printlog('debug', 'objet ' ~ id ~ ' already exists');
165
            return;
166
        }
167
        if (!contains(geometry, 'h') and !contains(geometry, 'l')) {
168
            printlog('debug', 'missing parameter l or h');
169
            return;
170
        }
fixes some bugs in windows
Sébastien MARQUE authored on 2017-04-12
171
        var save_x = geometry.x;
172
        var save_y = geometry.y;
add window management
Sébastien MARQUE authored on 2017-03-28
173
        me.state[id] = {
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
174
            objects: objects,
add window management
Sébastien MARQUE authored on 2017-03-28
175
            geometry: geometry,
176
            x_base : geometry.x + 10,
177
            h_max : contains(geometry, 'h') ? h : geometry.l * 24 + 8 + geometry.sep * 16,
178
        };
179
        me.state[id].y_max = me.state[id].h_max + me.state[id].geometry.y;
180
        me.window[id ~ '-bg'] = me.page.createChild('path');
181
        me.window[id ~ '-bg']
182
            .rect(geometry.x, geometry.y,
183
                  geometry.w, me.state[id].h_max)
184
            .setColor(1,1,1)
185
            .setColorFill(0,0,0);
186
        me.state[id].geometry.x += 10;
187
        me.state[id].geometry.y += 16;
changes about how to fill wi...
Sébastien MARQUE authored on 2017-04-02
188
        me.fill(id, scroll);
fixes some bugs in windows
Sébastien MARQUE authored on 2017-04-12
189
        geometry.x = save_x;
190
        geometry.y = save_y;
add window management
Sébastien MARQUE authored on 2017-03-28
191
    },
192
};