hud/mcl_formspec: add formspec V2 conversion utilities

Formspecs come in different versions. While maintaining the same basic
syntax, version 1->2 changed (among other things) how coordinates work
by eliminating some (previously) built-in spacing and padding. This
commit adds a number of utility functions that can be used to upgrade
a formspec in-place.

- `size2r` returns a string with the provided w,h coordinates converted
  for use with `size[]` in formspec versions 2+
- `i2r` converts a single coordinate from the V1 coordinates system to
  the V2+ "Real" coordinates
- `xy2r` returns a string suitable for use with other elements that
  accept a coordinate pair. It is a simple wrapper for `i2r`.

The formulas used reflect those specified by the Minetest Lua API docs.
This commit is contained in:
E 2021-05-02 00:54:52 -04:00
parent 32172676f3
commit 842cc6d1e4
1 changed files with 28 additions and 0 deletions

View File

@ -9,3 +9,31 @@ function mcl_formspec.get_itemslot_bg(x, y, w, h)
end
return out
end
-- From the Minetest Lua API doc, "Migrating to Real Coordinates"
local padding, spacing = 3/8, 5/4
function mcl_formspec.size2r(w, h)
return (((w-1)*spacing) + (padding*2) + 1)..","..(((h-1)*spacing) + (padding*2) + 1)
end
local function i2r(i)
return (i*spacing)+padding
end
mcl_formspec.i2r=i2r
function mcl_formspec.xy2r(x, y)
return i2r(x)..","..i2r(y)
end
function mcl_formspec.get_itemslot_bgv2(x, y, w, h)
local out = ""
x=i2r(x)-padding
y=i2r(y)-padding
for i = 0, w - 1, 1 do
for j = 0, h - 1, 1 do
out = out .."image["..x+i2r(i)..","..y+i2r(j)..";1,1;mcl_formspec_itemslot.png]"
end
end
return out
end