JustDoesGames

MenuAPI

Mar 14th, 2019
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.13 KB | None | 0 0
  1. -- MenuAPI --
  2.  
  3. local clr = function() term.clear() end
  4. local setText = function(col) term.setTextColor(colors[col]) end
  5. local setBack = function(col) term.setBackgroundColor(colors[col]) end
  6. local selector = {"[","]"}
  7. local selectorColor = "white"
  8. local selectedColor = "white"
  9. local l_color = {"white","orange","magenta","lightBlue","yellow","lime","pink","gray","lightGray","cyan","purple","blue","brown","green","red","black"}
  10. local upd = true
  11. local sel = 1
  12. version = 1
  13.  
  14. function setSelector(x,y)
  15.     selector = {x,y}
  16. end
  17.  
  18. function setSelectorColor(col)
  19.     if type(col) ~= "string" then error("Expected String, got "..type(col)) end
  20.     local tmp = false
  21.     for i=1, #l_color do
  22.         if col == l_color[i] then tmp = true end
  23.     end
  24.     if tmp == false then
  25.         error("Expect a Color, got "..type(col))
  26.     end
  27.     selectorColor = col
  28. end
  29.  
  30. function setSelectedColor(col)
  31.     if type(col) ~= "string" then error("Expected String, got "..type(col)) end
  32.     local tmp = false
  33.     for i=1, #l_color do
  34.         if col == l_color[i] then tmp = true end
  35.     end
  36.     if tmp == false then
  37.         error("Expect a Color, got "..type(col))
  38.     end
  39.     selectedColor = col
  40. end
  41.  
  42. function resetPosition(bool)
  43.     if type(bool) == "boolean" then
  44.         setpos = bool
  45.     else
  46.         error("Expected boolean, got "..bool)
  47.     end
  48. end
  49.  
  50. function run(menu)
  51.     if type(menu) ~= "table" then
  52.         error("Expected Table, Got "..type(menu))
  53.     end
  54.     local running = true
  55.     if setpos == nil or setpos == true then
  56.         sel = 1
  57.     end
  58.     upd = true
  59.     while running do
  60.         if upd == true then
  61.             clr()
  62.             term.setCursorPos(1,1)
  63.             for i=1, #menu do
  64.                 if sel == i then
  65.                     setText(selectorColor)
  66.                     write(selector[1].." ")
  67.                     setText(selectedColor)
  68.                     write(menu[i].." ")
  69.                     setText(selectorColor)
  70.                     print(selector[2].." ")
  71.                 else
  72.                     print("  "..menu[i].."  ")
  73.                 end
  74.             end
  75.             upd = false
  76.         end
  77.         a,i = os.pullEvent("key")
  78.         if i == keys.w or i == keys.up then
  79.             if sel ~= 1 then sel = sel - 1 upd = true end
  80.         elseif i == keys.s or i == keys.down then
  81.             if sel ~= #menu then sel = sel + 1 upd = true end
  82.         elseif i == keys.e or i == keys.enter then
  83.             running = false
  84.         end
  85.     end
  86.     return sel
  87. end
Advertisement
Add Comment
Please, Sign In to add comment