Allow paintable deletion

master
Lars Mueller 2022-01-15 15:45:40 +01:00
parent 73eba66b0a
commit 22b12126e1
3 changed files with 65 additions and 17 deletions

View File

@ -292,8 +292,12 @@ function def:on_activate()
end, true)
end
-- TODO (engine change needed) remove directory using `minetest.rmdir(self:_get_dir_path())` on object removal
-- TODO (engine change needed) call this on object removal
-- See https://github.com/minetest/minetest/pull/11931
function def:_delete()
epidermis.mark_for_deletion(self._.id)
self.object:remove()
end
function def:_get_intersection_infos(mt_pos, mt_direction)
local intersection_infos = {}
@ -404,7 +408,7 @@ function def:_show_control_panel(player)
end
local backface_culling = self._.backface_culling
epidermis.show_formspec(player, table.concat{
"size[5.5,1,false]",
"size[6.25,1,false]",
"real_coordinates[true]",
image_button(true, 0.25, "backface_culling", (backface_culling and "backface_visible" or "backface_hidden"),
(backface_culling and "Show" or "Hide") .. " back faces"),
@ -415,7 +419,8 @@ function def:_show_control_panel(player)
image_button(false, 2.75, "preview_texture", "checker", "Open texture preview"),
image_button(false, 3.5, "upload", "upload", "Upload to SkinDB"),
image_button(false, 4, "download", "download", "Pick from SkinDB"),
image_button(true, 4.75, "close", "cross", "Close"),
image_button(false, 4.75, "delete", "bin", "Delete"),
image_button(true, 5.5, "close", "cross", "Close"),
}, function(fields)
if fields.backface_culling then
self:_set_backface_culling(not self._.backface_culling)
@ -434,6 +439,8 @@ function def:_show_control_panel(player)
end)
elseif fields.preview_texture then
self:_show_texture_preview(player)
elseif fields.delete then
self:_show_delete_formspec(player)
elseif fields.upload then
self:_show_upload_formspec(player)
elseif fields.download then
@ -461,6 +468,22 @@ function def:_show_texture_preview(player)
end)
end
function def:_show_delete_formspec(player)
epidermis.show_formspec(player, table.concat{
"size[6,1,false]",
"real_coordinates[true]",
"label[0.25,0.5;Irreversably delete paintable?]",
"image_button_exit[4.75,0.25;0.5,0.5;epidermis_check.png;confirm;]";
"tooltip[confirm;Confirm]",
"image_button_exit[5.25,0.25;0.5,0.5;epidermis_cross.png;close;]",
"tooltip[close;Close]",
}, function(fields)
if fields.confirm then
self:_delete()
end
end)
end
function def:_show_upload_formspec(player, message)
local context = {}
epidermis.show_formspec(player, table.concat{

View File

@ -55,31 +55,48 @@ end)
-- Remove unused textures & store highest texture ID
local epidermi_texture_path = epidermis.paths.dynamic_textures.epidermi
for _, dirname in ipairs(minetest.get_dir_list(epidermi_texture_path, true)) do
local dir_path = concat_path{epidermi_texture_path, dirname}
local highest_number
local last_filename
local used = false
local function remove_if_unused(filename)
if not used_textures[filename] then
assert(os.remove(concat_path{epidermi_texture_path, dirname, filename}))
if used_textures[filename] then
used = true
else
assert(os.remove(concat_path{dir_path, filename}))
end
end
for _, filename in ipairs(minetest.get_dir_list(concat_path{epidermi_texture_path, dirname}, false)) do
local number = filename:match("^" .. modlib.text.escape_magic_chars(dirname) .. "_(%d+)%.png$")
if number then
number = tonumber(number)
if last_filename then
if number > highest_number then
remove_if_unused(last_filename)
local filenames = minetest.get_dir_list(dir_path, false)
local delete = modlib.table.contains(filenames, "delete")
if delete then
-- Move deletion marker to end through a swap so that it is deleted last
filenames[#filenames], filenames[delete] = filenames[delete], filenames[#filenames]
end
for _, filename in ipairs(filenames) do
if delete then
remove_if_unused(filename)
else
local number = filename:match("^" .. modlib.text.escape_magic_chars(dirname) .. "_(%d+)%.png$")
if number then
number = tonumber(number)
if last_filename then
if number > highest_number then
remove_if_unused(last_filename)
highest_number = number
last_filename = filename
else
remove_if_unused(filename)
end
else
highest_number = number
last_filename = filename
else
remove_if_unused(filename)
end
else
highest_number = number
last_filename = filename
end
end
end
if not used and (delete or #filenames == 0) then
assert(os.remove(dir_path))
end
end
function epidermis.get_epidermis_path(paintable_id, texture_id)
@ -117,6 +134,14 @@ function epidermis.write_epidermis(paintable_id, texture_id, raw_png_data)
return path, texture_name
end
function epidermis.mark_for_deletion(paintable_id)
assert(modlib.file.write_unsafe(concat_path{
epidermi_texture_path,
("epidermis_paintable_%d"):format(paintable_id),
"delete"
}, ""))
end
-- SkinDB
function epidermis.write_skindb_skin(id, raw_png_data, meta_data)

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 B