Move formspec helpers to fslib
parent
c10ef49e58
commit
f705f446b5
|
@ -1,8 +1,8 @@
|
||||||
globals = {
|
globals = {
|
||||||
"epidermis",
|
"epidermis",
|
||||||
minetest = {
|
minetest = {
|
||||||
"dynamic_add_media" -- overridden
|
"dynamic_add_media", -- overridden
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
read_globals = {
|
read_globals = {
|
||||||
|
@ -12,5 +12,6 @@ read_globals = {
|
||||||
"vector",
|
"vector",
|
||||||
-- Mods
|
-- Mods
|
||||||
"modlib",
|
"modlib",
|
||||||
"moblib"
|
"moblib",
|
||||||
}
|
"fslib",
|
||||||
|
}
|
||||||
|
|
|
@ -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", {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})
|
table.insert(fs, {"field_close_on_enter", "field_" .. component_short; true})
|
||||||
end
|
end
|
||||||
epidermis.show_formspec(player, fs, function(fields)
|
fslib.show_formspec(player, fs, function(fields)
|
||||||
if fields.random then
|
if fields.random then
|
||||||
color = modlib.minetest.colorspec.new{
|
color = modlib.minetest.colorspec.new{
|
||||||
r = math.random(0, 255),
|
r = math.random(0, 255),
|
||||||
|
|
173
formspec.lua
173
formspec.lua
|
@ -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
|
|
4
help.lua
4
help.lua
|
@ -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, ...)
|
local function item_(name, title, ...)
|
||||||
return {
|
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}},
|
{"size", {8.5, 5.25, false}},
|
||||||
{"real_coordinates", true},
|
{"real_coordinates", true},
|
||||||
{"image_button_exit", {7.75, 0.25}; {0.5, 0.5}; "epidermis_cross.png"; "close"; ""},
|
{"image_button_exit", {7.75, 0.25}; {0.5, 0.5}; "epidermis_cross.png"; "close"; ""},
|
||||||
|
|
1
init.lua
1
init.lua
|
@ -7,7 +7,6 @@ include"dynamic_add_media.lua"
|
||||||
include"persistence.lua"
|
include"persistence.lua"
|
||||||
include"theme.lua"
|
include"theme.lua"
|
||||||
include"send_notification.lua"
|
include"send_notification.lua"
|
||||||
include"formspec.lua"
|
|
||||||
include"colorpicker_rgb_formspec.lua"
|
include"colorpicker_rgb_formspec.lua"
|
||||||
include"colorpicker_hsv_ingame.lua"
|
include"colorpicker_hsv_ingame.lua"
|
||||||
local http = assert(minetest.request_http_api(), "add epidermis to secure.http_mods")
|
local http = assert(minetest.request_http_api(), "add epidermis to secure.http_mods")
|
||||||
|
|
4
mod.conf
4
mod.conf
|
@ -1,7 +1,7 @@
|
||||||
name = epidermis
|
name = epidermis
|
||||||
description = Feature-fledged skin (painting) mod
|
description = Feature-fledged skin (painting) mod
|
||||||
depends = modlib, moblib
|
depends = modlib, moblib, fslib
|
||||||
optional_depends = visible_wielditem, player_api, nc_player_model, nc_skins
|
optional_depends = visible_wielditem, player_api, nc_player_model, nc_skins
|
||||||
author = appguru(eu)
|
author = appguru(eu)
|
||||||
license = MIT
|
license = MIT
|
||||||
min_minetest_version = 5.4
|
min_minetest_version = 5.4
|
||||||
|
|
|
@ -483,7 +483,7 @@ function def:_show_control_panel(player)
|
||||||
x = x + 0.25
|
x = x + 0.25
|
||||||
end
|
end
|
||||||
fs[1] = {"size", {x, 1, false}}
|
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
|
if fields.backface_culling then
|
||||||
self:_set_backface_culling(not self._.backface_culling)
|
self:_set_backface_culling(not self._.backface_culling)
|
||||||
elseif fields.rotation_random then
|
elseif fields.rotation_random then
|
||||||
|
@ -514,7 +514,7 @@ end
|
||||||
function def:_show_texture_preview(player)
|
function def:_show_texture_preview(player)
|
||||||
local fs_content_width = 8
|
local fs_content_width = 8
|
||||||
local image_height = fs_content_width * (self._.height / self._.width) --[fs units]
|
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}},
|
{"size", {fs_content_width + 0.5, image_height + 1.25, false}},
|
||||||
{"real_coordinates", true},
|
{"real_coordinates", true},
|
||||||
{"label", {0.25, 0.5}; "Texture Preview:"},
|
{"label", {0.25, 0.5}; "Texture Preview:"},
|
||||||
|
@ -530,7 +530,7 @@ function def:_show_texture_preview(player)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
local delete_confirmation_fs = epidermis.build_formspec{
|
local delete_confirmation_fs = fslib.build_formspec{
|
||||||
{"size", {6, 1, false}},
|
{"size", {6, 1, false}},
|
||||||
{"real_coordinates", true},
|
{"real_coordinates", true},
|
||||||
{"label", {0.25, 0.5}; "Irreversably delete paintable?"},
|
{"label", {0.25, 0.5}; "Irreversably delete paintable?"},
|
||||||
|
@ -540,7 +540,7 @@ local delete_confirmation_fs = epidermis.build_formspec{
|
||||||
{"tooltip", "close", "Close"},
|
{"tooltip", "close", "Close"},
|
||||||
}
|
}
|
||||||
function def:_show_delete_formspec(player)
|
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
|
if fields.confirm then
|
||||||
self:_delete()
|
self:_delete()
|
||||||
end
|
end
|
||||||
|
@ -549,7 +549,7 @@ end
|
||||||
|
|
||||||
function def:_show_upload_formspec(player, message)
|
function def:_show_upload_formspec(player, message)
|
||||||
local context = {}
|
local context = {}
|
||||||
epidermis.show_formspec(player, {
|
fslib.show_formspec(player, {
|
||||||
{"size", {7.5, 4.75, false}},
|
{"size", {7.5, 4.75, false}},
|
||||||
{"real_coordinates", true},
|
{"real_coordinates", true},
|
||||||
{"label", {0.25, 0.5}; "Upload to SkinDB: " .. (message or "")},
|
{"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!"))
|
"Please fill out the form!"))
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
epidermis.close_formspec(player)
|
fslib.close_formspec(player)
|
||||||
local player_name = player:get_player_name()
|
local player_name = player:get_player_name()
|
||||||
if not minetest.get_player_privs(player_name).epidermis_upload then
|
if not minetest.get_player_privs(player_name).epidermis_upload then
|
||||||
epidermis.send_notification(player, 'Missing "epidermis_upload" privilege!', "error")
|
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
|
{"label", {3.5, 3.25}; context.message
|
||||||
or (skin.deleted and minetest.colorize(epidermis.colors.error:to_string(), "This skin was deleted!")) or ""},
|
or (skin.deleted and minetest.colorize(epidermis.colors.error:to_string(), "This skin was deleted!")) or ""},
|
||||||
-- HACK use hypertext for right-aligned text
|
-- HACK use hypertext for right-aligned text
|
||||||
{"hypertext", {4.75, 4.45}; {2, 0.7}; "_of"; epidermis.hypertext_root{
|
{"hypertext", {4.75, 4.45}; {2, 0.7}; "_of"; fslib.hypertext_root{
|
||||||
epidermis.hypertext_tags.global{valign = "middle", halign = "right"},
|
fslib.hypertext_tags.global{valign = "middle", halign = "right"},
|
||||||
("%d/%d"):format(context.index, #context.results)
|
("%d/%d"):format(context.index, #context.results)
|
||||||
}},
|
}},
|
||||||
{"image_button", {6.75, 4.5}; {0.5, 0.5}; epidermis.textures.dice, "random"; ""},
|
{"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
|
end
|
||||||
local function show_formspec()
|
local function show_formspec()
|
||||||
epidermis.show_formspec(player, get_formspec(), function(fields)
|
fslib.show_formspec(player, get_formspec(), function(fields)
|
||||||
if fields.set then
|
if fields.set then
|
||||||
local selected_skin = context.results[context.index]
|
local selected_skin = context.results[context.index]
|
||||||
if selected_skin.deleted then
|
if selected_skin.deleted then
|
||||||
|
|
Loading…
Reference in New Issue