cyanisaac

cyanisaac's basic menu API 1.1

Jan 16th, 2016
542
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. The MIT License (MIT)
  3. Copyright (c) 2016 Isaac Trimble-Pederson "cyanisaac", "ProjectB"
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  6.  
  7. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  8.  
  9. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  10. ]]
  11.  
  12. -- Dependencies
  13. function center(text, height, color)
  14.     local x,y=term.getSize()
  15.     if height ~= nil then
  16.         term.setCursorPos((x-string.len(text))/2, height)
  17.     else
  18.         term.setCursorPos((x-string.len(text))/2, y/2)
  19.     end
  20.     if color ~= nil then
  21.         term.setTextColor(color)
  22.     end
  23.     term.write(text)
  24. end
  25.  
  26. -- Main code
  27. local function drawTop(text)
  28.     local oldTC = term.getTextColor()
  29.     local oldBC = term.getBackgroundColor()
  30.  
  31.     term.setCursorPos(1,1)
  32.     term.setBackgroundColor(colors.white)
  33.     term.setTextColor(colors.black)
  34.     term.clearLine()
  35.     center(text, 1, colors.black)
  36.  
  37.     term.setTextColor(oldTC)
  38.     term.setBackgroundColor(oldBC)
  39. end
  40.  
  41. local function drawBottom(text)
  42.     local w,h = term.getSize()
  43.     local oldTC = term.getTextColor()
  44.     local oldBC = term.getBackgroundColor()
  45.  
  46.     term.setCursorPos(1,h)
  47.     term.setBackgroundColor(colors.white)
  48.     term.setTextColor(colors.black)
  49.     term.clearLine()
  50.     center(text, h, colors.black)
  51.  
  52.     term.setTextColor(oldTC)
  53.     term.setBackgroundColor(oldBC)
  54. end
  55.  
  56. local function renderOptions(options, selectedOption)
  57.     local w,h = term.getSize()
  58.     if #options > h-2 then
  59.         error("Too many options to display. Page support will come in the future.")
  60.     end
  61.  
  62.     for i=1, #options do
  63.         local oBG = term.getBackgroundColor()
  64.         local oTC = term.getTextColor()
  65.  
  66.         term.setCursorPos(1,i+2)
  67.         if selectedOption == i then
  68.             term.setBackgroundColor(colors.gray)
  69.         else
  70.             term.setBackgroundColor(colors.black)
  71.         end
  72.         term.clearLine()
  73.         center(options[i], i+2, colors.white)
  74.  
  75.         term.setBackgroundColor(oBG)
  76.         term.setTextColor(oTC)
  77.     end
  78. end
  79.  
  80. function doMenu(topText, options)
  81.     local w,h = term.getSize()
  82.     local selectedOption = 1
  83.     local function render(sOption)
  84.         term.clear()
  85.         term.setCursorPos(1,1)
  86.         drawTop(topText)
  87.         renderOptions(options, sOption)
  88.     end
  89.     local function changeSelectedOption(a)
  90.         if selectedOption+a > 0 and selectedOption+a < #options+1 then
  91.             selectedOption = selectedOption+a
  92.         end
  93.         render(selectedOption)
  94.     end
  95.  
  96.     local oldBG = term.getBackgroundColor()
  97.     local oldTX = term.getTextColor()
  98.  
  99.     term.setBackgroundColor(colors.black)
  100.     term.setTextColor(colors.white)
  101.  
  102.     render(selectedOption)
  103.  
  104.     while true do
  105.         local event, param1, param2, param3 = os.pullEvent()
  106.         if event == "key" then
  107.             if param1 == keys.enter then
  108.                 break
  109.             elseif param1 == keys.up then
  110.                 changeSelectedOption(-1)
  111.             elseif param1 == keys.down then
  112.                 changeSelectedOption(1)
  113.             end
  114.         end
  115.     end
  116.     term.setBackgroundColor(oldBG)
  117.     term.setTextColor(oldTX)
  118.     term.clear()
  119.     term.setCursorPos(1,1)
  120.     return selectedOption
  121. end
  122.  
  123. function doInfoScreen(title, info)
  124.     local w,h = term.getSize()
  125.     local oldBG = term.getBackgroundColor()
  126.     local oldTX = term.getTextColor()
  127.     term.setBackgroundColor(colors.black)
  128.     term.setTextColor(colors.white)
  129.     term.clear()
  130.     term.setCursorPos(1,2)
  131.     print(info)
  132.     term.setCursorPos(1,1)
  133.     drawTop(title)
  134.     drawBottom("Press ENTER to return...")
  135.     while true do
  136.         local event, param1 = os.pullEvent()
  137.         if event == "key" and param1 == keys.enter then
  138.             break
  139.         end
  140.     end
  141.     term.setBackgroundColor(oldBG)
  142.     term.setTextColor(oldTX)
  143.     term.clear()
  144.     term.setCursorPos(1,1)
  145.     return selectedOption
  146. end
RAW Paste Data