Skip to content

Touchscreen modify command doesn't handle item_grid correctly #48

Open
@OgelGames

Description

@OgelGames

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions