MinePossu

void

Apr 19th, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 28.68 KB | None | 0 0
  1. --this void os version is for advanced pocket copmuters this is modified by me MinePossu to work with advanced pocket
  2. --copmuters
  3. --but visol is original creator void os that does not work on advanced pocket computers
  4.  
  5. wall = paintutils.loadImage("/void/wall")
  6. lastclick = os.clock()
  7. pressed = 0
  8. posx = 0
  9. grabx = 0
  10. windows = {}
  11. windowid = 1
  12. deleteWindowid = 0
  13. voidmenu = {}
  14. voidmenu['active'] = false
  15. voidmenu['width'] = 4
  16. voids = {}
  17. voidid = 1
  18. order = {}
  19. textBoxOn = false
  20. menus = {}
  21. menui = {}
  22. menuID = 1
  23. menuiID = 1
  24. currentMenuPos = 1
  25. selectedMenu = -1
  26. flashMenuItemID = -1
  27. disableLoop = false
  28. icons = {}
  29. iconid = 1
  30. monicons = {}
  31. moniconid = 1
  32. rClick = {}
  33. rClick['active'] = false
  34. rClick['width'] = 0
  35. rClickItems  = {}
  36. rItemID = 1
  37. monitorside = ""
  38. monitor = ""
  39.  
  40. -- update deactivated for good reason
  41. function checkUpdate() 
  42. --versionFile = fs.open("/void/version", "r")
  43. --  currentVersion = tonumber(versionFile.readLine())
  44. --  newVersion = tonumber(http.get("https://pastebin.com/raw/r5H2vTeW").readAll())
  45. --  versionFile.close()
  46. --  if(newVersion > currentVersion)then
  47. --      return true
  48. --  end
  49. --  return false
  50. end
  51.  
  52. --------------------------
  53. -- Windows and Taskbars --
  54. --------------------------
  55.  
  56. function drawWallpaper()
  57.     paintutils.drawImage(wall, 1, 1)
  58. end
  59.  
  60. function makeWindow(width, height, name, xPos, yPos)
  61.     windows[windowid] = {}
  62.     windows[windowid]["width"] = width
  63.     windows[windowid]["height"] = height
  64.     windows[windowid]["xPos"] = xPos
  65.     windows[windowid]["yPos"] = yPos
  66.     windows[windowid]["name"] = name
  67.     windows[windowid]["minimised"] = false
  68.     ca = table.getn(windows)
  69.     order[ca + 1] = windowid
  70.     orderTop(windowid)
  71.     windowid = windowid + 1
  72.     return windowid - 1
  73. end
  74.  
  75. function orderTop(id)
  76.     new = {}
  77.     count = 1
  78.     for ik, data in pairs(windows) do
  79.         if(ik ~= id)then
  80.             new[count] = ik
  81.             count = count + 1
  82.         end
  83.     end
  84.     new[count] = id
  85.     order = new
  86. end
  87.  
  88. function drawWindow(id)
  89.     window = windows[id]
  90.     if(window['minimised'] == false)then
  91.         term.setBackgroundColor(colors.blue)
  92.         term.setTextColor(colors.white)
  93.         term.setCursorPos(window["xPos"], window["yPos"])
  94.         for h = 1, window['width'] + 1 do
  95.             term.write(" ")
  96.         end
  97.         term.setCursorPos(window['xPos'] + 3, window['yPos'])
  98.         term.write(window['name'])
  99.         term.setBackgroundColor(colors.red)
  100.         term.setCursorPos(window["xPos"], window["yPos"])
  101.         term.write("X")
  102.         term.setBackgroundColor(colors.lime)
  103.         term.setCursorPos(window["xPos"] + 1, window["yPos"])
  104.         term.write("_")
  105.         term.setBackgroundColor(colors.lightGray)
  106.         for x = window['xPos'], window['xPos'] + window['width'] do
  107.             for y = window['yPos'] + 1, window['yPos'] + window['height'] do
  108.                 term.setCursorPos(x, y)
  109.                 term.write(" ")
  110.             end
  111.         end
  112.     end
  113. end
  114.  
  115. function drawWindows()
  116.     if table.getn(windows) == nil then else
  117.         term.setBackgroundColor(colors.black)
  118.         for oid, idm in pairs(order) do
  119.             drawWindow(idm)
  120.         end
  121.         term.setBackgroundColor(colors.black)
  122.     end
  123. end
  124.  
  125. function checkWindows(xPos, yPos)
  126.     if table.getn(windows) >= 1 then
  127.         for oid, id in pairs(order) do
  128.             data = windows[id]
  129.             if(data['minimised'] == false)then
  130.                 if(xPos >= data['xPos'] + 2 and xPos <= data['xPos'] + data['width'])then
  131.                     if(yPos == data['yPos'])then
  132.                         orderTop(id)
  133.                         return id
  134.                     end
  135.                 elseif(xPos == data['xPos'] and yPos == data['yPos'])then
  136.                     orderTop(id)
  137.                     deleteWindowid = id
  138.                     return id
  139.                 elseif(xPos == data['xPos'] + 1 and yPos == data['yPos'])then
  140.                     data['minimised'] = true
  141.                     return id
  142.                 end
  143.             end
  144.         end
  145.     end
  146.     return 0
  147. end
  148.  
  149. function drawTaskBar()
  150.     w, h = term.getSize()
  151.     term.setBackgroundColor(colors.gray)
  152.     for x = 1, w do
  153.         term.setCursorPos(x, h)
  154.         term.write(" ")
  155.     end
  156.     term.setTextColor(colors.cyan)
  157.     if(voidmenu['active'] == false)then
  158.         term.setBackgroundColor(colors.orange)
  159.     else
  160.         total = table.getn(voids)
  161.         count = total
  162.         term.setBackgroundColor(colors.white)
  163.         term.setTextColor(colors.black)
  164.         for id, data in pairs(voids) do
  165.             if(data['flash'] == true)then
  166.                 term.setBackgroundColor(colors.red)
  167.             else
  168.                 term.setBackgroundColor(colors.white)
  169.             end
  170.             term.setCursorPos(1, h - total + count - 1)
  171.             term.write(data['name'])
  172.             if(string.len(data['name']) < voidmenu['width'])then
  173.                 for wx = string.len(data['name']) + 1, voidmenu['width'] do
  174.                     term.setCursorPos(wx, h - total + count - 1)
  175.                     term.write(" ")
  176.                 end
  177.             end
  178.             count = count - 1
  179.         end
  180.         term.setTextColor(colors.cyan)
  181.         term.setBackgroundColor(colors.red)
  182.     end
  183.     term.setCursorPos(1, h)
  184.     term.write("Void")
  185.     position = 6
  186.     for id, data in pairs(windows) do
  187.         if data['minimised'] == true then
  188.             term.setCursorPos(position, h)
  189.             term.setBackgroundColor(colors.blue)
  190.             term.write(data['name'])
  191.             position = position + string.len(data['name']) + 1
  192.         end
  193.     end
  194. end
  195.  
  196. function checkTaskBar(xPos, yPos)
  197.     w, h = term.getSize()
  198.     if(yPos == h)then
  199.         if(xPos <= 4)then
  200.             voidmenu['active'] = not voidmenu['active']
  201.             return true
  202.         else
  203.             position = 6
  204.             for id, data in pairs(windows) do
  205.                 if data['minimised'] == true then
  206.                     if(xPos >= position and xPos <= position + string.len(data['name']) - 1)then
  207.                         data['minimised'] = false
  208.                         return true
  209.                     end
  210.                     position = position + string.len(data['name']) + 1
  211.                 end
  212.             end
  213.         end
  214.     else
  215.         if(voidmenu['active'] == true)then
  216.             total = table.getn(voids)
  217.             count = total
  218.             if(xPos <= voidmenu['width'] and yPos >= h - count - 1 and yPos <= h - 1)then
  219.                 for id, data in pairs(voids) do
  220.                     if(yPos == h - total + count - 1)then
  221.                         activateMenu(id)
  222.                         return true
  223.                     end
  224.                     count = count - 1
  225.                 end
  226.             end
  227.         end
  228.     end
  229.     return false
  230. end
  231.  
  232. function drawScreen()
  233.     term.clear()
  234.     term.setBackgroundColor(colors.black)
  235.     drawWallpaper()
  236.     term.setBackgroundColor(colors.black)
  237.     drawIcons()
  238.     term.setBackgroundColor(colors.black)
  239.     drawWindows()
  240.     term.setBackgroundColor(colors.black)
  241.     drawMenus()
  242.     term.setBackgroundColor(colors.black)
  243.     drawTaskBar()
  244.     term.setBackgroundColor(colors.black)
  245.     drawClock()
  246.     term.setBackgroundColor(colors.black)
  247. end
  248.  
  249. function deleteWindow(id)
  250.     table.remove(order)
  251.     windows[id] = nil
  252.     return true
  253. end
  254.  
  255. function getWindowId(name)
  256.     for id, data in pairs(windows) do
  257.         if(data['name'] == name)then
  258.             return id
  259.         end
  260.     end
  261.     return false
  262. end
  263.  
  264. function getWindowSize(id)
  265.     if windows[id] ~= nil then
  266.         return windows[id]['width'], windows[id]['height']
  267.     end
  268.     return false
  269. end
  270.  
  271. function getWindowPos(id)
  272.     if windows[id] ~= nil then
  273.         return windows[id]['xPos'], windows[id]['yPos'] + 1
  274.     end
  275.     return false
  276. end
  277.  
  278. function addVoid(name, func)
  279.     voids[voidid] = {}
  280.     voids[voidid]["name"] = name
  281.     voids[voidid]["func"] = func
  282.     voids[voidid]["flash"] = false
  283.     if(voidmenu['width'] < string.len(name))then
  284.         voidmenu['width'] = string.len(name)
  285.     end
  286.     voidid = voidid + 1
  287. end
  288.  
  289. function activateMenu(id)
  290.     print(id)
  291.     voids[id]['flash'] = true
  292.     drawScreen()
  293.     sleep(0.2)
  294.     voids[id]['flash'] = false
  295.     drawScreen()
  296.     voids[id]['func']()
  297. end
  298.  
  299. function message(message, second)
  300.     width = string.len(message) + 2
  301.     start = w / 2 - width / 2
  302.     height = 2
  303.     if second ~= nil then
  304.         height = 3
  305.         if(string.len(second) + 2 > width)then
  306.             width = string.len(second) + 2
  307.         end
  308.     end
  309.     term.setBackgroundColor(colors.lightGray)
  310.     term.setTextColor(colors.orange)
  311.     for x = start, start + width - 1 do
  312.         for y = 5, 5 + height do
  313.             term.setCursorPos(x, y)
  314.             term.write(" ")
  315.         end
  316.     end
  317.     fls = w / 2 - (string.len(message) + 2) / 2 + 1
  318.     term.setCursorPos(fls, 6)
  319.     term.write(message)
  320.     if second ~= nil then
  321.         sls = w / 2 - (string.len(second) + 2) / 2 + 1
  322.         term.setCursorPos(sls, 7)
  323.         term.write(second)
  324.     end
  325. end
  326.  
  327. function textBox(question, correction, second)
  328.     if textBoxOn == false then
  329.         w, h = term.getSize()
  330.         textBoxOn = true
  331.         term.setBackgroundColor(colors.lightGray)
  332.         term.setTextColor(colors.orange)
  333.         width = string.len(question) + 2
  334.         height = 4
  335.         if(second ~= nil)then
  336.             height = 5
  337.             if(string.len(second) + 2 > width)then
  338.                 width = string.len(second) + 2
  339.             end
  340.         end
  341.         start = w / 2 - width / 2
  342.         fls = w / 2 - (string.len(question) + 2) / 2
  343.         if(correction ~= nil)then
  344.             start = start + correction
  345.         end
  346.         for x = start, start + width - 1 do
  347.             for y = 5, 5 + height do
  348.                 term.setCursorPos(x, y)
  349.                 term.write(" ")
  350.             end
  351.         end
  352.         term.setCursorPos(fls + 1, 6)
  353.         term.write(question)
  354.         if(second ~= nil)then
  355.             newstart = w / 2 - string.len(second) / 2
  356.             term.setCursorPos(newstart + 1, 7)
  357.             term.write(second)
  358.         end
  359.         for x = start + 1, start + width - 2 do
  360.             term.setBackgroundColor(colors.white)
  361.             if(height == 5)then
  362.                 term.setCursorPos(x, 9)
  363.             else
  364.                 term.setCursorPos(x, 8)
  365.             end
  366.             term.write(" ")
  367.         end
  368.         if(height == 5)then
  369.             term.setCursorPos(start + 1, 9)
  370.         else
  371.             term.setCursorPos(start + 1, 8)
  372.         end
  373.         term.setCursorBlink(true)
  374.         answer = read()
  375.         term.setCursorBlink(false)
  376.         term.setBackgroundColor(colors.black)
  377.         textBoxOn = false
  378.         return answer
  379.     else
  380.         return false
  381.     end
  382. end
  383.  
  384. ----------------
  385. -- RightClick --
  386. ----------------
  387.  
  388. function rClearItems()
  389.     rItemID = 1
  390.     rClickItems = {}
  391. end
  392.  
  393. function rClickReset()
  394.     rClick = {}
  395.     rClick['active'] = false
  396.     rClick['width'] = 0
  397.     rClearItems()
  398. end
  399.  
  400. function rAddItem(name, func)
  401.     rClickItems[rItemID] = {}
  402.     rClickItems[rItemID]['name'] = name
  403.     rClickItems[rItemID]['func'] = func
  404.     rItemID = rItemID + 1
  405.     if(string.len(name) > rClick['width'])then
  406.         rClick['width'] = string.len(name)
  407.     end
  408.     return rItemID - 1
  409. end
  410.  
  411. function rClickCheck(xPos, yPos)
  412.     if(rClick['active'])then
  413.         if(table.getn(rClickItems))then
  414.             items = table.getn(rClickItems)
  415.             addition = 0
  416.             for id, data in pairs(rClickItems) do
  417.                 if(xPos >= rClick['x'] and xPos <= rClick['x'] + rClick['width'] - 1)then
  418.                     if(yPos == rClick['y'] + addition)then
  419.                         rClickItem(id)
  420.                         return id
  421.                     end
  422.                 end
  423.                 addition = addition + 1
  424.             end
  425.         end
  426.     end
  427.     return false
  428. end
  429.  
  430. function rClickDraw()
  431.     if(rClick['active'])then
  432.         if(table.getn(rClickItems))then
  433.             drawScreen()
  434.             w, h = term.getSize()
  435.             items = table.getn(rClickItems)
  436.             if h - rClick['y'] < items then
  437.                 rClick['y'] = rClick['y'] - (items - (h - rClick['y']))
  438.             end
  439.             if w - rClick['x'] < rClick['width'] then
  440.                 rClick['x'] = rClick['x'] - (rClick['width'] - (w - rClick['x']))
  441.             end
  442.             term.setBackgroundColor(colors.white)
  443.             term.setTextColor(colors.black)
  444.             for x = rClick['x'], rClick['x'] + rClick['width'] - 1 do
  445.                 for y = rClick['y'], rClick['y'] + items - 1 do
  446.                     term.setCursorPos(x, y)
  447.                     term.write(" ")
  448.                 end
  449.             end
  450.             addition = 0
  451.             for id, data in pairs(rClickItems) do
  452.                 term.setCursorPos(rClick['x'], rClick['y'] + addition)
  453.                 term.write(data['name'])
  454.                 addition = addition + 1
  455.             end
  456.         end
  457.     end
  458. end
  459.  
  460. function rClickItem(ID)
  461.     rClickItems[ID]['func']()
  462. end
  463.  
  464. -------------
  465. -- Desktop --
  466. -------------
  467.  
  468. function checkDestktop(xPos, yPos)
  469.     w, h = term.getSize()
  470.     if yPos >= 2 and yPos <= h - 1 then
  471.         rClickReset()
  472.         rClick['active'] = true
  473.         rClick['x'] = xPos
  474.         rClick['y'] = yPos
  475.         rAddItem("Edit wallpaper", editWallpaper)
  476.     end
  477. end
  478.  
  479. function editWallpaper()
  480.     shell.run("paint", "/void/wall")
  481.     wall = paintutils.loadImage("/void/wall")
  482. end
  483.  
  484. --------------
  485. -- MonIcons --
  486. --------------
  487.  
  488. function changeMonitor()
  489.     detectMonitors()
  490.     monitorside = textBox("Enter new monitor ID!", 1, "Enter new id or leave empty!")
  491.     while not checkMonitor() and monitorside ~= "" do
  492.         monitorside = textBox("Enter new monitor ID!", 1, "Enter new id or leave empty!")
  493.     end
  494.     if(monitorside == "")then
  495.         fs.delete("/void/monitorside")
  496.     else
  497.         file = fs.open("/void/monitorside", "w")
  498.         file.writeLine(monitorside)
  499.         file.close()
  500.         drawMonIcons()
  501.     end
  502. end
  503.  
  504. function detectMonitors()
  505.     sides = {"left", "right", "top", "bottom", "front", "back"}
  506.     available = {}
  507.     counter = 1
  508.     for x = 1, #sides do
  509.         if(peripheral.getType(sides[x]) == "monitor")then
  510.             available[counter] = sides[x]
  511.             counter = counter + 1
  512.         end
  513.     end
  514.     end
  515.  
  516. function checkMonitor()
  517.     if(peripheral.wrap(monitorside))then
  518.         monitor = peripheral.wrap(monitorside)
  519.         return true
  520.     end
  521. end
  522. function loadMonIcons()
  523.     local FileList = fs.list("/void/monicons")
  524.     for _, file in ipairs(FileList) do
  525.         if fs.isDir("/void/monicons/" .. file) == false then
  526.             name = string.gsub(file, "_", " ")
  527.             makeMonIcon(name)
  528.         end
  529.     end
  530. end
  531.  
  532. function drawMonIcon(id, x, y)
  533.     filename = string.gsub(monicons[id]['name'], " ", "_")
  534.     icon = paintutils.loadImage("/void/monicons/" .. filename)
  535.     for f = 1, #icon do
  536.         for g = 1, #icon[f] do
  537.             if(icon[f][g] > 0)then
  538.                 monitor.setBackgroundColor(icon[f][g])
  539.                 monitor.setCursorPos(x + g - 1, y + f - 1)
  540.                 monitor.write(" ")
  541.             end
  542.         end
  543.     end
  544.     monicons[id]['x'] = x
  545.     monicons[id]['y'] = y
  546.     monitor.setBackgroundColor(colors.black)
  547. end
  548.  
  549. function drawMonIcons()
  550.     x = 1
  551.     y = 1
  552.     w, h = monitor.getSize()
  553.     monitor.clear()
  554.     if table.getn(monicons) == nil then else
  555.         monitor.setBackgroundColor(colors.black)
  556.         for id, data in pairs(monicons) do
  557.             drawMonIcon(id, x, y)
  558.             if(y + 6 < h)then
  559.                 y = y + 6
  560.             else
  561.                 x = x + 8
  562.                 y = 1
  563.             end
  564.         end
  565.         monitor.setBackgroundColor(colors.black)
  566.     end
  567. end
  568.  
  569. function addMonIcon()
  570.     if not checkMonitor() then
  571.         detectMonitors()
  572.         monitorside = textBox("Monitor disconnected!", 1, "Enter new id or leave empty!")
  573.         while not checkMonitor() and monitorside ~= "" do
  574.             monitorside = textBox("Monitor disconnected!", 1, "Enter new id or leave empty!")
  575.         end
  576.         if(monitorside == "")then
  577.             fs.delete("/void/monitorside")
  578.             return false
  579.         else
  580.             file = fs.open("/void/monitorside", "w")
  581.             file.writeLine(monitorside)
  582.             file.close()
  583.         end
  584.     end
  585.     drawScreen()
  586.     name = textBox("Enter icon name:")
  587.     filename = string.gsub(name, " ", "_")
  588.     icon = makeMonIcon(name)
  589.     cleanScreen()
  590.     print("Please draw an icon at top left corner!")
  591.     print("The max size is 7x5! Do not draw bigger!")
  592.     sleep(5)
  593.     shell.run("paint", "/void/monicons/" .. filename)
  594.     if(not fs.exists("/void/monicons/programs/"))then fs.makeDir("/void/monicons/programs") end
  595.     if(not fs.exists("/void/monicons/programs/" .. filename))then
  596.         file = assert(io.open("/void/monicons/programs/" .. filename, "w"))
  597.         file:write("")
  598.         file:close()
  599.     end
  600.     cleanScreen()
  601.     print("Now you can edit your program!")
  602.     sleep(2)
  603.     shell.run("edit", "/void/monicons/programs/" .. filename)
  604.     drawMonIcons()
  605. end
  606.  
  607. function makeMonIcon(name)
  608.     monicons[moniconid] = {}
  609.     monicons[moniconid]["name"] = name
  610.     moniconid = moniconid + 1
  611.     return moniconid - 1
  612. end
  613.  
  614. function checkMonIcons(xPos, yPos, nostart)
  615.     if table.getn(monicons) then
  616.         for id, data in pairs(monicons) do
  617.             if(xPos >= data['x'] and xPos <= data['x'] + 6)then
  618.                 if(yPos >= data['y'] and yPos <= data['y'] + 4)then
  619.                     if nostart == nil then
  620.                         clickMonIcon(id)
  621.                     end
  622.                     return id
  623.                 end
  624.             end
  625.         end
  626.     end
  627.     return false
  628. end
  629.  
  630. function clickMonIcon(id)
  631.     shell.run("/void/monicons/programs/" .. string.gsub(monicons[id]['name'], " ", "_"))
  632. end
  633.  
  634. function monIconClickID()
  635.     event, variable, xPos, yPos = os.pullEvent("monitor_touch")
  636.     return checkMonIcons(xPos, yPos)
  637. end
  638.  
  639. function editMonIcon(id)
  640.     filename = string.gsub(monicons[id]['name'], " ", "_")
  641.     shell.run("paint", "/void/monicons/" .. filename)
  642.     drawMonIcons()
  643. end
  644.  
  645. function editMonIconProgram(id)
  646.     filename = string.gsub(monicons[id]['name'], " ", "_")
  647.     shell.run("edit", "/void/monicons/programs/" .. filename)
  648. end
  649.  
  650. function deleteMonIcon(id)
  651.     oldfilename = string.gsub(monicons[id]['name'], " ", "_")
  652.     fs.delete("/void/monicons/programs/" .. oldfilename)
  653.     fs.delete("/void/monicons/" .. oldfilename)
  654.     monicons[id] = nil
  655.     drawMonIcons()
  656. end
  657.  
  658. function editMonIconW()
  659.     message("Click the icon you want to edit!", "Click nowhere to cancel")
  660.     click = monIconClickID()
  661.     if(click)then
  662.         editMonIcon(click)
  663.     end
  664. end
  665.  
  666. function editMonIconProgramW()
  667.     message("Click the icon you want to edit!", "Click nowhere to cancel")
  668.     click = monIconClickID()
  669.     if(click)then
  670.         editMonIconProgram(click)
  671.     end
  672. end
  673.  
  674. function deleteMonIconW()
  675.     message("Click the icon you want to delete!", "Click nowhere to cancel")
  676.     click = monIconClickID()
  677.     if(click)then
  678.         deleteMonIcon(click)
  679.     end
  680. end
  681.  
  682. -----------
  683. -- Icons --
  684. -----------
  685.  
  686. function addIcon()
  687.     name = textBox("Enter icon name:")
  688.     filename = string.gsub(name, " ", "_")
  689.     icon = makeIcon(name)
  690.     cleanScreen()
  691.     print("Please draw an icon at top left corner!")
  692.     print("The max size is 6x5! Do not draw bigger!")
  693.     sleep(5)
  694.     shell.run("paint", "/void/icons/" .. filename)
  695.     if(not fs.exists("/void/icons/programs/"))then fs.makeDir("/void/icons/programs") end
  696.     if(not fs.exists("/void/icons/programs/" .. filename))then
  697.         file = assert(io.open("/void/icons/programs/" .. filename, "w"))
  698.         file:write("")
  699.         file:close()
  700.     end
  701.     cleanScreen()
  702.     print("Now you can edit your program!")
  703.     sleep(2)
  704.     shell.run("edit", "/void/icons/programs/" .. filename)
  705. end
  706.  
  707. function makeIcon(name)
  708.     icons[iconid] = {}
  709.     icons[iconid]["name"] = name
  710.     iconid = iconid + 1
  711.     return iconid - 1
  712. end
  713.  
  714. function loadIcons()
  715.     local FileList = fs.list("/void/icons")
  716.     for _, file in ipairs(FileList) do
  717.         if fs.isDir("/void/icons/" .. file) == false then
  718.             name = string.gsub(file, "_", " ")
  719.             makeIcon(name)
  720.         end
  721.     end
  722. end
  723.  
  724. function drawIcon(id, x, y)
  725.     if x > 1 then x = x * 5 + 2 else x = 2 end
  726.     if y == 1 then y = 3
  727.     elseif y == 2 then y = 11 end
  728.     filename = string.gsub(icons[id]['name'], " ", "_")
  729.     icon = paintutils.loadImage("/void/icons/" .. filename)
  730.     paintutils.drawImage(icon, x, y)
  731.     icons[id]['x'] = x
  732.     icons[id]['y'] = y
  733.     term.setBackgroundColor(colors.black)
  734.     if(string.len(icons[id]['name']) > 6)then
  735.         term.setCursorPos(x, y + 5)
  736.         term.write(string.sub(icons[id]['name'], 1, 6))
  737.         term.setCursorPos(x, y + 6)
  738.         term.write(string.sub(icons[id]['name'], 7))
  739.     else
  740.         term.setCursorPos(x, y + 6)
  741.         term.write(icons[id]['name'])
  742.     end
  743. end
  744.  
  745. function drawIcons()
  746.     x = 1
  747.     y = 1
  748.     if table.getn(icons) == nil then else
  749.         term.setBackgroundColor(colors.black)
  750.         for id, data in pairs(icons) do
  751.             drawIcon(id, x, y)
  752.             if y == 2 then
  753.                 y = 1
  754.                 x = x + 1
  755.             else
  756.                 y = y + 1
  757.             end
  758.         end
  759.         term.setBackgroundColor(colors.black)
  760.     end
  761. end
  762.  
  763. function checkIcons(left, xPos, yPos)
  764.     if(table.getn(icons))then
  765.         for id, data in pairs(icons) do
  766.             if xPos >= data['x'] and xPos <= data['x'] + 5 then
  767.                 if yPos >= data['y'] and yPos <= data['y'] + 6 then
  768.                     if(left)then
  769.                         clickIcon(id)
  770.                     else
  771.                         rClickReset()
  772.                         rClick['active'] = true
  773.                         rClick['id'] = id
  774.                         rClick['x'] = xPos
  775.                         rClick['y'] = yPos
  776.                         rAddItem("Edit icon", editIcon)
  777.                         rAddItem("Edit name", renameIcon)
  778.                         rAddItem("Edit program", editIconProgram)
  779.                         rAddItem("Delete", deleteIcon)
  780.                     end
  781.                     return true
  782.                 end
  783.             end
  784.         end
  785.     end
  786.     return false
  787. end
  788.  
  789. function editIcon(id)
  790.     if(id == nil)then id = rClick['id'] end
  791.     filename = string.gsub(icons[id]['name'], " ", "_")
  792.     shell.run("paint", "/void/icons/" .. filename)
  793. end
  794.  
  795. function renameIcon(id)
  796.     if(id == nil)then id = rClick['id'] end
  797.     name = textBox("Enter icon name:")
  798.     oldfilename = string.gsub(icons[id]['name'], " ", "_")
  799.     filename = string.gsub(name, " ", "_")
  800.     fs.move("/void/icons/programs/" .. oldfilename, "/void/icons/programs/" .. filename)
  801.     fs.move("/void/icons/" .. oldfilename, "/void/icons/" .. filename)
  802.     icons[id]['name'] = name
  803. end
  804.  
  805. function editIconProgram(id)
  806.     if(id == nil)then id = rClick['id'] end
  807.     filename = string.gsub(icons[id]['name'], " ", "_")
  808.     shell.run("edit", "/void/icons/programs/" .. filename)
  809. end
  810.  
  811. function deleteIcon(id)
  812.     if(id == nil)then id = rClick['id'] end
  813.     oldfilename = string.gsub(icons[id]['name'], " ", "_")
  814.     fs.delete("/void/icons/programs/" .. oldfilename)
  815.     fs.delete("/void/icons/" .. oldfilename)
  816.     icons[id] = nil
  817. end
  818.  
  819. function clickIcon(id)
  820.     if(id == nil)then id = rClick['id'] end
  821.     shell.run("/void/icons/programs/" .. string.gsub(icons[id]['name'], " ", "_"))
  822. end
  823.  
  824. ----------------
  825. --  Menu API  --
  826. ----------------
  827.  
  828. function drawMenus()
  829.     currentMenuPos = 1
  830.     w, h = term.getSize()
  831.     term.setBackgroundColor(colors.gray)
  832.     for k = 1, w do
  833.         term.setCursorPos(k, 1)
  834.         term.write(" ")
  835.     end
  836.     for id, data in pairs(menus) do
  837.         drawMenu(id)
  838.     end
  839.     currentMenuPos = 1
  840. end
  841.  
  842. function checkMenus(posx, posy)
  843.     currentMenuPos = 1
  844.     for id, data in pairs(menus) do
  845.         if(selectedMenu == id)then
  846.             if(posx >= currentMenuPos and posx <= currentMenuPos + data['width'])then
  847.                 if(posy >= 1 and posy <= 1 + data['menuitems'])then
  848.                     if(posy > 1)then
  849.                         menuitempos = 1
  850.                         for mid, mdata in pairs(menui) do
  851.                             if(mdata['menuid'] == id)then
  852.                                 if(posy - 1 == menuitempos)then
  853.                                     clickMenuItem(mid)
  854.                                     return true
  855.                                 end
  856.                                 menuitempos = menuitempos + 1
  857.                             end
  858.                         end
  859.                     else
  860.                         clickMenu(id)
  861.                         return true
  862.                     end
  863.                 end
  864.             end
  865.         else
  866.             if(posx >= currentMenuPos and posx <= currentMenuPos + data['width'])then
  867.                 if(posy == 1)then
  868.                     clickMenu(id)
  869.                     return true
  870.                 end
  871.             end
  872.         end
  873.         currentMenuPos = data['width'] + currentMenuPos + 2
  874.     end
  875.     return false
  876. end
  877.  
  878. function clickMenu(ID)
  879.     currentMenuPos = 1
  880.     if(selectedMenu == ID)then
  881.         selectedMenu = -1
  882.     else
  883.         selectedMenu = ID
  884.     end
  885.     drawScreen()
  886. end
  887.  
  888. function clickMenuItem(ID)
  889.     currentMenuPos = 1
  890.     mi = menui[ID]
  891.     flashMenuItem(ID)
  892.     mi['func'](ID)
  893. end
  894.  
  895. function flashMenuItem(ID)
  896.     flashMenuItemID = ID
  897.     drawMenus()
  898.     sleep(0.2)
  899.     flashMenuItemID = -1
  900.     drawMenus()
  901. end
  902.  
  903. function drawMenu(ID)
  904.     menu = menus[ID]
  905.     if(selectedMenu == ID)then
  906.         term.setBackgroundColor(colors.red)
  907.     else
  908.         term.setBackgroundColor(colors.cyan)
  909.     end
  910.     term.setTextColor(colors.orange)
  911.     term.setCursorPos(currentMenuPos, 1)
  912.     term.write(menu['text'])
  913.     if(menu['width'] >= string.len(menu['text']))then
  914.         for k = currentMenuPos + string.len(menu['text']), currentMenuPos + string.len(menu['text']) + menu['width'] - string.len(menu['text']) do
  915.             term.setCursorPos(k, 1)
  916.             term.write(" ")
  917.         end
  918.     end
  919.     if(selectedMenu == ID)then
  920.         menuitempos = 1
  921.         for mid, mdata in pairs(menui) do
  922.             if(mdata['menuid'] == ID)then
  923.                 term.setCursorPos(currentMenuPos, 1 + menuitempos)
  924.                 term.setTextColor(colors.black)
  925.                 term.setBackgroundColor(colors.white)
  926.                 if(flashMenuItemID == mid)then
  927.                     term.setBackgroundColor(colors.red)
  928.                 end
  929.                 term.write(mdata['text'])
  930.                 if(menu['width'] >= string.len(mdata['text']))then
  931.                     for k = currentMenuPos + string.len(mdata['text']), currentMenuPos + string.len(mdata['text']) + menu['width'] - string.len(mdata['text']) do
  932.                         term.setCursorPos(k, 1 + menuitempos)
  933.                         term.write(" ")
  934.                     end
  935.                 end
  936.                 menuitempos = menuitempos + 1
  937.             end
  938.         end
  939.     end
  940.     currentMenuPos = currentMenuPos + menu['width'] + 2
  941.     term.setBackgroundColor(colors.black)
  942.     term.setTextColor(colors.white)
  943. end
  944.  
  945. function addMenu(text)
  946.     menus[menuID] = {}
  947.     menus[menuID]['width']          = string.len(text)
  948.     menus[menuID]['text']           = text
  949.     menus[menuID]['menuitems']      = 0
  950.     menuID = menuID + 1
  951.     return menuID - 1
  952. end
  953.  
  954. function addMenuItem(text, menuid, func)
  955.     if(menus[menuid] ~= nil)then
  956.         menui[menuiID] = {}
  957.         menui[menuiID]['text']          = text
  958.         menui[menuiID]['menuid']        = menuid
  959.         menui[menuiID]['func']          = func
  960.         menuiID = menuiID + 1
  961.         if(menus[menuid]['width'] < string.len(text))then
  962.             menus[menuid]['width'] = string.len(text) - 1
  963.         end
  964.         menus[menuid]['menuitems'] = menus[menuid]['menuitems'] + 1
  965.         return menuiID - 1
  966.     end
  967. end
  968.  
  969. -------------------
  970. -- Loops'n'Stuff --
  971. -------------------
  972.  
  973. function loop()
  974.     drawScreen()
  975.         if(not checkMonitor())then
  976.             detectMonitors()
  977.             while not checkMonitor() and monitorside ~= "" do
  978.                 monitorside = textBox("Monitor disconnected!", 1, "Enter new id or leave empty!")
  979.             end
  980.             if(monitorside == "")then
  981.                 fs.delete("/void/monitorside")
  982.             else
  983.                 file = fs.open("/void/monitorside", "w")
  984.                 file.writeLine(monitorside)
  985.                 file.close()
  986.             end
  987.             drawScreen()
  988.     end
  989.     if(checkMonitor())then
  990.         drawMonIcons()
  991.     end
  992.     while true do
  993.         if(deleteWindowid > 0)then
  994.             deleteWindow(deleteWindowid)
  995.             deleteWindowid = 0
  996.             drawScreen()
  997.         end
  998.         event, variable, xPos, yPos = os.pullEvent()
  999.         w, h = term.getSize()
  1000.         if event == "mouse_click" then
  1001.             if variable == 1 then
  1002.                 if(not rClickCheck(xPos, yPos))then
  1003.                     rClickReset()
  1004.                     check = 0
  1005.                     if(checkTaskBar(xPos, yPos) == false)then
  1006.                         if(checkMenus(xPos, yPos) == false)then
  1007.                             check = checkWindows(xPos, yPos)
  1008.                         end
  1009.                     end
  1010.                     if(check > 0)then
  1011.                         pressed = check
  1012.                         grabx = xPos - windows[pressed]['xPos']
  1013.                     else
  1014.                         pressed = 0
  1015.                     end
  1016.                     if(os.clock() - lastclick >= .05 and os.clock() - lastclick <= 0.5)then
  1017.                         checkIcons(true, xPos, yPos)
  1018.                     end
  1019.                     lastclick = os.clock()
  1020.                 end
  1021.                 drawScreen()
  1022.             else
  1023.                 if(not checkIcons(false, xPos, yPos))then
  1024.                     checkDestktop(xPos, yPos)
  1025.                 end
  1026.                 rClickDraw()
  1027.             end
  1028.         end
  1029.         if event == "monitor_touch" then
  1030.             if variable == monitorside then
  1031.                 checkMonIcons(xPos, yPos)
  1032.             end
  1033.         end
  1034.         if event == "mouse_drag" then
  1035.             if(pressed > 0)then
  1036.                 windows[pressed]['xPos'] = xPos - grabx
  1037.                 if(yPos < h and yPos > 1)then windows[pressed]['yPos'] = yPos end
  1038.                 drawScreen()
  1039.             end
  1040.         end
  1041.         if event == "alarm" then
  1042.             drawClock()
  1043.             if(os.time() == 0)then
  1044.                 drawScreen()
  1045.             end
  1046.         end
  1047.     end
  1048. end
  1049.  
  1050. function drawClock()
  1051.     w, h = term.getSize()
  1052.     term.setTextColor(colors.cyan)
  1053.     term.setBackgroundColor(colors.orange)
  1054.     tims = textutils.formatTime(os.time(), true)
  1055.     negative = string.len(tims .. "") - 1
  1056.     term.setCursorPos(w - negative, h)
  1057.     term.write(tims)
  1058.     term.setBackgroundColor(colors.black)
  1059.     term.setTextColor(colors.white)
  1060. end
  1061.  
  1062. function clocky()
  1063.     while true do
  1064.         time = os.time()
  1065.         if(time < 23.99) then
  1066.             os.setAlarm(time + 0.01)
  1067.         else
  1068.             os.setAlarm(0)
  1069.         end
  1070.         sleep(0.1)
  1071.     end
  1072. end
  1073.  
  1074. function run()
  1075.     term.clear()
  1076.     term.setCursorPos(1,1)
  1077.     print("Checking for updates...")
  1078.     if(checkUpdate())then
  1079.         print("An update has been detected!")
  1080.         print("Would you like to update? (Yes/No)")
  1081.         write("> ")
  1082.         update = read()
  1083.     end
  1084.     if(update == "Yes" or update == "yes" or update == "Y" or update == "y")then
  1085.         file = assert(io.open("/startup", "w"))
  1086.         file:write("shell.run('install')")
  1087.         file:close()
  1088.         os.reboot()
  1089.     else
  1090.         loadIcons()
  1091.         loadMonIcons()
  1092.         parallel.waitForAny(clocky, loop)
  1093.     end
  1094. end
  1095.  
  1096. function cleanScreen()
  1097.     term.setCursorPos(1, 1)
  1098.     term.setBackgroundColor(colors.black)
  1099.     term.setTextColor(colors.white)
  1100.     term.clear()
  1101. end
  1102.  
  1103. ----------------------
  1104. -- Custom Functions --
  1105. ----------------------
  1106.  
  1107. function quit()
  1108.     cleanScreen()
  1109.     print("Restart the computer to go back! (CTRL + R)")
  1110.     error()
  1111. end
  1112.  
  1113. function lua()
  1114.     cleanScreen()
  1115.     shell.run("lua")
  1116.     term.setCursorBlink(false)
  1117. end
  1118.  
  1119. function edit()
  1120.     file = textBox("Enter file name:")
  1121.     cleanScreen()
  1122.     shell.run("edit", file)
  1123. end
  1124.  
  1125. function paint()
  1126.     file = textBox("Enter file name:")
  1127.     cleanScreen()
  1128.     shell.run("paint", file)
  1129. end
  1130.  
  1131. function worm()
  1132.     cleanScreen()
  1133.     shell.run("worm")
  1134. end
  1135.  
  1136. function adventure()
  1137.     cleanScreen()
  1138.     shell.run("adventure")
  1139. end
  1140.  
  1141. function addotherwindow()
  1142.     name = textBox("Enter window name:")
  1143.     makeWindow(15, 8, name, 2, 3)
  1144.     cleanScreen()
  1145. end
  1146. function example()
  1147. end
  1148.  
  1149. ----------------
  1150. -- Population --
  1151. ----------------
  1152.  
  1153. addVoid("Exit", quit)
  1154. addVoid("Shutdown", os.shutdown)
  1155. addVoid("Restart", os.reboot)
  1156.  
  1157. programs = addMenu("Programs")
  1158. other = addMenu("X")
  1159. --sys = addMenu("System")
  1160. --monimenu = addMenu("Monicon")
  1161.  
  1162. addMenuItem("Lua", programs, lua)
  1163. addMenuItem("Edit", programs, edit)
  1164. addMenuItem("Paint", programs, paint)
  1165. addMenuItem("Worm", programs, worm)
  1166. addMenuItem("Adventure", programs, adventure)
  1167. addMenuItem("Add window", other, addotherwindow)
  1168. addMenuItem("Add icon", other, addIcon)
  1169. addMenuItem("Add monicon", other, addMonIcon)
  1170. addMenuItem("Edit monicon", other, editMonIconW)
  1171. addMenuItem("Edit program", other, editMonIconProgramW)
  1172. addMenuItem("Delete monicon", other, deleteMonIconW)
  1173. addMenuItem("Change monitor", other, changeMonitor)
  1174. addMenuItem("Error", other, example)
  1175.  
  1176. --addMenuItem("Lua", programs, lua)
  1177. --addMenuItem("Edit", programs, edit)
  1178. --addMenuItem("Paint", programs, paint)
  1179. --addMenuItem("Worm", games, worm)
  1180. --addMenuItem("Adventure", games, adventure)
  1181. --addMenuItem("Add window", sys, addotherwindow)
  1182. --addMenuItem("Add icon", sys, addIcon)
  1183. --addMenuItem("Add monicon", monimenu, addMonIcon)
  1184. --addMenuItem("Edit monicon", monimenu, editMonIcon)
  1185. --addMenuItem("Edit program", monimenu, editMonIconProgram)
  1186. --addMenuItem("Delete monicon", monimenu, deleteMonIconW)
  1187. --addMenuItem("Change monitor", monimenu, changeMonitor)
  1188. ----------------
  1189. ----- Run ------
  1190. ----------------
  1191.  
  1192. run()
Advertisement
Add Comment
Please, Sign In to add comment