Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ SimpleMenuAPI -- A Simple Menu API
- --SimpleMenuAPI Guide
- First, create a table variable, and make your new menu
- MyMenu = {}
- MyMenu[1] = menu.create('Awesome Menu', True)
- The function call above sets the title of the new menu to Awesome Menu
- and the True value sets it to show the titlebar
- Now you need to add some items to the menu!
- MyMenu[2] = menu.additem(MyMenu[1], 'New Game')
- That function call adds an item to the menu you created.
- MyMenu[1] tells the function the ID of the menu you want the item to add to
- 'New Game' sets the name of that item to New Game
- Lets add one more item to the menu
- MyMenu[3] = menu.additem(MyMenu[1], 'Load Game')
- That function call does the same, but makes the name "Load Game" instead of "New Game"
- So now you have;
- MyMenu[1] -- Contains your Menu's ID
- MyMenu[2] -- Contains Item 1's ID
- MyMenu[3] -- Contains Item 2's ID
- So, what now? Lets clear the screen!
- term.clear()
- Now everything is blank, right? That's okay!
- We want it blank, so we can draw the menu.
- Lets do that now;
- menu.draw(MyMenu[1], 5, 3, 0)
- This one takes some explaining.
- This draws your menu(MyMenu[1]) at position (5,3) in (x,y) on the screen
- And the items aren't spaced apart because the last argument is 0
- Alright, that looks cool, right?
- Lets set your menu to active.
- menu.startselect(MyMenu[1])
- This one starts the selection process of that menu.
- It sets the selection cursor to your first item, which we set as "New Game"
- Good, but... Nothing happened, it all looks the same.
- That's because we need to draw the menu again to see the changes.
- So, lets do this again;
- term.clear()
- menu.draw(MyMenu[1], 5, 3, 0)
- And there you go! You should see this;
- Awesome Menu
- >New Game
- Load Game
- That's awesome, as the name implies.
- Lets say that we already have a saved game. We need to load game, right?
- So, we need to change what we have selected.
- Lets do that now;
- menu.down(MyMenu[1])
- This moves the cursor down the list by one item. So, we now have our cursor at the right part.
- Lets draw the screen again;
- term.clear()
- menu.draw(MyMenu[1], 5, 3, 0)
- You should see this;
- Awesome Menu
- New Game
- >Load Game
- Now, lets say we changed our mind. Then we changed our mind again;
- menu.up(MyMenu[1])
- term.clear()
- menu.draw(MyMenu[1], 5, 3, 0)
- Oops, it's at New Game now.
- menu.down(MyMenu[1])
- term.clear()
- menu.draw(MyMenu[1], 5, 3, 0)
- Alright, we're back at Load Game. See, it's pretty easy to get what we want.
- Now, before we make a selection, we need to make a function to figure out what we selected.
- Lets do that;
- function menu.choice(item)
- if item == "New Game" then
- newgame()
- elseif item == "Load Game" then
- loadgame()
- end
- end
- But... What does this do?
- function menu.choice(item) <-- Makes a new function, that is called when you use menu.choose() later.
- if item == "New Game" then <-- This checks to see if the item you selected is "New Game"
- newgame() <-- This would be the New Game function for your game, this is outside the scope of this API
- elseif item == "Load Game" then <-- This checks to see if the item you selected is "Load Game"
- loadgame() <-- This would be the Load Game function for your game, this is outside the scope of this API
- end <-- This ends the "if" block
- end <-- This ends the function
- So, now you have a function that does something when you choose a menu item.
- We're on the menu item that we want, so lets just go straight to selecting it;
- menu.choose()
- That function just selects the current item for the active menu.
- Now, lets make the menu inactive again, because we don't need to select any more.
- menu.stopselect()
- That set menu.active to 0, and set the currently chosen menu item on your menu to the TitleBar, to hide the cursor
- And there you have it. That's the scope of this menu system.
- You can go incredibly complex, or incredibly lightweight.
- It's all in how you use the API
- See the following information for a rundown on everything we used, what it does, and what the arguments are
- --VARIABLES
- [menu.active]
- This variable contains the ID of the active menu
- If no menu is active, this variable is equal to
- nil( nil = 0 )
- --FUNCTIONS
- [menu.create(Title, TopBar)] -- Returns ID:Integer
- Title:String -- The Title bar of the menu
- TopBar:Bool -- Defines whether the Title bar on this menu is visible or not
- [menu.additem(ID, Name)] -- Returns ID2:Integer
- ID:Integer -- The ID of the menu to add the item to
- Name:String -- The name of the menu item to add
- [menu.setitem(ID, ID2, Name)]
- ID:Integer -- The ID of the menu containing the item
- ID2:Integer -- The ID of the menu item to change the name of
- Name:String -- The Name you want to set the menu item to
- [menu.startselect(ID)]
- ID:Integer -- The ID of the menu to start the menu selection process
- [menu.stopselect(ID)]
- ID:Integer -- The ID of the menu to stop the menu selection process
- [menu.up(ID)] -- Moves the selection up the list
- ID:Integer -- The ID of the menu to change the selected item
- [menu.down(ID)] -- Moves the selection down the list
- ID:Integer -- The ID of the menu to change the selected item
- [menu.choose()] -- Chooses the current selection for the active menu
- [menu.choice(Name)] -- Player Defined
- Name:String -- Contains the name of the menu item the user chose
- ( To get the ID of the menu they chose from, use menu.active )
- This variable is run when the user uses menu.choose()
- [menu.draw(ID, X, Y, Space)]
- ID:Integer -- The ID of the menu to draw
- X:Integer -- The X Position to draw the Menu at
- Y:Integer -- The Y Position to draw the Menu at
- Space:Integer -- The amount of spaces between menu items
- ( Recommended; 0 or 1 )
- ]]--
- menu = {}
- menu.list = {}
- menu.count = 0
- menu.select = {}
- menu.active = 0
- function menu.create(t, b)
- local id = menu.count+1
- menu.list[id] = {}
- menu.list[id][1] = t
- menu.list[id].count = 1
- menu.list[id].topbar = b
- menu.count = id
- menu.select[id] = 1
- return id
- end
- function menu.additem(id,t)
- local item = menu.list[id].count + 1
- menu.list[id][item] = t
- menu.list[id].count = item
- return item
- end
- function menu.setitem(id, id2, t)
- menu.list[id][id2] = t
- end
- function menu.startselect(id)
- if menu.list[id][2] then
- menu.select[id] = 2
- menu.active = id
- end
- end
- function menu.stopselect(id)
- if menu.list[id][2] and menu.active == id then
- menu.select[id] = 1
- menu.active = 0
- end
- end
- function menu.up(id)
- if menu.active == id then
- if menu.select[id] > 2 then
- menu.select[id] = menu.select[id] - 1
- end
- end
- end
- function menu.down(id)
- if menu.active == id then
- if menu.select[id] < menu.list[id].count then
- menu.select[id] = menu.select[id] + 1
- end
- end
- end
- function menu.choose()
- if menu.active ~= 0 then
- if menu.select[menu.active] ~= 1 then
- local temp1 = menu.select[menu.active]
- local temp2 = menu.list[menu.active][temp1]
- if menu.choice ~= nil then menu.choice(temp2) end
- end
- end
- end
- function menu.draw(id, x, y, s)
- local h = 0
- local r = 0
- local div = 1
- for i,v in ipairs(menu.list[id]) do
- if menu.select[id] == i and i ~= 1 then
- term.setCursorPos(x,y - div + h + h*s)
- --term.clearLine()
- term.write('>')
- term.setCursorPos(x+1,y - div + h + h*s)
- term.write(menu.list[id][i])
- elseif i == 1 and menu.list[id].topbar == true then
- div = 0
- term.setCursorPos(x,y + h + h*s)
- --term.clearLine()
- term.write('--')
- term.setCursorPos(x+2,y + h + h*s)
- term.write(menu.list[id][i])
- elseif i == 1 and menu.list[id].topbar == false then
- div = 1
- elseif i ~=1 then
- term.setCursorPos(x ,y - div + h + h*s)
- --term.clearLine()
- term.write(menu.list[id][i])
- end
- h = h+1
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement