masterdisasterHD

Todo 2.0

Oct 12th, 2013
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local barTextColour = colors.lime
  2. local barColour = colors.white
  3.  
  4. local primaryColour = colors.white
  5. local secondaryColour = colors.lime
  6.  
  7. local monitors = {
  8.  
  9. }
  10.  
  11. local printers = {
  12.  
  13. }
  14.  
  15. function smartPrint(text, pageTitle, printerSide)
  16.  
  17. if not peripheral.isPresent(printerSide) or peripheral.getType(printerSide)~="printer" then
  18.  error("No printer found on "..printerSide,0)
  19. elseif printerSide == "" then
  20.  error("Usage: smartPrint(text, label, printerSide)", 0)
  21. end
  22.  
  23. local A = peripheral.wrap(printerSide)
  24. if A.getPaperLevel() == 0 then
  25.  error("Out of paper!", 0)
  26. elseif A.getInkLevel() == 0 then
  27.  error("Out of ink!",0)
  28. end
  29.  
  30. A.newPage()
  31. A.setPageTitle(pageTitle)
  32. local w,h = A.getPageSize()
  33. local k = 1
  34.  
  35. for i=1,h do
  36.  A.setCursorPos(1,i)
  37.  for j=1, w do
  38.   A.write(text:sub(k, k))
  39.   k = k + 1
  40.  end
  41.  os.queueEvent("")
  42.  os.pullEvent()
  43. end
  44.  
  45. k = nil
  46. j,i = nil
  47.  
  48. A.endPage()
  49. return true, A.getPaperLevel(), A.getInkLevel()
  50. end
  51.  
  52. for i,v in ipairs(monitors) do
  53.  if not peripheral.isPresent(v) or peripheral.getType(v) ~= "monitor" then
  54.   table.remove(monitors, i)
  55.  end
  56. end
  57.  
  58. for i,v in ipairs(printers) do
  59.  if not peripheral.isPresent(v) or peripheral.getType(v) ~= "printer" then
  60.   table.remove(printers, i)
  61.  end
  62. end
  63.  
  64. local taskFile = ".tasks"
  65.  
  66. if not fs.exists(taskFile) then
  67.  A = fs.open(taskFile, "w")
  68.  A.write(textutils.serialize({ "Made by HD -click me!" }))
  69.  A.close()
  70. end
  71.  
  72. local A = 0
  73.  
  74. function remoteTasks()
  75.  for i,v in ipairs(rs.getSides()) do
  76.   if peripheral.isPresent(v) and peripheral.getType(v) == "modem" then
  77.    rednet.open(v)
  78.   end
  79.  end
  80.  
  81.  while true do
  82.   os.queueEvent("")
  83.   os.pullEvent()
  84.   local id, cmd = rednet.receive()
  85.   local cmd = textutils.unserialize(cmd)
  86.   if type(cmd) == "table" then
  87.    if cmd["command"] == "add" then
  88.     local A = getTasks()
  89.     table.insert(A, cmd["input"])
  90.     saveTasks(A)
  91.    elseif cmd["command"] == "del" then
  92.     local A = getTasks()
  93.     if A[tonumber(cmd["input"])] ~= nil then
  94.      table.remove(A, tonumber(cmd["input"]))
  95.      saveTasks(A)
  96.     end
  97.    end
  98.   end
  99.  end
  100. end
  101.  
  102. local function clear()
  103.  if #monitors >= 1 then
  104.   for i,v in ipairs(monitors) do
  105.    peripheral.call(v, "setCursorPos", 1, 1)
  106.    peripheral.call(v, "setBackgroundColour", colors.black)
  107.    peripheral.call(v, "setTextColour", colors.white)
  108.    peripheral.call(v, "clear")
  109.   end
  110.  end
  111.  
  112.  term.setCursorPos(1,1)
  113.  term.setBackgroundColour(colors.black)
  114.  term.setTextColour(colors.white)
  115.  term.clear()
  116. end
  117.  
  118. function getTasks()
  119.  if not fs.exists(taskFile) then
  120.   return {}
  121.  else
  122.   local file = fs.open(taskFile, "r")
  123.   local tmp = textutils.unserialize(file.readAll())
  124.   file.close()
  125.   return tmp
  126.  end
  127. end
  128.  
  129. local function redraw(screen, page)
  130.  
  131.  local text = "HD's Todo list."
  132.  local tasks = getTasks()
  133.  local maxX, maxY = screen.getSize()
  134.  local maxY = maxY - 2
  135.  
  136.  --[[ Text ]]--
  137.  --clear()
  138.  
  139.  screen.setCursorPos(1,1)
  140.  screen.setBackgroundColour(barColour)
  141.  screen.setTextColour(barTextColour)
  142.  screen.clearLine()
  143.  screen.setCursorPos(maxX/2-#text/2, 1)
  144.  screen.write(text)
  145.  --------------
  146.  
  147.  for i=1, maxY do
  148.   if i % 2 == 0 then
  149.    screen.setBackgroundColour(primaryColour)
  150.    screen.setTextColour(secondaryColour)
  151.   elseif i % 2 == 1 then
  152.    screen.setBackgroundColour(secondaryColour)
  153.    screen.setTextColour(primaryColour)
  154.   end
  155.  
  156.   screen.setCursorPos(1, 1+i)
  157.   screen.clearLine()
  158.  
  159.   local num = i+(maxY*(page-1))
  160.  
  161.   if tasks[num] ~= nil then
  162.    screen.write(num..": "..tasks[num])
  163.   else
  164.    screen.write(num..": ")
  165.   end
  166.  end
  167.  
  168.  screen.setBackgroundColour(barColour)
  169.  screen.setTextColour(barTextColour)
  170.  screen.setCursorPos(2, maxY+2)
  171.  screen.clearLine()
  172.  screen.write("Back")
  173.  screen.setCursorPos(maxX/2-#(tostring(page))/2, maxY+2)
  174.  screen.write(tostring(page))
  175.  screen.setCursorPos(maxX-#"Next", maxY+2)
  176.  screen.write("Next")
  177.  
  178.  term.clear()
  179.  term.setCursorPos(1,1)
  180.  term.setCursorPos(2,2)
  181.  write("HD's Todo list v2.0")
  182.  term.setCursorPos(2, 6)
  183.  term.setTextColour(colors.lime)
  184.  write("Add Task")
  185.  term.setTextColour(colors.red)
  186.  term.setCursorPos(2, 8)
  187.  write("Remove Task")
  188.  term.setTextColour(colors.white)
  189. end
  190.  
  191. local function showTask(pos, page, max, mon)
  192.  local pos = pos - 1
  193.  local screen = peripheral.wrap(mon)
  194.  local num = pos+(max*(page-1))
  195.  local tasks = getTasks()
  196.  
  197.  if tasks[num] ~= nil then
  198.   clear()
  199.  
  200.   if #monitors >= 1 then
  201.    for i,v in ipairs(monitors) do
  202.      local str = tasks[num]
  203.     local sizeX, sizeY = peripheral.call(v, "getSize")
  204.     local k = 1
  205.     for i=1, sizeY-1 do
  206.      peripheral.call(v, "setCursorPos", 1, i)
  207.      for j=1, sizeX do
  208.       peripheral.call(v, "write", str:sub(k,k))
  209.       k = k + 1
  210.      end
  211.     end
  212.    
  213.     peripheral.call(v, "setCursorPos", 2, sizeY)
  214.     peripheral.call(v, "write", "Delete")
  215.     peripheral.call(v, "setCursorPos", sizeX-#"Print"-1, sizeY)
  216.     peripheral.call(v, "write", "Print")
  217.    end
  218.   end
  219.  end
  220.  
  221.  local ev, _, xPos, yPos = os.pullEvent("monitor_touch")
  222.  local sizeX, sizeY = peripheral.call(_, "getSize")
  223.  if xPos >= 2 and xPos <= #"Delete"+1 and yPos == sizeY then
  224.   local A = getTasks()
  225.   table.remove(A, num)
  226.   saveTasks(A)
  227.  elseif xPos >= sizeX-#"Print"-1 and xPos <= sizeX-1 and yPos == sizeY then
  228.   for i,v in ipairs(printers) do
  229.    smartPrint(tasks[num], "Task #"..tostring(num), v)
  230.   end
  231.  else
  232.  
  233.  end
  234. end
  235.  
  236. local function addTask()
  237.  local tasks = getTasks()
  238.  term.clear()
  239.  term.setCursorPos(2,2)
  240.  write("Task: ")
  241.  tsk = read()
  242.  table.insert(tasks, tsk)
  243.  saveTasks(tasks)
  244. end
  245.  
  246. local function delTask()
  247.  local tasks = getTasks()
  248.  term.clear()
  249.  term.setCursorPos(2,2)
  250.  write("Task number: ")
  251.  nmbr = tonumber(read())
  252.  if tasks[nmbr] ~= nil then
  253.   table.remove(tasks, nmbr)
  254.  end
  255.  
  256.  saveTasks(tasks)
  257. end
  258.  
  259. function saveTasks(tbl)
  260.  local A = fs.open(taskFile, "w")
  261.  A.write(textutils.serialize(tbl))
  262.  A.close()
  263. end
  264.  
  265. local pageNmr = 1
  266.  
  267. function iCanRun()
  268.  
  269. while true do
  270.  if monitors[1] ~= nil then
  271.   clear()
  272.   for _,v in ipairs(monitors) do
  273.    local A = peripheral.wrap(v)
  274.    redraw(A, pageNmr)
  275.    os.queueEvent("")
  276.    os.pullEvent()
  277.   end
  278.  end
  279.  --redraw(term, pageNmr)
  280.  
  281.  local maxX, maxY = term.getSize()
  282.  local ev, _, xPos, yPos = os.pullEvent()
  283.  local mon = false
  284.  
  285.  if ev == "monitor_touch" then
  286.   local mon = true
  287.   maxX, maxY = (peripheral.wrap(_)).getSize()
  288.  end
  289.  
  290.  if ev == "monitor_touch" then
  291.   if xPos >= 2 and xPos <= 5 and yPos == maxY then
  292.    if pageNmr > 1 then
  293.     pageNmr = pageNmr - 1
  294.    end
  295.   elseif xPos >= maxX-4 and xPos <= maxX-1 and yPos == maxY then
  296.    pageNmr = pageNmr + 1
  297.   elseif yPos > 1 and yPos < maxY then
  298.    showTask(yPos, pageNmr, maxX, _)
  299.   end
  300.  elseif ev == "mouse_click" then
  301.   if yPos == 6 then
  302.    addTask()
  303.   elseif yPos == 8 then
  304.    delTask()
  305.   end
  306.  end
  307.  sleep(.001)
  308. end
  309.  
  310. end
  311.  
  312. parallel.waitForAny(iCanRun, remoteTasks)
Add Comment
Please, Sign In to add comment