allow chosing variants and omitting matrix file at capture time

- use smartfs.lua newly embedded library to handle formspecs
master
entuland 2018-06-07 17:36:39 +02:00
parent 8a58f258cf
commit 2694c54c41
2 changed files with 1514 additions and 46 deletions

138
init.lua
View File

@ -3,9 +3,39 @@ wesh = {
name = "wesh", name = "wesh",
modpath = minetest.get_modpath(minetest.get_current_modname()), modpath = minetest.get_modpath(minetest.get_current_modname()),
vt_size = 72, vt_size = 72,
player_canvas = {} player_canvas = {},
forms = {},
} }
local smartfs = dofile(wesh.modpath .. "/smartfs.lua")
wesh.forms.capture = smartfs.create("wesh.forms.capture", function(state)
state:size(6, 6)
local meshname_field = state:field(0.5, 0.5, 4, 1, "meshname", "Enter the name for your mesh")
meshname_field:onKeyEnter(wesh.mesh_capture_confirmed)
meshname_field:setCloseOnEnter(false)
local capture_button = state:button(4, 0.2, 2, 1, "capture", "Capture")
capture_button:click(wesh.mesh_capture_confirmed)
-- capture_button:setClose(true)
local cancel_button = state:button(4, 5.2, 2, 1, "cancel", "Cancel")
cancel_button:setClose(true)
state:checkbox(0.5, 1, "generate_matrix", "Generate backup matrix")
state:label(0.5, 2, "label_variants", "Select one or more variants:")
local y = 2.5
for name, texture in pairs(wesh.variants) do
local chk = state:checkbox(0.5, y, "variant_" .. name, name)
chk:setValue(true)
y = y + 0.5
end
end)
-- ======================================================================== -- ========================================================================
-- initialization functions -- initialization functions
-- ======================================================================== -- ========================================================================
@ -15,7 +45,7 @@ function wesh._init()
wesh.gen_prefix = "mesh_" wesh.gen_prefix = "mesh_"
if not minetest.mkdir(wesh.temp_path) then if not minetest.mkdir(wesh.temp_path) then
error("[" .. wesh.name .. "] Unable to create folder " .. wesh.temp_path) error("[wesh] Unable to create folder " .. wesh.temp_path)
end end
wesh._init_vertex_textures() wesh._init_vertex_textures()
wesh._init_colors() wesh._init_colors()
@ -27,7 +57,6 @@ function wesh._init()
end end
function wesh._register_canvas_nodes() function wesh._register_canvas_nodes()
minetest.register_on_player_receive_fields(wesh.on_receive_fields)
local function register_canvas(index, size, inner) local function register_canvas(index, size, inner)
minetest.register_craft({ minetest.register_craft({
@ -266,39 +295,57 @@ end
function wesh.canvas_interaction(clicked_pos, node, clicker) function wesh.canvas_interaction(clicked_pos, node, clicker)
-- called when the player right-clicks on a canvas block -- called when the player right-clicks on a canvas block
wesh.player_canvas[clicker:get_player_name()] = { pos = clicked_pos, facedir = node.param2 }; wesh.player_canvas[clicker:get_player_name()] = { pos = clicked_pos, facedir = node.param2 };
local formspec = "field[meshname;Enter the name for your mesh;]field_close_on_enter[meshname;false]" wesh.forms.capture:show(clicker:get_player_name())
minetest.show_formspec(clicker:get_player_name(), "save_mesh", formspec)
end end
function wesh.on_receive_fields(player, formname, fields) function wesh.mesh_capture_confirmed(button_or_field, state)
if formname == "save_mesh" then local meshname = state:get("meshname"):getText()
local canvas = wesh.player_canvas[player:get_player_name()] local playername = state.player
canvas.node = minetest.get_node_or_nil(canvas.pos) local canvas = wesh.player_canvas[playername]
if not canvas.node then return end canvas.generate_matrix = state:get("generate_matrix"):getValue()
canvas.size = canvas.node.name:gsub(".*(%d%d)$", "%1") canvas.chosen_variants = {}
canvas.size = tonumber(canvas.size)
if not wesh.valid_canvas_sizes[canvas.size] then local no_variants = true
canvas.size = 16 for name, texture in pairs(wesh.variants) do
if state:get("variant_" .. name):getValue() then
canvas.chosen_variants[name] = texture
no_variants = false
end end
end
if no_variants then
wesh.notify(playername, "Please choose at least one variant")
return
end
canvas.node = minetest.get_node_or_nil(canvas.pos)
if not canvas.node then return end
canvas.boundary = {} canvas.size = canvas.node.name:gsub(".*(%d%d)$", "%1")
wesh.save_new_mesh(canvas, player, fields.meshname) canvas.size = tonumber(canvas.size)
if not wesh.valid_canvas_sizes[canvas.size] then
canvas.size = 16
end
canvas.boundary = {}
if wesh.save_new_mesh(canvas, playername, meshname) then
minetest.close_formspec(playername, "wesh.forms.capture")
end end
end end
function wesh.save_new_mesh(canvas, player, description) function wesh.save_new_mesh(canvas, playername, description)
local sanitized_meshname = wesh.check_plain(description) local sanitized_meshname = wesh.check_plain(description)
if sanitized_meshname:len() < 3 then if sanitized_meshname:len() < 3 then
wesh.notify(player, "Mesh name too short, try again (min. 3 chars)") wesh.notify(playername, "Mesh name too short, try again (min. 3 chars)")
return return false
end end
local obj_filename = wesh.gen_prefix .. sanitized_meshname .. ".obj" local obj_filename = wesh.gen_prefix .. sanitized_meshname .. ".obj"
for _, entry in ipairs(wesh.get_all_files()) do for _, entry in ipairs(wesh.get_all_files()) do
if entry == obj_filename then if entry == obj_filename then
wesh.notify(player, "Mesh name '" .. description .. "' already taken, pick a new one") wesh.notify(playername, "Mesh name '" .. description .. "' already taken, pick a new one")
return return false
end end
end end
@ -323,21 +370,21 @@ function wesh.save_new_mesh(canvas, player, description)
local f_section = table.concat(wesh.faces, "\n") local f_section = table.concat(wesh.faces, "\n")
local meshdata = vt_section .. v_section .. vn_section .. f_section local meshdata = vt_section .. v_section .. vn_section .. f_section
wesh.save_mesh_to_file(obj_filename, meshdata, description, player, canvas) return wesh.save_mesh_to_file(obj_filename, meshdata, description, playername, canvas)
end end
-- ======================================================================== -- ========================================================================
-- mesh management helpers -- mesh management helpers
-- ======================================================================== -- ========================================================================
function wesh.save_mesh_to_file(obj_filename, meshdata, description, player, canvas) function wesh.save_mesh_to_file(obj_filename, meshdata, description, playername, canvas)
-- save .obj file -- save .obj file
local full_filename = wesh.temp_path .. "/" .. obj_filename local full_filename = wesh.temp_path .. "/" .. obj_filename
local file, errmsg = io.open(full_filename, "wb") local file, errmsg = io.open(full_filename, "wb")
if not file then if not file then
wesh.notify(player, "Unable to write to file '" .. obj_filename .. "' from '" .. wesh.temp_path .. "' - error: " .. errmsg) wesh.notify(playername, "Unable to write to file '" .. obj_filename .. "' from '" .. wesh.temp_path .. "' - error: " .. errmsg)
return return false
end end
file:write(meshdata) file:write(meshdata)
file:close() file:close()
@ -347,25 +394,27 @@ function wesh.save_mesh_to_file(obj_filename, meshdata, description, player, can
local full_data_filename = wesh.temp_path .. "/" .. data_filename local full_data_filename = wesh.temp_path .. "/" .. data_filename
local file, errmsg = io.open(full_data_filename, "wb") local file, errmsg = io.open(full_data_filename, "wb")
if not file then if not file then
wesh.notify(player, "Unable to write to file '" .. data_filename .. "' from '" .. wesh.temp_path .. "' - error: " .. errmsg) wesh.notify(playername, "Unable to write to file '" .. data_filename .. "' from '" .. wesh.temp_path .. "' - error: " .. errmsg)
return return false
end end
file:write(wesh.prepare_data_file(description, canvas)) file:write(wesh.prepare_data_file(description, canvas))
file:close() file:close()
-- save .matrix.dat file if canvas.generate_matrix then
local matrix_data_filename = obj_filename .. ".matrix.dat" -- save .matrix.dat file
local full_matrix_data_filename = wesh.temp_path .. "/" .. matrix_data_filename local matrix_data_filename = obj_filename .. ".matrix.dat"
local file, errmsg = io.open(full_matrix_data_filename, "wb") local full_matrix_data_filename = wesh.temp_path .. "/" .. matrix_data_filename
if not file then local file, errmsg = io.open(full_matrix_data_filename, "wb")
wesh.notify(player, "Unable to write to file '" .. matrix_data_filename .. "' from '" .. wesh.temp_path .. "' - error: " .. errmsg) if not file then
return wesh.notify(playername, "Unable to write to file '" .. matrix_data_filename .. "' from '" .. wesh.temp_path .. "' - error: " .. errmsg)
return false
end
file:write(minetest.serialize(wesh.matrix))
file:close()
end end
file:write(minetest.serialize(wesh.matrix))
file:close()
wesh.notify(playername, "Mesh saved to '" .. obj_filename .. "' in '" .. wesh.temp_path .. "', reload the world to move them to the mod folder and enable them")
wesh.notify(player, "Mesh saved to '" .. obj_filename .. "' in '" .. wesh.temp_path .. "', reload the world to move them to the mod folder and enable them") return true
end end
function wesh.prepare_data_file(description, canvas) function wesh.prepare_data_file(description, canvas)
@ -374,9 +423,10 @@ function wesh.prepare_data_file(description, canvas)
for _, box in ipairs(canvas.boxes) do for _, box in ipairs(canvas.boxes) do
table.insert(boxes, wesh.box_to_collision_box(box, canvas.size)) table.insert(boxes, wesh.box_to_collision_box(box, canvas.size))
end end
local data = { local data = {
description = description, description = description,
variants = wesh.variants, variants = canvas.chosen_variants,
collision_box = { collision_box = {
type = "fixed", type = "fixed",
fixed = boxes, fixed = boxes,
@ -780,12 +830,8 @@ function wesh.merge_tables(t1, t2)
end end
end end
function wesh.notify(player, message) function wesh.notify(playername, message)
local formspec = "size[10,5]textarea[1,1;8,3;notice;Notice;" .. minetest.formspec_escape(message) .. "]" minetest.chat_send_player(playername, "[wesh] " .. message)
.. "button_exit[6,4;3,0;exit;Okay]"
local playername = player:get_player_name()
minetest.show_formspec(playername, "notice_form", formspec)
minetest.chat_send_player(playername, "[" .. wesh.name .. "] " .. message)
end end
function wesh.out_of_bounds(pos, canv_size) function wesh.out_of_bounds(pos, canv_size)

1422
smartfs.lua Normal file

File diff suppressed because it is too large Load Diff