Advertisement
The3vilM0nk3y

monitorManager.lua

Aug 13th, 2022 (edited)
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.17 KB | None | 0 0
  1. --mData = {
  2.   -- {
  3.   --   peripheral = {},
  4.   --   name = "",
  5.   --   buttons = {
  6.   --     {x1=0, x2 = 0, y = 0},
  7.   --     {x1=0, x2 = 0, y = 0},
  8.   --     {x1=0, x2 = 0, y = 0},
  9.   --     {x1=0, x2 = 0, y = 0}
  10.   --   }
  11.   -- }
  12. --}
  13. -- mConfig = {
  14. --   {true, true, true, true, false},
  15. -- }
  16. -- mConfig = {}
  17. -- mData = {}
  18. -- m= {peripheral.find("monitor")}
  19. --print(#m)
  20. function init()
  21.   print("Initializing monManager")
  22.   mData = {}
  23.   if fs.exists("monitorConfig.lua") then
  24.     mConfig = loadMConfig()
  25.   end
  26.   checkMConfig()
  27.   print("Config confirmed")
  28.   for i=1, #m do
  29.     tData = {peripheral = m[i], name = peripheral.getName(m[i]), buttons = {}}
  30.     m[i].clear()
  31.     m[i].setCursorPos(1,1)
  32.     --m[i].setTextScale(.5)
  33.     print("Adding " .. tData.name)
  34.     table.insert(mData,tData)
  35.   end
  36. end
  37. function loadMConfig()
  38.  
  39.   file = fs.open("monitorConfig.lua","r")
  40.   content = file.readAll()
  41.   file.close()
  42.   return textutils.unserialize(content)
  43. end
  44. function saveMConfig()
  45.   file = fs.open("monitorConfig.lua","w")
  46.   file.write(textutils.serialize(mConfig))
  47.   file.close()
  48. end
  49. function checkMConfig()
  50.   for k,v in pairs(m) do
  51.     if mConfig[k] == nil then
  52.       mConfig[k] = {true, true, true, true, false}
  53.     end
  54.   end
  55.   saveMConfig()
  56. end
  57. function showButtons(dIndex)
  58.   mData[dIndex].buttons = {}
  59.   mon = mData[dIndex].peripheral
  60.   --buttons = {"Colony", "Requests", "Queue", "Research", "Citizens"}
  61.   buttons = {"Col", "Req", "Que", "Res", "Cit"}
  62.   --get size to try to space evenly
  63.   x,y = mon.getSize()
  64.   tLength = (#buttons * 4) - 1
  65.   remainingSpace = x - tLength
  66.   extraSpacing = remainingSpace / #buttons + 1
  67.   floatingSpacing = remainingSpace % #buttons + 1
  68.   --print("Available Width - " .. x .. "\nTotal Length - " .. tLength .. "\nRemaining Space - ".. remainingSpace .. "\nExtraSpacing - ".. extraSpacing .. "\nFloating Spacing - ".. floatingSpacing)
  69.   --check if we can add extra spacing
  70.   if extraSpacing >= 1 then
  71.     --Add in extra spacing
  72.   end
  73.   _,by = mon.getCursorPos()
  74.   lastX = 1 + math.floor(floatingSpacing/2);
  75.   for k,v in pairs(buttons) do
  76.     --add an extra space if there is a remainder left
  77.     xSpace = math.floor(extraSpacing)
  78.     bx = lastX + xSpace
  79.     -- add one here to add a space between ones in the middle
  80.     lastX = bx + 1
  81.     --push button's coords to data structure
  82.     table.insert(mData[dIndex].buttons,{x1 = bx, x2 = bx+2, y=by})
  83.     --set colors based on current value
  84.     tc = colors.white
  85.     bc = colors.red
  86.     if mConfig[dIndex][k] == true then
  87.       tc = colors.white
  88.       bc = colors.green
  89.     end
  90.     --print the button to the monitor
  91.     mon.setCursorPos(bx, by)
  92.     mon.setBackgroundColor(bc)
  93.     mon.setTextColor(tc)
  94.     mon.write(v)
  95.   end
  96.   mon.setBackgroundColor(colors.black)
  97.   mon.setTextColor(colors.white)
  98.   mon.write(v)
  99. end
  100. function getButton(mon,x,y)
  101.   for k,v in pairs(mon.buttons) do
  102.     if y == v.y and x >= v.x1 and x <= v.x2 then
  103.       return k
  104.     end
  105.   end
  106.   return 0
  107. end
  108. function getMonByName(n)
  109.   for k,v in pairs(mData) do
  110.     if v.name == n then
  111.       return k
  112.     end
  113.   end
  114. end
  115. function getButtonClick()
  116.   timerId = os.startTimer(10)
  117.   while true do
  118.     event, p1, x, y = os.pullEvent()
  119.     if event == "monitor_touch" then
  120.       --check if there is a button at that location
  121.       mon = getMonByName(p1)
  122.       bIndex = getButton(mData[mon],x,y)
  123.       if bIndex ~= 0 then
  124.         --toggleSection for this monitor based on button clicked
  125.         mConfig[mon][bIndex] = not mConfig[mon][bIndex]
  126.         saveMConfig()
  127.         return true, "Set - " .. bIndex .. " to " .. tostring(mConfig[mon][bIndex])
  128.         --break
  129.       end
  130.     elseif event == "timer" then
  131.       --check if we have the timer event
  132.       if p1 == timerId then
  133.         return true, "timer"
  134.         --break
  135.       end
  136.     end
  137.   end
  138. end
  139. function main()
  140.   init()
  141.   --print(textutils.serialize(mData))
  142.   foundButton = false
  143.   e= ""
  144.   while true do
  145.     showButtons(1)
  146.     foundButton, e = getButtonClick()
  147.     print("result - " .. e)
  148.   end
  149. end
  150. --main()
  151. --parallel.waitForAny(doRequests, handleMonitors)
  152. return {getButtonClick = getButtonClick, init = init, showButtons = showButtons}
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement