Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[Peripheral API]]--
- function getDeviceSide(deviceType)
- deviceType = deviceType:lower()
- for i, side in pairs(rs.getSides()) do
- if (peripheral.isPresent(side)) then
- if (string.lower(peripheral.getType(side)) == deviceType) then
- return side;
- end
- end
- end
- return nil;
- end
- function wrapThis(thing)
- local wrapped, i = nil, 0
- while wrapped == nil and i <= 100 do
- wrapped = peripheral.wrap(thing.."_"..i)
- i = i + 1
- end
- if wrapped == nil then
- side = getDeviceSide(thing)
- if side ~= nil then
- return peripheral.wrap(side)
- else
- return nil
- end
- else
- return wrapped
- end
- end
- function wrapMonitor()
- local monitor, i = nil, 0
- while monitor == nil and i <= 100 do
- monitor = peripheral.wrap("monitor_"..i)
- i = i + 1
- end
- if monitor == nil then
- side = getDeviceSide("Monitor")
- if side ~= nil then
- return peripheral.wrap(side)
- else
- return nil
- end
- else
- return monitor
- end
- end
- function wrapReactor()
- side = getDeviceSide("BigReactors-Reactor")
- if side ~= nil then
- return peripheral.wrap(side)
- else
- return nil
- end
- end
- --[[Monitor API]]--
- function clearMonitors(str)
- local gap = 2
- term.clear()
- local width, height = term.getSize()
- printCentered(str, 1)
- for i=1, width do
- term.setCursorPos(i, gap)
- term.write("-")
- end
- term.setCursorPos(1, gap+1)
- end
- function write(mon, text, colour)
- local col = colour or colors.white
- mon.setTextColor(col)
- mon.write(text)
- mon.setTextColor(colors.white)
- end
- function resetMon(mon)
- mon.setTextColour(colors.white)
- mon.setBackgroundColour(colors.black)
- mon.setCursorPos(1,1)
- mon.setCursorBlink(false)
- mon.clear()
- end
- --[[Time API]]--
- function formatSeconds(s)
- if s > 86400 then
- return math.ceil(s/86400).."d "..formatSeconds(s%86400)
- elseif s > 3600 then
- return math.ceil(s/3600).."h "..formatSeconds(s%3600)
- elseif s > 60 then
- return math.ceil(s/60).."m "..formatSeconds(s%60)
- else
- return math.ceil(s).."s"
- end
- end
- --[[Colour API]]--
- function convertToString( nColor )
- for k, v in pairs( colors ) do
- if nColor == v then
- return k
- end
- end
- return nil
- end
- --[[Http API]]--
- function download( sUrl, sPath )
- assert( type( sUrl ) == "string", "String expected, got " .. type( sUrl ), 2)
- assert( type( sPath ) == "string", "String expected, got " .. type( sPath ), 2)
- assert( not fs.exists( sPath ), "Path already exists", 2)
- local response = http.get( sUrl )
- if response then
- local f = fs.open( sPath, "w" )
- f.write( response.readAll() )
- f.close()
- response.close()
- return true
- end
- return false
- end
- --[[Number API]]--
- function round(float)
- if float%math.floor(float) >= 0 then
- return math.ceil(float)
- else
- return math.floor(float)
- end
- end
- function roundToTen(float)
- float = math.ceil(float)
- if ((float % 10) - 10) >= 0 then
- return math.ceil(float + float%10)
- else
- return math.floor(float - float%10)
- end
- end
- function roundThousanth(num)
- local mult = 10^(0)
- return math.floor(num * mult + 0.5) / mult
- end
- function generate( nLength, nMin, nMax )
- assert( type( nLength ) == "number", "Number expected, got " .. type( nLength ), 2)
- assert( type( nMins ) == "number", "Number expected, got " .. type( nMin ), 2)
- assert( type( nMax ) == "number", "Number expected, got " .. type( nMax ), 2)
- assert( nMin < nMax, "Minimum must be less than maximum", 2)
- local n = math.random( nMin, nMax )
- for i = 1, nLength do
- n = n .. math.random( nMin, nMax )
- end
- return tonumber( n )
- end
- function isOdd( n )
- assert( type( n ) == "number", "Number expected, got " .. type( n ), 2)
- return n % 2 ~= 0
- end
- function isEven( n )
- assert( type( n ) == "number", "Number expected, got " .. type( n ), 2)
- return n % 2 == 0
- end
- --[[Pastebin API]]--
- function get( sCode, sFile )
- assert( type( sCode ) == "string", "Number expected, got " .. type( sCode ), 2)
- assert( type( sFile ) == "string", "String expected, got " .. type( sFile ), 2)
- local sPath = shell.resolve( sFile )
- assert( not fs.exists( sPath ), "File exists", 2)
- local tResponse = http.get( "http://pastebin.com/" .. textutils.urlEncode( sCode ) )
- if tResponse then
- local sResponse = tResponse.readAll()
- tResponse.close()
- local handle = fs.open( sPath, "w" )
- handle.write( sResponse )
- handle.close()
- return true
- end
- return false
- end
- function put( sFile )
- assert( type( sFile ) == "string", "String expected, got " .. type( sFile ), 2)
- local sPath = shell.resolve( sFile )
- assert( not fs.isDir( sPath ), "Cannot upload directories", 2 )
- assert( not fs.exists( sPath ), "File doesn't exist", 2 )
- local sName = fs.getName( sPath )
- local handle = fs.open( sPath, "r" )
- local sText = handle.readAll()
- handle.close()
- local key = "0ec2eb25b6166c0c27a394ae118ad829"
- local response = http.post(
- "http://pastebin.com/api/api_post.php",
- "api_option=paste&" ..
- "api_dev_key=" .. key .. "&" ..
- "api_paste_format=lua&" ..
- "api_paste_name=" .. textutils.urlEncode( sPath ) .. "&" ..
- "api_paste_code=" .. textutils.urlEncode( sText )
- )
- if response then
- local sResponse = response.readAll()
- local sCode = string.match( sResponse, "[^/]+$" )
- response.close()
- return sCode, true
- end
- return nil, false
- end
- --[[Term API]]--
- function clear( nx, ny, nTextColour, nBackgroundColour )
- if nTextColour ~= nil then
- assert( type( nTextColour ) == "number", "Number expected, got " .. type( nTextColour ), 2)
- term.setTextColour( nTextColour )
- end
- if nBackgroundColour ~= nil then
- assert( type( nBackgroundColour ) == "number", "Number expected, got " .. type( nBackgroundColour ), 2)
- term.setBackgroundColour( nBackgroundColour )
- end
- if nx ~= nil then
- assert( type( nx ) == "number", "Number expected, got " .. type( nx ), 2)
- end
- if ny ~= nil then
- assert( type( ny ) == "number", "Number expected, got " .. type( nx ), 2)
- end
- term.setCursorPos( nx or currentX, ny or currentY )
- oldTerm.clear()
- local nMaxx,nMaxy = term.getSize()
- -- Clear pixel data
- for i = 1, nMaxx do
- for j = 1, nMaxy do
- tPixels[i.." "..j] = {
- Character = " ",
- TextColor = nTextColour or term.getTextColour(),
- TextColour = nTextColour or term.getTextColour(),
- BackgroundColor = nBackgroundColour or term.getBackgroundColour(),
- BackgroundColour = nBackgroundColour or term.getBackgroundColour()
- }
- end
- end
- end
- function restoreNativeTerminal()
- repeat
- term.restore()
- local w, h = term.getSize()
- until w == 51 and h == 19
- end
- function reset()
- term.setTextColour(colors.white)
- term.setBackgroundColour(colors.black)
- term.setCursorPos(1,1)
- term.setCursorBlink(false)
- term.clear()
- end
- --[[Text API]]--
- function comma_value(amount)
- local formatted = amount
- local swap = false
- if formatted < 0 then
- formatted = formatted*-1
- swap = true
- end
- while true do
- formatted, k = string.gsub(formatted, "^(%d+)(%d%d%d)", '%1,%2')
- if k == 0 then
- break
- end
- end
- if swap then
- formatted = "-"..formatted
- end
- return formatted
- end
- function generate( nLength, nCharSet )
- assert( type( nLength ) == "number", "Number expected, got " .. type( nLength ), 2)
- assert( type( nCharSet ) == "number", "Number expected, got " .. type( nCharSet ), 2)
- local nCharSet = nCharSet or 128
- local str = ""
- for i = 1, nLength do
- str = str .. math.random( 1, nCharSet ):char()
- end
- return str
- end
- function printCentered(str, yPos)
- local width, height = term.getSize()
- term.setCursorPos(math.floor(width/2) - math.ceil(str:len()/2) , yPos)
- term.clearLine()
- term.write(str)
- end
- function stringToBool(str)
- if str == "false" then
- return false
- end
- if str == "true" then
- return true
- else
- return nil
- end
- end
- function bracket( sText, nx, ny, nTextColour, nBracketColour, nBackgroundColour )
- assert( type( sText ) == "string", "String expected, got " .. type( sText ), 2)
- assert( type( nx ) == "number", "Number expected, got " .. type( nx ), 2)
- assert( type( ny ) == "number", "Number expected, got " .. type( ny ), 2)
- if nTextColour then
- assert( type( nTextColour ) == "number", "Number/nil expected, got " .. type( nTextColour ), 2)
- end
- if nBracketColour then
- assert( type( nBracketColour ) == "number", "Number/nil expected, got " .. type( nBracketColour ), 2)
- end
- if nBackgroundColour then
- assert( type( nBackgroundColour ) == "number", "Number/nil expected, got " .. type( nBackgroundColour ), 2)
- term.setBackgroundColour( nBackgroundColour )
- end
- local xPos, yPos = term.getCursorPos()
- term.setCursorPos( nx, ny )
- term.setTextColour( nBracketColour or colours.white )
- term.write( "[" .. string.rep(" ", #sText) .. "]" )
- term.setTextColour( nTextColour or colours.white )
- term.setCursorPos( nx + 1, ny )
- term.write( sText )
- end
- function center( sText, nx, ny )
- assert( type( sText ) == "string", "String expected, got " .. type( sText ), 2)
- assert( type( nx ) == "number", "Number expected, got " .. type( nx ), 2)
- assert( type( ny ) == "number", "Number expected, got " .. type( ny ), 2)
- term.setCursorPos( (( nx - #sText ) / 2) - 1, ny )
- term.write( sText )
- end
- --[[Touchpoint API]]--
- local function setupLabel(buttonLen, minY, maxY, name)
- local labelTable = {}
- if type(name) == "table" then
- for i = 1, #name do
- labelTable[i] = name[i]
- end
- name = name.label
- elseif type(name) == "string" then
- local buttonText = string.sub(name, 1, buttonLen - 2)
- if #buttonText < #name then
- buttonText = " "..buttonText.." "
- else
- local labelLine = string.rep(" ", math.floor((buttonLen - #buttonText) / 2))..buttonText
- buttonText = labelLine..string.rep(" ", buttonLen - #labelLine)
- end
- for i = 1, maxY - minY + 1 do
- if maxY == minY or i == math.floor((maxY - minY) / 2) + 1 then
- labelTable[i] = buttonText
- else
- labelTable[i] = string.rep(" ", buttonLen)
- end
- end
- end
- return labelTable, name
- end
- local Button = {
- draw = function(self)
- local old = term.redirect(self.mon)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- for name, buttonData in pairs(self.buttonList) do
- if buttonData.active then
- term.setBackgroundColor(buttonData.activeColor)
- term.setTextColor(buttonData.activeText)
- else
- term.setBackgroundColor(buttonData.inactiveColor)
- term.setTextColor(buttonData.inactiveText)
- end
- for i = buttonData.yMin, buttonData.yMax do
- term.setCursorPos(buttonData.xMin, i)
- term.write(buttonData.label[i - buttonData.yMin + 1])
- end
- end
- if old then
- term.redirect(old)
- else
- term.restore()
- end
- end,
- add = function(self, name, func, xMin, yMin, xMax, yMax, inactiveColor, activeColor, inactiveText, activeText)
- local label, name = setupLabel(xMax - xMin + 1, yMin, yMax, name)
- if self.buttonList[name] then error("button already exists", 2) end
- local x, y = self.mon.getSize()
- if xMin < 1 or yMin < 1 or xMax > x or yMax > y then error("button out of bounds", 2) end
- self.buttonList[name] = {
- func = func,
- xMin = xMin,
- yMin = yMin,
- xMax = xMax,
- yMax = yMax,
- active = false,
- inactiveColor = inactiveColor or colors.red,
- activeColor = activeColor or colors.lime,
- inactiveText = inactiveText or colors.white,
- activeText = activeText or colors.white,
- label = label,
- }
- for i = xMin, xMax do
- for j = yMin, yMax do
- if self.clickMap[i][j] ~= nil then
- --undo changes
- for k = xMin, xMax do
- for l = yMin, yMax do
- if self.clickMap[k][l] == name then
- self.clickMap[k][l] = nil
- end
- end
- end
- self.buttonList[name] = nil
- error("overlapping button", 2)
- end
- self.clickMap[i][j] = name
- end
- end
- end,
- remove = function(self, name)
- if self.buttonList[name] then
- local button = self.buttonList[name]
- for i = button.xMin, button.xMax do
- for j = button.yMin, button.yMax do
- self.clickMap[i][j] = nil
- end
- end
- self.buttonList[name] = nil
- end
- end,
- run = function(self)
- while true do
- self:draw()
- local event = {self:handleEvents(os.pullEvent("monitor_touch"))}
- if event[1] == "button_click" then
- self.buttonList[event[2]].func()
- end
- end
- end,
- handleEvents = function(self, ...)
- local event = {...}
- if #event == 0 then event = {os.pullEvent()} end
- if event[1] == "monitor_touch" and event[2] == self.side then
- local clicked = self.clickMap[event[3]][event[4]]
- if clicked and self.buttonList[clicked] then
- return "button_click", clicked
- end
- end
- return unpack(event)
- end,
- toggleButton = function(self, name, noDraw)
- self.buttonList[name].active = not self.buttonList[name].active
- if not noDraw then self:draw() end
- end,
- flash = function(self, name, duration)
- self:toggleButton(name)
- sleep(tonumber(duration) or 0.15)
- self:toggleButton(name)
- end,
- rename = function(self, name, newName)
- self.buttonList[name].label, newName = setupLabel(self.buttonList[name].xMax - self.buttonList[name].xMin + 1, self.buttonList[name].yMin, self.buttonList[name].yMax, newName)
- if not self.buttonList[name] then error("no such button", 2) end
- if name ~= newName then
- self.buttonList[newName] = self.buttonList[name]
- self.buttonList[name] = nil
- for i = self.buttonList[newName].xMin, self.buttonList[newName].xMax do
- for j = self.buttonList[newName].yMin, self.buttonList[newName].yMax do
- self.clickMap[i][j] = newName
- end
- end
- end
- self:draw()
- end,
- }
- function newTouchpoint(monSide)
- local buttonInstance = {
- side = monSide,
- mon = peripheral.wrap(monSide),
- buttonList = {},
- clickMap = {},
- }
- local x, y = buttonInstance.mon.getSize()
- for i = 1, x do
- buttonInstance.clickMap[i] = {}
- end
- setmetatable(buttonInstance, {__index = Button})
- return buttonInstance
- end
- --[[Button API]]--
- local mon = peripheral.wrap("right")
- mon.setTextScale(1)
- mon.setTextColor(colors.white)
- local button={}
- mon.setBackgroundColor(colors.black)
- function clearTable()
- button = {}
- end
- function setButton(name, buttonOn)
- print(name)
- print(button[name]["active"])
- button[name]["active"] = buttonOn
- screen()
- end
- function setTable(name, func, param, xmin, xmax, ymin, ymax)
- button[name] = {}
- button[name]["func"] = func
- button[name]["active"] = false
- button[name]["param"] = param
- button[name]["xmin"] = xmin
- button[name]["ymin"] = ymin
- button[name]["xmax"] = xmax
- button[name]["ymax"] = ymax
- end
- function funcName()
- print("You clicked buttonText")
- end
- function fillTable()
- setTable("ButtonText", funcName, 5, 25, 4, 8)
- end
- function fill(text, color, bData)
- mon.setBackgroundColor(color)
- local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
- local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
- for j = bData["ymin"], bData["ymax"] do
- mon.setCursorPos(bData["xmin"], j)
- if j == yspot then
- for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
- if k == xspot then
- mon.write(text)
- else
- mon.write(" ")
- end
- end
- else
- for i = bData["xmin"], bData["xmax"] do
- mon.write(" ")
- end
- end
- end
- mon.setBackgroundColor(colors.black)
- end
- function screen()
- local currColor
- for name,data in pairs(button) do
- local on = data["active"]
- if on == true then currColor = colors.lime else currColor = colors.red end
- fill(name, currColor, data)
- end
- end
- function toggleButton(name)
- button[name]["active"] = not button[name]["active"]
- screen()
- end
- function flash(name)
- toggleButton(name)
- screen()
- sleep(0.15)
- toggleButton(name)
- screen()
- end
- function checkxy(x, y)
- for name, data in pairs(button) do
- if y>=data["ymin"] and y <= data["ymax"] then
- if x>=data["xmin"] and x<= data["xmax"] then
- if data["param"] == "" then
- data["func"]()
- else
- data["func"](data["param"])
- end
- return true
- --data["active"] = not data["active"]
- --print(name)
- end
- end
- end
- return false
- end
- function heading(text)
- w, h = mon.getSize()
- mon.setCursorPos((w-string.len(text))/2+1, 1)
- mon.write(text)
- end
- function label(w, h, text)
- mon.setCursorPos(w, h)
- mon.write(text)
- end
- function getClick()
- event, side, x, y = os.pullEvent("monitor_touch")
- checkxy(x, y)
- end
- --[[Progress Bar API]]--
- local monitor
- local hasMonitor = false
- local ProgressBar = {}
- local FillColor = colors.orange
- local EmptyColor = colors.blue
- local TextColor = colors.white
- function SetPeripheral(side)
- if (peripheral.isPresent(side)) then
- monitor = peripheral.wrap(side)
- hasMonitor = true
- end
- end
- function SetTable(name, maxVal, curVal, xmin, xmax, y)
- ProgressBar[name] = {}
- ProgressBar[name]["Max"] = maxVal
- ProgressBar[name]["Current"] = curVal
- ProgressBar[name]["XMin"] = xmin
- ProgressBar[name]["XMax"] = xmax
- ProgressBar[name]["YVal"] = y
- end
- function ClearTable()
- if (hasMonitor) then
- ProgressBar = {}
- end
- end
- function SetFillColor(color)
- if (colors.combine(color,color)) then
- FillColor = color
- end
- end
- function SetEmptyColor(color)
- if (colors.combine(color,color)) then
- EmptyColor = color
- end
- end
- function SetTextColor(color)
- if (colors.combine(color,color)) then
- TextColor = color
- end
- end
- function SetMaxValue(name, intVal)
- if (ProgressBar[name]) then
- ProgressBar[name]["Max"] = intVal
- end
- end
- function SetCurValue(name, intVal)
- if (ProgressBar[name]) then
- ProgressBar[name]["Current"] = intVal
- end
- end
- function DrawToPeripheral(thename, filled, unfilled)
- if (fill == null) then
- filled = FillColor
- end
- if (unfilled == null) then
- unfilled= EmptyColor
- end
- if (hasMonitor) then
- DrawBar(thename, ProgressBar[thename], filled, unfilled)
- end
- end
- function DrawBar(name, arr, filled, unfilled)
- local y = arr["YVal"]
- local fill = math.floor((arr["XMax"] - arr["XMin"]) * (arr["Current"] / arr["Max"]))
- monitor.setBackgroundColor(filled)
- monitor.setTextColor(TextColor)
- for x = arr["XMin"], arr["XMax"] do
- local num = math.floor(x - arr["XMin"])
- monitor.setCursorPos(x,y)
- if (num >= fill) then
- monitor.setBackgroundColor(unfilled)
- end
- if (num == 0) then
- monitor.write("")
- end
- if (x == arr["XMax"]) then
- monitor.write("")
- else
- monitor.write(" ")
- end
- end
- monitor.setBackgroundColor(colors.black)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement