minor update
This commit is contained in:
parent
e69eb7576e
commit
3e07b13a4c
@ -13,7 +13,7 @@
|
||||
size='32' margin='0,0,10,15' gravity='bottom-right'
|
||||
color='#FFFFFF50' hover-color='#FFFFFF10'/>
|
||||
|
||||
<image id="open_config" onclick='open_config()' interactive='true' src='gui/settings_icon'
|
||||
<image id="open_config" enabled="false" onclick='open_config()' interactive='true' src='gui/settings_icon'
|
||||
size='32' margin='0,0,52,15' gravity='bottom-right'
|
||||
color='#FFFFFF50' hover-color='#FFFFFF10'/>
|
||||
|
||||
|
||||
@ -1,5 +1,9 @@
|
||||
local packs_installed = {}
|
||||
local pack_open = {}
|
||||
local parsers = {
|
||||
toml = toml,
|
||||
json = json
|
||||
}
|
||||
|
||||
function on_open(params)
|
||||
refresh()
|
||||
@ -45,14 +49,14 @@ end
|
||||
local function create_checkbox(id, name, cheaked)
|
||||
document.configs:add(string.format(
|
||||
"<checkbox id='%s' consumer='function(x) set_value(\"%s\", \"%s\", x) end' checked='%s'>%s</checkbox>",
|
||||
id, name, id, cheaked, name
|
||||
id, name, id, cheaked, name:gsub("^%l", string.upper)
|
||||
))
|
||||
end
|
||||
|
||||
local function create_textbox(id, name, text)
|
||||
document.configs:add(string.format(
|
||||
"<label id='%s'>%s</label>",
|
||||
id .. "_label", name
|
||||
id .. "_label", name:gsub("^%l", string.upper)
|
||||
))
|
||||
document.configs:add(string.format(
|
||||
"<textbox id='%s' consumer='function(x) set_value(\"%s\", \"%s\", x) end' hint='%s'>%s</textbox>",
|
||||
@ -63,7 +67,7 @@ end
|
||||
local function create_trackbar(id, name, val)
|
||||
document.configs:add(string.format(
|
||||
"<label id='%s'>%s (%s)</label>",
|
||||
id .. "_label", name, val
|
||||
id .. "_label", name:gsub("^%l", string.upper), val
|
||||
))
|
||||
document.configs:add(string.format(
|
||||
"<trackbar id='%s' consumer='function(x) set_value(\"%s\", \"%s\", x) end' value='%s' min='0' max='1000' step='10'>%s</trackbar>",
|
||||
@ -79,30 +83,30 @@ local function create_label(id, text, color)
|
||||
))
|
||||
else
|
||||
document.configs:add(string.format(
|
||||
"<label>%s</label>",
|
||||
text
|
||||
"<label color='%s'>%s</label>",
|
||||
color, text
|
||||
))
|
||||
end
|
||||
end
|
||||
|
||||
local function create_config(i, config, name, path)
|
||||
create_label(name, '[' .. name .. ']', "#008000")
|
||||
pack_open[2][i] = {{}, path = path}
|
||||
create_label(name, ('[' .. name .. ']'):upper(), "#FFFFFF")
|
||||
pack_open[2][i] = {elements = {}, path = path}
|
||||
for _, a in ipairs(config.checkboxes) do
|
||||
create_checkbox(i .. '_' .. a[1], a[1], a[2])
|
||||
pack_open[2][i][1][a[1]] = type(a[2])
|
||||
pack_open[2][i]["elements"][a[1]] = type(a[2])
|
||||
end
|
||||
create_label(nil, '', 0)
|
||||
for _, a in ipairs(config.trackbars) do
|
||||
create_trackbar(i .. '_' .. a[1], a[1], a[2])
|
||||
create_label(nil, '', 0)
|
||||
pack_open[2][i][1][a[1]] = type(a[2])
|
||||
pack_open[2][i]["elements"][a[1]] = type(a[2])
|
||||
end
|
||||
|
||||
for _, a in ipairs(config.textboxes) do
|
||||
create_textbox(i .. '_' .. a[1], a[1], a[2])
|
||||
create_label(nil, '', 0)
|
||||
pack_open[2][i][1][a[1]] = type(a[2])
|
||||
pack_open[2][i]["elements"][a[1]] = type(a[2])
|
||||
end
|
||||
create_label(nil, '', 0)
|
||||
end
|
||||
@ -115,12 +119,9 @@ local function load_config_file(path)
|
||||
return
|
||||
end
|
||||
|
||||
if parsers[extension] then
|
||||
local value = file.read(path)
|
||||
|
||||
if extension == "toml" then
|
||||
return toml.parse(value), extension
|
||||
elseif extension == "json" then
|
||||
return json.parse(value), extension
|
||||
return parsers[extension].parse(value), extension
|
||||
end
|
||||
end
|
||||
|
||||
@ -150,23 +151,33 @@ local function load_config(path)
|
||||
end
|
||||
end
|
||||
|
||||
local function valid_configs(path)
|
||||
if file.exists(path) and file.isdir(path) then
|
||||
for _, c in ipairs(file.list(path)) do
|
||||
local extension = c:match("%.(%w+)$")
|
||||
if parsers[extension] then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function set_value(name, id, value)
|
||||
local config_id = tonumber(id:match("^(%w+)_"))
|
||||
local elem_id = id:match("^[^_]+_(.+)$")
|
||||
local type_val = pack_open[2][config_id][1][elem_id]
|
||||
local type_val = pack_open[2][config_id]["elements"][elem_id]
|
||||
|
||||
if type_val == 'number' then
|
||||
document[id .. '_label'].text = string.format("%s (%s)", elem_id, value)
|
||||
document[id .. '_label'].text = string.format("%s (%s)", elem_id:gsub("^%l", string.upper), value)
|
||||
end
|
||||
|
||||
local path = pack_open[2][config_id].path
|
||||
local config, extension = load_config_file(path)
|
||||
config[name] = value
|
||||
|
||||
if extension == "toml" then
|
||||
file.write(path, toml.tostring(config))
|
||||
elseif extension == "json" then
|
||||
file.write(path, json.tostring(config))
|
||||
if parsers[extension] then
|
||||
file.write(path, parsers[extension].tostring(config))
|
||||
end
|
||||
end
|
||||
|
||||
@ -176,6 +187,7 @@ function open_pack(id)
|
||||
|
||||
document.configs.visible = false
|
||||
document.content_info.visible = true
|
||||
if valid_configs("config:" .. id) then document.open_config.enabled = true else document.open_config.enabled = false end
|
||||
|
||||
if packinfo['dependencies'] == nil then document.dependencies.text = 'None' else document.dependencies.text = table.tostring(packinfo['dependencies']) end
|
||||
if packinfo['creator'] == '' then document.author.text = 'None' else document.author.text = packinfo['creator'] end
|
||||
@ -185,13 +197,15 @@ end
|
||||
|
||||
function open_config()
|
||||
local id = pack_open[1]
|
||||
if (not document.configs.visible and id) and
|
||||
(file.exists("config:" .. id) and #file.list("config:" .. id) > 0) then
|
||||
local path = "config:" .. id
|
||||
|
||||
if (not document.configs.visible and id) and valid_configs(path) then
|
||||
document.configs:clear()
|
||||
document.configs.visible = true
|
||||
document.content_info.visible = false
|
||||
|
||||
local configs = file.list("config:" .. id)
|
||||
|
||||
for i, c in ipairs(configs) do
|
||||
local name = c:match("([^/]+)$")
|
||||
name = name:match("([^%.]+)")
|
||||
@ -201,11 +215,11 @@ function open_config()
|
||||
create_config(i, config, name, c)
|
||||
end
|
||||
end
|
||||
else
|
||||
return
|
||||
end
|
||||
document.configs.visible = false
|
||||
document.content_info.visible = true
|
||||
end
|
||||
end
|
||||
|
||||
function refresh()
|
||||
local packs_available = pack.get_available()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user