mirror of
https://github.com/minetest-mods/mesecons.git
synced 2024-08-20 13:04:10 +02:00
FPGA: Remove formspec from metadata
This commit is contained in:
parent
e78bbd6f98
commit
0dd530312b
@ -1,5 +1,7 @@
|
|||||||
local plg = {}
|
local plg = {}
|
||||||
plg.rules = {}
|
plg.rules = {}
|
||||||
|
-- per-player formspec positions
|
||||||
|
plg.open_formspecs = {}
|
||||||
|
|
||||||
|
|
||||||
local lcore = dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/logic.lua")
|
local lcore = dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/logic.lua")
|
||||||
@ -93,16 +95,20 @@ plg.register_nodes({
|
|||||||
|
|
||||||
meta:set_string("instr", lcore.serialize(is))
|
meta:set_string("instr", lcore.serialize(is))
|
||||||
meta:set_int("valid", 0)
|
meta:set_int("valid", 0)
|
||||||
meta:set_string("formspec", plg.to_formspec_string(is))
|
|
||||||
meta:set_string("infotext", "FPGA")
|
meta:set_string("infotext", "FPGA")
|
||||||
end,
|
end,
|
||||||
on_receive_fields = function(pos, formname, fields, sender)
|
on_rightclick = function(pos, node, clicker)
|
||||||
if fields.program == nil then return end -- we only care when the user clicks "Program"
|
if not minetest.is_player(clicker) then
|
||||||
|
return
|
||||||
|
end
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
local is = plg.from_formspec_fields(fields)
|
local name = clicker:get_player_name()
|
||||||
|
-- Erase formspecs of old FPGAs
|
||||||
|
meta:set_string("formspec", "")
|
||||||
|
|
||||||
meta:set_string("instr", lcore.serialize(is))
|
plg.open_formspecs[name] = pos
|
||||||
plg.update_formspec(pos, is)
|
local is = lcore.deserialize(meta:get_string("instr"))
|
||||||
|
minetest.show_formspec(name, "mesecons:fpga", plg.to_formspec_string(is, nil))
|
||||||
end,
|
end,
|
||||||
sounds = default.node_sound_stone_defaults(),
|
sounds = default.node_sound_stone_defaults(),
|
||||||
mesecons = {
|
mesecons = {
|
||||||
@ -116,6 +122,12 @@ plg.register_nodes({
|
|||||||
},
|
},
|
||||||
after_dig_node = function(pos, node)
|
after_dig_node = function(pos, node)
|
||||||
mesecon.receptor_off(pos, plg.rules[node.name])
|
mesecon.receptor_off(pos, plg.rules[node.name])
|
||||||
|
for name, open_pos in pairs(plg.open_formspecs) do
|
||||||
|
if vector.equals(pos, open_pos) then
|
||||||
|
minetest.close_formspec(name, "mesecons:fpga")
|
||||||
|
plg.open_formspecs[name] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
on_blast = mesecon.on_blastnode,
|
on_blast = mesecon.on_blastnode,
|
||||||
on_rotate = function(pos, node, user, mode)
|
on_rotate = function(pos, node, user, mode)
|
||||||
@ -153,13 +165,12 @@ plg.register_nodes({
|
|||||||
end
|
end
|
||||||
|
|
||||||
meta:set_string("instr", lcore.serialize(instr))
|
meta:set_string("instr", lcore.serialize(instr))
|
||||||
plg.update_formspec(pos, instr)
|
plg.update_meta(pos, instr)
|
||||||
return true
|
return true
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
plg.to_formspec_string = function(is, err)
|
||||||
plg.to_formspec_string = function(is)
|
|
||||||
local function dropdown_op(x, y, name, val)
|
local function dropdown_op(x, y, name, val)
|
||||||
local s = "dropdown[" .. tostring(x) .. "," .. tostring(y) .. ";"
|
local s = "dropdown[" .. tostring(x) .. "," .. tostring(y) .. ";"
|
||||||
.. "0.75,0.5;" .. name .. ";" -- the height seems to be ignored?
|
.. "0.75,0.5;" .. name .. ";" -- the height seems to be ignored?
|
||||||
@ -193,7 +204,7 @@ plg.to_formspec_string = function(is)
|
|||||||
end
|
end
|
||||||
local s = "size[9,9]"..
|
local s = "size[9,9]"..
|
||||||
"label[3.4,-0.15;FPGA gate configuration]"..
|
"label[3.4,-0.15;FPGA gate configuration]"..
|
||||||
"button_exit[7,7.5;2,2.5;program;Program]"..
|
"button[7,7.5;2,2.5;program;Program]"..
|
||||||
"box[4.2,0.5;0.03,7;#ffffff]"..
|
"box[4.2,0.5;0.03,7;#ffffff]"..
|
||||||
"label[0.25,0.25;op. 1]"..
|
"label[0.25,0.25;op. 1]"..
|
||||||
"label[1.0,0.25;gate type]"..
|
"label[1.0,0.25;gate type]"..
|
||||||
@ -219,6 +230,12 @@ plg.to_formspec_string = function(is)
|
|||||||
y = 1 - 0.25
|
y = 1 - 0.25
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
if err then
|
||||||
|
local fmsg = minetest.colorize("#ff0000", minetest.formspec_escape(err.msg))
|
||||||
|
s = s .. plg.red_box_around(err.i) ..
|
||||||
|
"label[0.25,8.25;The gate configuration is erroneous in the marked area:]"..
|
||||||
|
"label[0.25,8.5;" .. fmsg .. "]"
|
||||||
|
end
|
||||||
return s
|
return s
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -251,12 +268,11 @@ plg.from_formspec_fields = function(fields)
|
|||||||
return is
|
return is
|
||||||
end
|
end
|
||||||
|
|
||||||
plg.update_formspec = function(pos, is)
|
plg.update_meta = function(pos, is)
|
||||||
if type(is) == "string" then -- serialized string
|
if type(is) == "string" then -- serialized string
|
||||||
is = lcore.deserialize(is)
|
is = lcore.deserialize(is)
|
||||||
end
|
end
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
local form = plg.to_formspec_string(is)
|
|
||||||
|
|
||||||
local err = lcore.validate(is)
|
local err = lcore.validate(is)
|
||||||
if err == nil then
|
if err == nil then
|
||||||
@ -265,17 +281,20 @@ plg.update_formspec = function(pos, is)
|
|||||||
else
|
else
|
||||||
meta:set_int("valid", 0)
|
meta:set_int("valid", 0)
|
||||||
meta:set_string("infotext", "FPGA")
|
meta:set_string("infotext", "FPGA")
|
||||||
local fmsg = minetest.colorize("#ff0000", minetest.formspec_escape(err.msg))
|
|
||||||
form = form .. plg.red_box_around(err.i) ..
|
|
||||||
"label[0.25,8.25;The gate configuration is erroneous in the marked area:]"..
|
|
||||||
"label[0.25,8.5;" .. fmsg .. "]"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
meta:set_string("formspec", form)
|
|
||||||
|
|
||||||
-- reset ports and run programmed logic
|
-- reset ports and run programmed logic
|
||||||
plg.setports(pos, false, false, false, false)
|
plg.setports(pos, false, false, false, false)
|
||||||
plg.update(pos)
|
plg.update(pos)
|
||||||
|
|
||||||
|
-- Refresh open formspecs
|
||||||
|
local form = plg.to_formspec_string(is, err)
|
||||||
|
for name, open_pos in pairs(plg.open_formspecs) do
|
||||||
|
if vector.equals(pos, open_pos) then
|
||||||
|
minetest.show_formspec(name, "mesecons:fpga", form)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return err
|
||||||
end
|
end
|
||||||
|
|
||||||
plg.red_box_around = function(i)
|
plg.red_box_around = function(i)
|
||||||
@ -394,6 +413,38 @@ plg.setports = function(pos, A, B, C, D) -- sets states of OUTPUT
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
|
local player_name = player:get_player_name()
|
||||||
|
|
||||||
|
if formname ~= "mesecons:fpga" or fields.quit then
|
||||||
|
plg.open_formspecs[player_name] = nil -- potential garbage
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if not fields.program then
|
||||||
|
return -- we only care when the user clicks "Program"
|
||||||
|
end
|
||||||
|
local pos = plg.open_formspecs[player_name]
|
||||||
|
if minetest.is_protected(pos, player_name) then
|
||||||
|
minetest.record_protection_violation(pos, player_name)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local meta = minetest.get_meta(pos)
|
||||||
|
local is = plg.from_formspec_fields(fields)
|
||||||
|
|
||||||
|
meta:set_string("instr", lcore.serialize(is))
|
||||||
|
local err = plg.update_meta(pos, is)
|
||||||
|
|
||||||
|
if not err then
|
||||||
|
plg.open_formspecs[player_name] = nil
|
||||||
|
-- Close on success
|
||||||
|
minetest.close_formspec(player_name, "mesecons:fpga")
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
minetest.register_on_leaveplayer(function(player)
|
||||||
|
plg.open_formspecs[player:get_player_name()] = nil
|
||||||
|
end)
|
||||||
|
|
||||||
minetest.register_craft({
|
minetest.register_craft({
|
||||||
output = "mesecons_fpga:fpga0000 2",
|
output = "mesecons_fpga:fpga0000 2",
|
||||||
|
@ -22,7 +22,7 @@ minetest.register_tool("mesecons_fpga:programmer", {
|
|||||||
end
|
end
|
||||||
itemstack:set_metadata(meta:get_string("instr"))
|
itemstack:set_metadata(meta:get_string("instr"))
|
||||||
minetest.chat_send_player(placer:get_player_name(), "FPGA gate configuration was successfully copied!")
|
minetest.chat_send_player(placer:get_player_name(), "FPGA gate configuration was successfully copied!")
|
||||||
|
|
||||||
return itemstack
|
return itemstack
|
||||||
end,
|
end,
|
||||||
on_use = function(itemstack, user, pointed_thing)
|
on_use = function(itemstack, user, pointed_thing)
|
||||||
@ -34,17 +34,22 @@ minetest.register_tool("mesecons_fpga:programmer", {
|
|||||||
if minetest.get_node(pos).name:find("mesecons_fpga:fpga") ~= 1 then
|
if minetest.get_node(pos).name:find("mesecons_fpga:fpga") ~= 1 then
|
||||||
return itemstack
|
return itemstack
|
||||||
end
|
end
|
||||||
|
local player_name = user:get_player_name()
|
||||||
|
if minetest.is_protected(pos, player_name) then
|
||||||
|
minetest.record_protection_violation(pos, player_name)
|
||||||
|
return itemstack
|
||||||
|
end
|
||||||
|
|
||||||
local imeta = itemstack:get_metadata()
|
local imeta = itemstack:get_metadata()
|
||||||
if imeta == "" then
|
if imeta == "" then
|
||||||
minetest.chat_send_player(user:get_player_name(), "Use shift+right-click to copy a gate configuration first.")
|
minetest.chat_send_player(player_name, "Use shift+right-click to copy a gate configuration first.")
|
||||||
return itemstack
|
return itemstack
|
||||||
end
|
end
|
||||||
|
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
meta:set_string("instr", imeta)
|
meta:set_string("instr", imeta)
|
||||||
plg.update_formspec(pos, imeta)
|
plg.update_meta(pos, imeta)
|
||||||
minetest.chat_send_player(user:get_player_name(), "Gate configuration was successfully written to FPGA!")
|
minetest.chat_send_player(player_name, "Gate configuration was successfully written to FPGA!")
|
||||||
|
|
||||||
return itemstack
|
return itemstack
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user