Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local gfx = require("gfx")
- local gui = require("gui")
- local utils = require("utils")
- Exit = false
- -- The index of the current selection in the list of childen (current screen)
- SelectionIndex = 0
- -- Screen management
- Screens = {}
- CurrentScreen = 1
- LastScreen = 0
- function event()
- local event, key = os.pullEvent("key")
- if key == keys.e then
- Exit = true
- elseif key == keys.tab then
- local current = Screens[CurrentScreen]
- SelectionIndex = SelectionIndex + 1
- if SelectionIndex > #(current["children"]) then
- SelectionIndex = 1
- end
- if current["children"][SelectionIndex]["type"] == "button" then
- gfx.clear()
- gfx.flush()
- gui.setAllButtonsOff(Screens[CurrentScreen])
- gui.toggleButton(Screens[CurrentScreen], SelectionIndex)
- gui.show(Screens[CurrentScreen])
- end
- elseif key == keys.enter then
- local current = Screens[CurrentScreen]
- local element = current["children"][SelectionIndex]
- if element["type"] == "button" then
- local callback = element["callback"]
- if callback ~= nil then
- callback()
- end
- end
- end
- end
- function tick()
- if CurrentScreen ~= LastScreen then
- -- Close Last Screen
- gfx.flush()
- gfx.clear()
- -- Open New Screen
- gui.show(Screens[CurrentScreen])
- end
- gfx.display()
- LastScreen = CurrentScreen
- end
- function swap()
- if CurrentScreen == 1 then
- CurrentScreen = 2
- else
- CurrentScreen = 1
- end
- end
- function main()
- local mainScreen = gui.createParent()
- gui.addCentralXButton(mainScreen, "First Button", 6, swap)
- Screens[#Screens + 1] = mainScreen
- local secondScreen = gui.createParent()
- gui.addCentralXButton(secondScreen, "Second Button", 6, swap)
- Screens[#Screens + 1] = secondScreen
- gfx.clear()
- gfx.display()
- while true do
- if Exit then
- break
- end
- parallel.waitForAll(tick, event)
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement