Advertisement
kaibochan

Elements.lua

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