Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 + 1) / 2) then
- labelTable[i] = buttonText
- else
- labelTable[i] = string.rep(" ", buttonLen)
- end
- end
- end
- return labelTable, name
- end
- local Button = {
- draw = function(self, blink)
- blink = blink or false
- term.redirect(self.mon)
- term.setBackgroundColor(colors.black)
- term.clear()
- if blink then term.setCursorBlink(true) else term.setCursorBlink(false) end
- for name, buttonData in pairs(self.buttonList) do
- if buttonData.active then
- term.setBackgroundColor(buttonData.activeColor)
- term.setTextColor(buttonData.activeTextColor)
- else
- term.setBackgroundColor(buttonData.inactiveColor)
- term.setTextColor(buttonData.inactiveTextColor)
- end
- for i = buttonData.yMin, buttonData.yMax do
- term.setCursorPos(buttonData.xMin, i)
- term.write(buttonData.label[i - buttonData.yMin + 1])
- end
- end
- term.restore()
- end,
- debugPrint = function(self)
- for name, buttonData in pairs(self.buttonList) do
- print ("name, ",name," buttonData, ", buttonData)
- end
- end,
- add = function(self, name, func, xMin, xMax, yMin, yMax, inactiveTextColor, activeTextColor, inactiveColor, activeColor, activity)
- 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 = activity or false,
- inactiveTextColor = inactiveTextColor or colors.cyan,
- activeTextColor = activeTextColor or colors.black,
- inactiveColor = inactiveColor or colors.black,
- activeColor = activeColor or colors.black,
- label = label,
- }
- for i = xMin, xMax do
- for j = yMin, yMax do
- if self.clickMap[i][j] ~= nil then
- --undo changes
- print (self.clickMap[i][j])
- for k = xMin, xMax do
- for l = yMin, yMax do
- if self.clickMap[k][l] == name then
- print( "overlapping with ",self.clickMap[k][l])
- 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,
- run = function(self)
- while true do
- self:draw()
- local event = {os.pullEvent("monitor_touch")}
- if event[2] == self.side then
- local clicked = self.clickMap[event[3]][event[4]]
- if clicked and self.buttonList[clicked] and self.buttonList[clicked].func then
- self.buttonList[clicked].func()
- end
- end
- end
- end,
- run2 = function(self, p1)
- self.buttonList[p1].func()
- 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)
- self:toggleButton(name)
- sleep(0.15)
- self:toggleButton(name)
- end,
- rename = function(self, name, newName, noDraw)
- 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
- self.buttonList[newName] = self.buttonList[name]
- if name ~= newName then self.buttonList[name] = nil end
- 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
- if not noDraw then self:draw() end
- end,
- remove = function(self, name)
- if not self.buttonList[name] then error("no such button", 2) end
- for i = self.buttonList[name].xMin, self.buttonList[name].xMax do
- for j = self.buttonList[name].yMin, self.buttonList[name].yMax do
- self.clickMap[i][j] = nil
- end
- end
- self.buttonList[name] = nil
- end,
- redo = function (self, name, func, inactiveTextColor)
- if not self.buttonList[name] then error("no such button", 2) end
- self.buttonList[name].func = func
- self.buttonList[name].inactiveTextColor = inactiveTextColor
- end
- }
- function new(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, x, y
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement