Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --XUtils API, by Symmetryc.
- function init()
- tColors = {}
- for i=1, 51 do tColors[i] = {} for i2=1, 19 do tColors[i][i2] = "00 " end end
- return tColors
- end
- --[[
- Use this to inititialize your screen for modification.
- Arguments:
- None.
- Returns:
- tScreen = xutils.init()
- tScreen = your initialized screen variable.
- Example Usage:
- tScreen = xutils.init()
- xutils.text(tScreen, "Hi", 1, 1, colors.red)
- xutils.render(tScreen)
- Note:
- Unless you are experienced with handling more than one screen, make
- sure to use the variable that you initialized your screen with all
- of your xutils functions in that program.
- ]]
- function extract(tColors, ...)
- for i=1, 3 do _[i] = string.sub(tColors[arg[1]][arg[2]], i, i) end
- return _[1], _[2], _[3]
- end
- --[[
- Use this to extract and interpret data from any pixel of your initialized
- screen variable.
- Arguments:
- xutils.extract(tScreen, x, y)
- tScreen = your initialized screen variable.
- x = x-coordinate of the extraction pixel.
- y = y-coordinate of the extraction pixel.
- Returns:
- background_color, text_color, text = xutils.extract()
- background_color = background color of that pixel.
- text_color = text color of that pixel.
- text = text that is on that pixel.
- Example Usage:
- tScreen = xutils.init()
- _, _, text = xutils.extract(tScreen, 1, 1)
- print('The text that is on pixel (1, 1) is "'..text..'"')
- Note:
- The values of the background and text colors are in Hexadecimal and 2 is
- not put to the power of them.
- This does not actually alter the values, it just returns them.
- If your screen was just initialized, your text and background colors will
- be white and the text will be " ".
- ]]
- function insert(tColors, ...)
- tColors[arg[4]][arg[5]] = arg[1]..arg[2]..arg[3]
- end
- --[[
- Use this to insert data into any pixel of your initialized screen variable.
- Arguments:
- xutils.insert(tScreen, background_color, text_color, text, x, y)
- tScreen = your initialized screen variable.
- background_color = background color of the pixel.
- text_color = text color of your text.
- text = text you are putting in.
- x = x-coordinate of the pixel.
- y = y-coordinate of the pixel.
- Returns:
- nil.
- Example Usage:
- tScreen = xutils.init()
- xutils.insert(tScreen, "-", e, 6, 1, 1)
- xutils.render(tScreen)
- Note:
- The values of the background and text colors must be in Hexadecimal and
- 2 should not be put to the power of them.
- ]]
- function unload(tColors, path)
- h = fs.open(path, "w")
- h.writeLine(textutils.serialize(tColors))
- end
- --[[
- Use this to save screen variables in files.
- Arguments:
- xutils.unload(tScreen, path)
- tScreen = your initialized screen variable.
- path = file you want to save the screen variable to.
- Returns:
- nil.
- Example Usage:
- tScreen = xutils.init()
- xutils.unload(tScreen, "MyBlankFile")
- ]]
- function load(path)
- tColors = {}
- h = fs.open(path, "r")
- tColors = textutils.unserialize(h.readLine())
- return tColors
- end
- --[[
- Use this to initialize a screen variable from a file.
- Arguments:
- xutils.load(path)
- path = file your screen is saved on.
- Returns:
- tScreen = xutils.load()
- tScreen = your initialized screen variable.
- Example Usage:
- tScreen = xutils.load(CatPicture)
- xutils.render(tScreen)
- ]]
- function text(tColors, ...)
- arg[1] = tostring(arg[1])
- if arg[2]>51 or arg[2]<1 or arg[3]>19 or arg[3]<1
- then error("coordinate out of bounds") end
- if #arg[1]+arg[2]-1>51 then error("string too long", 0)
- elseif #arg<3 then error("more arguments expected", 0) end
- _ = {}
- if arg[4]==nil then _["text"] = true end
- if arg[5]==nil then _["color"] = true end
- for i=4, 5 do if arg[i] then arg[i] = math.log(arg[i])/math.log(2)
- for i2=11, 16 do
- if arg[i]==i2-1 then arg[i] = string.sub("0123456789abcdef", i2, i2) end
- end
- end end
- for i=1, #arg[1] do
- _[1], _[2] = extract(tColors, arg[2]+i-1, arg[3])
- if _["text"] then arg[4] = _[2] end
- if _["color"] then arg[5] = _[1] end
- insert(tColors, arg[5], arg[4], string.sub(arg[1], i, i), arg[2]+i-1, arg[3])
- end
- end
- --[[
- Equivalent of "write()" in the XUtils API.
- Arguments:
- xutils.text(tScreen, text, x, y, [text_color, background_color])
- tScreen = your initialized screen variable.
- text = what you are writing.
- x = x-coordinate of the start of the text.
- y = y-coordinate of text.
- text_color = text color of the text, nil if the text will adopt the
- text color that is already in the pixel(s).
- background_color = background color of the text, nil if the text
- will adopt the background color that is already in the pixel(s).
- Returns:
- nil.
- Example Usage:
- tScreen = xutils.init()
- xutils.text(tScreen, "Hello World", 1, 1, colors.red)
- xutils.render(tScreen)
- Note:
- Will error if the string is too long to be portrayed within a 51 by 19
- pixel space, given the x and y arguments of the function.
- ]]
- function color(tColors, ...)
- for i=4, 5 do if arg[i]==nil then arg[i] = arg[i-2] end end
- if #arg<3 then error("more arguments expected") end
- for i=2, 5 do if arg[i]<1 then error("coordinate out of bounds") end end
- for i=2, 4, 2 do if arg[i]>51 then error("coordinate out of bounds") end end
- for i=3, 5, 2 do if arg[i]>19 then error("coordinator out of bounds") end end
- arg[1] = math.log(arg[1])/math.log(2)
- for i=2, 3 do
- _ = math.min(arg[i], arg[i+2])
- arg[i+2] = math.max(arg[i], arg[i+2])
- arg[i] = _
- end
- _ = {}
- for i=11, 16 do
- if arg[1]==i-1 then arg[1] = string.sub("0123456789abcdef", i, i) end
- end
- for i=arg[2], arg[4] do for i2=arg[3], arg[5] do
- _[1], _[2], _[3] = extract(tColors, i, i2)
- insert(tColors, arg[1], _[2], _[3], i, i2)
- end end
- end
- --[[
- Pixel/Rectangle drawer, the main method of background color manipulation
- in the XUtils API.
- Arguments:
- xutils.color(tScreen, background_color, x1, y1, [x2, y2])
- tScreen = your initialized screen variable.
- background_color = color of what you're drawing.
- x1 = x-coordinate of the pixel/corner of the rectangle.
- y1 = y-coordinate of the pixel/corner of the rectangle.
- x2 = x-coordinate of the opposite corner of the rectangle.
- y2 = y-coordinate of the opposite corner of the rectangle.
- Returns:
- nil.
- Example Usage:
- tScreen = xutils.init()
- xutils.color(tScreen, colors.red, 1, 1, 5, 5)
- xutils.render(tScreen)
- Note:
- Will error if any of the x-coordinates are not within 1 to 51 and if
- any of the y-coordinates are not within 1 to 19.
- ]]
- function render(tColors)
- if tColors==nil then error("more arguments expected") end
- _ = {}
- for i=1, 51 do for i2=1, 19 do
- _[1], _[2], _[3] = extract(tColors, i, i2)
- for i3=1, 2 do for i4=11, 16 do
- if _[i3]==string.sub("0123456789abcdef", i4, i4) then _[i3] = i4-1 end
- end end
- term.setCursorPos(i, i2)
- term.setBackgroundColor(2^_[1])
- term.setTextColor(2^_[2])
- write(_[3])
- end end
- end
- --[[
- Renders the initialized screen variable onto the screen.
- Arguments:
- xutils.render(tScreen)
- tScreen = your initialized screen variable.
- Returns:
- nil.
- Example Usage:
- tScreen = xutils.init()
- xutils.text(tScreen, "Rendered stuffs", 1, 1)
- xutils.render(tScreen)
- ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement