Theshadow989

Test File (passing variables part 2)

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