230 lines
12 KiB
Lua
230 lines
12 KiB
Lua
-- 5. FORMSPEC HANDLER
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
local name = player:get_player_name()
|
|
|
|
-- --- ADMIN PANEL ---
|
|
if formname:sub(1, 18) == "smart_light:admin_" then
|
|
local pos_str = formname:sub(19)
|
|
local pos = minetest.string_to_pos(pos_str)
|
|
local meta = minetest.get_meta(pos)
|
|
local area = meta:get_string("area_id")
|
|
|
|
if fields.chan_list then
|
|
local evt = minetest.explode_textlist_event(fields.chan_list)
|
|
if evt.type == "CHG" or evt.type == "DCL" then meta:set_int("selected_chan_idx", evt.index) end
|
|
end
|
|
|
|
if fields.save_admin and fields.new_area ~= "" then
|
|
meta:set_string("area_id", fields.new_area)
|
|
smart_light.registry.areas[fields.new_area] = smart_light.registry.areas[fields.new_area] or { channels = {} }
|
|
smart_light.save()
|
|
minetest.show_formspec(name, formname, smart_light.get_admin_fs(fields.new_area))
|
|
end
|
|
|
|
if fields.del_chan then
|
|
local idx = meta:get_int("selected_chan_idx")
|
|
if idx > 0 then
|
|
local channels = {}
|
|
if smart_light.registry.areas[area] then
|
|
for c_id, _ in pairs(smart_light.registry.areas[area].channels) do table.insert(channels, c_id) end
|
|
table.sort(channels)
|
|
end
|
|
local target = channels[idx]
|
|
if target then
|
|
local c_data = smart_light.registry.areas[area].channels[target]
|
|
-- Lampen Reset
|
|
for p_str, _ in pairs(c_data.nodes or {}) do
|
|
local l_pos = minetest.string_to_pos(p_str)
|
|
local l_node = minetest.get_node(l_pos)
|
|
local original = smart_light.shadow_to_original[l_node.name] or l_node.name
|
|
minetest.swap_node(l_pos, {name = original, param2 = l_node.param2})
|
|
local l_meta = minetest.get_meta(l_pos)
|
|
l_meta:set_string("sl_area", "")
|
|
l_meta:set_string("sl_chan", "")
|
|
end
|
|
-- Schalter Reset
|
|
for s_str, _ in pairs(c_data.switches or {}) do
|
|
local s_pos = minetest.string_to_pos(s_str)
|
|
minetest.swap_node(s_pos, {name = "smart_light:switch"})
|
|
local s_meta = minetest.get_meta(s_pos)
|
|
s_meta:set_string("sl_area", "")
|
|
s_meta:set_string("sl_chan", "")
|
|
s_meta:set_string("infotext", "")
|
|
end
|
|
smart_light.registry.areas[area].channels[target] = nil
|
|
smart_light.save()
|
|
meta:set_int("selected_chan_idx", 0)
|
|
minetest.show_formspec(name, formname, smart_light.get_admin_fs(area))
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- --- SCHALTER PROGRAMMIERER (REBOOTED) ---
|
|
if formname:sub(1, 20) == "smart_light:prog_sw_" then
|
|
local pos_str = formname:sub(21)
|
|
local pos = minetest.string_to_pos(pos_str)
|
|
if not pos then return end
|
|
if fields.save_switch then
|
|
local area, chan = fields.area_sel, fields.chan_sel
|
|
if area == "-- Bitte wählen --" or chan == "-- Bitte wählen --" then
|
|
minetest.chat_send_player(name, "FEHLER: Bitte Area UND Kanal auswählen!")
|
|
return
|
|
end
|
|
local meta = minetest.get_meta(pos)
|
|
meta:set_string("sl_area", area)
|
|
meta:set_string("sl_chan", chan)
|
|
meta:set_string("infotext", "Schalter: " .. area .. ":" .. chan)
|
|
if smart_light.registry.areas[area] and smart_light.registry.areas[area].channels[chan] then
|
|
smart_light.registry.areas[area].channels[chan].switches = smart_light.registry.areas[area].channels[chan].switches or {}
|
|
smart_light.registry.areas[area].channels[chan].switches[pos_str] = true
|
|
smart_light.save()
|
|
local level = smart_light.registry.areas[area].channels[chan].last_level or "max"
|
|
smart_light.update_switch_visuals(area, chan, level)
|
|
minetest.chat_send_player(name, "Schalter gespeichert!")
|
|
end
|
|
return
|
|
end
|
|
if fields.area_sel and fields.area_sel ~= "-- Bitte wählen --" then
|
|
minetest.get_meta(pos):set_string("sl_area", fields.area_sel)
|
|
minetest.registered_tools["smart_light:programmer"].on_place(player:get_wielded_item(), player, {type="node", under=pos})
|
|
end
|
|
end
|
|
|
|
-- --- NEU: BEWEGUNGSMELDER PROGRAMMIERER (Kanal-Zuweisung) ---
|
|
if formname:sub(1, 20) == "smart_light:prog_ms_" then
|
|
local pos_str = formname:sub(21)
|
|
local pos = minetest.string_to_pos(pos_str)
|
|
if not pos then return end
|
|
if fields.save_ms then
|
|
local area, chan = fields.area_sel, fields.chan_sel
|
|
if area == "-- Bitte wählen --" or chan == "-- Bitte wählen --" then
|
|
minetest.chat_send_player(name, "FEHLER: Bitte Area UND Kanal auswählen!")
|
|
return
|
|
end
|
|
local meta = minetest.get_meta(pos)
|
|
meta:set_string("sl_area", area)
|
|
meta:set_string("sl_chan", chan)
|
|
meta:set_string("infotext", "Bewegungsmelder: " .. area .. ":" .. chan)
|
|
minetest.get_node_timer(pos):start(1.5) -- Timer starten
|
|
minetest.chat_send_player(name, "Bewegungsmelder verknüpft!")
|
|
return
|
|
end
|
|
if fields.area_sel and fields.area_sel ~= "-- Bitte wählen --" then
|
|
minetest.get_meta(pos):set_string("sl_area", fields.area_sel)
|
|
minetest.registered_tools["smart_light:programmer"].on_place(player:get_wielded_item(), player, {type="node", under=pos})
|
|
end
|
|
end
|
|
|
|
-- --- NEU: BEWEGUNGSMELDER WERTE (Radius/Dauer) ---
|
|
if formname:sub(1, 19) == "smart_light:sensor_" then
|
|
if fields.save_sensor then
|
|
local pos = minetest.string_to_pos(formname:sub(20))
|
|
local meta = minetest.get_meta(pos)
|
|
meta:set_int("range", tonumber(fields.range) or 8)
|
|
meta:set_int("duration", tonumber(fields.duration) or 30)
|
|
minetest.chat_send_player(name, "Sensor-Parameter gespeichert.")
|
|
end
|
|
end
|
|
|
|
-- --- LICHT PROGRAMMIERER ---
|
|
if formname:sub(1, 17) == "smart_light:prog_" and formname:sub(1, 20) ~= "smart_light:prog_sw_" then
|
|
if fields.save then
|
|
local pos_str = formname:sub(18)
|
|
local pos = minetest.string_to_pos(pos_str)
|
|
local area, chan = fields.area_sel, fields.chan_in
|
|
if not area or area == "-- Bitte wählen --" or chan == "" then return end
|
|
minetest.get_meta(pos):set_string("sl_area", area)
|
|
minetest.get_meta(pos):set_string("sl_chan", chan)
|
|
local item = player:get_wielded_item()
|
|
local t_meta = item:get_meta()
|
|
t_meta:set_string("last_area", area)
|
|
t_meta:set_string("last_channel", chan)
|
|
t_meta:set_string("description", "Programmer Area: " .. area .. "\nChannel: " .. chan)
|
|
player:set_wielded_item(item)
|
|
smart_light.registry.areas[area] = smart_light.registry.areas[area] or { channels = {} }
|
|
smart_light.registry.areas[area].channels[chan] = smart_light.registry.areas[area].channels[chan] or { nodes = {}, last_level = "max", brightness = "max" }
|
|
smart_light.registry.areas[area].channels[chan].nodes[pos_str] = true
|
|
smart_light.save()
|
|
minetest.chat_send_player(name, "Licht registriert!")
|
|
end
|
|
end
|
|
|
|
-- --- HAUPTMENÜ (PANEL) ---
|
|
if formname:sub(1, 17) == "smart_light:main_" then
|
|
local pos_str = formname:sub(18)
|
|
local pos = minetest.string_to_pos(pos_str)
|
|
local area = minetest.get_meta(pos):get_string("area_id")
|
|
if fields.all_off then
|
|
for c,_ in pairs(smart_light.registry.areas[area].channels) do smart_light.switch_channel(area, c, "off") end
|
|
minetest.registered_nodes["smart_light:controller"].on_rightclick(pos, nil, player)
|
|
elseif fields.all_on then
|
|
for c,_ in pairs(smart_light.registry.areas[area].channels) do smart_light.switch_channel(area, c, "on") end
|
|
minetest.registered_nodes["smart_light:controller"].on_rightclick(pos, nil, player)
|
|
else
|
|
for k, _ in pairs(fields) do
|
|
local cid = k:match("^chan_(.+)$")
|
|
if cid then
|
|
local count = smart_light.get_count(area, cid)
|
|
local status = smart_light.get_status_text(area, cid)
|
|
local last_lvl = smart_light.registry.areas[area].channels[cid].last_level or "max"
|
|
local subfs = "size[4,5.5]label[0.5,0.2;Channel: " .. cid .. "]" ..
|
|
"label[0.5,0.7;Anzahl Leuchten: " .. count .. "]" ..
|
|
"label[0.5,1.2;Status: " .. status .. "]" ..
|
|
"label[0.5,1.7;Dimmlevel: " .. last_lvl .. "]" ..
|
|
"button[0.5,2.5;1.5,0.8;sub_"..cid.."_on;An]" ..
|
|
"button[2.2,2.5;1.0,0.8;sub_"..cid.."_up;D+]" ..
|
|
"button[0.5,3.5;1.5,0.8;sub_"..cid.."_off;Aus]" ..
|
|
"button[2.2,3.5;1.0,0.8;sub_"..cid.."_down;D-]" ..
|
|
"button[0.5,4.7;3,0.6;back;Zurück]"
|
|
minetest.show_formspec(name, "smart_light:sub_" .. pos_str, subfs)
|
|
return
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- --- SUB-MENÜ ---
|
|
if formname:sub(1, 16) == "smart_light:sub_" then
|
|
local pos_str = formname:sub(17)
|
|
local pos = minetest.string_to_pos(pos_str)
|
|
local meta = minetest.get_meta(pos)
|
|
local area = meta:get_string("area_id")
|
|
if area == "" then area = meta:get_string("sl_area") end
|
|
for k, _ in pairs(fields) do
|
|
local cid, action = k:match("^sub_(.+)_([%a%d]+)$")
|
|
if cid and action then
|
|
smart_light.switch_channel(area, cid, action)
|
|
local count = smart_light.get_count(area, cid)
|
|
local status = smart_light.get_status_text(area, cid)
|
|
local last_lvl = smart_light.registry.areas[area].channels[cid].last_level or "max"
|
|
local subfs = "size[4,5.5]label[0.5,0.2;Channel: " .. cid .. "]" ..
|
|
"label[0.5,0.7;Anzahl Leuchten: " .. count .. "]" ..
|
|
"label[0.5,1.2;Status: " .. status .. "]" ..
|
|
"label[0.5,1.7;Dimmlevel: " .. last_lvl .. "]" ..
|
|
"button[0.5,2.5;1.5,0.8;sub_"..cid.."_on;An]" ..
|
|
"button[2.2,2.5;1.0,0.8;sub_"..cid.."_up;D+]" ..
|
|
"button[0.5,3.5;1.5,0.8;sub_"..cid.."_off;Aus]" ..
|
|
"button[2.2,3.5;1.0,0.8;sub_"..cid.."_down;D-]" ..
|
|
"button[0.5,4.7;3,0.6;back;Zurück]"
|
|
minetest.show_formspec(name, formname, subfs)
|
|
return
|
|
end
|
|
end
|
|
if fields.back or fields.exit then
|
|
if minetest.get_node(pos).name == "smart_light:controller" then
|
|
minetest.registered_nodes["smart_light:controller"].on_rightclick(pos, nil, player)
|
|
else minetest.close_formspec(name, formname) end
|
|
end
|
|
end
|
|
|
|
-- --- PANEL SETUP ---
|
|
if formname:sub(1, 18) == "smart_light:setup_" then
|
|
if fields.set_area and fields.area_id ~= "" then
|
|
local pos = minetest.string_to_pos(formname:sub(19))
|
|
minetest.get_meta(pos):set_string("area_id", fields.area_id)
|
|
smart_light.registry.areas[fields.area_id] = smart_light.registry.areas[fields.area_id] or { channels = {} }
|
|
smart_light.save()
|
|
end
|
|
end
|
|
end)
|