local appName = "SwitchFunctions" local appVersion = "1.0" local appAuthor = "Your Name" local switchConfigs = {} local MAX_ITEMS = 20 local currentForm = 1 local selectedSwitch = 1 local system = system local json = json local form = form local io = io local function saveConfigs() local file = io.open("/Apps/" .. appName .. "/config.json", "w") if file then io.write(file, json.encode(switchConfigs)) io.close(file) end end local function loadConfigs() local file = io.open("/Apps/" .. appName .. "/config.json", "r") if file then switchConfigs = json.decode(io.read(file, "*a")) or {} io.close(file) end end local function addSwitchConfig() if #switchConfigs < MAX_ITEMS then table.insert(switchConfigs, {switchID = "", position = "", text = ""}) saveConfigs() form.reinit() end end local function deleteSwitchConfig() if #switchConfigs > 0 then table.remove(switchConfigs, selectedSwitch) if selectedSwitch > #switchConfigs then selectedSwitch = #switchConfigs end saveConfigs() form.reinit() end end local function drawMainWindow() form.setTitle(appName) for i, config in ipairs(switchConfigs) do form.addLabel({label = config.switchID .. " " .. config.position .. " " .. config.text}) end end local function drawConfigWindow() local config = switchConfigs[selectedSwitch] form.addRow(2) form.addLabel({label = "Switch ID", width = 220}) form.addTextbox(config.switchID, 5, function(value) config.switchID = value end) form.addRow(2) form.addLabel({label = "Position", width = 220}) form.addTextbox(config.position, 3, function(value) config.position = value end) form.addRow(2) form.addLabel({label = "Text", width = 220}) form.addTextbox(config.text, 32, function(value) config.text = value end) saveConfigs() end local function checkButtons() if currentForm == 1 then form.setButton(3, ":add", #switchConfigs < MAX_ITEMS and ENABLED or DISABLED) form.setButton(4, ":delete", #switchConfigs > 0 and ENABLED or DISABLED) form.setButton(1, ":tools", ENABLED) else form.setButton(1, ":play", ENABLED) end end local function initForm(formID) currentForm = formID if formID == 1 then drawMainWindow() else drawConfigWindow() end checkButtons() end local function keyPressed(key) if key == KEY_5 then currentForm = 2 elseif key == KEY_6 then deleteSwitchConfig() elseif key == KEY_3 then addSwitchConfig() elseif key == KEY_ESC then currentForm = 1 form.reinit() end end local function init() system.registerForm(1, MENU_ADVANCED, appName, initForm, keyPressed, nil, keyPressed) loadConfigs() end return {init = init, author = appAuthor, version = appVersion, name = appName}