Advertisement
Guest User

RaccoonAPI

a guest
Jan 27th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.78 KB | None | 0 0
  1. --[[Peripheral API]]--
  2.  
  3. function getDeviceSide(deviceType)
  4.         deviceType = deviceType:lower()
  5.    
  6.         for i, side in pairs(rs.getSides()) do
  7.                 if (peripheral.isPresent(side)) then
  8.                         if (string.lower(peripheral.getType(side)) == deviceType) then
  9.                                         return side;
  10.                         end
  11.                 end
  12.         end
  13.    
  14.         return nil;
  15. end
  16.  
  17. function wrapThis(thing)
  18.         local wrapped, i = nil, 0
  19.         while wrapped == nil and i <= 100 do
  20.                 wrapped = peripheral.wrap(thing.."_"..i)
  21.                 i = i + 1
  22.         end
  23.  
  24.         if wrapped == nil then
  25.                 side = getDeviceSide(thing)
  26.                 if side ~= nil then
  27.                         return peripheral.wrap(side)
  28.                 else
  29.                         return nil
  30.                 end
  31.         else
  32.                 return wrapped
  33.         end
  34. end
  35.  
  36. function wrapMonitor()
  37.         local monitor, i = nil, 0
  38.         while monitor == nil and i <= 100 do
  39.                 monitor = peripheral.wrap("monitor_"..i)
  40.                 i = i + 1
  41.         end
  42.  
  43.         if monitor == nil then
  44.                 side = getDeviceSide("Monitor")
  45.                 if side ~= nil then
  46.                         return peripheral.wrap(side)
  47.                 else
  48.                         return nil
  49.                 end
  50.         else
  51.                 return monitor
  52.         end
  53. end
  54.  
  55. function wrapReactor()
  56.         side = getDeviceSide("BigReactors-Reactor")
  57.         if side ~= nil then
  58.                 return peripheral.wrap(side)
  59.         else
  60.                 return nil
  61.         end
  62. end
  63.  
  64. --[[Monitor API]]--
  65.  
  66. function clearMonitors(str)
  67.         local gap = 2
  68.         term.clear()
  69.         local width, height = term.getSize()
  70.        
  71.         printCentered(str, 1)
  72.  
  73.         for i=1, width do
  74.                 term.setCursorPos(i, gap)
  75.                 term.write("-")
  76.         end            
  77.        
  78.         term.setCursorPos(1, gap+1)
  79. end
  80.  
  81. function write(mon, text, colour)
  82.     local col = colour or colors.white
  83.     mon.setTextColor(col)
  84.     mon.write(text)
  85.     mon.setTextColor(colors.white) 
  86. end
  87.  
  88. function resetMon(mon)
  89.   mon.setTextColour(colors.white)
  90.   mon.setBackgroundColour(colors.black)
  91.   mon.setCursorPos(1,1)
  92.   mon.setCursorBlink(false)
  93.   mon.clear()
  94. end
  95.  
  96. --[[Time API]]--
  97.  
  98. function formatSeconds(s)
  99.         if s > 86400 then
  100.                 return math.ceil(s/86400).."d "..formatSeconds(s%86400)
  101.         elseif s > 3600 then
  102.                 return math.ceil(s/3600).."h "..formatSeconds(s%3600)
  103.         elseif s > 60 then
  104.                 return math.ceil(s/60).."m "..formatSeconds(s%60)
  105.         else
  106.                 return math.ceil(s).."s"
  107.         end
  108. end
  109.  
  110. --[[Colour API]]--
  111.  
  112. function convertToString( nColor )
  113.   for k, v in pairs( colors ) do
  114.     if nColor == v then
  115.       return k
  116.     end
  117.   end
  118.   return nil
  119. end
  120.  
  121. --[[Http API]]--
  122.  
  123. function download( sUrl, sPath )
  124.   assert( type( sUrl ) == "string", "String expected, got " .. type( sUrl ), 2)
  125.   assert( type( sPath ) == "string", "String expected, got " .. type( sPath ), 2)
  126.   assert( not fs.exists( sPath ), "Path already exists", 2)
  127.  
  128.   local response = http.get( sUrl )
  129.   if response then
  130.     local f = fs.open( sPath, "w" )
  131.     f.write( response.readAll() )
  132.     f.close()
  133.     response.close()
  134.     return true
  135.   end
  136.   return false
  137. end
  138.  
  139. --[[Number API]]--
  140.  
  141. function round(float)
  142.         if float%math.floor(float) >= 0 then
  143.                 return math.ceil(float)
  144.         else
  145.                 return math.floor(float)
  146.         end
  147. end
  148.  
  149. function roundToTen(float)
  150.         float = math.ceil(float)
  151.         if ((float % 10) - 10) >= 0 then
  152.                 return math.ceil(float + float%10)
  153.         else
  154.                 return math.floor(float - float%10)
  155.         end
  156. end
  157.  
  158. function roundThousanth(num)
  159.   local mult = 10^(0)
  160.   return math.floor(num * mult + 0.5) / mult
  161. end
  162.  
  163. function generate( nLength, nMin, nMax )
  164.   assert( type( nLength ) == "number", "Number expected, got " .. type( nLength ), 2)
  165.   assert( type( nMins ) == "number", "Number expected, got " .. type( nMin ), 2)
  166.   assert( type( nMax ) == "number", "Number expected, got " .. type( nMax ), 2)
  167.   assert( nMin < nMax, "Minimum must be less than maximum", 2)
  168.  
  169.   local n = math.random( nMin, nMax )
  170.   for i = 1, nLength do
  171.     n = n .. math.random( nMin, nMax )
  172.   end
  173.   return tonumber( n )
  174. end
  175.  
  176. function isOdd( n )
  177.   assert( type( n ) == "number", "Number expected, got " .. type( n ), 2)
  178.   return n % 2 ~= 0
  179. end
  180.  
  181. function isEven( n )
  182.   assert( type( n ) == "number", "Number expected, got " .. type( n ), 2)
  183.   return n % 2 == 0
  184. end
  185.  
  186. --[[Pastebin API]]--
  187.  
  188. function get( sCode, sFile )
  189.   assert( type( sCode ) == "string", "Number expected, got " .. type( sCode ), 2)
  190.   assert( type( sFile ) == "string", "String expected, got " .. type( sFile ), 2)
  191.   local sPath = shell.resolve( sFile )
  192.   assert( not fs.exists( sPath ), "File exists", 2)
  193.  
  194.   local tResponse = http.get( "http://pastebin.com/" .. textutils.urlEncode( sCode ) )
  195.   if tResponse then
  196.     local sResponse = tResponse.readAll()
  197.     tResponse.close()
  198.  
  199.     local handle = fs.open( sPath, "w" )
  200.     handle.write( sResponse )
  201.     handle.close()
  202.     return true
  203.   end
  204.   return false
  205. end
  206.  
  207. function put( sFile )
  208.   assert( type( sFile ) == "string", "String expected, got " .. type( sFile ), 2)
  209.  
  210.   local sPath = shell.resolve( sFile )
  211.   assert( not fs.isDir( sPath ), "Cannot upload directories", 2 )
  212.   assert( not fs.exists( sPath ), "File doesn't exist", 2 )
  213.  
  214.   local sName = fs.getName( sPath )
  215.   local handle = fs.open( sPath, "r" )
  216.   local sText = handle.readAll()
  217.   handle.close()
  218.   local key = "0ec2eb25b6166c0c27a394ae118ad829"
  219.   local response = http.post(
  220.     "http://pastebin.com/api/api_post.php",
  221.     "api_option=paste&" ..
  222.     "api_dev_key=" .. key .. "&" ..
  223.     "api_paste_format=lua&" ..
  224.     "api_paste_name=" .. textutils.urlEncode( sPath ) .. "&" ..
  225.     "api_paste_code=" .. textutils.urlEncode( sText )
  226.   )
  227.  
  228.   if response then
  229.     local sResponse = response.readAll()
  230.     local sCode = string.match( sResponse, "[^/]+$" )
  231.     response.close()
  232.     return sCode, true
  233.   end
  234.   return nil, false
  235. end
  236.  
  237. --[[Term API]]--
  238.  
  239. function clear( nx, ny, nTextColour, nBackgroundColour )
  240.   if nTextColour ~= nil then
  241.     assert( type( nTextColour ) == "number", "Number expected, got " .. type( nTextColour ), 2)
  242.     term.setTextColour( nTextColour )
  243.   end
  244.   if nBackgroundColour ~= nil then
  245.     assert( type( nBackgroundColour ) == "number", "Number expected, got " .. type( nBackgroundColour ), 2)
  246.     term.setBackgroundColour( nBackgroundColour )
  247.   end
  248.   if nx ~= nil then
  249.     assert( type( nx ) == "number", "Number expected, got " .. type( nx ), 2)
  250.   end
  251.   if ny ~= nil then
  252.     assert( type( ny ) == "number", "Number expected, got " .. type( nx ), 2)
  253.   end
  254.   term.setCursorPos( nx or currentX, ny or currentY )
  255.   oldTerm.clear()
  256.   local nMaxx,nMaxy = term.getSize()
  257.   -- Clear pixel data
  258.   for i = 1, nMaxx do
  259.     for j = 1, nMaxy do
  260.       tPixels[i.." "..j] = {
  261.         Character = " ",
  262.         TextColor = nTextColour or term.getTextColour(),
  263.         TextColour = nTextColour or term.getTextColour(),
  264.         BackgroundColor = nBackgroundColour or term.getBackgroundColour(),
  265.         BackgroundColour = nBackgroundColour or term.getBackgroundColour()
  266.       }
  267.     end
  268.   end
  269. end
  270.  
  271. function restoreNativeTerminal()
  272.         repeat
  273.                 term.restore()
  274.                 local w, h = term.getSize()
  275.         until w == 51 and h == 19
  276. end
  277.  
  278. function reset()
  279.   term.setTextColour(colors.white)
  280.   term.setBackgroundColour(colors.black)
  281.   term.setCursorPos(1,1)
  282.   term.setCursorBlink(false)
  283.   term.clear()
  284. end
  285.  
  286. --[[Text API]]--
  287.  
  288. function comma_value(amount)
  289.    local formatted = amount
  290.    local swap = false
  291.    if formatted < 0 then
  292.       formatted = formatted*-1
  293.       swap = true
  294.    end
  295.    while true do
  296.       formatted, k = string.gsub(formatted, "^(%d+)(%d%d%d)", '%1,%2')
  297.       if k == 0 then
  298.          break
  299.       end
  300.    end
  301.    if swap then
  302.      formatted = "-"..formatted
  303.    end
  304.    return formatted
  305. end
  306.  
  307. function generate( nLength, nCharSet )
  308.   assert( type( nLength ) == "number", "Number expected, got " .. type( nLength ), 2)
  309.   assert( type( nCharSet ) == "number", "Number expected, got " .. type( nCharSet ), 2)
  310.     local nCharSet = nCharSet or 128
  311.     local str = ""
  312.     for i = 1, nLength do
  313.         str = str .. math.random( 1, nCharSet ):char()
  314.     end
  315.     return str
  316. end
  317.  
  318. function printCentered(str, yPos)
  319.         local width, height = term.getSize()
  320.         term.setCursorPos(math.floor(width/2) - math.ceil(str:len()/2) , yPos)
  321.         term.clearLine()
  322.         term.write(str)
  323. end
  324.  
  325. function stringToBool(str)
  326.         if str == "false" then
  327.                 return false
  328.                 end
  329.         if str == "true" then
  330.                 return true
  331.         else
  332.                 return nil
  333.         end
  334. end
  335.  
  336. function bracket( sText, nx, ny, nTextColour, nBracketColour, nBackgroundColour )
  337.     assert( type( sText ) == "string", "String expected, got " .. type( sText ), 2)
  338.     assert( type( nx ) == "number", "Number expected, got " .. type( nx ), 2)
  339.     assert( type( ny ) == "number", "Number expected, got " .. type( ny ), 2)
  340.  
  341.   if nTextColour then
  342.     assert( type( nTextColour ) == "number", "Number/nil expected, got " .. type( nTextColour ), 2)
  343.   end
  344.   if nBracketColour then
  345.     assert( type( nBracketColour ) == "number", "Number/nil expected, got " .. type( nBracketColour ), 2)
  346.   end
  347.   if nBackgroundColour then
  348.         assert( type( nBackgroundColour ) == "number", "Number/nil expected, got " .. type( nBackgroundColour ), 2)
  349.     term.setBackgroundColour( nBackgroundColour )
  350.     end
  351.  
  352.     local xPos, yPos = term.getCursorPos()
  353.     term.setCursorPos( nx, ny )
  354.     term.setTextColour( nBracketColour or colours.white )
  355.     term.write( "[" .. string.rep(" ", #sText) .. "]" )
  356.  
  357.     term.setTextColour( nTextColour or colours.white )
  358.   term.setCursorPos( nx + 1, ny )
  359.     term.write( sText )
  360. end
  361.  
  362. function center( sText, nx, ny )
  363.     assert( type( sText ) == "string", "String expected, got " .. type( sText ), 2)
  364.     assert( type( nx ) == "number", "Number expected, got " .. type( nx ), 2)
  365.     assert( type( ny ) == "number", "Number expected, got " .. type( ny ), 2)
  366.     term.setCursorPos( (( nx - #sText ) / 2) - 1, ny )
  367.     term.write( sText )
  368. end
  369.  
  370. --[[Touchpoint API]]--
  371.  
  372. local function setupLabel(buttonLen, minY, maxY, name)
  373.         local labelTable = {}
  374.         if type(name) == "table" then
  375.                 for i = 1, #name do
  376.                         labelTable[i] = name[i]
  377.                 end
  378.                 name = name.label
  379.         elseif type(name) == "string" then
  380.                 local buttonText = string.sub(name, 1, buttonLen - 2)
  381.                 if #buttonText < #name then
  382.                         buttonText = " "..buttonText.." "
  383.                 else
  384.                         local labelLine = string.rep(" ", math.floor((buttonLen - #buttonText) / 2))..buttonText
  385.                         buttonText = labelLine..string.rep(" ", buttonLen - #labelLine)
  386.                 end
  387.                 for i = 1, maxY - minY + 1 do
  388.                         if maxY == minY or i == math.floor((maxY - minY) / 2) + 1 then
  389.                                 labelTable[i] = buttonText
  390.                         else
  391.                                 labelTable[i] = string.rep(" ", buttonLen)
  392.                         end
  393.                 end
  394.         end
  395.         return labelTable, name
  396. end
  397.  
  398. local Button = {
  399.         draw = function(self)
  400.                 local old = term.redirect(self.mon)
  401.                 term.setTextColor(colors.white)
  402.                 term.setBackgroundColor(colors.black)
  403.                 term.clear()
  404.                 for name, buttonData in pairs(self.buttonList) do
  405.                         if buttonData.active then
  406.                                 term.setBackgroundColor(buttonData.activeColor)
  407.                                 term.setTextColor(buttonData.activeText)
  408.                         else
  409.                                 term.setBackgroundColor(buttonData.inactiveColor)
  410.                                 term.setTextColor(buttonData.inactiveText)
  411.                         end
  412.                         for i = buttonData.yMin, buttonData.yMax do
  413.                                 term.setCursorPos(buttonData.xMin, i)
  414.                                 term.write(buttonData.label[i - buttonData.yMin + 1])
  415.                         end
  416.                 end
  417.                 if old then
  418.                         term.redirect(old)
  419.                 else
  420.                         term.restore()
  421.                 end
  422.         end,
  423.         add = function(self, name, func, xMin, yMin, xMax, yMax, inactiveColor, activeColor, inactiveText, activeText)
  424.                 local label, name = setupLabel(xMax - xMin + 1, yMin, yMax, name)
  425.                 if self.buttonList[name] then error("button already exists", 2) end
  426.                 local x, y = self.mon.getSize()
  427.                 if xMin < 1 or yMin < 1 or xMax > x or yMax > y then error("button out of bounds", 2) end
  428.                 self.buttonList[name] = {
  429.                         func = func,
  430.                         xMin = xMin,
  431.                         yMin = yMin,
  432.                         xMax = xMax,
  433.                         yMax = yMax,
  434.                         active = false,
  435.                         inactiveColor = inactiveColor or colors.red,
  436.                         activeColor = activeColor or colors.lime,
  437.                         inactiveText = inactiveText or colors.white,
  438.                         activeText = activeText or colors.white,
  439.                         label = label,
  440.                 }
  441.                 for i = xMin, xMax do
  442.                         for j = yMin, yMax do
  443.                                 if self.clickMap[i][j] ~= nil then
  444.                                         --undo changes
  445.                                         for k = xMin, xMax do
  446.                                                 for l = yMin, yMax do
  447.                                                         if self.clickMap[k][l] == name then
  448.                                                                 self.clickMap[k][l] = nil
  449.                                                         end
  450.                                                 end
  451.                                         end
  452.                                         self.buttonList[name] = nil
  453.                                         error("overlapping button", 2)
  454.                                 end
  455.                                 self.clickMap[i][j] = name
  456.                         end
  457.                 end
  458.         end,
  459.         remove = function(self, name)
  460.                 if self.buttonList[name] then
  461.                         local button = self.buttonList[name]
  462.                         for i = button.xMin, button.xMax do
  463.                                 for j = button.yMin, button.yMax do
  464.                                         self.clickMap[i][j] = nil
  465.                                 end
  466.                         end
  467.                         self.buttonList[name] = nil
  468.                 end
  469.         end,
  470.         run = function(self)
  471.                 while true do
  472.                         self:draw()
  473.                         local event = {self:handleEvents(os.pullEvent("monitor_touch"))}
  474.                         if event[1] == "button_click" then
  475.                                 self.buttonList[event[2]].func()
  476.                         end
  477.                 end
  478.         end,
  479.         handleEvents = function(self, ...)
  480.                 local event = {...}
  481.                 if #event == 0 then event = {os.pullEvent()} end
  482.                 if event[1] == "monitor_touch" and event[2] == self.side then
  483.                         local clicked = self.clickMap[event[3]][event[4]]
  484.                         if clicked and self.buttonList[clicked] then
  485.                                 return "button_click", clicked
  486.                         end
  487.                 end
  488.                 return unpack(event)
  489.         end,
  490.         toggleButton = function(self, name, noDraw)
  491.                 self.buttonList[name].active = not self.buttonList[name].active
  492.                 if not noDraw then self:draw() end
  493.         end,
  494.         flash = function(self, name, duration)
  495.                 self:toggleButton(name)
  496.                 sleep(tonumber(duration) or 0.15)
  497.                 self:toggleButton(name)
  498.         end,
  499.         rename = function(self, name, newName)
  500.                 self.buttonList[name].label, newName = setupLabel(self.buttonList[name].xMax - self.buttonList[name].xMin + 1, self.buttonList[name].yMin, self.buttonList[name].yMax, newName)
  501.                 if not self.buttonList[name] then error("no such button", 2) end
  502.                 if name ~= newName then
  503.                         self.buttonList[newName] = self.buttonList[name]
  504.                         self.buttonList[name] = nil
  505.                         for i = self.buttonList[newName].xMin, self.buttonList[newName].xMax do
  506.                                 for j = self.buttonList[newName].yMin, self.buttonList[newName].yMax do
  507.                                         self.clickMap[i][j] = newName
  508.                                 end
  509.                         end
  510.                 end
  511.                 self:draw()
  512.         end,
  513. }
  514.  
  515. function newTouchpoint(monSide)
  516.         local buttonInstance = {
  517.                 side = monSide,
  518.                 mon = peripheral.wrap(monSide),
  519.                 buttonList = {},
  520.                 clickMap = {},
  521.         }
  522.         local x, y = buttonInstance.mon.getSize()
  523.         for i = 1, x do
  524.                 buttonInstance.clickMap[i] = {}
  525.         end
  526.         setmetatable(buttonInstance, {__index = Button})
  527.         return buttonInstance
  528. end
  529.  
  530. --[[Button API]]--
  531.  
  532. local mon = peripheral.wrap("right")
  533. mon.setTextScale(1)
  534. mon.setTextColor(colors.white)
  535. local button={}
  536. mon.setBackgroundColor(colors.black)
  537.  
  538. function clearTable()
  539.    button = {}
  540. end
  541.  
  542. function setButton(name, buttonOn)
  543.    print(name)
  544.    print(button[name]["active"])
  545.    button[name]["active"] = buttonOn
  546.    screen()
  547. end
  548.                                              
  549. function setTable(name, func, param, xmin, xmax, ymin, ymax)
  550.    button[name] = {}
  551.    button[name]["func"] = func
  552.    button[name]["active"] = false
  553.    button[name]["param"] = param
  554.    button[name]["xmin"] = xmin
  555.    button[name]["ymin"] = ymin
  556.    button[name]["xmax"] = xmax
  557.    button[name]["ymax"] = ymax
  558. end
  559.  
  560. function funcName()
  561.    print("You clicked buttonText")
  562. end
  563.        
  564. function fillTable()
  565.    setTable("ButtonText", funcName, 5, 25, 4, 8)
  566. end    
  567.  
  568. function fill(text, color, bData)
  569.    mon.setBackgroundColor(color)
  570.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  571.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  572.    for j = bData["ymin"], bData["ymax"] do
  573.       mon.setCursorPos(bData["xmin"], j)
  574.       if j == yspot then
  575.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  576.             if k == xspot then
  577.                mon.write(text)
  578.             else
  579.                mon.write(" ")
  580.             end
  581.          end
  582.       else
  583.          for i = bData["xmin"], bData["xmax"] do
  584.             mon.write(" ")
  585.          end
  586.       end
  587.    end
  588.    mon.setBackgroundColor(colors.black)
  589. end
  590.      
  591. function screen()
  592.    local currColor
  593.    for name,data in pairs(button) do
  594.       local on = data["active"]
  595.       if on == true then currColor = colors.lime else currColor = colors.red end
  596.       fill(name, currColor, data)
  597.    end
  598. end
  599.  
  600. function toggleButton(name)
  601.    button[name]["active"] = not button[name]["active"]
  602.    screen()
  603. end    
  604.  
  605. function flash(name)
  606.    toggleButton(name)
  607.    screen()
  608.    sleep(0.15)
  609.    toggleButton(name)
  610.    screen()
  611. end
  612.                                              
  613. function checkxy(x, y)
  614.    for name, data in pairs(button) do
  615.       if y>=data["ymin"] and  y <= data["ymax"] then
  616.          if x>=data["xmin"] and x<= data["xmax"] then
  617.             if data["param"] == "" then
  618.               data["func"]()
  619.             else
  620.               data["func"](data["param"])
  621.             end
  622.             return true
  623.             --data["active"] = not data["active"]
  624.             --print(name)
  625.          end
  626.       end
  627.    end
  628.    return false
  629. end
  630.      
  631. function heading(text)
  632.    w, h = mon.getSize()
  633.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  634.    mon.write(text)
  635. end
  636.      
  637. function label(w, h, text)
  638.    mon.setCursorPos(w, h)
  639.    mon.write(text)
  640. end
  641.  
  642. function getClick()
  643.     event, side, x, y = os.pullEvent("monitor_touch")
  644.     checkxy(x, y)
  645. end
  646.  
  647. --[[Progress Bar API]]--
  648.  
  649. local monitor
  650. local hasMonitor = false
  651. local ProgressBar = {}
  652. local FillColor = colors.orange
  653. local EmptyColor = colors.blue
  654. local TextColor = colors.white
  655.  
  656. function SetPeripheral(side)
  657.   if (peripheral.isPresent(side)) then
  658.     monitor = peripheral.wrap(side)
  659.     hasMonitor = true
  660.   end
  661. end
  662.  
  663. function SetTable(name, maxVal, curVal, xmin, xmax, y)
  664.   ProgressBar[name] = {}
  665.   ProgressBar[name]["Max"] = maxVal
  666.   ProgressBar[name]["Current"] = curVal
  667.   ProgressBar[name]["XMin"] = xmin
  668.   ProgressBar[name]["XMax"] = xmax
  669.   ProgressBar[name]["YVal"] = y
  670. end
  671.  
  672. function ClearTable()
  673.   if (hasMonitor) then
  674.     ProgressBar = {}
  675.   end
  676. end
  677.  
  678. function SetFillColor(color)
  679.   if (colors.combine(color,color)) then  
  680.     FillColor = color
  681.   end
  682. end
  683.  
  684. function SetEmptyColor(color)
  685.   if (colors.combine(color,color)) then
  686.     EmptyColor = color
  687.   end
  688. end
  689.  
  690. function SetTextColor(color)
  691.   if (colors.combine(color,color)) then
  692.     TextColor = color
  693.   end
  694. end
  695.  
  696. function SetMaxValue(name, intVal)
  697.   if (ProgressBar[name]) then
  698.     ProgressBar[name]["Max"] = intVal
  699.   end
  700. end
  701.  
  702. function SetCurValue(name, intVal)
  703.   if (ProgressBar[name]) then
  704.     ProgressBar[name]["Current"] = intVal
  705.   end
  706. end
  707.  
  708. function DrawToPeripheral(thename, filled, unfilled)
  709.     if (fill == null) then
  710.        filled = FillColor
  711.      end
  712.      if (unfilled == null) then
  713.        unfilled= EmptyColor
  714.      end
  715.    
  716.   if (hasMonitor) then
  717.       DrawBar(thename, ProgressBar[thename], filled, unfilled)
  718.   end
  719. end
  720.  
  721. function DrawBar(name, arr, filled, unfilled)
  722.   local y = arr["YVal"]
  723.   local fill = math.floor((arr["XMax"] - arr["XMin"]) * (arr["Current"] / arr["Max"]))
  724.  
  725.   monitor.setBackgroundColor(filled)
  726.   monitor.setTextColor(TextColor)
  727.  
  728.   for x = arr["XMin"], arr["XMax"] do
  729.     local num = math.floor(x - arr["XMin"])
  730.     monitor.setCursorPos(x,y)
  731.  
  732.     if (num >= fill) then
  733.       monitor.setBackgroundColor(unfilled)
  734.     end
  735.  
  736.     if (num == 0) then
  737.       monitor.write("")
  738.     end
  739.     if (x == arr["XMax"]) then
  740.       monitor.write("")
  741.     else
  742.       monitor.write(" ")
  743.     end
  744.   end
  745.  
  746.   monitor.setBackgroundColor(colors.black)
  747. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement