Advertisement
Megaddd

Megaddd - Touchscreen Tasklist - Computercraft

Apr 18th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.90 KB | None | 0 0
  1. --Feel free to change the values in this column
  2. local monside= "right"
  3. local header = "Task List 1.5"
  4. local bgcol  = colors.blue
  5. local bgcol2 = colors.lightBlue
  6. local fgcol  = colors.yellow
  7. local fgcol2 = colors.orange
  8. local selcol = colors.green
  9. local button1_text = "Delete"
  10.  
  11.  
  12. local FILENAME = "tasklist.dat"
  13. local tasklist
  14. local selection = -1
  15.  
  16. function save()
  17.     f = fs.open(FILENAME,"w")
  18.     f.writeLine(textutils.serialize(tasklist))
  19.     f.close()
  20. end
  21.  
  22. function load()
  23.     if fs.exists(FILENAME) then
  24.         f = fs.open(FILENAME,"r")
  25.         local readline = f.readLine()
  26.         tasklist = textutils.unserialize(readline)
  27.         f.close()
  28.     else
  29.         f = fs.open(FILENAME,"w")
  30.         f.writeLine("{[1]=\'Make a new task\'}")
  31.         f.close()
  32.         tasklist = {"Make a new task"}
  33.     end
  34. end
  35.  
  36. function printLine(x)
  37.     s = peripheral.wrap(monside)
  38.     for i=1,x do
  39.         s.write(" ")
  40.     end
  41. end
  42.  
  43. function drawButton(text, x, y, colT, colB)
  44.     s = peripheral.wrap(monside)
  45.     for i=0, 2 do
  46.         s.setCursorPos(x, y+i)
  47.         s.setBackgroundColor(colB)
  48.         printLine(string.len(text)+2)
  49.     end
  50.     s.setCursorPos(x+1, y+1)
  51.     s.setTextColor(colT)
  52.     s.write(text)
  53. end
  54.  
  55. function display()
  56.     if tasklistlen == nil then tasklistlen = 0 end
  57.     s = peripheral.wrap(monside)
  58.     local monx, mony = s.getSize()
  59.     s.setTextColor(fgcol2)
  60.     s.setBackgroundColor(bgcol)
  61.     s.clear()
  62.     s.setCursorPos(monx/2-string.len(header)/2,1)
  63.     s.write(header)
  64.    
  65.     if tasklist == nil then
  66.     else
  67.         for i=1, #tasklist do
  68.             if i+2 >= mony-2 then break end
  69.             s.setCursorPos(1,i+2)
  70.             if selection == i then
  71.                 s.setBackgroundColor(selcol)
  72.             else
  73.                 if i % 2 == 1 then
  74.                     s.setBackgroundColor(bgcol)
  75.                 else
  76.                     s.setBackgroundColor(bgcol2)
  77.                 end
  78.             end
  79.             printLine(monx)
  80.             s.setCursorPos(1,i+2)
  81.             s.setTextColor(fgcol2)
  82.             s.write(i..". ")
  83.             s.setTextColor(fgcol)
  84.             s.write(tasklist[i])
  85.         end
  86.        
  87.         if selection == -1 then
  88.         else
  89.             drawButton(button1_text, 1, mony-2, colors.white, colors.red)
  90.         end
  91.     end
  92.     save()
  93. end
  94.  
  95. function termedit()
  96.     s = peripheral.wrap(monside)
  97.     local monx, mony = s.getSize()
  98.     while true do
  99.         term.setBackgroundColor(bgcol)
  100.         term.clear()
  101.         term.setCursorPos(1,1)
  102.         term.setTextColor(fgcol)
  103.         write("Begin typing to add a new task..")
  104.         local event, char = os.pullEvent("char")
  105.         term.setCursorPos(1,3)
  106.         write("New Task: "..char)
  107.         local newtask = char..io.read()
  108.         if string.len(newtask) > monx-3 then
  109.             term.setCursorPos(1,5)
  110.             term.setTextColor(colors.red)
  111.             write("Text too long, limit: "..monx-3)
  112.         else
  113.             local notdone = true
  114.             local i = 1
  115.             if tasklist == nil then
  116.                 tasklist = {newtask}
  117.                 term.setCursorPos(1,5)
  118.                 write("New task added!")
  119.             else
  120.                 while notdone do
  121.                     if tasklist[i] == nil then
  122.                         tasklist[i] = newtask
  123.                         notdone = false
  124.                         term.setCursorPos(1,5)
  125.                         write("New task added!")
  126.                     end
  127.                     i = i + 1
  128.                 end
  129.             end
  130.         end
  131.         display()
  132.         sleep(0.75)
  133.     end
  134. end
  135.  
  136. function touchscreen()
  137.     s = peripheral.wrap(monside)
  138.     local monx, mony = s.getSize()
  139.     while true do
  140.         local etype, side, tx, ty = os.pullEvent("monitor_touch")
  141.         if #tasklist ~= nil then
  142.             if 2 < ty and ty <= mony-2 then
  143.                 if ty-2 > #tasklist then
  144.                     selection = -1
  145.                 else
  146.                     selection = ty-2
  147.                 end
  148.             end
  149.             if ty > mony-3 then
  150.                 local stringlen = string.len(button1_text)
  151.                 if tx <= stringlen + 2 then
  152.                     local si = selection
  153.                     table.remove(tasklist,si)
  154.                     --[[
  155.                     tasklist[si] = nil
  156.                     j = 0
  157.                     for a = 1, #tasklist do
  158.                         if tasklist[a] == nil then j = j+1 end
  159.                         tasklist[a] = tasklist[a+j]
  160.                     end
  161.                     --]]
  162.                 else
  163.                     selection = -1
  164.                 end
  165.             end
  166.             display()
  167.         end
  168.     end
  169. end
  170.  
  171. function main()
  172.     print("programInit..")
  173.     load()
  174.     display()
  175.     print("Attempting to boot parallel..")
  176.     parallel.waitForAny(termedit, touchscreen)
  177.     term.setBackgroundColor(colors.black)
  178.     term.setTextColor(colors.white)
  179.     term.clear()
  180.     print("ProgramExit")
  181. end
  182.  
  183. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement