Jump to content




A quick guide through menu making


  • This topic is locked This topic is locked
42 replies to this topic

#41 Chrisvn

  • New Members
  • 2 posts

Posted 06 June 2013 - 12:53 PM

View PostLiraal, on 18 March 2012 - 12:22 PM, said:

Okay, so you have come here to look for a quick way to set up a menu? Good. Before we start, please note that I will first show an algorithm, then the code and finally an explanation. Now we can begin.

1. Yes/No Menu

First and easiest type of menu is a yes/no menu. We need it to act like:
1. Print "Yes No" with one option highlighted, like ">Yes< No"
2. Wait for key pressed.
3. If pressed enter return yes (true) or no (false) and end program
4. If pressed arrow left/right highlight the correct option and go to Step 1.


okay, here is the code:
Spoiler

what it does is:

Spoiler

2. Customizable Menu


Okay, it's now time for the more advanced stuff. Say, you don't want all your menus to only have yes/no options, right? Now I will show you how to make a better version.
1. Get the list of options, preferably a table.
2. Print those options.
3. Select the first one.
4. If pressed arrow up go one option up and go to step#2.
5. If pressed arrow down do the reverse and go to step#2.
6. If pressed enter end and return selected option.


Code:
function CUI(m)
n=1
l=#m
while true do
term.clear()
term.setCursorPos(1,2)
for i=1, l, 1 do
if i==n then print(i, " ["..m[i].."]") else print(i, " ", m[i]) end
end
print("Select a number[arrow up/arrow down]")
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<=l then n=n+1 end
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
And explanation:
function CUI(m) --declare function
n=1 --declare selected option
while true do --start a loop for the 'go to step#2' part
term.clear() term.setCursorPos(1,2) --clear the sceen and position the cursor
for i=1, #m, 1 do --traverse the table of options
if i==n then print(i, " ["..m[i].."]") else print(i, " ", m[i]) end --print them
end
a, b= os.pullEvent("key") --wait for keypress
if b==200 and n>1 then n=n-1 end --arrow up pressed, one option up
if b==208 and n<=l then n=n+1 end --arrow down pressed, one option down
if b==28 then break end --enter pressed, break the loop
end
term.clear() term.setCursorPos(1,1) --clear screen
return n --return the value
end

Please note that the above function requires to be called with a table, like this:
local options={
"option1",
"option2",
"option3"
}
local n=CUI(options)
print(n)

and returns the number of the option chosen.

Do you want me to make some other types of menus as well? If so, post them below.
I'm able to print 3 options but the last one can't be selected, any idea on why?

#42 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 06 June 2013 - 01:19 PM

when asking for help with a problem in your code always provide your code

#43 Chrisvn

  • New Members
  • 2 posts

Posted 07 June 2013 - 03:01 PM

View PostKaoS, on 06 June 2013 - 01:19 PM, said:

when asking for help with a problem in your code always provide your code
I sent that in a rush and pressed complete because I had to drive my girlfriend home, sorry.
Anyway I managed to fix that problem I used this

Quote

if b==208 and n<=1 then n=n+1 end

instead of this

Quote

if b==208 and n<=l then n=n+1 end

If you can't see it well I used 1 (number one) instead of l (L), quite confusing for peope like me with dyslexia.
Then I stumbled on another problem which is that I tried to pull a menu from a menu option however after several attempts I decided to try and use the first code but with more options than yes and no, this is what I came up with (it works fyi).
Please note everything is declared in Dutch so I putted English commentary.

Quote

selectie = 1 --declare that selection is 1 at the start

function hoofdMenu() --declare the options
if selectie == 1 then print("> Meer opties <") else print(" Meer opties ") end --if the selection is 1 then meer opties will be highlighted else it won't
if selectie == 2 then print("> Print test <") else print(" Print test") end
if selectie == 3 then print("> Afsluiten <") else print(" Afsluiten")end
end

function tekenhoofdMenu() --drawing the menu and using the arrow keys to change our variable
term.clear()
term.setCursorPos(1,1)
while true do
hoofdMenu()

local id, key = os.pullEvent("key")
if key == 200 and selectie > 1 then --You should recognise it from the original code
selectie = selectie - 1
elseif key == 208 and selectie < 3 then --However here I predefined the final possible value rather than giving it as a variabel
selectie = selectie + 1
end

if key == 28 then --here is where the real magic happens
if selectie == 3 then --inputted shutdown so I can shutdown the OS and remove the floppy where I saved this as startup
os.shutdown()
elseif select == 1 then tekenmeerMenu() --this will draw another menu when meer opties was highlighted and enter pressed
elseif select == 2 then print("Test") --prints test
end
break
end
end
end

function meerMenu() --declaring of the extra options
if selectie == 1 then print("> Optie 1 <") else print(" Optie 1") end
if selectie == 2 then print("> Optie 2 <") else print(" Optie 2") end
end

function tekenmeerMenu() --drawing the second menu
term.clear()
term.setCursorPos(1,1)
while true do
meerMenu()

local id, key = os.pullEvent("key")
if key == 200 and selectie > 1 then
selectie = selectie - 1
elseif key == 208 and selectie < 2 then
selectie = selectie + 1
end

if key == 28 then
if select == 1 then print(" Optie 1 is gekozen")
elseif select == 2 then print("Optie 2 is gekozen")
end
break
end
end
end


Note that this is saved on a server I have no access to so I typed it over(I always type a double in Notepad to find errors more easily), not sure if it is 100% correct but it should work, for anyone that sees it please feel free to use it as I based myself on the original code of this thread.
Might try to test it out with

Quote

function drawMenu(Menu)
Where menu is your function (like hoofdmenu()) so you only need 1 draw function, but I doubt it works as I am not sure if you can pas down other functions into a function.

Hope this helps anyone.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users