Advertisement
Theshadow989

Reactor Control

Aug 25th, 2021 (edited)
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.21 KB | None | 0 0
  1. -- Reactor name appears center of screen.
  2. local reactorTitle = "Reactor Core #01"
  3.  
  4. -- Reactor menu options
  5. local menu_options =
  6. {
  7.     [1] = {menuOption="Reactor:", option1="  ON  ", option2="  OFF ", yPos=8, option1xPos=22, option2xPos=31, selected=2},
  8.     [2] = {menuOption="Lights:", option1="  ON  ", option2="  OFF ", yPos=9, option1xPos=22, option2xPos=31, selected=2},
  9.     [3] = {menuOption="Info Displays:", option1="  ON  ", option2="  OFF ", yPos=10, option1xPos=22, option2xPos=31, selected=2},
  10.     [4] = {menuOption="Emerg. Flood:", option1="  ON  ", option2="  OFF ", yPos=11, option1xPos=22, option2xPos=31, selected=2},
  11.     [5] = {menuOption="Access Doors:", option1=" OPEN ", option2="CLOSED", yPos=12, option1xPos=22, option2xPos=31, selected=2},
  12.     [6] = {menuOption="", option1="Emergency Shutdown", option2="", yPos=16, option1xPos=17, option2xPos=0, selected=0},
  13. }
  14.  
  15. -- Clear Screen
  16. term.clear()
  17.  
  18. -- FUNCTION (Draw Menu)
  19. local function drawMenu()
  20.     for index, data in pairs(menu_options) do
  21.         term.setCursorPos(data.option1xPos-2-data.menuOption:len(), data.yPos)
  22.         term.setBackgroundColor(colors.black)
  23.         term.write(data.menuOption)
  24.  
  25.         term.setCursorPos(data.option1xPos, data.yPos)
  26.         if data.selected == 1 then
  27.             term.setBackgroundColor(colors.green)
  28.         elseif data.selected == 0 then
  29.             term.setBackgroundColor(colors.red)
  30.         else
  31.             term.setBackgroundColor(colors.gray)
  32.         end
  33.         term.write(data.option1)
  34.  
  35.         term.setCursorPos(data.option2xPos, data.yPos)
  36.         if data.selected == 2 then
  37.             term.setBackgroundColor(colors.red)
  38.         elseif data.selected == 0 then
  39.             term.setBackgroundColor(colors.red)
  40.         else
  41.             term.setBackgroundColor(colors.gray)
  42.         end
  43.         term.write(data.option2)
  44.     end
  45. end
  46.  
  47. drawMenu()
  48.  
  49. -- FUNCTION (Center Text)
  50. local function centerText(text)
  51.     term.setCursorPos(1,3)
  52.     local x,y = term.getSize()
  53.     local x2,y2 = term.getCursorPos()
  54.     term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  55.     term.setBackgroundColor(colors.black)
  56.     write(text)
  57. end
  58.  
  59. centerText(reactorTitle)
  60.  
  61. local function checkClick(x,y)
  62.     for index, data in pairs(menu_options) do
  63.         if x > data.option1xPos and x < data.option1:len() + data.option1xPos and y == data.yPos then
  64.             if data.selected ~= 0 then
  65.                 return index, 1
  66.             else
  67.                 return index, 0
  68.             end
  69.         end
  70.  
  71.         if x > data.option2xPos and x < data.option2:len() + data.option2xPos and y == data.yPos then
  72.             if data.selected ~= 0 then
  73.                 return index, 2
  74.             else
  75.                 return index, 0
  76.             end
  77.         end
  78.     end
  79.     return false, false
  80. end
  81.  
  82. while true do
  83.     local e = {os.pullEvent()}
  84.     local indexSelected, optionSelected = "", ""
  85.  
  86.     if e[1] == "mouse_click" then
  87.         indexSelected, optionSelected = checkClick(e[3], e[4]) --Check the mouse click
  88.  
  89.         if indexSelected ~= false and optionSelected ~= false then
  90.             menu_options[indexSelected].selected = optionSelected
  91.         end
  92.     end
  93.  
  94.     term.setCursorPos(1,19)
  95.     term.setBackgroundColor(colors.black)
  96.    
  97.     if optionSelected == 1 then
  98.         term.write(menu_options[indexSelected].menuOption .. " " .. menu_options[indexSelected].option1 .. "             ")
  99.     elseif optionSelected == 2 then
  100.         term.write(menu_options[indexSelected].menuOption .. " " .. menu_options[indexSelected].option2 .. "             ")
  101.     elseif optionSelected == 0 then
  102.         term.write(menu_options[indexSelected].menuOption .. " " .. menu_options[indexSelected].option1 .. " " .. menu_options[indexSelected].option2 .. "             ")
  103.     end
  104.  
  105.     -- Put button actions below
  106.     if indexSelected == 1 and optionSelected == 1 then     -- Reactor On
  107.        
  108.     elseif indexSelected == 1 and optionSelected == 2 then -- Reactor Off
  109.        
  110.     elseif indexSelected == 2 and optionSelected == 1 then -- Lights On
  111.        
  112.     elseif indexSelected == 2 and optionSelected == 2 then -- Lights Off
  113.        
  114.     elseif indexSelected == 3 and optionSelected == 1 then -- Info Displays ON
  115.        
  116.     elseif indexSelected == 3 and optionSelected == 2 then -- Info Displays OFF
  117.  
  118.     elseif indexSelected == 4 and optionSelected == 1 then -- Access Doors On
  119.        
  120.     elseif indexSelected == 4 and optionSelected == 2 then -- Access Doors Off
  121.  
  122.     elseif indexSelected == 4 and optionSelected == 0 then -- Emergency Shutdown
  123.        
  124.     end
  125.  
  126.     drawMenu()
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement