Advertisement
Guest User

run

a guest
Feb 1st, 2015
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.07 KB | None | 0 0
  1. function c() return term.clear() end
  2. function sc(...) return term.setBackgroundColor(...) end
  3. function stc(...) return term.setTextColor(...) end
  4. function sp(...) return term.setCursorPos(...) end
  5.  
  6. -- Vars
  7.  
  8. local w,h = term.getSize()
  9. local state = "idle"
  10. local barcolor = colors.blue
  11. local options = {"Start", "Reboot", "Shutdown", "Logout", "---", "Run", "Paint", "Edit Code"}
  12. local running = true
  13. local extra_menu_width = 2
  14. local background = "Images/background"
  15.  
  16. -- Password lock
  17. if not fs.exists("/".. shell.dir().. "/Pass/runp") then
  18.   if not http then
  19.     error("HTTP Must be enabled.")
  20.   end
  21.   shell.run("pastebin get ApZBXSmg /".. shell.dir() .."/Pass/runp")
  22. end
  23. if not fs.exists("passes") then
  24.   file = fs.open("passes", "w")
  25.   file.close()
  26. end
  27.  
  28. shell.run("/".. shell.dir().. "/Pass/runp")
  29.  
  30. -- Graphical Functions
  31.  
  32. function drawBar(color)
  33.  
  34.   sp(1,h + 1)
  35.   sc(color)
  36.   print(string.rep(" ", w))
  37.   sp(1,h)
  38.   stc(colors.lightBlue)
  39.   print(" Start ")
  40.  
  41. end
  42.  
  43. function findLongest(array)
  44.  
  45.   longest = 0
  46.  
  47.   for i = 1, #options do
  48.     if #options[i] > longest then
  49.       longest = #options[i]
  50.     end
  51.   end
  52.  
  53.   return longest
  54.  
  55. end
  56.  
  57. function drawStage()
  58.  
  59.   if state == "menu" or state == "games" then
  60.    
  61.     longest = findLongest(options) + extra_menu_width
  62.     sc(colors.lightBlue)
  63.     stc(colors.blue)
  64.     sp(1, h - #options)
  65.     for i = 1, #options do
  66.       if i == 1 then
  67.         sc(colors.blue)
  68.         stc(colors.lightBlue)
  69.       end
  70.       print(string.rep(" ", longest))
  71.       sc(colors.lightBlue)
  72.       stc(colors.blue)
  73.     end
  74.     sp(1, h - #options)
  75.     for i = 1, #options do
  76.       if i == 1 then
  77.         sc(colors.blue)
  78.         stc(colors.lightBlue)
  79.       end
  80.      
  81.       if i ~= 1 then
  82.         print(" ".. options[i])
  83.       else
  84.         print(string.rep(" ", (longest / 2) - (#options[i] / 2)).. options[i])
  85.       end
  86.       sc(colors.lightBlue)
  87.       stc(colors.blue)
  88.     end
  89.    
  90.   end
  91.  
  92. end
  93.  
  94. function calculateOption(y) -- Here trying to calculate it
  95.  
  96.   start = h - #options
  97.   opt = y - start
  98.   return opt
  99.  
  100. end
  101.  
  102. function popup(text)
  103.  
  104.   -- FIRST LINE
  105.   sp((w / 2) - (#text / 2) - 1, h / 2 - 1)
  106.   sc(colors.blue)
  107.   print(string.rep(" ", #text + 2))
  108.   -- MIDDLE LINE
  109.   sc(colors.blue)
  110.   sp((w / 2) - (#text / 2) - 1, h / 2)
  111.   write(" ")
  112.   sc(colors.lightBlue)
  113.   write(text)
  114.   sc(colors.blue)
  115.   print(" ")
  116.   -- LAST LINE
  117.   sc(colors.blue)
  118.   sp((w / 2) - (#text / 2) - 1, h / 2 + 1)
  119.   print(string.rep(" ", #text + 2))
  120.      
  121.   -- Wait for click
  122.   event = os.pullEvent("mouse_click")
  123.    
  124. end
  125.  
  126. function inputPopup(text)
  127.  
  128.   sp((w / 2) - (#text / 2) - 1, h / 2 - 1)
  129.   sc(colors.blue)
  130.   print(string.rep(" ", #text + 2))
  131.  
  132.   sc(colors.blue)
  133.   sp((w / 2) - (#text / 2) - 1, h / 2)
  134.   write(" ")
  135.   sc(colors.lightBlue)
  136.   write(text)
  137.   sc(colors.blue)
  138.   print(" ")
  139.  
  140.   sc(colors.blue)
  141.   sp((w / 2) - (#text / 2) - 1, h / 2 + 1)
  142.   write(" ")
  143.   sc(colors.lightBlue)
  144.   write(string.rep(" ", #text))
  145.   sc(colors.blue)
  146.   print(" ")
  147.  
  148.   sp((w / 2) - (#text / 2) - 1, h / 2 + 2)
  149.   sc(colors.blue)
  150.   print(string.rep(" ", #text + 2))
  151.  
  152.   sp((w / 2) - (#text / 2) + 1, h / 2 + 1)
  153.   sc(colors.lightBlue)
  154.   stc(colors.blue)
  155.   input = read()
  156.   return input
  157. end
  158. function callMenuFunction(option)
  159.  
  160.   if option == 1 then -- Reboot
  161.     os.reboot()
  162.   end
  163.   if option == 2 then -- Shutdown
  164.     os.shutdown()
  165.   end
  166.   if option == 3 then -- Logout
  167.     if not fs.exists("/".. shell.dir() .."/passes") then
  168.       popup("Something went wrong, rebooting.")
  169.       -- This would occur if the user deletes the
  170.       -- Password program while in the OS
  171.       os.reboot()
  172.     end
  173.     file = fs.open("/".. shell.dir() .."/passes", "r")
  174.     redline = file.readLine()
  175.     file.close()
  176.     if redline == "" or redline == nil then
  177.       popup("You have no password, Also no logout.")
  178.       return
  179.     end
  180.     shell.run("/".. shell.dir().. "/Pass/runp")
  181.   end
  182.   if option == 5 then -- Open
  183.     sc(colors.white)
  184.     stc(colors.black)
  185.     c()
  186.     comm = inputPopup("Command:                 ")
  187.     sp(1,1)
  188.     sc(colors.black)
  189.     stc(colors.white)
  190.     c()
  191.     shell.run(comm)
  192.     stc(colors.red)
  193.     print("Press any key..")
  194.     event = os.pullEvent("key")
  195.   end
  196.   if option == 6 then
  197.    
  198.     sc(colors.white)
  199.     stc(colors.black)
  200.     c()
  201.     comm = inputPopup("File Name:           ")
  202.     shell.run("paint ".. comm)
  203.    
  204.   end
  205.   if option == 7 then
  206.    
  207.     shell.run("edit run")
  208.     shell.run("run")
  209.     error("Exited after editing")
  210.    
  211.   end
  212. end
  213.  
  214. -- Main Loop
  215.  
  216. h = h - 1
  217.  
  218. while running do
  219.  
  220.   stc(colors.orange)
  221.   sc(colors.white)
  222.   c()
  223.   drawBar(barcolor)
  224.   drawStage()
  225.  
  226.   local event, button, x, y = os.pullEvent("mouse_click")
  227.  
  228.   if state == "idle" then
  229.     if x < (#" Start ") and y == h then
  230.       state = "menu"
  231.     end
  232.   end
  233.   if state == "menu" then
  234.     if x < ((findLongest(options) + 1) + extra_menu_width) and y > ((h - 1) - #options) then
  235.       callMenuFunction(calculateOption(y))
  236.     else
  237.       state = "idle"
  238.     end
  239.   end
  240. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement