Advertisement
MysticT

Sync API

Sep 20th, 2012
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1. -- Sync API
  2. -- Terminal-Monitor Synchronization
  3. -- by MysticT
  4.  
  5. local tApi = {}
  6. local tTargets = {}
  7. local tMonitors = {}
  8.  
  9. local bMax = false
  10. local nWidth, nHeight = 0, 0
  11. local nCursorX, nCursorY = 1, 1
  12. local bBlink = false
  13. local bgColor = colors.black
  14. local txtColor = colors.white
  15.  
  16. local function resize()
  17.     local w, h
  18.     for target in pairs(tTargets) do
  19.         local _w, _h = target.getSize()
  20.         if bMax then
  21.             if (w == nil or h == nil) or (_w * _h) > (w * h) then
  22.                 w, h = _w, _h
  23.             end
  24.         else
  25.             if (w == nil or h == nil) or (_w * _h) < (w * h) then
  26.                 w, h = _w, _h
  27.             end
  28.         end
  29.     end
  30.     if w and h then
  31.         nWidth = w
  32.         nHeight = h
  33.     end
  34. end
  35.  
  36. local function call(func, ...)
  37.     for target in pairs(tTargets) do
  38.         target[func](...)
  39.     end
  40. end
  41.  
  42. -- Sync Functions
  43.  
  44. function tApi.getSize()
  45.     return nWidth, nHeight
  46. end
  47.  
  48. function tApi.getCursorPos()
  49.     return nCursorX, nCursorY
  50. end
  51.  
  52. function tApi.setCursorPos(x, y)
  53.     nCursorX, nCursorY = x, y
  54.     call("setCursorPos", x, y)
  55. end
  56.  
  57. function tApi.setCursorBlink(b)
  58.     bBlink = b
  59.     call("setCursorBlink", b)
  60. end
  61.  
  62. function tApi.clear()
  63.     call("clear")
  64. end
  65.  
  66. function tApi.clearLine()
  67.     call("clearLine")
  68. end
  69.  
  70. function tApi.write(text)
  71.     call("write", text)
  72.     nCursorX = nCursorX + #text
  73. end
  74.  
  75. function tApi.scroll(n)
  76.     call("scroll", n)
  77. end
  78.  
  79. function tApi.isColor()
  80.     for target in pairs(tTargets) do
  81.         if not target.isColor() then
  82.             return false
  83.         end
  84.     end
  85.     return true
  86. end
  87. tApi.isColour = tApi.isColor
  88.  
  89. function tApi.setBackgroundColor(c)
  90.     bgColor = c
  91.     call("setBackgroundColor", c)
  92. end
  93. tApi.setBackgroundColour = tApi.setBackgroundColor
  94.  
  95. function tApi.setTextColor(c)
  96.     txtColor = c
  97.     call("setTextColor", c)
  98. end
  99. tApi.setTextColour = tApi.setTextColor
  100.  
  101. -- API Functions
  102.  
  103. function addTarget(target)
  104.     tTargets[target] = true
  105.     resize()
  106.     target.setCursorPos(nCursorX, nCursorY)
  107.     target.setCursorBlink(bBlink)
  108.     target.setBackgroundColor(bgColor)
  109.     target.setTextColor(txtColor)
  110. end
  111.  
  112. function removeTarget(target)
  113.     tTargets[target] = nil
  114. end
  115.  
  116. function addMonitor(sSide)
  117.     if tMonitors[sSide] then
  118.         return true
  119.     end
  120.     if peripheral.isPresent(sSide) and peripheral.getType(sSide) == "monitor" then
  121.         local mon = peripheral.wrap(sSide)
  122.         tMonitors[sSide] = mon
  123.         addTarget(mon)
  124.         return true
  125.     end
  126.     return false
  127. end
  128.  
  129. function removeMonitor(sSide)
  130.     if tMonitors[sSide] then
  131.         removeTarget(tMonitors[sSide])
  132.         tMonitors[sSide] = nil
  133.     end
  134. end
  135.  
  136. function addMonitors()
  137.     for _,s in ipairs(rs.getSides()) do
  138.         addMonitor(s)
  139.     end
  140. end
  141.  
  142. function removeMonitors()
  143.     for _,s in ipairs(rs.getSides()) do
  144.         removeMonitor(s)
  145.     end
  146. end
  147.  
  148. function useMaxSize(b)
  149.     if b ~= bMax then
  150.         bMax = b
  151.         resize()
  152.     end
  153. end
  154.  
  155. function redirect(bAddTerm)
  156.     if bAddTerm then
  157.         addTarget(term.native or term)
  158.     end
  159.     term.redirect(tApi)
  160. end
  161.  
  162. function restore()
  163.     term.restore()
  164. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement