diff --git a/res/layouts/pages/content_menu.xml b/res/layouts/pages/content_menu.xml index af15b32c..9197ee21 100644 --- a/res/layouts/pages/content_menu.xml +++ b/res/layouts/pages/content_menu.xml @@ -1,15 +1,23 @@ + - - + + + + + @@ -25,4 +33,8 @@ + + + + diff --git a/res/layouts/pages/content_menu.xml.lua b/res/layouts/pages/content_menu.xml.lua index 130b7387..75a4e5de 100644 --- a/res/layouts/pages/content_menu.xml.lua +++ b/res/layouts/pages/content_menu.xml.lua @@ -1,4 +1,5 @@ local packs_installed = {} +local pack_open = {} function on_open(params) refresh() @@ -41,9 +42,140 @@ function refresh_search() end end +local function create_checkbox(id, name, cheaked) + document.configs:add(string.format( + "%s", + id, name, id, cheaked, name + )) +end + +local function create_textbox(id, name, text) + document.configs:add(string.format( + "", + id .. "_label", name + )) + document.configs:add(string.format( + "%s", + id, name, id, name, text + )) +end + +local function create_trackbar(id, name, val) + document.configs:add(string.format( + "", + id .. "_label", name, val + )) + document.configs:add(string.format( + "%s", + id, name, id, val, name + )) +end + +local function create_label(id, text, color) + if id then + document.configs:add(string.format( + "", + id, color, text + )) + else + document.configs:add(string.format( + "", + text + )) + end +end + +local function create_config(i, config, name, path) + create_label(name, '[' .. name .. ']', "#008000") + pack_open[2][i] = {{}, 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]) + 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]) + 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]) + end + create_label(nil, '', 0) +end + + +local function load_config_file(path) + local extension = path:match("%.(%w+)$") + + if not extension or file.isdir(path) then + return + end + + local value = file.read(path) + + if extension == "toml" then + return toml.parse(value), extension + elseif extension == "json" then + return json.parse(value), extension + end +end + +local function load_config(path) + if file.exists(path) then + local value = load_config_file(path) + local config = {checkboxes = {}, + trackbars = {}, + textboxes = {} + } + + if not value then + return + end + + for i, v in pairs(value) do + if type(v) == "string" then + table.insert(config.textboxes, {i, v}) + elseif type(v) == "boolean" then + table.insert(config.checkboxes, {i, v}) + elseif type(v) == "number" then + table.insert(config.trackbars, {i, v}) + end + end + + return config + end +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] + + if type_val == 'number' then + document[id .. '_label'].text = string.format("%s (%s)", elem_id, 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)) + end +end function open_pack(id) local packinfo = pack.get_info(id) + pack_open = {id, {}} + + document.configs.visible = false + document.content_info.visible = true 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 @@ -51,11 +183,35 @@ function open_pack(id) if packinfo['description'] == nil then document.description.text = 'None' else document.description.text = packinfo['description'] end 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 + + 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("([^%.]+)") + local config = load_config(c) + + if config then + create_config(i, config, name, c) + end + end + else + document.configs.visible = false + document.content_info.visible = true + end +end + function refresh() local packs_available = pack.get_available() packs_installed = pack.get_installed() - for i,k in ipairs(packs_available) do + for _, k in ipairs(packs_available) do table.insert(packs_installed, k) end diff --git a/res/preload.json b/res/preload.json index 8971103f..5efe0162 100644 --- a/res/preload.json +++ b/res/preload.json @@ -17,7 +17,9 @@ "gui/warning", "gui/error", "gui/cross", - "gui/refresh" + "gui/refresh", + "gui/folder_icon", + "gui/settings_icon" ], "fonts": [ { diff --git a/res/textures/gui/folder_icon.png b/res/textures/gui/folder_icon.png new file mode 100644 index 00000000..6dd9a7a2 Binary files /dev/null and b/res/textures/gui/folder_icon.png differ diff --git a/res/textures/gui/settings_icon.png b/res/textures/gui/settings_icon.png new file mode 100644 index 00000000..6a7aea33 Binary files /dev/null and b/res/textures/gui/settings_icon.png differ