Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local chan=39701
- local monitor=peripheral.wrap("monitor_6")
- local modem=peripheral.wrap("left")
- modem.open(chan)
- 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 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.toggle = function ()
- button.isPressed = not button.isPressed
- return button.isPressed
- end
- 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
- return button
- end
- -- Quit button
- local quit=Button(12,7,"Quit")
- quit.startRow=3
- quit.startColumn=38
- quit.backgroundColorNormal=colors.red
- quit.backgroundColorPressed=colors.white
- quit.hasBorder=true
- quit.borderColorNormal=colors.white
- quit.borderColorPressed=colors.red
- quit.textColorNormal=colors.white
- quit.textColorPressed=colors.red
- quit.draw(monitor)
- -- Setting up variables
- local totalData={0,0,0}
- local writeFormat={[1]={"Maximum Energy Stored: "," RF"},[2]={"Energy Stored: "," RF"},[3]={"Energy Percent: ","%"}} -- each record represents a statistic, each statistic includes a label and a unit
- local reporters={}
- local width,height=monitor.getSize()
- -- Preparing the monitor for first use
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- monitor.setTextScale(.5)
- monitor.clear()
- -- Typing the monitor's title and colors
- monitor.setBackgroundColor(colors.purple)
- monitor.setTextColor(colors.yellow)
- monitor.setCursorPos(1,1)
- monitor.write("Energy Cell Management"..string.rep(" ",width-#"Energy Cell Management"))
- -- Resetting the colors
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- while true do
- local totalData={0,0,0}
- local event={os.pullEvent()}
- if event[1]=="monitor_touch" then
- if quit.clicked(event[3],event[4])==true then
- monitor.clear()
- break
- end
- elseif event[1]=="key" and event[2]==keys.q then
- monitor.clear()
- break
- elseif event[1]=="modem_message" and event[3]==chan then
- local data={}
- print(event[5]) -- debug purposes
- for word in string.gmatch(event[5],"[^%s]+") do -- splitting the string into usable data (3 slots)
- table.insert(data,word)
- end
- reporters[data[1]]={[1]=data[2],[2]=data[3],[3]=data[4]} -- data[1] is the reporter's label(and key amongst the reporetrs table), data[2] is energy stored, data[3] is max energy, data[4] is energy percent
- end
- local maxLength=#writeFormat[1][1]
- local currentRow=3
- for key,value in pairs(reporters) do -- going over all reporters (at a random order)
- monitor.setCursorPos(1,currentRow)
- monitor.write(key..": ")
- currentRow=currentRow+1
- for key2,value2 in ipairs(reporters[key]) do -- printing data for each reporter(at a specific order)
- monitor.setCursorPos(3,currentRow)
- monitor.write(string.rep(" ",maxLength-#writeFormat[key2][1])..writeFormat[key2][1]..value2..writeFormat[key2][2])
- totalData[key2]=totalData[key2]+value2
- currentRow=currentRow+1
- end
- currentRow=currentRow+1
- end
- -- Calculating and printing total statistics
- totalData[3]=math.floor(100*(totalData[2]/totalData[1])) -- total energy percent
- monitor.setCursorPos(1,currentRow)
- monitor.write("Total Energy Grid Information: ")
- currentRow=currentRow+1
- for i=1,3 do -- going over each statistic
- monitor.setCursorPos(3,currentRow)
- monitor.write(string.rep(" ",maxLength-#writeFormat[i][1])..writeFormat[i][1]..totalData[i]..writeFormat[i][2])
- currentRow=currentRow+1
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment