Advertisement
Symmetryc

XUtils V1.1

Apr 15th, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.06 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(tScreen, x, y)
  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 text(tColors, ...)
  87.     arg[1] = tostring(arg[1])
  88.     if #arg[1]+arg[2]>51 then error("string too long", 0)
  89.     elseif #arg<3 then error("more arguments expected", 0) end
  90.     _ = {}
  91.     if arg[4]==nil then _["text"] = true end
  92.     if arg[5]==nil then _["color"] = true end
  93.     for i=4, 5 do if arg[i] then arg[i] = math.log(arg[i])/math.log(2)
  94.         for i2=11, 16 do
  95.             if arg[i]==i2-1 then arg[i] = string.sub("0123456789abcdef", i2, i2) end
  96.         end
  97.     end end
  98.     for i=1, #arg[1] do
  99.         _[1], _[2] = extract(tColors, arg[2]+i-1, arg[3])
  100.         if _["text"] then arg[4] = _[2] end
  101.         if _["color"] then arg[5] = _[1] end
  102.         insert(tColors, arg[5], arg[4], string.sub(arg[1], i, i), arg[2]+i-1, arg[3])
  103.     end
  104. end
  105. --[[
  106. Equivalent of "write()" in the XUtils API.
  107.  
  108. Arguments:
  109. xutils.text(tScreen, text, x, y, [text_color, background_color])
  110. tScreen = your initialized screen variable.
  111. text = what you are writing.
  112. x = x-coordinate of the start of the text.
  113. y = y-coordinate of text.
  114. text_color = text color of the text, nil if the text will adopt the
  115. text color that is already in the pixel(s).
  116. background_color = background color of the text, nil if the text
  117. will adopt the background color that is already in the pixel(s).
  118.  
  119. Returns:
  120. nil.
  121.  
  122. Example Usage:
  123. tScreen = xutils.init()
  124. xutils.text(tScreen, "Hello World", 1, 1, colors.red)
  125. xutils.render(tScreen)
  126.  
  127. Note:
  128. Will error if the string is too long to be portrayed within a 51 by 19
  129. pixel space, given the x and y arguments of the function.
  130. ]]
  131. function color(tColors, ...)
  132.     for i=4, 5 do if arg[i]==nil then arg[i] = arg[i-2] end end
  133.     if #arg<3 then error("more arguments expected") end
  134.     for i=2, 5 do if arg[i]<1 then error("coordinate out of bounds") end end
  135.     for i=2, 4, 2 do if arg[i]>51 then error("coordinate out of bounds") end end
  136.     for i=3, 5, 2 do if arg[i]>19 then error("coordinator out of bounds") end end
  137.     arg[1] = math.log(arg[1])/math.log(2)
  138.     for i=2, 3 do
  139.         _ = math.min(arg[i], arg[i+2])
  140.         arg[i+2] = math.max(arg[i], arg[i+2])
  141.         arg[i] = _
  142.     end
  143.     _ = {}
  144.     for i=11, 16 do
  145.         if arg[1]==i-1 then arg[1] = string.sub("0123456789abcdef", i, i) end
  146.     end
  147.     for i=arg[2], arg[4] do for i2=arg[3], arg[5] do
  148.         _[1], _[2], _[3] = extract(tColors, i, i2)
  149.         insert(tColors, arg[1], _[2], _[3], i, i2)
  150.     end end
  151. end
  152. --[[
  153. Pixel/Rectangle drawer, the main method of background color manipulation
  154. in the XUtils API.
  155.  
  156. Arguments:
  157. xutils.color(tScreen, background_color, x1, y1, [x2, y2])
  158. tScreen = your initialized screen variable.
  159. background_color = color of what you're drawing.
  160. x1 = x-coordinate of the pixel/corner of the rectangle.
  161. y1 = y-coordinate of the pixel/corner of the rectangle.
  162. x2 = x-coordinate of the opposite corner of the rectangle.
  163. y2 = y-coordinate of the opposite corner of the rectangle.
  164.  
  165. Returns:
  166. nil.
  167.  
  168. Example Usage:
  169. tScreen = xutils.init()
  170. xutils.color(tScreen, colors.red, 1, 1, 5, 5)
  171. xutils.render(tScreen)
  172.  
  173. Note:
  174. Will error if any of the x-coordinates are not within 1 to 51 and if
  175. any of the y-coordinates are not within 1 to 19.
  176. ]]
  177. function render(tColors)
  178.     if tColors==nil then error("more arguments expected") end
  179.     _ = {}
  180.     for i=1, 51 do for i2=1, 19 do
  181.         _[1], _[2], _[3] = extract(tColors, i, i2)
  182.         for i3=1, 2 do for i4=11, 16 do
  183.             if _[i3]==string.sub("0123456789abcdef", i4, i4) then _[i3] = i4-1 end
  184.         end end
  185.         term.setCursorPos(i, i2)
  186.         term.setBackgroundColor(2^_[1])
  187.         term.setTextColor(2^_[2])
  188.         write(_[3])
  189.     end end
  190. end
  191. --[[
  192. Renders the initialized screen variable onto the screen.
  193.  
  194. Arguments:
  195. xutils.render(tScreen)
  196. tScreen = your initialized screen variable.
  197.  
  198. Returns:
  199. nil.
  200.  
  201. Example Usage:
  202. tScreen = xutils.init()
  203. xutils.text(tScreen, "Rendered stuffs", 1, 1)
  204. xutils.render(tScreen)
  205. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement