Advertisement
PeachGaming

Untitled

Jan 22nd, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. local menu_options = { --This is our menu table. It contains basic data about the menu
  2. [1] = {text="Open CraftOS", color=colors.blue},
  3. [2] = {text="Strafe - Games", color=colors.orange},
  4.  
  5. }
  6. term.clear()
  7. term.setBackgroundColor(colors.white)
  8. local termX, termY = term.getSize() --The x/y size of the terminal
  9. local function menuDraw(selected) --Our main draw function
  10. local yPos = termY/2 - #menu_options/2 --The initial y position
  11. for index, data in pairs(menu_options) do
  12. menu_options[index].bounds = { --Create a new table in each option with the boundary data
  13. x1 = termX/2 - (#data.text+4)/2;
  14. x2 = termX/2 + (#data.text+4)/2;
  15. y = yPos;
  16. }
  17.  
  18.  
  19. term.setTextColor(data.color)
  20. term.setCursorPos(data.bounds.x1, data.bounds.y)
  21.  
  22. local text =
  23. index==selected and "[ "..data.text.." ]" or
  24. " "..data.text.." " --Essentially an if statement, but in a contracted form
  25. term.write(text)
  26. yPos = yPos+1 --Increment the initial y pos so we can move on the next line
  27. end
  28. end
  29.  
  30. local function checkClick(x,y) --Check the mouse click to see if there's a menu option
  31. for index, data in pairs(menu_options) do
  32. if x>= data.bounds.x1 and x<= data.bounds.x2 and y==data.bounds.y then
  33. return index --Returns the index of the clicked option
  34. end
  35. end
  36. return false --If it went through the entire for loop without success, return false
  37. end
  38.  
  39.  
  40. term.setTextColor(colors.green) --Ok, so lets change "Strafe for gaming" to just "Strafe - Games"Ok.
  41. term.clear()
  42.  
  43. local selector = 1 --Our selector
  44. while true do --The main loop. I would generally put this inside of a function for a program.
  45. menuDraw(selector) --Draw the menu first
  46. local e = {os.pullEvent()} --Pull an event and put the returned values into a table
  47. if e[1] == "key" then --If it's a key...
  48. if e[2] == keys.down then -- ... and it's the down arrow
  49. selector = selector < #menu_options and selector+1 or 1 --Increment the selector if the selector < #menu_options. Otherwise reset it to 1
  50. elseif e[2] == keys.up then
  51. selector = selector > 1 and selector-1 or #menu_options --Decrement the selector if the selector > 1. Otherwise, reset it to #menu_options
  52. elseif e[2] == keys.enter then
  53. break --Break out of the loop
  54. end
  55. elseif e[1] == "mouse_click" then
  56. local value = checkClick(e[3], e[4]) --Check the mouse click
  57. if value then --If checkClick returns a value and not false
  58. selector = value --Set the selector to that value and break out of the loop
  59. break --um what was this
  60. end
  61. end
  62. end
  63.  
  64. term.clear()
  65. term.setCursorPos(1,1)
  66.  
  67. if selector == 2 then
  68. term.write("Strafe by Crazed Programmer...")
  69. shell.run("disk/strafe") --Oh cool!
  70. end
  71. --ok great i'm retarded, thought integers didn't have quotes but ok..
  72. -- AAAAAAAAAAAAAAA Lol.
  73. if selector == 1 then
  74. term.write("Opening default shell..")
  75. term.setTextColor(colors.white)
  76. term.setBackgroundColor(colors.black)
  77. term.clear()
  78. shell.run("shell")
  79. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement