1
0
mirror of https://github.com/sys4-fr/server-nalc.git synced 2024-11-16 07:20:31 +01:00

add an menu in soundset

add an menu in soundset and and button in unified_inventory and chatcommand to display menu
This commit is contained in:
crabman77 2015-03-27 20:46:17 +01:00
parent 26bd691550
commit a32ed5e3cf
6 changed files with 136 additions and 27 deletions

View File

@ -0,0 +1 @@
unified_inventory?

View File

@ -3,8 +3,34 @@ minetest.log("action","[mod soundset] Loading...")
sounds = {}
sounds.file = minetest.get_worldpath() .. "/sounds_config.txt"
sounds.gainplayers = {}
sounds.tmp = {}
local function save_sounds_config()
local input = io.open(sounds.file, "w")
if input then
input:write(minetest.serialize(sounds.gainplayers))
input:close()
else
minetest.log("action","echec d'ouverture (mode:w) de " .. sounds.file)
end
end
local function load_sounds_config()
local file = io.open(sounds.file, "r")
if file then
sounds.gainplayers = minetest.deserialize(file:read("*all"))
file:close()
end
print("type: "..tostring(type(sounds.gainplayers)))
if sounds.gainplayers == nil or type(sounds.gainplayers) ~= "table" then
sounds.gainplayers = {}
end
end
load_sounds_config()
sounds.set_sound = function(name, param)
if param == "" then
minetest.chat_send_player(name, "/setsound <music|ambience|mobs|other> <number>")
@ -13,18 +39,16 @@ sounds.set_sound = function(name, param)
local param_name, param_value = param:match("^(%S+)%s(%S+)$")
if param_name == nil or param_value == nil then
minetest.chat_send_player(name, "invalid param, /setsound <music|ambience|mobs|other> <number>")
minetest.log("action", "invalid param, see /setsound <music|ambience|mobs|other> <number>")
return
end
if param_name ~= "music" and param_name ~= "ambience" and param_name ~= "mobs" and param_name ~= "other" then
minetest.chat_send_player(name, "invalid param " .. param_name)
minetest.log("action", "invalid param, /setsound " .. param_name)
return
end
local value = tonumber(param_value)
if value == nil then
minetest.log("action", "invalid value, " ..param_value .. " must be number")
minetest.chat_send_player(name, "invalid value, " ..param_value .. " must be number")
return
end
@ -36,22 +60,12 @@ sounds.set_sound = function(name, param)
if sounds.gainplayers[name][param_name] == value then
minetest.chat_send_player(name, "volume " .. param_name .. " already set to " .. value)
minetest.log("action", name ..", volume " .. param_name .. " already set to " .. value)
return
end
sounds.gainplayers[name][param_name] = value
minetest.chat_send_player(name, "sound " .. param_name .. " set to " .. value)
minetest.log("action", name ..", sound " .. param_name .. " set to " .. value)
local input = io.open(sounds.file, "w")
if input then
input:write(minetest.serialize(sounds.gainplayers))
input:close()
else
minetest.log("action","echec d'ouverture (mode:w) de " .. sounds.file)
end
save_sounds_config()
end
@ -67,31 +81,119 @@ sounds.get_gain = function(name, sound_type)
end
local formspec = "size[6,6]"..
"label[2,0;Sound Menu]"..
"label[0.3,1.2;MUSIC]"..
"image_button[1.6,1;1,1;soundset_dec.png;vmusic;-]"..
"label[2.7,1.2;%s]"..
"image_button[3.5,1;1,1;soundset_inc.png;vmusic;+]"..
"label[0,2.2;AMBIENCE]"..
"image_button[1.6,2;1,1;soundset_dec.png;vambience;-]"..
"label[2.7,2.2;%s]"..
"image_button[3.5,2;1,1;soundset_inc.png;vambience;+]"..
"button_exit[0.5,5.2;1.5,1;abort;Abort]"..
"button_exit[4,5.2;1.5,1;abort;Ok]"
local function load_sounds_config()
local file = io.open(sounds.file, "r")
if file then
sounds.gainplayers = minetest.deserialize(file:read("*all"))
file:close()
end
if sounds.gainplayers == nil then
sounds.gainplayers = {}
local on_show_settings = function(name, music, ambience)
if not sounds.tmp[name] then
sounds.tmp[name] = {}
end
sounds.tmp[name]["music"] = music
sounds.tmp[name]["ambience"] = ambience
minetest.show_formspec( name, "soundset:settings", string.format(formspec, tostring(music), tostring(ambience) ))
end
load_sounds_config()
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname == "soundset:settings" then
local name = player:get_player_name()
local fmusic = sounds.tmp[name]["music"]
local fambience = sounds.tmp[name]["ambience"]
if fields["abort"] == "Ok" then
if sounds.gainplayers[name]["music"] ~= fmusic or sounds.gainplayers[name]["ambience"] ~= fambience then
sounds.gainplayers[name]["music"] = fmusic
sounds.gainplayers[name]["ambience"] = fambience
save_sounds_config()
end
sounds.tmp[name] = nil
return
elseif fields["abort"] == "Abort" then
sounds.tmp[name] = nil
return
elseif fields["vmusic"] == "+" then
if fmusic < 100 then
fmusic = fmusic +5
if fmusic > 100 then
fmusic = 100
end
end
elseif fields["vmusic"] == "-" then
if fmusic > 0 then
fmusic = fmusic -5
if fmusic < 0 then
fmusic = 0
end
end
elseif fields["vambience"] == "+" then
if fambience < 100 then
fambience = fambience +5
if fambience > 100 then
fambience = 100
end
end
elseif fields["vambience"] == "-" then
if fambience > 0 then
fambience = fambience -5
if fambience < 0 then
fambience = 0
end
end
elseif fields["quit"] == "true" then
sounds.tmp[name] = nil
return
else
return
end
minetest.register_chatcommand("setsound", {
on_show_settings(name, fmusic, fambience)
end
end)
if (minetest.get_modpath("unified_inventory")) then
unified_inventory.register_button("menu_soundset", {
type = "image",
image = "soundset_menu_icon.png",
tooltip = "sounds menu ",
action = function(player)
local name = player:get_player_name()
on_show_settings(name, sounds.gainplayers[name]["music"], sounds.gainplayers[name]["ambience"])
end,
})
end
minetest.register_chatcommand("soundset", {
params = "",
description = "Display volume menu formspec",
privs = {interact=true},
func = function(name, param)
on_show_settings(name, sounds.gainplayers[name]["music"], sounds.gainplayers[name]["ambience"])
end
})
minetest.register_chatcommand("soundsets", {
params = "<music|ambience|mobs|other> <number>",
description = "set volume sound <music|ambience|mobs|other>",
description = "Set volume sound <music|ambience|mobs|other>",
privs = {interact=true},
func = sounds.set_sound,
})
minetest.register_chatcommand("getsound", {
minetest.register_chatcommand("soundsetg", {
params = "",
description = "print volume sound <music|ambience|mobs|other>",
description = "Display volume sound <music|ambience|mobs|other>",
privs = {interact=true},
func = function(name, param)
local conf = ""
@ -109,5 +211,6 @@ minetest.register_on_joinplayer(function(player)
sounds.gainplayers[name] = { ["music"] = 50, ["ambience"] = 50, ["mobs"] = 50, ["other"] = 50 }
end
end)
minetest.log("action","[mod soundset] Loaded")

View File

@ -4,3 +4,8 @@ by LeMagnesium and crabman77
YOUR mod can use THIS mod to have a volume that's adjustable by the player(s)
0.1 - Initial Release
0.2 - add menu setting and button for unified_inventory and chatcommand to display menu
Licenses images: Author:mikhog http://opengameart.org/content/play-pause-mute-and-unmute-buttons CC0 1.0 Universal (CC0 1.0) Public Domain Dedication

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB