Advertisement
SpaceRanger4321

Main OS

Dec 31st, 2019
1,935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.25 KB | None | 0 0
  1. -- SpaceOS --
  2. -- Config || Changeing these colors require a Restart
  3. local bg = colors.yellow
  4. local tcolor = colors.lightGray
  5. -- *** MAIN ENGINE *** *** MAIN ENGINE *** *** MAIN ENGINE *** --
  6. -- FUNCTIONS --
  7. local w,h = term.getSize()
  8. function valid(value,expected) if type(value) == expected then return true else return false end end
  9. function clr() term.clear() end
  10. function cp(x,y) if not valid(x,"number") then return error("Expected x to be a number, got "..type(x)) end if not valid(y,"number") then return error("Expected y to be a number, got "..type(y)) end term.setCursorPos(x,y) end
  11. function setText(col) if not valid(col,"string") then return error("Expected col to be a string, got "..type(x)) else return term.setTextColor(colors[col]) end end
  12. function setBack(col) if not valid(col,"string") then return error("Expected col to be a string, got "..type(x)) else return term.setBackgroundColor(colors[col]) end end
  13. function notnil(vari) if vari == nil then return false else return true end end
  14. function lt(tab) if not valid(tab,"table") then return error("Expected tab to be a table, got "..type(tab)) end local lt = 0 for i=1, #tab do if string.len(tab[i]) > lt then lt = string.len(tab[i]) end end return lt end
  15. function pullEvents(...) -- for pulling multiple events at a time, first event get priority
  16.     local args = {...}
  17.     --if #args == 0 then return os.pullEvent() end
  18.     while true do
  19.         sleep(.000000001)
  20.         event, arg1, arg2, arg3, arg4 = os.pullEvent()
  21.         for i=1, #args do
  22.             if args[i] == event then
  23.                 return event, arg1, arg2, arg3, arg4
  24.             end
  25.         end
  26.     end
  27. end
  28. -- FUNCTIONS --
  29. -- COLLISION --
  30. col = {}
  31.  
  32. function printCentered(Text, Line)
  33.     local x, y = term.getSize()
  34.     x = x/2 - #Text/2
  35.     term.setCursorPos(x, Line)
  36.     if Color then
  37.         col.set(Color, BkgColor)
  38.     end
  39.     term.write(Text)
  40.     return true  
  41. end
  42.  
  43. function debug()   
  44.     setBack("blue")
  45.     clr()
  46.     setText("orange")
  47.     printCentered("Debugging Editor ", 5)
  48.     textutils.slowPrint(". . .", 3)
  49.     sleep(0.1)
  50.     shell.run("edit /.os/.gui")
  51.     shell.run("/.os/.gui")
  52. end
  53.  
  54. function reload()
  55.     shell.run("/.os/.gui")
  56. end
  57.  
  58. function createCol(name,x,y,x2,y2,status)
  59.     name = name or "name_"..#col table.insert(col,{}) status = status or true
  60.     col[#col].x, col[#col].y, col[#col].x2, col[#col].y2, col[#col].name, col[#col].status = x, y, x2, y2, name, status
  61. end
  62. function colExists(name)
  63.     for i=1, #col do
  64.         if col[i].name == name then return i end
  65.     end
  66.     return false
  67. end
  68. function removeCol(name)
  69.     if colExists(name) ~= false then table.remove(col, colExists(name)) end
  70. end
  71. function moveCol(name,newx,newy)
  72.     if not colExists(name) then return error("Collision does not exists") end
  73.     name = colExists(name)
  74.     col[name].x2,col[name].y2 = col[name].x2+newx-col[name].x, col[name].y2+newy-col[name].y
  75.     col[name].x,col[name].y = newx,newy
  76. end
  77. function drawCol(name, color)
  78.     if not notnil(color) then if col[name].status then color = colors.green else color = colors.red end end
  79.     for i=1, #col do
  80.         if name == col[i].name then
  81.             return paintutils.drawFilledBox(col[i].x,col[i].y,col[i].x2,col[i].y2,color)
  82.         end
  83.     end
  84.     return false
  85. end
  86. function checkCol(x,y)
  87.     for i=1, #col do
  88.         if x >= col[i].x and x <= col[i].x2 and y >= col[i].y and y <= col[i].y2 then
  89.             return col[i].name
  90.         end
  91.     end
  92.     return false
  93. end
  94. -- COLLISION --
  95. -- COLOR RELATED --
  96. function renderImage(image,x,y) -- used to display custom images fast and easy
  97.     if not valid(image,"table") then return error("Expected table for image, got "..type(image)) end
  98.     if not valid(x,"number") then return error("Expected number for x, got "..type(x)) end
  99.     if not valid(y,"number") then return error("Expected number for y, got "..type(y)) end
  100.    
  101.     cp(x,y)
  102.     for i=1, #image do
  103.         term.blit(string.rep(" ", #image[i]),string.rep("0", #image[i]),image[i])
  104.         cp(x,y+i)
  105.     end
  106. end
  107. -- COLOR RELATED --
  108. -- RCM --
  109. local rcm_data = {}
  110. rcm_data.menu = {"Test 1", "Test 2", "Test 3"}
  111. rcm_data.active = false
  112. rcm_data.x, rcm_data.y = 1,1
  113. function rcm_click(x,y)
  114.     if x >= rcm_data.x and x <= rcm_data.x+lt(rcm_data.menu) and y >= rcm_data.y and y <= rcm_data.y+#rcm_data.menu then
  115.         return true
  116.     end
  117.     return false
  118. end
  119. function setRcmMenu(tab)
  120.     if not valid(tab,"table") then return error("Expected tab to be a table, got "..type(tab)) end
  121.     rcm_data.menu = tab
  122. end
  123. function rcm(x,y,options,wid,hei)
  124.     if not valid(x,"number") then return error("Expected number for x, got "..type(x)) end
  125.     if not valid(y,"number") then return error("Expected number for y, got "..type(y)) end
  126.     if not valid(options,"table") then return error("Expected table for image, got "..type(options)) end
  127.     local lnt = lt(options) -- short for longest term (used to set width of menu)
  128.     local w,h if not notnil(wid) or not notnil(hei) then w,h = term.getSize() else w,h = wid,hei end
  129.     if x >= w-lnt then
  130.         if y > h-#options-1 then
  131.             paintutils.drawBox(x-1,y-1,x-lnt-1,y-#options-1,colors.lightGray)
  132.             paintutils.drawFilledBox(x,y,x-lnt,y-#options,colors.white)
  133.             --cp(x,y) write("1")
  134.             setText("black")
  135.             for i=1, #options do
  136.                 cp(x-lnt,y-#options+i-1) write(options[i])
  137.             end
  138.         else
  139.             paintutils.drawBox(x-lnt-1,y+1,x-1,y+#options+1,colors.lightGray)
  140.             paintutils.drawFilledBox(x-lnt,y,x,y+#options,colors.white)
  141.             --cp(x,y) write("2")
  142.             setText("black")
  143.             for i=1, #options do
  144.                 cp(x-lnt,y+i-1) write(options[i])
  145.             end
  146.         end
  147.     elseif x < w-lnt then
  148.         if y > h-#options-1 then
  149.             paintutils.drawBox(x+1,y-#options-1,x+lnt+1,y-1,colors.lightGray)
  150.             paintutils.drawFilledBox(x,y-#options,x+lnt,y,colors.white)
  151.             --cp(x,y) write("3")
  152.             setText("black")
  153.             for i=1, #options do
  154.                 cp(x,y-#options+i-1) write(options[i])
  155.             end
  156.         else
  157.             paintutils.drawBox(x+1,y+1,x+lnt+1,y+#options+1,colors.lightGray)
  158.             paintutils.drawFilledBox(x,y,x+lnt,y+#options,colors.white)
  159.             --cp(x,y) write("4")
  160.             setText("black")
  161.             for i=1, #options do
  162.                 cp(x,y+i-1) write(options[i])
  163.             end
  164.         end
  165.     end
  166.     sleep(.0001)
  167. end
  168. -- RCM --
  169. -- WINDOWS & SHORTCUTS --
  170. window = {}
  171. desktop = {}
  172. local current = term.current()
  173. function createWindow(name,x,y,x2,y2,program)
  174.     table.insert(window,window.create(term.current(), x,y,x2,y2-1))
  175.     window[#window].display = true
  176.     window[#window].name = name or "window_"..#window -- reason being user programs and use manipulation
  177. end
  178. function findWindow(name)
  179.     for i=1, #window do
  180.         if window[i].name == name then
  181.             return i
  182.         end
  183.     end
  184.     return false
  185. end
  186. function moveWindow(id,newx,newy)
  187.     local current = term.current()
  188.     term.redirect(window[id])
  189.     window.reposition()
  190.     term.redirect(current)
  191. end
  192. function toggleWindow(id)
  193.     local current = term.current()
  194.     window.redirect(window[id])
  195.     window.setVisible(not window[id].display)
  196.     window[id].display = not window[id].display
  197.     window.redirect(current)
  198. end
  199. function shortcutExists(name)
  200.     for i=1, #desktop do
  201.         if desktop[i].name == name then
  202.             return i
  203.         end
  204.     end
  205.     return false
  206. end
  207. function drawShortcut(name,x,y)
  208.     if shortcutExists(name) ~= false then
  209.         cp(x,y) paintutils.drawImage(desktop[shortcutExists(name)].icon)
  210.         cp(x,y)
  211.     end
  212. end
  213.  
  214. function createShortcut(name, icon)
  215.     if not shortcutExists(name) then
  216.         table.insert(desktop, {})
  217.         desktop[#desktop].name = name
  218.         desktop[#desktop].icon = icon
  219.         desktop[#desktop].x = 1
  220.         desktop[#desktop].y = 1
  221.     end
  222. end
  223. -- WINDOWS & SHORTCUTS --
  224. -- *** MAIN ENGINE *** *** MAIN ENGINE *** *** MAIN ENGINE *** --
  225.  
  226. local dir = ".os"
  227. if fs.exists(dir.."/.backgrounds/desktop") then
  228.     desktop_img = paintutils.loadImage(dir.."/.backgrounds/desktop")
  229. end
  230. local functions = { -- PUT RCM OPTIONS HERE (ALL LOWERCASE AND WITH NO SPACES)
  231.     refresh = function() sleep(2) setBack("orange") clr() printCentered("Refreshing . . .", 5) sleep(3) shell.run("/.os/.gui") end,
  232.     debug = function() debug() end,
  233.     lua = function() setBack("black") term.setCursorPos(1,1) term.clear() shell.run("lua") end,
  234.     commandline = function() setBack("black") clr() term.setCursorPos(1,1) shell.run("shell") end,
  235.     desktop = function() shell.run("paint", "/.os//.backgrounds/desktop") reload() end,
  236.     usericon = function() shell.run("paint", "/.os/.backgrounds/pIcon") reload() end,
  237.     reboot = function() setBack("yellow") setText("red") clr() printCentered("- "..string.char(7).." % Re-Booting % "..string.char(7).." -", 5) sleep(2) os.reboot() end,
  238.     shutdown = function() setBack("yellow") setText("blue") clr() printCentered("- "..string.char(7).." % Shutting Down % "..string.char(7).." -", 5) sleep(2) os.shutdown() end,
  239.     reinstall = function() shell.run("/.os/.reinstall") end,
  240.     seperator = function() shell.run("/.os/.gui") end,
  241.     uninstaller = function() shell.run("/.os/.uninstall") end
  242. }
  243. running = true
  244. local update = true
  245.  
  246. function runtime()
  247.     setRcmMenu({"Refresh", "Debug", "Lua", "CommandLine", "Desktop", "UserIcon", "ReBoot", "ShutDown", "ReInstall", "", "UnInstaller"})
  248.     term.setBackgroundColor(bg)
  249.     clr()
  250.     while running do
  251.         if update then
  252.             update = false
  253.             term.setBackgroundColor(bg)
  254.             term.clear()
  255.             paintutils.drawImage(desktop_img,1,1)
  256.             paintutils.drawLine(1,h,w,h,tcolor)
  257.             paintutils.drawPixel(1,h,colors.blue)
  258.             local current = term.current()
  259.             for i=1, #window do
  260.                 if window[i].display then
  261.                     local current2 = term.current()
  262.                     term.redirect(current)
  263.                     paintutils.drawPixel(i+2,h,colors.lightGray) cp(i+2,h) write("^")
  264.                     local wx,wy = term.getSize()
  265.                     paintutils.drawLine(1,0,wx,0,colors.white)
  266.                     term.redirect(current2)
  267.                 else
  268.                     local current2 = term.current()
  269.                     term.redirect(current)
  270.                     paintutils.drawPixel(i+2,h,colors.gray) cp(i+2,h) write("-")
  271.                     term.redirect(current2)
  272.                 end
  273.             end
  274.             term.redirect(current)
  275.             if rcm_data.active == true then
  276.                 rcm(rcm_data.x,rcm_data.y,rcm_data.menu,w,h-1)
  277.             end
  278.         end
  279.         a,i,x,y = pullEvents("mouse_click", "mouse_drag")
  280.         if a == "mouse_click" then
  281.             if i == 1 then
  282.                 if rcm_data.active then
  283.                     if rcm_click(x,y) then
  284.                         if notnil(functions[string.lower(rcm_data.menu[y-rcm_data.y+1] or "")]) then functions[string.lower(rcm_data.menu[y-rcm_data.y+1])]() end
  285.                     end
  286.                     rcm_data.active = false update = true setBack("yellow") clr()
  287.                 else
  288.                    
  289.                 end
  290.             elseif i == 2 then
  291.                 setBack("yellow") clr() rcm_data.active = true rcm_data.x,rcm_data.y = x,y update = true
  292.             end
  293.         elseif a == "mouse_drag" then
  294.            
  295.         end
  296.     end
  297. end
  298.  
  299. function engine() -- this will run windows that run programs that keep runtime running. runtime controls actions to the window
  300.     while running do
  301.         sleep(1)
  302.     end
  303. end
  304.  
  305. local r,e = pcall(parallel.waitForAny(runtime,engine))
  306.  
  307. if not r then
  308.     setBack("yellow") setText("white") clr()
  309.   setBack("gray") cp(1,h/2-4) term.clearLine() cp(1,h/2-4)
  310.   print("Yellow screen of Death") setText("yellow")
  311.   setBack("white")
  312.   cp(1,h/2-3)
  313.   for i=1, w do
  314.     write(" ")
  315.   end
  316.   setBack("lightGray")
  317.   for i=1, 8 do
  318.     cp(1,h/2-3+i)
  319.     term.clearLine()
  320.   end
  321.   cp(1,h/2-2)
  322.   print("A yellow screen error (also called a stop error) can occur if a problem causes your device to shut down or restart unexpectedly. Your device ran into a problem and needs to restart.")
  323.   print("")
  324.   write("Error Code: ")
  325.   printError(err)
  326.   term.setCursorBlink(false)
  327.   sleep(1)
  328.   print("")
  329.   print("Press any key to reboot...")
  330.   os.pullEvent("key")
  331. else
  332.     print("Exited without Errors")
  333. end
  334. os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement