Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- This is a api designed to provide basic utilities for
- when you are coding and making interfaces on monitors.
- This will eventually be expanded to handle GUIs and
- interactive menus.
- ]]
- local textColor = colors.white
- local backgroundColor = colors.black
- local lastMonitor
- --MONITOR SELECTION--
- lastMon = term.current()
- function setMon(monitor)
- --Sets active terminal to the one you specify
- lastMon = term.current()
- local mon = monitor
- term.redirect(monitor)
- end
- function lastMon()
- --Sets the current to the last monitor and restores the new 'old' one to lastMon so
- --you can toggle back and forth
- setMon(lastMonitor)
- end
- --Text Manipulation--
- function center(text)
- --Writes to center text on the line, does not go to next line
- width = select(1, term.getSize())
- currentLine = select(2,term.getCursorPos())
- if #text >= width then
- term.setCursorPos(1,currentLine) --No space left, use everything
- else
- term.setCursorPos(math.floor((width-#text)/2),currentLine) --Space left is width - number of chars in the text, put the cursor halfway (round down so you don't go over)
- end
- term.write(text)
- end
- function right(text)
- --Writes text with right alignment to the terminal
- width = select(1, term.getSize())
- currentLine = select(2,term.getCursorPos())
- if #text >= width then
- term.setCursorPos(1,currentLine) --No space left, use everything
- else
- term.setCursorPos((width-text),currentLine)
- end
- term.write(text)
- end
- --PAINTING--
- function paintLine(color)
- --Paints the line you are currently on and goes to x = 1 of that line
- term.setBackgroundColor(color)
- term.clearLine()
- term.setCursorPos(1, select(2, term.getCursorPos()))
- end
- function paintScreen(color)
- --Paints the screen and sets cursor to 1,1
- term.setBackgroundColor(color)
- term.clear()
- term.setCursorPos(1,1)
- end
- function paintPBar(value, ...) --Value should be a number between .00 and 100. Numbers bigger than 1 mean you are making 100 your 'top number' for percentages
- --paintPBar(value, isDecimal?
- --Paints a progress bar displaying how far something is depending on how much room it has in the window
- oldText = textColor
- oldBack = backgroundColor
- width, height = term.getSize()
- if type(select(1, ...)) == "number" and select(2,...) <= select(1, term.getCursorPos()) then
- --This alows the user to input a custom stopping point (which comes before the end of the screen)
- --It requires the number entered to be less than or equal to the total width of the screen (for obvious reason)
- width = select(1,...)
- end
- --Converts all values to usable numbers for unified mathy stuff later in
- if value > 100 then
- error("The value you entered: "..value.." is too large!", 2)
- end
- if value > 1 then
- value = value/100
- end
- barSize = width - select(1,term.getCursorPos())
- incAmnt = (math.ceil(100/barSize)/100)
- --Now to paint the progress bar
- if term.isColor then --Just a check on if it's a colored display or not
- inc = 0 --This keeps track of how full the bar is
- --This is a fancy effect for being an indicator on the bar
- fullColor = colors.blue
- if value > .74 then
- fullColor = colors.blue
- elseif value > .39 then
- fullColor = colors.yellow
- else
- fullColor = colors.red
- end
- for i=select(1,term.getCursorPos()), width do
- inc = inc + incAmnt
- if value >= inc then
- term.setBackgroundColor(fullColor)
- else
- term.setBackgroundColor(colors.gray)
- end
- --Now that the right color is set, time to finally draw it
- term.write(" ")
- end
- else
- inc = 0 --This keeps track of how full the bar is
- incAmnt = (math.ceil(100/(barSize-2)/100))
- term.write("<")
- for i=select(1,term.getCursorPos()), width do
- inc = inc + incAmnt
- term.setBackgroundColor(colors.white)
- if value >= inc then
- term.write("|")
- else
- term.write(" ")
- end
- end
- term.write(">")
- end
- --Now I"m restoring old settings so after the draw, you can return to what you were doing
- term.setTextColor(oldText)
- term.setBackgroundColor(oldBack)
- end
- function title(titleText, textColor, backColor)
- --title(titleText, textColor, backgroundColor), default is white and blue
- if not textColor then textColor = colors.white end
- if not backColor then backColor = colors.blue end
- end
- --Overwrites--
- --I am modifying setTextColor and setBackgroundColor so that they do what they normally do, but also store the color in here when they do it for me to access
- setTextColor = term.setTextColor
- setBackgroundColor = term.setBackgroundColor
- function setColor(color, var, func)
- func(color)
- var = color
- end
- term.setTextColor = function(color) return setColor(color, textColor, setTextColor) end
- term.setBackgroundColor = function(color) return setColor(color, BackgroundColor, setBackgroundColor) end
- --I did all of that so I could add these functions to the table (because we NEED them?)
- function term.getTextColor() return textColor end
- function term.getBackgroundColor() return backgroundColor end
Advertisement
Add Comment
Please, Sign In to add comment