zkv1000 / Nasal / menu.nas /
Newer Older
155 lines | 5.275kb
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) {
12
        if (id != nil and typeof(v) == 'scalar') {
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

            
72
    fill : func (id) {
73
        var state = me.state[id];
74
        forindex (var line; state.lines) {
75
            if (find('separator', state.lines[line].type) > -1) {
76
                me.window[id ~ '-' ~ line] = me.page.createChild('path')
77
                    .setStrokeLineWidth(1)
78
                    .moveTo(state.x_base, state.geometry.y - 12)
79
                    .horiz(state.geometry.w - 20)
80
                    .setColor(1,1,1);
81
                state.geometry.x = state.x_base;
82
                state.geometry.y += 8;
83
            }
84
            else {
85
                me.window[id ~ '-' ~ line] = me.page.createChild('text');
86
                if (find('normal', state.lines[line].type) > -1)
87
                    me._normal_text(
88
                            id ~ '-' ~ line,
89
                            state.lines[line].text,
90
                            state.geometry.x,
91
                            state.geometry.y,
92
                            );
93

            
94
                elsif (find('selected', state.lines[line].type) > -1)
95
                    me._selected_text(
96
                            id ~ '-' ~ line,
97
                            state.lines[line].text,
98
                            state.geometry.x,
99
                            state.geometry.y,
100
                            );
101

            
102
                elsif (find('editable', state.lines[line].type) > -1)
103
                    me._editable_text(
104
                            id ~ '-' ~ line,
105
                            state.lines[line].text,
106
                            state.geometry.x,
107
                            state.geometry.y,
108
                            );
109

            
110
                elsif (find('title', state.lines[line].type) > -1)
111
                    me._title_text(
112
                            id ~ '-' ~ line,
113
                            state.lines[line].text,
114
                            state.x_base - 10 + state.geometry.w / 2,
115
                            state.geometry.y
116
                        );
117

            
118
                if (find('end-of-line', state.lines[line].type) > -1
119
                or  find('title', state.lines[line].type) > -1) {
120
                    state.geometry.x = state.x_base;
121
                    state.geometry.y += 24;
122
                }
123
                else
124
                    state.geometry.x += size(state.lines[line].text) * 10 + 8;
125
            }
126
        }
127
    },
128
    
129
    draw : func (id, geometry, lines) {
130
        if (contains(me.window, id ~ '-bg')) {
131
            printlog('debug', 'objet ' ~ id ~ ' already exists');
132
            return;
133
        }
134
        if (!contains(geometry, 'h') and !contains(geometry, 'l')) {
135
            printlog('debug', 'missing parameter l or h');
136
            return;
137
        }
138
        me.state[id] = {
139
            lines: lines, 
140
            geometry: geometry,
141
            x_base : geometry.x + 10,
142
            h_max : contains(geometry, 'h') ? h : geometry.l * 24 + 8 + geometry.sep * 16,
143
        };
144
        me.state[id].y_max = me.state[id].h_max + me.state[id].geometry.y;
145
        me.window[id ~ '-bg'] = me.page.createChild('path');
146
        me.window[id ~ '-bg']
147
            .rect(geometry.x, geometry.y,
148
                  geometry.w, me.state[id].h_max)
149
            .setColor(1,1,1)
150
            .setColorFill(0,0,0);
151
        me.state[id].geometry.x += 10;
152
        me.state[id].geometry.y += 16;
153
        me.fill(id);
154
    },
155
};