Advertisement
Symmetryc

XUtils V1.2

Apr 18th, 2013
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.96 KB | None | 0 0
  1. --XUtils API, by Symmetryc.
  2. function init()
  3.     tColors = {}
  4.     for i=1, 51 do tColors[i] = {} for i2=1, 19 do tColors[i][i2] = "00 " end end
  5.     return tColors
  6. end
  7. --[[
  8. Use this to inititialize your screen for modification.
  9.  
  10. Arguments:
  11. None.
  12.  
  13. Returns:
  14. tScreen = xutils.init()
  15. tScreen = your initialized screen variable.
  16.  
  17. Example Usage:
  18. tScreen = xutils.init()
  19. xutils.text(tScreen, "Hi", 1, 1, colors.red)
  20. xutils.render(tScreen)
  21.  
  22. Note:
  23. Unless you are experienced with handling more than one screen, make
  24. sure to use the variable that you initialized your screen with all
  25. of your xutils functions in that program.
  26. ]]
  27. function extract(tColors, ...)
  28.     for i=1, 3 do _[i] = string.sub(tColors[arg[1]][arg[2]], i, i) end
  29.     return _[1], _[2], _[3]
  30. end
  31. --[[
  32. Use this to extract and interpret data from any pixel of your initialized
  33. screen variable.
  34.  
  35. Arguments:
  36. xutils.extract(tScreen, x, y)
  37. tScreen = your initialized screen variable.
  38. x = x-coordinate of the extraction pixel.
  39. y = y-coordinate of the extraction pixel.
  40.  
  41. Returns:
  42. background_color, text_color, text = xutils.extract()
  43. background_color = background color of that pixel.
  44. text_color = text color of that pixel.
  45. text = text that is on that pixel.
  46.  
  47. Example Usage:
  48. tScreen = xutils.init()
  49. _, _, text = xutils.extract(tScreen, 1, 1)
  50. print('The text that is on pixel (1, 1) is "'..text..'"')
  51.  
  52. Note:
  53. The values of the background and text colors are in Hexadecimal and 2 is
  54. not put to the power of them.
  55. This does not actually alter the values, it just returns them.
  56. If your screen was just initialized, your text and background colors will
  57. be white and the text will be " ".
  58. ]]
  59. function insert(tColors, ...)
  60.     tColors[arg[4]][arg[5]] = arg[1]..arg[2]..arg[3]
  61. end
  62. --[[
  63. Use this to insert data into any pixel of your initialized screen variable.
  64.  
  65. Arguments:
  66. xutils.insert(tScreen, background_color, text_color, text, x, y)
  67. tScreen = your initialized screen variable.
  68. background_color = background color of the pixel.
  69. text_color = text color of your text.
  70. text = text you are putting in.
  71. x = x-coordinate of the pixel.
  72. y = y-coordinate of the pixel.
  73.  
  74. Returns:
  75. nil.
  76.  
  77. Example Usage:
  78. tScreen = xutils.init()
  79. xutils.insert(tScreen, "-", e, 6, 1, 1)
  80. xutils.render(tScreen)
  81.  
  82. Note:
  83. The values of the background and text colors must be in Hexadecimal and
  84. 2 should not be put to the power of them.
  85. ]]
  86. function unload(tColors, path)
  87.     h = fs.open(path, "w")
  88.     h.writeLine(textutils.serialize(tColors))
  89. end
  90. --[[
  91. Use this to save screen variables in files.
  92.  
  93. Arguments:
  94. xutils.unload(tScreen, path)
  95. tScreen = your initialized screen variable.
  96. path = file you want to save the screen variable to.
  97.  
  98. Returns:
  99. nil.
  100.  
  101. Example Usage:
  102. tScreen = xutils.init()
  103. xutils.unload(tScreen, "MyBlankFile")
  104. ]]
  105. function load(path)
  106.     tColors = {}
  107.     h = fs.open(path, "r")
  108.     tColors = textutils.unserialize(h.readLine())
  109.     return tColors
  110. end
  111. --[[
  112. Use this to initialize a screen variable from a file.
  113.  
  114. Arguments:
  115. xutils.load(path)
  116. path = file your screen is saved on.
  117.  
  118. Returns:
  119. tScreen = xutils.load()
  120. tScreen = your initialized screen variable.
  121.  
  122. Example Usage:
  123. tScreen = xutils.load(CatPicture)
  124. xutils.render(tScreen)
  125. ]]
  126. function text(tColors, ...)
  127.     arg[1] = tostring(arg[1])
  128.     if arg[2]>51 or arg[2]<1 or arg[3]>19 or arg[3]<1
  129.     then error("coordinate out of bounds") end
  130.     if #arg[1]+arg[2]-1>51 then error("string too long", 0)
  131.     elseif #arg<3 then error("more arguments expected", 0) end
  132.     _ = {}
  133.     if arg[4]==nil then _["text"] = true end
  134.     if arg[5]==nil then _["color"] = true end
  135.     for i=4, 5 do if arg[i] then arg[i] = math.log(arg[i])/math.log(2)
  136.         for i2=11, 16 do
  137.             if arg[i]==i2-1 then arg[i] = string.sub("0123456789abcdef", i2, i2) end
  138.         end
  139.     end end
  140.     for i=1, #arg[1] do
  141.         _[1], _[2] = extract(tColors, arg[2]+i-1, arg[3])
  142.         if _["text"] then arg[4] = _[2] end
  143.         if _["color"] then arg[5] = _[1] end
  144.         insert(tColors, arg[5], arg[4], string.sub(arg[1], i, i), arg[2]+i-1, arg[3])
  145.     end
  146. end
  147. --[[
  148. Equivalent of "write()" in the XUtils API.
  149.  
  150. Arguments:
  151. xutils.text(tScreen, text, x, y, [text_color, background_color])
  152. tScreen = your initialized screen variable.
  153. text = what you are writing.
  154. x = x-coordinate of the start of the text.
  155. y = y-coordinate of text.
  156. text_color = text color of the text, nil if the text will adopt the
  157. text color that is already in the pixel(s).
  158. background_color = background color of the text, nil if the text
  159. will adopt the background color that is already in the pixel(s).
  160.  
  161. Returns:
  162. nil.
  163.  
  164. Example Usage:
  165. tScreen = xutils.init()
  166. xutils.text(tScreen, "Hello World", 1, 1, colors.red)
  167. xutils.render(tScreen)
  168.  
  169. Note:
  170. Will error if the string is too long to be portrayed within a 51 by 19
  171. pixel space, given the x and y arguments of the function.
  172. ]]
  173. function color(tColors, ...)
  174.     for i=4, 5 do if arg[i]==nil then arg[i] = arg[i-2] end end
  175.     if #arg<3 then error("more arguments expected") end
  176.     for i=2, 5 do if arg[i]<1 then error("coordinate out of bounds") end end
  177.     for i=2, 4, 2 do if arg[i]>51 then error("coordinate out of bounds") end end
  178.     for i=3, 5, 2 do if arg[i]>19 then error("coordinator out of bounds") end end
  179.     arg[1] = math.log(arg[1])/math.log(2)
  180.     for i=2, 3 do
  181.         _ = math.min(arg[i], arg[i+2])
  182.         arg[i+2] = math.max(arg[i], arg[i+2])
  183.         arg[i] = _
  184.     end
  185.     _ = {}
  186.     for i=11, 16 do
  187.         if arg[1]==i-1 then arg[1] = string.sub("0123456789abcdef", i, i) end
  188.     end
  189.     for i=arg[2], arg[4] do for i2=arg[3], arg[5] do
  190.         _[1], _[2], _[3] = extract(tColors, i, i2)
  191.         insert(tColors, arg[1], _[2], _[3], i, i2)
  192.     end end
  193. end
  194. --[[
  195. Pixel/Rectangle drawer, the main method of background color manipulation
  196. in the XUtils API.
  197.  
  198. Arguments:
  199. xutils.color(tScreen, background_color, x1, y1, [x2, y2])
  200. tScreen = your initialized screen variable.
  201. background_color = color of what you're drawing.
  202. x1 = x-coordinate of the pixel/corner of the rectangle.
  203. y1 = y-coordinate of the pixel/corner of the rectangle.
  204. x2 = x-coordinate of the opposite corner of the rectangle.
  205. y2 = y-coordinate of the opposite corner of the rectangle.
  206.  
  207. Returns:
  208. nil.
  209.  
  210. Example Usage:
  211. tScreen = xutils.init()
  212. xutils.color(tScreen, colors.red, 1, 1, 5, 5)
  213. xutils.render(tScreen)
  214.  
  215. Note:
  216. Will error if any of the x-coordinates are not within 1 to 51 and if
  217. any of the y-coordinates are not within 1 to 19.
  218. ]]
  219. function render(tColors)
  220.     if tColors==nil then error("more arguments expected") end
  221.     _ = {}
  222.     for i=1, 51 do for i2=1, 19 do
  223.         _[1], _[2], _[3] = extract(tColors, i, i2)
  224.         for i3=1, 2 do for i4=11, 16 do
  225.             if _[i3]==string.sub("0123456789abcdef", i4, i4) then _[i3] = i4-1 end
  226.         end end
  227.         term.setCursorPos(i, i2)
  228.         term.setBackgroundColor(2^_[1])
  229.         term.setTextColor(2^_[2])
  230.         write(_[3])
  231.     end end
  232. end
  233. --[[
  234. Renders the initialized screen variable onto the screen.
  235.  
  236. Arguments:
  237. xutils.render(tScreen)
  238. tScreen = your initialized screen variable.
  239.  
  240. Returns:
  241. nil.
  242.  
  243. Example Usage:
  244. tScreen = xutils.init()
  245. xutils.text(tScreen, "Rendered stuffs", 1, 1)
  246. xutils.render(tScreen)
  247. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement