Advertisement
Metalhead33

Menusys (ComputerCraft)

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