Repository navigation
Expand file tree
/
Copy pathuiView.lua
More file actions
159 lines (150 loc) · 4.05 KB
/
Copy pathuiView.lua
File metadata and controls
159 lines (150 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
local UI = {
{
{button="B1", text="Btn 1", onReleased='A'},
{button="B2", text="Btn 2", onReleased='B'}
}
}
local function uiType(c)
return c.button and 'button'
or c.label and 'label'
or c.slider and 'slider'
or c.select and 'select'
or "unknown"
end
local function dflt(v,d)
if v~=nil then return v else return d end
end
local function createEventbindings(c,id,actions,opts,...)
local binds,act = {},nil
for _,a in ipairs(actions) do
if not opts.child then
act = {
params = {
actionName = 'UIAction',
args = {
a,
id,
...
}
},
type = 'deviceAction'
}
else
act = {
params = {
actionName = c[a] or c['onReleased'],
args = {},
},
type = 'deviceAction'
}
end --if
binds[a] = { act }
end -- loop
return binds
end
local decoder = {}
function decoder.button(c,opt)
return {
eventBinding = createEventbindings(c,c.button,
{"onReleased","onLongPressReleased","onLongPressDown"},
opts),
name = c.button,
style = { weight = c.weight or opt.weight },
text = c.text,
type = 'button',
visible = dflt(c.visible,opt.visible)
}
end
function decoder.label(c)
return {
name = c.label,
style = { weight = c.weight or opt.weight },
text = c.text,
type = 'label',
visible = dflt(c.visible,opt.visible)
}
end
function decoder.slider(c,opts)
return {
eventBinding = createEventbindings(c,c.slider,
{"onChanged"},
opts,"$event.value"),
name = c.slider,
style = { weight = c.weight or opt.weight },
min = c.min or opts.min,
max = c.max or opts.max,
step = c.step or opts.step,
text = c.text or opts.text,
type = 'slider',
visible = dflt(c.visible,opt.visible)
}
end
function decoder.switch(c,opt)
return {
eventBinding = createEventbindings(c,c.switch,
{"onReleased","onLongPressReleased","onLongPressDown"},
opts,"$event.value"),
name = c.switch,
style = { weight = c.weight or opt.weight },
text = c.text,
type = 'switch',
value = dflt(c.value,opts.value),
visible = dflt(c.visible,opt.visible)
}
end
function decoder.select(c,opts)
return {
eventBinding = createEventbindings(c,c.select,
{"onToggled"},
opts,"$event.value"),
name = c.select,
style = { weight = c.weight or opts.weight },
options = dflt(c.options,opts.options),
selectionType = c.selectionType or opts.selectionType,
values = dflt(c.values,opts.values),
text = c.text or opts.text,
type = 'select',
visible = dflt(c.visible,opts.visible)
}
end
function decoder.unknown(c,opts)
error("Unknown ui type ",json.encode(c))
end
local defaultWeight = {
1.0,
0.50,
0.33,
0.25,
0.20,
}
local function decodeComponents(row,cfg)
cmps = {}
opts = {
child = cfg.child == true,
weight = defaultWeight[#row] or 1.0,
min = 0, --slider
max = 100, --slider
step = 1, --slider
text = "",
value = false, -- switch
selectionType = 'single', --select
options = {}, -- select
values = {}, -- select
visible = true
}
for _,c in ipairs(row) do cmps[#cmps+1] = decoder[uiType(c)](c,opts) end
return cmps
end
local function decodeUI(ui,cfg)
cfg = cfg or { child = true }
local components = {}
for _,row in ipairs(ui) do
components[#components+1] = {
components = decodeComponents(row,cfg),
style = { weight = 1.0 },
type = 'horizontal'
}
end
return components
end
print(json.encode(decodeUI(UI)))