Advertisement
Vilsol

sAPI

Sep 20th, 2013
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.71 KB | None | 0 0
  1. monitors = ""
  2.  
  3. function setMonitors(monitortable)
  4.     if(type(monitortable) == "table")then
  5.         monitors = monitortable
  6.         return true
  7.     end
  8.     return false
  9. end
  10.  
  11. function checkMonitors()
  12.     if(type(monitors) == "table")then
  13.         return true
  14.     end
  15.     term.setTextColor(colors.red)
  16.     print("Error: No monitors defined!")
  17.     error()
  18. end
  19.  
  20. function printNames()
  21.     checkMonitors()
  22.     list = peripheral.getNames();
  23.     for i, d in pairs(list) do
  24.         if(peripheral.getType(d) == "monitor")then
  25.             mon = peripheral.wrap(d)
  26.             mon.clear()
  27.             mon.setCursorPos(1, 1)
  28.             mon.write(d)
  29.         end
  30.     end
  31. end
  32.  
  33. function getSize()
  34.     checkMonitors()
  35.     ySize = 0
  36.     for _, _S in pairs(monitors) do
  37.         xSize = 0
  38.         for i, d in pairs(_S) do
  39.             mon = peripheral.wrap("monitor_" .. d)
  40.             w, h = mon.getSize()
  41.             xSize = xSize + w
  42.         end
  43.         ySize = ySize + h
  44.     end
  45.     return xSize, ySize
  46. end
  47.  
  48. function setCursorPos(x, y)
  49.     checkMonitors()
  50.     yNeg = 0
  51.     for _, _S in pairs(monitors) do
  52.         xNeg = 0
  53.         for i, d in pairs(_S) do
  54.             mon = peripheral.wrap("monitor_" .. d)
  55.             w, h = mon.getSize()
  56.             mon.setCursorPos(0 - xNeg + x, 0 - yNeg + y)
  57.             xNeg = xNeg + w
  58.         end
  59.         yNeg = yNeg + h
  60.     end
  61. end
  62.  
  63. function getCursorPos()
  64.     checkMonitors()
  65.     mon = peripheral.wrap("monitor_" .. monitors[1][1])
  66.     return mon.getCursorPos()
  67. end
  68.  
  69. function writeText(text)
  70.     checkMonitors()
  71.     w, h = getSize()
  72.     for _, _S in pairs(monitors) do
  73.         for i, d in pairs(_S) do
  74.             mon = peripheral.wrap("monitor_" .. d)
  75.             mon.write(text)
  76.         end
  77.     end
  78. end
  79.  
  80. function printText(text)
  81.     checkMonitors()
  82.     width, height = getSize()
  83.     oldx, oldy = getCursorPos()
  84.     wrapped = calcWrap(text, width - oldx)
  85.     for row = 1, #wrapped do
  86.         writeText(wrapped[row])
  87.         setCursorPos(oldx, oldy+row)
  88.     end
  89. end
  90.  
  91. function slowPrintText(text, speed)
  92.     checkMonitors()
  93.     width, height = getSize()
  94.     oldx, oldy = getCursorPos()
  95.     wrapped = calcWrap(text, width - oldx)
  96.     for row = 1, #wrapped do
  97.         for hg = 1, string.len(wrapped[row]) do
  98.             writeText(string.sub(wrapped[row], hg, hg))
  99.             sleep(speed)
  100.         end
  101.         setCursorPos(oldx, oldy+row)
  102.     end
  103. end
  104.  
  105. function setBackgroundColor(color)
  106.     checkMonitors()
  107.     for _, _S in pairs(monitors) do
  108.         for i, d in pairs(_S) do
  109.             mon = peripheral.wrap("monitor_" .. d)
  110.             mon.setBackgroundColor(color)
  111.         end
  112.     end
  113. end
  114.  
  115. function setTextColor(color)
  116.     checkMonitors()
  117.     for _, _S in pairs(monitors) do
  118.         for i, d in pairs(_S) do
  119.             mon = peripheral.wrap("monitor_" .. d)
  120.             mon.setTextColor(color)
  121.         end
  122.     end
  123. end
  124.  
  125. function fillBox(x, y, width, height, character)
  126.     checkMonitors()
  127.     for xPos = 0, width-1 do
  128.         for yPos = 0, height-1 do
  129.             setCursorPos(xPos+x, yPos+y)
  130.             writeText(character)
  131.         end
  132.     end
  133. end
  134.  
  135. function clear()
  136.     checkMonitors()
  137.     for _, _S in pairs(monitors) do
  138.         for i, d in pairs(_S) do
  139.             mon = peripheral.wrap("monitor_" .. d)
  140.             mon.clear()
  141.         end
  142.     end
  143. end
  144.  
  145. function clearLine(y)
  146.     checkMonitors()
  147.     for _, _S in pairs(monitors) do
  148.         for i, d in pairs(_S) do
  149.             mon = peripheral.wrap("monitor_" .. d)
  150.             for x = 1, w do
  151.                 setCursorPos(x, y)
  152.                 writeText(" ")
  153.             end
  154.         end
  155.     end
  156. end
  157.  
  158. function calcWrap(text, width)
  159.     lines = {""}
  160.     for word, space in text:gmatch('(%S+)(%s*)') do
  161.         temp = lines[#lines] .. word .. space:gsub('\n','')
  162.         if #temp > width then
  163.             table.insert(lines, '')
  164.         end
  165.         if space:find('\n') then
  166.             lines[#lines] = lines[#lines] .. word
  167.             space = space:gsub('\n', function()
  168.                 table.insert(lines, '')
  169.                 return ''
  170.             end)
  171.         else
  172.             lines[#lines] = lines[#lines] .. word .. space
  173.         end
  174.     end
  175.     return lines
  176. end
  177.  
  178. function setTextScale(scale)
  179.     checkMonitors()
  180.     for _, _S in pairs(monitors) do
  181.         for i, d in pairs(_S) do
  182.             mon = peripheral.wrap("monitor_" .. d)
  183.             mon.setTextScale(scale)
  184.         end
  185.     end
  186. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement