Move formspec helpers to fslib

master
Lars Mueller 2022-06-15 15:43:35 +02:00
parent c10ef49e58
commit f705f446b5
7 changed files with 19 additions and 192 deletions

View File

@ -1,8 +1,8 @@
globals = {
"epidermis",
minetest = {
"dynamic_add_media" -- overridden
}
"dynamic_add_media", -- overridden
},
}
read_globals = {
@ -12,5 +12,6 @@ read_globals = {
"vector",
-- Mods
"modlib",
"moblib"
}
"moblib",
"fslib",
}

View File

@ -39,7 +39,7 @@ function epidermis.show_colorpicker_formspec(player, color, callback)
table.insert(fs, {"field", {7.25, y + 0.5}; {1, 0.5}; "field_" .. component_short; ""; color[component_short]})
table.insert(fs, {"field_close_on_enter", "field_" .. component_short; true})
end
epidermis.show_formspec(player, fs, function(fields)
fslib.show_formspec(player, fs, function(fields)
if fields.random then
color = modlib.minetest.colorspec.new{
r = math.random(0, 255),

View File

@ -1,173 +0,0 @@
local formspecs = {}
local id = 1
minetest.register_on_leaveplayer(function(player)
formspecs[player:get_player_name()] = nil
end)
minetest.register_on_player_receive_fields(function(player, formname, fields)
local player_name = player:get_player_name()
local formspec = formspecs[player_name]
if formname ~= (formspec or {}).name then return end
if fields.quit then
formspecs[player_name] = nil
end
formspec.handler(fields)
return true -- don't call remaining functions
end)
-- S-expression formspec AST to string; purely a syntactical string building helper unaware of semantics
local function build_formspec(formspec)
local rope = {}
local function write(text)
return table.insert(rope, text)
end
local function write_escaped(text)
return write(minetest.formspec_escape(text))
end
local function write_primitive(param, write_func)
write_func = write_func or write_escaped
if param == true then
return write_func"true"
end
if param == false then
return write_func"false"
end
local type_ = type(param)
if type_ == "number" then
return write_func((param % 1 == 0 and "%d" or "%f"):format(param))
end
if type_ == "string" then
return write_func(param)
end
error("primitive type expected, got " .. type_)
end
local function write_options(options, delim)
local options_pushed = false
for key, value in pairs(options) do
if type(key) == "string" then
if options_pushed then
write(delim)
end
write_escaped(key)
write"="
write_primitive(value)
options_pushed = true
end
end
end
local function write_hypertext_escaped(text)
return write_escaped(text:gsub(".", { ["\\"] = [[\\]], ["<"] = [[\<]] }))
end
local function write_hypertext_primitive(value)
return write_primitive(value, write_hypertext_escaped)
end
local function write_hypertext(element)
local mt = getmetatable(element) or {}
if mt.tag_name then
write"<"
write_hypertext_escaped(mt.tag_name)
for k, v in pairs(element) do
if type(k) == "string" then
write" "
write_hypertext_primitive(k)
write"="
write_hypertext_primitive(v)
end
end
write">"
end
if mt.self_enclosing then
assert(#element == 0)
return
end
for _, child in ipairs(element) do
if type(child) == "string" then
write_hypertext_primitive(child)
else
write_hypertext(child)
end
end
if mt.tag_name then
write"</"
write_hypertext_escaped(mt.tag_name)
write">"
end
end
for _, element in ipairs(formspec) do
if type(element) == "string" then -- assume top-level elements are simply formspec elements in string form
write(element)
else
write(element[1])
write"["
local len = #element
for index = 2, len do
if index > 2 then write";" end
local param = element[index]
if type(param) == "table" then
local mt = getmetatable(param)
if mt and mt.hypertext then
write_hypertext(param)
elseif param[1] == nil then -- sub-options or empty table
write_options(param, ",")
else -- comma-delimited subparam list
for subindex, subparam in ipairs(param) do
if subindex > 1 then write"," end
write_primitive(subparam)
end
end
else
write_primitive(param)
end
end
if len == 1 then -- options element or empty table
write_options(element, ";")
end
write"]"
write"\n" -- for debugging purposes (improved readability)
end
end
return table.concat(rope)
end
function epidermis.build_formspec(formspec)
if type(formspec) == "table" then
return build_formspec(formspec)
end
return formspec
end
local self_enclosing = modlib.table.set{"item", "img", "tag", "global"}
epidermis.hypertext_tags = modlib.func.memoize(function(tag_name)
local metatable = {
hypertext = true,
tag_name = tag_name,
self_enclosing = self_enclosing[tag_name]
}
return function(table)
return setmetatable(table, metatable)
end
end)
epidermis.hypertext_root = epidermis.hypertext_tags[false]
function epidermis.show_formspec(player, formspec, handler)
formspec = epidermis.build_formspec(formspec)
local player_name = player:get_player_name()
local formspec_name = "epidermis:" .. id
formspecs[player_name] = {
name = formspec_name,
handler = handler or modlib.func.no_op,
}
id = id + 1
if id > 2^50 then id = 1 end
minetest.show_formspec(player_name, formspec_name, formspec)
end
function epidermis.close_formspec(player)
local player_name = player:get_player_name()
local formspec = assert(formspecs[player_name])
formspecs[player_name] = nil
minetest.close_formspec(player_name, formspec.name)
end

View File

@ -1,4 +1,4 @@
local tags, root_tag = epidermis.hypertext_tags, epidermis.hypertext_root
local tags, root_tag = fslib.hypertext_tags, fslib.hypertext_root
local function item_(name, title, ...)
return {
@ -82,7 +82,7 @@ local help = root_tag{
),
}
local formspec = epidermis.build_formspec{
local formspec = fslib.build_formspec{
{"size", {8.5, 5.25, false}},
{"real_coordinates", true},
{"image_button_exit", {7.75, 0.25}; {0.5, 0.5}; "epidermis_cross.png"; "close"; ""},

View File

@ -7,7 +7,6 @@ include"dynamic_add_media.lua"
include"persistence.lua"
include"theme.lua"
include"send_notification.lua"
include"formspec.lua"
include"colorpicker_rgb_formspec.lua"
include"colorpicker_hsv_ingame.lua"
local http = assert(minetest.request_http_api(), "add epidermis to secure.http_mods")

View File

@ -1,7 +1,7 @@
name = epidermis
description = Feature-fledged skin (painting) mod
depends = modlib, moblib
depends = modlib, moblib, fslib
optional_depends = visible_wielditem, player_api, nc_player_model, nc_skins
author = appguru(eu)
license = MIT
min_minetest_version = 5.4
min_minetest_version = 5.4

View File

@ -483,7 +483,7 @@ function def:_show_control_panel(player)
x = x + 0.25
end
fs[1] = {"size", {x, 1, false}}
epidermis.show_formspec(player, fs, function(fields)
fslib.show_formspec(player, fs, function(fields)
if fields.backface_culling then
self:_set_backface_culling(not self._.backface_culling)
elseif fields.rotation_random then
@ -514,7 +514,7 @@ end
function def:_show_texture_preview(player)
local fs_content_width = 8
local image_height = fs_content_width * (self._.height / self._.width) --[fs units]
epidermis.show_formspec(player, {
fslib.show_formspec(player, {
{"size", {fs_content_width + 0.5, image_height + 1.25, false}},
{"real_coordinates", true},
{"label", {0.25, 0.5}; "Texture Preview:"},
@ -530,7 +530,7 @@ function def:_show_texture_preview(player)
end)
end
local delete_confirmation_fs = epidermis.build_formspec{
local delete_confirmation_fs = fslib.build_formspec{
{"size", {6, 1, false}},
{"real_coordinates", true},
{"label", {0.25, 0.5}; "Irreversably delete paintable?"},
@ -540,7 +540,7 @@ local delete_confirmation_fs = epidermis.build_formspec{
{"tooltip", "close", "Close"},
}
function def:_show_delete_formspec(player)
epidermis.show_formspec(player, delete_confirmation_fs, function(fields)
fslib.show_formspec(player, delete_confirmation_fs, function(fields)
if fields.confirm then
self:_delete()
end
@ -549,7 +549,7 @@ end
function def:_show_upload_formspec(player, message)
local context = {}
epidermis.show_formspec(player, {
fslib.show_formspec(player, {
{"size", {7.5, 4.75, false}},
{"real_coordinates", true},
{"label", {0.25, 0.5}; "Upload to SkinDB: " .. (message or "")},
@ -604,7 +604,7 @@ function def:_show_upload_formspec(player, message)
"Please fill out the form!"))
return
end
epidermis.close_formspec(player)
fslib.close_formspec(player)
local player_name = player:get_player_name()
if not minetest.get_player_privs(player_name).epidermis_upload then
epidermis.send_notification(player, 'Missing "epidermis_upload" privilege!', "error")
@ -670,8 +670,8 @@ function def:_show_picker_formspec(player)
{"label", {3.5, 3.25}; context.message
or (skin.deleted and minetest.colorize(epidermis.colors.error:to_string(), "This skin was deleted!")) or ""},
-- HACK use hypertext for right-aligned text
{"hypertext", {4.75, 4.45}; {2, 0.7}; "_of"; epidermis.hypertext_root{
epidermis.hypertext_tags.global{valign = "middle", halign = "right"},
{"hypertext", {4.75, 4.45}; {2, 0.7}; "_of"; fslib.hypertext_root{
fslib.hypertext_tags.global{valign = "middle", halign = "right"},
("%d/%d"):format(context.index, #context.results)
}},
{"image_button", {6.75, 4.5}; {0.5, 0.5}; epidermis.textures.dice, "random"; ""},
@ -683,7 +683,7 @@ function def:_show_picker_formspec(player)
}
end
local function show_formspec()
epidermis.show_formspec(player, get_formspec(), function(fields)
fslib.show_formspec(player, get_formspec(), function(fields)
if fields.set then
local selected_skin = context.results[context.index]
if selected_skin.deleted then