Advertisement
Theshadow989

Simple Menu System

May 3rd, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.71 KB | None | 0 0
  1. --[[
  2. Menu File
  3. Written by TheShadow989
  4. ]]--
  5.  
  6. local menu_options =
  7. {
  8.     [1] = {text="Command ", yMenu=1, xMenu=1, yPos=8, xPos=9, color=colors.yellow},
  9.     [2] = {text="Programs", yMenu=2, xMenu=1, yPos=9, xPos=9, color=colors.yellow},
  10.     [3] = {text="Accounts", yMenu=3, xMenu=1, yPos=10, xPos=9, color=colors.yellow},
  11.     [4] = {text="Details ", yMenu=4, xMenu=1, yPos=11, xPos=9, color=colors.yellow},
  12.     [5] = {text="Shutdown", yMenu=5, xMenu=1, yPos=12, xPos=9, color=colors.red},
  13.     [6] = {text="Settings", yMenu=1, xMenu=2, yPos=8, xPos=21, color=colors.yellow},
  14.     [7] = {text="Option2 ", yMenu=2, xMenu=2, yPos=9, xPos=21, color=colors.yellow},
  15.     [8] = {text="Option3 ", yMenu=1, xMenu=3, yPos=8, xPos=33, color=colors.yellow},
  16.     [9] = {text="Option4 ", yMenu=2, xMenu=3, yPos=9, xPos=33, color=colors.yellow},
  17.     [10] = {text="Option5 ", yMenu=3, xMenu=3, yPos=10, xPos=33, color=colors.yellow},
  18. }
  19.  
  20. local function menuDraw(yyMenu, xxMenu)
  21.    
  22.     for index, data in pairs(menu_options) do
  23.         term.setCursorPos(data.xPos, data.yPos)
  24.  
  25.         menu_options[index].bounds =
  26.         {
  27.             x1 = data.xPos;
  28.             x2 = #data.text+data.xPos+3;
  29.             y = data.yPos;
  30.         }
  31.        
  32.         if data.yMenu == yyMenu and data.xMenu == xxMenu then
  33.             term.setTextColor(data.color)
  34.             text = "[ "..data.text.." ]"
  35.         else
  36.             term.setTextColor(colors.white)
  37.             text = "  "..data.text.."  "
  38.         end
  39.         term.write(text)
  40.     end
  41. end
  42.  
  43. local function checkClick(x,y)
  44.     for index, data in pairs(menu_options) do
  45.         if x>data.bounds.x1 and x<data.bounds.x2 and y==data.bounds.y then
  46.             return data.yMenu, data.xMenu
  47.         end
  48.     end
  49.     return false, false
  50. end
  51.  
  52. term.clear()
  53. local yyMenu, xxMenu = 1,1
  54.  
  55. while true do
  56.     menuDraw(yyMenu, xxMenu)
  57.     local e = {os.pullEvent()}
  58.  
  59.     if e[1] == "key" then --dont need this if you only use keyboard controls
  60.         if e[2] == keys.down then
  61.             for index, data in pairs(menu_options) do
  62.                 if data.yMenu == yyMenu+1 and data.xMenu == xxMenu then
  63.                     yyMenu = yyMenu+1
  64.                     break
  65.                 end
  66.             end
  67.  
  68.         elseif e[2] == keys.up then
  69.             for index, data in pairs(menu_options) do
  70.                 if data.yMenu == yyMenu-1 and data.xMenu == xxMenu then
  71.                     yyMenu = yyMenu-1
  72.                     break
  73.                 end
  74.             end
  75.            
  76.         elseif e[2] == keys.right then
  77.             for index, data in pairs(menu_options) do
  78.                 if data.yMenu == yyMenu and data.xMenu == xxMenu+1 then
  79.                     xxMenu = xxMenu+1
  80.                     break
  81.                 end
  82.             end
  83.  
  84.         elseif e[2] == keys.left then
  85.             for index, data in pairs(menu_options) do
  86.                 if data.yMenu == yyMenu and data.xMenu == xxMenu-1 then
  87.                     xxMenu = xxMenu-1
  88.                     break
  89.                 end
  90.             end
  91.  
  92.         elseif e[2] == keys.enter then
  93.             break
  94.         end
  95.     elseif e[1] == "mouse_click" then
  96.         yyMenuclick, xxMenuclick = checkClick(e[3], e[4]) --Check the mouse click
  97.         if yyMenuclick and xxMenuclick then --If checkClick returns a value and not false
  98.             yyMenu = yyMenuclick
  99.             xxMenu = xxMenuclick
  100.             break
  101.         end
  102.     end
  103. end
  104.  
  105. if yyMenu == 1 and xxMenu == 1 then
  106.     print("Command Was Selected")
  107. end
  108. if yyMenu == 3 and xxMenu == 1 then
  109.     print("Accounts Was Selected")
  110. end
  111. if yyMenu == 2 and xxMenu == 2 then
  112.     print("Option2 Was Selected")
  113. end
  114. if yyMenu == 1 and xxMenu == 2 then
  115.     print("Settings Was Selected")
  116. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement