Advertisement
Metalhead33

Menusys (OpenComputers version)

Nov 7th, 2016
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.91 KB | None | 0 0
  1. -- This is for OpenComputers
  2. -- You can find the ComputerCraft version here: http://pastebin.com/hWNaWTz6
  3.  
  4. --[[
  5. Copyright (c) 2016, Zsolt Tóth
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in all
  15. copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. SOFTWARE.
  24.  
  25. ]]
  26.  
  27. local term = require("term")
  28. local component = require("component")
  29. local gpu = component.gpu
  30. local keyboard = require("keyboard")
  31. local event = require("event")
  32.  
  33. local white = 0xFFFFFF
  34. local black = 0x000000
  35.  
  36. local menusys = { }
  37.  
  38. -- ONE DIMENSIONAL MENUS
  39.  
  40. local function displayChoice(choices,state,intro,outro)
  41.     term.clear()
  42.     -- Write out the intro.
  43.     if intro ~= nil then
  44.         if type(intro) == "function" then
  45.             intro()
  46.         else
  47.             print(intro .. "\n")
  48.         end
  49.     else
  50.         print("\n\n")
  51.     end
  52.     -- Present the choices
  53.     for key,value in ipairs(choices) do
  54.         x0,y0 = gpu.getResolution()
  55.         x1,y1 = term.getCursor()
  56.         y1 = y1 + 1
  57.         local tvalue = ""
  58.         if type(value) == "function" then
  59.             tvalue = value()
  60.         else
  61.             tvalue = value
  62.         end
  63.         x1 = math.floor((x0 / 2) - ((string.len(tvalue) + 2) / 2))
  64.         term.setCursor(x1,y1)
  65.         if key == state then
  66.             gpu.setBackground(white)
  67.             gpu.setForeground(black)
  68.             term.write(" " .. tvalue .. " ")
  69.             gpu.setForeground(white)
  70.             gpu.setBackground(black)
  71.         else
  72.             term.write(" " .. tvalue .. " ")
  73.         end
  74.     end
  75.     -- Write out the outro.
  76.     if outro ~= nil then
  77.         if type(outro) == "function" then
  78.             outro()
  79.         else
  80.             print("\n\n" .. outro)
  81.         end
  82.     else
  83.         print("\n\n")
  84.     end
  85. end
  86.  
  87. function menusys.displayMenu(choices,intro,outro,init)
  88.     local status = 1
  89.     if init ~= nil then
  90.         status = init
  91.     end
  92.     local statmax = #choices
  93.     local update = false
  94.     displayChoice(choices,status,intro,outro)
  95.     while true do
  96.         local ev, address, char, code, userName = event.pull("key_down")
  97.         if code == keyboard.keys.space or code == keyboard.keys.enter then
  98.             return status
  99.         elseif code == keyboard.keys.back then
  100.             return 0
  101.         elseif code == keyboard.keys.up then
  102.             status = status - 1
  103.             update = true
  104.         elseif code ==  keyboard.keys.down then
  105.             status = status + 1
  106.             update = true
  107.         end
  108.         if update == true then
  109.             status = status % statmax
  110.             if status <= 0 then
  111.                 status = statmax
  112.             end
  113.             displayChoice(choices,status,intro,outro)
  114.             update = false
  115.         end
  116.     end
  117. end
  118.  
  119. -- TWO-DIMENSIONAL MENUS
  120.  
  121.  
  122. local function displayChoiceMult(choices,xstate,ystate,intro,outro,sizex)
  123.     term.clear()
  124.     x0,y0 = gpu.getResolution()
  125.     x0 = math.floor(x0 / sizex) -- We are going to divide by segments.
  126.     -- Write out the intro.
  127.     if intro ~= nil then
  128.         if type(intro) == "function" then
  129.             intro()
  130.         else
  131.             print(intro .. "\n")
  132.         end
  133.     else
  134.         print("\n\n")
  135.     end
  136.     -- Present the choices
  137.     for ykey,yvalue in ipairs(choices) do
  138.         x1,y1 = term.getCursor()
  139.         y1 = y1 + 1
  140.         for xkey,xvalue in ipairs(yvalue) do
  141.             local tvalue = ""
  142.             if type(xvalue) == "function" then
  143.                 tvalue = xvalue()
  144.             else
  145.                 tvalue = xvalue
  146.             end
  147.             x1 = math.floor( (x0 / 2) - ((string.len(tvalue) + 2) / 2) + (x0 * (xkey - 1) ) )
  148.             term.setCursor(x1,y1)
  149.             if ykey == ystate and xkey == xstate then
  150.                     gpu.setBackground(white)
  151.                     gpu.setForeground(black)
  152.                     term.write(" " .. tvalue .. " ")
  153.                     gpu.setBackground(black)
  154.                     gpu.setForeground(white)
  155.             else
  156.                     term.write(" " .. tvalue .. " ")
  157.             end
  158.         end
  159.     end
  160.     -- Write out the outro.
  161.     if outro ~= nil then
  162.         if type(outro) == "function" then
  163.             outro()
  164.         else
  165.             print("\n\n" .. outro)
  166.         end
  167.     else
  168.         print("\n\n")
  169.     end
  170. end
  171.  
  172. function menusys.displayMenuMult(choices,intro,outro,initx,inity)
  173.     local x_status = 1
  174.     local y_status = 1
  175.     if initx ~= nil then
  176.         x_status = initx
  177.     end
  178.     if inity ~= nil then
  179.         y_status = inity
  180.     end
  181.     local y_statmax = #choices
  182.     local x_size = 0
  183.      for ykey,yvalue in ipairs(choices) do
  184.          local temp = #yvalue
  185.          if temp > x_size then
  186.              x_size = temp
  187.          end
  188.      end
  189.     local update = false
  190.     displayChoiceMult(choices,x_status,y_status,intro,outro,x_size)
  191.     while true do
  192.         local ev, address, char, code, userName = event.pull("key_down")
  193.         if code == keyboard.keys.space or code == keyboard.keys.enter then
  194.             return x_status,y_status
  195.         elseif code == keyboard.keys.back then
  196.             return 0,0
  197.         elseif code == keyboard.keys.up then
  198.             y_status = y_status - 1
  199.             update = true
  200.         elseif code == keyboard.keys.left then
  201.             x_status = x_status - 1
  202.             update = true
  203.         elseif code == keyboard.keys.down then
  204.             y_status = y_status + 1
  205.             update = true
  206.         elseif code == keyboard.keys.right then
  207.             x_status = x_status + 1
  208.             update = true
  209.         end
  210.         if update == true then
  211.             y_status = y_status % y_statmax
  212.             if y_status <= 0 then
  213.                 y_status = y_statmax
  214.             end
  215.             local x_statmax = #choices[y_status]
  216.             x_status = x_status % x_statmax
  217.             if x_status <= 0 then
  218.                 x_status = x_statmax
  219.             end
  220.             displayChoiceMult(choices,x_status,y_status,intro,outro,x_size)
  221.             update = false
  222.         end
  223.     end
  224. end
  225.  
  226. return menusys
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement