Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Touchscreen modify command doesn't handle item_grid correctly #48

Open
OgelGames opened this issue Feb 3, 2024 · 0 comments
Open

Touchscreen modify command doesn't handle item_grid correctly #48

OgelGames opened this issue Feb 3, 2024 · 0 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@OgelGames
Copy link
Contributor

Currently the modify command looks at the start of the element string to identify the element that it contains, however this does not work with the custom item_grid element, because that element string consists of multiple item_image or item_image_button.

Possible solutions:

  • Change how elements are stored in metadata so that the modify command does not need to use pattern matching.
  • Add code specifically for item_grid modification. (and tables too)
  • Something else?

Relevant code:

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

formspec_elements.item_grid = function(values)
for v,d in pairs({X = 0, Y = 0, W = 1, H = 1, spacing = 0, size = 1, offset = 1}) do
if type(values[v]) ~= "number" then
values[v] = d
end
end
local name = str(values.name, "grid").."_"
local items = type(values.items) == "table" and values.items or {}
local offset = math.max(1, math.floor(values.offset)) - 1
local x, y, n, item = values.X, values.Y, 1
local grid = {}
for _=1, values.H do
for _=1, values.W do
item = items[n + offset]
if type(item) ~= "string" then
return table.concat(grid)
end
item = string.match(item, "^[^ %[%]\\,;]* ?%d* ?%d*")
if values.interactable ~= false then
grid[n] = string.format("item_image_button[%s,%s;%s,%s;%s;%s;]",
x, y, values.size, values.size, item, name..n)
else
grid[n] = string.format("item_image[%s,%s;%s,%s;%s]",
x, y, values.size, values.size, item)
end
n = n + 1
x = x + values.size + values.spacing
end
x = values.X
y = y + values.size + values.spacing
end
return table.concat(grid)
end

@OgelGames OgelGames added bug Something isn't working enhancement New feature or request labels Feb 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant