Advertisement
Theshadow989

Advanced Menu System

May 3rd, 2017
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.13 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=3, color=colors.yellow},
  9.     [2] = {text="Programs", yMenu=2, xMenu=1, yPos=9, xPos=3, color=colors.yellow},
  10.     [3] = {text="Accounts", yMenu=3, xMenu=1, yPos=10, xPos=3, color=colors.yellow},
  11.     [4] = {text="Details ", yMenu=4, xMenu=1, yPos=11, xPos=3, color=colors.yellow},
  12.     [5] = {text="Shutdown", yMenu=5, xMenu=1, yPos=12, xPos=3, color=colors.red},
  13.     [6] = {text="Settings", yMenu=1, xMenu=2, yPos=8, xPos=15, color=colors.yellow},
  14.     [7] = {text="Scripts ", yMenu=2, xMenu=2, yPos=9, xPos=15, color=colors.yellow},
  15.     [8] = {text="Attacks ", yMenu=1, xMenu=3, yPos=8, xPos=27, color=colors.yellow},
  16.     [9] = {text="Internet", yMenu=2, xMenu=3, yPos=9, xPos=27, color=colors.yellow},
  17.     [10] = {text="ServerFr", yMenu=3, xMenu=3, yPos=10, xPos=27, color=colors.yellow},
  18.     [11] = {text="  Back  ", yMenu=99, xMenu=0, yPos=14, xPos=15, color=colors.blue},
  19.     [12] = {text="  Back  ", yMenu=0, xMenu=99, yPos=9, xPos=39, color=colors.blue},
  20. }
  21.  
  22. local function menuDraw(yyMenu, xxMenu)
  23.    
  24.     for index, data in pairs(menu_options) do
  25.         term.setCursorPos(data.xPos, data.yPos)
  26.  
  27.          menu_options[index].bounds =
  28.         {
  29.             x1 = data.xPos;
  30.             x2 = #data.text+data.xPos+3;
  31.             y = data.yPos;
  32.         }
  33.        
  34.         if data.yMenu == yyMenu and data.xMenu == xxMenu then
  35.             term.setTextColor(data.color)
  36.             text = "[ "..data.text.." ]"
  37.         else
  38.             text = "  "..data.text.."  "
  39.         end
  40.         term.write(text)
  41.         term.setTextColor(colors.white)
  42.     end
  43. end
  44.  
  45. local function checkClick(x,y)
  46.     for index, data in pairs(menu_options) do
  47.         if x>data.bounds.x1 and x<data.bounds.x2 and y==data.bounds.y then
  48.             return data.yMenu, data.xMenu
  49.         end
  50.     end
  51.     return false, false
  52. end
  53.  
  54. term.clear()
  55. local yyMenu, xxMenu = 1,1
  56. local btnbacktoggle, btnbacktrue = false, false
  57. local xxxMenu, yyyMenu = 0, 0
  58.  
  59. while true do
  60.     menuDraw(yyMenu, xxMenu)
  61.     local e = {os.pullEvent()}
  62.     local limit, found = true, false
  63.     local yMenulimit, xMenulimit = yyMenu, xxMenu
  64.  
  65.     if e[1] == "key" then --dont need this if you only use keyboard controls
  66.         if e[2] == keys.down then
  67.             for index, data in pairs(menu_options) do
  68.                 if data.xMenu == 0 and data.yMenu == 99 then
  69.                     btnbacktrue = true
  70.                 end
  71.                 if data.xMenu == xxMenu then
  72.                     if found == false and data.yMenu > yyMenu then
  73.                         for i=1, 25, 1 do
  74.                             if data.yMenu == yyMenu+i then
  75.                                 yyMenu = yyMenu+i
  76.                                 limit = false
  77.                                 found = true
  78.                                 break
  79.                             end
  80.                         end
  81.                     end
  82.                     if data.yMenu < yMenulimit then
  83.                         yMenulimit = data.yMenu
  84.                     end
  85.                 end
  86.             end
  87.             if limit == true then
  88.                 if btnbacktoggle == false and btnbacktrue == true then
  89.                     btnbacktoggle = true
  90.                     xxxMenu, yyyMenu = xxMenu, yMenulimit
  91.                     yyMenu = 99
  92.                     xxMenu = 0
  93.                 elseif btnbacktoggle == true and btnbacktrue == true then
  94.                     btnbacktoggle = false
  95.                     xxMenu = xxxMenu
  96.                     yyMenu = yyyMenu
  97.                 else
  98.                     yyMenu = yMenulimit
  99.                 end
  100.             end
  101.  
  102.         elseif e[2] == keys.up then
  103.             for index, data in pairs(menu_options) do
  104.                 if data.xMenu == 0 and data.yMenu == 99 then
  105.                     btnbacktrue = true
  106.                 end
  107.                 if data.xMenu == xxMenu then
  108.                     if data.yMenu < yyMenu then
  109.                         for i=1, 25, 1 do
  110.                             if data.yMenu == yyMenu-i then
  111.                                 f=i
  112.                                 limit = false
  113.                                 break
  114.                             end
  115.                         end
  116.                     end
  117.                     if data.yMenu > yMenulimit then
  118.                         yMenulimit = data.yMenu
  119.                     end
  120.                 end
  121.             end
  122.             if limit == true then
  123.                 if btnbacktoggle == false and btnbacktrue == true then
  124.                     btnbacktoggle = true
  125.                     xxxMenu, yyyMenu = xxMenu, yMenulimit
  126.                     yyMenu = 99
  127.                     xxMenu = 0
  128.                 elseif btnbacktoggle == true and btnbacktrue == true then
  129.                     btnbacktoggle = false
  130.                     xxMenu = xxxMenu
  131.                     yyMenu = yyyMenu
  132.                 else
  133.                     yyMenu = yMenulimit
  134.                 end
  135.             else
  136.                 yyMenu = yyMenu-f
  137.             end
  138.            
  139.         elseif e[2] == keys.right then
  140.             for index, data in pairs(menu_options) do
  141.                 if data.yMenu == yyMenu then
  142.                     if found == false and data.xMenu > xxMenu then
  143.                         for i=1, 25, 1 do
  144.                             if data.xMenu == xxMenu+i then
  145.                                 xxMenu = xxMenu+i
  146.                                 limit = false
  147.                                 found = true
  148.                                 break
  149.                             end
  150.                         end
  151.                     end
  152.                     if data.xMenu < xMenulimit then
  153.                         xMenulimit = data.xMenu
  154.                     end
  155.                 end
  156.             end
  157.             if limit == true then
  158.                 xxMenu = xMenulimit
  159.             end
  160.  
  161.         elseif e[2] == keys.left then
  162.             for index, data in pairs(menu_options) do
  163.                 if data.yMenu == yyMenu then
  164.                     if data.xMenu < xxMenu then
  165.                         for i=1, 25, 1 do
  166.                             if data.xMenu == xxMenu-i then
  167.                                 f=i
  168.                                 limit = false
  169.                                 break
  170.                             end
  171.                         end
  172.                     end
  173.                     if data.xMenu > xMenulimit then
  174.                         xMenulimit = data.xMenu
  175.                     end
  176.                 end
  177.             end
  178.             if limit == true then
  179.                 xxMenu = xMenulimit
  180.             else
  181.                 xxMenu = xxMenu-f
  182.             end
  183.  
  184.         elseif e[2] == keys.enter then
  185.             break
  186.         end
  187.     elseif e[1] == "mouse_click" then
  188.         yyMenuclick, xxMenuclick = checkClick(e[3], e[4]) --Check the mouse click
  189.         if yyMenuclick and xxMenuclick then --If checkClick returns a value and not false
  190.             yyMenu = yyMenuclick
  191.             xxMenu = xxMenuclick
  192.             break
  193.         end
  194.     end
  195. end
  196.  
  197. term.clear()
  198. term.setCursorPos(8,9)
  199.  
  200. if yyMenu == 1 and xxMenu == 1 then
  201.     print("You have chosen the 'Command' option.")
  202. elseif yyMenu == 2 and xxMenu == 1 then
  203.     print("You have chosen the 'Programs' option.")
  204. elseif yyMenu == 3 and xxMenu == 1 then
  205.     print("You have chosen the 'Accounts' option.")
  206. elseif yyMenu == 4 and xxMenu == 1 then
  207.     print("You have chosen the 'Details' option.")
  208. elseif yyMenu == 5 and xxMenu == 1 then
  209.     print("You have chosen the 'Shutdown' option.")
  210. elseif yyMenu == 1 and xxMenu == 2 then
  211.     print("You have chosen the 'Settings' option.")
  212. elseif yyMenu == 2 and xxMenu == 2 then
  213.     print("You have chosen the 'Scripts' option.")
  214. elseif yyMenu == 1 and xxMenu == 3 then
  215.     print("You have chosen the 'Attacks' option.")
  216. elseif yyMenu == 2 and xxMenu == 3 then
  217.     print("You have chosen the 'Internet' option.")
  218. elseif yyMenu == 3 and xxMenu == 3 then
  219.     print("You have chosen the 'SeverFr' option.")
  220. elseif yyMenu == 99 and xxMenu == 0 then
  221.     print("You have chosen the 'back' option.")
  222. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement