From 842cc6d1e4b499c8e74f69ba95fe1017935189b3 Mon Sep 17 00:00:00 2001 From: E Date: Sun, 2 May 2021 00:54:52 -0400 Subject: [PATCH] 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. --- mods/HUD/mcl_formspec/init.lua | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/mods/HUD/mcl_formspec/init.lua b/mods/HUD/mcl_formspec/init.lua index 7013fc0e..3d35421c 100644 --- a/mods/HUD/mcl_formspec/init.lua +++ b/mods/HUD/mcl_formspec/init.lua @@ -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