Advertisement
MadeaInaba

SimpleMenuAPI v1.0

Jan 17th, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.68 KB | None | 0 0
  1. --[[ SimpleMenuAPI -- A Simple Menu API
  2.  
  3. --SimpleMenuAPI Guide
  4.  
  5. First, create a table variable, and make your new menu
  6. MyMenu = {}
  7. MyMenu[1] = menu.create('Awesome Menu', True)
  8. The function call above sets the title of the new menu to Awesome Menu
  9. and the True value sets it to show the titlebar
  10.  
  11. Now you need to add some items to the menu!
  12. MyMenu[2] = menu.additem(MyMenu[1], 'New Game')
  13. That function call adds an item to the menu you created.
  14. MyMenu[1] tells the function the ID of the menu you want the item to add to
  15. 'New Game' sets the name of that item to New Game
  16.  
  17. Lets add one more item to the menu
  18. MyMenu[3] = menu.additem(MyMenu[1], 'Load Game')
  19. That function call does the same, but makes the name "Load Game" instead of "New Game"
  20.  
  21. So now you have;
  22. MyMenu[1] -- Contains your Menu's ID
  23. MyMenu[2] -- Contains Item 1's ID
  24. MyMenu[3] -- Contains Item 2's ID
  25.  
  26. So, what now? Lets clear the screen!
  27. term.clear()
  28. Now everything is blank, right? That's okay!
  29. We want it blank, so we can draw the menu.
  30.  
  31. Lets do that now;
  32. menu.draw(MyMenu[1], 5, 3, 0)
  33. This one takes some explaining.
  34. This draws your menu(MyMenu[1]) at position (5,3) in (x,y) on the screen
  35. And the items aren't spaced apart because the last argument is 0
  36.  
  37. Alright, that looks cool, right?
  38.  
  39. Lets set your menu to active.
  40. menu.startselect(MyMenu[1])
  41. This one starts the selection process of that menu.
  42. It sets the selection cursor to your first item, which we set as "New Game"
  43.  
  44. Good, but... Nothing happened, it all looks the same.
  45. That's because we need to draw the menu again to see the changes.
  46.  
  47. So, lets do this again;
  48. term.clear()
  49. menu.draw(MyMenu[1], 5, 3, 0)
  50.  
  51. And there you go! You should see this;
  52. Awesome Menu
  53. >New Game
  54. Load Game
  55.  
  56. That's awesome, as the name implies.
  57. Lets say that we already have a saved game. We need to load game, right?
  58. So, we need to change what we have selected.
  59.  
  60. Lets do that now;
  61. menu.down(MyMenu[1])
  62. This moves the cursor down the list by one item. So, we now have our cursor at the right part.
  63.  
  64. Lets draw the screen again;
  65. term.clear()
  66. menu.draw(MyMenu[1], 5, 3, 0)
  67.  
  68. You should see this;
  69. Awesome Menu
  70. New Game
  71. >Load Game
  72.  
  73. Now, lets say we changed our mind. Then we changed our mind again;
  74.  
  75. menu.up(MyMenu[1])
  76. term.clear()
  77. menu.draw(MyMenu[1], 5, 3, 0)
  78.  
  79. Oops, it's at New Game now.
  80.  
  81. menu.down(MyMenu[1])
  82. term.clear()
  83. menu.draw(MyMenu[1], 5, 3, 0)
  84.  
  85. Alright, we're back at Load Game. See, it's pretty easy to get what we want.
  86.  
  87. Now, before we make a selection, we need to make a function to figure out what we selected.
  88.  
  89. Lets do that;
  90. function menu.choice(item)
  91.     if item == "New Game" then
  92.         newgame()
  93.     elseif item == "Load Game" then
  94.         loadgame()
  95.     end
  96. end
  97. But... What does this do?
  98.  
  99. function menu.choice(item) <-- Makes a new function, that is called when you use menu.choose() later.
  100.  
  101.     if item == "New Game" then <-- This checks to see if the item you selected is "New Game"
  102.  
  103.         newgame() <-- This would be the New Game function for your game, this is outside the scope of this API
  104.        
  105.     elseif item == "Load Game" then <-- This checks to see if the item you selected is "Load Game"
  106.    
  107.         loadgame() <-- This would be the Load Game function for your game, this is outside the scope of this API
  108.        
  109.     end <-- This ends the "if" block
  110.    
  111. end <-- This ends the function
  112.  
  113. So, now you have a function that does something when you choose a menu item.
  114.  
  115. We're on the menu item that we want, so lets just go straight to selecting it;
  116. menu.choose()
  117. That function just selects the current item for the active menu.
  118.  
  119. Now, lets make the menu inactive again, because we don't need to select any more.
  120. menu.stopselect()
  121. That set menu.active to 0, and set the currently chosen menu item on your menu to the TitleBar, to hide the cursor
  122.  
  123. And there you have it. That's the scope of this menu system.
  124. You can go incredibly complex, or incredibly lightweight.
  125. It's all in how you use the API
  126.  
  127.  
  128.  
  129. See the following information for a rundown on everything we used, what it does, and what the arguments are
  130.  
  131. --VARIABLES
  132.  
  133. [menu.active]
  134. This variable contains the ID of the active menu
  135. If no menu is active, this variable is equal to
  136. nil( nil = 0 )
  137.  
  138. --FUNCTIONS
  139. [menu.create(Title, TopBar)] -- Returns ID:Integer
  140. Title:String -- The Title bar of the menu
  141. TopBar:Bool -- Defines whether the Title bar on this menu is visible or not
  142.  
  143. [menu.additem(ID, Name)] -- Returns ID2:Integer
  144. ID:Integer -- The ID of the menu to add the item to
  145. Name:String -- The name of the menu item to add
  146.  
  147. [menu.setitem(ID, ID2, Name)]
  148. ID:Integer -- The ID of the menu containing the item
  149. ID2:Integer -- The ID of the menu item to change the name of
  150. Name:String -- The Name you want to set the menu item to
  151.  
  152. [menu.startselect(ID)]
  153. ID:Integer -- The ID of the menu to start the menu selection process
  154.  
  155. [menu.stopselect(ID)]
  156. ID:Integer -- The ID of the menu to stop the menu selection process
  157.  
  158. [menu.up(ID)] -- Moves the selection up the list
  159. ID:Integer -- The ID of the menu to change the selected item
  160.  
  161. [menu.down(ID)] -- Moves the selection down the list
  162. ID:Integer -- The ID of the menu to change the selected item
  163.  
  164. [menu.choose()] -- Chooses the current selection for the active menu
  165.  
  166. [menu.choice(Name)] -- Player Defined
  167. Name:String -- Contains the name of the menu item the user chose
  168. ( To get the ID of the menu they chose from, use menu.active )
  169. This variable is run when the user uses menu.choose()
  170.  
  171. [menu.draw(ID, X, Y, Space)]
  172. ID:Integer -- The ID of the menu to draw
  173. X:Integer -- The X Position to draw the Menu at
  174. Y:Integer -- The Y Position to draw the Menu at
  175. Space:Integer -- The amount of spaces between menu items
  176. ( Recommended; 0 or 1 )
  177.  
  178. ]]--
  179.  
  180. menu = {}
  181. menu.list = {}
  182. menu.count = 0
  183. menu.select = {}
  184. menu.active = 0
  185.  
  186. function menu.create(t, b)
  187.     local id = menu.count+1
  188.     menu.list[id] = {}
  189.     menu.list[id][1] = t
  190.     menu.list[id].count = 1
  191.     menu.list[id].topbar = b
  192.     menu.count = id
  193.     menu.select[id] = 1
  194.     return id
  195. end
  196.  
  197. function menu.additem(id,t)
  198.     local item = menu.list[id].count + 1
  199.     menu.list[id][item] = t
  200.     menu.list[id].count = item
  201.     return item
  202. end
  203.  
  204. function menu.setitem(id, id2, t)
  205.     menu.list[id][id2] = t
  206. end
  207.  
  208. function menu.startselect(id)
  209.     if menu.list[id][2] then
  210.         menu.select[id] = 2
  211.         menu.active = id
  212.     end
  213. end
  214.  
  215. function menu.stopselect(id)
  216.     if menu.list[id][2] and menu.active == id then
  217.         menu.select[id] = 1
  218.         menu.active = 0
  219.     end
  220. end
  221.  
  222. function menu.up(id)
  223.     if menu.active == id then
  224.         if menu.select[id] > 2 then
  225.             menu.select[id] = menu.select[id] - 1
  226.         end
  227.     end
  228. end
  229.  
  230. function menu.down(id)
  231.     if menu.active == id then
  232.         if menu.select[id] < menu.list[id].count then
  233.             menu.select[id] = menu.select[id] + 1
  234.         end
  235.     end
  236. end
  237.  
  238. function menu.choose()
  239.     if menu.active ~= 0 then
  240.         if menu.select[menu.active] ~= 1 then
  241.             local temp1 = menu.select[menu.active]
  242.             local temp2 = menu.list[menu.active][temp1]
  243.             if menu.choice ~= nil then menu.choice(temp2) end
  244.         end
  245.     end
  246. end
  247.            
  248.  
  249. function menu.draw(id, x, y, s)
  250.     local h = 0
  251.     local r = 0
  252.     local div = 1
  253.     for i,v in ipairs(menu.list[id]) do
  254.         if menu.select[id] == i and i ~= 1 then
  255.             term.setCursorPos(x,y - div + h + h*s)
  256.             --term.clearLine()
  257.             term.write('>')
  258.             term.setCursorPos(x+1,y - div + h + h*s)
  259.             term.write(menu.list[id][i])
  260.         elseif i == 1 and menu.list[id].topbar == true then
  261.             div = 0
  262.             term.setCursorPos(x,y + h + h*s)
  263.             --term.clearLine()
  264.             term.write('--')
  265.             term.setCursorPos(x+2,y + h + h*s)
  266.             term.write(menu.list[id][i])
  267.         elseif i == 1 and menu.list[id].topbar == false then
  268.             div = 1
  269.         elseif i ~=1 then
  270.             term.setCursorPos(x ,y - div + h + h*s)
  271.             --term.clearLine()
  272.             term.write(menu.list[id][i])
  273.         end
  274.         h = h+1
  275.     end
  276. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement