Advertisement
pedrosgali

PedrOS OSMain Alpha v1.0

Nov 21st, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.93 KB | None | 0 0
  1. --rednet.open("bottom")
  2. os.loadAPI("OS/API/file")
  3. os.loadAPI("OS/API/draw")
  4.  
  5. --GLOBAL VARIABLES--
  6.  
  7. maxX, maxY = term.getSize()
  8. clockX = maxX - 1
  9. screen = "Desktop"
  10. menu = 1
  11. menuI = 1
  12. menuII = 1
  13. minCol = 2
  14. maxCol = 3
  15. line = 4
  16. state = 5
  17. startMin = 1
  18. startMax = 5
  19. startY = maxY
  20. start = false
  21. subMenu = false
  22.  
  23. --COLOURS!!! THAT'S RIGHT A FUCKING U IN COLOURS!!--
  24.  
  25. white = 1
  26. orange = 2
  27. magenta = 4
  28. lightBlue = 8
  29. yellow = 16
  30. lime = 32
  31. pink = 64
  32. grey = 128
  33. lightGrey = 256
  34. cyan = 512
  35. purple = 1024
  36. blue = 2048
  37. brown = 4096
  38. green = 8192
  39. red = 16384
  40. black = 32768
  41. --playerColor = lightBlue
  42. term.setTextColor(white)
  43.  
  44. --PERIPHERAL FUNCTIONS--
  45.  
  46. function check()
  47.     perData = peripheral.getNames()
  48.     for i = 1, #perData do
  49.         if peripheral.getType(perData[i]) == "modem" then
  50.             m = peripheral.wrap(perData[i])
  51.             if m.isWireless() then
  52.                 wifiSide = perData[i]
  53.             else
  54.             end
  55.             --print("Modem installed on "..perData[i])
  56.         elseif peripheral.getType(perData[i]) == "printer" then
  57.             p = peripheral.wrap(perData[i])
  58.             --print("Printer installed on "..perData[i])
  59.         end
  60.     end
  61. end
  62.  
  63. --TABLE FUNCTIONS--
  64.  
  65. function menuTable()
  66.     mTable = {}
  67.     menuWidth = 1
  68.     data = fs.open("OS/menus/menuData", "r")
  69.     maxEntries = tonumber(data.readLine())
  70.     for i = 1, maxEntries do
  71.         mTable[i] = {}
  72.         mTable[i][menu] = tostring(data.readLine())
  73.         mTable[i][minCol] = 1
  74.         mTable[i][maxCol] = string.len(mTable[i][menu]) + 1
  75.         mTable[i][line] = (maxY - 1) - (i - 1)
  76.         mTable[i][state] = false
  77.         newWidth = mTable[i][maxCol]
  78.         if newWidth > menuWidth then
  79.             menuWidth = newWidth
  80.         end
  81.     end
  82.     for i = 1, maxEntries do
  83.         mTable[i][maxCol] = menuWidth
  84.     end
  85.     data.close()
  86. end
  87.  
  88. function loadTable(fileName)
  89.     subMenuWidth = 0
  90.     newMax = 0
  91.     subMenuTable = nil
  92.     subMenuTable = {}
  93.     data = fs.open("OS/menus/"..fileName, "r")
  94.     newMax = tonumber(data.readLine())
  95.     for i = 1, newMax do
  96.         subMenuTable[i] = {}
  97.         subMenuTable[i][menu] = tostring(data.readLine())
  98.         subMenuTable[i][minCol] = menuWidth + 1
  99.         subMenuTable[i][maxCol] = string.len(subMenuTable[i][menu])
  100.         subMenuTable[i][line] = (y - (i - 1))
  101.         subMenuTable[i][state] = false
  102.         newWidth = subMenuTable[i][maxCol]
  103.         if newWidth > subMenuWidth then
  104.             subMenuWidth = newWidth
  105.         end
  106.     end
  107.     for i = 1, newMax do
  108.         subMenuTable[i][maxCol] = (subMenuWidth + menuWidth)
  109.     end
  110.     data.close()
  111.     subMenu = true
  112. end
  113.  
  114. --DRAW FUNCTIONS--
  115.  
  116. function drawBackground()
  117.     draw.fakeScreen()
  118. end
  119.  
  120. function drawScreen()
  121.     draw.fakeScreen()
  122.     term.setBackgroundColor(lightGrey)
  123.     if start then
  124.         for i = 1, maxEntries do
  125.             term.setCursorPos(mTable[i][minCol], mTable[i][line])
  126.             for j = 1, menuWidth do
  127.                 term.write(" ")
  128.             end
  129.             term.setCursorPos(mTable[i][minCol], mTable[i][line])
  130.             term.write(mTable[i][menu])
  131.             if mTable[i][state] then
  132.                 loadTable(mTable[i][menu])
  133.                 for k = 1, newMax do
  134.                     term.setCursorPos(subMenuTable[k][minCol], subMenuTable[k][line])
  135.                     for l = 1, subMenuWidth do
  136.                         term.write(" ")
  137.                     end
  138.                     term.setCursorPos(subMenuTable[k][minCol], subMenuTable[k][line])
  139.                     term.write(subMenuTable[k][menu])
  140.                 end
  141.             else
  142.                
  143.             end
  144.         end
  145.     else
  146.     draw.fakeScreen()
  147.     end
  148.     term.setCursorPos((maxX + 1), maxY)
  149. end
  150.  
  151. --USER FUNCTIONS--
  152.  
  153. function login()
  154.     draw.loginScreen("Enter username:")
  155.     input = read()
  156.     user = tostring(input)
  157.     if fs.exists("OS/Users/"..user) then
  158.     else
  159.         uData = fs.open("OS/Users/"..user, "w")
  160.         draw.loginScreen("Enter password:")
  161.         input = read("*")
  162.         passKey = tostring(input)
  163.         uData.writeLine(passKey)
  164.         uData.writeLine("'OS/Artwork/Background'")
  165.         uData.writeLine(draw.magenta)
  166.         uData.close()
  167.     end
  168.     uData = fs.open("OS/Users/"..user, "r")
  169.     passKey = uData.readLine()
  170.     background = tostring(uData.readLine())
  171.     hColour = uData.readLine()
  172.     uData.close()
  173.     uData = fs.open("OS/Users/curUser", "w")
  174.    
  175.     uData.writeLine("background = "..background)
  176.     uData.writeLine("hColour = "..hColour)
  177.     uData.close()
  178.     draw.inputBox("Enter password:")
  179.     input = read("*")
  180.     pw = tostring(input)
  181.     while true do
  182.         if pw == passKey then
  183.             os.loadAPI("OS/Users/curUser")
  184.             break
  185.         else
  186.             draw.inputBox("Try again:")
  187.             input = read("*")
  188.             pw = tostring(input)
  189.         end
  190.     end
  191. end
  192.  
  193. function launch(prog)
  194.     if prog == "Shutdown" then
  195.         os.shutdown()
  196.     elseif prog == "Reboot" then
  197.         os.reboot()
  198.     elseif prog == "To_COS" then
  199.         quitToCOS = true
  200.     else
  201.         for i = 1, 2 do
  202.             shell.run("cd ..")
  203.         end
  204.         shell.run("cd OS")
  205.         shell.run("cd "..menuChoice)
  206.         closeMenus()
  207.         drawScreen()
  208.         if button == 1 then
  209.             term.setCursorPos(7, 19)
  210.             term.setBackgroundColor(black)
  211.             write(prog)
  212.             shell.run(prog)
  213.         else
  214.             shell.run("edit "..prog)
  215.         end
  216.         shell.run("cd ..")
  217.         shell.run("cd ..")
  218.         os.startTimer(1)
  219.     end
  220. end
  221.  
  222. function closeMenus()
  223.     start = false
  224.     subMenu = false
  225.     for i = 1, maxEntries do
  226.         mTable[i][state] = false
  227.     end
  228. end
  229.  
  230. function mouse()
  231.     --event, button, x, y = os.pullEvent("mouse_click")
  232.     closeStart = true
  233.     if start then
  234.         for i = 1, maxEntries do
  235.             if x >= mTable[i][minCol] and x <= mTable[i][maxCol] then
  236.                 if y == mTable[i][line] then
  237.                     mTable[i][state] = not mTable[i][state]
  238.                     if mTable[i][state] then
  239.                         loadTable(mTable[i][menu])
  240.                         menuChoice = mTable[i][menu]
  241.                     end
  242.                     closeStart = false
  243.                     break
  244.                 else
  245.                     --closeMenus()
  246.                 end
  247.             end
  248.         end
  249.         if subMenu then
  250.             for j = 1, newMax do
  251.                 if x >= subMenuTable[j][minCol] and x <= subMenuTable[j][maxCol] then
  252.                     if y == subMenuTable[j][line] then
  253.                         program = subMenuTable[j][menu]
  254.                         launch(program)
  255.                         closeMenus()
  256.                         closeStart = false
  257.                         break
  258.                     else
  259.                         --closeMenus()
  260.                     end
  261.                 end
  262.             end
  263.         else
  264.         end
  265.     end
  266.     if x >= startMin and x <= startMax then
  267.         if y == startY then
  268.             if start then
  269.                 closeMenus()
  270.             else
  271.                 start = not start
  272.                 for i = 1, maxEntries do
  273.                     if mTable[i][menu] == "Turn_Off" or mTable[i][menu] == "Commands" then
  274.                     else
  275.                         file.updateMenu(mTable[i][menu])
  276.                     end
  277.                 end
  278.             end
  279.             closeStart = false
  280.         end
  281.     end
  282.     if closeStart then
  283.        -- if x >= (1) and x <= (menuWidth + subMenuWidth) then
  284.        closeMenus()
  285.     --end
  286.     end
  287.     drawScreen()
  288. end
  289.  
  290. function timeUpdate()
  291.     term.setBackgroundColor(lightGrey)
  292.     osTime = os.time()
  293.     osTime = tostring(osTime)
  294.     tLength = string.len(osTime)    
  295.     term.setCursorPos(clockX - tLength, maxY)
  296.     write(osTime)
  297.     --os.startTimer(1)
  298. end
  299.  
  300. function inputs()
  301.     event, p1, p2, p3 = os.pullEvent()
  302.     if event == "mouse_click" then
  303.         button, x, y = p1, p2, p3
  304.         mouse()
  305.     elseif event == "peripheral" or event == "peripheral_detach" then
  306.         check()
  307.     elseif event == "timer" then
  308.         drawScreen()
  309.         os.startTimer(1)
  310.     end
  311. end
  312.  
  313. --DEBUG FUNCTIONS--
  314.  
  315. function checkTable()
  316.     for i = 1, maxEntries do
  317.         print("Name: "..textutils.serialize(mTable[i][menu])) --.." : "..tostring(mTable[i][minCol]).." : "..tostring(mTable[i][maxCol]).." : "..tostring(mTable[i][line]).." : "..tostring(mTable[i][state]))
  318. --        print("Name: "..tostring(mTable[i][menu][menu1]))
  319.     end
  320. --    print("Longest entry: "..bigLength)
  321. end
  322.  
  323. --PROGRAM--
  324.  
  325. quitToCOS = false
  326. check()
  327. menuTable()
  328. login()
  329. term.clear()
  330. drawScreen()
  331. os.startTimer(2)
  332. repeat
  333.     inputs()
  334.     --mouse()
  335.     drawScreen()
  336. until quitToCOS
  337. term.setBackgroundColor(black)
  338. term.clear()
  339. term.setCursorPos(1, 19)
  340. term.setTextColor(white)
  341. print("CraftOS prompt:")
  342. print("Type exit to return.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement