Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --mData = {
- -- {
- -- peripheral = {},
- -- name = "",
- -- buttons = {
- -- {x1=0, x2 = 0, y = 0},
- -- {x1=0, x2 = 0, y = 0},
- -- {x1=0, x2 = 0, y = 0},
- -- {x1=0, x2 = 0, y = 0}
- -- }
- -- }
- --}
- -- mConfig = {
- -- {true, true, true, true, false},
- -- }
- -- mConfig = {}
- -- mData = {}
- -- m= {peripheral.find("monitor")}
- --print(#m)
- function init()
- print("Initializing monManager")
- mData = {}
- if fs.exists("monitorConfig.lua") then
- mConfig = loadMConfig()
- end
- checkMConfig()
- print("Config confirmed")
- for i=1, #m do
- tData = {peripheral = m[i], name = peripheral.getName(m[i]), buttons = {}}
- m[i].clear()
- m[i].setCursorPos(1,1)
- --m[i].setTextScale(.5)
- print("Adding " .. tData.name)
- table.insert(mData,tData)
- end
- end
- function loadMConfig()
- file = fs.open("monitorConfig.lua","r")
- content = file.readAll()
- file.close()
- return textutils.unserialize(content)
- end
- function saveMConfig()
- file = fs.open("monitorConfig.lua","w")
- file.write(textutils.serialize(mConfig))
- file.close()
- end
- function checkMConfig()
- for k,v in pairs(m) do
- if mConfig[k] == nil then
- mConfig[k] = {true, true, true, true, false}
- end
- end
- saveMConfig()
- end
- function showButtons(dIndex)
- mData[dIndex].buttons = {}
- mon = mData[dIndex].peripheral
- --buttons = {"Colony", "Requests", "Queue", "Research", "Citizens"}
- buttons = {"Col", "Req", "Que", "Res", "Cit"}
- --get size to try to space evenly
- x,y = mon.getSize()
- tLength = (#buttons * 4) - 1
- remainingSpace = x - tLength
- extraSpacing = remainingSpace / #buttons + 1
- floatingSpacing = remainingSpace % #buttons + 1
- --print("Available Width - " .. x .. "\nTotal Length - " .. tLength .. "\nRemaining Space - ".. remainingSpace .. "\nExtraSpacing - ".. extraSpacing .. "\nFloating Spacing - ".. floatingSpacing)
- --check if we can add extra spacing
- if extraSpacing >= 1 then
- --Add in extra spacing
- end
- _,by = mon.getCursorPos()
- lastX = 1 + math.floor(floatingSpacing/2);
- for k,v in pairs(buttons) do
- --add an extra space if there is a remainder left
- xSpace = math.floor(extraSpacing)
- bx = lastX + xSpace
- -- add one here to add a space between ones in the middle
- lastX = bx + 1
- --push button's coords to data structure
- table.insert(mData[dIndex].buttons,{x1 = bx, x2 = bx+2, y=by})
- --set colors based on current value
- tc = colors.white
- bc = colors.red
- if mConfig[dIndex][k] == true then
- tc = colors.white
- bc = colors.green
- end
- --print the button to the monitor
- mon.setCursorPos(bx, by)
- mon.setBackgroundColor(bc)
- mon.setTextColor(tc)
- mon.write(v)
- end
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- mon.write(v)
- end
- function getButton(mon,x,y)
- for k,v in pairs(mon.buttons) do
- if y == v.y and x >= v.x1 and x <= v.x2 then
- return k
- end
- end
- return 0
- end
- function getMonByName(n)
- for k,v in pairs(mData) do
- if v.name == n then
- return k
- end
- end
- end
- function getButtonClick()
- timerId = os.startTimer(10)
- while true do
- event, p1, x, y = os.pullEvent()
- if event == "monitor_touch" then
- --check if there is a button at that location
- mon = getMonByName(p1)
- bIndex = getButton(mData[mon],x,y)
- if bIndex ~= 0 then
- --toggleSection for this monitor based on button clicked
- mConfig[mon][bIndex] = not mConfig[mon][bIndex]
- saveMConfig()
- return true, "Set - " .. bIndex .. " to " .. tostring(mConfig[mon][bIndex])
- --break
- end
- elseif event == "timer" then
- --check if we have the timer event
- if p1 == timerId then
- return true, "timer"
- --break
- end
- end
- end
- end
- function main()
- init()
- --print(textutils.serialize(mData))
- foundButton = false
- e= ""
- while true do
- showButtons(1)
- foundButton, e = getButtonClick()
- print("result - " .. e)
- end
- end
- --main()
- --parallel.waitForAny(doRequests, handleMonitors)
- return {getButtonClick = getButtonClick, init = init, showButtons = showButtons}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement