Advertisement
PrinzJuliano

iNote

Feb 1st, 2013
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.11 KB | None | 0 0
  1. --[[
  2.         Created and Designed by PrinzJuliano,
  3.        
  4. ]]
  5. w,h = term.getSize()
  6. running = true
  7. timeR = nil
  8. refreshRate = 0.2
  9. isDrawed = false
  10. done = false
  11. if not term.isColor() then
  12. running = false
  13. error("You need to have an advanced Computer")
  14. end
  15.  
  16. if not fs.exists("notes.dat") then
  17.  file = io.open("iNotes.dat","w")
  18.  file:write("a%%Create some notes")
  19.  file:close()
  20. end
  21. notes = {
  22.  
  23. }
  24. function drawAll()
  25.  
  26. end
  27. function refresh()
  28.     notes = {}
  29.    
  30.      file = io.open("iNotes.dat","r")
  31.      repeat
  32.      content = file:read()
  33.                
  34.                   table.insert(notes, content)
  35.                  
  36.                  until content == nil
  37.                  file:close()
  38.                  drawAll()
  39.                
  40. end
  41. function redo()
  42. end
  43. items = {
  44. --last one first
  45.         {
  46.                 name="Exit",
  47.                 func = function()
  48.                 running = false
  49.                 shell.run("clear")
  50.                 timeR = nil
  51.                 end,
  52.                 ypos = 0,
  53.                 xpos = 0, -- Changed this
  54.                 width = 0
  55.         },
  56.         {
  57.             name="Delete Notes",
  58.             func = function()
  59.                  shell.run("delete","notes.dat")
  60.                  file = io.open("iNotes.dat","w")
  61.                  file:write("a%%All notes have been deleted!")
  62.                  file:close()
  63.                  refresh()
  64.             end,
  65.             xpos = 0,
  66.             ypos = 0,
  67.             width = 0
  68.         },
  69.         {
  70.                 name = "Editor",
  71.                 func = function()          
  72.                     shell.run("edit","iNotes.dat")
  73.                     isDrawed = false
  74.                     redo()
  75.                     refresh()
  76.                 end,
  77.                 xpos = 0,
  78.                 ypos = 0,
  79.                 width = 0
  80.         }
  81.        
  82. }
  83.  
  84. function drawTask()
  85.         term.setCursorPos(1,h-1)
  86.         term.setBackgroundColor(colors.blue)
  87.         term.write(string.rep(" ",w))
  88.  
  89.         term.setCursorPos(1,h)
  90.         term.write(string.rep(" ",w))
  91.  
  92.         time = textutils.formatTime(os.time(),true)
  93.         term.setCursorPos(w-string.len(time),h)
  94.         term.write(time)
  95.  
  96.         term.setCursorPos(1,h-1)
  97.         term.write("+-+")
  98.         term.setCursorPos(1,h)
  99.         term.write("+-+")
  100.         term.setBackgroundColor(colors.black)
  101. end
  102. function onDrawNotes()
  103.  term.setCursorPos(1,1)
  104.  for i=1,table.getn(notes) do
  105.   if notes[i]:sub(1,3) == "a%%" then
  106.   fail = string.sub(notes[i],4,string.len(notes[i]))
  107.     print(fail)
  108.   else
  109.     print(i..". "..notes[i])
  110.   end
  111.   if i == h-4 then
  112.    print("...")
  113.    break;
  114.   end
  115.  end
  116. end
  117. function onDrawMenu()
  118.         term.setCursorPos(1,h-2-table.getn(items))
  119.         term.setBackgroundColor(colors.white)
  120.         term.setTextColor(colors.black)
  121.         large = 0
  122.         for i=1,table.getn(items) do
  123.                 if string.len(items[i].name) > large then
  124.                         large = string.len(items[i].name)
  125.                 end
  126.         end
  127.         for i=1,table.getn(items) do
  128.             items[i].width = large;
  129.         end
  130.         for i=1,table.getn(items) do
  131.                 term.setCursorPos(1,h-1-i)
  132.                 term.write(string.rep(" ",large))
  133.                 term.setCursorPos(1,h-1-i)
  134.                 term.write(items[i].name)
  135.                 items[i].xpos = 1
  136.                 items[i].ypos = h-1-i
  137.         end
  138.         isDrawed = true
  139.         term.setBackgroundColor(colors.black)
  140.         term.setTextColor(colors.white)
  141. end
  142.  
  143. function updateMouse(button,xpos,ypos)
  144.         if button == 1 then
  145.                 if xpos == 1 and isDrawed == false or xpos == 2 and isDrawed == false or xpos == 3 and isDrawed == false then
  146.                         if ypos == h-1 or ypos == h then
  147.                                 onDrawMenu()
  148.                         end
  149.                 elseif isDrawed then
  150.                         done = false
  151.                         for j=1, #items do
  152.                                 if xpos >= items[j].xpos and xpos <= items[j].width and ypos == items[j].ypos then -- this is the way to do it :D/>
  153.                                         term.clear()
  154.                                         items[j].func()
  155.                                         isDrawed = false
  156.                                         done = true
  157.                                         break
  158.                                 end
  159.                         end
  160.                         if done == false then
  161.                                 isDrawed = false
  162.                                 drawAll()
  163.                         end
  164.                 end
  165.         end
  166. end
  167.  
  168. function drawAll()
  169.         shell.run("clear")
  170.         drawTask()
  171.         onDrawNotes()
  172.         if isDrawed == true then
  173.           onDrawMenu()
  174.         end
  175.         term.setCursorPos(1,1)
  176.        
  177. end
  178.  
  179. function updateAll()
  180.         e,p1,p2,p3 = os.pullEvent()
  181.         if e == "timer" and p1 == timeR then
  182.                 drawTask()
  183.                 refresh()
  184.                 timeR = os.startTimer(refreshRate)
  185.         elseif e == "mouse_click" then
  186.                 updateMouse(p1,p2,p3)
  187.         end
  188. end
  189.  
  190.  
  191. function mainLoop()
  192. while running do
  193.         drawAll()
  194.         updateAll()
  195. end
  196. end
  197. function redo()
  198.     timeR = os.startTimer(refreshRate)
  199.     running = true
  200.     mainLoop()
  201. end
  202. timeR = os.startTimer(refreshRate)
  203. refresh()
  204. mainLoop()
  205. shell.run("clear")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement