ReIative

framebuffer from lyqyd git

Nov 19th, 2015
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.81 KB | None | 0 0
  1. --[[
  2. The MIT License (MIT)
  3.  
  4. Copyright (c) 2013 Lyqyd
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. --]]
  24.  
  25. function new(_sizeX, _sizeY, _color, _xOffset, _yOffset)
  26. local redirect = {buffer = {text = {}, textColor = {}, backColor = {}, cursorX = 1, cursorY = 1, cursorBlink = false, curTextColor = "0", curBackColor = "f", sizeX = _sizeX or 51, sizeY = _sizeY or 19, color = _color, xOffset = _xOffset or 0, yOffset = _yOffset or 0}}
  27. local function doWrite(text, textColor, backColor)
  28. local pos = redirect.buffer.cursorX
  29. if redirect.buffer.cursorY > redirect.buffer.sizeY or redirect.buffer.cursorY < 1 then
  30. redirect.buffer.cursorX = pos + #text
  31. return
  32. end
  33. local writeText, writeTC, writeBC
  34. if pos + #text <= 1 then
  35. --skip entirely.
  36. redirect.buffer.cursorX = pos + #text
  37. return
  38. elseif pos < 1 then
  39. --adjust text to fit on screen starting at one.
  40. local len = math.abs(redirect.buffer.cursorX) + 2
  41. writeText = string.sub(text, len)
  42. writeTC = string.sub(textColor, len)
  43. writeBC = string.sub(backColor, len)
  44. redirect.buffer.cursorX = 1
  45. elseif pos > redirect.buffer.sizeX then
  46. --if we're off the edge to the right, skip entirely.
  47. redirect.buffer.cursorX = pos + #text
  48. return
  49. else
  50. writeText = text
  51. writeTC = textColor
  52. writeBC = backColor
  53. end
  54. local lineText = redirect.buffer.text[redirect.buffer.cursorY]
  55. local lineColor = redirect.buffer.textColor[redirect.buffer.cursorY]
  56. local lineBack = redirect.buffer.backColor[redirect.buffer.cursorY]
  57. local preStop = redirect.buffer.cursorX - 1
  58. local preStart = math.min(1, preStop)
  59. local postStart = redirect.buffer.cursorX + string.len(writeText)
  60. local postStop = redirect.buffer.sizeX
  61. redirect.buffer.text[redirect.buffer.cursorY] = string.sub(lineText, preStart, preStop)..writeText..string.sub(lineText, postStart, postStop)
  62. redirect.buffer.textColor[redirect.buffer.cursorY] = string.sub(lineColor, preStart, preStop)..writeTC..string.sub(lineColor, postStart, postStop)
  63. redirect.buffer.backColor[redirect.buffer.cursorY] = string.sub(lineBack, preStart, preStop)..writeBC..string.sub(lineBack, postStart, postStop)
  64. redirect.buffer.cursorX = pos + string.len(text)
  65. end
  66. redirect.write = function(text)
  67. local text = tostring(text)
  68. doWrite(text, string.rep(redirect.buffer.curTextColor, #text), string.rep(redirect.buffer.curBackColor, #text))
  69. end
  70. redirect.blit = function(text, textColor, backColor)
  71. if type(text) ~= "string" or type(textColor) ~= "string" or type(backColor) ~= "string" then
  72. error("Expected string, string, string", 2)
  73. end
  74. if #textColor ~= #text or #backColor ~= #text then
  75. error("Arguments must be the same length", 2)
  76. end
  77. doWrite(text, textColor, backColor)
  78. end
  79. redirect.clear = function()
  80. for i=1, redirect.buffer.sizeY do
  81. redirect.buffer.text[i] = string.rep(" ", redirect.buffer.sizeX)
  82. redirect.buffer.textColor[i] = string.rep(redirect.buffer.curTextColor, redirect.buffer.sizeX)
  83. redirect.buffer.backColor[i] = string.rep(redirect.buffer.curBackColor, redirect.buffer.sizeX)
  84. end
  85. end
  86. redirect.clearLine = function()
  87. redirect.buffer.text[redirect.buffer.cursorY] = string.rep(" ", redirect.buffer.sizeX)
  88. redirect.buffer.textColor[redirect.buffer.cursorY] = string.rep(redirect.buffer.curTextColor, redirect.buffer.sizeX)
  89. redirect.buffer.backColor[redirect.buffer.cursorY] = string.rep(redirect.buffer.curBackColor, redirect.buffer.sizeX)
  90. end
  91. redirect.getCursorPos = function()
  92. return redirect.buffer.cursorX, redirect.buffer.cursorY
  93. end
  94. redirect.setCursorPos = function(x, y)
  95. redirect.buffer.cursorX = math.floor(tonumber(x) or redirect.buffer.cursorX)
  96. redirect.buffer.cursorY = math.floor(tonumber(y) or redirect.buffer.cursorY)
  97. end
  98. redirect.setCursorBlink = function(b)
  99. redirect.buffer.cursorBlink = b
  100. end
  101. redirect.getSize = function()
  102. return redirect.buffer.sizeX, redirect.buffer.sizeY
  103. end
  104. redirect.scroll = function(n)
  105. n = tonumber(n) or 1
  106. if n > 0 then
  107. for i = 1, redirect.buffer.sizeY - n do
  108. if redirect.buffer.text[i + n] then
  109. redirect.buffer.text[i] = redirect.buffer.text[i + n]
  110. redirect.buffer.textColor[i] = redirect.buffer.textColor[i + n]
  111. redirect.buffer.backColor[i] = redirect.buffer.backColor[i + n]
  112. end
  113. end
  114. for i = redirect.buffer.sizeY, redirect.buffer.sizeY - n + 1, -1 do
  115. redirect.buffer.text[i] = string.rep(" ", redirect.buffer.sizeX)
  116. redirect.buffer.textColor[i] = string.rep(redirect.buffer.curTextColor, redirect.buffer.sizeX)
  117. redirect.buffer.backColor[i] = string.rep(redirect.buffer.curBackColor, redirect.buffer.sizeX)
  118. end
  119. elseif n < 0 then
  120. for i = redirect.buffer.sizeY, math.abs(n) + 1, -1 do
  121. if redirect.buffer.text[i + n] then
  122. redirect.buffer.text[i] = redirect.buffer.text[i + n]
  123. redirect.buffer.textColor[i] = redirect.buffer.textColor[i + n]
  124. redirect.buffer.backColor[i] = redirect.buffer.backColor[i + n]
  125. end
  126. end
  127. for i = 1, math.abs(n) do
  128. redirect.buffer.text[i] = string.rep(" ", redirect.buffer.sizeX)
  129. redirect.buffer.textColor[i] = string.rep(redirect.buffer.curTextColor, redirect.buffer.sizeX)
  130. redirect.buffer.backColor[i] = string.rep(redirect.buffer.curBackColor, redirect.buffer.sizeX)
  131. end
  132. end
  133. end
  134. redirect.getTextColor = function()
  135. return 2 ^ tonumber(redirect.buffer.curTextColor, 16)
  136. end
  137. redirect.getTextColour = redirect.getTextColor
  138. redirect.setTextColor = function(clr)
  139. if clr and clr <= 32768 and clr >= 1 then
  140. if redirect.buffer.color then
  141. redirect.buffer.curTextColor = string.format("%x", math.floor(math.log(clr) / math.log(2)))
  142. elseif clr == 1 or clr == 32768 then
  143. redirect.buffer.curTextColor = string.format("%x", math.floor(math.log(clr) / math.log(2)))
  144. else
  145. return nil, "Colour not supported"
  146. end
  147. end
  148. end
  149. redirect.setTextColour = redirect.setTextColor
  150. redirect.getBackgroundColor = function()
  151. return 2 ^ tonumber(redirect.buffer.curBackColor, 16)
  152. end
  153. redirect.getBackgroundColour = redirect.getBackgroundColor
  154. redirect.setBackgroundColor = function(clr)
  155. if clr and clr <= 32768 and clr >= 1 then
  156. if redirect.buffer.color then
  157. redirect.buffer.curBackColor = string.format("%x", math.floor(math.log(clr) / math.log(2)))
  158. elseif clr == 32768 or clr == 1 then
  159. redirect.buffer.curBackColor = string.format("%x", math.floor(math.log(clr) / math.log(2)))
  160. else
  161. return nil, "Colour not supported"
  162. end
  163. end
  164. end
  165. redirect.setBackgroundColour = redirect.setBackgroundColor
  166. redirect.isColor = function()
  167. return redirect.buffer.color == true
  168. end
  169. redirect.isColour = redirect.isColor
  170. redirect.render = function(inputBuffer)
  171. for i = 1, redirect.buffer.sizeY do
  172. redirect.buffer.text[i] = inputBuffer.text[i]
  173. redirect.buffer.textColor[i] = inputBuffer.textColor[i]
  174. redirect.buffer.backColor[i] = inputBuffer.backColor[i]
  175. end
  176. end
  177. redirect.setBounds = function(x_min, y_min, x_max, y_max)
  178. redirect.buffer.minX = x_min
  179. redirect.buffer.maxX = x_max
  180. redirect.buffer.minY = y_min
  181. redirect.buffer.maxY = y_max
  182. end
  183. redirect.setBounds(1, 1, redirect.buffer.sizeX, redirect.buffer.sizeY)
  184. redirect.clear()
  185. return redirect
  186. end
  187.  
  188. function draw(buffer, current)
  189. for i = buffer.minY, buffer.maxY do
  190. term.setCursorPos(buffer.minX + buffer.xOffset, i + buffer.yOffset)
  191. if (current and (buffer.text[i] ~= current.text[i] or buffer.textColor[i] ~= current.textColor[i] or buffer.backColor[i] ~= current.backColor[i])) or not current then
  192. if term.blit then
  193. term.blit(buffer.text[i], buffer.textColor[i], buffer.backColor[i])
  194. else
  195. local lineEnd = false
  196. local offset = buffer.minX
  197. while not lineEnd do
  198. local limit = buffer.maxX - offset + 1
  199. local textColorString = string.match(string.sub(buffer.textColor[i], offset), string.sub(buffer.textColor[i], offset, offset).."*")
  200. local backColorString = string.match(string.sub(buffer.backColor[i], offset), string.sub(buffer.backColor[i], offset, offset).."*")
  201. term.setTextColor(2 ^ tonumber(string.sub(textColorString, 1, 1), 16))
  202. term.setBackgroundColor(2 ^ tonumber(string.sub(backColorString, 1, 1), 16))
  203. term.write(string.sub(buffer.text[i], offset, offset + math.min(#textColorString, #backColorString, limit) - 1))
  204. offset = offset + math.min(#textColorString, #backColorString, limit)
  205. if offset > buffer.maxX then lineEnd = true end
  206. end
  207. end
  208. if current then
  209. current.text[i] = buffer.text[i]
  210. current.textColor[i] = buffer.textColor[i]
  211. current.backColor[i] = buffer.backColor[i]
  212. end
  213. end
  214. end
  215. term.setCursorPos(buffer.cursorX + buffer.xOffset, buffer.cursorY + buffer.yOffset)
  216. term.setTextColor(2 ^ tonumber(buffer.curTextColor, 16))
  217. term.setBackgroundColor(2 ^ tonumber(buffer.curBackColor, 16))
  218. term.setCursorBlink(buffer.cursorBlink)
  219. return current
  220. end
Add Comment
Please, Sign In to add comment