Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- monitorSide = "right"
- mon = peripheral.wrap(monitorSide)
- buttonInfo = {}
- textInfo = {}
- if mon.isColor() then
- print("Advanced monitor attached, continuing!")
- else
- print("You must attach an advanced monitor!\n\nAborting!")
- error()
- end
- function fillBackground(peripheral, color, xStart, yStart, xEnd, yEnd)
- peripheral.setBackgroundColor(color)
- for y = yStart, yEnd do
- peripheral.setCursorPos(xStart, y)
- peripheral.write(string.rep(" ", xEnd - xStart))
- end
- end
- -- Parameters:
- -- per = attached peripheral function
- -- func = function to execute onClick
- -- colorON/OFF = (in-)active color
- -- enabled = enabled when drawing?
- -- mode = 0 toggle | >0 - change color for * seconds | <0 - blink for * seconds
- function createButton(per, func, text, textColorOFF, textColorON, bgColorOFF, bgColorON, enabled, mode, xStart, yStart, xSize, ySize)
- if textColorON == -1 then
- textColorON = textColorOFF
- end
- if bgColorON == -1 then
- bgColorON = bgColorOFF
- end
- if enabled then
- drawButton(per, text, textColorON, bgColorON, xStart, yStart, xSize, ySize)
- else
- drawButton(per, text, textColorOFF, bgColorOFF, xStart, yStart, xSize, ySize)
- end
- buttonInfo[ #buttonInfo + 1 ] = {
- [1] = xStart,
- [2] = yStart,
- [3] = (xSize == 0 and xStart + string.len(text) or xSize), -- xEnd
- [4] = (ySize == 0 and ySize + 1 or ySize), -- yEnd
- [5] = per,
- [6] = func,
- [7] = text,
- [8] = textColorOFF,
- [9] = textColorON,
- [10] = bgColorOFF,
- [11] = bgColorON,
- [12] = enabled,
- [13] = mode,
- [14] = xSize,
- [15] = ySize
- }
- return #buttonInfo - 1
- end
- function createText(per, text, textColor, bgColor, xStart, yStart, xSize, ySize)
- -- multiline draw functionality
- --drawButton(per, text, textColor, bgColor, xStart, yStart, xSize, ySize)
- textInfo[ #buttonInfo + 1 ] = {
- [1] = xStart,
- [2] = yStart,
- [3] = (xSize == 0 and xStart + string.len(text) or xSize), -- xEnd
- [4] = (ySize == 0 and ySize + 1 or ySize), -- yEnd
- [5] = per,
- [6] = text,
- [7] = textColor,
- [8] = bgColor,
- [9] = xSize,
- [10] = ySize
- }
- return #textInfo - 1
- end
- function drawButton(per, text, textColor, bgColor, xStart, yStart, xSize, ySize)
- if xSize or ySize then -- need to fill background?
- if not xSize then xSize = 0 end
- if not ySize then ySize = 0 end
- fillBackground(per, bgColor, xStart - xSize, yStart - ySize, xStart + string.len(text) + xSize, yStart + ySize)
- end
- per.setBackgroundColor(bgColor)
- per.setTextColor(textColor)
- per.setCursorPos(xStart, yStart)
- per.write(text)
- end
- function blinkButton(per, text, textColorOFF, textColorON, bgColorOFF, bgColorON, mode, enabled, xStart, yStart, xSize, ySize)
- end
- function executeMode(per, text, textColorOFF, textColorON, bgColorOFF, bgColorON, mode, enabled, xStart, yStart, xSize, ySize)
- local textColor1, bgColor1, textColor2, bgColor2
- if enabled then -- swap colors
- textColor1, bgColor1, textColor2, bgColor2 = textColorOFF, bgColorOFF, textColorON, bgColorON
- else
- textColor1, bgColor1, textColor2, bgColor2 = textColorON, bgColorON, textColorOFF, bgColorOFF
- end
- if mode == 0 then -- TOGGLE
- drawButton(per, text, textColor1, bgColor1, xStart, yStart, xSize, ySize)
- elseif mode > 0 then -- CHANGE COLOR FOR <MODE> SECONDS
- drawButton(per, text, textColorOFF, bgColorOFF, xStart, yStart, xSize, ySize)
- sleep(mode)
- drawButton(per, text, textColorON, bgColorON, xStart, yStart, xSize, ySize)
- else -- mode < 0 && BLINK
- local time = math.abs(mode)
- print("Blink time: ".. time)
- while time > 0 do
- print("tick ", time)
- drawButton(per, text, textColorOFF, bgColorOFF, xStart, yStart, xSize, ySize)
- sleep( .4 )
- drawButton(per, text, textColorON, bgColorON, xStart, yStart, xSize, ySize)
- sleep( .4 )
- print("tok")
- time = time - .4
- end
- end
- end
- function onButtonClick(id, x, y)
- local xStart, yStart, xEnd, yEnd, per, func, text, textColorOFF, textColorON, bgColorOFF, bgColorON, enabled, mode, xSize, ySize = unpack(buttonInfo[ id ])
- -- ^ should work defining all vars
- print(xStart == buttonInfo[id][1], mode == buttonInfo[id][13], ySize == buttonInfo[ id ][15])
- func(id, x, y)
- executeMode(per, text, textColorOFF, textColorON, bgColorOFF, bgColorON, mode, enabled, xStart, yStart, xSize, ySize)
- buttonInfo[ id ][12] = not enabled --toggle state
- end
- function buttonBlink(id)
- end
- fillBackground(mon, 4096, 0, 0, mon.getSize())
- --fillBackground(mon, 2048, 5, 8, 4, 5)
- createButton(mon, print, "TOGGLE", 1, -1, 8192, 2048, true, 0, 7, 8, 4, 3)
- createButton(mon, print, "BLINK", 1, 16, 64, 256, true, -6, 7, 1, 10, 1)
- createButton(mon, print, "CHANGE COLOR", 1, 16, 64, 256, true, 2, 15, 16, 2.5, 1)
- while true do
- local event, side, x, y = os.pullEvent("monitor_touch")
- print("\n"..x .. ":" .. y)
- for key, button in pairs(buttonInfo) do
- local xStart, yStart, xEnd, yEnd = button[1] - button[14], button[2] - button[15], button[1] + string.len(button[7]) + button[14], button[2] + button[15]
- print("\n"..xStart .. ":" .. yStart .. " -- " .. xEnd .. ":" .. yEnd)
- print("cYs: " .. yStart .." <= "..y..", "..tostring(yStart<=y) )
- print("cYe: " .. y .." <= "..yEnd..", "..tostring(y<=yEnd) )
- if yStart <= y and y <= yEnd then
- print("cXs: " .. xStart .." <= "..x..", "..tostring(xStart<=x) )
- print("cXe: " .. x .." <= "..xEnd..", "..tostring(x<=xEnd) )
- if xStart <= x and x <= xEnd then
- print("Key is: " .. key)
- onButtonClick(key, x, y)
- break
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement