Advertisement
jig487

programMenuGUI

Mar 15th, 2024 (edited)
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.80 KB | None | 0 0
  1. local quarryMiner = require("quarryMiner")
  2. local stripMiner = require("stripMiner")
  3.  
  4. --##Key grabber##
  5. --local evt, key = os.pullEvent("key")
  6.  
  7. local titleColor = colors.lightBlue
  8. local menuColor = colors.purple
  9. local textColor = colors.white
  10. local highlightColor = colors.pink
  11.  
  12. local menuColors = {
  13.     title = titleColor,
  14.     menu = menuColor,
  15.     text = textColor,
  16.     highlight = highlightColor
  17. }
  18.  
  19. --(menu = array of strings, colorList = array of named colors)
  20. --returns: cursor as int, item name as string, bool as true if user chose option/ false if user wants to exit
  21. local function openMenu(menu,colorList)
  22.     local col = term.getTextColor()
  23.     local cursor = 1
  24.     term.clear()
  25.     while true do
  26.         term.setCursorPos(1,1)
  27.         term.setTextColor(colorList.menu)
  28.         print("Use the arrow keys to navigate")
  29.         print("Press 'enter' to select, 'backspace' to exit")
  30.         if menu.name then
  31.             term.setTextColor(colorList.title)
  32.             print("<<< "..menu.name.." >>>")
  33.         end
  34.         term.setCursorPos(1,5)
  35.         for i = 1, #menu do
  36.             if cursor == i then
  37.                 term.setTextColor(colorList.highlight)
  38.                 print(">"..i.."< "..menu[i])
  39.             else
  40.                 term.setTextColor(colorList.text)
  41.                 print(" "..i.."  "..menu[i])
  42.             end
  43.         end
  44.  
  45.         local evt, key = os.pullEvent("key")
  46.         key = keys.getName(key)
  47.         if key == "up" and cursor > 1 then
  48.             cursor = cursor - 1
  49.         elseif key == "down" and cursor < #menu then
  50.             cursor = cursor + 1
  51.         --elseif key == "left" then
  52.         --elseif key == "right" then
  53.         elseif key == "enter" then
  54.             term.setTextColor(col)
  55.             return cursor, menu[cursor], true
  56.         elseif key == "backspace" then
  57.             term.setTextColor(col)
  58.             return cursor, menu[cursor], false
  59.         end
  60.     end
  61. end
  62.  
  63. local function openNumberMenu(menu,colorList,numberList)
  64.     --numberList = numberList or {}
  65.     if numberList == nil then
  66.         numberList = {}
  67.         for i = 1, #menu do
  68.             numberList[i] = 0
  69.         end
  70.     end
  71.  
  72.     local col = term.getTextColor()
  73.     local cursor = 1
  74.     --term.clear()
  75.     while true do
  76.         term.clear()
  77.         term.setCursorPos(1,1)
  78.         term.setTextColor(colorList.menu)
  79.         print("Use the arrow keys to navigate")
  80.         print("Press 'enter' to select, 'backspace' to exit")
  81.         if menu.name then
  82.             term.setTextColor(colorList.title)
  83.             print("<<< "..menu.name.." >>>")
  84.         end
  85.         term.setCursorPos(1,5)
  86.         for i = 1, #menu do
  87.             if cursor == i then
  88.                 term.setTextColor(colorList.highlight)
  89.                 print(">"..i.."< "..menu[i]..": "..numberList[i])
  90.             else
  91.                 term.setTextColor(colorList.text)
  92.                 print(" "..i.."  "..menu[i]..": "..numberList[i])
  93.             end
  94.         end
  95.  
  96.         local len = #menu+2
  97.         if cursor == len-1 then
  98.             term.setTextColor(colorList.highlight)
  99.             print(">"..(len-1).."< Confirm")
  100.             term.setTextColor(colorList.text)
  101.             print(" "..(len).."  Exit")
  102.         elseif cursor == len then
  103.             term.setTextColor(colorList.text)
  104.             print(" "..(len-1).."  Confirm")
  105.             term.setTextColor(colorList.highlight)
  106.             print(">"..(len).."< Exit")
  107.         else
  108.             term.setTextColor(colorList.text)
  109.             print(" "..(len-1).."  Confirm")
  110.             print(" "..(len).."  Exit")
  111.         end
  112.  
  113.         local evt, key = os.pullEvent("key")
  114.         key = keys.getName(key)
  115.         if key == "up" and cursor > 1 then
  116.             cursor = cursor - 1
  117.         elseif key == "down" and cursor < len then
  118.             cursor = cursor + 1
  119.         --elseif key == "left" then
  120.         --elseif key == "right" then
  121.         elseif key == "enter" then
  122.             if cursor == len-1 then
  123.                 term.setTextColor(col)
  124.                 return cursor, numberList, true
  125.             elseif cursor == len then
  126.                 term.setTextColor(col)
  127.                 return cursor, numberList, false
  128.             else
  129.                 local lineLen = #("{"..cursor.."} "..menu[cursor]..": ")
  130.                 term.setCursorPos(lineLen+1,cursor+4)
  131.                 term.setTextColor(colorList.menu)
  132.                 numberList[cursor] = tonumber(read(nil,nil,nil,tostring(numberList[cursor])))
  133.                 term.setTextColor(colorList.text)
  134.             end
  135.         elseif key == "backspace" then
  136.             term.setTextColor(col)
  137.             return cursor, numberList, false
  138.         end
  139.     end
  140. end
  141.  
  142. --######### Main Program Start ##############
  143.  
  144. local menu = {name = "Main Menu", "Quarry Miner", "Strip Miner", "Refuel"}
  145.  
  146. local quarryMenu = {name = "Quarry Miner", "X (right)", "Y (forward)", "Z (depth)"}
  147. local quarryNums = {0,0,0}
  148.  
  149. local stripMineMenu = {name = "Strip Miner", "X (columns)", "Y (depth)", "direction (0=right, 1=left)"}
  150. local stripMineNums = {0,0,0}
  151.  
  152. while true do
  153.     local cursor,_,_ = openMenu(menu,menuColors)
  154.     term.clear()
  155.  
  156.     if cursor == 1 then
  157.         --quarry miner
  158.         _,quarryNums,val = openNumberMenu(quarryMenu,menuColors,quarryNums)
  159.         if val == true then
  160.             quarryMiner.run(quarryNums[1],quarryNums[2],quarryNums[3])
  161.         end
  162.     elseif cursor == 2 then
  163.         --strip miner
  164.         _,stripMineNums,val = openNumberMenu(stripMineMenu,menuColors,stripMineNums)
  165.         if val == true then
  166.             stripMiner.run(stripMineNums[1],stripMineNums[2],stripMineNums[3])
  167.         end
  168.     elseif cursor == 3 then
  169.         --refuel
  170.         for i = 1, 16 do
  171.             turtle.select(i)
  172.             turtle.refuel()
  173.         end
  174.     end
  175.     term.clear()
  176. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement