April_The_Sergal

buttonManager

Nov 25th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.43 KB | Gaming | 0 0
  1. local buttonMap = {}
  2.  
  3. local function drawButtons(monitor, nodes)
  4.     monitor.clear()
  5.     monitor.setCursorPos(1, 1)
  6.     monitor.write("Mob Farm Controller")
  7.     buttonMap = {}
  8.  
  9.     local sortedKeys = {}
  10.     for key in pairs(nodes) do table.insert(sortedKeys, key) end
  11.     table.sort(sortedKeys)
  12.  
  13.     local x, y = 2, 2
  14.     for _, hostName in ipairs(sortedKeys) do
  15.         local node = nodes[hostName]
  16.         local label = string.format("%s FARM %d (%s)", node.type, node.id, node.status)
  17.         monitor.setCursorPos(x, y)
  18.         monitor.write(label)
  19.  
  20.         -- Toggle button
  21.         table.insert(buttonMap, {
  22.             x1 = x + #label + 2,
  23.             y1 = y,
  24.             x2 = x + #label + 6,
  25.             y2 = y,
  26.             action = function()
  27.                 print("Toggling node: " .. hostName)
  28.                 rednet.send(nodeIDs[hostName], { type = "toggle" }, PROTOCOL)
  29.                 node.status = "PENDING"
  30.             end
  31.         })
  32.  
  33.         y = y + 2
  34.     end
  35. end
  36.  
  37. local function handleMouseClicks()
  38.     while true do
  39.         local _, _, x, y = os.pullEvent("monitor_touch")
  40.         for _, button in ipairs(buttonMap) do
  41.             if x >= button.x1 and x <= button.x2 and y >= button.y1 and y <= button.y2 then
  42.                 button.action()
  43.                 break
  44.             end
  45.         end
  46.     end
  47. end
  48.  
  49. return {
  50.     drawButtons = drawButtons,
  51.     handleMouseClicks = handleMouseClicks,
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment