Rework touchscreen insert command and add delete command (#40)

Co-authored-by: OgelGames <olliverdc28@gmail.com>
This commit is contained in:
Infi-power 2023-11-24 08:40:29 +00:00 committed by GitHub
parent 80bbcc6739
commit 68ad7b4fcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 9 deletions

View File

@ -34,7 +34,7 @@ digiline_send("touchscreen", {
}) })
``` ```
**`insert`** - Adds an element at a specific index. **`insert`** - Adds an element at a specific index. The index can be any positive number.
```lua ```lua
digiline_send("touchscreen", { digiline_send("touchscreen", {
@ -63,7 +63,7 @@ digiline_send("touchscreen", {
}) })
``` ```
**`remove`** - Removes an element. **`remove`** - Removes an element, shifting elements to fill the space.
```lua ```lua
digiline_send("touchscreen", { digiline_send("touchscreen", {
@ -72,7 +72,16 @@ digiline_send("touchscreen", {
}) })
``` ```
**`clear`** - Removes all elements, but keeps settings. **`delete`** - Removes an element without changing element indexes.
```lua
digiline_send("touchscreen", {
command = "delete",
index = 1,
})
```
**`clear`** - Removes all elements.
```lua ```lua
digiline_send("touchscreen", { digiline_send("touchscreen", {

View File

@ -73,11 +73,7 @@ local function process_command(meta, data, msg)
local index = tonumber(msg.index) local index = tonumber(msg.index)
if element and index and index > 0 then if element and index and index > 0 then
local str = create_element_string(element, msg) local str = create_element_string(element, msg)
if index > #data then table.insert(data, index, str)
table.insert(data, str)
else
table.insert(data, index, str)
end
end end
elseif cmd == "replace" then elseif cmd == "replace" then
@ -101,6 +97,12 @@ local function process_command(meta, data, msg)
table.remove(data, index) table.remove(data, index)
end end
elseif cmd == "delete" then
local index = tonumber(msg.index)
if index and data[index] then
data[index] = nil
end
elseif cmd == "set" then elseif cmd == "set" then
if msg.locked ~= nil then if msg.locked ~= nil then
meta:set_int("locked", msg.locked == false and 0 or 1) meta:set_int("locked", msg.locked == false and 0 or 1)
@ -161,7 +163,18 @@ local function create_formspec(meta, data)
if focus then if focus then
fs = fs.."set_focus["..focus.."]" fs = fs.."set_focus["..focus.."]"
end end
return fs..table.concat(data) 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 end
local function update_formspec(pos, meta, data) local function update_formspec(pos, meta, data)