Advertisement
Vladlen_Info

menu

Apr 23rd, 2018
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.86 KB | None | 0 0
  1. --[[ -- Либа для проги m_file.lua //Библиотека создающая список и стилизованный курсор для выбора//
  2. Файл либы должен возвращать таблицу с созданными функциями:
  3. local liba = {}
  4. function liba.menu()
  5.   ...
  6. end
  7. function liba.test()
  8.   ...
  9. end
  10. return liba
  11. --]]
  12. -- указание локальной переменной local тоже имеет значение
  13.  
  14. local library = {}
  15.  
  16. local component = require("component")
  17. local gpu = component.gpu
  18. local term = require("term")
  19. local w, h = gpu.getResolution()
  20. local event = require("event")
  21.  
  22. function library.mprint (array, this, s1, s2, x, y)
  23.    for i = 1, #array do
  24.       term.setCursor(x, y)
  25.       --Check if option selected
  26.       if i == this then
  27.       --Option selected  
  28.          io.write(s1)
  29.       else
  30.       --Option NOT selected
  31.          io.write(s2)
  32.       end
  33.       --Print the string
  34.       print(array[i])
  35.       y = y + 1
  36.    end
  37. end
  38.  
  39. function library.CreateMenu(array, state1, state2, pos_x, pos_y)
  40.    local select = 1
  41.    local x, y = term.getCursor()
  42.    
  43.    while true do
  44.       term.setCursor(x, y)
  45.       library.mprint(array, select, state1, state2, pos_x, pos_y)
  46.       local name, address, char, code, player = event.pull("key_down")
  47.       --Enter
  48.       if code == 28 then
  49.          return select
  50.       end
  51.      
  52.       if #array > 1 then
  53.          --Arrow UP or W key
  54.          if code == 200 or code == 17 then
  55.                 select = select - 1
  56.            
  57.             if select < 1 then
  58.                select = #array
  59.             end
  60.          --Arrow Down or S key
  61.          elseif code == 208 or code == 31 then
  62.                 select = select + 1    
  63.                      if select > #array then select = 1 end
  64.           end
  65.       end  
  66.    end
  67. end
  68.  
  69. return library
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement