Advertisement
Zekrommaster110

[LUA | CC 1.4.7] Note Tool v1.2.3

Nov 10th, 2016 (edited)
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.32 KB | None | 0 0
  1. -- NOTE TOOL V.1.3.1
  2. -- FOR MINECRAFT COMPUTER CRAFT
  3. -- (C) zekro 2016
  4.  
  5. -- PASTEBIN: https://pastebin.com/reVGU1Kw
  6.  
  7.  
  8. ------------------------------------------------------
  9. ---------------  C U S T O M  V A R S  ---------------
  10. ------------------------------------------------------
  11. local noteFileName = "notes"
  12. local colorTitle = colors.red
  13. local colorTitleBackground = colors.gray
  14. local colorSelectLine = { background = colors.pink, text = colors.gray, controls = colors.red }
  15. local colorPageButtons = colors.pink
  16. local colorAddButton = colors.red
  17. ------------------------------------------------------
  18. -- For a color table use this:
  19. -- https://tweaked.cc/module/colors.html
  20. ------------------------------------------------------
  21.  
  22.  
  23. local notes = {}
  24. local selected = nil
  25.  
  26. -- test if a monitor is "pluged in"
  27. local function testForMonitor()
  28.     if not peripheral.find("monitor") then
  29.         print("Please start this program on a monitor!")
  30.         print("\nUse 'monitor <side> <program name>' or put this in your startup:")
  31.         print("\nshell.run(''monitor <side> <program name>'')")
  32.         error()
  33.     end
  34. end
  35.  
  36. -- get monitor size: width = monSize().w ; height = monSize().h
  37. local function monSize()
  38.     for _, v in ipairs(rs.getSides()) do
  39.         if peripheral.isPresent(v) == true then
  40.             local monitor = peripheral.wrap(v)
  41.             local w, h = monitor.getSize()
  42.             return { w = w, h = h }
  43.         end
  44.     end
  45. end
  46.  
  47. -- clear screen
  48. local function cls()
  49.     term.setBackgroundColor(colors.black)
  50.     term.clear()
  51.     term.setCursorPos(1, 1)
  52. end
  53.  
  54. -- load notes file or if it not exists, create a file with default values
  55. local function loadTabFile(filename)
  56.     local file = fs.open(filename, "r")
  57.     local t = nil
  58.     if file ~= nil then
  59.         t = textutils.unserialize(file.readAll())
  60.         file.close()
  61.     else
  62.         local defaultTable = {
  63.             "NOTE TOOL v1.2",
  64.             "(c) 2016 by zekro",
  65.             "Click add to add note",
  66.             "Click on note to remove",
  67.         }
  68.  
  69.         file = fs.open(filename, "w")
  70.         file.write(textutils.serialize(defaultTable))
  71.         file.close()
  72.         t = defaultTable
  73.     end
  74.     return t
  75. end
  76.  
  77. -- save the table from memory
  78. local function saveTabFile(filename, input)
  79.     local file = fs.open(filename, "w")
  80.     file.write(textutils.serialize(input))
  81.     file.close()
  82. end
  83.  
  84. local function printEmptyLine(w)
  85.     local line = ""
  86.     for _ = 1, w do
  87.         line = line .. " "
  88.     end
  89.     write(line)
  90. end
  91.  
  92. -- render main gui
  93. local function renderGui(page)
  94.     local size = monSize()
  95.     local maxItems = size.h - 2
  96.     local maxPage = math.floor(#notes / maxItems) + 1
  97.  
  98.     cls()
  99.  
  100.     term.setBackgroundColor(colorTitleBackground)
  101.     term.setCursorPos(1, 1)
  102.     printEmptyLine(size.w)
  103.     term.setBackgroundColor(colorTitle)
  104.     local title = " NOTES - Page " .. page .. "/" .. maxPage .. " "
  105.     term.setCursorPos(size.w / 2 - #title / 2, 1)
  106.     print(title)
  107.  
  108.     term.setBackgroundColor(colors.black)
  109.  
  110.     for i = (page - 1) * maxItems + 1, math.min(page * maxItems, #notes) do
  111.         if selected == i then
  112.             term.setBackgroundColor(colorSelectLine.background)
  113.             term.setTextColor(colorSelectLine.text)
  114.             printEmptyLine(size.w)
  115.             term.setCursorPos(1, i + 1)
  116.             print(i, "-", notes[i])
  117.             term.setCursorPos(size.w - 6, i + 1)
  118.             term.setBackgroundColor(colorSelectLine.controls)
  119.             print(" ^ v X ")
  120.             term.setBackgroundColor(colors.black)
  121.             term.setTextColor(colors.white)
  122.         else
  123.             print(i, "-", notes[i])
  124.         end
  125.     end
  126.  
  127.     term.setCursorPos(1, size.h)
  128.     term.setBackgroundColor(colorTitleBackground)
  129.     printEmptyLine(size.w)
  130.  
  131.     term.setBackgroundColor(colorAddButton)
  132.     term.setCursorPos(size.w / 2 - 3, size.h)
  133.     write("[ ADD ]")
  134.  
  135.     if page < maxPage then
  136.         term.setBackgroundColor(colorPageButtons)
  137.         term.setCursorPos(size.w - 4, size.h)
  138.         write("[ > ]")
  139.     end
  140.  
  141.     if page > 1 then
  142.         term.setBackgroundColor(colorPageButtons)
  143.         term.setCursorPos(1, size.h)
  144.         write("[ < ]")
  145.     end
  146. end
  147.  
  148.  
  149. --------------------------------------------------------
  150. ---------------  M A I N  P R O G R A M  ---------------
  151. --------------------------------------------------------
  152.  
  153. testForMonitor()
  154.  
  155. notes = loadTabFile(noteFileName)
  156. local pageNumb = 1
  157.  
  158. while true do
  159.     renderGui(pageNumb)
  160.     saveTabFile(noteFileName, notes)
  161.  
  162.     local event, _, x, y = os.pullEvent()
  163.     local size = monSize()
  164.  
  165.     if event ~= "monitor_touch" then
  166.         goto continue
  167.     end
  168.  
  169.     -- TOUCH EVENT: ADD
  170.     if (x >= (size.w / 2 - 3)) and (x <= (size.w / 2 + 3)) and (y == size.h) then
  171.         cls()
  172.         print("Add note:")
  173.  
  174.         local input = io.read()
  175.         if #input == 0 then
  176.             goto continue
  177.         end
  178.  
  179.         if (string.len(input) <= size.w - 4) then
  180.             table.insert(notes, input)
  181.         else
  182.             print("\n Too much chars. Please only use ", (size.w - 4), " chars or change display width.")
  183.             _ = io.read()
  184.         end
  185.  
  186.         goto continue
  187.     end
  188.  
  189.     -- TOUCH EVENT: PAGE UP
  190.     if (x >= ((size.w - 4)) and (x <= size.w)) and (y == size.h) then
  191.         pageNumb = pageNumb + 1
  192.         goto continue
  193.     end
  194.  
  195.     -- TOUCH EVENT: PAGE DOWN
  196.     if (x >= 0) and (x <= 4) and (y == size.h) then
  197.         if pageNumb >= 2 then
  198.             pageNumb = pageNumb - 1
  199.         end
  200.         goto continue
  201.     end
  202.  
  203.     if y > 1 and y < size.h then
  204.         local i = y - 1
  205.         if selected == i then
  206.             if x == size.w - 1 then
  207.                 table.remove(notes, i)
  208.                 selected = nil
  209.             elseif x == size.w - 3 then
  210.                 if i < #notes then
  211.                     notes[i], notes[i + 1] = notes[i + 1], notes[i]
  212.                     selected = selected + 1
  213.                 end
  214.             elseif x == size.w - 5 then
  215.                 if i > 1 then
  216.                     notes[i], notes[i - 1] = notes[i - 1], notes[i]
  217.                     selected = selected - 1
  218.                 end
  219.             else
  220.                 selected = nil
  221.             end
  222.         else
  223.             selected = i
  224.         end
  225.     end
  226.  
  227.     ::continue::
  228. end
  229.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement