-
Notifications
You must be signed in to change notification settings - Fork 6
/
touchscreen.lua
280 lines (256 loc) · 7.38 KB
/
touchscreen.lua
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
local unpack = table.unpack or unpack
local formspec_elements = dofile(minetest.get_modpath("digistuff").."/formspec_elements.lua")
local formspec_version = 6
local function create_element_string(element, values)
if type(element) == "function" then
return element(values)
end
local new_values = {}
for i,name in ipairs(element[2]) do
local value = element[4][i](values[name], element[3][i])
table.insert(new_values, value)
end
return string.format(element[1], unpack(new_values))
end
local function modify_element_string(old, values)
local e = string.match(old, "^(.-)%[")
local element = formspec_elements[e]
if type(element) == "function" then
return old -- No-op for special elements, as there is no format string
end
local old_values = {string.match(old, element[5])}
local new_values = {}
for i,name in ipairs(element[2]) do
local value = element[4][i](values[name], old_values[i] or element[3][i])
table.insert(new_values, value)
end
return string.format(element[1], unpack(new_values))
end
local function check_old_command(msg)
local cmd = msg.command
if cmd == "lock" then
return {command = "set", locked = true}
end
if cmd == "unlock" then
return {command = "set", locked = false}
end
if cmd == "realcoordinates" then
return {command = "set", real_coordinates = msg.enabled}
end
if string.match(cmd, "^add%a+") then
msg.element = string.sub(cmd, 4)
msg.command = "add"
if msg.image then
-- Compatibility for old name
msg.texture_name = msg.image
end
end
return msg
end
local function process_command(meta, data, msg)
msg = check_old_command(msg)
local cmd = msg.command
if cmd == "clear" then
data = {}
elseif cmd == "add" then
local element = formspec_elements[msg.element]
if element then
local str = create_element_string(element, msg)
table.insert(data, str)
end
elseif cmd == "insert" then
local element = formspec_elements[msg.element]
local index = tonumber(msg.index)
if element and index and index > 0 then
local str = create_element_string(element, msg)
table.insert(data, index, str)
end
elseif cmd == "replace" then
local element = formspec_elements[msg.element]
local index = tonumber(msg.index)
if element and index and data[index] then
local str = create_element_string(element, msg)
data[index] = str
end
elseif cmd == "modify" then
local index = tonumber(msg.index)
if index and data[index] then
local str = modify_element_string(data[index], msg)
data[index] = str
end
elseif cmd == "remove" then
local index = tonumber(msg.index)
if index and data[index] then
table.remove(data, index)
end
elseif cmd == "delete" then
local index = tonumber(msg.index)
if index and data[index] then
data[index] = nil
end
elseif cmd == "set" then
if msg.locked ~= nil then
meta:set_int("locked", msg.locked == false and 0 or 1)
end
if msg.no_prepend ~= nil then
meta:set_int("no_prepend", msg.no_prepend == false and 0 or 1)
end
if msg.real_coordinates ~= nil then
meta:set_int("real_coordinates", msg.real_coordinates == false and 0 or 1)
end
if msg.fixed_size ~= nil then
meta:set_int("fixed_size", msg.fixed_size == false and 0 or 1)
end
if type(msg.width) == "number" then
local value = math.max(1, math.min(100, msg.width))
meta:set_string("width", string.format("%.4g", value))
end
if type(msg.height) == "number" then
local value = math.max(1, math.min(100, msg.height))
meta:set_string("height", string.format("%.4g", value))
end
if type(msg.focus) == "string" then
meta:set_string("focus", minetest.formspec_escape(msg.focus))
end
end
return data
end
local function get_data(meta)
local data = minetest.deserialize(meta:get("data"))
if data and type(data[1]) == "table" then
-- Old data, convert to new format
for i,v in ipairs(data) do
local element = formspec_elements[v.type]
data[i] = create_element_string(element, v)
end
end
return data
end
local function create_formspec(meta, data)
local fs = "formspec_version["..formspec_version.."]"
local width = tonumber(meta:get_string("width")) or 10
local height = tonumber(meta:get_string("height")) or 8
if meta:get_int("fixed_size") == 1 then
fs = fs.."size["..width..","..height..",true]"
else
fs = fs.."size["..width..","..height.."]"
end
if meta:get_int("no_prepend") == 1 then
fs = fs.."no_prepend[]"
end
if not meta:get("real_coordinates") and not meta:get("realcoordinates") then
fs = fs.."real_coordinates[false]"
end
local focus = meta:get("focus")
if focus then
fs = fs.."set_focus["..focus.."]"
end
local data_size = 0
for i in pairs(data) do
if i > data_size then
data_size = i
end
end
for i=1, data_size do
if data[i] then
fs = fs..data[i]
end
end
return fs
end
local function update_formspec(pos, meta, data)
data = data or get_data(meta)
if not meta:get("init") then
meta:set_string("formspec", "field[channel;Channel;]")
elseif not data then
meta:set_string("formspec", "size[10,8]")
else
meta:set_string("formspec", create_formspec(meta, data))
end
end
local function on_digiline(pos, node, channel, msg)
if type(msg) ~= "table" then
return
end
local meta = minetest.get_meta(pos)
if channel ~= meta:get_string("channel") then
return
end
local data = get_data(meta) or {}
if type(msg.command) == "string" then
data = process_command(meta, data, msg)
else
for _,v in ipairs(msg) do
if type(v) == "table" and type(v.command) == "string" then
data = process_command(meta, data, v)
end
end
end
meta:set_string("data", minetest.serialize(data))
update_formspec(pos, meta, data)
end
local function on_receive_fields(pos, _, fields, player)
local meta = minetest.get_meta(pos)
local name = player:get_player_name()
if meta:get_int("locked") == 1 and minetest.is_protected(pos, name) then
minetest.chat_send_player(name, "You are not authorized to use this screen.")
return
end
if not meta:get("init") and fields.channel then
meta:set_string("channel", fields.channel)
meta:set_int("init", 1)
update_formspec(pos, meta)
else
local channel = meta:get_string("channel")
fields.clicker = name
digilines.receptor_send(pos, digilines.rules.default, channel, fields)
end
end
minetest.register_node("digistuff:touchscreen", {
description = "Digilines Touchscreen",
groups = {cracky = 3},
is_ground_content = false,
tiles = {
"digistuff_panel_back.png",
"digistuff_panel_back.png",
"digistuff_panel_back.png",
"digistuff_panel_back.png",
"digistuff_panel_back.png",
"digistuff_ts_front.png"
},
drawtype = "nodebox",
paramtype = "light",
paramtype2 = "facedir",
node_box = {
type = "fixed",
fixed = {
{-0.5, -0.5, 0.4, 0.5, 0.5, 0.5}
}
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
update_formspec(pos, meta)
end,
on_receive_fields = on_receive_fields,
digilines = {
receptor = {},
effector = {
action = on_digiline
}
},
_digistuff_channelcopier_fieldname = "channel",
_digistuff_channelcopier_onset = function(pos)
local meta = minetest.get_meta(pos)
meta:set_int("init", 1)
update_formspec(pos, meta)
end,
})
minetest.register_craft({
output = "digistuff:touchscreen",
recipe = {
{"mesecons_luacontroller:luacontroller0000", "default:glass", "default:glass"},
{"default:glass", "digilines:lcd", "default:glass"},
{"default:glass", "default:glass", "default:glass"}
}
})
minetest.register_alias("digistuff:advtouchscreen", "digistuff:touchscreen")