Advertisement
rithrin

duel2

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