2020-06-13 07:41:56 -04:00
|
|
|
-- Gui API (Internal)
|
|
|
|
--
|
|
|
|
-- Contains functions for building formspec-based uis
|
|
|
|
local gui = {
|
|
|
|
};
|
|
|
|
|
|
|
|
-- Create a formspec
|
|
|
|
--
|
|
|
|
-- w: The width of the window
|
|
|
|
-- h: The height of the window
|
|
|
|
-- (Optional) version: The formspec version. Defaults to 3.
|
|
|
|
-- (Optional) bg: A 9-slice background skin object
|
|
|
|
-- Additional arguments are added as additional formspec elements
|
|
|
|
--
|
|
|
|
-- Returns a table. Calling table.concat on the result will produce
|
|
|
|
-- a usable formspec string.
|
2020-06-07 18:09:58 -04:00
|
|
|
function gui.formspec(args)
|
2020-06-26 18:02:18 -04:00
|
|
|
local data = string.format("formspec_version[%d] size[%f,%f] no_prepend[] bgcolor[#00000000;false]",
|
|
|
|
args.version or 3,
|
|
|
|
args.w, args.h);
|
2020-06-07 18:09:58 -04:00
|
|
|
|
|
|
|
if args.bg then
|
|
|
|
data = data .. gui.bg9 {
|
|
|
|
skin = args.bg,
|
2021-11-24 06:37:28 -05:00
|
|
|
fullsize = "true",
|
2020-06-07 18:09:58 -04:00
|
|
|
};
|
|
|
|
end
|
|
|
|
|
2020-06-08 18:34:57 -04:00
|
|
|
for _,element in ipairs(args) do
|
|
|
|
data = data .. element;
|
|
|
|
end
|
|
|
|
|
2020-06-07 18:09:58 -04:00
|
|
|
return data;
|
|
|
|
end
|
|
|
|
|
2020-06-13 07:41:56 -04:00
|
|
|
-- Create an animated image formspec element
|
|
|
|
--
|
|
|
|
-- animation: An animation skin object
|
|
|
|
-- (Optional) x: The x position of the element. Defaults to 0.
|
|
|
|
-- (Optional) y: The y position of the element. Defaults to 0.
|
|
|
|
-- (Optional) w: The width of the element. Defaults to 1.
|
|
|
|
-- (Optional) h: The height of the element. Defaults to 1.
|
|
|
|
-- (Optional) size: Multiplies the width and height. Defaults to 1.
|
|
|
|
-- (Optional) id: The element id
|
|
|
|
--
|
|
|
|
-- Returns a formspec string
|
2020-06-08 18:34:57 -04:00
|
|
|
function gui.animated_image(args)
|
2020-06-13 07:41:56 -04:00
|
|
|
local x = args.x or 0;
|
|
|
|
local y = args.y or 0;
|
|
|
|
local w = args.w or 1 * (args.size or 1);
|
|
|
|
local h = args.h or 1 * (args.size or 1);
|
|
|
|
|
2020-06-08 18:34:57 -04:00
|
|
|
return string.format("animated_image[%f,%f;%f,%f;%s;%s;%d;%d]",
|
2020-06-13 07:41:56 -04:00
|
|
|
x, y,
|
|
|
|
w, h,
|
2020-06-08 18:34:57 -04:00
|
|
|
args.id or "",
|
|
|
|
args.animation.texture .. ".png",
|
|
|
|
args.animation.frame_count,
|
|
|
|
args.animation.frame_duration);
|
|
|
|
end
|
|
|
|
|
2020-06-13 07:41:56 -04:00
|
|
|
-- Create a 9-slice background formspec element
|
|
|
|
--
|
|
|
|
-- skin: A 9-slice background skin object
|
|
|
|
-- (Optional) x: The x position of the element. Defaults to 0.
|
|
|
|
-- (Optional) y: The y position of the element. Defaults to 0.
|
|
|
|
-- (Optional) w: The width of the element. Defaults to 1.
|
|
|
|
-- (Optional) h: The height of the element. Defaults to 1.
|
|
|
|
-- (Optional) size: Multiplies the width and height. Defaults to 1.
|
|
|
|
-- (Optional) fullsize: Whether or not to fill the parent formspec. Defaults to false.
|
|
|
|
--
|
|
|
|
-- Returns a formspec string
|
2020-06-02 08:01:11 -04:00
|
|
|
function gui.bg9(args)
|
2020-06-13 07:41:56 -04:00
|
|
|
local x = args.x or 0;
|
|
|
|
local y = args.y or 0;
|
|
|
|
local w = args.w or 1 * (args.size or 1);
|
|
|
|
local h = args.h or 1 * (args.size or 1);
|
|
|
|
|
2020-06-02 19:36:33 -04:00
|
|
|
return string.format("background9[%f,%f;%f,%f;%s;%s;%s]",
|
2020-06-13 07:41:56 -04:00
|
|
|
x, y,
|
|
|
|
w, h,
|
2020-06-02 19:36:33 -04:00
|
|
|
args.skin.texture .. ".png",
|
2021-11-24 06:37:28 -05:00
|
|
|
args.fullsize or "false",
|
2020-06-02 19:36:33 -04:00
|
|
|
tostring(args.skin.radius));
|
2020-06-02 08:01:11 -04:00
|
|
|
end
|
|
|
|
|
2020-06-13 07:41:56 -04:00
|
|
|
-- Create a button formspec element
|
|
|
|
--
|
|
|
|
-- (Optional) text: The text to display on the button
|
|
|
|
-- (Optional) x: The x position of the element. Defaults to 0.
|
|
|
|
-- (Optional) y: The y position of the element. Defaults to 0.
|
|
|
|
-- (Optional) w: The width of the element. Defaults to 1.
|
|
|
|
-- (Optional) h: The height of the element. Defaults to 1.
|
|
|
|
-- (Optional) size: Multiplies the width and height. Defaults to 1.
|
|
|
|
-- (Optional) id: The element id
|
|
|
|
-- (Optional) tooltip: The tooltip to display when hovering this element.
|
|
|
|
-- (Optional) disabled: Replaces the id with "disabled_button", allowing it to
|
|
|
|
-- receive a specific style
|
|
|
|
--
|
|
|
|
-- Returns a formspec string
|
2020-06-02 08:01:11 -04:00
|
|
|
function gui.button(args)
|
2020-06-13 07:41:56 -04:00
|
|
|
local x = args.x or 0;
|
|
|
|
local y = args.y or 0;
|
|
|
|
local w = args.w or 1 * (args.size or 1);
|
|
|
|
local h = args.h or 1 * (args.size or 1);
|
|
|
|
|
2020-06-07 18:09:58 -04:00
|
|
|
if args.disabled then
|
2020-06-13 07:41:56 -04:00
|
|
|
return string.format("button[%f,%f;%f,%f;disabled_button;%s]", x, y, w, h, args.text or "");
|
2020-06-07 18:09:58 -04:00
|
|
|
end
|
|
|
|
|
2020-06-13 07:41:56 -04:00
|
|
|
local data = string.format("button[%f,%f;%f,%f;%s;%s]", x, y, w, h, args.id or "", args.text or "");
|
2020-06-02 08:01:11 -04:00
|
|
|
|
|
|
|
if args.tooltip then
|
2020-06-13 07:41:56 -04:00
|
|
|
if args.id and not args.disabled then
|
|
|
|
data = data .. gui.tooltip {
|
|
|
|
id = args.id,
|
|
|
|
text = args.tooltip
|
|
|
|
};
|
|
|
|
else
|
|
|
|
data = data .. gui.tooltip {
|
|
|
|
x = x,
|
|
|
|
y = y,
|
|
|
|
w = w,
|
|
|
|
h = h,
|
|
|
|
text = args.tooltip
|
|
|
|
};
|
|
|
|
end
|
2020-06-02 08:01:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
return data;
|
|
|
|
end
|
|
|
|
|
2020-06-13 07:41:56 -04:00
|
|
|
-- Create a formspec container
|
|
|
|
--
|
|
|
|
-- (Optional) x: The x offset of the container. Defaults to 0.
|
|
|
|
-- (Optional) y: The y offset of the container. Defaults to 0.
|
|
|
|
-- (Optional) w: The width of the container (for drawing a background). Defaults to 1.
|
|
|
|
-- (Optional) h: The height of the container (for drawing a background). Defaults to 1.
|
|
|
|
-- (Optional) size: Multiplies the width and height. Defaults to 1.
|
|
|
|
-- (Optional) bg: A 9-slice background skin object
|
|
|
|
--
|
|
|
|
-- Additional arguments are added as the container's child elements
|
|
|
|
--
|
|
|
|
-- Returns a formspec string
|
2020-06-07 18:09:58 -04:00
|
|
|
function gui.container(args)
|
2020-06-13 07:41:56 -04:00
|
|
|
local x = args.x or 0;
|
|
|
|
local y = args.y or 0;
|
|
|
|
local w = args.w or 1 * (args.size or 1);
|
|
|
|
local h = args.h or 1 * (args.size or 1);
|
|
|
|
|
|
|
|
local data = string.format("container[%f,%f]", x, y);
|
2020-06-07 20:35:17 -04:00
|
|
|
|
|
|
|
if args.bg then
|
|
|
|
data = data .. gui.bg9 {
|
|
|
|
x = 0,
|
|
|
|
y = 0,
|
2020-06-13 07:41:56 -04:00
|
|
|
w = w,
|
|
|
|
h = h,
|
2020-06-07 20:35:17 -04:00
|
|
|
|
|
|
|
skin = args.bg,
|
|
|
|
};
|
|
|
|
end
|
|
|
|
|
2020-06-07 18:09:58 -04:00
|
|
|
for _,element in ipairs(args) do
|
|
|
|
data = data .. element;
|
|
|
|
end
|
|
|
|
|
|
|
|
return data .. "container_end[]";
|
|
|
|
end
|
|
|
|
|
2020-06-13 07:41:56 -04:00
|
|
|
-- Create an image formspec element
|
|
|
|
--
|
|
|
|
-- image: The image to display.
|
|
|
|
-- (Optional) x: The x offset of the container. Defaults to 0.
|
|
|
|
-- (Optional) y: The y offset of the container. Defaults to 0.
|
|
|
|
-- (Optional) w: The width of the container (for drawing a background). Defaults to 1.
|
|
|
|
-- (Optional) h: The height of the container (for drawing a background). Defaults to 1.
|
|
|
|
-- (Optional) size: Multiplies the width and height. Defaults to 1.
|
|
|
|
--
|
|
|
|
-- Returns a formspec string
|
2020-06-07 18:09:58 -04:00
|
|
|
function gui.image(args)
|
2020-06-13 07:41:56 -04:00
|
|
|
local x = args.x or 0;
|
|
|
|
local y = args.y or 0;
|
|
|
|
local w = args.w or 1 * (args.size or 1);
|
|
|
|
local h = args.h or 1 * (args.size or 1);
|
|
|
|
|
|
|
|
return string.format("image[%f,%f;%f,%f;%s]", x, y, w, h, args.image);
|
2020-06-07 18:09:58 -04:00
|
|
|
end
|
|
|
|
|
2020-06-13 07:41:56 -04:00
|
|
|
-- Create an image button formspec element
|
|
|
|
--
|
|
|
|
-- image: The image to display on the button
|
|
|
|
-- (Optional) text: The text to display on the button
|
|
|
|
-- (Optional) x: The x position of the element. Defaults to 0.
|
|
|
|
-- (Optional) y: The y position of the element. Defaults to 0.
|
|
|
|
-- (Optional) w: The width of the element. Defaults to 1.
|
|
|
|
-- (Optional) h: The height of the element. Defaults to 1.
|
|
|
|
-- (Optional) size: Multiplies the width and height. Defaults to 1.
|
|
|
|
-- (Optional) id: The element id
|
|
|
|
-- (Optional) tooltip: The tooltip to display when hovering this element.
|
|
|
|
-- (Optional) disabled: Replaces the id with "disabled_button", allowing it to
|
|
|
|
-- receive a specific style
|
|
|
|
--
|
|
|
|
-- Returns a formspec string
|
2020-06-02 08:01:11 -04:00
|
|
|
function gui.image_button(args)
|
2020-06-13 07:41:56 -04:00
|
|
|
local x = args.x or 0;
|
|
|
|
local y = args.y or 0;
|
|
|
|
local w = args.w or 1 * (args.size or 1);
|
|
|
|
local h = args.h or 1 * (args.size or 1);
|
|
|
|
|
|
|
|
if args.disabled then
|
|
|
|
return string.format("image_button[%f,%f;%f,%f;%s;disabled_button;%s]",
|
|
|
|
x, y,
|
|
|
|
w, h,
|
|
|
|
args.image,
|
|
|
|
args.text or "");
|
|
|
|
end
|
|
|
|
|
2020-06-02 19:36:33 -04:00
|
|
|
local data = string.format("image_button[%f,%f;%f,%f;%s;%s;%s]",
|
2020-06-13 07:41:56 -04:00
|
|
|
x, y,
|
|
|
|
w, h,
|
2020-06-02 19:36:33 -04:00
|
|
|
args.image,
|
2020-06-13 07:41:56 -04:00
|
|
|
args.id or "",
|
2020-06-02 19:36:33 -04:00
|
|
|
args.text or "");
|
2020-06-02 08:01:11 -04:00
|
|
|
|
|
|
|
if args.tooltip then
|
2020-06-13 07:41:56 -04:00
|
|
|
if args.id and not args.disabled then
|
|
|
|
data = data .. gui.tooltip {
|
|
|
|
id = args.id,
|
|
|
|
text = args.tooltip
|
|
|
|
};
|
|
|
|
else
|
|
|
|
data = data .. gui.tooltip {
|
|
|
|
x = x,
|
|
|
|
y = y,
|
|
|
|
w = w,
|
|
|
|
h = h,
|
|
|
|
text = args.tooltip
|
|
|
|
};
|
|
|
|
end
|
2020-06-02 08:01:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
return data;
|
|
|
|
end
|
|
|
|
|
2020-06-13 07:41:56 -04:00
|
|
|
-- Create an inventory list formspec element
|
|
|
|
--
|
|
|
|
-- location: The location of the inventory
|
|
|
|
-- id: The id of the inventory list
|
|
|
|
-- w: The number of columns in the inventory list
|
|
|
|
-- h: The number of rows in the inventory list
|
|
|
|
-- (Optional) x: The x position of the element. Defaults to 0.
|
|
|
|
-- (Optional) y: The y position of the element. Defaults to 0.
|
|
|
|
-- (Optional) bg: A 9-slice background skin object (To display under each slot)
|
2021-11-09 07:36:25 -05:00
|
|
|
-- (Optional) slot_w: The width of each inventory slot. Defaults to 1.
|
|
|
|
-- (Optional) slot_h: The height of each inventory slot. Defaults to 1.
|
|
|
|
-- (Optional) slot_column_margin: The horizontal margin between slots. Defaults to 0.25
|
|
|
|
-- (Optional) slot_row_margin: The vertical margin between slots. Defaults to 0.25
|
|
|
|
-- (Optional) slot_h: The height of each inventory slot. Defaults to 1.
|
2020-06-13 07:41:56 -04:00
|
|
|
-- (Optional) tooltip: The tooltip to display when hovering this element.
|
|
|
|
--
|
|
|
|
-- Returns a formspec string
|
2020-06-07 18:09:58 -04:00
|
|
|
function gui.inventory(args)
|
|
|
|
local data = "";
|
|
|
|
|
2021-11-09 07:36:25 -05:00
|
|
|
local slot_metrics = {
|
|
|
|
w = args.slot_w or 1,
|
|
|
|
h = args.slot_h or 1,
|
|
|
|
column_margin = args.slot_column_margin or 0.25,
|
|
|
|
row_margin = args.slot_row_margin or 0.25,
|
|
|
|
}
|
|
|
|
|
2020-06-07 18:09:58 -04:00
|
|
|
if args.bg then
|
|
|
|
for i = 0,args.w - 1 do
|
|
|
|
for j = 0,args.h - 1 do
|
|
|
|
data = data .. gui.bg9 {
|
2021-11-09 07:36:25 -05:00
|
|
|
x = args.x + (i * (slot_metrics.w + slot_metrics.column_margin)),
|
|
|
|
y = args.y + (j * (slot_metrics.h + slot_metrics.row_margin)),
|
|
|
|
w = slot_metrics.w,
|
|
|
|
h = slot_metrics.h,
|
2020-06-07 18:09:58 -04:00
|
|
|
|
|
|
|
skin = args.bg,
|
|
|
|
};
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-09 07:36:25 -05:00
|
|
|
data = data .. gui.style_type { selector = "list", properties = slot_metrics }
|
|
|
|
.. string.format("listcolors[#00000000;#00000022] list[%s;%s;%f,%f;%f,%f;]",
|
2020-06-13 07:41:56 -04:00
|
|
|
args.location, args.id,
|
|
|
|
args.x or 0, args.y or 0,
|
|
|
|
args.w, args.h);
|
2020-06-07 18:09:58 -04:00
|
|
|
|
|
|
|
if args.tooltip then
|
|
|
|
data = data .. gui.tooltip {
|
|
|
|
x = args.x,
|
|
|
|
y = args.y,
|
|
|
|
w = args.w,
|
|
|
|
h = args.h,
|
|
|
|
text = args.tooltip,
|
|
|
|
};
|
|
|
|
end
|
|
|
|
|
|
|
|
return data;
|
|
|
|
end
|
|
|
|
|
2020-06-13 07:41:56 -04:00
|
|
|
-- Create a label formspec element
|
|
|
|
--
|
|
|
|
-- text: The text of the label
|
|
|
|
-- (Optional) textcolor: The color of the label
|
|
|
|
-- (Optional) x: The x position of the element. Defaults to 0.
|
|
|
|
-- (Optional) y: The y position of the element. Defaults to 0.
|
|
|
|
--
|
|
|
|
-- Returns a formspec string
|
2020-06-02 08:01:11 -04:00
|
|
|
function gui.label(args)
|
2020-06-07 18:09:58 -04:00
|
|
|
if args.textcolor then
|
|
|
|
return string.format("label[%f,%f;%s%s]",
|
2020-06-13 07:41:56 -04:00
|
|
|
args.x or 0,
|
|
|
|
args.y or 0,
|
2020-06-07 18:09:58 -04:00
|
|
|
minetest.get_color_escape_sequence(args.textcolor),
|
|
|
|
args.text);
|
|
|
|
end
|
|
|
|
|
2020-06-13 07:41:56 -04:00
|
|
|
return string.format("label[%f,%f;%s]", args.x or 0, args.y or 0, args.text);
|
2020-06-02 08:01:11 -04:00
|
|
|
end
|
|
|
|
|
2020-06-13 07:41:56 -04:00
|
|
|
local function style_internal(selector, properties)
|
2020-06-02 08:01:11 -04:00
|
|
|
local data = "[" .. selector;
|
|
|
|
for name,value in pairs(properties) do
|
|
|
|
data = data .. string.format(";%s=%s", name, tostring(value));
|
|
|
|
end
|
|
|
|
return data .. "]";
|
|
|
|
end
|
|
|
|
|
2020-06-13 07:41:56 -04:00
|
|
|
-- Create a formspec style
|
|
|
|
--
|
|
|
|
-- selector: A valid comma-separated list of id-based style selectors
|
|
|
|
-- properties: A table of property names and values
|
|
|
|
--
|
|
|
|
-- Returns a formspec string
|
2020-06-02 08:01:11 -04:00
|
|
|
function gui.style(args)
|
2020-06-13 07:41:56 -04:00
|
|
|
return "style" .. style_internal(args.selector, args.properties);
|
2020-06-02 08:01:11 -04:00
|
|
|
end
|
|
|
|
|
2020-06-13 07:41:56 -04:00
|
|
|
-- Create a formspec style
|
|
|
|
--
|
|
|
|
-- selector: A valid comma-separated list of type-based style selectors
|
|
|
|
-- properties: A table of property names and values
|
|
|
|
--
|
|
|
|
-- Returns a formspec string
|
2020-06-02 08:01:11 -04:00
|
|
|
function gui.style_type(args)
|
2020-06-13 07:41:56 -04:00
|
|
|
return "style_type" .. style_internal(args.selector, args.properties);
|
2020-06-02 08:01:11 -04:00
|
|
|
end
|
|
|
|
|
2020-06-13 07:41:56 -04:00
|
|
|
-- Create a formspec tooltip element
|
|
|
|
--
|
|
|
|
-- text: The text of the tooltip
|
|
|
|
-- (Optional) id: The ID of the element to display on
|
|
|
|
--
|
|
|
|
-- (Required when id == nil) x: The x position of the element
|
|
|
|
-- (Required when id == nil) y: The y position of the element
|
|
|
|
-- (Required when id == nil) w: The width of the element
|
|
|
|
-- (Required when id == nil) h: The height of the element
|
|
|
|
--
|
|
|
|
-- Returns a formspec string
|
2020-06-02 08:01:11 -04:00
|
|
|
function gui.tooltip(args)
|
2020-06-07 18:09:58 -04:00
|
|
|
if args.id then
|
|
|
|
return string.format("tooltip[%s;%s]", args.id, args.text);
|
|
|
|
else
|
|
|
|
return string.format("tooltip[%f,%f;%f,%f;%s]", args.x, args.y, args.w, args.h, args.text);
|
|
|
|
end
|
2020-06-02 08:01:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
return gui;
|