Advertisement
Guest User

ME Controller Manager

a guest
Jul 5th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.00 KB | None | 0 0
  1. mode = false --true = link, false = coords
  2. scroll = 0
  3. list = {}
  4. col1 = {}
  5. col2 = {}
  6. textbox1 = ""
  7. textbox2 = ""
  8. selectedbox = 0
  9. curScreen = "main"
  10.  
  11. function saveListFile()
  12.  file = fs.open("config", "w")
  13.  file.write(textutils.serialize(list))
  14.  file.close()
  15. end
  16.  
  17. function loadListFile()
  18.  if fs.exists("config") then
  19.   file = fs.open("config", "r")
  20.   text = textutils.unserialize(file.readAll())
  21.   if text == nil then
  22.    fs.move("config", "config_backup_"..tostring(os.day()).."_"..tostring(os.time()))
  23.    saveListFile()
  24.    error("Config file corrupted! Backing up and creating new (blank) one.")
  25.   else
  26.    list = text
  27.   end
  28.   file.close()
  29.  else
  30.   saveListFile()
  31.   error("Config file missing! Creating new (blank) one.")
  32.  end
  33. end
  34.  
  35. function needSwap(text1, text2, char)
  36.  byte1 = string.byte(text1, char)
  37.  byte2 = string.byte(text2, char)
  38.  if byte1 == nil then return false end
  39.  if byte2 == nil then return true end
  40.  if byte1 == byte2 then return needSwap(text1, text2, char + 1) end
  41.  return byte1 > byte2
  42. end
  43.  
  44. function sort(type)
  45.  col1 = {}
  46.  col2 = {}
  47.  n = 1
  48.  for key,value in pairs(list) do
  49.   col1[n],col2[n] = value[1],value[2]
  50.   n = n + 1
  51.  end
  52.  n = nil
  53.  
  54.  list = {}
  55.  for i = 1, #col1 do
  56.   list[i] = {col1[i], col2[i]}
  57.  end
  58.  
  59.  if type then col1,col2 = col2,col1 end
  60.  done = false
  61.  while not done do
  62.   done = true
  63.   for i = 1, #col1 - 1 do
  64.    if needSwap(string.lower(col1[i]), string.lower(col1[i + 1]), 1) then
  65.     done = false
  66.     col1[i], col1[i + 1] = col1[i + 1], col1[i]
  67.     col2[i], col2[i + 1] = col2[i + 1], col2[i]
  68.     list[i], list[i + 1] = list[i + 1], list[i]
  69.    end
  70.   end
  71.  end
  72. end
  73.  
  74. function drawMain()
  75.  term.setBackgroundColor(colors.black)
  76.  term.clear()
  77.  term.setCursorBlink(false)
  78.  for i = 1, 18 do
  79.   term.setCursorPos(1, i)
  80.   if i % 2 == 0 then term.setBackgroundColor(colors.gray)
  81.   else term.setBackgroundColor(colors.lightGray) end
  82.   term.write("                                             ")
  83.   term.setBackgroundColor(colors.red)
  84.   term.write("Delete")
  85.  end
  86.  
  87.  term.setCursorPos(1, 19)
  88.  term.setBackgroundColor(colors.green)
  89.  term.write("Toggle sort | Add entry |                          ")
  90.  term.setCursorPos(27, 19)
  91.  term.write("Line "..tostring(scroll))
  92.  
  93.  for i = 1, 18 do
  94.   if col1[i + scroll] == nil then
  95.    break
  96.   else
  97.    term.setCursorPos(1, i)
  98.    if i % 2 == 0 then term.setBackgroundColor(colors.gray)
  99.    else term.setBackgroundColor(colors.lightGray) end
  100.    term.write(col1[i + scroll])
  101.    term.setCursorPos(25, i)
  102.    term.write(col2[i + scroll])
  103.   end
  104.  end
  105. end
  106.  
  107. function drawAdd()
  108.  term.setBackgroundColor(colors.lightBlue)
  109.  term.setCursorPos(10,10)
  110.  term.write("Coords:                 ")
  111.  term.setCursorPos(10,11)
  112.  term.write("Link:                   ")
  113.  term.setCursorPos(10, 12)
  114.  term.setBackgroundColor(colors.green)
  115.  term.write("Save")
  116.  term.setBackgroundColor(colors.lightBlue)
  117.  term.write("              ")
  118.  term.setBackgroundColor(colors.red)
  119.  term.write("Cancel")
  120.  
  121.  term.setBackgroundColor(colors.lightBlue)
  122.  term.setCursorPos(18, 10)
  123.  term.write(textbox1)
  124.  term.setCursorPos(18, 11)
  125.  term.write(textbox2)
  126.  if selectedbox == 1 then term.setCursorPos(18 + string.len(textbox1), 10)
  127.  elseif selectedbox == 2 then term.setCursorPos(18 + string.len(textbox2), 11) end
  128.  term.setCursorBlink(selectedbox ~= 0)
  129. end
  130.  
  131. function getEvent()
  132.  event,par,x,y = os.pullEvent()
  133.  
  134.  if event == "mouse_click" then
  135.   if curScreen == "main" then
  136.    if x > 45 then
  137.     list[y + scroll] = nil
  138.     saveListFile()
  139.     sort(mode)
  140.     drawMain()
  141.    elseif y == 19 and x < 12 then
  142.     mode = not mode
  143.     sort(mode)
  144.     drawMain()
  145.    elseif y == 19 and x > 14 and x < 24 then
  146.     curScreen = "add"
  147.     drawAdd()
  148.    end
  149.  
  150.   else --screen is add
  151.    if y == 10 then
  152.     selectedbox = 1
  153.     drawAdd()
  154.    elseif y == 11 then
  155.     selectedbox = 2
  156.     drawAdd()
  157.    elseif y == 12 then
  158.     if x > 9 and x < 14 then
  159.      list[#list + 1] = {textbox1, textbox2}
  160.      saveListFile()
  161.      curScreen = "main"
  162.      textbox1 = ""
  163.      textbox2 = ""
  164.      selectedbox = 0
  165.      sort(mode)
  166.      drawMain()
  167.     elseif x > 27 and x < 34 then
  168.      curScreen = "main"
  169.      textbox1 = ""
  170.      textbox2 = ""
  171.      selectedbox = 0
  172.      drawMain()
  173.     end
  174.    end
  175.   end
  176.  
  177.  elseif event == "mouse_scroll" and curScreen == "main" then
  178.   if par == -1 and scroll > 0 then
  179.    scroll = scroll - 1
  180.   elseif par == 1 and scroll < #col1 - 17 then
  181.    scroll = scroll + 1
  182.   end
  183.   drawMain()
  184.  
  185.  elseif event == "char" and curScreen == "add" then
  186.   term.setBackgroundColor(colors.lightBlue)
  187.   term.write(par)
  188.   if selectedbox == 1 then textbox1 = textbox1..par
  189.   elseif selectedbox == 2 then textbox2 = textbox2..par end
  190.  
  191.  elseif event == "key" and par == 14 and curScreen == "add" then
  192.   if selectedbox == 1 then textbox1 = string.sub(textbox1, 1, -2)
  193.   elseif selectedbox == 2 then textbox2 = string.sub(textbox2, 1, -2) end
  194.   drawAdd()
  195.  end --event type
  196. end --function getEvent
  197.  
  198. loadListFile()
  199. sort(mode)
  200. drawMain()
  201.  
  202. while true do
  203.  getEvent()
  204. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement