Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Set up variables
- local attachedMonitor = peripheral.wrap("top")
- local defaultBackgroundColor =colors.black
- local defaultTextColor=colors.white
- local backgroundColorNormal = colors.blue
- local backgroundColorPressed = colors.red
- local textColorNormal = colors.white
- local textColorPressed = colors.yellow
- local hasBorder = false
- local borderColorPressed = colors.pink
- local borderColorNormal = colors.lightBlue
- local width=10
- local height = 3
- local label="Button"
- local startColumn=3
- local startRow=3
- local labelPad = 2
- -- our draw function
- local function draw(isPressed)
- if isPressed == false or isPressed then
- isPressed = isPressed
- else
- isPressed = false
- end
- -- store values
- local width_old = width
- local height_old = height
- local startColumn_old = startColumn
- local startRow_old = startRow
- -- set border params and draw border if hasBorder
- if 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 attachedMonitor.isColor() then
- attachedMonitor.setBackgroundColor(colors.white)
- else
- attachedMonitor.setBackgroundColor(borderColorNormal)
- end
- else
- if not attachedMonitor.isColor() then
- attachedMonitor.setBackgroundColor(colors.white)
- else
- attachedMonitor.setBackgroundColor(borderColorPressed)
- end
- end
- -- draw button border
- attachedMonitor.setCursorPos(startColumn,startRow)
- attachedMonitor.write(string.rep(" ",width))
- for row = 2,height-1 do
- attachedMonitor.setCursorPos(startColumn,startRow+row -1)
- attachedMonitor.write(" ")
- attachedMonitor.setCursorPos(startColumn+width -1 ,startRow + row-1)
- attachedMonitor.write(" ")
- end
- attachedMonitor.setCursorPos(startColumn,startRow+height-1)
- attachedMonitor.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 the button/label colors
- if not isPressed then
- if not attachedMonitor.isColor() then
- attachedMonitor.setBackgroundColor(colors.black)
- attachedMonitor.setTextColor(colors.white)
- else
- attachedMonitor.setBackgroundColor(backgroundColorNormal)
- attachedMonitor.setTextColor(textColorNormal)
- end
- else
- if not attachedMonitor.isColor() then
- attachedMonitor.setBackgroundColor(colors.white)
- attachedMonitor.setTextColor(colors.black)
- else
- attachedMonitor.setBackgroundColor(backgroundColorPressed)
- attachedMonitor.setTextColor(textColorPressed)
- end
- end
- -- Display the button Background
- for row = startRow,startRow+height-1 do
- attachedMonitor.setCursorPos(startColumn,row)
- attachedMonitor.write(string.rep(" ",width))
- end
- -- 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
- --Display the label (auto centered)
- attachedMonitor.setCursorPos(startColumn + math.floor((width - #label)/2),startRow + math.floor((height-1)/2))
- attachedMonitor.write(label)
- -- reset to original text/background colors
- if not attachedMonitor.isColor() then
- attachedMonitor.setBackgroundColor(colors.black)
- attachedMonitor.setTextColor(colors.white)
- else
- attachedMonitor.setBackgroundColor(defaultBackgroundColor)
- attachedMonitor.setTextColor(defaultTextColor)
- end
- --restore values if changed
- if hasBorder then
- width = width_old
- height = height_old
- startColumn = startColumn_old
- startRow = startRow_old
- end
- end
- -- Set up Monitor
- if not attachedMonitor.isColor() then
- attachedMonitor.setBackgroundColor(colors.black)
- attachedMonitor.setTextColor(colors.white)
- else
- attachedMonitor.setBackgroundColor(defaultBackgroundColor)
- attachedMonitor.setTextColor(defaultTextColor)
- end
- --attachedMonitor.setTextScale(.5)
- attachedMonitor.clear()
- hasBorder = true
- draw(false)
- sleep(3)
- draw(true)
- attachedMonitor.setCursorPos(1,8)
- attachedMonitor.write("Done.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement