Advertisement
DabDaddy6223

os_run

Mar 23rd, 2023 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.13 KB | None | 0 0
  1. local gfx = require("gfx")
  2. local gui = require("gui")
  3. local utils = require("utils")
  4.  
  5. Exit = false
  6.  
  7. -- The index of the current selection in the list of childen (current screen)
  8. SelectionIndex = 0
  9.  
  10. -- Screen management
  11. Screens = {}
  12. CurrentScreen = 1
  13. LastScreen = 0
  14.  
  15. function event()
  16.     local event, key = os.pullEvent("key")
  17.     if key == keys.e then
  18.         Exit = true
  19.     elseif key == keys.tab then
  20.         local current = Screens[CurrentScreen]
  21.  
  22.         SelectionIndex = SelectionIndex + 1
  23.         if SelectionIndex > #(current["children"]) then
  24.             SelectionIndex = 1
  25.         end
  26.  
  27.         if current["children"][SelectionIndex]["type"] == "button" then
  28.             gfx.clear()
  29.             gfx.flush()
  30.  
  31.             gui.setAllButtonsOff(Screens[CurrentScreen])
  32.             gui.toggleButton(Screens[CurrentScreen], SelectionIndex)
  33.             gui.show(Screens[CurrentScreen])
  34.         end
  35.     elseif key == keys.enter then
  36.         local current = Screens[CurrentScreen]
  37.         local element = current["children"][SelectionIndex]
  38.         if element["type"] == "button" then
  39.             local callback = element["callback"]
  40.             if callback ~= nil then
  41.                 callback()
  42.             end
  43.         end
  44.     end
  45. end
  46.  
  47. function tick()
  48.     if CurrentScreen ~= LastScreen then
  49.         -- Close Last Screen
  50.         gfx.flush()
  51.         gfx.clear()
  52.  
  53.         -- Open New Screen
  54.         gui.show(Screens[CurrentScreen])
  55.     end
  56.  
  57.     gfx.display()
  58.  
  59.     LastScreen = CurrentScreen
  60. end
  61.  
  62. function swap()
  63.     if CurrentScreen == 1 then
  64.         CurrentScreen = 2
  65.     else
  66.         CurrentScreen = 1
  67.     end
  68. end
  69.  
  70. function main()
  71.     local mainScreen = gui.createParent()
  72.     gui.addCentralXButton(mainScreen, "First Button", 6, swap)
  73.     Screens[#Screens + 1] = mainScreen
  74.  
  75.     local secondScreen = gui.createParent()
  76.     gui.addCentralXButton(secondScreen, "Second Button", 6, swap)
  77.     Screens[#Screens + 1] = secondScreen
  78.  
  79.     gfx.clear()
  80.     gfx.display()
  81.     while true do
  82.         if Exit then
  83.             break
  84.         end
  85.  
  86.         parallel.waitForAll(tick, event)
  87.     end
  88. end
  89.  
  90. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement