soundblock/form.lua

130 lines
3.4 KiB
Lua
Raw Permalink Normal View History

2019-04-29 02:07:48 -07:00
local FORMNAME = "soundblock_config"
--[[
config options:
2019-04-29 03:55:15 -07:00
* state: [off, on]
2019-04-29 02:07:48 -07:00
2019-04-29 03:55:15 -07:00
* interval-min <float>
* interval-max <float>
2019-04-29 02:07:48 -07:00
* gain <float>
* hear-distance <int>
* randomize-position <int> 0...64
--]]
2019-10-07 00:26:26 -07:00
soundblock.showform = function(pos, _, player)
2019-04-29 02:07:48 -07:00
local meta = minetest.get_meta(pos)
local has_override = minetest.check_player_privs(player, "protection_bypass")
-- check if plain user rightclicks
2019-04-29 09:10:05 -07:00
if minetest.is_protected(pos, player:get_player_name()) and not has_override then
2019-04-29 02:07:48 -07:00
return
end
local selected_sound_key = meta:get_string("selected_sound_key")
2019-04-29 09:10:05 -07:00
local selected_id = 1
2019-04-29 02:07:48 -07:00
-- sound list
2019-04-29 04:05:08 -07:00
local sound_list = "textlist[0,1;4,6;sounds;"
2019-11-09 12:02:33 -08:00
2019-11-22 00:52:05 -08:00
for i, sounddef in ipairs(soundblock.sounds) do
if selected_sound_key == sounddef.key then
2019-04-29 09:10:05 -07:00
selected_id = i
2019-04-29 02:07:48 -07:00
end
2019-11-22 00:52:05 -08:00
sound_list = sound_list .. minetest.formspec_escape(sounddef.name) .. ","
2019-04-29 02:07:48 -07:00
end
2019-04-29 09:10:05 -07:00
sound_list = sound_list:sub(1, #sound_list-1)
sound_list = sound_list .. ";" .. selected_id .. "]";
2019-04-29 02:07:48 -07:00
2019-04-29 03:55:15 -07:00
local state = meta:get_string("state")
local interval_min = meta:get_int("interval_min")
local interval_max = meta:get_int("interval_max")
local gain = meta:get_int("gain")
local hear_distance = meta:get_int("hear_distance")
local randomize_position = meta:get_int("randomize_position")
2019-04-29 02:07:48 -07:00
local formspec = "size[8,8;]" ..
--left
2019-04-29 03:55:15 -07:00
"label[0,0;Soundblock <" .. state .. ">]" ..
2019-04-29 09:10:05 -07:00
"field[5,1;3,1;interval_min;Interval-min;" .. interval_min .. "]" ..
"field[5,2;3,1;interval_max;Interval-max;" .. interval_max .. "]" ..
2019-04-29 03:55:15 -07:00
2019-04-29 09:10:05 -07:00
"field[5,3;3,1;gain;Gain;" .. gain .. "]" ..
"field[5,4;3,1;hear_distance;Hear distance;" .. hear_distance .. "]" ..
"field[5,5;3,1;randomize_position;Randomize position;" .. randomize_position .. "]" ..
2019-04-29 03:55:15 -07:00
sound_list ..
2019-04-29 04:05:08 -07:00
"button_exit[0,7;4,1;save;Save]" ..
"button_exit[4,7;4,1;toggle_state;Toggle state]"
2019-04-29 02:07:48 -07:00
minetest.show_formspec(player:get_player_name(),
FORMNAME .. ";" .. minetest.pos_to_string(pos),
formspec
)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
local parts = formname:split(";")
local name = parts[1]
if name ~= FORMNAME then
return
end
local pos = minetest.string_to_pos(parts[2])
local meta = minetest.get_meta(pos)
local has_override = minetest.check_player_privs(player, "protection_bypass")
if not has_override and minetest.is_protected(pos, player:get_player_name()) then
return
end
2019-04-29 03:55:15 -07:00
local timer = minetest.get_node_timer(pos)
2019-11-22 00:52:05 -08:00
local state = meta:get_string("state")
2019-04-29 03:55:15 -07:00
if fields.toggle_state then
if state == "on" then
state = "off"
timer:stop()
else -- off
state = "on"
2019-04-29 02:07:48 -07:00
end
2019-04-29 03:55:15 -07:00
meta:set_string("state", state)
2019-04-29 02:07:48 -07:00
end
2019-04-29 03:55:15 -07:00
if fields.toggle_state or fields.save then
meta:set_int("interval_min", tonumber(fields.interval_min) or 1)
meta:set_int("interval_max", tonumber(fields.interval_max) or 1)
meta:set_int("gain", tonumber(fields.gain) or 1)
meta:set_int("hear_distance", math.min(tonumber(fields.hear_distance) or 16, 64))
2019-04-29 03:55:15 -07:00
meta:set_int("randomize_position", tonumber(fields.randomize_position) or 0)
end
2019-04-29 02:07:48 -07:00
2019-04-29 03:55:15 -07:00
if fields.sounds then
parts = fields.sounds:split(":")
if parts[1] == "CHG" then
2019-04-29 09:10:05 -07:00
local selected_id = tonumber(parts[2])
2019-11-22 00:52:05 -08:00
local sounddef = soundblock.sounds[selected_id]
2019-04-29 03:55:15 -07:00
2019-11-22 00:52:05 -08:00
meta:set_string("selected_sound_key", sounddef.key)
2019-10-06 23:48:13 -07:00
if sounddef and sounddef.length then
meta:set_int("interval_min", sounddef.length)
meta:set_int("interval_max", sounddef.length)
2019-11-09 12:02:33 -08:00
soundblock.showform(pos, nil, player)
2019-10-06 23:48:13 -07:00
end
2019-04-29 03:55:15 -07:00
end
end
2019-04-29 02:07:48 -07:00
2019-11-23 10:38:00 -08:00
timer:start(0)
2019-04-29 02:07:48 -07:00
end)