Thunder7102

Monitor API

Jul 11th, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.00 KB | None | 0 0
  1. --[[
  2.     This is a api designed to provide basic utilities for
  3.     when you are coding and making interfaces on monitors.
  4.     This will eventually be expanded to handle GUIs and
  5.     interactive menus.
  6. ]]
  7. local textColor = colors.white
  8. local backgroundColor = colors.black
  9. local lastMonitor
  10.  
  11. --MONITOR SELECTION--
  12. lastMon = term.current()
  13. function setMon(monitor)
  14.     --Sets active terminal to the one you specify
  15.     lastMon = term.current()
  16.     local mon = monitor
  17.     term.redirect(monitor)
  18. end
  19.  
  20. function lastMon()
  21.   --Sets the current to the last monitor and restores the new 'old' one to lastMon so
  22.   --you can toggle back and forth
  23.   setMon(lastMonitor)
  24. end
  25.  
  26. --Text Manipulation--
  27. function center(text)
  28.     --Writes to center text on the line, does not go to next line
  29.     width = select(1, term.getSize())
  30.     currentLine = select(2,term.getCursorPos())
  31.     if #text >= width then
  32.         term.setCursorPos(1,currentLine) --No space left, use everything
  33.     else
  34.         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)
  35.     end
  36.    
  37.     term.write(text)
  38. end
  39.  
  40. function right(text)
  41.     --Writes text with right alignment to the terminal
  42.     width = select(1, term.getSize())
  43.     currentLine = select(2,term.getCursorPos())
  44.     if #text >= width then
  45.         term.setCursorPos(1,currentLine) --No space left, use everything
  46.     else
  47.         term.setCursorPos((width-text),currentLine)  
  48.     end
  49.    
  50.     term.write(text)
  51. end
  52.  
  53. --PAINTING--
  54. function paintLine(color)
  55.     --Paints the line you are currently on and goes to x = 1 of that line
  56.     term.setBackgroundColor(color)
  57.     term.clearLine()
  58.     term.setCursorPos(1, select(2, term.getCursorPos()))
  59. end
  60.  
  61. function paintScreen(color)
  62.     --Paints the screen and sets cursor to 1,1
  63.     term.setBackgroundColor(color)
  64.     term.clear()
  65.     term.setCursorPos(1,1)
  66. end
  67.  
  68. 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
  69.     --paintPBar(value, isDecimal?
  70.     --Paints a progress bar displaying how far something is depending on how much room it has in the window
  71.     oldText = textColor
  72.     oldBack = backgroundColor
  73.    
  74.     width, height = term.getSize()
  75.     if type(select(1, ...)) == "number" and select(2,...) <= select(1, term.getCursorPos()) then
  76.         --This alows the user to input a custom stopping point (which comes before the end of the screen)
  77.         --It requires the number entered to be less than or equal to the total width of the screen (for obvious reason)
  78.         width = select(1,...)
  79.     end
  80.    
  81.     --Converts all values to usable numbers for unified mathy stuff later in
  82.     if value > 100 then
  83.         error("The value you entered: "..value.." is too large!", 2)
  84.     end
  85.    
  86.     if value > 1 then
  87.         value = value/100
  88.     end
  89.    
  90.     barSize = width - select(1,term.getCursorPos())
  91.     incAmnt = (math.ceil(100/barSize)/100)
  92.    
  93.     --Now to paint the progress bar
  94.     if term.isColor then --Just a check on if it's a colored display or not
  95.         inc = 0 --This keeps track of how full the bar is
  96.        
  97.         --This is a fancy effect for being an indicator on the bar
  98.         fullColor = colors.blue
  99.         if value > .74 then
  100.             fullColor = colors.blue
  101.         elseif value > .39 then
  102.             fullColor = colors.yellow
  103.         else
  104.             fullColor = colors.red
  105.         end
  106.        
  107.         for i=select(1,term.getCursorPos()), width do
  108.             inc = inc + incAmnt
  109.             if value >= inc then
  110.                 term.setBackgroundColor(fullColor)
  111.             else
  112.                 term.setBackgroundColor(colors.gray)
  113.             end
  114.            
  115.             --Now that the right color is set, time to finally draw it
  116.             term.write(" ")
  117.         end
  118.     else
  119.         inc = 0 --This keeps track of how full the bar is
  120.         incAmnt = (math.ceil(100/(barSize-2)/100))
  121.         term.write("<")
  122.         for i=select(1,term.getCursorPos()), width do
  123.             inc = inc + incAmnt
  124.             term.setBackgroundColor(colors.white)
  125.             if value >= inc then
  126.                 term.write("|")
  127.             else
  128.                 term.write(" ")
  129.             end
  130.         end
  131.         term.write(">")
  132.     end
  133.    
  134.     --Now I"m restoring old settings so after the draw, you can return to what you were doing
  135.     term.setTextColor(oldText)
  136.     term.setBackgroundColor(oldBack)
  137. end
  138.  
  139. function title(titleText, textColor, backColor)
  140.     --title(titleText, textColor, backgroundColor), default is white and blue
  141.     if not textColor then textColor = colors.white end
  142.     if not backColor then backColor = colors.blue end
  143. end
  144.  
  145. --Overwrites--
  146. --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
  147. setTextColor = term.setTextColor
  148. setBackgroundColor = term.setBackgroundColor
  149.  
  150. function setColor(color, var, func)
  151.     func(color)
  152.     var = color
  153. end
  154. term.setTextColor = function(color) return setColor(color, textColor, setTextColor) end
  155. term.setBackgroundColor = function(color) return setColor(color, BackgroundColor, setBackgroundColor) end
  156.  
  157. --I did all of that so I could add these functions to the table (because we NEED them?)
  158. function term.getTextColor() return textColor end
  159. function term.getBackgroundColor() return backgroundColor end
Advertisement
Add Comment
Please, Sign In to add comment