local menu_items = {
items = {},
visibler = {}
}
menu_items.run_update = function()
for name, m_table in pairs(menu_items.visibler) do
m_table.ref:set_visible(m_table.condition())
end
end
menu_items.new = function(name, item, conditions)
if menu_items.items[name] ~= nil then
error("item already have")
return
end
menu_items.items[name] = item
if type(conditions) == "function" then
menu_items.visibler[name] = {
ref = item,
condition = conditions
}
end
item:set_callback(menu_items.run_update)
return item
end
local configs = ui.create("Configs")
local example = ui.create("Example")
local import = configs:button("Import")
local export = configs:button("Export")
local switches = {}
local switch = menu_items.new("Master Switch", example:switch("Master Switch"))
for i = 1, 10 do
switches[i] = menu_items.new("item " .. i, example:switch("item " .. i), function()
return switch:get()
end)
end
local clipboard = require("neverlose/clipboard")
export:set_callback(function()
local status, message = pcall(function()
local configuration = {}
for key, value in pairs(menu_items.items) do
local ui_value = value:get()
if type(ui_value) == "userdata" then
configuration[key] = ui_value:to_hex()
else
configuration[key] = ui_value
end
end
clipboard.set(json.stringify(configuration))
end)
if not status then
return
end
end)
local config_load = function(configuration)
local status, message = pcall(function()
configuration = json.parse(configuration)
if configuration ~= nil then
for key, value in pairs(configuration) do
local item = menu_items.items[key]
if item ~= nil then
local invalue = value
item:set(invalue)
end
end
end
end)
if not status then
print_dev("Failed to load config")
end
end
import:set_callback(function()
config_load(clipboard.get())
end)
menu_items.run_update()