Fix and enhance grid shapes in craft guide

Commit 043f608145 made shaped crafting
recipes display in a grid of the recipe's minimum bounding box,
but broke the use of a fixed 1x1 grid shape for cooking recipes.
This commit generalises the dynamic-grid-shape facility, putting the
craft type registration in charge.  This restores the correct shape for
cooking recipes.

Also change the logic for dynamic grid sizes for regular shaped and
shapeless crafting.  We'd like to always use a grid shape that is
reminiscent of a regular crafting grid, as a visual cue to the crafting
method.  But it's also nice to show smaller recipes in a smaller grid.
In the expectation that crafting grids will always be square, show the
recipe in the smallest square grid that will accommodate it.
This commit is contained in:
Zefram 2014-06-13 12:15:50 +01:00 committed by Diego Martinez
parent a8c8ef0890
commit 4c982dd667
2 changed files with 22 additions and 16 deletions

13
api.lua
View File

@ -134,6 +134,13 @@ unified_inventory.register_craft_type("normal", {
description = "Crafting", description = "Crafting",
width = 3, width = 3,
height = 3, height = 3,
get_shaped_craft_width = function (craft) return craft.width end,
dynamic_display_size = function (craft)
local w = craft.width
local h = math.ceil(table.maxn(craft.items) / craft.width)
local g = w < h and h or w
return { width = g, height = g }
end,
uses_crafting_grid = true, uses_crafting_grid = true,
}) })
@ -142,6 +149,12 @@ unified_inventory.register_craft_type("shapeless", {
description = "Mixing", description = "Mixing",
width = 3, width = 3,
height = 3, height = 3,
dynamic_display_size = function (craft)
local maxn = table.maxn(craft.items)
local g = 1
while g*g < maxn do g = g + 1 end
return { width = g, height = g }
end,
uses_crafting_grid = true, uses_crafting_grid = true,
}) })

View File

@ -207,24 +207,18 @@ unified_inventory.register_page("craftguide", {
formspec = formspec.."label[6,3.35;No recipes]" formspec = formspec.."label[6,3.35;No recipes]"
end end
local width = craft and craft.width or 0 local display_size = craft_type.dynamic_display_size and craft_type.dynamic_display_size(craft) or { width = craft_type.width, height = craft_type.height }
if width == 0 then local craft_width = craft_type.get_shaped_craft_width and craft_type.get_shaped_craft_width(craft) or display_size.width
-- Shapeless recipe
width = craft_type.width
end
local height = craft_type.height
if craft then
height = math.ceil(table.maxn(craft.items) / width)
end
local i = 1
-- This keeps recipes aligned to the right, -- This keeps recipes aligned to the right,
-- so that they're close to the arrow. -- so that they're close to the arrow.
local xoffset = 1 + (3 - width) local xoffset = 1 + (3 - display_size.width)
for y = 1, height do for y = 1, display_size.height do
for x = 1, width do for x = 1, display_size.width do
local item = craft and craft.items[i] local item
if craft and x <= craft_width then
item = craft.items[(y-1) * craft_width + x]
end
if item then if item then
formspec = formspec..stack_image_button( formspec = formspec..stack_image_button(
xoffset + x, y, 1.1, 1.1, xoffset + x, y, 1.1, 1.1,
@ -235,7 +229,6 @@ unified_inventory.register_page("craftguide", {
..tostring(xoffset + x)..","..tostring(y) ..tostring(xoffset + x)..","..tostring(y)
..";1,1;ui_blank_image.png;;]" ..";1,1;ui_blank_image.png;;]"
end end
i = i + 1
end end
end end