masterdisasterHD

menuAPI w/ colors

Mar 30th, 2013
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function stopMenu()
  2.   repeating = false
  3.   running = false
  4.  term.clear()
  5.  term.setCursorPos(1,1)
  6. end
  7.  
  8. function clear()
  9.   term.clear()
  10.   term.setCursorPos(1,1)
  11. end
  12.  
  13. function runMenu( menu, repeating, selectedItem, tColor )
  14. running = true
  15. --[[ Printing Methods ]]--
  16.  
  17. local function printMenu( menu, D )
  18.   for i=1,#menu do
  19.     if i == selectedItem then
  20.       if term.isColour() then
  21.         term.setTextColour(D) write(">> ") term.setTextColour(colors.white) write(menu[i].text) term.setTextColour(D) write(" <<") term.setTextColour(colors.white) print("")
  22.     else
  23.       print(">> "..menu[i].text.." <<")
  24.     end
  25.   else
  26.     print("   "..menu[i].text.."   ")
  27.   end
  28. end
  29.  
  30. --[[ Handler Methods ]]--
  31.  
  32. local function onKeyPressed( key, menu )
  33.   if key == keys.enter then
  34.         onItemSelected(menu)
  35.   elseif key == keys.up then
  36.         if selectedItem > 1 then
  37.           selectedItem = selectedItem - 1
  38.         end
  39.   elseif key == keys.down then
  40.         if selectedItem < #menu then
  41.           selectedItem = selectedItem + 1
  42.         end
  43.      end    
  44.   end
  45. end
  46.  
  47. local function onItemSelected( menu )
  48.   runCommand(menu[selectedItem].handler)
  49. end
  50.  
  51. local function runcommand(args)
  52.  local tEnv = { ["exit"] = function() bRunning = false end, }
  53.  setmetatable( tEnv, { __index = getfenv() } )
  54.  local func, e = loadstring(args, "lua")
  55.  local func2, e2 = loadstring( "return "..s, "lua")
  56.  if not func then
  57.   func = func2
  58.   e = nil
  59.   nForcePrint = 1
  60.  else
  61.   if func2 then
  62.    func = func2
  63.   end
  64.  end
  65.  end
  66.  
  67.  if func then
  68.   setfenv(func, tEnv)
  69.   local tResults = { pcall( function() return func() end) }
  70.  end
  71. end
  72.  
  73. local function onClick( button, xPos, yPos, menu, D )
  74.  if button == 1 then
  75.    if yPos <= #menu then
  76.      selectedItem = yPos
  77.      runCommand(menu[selectedItem].handler)
  78.    else
  79.      runCommand(menu[#menu].handler)
  80.    end
  81.  elseif button == 2 then
  82.     if yPos > #menu then
  83.       yPos = #menu
  84.     end
  85.     selectedItem = yPos
  86.     clear()
  87.     printMenu(menu,D)
  88.  end
  89. end
  90.  
  91. local function loadMenu( menu, repeating, D )
  92.   while repeating and running do
  93.    clear()
  94.     printMenu(menu, D)
  95.     term.setCursorPos(50, 1)
  96.     if term.isColour() then
  97.     term.setTextColour(colors.yellow)
  98.     print(selectedItem)
  99.     term.setTextColour(colors.white)
  100.     else
  101.     print(selectedItem)
  102.     end
  103.     event, arg1, arg2, arg3 = os.pullEvent()
  104.     if event=="mouse_click" then
  105.       onClick( arg1, arg2, arg3, menu, D )
  106.     elseif event == "key" then
  107.       onKeyPressed( arg1, menu )
  108.     end
  109.   end
  110. end
  111.  
  112.  
  113. tColor = string.lower(tColor)
  114.  if tColor == "white" then
  115.    D = 1
  116.  elseif tColor == "orange" then
  117.    D = 2
  118.  elseif tColor == "magenta" then
  119.    D = 4
  120.  elseif tColor == "lightblue" then
  121.    D = 8
  122.  elseif tColor == "yellow" then
  123.    D = 16
  124.  elseif tColor == "lime" then
  125.    D = 32
  126.  elseif tColor == "pink" then
  127.    D = 64
  128.  elseif tColor == "gray" then
  129.    D = 128
  130.  elseif tColor == "lightgray" then
  131.    D = 256
  132.  elseif tColor == "cyan" then
  133.    D = 512
  134.  elseif tColor == "purple" then
  135.    D = 1024
  136.  elseif tColor == "blue" then
  137.    D = 2048
  138.  elseif tColor == "brown" then
  139.    D = 4096
  140.  elseif tColor == "green" then
  141.    D = 8192
  142.  elseif tColor == "red" then
  143.    D = 16384
  144.  elseif tColor == "black" then
  145.    D = 32768
  146.  elseif tColor ~= "white" or "orange" or "magenta" or "lightblue" or "yellow" or "lime" or "pink" or "gray" or "lightgray" or "cyan" or "purple" or "blue" or "brown" or "green" or "red" or "black" then
  147.   D = 1
  148.  end
  149.  
  150. loadMenu(menu, repeating, D)
  151.  
  152. end
  153.  
  154. function setSelectedItem( newSelItem )
  155.   selectedItem = newSelItem
  156.   clear()
  157. end
  158.  
  159. function subMenu(newMenu, oldMenu, D)
  160.   returnMenu = oldMenu
  161.   stopMenu()
  162.   runMenu(newMenu, true, 1, D)
  163. end
  164.  
  165. function returnToMenu()
  166.   stopMenu()
  167.   runMenu(returnMenu, true, 1, "lime")
  168. end
Add Comment
Please, Sign In to add comment