Maniklas

IEMP prototype

Nov 19th, 2025 (edited)
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.26 KB | None | 0 0
  1. function CenterCursor(mode, monitor)
  2.     if (monitor == nil) then
  3.         local w,h = term.getSize()
  4.         local cw, ch = term.getCursorPos()
  5.         if (mode == "width") then
  6.         term.setCursorPos(w/2, ch)
  7.         elseif (mode == "height") then
  8.             term.setCursorPos(cw, h/2)
  9.         else
  10.             term.setCursorPos(w/2, h/2)
  11.         end
  12.         return term.getCursorPos()
  13.     else
  14.         local w,h = monitor.getSize()
  15.         local cw, ch = monitor.getCursorPos()
  16.         if (mode == "width") then
  17.         monitor.setCursorPos(w/2, ch)
  18.         elseif (mode == "height") then
  19.             monitor.setCursorPos(cw, h/2)
  20.         else
  21.             monitor.setCursorPos(w/2, h/2)
  22.         end
  23.         return monitor.getCursorPos()
  24.     end
  25.  
  26. end
  27.  
  28. function MoveCursor(modw, modh, monitor)
  29.     if (monitor == nil) then
  30.         local cw, ch = term.getCursorPos()
  31.         term.setCursorPos(cw+modw, ch+modh)
  32.     else
  33.         local cw, ch = monitor.getCursorPos()
  34.         monitor.setCursorPos(cw+modw, ch+modh)
  35.     end
  36. end
  37.  
  38. function WriteCentered(text, mode, monitor)
  39.     if (monitor == nil) then
  40.         local modif = #text/2
  41.         CenterCursor(mode)
  42.         MoveCursor(0-modif, 0)
  43.         write(text)
  44.     else
  45.         local modif = #text/2
  46.         CenterCursor(mode, monitor)
  47.         MoveCursor(0-modif, 0, monitor)
  48.         monitor.write(text)
  49.     end
  50. end
  51.  
  52. function PrintCentered(text, mode, monitor)
  53.     if (monitor == nil) then
  54.         local modif = #text/2
  55.         CenterCursor(mode)
  56.         MoveCursor(0-modif, 0)
  57.         print(text)
  58.     else
  59.         local modif = #text/2
  60.         CenterCursor(mode, monitor)
  61.         MoveCursor(0-modif, 0, monitor)
  62.         monitor.write(text .. "\n")
  63.     end
  64. end
  65.  
  66. -- program main functions
  67.  
  68. function RenderMainMenu(monitor)
  69.     if (monitor == nil) then
  70.         term.clear()
  71.         term.setCursorPos(0,0)
  72.         CenterCursor()
  73.         MoveCursor(0, -2)
  74.         WriteCentered("Continue", "width")
  75.         MoveCursor(0, 2)
  76.         WriteCentered("Options", "width")
  77.         MoveCursor(0, 2)
  78.         WriteCentered("Exit", "width")
  79.     else
  80.         monitor.clear()
  81.         monitor.setCursorPos(0,0)
  82.         CenterCursor(monitor)
  83.         MoveCursor(0, -2, monitor)
  84.         WriteCentered("Continue", "width", monitor)
  85.         MoveCursor(0, 2, monitor)
  86.         WriteCentered("Options", "width", monitor)
  87.         MoveCursor(0, 2, monitor)
  88.         WriteCentered("Exit", "width", monitor)
  89.     end
  90. end
  91.  
  92. function Setup(monitor)
  93.  
  94.     local config = settings.get("IEMPconfig")
  95.     print("Please click the top left and bottom right corners of each button until they are configured")
  96.     RenderMainMenu(monitor)
  97.  
  98.     print("Please configure \"Continue\"")
  99.     local event, side, x, y = os.pullEvent("monitor_touch")
  100.     config.continueButtonPos[1] = x
  101.     config.continueButtonPos[2] = y
  102.     local event, side, x, y = os.pullEvent("monitor_touch")
  103.     config.continueButtonPos[3] = x
  104.     config.continueButtonPos[4] = y
  105.  
  106.     print("Please configure \"Options\"")
  107.     local event, side, x, y = os.pullEvent("monitor_touch")
  108.     config.optionsButtonPos[1] = x
  109.     config.optionsButtonPos[2] = y
  110.     local event, side, x, y = os.pullEvent("monitor_touch")
  111.     config.optionsButtonPos[3] = x
  112.     config.optionsButtonPos[4] = y
  113.  
  114.     print("Please configure \"Exit\"")
  115.     local event, side, x, y = os.pullEvent("monitor_touch")
  116.     config.exitButtonPos[1] = x
  117.     config.exitButtonPos[2] = y
  118.     local event, side, x, y = os.pullEvent("monitor_touch")
  119.     config.exitButtonPos[3] = x
  120.     config.exitButtonPos[4] = y
  121.  
  122.     settings.set("IEMPconfig", config)
  123.     settings.save()
  124.  
  125.     print("Setup complete!")
  126. end
  127.  
  128. --define settings for program
  129.  
  130. settings.define("IEMPconfig", {
  131.     description = "Configurations for Icons Energy Monitor Program",
  132.     default = {
  133.         newInstall = true,
  134.         continueButtonPos = {0, 0, 0, 0},
  135.         optionsButtonPos = {0, 0, 0, 0},
  136.         exitButtonPos = {0, 0, 0, 0}
  137.     },
  138.     type = "table"
  139. })
  140.  
  141. --program start
  142.  
  143. -- start by finding monitor to use and loading settings as "config"
  144.  
  145. mon = peripheral.find("monitor")
  146. settings.load()
  147. config = settings.get("IEMPconfig")
  148.  
  149. -- if this is the first time the program is launched (or the settings are loaded with the default values for any other reason)
  150.  
  151. if (config.newInstall == true) then
  152.     Setup(mon)
  153.     config = settings.get("IEMPconfig")
  154.     config.newInstall = false
  155.     settings.set("IEMPconfig", config)
  156.     settings.save()
  157. end
  158.  
  159. mon.clear()
  160. WriteCentered("Welcome", "none", mon)
  161. sleep(3)
  162. mon.clear()
  163. sleep(0.5)
  164.  
  165. local loop = true
  166.  
  167. while loop do
  168.     RenderMainMenu(mon)
  169.     local event, side, x, y = os.pullEvent("monitor_touch")
  170.     if (x >= config.continueButtonPos[1] and x <= config.continueButtonPos[2] and y >= config.continueButtonPos[3] and config.continueButtonPos[4]) then
  171.         write("Continue")
  172.     elseif (x >= config.optionsButtonPos[1] and x <= config.optionsButtonPos[2] and y >= config.optionsButtonPos[3] and config.optionsButtonPos[4]) then
  173.         write("Options")
  174.     elseif (x >= config.exitButtonPos[1] and x <= config.exitButtonPos[2] and y >= config.exitButtonPos[3] and config.exitButtonPos[4]) then
  175.         write("Exit")
  176.         loop = false
  177.     end
  178. end
Advertisement
Add Comment
Please, Sign In to add comment