Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function Button(
- width,
- height,
- label,
- backgroundColorNormal,
- backgroundColorPressed,
- textColorNormal,
- textColorPressed,
- hasBorder,
- borderColorNormal,
- borderColorPressed,
- startColumn,
- startRow,
- isPressed,
- defaultBackgroundColor,
- defaultTextColor
- )
- local button = {}
- button.height=height or 1
- button.width=width or 1
- button.label=label or ""
- button.backgroundColorNormal=backgroundColorNormal or colors.green
- button.backgroundColorPressed=backgroundColorPressed or colors.red
- button.textColorNormal=textColorNormal or colors.black
- button.textColorPressed=textColorPressed or colors.black
- button.hasBorder = hasBorder or false
- button.borderColorNormal = borderColorNormal or backGroundColorNormal
- button.borderColorPressed = borderColorPressed or backGroundColorPressed
- button.defaultBackgroundColor = defaultBackgroundColor or colors.black
- button.defaultTextColor = defaultTextColor or colors.white
- button.startColumn = startColumn or 1
- button.startRow = startRow or 1
- button.isPressed=isPressed or false
- function button.clicked(column,row)
- return (column >= button.startColumn and column < button.startColumn + button.width and row >= button.startRow and row < button.startRow + button.height)
- end
- function button.draw(display,isPressed,startColumn,startRow)
- button.startColumn = startColumn or button.startColumn
- button.startRow = startRow or button.startRow
- display = display or term
- if isPressed == false or isPressed then
- button.isPressed = isPressed
- else isPressed = button.isPressed
- end
- local width = button.width
- local height = button.height
- startRow = button.startRow
- startColumn = button.startColumn
- local label = button.label
- local labelPad = 2
- -- set border params and draw border if hasBorder
- if button.hasBorder == true then
- -- button must be at least 3x3, if not, make it so
- if width < 3 then
- width = 3
- end
- if height < 3 then
- height = 3
- end
- -- set border colors
- if not isPressed then
- if not display.isColor() then
- display.setBackgroundColor(colors.white)
- else
- display.setBackgroundColor(button.borderColorNormal)
- end
- else
- if not display.isColor() then
- display.setBackgroundColor(colors.white)
- else
- display.setBackgroundColor(button.borderColorPressed)
- end
- end
- -- draw button border (inset)
- display.setCursorPos(startColumn,startRow)
- display.write(string.rep(" ",width))
- for row = 2,height-1 do
- display.setCursorPos(startColumn,button.startRow+row -1)
- display.write(" ")
- display.setCursorPos(startColumn+width -1 ,startRow + row-1)
- display.write(" ")
- end
- display.setCursorPos(startColumn,startRow+height-1)
- display.write(string.rep(" ",width))
- -- reset startColumn,startRow,width,column to inset button and label
- startColumn=startColumn+1
- startRow = startRow +1
- width = width - 2
- height = height - 2
- end
- --set button background and text colors
- if not isPressed then
- if not display.isColor() then
- display.setBackgroundColor(colors.black)
- display.setTextColor(colors.white)
- else
- display.setBackgroundColor(button.backgroundColorNormal)
- display.setTextColor(button.textColorNormal)
- end
- else
- if not display.isColor() then
- display.setBackgroundColor(colors.white)
- display.setTextColor(colors.black)
- else
- display.setBackgroundColor(button.backgroundColorPressed)
- display.setTextColor(button.textColorPressed)
- end
- end
- -- draw button background (will be inside border if there is one)
- for row = 1,height do
- --print(tostring(startColumn)..","..tostring(startRow-row))
- display.setCursorPos(startColumn,startRow + row -1)
- display.write(string.rep(" ",width))
- end
- -- prepare label, truncate label if necessary
- -- prepare label, truncate label if necessary
- if width < 3 then
- labelPad = 0
- end
- if #label > width - labelPad then
- label = label:sub(1,width - labelPad)
- end
- -- draw label
- display.setCursorPos(startColumn + math.floor((width - #label)/2),startRow + math.floor((height-1)/2))
- display.write(label)
- display.setBackgroundColor(button.defaultBackgroundColor)
- display.setTextColor(button.defaultTextColor)
- end
- button.getLabel = function ()
- return button.label
- end
- button.toggle = function ()
- button.isPressed = not button.isPressed
- return button.isPressed
- end
- return button
- end
- sSide = "bottom"
- local function toogleCableOutput(color, signal)
- if signal then
- redstone.setBundledOutput(sSide,colors.subtract(redstone.getBundledOutput(sSide), color))
- else
- redstone.setBundledOutput(sSide,colors.combine(redstone.getBundledOutput(sSide),color))
- end
- end
- -- color codes at the end
- local function resetCableOutput()
- for i = 0, 15, 1 do
- redstone.setBundledOutput(sSide,colors.combine(redstone.getBundledOutput(sSide),2^i))
- end
- end
- -- start of the program
- attachedMonitor = peripheral.wrap("right")
- attachedMonitor.setTextScale(1)
- resetCableOutput() -- disable all output, set redstone signal to true
- buttons = {}
- -- define buttons
- buttons.ghast= Button(9,6,"Ghast",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,1,1,false,nil,nil)
- buttons.empty2 = Button(9,7,"Empty2",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,1,7,false,nil,nil)
- buttons.empty3 = Button(9,6,"Empty3",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,1,14,false,nil,nil)
- buttons.empty4 = Button(11,6,"Empty4",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,10,1,false,nil,nil)
- buttons.empty5 = Button(11,7,"Empty5",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,10,7,false,nil,nil)
- buttons.empty6 = Button(11,6,"Empty6",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,10,14,false,nil,nil)
- buttons.empty7 = Button(9,6,"Empty7",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,21,1,false,nil,nil)
- buttons.empty8 = Button(9,7,"Empty8",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,21,7,false,nil,nil)
- buttons.empty9 = Button(9,6,"Empty9",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,21,14,false,nil,nil)
- -- display buttons on attachedMonitor
- attachedMonitor.clear()
- for key,button in pairs(buttons) do
- button.draw(attachedMonitor)
- end
- -- event loop
- while true do
- event = {os.pullEvent()}
- if event[1] =="monitor_touch" then
- for key,button in pairs(buttons) do
- if button.clicked(event[3],event[4]) then -- column,row
- if button.getLabel() == "Ghast" then
- toogleCableOutput(colors.white, button.toggle())
- button.draw(attachedMonitor)
- break -- we found on, so we don't need to keep looking
- end
- end
- end
- end
- end
- --[[ color codes
- 0 nothing
- 1 white
- 2 orange
- 4 magenta
- 8 light blue
- 16 yellow
- 32 lime
- 64 pink
- 128 gray
- 256 light gray
- 512 cyan
- 1024 purple
- 2048 blue
- 4096 brown
- 8192 green
- 16384 red
- 32768 black
- 65535 all ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement