Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Please forgive the problems with this script, I wrote it a long time ago when I was learning Lua.
- component = require("component")
- term = require("term")
- text = require("text")
- event = require("event")
- button = {}
- local function splitMessage(message, del) -- Splits a string by del (delimiter)
- if not text then text = require("text") end
- -- Default to whitespace
- if del == nil then
- del = "%s"
- end
- local t={} ; i=1
- for str in string.gmatch(message, "([^"..del.."]+)") do
- t[i] = str
- i = i + 1
- end
- return t
- end
- -- Generates a new button
- -- Usage:
- -- buttonList = button.newButton(buttonList: table, label: string, action: function, width: number, height: number, paddings: string[ e.g. "left:right:top:bottom"], colorSet: string [e.g. "0x00AA00:0xAA0000"])
- function button.newButton(buttonList, label, action, actionArgT, width, height, paddings, colorSet, xBorder, yBorder) -- format colors as color:color
- local tX, tY = component.gpu.getResolution()
- local buttonData = {}
- -- Screen margins (defaults for first button)
- local minX = 1
- local maxX = 0
- local minY = 2
- local maxY = 0
- -- Determine button placement
- if buttonList[#buttonList] then -- Add button to existing table
- -- Row end?
- local startX = buttonList[#buttonList].spatialT.maxX + width
- if startX > tX then -- New row
- minY = buttonList[#buttonList].spatialT.maxY
- minX = minX
- else -- Same row
- minX = buttonList[#buttonList].spatialT.maxX
- minY = buttonList[#buttonList].spatialT.minY
- end
- else -- Initial button on table
- minX = minX
- minY = minY
- end
- local maxX = minX + width
- local maxY = minY + height
- -- Set padding
- local paddingT = splitMessage(paddings, ":")
- -- Set render area
- local xMinRender = minX + (tonumber(text.trim(paddingT[1])) * 2)
- local xMaxRender = maxX - (tonumber(text.trim(paddingT[2])) * 2)
- local yMinRender = minY + tonumber(text.trim(paddingT[3]))
- local yMaxRender = maxY - tonumber(text.trim(paddingT[4]))
- local renderWidth = xMaxRender - xMinRender
- local renderHeight = yMaxRender - yMinRender
- -- Set colors
- local colorT = splitMessage(colorSet, ":")
- local spatialT =
- {
- minX=minX,
- maxX=maxX,
- minY=minY,
- maxY=maxY,
- width=width,
- height=height,
- xMinPadding=xMinPadding,
- xMaxPadding=xMaxPadding,
- yMinPadding=yMinPadding,
- yMaxPadding=yMaxPadding,
- xMinRender=xMinRender,
- xMaxRender=xMaxRender,
- yMinRender=yMinRender,
- yMaxRender=yMaxRender,
- renderWidth=renderWidth,
- renderHeight=renderHeight,
- }
- local buttonData =
- {
- label = label,
- action = action,
- actionArgT=actionArgT,
- state = false,
- colorT = colorT,
- spatialT=spatialT,
- xBorder=xBorder,
- yBorder=yBorder,
- }
- for i=1, #buttonData do
- assert(buttonData[i], "Missing buttonData "..i)
- end
- for i=1, #spatialT do
- assert(spatialT[i], "Missing spatialT"..i)
- end
- table.insert(buttonList, buttonData)
- return buttonList
- end
- function button.render(buttonData)
- local bG = component.gpu.getBackground()
- -- Create string of spaces
- local spacers = ""
- for i=1, buttonData.spatialT.renderWidth do
- spacers = spacers.." "
- end
- -- Create string of margin spaces for label
- local margin = ""
- for i=1, (buttonData.spatialT.renderWidth - string.len(buttonData.label))/2 do
- margin = margin.." "
- end
- -- Wrap label in margins
- local spacedLabel = margin..buttonData.label..margin
- if string.len(spacedLabel) < buttonData.spatialT.renderWidth then spacedLabel = spacedLabel.." " end
- -- Set color from state
- if buttonData.state then
- component.gpu.setBackground(tonumber(buttonData.colorT[1]))
- else
- component.gpu.setBackground(tonumber(buttonData.colorT[2]))
- end
- -- Write button to terminal
- for i=0, buttonData.spatialT.renderHeight-1 do
- term.setCursor(buttonData.spatialT.xMinRender, buttonData.spatialT.yMinRender+i)
- if i == math.floor((buttonData.spatialT.renderHeight/2)-1) then -- If we're at the center/high-center
- term.write(spacedLabel)
- else
- term.write(spacers)
- end
- end
- component.gpu.setBackground(bG)
- end
- function button.renderAll(buttonList)
- term.clear()
- for i=1, #buttonList do
- button.render(buttonList[i])
- end
- end
- function button.checkTouch(buttonList, x, y)
- local buttonList = buttonList
- local cButton = {}
- local onButton = false
- for i=1, #buttonList do
- if (x >= buttonList[i].spatialT.xMinRender) and (x <= buttonList[i].spatialT.xMaxRender) and (y >= buttonList[i].spatialT.yMinRender) and (y <= buttonList[i].spatialT.yMaxRender) then
- onButton = true
- -- Execute function with current button as arg
- -- Make sure the function returns current button table
- cButton = buttonList[i].action(buttonList[i], buttonList[i].actionArgT)
- -- Rewrite current button with table returned by action
- buttonList[i] = cButton
- end
- end
- return buttonList, cButton, onButton
- end
- function button.toggle(cButton, argT)
- if cButton.state then cButton.state = false else cButton.state = true end
- return cButton
- end
- function button.setAllStates(buttonList, argT)
- local buttonList = buttonList
- for i=1, #buttonList do
- buttonList[i].state = argT.state
- end
- return buttonList
- end
- return button
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement