Jyzarc

Email

Feb 13th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.56 KB | None | 0 0
  1. --Objects (members)
  2. local menu = {
  3.     currentMenu = 1;
  4.     items = {};
  5.     [1] = {
  6.         menuText = "Is this computer a client or server?";
  7.         pos = 1;
  8.         backColor = colors.black;
  9.         textColor = colors.red;
  10.         items = {};
  11.     };
  12.     [2] = {
  13.         menuText = "Which side is the modem attached to?";
  14.         pos = 1;
  15.         backColor = colors.black;
  16.         textColor = colors.red;
  17.         items = {};
  18.     };
  19.     [3] = {
  20.         menuText = "Main menu";
  21.         pos = 1;
  22.         backColor = colors.black;
  23.         textColor = colors.red;
  24.         items = {};
  25.     };
  26.     [4] = {
  27.         menuText = "Signed in as Jyzarc";
  28.         pos = 1;
  29.         backColor = colors.black;
  30.         textColor = colors.red;
  31.         items = {};
  32.     };
  33. }
  34.  
  35. local program = {
  36.     eventData = {};
  37.     name = shell.getRunningProgram();
  38. }
  39.  
  40. local account = {
  41.     loggedIn = false;
  42. }
  43.  
  44. local writer = {
  45.     running = true;
  46.     text = "";
  47.     param = {};
  48. }
  49.  
  50. function program.exit()
  51.     error()
  52. end
  53.  
  54. function program.uninstall()
  55.     --Clear screen
  56.     term.clear()
  57.     --Set cursor pos to top left
  58.     term.setCursorPos(1,1)
  59.     print("Are you sure you would like to uninstall?")
  60.     program.input = read()
  61.     --If player answer is yes, then uninstall program
  62.     if program.input == "yes"
  63.     or program.input == "YES"
  64.     or program.input == "y"
  65.     or program.input == "Y"
  66.     or program.input == "true"
  67.     or program.input == "TRUE" then
  68.         --Deletes current program file
  69.         fs.delete(program.name)
  70.         print("Program uninstalled")
  71.         --Waits for key press
  72.         os.pullEvent("key")
  73.         --Exits program
  74.         program.exit()
  75.     end
  76. end
  77.  
  78. --Reinstalls/updates the currently running program
  79. function program.reinstall()
  80.     if http then
  81.         --Deletes current program file
  82.         fs.delete(program.name)
  83.         --Runs built it pastebin program (too lazy to write my own function)
  84.         shell.run("pastebin get "..program.pastebinCode.." "..program.name)
  85.         print("Program reinstalled, press any key to continue")
  86.         --Waits for key press
  87.         os.pullEvent("key")
  88.         --Exits program gracefully
  89.         program.exit()
  90.     else
  91.         print("Http not enabled")
  92.         --Waits for key press
  93.         os.pullEvent("key")
  94.     end
  95. end
  96.  
  97. function program.edit()
  98.     --Opens file to be edited
  99.     shell.run("edit "..program.name)
  100.     --Exits program
  101.     program.exit()
  102. end
  103.  
  104. function account.login()
  105.     term.clear()
  106.     term.setCursorPos(1,1)
  107.     term.write("Username: ")
  108.     program.input = io.read()
  109.     term.write("Password: ")
  110.     program.input = io.read()
  111. end
  112.  
  113. function program.startup()
  114. end
  115.  
  116. function writer.updateScreen()
  117.     term.setCursorBlink(true)
  118.     term.clear()
  119.     term.setCursorPos(1,1)
  120.     write(writer.text)
  121. end
  122.  
  123. function writer.run()
  124.     while writer.running do
  125.         writer.updateScreen()
  126.         program.eventData[1],program.eventData[2],program.eventData[3],program.eventData[4] = os.pullEvent()
  127.         if program.eventData[1] == "char" then
  128.             writer.text = writer.text..program.eventData[2]
  129.         elseif program.eventData[1] == "key" then
  130.             if program.eventData[2] == 28 then
  131.                 writer.text = writer.text.."\n"
  132.             elseif program.eventData[2] == 14 then
  133.                 writer.text = string.sub(writer.text,1,string.len(writer.text)-1)
  134.             end
  135.         end
  136.     end
  137. end
  138.  
  139. menu[1].items = {
  140.     [1] = { text = "client", color = colors.white, handler = read };
  141.     [2] = { text = "server", color = colors.white, handler = read };
  142. }
  143.  
  144. menu[2].items = {
  145.     [1] = { text = "top", color = colors.white, handler = read };
  146.     [2] = { text = "bottom", color = colors.white, handler = read };
  147.     [3] = { text = "left", color = colors.white, handler = read };
  148.     [4] = { text = "right", color = colors.white, handler = read };
  149.     [5] = { text = "front", color = colors.white, handler = read };
  150.     [6] = { text = "back", color = colors.white, handler = read };
  151. }
  152.  
  153. menu[3].items = {
  154.     [1] = { text = "login", color = colors.white, handler = read };
  155.     [2] = { text = "create account", color = colors.white, handler = read };
  156.     [3] = { text = "exit", color = colors.white, handler = program.exit};
  157.     [4] = { text = "reinstall", color = colors.white, handler = program.reinstall };
  158.     [5] = { text = "uninstall", color = colors.white, handler = program.uninstall };
  159. }
  160.  
  161. menu[4].items = {
  162.     [1] = { text = "Inbox", color = colors.white, handler = read };
  163.     [2] = { text = "archive", color = colors.white, handler = read };
  164.     [3] = { text = "compose", color = colors.white, handler = read };
  165.     [4] = { text = "uninstall", color = colors.white, handler = read };
  166. }
  167.  
  168. function menu.print()
  169.     if term.isColor() then
  170.         term.setBackgroundColor(menu[menu.currentMenu].backColor)
  171.     else
  172.         term.setBackgroundColor(colors.black)
  173.     end
  174.     term.clear()
  175.     term.setCursorPos(1,1)
  176.     if term.isColor() then
  177.         term.setTextColor(menu[menu.currentMenu].textColor)
  178.     else
  179.         term.setTextColor(colors.white)
  180.     end
  181.     print(menu[menu.currentMenu].text)
  182.     menu.x,menu.y = term.getSize()
  183.     menu.x = (menu.x/2)
  184.     menu.y = (menu.y/2) - (#menu.items/2)
  185.     for i = 1,#menu.items do
  186.         if term.isColor() then
  187.             term.setTextColor(menu.items[i].color)
  188.         else
  189.             term.setTextColor(colors.white)
  190.         end
  191.         term.setCursorPos(menu.x-(string.len(menu.items[i].text)/2),menu.y+(i))
  192.         menu.items[i].xPos1,menu.items[i].yPos = term.getCursorPos()
  193.         if menu[menu.currentMenu].pos == i then
  194.             print("<"..menu.items[i].text..">")
  195.         else
  196.             print(" "..menu.items[i].text)
  197.         end
  198.         menu.items[i].xPos2 = menu.items[i].xPos1 + string.len(menu.items[i].text)
  199.     end
  200. end
  201.  
  202. function menu.run()
  203.     menu.items[menu[menu.currentMenu].pos].handler()
  204. end
  205.  
  206. function menu.checkClick(x,y)
  207.     for i = 1,#menu.items do
  208.         if x >= menu.items[i].xPos1
  209.         and x <= menu.items[i].xPos2
  210.         and y == menu.items[i].yPos then
  211.             return i
  212.         end
  213.     end
  214. end
  215.  
  216. while true do
  217.     menu.items = menu[menu.currentMenu].items
  218.     menu.print(menu.pos)
  219.     program.eventData[1],program.eventData[2],program.eventData[3],program.eventData[4] = os.pullEvent()
  220.     if program.eventData[1] == "key" then
  221.         if program.eventData[2] == 208
  222.         or program.eventData[2] == 205 then
  223.             menu[menu.currentMenu].pos = menu[menu.currentMenu].pos + 1
  224.         elseif program.eventData[2] == 200
  225.         or program.eventData[2] == 203 then
  226.             menu[menu.currentMenu].pos = menu[menu.currentMenu].pos - 1
  227.         elseif program.eventData[2] == 28 then
  228.             menu.run()
  229.         end
  230.     elseif program.eventData[1] == "mouse_scroll" then
  231.         menu[menu.currentMenu].pos = menu[menu.currentMenu].pos + program.eventData[2]
  232.     elseif program.eventData[1] == "mouse_click" then
  233.         if program.eventData[2] == 1 then
  234.             menu.temp = menu.checkClick(program.eventData[3],program.eventData[4])
  235.             if menu.temp ~= nil then
  236.                 menu[menu.currentMenu].pos = menu.temp
  237.                 menu.run()
  238.             end
  239.         end
  240.     end
  241.     if menu[menu.currentMenu].pos > #menu.items then
  242.         menu[menu.currentMenu].pos = 1
  243.     elseif menu[menu.currentMenu].pos < 1 then
  244.         menu[menu.currentMenu].pos = #menu.items
  245.     end
  246. end
Advertisement
Add Comment
Please, Sign In to add comment