Guest User

Colored book selector

a guest
Mar 25th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.52 KB | None | 0 0
  1. local list = { --This contains the numbers and colors. You can extend this to as long as you'd like - it's not hardcoded anywhere else
  2.     [1] = colors.blue,
  3.     [2] = colors.yellow,
  4.     [3] = colors.white,
  5.     [4] = colors.green,
  6.     [5] = colors.red
  7. }
  8.  
  9. local function drawList(selected, offX, offY) --This function will draw the list with the selected item at the given offsets
  10.     for i=1,#list do
  11.         term.setCursorPos(offX, offY)
  12.         term.setTextColor(list[i])
  13.         if i==selected then
  14.             term.write("["..i.."]")
  15.         else
  16.             term.write(" "..i.." ")
  17.         end
  18.         offY=offY+1
  19.     end
  20. end
  21.  
  22. local function run() --This just captures key presses and adds or subtracts from the selected variable depending on the key event. If the user presses enter, it returns true
  23.     while true do
  24.         local startX, startY = 3,3
  25.         local selected = 1
  26.         while true do
  27.             drawList(selected, startX, startY)
  28.             local e = {os.pullEvent()}
  29.             if e[1] == "key" then
  30.                 if e[2] == keys.up then
  31.                     selected = selected>1 and selected - 1 or selected
  32.                 elseif e[2] == keys.down then
  33.                     selected = selected<#list and selected+1 or selected
  34.                 elseif e[2] == keys.enter then
  35.                     return selected
  36.                 end
  37.             end
  38.         end
  39.     end
  40. end
  41.  
  42. while true do --The main loop.
  43.     local result = run()
  44.     --Now you've got the result in number format. I'm not sure what you were trying to do with the book function, but I can imagine that you'll be able
  45.     --to use this now. If you need the actual color value instead of the number, you can just use list[result] and it will give you the color.
  46. end
Advertisement
Add Comment
Please, Sign In to add comment