Advertisement
kaibochan

Elements.lua

Feb 22nd, 2025 (edited)
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.28 KB | None | 0 0
  1. local gui = require("/apis/gui")
  2. local Instruments = require("Instrumentation")
  3.  
  4. function createActiveButton(display, reactor)
  5.     local display_width, display_height = display.window:getSize()
  6.  
  7.     local activity_button = gui.Text:new {
  8.         x = display_width - 9,
  9.         y = 1,
  10.         width = 8,
  11.         height = 1,
  12.     }
  13.     display.window:addElement(activity_button)
  14.  
  15.     local function update(active)
  16.         activity_button:setText(active and " active " or "inactive")
  17.         activity_button:setBGColor(active and colors.green or colors.red)
  18.     end
  19.     function activity_button:monitor_touch(e)
  20.         if e.display ~= peripheral.getName(display.device) then
  21.             return
  22.         end
  23.  
  24.         local active = reactor.active()
  25.         if active then
  26.             reactor.setActive(false)
  27.         else
  28.             reactor.setActive(true)
  29.         end
  30.         update(reactor.active())
  31.     end
  32.  
  33.     update(reactor.active())
  34.     return activity_button
  35. end
  36.  
  37. function createDebugLog(display, reactor)
  38.     local display_width, display_height = display.window:getSize()
  39.  
  40.     local debug_log = gui.Text:new {
  41.         x = 1,
  42.         y = 1,
  43.         width = display_width - 11,
  44.         height = display_height - 2,
  45.         bg_color = colors.white,
  46.         text_color = colors.black,
  47.         auto_scroll = true,
  48.     }
  49.     display.window:addElement(debug_log)
  50.  
  51.     debug_log.logging = false
  52.     local function log()
  53.         local status = Instruments.CreateReactorStatus(reactor)
  54.         debug_log:write(
  55.            "[" .. os.clock() .. "]\n"
  56.             .. "Reactor Status: " .. (status.activity and "Active" or "Not Active")
  57.             .. ", " .. status.fuel .. " mB"
  58.             .. ", " .. status.waste .. " mB"
  59.             .. ", " .. status.power_stored .. " RF"
  60.             .. "\n")
  61.  
  62.         -- set timer until next log
  63.         local time_to_wait = 1
  64.         local timer_id = os.startTimer(time_to_wait)
  65.         debug_log.timer = function(self, e)
  66.             if e.id == timer_id then
  67.                 if debug_log.logging then
  68.                     log()
  69.                 else
  70.                     os.cancelTimer(timer_id)
  71.                 end
  72.             end
  73.         end
  74.     end
  75.  
  76.     function debug_log.startLogging()
  77.         debug_log.logging = true
  78.         log()
  79.     end
  80.  
  81.     function debug_log.endLogging()
  82.         debug_log.logging = false
  83.     end
  84.  
  85.     return debug_log
  86. end
  87.  
  88. function createLogButton(display, debug_log)
  89.     local display_width, display_height = display.window:getSize()
  90.  
  91.     -- [log: on ]
  92.     -- [log: off]
  93.     local log_button = gui.Text:new {
  94.         x = display_width - 9,
  95.         y = 1,
  96.         width = 8,
  97.         height = 1,
  98.     }
  99.     display.window:addElement(log_button)
  100.  
  101.     local function update()
  102.         log_button:setText("log: " .. (debug_log.logging and "on" or "off"))
  103.         log_button:setBGColor(debug_log.logging and colors.green or colors.red)
  104.     end
  105.  
  106.     function log_button:monitor_touch(e)
  107.         if e.display ~= peripheral.getName(display.device) then
  108.             return
  109.         end
  110.  
  111.         if not debug_log.logging then
  112.             debug_log:startLogging()
  113.         else
  114.             debug_log:endLogging()
  115.         end
  116.  
  117.         update()
  118.     end
  119.  
  120.     update()
  121.     return log_button
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement