jkniest

reactor-room

Apr 15th, 2022 (edited)
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.65 KB | None | 0 0
  1. -- Consts
  2. local NAME = 'Primary Reactor'
  3. local BTN_PADDING = 5
  4. local REDSTONE_SIDE = 'right'
  5.  
  6.  
  7. -- Data
  8. local loadTestRunning = false
  9.  
  10.  
  11. -- Peripherals
  12. local monitor = peripheral.find('monitor')
  13.  
  14. if monitor == nil then
  15.     print('Error: You need to connect a monitor!')
  16.     return
  17. end
  18.  
  19.  
  20. -- Cached properties
  21. local monitorWidth, monitorHeight = monitor.getSize()
  22. local halfWidth = monitorWidth / 2
  23.  
  24.  
  25. -- Generic methods
  26. function resetMonitor()
  27.     monitor.clear()
  28.     monitor.setCursorPos(0, 0)
  29. end
  30.  
  31. function drawButton(label, y, color, parent)
  32.     local parentWidth = parent.getSize()
  33.  
  34.     local button = window.create(parent, BTN_PADDING, y, parentWidth - BTN_PADDING * 2, 3)
  35.     button.setBackgroundColor(color)
  36.     button.clear()
  37.  
  38.     local btnWidth = parentWidth - 10;
  39.     button.setCursorPos(1, 2)
  40.  
  41.     local remainingWidth = btnWidth - label:len()
  42.     button.write(string.rep(' ', remainingWidth / 2) .. label .. string.rep(' ', remainingWidth / 2))
  43.  
  44.     button.redraw();
  45. end
  46.  
  47.  
  48. -- Main logic
  49.  
  50. function drawLoop()
  51.     resetMonitor()
  52.    
  53.    
  54.     --
  55.     -- First half  -> Reactor information
  56.     --
  57.     local leftHalf = window.create(monitor, 1, 1, halfWidth, monitorHeight)
  58.     leftHalf.setBackgroundColor(colors.pink)
  59.     leftHalf.clear()
  60.    
  61.     -- Draw the reactor name
  62.     leftHalf.setTextColor(colors.black)
  63.     leftHalf.setCursorPos(
  64.         halfWidth / 2 - NAME:len() / 2,
  65.         2
  66.     )
  67.     leftHalf.write(NAME)
  68.    
  69.     if loadTestRunning then
  70.         -- Draw a button to stop the load test
  71.         drawButton('Stop load test', 5, colors.red, leftHalf)
  72.     else
  73.         -- Draw a button to start the load test
  74.         drawButton('Start load test', 5, colors.green, leftHalf)
  75.     end
  76.    
  77.    
  78.     leftHalf.redraw()
  79.    
  80.    
  81.     --
  82.     -- Second half -> Generic base energy information (todo)
  83.     --
  84.     local rightHalf = window.create(monitor, halfWidth + 2, 1, halfWidth, monitorHeight)
  85.     rightHalf.setBackgroundColor(colors.blue)
  86.     rightHalf.clear()
  87.     rightHalf.redraw()    
  88. end
  89.  
  90. function checkClick(x, y)
  91.     -- Check if the click is the start / stop load test button
  92.     if x >= BTN_PADDING and x < BTN_PADDING + halfWidth - BTN_PADDING * 2
  93.     and y >= 5 and y < 8
  94.      then
  95.         if loadTestRunning then
  96.             redstone.setOutput(REDSTONE_SIDE, false)
  97.             loadTestRunning = false
  98.         else
  99.             redstone.setOutput(REDSTONE_SIDE, true)
  100.             loadTestRunning = true
  101.         end
  102.     end
  103. end
  104.  
  105. redstone.setOutput(REDSTONE_SIDE, false)
  106.  
  107. while true do
  108.     drawLoop()
  109.  
  110.     local _, _, x, y = os.pullEvent('monitor_touch')
  111.     checkClick(x, y)
  112. end
  113.  
Advertisement
Add Comment
Please, Sign In to add comment