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.black
- button.backgroundColorPressed=backgroundColorPressed or colors.white
- button.textColorNormal=textColorNormal or colors.white
- 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.press()
- button.isPressed = not button.isPressed
- 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 localPad = 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
- if width < 3 then
- localPad = 0
- end
- if #label > width - localPad then
- label = label:sub(1,width - localPad)
- 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
- return button
- end
- local attachedMonitor = peripheral.wrap("top")
- attachedMonitor.clear()
- attachedMonitor.setTextScale(1)
- local HQ_button = Button(
- 10,3, --width,height
- "HQ", --label
- colors.blue,colors.red, -- background colors
- colors.white,colors.yellow, -- text colors
- true,colors.lightBlue, colors.pink, -- hasBorder, border colors
- 10,10, -- starting location
- false, -- isPressed?
- colors.black,colors.white -- default colors
- )
- rednet.open("back")
- HQ_button.draw(attachedMonitor)
- while true do
- event = {os.pullEvent()}
- trainPresent = rs.getInput("left")
- if event[1] == "monitor_touch" and not trainPresent then
- attachedMonitor.clear()
- attachedMonitor.setCursorPos(1,1)
- attachedMonitor.write("> Requesting train.")
- HQ_button.draw(attachedMonitor,true)
- rednet.broadcast("HQ", "dispatch_request")
- id, message = rednet.receive("dispatch_confirmation",5)
- if not (message == nil) then
- attachedMonitor.clear()
- attachedMonitor.setCursorPos(1,1)
- attachedMonitor.write("> Inbound from: "..message)
- else
- attachedMonitor.clear()
- attachedMonitor.setCursorPos(1,1)
- attachedMonitor.write("> No response from depot.")
- end
- HQ_button.draw(attachedMonitor, false)
- elseif event[1] == "rednet_message" and event[4] == "dispatch_request" and trainPresent then
- rednet.broadcast("Desert Station", "dispatch_confirmation")
- rs.setOutput("bottom", true)
- sleep(5)
- rs.setOutput("bottom", true)
- end
- end
- attachedMonitor.setCursorPos(1,9)
- print("Done.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement